elf32-arc.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159
  1. /* ARC-specific support for 32-bit ELF
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. Contributed by Cupertino Miranda (cmiranda@synopsys.com).
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "elf/arc.h"
  22. #include "libiberty.h"
  23. #include "opcode/arc-func.h"
  24. #include "opcode/arc.h"
  25. #include "arc-plt.h"
  26. #define FEATURE_LIST_NAME bfd_feature_list
  27. #define CONFLICT_LIST bfd_conflict_list
  28. #include "opcode/arc-attrs.h"
  29. /* #define ARC_ENABLE_DEBUG 1 */
  30. #ifdef ARC_ENABLE_DEBUG
  31. static const char *
  32. name_for_global_symbol (struct elf_link_hash_entry *h)
  33. {
  34. static char *local_str = "(local)";
  35. if (h == NULL)
  36. return local_str;
  37. return h->root.root.string;
  38. }
  39. #define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
  40. #else
  41. #define ARC_DEBUG(...)
  42. #endif
  43. #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \
  44. { \
  45. struct elf_link_hash_table *_htab = elf_hash_table (info); \
  46. Elf_Internal_Rela _rel; \
  47. bfd_byte * _loc; \
  48. \
  49. if (_htab->dynamic_sections_created) \
  50. { \
  51. BFD_ASSERT (_htab->srel##SECTION &&_htab->srel##SECTION->contents); \
  52. _loc = _htab->srel##SECTION->contents \
  53. + ((_htab->srel##SECTION->reloc_count) \
  54. * sizeof (Elf32_External_Rela)); \
  55. _htab->srel##SECTION->reloc_count++; \
  56. _rel.r_addend = ADDEND; \
  57. _rel.r_offset = (_htab->s##SECTION)->output_section->vma \
  58. + (_htab->s##SECTION)->output_offset + OFFSET; \
  59. BFD_ASSERT ((long) SYM_IDX != -1); \
  60. _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \
  61. bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \
  62. } \
  63. }
  64. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  65. case VALUE: \
  66. return "R_" #TYPE; \
  67. break;
  68. static ATTRIBUTE_UNUSED const char *
  69. reloc_type_to_name (unsigned int type)
  70. {
  71. switch (type)
  72. {
  73. #include "elf/arc-reloc.def"
  74. default:
  75. return "UNKNOWN";
  76. break;
  77. }
  78. }
  79. #undef ARC_RELOC_HOWTO
  80. /* Try to minimize the amount of space occupied by relocation tables
  81. on the ROM (not that the ROM won't be swamped by other ELF overhead). */
  82. #define USE_REL 1
  83. /* Similar with bfd_get_32 but taking into account the
  84. middle-endianess of the ARC CPUs. Only to be used in code
  85. sections. */
  86. static bfd_vma
  87. bfd_get_32_me (bfd * abfd,const unsigned char * data)
  88. {
  89. bfd_vma value = 0;
  90. if (bfd_big_endian (abfd))
  91. value = bfd_get_32 (abfd, data);
  92. else
  93. {
  94. value = ((bfd_get_8 (abfd, data) & 255) << 16);
  95. value |= ((bfd_get_8 (abfd, data + 1) & 255) << 24);
  96. value |= (bfd_get_8 (abfd, data + 2) & 255);
  97. value |= ((bfd_get_8 (abfd, data + 3) & 255) << 8);
  98. }
  99. return value;
  100. }
  101. static void
  102. bfd_put_32_me (bfd *abfd, bfd_vma value,unsigned char *data)
  103. {
  104. bfd_put_16 (abfd, (value & 0xffff0000) >> 16, data);
  105. bfd_put_16 (abfd, value & 0xffff, data + 2);
  106. }
  107. static ATTRIBUTE_UNUSED bool
  108. is_reloc_PC_relative (reloc_howto_type *howto)
  109. {
  110. return strstr (howto->name, "PC") != NULL;
  111. }
  112. static bool
  113. is_reloc_SDA_relative (reloc_howto_type *howto)
  114. {
  115. return strstr (howto->name, "SDA") != NULL;
  116. }
  117. static bool
  118. is_reloc_for_GOT (reloc_howto_type * howto)
  119. {
  120. if (strstr (howto->name, "TLS") != NULL)
  121. return false;
  122. return strstr (howto->name, "GOT") != NULL;
  123. }
  124. static bool
  125. is_reloc_for_PLT (reloc_howto_type * howto)
  126. {
  127. return strstr (howto->name, "PLT") != NULL;
  128. }
  129. static bool
  130. is_reloc_for_TLS (reloc_howto_type *howto)
  131. {
  132. return strstr (howto->name, "TLS") != NULL;
  133. }
  134. struct arc_relocation_data
  135. {
  136. bfd_signed_vma reloc_offset;
  137. bfd_signed_vma reloc_addend;
  138. bfd_signed_vma got_offset_value;
  139. bfd_signed_vma sym_value;
  140. asection *sym_section;
  141. reloc_howto_type *howto;
  142. asection *input_section;
  143. bfd_signed_vma sdata_begin_symbol_vma;
  144. bool sdata_begin_symbol_vma_set;
  145. bfd_signed_vma got_symbol_vma;
  146. bool should_relocate;
  147. const char *symbol_name;
  148. };
  149. /* ARC ELF linker hash entry. */
  150. struct elf_arc_link_hash_entry
  151. {
  152. struct elf_link_hash_entry root;
  153. struct got_entry *got_ents;
  154. };
  155. /* Should be included at this location due to static declarations
  156. defined before this point. */
  157. #include "arc-got.h"
  158. #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
  159. #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
  160. #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
  161. #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
  162. #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
  163. #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
  164. static bfd_reloc_status_type
  165. arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  166. arelent *reloc_entry,
  167. asymbol *symbol_in,
  168. void *data ATTRIBUTE_UNUSED,
  169. asection *input_section,
  170. bfd *output_bfd,
  171. char ** error_message ATTRIBUTE_UNUSED)
  172. {
  173. if (output_bfd != NULL)
  174. {
  175. reloc_entry->address += input_section->output_offset;
  176. /* In case of relocateable link and if the reloc is against a
  177. section symbol, the addend needs to be adjusted according to
  178. where the section symbol winds up in the output section. */
  179. if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section)
  180. reloc_entry->addend += symbol_in->section->output_offset;
  181. return bfd_reloc_ok;
  182. }
  183. return bfd_reloc_continue;
  184. }
  185. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  186. TYPE = VALUE,
  187. enum howto_list
  188. {
  189. #include "elf/arc-reloc.def"
  190. HOWTO_LIST_LAST
  191. };
  192. #undef ARC_RELOC_HOWTO
  193. #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  194. [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, false, 0, \
  195. complain_overflow_##OVERFLOW, arc_elf_reloc, \
  196. "R_" #TYPE, false, 0, 0, false),
  197. static struct reloc_howto_struct elf_arc_howto_table[] =
  198. {
  199. #include "elf/arc-reloc.def"
  200. /* Example of what is generated by the preprocessor. Currently kept as an
  201. example.
  202. HOWTO (R_ARC_NONE, // Type.
  203. 0, // Rightshift.
  204. 2, // Size (0 = byte, 1 = short, 2 = long).
  205. 32, // Bitsize.
  206. false, // PC_relative.
  207. 0, // Bitpos.
  208. complain_overflow_bitfield, // Complain_on_overflow.
  209. bfd_elf_generic_reloc, // Special_function.
  210. "R_ARC_NONE", // Name.
  211. true, // Partial_inplace.
  212. 0, // Src_mask.
  213. 0, // Dst_mask.
  214. false), // PCrel_offset.
  215. */
  216. };
  217. #undef ARC_RELOC_HOWTO
  218. static void
  219. arc_elf_howto_init (void)
  220. {
  221. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  222. elf_arc_howto_table[TYPE].pc_relative = \
  223. (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
  224. elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
  225. /* Only 32 bit data relocations should be marked as ME. */ \
  226. if (strstr (#FORMULA, " ME ") != NULL) \
  227. { \
  228. BFD_ASSERT (SIZE == 2); \
  229. }
  230. #include "elf/arc-reloc.def"
  231. }
  232. #undef ARC_RELOC_HOWTO
  233. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  234. [TYPE] = VALUE,
  235. const int howto_table_lookup[] =
  236. {
  237. #include "elf/arc-reloc.def"
  238. };
  239. #undef ARC_RELOC_HOWTO
  240. static reloc_howto_type *
  241. arc_elf_howto (unsigned int r_type)
  242. {
  243. if (elf_arc_howto_table[R_ARC_32].dst_mask == 0)
  244. arc_elf_howto_init ();
  245. return &elf_arc_howto_table[r_type];
  246. }
  247. /* Map BFD reloc types to ARC ELF reloc types. */
  248. struct arc_reloc_map
  249. {
  250. bfd_reloc_code_real_type bfd_reloc_val;
  251. unsigned char elf_reloc_val;
  252. };
  253. /* ARC ELF linker hash table. */
  254. struct elf_arc_link_hash_table
  255. {
  256. struct elf_link_hash_table elf;
  257. };
  258. static struct bfd_hash_entry *
  259. elf_arc_link_hash_newfunc (struct bfd_hash_entry *entry,
  260. struct bfd_hash_table *table,
  261. const char *string)
  262. {
  263. struct elf_arc_link_hash_entry * ret =
  264. (struct elf_arc_link_hash_entry *) entry;
  265. /* Allocate the structure if it has not already been allocated by a
  266. subclass. */
  267. if (ret == NULL)
  268. ret = (struct elf_arc_link_hash_entry *)
  269. bfd_hash_allocate (table, sizeof (struct elf_arc_link_hash_entry));
  270. if (ret == NULL)
  271. return (struct bfd_hash_entry *) ret;
  272. /* Call the allocation method of the superclass. */
  273. ret = ((struct elf_arc_link_hash_entry *)
  274. _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  275. table, string));
  276. if (ret != NULL)
  277. {
  278. ret->got_ents = NULL;
  279. }
  280. return (struct bfd_hash_entry *) ret;
  281. }
  282. /* Destroy an ARC ELF linker hash table. */
  283. static void
  284. elf_arc_link_hash_table_free (bfd *obfd)
  285. {
  286. _bfd_elf_link_hash_table_free (obfd);
  287. }
  288. /* Create an ARC ELF linker hash table. */
  289. static struct bfd_link_hash_table *
  290. arc_elf_link_hash_table_create (bfd *abfd)
  291. {
  292. struct elf_arc_link_hash_table *ret;
  293. ret = (struct elf_arc_link_hash_table *) bfd_zmalloc (sizeof (*ret));
  294. if (ret == NULL)
  295. return NULL;
  296. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  297. elf_arc_link_hash_newfunc,
  298. sizeof (struct elf_arc_link_hash_entry),
  299. ARC_ELF_DATA))
  300. {
  301. free (ret);
  302. return NULL;
  303. }
  304. ret->elf.root.hash_table_free = elf_arc_link_hash_table_free;
  305. return &ret->elf.root;
  306. }
  307. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  308. { BFD_RELOC_##TYPE, R_##TYPE },
  309. static const struct arc_reloc_map arc_reloc_map[] =
  310. {
  311. #include "elf/arc-reloc.def"
  312. {BFD_RELOC_NONE, R_ARC_NONE},
  313. {BFD_RELOC_8, R_ARC_8},
  314. {BFD_RELOC_16, R_ARC_16},
  315. {BFD_RELOC_24, R_ARC_24},
  316. {BFD_RELOC_32, R_ARC_32},
  317. };
  318. #undef ARC_RELOC_HOWTO
  319. typedef ATTRIBUTE_UNUSED unsigned (*replace_func) (unsigned, int ATTRIBUTE_UNUSED);
  320. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  321. case TYPE: \
  322. func = RELOC_FUNCTION; \
  323. break;
  324. static replace_func
  325. get_replace_function (bfd *abfd, unsigned int r_type)
  326. {
  327. replace_func func = NULL;
  328. switch (r_type)
  329. {
  330. #include "elf/arc-reloc.def"
  331. }
  332. if (func == replace_bits24 && bfd_big_endian (abfd))
  333. func = replace_bits24_be;
  334. return func;
  335. }
  336. #undef ARC_RELOC_HOWTO
  337. static reloc_howto_type *
  338. arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  339. bfd_reloc_code_real_type code)
  340. {
  341. unsigned int i;
  342. for (i = ARRAY_SIZE (arc_reloc_map); i--;)
  343. {
  344. if (arc_reloc_map[i].bfd_reloc_val == code)
  345. return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
  346. }
  347. return NULL;
  348. }
  349. /* Function to set the ELF flag bits. */
  350. static bool
  351. arc_elf_set_private_flags (bfd *abfd, flagword flags)
  352. {
  353. elf_elfheader (abfd)->e_flags = flags;
  354. elf_flags_init (abfd) = true;
  355. return true;
  356. }
  357. /* Print private flags. */
  358. static bool
  359. arc_elf_print_private_bfd_data (bfd *abfd, void * ptr)
  360. {
  361. FILE *file = (FILE *) ptr;
  362. flagword flags;
  363. BFD_ASSERT (abfd != NULL && ptr != NULL);
  364. /* Print normal ELF private data. */
  365. _bfd_elf_print_private_bfd_data (abfd, ptr);
  366. flags = elf_elfheader (abfd)->e_flags;
  367. fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
  368. switch (flags & EF_ARC_MACH_MSK)
  369. {
  370. case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS"); break;
  371. case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM"); break;
  372. case E_ARC_MACH_ARC600 : fprintf (file, " -mcpu=ARC600"); break;
  373. case E_ARC_MACH_ARC601 : fprintf (file, " -mcpu=ARC601"); break;
  374. case E_ARC_MACH_ARC700 : fprintf (file, " -mcpu=ARC700"); break;
  375. default:
  376. fprintf (file, "-mcpu=unknown");
  377. break;
  378. }
  379. switch (flags & EF_ARC_OSABI_MSK)
  380. {
  381. case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break;
  382. case E_ARC_OSABI_V2 : fprintf (file, " (ABI:v2)"); break;
  383. case E_ARC_OSABI_V3 : fprintf (file, " (ABI:v3)"); break;
  384. case E_ARC_OSABI_V4 : fprintf (file, " (ABI:v4)"); break;
  385. default:
  386. fprintf (file, " (ABI:unknown)");
  387. break;
  388. }
  389. fputc ('\n', file);
  390. return true;
  391. }
  392. /* Copy backend specific data from one object module to another. */
  393. static bool
  394. arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
  395. {
  396. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  397. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  398. return true;
  399. BFD_ASSERT (!elf_flags_init (obfd)
  400. || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
  401. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  402. elf_flags_init (obfd) = true;
  403. /* Copy object attributes. */
  404. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  405. return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
  406. }
  407. static reloc_howto_type *
  408. bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  409. const char *r_name)
  410. {
  411. unsigned int i;
  412. for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
  413. if (elf_arc_howto_table[i].name != NULL
  414. && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
  415. return arc_elf_howto (i);
  416. return NULL;
  417. }
  418. /* Set the howto pointer for an ARC ELF reloc. */
  419. static bool
  420. arc_info_to_howto_rel (bfd * abfd,
  421. arelent * cache_ptr,
  422. Elf_Internal_Rela * dst)
  423. {
  424. unsigned int r_type;
  425. r_type = ELF32_R_TYPE (dst->r_info);
  426. if (r_type >= (unsigned int) R_ARC_max)
  427. {
  428. /* xgettext:c-format */
  429. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  430. abfd, r_type);
  431. bfd_set_error (bfd_error_bad_value);
  432. return false;
  433. }
  434. cache_ptr->howto = arc_elf_howto (r_type);
  435. return true;
  436. }
  437. /* Extract CPU features from an NTBS. */
  438. static unsigned
  439. arc_extract_features (const char *p)
  440. {
  441. unsigned i, r = 0;
  442. if (!p)
  443. return 0;
  444. for (i = 0; i < ARRAY_SIZE (bfd_feature_list); i++)
  445. {
  446. char *t = strstr (p, bfd_feature_list[i].attr);
  447. unsigned l = strlen (bfd_feature_list[i].attr);
  448. if ((t != NULL)
  449. && (t[l] == ','
  450. || t[l] == '\0'))
  451. r |= bfd_feature_list[i].feature;
  452. }
  453. return r;
  454. }
  455. /* Concatenate two strings. s1 can be NULL but not
  456. s2. */
  457. static char *
  458. arc_stralloc (char * s1, const char * s2)
  459. {
  460. char *p;
  461. /* Only s1 can be null. */
  462. BFD_ASSERT (s2);
  463. p = s1 ? concat (s1, ",", s2, NULL) : (char *)s2;
  464. return p;
  465. }
  466. /* Merge ARC object attributes from IBFD into OBFD. Raise an error if
  467. there are conflicting attributes. */
  468. static bool
  469. arc_elf_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
  470. {
  471. bfd *obfd = info->output_bfd;
  472. obj_attribute *in_attr;
  473. obj_attribute *out_attr;
  474. int i;
  475. bool result = true;
  476. const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
  477. char *tagname = NULL;
  478. /* Skip the linker stubs file. This preserves previous behavior
  479. of accepting unknown attributes in the first input file - but
  480. is that a bug? */
  481. if (ibfd->flags & BFD_LINKER_CREATED)
  482. return true;
  483. /* Skip any input that hasn't attribute section.
  484. This enables to link object files without attribute section with
  485. any others. */
  486. if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
  487. return true;
  488. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  489. {
  490. /* This is the first object. Copy the attributes. */
  491. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  492. out_attr = elf_known_obj_attributes_proc (obfd);
  493. /* Use the Tag_null value to indicate the attributes have been
  494. initialized. */
  495. out_attr[0].i = 1;
  496. return true;
  497. }
  498. in_attr = elf_known_obj_attributes_proc (ibfd);
  499. out_attr = elf_known_obj_attributes_proc (obfd);
  500. for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
  501. {
  502. /* Merge this attribute with existing attributes. */
  503. switch (i)
  504. {
  505. case Tag_ARC_PCS_config:
  506. if (out_attr[i].i == 0)
  507. out_attr[i].i = in_attr[i].i;
  508. else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
  509. {
  510. const char *tagval[] = { "Absent", "Bare-metal/mwdt",
  511. "Bare-metal/newlib", "Linux/uclibc",
  512. "Linux/glibc" };
  513. BFD_ASSERT (in_attr[i].i < 5);
  514. BFD_ASSERT (out_attr[i].i < 5);
  515. /* It's sometimes ok to mix different configs, so this is only
  516. a warning. */
  517. _bfd_error_handler
  518. (_("warning: %pB: conflicting platform configuration "
  519. "%s with %s"), ibfd,
  520. tagval[in_attr[i].i],
  521. tagval[out_attr[i].i]);
  522. }
  523. break;
  524. case Tag_ARC_CPU_base:
  525. if (out_attr[i].i == 0)
  526. out_attr[i].i = in_attr[i].i;
  527. else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i
  528. && ((out_attr[i].i + in_attr[i].i) < 6))
  529. {
  530. const char *tagval[] = { "Absent", "ARC6xx", "ARC7xx",
  531. "ARCEM", "ARCHS" };
  532. BFD_ASSERT (in_attr[i].i < 5);
  533. BFD_ASSERT (out_attr[i].i < 5);
  534. /* We cannot mix code for different CPUs. */
  535. _bfd_error_handler
  536. (_("error: %pB: unable to merge CPU base attributes "
  537. "%s with %s"),
  538. obfd,
  539. tagval[in_attr[i].i],
  540. tagval[out_attr[i].i]);
  541. result = false;
  542. break;
  543. }
  544. else
  545. {
  546. /* The CPUs may be different, check if we can still mix
  547. the objects against the output choosen CPU. */
  548. unsigned in_feature = 0;
  549. unsigned out_feature = 0;
  550. char *p1 = in_attr[Tag_ARC_ISA_config].s;
  551. char *p2 = out_attr[Tag_ARC_ISA_config].s;
  552. unsigned j;
  553. unsigned cpu_out;
  554. unsigned opcode_map[] = {0, ARC_OPCODE_ARC600, ARC_OPCODE_ARC700,
  555. ARC_OPCODE_ARCv2EM, ARC_OPCODE_ARCv2HS};
  556. BFD_ASSERT (in_attr[i].i < (sizeof (opcode_map)
  557. / sizeof (unsigned)));
  558. BFD_ASSERT (out_attr[i].i < (sizeof (opcode_map)
  559. / sizeof (unsigned)));
  560. cpu_out = opcode_map[out_attr[i].i];
  561. in_feature = arc_extract_features (p1);
  562. out_feature = arc_extract_features (p2);
  563. /* First, check if a feature is compatible with the
  564. output object chosen CPU. */
  565. for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
  566. if (((in_feature | out_feature) & bfd_feature_list[j].feature)
  567. && (!(cpu_out & bfd_feature_list[j].cpus)))
  568. {
  569. _bfd_error_handler
  570. (_("error: %pB: unable to merge ISA extension attributes "
  571. "%s"),
  572. obfd, bfd_feature_list[j].name);
  573. result = false;
  574. break;
  575. }
  576. /* Second, if we have compatible features with the
  577. chosen CPU, check if they are compatible among
  578. them. */
  579. for (j = 0; j < ARRAY_SIZE (bfd_conflict_list); j++)
  580. if (((in_feature | out_feature) & bfd_conflict_list[j])
  581. == bfd_conflict_list[j])
  582. {
  583. unsigned k;
  584. for (k = 0; k < ARRAY_SIZE (bfd_feature_list); k++)
  585. {
  586. if (in_feature & bfd_feature_list[k].feature
  587. & bfd_conflict_list[j])
  588. p1 = (char *) bfd_feature_list[k].name;
  589. if (out_feature & bfd_feature_list[k].feature
  590. & bfd_conflict_list[j])
  591. p2 = (char *) bfd_feature_list[k].name;
  592. }
  593. _bfd_error_handler
  594. (_("error: %pB: conflicting ISA extension attributes "
  595. "%s with %s"),
  596. obfd, p1, p2);
  597. result = false;
  598. break;
  599. }
  600. /* Everithing is alright. */
  601. out_feature |= in_feature;
  602. p1 = NULL;
  603. for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
  604. if (out_feature & bfd_feature_list[j].feature)
  605. p1 = arc_stralloc (p1, bfd_feature_list[j].attr);
  606. if (p1)
  607. out_attr[Tag_ARC_ISA_config].s =
  608. _bfd_elf_attr_strdup (obfd, p1);
  609. }
  610. /* Fall through. */
  611. case Tag_ARC_CPU_variation:
  612. case Tag_ARC_ISA_mpy_option:
  613. case Tag_ARC_ABI_osver:
  614. /* Use the largest value specified. */
  615. if (in_attr[i].i > out_attr[i].i)
  616. out_attr[i].i = in_attr[i].i;
  617. break;
  618. /* The CPU name is given by the vendor, just choose an
  619. existing one if missing or different. There are no fail
  620. criteria if they different or both missing. */
  621. case Tag_ARC_CPU_name:
  622. if (!out_attr[i].s && in_attr[i].s)
  623. out_attr[i].s = _bfd_elf_attr_strdup (obfd, in_attr[i].s);
  624. break;
  625. case Tag_ARC_ABI_rf16:
  626. if (out_attr[i].i == 0)
  627. out_attr[i].i = in_attr[i].i;
  628. else if (out_attr[i].i != in_attr[i].i)
  629. {
  630. /* We cannot mix code with rf16 and without. */
  631. _bfd_error_handler
  632. (_("error: %pB: cannot mix rf16 with full register set %pB"),
  633. obfd, ibfd);
  634. result = false;
  635. }
  636. break;
  637. case Tag_ARC_ABI_pic:
  638. tagname = "PIC";
  639. /* fall through */
  640. case Tag_ARC_ABI_sda:
  641. if (!tagname)
  642. tagname = "SDA";
  643. /* fall through */
  644. case Tag_ARC_ABI_tls:
  645. {
  646. const char *tagval[] = { "Absent", "MWDT", "GNU" };
  647. if (!tagname)
  648. tagname = "TLS";
  649. BFD_ASSERT (in_attr[i].i < 3);
  650. BFD_ASSERT (out_attr[i].i < 3);
  651. if (out_attr[i].i == 0)
  652. out_attr[i].i = in_attr[i].i;
  653. else if (out_attr[i].i != 0 && in_attr[i].i != 0
  654. && out_attr[i].i != in_attr[i].i)
  655. {
  656. _bfd_error_handler
  657. (_("error: %pB: conflicting attributes %s: %s with %s"),
  658. obfd, tagname,
  659. tagval[in_attr[i].i],
  660. tagval[out_attr[i].i]);
  661. result = false;
  662. }
  663. tagname = NULL;
  664. break;
  665. }
  666. case Tag_ARC_ABI_double_size:
  667. tagname = "Double size";
  668. /* fall through */
  669. case Tag_ARC_ABI_enumsize:
  670. if (!tagname)
  671. tagname = "Enum size";
  672. /* fall through */
  673. case Tag_ARC_ABI_exceptions:
  674. if (!tagname)
  675. tagname = "ABI exceptions";
  676. if (out_attr[i].i == 0)
  677. out_attr[i].i = in_attr[i].i;
  678. else if (out_attr[i].i != 0 && in_attr[i].i != 0
  679. && out_attr[i].i != in_attr[i].i)
  680. {
  681. _bfd_error_handler
  682. (_("error: %pB: conflicting attributes %s"),
  683. obfd, tagname);
  684. result = false;
  685. }
  686. break;
  687. case Tag_ARC_ISA_apex:
  688. break; /* Do nothing for APEX attributes. */
  689. case Tag_ARC_ISA_config:
  690. /* It is handled in Tag_ARC_CPU_base. */
  691. break;
  692. case Tag_ARC_ATR_version:
  693. if (out_attr[i].i == 0)
  694. out_attr[i].i = in_attr[i].i;
  695. break;
  696. default:
  697. result
  698. = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
  699. }
  700. /* If out_attr was copied from in_attr then it won't have a type yet. */
  701. if (in_attr[i].type && !out_attr[i].type)
  702. out_attr[i].type = in_attr[i].type;
  703. }
  704. /* Merge Tag_compatibility attributes and any common GNU ones. */
  705. if (!_bfd_elf_merge_object_attributes (ibfd, info))
  706. return false;
  707. /* Check for any attributes not known on ARC. */
  708. result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
  709. return result;
  710. }
  711. /* Merge backend specific data from an object file to the output
  712. object file when linking. */
  713. static bool
  714. arc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  715. {
  716. bfd *obfd = info->output_bfd;
  717. unsigned short mach_ibfd;
  718. static unsigned short mach_obfd = EM_NONE;
  719. flagword out_flags;
  720. flagword in_flags;
  721. asection *sec;
  722. /* Check if we have the same endianess. */
  723. if (! _bfd_generic_verify_endian_match (ibfd, info))
  724. return false;
  725. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  726. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  727. return true;
  728. /* Collect ELF flags. */
  729. in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK;
  730. out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK;
  731. if (!elf_flags_init (obfd)) /* First call, no flags set. */
  732. {
  733. elf_flags_init (obfd) = true;
  734. out_flags = in_flags;
  735. }
  736. if (!arc_elf_merge_attributes (ibfd, info))
  737. return false;
  738. /* Check to see if the input BFD actually contains any sections. Do
  739. not short-circuit dynamic objects; their section list may be
  740. emptied by elf_link_add_object_symbols. */
  741. if (!(ibfd->flags & DYNAMIC))
  742. {
  743. bool null_input_bfd = true;
  744. bool only_data_sections = true;
  745. for (sec = ibfd->sections; sec != NULL; sec = sec->next)
  746. {
  747. if ((bfd_section_flags (sec)
  748. & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
  749. == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
  750. only_data_sections = false;
  751. null_input_bfd = false;
  752. }
  753. if (null_input_bfd || only_data_sections)
  754. return true;
  755. }
  756. /* Complain about various flag/architecture mismatches. */
  757. mach_ibfd = elf_elfheader (ibfd)->e_machine;
  758. if (mach_obfd == EM_NONE)
  759. {
  760. mach_obfd = mach_ibfd;
  761. }
  762. else
  763. {
  764. if (mach_ibfd != mach_obfd)
  765. {
  766. /* xgettext:c-format */
  767. _bfd_error_handler (_("error: attempting to link %pB "
  768. "with a binary %pB of different architecture"),
  769. ibfd, obfd);
  770. return false;
  771. }
  772. else if ((in_flags != out_flags)
  773. /* If we have object attributes, then we already
  774. checked the objects compatibility, skip it. */
  775. && !bfd_elf_get_obj_attr_int (ibfd, OBJ_ATTR_PROC,
  776. Tag_ARC_CPU_base))
  777. {
  778. if (in_flags && out_flags)
  779. {
  780. /* Warn if different flags. */
  781. _bfd_error_handler
  782. /* xgettext:c-format */
  783. (_("%pB: uses different e_flags (%#x) fields than "
  784. "previous modules (%#x)"),
  785. ibfd, in_flags, out_flags);
  786. return false;
  787. }
  788. /* MWDT doesnt set the eflags hence make sure we choose the
  789. eflags set by gcc. */
  790. in_flags = in_flags > out_flags ? in_flags : out_flags;
  791. }
  792. else
  793. {
  794. /* Everything is correct; don't change the output flags. */
  795. in_flags = out_flags;
  796. }
  797. }
  798. /* Update the flags. */
  799. elf_elfheader (obfd)->e_flags = in_flags;
  800. if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
  801. {
  802. return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd));
  803. }
  804. return true;
  805. }
  806. /* Return a best guess for the machine number based on the attributes. */
  807. static unsigned int
  808. bfd_arc_get_mach_from_attributes (bfd * abfd)
  809. {
  810. int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ARC_CPU_base);
  811. unsigned e_machine = elf_elfheader (abfd)->e_machine;
  812. switch (arch)
  813. {
  814. case TAG_CPU_ARC6xx:
  815. return bfd_mach_arc_arc600;
  816. case TAG_CPU_ARC7xx:
  817. return bfd_mach_arc_arc700;
  818. case TAG_CPU_ARCEM:
  819. case TAG_CPU_ARCHS:
  820. return bfd_mach_arc_arcv2;
  821. default:
  822. break;
  823. }
  824. return (e_machine == EM_ARC_COMPACT)
  825. ? bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
  826. }
  827. /* Set the right machine number for an ARC ELF file. */
  828. static bool
  829. arc_elf_object_p (bfd * abfd)
  830. {
  831. /* Make sure this is initialised, or you'll have the potential of passing
  832. garbage---or misleading values---into the call to
  833. bfd_default_set_arch_mach (). */
  834. unsigned int mach = bfd_mach_arc_arc700;
  835. unsigned long arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK;
  836. unsigned e_machine = elf_elfheader (abfd)->e_machine;
  837. if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
  838. {
  839. switch (arch)
  840. {
  841. case E_ARC_MACH_ARC600:
  842. mach = bfd_mach_arc_arc600;
  843. break;
  844. case E_ARC_MACH_ARC601:
  845. mach = bfd_mach_arc_arc601;
  846. break;
  847. case E_ARC_MACH_ARC700:
  848. mach = bfd_mach_arc_arc700;
  849. break;
  850. case EF_ARC_CPU_ARCV2HS:
  851. case EF_ARC_CPU_ARCV2EM:
  852. mach = bfd_mach_arc_arcv2;
  853. break;
  854. default:
  855. mach = bfd_arc_get_mach_from_attributes (abfd);
  856. break;
  857. }
  858. }
  859. else
  860. {
  861. if (e_machine == EM_ARC)
  862. {
  863. _bfd_error_handler
  864. (_("error: the ARC4 architecture is no longer supported"));
  865. return false;
  866. }
  867. else
  868. {
  869. _bfd_error_handler
  870. (_("warning: unset or old architecture flags; "
  871. "use default machine"));
  872. }
  873. }
  874. return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
  875. }
  876. /* The final processing done just before writing out an ARC ELF object file.
  877. This gets the ARC architecture right based on the machine number. */
  878. static bool
  879. arc_elf_final_write_processing (bfd *abfd)
  880. {
  881. unsigned long emf;
  882. int osver = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC,
  883. Tag_ARC_ABI_osver);
  884. flagword e_flags = elf_elfheader (abfd)->e_flags & ~EF_ARC_OSABI_MSK;
  885. switch (bfd_get_mach (abfd))
  886. {
  887. case bfd_mach_arc_arcv2:
  888. emf = EM_ARC_COMPACT2;
  889. break;
  890. default:
  891. emf = EM_ARC_COMPACT;
  892. break;
  893. }
  894. elf_elfheader (abfd)->e_machine = emf;
  895. /* Record whatever is the current syscall ABI version. */
  896. if (osver)
  897. e_flags |= ((osver & 0x0f) << 8);
  898. else
  899. e_flags |= E_ARC_OSABI_V3;
  900. elf_elfheader (abfd)->e_flags |= e_flags;
  901. return _bfd_elf_final_write_processing (abfd);
  902. }
  903. #ifdef ARC_ENABLE_DEBUG
  904. #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
  905. static void
  906. debug_arc_reloc (struct arc_relocation_data reloc_data)
  907. {
  908. ARC_DEBUG ("Reloc type=%s, should_relocate = %s\n",
  909. reloc_data.howto->name,
  910. reloc_data.should_relocate ? "true" : "false");
  911. ARC_DEBUG (" offset = 0x%x, addend = 0x%x\n",
  912. (unsigned int) reloc_data.reloc_offset,
  913. (unsigned int) reloc_data.reloc_addend);
  914. ARC_DEBUG (" Symbol:\n");
  915. ARC_DEBUG (" value = 0x%08x\n",
  916. (unsigned int) reloc_data.sym_value);
  917. if (reloc_data.sym_section != NULL)
  918. {
  919. ARC_DEBUG (" Symbol Section:\n");
  920. ARC_DEBUG (" section name = %s, output_offset 0x%08x",
  921. reloc_data.sym_section->name,
  922. (unsigned int) reloc_data.sym_section->output_offset);
  923. if (reloc_data.sym_section->output_section != NULL)
  924. ARC_DEBUG (", output_section->vma = 0x%08x",
  925. ((unsigned int) reloc_data.sym_section->output_section->vma));
  926. ARC_DEBUG ("\n");
  927. if (reloc_data.sym_section->owner
  928. && reloc_data.sym_section->owner->filename)
  929. ARC_DEBUG (" file: %s\n", reloc_data.sym_section->owner->filename);
  930. }
  931. else
  932. {
  933. ARC_DEBUG (" symbol section is NULL\n");
  934. }
  935. ARC_DEBUG (" Input_section:\n");
  936. if (reloc_data.input_section != NULL)
  937. {
  938. ARC_DEBUG (" section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
  939. reloc_data.input_section->name,
  940. (unsigned int) reloc_data.input_section->output_offset,
  941. (unsigned int) reloc_data.input_section->output_section->vma);
  942. ARC_DEBUG (" changed_address = 0x%08x\n",
  943. (unsigned int) (reloc_data.input_section->output_section->vma
  944. + reloc_data.input_section->output_offset
  945. + reloc_data.reloc_offset));
  946. ARC_DEBUG (" file: %s\n", reloc_data.input_section->owner->filename);
  947. }
  948. else
  949. {
  950. ARC_DEBUG (" input section is NULL\n");
  951. }
  952. }
  953. #else
  954. #define DEBUG_ARC_RELOC(A)
  955. #endif /* ARC_ENABLE_DEBUG */
  956. static bfd_vma
  957. middle_endian_convert (bfd_vma insn, bool do_it)
  958. {
  959. if (do_it)
  960. {
  961. insn
  962. = ((insn & 0xffff0000) >> 16)
  963. | ((insn & 0xffff) << 16);
  964. }
  965. return insn;
  966. }
  967. /* This function is called for relocations that are otherwise marked as NOT
  968. requiring overflow checks. In here we perform non-standard checks of
  969. the relocation value. */
  970. static inline bfd_reloc_status_type
  971. arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
  972. bfd_signed_vma relocation,
  973. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  974. {
  975. switch (reloc_data.howto->type)
  976. {
  977. case R_ARC_NPS_CMEM16:
  978. if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
  979. {
  980. if (reloc_data.reloc_addend == 0)
  981. _bfd_error_handler
  982. /* xgettext:c-format */
  983. (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s' is invalid, "
  984. "16 MSB should be %#x (value is %#" PRIx64 ")"),
  985. reloc_data.input_section->owner,
  986. reloc_data.input_section,
  987. (uint64_t) reloc_data.reloc_offset,
  988. reloc_data.symbol_name,
  989. NPS_CMEM_HIGH_VALUE,
  990. (uint64_t) relocation);
  991. else
  992. _bfd_error_handler
  993. /* xgettext:c-format */
  994. (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s+%#" PRIx64
  995. "' is invalid, 16 MSB should be %#x (value is %#" PRIx64 ")"),
  996. reloc_data.input_section->owner,
  997. reloc_data.input_section,
  998. (uint64_t) reloc_data.reloc_offset,
  999. reloc_data.symbol_name,
  1000. (uint64_t) reloc_data.reloc_addend,
  1001. NPS_CMEM_HIGH_VALUE,
  1002. (uint64_t) relocation);
  1003. return bfd_reloc_overflow;
  1004. }
  1005. break;
  1006. default:
  1007. break;
  1008. }
  1009. return bfd_reloc_ok;
  1010. }
  1011. #define ME(reloc) (reloc)
  1012. #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
  1013. && (!bfd_big_endian (BFD)))
  1014. #define S ((bfd_signed_vma) (reloc_data.sym_value \
  1015. + (reloc_data.sym_section->output_section != NULL ? \
  1016. (reloc_data.sym_section->output_offset \
  1017. + reloc_data.sym_section->output_section->vma) : 0)))
  1018. #define L ((bfd_signed_vma) (reloc_data.sym_value \
  1019. + (reloc_data.sym_section->output_section != NULL ? \
  1020. (reloc_data.sym_section->output_offset \
  1021. + reloc_data.sym_section->output_section->vma) : 0)))
  1022. #define A (reloc_data.reloc_addend)
  1023. #define B (0)
  1024. #define G (reloc_data.got_offset_value)
  1025. #define GOT (reloc_data.got_symbol_vma)
  1026. #define GOT_BEGIN (htab->sgot->output_section->vma)
  1027. #define MES (0)
  1028. /* P: relative offset to PCL The offset should be to the
  1029. current location aligned to 32 bits. */
  1030. #define P ((bfd_signed_vma) ( \
  1031. ( \
  1032. (reloc_data.input_section->output_section != NULL ? \
  1033. reloc_data.input_section->output_section->vma : 0) \
  1034. + reloc_data.input_section->output_offset \
  1035. + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \
  1036. & ~0x3))
  1037. #define PDATA ((bfd_signed_vma) ( \
  1038. (reloc_data.input_section->output_section->vma \
  1039. + reloc_data.input_section->output_offset \
  1040. + (reloc_data.reloc_offset))))
  1041. #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
  1042. + reloc_data.sym_section->output_offset)
  1043. #define FINAL_SECTSTART \
  1044. (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
  1045. #define JLI (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
  1046. #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
  1047. #define TLS_REL (bfd_signed_vma) \
  1048. ((elf_hash_table (info))->tls_sec->output_section->vma)
  1049. #define TLS_TBSS (align_power(TCB_SIZE, \
  1050. reloc_data.sym_section->alignment_power))
  1051. #define none (0)
  1052. #ifdef ARC_ENABLE_DEBUG
  1053. #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
  1054. do \
  1055. { \
  1056. asection *sym_section = reloc_data.sym_section; \
  1057. asection *input_section = reloc_data.input_section; \
  1058. ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
  1059. ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
  1060. ARC_DEBUG ("S = %#lx\n", S); \
  1061. ARC_DEBUG ("A = %#lx\n", A); \
  1062. ARC_DEBUG ("L = %lx\n", L); \
  1063. if (sym_section->output_section != NULL) \
  1064. ARC_DEBUG ("symbol_section->vma = %#lx\n", \
  1065. sym_section->output_section->vma \
  1066. + sym_section->output_offset); \
  1067. else \
  1068. ARC_DEBUG ("symbol_section->vma = NULL\n"); \
  1069. if (input_section->output_section != NULL) \
  1070. ARC_DEBUG ("input_section->vma = %#lx\n", \
  1071. input_section->output_section->vma \
  1072. + input_section->output_offset); \
  1073. else \
  1074. ARC_DEBUG ("input_section->vma = NULL\n"); \
  1075. ARC_DEBUG ("PCL = %#lx\n", P); \
  1076. ARC_DEBUG ("P = %#lx\n", P); \
  1077. ARC_DEBUG ("G = %#lx\n", G); \
  1078. ARC_DEBUG ("SDA_OFFSET = %#lx\n", _SDA_BASE_); \
  1079. ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
  1080. ARC_DEBUG ("GOT_OFFSET = %#lx\n", GOT); \
  1081. ARC_DEBUG ("relocation = %#08lx\n", relocation); \
  1082. ARC_DEBUG ("before = %#08x\n", (unsigned) insn); \
  1083. ARC_DEBUG ("data = %08x (%u) (%d)\n", (unsigned) relocation, \
  1084. (unsigned) relocation, (int) relocation); \
  1085. } \
  1086. while (0)
  1087. #define PRINT_DEBUG_RELOC_INFO_AFTER \
  1088. do \
  1089. { \
  1090. ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \
  1091. } \
  1092. while (0)
  1093. #else
  1094. #define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
  1095. #define PRINT_DEBUG_RELOC_INFO_AFTER
  1096. #endif /* ARC_ENABLE_DEBUG */
  1097. #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  1098. case R_##TYPE: \
  1099. { \
  1100. bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
  1101. relocation = FORMULA ; \
  1102. PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
  1103. insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
  1104. insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
  1105. insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
  1106. PRINT_DEBUG_RELOC_INFO_AFTER; \
  1107. } \
  1108. break;
  1109. static bfd_reloc_status_type
  1110. arc_do_relocation (bfd_byte * contents,
  1111. struct arc_relocation_data reloc_data,
  1112. struct bfd_link_info *info)
  1113. {
  1114. bfd_signed_vma relocation = 0;
  1115. bfd_vma insn;
  1116. bfd_vma orig_insn ATTRIBUTE_UNUSED;
  1117. bfd * abfd = reloc_data.input_section->owner;
  1118. struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
  1119. bfd_reloc_status_type flag;
  1120. if (!reloc_data.should_relocate)
  1121. return bfd_reloc_ok;
  1122. switch (reloc_data.howto->size)
  1123. {
  1124. case 2:
  1125. insn = arc_bfd_get_32 (abfd,
  1126. contents + reloc_data.reloc_offset,
  1127. reloc_data.input_section);
  1128. break;
  1129. case 1:
  1130. insn = arc_bfd_get_16 (abfd,
  1131. contents + reloc_data.reloc_offset,
  1132. reloc_data.input_section);
  1133. break;
  1134. case 0:
  1135. insn = arc_bfd_get_8 (abfd,
  1136. contents + reloc_data.reloc_offset,
  1137. reloc_data.input_section);
  1138. break;
  1139. default:
  1140. insn = 0;
  1141. BFD_ASSERT (0);
  1142. break;
  1143. }
  1144. orig_insn = insn;
  1145. switch (reloc_data.howto->type)
  1146. {
  1147. #include "elf/arc-reloc.def"
  1148. default:
  1149. BFD_ASSERT (0);
  1150. break;
  1151. }
  1152. /* Check for relocation overflow. */
  1153. if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
  1154. flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
  1155. reloc_data.howto->bitsize,
  1156. reloc_data.howto->rightshift,
  1157. bfd_arch_bits_per_address (abfd),
  1158. relocation);
  1159. else
  1160. flag = arc_special_overflow_checks (reloc_data, relocation, info);
  1161. if (flag != bfd_reloc_ok)
  1162. {
  1163. ARC_DEBUG ("Relocation overflows !\n");
  1164. DEBUG_ARC_RELOC (reloc_data);
  1165. ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u"
  1166. ", hex -> (0x%08x)\n",
  1167. (int) relocation, (unsigned) relocation, (int) relocation);
  1168. return flag;
  1169. }
  1170. /* Write updated instruction back to memory. */
  1171. switch (reloc_data.howto->size)
  1172. {
  1173. case 2:
  1174. arc_bfd_put_32 (abfd, insn,
  1175. contents + reloc_data.reloc_offset,
  1176. reloc_data.input_section);
  1177. break;
  1178. case 1:
  1179. arc_bfd_put_16 (abfd, insn,
  1180. contents + reloc_data.reloc_offset,
  1181. reloc_data.input_section);
  1182. break;
  1183. case 0:
  1184. arc_bfd_put_8 (abfd, insn,
  1185. contents + reloc_data.reloc_offset,
  1186. reloc_data.input_section);
  1187. break;
  1188. default:
  1189. ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
  1190. BFD_ASSERT (0);
  1191. break;
  1192. }
  1193. return bfd_reloc_ok;
  1194. }
  1195. #undef S
  1196. #undef A
  1197. #undef B
  1198. #undef G
  1199. #undef GOT
  1200. #undef L
  1201. #undef MES
  1202. #undef P
  1203. #undef SECTSTAR
  1204. #undef SECTSTART
  1205. #undef JLI
  1206. #undef _SDA_BASE_
  1207. #undef none
  1208. #undef ARC_RELOC_HOWTO
  1209. /* Relocate an arc ELF section.
  1210. Function : elf_arc_relocate_section
  1211. Brief : Relocate an arc section, by handling all the relocations
  1212. appearing in that section.
  1213. Args : output_bfd : The bfd being written to.
  1214. info : Link information.
  1215. input_bfd : The input bfd.
  1216. input_section : The section being relocated.
  1217. contents : contents of the section being relocated.
  1218. relocs : List of relocations in the section.
  1219. local_syms : is a pointer to the swapped in local symbols.
  1220. local_section : is an array giving the section in the input file
  1221. corresponding to the st_shndx field of each
  1222. local symbol. */
  1223. static int
  1224. elf_arc_relocate_section (bfd * output_bfd,
  1225. struct bfd_link_info * info,
  1226. bfd * input_bfd,
  1227. asection * input_section,
  1228. bfd_byte * contents,
  1229. Elf_Internal_Rela * relocs,
  1230. Elf_Internal_Sym * local_syms,
  1231. asection ** local_sections)
  1232. {
  1233. Elf_Internal_Shdr * symtab_hdr;
  1234. struct elf_link_hash_entry ** sym_hashes;
  1235. Elf_Internal_Rela * rel;
  1236. Elf_Internal_Rela * wrel;
  1237. Elf_Internal_Rela * relend;
  1238. struct elf_link_hash_table * htab = elf_hash_table (info);
  1239. symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
  1240. sym_hashes = elf_sym_hashes (input_bfd);
  1241. rel = wrel = relocs;
  1242. relend = relocs + input_section->reloc_count;
  1243. for (; rel < relend; wrel++, rel++)
  1244. {
  1245. enum elf_arc_reloc_type r_type;
  1246. reloc_howto_type *howto;
  1247. unsigned long r_symndx;
  1248. struct elf_link_hash_entry *h;
  1249. Elf_Internal_Sym *sym;
  1250. asection *sec;
  1251. struct elf_link_hash_entry *h2;
  1252. const char *msg;
  1253. bool unresolved_reloc = false;
  1254. struct arc_relocation_data reloc_data =
  1255. {
  1256. .reloc_offset = 0,
  1257. .reloc_addend = 0,
  1258. .got_offset_value = 0,
  1259. .sym_value = 0,
  1260. .sym_section = NULL,
  1261. .howto = NULL,
  1262. .input_section = NULL,
  1263. .sdata_begin_symbol_vma = 0,
  1264. .sdata_begin_symbol_vma_set = false,
  1265. .got_symbol_vma = 0,
  1266. .should_relocate = false
  1267. };
  1268. r_type = ELF32_R_TYPE (rel->r_info);
  1269. if (r_type >= (int) R_ARC_max)
  1270. {
  1271. bfd_set_error (bfd_error_bad_value);
  1272. return false;
  1273. }
  1274. howto = arc_elf_howto (r_type);
  1275. r_symndx = ELF32_R_SYM (rel->r_info);
  1276. /* If we are generating another .o file and the symbol in not
  1277. local, skip this relocation. */
  1278. if (bfd_link_relocatable (info))
  1279. {
  1280. /* This is a relocateable link. We don't have to change
  1281. anything, unless the reloc is against a section symbol,
  1282. in which case we have to adjust according to where the
  1283. section symbol winds up in the output section. */
  1284. /* Checks if this is a local symbol and thus the reloc
  1285. might (will??) be against a section symbol. */
  1286. if (r_symndx < symtab_hdr->sh_info)
  1287. {
  1288. sym = local_syms + r_symndx;
  1289. if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  1290. {
  1291. sec = local_sections[r_symndx];
  1292. /* For RELA relocs. Just adjust the addend
  1293. value in the relocation entry. */
  1294. rel->r_addend += sec->output_offset + sym->st_value;
  1295. ARC_DEBUG ("local symbols reloc (section=%d %s) seen in %s\n",
  1296. (int) r_symndx, local_sections[r_symndx]->name,
  1297. __PRETTY_FUNCTION__);
  1298. }
  1299. }
  1300. }
  1301. h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
  1302. false, false, true);
  1303. if (!reloc_data.sdata_begin_symbol_vma_set
  1304. && h2 != NULL && h2->root.type != bfd_link_hash_undefined
  1305. && h2->root.u.def.section->output_section != NULL)
  1306. /* TODO: Verify this condition. */
  1307. {
  1308. reloc_data.sdata_begin_symbol_vma =
  1309. (h2->root.u.def.value
  1310. + h2->root.u.def.section->output_section->vma);
  1311. reloc_data.sdata_begin_symbol_vma_set = true;
  1312. }
  1313. reloc_data.input_section = input_section;
  1314. reloc_data.howto = howto;
  1315. reloc_data.reloc_offset = rel->r_offset;
  1316. reloc_data.reloc_addend = rel->r_addend;
  1317. /* This is a final link. */
  1318. h = NULL;
  1319. sym = NULL;
  1320. sec = NULL;
  1321. if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
  1322. {
  1323. sym = local_syms + r_symndx;
  1324. sec = local_sections[r_symndx];
  1325. }
  1326. else
  1327. {
  1328. bool warned, ignored;
  1329. bfd_vma relocation ATTRIBUTE_UNUSED;
  1330. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1331. r_symndx, symtab_hdr, sym_hashes,
  1332. h, sec, relocation,
  1333. unresolved_reloc, warned, ignored);
  1334. /* TODO: This code is repeated from below. We should
  1335. clean it and remove duplications.
  1336. Sec is used check for discarded sections.
  1337. Need to redesign code below. */
  1338. /* Get the symbol's entry in the symtab. */
  1339. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1340. while (h->root.type == bfd_link_hash_indirect
  1341. || h->root.type == bfd_link_hash_warning)
  1342. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1343. /* If we have encountered a definition for this symbol. */
  1344. if (h->root.type == bfd_link_hash_defined
  1345. || h->root.type == bfd_link_hash_defweak)
  1346. {
  1347. reloc_data.sym_value = h->root.u.def.value;
  1348. sec = h->root.u.def.section;
  1349. }
  1350. }
  1351. /* Clean relocs for symbols in discarded sections. */
  1352. if (sec != NULL && discarded_section (sec))
  1353. {
  1354. _bfd_clear_contents (howto, input_bfd, input_section,
  1355. contents, rel->r_offset);
  1356. rel->r_info = 0;
  1357. rel->r_addend = 0;
  1358. /* For ld -r, remove relocations in debug sections against
  1359. sections defined in discarded sections. Not done for
  1360. eh_frame editing code expects to be present. */
  1361. if (bfd_link_relocatable (info)
  1362. && (input_section->flags & SEC_DEBUGGING))
  1363. wrel--;
  1364. continue;
  1365. }
  1366. if (bfd_link_relocatable (info))
  1367. {
  1368. if (wrel != rel)
  1369. *wrel = *rel;
  1370. continue;
  1371. }
  1372. if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
  1373. {
  1374. reloc_data.sym_value = sym->st_value;
  1375. reloc_data.sym_section = sec;
  1376. reloc_data.symbol_name =
  1377. bfd_elf_string_from_elf_section (input_bfd,
  1378. symtab_hdr->sh_link,
  1379. sym->st_name);
  1380. /* Mergeable section handling. */
  1381. if ((sec->flags & SEC_MERGE)
  1382. && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  1383. {
  1384. asection *msec;
  1385. msec = sec;
  1386. rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym,
  1387. &msec, rel->r_addend);
  1388. rel->r_addend -= (sec->output_section->vma
  1389. + sec->output_offset
  1390. + sym->st_value);
  1391. rel->r_addend += msec->output_section->vma + msec->output_offset;
  1392. reloc_data.reloc_addend = rel->r_addend;
  1393. }
  1394. BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
  1395. if (htab->sgot != NULL)
  1396. reloc_data.got_symbol_vma = htab->sgot->output_section->vma
  1397. + htab->sgot->output_offset;
  1398. reloc_data.should_relocate = true;
  1399. }
  1400. else /* Global symbol. */
  1401. {
  1402. /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
  1403. (defined in elf-bfd.h) here. */
  1404. /* Get the symbol's entry in the symtab. */
  1405. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1406. while (h->root.type == bfd_link_hash_indirect
  1407. || h->root.type == bfd_link_hash_warning)
  1408. {
  1409. struct elf_arc_link_hash_entry *ah_old =
  1410. (struct elf_arc_link_hash_entry *) h;
  1411. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1412. struct elf_arc_link_hash_entry *ah =
  1413. (struct elf_arc_link_hash_entry *) h;
  1414. if (ah->got_ents == 0 && ah_old->got_ents != ah->got_ents)
  1415. ah->got_ents = ah_old->got_ents;
  1416. }
  1417. /* TODO: Need to validate what was the intention. */
  1418. /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
  1419. reloc_data.symbol_name = h->root.root.string;
  1420. /* If we have encountered a definition for this symbol. */
  1421. if (h->root.type == bfd_link_hash_defined
  1422. || h->root.type == bfd_link_hash_defweak)
  1423. {
  1424. reloc_data.sym_value = h->root.u.def.value;
  1425. reloc_data.sym_section = h->root.u.def.section;
  1426. reloc_data.should_relocate = true;
  1427. if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
  1428. {
  1429. struct elf_arc_link_hash_entry *ah =
  1430. (struct elf_arc_link_hash_entry *) h;
  1431. /* TODO: Change it to use arc_do_relocation with
  1432. ARC_32 reloc. Try to use ADD_RELA macro. */
  1433. bfd_vma relocation =
  1434. reloc_data.sym_value + reloc_data.reloc_addend
  1435. + (reloc_data.sym_section->output_section != NULL ?
  1436. (reloc_data.sym_section->output_offset
  1437. + reloc_data.sym_section->output_section->vma)
  1438. : 0);
  1439. BFD_ASSERT (ah->got_ents);
  1440. bfd_vma got_offset = ah->got_ents->offset;
  1441. bfd_put_32 (output_bfd, relocation,
  1442. htab->sgot->contents + got_offset);
  1443. }
  1444. if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
  1445. {
  1446. /* TODO: This is repeated up here. */
  1447. reloc_data.sym_value = h->plt.offset;
  1448. reloc_data.sym_section = htab->splt;
  1449. }
  1450. }
  1451. else if (h->root.type == bfd_link_hash_undefweak)
  1452. {
  1453. /* Is weak symbol and has no definition. */
  1454. if (is_reloc_for_GOT (howto))
  1455. {
  1456. reloc_data.sym_value = h->root.u.def.value;
  1457. reloc_data.sym_section = htab->sgot;
  1458. reloc_data.should_relocate = true;
  1459. }
  1460. else if (is_reloc_for_PLT (howto)
  1461. && h->plt.offset != (bfd_vma) -1)
  1462. {
  1463. /* TODO: This is repeated up here. */
  1464. reloc_data.sym_value = h->plt.offset;
  1465. reloc_data.sym_section = htab->splt;
  1466. reloc_data.should_relocate = true;
  1467. }
  1468. else
  1469. continue;
  1470. }
  1471. else
  1472. {
  1473. if (is_reloc_for_GOT (howto))
  1474. {
  1475. reloc_data.sym_value = h->root.u.def.value;
  1476. reloc_data.sym_section = htab->sgot;
  1477. reloc_data.should_relocate = true;
  1478. }
  1479. else if (is_reloc_for_PLT (howto))
  1480. {
  1481. /* Fail if it is linking for PIE and the symbol is
  1482. undefined. */
  1483. if (bfd_link_executable (info))
  1484. (*info->callbacks->undefined_symbol)
  1485. (info, h->root.root.string, input_bfd, input_section,
  1486. rel->r_offset, true);
  1487. reloc_data.sym_value = h->plt.offset;
  1488. reloc_data.sym_section = htab->splt;
  1489. reloc_data.should_relocate = true;
  1490. }
  1491. else if (!bfd_link_pic (info) || bfd_link_executable (info))
  1492. (*info->callbacks->undefined_symbol)
  1493. (info, h->root.root.string, input_bfd, input_section,
  1494. rel->r_offset, true);
  1495. }
  1496. BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
  1497. if (htab->sgot != NULL)
  1498. reloc_data.got_symbol_vma = htab->sgot->output_section->vma
  1499. + htab->sgot->output_offset;
  1500. }
  1501. if ((is_reloc_for_GOT (howto)
  1502. || is_reloc_for_TLS (howto)))
  1503. {
  1504. reloc_data.should_relocate = true;
  1505. struct got_entry **list
  1506. = get_got_entry_list_for_symbol (input_bfd, r_symndx, h);
  1507. reloc_data.got_offset_value
  1508. = relocate_fix_got_relocs_for_got_info (list,
  1509. tls_type_for_reloc (howto),
  1510. info,
  1511. output_bfd,
  1512. r_symndx,
  1513. local_syms,
  1514. local_sections,
  1515. h,
  1516. &reloc_data);
  1517. if (h == NULL)
  1518. {
  1519. create_got_dynrelocs_for_single_entry (
  1520. got_entry_for_type (list,
  1521. arc_got_entry_type_for_reloc (howto)),
  1522. output_bfd, info, NULL);
  1523. }
  1524. }
  1525. #define IS_ARC_PCREL_TYPE(TYPE) \
  1526. ( (TYPE == R_ARC_PC32) \
  1527. || (TYPE == R_ARC_32_PCREL))
  1528. switch (r_type)
  1529. {
  1530. case R_ARC_32:
  1531. case R_ARC_32_ME:
  1532. case R_ARC_PC32:
  1533. case R_ARC_32_PCREL:
  1534. if (bfd_link_pic (info)
  1535. && (input_section->flags & SEC_ALLOC) != 0
  1536. && (!IS_ARC_PCREL_TYPE (r_type)
  1537. || (h != NULL
  1538. && h->dynindx != -1
  1539. && !h->def_regular
  1540. && (!info->symbolic || !h->def_regular))))
  1541. {
  1542. Elf_Internal_Rela outrel;
  1543. bfd_byte *loc;
  1544. bool skip = false;
  1545. bool relocate = false;
  1546. asection *sreloc = _bfd_elf_get_dynamic_reloc_section
  1547. (input_bfd, input_section,
  1548. /*RELA*/ true);
  1549. BFD_ASSERT (sreloc != NULL);
  1550. outrel.r_offset = _bfd_elf_section_offset (output_bfd,
  1551. info,
  1552. input_section,
  1553. rel->r_offset);
  1554. if (outrel.r_offset == (bfd_vma) -1)
  1555. skip = true;
  1556. outrel.r_addend = rel->r_addend;
  1557. outrel.r_offset += (input_section->output_section->vma
  1558. + input_section->output_offset);
  1559. if (skip)
  1560. {
  1561. memset (&outrel, 0, sizeof outrel);
  1562. relocate = false;
  1563. }
  1564. else if (h != NULL
  1565. && h->dynindx != -1
  1566. && (IS_ARC_PCREL_TYPE (r_type)
  1567. || !(bfd_link_executable (info)
  1568. || SYMBOLIC_BIND (info, h))
  1569. || ! h->def_regular))
  1570. {
  1571. BFD_ASSERT (h != NULL);
  1572. if ((input_section->flags & SEC_ALLOC) != 0)
  1573. relocate = false;
  1574. else
  1575. relocate = true;
  1576. BFD_ASSERT (h->dynindx != -1);
  1577. outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
  1578. }
  1579. else
  1580. {
  1581. /* Handle local symbols, they either do not have a
  1582. global hash table entry (h == NULL), or are
  1583. forced local due to a version script
  1584. (h->forced_local), or the third condition is
  1585. legacy, it appears to say something like, for
  1586. links where we are pre-binding the symbols, or
  1587. there's not an entry for this symbol in the
  1588. dynamic symbol table, and it's a regular symbol
  1589. not defined in a shared object, then treat the
  1590. symbol as local, resolve it now. */
  1591. relocate = true;
  1592. /* outrel.r_addend = 0; */
  1593. outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
  1594. }
  1595. BFD_ASSERT (sreloc->contents != 0);
  1596. loc = sreloc->contents;
  1597. loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
  1598. sreloc->reloc_count += 1;
  1599. bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
  1600. if (!relocate)
  1601. continue;
  1602. }
  1603. break;
  1604. default:
  1605. break;
  1606. }
  1607. if (is_reloc_SDA_relative (howto)
  1608. && !reloc_data.sdata_begin_symbol_vma_set)
  1609. {
  1610. _bfd_error_handler
  1611. ("error: linker symbol __SDATA_BEGIN__ not found");
  1612. bfd_set_error (bfd_error_bad_value);
  1613. return false;
  1614. }
  1615. DEBUG_ARC_RELOC (reloc_data);
  1616. /* Make sure we have with a dynamic linker. In case of GOT and PLT
  1617. the sym_section should point to .got or .plt respectively. */
  1618. if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
  1619. && reloc_data.sym_section == NULL)
  1620. {
  1621. _bfd_error_handler
  1622. (_("GOT and PLT relocations cannot be fixed with a non dynamic linker"));
  1623. bfd_set_error (bfd_error_bad_value);
  1624. return false;
  1625. }
  1626. msg = NULL;
  1627. switch (arc_do_relocation (contents, reloc_data, info))
  1628. {
  1629. case bfd_reloc_ok:
  1630. continue; /* The reloc processing loop. */
  1631. case bfd_reloc_overflow:
  1632. (*info->callbacks->reloc_overflow)
  1633. (info, (h ? &h->root : NULL), reloc_data.symbol_name, howto->name, (bfd_vma) 0,
  1634. input_bfd, input_section, rel->r_offset);
  1635. break;
  1636. case bfd_reloc_undefined:
  1637. (*info->callbacks->undefined_symbol)
  1638. (info, reloc_data.symbol_name, input_bfd, input_section, rel->r_offset, true);
  1639. break;
  1640. case bfd_reloc_other:
  1641. /* xgettext:c-format */
  1642. msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
  1643. break;
  1644. case bfd_reloc_outofrange:
  1645. /* xgettext:c-format */
  1646. msg = _("%pB(%pA): internal error: out of range error");
  1647. break;
  1648. case bfd_reloc_notsupported:
  1649. /* xgettext:c-format */
  1650. msg = _("%pB(%pA): internal error: unsupported relocation error");
  1651. break;
  1652. case bfd_reloc_dangerous:
  1653. /* xgettext:c-format */
  1654. msg = _("%pB(%pA): internal error: dangerous relocation");
  1655. break;
  1656. default:
  1657. /* xgettext:c-format */
  1658. msg = _("%pB(%pA): internal error: unknown error");
  1659. break;
  1660. }
  1661. if (msg)
  1662. _bfd_error_handler (msg, input_bfd, input_section, reloc_data.symbol_name);
  1663. return false;
  1664. }
  1665. return true;
  1666. }
  1667. #define elf_arc_hash_table(p) \
  1668. ((is_elf_hash_table ((p)->hash) \
  1669. && elf_hash_table_id (elf_hash_table (p)) == ARC_ELF_DATA) \
  1670. ? (struct elf_arc_link_hash_table *) (p)->hash : NULL)
  1671. static bool
  1672. elf_arc_check_relocs (bfd * abfd,
  1673. struct bfd_link_info * info,
  1674. asection * sec,
  1675. const Elf_Internal_Rela * relocs)
  1676. {
  1677. Elf_Internal_Shdr * symtab_hdr;
  1678. struct elf_link_hash_entry ** sym_hashes;
  1679. const Elf_Internal_Rela * rel;
  1680. const Elf_Internal_Rela * rel_end;
  1681. bfd * dynobj;
  1682. asection * sreloc = NULL;
  1683. struct elf_link_hash_table * htab = elf_hash_table (info);
  1684. if (bfd_link_relocatable (info))
  1685. return true;
  1686. if (htab->dynobj == NULL)
  1687. htab->dynobj = abfd;
  1688. dynobj = (elf_hash_table (info))->dynobj;
  1689. symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);
  1690. sym_hashes = elf_sym_hashes (abfd);
  1691. rel_end = relocs + sec->reloc_count;
  1692. for (rel = relocs; rel < rel_end; rel++)
  1693. {
  1694. enum elf_arc_reloc_type r_type;
  1695. reloc_howto_type *howto;
  1696. unsigned long r_symndx;
  1697. struct elf_link_hash_entry *h;
  1698. r_type = ELF32_R_TYPE (rel->r_info);
  1699. if (r_type >= (int) R_ARC_max)
  1700. {
  1701. bfd_set_error (bfd_error_bad_value);
  1702. return false;
  1703. }
  1704. howto = arc_elf_howto (r_type);
  1705. /* Load symbol information. */
  1706. r_symndx = ELF32_R_SYM (rel->r_info);
  1707. if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol. */
  1708. h = NULL;
  1709. else /* Global one. */
  1710. {
  1711. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1712. while (h->root.type == bfd_link_hash_indirect
  1713. || h->root.type == bfd_link_hash_warning)
  1714. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1715. }
  1716. switch (r_type)
  1717. {
  1718. case R_ARC_32:
  1719. case R_ARC_32_ME:
  1720. /* During shared library creation, these relocs should not
  1721. appear in a shared library (as memory will be read only
  1722. and the dynamic linker can not resolve these. However
  1723. the error should not occur for e.g. debugging or
  1724. non-readonly sections. */
  1725. if (h != NULL
  1726. && (bfd_link_dll (info) && !bfd_link_pie (info))
  1727. && (sec->flags & SEC_ALLOC) != 0
  1728. && (sec->flags & SEC_READONLY) != 0
  1729. && ((sec->flags & SEC_CODE) != 0
  1730. || (sec->flags & SEC_DEBUGGING) != 0))
  1731. {
  1732. const char *name;
  1733. if (h)
  1734. name = h->root.root.string;
  1735. else
  1736. name = "UNKNOWN";
  1737. _bfd_error_handler
  1738. /* xgettext:c-format */
  1739. (_("%pB: relocation %s against `%s' can not be used"
  1740. " when making a shared object; recompile with -fPIC"),
  1741. abfd,
  1742. arc_elf_howto (r_type)->name,
  1743. name);
  1744. bfd_set_error (bfd_error_bad_value);
  1745. return false;
  1746. }
  1747. /* In some cases we are not setting the 'non_got_ref'
  1748. flag, even though the relocations don't require a GOT
  1749. access. We should extend the testing in this area to
  1750. ensure that no significant cases are being missed. */
  1751. if (h)
  1752. h->non_got_ref = 1;
  1753. /* FALLTHROUGH */
  1754. case R_ARC_PC32:
  1755. case R_ARC_32_PCREL:
  1756. if ((bfd_link_pic (info))
  1757. && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
  1758. || (h != NULL
  1759. && (!info->symbolic || !h->def_regular))))
  1760. {
  1761. if (sreloc == NULL)
  1762. {
  1763. if (info->dynamic
  1764. && ! htab->dynamic_sections_created
  1765. && ! _bfd_elf_link_create_dynamic_sections (abfd, info))
  1766. return false;
  1767. sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
  1768. 2, abfd,
  1769. /*rela*/
  1770. true);
  1771. if (sreloc == NULL)
  1772. return false;
  1773. }
  1774. sreloc->size += sizeof (Elf32_External_Rela);
  1775. }
  1776. default:
  1777. break;
  1778. }
  1779. if (is_reloc_for_PLT (howto))
  1780. {
  1781. if (h == NULL)
  1782. continue;
  1783. else
  1784. if (h->forced_local == 0)
  1785. h->needs_plt = 1;
  1786. }
  1787. /* Add info to the symbol got_entry_list. */
  1788. if (is_reloc_for_GOT (howto)
  1789. || is_reloc_for_TLS (howto))
  1790. {
  1791. if (bfd_link_dll (info) && !bfd_link_pie (info)
  1792. && (r_type == R_ARC_TLS_LE_32 || r_type == R_ARC_TLS_LE_S9))
  1793. {
  1794. const char *name;
  1795. if (h)
  1796. name = h->root.root.string;
  1797. else
  1798. /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */
  1799. name = "UNKNOWN";
  1800. _bfd_error_handler
  1801. /* xgettext:c-format */
  1802. (_("%pB: relocation %s against `%s' can not be used"
  1803. " when making a shared object; recompile with -fPIC"),
  1804. abfd,
  1805. arc_elf_howto (r_type)->name,
  1806. name);
  1807. bfd_set_error (bfd_error_bad_value);
  1808. return false;
  1809. }
  1810. if (! _bfd_elf_create_got_section (dynobj, info))
  1811. return false;
  1812. arc_fill_got_info_for_reloc (
  1813. arc_got_entry_type_for_reloc (howto),
  1814. get_got_entry_list_for_symbol (abfd, r_symndx, h),
  1815. info,
  1816. h);
  1817. }
  1818. }
  1819. return true;
  1820. }
  1821. #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so"
  1822. static const struct plt_version_t *
  1823. arc_get_plt_version (struct bfd_link_info *info)
  1824. {
  1825. int i;
  1826. for (i = 0; i < 1; i++)
  1827. {
  1828. ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
  1829. (int) plt_versions[i].entry_size,
  1830. (int) plt_versions[i].elem_size);
  1831. }
  1832. if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
  1833. {
  1834. if (bfd_link_pic (info))
  1835. return &(plt_versions[ELF_ARCV2_PIC]);
  1836. else
  1837. return &(plt_versions[ELF_ARCV2_ABS]);
  1838. }
  1839. else
  1840. {
  1841. if (bfd_link_pic (info))
  1842. return &(plt_versions[ELF_ARC_PIC]);
  1843. else
  1844. return &(plt_versions[ELF_ARC_ABS]);
  1845. }
  1846. }
  1847. static bfd_vma
  1848. add_symbol_to_plt (struct bfd_link_info *info)
  1849. {
  1850. struct elf_link_hash_table *htab = elf_hash_table (info);
  1851. bfd_vma ret;
  1852. const struct plt_version_t *plt_data = arc_get_plt_version (info);
  1853. /* If this is the first .plt entry, make room for the special first
  1854. entry. */
  1855. if (htab->splt->size == 0)
  1856. htab->splt->size += plt_data->entry_size;
  1857. ret = htab->splt->size;
  1858. htab->splt->size += plt_data->elem_size;
  1859. ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size);
  1860. htab->sgotplt->size += 4;
  1861. htab->srelplt->size += sizeof (Elf32_External_Rela);
  1862. return ret;
  1863. }
  1864. #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
  1865. plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
  1866. static void
  1867. plt_do_relocs_for_symbol (bfd *abfd,
  1868. struct elf_link_hash_table *htab,
  1869. const struct plt_reloc *reloc,
  1870. bfd_vma plt_offset,
  1871. bfd_vma symbol_got_offset)
  1872. {
  1873. while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
  1874. {
  1875. bfd_vma relocation = 0;
  1876. switch (SYM_ONLY (reloc->symbol))
  1877. {
  1878. case SGOT:
  1879. relocation
  1880. = htab->sgotplt->output_section->vma
  1881. + htab->sgotplt->output_offset + symbol_got_offset;
  1882. break;
  1883. }
  1884. relocation += reloc->addend;
  1885. if (IS_RELATIVE (reloc->symbol))
  1886. {
  1887. bfd_vma reloc_offset = reloc->offset;
  1888. reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
  1889. reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
  1890. relocation -= htab->splt->output_section->vma
  1891. + htab->splt->output_offset
  1892. + plt_offset + reloc_offset;
  1893. }
  1894. /* TODO: being ME is not a property of the relocation but of the
  1895. section of which is applying the relocation. */
  1896. if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
  1897. {
  1898. relocation
  1899. = ((relocation & 0xffff0000) >> 16)
  1900. | ((relocation & 0xffff) << 16);
  1901. }
  1902. switch (reloc->size)
  1903. {
  1904. case 32:
  1905. bfd_put_32 (htab->splt->output_section->owner,
  1906. relocation,
  1907. htab->splt->contents + plt_offset + reloc->offset);
  1908. break;
  1909. }
  1910. reloc = &(reloc[1]); /* Jump to next relocation. */
  1911. }
  1912. }
  1913. static void
  1914. relocate_plt_for_symbol (bfd *output_bfd,
  1915. struct bfd_link_info *info,
  1916. struct elf_link_hash_entry *h)
  1917. {
  1918. const struct plt_version_t *plt_data = arc_get_plt_version (info);
  1919. struct elf_link_hash_table *htab = elf_hash_table (info);
  1920. bfd_vma plt_index = (h->plt.offset - plt_data->entry_size)
  1921. / plt_data->elem_size;
  1922. bfd_vma got_offset = (plt_index + 3) * 4;
  1923. ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \
  1924. GOT_ENTRY_OFFSET = %#lx, GOT_ENTRY_VMA = %#lx, for symbol %s\n",
  1925. (long) h->plt.offset,
  1926. (long) (htab->splt->output_section->vma
  1927. + htab->splt->output_offset
  1928. + h->plt.offset),
  1929. (long) got_offset,
  1930. (long) (htab->sgotplt->output_section->vma
  1931. + htab->sgotplt->output_offset
  1932. + got_offset),
  1933. h->root.root.string);
  1934. {
  1935. bfd_vma i = 0;
  1936. uint16_t *ptr = (uint16_t *) plt_data->elem;
  1937. for (i = 0; i < plt_data->elem_size/2; i++)
  1938. {
  1939. uint16_t data = ptr[i];
  1940. bfd_put_16 (output_bfd,
  1941. (bfd_vma) data,
  1942. htab->splt->contents + h->plt.offset + (i*2));
  1943. }
  1944. }
  1945. plt_do_relocs_for_symbol (output_bfd, htab,
  1946. plt_data->elem_relocs,
  1947. h->plt.offset,
  1948. got_offset);
  1949. /* Fill in the entry in the global offset table. */
  1950. bfd_put_32 (output_bfd,
  1951. (bfd_vma) (htab->splt->output_section->vma
  1952. + htab->splt->output_offset),
  1953. htab->sgotplt->contents + got_offset);
  1954. /* TODO: Fill in the entry in the .rela.plt section. */
  1955. {
  1956. Elf_Internal_Rela rel;
  1957. bfd_byte *loc;
  1958. rel.r_offset = (htab->sgotplt->output_section->vma
  1959. + htab->sgotplt->output_offset
  1960. + got_offset);
  1961. rel.r_addend = 0;
  1962. BFD_ASSERT (h->dynindx != -1);
  1963. rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
  1964. loc = htab->srelplt->contents;
  1965. loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
  1966. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  1967. }
  1968. }
  1969. static void
  1970. relocate_plt_for_entry (bfd *abfd,
  1971. struct bfd_link_info *info)
  1972. {
  1973. const struct plt_version_t *plt_data = arc_get_plt_version (info);
  1974. struct elf_link_hash_table *htab = elf_hash_table (info);
  1975. {
  1976. bfd_vma i = 0;
  1977. uint16_t *ptr = (uint16_t *) plt_data->entry;
  1978. for (i = 0; i < plt_data->entry_size/2; i++)
  1979. {
  1980. uint16_t data = ptr[i];
  1981. bfd_put_16 (abfd,
  1982. (bfd_vma) data,
  1983. htab->splt->contents + (i*2));
  1984. }
  1985. }
  1986. PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
  1987. }
  1988. /* Desc : Adjust a symbol defined by a dynamic object and referenced
  1989. by a regular object. The current definition is in some section of
  1990. the dynamic object, but we're not including those sections. We
  1991. have to change the definition to something the rest of the link can
  1992. understand. */
  1993. static bool
  1994. elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
  1995. struct elf_link_hash_entry *h)
  1996. {
  1997. asection *s;
  1998. bfd *dynobj = (elf_hash_table (info))->dynobj;
  1999. struct elf_link_hash_table *htab = elf_hash_table (info);
  2000. if (h->type == STT_FUNC
  2001. || h->type == STT_GNU_IFUNC
  2002. || h->needs_plt == 1)
  2003. {
  2004. if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
  2005. {
  2006. /* This case can occur if we saw a PLT32 reloc in an input
  2007. file, but the symbol was never referred to by a dynamic
  2008. object. In such a case, we don't actually need to build
  2009. a procedure linkage table, and we can just do a PC32
  2010. reloc instead. */
  2011. BFD_ASSERT (h->needs_plt);
  2012. return true;
  2013. }
  2014. /* Make sure this symbol is output as a dynamic symbol. */
  2015. if (h->dynindx == -1 && !h->forced_local
  2016. && !bfd_elf_link_record_dynamic_symbol (info, h))
  2017. return false;
  2018. if (bfd_link_pic (info)
  2019. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
  2020. {
  2021. bfd_vma loc = add_symbol_to_plt (info);
  2022. if (bfd_link_executable (info) && !h->def_regular)
  2023. {
  2024. h->root.u.def.section = htab->splt;
  2025. h->root.u.def.value = loc;
  2026. }
  2027. h->plt.offset = loc;
  2028. }
  2029. else
  2030. {
  2031. h->plt.offset = (bfd_vma) -1;
  2032. h->needs_plt = 0;
  2033. }
  2034. return true;
  2035. }
  2036. /* If this is a weak symbol, and there is a real definition, the
  2037. processor independent code will have arranged for us to see the
  2038. real definition first, and we can just use the same value. */
  2039. if (h->is_weakalias)
  2040. {
  2041. struct elf_link_hash_entry *def = weakdef (h);
  2042. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  2043. h->root.u.def.section = def->root.u.def.section;
  2044. h->root.u.def.value = def->root.u.def.value;
  2045. return true;
  2046. }
  2047. /* This is a reference to a symbol defined by a dynamic object which
  2048. is not a function. */
  2049. /* If we are creating a shared library, we must presume that the
  2050. only references to the symbol are via the global offset table.
  2051. For such cases we need not do anything here; the relocations will
  2052. be handled correctly by relocate_section. */
  2053. if (!bfd_link_executable (info))
  2054. return true;
  2055. /* If there are no non-GOT references, we do not need a copy
  2056. relocation. */
  2057. if (!h->non_got_ref)
  2058. return true;
  2059. /* If -z nocopyreloc was given, we won't generate them either. */
  2060. if (info->nocopyreloc)
  2061. {
  2062. h->non_got_ref = 0;
  2063. return true;
  2064. }
  2065. /* We must allocate the symbol in our .dynbss section, which will
  2066. become part of the .bss section of the executable. There will be
  2067. an entry for this symbol in the .dynsym section. The dynamic
  2068. object will contain position independent code, so all references
  2069. from the dynamic object to this symbol will go through the global
  2070. offset table. The dynamic linker will use the .dynsym entry to
  2071. determine the address it must put in the global offset table, so
  2072. both the dynamic object and the regular object will refer to the
  2073. same memory location for the variable. */
  2074. if (htab == NULL)
  2075. return false;
  2076. /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
  2077. copy the initial value out of the dynamic object and into the
  2078. runtime process image. We need to remember the offset into the
  2079. .rela.bss section we are going to use. */
  2080. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
  2081. {
  2082. struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
  2083. BFD_ASSERT (arc_htab->elf.srelbss != NULL);
  2084. arc_htab->elf.srelbss->size += sizeof (Elf32_External_Rela);
  2085. h->needs_copy = 1;
  2086. }
  2087. /* TODO: Move this also to arc_hash_table. */
  2088. s = bfd_get_section_by_name (dynobj, ".dynbss");
  2089. BFD_ASSERT (s != NULL);
  2090. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  2091. }
  2092. /* Function : elf_arc_finish_dynamic_symbol
  2093. Brief : Finish up dynamic symbol handling. We set the
  2094. contents of various dynamic sections here.
  2095. Args : output_bfd :
  2096. info :
  2097. h :
  2098. sym :
  2099. Returns : True/False as the return status. */
  2100. static bool
  2101. elf_arc_finish_dynamic_symbol (bfd * output_bfd,
  2102. struct bfd_link_info *info,
  2103. struct elf_link_hash_entry *h,
  2104. Elf_Internal_Sym * sym)
  2105. {
  2106. if (h->plt.offset != (bfd_vma) -1)
  2107. {
  2108. relocate_plt_for_symbol (output_bfd, info, h);
  2109. if (!h->def_regular)
  2110. {
  2111. /* Mark the symbol as undefined, rather than as defined in
  2112. the .plt section. Leave the value alone. */
  2113. sym->st_shndx = SHN_UNDEF;
  2114. }
  2115. }
  2116. /* This function traverses list of GOT entries and
  2117. create respective dynamic relocs. */
  2118. /* TODO: Make function to get list and not access the list directly. */
  2119. /* TODO: Move function to relocate_section create this relocs eagerly. */
  2120. struct elf_arc_link_hash_entry *ah =
  2121. (struct elf_arc_link_hash_entry *) h;
  2122. create_got_dynrelocs_for_got_info (&ah->got_ents,
  2123. output_bfd,
  2124. info,
  2125. h);
  2126. if (h->needs_copy)
  2127. {
  2128. struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
  2129. if (arc_htab == NULL)
  2130. return false;
  2131. if (h->dynindx == -1
  2132. || (h->root.type != bfd_link_hash_defined
  2133. && h->root.type != bfd_link_hash_defweak)
  2134. || arc_htab->elf.srelbss == NULL)
  2135. abort ();
  2136. bfd_vma rel_offset = (h->root.u.def.value
  2137. + h->root.u.def.section->output_section->vma
  2138. + h->root.u.def.section->output_offset);
  2139. bfd_byte * loc = arc_htab->elf.srelbss->contents
  2140. + (arc_htab->elf.srelbss->reloc_count * sizeof (Elf32_External_Rela));
  2141. arc_htab->elf.srelbss->reloc_count++;
  2142. Elf_Internal_Rela rel;
  2143. rel.r_addend = 0;
  2144. rel.r_offset = rel_offset;
  2145. BFD_ASSERT (h->dynindx != -1);
  2146. rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
  2147. bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
  2148. }
  2149. /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
  2150. if (strcmp (h->root.root.string, "_DYNAMIC") == 0
  2151. || strcmp (h->root.root.string, "__DYNAMIC") == 0
  2152. || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
  2153. sym->st_shndx = SHN_ABS;
  2154. return true;
  2155. }
  2156. #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \
  2157. case TAG: \
  2158. if (SYMBOL != NULL) \
  2159. h = elf_link_hash_lookup (elf_hash_table (info), \
  2160. SYMBOL, false, false, true); \
  2161. else if (SECTION != NULL) \
  2162. s = bfd_get_linker_section (dynobj, SECTION); \
  2163. break;
  2164. struct obfd_info_group {
  2165. bfd *output_bfd;
  2166. struct bfd_link_info *info;
  2167. };
  2168. static bool
  2169. arc_create_forced_local_got_entries_for_tls (struct bfd_hash_entry *bh,
  2170. void *data)
  2171. {
  2172. struct elf_arc_link_hash_entry * h =
  2173. (struct elf_arc_link_hash_entry *) bh;
  2174. struct obfd_info_group *tmp = (struct obfd_info_group *) data;
  2175. if (h->got_ents != NULL)
  2176. {
  2177. BFD_ASSERT (h);
  2178. struct got_entry *list = h->got_ents;
  2179. while (list != NULL)
  2180. {
  2181. create_got_dynrelocs_for_single_entry (list, tmp->output_bfd,
  2182. tmp->info,
  2183. (struct elf_link_hash_entry *) h);
  2184. list = list->next;
  2185. }
  2186. }
  2187. return true;
  2188. }
  2189. /* Function : elf_arc_finish_dynamic_sections
  2190. Brief : Finish up the dynamic sections handling.
  2191. Args : output_bfd :
  2192. info :
  2193. h :
  2194. sym :
  2195. Returns : True/False as the return status. */
  2196. static bool
  2197. elf_arc_finish_dynamic_sections (bfd * output_bfd,
  2198. struct bfd_link_info *info)
  2199. {
  2200. struct elf_link_hash_table *htab = elf_hash_table (info);
  2201. bfd *dynobj = (elf_hash_table (info))->dynobj;
  2202. asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2203. if (sdyn)
  2204. {
  2205. Elf32_External_Dyn *dyncon, *dynconend;
  2206. dyncon = (Elf32_External_Dyn *) sdyn->contents;
  2207. dynconend
  2208. = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
  2209. for (; dyncon < dynconend; dyncon++)
  2210. {
  2211. Elf_Internal_Dyn internal_dyn;
  2212. bool do_it = false;
  2213. struct elf_link_hash_entry *h = NULL;
  2214. asection *s = NULL;
  2215. bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn);
  2216. switch (internal_dyn.d_tag)
  2217. {
  2218. GET_SYMBOL_OR_SECTION (DT_INIT, info->init_function, NULL)
  2219. GET_SYMBOL_OR_SECTION (DT_FINI, info->fini_function, NULL)
  2220. GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
  2221. GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
  2222. GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
  2223. GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
  2224. GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
  2225. GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
  2226. default:
  2227. break;
  2228. }
  2229. /* In case the dynamic symbols should be updated with a symbol. */
  2230. if (h != NULL
  2231. && (h->root.type == bfd_link_hash_defined
  2232. || h->root.type == bfd_link_hash_defweak))
  2233. {
  2234. asection *asec_ptr;
  2235. internal_dyn.d_un.d_val = h->root.u.def.value;
  2236. asec_ptr = h->root.u.def.section;
  2237. if (asec_ptr->output_section != NULL)
  2238. {
  2239. internal_dyn.d_un.d_val +=
  2240. (asec_ptr->output_section->vma
  2241. + asec_ptr->output_offset);
  2242. }
  2243. else
  2244. {
  2245. /* The symbol is imported from another shared
  2246. library and does not apply to this one. */
  2247. internal_dyn.d_un.d_val = 0;
  2248. }
  2249. do_it = true;
  2250. }
  2251. else if (s != NULL) /* With a section information. */
  2252. {
  2253. switch (internal_dyn.d_tag)
  2254. {
  2255. case DT_PLTGOT:
  2256. case DT_JMPREL:
  2257. case DT_VERSYM:
  2258. case DT_VERDEF:
  2259. case DT_VERNEED:
  2260. internal_dyn.d_un.d_ptr = (s->output_section->vma
  2261. + s->output_offset);
  2262. do_it = true;
  2263. break;
  2264. case DT_PLTRELSZ:
  2265. internal_dyn.d_un.d_val = s->size;
  2266. do_it = true;
  2267. break;
  2268. default:
  2269. break;
  2270. }
  2271. }
  2272. if (do_it)
  2273. bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon);
  2274. }
  2275. if (htab->splt->size > 0)
  2276. {
  2277. relocate_plt_for_entry (output_bfd, info);
  2278. }
  2279. /* TODO: Validate this. */
  2280. if (htab->srelplt->output_section != bfd_abs_section_ptr)
  2281. elf_section_data (htab->srelplt->output_section)
  2282. ->this_hdr.sh_entsize = 12;
  2283. }
  2284. /* Fill in the first three entries in the global offset table. */
  2285. if (htab->sgot)
  2286. {
  2287. struct elf_link_hash_entry *h;
  2288. h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
  2289. false, false, true);
  2290. if (h != NULL && h->root.type != bfd_link_hash_undefined
  2291. && h->root.u.def.section != NULL)
  2292. {
  2293. asection *sec = h->root.u.def.section;
  2294. if (sdyn == NULL)
  2295. bfd_put_32 (output_bfd, (bfd_vma) 0,
  2296. sec->contents);
  2297. else
  2298. bfd_put_32 (output_bfd,
  2299. sdyn->output_section->vma + sdyn->output_offset,
  2300. sec->contents);
  2301. bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4);
  2302. bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8);
  2303. }
  2304. }
  2305. struct obfd_info_group group;
  2306. group.output_bfd = output_bfd;
  2307. group.info = info;
  2308. bfd_hash_traverse (&info->hash->table,
  2309. arc_create_forced_local_got_entries_for_tls, &group);
  2310. return true;
  2311. }
  2312. #define ADD_DYNAMIC_SYMBOL(NAME, TAG) \
  2313. h = elf_link_hash_lookup (elf_hash_table (info), \
  2314. NAME, false, false, false); \
  2315. if ((h != NULL && (h->ref_regular || h->def_regular))) \
  2316. if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \
  2317. return false;
  2318. /* Set the sizes of the dynamic sections. */
  2319. static bool
  2320. elf_arc_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  2321. struct bfd_link_info *info)
  2322. {
  2323. bfd *dynobj;
  2324. asection *s;
  2325. bool relocs_exist = false;
  2326. struct elf_link_hash_table *htab = elf_hash_table (info);
  2327. dynobj = htab->dynobj;
  2328. BFD_ASSERT (dynobj != NULL);
  2329. if (htab->dynamic_sections_created)
  2330. {
  2331. struct elf_link_hash_entry *h;
  2332. /* Set the contents of the .interp section to the
  2333. interpreter. */
  2334. if (bfd_link_executable (info) && !info->nointerp)
  2335. {
  2336. s = bfd_get_section_by_name (dynobj, ".interp");
  2337. BFD_ASSERT (s != NULL);
  2338. s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
  2339. s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
  2340. }
  2341. /* Add some entries to the .dynamic section. We fill in some of
  2342. the values later, in elf_bfd_final_link, but we must add the
  2343. entries now so that we know the final size of the .dynamic
  2344. section. Checking if the .init section is present. We also
  2345. create DT_INIT and DT_FINI entries if the init_str has been
  2346. changed by the user. */
  2347. ADD_DYNAMIC_SYMBOL (info->init_function, DT_INIT);
  2348. ADD_DYNAMIC_SYMBOL (info->fini_function, DT_FINI);
  2349. }
  2350. else
  2351. {
  2352. /* We may have created entries in the .rela.got section.
  2353. However, if we are not creating the dynamic sections, we will
  2354. not actually use these entries. Reset the size of .rela.got,
  2355. which will cause it to get stripped from the output file
  2356. below. */
  2357. if (htab->srelgot != NULL)
  2358. htab->srelgot->size = 0;
  2359. }
  2360. for (s = dynobj->sections; s != NULL; s = s->next)
  2361. {
  2362. if ((s->flags & SEC_LINKER_CREATED) == 0)
  2363. continue;
  2364. if (s == htab->splt
  2365. || s == htab->sgot
  2366. || s == htab->sgotplt
  2367. || s == htab->sdynbss)
  2368. {
  2369. /* Strip this section if we don't need it. */
  2370. }
  2371. else if (startswith (s->name, ".rela"))
  2372. {
  2373. if (s->size != 0 && s != htab->srelplt)
  2374. relocs_exist = true;
  2375. /* We use the reloc_count field as a counter if we need to
  2376. copy relocs into the output file. */
  2377. s->reloc_count = 0;
  2378. }
  2379. else
  2380. {
  2381. /* It's not one of our sections, so don't allocate space. */
  2382. continue;
  2383. }
  2384. if (s->size == 0)
  2385. {
  2386. s->flags |= SEC_EXCLUDE;
  2387. continue;
  2388. }
  2389. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2390. continue;
  2391. /* Allocate memory for the section contents. */
  2392. s->contents = bfd_zalloc (dynobj, s->size);
  2393. if (s->contents == NULL)
  2394. return false;
  2395. }
  2396. return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs_exist);
  2397. }
  2398. /* Classify dynamic relocs such that -z combreloc can reorder and combine
  2399. them. */
  2400. static enum elf_reloc_type_class
  2401. elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2402. const asection *rel_sec ATTRIBUTE_UNUSED,
  2403. const Elf_Internal_Rela *rela)
  2404. {
  2405. switch ((int) ELF32_R_TYPE (rela->r_info))
  2406. {
  2407. case R_ARC_RELATIVE:
  2408. return reloc_class_relative;
  2409. case R_ARC_JMP_SLOT:
  2410. return reloc_class_plt;
  2411. case R_ARC_COPY:
  2412. return reloc_class_copy;
  2413. /* TODO: Needed in future to support ifunc. */
  2414. /*
  2415. case R_ARC_IRELATIVE:
  2416. return reloc_class_ifunc;
  2417. */
  2418. default:
  2419. return reloc_class_normal;
  2420. }
  2421. }
  2422. const struct elf_size_info arc_elf32_size_info =
  2423. {
  2424. sizeof (Elf32_External_Ehdr),
  2425. sizeof (Elf32_External_Phdr),
  2426. sizeof (Elf32_External_Shdr),
  2427. sizeof (Elf32_External_Rel),
  2428. sizeof (Elf32_External_Rela),
  2429. sizeof (Elf32_External_Sym),
  2430. sizeof (Elf32_External_Dyn),
  2431. sizeof (Elf_External_Note),
  2432. 4,
  2433. 1,
  2434. 32, 2,
  2435. ELFCLASS32, EV_CURRENT,
  2436. bfd_elf32_write_out_phdrs,
  2437. bfd_elf32_write_shdrs_and_ehdr,
  2438. bfd_elf32_checksum_contents,
  2439. bfd_elf32_write_relocs,
  2440. bfd_elf32_swap_symbol_in,
  2441. bfd_elf32_swap_symbol_out,
  2442. bfd_elf32_slurp_reloc_table,
  2443. bfd_elf32_slurp_symbol_table,
  2444. bfd_elf32_swap_dyn_in,
  2445. bfd_elf32_swap_dyn_out,
  2446. bfd_elf32_swap_reloc_in,
  2447. bfd_elf32_swap_reloc_out,
  2448. bfd_elf32_swap_reloca_in,
  2449. bfd_elf32_swap_reloca_out
  2450. };
  2451. #define elf_backend_size_info arc_elf32_size_info
  2452. /* GDB expects general purpose registers to be in section .reg. However Linux
  2453. kernel doesn't create this section and instead writes registers to NOTE
  2454. section. It is up to the binutils to create a pseudo-section .reg from the
  2455. contents of NOTE. Also BFD will read pid and signal number from NOTE. This
  2456. function relies on offsets inside elf_prstatus structure in Linux to be
  2457. stable. */
  2458. static bool
  2459. elf32_arc_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
  2460. {
  2461. int offset;
  2462. size_t size;
  2463. switch (note->descsz)
  2464. {
  2465. default:
  2466. return false;
  2467. case 236: /* sizeof (struct elf_prstatus) on Linux/arc. */
  2468. /* pr_cursig */
  2469. elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
  2470. /* pr_pid */
  2471. elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
  2472. /* pr_regs */
  2473. offset = 72;
  2474. size = (40 * 4); /* There are 40 registers in user_regs_struct. */
  2475. break;
  2476. }
  2477. /* Make a ".reg/999" section. */
  2478. return _bfd_elfcore_make_pseudosection (abfd, ".reg", size,
  2479. note->descpos + offset);
  2480. }
  2481. /* Determine whether an object attribute tag takes an integer, a
  2482. string or both. */
  2483. static int
  2484. elf32_arc_obj_attrs_arg_type (int tag)
  2485. {
  2486. if (tag == Tag_ARC_CPU_name
  2487. || tag == Tag_ARC_ISA_config
  2488. || tag == Tag_ARC_ISA_apex)
  2489. return ATTR_TYPE_FLAG_STR_VAL;
  2490. else if (tag < (Tag_ARC_ISA_mpy_option + 1))
  2491. return ATTR_TYPE_FLAG_INT_VAL;
  2492. else
  2493. return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
  2494. }
  2495. /* Attribute numbers >=14 can be safely ignored. */
  2496. static bool
  2497. elf32_arc_obj_attrs_handle_unknown (bfd *abfd, int tag)
  2498. {
  2499. if ((tag & 127) < (Tag_ARC_ISA_mpy_option + 1))
  2500. {
  2501. _bfd_error_handler
  2502. (_("%pB: unknown mandatory ARC object attribute %d"),
  2503. abfd, tag);
  2504. bfd_set_error (bfd_error_bad_value);
  2505. return false;
  2506. }
  2507. else
  2508. {
  2509. _bfd_error_handler
  2510. (_("warning: %pB: unknown ARC object attribute %d"),
  2511. abfd, tag);
  2512. return true;
  2513. }
  2514. }
  2515. /* Handle an ARC specific section when reading an object file. This is
  2516. called when bfd_section_from_shdr finds a section with an unknown
  2517. type. */
  2518. static bool
  2519. elf32_arc_section_from_shdr (bfd *abfd,
  2520. Elf_Internal_Shdr * hdr,
  2521. const char *name,
  2522. int shindex)
  2523. {
  2524. switch (hdr->sh_type)
  2525. {
  2526. case 0x0c: /* MWDT specific section, don't complain about it. */
  2527. case SHT_ARC_ATTRIBUTES:
  2528. break;
  2529. default:
  2530. return false;
  2531. }
  2532. if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  2533. return false;
  2534. return true;
  2535. }
  2536. /* Relaxation hook.
  2537. These are the current relaxing opportunities available:
  2538. * R_ARC_GOTPC32 => R_ARC_PCREL.
  2539. */
  2540. static bool
  2541. arc_elf_relax_section (bfd *abfd, asection *sec,
  2542. struct bfd_link_info *link_info, bool *again)
  2543. {
  2544. Elf_Internal_Shdr *symtab_hdr;
  2545. Elf_Internal_Rela *internal_relocs;
  2546. Elf_Internal_Rela *irel, *irelend;
  2547. bfd_byte *contents = NULL;
  2548. Elf_Internal_Sym *isymbuf = NULL;
  2549. /* Assume nothing changes. */
  2550. *again = false;
  2551. /* We don't have to do anything for a relocatable link, if this
  2552. section does not have relocs, or if this is not a code
  2553. section. */
  2554. if (bfd_link_relocatable (link_info)
  2555. || (sec->flags & SEC_RELOC) == 0
  2556. || sec->reloc_count == 0
  2557. || (sec->flags & SEC_CODE) == 0)
  2558. return true;
  2559. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2560. /* Get a copy of the native relocations. */
  2561. internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
  2562. link_info->keep_memory);
  2563. if (internal_relocs == NULL)
  2564. goto error_return;
  2565. /* Walk through them looking for relaxing opportunities. */
  2566. irelend = internal_relocs + sec->reloc_count;
  2567. for (irel = internal_relocs; irel < irelend; irel++)
  2568. {
  2569. /* If this isn't something that can be relaxed, then ignore
  2570. this reloc. */
  2571. if (ELF32_R_TYPE (irel->r_info) != (int) R_ARC_GOTPC32)
  2572. continue;
  2573. /* Get the section contents if we haven't done so already. */
  2574. if (contents == NULL)
  2575. {
  2576. /* Get cached copy if it exists. */
  2577. if (elf_section_data (sec)->this_hdr.contents != NULL)
  2578. contents = elf_section_data (sec)->this_hdr.contents;
  2579. /* Go get them off disk. */
  2580. else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  2581. goto error_return;
  2582. }
  2583. /* Read this BFD's local symbols if we haven't done so already. */
  2584. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  2585. {
  2586. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2587. if (isymbuf == NULL)
  2588. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  2589. symtab_hdr->sh_info, 0,
  2590. NULL, NULL, NULL);
  2591. if (isymbuf == NULL)
  2592. goto error_return;
  2593. }
  2594. struct elf_link_hash_entry *htop = NULL;
  2595. if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
  2596. {
  2597. /* An external symbol. */
  2598. unsigned int indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  2599. htop = elf_sym_hashes (abfd)[indx];
  2600. }
  2601. if (ELF32_R_TYPE (irel->r_info) == (int) R_ARC_GOTPC32
  2602. && SYMBOL_REFERENCES_LOCAL (link_info, htop))
  2603. {
  2604. unsigned int code;
  2605. /* Get the opcode. */
  2606. code = bfd_get_32_me (abfd, contents + irel->r_offset - 4);
  2607. /* Note that we've changed the relocs, section contents, etc. */
  2608. elf_section_data (sec)->relocs = internal_relocs;
  2609. elf_section_data (sec)->this_hdr.contents = contents;
  2610. symtab_hdr->contents = (unsigned char *) isymbuf;
  2611. /* Fix the relocation's type. */
  2612. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_ARC_PC32);
  2613. /* ld rA,[pcl,symbol@tgot] -> add rA,pcl,symbol@pcl. */
  2614. /* 0010 0bbb aa11 0ZZX DBBB 1111 10AA AAAA.
  2615. 111 00 000 0111 xx xxxx*/
  2616. code &= ~0x27307F80;
  2617. BFD_ASSERT (code <= 62UL);
  2618. code |= 0x27007F80;
  2619. /* Write back the new instruction. */
  2620. bfd_put_32_me (abfd, code, contents + irel->r_offset - 4);
  2621. /* The size isn't changed, don't redo. */
  2622. *again = false;
  2623. }
  2624. }
  2625. if (isymbuf != NULL
  2626. && symtab_hdr->contents != (unsigned char *) isymbuf)
  2627. {
  2628. if (!link_info->keep_memory)
  2629. free (isymbuf);
  2630. else
  2631. /* Cache the symbols for elf_link_input_bfd. */
  2632. symtab_hdr->contents = (unsigned char *) isymbuf;
  2633. }
  2634. if (contents != NULL
  2635. && elf_section_data (sec)->this_hdr.contents != contents)
  2636. {
  2637. if (!link_info->keep_memory)
  2638. free (contents);
  2639. else
  2640. /* Cache the section contents for elf_link_input_bfd. */
  2641. elf_section_data (sec)->this_hdr.contents = contents;
  2642. }
  2643. if (elf_section_data (sec)->relocs != internal_relocs)
  2644. free (internal_relocs);
  2645. return true;
  2646. error_return:
  2647. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  2648. free (isymbuf);
  2649. if (elf_section_data (sec)->this_hdr.contents != contents)
  2650. free (contents);
  2651. if (elf_section_data (sec)->relocs != internal_relocs)
  2652. free (internal_relocs);
  2653. return false;
  2654. }
  2655. #define TARGET_LITTLE_SYM arc_elf32_le_vec
  2656. #define TARGET_LITTLE_NAME "elf32-littlearc"
  2657. #define TARGET_BIG_SYM arc_elf32_be_vec
  2658. #define TARGET_BIG_NAME "elf32-bigarc"
  2659. #define ELF_ARCH bfd_arch_arc
  2660. #define ELF_TARGET_ID ARC_ELF_DATA
  2661. #define ELF_MACHINE_CODE EM_ARC_COMPACT
  2662. #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2
  2663. #define ELF_MAXPAGESIZE 0x2000
  2664. #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create
  2665. #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data
  2666. #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup
  2667. #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags
  2668. #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data
  2669. #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data
  2670. #define bfd_elf32_bfd_relax_section arc_elf_relax_section
  2671. #define elf_info_to_howto_rel arc_info_to_howto_rel
  2672. #define elf_backend_object_p arc_elf_object_p
  2673. #define elf_backend_final_write_processing arc_elf_final_write_processing
  2674. #define elf_backend_relocate_section elf_arc_relocate_section
  2675. #define elf_backend_check_relocs elf_arc_check_relocs
  2676. #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
  2677. #define elf_backend_reloc_type_class elf32_arc_reloc_type_class
  2678. #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol
  2679. #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol
  2680. #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections
  2681. #define elf_backend_size_dynamic_sections elf_arc_size_dynamic_sections
  2682. #define elf_backend_can_gc_sections 1
  2683. #define elf_backend_want_got_plt 1
  2684. #define elf_backend_plt_readonly 1
  2685. #define elf_backend_rela_plts_and_copies_p 1
  2686. #define elf_backend_want_plt_sym 0
  2687. #define elf_backend_got_header_size 12
  2688. #define elf_backend_dtrel_excludes_plt 1
  2689. #define elf_backend_may_use_rel_p 0
  2690. #define elf_backend_may_use_rela_p 1
  2691. #define elf_backend_default_use_rela_p 1
  2692. #define elf_backend_grok_prstatus elf32_arc_grok_prstatus
  2693. #define elf_backend_default_execstack 0
  2694. #undef elf_backend_obj_attrs_vendor
  2695. #define elf_backend_obj_attrs_vendor "ARC"
  2696. #undef elf_backend_obj_attrs_section
  2697. #define elf_backend_obj_attrs_section ".ARC.attributes"
  2698. #undef elf_backend_obj_attrs_arg_type
  2699. #define elf_backend_obj_attrs_arg_type elf32_arc_obj_attrs_arg_type
  2700. #undef elf_backend_obj_attrs_section_type
  2701. #define elf_backend_obj_attrs_section_type SHT_ARC_ATTRIBUTES
  2702. #define elf_backend_obj_attrs_handle_unknown elf32_arc_obj_attrs_handle_unknown
  2703. #define elf_backend_section_from_shdr elf32_arc_section_from_shdr
  2704. #include "elf32-target.h"