elf32-m32r.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714
  1. /* M32R-specific support for 32-bit ELF.
  2. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/m32r.h"
  21. #define NOP_INSN 0x7000
  22. #define MAKE_PARALLEL(insn) ((insn) | 0x8000)
  23. /* Use REL instead of RELA to save space.
  24. This only saves space in libraries and object files, but perhaps
  25. relocs will be put in ROM? All in all though, REL relocs are a pain
  26. to work with. */
  27. /* #define USE_REL 1
  28. #ifndef USE_REL
  29. #define USE_REL 0
  30. #endif */
  31. /* Use RELA. But use REL to link old objects for backwords compatibility. */
  32. /* Functions for the M32R ELF linker. */
  33. /* The name of the dynamic interpreter. This is put in the .interp
  34. section. */
  35. #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  36. /* The nop opcode we use. */
  37. #define M32R_NOP 0x7000f000
  38. #define PLT_EMPTY 0x10101010 /* RIE -> RIE */
  39. /* The size in bytes of an entry in the procedure linkage table. */
  40. #define PLT_ENTRY_SIZE 20
  41. #define PLT_HEADER_SIZE 20
  42. /* The first one entries in a procedure linkage table are reserved,
  43. and the initial contents are unimportant (we zero them out).
  44. Subsequent entries look like this. */
  45. #define PLT0_ENTRY_WORD0 0xd6c00000 /* seth r6, #high(.got+4) */
  46. #define PLT0_ENTRY_WORD1 0x86e60000 /* or3 r6, r6, #low(.got)+4) */
  47. #define PLT0_ENTRY_WORD2 0x24e626c6 /* ld r4, @r6+ -> ld r6, @r6 */
  48. #define PLT0_ENTRY_WORD3 0x1fc6f000 /* jmp r6 || pnop */
  49. #define PLT0_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
  50. #define PLT0_PIC_ENTRY_WORD0 0xa4cc0004 /* ld r4, @(4,r12) */
  51. #define PLT0_PIC_ENTRY_WORD1 0xa6cc0008 /* ld r6, @(8,r12) */
  52. #define PLT0_PIC_ENTRY_WORD2 0x1fc6f000 /* jmp r6 || nop */
  53. #define PLT0_PIC_ENTRY_WORD3 PLT_EMPTY /* RIE -> RIE */
  54. #define PLT0_PIC_ENTRY_WORD4 PLT_EMPTY /* RIE -> RIE */
  55. #define PLT_ENTRY_WORD0 0xe6000000 /* ld24 r6, .name_in_GOT */
  56. #define PLT_ENTRY_WORD1 0x06acf000 /* add r6, r12 || nop */
  57. #define PLT_ENTRY_WORD0b 0xd6c00000 /* seth r6, #high(.name_in_GOT) */
  58. #define PLT_ENTRY_WORD1b 0x86e60000 /* or3 r6, r6, #low(.name_in_GOT) */
  59. #define PLT_ENTRY_WORD2 0x26c61fc6 /* ld r6, @r6 -> jmp r6 */
  60. #define PLT_ENTRY_WORD3 0xe5000000 /* ld24 r5, $offset */
  61. #define PLT_ENTRY_WORD4 0xff000000 /* bra .plt0. */
  62. /* Utility to actually perform an R_M32R_10_PCREL reloc. */
  63. static bfd_reloc_status_type
  64. m32r_elf_do_10_pcrel_reloc (bfd *abfd,
  65. reloc_howto_type *howto,
  66. asection *input_section,
  67. bfd_byte *data,
  68. bfd_vma offset,
  69. asection *symbol_section ATTRIBUTE_UNUSED,
  70. bfd_vma symbol_value,
  71. bfd_vma addend)
  72. {
  73. bfd_signed_vma relocation;
  74. unsigned long x;
  75. bfd_reloc_status_type status;
  76. /* Sanity check the address (offset in section). */
  77. if (offset > bfd_get_section_limit (abfd, input_section))
  78. return bfd_reloc_outofrange;
  79. relocation = symbol_value + addend;
  80. /* Make it pc relative. */
  81. relocation -= (input_section->output_section->vma
  82. + input_section->output_offset);
  83. /* These jumps mask off the lower two bits of the current address
  84. before doing pcrel calculations. */
  85. relocation -= (offset & -(bfd_vma) 4);
  86. if (relocation < -0x200 || relocation > 0x1ff)
  87. status = bfd_reloc_overflow;
  88. else
  89. status = bfd_reloc_ok;
  90. x = bfd_get_16 (abfd, data + offset);
  91. relocation >>= howto->rightshift;
  92. relocation <<= howto->bitpos;
  93. x = (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask);
  94. bfd_put_16 (abfd, (bfd_vma) x, data + offset);
  95. return status;
  96. }
  97. /* Handle the R_M32R_10_PCREL reloc. */
  98. static bfd_reloc_status_type
  99. m32r_elf_10_pcrel_reloc (bfd * abfd,
  100. arelent * reloc_entry,
  101. asymbol * symbol,
  102. void * data,
  103. asection * input_section,
  104. bfd * output_bfd,
  105. char ** error_message ATTRIBUTE_UNUSED)
  106. {
  107. /* This part is from bfd_elf_generic_reloc. */
  108. if (output_bfd != NULL
  109. && (symbol->flags & BSF_SECTION_SYM) == 0
  110. && (! reloc_entry->howto->partial_inplace
  111. || reloc_entry->addend == 0))
  112. {
  113. reloc_entry->address += input_section->output_offset;
  114. return bfd_reloc_ok;
  115. }
  116. if (output_bfd != NULL)
  117. /* FIXME: See bfd_perform_relocation. Is this right? */
  118. return bfd_reloc_continue;
  119. return m32r_elf_do_10_pcrel_reloc (abfd, reloc_entry->howto,
  120. input_section,
  121. data, reloc_entry->address,
  122. symbol->section,
  123. (symbol->value
  124. + symbol->section->output_section->vma
  125. + symbol->section->output_offset),
  126. reloc_entry->addend);
  127. }
  128. /* Do generic partial_inplace relocation.
  129. This is a local replacement for bfd_elf_generic_reloc. */
  130. static bfd_reloc_status_type
  131. m32r_elf_generic_reloc (bfd *input_bfd,
  132. arelent *reloc_entry,
  133. asymbol *symbol,
  134. void * data,
  135. asection *input_section,
  136. bfd *output_bfd,
  137. char **error_message ATTRIBUTE_UNUSED)
  138. {
  139. bfd_reloc_status_type ret;
  140. bfd_vma relocation;
  141. bfd_byte *inplace_address;
  142. /* This part is from bfd_elf_generic_reloc.
  143. If we're relocating, and this an external symbol, we don't want
  144. to change anything. */
  145. if (output_bfd != NULL
  146. && (symbol->flags & BSF_SECTION_SYM) == 0
  147. && reloc_entry->addend == 0)
  148. {
  149. reloc_entry->address += input_section->output_offset;
  150. return bfd_reloc_ok;
  151. }
  152. /* Now do the reloc in the usual way.
  153. ??? It would be nice to call bfd_elf_generic_reloc here,
  154. but we have partial_inplace set. bfd_elf_generic_reloc will
  155. pass the handling back to bfd_install_relocation which will install
  156. a section relative addend which is wrong. */
  157. /* Sanity check the address (offset in section). */
  158. if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
  159. return bfd_reloc_outofrange;
  160. ret = bfd_reloc_ok;
  161. if (bfd_is_und_section (symbol->section)
  162. && output_bfd == NULL)
  163. ret = bfd_reloc_undefined;
  164. if (bfd_is_com_section (symbol->section)
  165. || output_bfd != NULL)
  166. relocation = 0;
  167. else
  168. relocation = symbol->value;
  169. /* Only do this for a final link. */
  170. if (output_bfd == NULL)
  171. {
  172. relocation += symbol->section->output_section->vma;
  173. relocation += symbol->section->output_offset;
  174. }
  175. relocation += reloc_entry->addend;
  176. inplace_address = (bfd_byte *) data + reloc_entry->address;
  177. #define DOIT(x) \
  178. x = ( (x & ~reloc_entry->howto->dst_mask) | \
  179. (((x & reloc_entry->howto->src_mask) + relocation) & \
  180. reloc_entry->howto->dst_mask))
  181. switch (reloc_entry->howto->size)
  182. {
  183. case 1:
  184. {
  185. short x = bfd_get_16 (input_bfd, inplace_address);
  186. DOIT (x);
  187. bfd_put_16 (input_bfd, (bfd_vma) x, inplace_address);
  188. }
  189. break;
  190. case 2:
  191. {
  192. unsigned long x = bfd_get_32 (input_bfd, inplace_address);
  193. DOIT (x);
  194. bfd_put_32 (input_bfd, (bfd_vma)x , inplace_address);
  195. }
  196. break;
  197. default:
  198. BFD_ASSERT (0);
  199. }
  200. if (output_bfd != NULL)
  201. reloc_entry->address += input_section->output_offset;
  202. return ret;
  203. }
  204. /* Handle the R_M32R_SDA16 reloc.
  205. This reloc is used to compute the address of objects in the small data area
  206. and to perform loads and stores from that area.
  207. The lower 16 bits are sign extended and added to the register specified
  208. in the instruction, which is assumed to point to _SDA_BASE_. */
  209. static bfd_reloc_status_type
  210. m32r_elf_sda16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  211. arelent *reloc_entry,
  212. asymbol *symbol,
  213. void * data ATTRIBUTE_UNUSED,
  214. asection *input_section,
  215. bfd *output_bfd,
  216. char **error_message ATTRIBUTE_UNUSED)
  217. {
  218. /* This part is from bfd_elf_generic_reloc. */
  219. if (output_bfd != NULL
  220. && (symbol->flags & BSF_SECTION_SYM) == 0
  221. && (! reloc_entry->howto->partial_inplace
  222. || reloc_entry->addend == 0))
  223. {
  224. reloc_entry->address += input_section->output_offset;
  225. return bfd_reloc_ok;
  226. }
  227. if (output_bfd != NULL)
  228. /* FIXME: See bfd_perform_relocation. Is this right? */
  229. return bfd_reloc_continue;
  230. /* FIXME: not sure what to do here yet. But then again, the linker
  231. may never call us. */
  232. abort ();
  233. }
  234. /* Handle the R_M32R_HI16_[SU]LO relocs.
  235. HI16_SLO is for the add3 and load/store with displacement instructions.
  236. HI16_ULO is for the or3 instruction.
  237. For R_M32R_HI16_SLO, the lower 16 bits are sign extended when added to
  238. the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
  239. we must add one to the high 16 bytes (which will get subtracted off when
  240. the low 16 bits are added).
  241. These relocs have to be done in combination with an R_M32R_LO16 reloc
  242. because there is a carry from the LO16 to the HI16. Here we just save
  243. the information we need; we do the actual relocation when we see the LO16.
  244. This code is copied from the elf32-mips.c. We also support an arbitrary
  245. number of HI16 relocs to be associated with a single LO16 reloc. The
  246. assembler sorts the relocs to ensure each HI16 immediately precedes its
  247. LO16. However if there are multiple copies, the assembler may not find
  248. the real LO16 so it picks the first one it finds. */
  249. struct m32r_hi16
  250. {
  251. struct m32r_hi16 *next;
  252. bfd_byte *addr;
  253. bfd_vma addend;
  254. };
  255. /* FIXME: This should not be a static variable. */
  256. static struct m32r_hi16 *m32r_hi16_list;
  257. static bfd_reloc_status_type
  258. m32r_elf_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  259. arelent *reloc_entry,
  260. asymbol *symbol,
  261. void * data,
  262. asection *input_section,
  263. bfd *output_bfd,
  264. char **error_message ATTRIBUTE_UNUSED)
  265. {
  266. bfd_reloc_status_type ret;
  267. bfd_vma relocation;
  268. struct m32r_hi16 *n;
  269. /* This part is from bfd_elf_generic_reloc.
  270. If we're relocating, and this an external symbol, we don't want
  271. to change anything. */
  272. if (output_bfd != NULL
  273. && (symbol->flags & BSF_SECTION_SYM) == 0
  274. && reloc_entry->addend == 0)
  275. {
  276. reloc_entry->address += input_section->output_offset;
  277. return bfd_reloc_ok;
  278. }
  279. /* Sanity check the address (offset in section). */
  280. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  281. return bfd_reloc_outofrange;
  282. ret = bfd_reloc_ok;
  283. if (bfd_is_und_section (symbol->section)
  284. && output_bfd == NULL)
  285. ret = bfd_reloc_undefined;
  286. if (bfd_is_com_section (symbol->section))
  287. relocation = 0;
  288. else
  289. relocation = symbol->value;
  290. relocation += symbol->section->output_section->vma;
  291. relocation += symbol->section->output_offset;
  292. relocation += reloc_entry->addend;
  293. /* Save the information, and let LO16 do the actual relocation. */
  294. n = bfd_malloc ((bfd_size_type) sizeof *n);
  295. if (n == NULL)
  296. return bfd_reloc_outofrange;
  297. n->addr = (bfd_byte *) data + reloc_entry->address;
  298. n->addend = relocation;
  299. n->next = m32r_hi16_list;
  300. m32r_hi16_list = n;
  301. if (output_bfd != NULL)
  302. reloc_entry->address += input_section->output_offset;
  303. return ret;
  304. }
  305. /* Handle an M32R ELF HI16 reloc. */
  306. static void
  307. m32r_elf_relocate_hi16 (bfd *input_bfd,
  308. int type,
  309. Elf_Internal_Rela *relhi,
  310. Elf_Internal_Rela *rello,
  311. bfd_byte *contents,
  312. bfd_vma addend)
  313. {
  314. unsigned long insn;
  315. bfd_vma addlo;
  316. insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  317. addlo = bfd_get_32 (input_bfd, contents + rello->r_offset);
  318. if (type == R_M32R_HI16_SLO)
  319. addlo = ((addlo & 0xffff) ^ 0x8000) - 0x8000;
  320. else
  321. addlo &= 0xffff;
  322. addend += ((insn & 0xffff) << 16) + addlo;
  323. /* Reaccount for sign extension of low part. */
  324. if (type == R_M32R_HI16_SLO
  325. && (addend & 0x8000) != 0)
  326. addend += 0x10000;
  327. bfd_put_32 (input_bfd,
  328. (insn & 0xffff0000) | ((addend >> 16) & 0xffff),
  329. contents + relhi->r_offset);
  330. }
  331. /* Do an R_M32R_LO16 relocation. This is a straightforward 16 bit
  332. inplace relocation; this function exists in order to do the
  333. R_M32R_HI16_[SU]LO relocation described above. */
  334. static bfd_reloc_status_type
  335. m32r_elf_lo16_reloc (bfd *input_bfd,
  336. arelent *reloc_entry,
  337. asymbol *symbol,
  338. void * data,
  339. asection *input_section,
  340. bfd *output_bfd,
  341. char **error_message)
  342. {
  343. /* This part is from bfd_elf_generic_reloc.
  344. If we're relocating, and this an external symbol, we don't want
  345. to change anything. */
  346. if (output_bfd != NULL
  347. && (symbol->flags & BSF_SECTION_SYM) == 0
  348. && reloc_entry->addend == 0)
  349. {
  350. reloc_entry->address += input_section->output_offset;
  351. return bfd_reloc_ok;
  352. }
  353. if (m32r_hi16_list != NULL)
  354. {
  355. struct m32r_hi16 *l;
  356. l = m32r_hi16_list;
  357. while (l != NULL)
  358. {
  359. unsigned long insn;
  360. unsigned long val;
  361. unsigned long vallo;
  362. struct m32r_hi16 *next;
  363. /* Do the HI16 relocation. Note that we actually don't need
  364. to know anything about the LO16 itself, except where to
  365. find the low 16 bits of the addend needed by the LO16. */
  366. insn = bfd_get_32 (input_bfd, l->addr);
  367. vallo = ((bfd_get_32 (input_bfd, (bfd_byte *) data + reloc_entry->address)
  368. & 0xffff) ^ 0x8000) - 0x8000;
  369. val = ((insn & 0xffff) << 16) + vallo;
  370. val += l->addend;
  371. /* Reaccount for sign extension of low part. */
  372. if ((val & 0x8000) != 0)
  373. val += 0x10000;
  374. insn = (insn &~ (bfd_vma) 0xffff) | ((val >> 16) & 0xffff);
  375. bfd_put_32 (input_bfd, (bfd_vma) insn, l->addr);
  376. next = l->next;
  377. free (l);
  378. l = next;
  379. }
  380. m32r_hi16_list = NULL;
  381. }
  382. /* Now do the LO16 reloc in the usual way.
  383. ??? It would be nice to call bfd_elf_generic_reloc here,
  384. but we have partial_inplace set. bfd_elf_generic_reloc will
  385. pass the handling back to bfd_install_relocation which will install
  386. a section relative addend which is wrong. */
  387. return m32r_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
  388. input_section, output_bfd, error_message);
  389. }
  390. static reloc_howto_type m32r_elf_howto_table[] =
  391. {
  392. /* This reloc does nothing. */
  393. HOWTO (R_M32R_NONE, /* type */
  394. 0, /* rightshift */
  395. 3, /* size (0 = byte, 1 = short, 2 = long) */
  396. 0, /* bitsize */
  397. false, /* pc_relative */
  398. 0, /* bitpos */
  399. complain_overflow_dont, /* complain_on_overflow */
  400. bfd_elf_generic_reloc, /* special_function */
  401. "R_M32R_NONE", /* name */
  402. false, /* partial_inplace */
  403. 0, /* src_mask */
  404. 0, /* dst_mask */
  405. false), /* pcrel_offset */
  406. /* A 16 bit absolute relocation. */
  407. HOWTO (R_M32R_16, /* type */
  408. 0, /* rightshift */
  409. 1, /* size (0 = byte, 1 = short, 2 = long) */
  410. 16, /* bitsize */
  411. false, /* pc_relative */
  412. 0, /* bitpos */
  413. complain_overflow_bitfield, /* complain_on_overflow */
  414. m32r_elf_generic_reloc,/* special_function */
  415. "R_M32R_16", /* name */
  416. true, /* partial_inplace */
  417. 0xffff, /* src_mask */
  418. 0xffff, /* dst_mask */
  419. false), /* pcrel_offset */
  420. /* A 32 bit absolute relocation. */
  421. HOWTO (R_M32R_32, /* type */
  422. 0, /* rightshift */
  423. 2, /* size (0 = byte, 1 = short, 2 = long) */
  424. 32, /* bitsize */
  425. false, /* pc_relative */
  426. 0, /* bitpos */
  427. complain_overflow_bitfield, /* complain_on_overflow */
  428. m32r_elf_generic_reloc,/* special_function */
  429. "R_M32R_32", /* name */
  430. true, /* partial_inplace */
  431. 0xffffffff, /* src_mask */
  432. 0xffffffff, /* dst_mask */
  433. false), /* pcrel_offset */
  434. /* A 24 bit address. */
  435. HOWTO (R_M32R_24, /* type */
  436. 0, /* rightshift */
  437. 2, /* size (0 = byte, 1 = short, 2 = long) */
  438. 24, /* bitsize */
  439. false, /* pc_relative */
  440. 0, /* bitpos */
  441. complain_overflow_unsigned, /* complain_on_overflow */
  442. m32r_elf_generic_reloc,/* special_function */
  443. "R_M32R_24", /* name */
  444. true, /* partial_inplace */
  445. 0xffffff, /* src_mask */
  446. 0xffffff, /* dst_mask */
  447. false), /* pcrel_offset */
  448. /* An PC Relative 10-bit relocation, shifted by 2.
  449. This reloc is complicated because relocations are relative to pc & -4.
  450. i.e. branches in the right insn slot use the address of the left insn
  451. slot for pc. */
  452. /* ??? It's not clear whether this should have partial_inplace set or not.
  453. Branch relaxing in the assembler can store the addend in the insn,
  454. and if bfd_install_relocation gets called the addend may get added
  455. again. */
  456. HOWTO (R_M32R_10_PCREL, /* type */
  457. 2, /* rightshift */
  458. 1, /* size (0 = byte, 1 = short, 2 = long) */
  459. 10, /* bitsize */
  460. true, /* pc_relative */
  461. 0, /* bitpos */
  462. complain_overflow_signed, /* complain_on_overflow */
  463. m32r_elf_10_pcrel_reloc, /* special_function */
  464. "R_M32R_10_PCREL", /* name */
  465. false, /* partial_inplace */
  466. 0xff, /* src_mask */
  467. 0xff, /* dst_mask */
  468. true), /* pcrel_offset */
  469. /* A relative 18 bit relocation, right shifted by 2. */
  470. HOWTO (R_M32R_18_PCREL, /* type */
  471. 2, /* rightshift */
  472. 2, /* size (0 = byte, 1 = short, 2 = long) */
  473. 16, /* bitsize */
  474. true, /* pc_relative */
  475. 0, /* bitpos */
  476. complain_overflow_signed, /* complain_on_overflow */
  477. bfd_elf_generic_reloc, /* special_function */
  478. "R_M32R_18_PCREL", /* name */
  479. false, /* partial_inplace */
  480. 0xffff, /* src_mask */
  481. 0xffff, /* dst_mask */
  482. true), /* pcrel_offset */
  483. /* A relative 26 bit relocation, right shifted by 2. */
  484. /* ??? It's not clear whether this should have partial_inplace set or not.
  485. Branch relaxing in the assembler can store the addend in the insn,
  486. and if bfd_install_relocation gets called the addend may get added
  487. again. */
  488. HOWTO (R_M32R_26_PCREL, /* type */
  489. 2, /* rightshift */
  490. 2, /* size (0 = byte, 1 = short, 2 = long) */
  491. 26, /* bitsize */
  492. true, /* pc_relative */
  493. 0, /* bitpos */
  494. complain_overflow_signed, /* complain_on_overflow */
  495. bfd_elf_generic_reloc, /* special_function */
  496. "R_M32R_26_PCREL", /* name */
  497. false, /* partial_inplace */
  498. 0xffffff, /* src_mask */
  499. 0xffffff, /* dst_mask */
  500. true), /* pcrel_offset */
  501. /* High 16 bits of address when lower 16 is or'd in. */
  502. HOWTO (R_M32R_HI16_ULO, /* type */
  503. 16, /* rightshift */
  504. 2, /* size (0 = byte, 1 = short, 2 = long) */
  505. 16, /* bitsize */
  506. false, /* pc_relative */
  507. 0, /* bitpos */
  508. complain_overflow_dont, /* complain_on_overflow */
  509. m32r_elf_hi16_reloc, /* special_function */
  510. "R_M32R_HI16_ULO", /* name */
  511. true, /* partial_inplace */
  512. 0x0000ffff, /* src_mask */
  513. 0x0000ffff, /* dst_mask */
  514. false), /* pcrel_offset */
  515. /* High 16 bits of address when lower 16 is added in. */
  516. HOWTO (R_M32R_HI16_SLO, /* type */
  517. 16, /* rightshift */
  518. 2, /* size (0 = byte, 1 = short, 2 = long) */
  519. 16, /* bitsize */
  520. false, /* pc_relative */
  521. 0, /* bitpos */
  522. complain_overflow_dont, /* complain_on_overflow */
  523. m32r_elf_hi16_reloc, /* special_function */
  524. "R_M32R_HI16_SLO", /* name */
  525. true, /* partial_inplace */
  526. 0x0000ffff, /* src_mask */
  527. 0x0000ffff, /* dst_mask */
  528. false), /* pcrel_offset */
  529. /* Lower 16 bits of address. */
  530. HOWTO (R_M32R_LO16, /* type */
  531. 0, /* rightshift */
  532. 2, /* size (0 = byte, 1 = short, 2 = long) */
  533. 16, /* bitsize */
  534. false, /* pc_relative */
  535. 0, /* bitpos */
  536. complain_overflow_dont, /* complain_on_overflow */
  537. m32r_elf_lo16_reloc, /* special_function */
  538. "R_M32R_LO16", /* name */
  539. true, /* partial_inplace */
  540. 0x0000ffff, /* src_mask */
  541. 0x0000ffff, /* dst_mask */
  542. false), /* pcrel_offset */
  543. /* Small data area 16 bits offset. */
  544. HOWTO (R_M32R_SDA16, /* type */
  545. 0, /* rightshift */
  546. 2, /* size (0 = byte, 1 = short, 2 = long) */
  547. 16, /* bitsize */
  548. false, /* pc_relative */
  549. 0, /* bitpos */
  550. complain_overflow_signed, /* complain_on_overflow */
  551. m32r_elf_sda16_reloc, /* special_function */
  552. "R_M32R_SDA16", /* name */
  553. true, /* partial_inplace */ /* FIXME: correct? */
  554. 0x0000ffff, /* src_mask */
  555. 0x0000ffff, /* dst_mask */
  556. false), /* pcrel_offset */
  557. /* GNU extension to record C++ vtable hierarchy. */
  558. HOWTO (R_M32R_GNU_VTINHERIT, /* type */
  559. 0, /* rightshift */
  560. 2, /* size (0 = byte, 1 = short, 2 = long) */
  561. 0, /* bitsize */
  562. false, /* pc_relative */
  563. 0, /* bitpos */
  564. complain_overflow_dont, /* complain_on_overflow */
  565. NULL, /* special_function */
  566. "R_M32R_GNU_VTINHERIT", /* name */
  567. false, /* partial_inplace */
  568. 0, /* src_mask */
  569. 0, /* dst_mask */
  570. false), /* pcrel_offset */
  571. /* GNU extension to record C++ vtable member usage. */
  572. HOWTO (R_M32R_GNU_VTENTRY, /* type */
  573. 0, /* rightshift */
  574. 2, /* size (0 = byte, 1 = short, 2 = long) */
  575. 0, /* bitsize */
  576. false, /* pc_relative */
  577. 0, /* bitpos */
  578. complain_overflow_dont, /* complain_on_overflow */
  579. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  580. "R_M32R_GNU_VTENTRY", /* name */
  581. false, /* partial_inplace */
  582. 0, /* src_mask */
  583. 0, /* dst_mask */
  584. false), /* pcrel_offset */
  585. EMPTY_HOWTO (13),
  586. EMPTY_HOWTO (14),
  587. EMPTY_HOWTO (15),
  588. EMPTY_HOWTO (16),
  589. EMPTY_HOWTO (17),
  590. EMPTY_HOWTO (18),
  591. EMPTY_HOWTO (19),
  592. EMPTY_HOWTO (20),
  593. EMPTY_HOWTO (21),
  594. EMPTY_HOWTO (22),
  595. EMPTY_HOWTO (23),
  596. EMPTY_HOWTO (24),
  597. EMPTY_HOWTO (25),
  598. EMPTY_HOWTO (26),
  599. EMPTY_HOWTO (27),
  600. EMPTY_HOWTO (28),
  601. EMPTY_HOWTO (29),
  602. EMPTY_HOWTO (30),
  603. EMPTY_HOWTO (31),
  604. EMPTY_HOWTO (32),
  605. /* A 16 bit absolute relocation. */
  606. HOWTO (R_M32R_16_RELA, /* type */
  607. 0, /* rightshift */
  608. 1, /* size (0 = byte, 1 = short, 2 = long) */
  609. 16, /* bitsize */
  610. false, /* pc_relative */
  611. 0, /* bitpos */
  612. complain_overflow_bitfield, /* complain_on_overflow */
  613. bfd_elf_generic_reloc, /* special_function */
  614. "R_M32R_16_RELA", /* name */
  615. false, /* partial_inplace */
  616. 0xffff, /* src_mask */
  617. 0xffff, /* dst_mask */
  618. false), /* pcrel_offset */
  619. /* A 32 bit absolute relocation. */
  620. HOWTO (R_M32R_32_RELA, /* type */
  621. 0, /* rightshift */
  622. 2, /* size (0 = byte, 1 = short, 2 = long) */
  623. 32, /* bitsize */
  624. false, /* pc_relative */
  625. 0, /* bitpos */
  626. complain_overflow_bitfield, /* complain_on_overflow */
  627. bfd_elf_generic_reloc,/* special_function */
  628. "R_M32R_32_RELA", /* name */
  629. false, /* partial_inplace */
  630. 0xffffffff, /* src_mask */
  631. 0xffffffff, /* dst_mask */
  632. false), /* pcrel_offset */
  633. /* A 24 bit address. */
  634. HOWTO (R_M32R_24_RELA, /* type */
  635. 0, /* rightshift */
  636. 2, /* size (0 = byte, 1 = short, 2 = long) */
  637. 24, /* bitsize */
  638. false, /* pc_relative */
  639. 0, /* bitpos */
  640. complain_overflow_unsigned, /* complain_on_overflow */
  641. bfd_elf_generic_reloc,/* special_function */
  642. "R_M32R_24_RELA", /* name */
  643. false, /* partial_inplace */
  644. 0xffffff, /* src_mask */
  645. 0xffffff, /* dst_mask */
  646. false), /* pcrel_offset */
  647. HOWTO (R_M32R_10_PCREL_RELA, /* type */
  648. 2, /* rightshift */
  649. 1, /* size (0 = byte, 1 = short, 2 = long) */
  650. 10, /* bitsize */
  651. true, /* pc_relative */
  652. 0, /* bitpos */
  653. complain_overflow_signed, /* complain_on_overflow */
  654. m32r_elf_10_pcrel_reloc, /* special_function */
  655. "R_M32R_10_PCREL_RELA",/* name */
  656. false, /* partial_inplace */
  657. 0xff, /* src_mask */
  658. 0xff, /* dst_mask */
  659. true), /* pcrel_offset */
  660. /* A relative 18 bit relocation, right shifted by 2. */
  661. HOWTO (R_M32R_18_PCREL_RELA, /* type */
  662. 2, /* rightshift */
  663. 2, /* size (0 = byte, 1 = short, 2 = long) */
  664. 16, /* bitsize */
  665. true, /* pc_relative */
  666. 0, /* bitpos */
  667. complain_overflow_signed, /* complain_on_overflow */
  668. bfd_elf_generic_reloc, /* special_function */
  669. "R_M32R_18_PCREL_RELA",/* name */
  670. false, /* partial_inplace */
  671. 0xffff, /* src_mask */
  672. 0xffff, /* dst_mask */
  673. true), /* pcrel_offset */
  674. /* A relative 26 bit relocation, right shifted by 2. */
  675. HOWTO (R_M32R_26_PCREL_RELA, /* type */
  676. 2, /* rightshift */
  677. 2, /* size (0 = byte, 1 = short, 2 = long) */
  678. 26, /* bitsize */
  679. true, /* pc_relative */
  680. 0, /* bitpos */
  681. complain_overflow_signed, /* complain_on_overflow */
  682. bfd_elf_generic_reloc, /* special_function */
  683. "R_M32R_26_PCREL_RELA",/* name */
  684. false, /* partial_inplace */
  685. 0xffffff, /* src_mask */
  686. 0xffffff, /* dst_mask */
  687. true), /* pcrel_offset */
  688. /* High 16 bits of address when lower 16 is or'd in. */
  689. HOWTO (R_M32R_HI16_ULO_RELA, /* type */
  690. 16, /* rightshift */
  691. 2, /* size (0 = byte, 1 = short, 2 = long) */
  692. 16, /* bitsize */
  693. false, /* pc_relative */
  694. 0, /* bitpos */
  695. complain_overflow_dont, /* complain_on_overflow */
  696. bfd_elf_generic_reloc, /* special_function */
  697. "R_M32R_HI16_ULO_RELA",/* name */
  698. false, /* partial_inplace */
  699. 0x0000ffff, /* src_mask */
  700. 0x0000ffff, /* dst_mask */
  701. false), /* pcrel_offset */
  702. /* High 16 bits of address when lower 16 is added in. */
  703. HOWTO (R_M32R_HI16_SLO_RELA, /* type */
  704. 16, /* rightshift */
  705. 2, /* size (0 = byte, 1 = short, 2 = long) */
  706. 16, /* bitsize */
  707. false, /* pc_relative */
  708. 0, /* bitpos */
  709. complain_overflow_dont, /* complain_on_overflow */
  710. bfd_elf_generic_reloc, /* special_function */
  711. "R_M32R_HI16_SLO_RELA",/* name */
  712. false, /* partial_inplace */
  713. 0x0000ffff, /* src_mask */
  714. 0x0000ffff, /* dst_mask */
  715. false), /* pcrel_offset */
  716. /* Lower 16 bits of address. */
  717. HOWTO (R_M32R_LO16_RELA, /* type */
  718. 0, /* rightshift */
  719. 2, /* size (0 = byte, 1 = short, 2 = long) */
  720. 16, /* bitsize */
  721. false, /* pc_relative */
  722. 0, /* bitpos */
  723. complain_overflow_dont, /* complain_on_overflow */
  724. bfd_elf_generic_reloc, /* special_function */
  725. "R_M32R_LO16_RELA", /* name */
  726. false, /* partial_inplace */
  727. 0x0000ffff, /* src_mask */
  728. 0x0000ffff, /* dst_mask */
  729. false), /* pcrel_offset */
  730. /* Small data area 16 bits offset. */
  731. HOWTO (R_M32R_SDA16_RELA, /* type */
  732. 0, /* rightshift */
  733. 2, /* size (0 = byte, 1 = short, 2 = long) */
  734. 16, /* bitsize */
  735. false, /* pc_relative */
  736. 0, /* bitpos */
  737. complain_overflow_signed, /* complain_on_overflow */
  738. bfd_elf_generic_reloc, /* special_function */
  739. "R_M32R_SDA16_RELA", /* name */
  740. true, /* partial_inplace */ /* FIXME: correct? */
  741. 0x0000ffff, /* src_mask */
  742. 0x0000ffff, /* dst_mask */
  743. false), /* pcrel_offset */
  744. /* GNU extension to record C++ vtable hierarchy. */
  745. HOWTO (R_M32R_RELA_GNU_VTINHERIT, /* type */
  746. 0, /* rightshift */
  747. 2, /* size (0 = byte, 1 = short, 2 = long) */
  748. 0, /* bitsize */
  749. false, /* pc_relative */
  750. 0, /* bitpos */
  751. complain_overflow_dont, /* complain_on_overflow */
  752. NULL, /* special_function */
  753. "R_M32R_RELA_GNU_VTINHERIT", /* name */
  754. false, /* partial_inplace */
  755. 0, /* src_mask */
  756. 0, /* dst_mask */
  757. false), /* pcrel_offset */
  758. /* GNU extension to record C++ vtable member usage. */
  759. HOWTO (R_M32R_RELA_GNU_VTENTRY, /* type */
  760. 0, /* rightshift */
  761. 2, /* size (0 = byte, 1 = short, 2 = long) */
  762. 0, /* bitsize */
  763. false, /* pc_relative */
  764. 0, /* bitpos */
  765. complain_overflow_dont, /* complain_on_overflow */
  766. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  767. "R_M32R_RELA_GNU_VTENTRY", /* name */
  768. false, /* partial_inplace */
  769. 0, /* src_mask */
  770. 0, /* dst_mask */
  771. false), /* pcrel_offset */
  772. /* A 32 bit PC relative relocation. */
  773. HOWTO (R_M32R_REL32, /* type */
  774. 0, /* rightshift */
  775. 2, /* size (0 = byte, 1 = short, 2 = long) */
  776. 32, /* bitsize */
  777. true, /* pc_relative */
  778. 0, /* bitpos */
  779. complain_overflow_bitfield, /* complain_on_overflow */
  780. bfd_elf_generic_reloc,/* special_function */
  781. "R_M32R_REL32", /* name */
  782. false, /* partial_inplace */
  783. 0xffffffff, /* src_mask */
  784. 0xffffffff, /* dst_mask */
  785. true), /* pcrel_offset */
  786. EMPTY_HOWTO (46),
  787. EMPTY_HOWTO (47),
  788. /* Like R_M32R_24, but referring to the GOT table entry for
  789. the symbol. */
  790. HOWTO (R_M32R_GOT24, /* type */
  791. 0, /* rightshift */
  792. 2, /* size (0 = byte, 1 = short, 2 = long) */
  793. 24, /* bitsize */
  794. false, /* pc_relative */
  795. 0, /* bitpos */
  796. complain_overflow_unsigned, /* complain_on_overflow */
  797. bfd_elf_generic_reloc, /* special_function */
  798. "R_M32R_GOT24", /* name */
  799. false, /* partial_inplace */
  800. 0xffffff, /* src_mask */
  801. 0xffffff, /* dst_mask */
  802. false), /* pcrel_offset */
  803. /* Like R_M32R_PCREL, but referring to the procedure linkage table
  804. entry for the symbol. */
  805. HOWTO (R_M32R_26_PLTREL, /* type */
  806. 2, /* rightshift */
  807. 2, /* size (0 = byte, 1 = short, 2 = long) */
  808. 24, /* bitsize */
  809. true, /* pc_relative */
  810. 0, /* bitpos */
  811. complain_overflow_signed, /* complain_on_overflow */
  812. bfd_elf_generic_reloc, /* special_function */
  813. "R_M32R_26_PLTREL", /* name */
  814. false, /* partial_inplace */
  815. 0xffffff, /* src_mask */
  816. 0xffffff, /* dst_mask */
  817. true), /* pcrel_offset */
  818. /* This is used only by the dynamic linker. The symbol should exist
  819. both in the object being run and in some shared library. The
  820. dynamic linker copies the data addressed by the symbol from the
  821. shared library into the object, because the object being
  822. run has to have the data at some particular address. */
  823. HOWTO (R_M32R_COPY, /* type */
  824. 0, /* rightshift */
  825. 2, /* size (0 = byte, 1 = short, 2 = long) */
  826. 32, /* bitsize */
  827. false, /* pc_relative */
  828. 0, /* bitpos */
  829. complain_overflow_bitfield, /* complain_on_overflow */
  830. bfd_elf_generic_reloc, /* special_function */
  831. "R_M32R_COPY", /* name */
  832. false, /* partial_inplace */
  833. 0xffffffff, /* src_mask */
  834. 0xffffffff, /* dst_mask */
  835. false), /* pcrel_offset */
  836. /* Like R_M32R_24, but used when setting global offset table
  837. entries. */
  838. HOWTO (R_M32R_GLOB_DAT, /* type */
  839. 0, /* rightshift */
  840. 2, /* size (0 = byte, 1 = short, 2 = long) */
  841. 32, /* bitsize */
  842. false, /* pc_relative */
  843. 0, /* bitpos */
  844. complain_overflow_bitfield, /* complain_on_overflow */
  845. bfd_elf_generic_reloc, /* special_function */
  846. "R_M32R_GLOB_DAT", /* name */
  847. false, /* partial_inplace */
  848. 0xffffffff, /* src_mask */
  849. 0xffffffff, /* dst_mask */
  850. false), /* pcrel_offset */
  851. /* Marks a procedure linkage table entry for a symbol. */
  852. HOWTO (R_M32R_JMP_SLOT, /* type */
  853. 0, /* rightshift */
  854. 2, /* size (0 = byte, 1 = short, 2 = long) */
  855. 32, /* bitsize */
  856. false, /* pc_relative */
  857. 0, /* bitpos */
  858. complain_overflow_bitfield, /* complain_on_overflow */
  859. bfd_elf_generic_reloc, /* special_function */
  860. "R_M32R_JMP_SLOT", /* name */
  861. false, /* partial_inplace */
  862. 0xffffffff, /* src_mask */
  863. 0xffffffff, /* dst_mask */
  864. false), /* pcrel_offset */
  865. /* Used only by the dynamic linker. When the object is run, this
  866. longword is set to the load address of the object, plus the
  867. addend. */
  868. HOWTO (R_M32R_RELATIVE, /* type */
  869. 0, /* rightshift */
  870. 2, /* size (0 = byte, 1 = short, 2 = long) */
  871. 32, /* bitsize */
  872. false, /* pc_relative */
  873. 0, /* bitpos */
  874. complain_overflow_bitfield, /* complain_on_overflow */
  875. bfd_elf_generic_reloc, /* special_function */
  876. "R_M32R_RELATIVE", /* name */
  877. false, /* partial_inplace */
  878. 0xffffffff, /* src_mask */
  879. 0xffffffff, /* dst_mask */
  880. false), /* pcrel_offset */
  881. HOWTO (R_M32R_GOTOFF, /* type */
  882. 0, /* rightshift */
  883. 2, /* size (0 = byte, 1 = short, 2 = long) */
  884. 24, /* bitsize */
  885. false, /* pc_relative */
  886. 0, /* bitpos */
  887. complain_overflow_bitfield, /* complain_on_overflow */
  888. bfd_elf_generic_reloc, /* special_function */
  889. "R_M32R_GOTOFF", /* name */
  890. false, /* partial_inplace */
  891. 0xffffff, /* src_mask */
  892. 0xffffff, /* dst_mask */
  893. false), /* pcrel_offset */
  894. /* An PC Relative 24-bit relocation used when setting PIC offset
  895. table register. */
  896. HOWTO (R_M32R_GOTPC24, /* type */
  897. 0, /* rightshift */
  898. 2, /* size (0 = byte, 1 = short, 2 = long) */
  899. 24, /* bitsize */
  900. true, /* pc_relative */
  901. 0, /* bitpos */
  902. complain_overflow_unsigned, /* complain_on_overflow */
  903. bfd_elf_generic_reloc, /* special_function */
  904. "R_M32R_GOTPC24", /* name */
  905. false, /* partial_inplace */
  906. 0xffffff, /* src_mask */
  907. 0xffffff, /* dst_mask */
  908. true), /* pcrel_offset */
  909. /* Like R_M32R_HI16_ULO, but referring to the GOT table entry for
  910. the symbol. */
  911. HOWTO (R_M32R_GOT16_HI_ULO, /* type */
  912. 16, /* rightshift */
  913. 2, /* size (0 = byte, 1 = short, 2 = long) */
  914. 16, /* bitsize */
  915. false, /* pc_relative */
  916. 0, /* bitpos */
  917. complain_overflow_dont, /* complain_on_overflow */
  918. bfd_elf_generic_reloc, /* special_function */
  919. "R_M32R_GOT16_HI_ULO", /* name */
  920. false, /* partial_inplace */
  921. 0x0000ffff, /* src_mask */
  922. 0x0000ffff, /* dst_mask */
  923. false), /* pcrel_offset */
  924. /* Like R_M32R_HI16_SLO, but referring to the GOT table entry for
  925. the symbol. */
  926. HOWTO (R_M32R_GOT16_HI_SLO, /* type */
  927. 16, /* rightshift */
  928. 2, /* size (0 = byte, 1 = short, 2 = long) */
  929. 16, /* bitsize */
  930. false, /* pc_relative */
  931. 0, /* bitpos */
  932. complain_overflow_dont, /* complain_on_overflow */
  933. bfd_elf_generic_reloc, /* special_function */
  934. "R_M32R_GOT16_HI_SLO", /* name */
  935. false, /* partial_inplace */
  936. 0x0000ffff, /* src_mask */
  937. 0x0000ffff, /* dst_mask */
  938. false), /* pcrel_offset */
  939. /* Like R_M32R_LO16, but referring to the GOT table entry for
  940. the symbol. */
  941. HOWTO (R_M32R_GOT16_LO, /* type */
  942. 0, /* rightshift */
  943. 2, /* size (0 = byte, 1 = short, 2 = long) */
  944. 16, /* bitsize */
  945. false, /* pc_relative */
  946. 0, /* bitpos */
  947. complain_overflow_dont, /* complain_on_overflow */
  948. bfd_elf_generic_reloc, /* special_function */
  949. "R_M32R_GOT16_LO", /* name */
  950. false, /* partial_inplace */
  951. 0x0000ffff, /* src_mask */
  952. 0x0000ffff, /* dst_mask */
  953. false), /* pcrel_offset */
  954. /* An PC Relative relocation used when setting PIC offset table register.
  955. Like R_M32R_HI16_ULO, but referring to the GOT table entry for
  956. the symbol. */
  957. HOWTO (R_M32R_GOTPC_HI_ULO, /* type */
  958. 16, /* rightshift */
  959. 2, /* size (0 = byte, 1 = short, 2 = long) */
  960. 16, /* bitsize */
  961. false, /* pc_relative */
  962. 0, /* bitpos */
  963. complain_overflow_dont, /* complain_on_overflow */
  964. bfd_elf_generic_reloc, /* special_function */
  965. "R_M32R_GOTPC_HI_ULO", /* name */
  966. false, /* partial_inplace */
  967. 0x0000ffff, /* src_mask */
  968. 0x0000ffff, /* dst_mask */
  969. true), /* pcrel_offset */
  970. /* An PC Relative relocation used when setting PIC offset table register.
  971. Like R_M32R_HI16_SLO, but referring to the GOT table entry for
  972. the symbol. */
  973. HOWTO (R_M32R_GOTPC_HI_SLO, /* type */
  974. 16, /* rightshift */
  975. 2, /* size (0 = byte, 1 = short, 2 = long) */
  976. 16, /* bitsize */
  977. false, /* pc_relative */
  978. 0, /* bitpos */
  979. complain_overflow_dont, /* complain_on_overflow */
  980. bfd_elf_generic_reloc, /* special_function */
  981. "R_M32R_GOTPC_HI_SLO", /* name */
  982. false, /* partial_inplace */
  983. 0x0000ffff, /* src_mask */
  984. 0x0000ffff, /* dst_mask */
  985. true), /* pcrel_offset */
  986. /* An PC Relative relocation used when setting PIC offset table register.
  987. Like R_M32R_LO16, but referring to the GOT table entry for
  988. the symbol. */
  989. HOWTO (R_M32R_GOTPC_LO, /* type */
  990. 0, /* rightshift */
  991. 2, /* size (0 = byte, 1 = short, 2 = long) */
  992. 16, /* bitsize */
  993. false, /* pc_relative */
  994. 0, /* bitpos */
  995. complain_overflow_dont, /* complain_on_overflow */
  996. bfd_elf_generic_reloc, /* special_function */
  997. "R_M32R_GOTPC_LO", /* name */
  998. false, /* partial_inplace */
  999. 0x0000ffff, /* src_mask */
  1000. 0x0000ffff, /* dst_mask */
  1001. true), /* pcrel_offset */
  1002. HOWTO (R_M32R_GOTOFF_HI_ULO, /* type */
  1003. 16, /* rightshift */
  1004. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1005. 16, /* bitsize */
  1006. false, /* pc_relative */
  1007. 0, /* bitpos */
  1008. complain_overflow_dont, /* complain_on_overflow */
  1009. bfd_elf_generic_reloc, /* special_function */
  1010. "R_M32R_GOTOFF_HI_ULO",/* name */
  1011. false, /* partial_inplace */
  1012. 0x0000ffff, /* src_mask */
  1013. 0x0000ffff, /* dst_mask */
  1014. false), /* pcrel_offset */
  1015. HOWTO (R_M32R_GOTOFF_HI_SLO, /* type */
  1016. 16, /* rightshift */
  1017. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1018. 16, /* bitsize */
  1019. false, /* pc_relative */
  1020. 0, /* bitpos */
  1021. complain_overflow_dont, /* complain_on_overflow */
  1022. bfd_elf_generic_reloc, /* special_function */
  1023. "R_M32R_GOTOFF_HI_SLO",/* name */
  1024. false, /* partial_inplace */
  1025. 0x0000ffff, /* src_mask */
  1026. 0x0000ffff, /* dst_mask */
  1027. false), /* pcrel_offset */
  1028. HOWTO (R_M32R_GOTOFF_LO, /* type */
  1029. 0, /* rightshift */
  1030. 2, /* size (0 = byte, 1 = short, 2 = long) */
  1031. 16, /* bitsize */
  1032. false, /* pc_relative */
  1033. 0, /* bitpos */
  1034. complain_overflow_dont, /* complain_on_overflow */
  1035. bfd_elf_generic_reloc, /* special_function */
  1036. "R_M32R_GOTOFF_LO", /* name */
  1037. false, /* partial_inplace */
  1038. 0x0000ffff, /* src_mask */
  1039. 0x0000ffff, /* dst_mask */
  1040. false), /* pcrel_offset */
  1041. };
  1042. /* Map BFD reloc types to M32R ELF reloc types. */
  1043. struct m32r_reloc_map
  1044. {
  1045. bfd_reloc_code_real_type bfd_reloc_val;
  1046. unsigned char elf_reloc_val;
  1047. };
  1048. #ifdef USE_M32R_OLD_RELOC
  1049. static const struct m32r_reloc_map m32r_reloc_map_old[] =
  1050. {
  1051. { BFD_RELOC_NONE, R_M32R_NONE },
  1052. { BFD_RELOC_16, R_M32R_16 },
  1053. { BFD_RELOC_32, R_M32R_32 },
  1054. { BFD_RELOC_M32R_24, R_M32R_24 },
  1055. { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL },
  1056. { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL },
  1057. { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL },
  1058. { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO },
  1059. { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO },
  1060. { BFD_RELOC_M32R_LO16, R_M32R_LO16 },
  1061. { BFD_RELOC_M32R_SDA16, R_M32R_SDA16 },
  1062. { BFD_RELOC_VTABLE_INHERIT, R_M32R_GNU_VTINHERIT },
  1063. { BFD_RELOC_VTABLE_ENTRY, R_M32R_GNU_VTENTRY },
  1064. };
  1065. #else
  1066. static const struct m32r_reloc_map m32r_reloc_map[] =
  1067. {
  1068. { BFD_RELOC_NONE, R_M32R_NONE },
  1069. { BFD_RELOC_16, R_M32R_16_RELA },
  1070. { BFD_RELOC_32, R_M32R_32_RELA },
  1071. { BFD_RELOC_M32R_24, R_M32R_24_RELA },
  1072. { BFD_RELOC_M32R_10_PCREL, R_M32R_10_PCREL_RELA },
  1073. { BFD_RELOC_M32R_18_PCREL, R_M32R_18_PCREL_RELA },
  1074. { BFD_RELOC_M32R_26_PCREL, R_M32R_26_PCREL_RELA },
  1075. { BFD_RELOC_M32R_HI16_ULO, R_M32R_HI16_ULO_RELA },
  1076. { BFD_RELOC_M32R_HI16_SLO, R_M32R_HI16_SLO_RELA },
  1077. { BFD_RELOC_M32R_LO16, R_M32R_LO16_RELA },
  1078. { BFD_RELOC_M32R_SDA16, R_M32R_SDA16_RELA },
  1079. { BFD_RELOC_VTABLE_INHERIT, R_M32R_RELA_GNU_VTINHERIT },
  1080. { BFD_RELOC_VTABLE_ENTRY, R_M32R_RELA_GNU_VTENTRY },
  1081. { BFD_RELOC_32_PCREL, R_M32R_REL32 },
  1082. { BFD_RELOC_M32R_GOT24, R_M32R_GOT24 },
  1083. { BFD_RELOC_M32R_26_PLTREL, R_M32R_26_PLTREL },
  1084. { BFD_RELOC_M32R_COPY, R_M32R_COPY },
  1085. { BFD_RELOC_M32R_GLOB_DAT, R_M32R_GLOB_DAT },
  1086. { BFD_RELOC_M32R_JMP_SLOT, R_M32R_JMP_SLOT },
  1087. { BFD_RELOC_M32R_RELATIVE, R_M32R_RELATIVE },
  1088. { BFD_RELOC_M32R_GOTOFF, R_M32R_GOTOFF },
  1089. { BFD_RELOC_M32R_GOTPC24, R_M32R_GOTPC24 },
  1090. { BFD_RELOC_M32R_GOT16_HI_ULO, R_M32R_GOT16_HI_ULO },
  1091. { BFD_RELOC_M32R_GOT16_HI_SLO, R_M32R_GOT16_HI_SLO },
  1092. { BFD_RELOC_M32R_GOT16_LO, R_M32R_GOT16_LO },
  1093. { BFD_RELOC_M32R_GOTPC_HI_ULO, R_M32R_GOTPC_HI_ULO },
  1094. { BFD_RELOC_M32R_GOTPC_HI_SLO, R_M32R_GOTPC_HI_SLO },
  1095. { BFD_RELOC_M32R_GOTPC_LO, R_M32R_GOTPC_LO },
  1096. { BFD_RELOC_M32R_GOTOFF_HI_ULO, R_M32R_GOTOFF_HI_ULO },
  1097. { BFD_RELOC_M32R_GOTOFF_HI_SLO, R_M32R_GOTOFF_HI_SLO },
  1098. { BFD_RELOC_M32R_GOTOFF_LO, R_M32R_GOTOFF_LO },
  1099. };
  1100. #endif
  1101. static reloc_howto_type *
  1102. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  1103. bfd_reloc_code_real_type code)
  1104. {
  1105. unsigned int i;
  1106. #ifdef USE_M32R_OLD_RELOC
  1107. for (i = 0;
  1108. i < sizeof (m32r_reloc_map_old) / sizeof (struct m32r_reloc_map);
  1109. i++)
  1110. if (m32r_reloc_map_old[i].bfd_reloc_val == code)
  1111. return &m32r_elf_howto_table[m32r_reloc_map_old[i].elf_reloc_val];
  1112. #else /* ! USE_M32R_OLD_RELOC */
  1113. for (i = 0;
  1114. i < sizeof (m32r_reloc_map) / sizeof (struct m32r_reloc_map);
  1115. i++)
  1116. if (m32r_reloc_map[i].bfd_reloc_val == code)
  1117. return &m32r_elf_howto_table[m32r_reloc_map[i].elf_reloc_val];
  1118. #endif
  1119. return NULL;
  1120. }
  1121. static reloc_howto_type *
  1122. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  1123. const char *r_name)
  1124. {
  1125. unsigned int i;
  1126. for (i = 0;
  1127. i < sizeof (m32r_elf_howto_table) / sizeof (m32r_elf_howto_table[0]);
  1128. i++)
  1129. if (m32r_elf_howto_table[i].name != NULL
  1130. && strcasecmp (m32r_elf_howto_table[i].name, r_name) == 0)
  1131. return &m32r_elf_howto_table[i];
  1132. return NULL;
  1133. }
  1134. /* Set the howto pointer for an M32R ELF reloc. */
  1135. static bool
  1136. m32r_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
  1137. arelent *cache_ptr,
  1138. Elf_Internal_Rela *dst)
  1139. {
  1140. unsigned int r_type;
  1141. r_type = ELF32_R_TYPE (dst->r_info);
  1142. if (r_type > (unsigned int) R_M32R_GNU_VTENTRY)
  1143. {
  1144. /* xgettext:c-format */
  1145. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  1146. abfd, r_type);
  1147. bfd_set_error (bfd_error_bad_value);
  1148. return false;
  1149. }
  1150. cache_ptr->howto = &m32r_elf_howto_table[r_type];
  1151. return true;
  1152. }
  1153. static bool
  1154. m32r_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  1155. arelent *cache_ptr,
  1156. Elf_Internal_Rela *dst)
  1157. {
  1158. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  1159. if (r_type == (unsigned int) R_M32R_NONE
  1160. || ((r_type > (unsigned int) R_M32R_GNU_VTENTRY)
  1161. && (r_type < (unsigned int) R_M32R_max)))
  1162. {
  1163. cache_ptr->howto = &m32r_elf_howto_table[r_type];
  1164. return true;
  1165. }
  1166. /* xgettext:c-format */
  1167. _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
  1168. bfd_set_error (bfd_error_bad_value);
  1169. return false;
  1170. }
  1171. /* Given a BFD section, try to locate the corresponding ELF section
  1172. index. */
  1173. static bool
  1174. _bfd_m32r_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
  1175. asection *sec,
  1176. int *retval)
  1177. {
  1178. if (strcmp (bfd_section_name (sec), ".scommon") == 0)
  1179. {
  1180. *retval = SHN_M32R_SCOMMON;
  1181. return true;
  1182. }
  1183. return false;
  1184. }
  1185. /* M32R ELF uses two common sections. One is the usual one, and the other
  1186. is for small objects. All the small objects are kept together, and then
  1187. referenced via one register, which yields faster assembler code. It is
  1188. up to the compiler to emit an instruction to load the register with
  1189. _SDA_BASE. This is what we use for the small common section. This
  1190. approach is copied from elf32-mips.c. */
  1191. static asection m32r_elf_scom_section;
  1192. static const asymbol m32r_elf_scom_symbol =
  1193. GLOBAL_SYM_INIT (".scommon", &m32r_elf_scom_section);
  1194. static asection m32r_elf_scom_section =
  1195. BFD_FAKE_SECTION (m32r_elf_scom_section, &m32r_elf_scom_symbol,
  1196. ".scommon", 0, SEC_IS_COMMON | SEC_SMALL_DATA);
  1197. /* Handle the special M32R section numbers that a symbol may use. */
  1198. static void
  1199. _bfd_m32r_elf_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
  1200. {
  1201. elf_symbol_type *elfsym = (elf_symbol_type *) asym;
  1202. switch (elfsym->internal_elf_sym.st_shndx)
  1203. {
  1204. case SHN_M32R_SCOMMON:
  1205. asym->section = &m32r_elf_scom_section;
  1206. asym->value = elfsym->internal_elf_sym.st_size;
  1207. break;
  1208. }
  1209. }
  1210. /* Hook called by the linker routine which adds symbols from an object
  1211. file. We must handle the special M32R section numbers here.
  1212. We also keep watching for whether we need to create the sdata special
  1213. linker sections. */
  1214. static bool
  1215. m32r_elf_add_symbol_hook (bfd *abfd,
  1216. struct bfd_link_info *info,
  1217. Elf_Internal_Sym *sym,
  1218. const char **namep,
  1219. flagword *flagsp ATTRIBUTE_UNUSED,
  1220. asection **secp,
  1221. bfd_vma *valp)
  1222. {
  1223. if (! bfd_link_relocatable (info)
  1224. && (*namep)[0] == '_' && (*namep)[1] == 'S'
  1225. && strcmp (*namep, "_SDA_BASE_") == 0
  1226. && is_elf_hash_table (info->hash))
  1227. {
  1228. /* This is simpler than using _bfd_elf_create_linker_section
  1229. (our needs are simpler than ppc's needs). Also
  1230. _bfd_elf_create_linker_section currently has a bug where if a .sdata
  1231. section already exists a new one is created that follows it which
  1232. screws of _SDA_BASE_ address calcs because output_offset != 0. */
  1233. struct elf_link_hash_entry *h;
  1234. struct bfd_link_hash_entry *bh;
  1235. asection *s = bfd_get_section_by_name (abfd, ".sdata");
  1236. /* The following code was cobbled from elf32-ppc.c and elflink.c. */
  1237. if (s == NULL)
  1238. {
  1239. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  1240. | SEC_IN_MEMORY | SEC_LINKER_CREATED);
  1241. s = bfd_make_section_anyway_with_flags (abfd, ".sdata",
  1242. flags);
  1243. if (s == NULL)
  1244. return false;
  1245. if (!bfd_set_section_alignment (s, 2))
  1246. return false;
  1247. }
  1248. bh = bfd_link_hash_lookup (info->hash, "_SDA_BASE_",
  1249. false, false, false);
  1250. if ((bh == NULL || bh->type == bfd_link_hash_undefined)
  1251. && !(_bfd_generic_link_add_one_symbol (info,
  1252. abfd,
  1253. "_SDA_BASE_",
  1254. BSF_GLOBAL,
  1255. s,
  1256. (bfd_vma) 32768,
  1257. NULL,
  1258. false,
  1259. get_elf_backend_data (abfd)->collect,
  1260. &bh)))
  1261. return false;
  1262. h = (struct elf_link_hash_entry *) bh;
  1263. h->type = STT_OBJECT;
  1264. }
  1265. switch (sym->st_shndx)
  1266. {
  1267. case SHN_M32R_SCOMMON:
  1268. *secp = bfd_make_section_old_way (abfd, ".scommon");
  1269. (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
  1270. *valp = sym->st_size;
  1271. break;
  1272. }
  1273. return true;
  1274. }
  1275. /* We have to figure out the SDA_BASE value, so that we can adjust the
  1276. symbol value correctly. We look up the symbol _SDA_BASE_ in the output
  1277. BFD. If we can't find it, we're stuck. We cache it in the ELF
  1278. target data. We don't need to adjust the symbol value for an
  1279. external symbol if we are producing relocatable output. */
  1280. static bfd_reloc_status_type
  1281. m32r_elf_final_sda_base (bfd *output_bfd,
  1282. struct bfd_link_info *info,
  1283. const char **error_message,
  1284. bfd_vma *psb)
  1285. {
  1286. if (elf_gp (output_bfd) == 0)
  1287. {
  1288. struct bfd_link_hash_entry *h;
  1289. h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
  1290. if (h != NULL && h->type == bfd_link_hash_defined)
  1291. elf_gp (output_bfd) = (h->u.def.value
  1292. + h->u.def.section->output_section->vma
  1293. + h->u.def.section->output_offset);
  1294. else
  1295. {
  1296. /* Only get the error once. */
  1297. *psb = elf_gp (output_bfd) = 4;
  1298. *error_message =
  1299. (const char *) _("SDA relocation when _SDA_BASE_ not defined");
  1300. return bfd_reloc_dangerous;
  1301. }
  1302. }
  1303. *psb = elf_gp (output_bfd);
  1304. return bfd_reloc_ok;
  1305. }
  1306. /* Return size of a PLT entry. */
  1307. #define elf_m32r_sizeof_plt(info) PLT_ENTRY_SIZE
  1308. /* The m32r linker needs to keep track of the number of relocs that it
  1309. decides to copy in check_relocs for each symbol. This is so that
  1310. it can discard PC relative relocs if it doesn't need them when
  1311. linking with -Bsymbolic. We store the information in a field
  1312. extending the regular ELF linker hash table. */
  1313. /* This structure keeps track of the number of PC relative relocs we
  1314. have copied for a given symbol. */
  1315. struct elf_m32r_pcrel_relocs_copied
  1316. {
  1317. /* Next section. */
  1318. struct elf_m32r_pcrel_relocs_copied *next;
  1319. /* A section in dynobj. */
  1320. asection *section;
  1321. /* Number of relocs copied in this section. */
  1322. bfd_size_type count;
  1323. };
  1324. /* Traverse an m32r ELF linker hash table. */
  1325. #define m32r_elf_link_hash_traverse(table, func, info) \
  1326. (elf_link_hash_traverse \
  1327. (&(table)->root, \
  1328. (bool (*) (struct elf_link_hash_entry *, void *)) (func), \
  1329. (info)))
  1330. /* Get the m32r ELF linker hash table from a link_info structure. */
  1331. #define m32r_elf_hash_table(p) \
  1332. ((is_elf_hash_table ((p)->hash) \
  1333. && elf_hash_table_id (elf_hash_table (p)) == M32R_ELF_DATA) \
  1334. ? (struct elf_link_hash_table *) (p)->hash : NULL)
  1335. /* Create an m32r ELF linker hash table. */
  1336. static struct bfd_link_hash_table *
  1337. m32r_elf_link_hash_table_create (bfd *abfd)
  1338. {
  1339. struct elf_link_hash_table *ret;
  1340. size_t amt = sizeof (struct elf_link_hash_table);
  1341. ret = bfd_zmalloc (amt);
  1342. if (ret == NULL)
  1343. return NULL;
  1344. if (!_bfd_elf_link_hash_table_init (ret, abfd,
  1345. _bfd_elf_link_hash_newfunc,
  1346. sizeof (struct elf_link_hash_entry),
  1347. M32R_ELF_DATA))
  1348. {
  1349. free (ret);
  1350. return NULL;
  1351. }
  1352. return &ret->root;
  1353. }
  1354. /* Create dynamic sections when linking against a dynamic object. */
  1355. static bool
  1356. m32r_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
  1357. {
  1358. struct elf_link_hash_table *htab;
  1359. flagword flags, pltflags;
  1360. asection *s;
  1361. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1362. int ptralign = 2; /* 32bit */
  1363. htab = m32r_elf_hash_table (info);
  1364. if (htab == NULL)
  1365. return false;
  1366. /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
  1367. .rel[a].bss sections. */
  1368. flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  1369. | SEC_LINKER_CREATED);
  1370. pltflags = flags;
  1371. pltflags |= SEC_CODE;
  1372. if (bed->plt_not_loaded)
  1373. pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
  1374. if (bed->plt_readonly)
  1375. pltflags |= SEC_READONLY;
  1376. s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
  1377. htab->splt = s;
  1378. if (s == NULL
  1379. || !bfd_set_section_alignment (s, bed->plt_alignment))
  1380. return false;
  1381. if (bed->want_plt_sym)
  1382. {
  1383. /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
  1384. .plt section. */
  1385. struct bfd_link_hash_entry *bh = NULL;
  1386. struct elf_link_hash_entry *h;
  1387. if (! (_bfd_generic_link_add_one_symbol
  1388. (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
  1389. (bfd_vma) 0, NULL, false,
  1390. get_elf_backend_data (abfd)->collect, &bh)))
  1391. return false;
  1392. h = (struct elf_link_hash_entry *) bh;
  1393. h->def_regular = 1;
  1394. h->type = STT_OBJECT;
  1395. htab->hplt = h;
  1396. if (bfd_link_pic (info)
  1397. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  1398. return false;
  1399. }
  1400. s = bfd_make_section_anyway_with_flags (abfd,
  1401. bed->default_use_rela_p
  1402. ? ".rela.plt" : ".rel.plt",
  1403. flags | SEC_READONLY);
  1404. htab->srelplt = s;
  1405. if (s == NULL
  1406. || !bfd_set_section_alignment (s, ptralign))
  1407. return false;
  1408. if (htab->sgot == NULL
  1409. && !_bfd_elf_create_got_section (abfd, info))
  1410. return false;
  1411. if (bed->want_dynbss)
  1412. {
  1413. /* The .dynbss section is a place to put symbols which are defined
  1414. by dynamic objects, are referenced by regular objects, and are
  1415. not functions. We must allocate space for them in the process
  1416. image and use a R_*_COPY reloc to tell the dynamic linker to
  1417. initialize them at run time. The linker script puts the .dynbss
  1418. section into the .bss section of the final image. */
  1419. s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
  1420. SEC_ALLOC | SEC_LINKER_CREATED);
  1421. htab->sdynbss = s;
  1422. if (s == NULL)
  1423. return false;
  1424. /* The .rel[a].bss section holds copy relocs. This section is not
  1425. normally needed. We need to create it here, though, so that the
  1426. linker will map it to an output section. We can't just create it
  1427. only if we need it, because we will not know whether we need it
  1428. until we have seen all the input files, and the first time the
  1429. main linker code calls BFD after examining all the input files
  1430. (size_dynamic_sections) the input sections have already been
  1431. mapped to the output sections. If the section turns out not to
  1432. be needed, we can discard it later. We will never need this
  1433. section when generating a shared object, since they do not use
  1434. copy relocs. */
  1435. if (! bfd_link_pic (info))
  1436. {
  1437. s = bfd_make_section_anyway_with_flags (abfd,
  1438. (bed->default_use_rela_p
  1439. ? ".rela.bss" : ".rel.bss"),
  1440. flags | SEC_READONLY);
  1441. htab->srelbss = s;
  1442. if (s == NULL
  1443. || !bfd_set_section_alignment (s, ptralign))
  1444. return false;
  1445. }
  1446. }
  1447. return true;
  1448. }
  1449. /* Adjust a symbol defined by a dynamic object and referenced by a
  1450. regular object. The current definition is in some section of the
  1451. dynamic object, but we're not including those sections. We have to
  1452. change the definition to something the rest of the link can
  1453. understand. */
  1454. static bool
  1455. m32r_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  1456. struct elf_link_hash_entry *h)
  1457. {
  1458. struct elf_link_hash_table *htab;
  1459. bfd *dynobj;
  1460. asection *s;
  1461. #ifdef DEBUG_PIC
  1462. printf ("m32r_elf_adjust_dynamic_symbol()\n");
  1463. #endif
  1464. dynobj = elf_hash_table (info)->dynobj;
  1465. /* Make sure we know what is going on here. */
  1466. BFD_ASSERT (dynobj != NULL
  1467. && (h->needs_plt
  1468. || h->is_weakalias
  1469. || (h->def_dynamic
  1470. && h->ref_regular
  1471. && !h->def_regular)));
  1472. /* If this is a function, put it in the procedure linkage table. We
  1473. will fill in the contents of the procedure linkage table later,
  1474. when we know the address of the .got section. */
  1475. if (h->type == STT_FUNC
  1476. || h->needs_plt)
  1477. {
  1478. if (! bfd_link_pic (info)
  1479. && !h->def_dynamic
  1480. && !h->ref_dynamic
  1481. && h->root.type != bfd_link_hash_undefweak
  1482. && h->root.type != bfd_link_hash_undefined)
  1483. {
  1484. /* This case can occur if we saw a PLT reloc in an input
  1485. file, but the symbol was never referred to by a dynamic
  1486. object. In such a case, we don't actually need to build
  1487. a procedure linkage table, and we can just do a PCREL
  1488. reloc instead. */
  1489. h->plt.offset = (bfd_vma) -1;
  1490. h->needs_plt = 0;
  1491. }
  1492. return true;
  1493. }
  1494. else
  1495. h->plt.offset = (bfd_vma) -1;
  1496. /* If this is a weak symbol, and there is a real definition, the
  1497. processor independent code will have arranged for us to see the
  1498. real definition first, and we can just use the same value. */
  1499. if (h->is_weakalias)
  1500. {
  1501. struct elf_link_hash_entry *def = weakdef (h);
  1502. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  1503. h->root.u.def.section = def->root.u.def.section;
  1504. h->root.u.def.value = def->root.u.def.value;
  1505. return true;
  1506. }
  1507. /* This is a reference to a symbol defined by a dynamic object which
  1508. is not a function. */
  1509. /* If we are creating a shared library, we must presume that the
  1510. only references to the symbol are via the global offset table.
  1511. For such cases we need not do anything here; the relocations will
  1512. be handled correctly by relocate_section. */
  1513. if (bfd_link_pic (info))
  1514. return true;
  1515. /* If there are no references to this symbol that do not use the
  1516. GOT, we don't need to generate a copy reloc. */
  1517. if (!h->non_got_ref)
  1518. return true;
  1519. /* If -z nocopyreloc was given, we won't generate them either. */
  1520. if (0 && info->nocopyreloc)
  1521. {
  1522. h->non_got_ref = 0;
  1523. return true;
  1524. }
  1525. /* If we don't find any dynamic relocs in read-only sections, then
  1526. we'll be keeping the dynamic relocs and avoiding the copy reloc. */
  1527. if (0 && !_bfd_elf_readonly_dynrelocs (h))
  1528. {
  1529. h->non_got_ref = 0;
  1530. return true;
  1531. }
  1532. /* We must allocate the symbol in our .dynbss section, which will
  1533. become part of the .bss section of the executable. There will be
  1534. an entry for this symbol in the .dynsym section. The dynamic
  1535. object will contain position independent code, so all references
  1536. from the dynamic object to this symbol will go through the global
  1537. offset table. The dynamic linker will use the .dynsym entry to
  1538. determine the address it must put in the global offset table, so
  1539. both the dynamic object and the regular object will refer to the
  1540. same memory location for the variable. */
  1541. htab = m32r_elf_hash_table (info);
  1542. if (htab == NULL)
  1543. return false;
  1544. s = htab->sdynbss;
  1545. BFD_ASSERT (s != NULL);
  1546. /* We must generate a R_M32R_COPY reloc to tell the dynamic linker
  1547. to copy the initial value out of the dynamic object and into the
  1548. runtime process image. We need to remember the offset into the
  1549. .rela.bss section we are going to use. */
  1550. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  1551. {
  1552. asection *srel;
  1553. srel = htab->srelbss;
  1554. BFD_ASSERT (srel != NULL);
  1555. srel->size += sizeof (Elf32_External_Rela);
  1556. h->needs_copy = 1;
  1557. }
  1558. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  1559. }
  1560. /* Allocate space in .plt, .got and associated reloc sections for
  1561. dynamic relocs. */
  1562. static bool
  1563. allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
  1564. {
  1565. struct bfd_link_info *info;
  1566. struct elf_link_hash_table *htab;
  1567. struct elf_dyn_relocs *p;
  1568. if (h->root.type == bfd_link_hash_indirect)
  1569. return true;
  1570. info = (struct bfd_link_info *) inf;
  1571. htab = m32r_elf_hash_table (info);
  1572. if (htab == NULL)
  1573. return false;
  1574. if (htab->dynamic_sections_created
  1575. && h->plt.refcount > 0)
  1576. {
  1577. /* Make sure this symbol is output as a dynamic symbol.
  1578. Undefined weak syms won't yet be marked as dynamic. */
  1579. if (h->dynindx == -1
  1580. && !h->forced_local)
  1581. {
  1582. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1583. return false;
  1584. }
  1585. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
  1586. {
  1587. asection *s = htab->splt;
  1588. /* If this is the first .plt entry, make room for the special
  1589. first entry. */
  1590. if (s->size == 0)
  1591. s->size += PLT_ENTRY_SIZE;
  1592. h->plt.offset = s->size;
  1593. /* If this symbol is not defined in a regular file, and we are
  1594. not generating a shared library, then set the symbol to this
  1595. location in the .plt. This is required to make function
  1596. pointers compare as equal between the normal executable and
  1597. the shared library. */
  1598. if (! bfd_link_pic (info)
  1599. && !h->def_regular)
  1600. {
  1601. h->root.u.def.section = s;
  1602. h->root.u.def.value = h->plt.offset;
  1603. }
  1604. /* Make room for this entry. */
  1605. s->size += PLT_ENTRY_SIZE;
  1606. /* We also need to make an entry in the .got.plt section, which
  1607. will be placed in the .got section by the linker script. */
  1608. htab->sgotplt->size += 4;
  1609. /* We also need to make an entry in the .rel.plt section. */
  1610. htab->srelplt->size += sizeof (Elf32_External_Rela);
  1611. }
  1612. else
  1613. {
  1614. h->plt.offset = (bfd_vma) -1;
  1615. h->needs_plt = 0;
  1616. }
  1617. }
  1618. else
  1619. {
  1620. h->plt.offset = (bfd_vma) -1;
  1621. h->needs_plt = 0;
  1622. }
  1623. if (h->got.refcount > 0)
  1624. {
  1625. asection *s;
  1626. bool dyn;
  1627. /* Make sure this symbol is output as a dynamic symbol.
  1628. Undefined weak syms won't yet be marked as dynamic. */
  1629. if (h->dynindx == -1
  1630. && !h->forced_local)
  1631. {
  1632. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1633. return false;
  1634. }
  1635. s = htab->sgot;
  1636. h->got.offset = s->size;
  1637. s->size += 4;
  1638. dyn = htab->dynamic_sections_created;
  1639. if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
  1640. htab->srelgot->size += sizeof (Elf32_External_Rela);
  1641. }
  1642. else
  1643. h->got.offset = (bfd_vma) -1;
  1644. if (h->dyn_relocs == NULL)
  1645. return true;
  1646. /* In the shared -Bsymbolic case, discard space allocated for
  1647. dynamic pc-relative relocs against symbols which turn out to be
  1648. defined in regular objects. For the normal shared case, discard
  1649. space for pc-relative relocs that have become local due to symbol
  1650. visibility changes. */
  1651. if (bfd_link_pic (info))
  1652. {
  1653. if (h->def_regular
  1654. && (h->forced_local
  1655. || info->symbolic))
  1656. {
  1657. struct elf_dyn_relocs **pp;
  1658. for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
  1659. {
  1660. p->count -= p->pc_count;
  1661. p->pc_count = 0;
  1662. if (p->count == 0)
  1663. *pp = p->next;
  1664. else
  1665. pp = &p->next;
  1666. }
  1667. }
  1668. /* Also discard relocs on undefined weak syms with non-default
  1669. visibility. */
  1670. if (h->dyn_relocs != NULL
  1671. && h->root.type == bfd_link_hash_undefweak)
  1672. {
  1673. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
  1674. h->dyn_relocs = NULL;
  1675. /* Make sure undefined weak symbols are output as a dynamic
  1676. symbol in PIEs. */
  1677. else if (h->dynindx == -1
  1678. && !h->forced_local)
  1679. {
  1680. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1681. return false;
  1682. }
  1683. }
  1684. }
  1685. else
  1686. {
  1687. /* For the non-shared case, discard space for relocs against
  1688. symbols which turn out to need copy relocs or are not
  1689. dynamic. */
  1690. if (!h->non_got_ref
  1691. && ((h->def_dynamic
  1692. && !h->def_regular)
  1693. || (htab->dynamic_sections_created
  1694. && (h->root.type == bfd_link_hash_undefweak
  1695. || h->root.type == bfd_link_hash_undefined))))
  1696. {
  1697. /* Make sure this symbol is output as a dynamic symbol.
  1698. Undefined weak syms won't yet be marked as dynamic. */
  1699. if (h->dynindx == -1
  1700. && !h->forced_local)
  1701. {
  1702. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  1703. return false;
  1704. }
  1705. /* If that succeeded, we know we'll be keeping all the
  1706. relocs. */
  1707. if (h->dynindx != -1)
  1708. goto keep;
  1709. }
  1710. h->dyn_relocs = NULL;
  1711. keep: ;
  1712. }
  1713. /* Finally, allocate space. */
  1714. for (p = h->dyn_relocs; p != NULL; p = p->next)
  1715. {
  1716. asection *sreloc = elf_section_data (p->sec)->sreloc;
  1717. sreloc->size += p->count * sizeof (Elf32_External_Rela);
  1718. }
  1719. return true;
  1720. }
  1721. /* Set the sizes of the dynamic sections. */
  1722. static bool
  1723. m32r_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  1724. struct bfd_link_info *info)
  1725. {
  1726. struct elf_link_hash_table *htab;
  1727. bfd *dynobj;
  1728. asection *s;
  1729. bool relocs;
  1730. bfd *ibfd;
  1731. #ifdef DEBUG_PIC
  1732. printf ("m32r_elf_size_dynamic_sections()\n");
  1733. #endif
  1734. htab = m32r_elf_hash_table (info);
  1735. if (htab == NULL)
  1736. return false;
  1737. dynobj = htab->dynobj;
  1738. BFD_ASSERT (dynobj != NULL);
  1739. if (htab->dynamic_sections_created)
  1740. {
  1741. /* Set the contents of the .interp section to the interpreter. */
  1742. if (bfd_link_executable (info) && !info->nointerp)
  1743. {
  1744. s = bfd_get_linker_section (dynobj, ".interp");
  1745. BFD_ASSERT (s != NULL);
  1746. s->size = sizeof ELF_DYNAMIC_INTERPRETER;
  1747. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  1748. }
  1749. }
  1750. /* Set up .got offsets for local syms, and space for local dynamic
  1751. relocs. */
  1752. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1753. {
  1754. bfd_signed_vma *local_got;
  1755. bfd_signed_vma *end_local_got;
  1756. bfd_size_type locsymcount;
  1757. Elf_Internal_Shdr *symtab_hdr;
  1758. asection *srel;
  1759. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  1760. continue;
  1761. for (s = ibfd->sections; s != NULL; s = s->next)
  1762. {
  1763. struct elf_dyn_relocs *p;
  1764. for (p = ((struct elf_dyn_relocs *)
  1765. elf_section_data (s)->local_dynrel);
  1766. p != NULL;
  1767. p = p->next)
  1768. {
  1769. if (! bfd_is_abs_section (p->sec)
  1770. && bfd_is_abs_section (p->sec->output_section))
  1771. {
  1772. /* Input section has been discarded, either because
  1773. it is a copy of a linkonce section or due to
  1774. linker script /DISCARD/, so we'll be discarding
  1775. the relocs too. */
  1776. }
  1777. else if (p->count != 0)
  1778. {
  1779. srel = elf_section_data (p->sec)->sreloc;
  1780. srel->size += p->count * sizeof (Elf32_External_Rela);
  1781. if ((p->sec->output_section->flags & SEC_READONLY) != 0)
  1782. info->flags |= DF_TEXTREL;
  1783. }
  1784. }
  1785. }
  1786. local_got = elf_local_got_refcounts (ibfd);
  1787. if (!local_got)
  1788. continue;
  1789. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  1790. locsymcount = symtab_hdr->sh_info;
  1791. end_local_got = local_got + locsymcount;
  1792. s = htab->sgot;
  1793. srel = htab->srelgot;
  1794. for (; local_got < end_local_got; ++local_got)
  1795. {
  1796. if (*local_got > 0)
  1797. {
  1798. *local_got = s->size;
  1799. s->size += 4;
  1800. if (bfd_link_pic (info))
  1801. srel->size += sizeof (Elf32_External_Rela);
  1802. }
  1803. else
  1804. *local_got = (bfd_vma) -1;
  1805. }
  1806. }
  1807. /* Allocate global sym .plt and .got entries, and space for global
  1808. sym dynamic relocs. */
  1809. elf_link_hash_traverse (htab, allocate_dynrelocs, info);
  1810. /* We now have determined the sizes of the various dynamic sections.
  1811. Allocate memory for them. */
  1812. relocs = false;
  1813. for (s = dynobj->sections; s != NULL; s = s->next)
  1814. {
  1815. if ((s->flags & SEC_LINKER_CREATED) == 0)
  1816. continue;
  1817. if (s == htab->splt
  1818. || s == htab->sgot
  1819. || s == htab->sgotplt
  1820. || s == htab->sdynbss)
  1821. {
  1822. /* Strip this section if we don't need it; see the
  1823. comment below. */
  1824. }
  1825. else if (startswith (bfd_section_name (s), ".rela"))
  1826. {
  1827. if (s->size != 0 && s != htab->srelplt)
  1828. relocs = true;
  1829. /* We use the reloc_count field as a counter if we need
  1830. to copy relocs into the output file. */
  1831. s->reloc_count = 0;
  1832. }
  1833. else
  1834. /* It's not one of our sections, so don't allocate space. */
  1835. continue;
  1836. if (s->size == 0)
  1837. {
  1838. /* If we don't need this section, strip it from the
  1839. output file. This is mostly to handle .rela.bss and
  1840. .rela.plt. We must create both sections in
  1841. create_dynamic_sections, because they must be created
  1842. before the linker maps input sections to output
  1843. sections. The linker does that before
  1844. adjust_dynamic_symbol is called, and it is that
  1845. function which decides whether anything needs to go
  1846. into these sections. */
  1847. s->flags |= SEC_EXCLUDE;
  1848. continue;
  1849. }
  1850. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  1851. continue;
  1852. /* Allocate memory for the section contents. We use bfd_zalloc
  1853. here in case unused entries are not reclaimed before the
  1854. section's contents are written out. This should not happen,
  1855. but this way if it does, we get a R_M32R_NONE reloc instead
  1856. of garbage. */
  1857. s->contents = bfd_zalloc (dynobj, s->size);
  1858. if (s->contents == NULL)
  1859. return false;
  1860. }
  1861. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
  1862. }
  1863. /* Relocate an M32R/D ELF section.
  1864. There is some attempt to make this function usable for many architectures,
  1865. both for RELA and REL type relocs, if only to serve as a learning tool.
  1866. The RELOCATE_SECTION function is called by the new ELF backend linker
  1867. to handle the relocations for a section.
  1868. The relocs are always passed as Rela structures; if the section
  1869. actually uses Rel structures, the r_addend field will always be
  1870. zero.
  1871. This function is responsible for adjust the section contents as
  1872. necessary, and (if using Rela relocs and generating a
  1873. relocatable output file) adjusting the reloc addend as
  1874. necessary.
  1875. This function does not have to worry about setting the reloc
  1876. address or the reloc symbol index.
  1877. LOCAL_SYMS is a pointer to the swapped in local symbols.
  1878. LOCAL_SECTIONS is an array giving the section in the input file
  1879. corresponding to the st_shndx field of each local symbol.
  1880. The global hash table entry for the global symbols can be found
  1881. via elf_sym_hashes (input_bfd).
  1882. When generating relocatable output, this function must handle
  1883. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  1884. going to be the section symbol corresponding to the output
  1885. section, which means that the addend must be adjusted
  1886. accordingly. */
  1887. static int
  1888. m32r_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  1889. struct bfd_link_info *info,
  1890. bfd *input_bfd,
  1891. asection *input_section,
  1892. bfd_byte *contents,
  1893. Elf_Internal_Rela *relocs,
  1894. Elf_Internal_Sym *local_syms,
  1895. asection **local_sections)
  1896. {
  1897. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1898. struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
  1899. Elf_Internal_Rela *rel, *relend;
  1900. /* Assume success. */
  1901. bool ret = true;
  1902. struct elf_link_hash_table *htab = m32r_elf_hash_table (info);
  1903. bfd_vma *local_got_offsets;
  1904. asection *sgot, *splt, *sreloc;
  1905. bfd_vma high_address = bfd_get_section_limit (input_bfd, input_section);
  1906. if (htab == NULL)
  1907. return false;
  1908. local_got_offsets = elf_local_got_offsets (input_bfd);
  1909. sgot = htab->sgot;
  1910. splt = htab->splt;
  1911. sreloc = NULL;
  1912. rel = relocs;
  1913. relend = relocs + input_section->reloc_count;
  1914. for (; rel < relend; rel++)
  1915. {
  1916. int r_type;
  1917. reloc_howto_type *howto;
  1918. unsigned long r_symndx;
  1919. struct elf_link_hash_entry *h;
  1920. /* We can't modify r_addend here as elf_link_input_bfd has an assert to
  1921. ensure it's zero (we use REL relocs, not RELA). Therefore this
  1922. should be assigning zero to `addend', but for clarity we use
  1923. `r_addend'. */
  1924. bfd_vma addend = rel->r_addend;
  1925. bfd_vma offset = rel->r_offset;
  1926. bfd_vma relocation;
  1927. Elf_Internal_Sym *sym;
  1928. asection *sec;
  1929. const char *sym_name;
  1930. bfd_reloc_status_type r;
  1931. const char *errmsg = NULL;
  1932. bool use_rel = false;
  1933. h = NULL;
  1934. r_type = ELF32_R_TYPE (rel->r_info);
  1935. if (r_type < 0 || r_type >= (int) R_M32R_max)
  1936. {
  1937. /* xgettext:c-format */
  1938. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  1939. input_bfd, (int) r_type);
  1940. bfd_set_error (bfd_error_bad_value);
  1941. ret = false;
  1942. continue;
  1943. }
  1944. if ( r_type == R_M32R_GNU_VTENTRY
  1945. || r_type == R_M32R_GNU_VTINHERIT
  1946. || r_type == R_M32R_NONE
  1947. || r_type == R_M32R_RELA_GNU_VTENTRY
  1948. || r_type == R_M32R_RELA_GNU_VTINHERIT)
  1949. continue;
  1950. if (r_type <= R_M32R_GNU_VTENTRY)
  1951. use_rel = true;
  1952. howto = m32r_elf_howto_table + r_type;
  1953. r_symndx = ELF32_R_SYM (rel->r_info);
  1954. sym = NULL;
  1955. sec = NULL;
  1956. h = NULL;
  1957. if (r_symndx < symtab_hdr->sh_info)
  1958. {
  1959. /* Local symbol. */
  1960. sym = local_syms + r_symndx;
  1961. sec = local_sections[r_symndx];
  1962. sym_name = "<local symbol>";
  1963. if (!use_rel)
  1964. {
  1965. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1966. addend = rel->r_addend;
  1967. }
  1968. else
  1969. {
  1970. relocation = (sec->output_section->vma
  1971. + sec->output_offset
  1972. + sym->st_value);
  1973. }
  1974. }
  1975. else
  1976. {
  1977. /* External symbol. */
  1978. relocation = 0;
  1979. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1980. if (info->wrap_hash != NULL
  1981. && (input_section->flags & SEC_DEBUGGING) != 0)
  1982. h = ((struct elf_link_hash_entry *)
  1983. unwrap_hash_lookup (info, input_bfd, &h->root));
  1984. while (h->root.type == bfd_link_hash_indirect
  1985. || h->root.type == bfd_link_hash_warning)
  1986. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1987. sym_name = h->root.root.string;
  1988. if (h->root.type == bfd_link_hash_defined
  1989. || h->root.type == bfd_link_hash_defweak)
  1990. {
  1991. bool dyn;
  1992. sec = h->root.u.def.section;
  1993. dyn = htab->dynamic_sections_created;
  1994. sec = h->root.u.def.section;
  1995. if (r_type == R_M32R_GOTPC24
  1996. || (r_type == R_M32R_GOTPC_HI_ULO
  1997. || r_type == R_M32R_GOTPC_HI_SLO
  1998. || r_type == R_M32R_GOTPC_LO)
  1999. || (r_type == R_M32R_26_PLTREL
  2000. && h->plt.offset != (bfd_vma) -1)
  2001. || ((r_type == R_M32R_GOT24
  2002. || r_type == R_M32R_GOT16_HI_ULO
  2003. || r_type == R_M32R_GOT16_HI_SLO
  2004. || r_type == R_M32R_GOT16_LO)
  2005. && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  2006. bfd_link_pic (info),
  2007. h)
  2008. && (! bfd_link_pic (info)
  2009. || (! info->symbolic && h->dynindx != -1)
  2010. || !h->def_regular))
  2011. || (bfd_link_pic (info)
  2012. && ((! info->symbolic && h->dynindx != -1)
  2013. || !h->def_regular)
  2014. && (((r_type == R_M32R_16_RELA
  2015. || r_type == R_M32R_32_RELA
  2016. || r_type == R_M32R_24_RELA
  2017. || r_type == R_M32R_HI16_ULO_RELA
  2018. || r_type == R_M32R_HI16_SLO_RELA
  2019. || r_type == R_M32R_LO16_RELA)
  2020. && !h->forced_local)
  2021. || r_type == R_M32R_REL32
  2022. || r_type == R_M32R_10_PCREL_RELA
  2023. || r_type == R_M32R_18_PCREL_RELA
  2024. || r_type == R_M32R_26_PCREL_RELA)
  2025. && ((input_section->flags & SEC_ALLOC) != 0
  2026. /* DWARF will emit R_M32R_16(24,32) relocations
  2027. in its sections against symbols defined
  2028. externally in shared libraries. We can't do
  2029. anything with them here. */
  2030. || ((input_section->flags & SEC_DEBUGGING) != 0
  2031. && h->def_dynamic))))
  2032. {
  2033. /* In these cases, we don't need the relocation
  2034. value. We check specially because in some
  2035. obscure cases sec->output_section will be NULL. */
  2036. }
  2037. else if (sec->output_section != NULL)
  2038. relocation = (h->root.u.def.value
  2039. + sec->output_section->vma
  2040. + sec->output_offset);
  2041. else if (!bfd_link_relocatable (info)
  2042. && (_bfd_elf_section_offset (output_bfd, info,
  2043. input_section,
  2044. rel->r_offset)
  2045. != (bfd_vma) -1))
  2046. {
  2047. _bfd_error_handler
  2048. /* xgettext:c-format */
  2049. (_("%pB(%pA+%#" PRIx64 "): unresolvable %s relocation "
  2050. "against symbol `%s'"),
  2051. input_bfd,
  2052. input_section,
  2053. (uint64_t) rel->r_offset,
  2054. howto->name,
  2055. h->root.root.string);
  2056. }
  2057. }
  2058. else if (h->root.type == bfd_link_hash_undefweak)
  2059. ;
  2060. else if (info->unresolved_syms_in_objects == RM_IGNORE
  2061. && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
  2062. ;
  2063. else if (!bfd_link_relocatable (info))
  2064. info->callbacks->undefined_symbol
  2065. (info, h->root.root.string, input_bfd, input_section, offset,
  2066. (info->unresolved_syms_in_objects == RM_DIAGNOSE
  2067. && !info->warn_unresolved_syms)
  2068. || ELF_ST_VISIBILITY (h->other));
  2069. }
  2070. if (sec != NULL && discarded_section (sec))
  2071. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  2072. rel, 1, relend, howto, 0, contents);
  2073. if (bfd_link_relocatable (info) && !use_rel)
  2074. {
  2075. /* This is a relocatable link. We don't have to change
  2076. anything, unless the reloc is against a section symbol,
  2077. in which case we have to adjust according to where the
  2078. section symbol winds up in the output section. */
  2079. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  2080. rel->r_addend += sec->output_offset;
  2081. continue;
  2082. }
  2083. if (bfd_link_relocatable (info) && use_rel)
  2084. {
  2085. /* This is a relocatable link. We don't have to change
  2086. anything, unless the reloc is against a section symbol,
  2087. in which case we have to adjust according to where the
  2088. section symbol winds up in the output section. */
  2089. if (sym == NULL || ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  2090. continue;
  2091. addend += sec->output_offset;
  2092. /* If partial_inplace, we need to store any additional addend
  2093. back in the section. */
  2094. if (! howto->partial_inplace)
  2095. continue;
  2096. /* ??? Here is a nice place to call a special_function
  2097. like handler. */
  2098. if (r_type != R_M32R_HI16_SLO && r_type != R_M32R_HI16_ULO)
  2099. r = _bfd_relocate_contents (howto, input_bfd,
  2100. addend, contents + offset);
  2101. else
  2102. {
  2103. Elf_Internal_Rela *lorel;
  2104. /* We allow an arbitrary number of HI16 relocs before the
  2105. LO16 reloc. This permits gcc to emit the HI and LO relocs
  2106. itself. */
  2107. for (lorel = rel + 1;
  2108. (lorel < relend
  2109. && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
  2110. || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
  2111. lorel++)
  2112. continue;
  2113. if (lorel < relend
  2114. && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
  2115. {
  2116. m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
  2117. contents, addend);
  2118. r = bfd_reloc_ok;
  2119. }
  2120. else
  2121. r = _bfd_relocate_contents (howto, input_bfd,
  2122. addend, contents + offset);
  2123. }
  2124. }
  2125. else
  2126. {
  2127. /* Sanity check the address. */
  2128. if (offset > high_address)
  2129. {
  2130. r = bfd_reloc_outofrange;
  2131. goto check_reloc;
  2132. }
  2133. switch ((int) r_type)
  2134. {
  2135. case R_M32R_GOTOFF:
  2136. /* Relocation is relative to the start of the global offset
  2137. table (for ld24 rx, #uimm24). eg access at label+addend
  2138. ld24 rx. #label@GOTOFF + addend
  2139. sub rx, r12. */
  2140. BFD_ASSERT (sgot != NULL);
  2141. relocation = -(relocation - sgot->output_section->vma);
  2142. rel->r_addend = -rel->r_addend;
  2143. break;
  2144. case R_M32R_GOTOFF_HI_ULO:
  2145. case R_M32R_GOTOFF_HI_SLO:
  2146. case R_M32R_GOTOFF_LO:
  2147. BFD_ASSERT (sgot != NULL);
  2148. relocation -= sgot->output_section->vma;
  2149. if ((r_type == R_M32R_GOTOFF_HI_SLO)
  2150. && ((relocation + rel->r_addend) & 0x8000))
  2151. rel->r_addend += 0x10000;
  2152. break;
  2153. case R_M32R_GOTPC24:
  2154. /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
  2155. ld24 rx,#_GLOBAL_OFFSET_TABLE_
  2156. */
  2157. relocation = sgot->output_section->vma;
  2158. break;
  2159. case R_M32R_GOTPC_HI_ULO:
  2160. case R_M32R_GOTPC_HI_SLO:
  2161. case R_M32R_GOTPC_LO:
  2162. {
  2163. /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
  2164. bl .+4
  2165. seth rx,#high(_GLOBAL_OFFSET_TABLE_)
  2166. or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
  2167. or
  2168. bl .+4
  2169. seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
  2170. add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
  2171. */
  2172. relocation = sgot->output_section->vma;
  2173. relocation -= (input_section->output_section->vma
  2174. + input_section->output_offset
  2175. + rel->r_offset);
  2176. if ((r_type == R_M32R_GOTPC_HI_SLO)
  2177. && ((relocation + rel->r_addend) & 0x8000))
  2178. rel->r_addend += 0x10000;
  2179. break;
  2180. }
  2181. case R_M32R_GOT16_HI_ULO:
  2182. case R_M32R_GOT16_HI_SLO:
  2183. case R_M32R_GOT16_LO:
  2184. /* Fall through. */
  2185. case R_M32R_GOT24:
  2186. /* Relocation is to the entry for this symbol in the global
  2187. offset table. */
  2188. BFD_ASSERT (sgot != NULL);
  2189. if (h != NULL)
  2190. {
  2191. bool dyn;
  2192. bfd_vma off;
  2193. off = h->got.offset;
  2194. BFD_ASSERT (off != (bfd_vma) -1);
  2195. dyn = htab->dynamic_sections_created;
  2196. if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
  2197. bfd_link_pic (info),
  2198. h)
  2199. || (bfd_link_pic (info)
  2200. && (info->symbolic
  2201. || h->dynindx == -1
  2202. || h->forced_local)
  2203. && h->def_regular))
  2204. {
  2205. /* This is actually a static link, or it is a
  2206. -Bsymbolic link and the symbol is defined
  2207. locally, or the symbol was forced to be local
  2208. because of a version file. We must initialize
  2209. this entry in the global offset table. Since the
  2210. offset must always be a multiple of 4, we use the
  2211. least significant bit to record whether we have
  2212. initialized it already.
  2213. When doing a dynamic link, we create a .rela.got
  2214. relocation entry to initialize the value. This
  2215. is done in the finish_dynamic_symbol routine. */
  2216. if ((off & 1) != 0)
  2217. off &= ~1;
  2218. else
  2219. {
  2220. bfd_put_32 (output_bfd, relocation,
  2221. sgot->contents + off);
  2222. h->got.offset |= 1;
  2223. }
  2224. }
  2225. relocation = sgot->output_offset + off;
  2226. }
  2227. else
  2228. {
  2229. bfd_vma off;
  2230. bfd_byte *loc;
  2231. BFD_ASSERT (local_got_offsets != NULL
  2232. && local_got_offsets[r_symndx] != (bfd_vma) -1);
  2233. off = local_got_offsets[r_symndx];
  2234. /* The offset must always be a multiple of 4. We use
  2235. the least significant bit to record whether we have
  2236. already processed this entry. */
  2237. if ((off & 1) != 0)
  2238. off &= ~1;
  2239. else
  2240. {
  2241. bfd_put_32 (output_bfd, relocation, sgot->contents + off);
  2242. if (bfd_link_pic (info))
  2243. {
  2244. asection *srelgot;
  2245. Elf_Internal_Rela outrel;
  2246. /* We need to generate a R_M32R_RELATIVE reloc
  2247. for the dynamic linker. */
  2248. srelgot = htab->srelgot;
  2249. BFD_ASSERT (srelgot != NULL);
  2250. outrel.r_offset = (sgot->output_section->vma
  2251. + sgot->output_offset
  2252. + off);
  2253. outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
  2254. outrel.r_addend = relocation;
  2255. loc = srelgot->contents;
  2256. loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
  2257. bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
  2258. ++srelgot->reloc_count;
  2259. }
  2260. local_got_offsets[r_symndx] |= 1;
  2261. }
  2262. relocation = sgot->output_offset + off;
  2263. }
  2264. if ((r_type == R_M32R_GOT16_HI_SLO)
  2265. && ((relocation + rel->r_addend) & 0x8000))
  2266. rel->r_addend += 0x10000;
  2267. break;
  2268. case R_M32R_26_PLTREL:
  2269. /* Relocation is to the entry for this symbol in the
  2270. procedure linkage table. */
  2271. /* The native assembler will generate a 26_PLTREL reloc
  2272. for a local symbol if you assemble a call from one
  2273. section to another when using -K pic. */
  2274. if (h == NULL)
  2275. break;
  2276. if (h->forced_local)
  2277. break;
  2278. if (h->plt.offset == (bfd_vma) -1)
  2279. /* We didn't make a PLT entry for this symbol. This
  2280. happens when statically linking PIC code, or when
  2281. using -Bsymbolic. */
  2282. break;
  2283. relocation = (splt->output_section->vma
  2284. + splt->output_offset
  2285. + h->plt.offset);
  2286. break;
  2287. case R_M32R_HI16_SLO_RELA:
  2288. if ((relocation + rel->r_addend) & 0x8000)
  2289. rel->r_addend += 0x10000;
  2290. /* Fall through. */
  2291. case R_M32R_16_RELA:
  2292. case R_M32R_24_RELA:
  2293. case R_M32R_32_RELA:
  2294. case R_M32R_REL32:
  2295. case R_M32R_10_PCREL_RELA:
  2296. case R_M32R_18_PCREL_RELA:
  2297. case R_M32R_26_PCREL_RELA:
  2298. case R_M32R_HI16_ULO_RELA:
  2299. case R_M32R_LO16_RELA:
  2300. if (bfd_link_pic (info)
  2301. && r_symndx != STN_UNDEF
  2302. && (input_section->flags & SEC_ALLOC) != 0
  2303. && (( r_type != R_M32R_10_PCREL_RELA
  2304. && r_type != R_M32R_18_PCREL_RELA
  2305. && r_type != R_M32R_26_PCREL_RELA
  2306. && r_type != R_M32R_REL32)
  2307. || (h != NULL
  2308. && h->dynindx != -1
  2309. && (! info->symbolic
  2310. || !h->def_regular))))
  2311. {
  2312. Elf_Internal_Rela outrel;
  2313. bool skip, relocate;
  2314. bfd_byte *loc;
  2315. /* When generating a shared object, these relocations
  2316. are copied into the output file to be resolved at run
  2317. time. */
  2318. if (sreloc == NULL)
  2319. {
  2320. sreloc = _bfd_elf_get_dynamic_reloc_section
  2321. (input_bfd, input_section, /*rela?*/ true);
  2322. if (sreloc == NULL)
  2323. return false;
  2324. }
  2325. skip = false;
  2326. relocate = false;
  2327. outrel.r_offset = _bfd_elf_section_offset (output_bfd,
  2328. info,
  2329. input_section,
  2330. rel->r_offset);
  2331. if (outrel.r_offset == (bfd_vma) -1)
  2332. skip = true;
  2333. else if (outrel.r_offset == (bfd_vma) -2)
  2334. skip = relocate = true;
  2335. outrel.r_offset += (input_section->output_section->vma
  2336. + input_section->output_offset);
  2337. if (skip)
  2338. memset (&outrel, 0, sizeof outrel);
  2339. else if ( r_type == R_M32R_10_PCREL_RELA
  2340. || r_type == R_M32R_18_PCREL_RELA
  2341. || r_type == R_M32R_26_PCREL_RELA
  2342. || r_type == R_M32R_REL32)
  2343. {
  2344. BFD_ASSERT (h != NULL && h->dynindx != -1);
  2345. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  2346. outrel.r_addend = rel->r_addend;
  2347. }
  2348. else
  2349. {
  2350. /* h->dynindx may be -1 if this symbol was marked to
  2351. become local. */
  2352. if (h == NULL
  2353. || ((info->symbolic || h->dynindx == -1)
  2354. && h->def_regular))
  2355. {
  2356. relocate = true;
  2357. outrel.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
  2358. outrel.r_addend = relocation + rel->r_addend;
  2359. }
  2360. else
  2361. {
  2362. BFD_ASSERT (h->dynindx != -1);
  2363. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  2364. outrel.r_addend = relocation + rel->r_addend;
  2365. }
  2366. }
  2367. loc = sreloc->contents;
  2368. loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
  2369. bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
  2370. ++sreloc->reloc_count;
  2371. /* If this reloc is against an external symbol, we do
  2372. not want to fiddle with the addend. Otherwise, we
  2373. need to include the symbol value so that it becomes
  2374. an addend for the dynamic reloc. */
  2375. if (! relocate)
  2376. continue;
  2377. break;
  2378. }
  2379. else if (r_type != R_M32R_10_PCREL_RELA)
  2380. break;
  2381. /* Fall through. */
  2382. case (int) R_M32R_10_PCREL :
  2383. r = m32r_elf_do_10_pcrel_reloc (input_bfd, howto, input_section,
  2384. contents, offset,
  2385. sec, relocation, addend);
  2386. goto check_reloc;
  2387. case (int) R_M32R_HI16_SLO :
  2388. case (int) R_M32R_HI16_ULO :
  2389. {
  2390. Elf_Internal_Rela *lorel;
  2391. /* We allow an arbitrary number of HI16 relocs before the
  2392. LO16 reloc. This permits gcc to emit the HI and LO relocs
  2393. itself. */
  2394. for (lorel = rel + 1;
  2395. (lorel < relend
  2396. && (ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_SLO
  2397. || ELF32_R_TYPE (lorel->r_info) == R_M32R_HI16_ULO));
  2398. lorel++)
  2399. continue;
  2400. if (lorel < relend
  2401. && ELF32_R_TYPE (lorel->r_info) == R_M32R_LO16)
  2402. {
  2403. m32r_elf_relocate_hi16 (input_bfd, r_type, rel, lorel,
  2404. contents, relocation + addend);
  2405. r = bfd_reloc_ok;
  2406. }
  2407. else
  2408. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2409. contents, offset,
  2410. relocation, addend);
  2411. }
  2412. goto check_reloc;
  2413. case (int) R_M32R_SDA16_RELA:
  2414. case (int) R_M32R_SDA16 :
  2415. {
  2416. const char *name;
  2417. BFD_ASSERT (sec != NULL);
  2418. name = bfd_section_name (sec);
  2419. if ( strcmp (name, ".sdata") == 0
  2420. || strcmp (name, ".sbss") == 0
  2421. || strcmp (name, ".scommon") == 0)
  2422. {
  2423. bfd_vma sda_base;
  2424. bfd *out_bfd = sec->output_section->owner;
  2425. r = m32r_elf_final_sda_base (out_bfd, info,
  2426. &errmsg,
  2427. &sda_base);
  2428. if (r != bfd_reloc_ok)
  2429. {
  2430. ret = false;
  2431. goto check_reloc;
  2432. }
  2433. /* At this point `relocation' contains the object's
  2434. address. */
  2435. relocation -= sda_base;
  2436. /* Now it contains the offset from _SDA_BASE_. */
  2437. }
  2438. else
  2439. {
  2440. _bfd_error_handler
  2441. /* xgettext:c-format */
  2442. (_("%pB: the target (%s) of an %s relocation"
  2443. " is in the wrong section (%pA)"),
  2444. input_bfd,
  2445. sym_name,
  2446. m32r_elf_howto_table[(int) r_type].name,
  2447. sec);
  2448. /*bfd_set_error (bfd_error_bad_value); ??? why? */
  2449. ret = false;
  2450. continue;
  2451. }
  2452. }
  2453. /* Fall through. */
  2454. default : /* OLD_M32R_RELOC */
  2455. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2456. contents, offset,
  2457. relocation, addend);
  2458. goto check_reloc;
  2459. }
  2460. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2461. contents, rel->r_offset,
  2462. relocation, rel->r_addend);
  2463. }
  2464. check_reloc:
  2465. if (r != bfd_reloc_ok)
  2466. {
  2467. /* FIXME: This should be generic enough to go in a utility. */
  2468. const char *name;
  2469. if (h != NULL)
  2470. name = h->root.root.string;
  2471. else
  2472. {
  2473. name = (bfd_elf_string_from_elf_section
  2474. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  2475. if (name == NULL || *name == '\0')
  2476. name = bfd_section_name (sec);
  2477. }
  2478. if (errmsg != NULL)
  2479. goto common_error;
  2480. switch (r)
  2481. {
  2482. case bfd_reloc_overflow:
  2483. (*info->callbacks->reloc_overflow)
  2484. (info, (h ? &h->root : NULL), name, howto->name,
  2485. (bfd_vma) 0, input_bfd, input_section, offset);
  2486. break;
  2487. case bfd_reloc_undefined:
  2488. (*info->callbacks->undefined_symbol)
  2489. (info, name, input_bfd, input_section, offset, true);
  2490. break;
  2491. case bfd_reloc_outofrange:
  2492. errmsg = _("internal error: out of range error");
  2493. goto common_error;
  2494. case bfd_reloc_notsupported:
  2495. errmsg = _("internal error: unsupported relocation error");
  2496. goto common_error;
  2497. case bfd_reloc_dangerous:
  2498. errmsg = _("internal error: dangerous error");
  2499. goto common_error;
  2500. default:
  2501. errmsg = _("internal error: unknown error");
  2502. /* fall through */
  2503. common_error:
  2504. (*info->callbacks->warning) (info, errmsg, name, input_bfd,
  2505. input_section, offset);
  2506. break;
  2507. }
  2508. }
  2509. }
  2510. return ret;
  2511. }
  2512. /* Finish up dynamic symbol handling. We set the contents of various
  2513. dynamic sections here. */
  2514. static bool
  2515. m32r_elf_finish_dynamic_symbol (bfd *output_bfd,
  2516. struct bfd_link_info *info,
  2517. struct elf_link_hash_entry *h,
  2518. Elf_Internal_Sym *sym)
  2519. {
  2520. struct elf_link_hash_table *htab;
  2521. bfd_byte *loc;
  2522. #ifdef DEBUG_PIC
  2523. printf ("m32r_elf_finish_dynamic_symbol()\n");
  2524. #endif
  2525. htab = m32r_elf_hash_table (info);
  2526. if (htab == NULL)
  2527. return false;
  2528. if (h->plt.offset != (bfd_vma) -1)
  2529. {
  2530. asection *splt;
  2531. asection *sgot;
  2532. asection *srela;
  2533. bfd_vma plt_index;
  2534. bfd_vma got_offset;
  2535. Elf_Internal_Rela rela;
  2536. /* This symbol has an entry in the procedure linkage table. Set
  2537. it up. */
  2538. BFD_ASSERT (h->dynindx != -1);
  2539. splt = htab->splt;
  2540. sgot = htab->sgotplt;
  2541. srela = htab->srelplt;
  2542. BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
  2543. /* Get the index in the procedure linkage table which
  2544. corresponds to this symbol. This is the index of this symbol
  2545. in all the symbols for which we are making plt entries. The
  2546. first entry in the procedure linkage table is reserved. */
  2547. plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
  2548. /* Get the offset into the .got table of the entry that
  2549. corresponds to this function. Each .got entry is 4 bytes.
  2550. The first three are reserved. */
  2551. got_offset = (plt_index + 3) * 4;
  2552. /* Fill in the entry in the procedure linkage table. */
  2553. if (! bfd_link_pic (info))
  2554. {
  2555. bfd_put_32 (output_bfd,
  2556. (PLT_ENTRY_WORD0b
  2557. + (((sgot->output_section->vma
  2558. + sgot->output_offset
  2559. + got_offset) >> 16) & 0xffff)),
  2560. splt->contents + h->plt.offset);
  2561. bfd_put_32 (output_bfd,
  2562. (PLT_ENTRY_WORD1b
  2563. + ((sgot->output_section->vma
  2564. + sgot->output_offset
  2565. + got_offset) & 0xffff)),
  2566. splt->contents + h->plt.offset + 4);
  2567. bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
  2568. splt->contents + h->plt.offset + 8);
  2569. bfd_put_32 (output_bfd,
  2570. (PLT_ENTRY_WORD3
  2571. + plt_index * sizeof (Elf32_External_Rela)),
  2572. splt->contents + h->plt.offset + 12);
  2573. bfd_put_32 (output_bfd,
  2574. (PLT_ENTRY_WORD4
  2575. + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
  2576. splt->contents + h->plt.offset + 16);
  2577. }
  2578. else
  2579. {
  2580. bfd_put_32 (output_bfd,
  2581. PLT_ENTRY_WORD0 + got_offset,
  2582. splt->contents + h->plt.offset);
  2583. bfd_put_32 (output_bfd, PLT_ENTRY_WORD1,
  2584. splt->contents + h->plt.offset + 4);
  2585. bfd_put_32 (output_bfd, PLT_ENTRY_WORD2,
  2586. splt->contents + h->plt.offset + 8);
  2587. bfd_put_32 (output_bfd,
  2588. (PLT_ENTRY_WORD3
  2589. + plt_index * sizeof (Elf32_External_Rela)),
  2590. splt->contents + h->plt.offset + 12);
  2591. bfd_put_32 (output_bfd,
  2592. (PLT_ENTRY_WORD4
  2593. + (((unsigned int) ((- (h->plt.offset + 16)) >> 2)) & 0xffffff)),
  2594. splt->contents + h->plt.offset + 16);
  2595. }
  2596. /* Fill in the entry in the global offset table. */
  2597. bfd_put_32 (output_bfd,
  2598. (splt->output_section->vma
  2599. + splt->output_offset
  2600. + h->plt.offset
  2601. + 12), /* same offset */
  2602. sgot->contents + got_offset);
  2603. /* Fill in the entry in the .rela.plt section. */
  2604. rela.r_offset = (sgot->output_section->vma
  2605. + sgot->output_offset
  2606. + got_offset);
  2607. rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_JMP_SLOT);
  2608. rela.r_addend = 0;
  2609. loc = srela->contents;
  2610. loc += plt_index * sizeof (Elf32_External_Rela);
  2611. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  2612. if (!h->def_regular)
  2613. {
  2614. /* Mark the symbol as undefined, rather than as defined in
  2615. the .plt section. Leave the value alone. */
  2616. sym->st_shndx = SHN_UNDEF;
  2617. }
  2618. }
  2619. if (h->got.offset != (bfd_vma) -1)
  2620. {
  2621. asection *sgot;
  2622. asection *srela;
  2623. Elf_Internal_Rela rela;
  2624. /* This symbol has an entry in the global offset table. Set it
  2625. up. */
  2626. sgot = htab->sgot;
  2627. srela = htab->srelgot;
  2628. BFD_ASSERT (sgot != NULL && srela != NULL);
  2629. rela.r_offset = (sgot->output_section->vma
  2630. + sgot->output_offset
  2631. + (h->got.offset &~ 1));
  2632. /* If this is a -Bsymbolic link, and the symbol is defined
  2633. locally, we just want to emit a RELATIVE reloc. Likewise if
  2634. the symbol was forced to be local because of a version file.
  2635. The entry in the global offset table will already have been
  2636. initialized in the relocate_section function. */
  2637. if (bfd_link_pic (info)
  2638. && (info->symbolic
  2639. || h->dynindx == -1
  2640. || h->forced_local)
  2641. && h->def_regular)
  2642. {
  2643. rela.r_info = ELF32_R_INFO (0, R_M32R_RELATIVE);
  2644. rela.r_addend = (h->root.u.def.value
  2645. + h->root.u.def.section->output_section->vma
  2646. + h->root.u.def.section->output_offset);
  2647. }
  2648. else
  2649. {
  2650. BFD_ASSERT ((h->got.offset & 1) == 0);
  2651. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
  2652. rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_GLOB_DAT);
  2653. rela.r_addend = 0;
  2654. }
  2655. loc = srela->contents;
  2656. loc += srela->reloc_count * sizeof (Elf32_External_Rela);
  2657. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  2658. ++srela->reloc_count;
  2659. }
  2660. if (h->needs_copy)
  2661. {
  2662. asection *s;
  2663. Elf_Internal_Rela rela;
  2664. /* This symbols needs a copy reloc. Set it up. */
  2665. BFD_ASSERT (h->dynindx != -1
  2666. && (h->root.type == bfd_link_hash_defined
  2667. || h->root.type == bfd_link_hash_defweak));
  2668. s = bfd_get_linker_section (htab->dynobj, ".rela.bss");
  2669. BFD_ASSERT (s != NULL);
  2670. rela.r_offset = (h->root.u.def.value
  2671. + h->root.u.def.section->output_section->vma
  2672. + h->root.u.def.section->output_offset);
  2673. rela.r_info = ELF32_R_INFO (h->dynindx, R_M32R_COPY);
  2674. rela.r_addend = 0;
  2675. loc = s->contents;
  2676. loc += s->reloc_count * sizeof (Elf32_External_Rela);
  2677. bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
  2678. ++s->reloc_count;
  2679. }
  2680. /* Mark some specially defined symbols as absolute. */
  2681. if (h == htab->hdynamic || h == htab->hgot)
  2682. sym->st_shndx = SHN_ABS;
  2683. return true;
  2684. }
  2685. /* Finish up the dynamic sections. */
  2686. static bool
  2687. m32r_elf_finish_dynamic_sections (bfd *output_bfd,
  2688. struct bfd_link_info *info)
  2689. {
  2690. struct elf_link_hash_table *htab;
  2691. bfd *dynobj;
  2692. asection *sdyn;
  2693. asection *sgot;
  2694. #ifdef DEBUG_PIC
  2695. printf ("m32r_elf_finish_dynamic_sections()\n");
  2696. #endif
  2697. htab = m32r_elf_hash_table (info);
  2698. if (htab == NULL)
  2699. return false;
  2700. dynobj = htab->dynobj;
  2701. sgot = htab->sgotplt;
  2702. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2703. if (htab->dynamic_sections_created)
  2704. {
  2705. asection *splt;
  2706. Elf32_External_Dyn *dyncon, *dynconend;
  2707. BFD_ASSERT (sgot != NULL && sdyn != NULL);
  2708. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2709. dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2710. for (; dyncon < dynconend; dyncon++)
  2711. {
  2712. Elf_Internal_Dyn dyn;
  2713. asection *s;
  2714. bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
  2715. switch (dyn.d_tag)
  2716. {
  2717. default:
  2718. break;
  2719. case DT_PLTGOT:
  2720. s = htab->sgotplt;
  2721. goto get_vma;
  2722. case DT_JMPREL:
  2723. s = htab->srelplt;
  2724. get_vma:
  2725. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2726. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2727. break;
  2728. case DT_PLTRELSZ:
  2729. s = htab->srelplt;
  2730. dyn.d_un.d_val = s->size;
  2731. bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
  2732. break;
  2733. }
  2734. }
  2735. /* Fill in the first entry in the procedure linkage table. */
  2736. splt = htab->splt;
  2737. if (splt && splt->size > 0)
  2738. {
  2739. if (bfd_link_pic (info))
  2740. {
  2741. bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD0, splt->contents);
  2742. bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD1, splt->contents + 4);
  2743. bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD2, splt->contents + 8);
  2744. bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD3, splt->contents + 12);
  2745. bfd_put_32 (output_bfd, PLT0_PIC_ENTRY_WORD4, splt->contents + 16);
  2746. }
  2747. else
  2748. {
  2749. unsigned long addr;
  2750. /* addr = .got + 4 */
  2751. addr = sgot->output_section->vma + sgot->output_offset + 4;
  2752. bfd_put_32 (output_bfd,
  2753. PLT0_ENTRY_WORD0 | ((addr >> 16) & 0xffff),
  2754. splt->contents);
  2755. bfd_put_32 (output_bfd,
  2756. PLT0_ENTRY_WORD1 | (addr & 0xffff),
  2757. splt->contents + 4);
  2758. bfd_put_32 (output_bfd, PLT0_ENTRY_WORD2, splt->contents + 8);
  2759. bfd_put_32 (output_bfd, PLT0_ENTRY_WORD3, splt->contents + 12);
  2760. bfd_put_32 (output_bfd, PLT0_ENTRY_WORD4, splt->contents + 16);
  2761. }
  2762. elf_section_data (splt->output_section)->this_hdr.sh_entsize =
  2763. PLT_ENTRY_SIZE;
  2764. }
  2765. }
  2766. /* Fill in the first three entries in the global offset table. */
  2767. if (sgot && sgot->size > 0)
  2768. {
  2769. if (sdyn == NULL)
  2770. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
  2771. else
  2772. bfd_put_32 (output_bfd,
  2773. sdyn->output_section->vma + sdyn->output_offset,
  2774. sgot->contents);
  2775. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
  2776. bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
  2777. elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
  2778. }
  2779. return true;
  2780. }
  2781. /* Set the right machine number. */
  2782. static bool
  2783. m32r_elf_object_p (bfd *abfd)
  2784. {
  2785. switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
  2786. {
  2787. default:
  2788. case E_M32R_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r); break;
  2789. case E_M32RX_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32rx); break;
  2790. case E_M32R2_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_m32r, bfd_mach_m32r2); break;
  2791. }
  2792. return true;
  2793. }
  2794. /* Store the machine number in the flags field. */
  2795. static bool
  2796. m32r_elf_final_write_processing (bfd *abfd)
  2797. {
  2798. unsigned long val;
  2799. switch (bfd_get_mach (abfd))
  2800. {
  2801. default:
  2802. case bfd_mach_m32r: val = E_M32R_ARCH; break;
  2803. case bfd_mach_m32rx: val = E_M32RX_ARCH; break;
  2804. case bfd_mach_m32r2: val = E_M32R2_ARCH; break;
  2805. }
  2806. elf_elfheader (abfd)->e_flags &=~ EF_M32R_ARCH;
  2807. elf_elfheader (abfd)->e_flags |= val;
  2808. return _bfd_elf_final_write_processing (abfd);
  2809. }
  2810. /* Function to keep M32R specific file flags. */
  2811. static bool
  2812. m32r_elf_set_private_flags (bfd *abfd, flagword flags)
  2813. {
  2814. BFD_ASSERT (!elf_flags_init (abfd)
  2815. || elf_elfheader (abfd)->e_flags == flags);
  2816. elf_elfheader (abfd)->e_flags = flags;
  2817. elf_flags_init (abfd) = true;
  2818. return true;
  2819. }
  2820. /* Merge backend specific data from an object file to the output
  2821. object file when linking. */
  2822. static bool
  2823. m32r_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  2824. {
  2825. bfd *obfd = info->output_bfd;
  2826. flagword out_flags;
  2827. flagword in_flags;
  2828. if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  2829. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  2830. return true;
  2831. in_flags = elf_elfheader (ibfd)->e_flags;
  2832. out_flags = elf_elfheader (obfd)->e_flags;
  2833. if (! elf_flags_init (obfd))
  2834. {
  2835. /* If the input is the default architecture then do not
  2836. bother setting the flags for the output architecture,
  2837. instead allow future merges to do this. If no future
  2838. merges ever set these flags then they will retain their
  2839. unitialised values, which surprise surprise, correspond
  2840. to the default values. */
  2841. if (bfd_get_arch_info (ibfd)->the_default)
  2842. return true;
  2843. elf_flags_init (obfd) = true;
  2844. elf_elfheader (obfd)->e_flags = in_flags;
  2845. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  2846. && bfd_get_arch_info (obfd)->the_default)
  2847. return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  2848. bfd_get_mach (ibfd));
  2849. return true;
  2850. }
  2851. /* Check flag compatibility. */
  2852. if (in_flags == out_flags)
  2853. return true;
  2854. if ((in_flags & EF_M32R_ARCH) != (out_flags & EF_M32R_ARCH))
  2855. {
  2856. if ( ((in_flags & EF_M32R_ARCH) != E_M32R_ARCH)
  2857. || ((out_flags & EF_M32R_ARCH) == E_M32R_ARCH)
  2858. || ((in_flags & EF_M32R_ARCH) == E_M32R2_ARCH))
  2859. {
  2860. _bfd_error_handler
  2861. (_("%pB: instruction set mismatch with previous modules"), ibfd);
  2862. bfd_set_error (bfd_error_bad_value);
  2863. return false;
  2864. }
  2865. }
  2866. return true;
  2867. }
  2868. /* Display the flags field. */
  2869. static bool
  2870. m32r_elf_print_private_bfd_data (bfd *abfd, void * ptr)
  2871. {
  2872. FILE * file = (FILE *) ptr;
  2873. BFD_ASSERT (abfd != NULL && ptr != NULL);
  2874. _bfd_elf_print_private_bfd_data (abfd, ptr);
  2875. fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
  2876. switch (elf_elfheader (abfd)->e_flags & EF_M32R_ARCH)
  2877. {
  2878. default:
  2879. case E_M32R_ARCH: fprintf (file, _(": m32r instructions")); break;
  2880. case E_M32RX_ARCH: fprintf (file, _(": m32rx instructions")); break;
  2881. case E_M32R2_ARCH: fprintf (file, _(": m32r2 instructions")); break;
  2882. }
  2883. fputc ('\n', file);
  2884. return true;
  2885. }
  2886. static asection *
  2887. m32r_elf_gc_mark_hook (asection *sec,
  2888. struct bfd_link_info *info,
  2889. Elf_Internal_Rela *rel,
  2890. struct elf_link_hash_entry *h,
  2891. Elf_Internal_Sym *sym)
  2892. {
  2893. if (h != NULL)
  2894. switch (ELF32_R_TYPE (rel->r_info))
  2895. {
  2896. case R_M32R_GNU_VTINHERIT:
  2897. case R_M32R_GNU_VTENTRY:
  2898. case R_M32R_RELA_GNU_VTINHERIT:
  2899. case R_M32R_RELA_GNU_VTENTRY:
  2900. return NULL;
  2901. }
  2902. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  2903. }
  2904. /* Look through the relocs for a section during the first phase.
  2905. Since we don't do .gots or .plts, we just need to consider the
  2906. virtual table relocs for gc. */
  2907. static bool
  2908. m32r_elf_check_relocs (bfd *abfd,
  2909. struct bfd_link_info *info,
  2910. asection *sec,
  2911. const Elf_Internal_Rela *relocs)
  2912. {
  2913. Elf_Internal_Shdr *symtab_hdr;
  2914. struct elf_link_hash_entry **sym_hashes;
  2915. const Elf_Internal_Rela *rel;
  2916. const Elf_Internal_Rela *rel_end;
  2917. struct elf_link_hash_table *htab;
  2918. bfd *dynobj;
  2919. asection *sreloc;
  2920. if (bfd_link_relocatable (info))
  2921. return true;
  2922. sreloc = NULL;
  2923. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2924. sym_hashes = elf_sym_hashes (abfd);
  2925. htab = m32r_elf_hash_table (info);
  2926. if (htab == NULL)
  2927. return false;
  2928. dynobj = htab->dynobj;
  2929. rel_end = relocs + sec->reloc_count;
  2930. for (rel = relocs; rel < rel_end; rel++)
  2931. {
  2932. int r_type;
  2933. struct elf_link_hash_entry *h;
  2934. unsigned long r_symndx;
  2935. r_symndx = ELF32_R_SYM (rel->r_info);
  2936. r_type = ELF32_R_TYPE (rel->r_info);
  2937. if (r_symndx < symtab_hdr->sh_info)
  2938. h = NULL;
  2939. else
  2940. {
  2941. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  2942. while (h->root.type == bfd_link_hash_indirect
  2943. || h->root.type == bfd_link_hash_warning)
  2944. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  2945. }
  2946. /* Some relocs require a global offset table. */
  2947. if (htab->sgot == NULL)
  2948. {
  2949. switch (r_type)
  2950. {
  2951. case R_M32R_GOT16_HI_ULO:
  2952. case R_M32R_GOT16_HI_SLO:
  2953. case R_M32R_GOTOFF:
  2954. case R_M32R_GOTOFF_HI_ULO:
  2955. case R_M32R_GOTOFF_HI_SLO:
  2956. case R_M32R_GOTOFF_LO:
  2957. case R_M32R_GOT16_LO:
  2958. case R_M32R_GOTPC24:
  2959. case R_M32R_GOTPC_HI_ULO:
  2960. case R_M32R_GOTPC_HI_SLO:
  2961. case R_M32R_GOTPC_LO:
  2962. case R_M32R_GOT24:
  2963. if (dynobj == NULL)
  2964. htab->dynobj = dynobj = abfd;
  2965. if (!_bfd_elf_create_got_section (dynobj, info))
  2966. return false;
  2967. break;
  2968. default:
  2969. break;
  2970. }
  2971. }
  2972. switch (r_type)
  2973. {
  2974. case R_M32R_GOT16_HI_ULO:
  2975. case R_M32R_GOT16_HI_SLO:
  2976. case R_M32R_GOT16_LO:
  2977. case R_M32R_GOT24:
  2978. if (h != NULL)
  2979. h->got.refcount += 1;
  2980. else
  2981. {
  2982. bfd_signed_vma *local_got_refcounts;
  2983. /* This is a global offset table entry for a local
  2984. symbol. */
  2985. local_got_refcounts = elf_local_got_refcounts (abfd);
  2986. if (local_got_refcounts == NULL)
  2987. {
  2988. bfd_size_type size;
  2989. size = symtab_hdr->sh_info;
  2990. size *= sizeof (bfd_signed_vma);
  2991. local_got_refcounts = bfd_zalloc (abfd, size);
  2992. if (local_got_refcounts == NULL)
  2993. return false;
  2994. elf_local_got_refcounts (abfd) = local_got_refcounts;
  2995. }
  2996. local_got_refcounts[r_symndx] += 1;
  2997. }
  2998. break;
  2999. case R_M32R_26_PLTREL:
  3000. /* This symbol requires a procedure linkage table entry. We
  3001. actually build the entry in adjust_dynamic_symbol,
  3002. because this might be a case of linking PIC code without
  3003. linking in any dynamic objects, in which case we don't
  3004. need to generate a procedure linkage table after all. */
  3005. /* If this is a local symbol, we resolve it directly without
  3006. creating a procedure linkage table entry. */
  3007. if (h == NULL)
  3008. continue;
  3009. if (h->forced_local)
  3010. break;
  3011. h->needs_plt = 1;
  3012. h->plt.refcount += 1;
  3013. break;
  3014. case R_M32R_16_RELA:
  3015. case R_M32R_24_RELA:
  3016. case R_M32R_32_RELA:
  3017. case R_M32R_REL32:
  3018. case R_M32R_HI16_ULO_RELA:
  3019. case R_M32R_HI16_SLO_RELA:
  3020. case R_M32R_LO16_RELA:
  3021. case R_M32R_SDA16_RELA:
  3022. case R_M32R_10_PCREL_RELA:
  3023. case R_M32R_18_PCREL_RELA:
  3024. case R_M32R_26_PCREL_RELA:
  3025. if (h != NULL && !bfd_link_pic (info))
  3026. {
  3027. h->non_got_ref = 1;
  3028. h->plt.refcount += 1;
  3029. }
  3030. /* If we are creating a shared library, and this is a reloc
  3031. against a global symbol, or a non PC relative reloc
  3032. against a local symbol, then we need to copy the reloc
  3033. into the shared library. However, if we are linking with
  3034. -Bsymbolic, we do not need to copy a reloc against a
  3035. global symbol which is defined in an object we are
  3036. including in the link (i.e., DEF_REGULAR is set). At
  3037. this point we have not seen all the input files, so it is
  3038. possible that DEF_REGULAR is not set now but will be set
  3039. later (it is never cleared). We account for that
  3040. possibility below by storing information in the
  3041. dyn_relocs field of the hash table entry. A similar
  3042. situation occurs when creating shared libraries and symbol
  3043. visibility changes render the symbol local.
  3044. If on the other hand, we are creating an executable, we
  3045. may need to keep relocations for symbols satisfied by a
  3046. dynamic library if we manage to avoid copy relocs for the
  3047. symbol. */
  3048. if ((bfd_link_pic (info)
  3049. && (sec->flags & SEC_ALLOC) != 0
  3050. && (( r_type != R_M32R_26_PCREL_RELA
  3051. && r_type != R_M32R_18_PCREL_RELA
  3052. && r_type != R_M32R_10_PCREL_RELA
  3053. && r_type != R_M32R_REL32)
  3054. || (h != NULL
  3055. && (! info->symbolic
  3056. || h->root.type == bfd_link_hash_defweak
  3057. || !h->def_regular))))
  3058. || (!bfd_link_pic (info)
  3059. && (sec->flags & SEC_ALLOC) != 0
  3060. && h != NULL
  3061. && (h->root.type == bfd_link_hash_defweak
  3062. || !h->def_regular)))
  3063. {
  3064. struct elf_dyn_relocs *p;
  3065. struct elf_dyn_relocs **head;
  3066. if (dynobj == NULL)
  3067. htab->dynobj = dynobj = abfd;
  3068. /* When creating a shared object, we must copy these
  3069. relocs into the output file. We create a reloc
  3070. section in dynobj and make room for the reloc. */
  3071. if (sreloc == NULL)
  3072. {
  3073. sreloc = _bfd_elf_make_dynamic_reloc_section
  3074. (sec, dynobj, 2, abfd, /*rela?*/ true);
  3075. if (sreloc == NULL)
  3076. return false;
  3077. }
  3078. /* If this is a global symbol, we count the number of
  3079. relocations we need for this symbol. */
  3080. if (h != NULL)
  3081. head = &h->dyn_relocs;
  3082. else
  3083. {
  3084. /* Track dynamic relocs needed for local syms too. */
  3085. asection *s;
  3086. void *vpp;
  3087. Elf_Internal_Sym *isym;
  3088. isym = bfd_sym_from_r_symndx (&htab->sym_cache,
  3089. abfd, r_symndx);
  3090. if (isym == NULL)
  3091. return false;
  3092. s = bfd_section_from_elf_index (abfd, isym->st_shndx);
  3093. if (s == NULL)
  3094. s = sec;
  3095. vpp = &elf_section_data (s)->local_dynrel;
  3096. head = (struct elf_dyn_relocs **) vpp;
  3097. }
  3098. p = *head;
  3099. if (p == NULL || p->sec != sec)
  3100. {
  3101. size_t amt = sizeof (*p);
  3102. p = bfd_alloc (dynobj, amt);
  3103. if (p == NULL)
  3104. return false;
  3105. p->next = *head;
  3106. *head = p;
  3107. p->sec = sec;
  3108. p->count = 0;
  3109. p->pc_count = 0;
  3110. }
  3111. p->count += 1;
  3112. if ( ELF32_R_TYPE (rel->r_info) == R_M32R_26_PCREL_RELA
  3113. || ELF32_R_TYPE (rel->r_info) == R_M32R_18_PCREL_RELA
  3114. || ELF32_R_TYPE (rel->r_info) == R_M32R_10_PCREL_RELA
  3115. || ELF32_R_TYPE (rel->r_info) == R_M32R_REL32)
  3116. p->pc_count += 1;
  3117. }
  3118. break;
  3119. /* This relocation describes the C++ object vtable hierarchy.
  3120. Reconstruct it for later use during GC. */
  3121. case R_M32R_RELA_GNU_VTINHERIT:
  3122. case R_M32R_GNU_VTINHERIT:
  3123. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  3124. return false;
  3125. break;
  3126. /* This relocation describes which C++ vtable entries are actually
  3127. used. Record for later use during GC. */
  3128. case R_M32R_GNU_VTENTRY:
  3129. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
  3130. return false;
  3131. break;
  3132. case R_M32R_RELA_GNU_VTENTRY:
  3133. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  3134. return false;
  3135. break;
  3136. }
  3137. }
  3138. return true;
  3139. }
  3140. static const struct bfd_elf_special_section m32r_elf_special_sections[] =
  3141. {
  3142. { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
  3143. { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
  3144. { NULL, 0, 0, 0, 0 }
  3145. };
  3146. static bool
  3147. m32r_elf_section_flags (const Elf_Internal_Shdr *hdr)
  3148. {
  3149. const char *name = hdr->bfd_section->name;
  3150. if (startswith (name, ".sbss")
  3151. || startswith (name, ".sdata"))
  3152. hdr->bfd_section->flags |= SEC_SMALL_DATA;
  3153. return true;
  3154. }
  3155. static enum elf_reloc_type_class
  3156. m32r_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  3157. const asection *rel_sec ATTRIBUTE_UNUSED,
  3158. const Elf_Internal_Rela *rela)
  3159. {
  3160. switch ((int) ELF32_R_TYPE (rela->r_info))
  3161. {
  3162. case R_M32R_RELATIVE: return reloc_class_relative;
  3163. case R_M32R_JMP_SLOT: return reloc_class_plt;
  3164. case R_M32R_COPY: return reloc_class_copy;
  3165. default: return reloc_class_normal;
  3166. }
  3167. }
  3168. #define ELF_ARCH bfd_arch_m32r
  3169. #define ELF_TARGET_ID M32R_ELF_DATA
  3170. #define ELF_MACHINE_CODE EM_M32R
  3171. #define ELF_MACHINE_ALT1 EM_CYGNUS_M32R
  3172. #define ELF_MAXPAGESIZE 0x1 /* Explicitly requested by Mitsubishi. */
  3173. #define TARGET_BIG_SYM m32r_elf32_vec
  3174. #define TARGET_BIG_NAME "elf32-m32r"
  3175. #define TARGET_LITTLE_SYM m32r_elf32_le_vec
  3176. #define TARGET_LITTLE_NAME "elf32-m32rle"
  3177. #define elf_info_to_howto m32r_info_to_howto
  3178. #define elf_info_to_howto_rel m32r_info_to_howto_rel
  3179. #define elf_backend_section_from_bfd_section _bfd_m32r_elf_section_from_bfd_section
  3180. #define elf_backend_symbol_processing _bfd_m32r_elf_symbol_processing
  3181. #define elf_backend_add_symbol_hook m32r_elf_add_symbol_hook
  3182. #define elf_backend_relocate_section m32r_elf_relocate_section
  3183. #define elf_backend_gc_mark_hook m32r_elf_gc_mark_hook
  3184. #define elf_backend_check_relocs m32r_elf_check_relocs
  3185. #define elf_backend_create_dynamic_sections m32r_elf_create_dynamic_sections
  3186. #define bfd_elf32_bfd_link_hash_table_create m32r_elf_link_hash_table_create
  3187. #define elf_backend_size_dynamic_sections m32r_elf_size_dynamic_sections
  3188. #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
  3189. #define elf_backend_finish_dynamic_sections m32r_elf_finish_dynamic_sections
  3190. #define elf_backend_adjust_dynamic_symbol m32r_elf_adjust_dynamic_symbol
  3191. #define elf_backend_finish_dynamic_symbol m32r_elf_finish_dynamic_symbol
  3192. #define elf_backend_reloc_type_class m32r_elf_reloc_type_class
  3193. #define elf_backend_can_gc_sections 1
  3194. /*#if !USE_REL
  3195. #define elf_backend_rela_normal 1
  3196. #endif*/
  3197. #define elf_backend_can_refcount 1
  3198. #define elf_backend_want_got_plt 1
  3199. #define elf_backend_plt_readonly 1
  3200. #define elf_backend_want_plt_sym 0
  3201. #define elf_backend_got_header_size 12
  3202. #define elf_backend_dtrel_excludes_plt 1
  3203. #define elf_backend_may_use_rel_p 1
  3204. #ifdef USE_M32R_OLD_RELOC
  3205. #define elf_backend_default_use_rela_p 0
  3206. #define elf_backend_may_use_rela_p 0
  3207. #else
  3208. #define elf_backend_default_use_rela_p 1
  3209. #define elf_backend_may_use_rela_p 1
  3210. #endif
  3211. #define elf_backend_object_p m32r_elf_object_p
  3212. #define elf_backend_final_write_processing m32r_elf_final_write_processing
  3213. #define bfd_elf32_bfd_merge_private_bfd_data m32r_elf_merge_private_bfd_data
  3214. #define bfd_elf32_bfd_set_private_flags m32r_elf_set_private_flags
  3215. #define bfd_elf32_bfd_print_private_bfd_data m32r_elf_print_private_bfd_data
  3216. #define elf_backend_special_sections m32r_elf_special_sections
  3217. #define elf_backend_section_flags m32r_elf_section_flags
  3218. #define elf_backend_linux_prpsinfo32_ugid16 true
  3219. #include "elf32-target.h"
  3220. #undef ELF_MAXPAGESIZE
  3221. #define ELF_MAXPAGESIZE 0x1000
  3222. #undef TARGET_BIG_SYM
  3223. #define TARGET_BIG_SYM m32r_elf32_linux_vec
  3224. #undef TARGET_BIG_NAME
  3225. #define TARGET_BIG_NAME "elf32-m32r-linux"
  3226. #undef TARGET_LITTLE_SYM
  3227. #define TARGET_LITTLE_SYM m32r_elf32_linux_le_vec
  3228. #undef TARGET_LITTLE_NAME
  3229. #define TARGET_LITTLE_NAME "elf32-m32rle-linux"
  3230. #undef elf32_bed
  3231. #define elf32_bed elf32_m32r_lin_bed
  3232. #include "elf32-target.h"