elfxx-x86.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230
  1. /* x86 specific support for ELF
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "elfxx-x86.h"
  17. #include "elf-vxworks.h"
  18. #include "objalloc.h"
  19. /* The name of the dynamic interpreter. This is put in the .interp
  20. section. */
  21. #define ELF32_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
  22. #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
  23. #define ELFX32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
  24. bool
  25. _bfd_x86_elf_mkobject (bfd *abfd)
  26. {
  27. return bfd_elf_allocate_object (abfd,
  28. sizeof (struct elf_x86_obj_tdata),
  29. get_elf_backend_data (abfd)->target_id);
  30. }
  31. /* _TLS_MODULE_BASE_ needs to be treated especially when linking
  32. executables. Rather than setting it to the beginning of the TLS
  33. section, we have to set it to the end. This function may be called
  34. multiple times, it is idempotent. */
  35. void
  36. _bfd_x86_elf_set_tls_module_base (struct bfd_link_info *info)
  37. {
  38. struct elf_x86_link_hash_table *htab;
  39. struct bfd_link_hash_entry *base;
  40. const struct elf_backend_data *bed;
  41. if (!bfd_link_executable (info))
  42. return;
  43. bed = get_elf_backend_data (info->output_bfd);
  44. htab = elf_x86_hash_table (info, bed->target_id);
  45. if (htab == NULL)
  46. return;
  47. base = htab->tls_module_base;
  48. if (base == NULL)
  49. return;
  50. base->u.def.value = htab->elf.tls_size;
  51. }
  52. /* Return the base VMA address which should be subtracted from real addresses
  53. when resolving @dtpoff relocation.
  54. This is PT_TLS segment p_vaddr. */
  55. bfd_vma
  56. _bfd_x86_elf_dtpoff_base (struct bfd_link_info *info)
  57. {
  58. /* If tls_sec is NULL, we should have signalled an error already. */
  59. if (elf_hash_table (info)->tls_sec == NULL)
  60. return 0;
  61. return elf_hash_table (info)->tls_sec->vma;
  62. }
  63. /* Allocate space in .plt, .got and associated reloc sections for
  64. dynamic relocs. */
  65. static bool
  66. elf_x86_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
  67. {
  68. struct bfd_link_info *info;
  69. struct elf_x86_link_hash_table *htab;
  70. struct elf_x86_link_hash_entry *eh;
  71. struct elf_dyn_relocs *p;
  72. unsigned int plt_entry_size;
  73. bool resolved_to_zero;
  74. const struct elf_backend_data *bed;
  75. if (h->root.type == bfd_link_hash_indirect)
  76. return true;
  77. eh = (struct elf_x86_link_hash_entry *) h;
  78. info = (struct bfd_link_info *) inf;
  79. bed = get_elf_backend_data (info->output_bfd);
  80. htab = elf_x86_hash_table (info, bed->target_id);
  81. if (htab == NULL)
  82. return false;
  83. plt_entry_size = htab->plt.plt_entry_size;
  84. resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
  85. /* We can't use the GOT PLT if pointer equality is needed since
  86. finish_dynamic_symbol won't clear symbol value and the dynamic
  87. linker won't update the GOT slot. We will get into an infinite
  88. loop at run-time. */
  89. if (htab->plt_got != NULL
  90. && h->type != STT_GNU_IFUNC
  91. && !h->pointer_equality_needed
  92. && h->plt.refcount > 0
  93. && h->got.refcount > 0)
  94. {
  95. /* Don't use the regular PLT if there are both GOT and GOTPLT
  96. reloctions. */
  97. h->plt.offset = (bfd_vma) -1;
  98. /* Use the GOT PLT. */
  99. eh->plt_got.refcount = 1;
  100. }
  101. /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
  102. here if it is defined and referenced in a non-shared object. */
  103. if (h->type == STT_GNU_IFUNC
  104. && h->def_regular)
  105. {
  106. /* GOTOFF relocation needs PLT. */
  107. if (eh->gotoff_ref)
  108. h->plt.refcount = 1;
  109. if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h, &h->dyn_relocs,
  110. plt_entry_size,
  111. (htab->plt.has_plt0
  112. * plt_entry_size),
  113. htab->got_entry_size,
  114. true))
  115. {
  116. asection *s = htab->plt_second;
  117. if (h->plt.offset != (bfd_vma) -1 && s != NULL)
  118. {
  119. /* Use the second PLT section if it is created. */
  120. eh->plt_second.offset = s->size;
  121. /* Make room for this entry in the second PLT section. */
  122. s->size += htab->non_lazy_plt->plt_entry_size;
  123. }
  124. return true;
  125. }
  126. else
  127. return false;
  128. }
  129. /* Don't create the PLT entry if there are only function pointer
  130. relocations which can be resolved at run-time. */
  131. else if (htab->elf.dynamic_sections_created
  132. && (h->plt.refcount > 0
  133. || eh->plt_got.refcount > 0))
  134. {
  135. bool use_plt_got = eh->plt_got.refcount > 0;
  136. /* Make sure this symbol is output as a dynamic symbol.
  137. Undefined weak syms won't yet be marked as dynamic. */
  138. if (h->dynindx == -1
  139. && !h->forced_local
  140. && !resolved_to_zero
  141. && h->root.type == bfd_link_hash_undefweak)
  142. {
  143. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  144. return false;
  145. }
  146. if (bfd_link_pic (info)
  147. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
  148. {
  149. asection *s = htab->elf.splt;
  150. asection *second_s = htab->plt_second;
  151. asection *got_s = htab->plt_got;
  152. bool use_plt;
  153. /* If this is the first .plt entry, make room for the special
  154. first entry. The .plt section is used by prelink to undo
  155. prelinking for dynamic relocations. */
  156. if (s->size == 0)
  157. s->size = htab->plt.has_plt0 * plt_entry_size;
  158. if (use_plt_got)
  159. eh->plt_got.offset = got_s->size;
  160. else
  161. {
  162. h->plt.offset = s->size;
  163. if (second_s)
  164. eh->plt_second.offset = second_s->size;
  165. }
  166. /* If this symbol is not defined in a regular file, and we are
  167. generating PDE, then set the symbol to this location in the
  168. .plt. This is required to make function pointers compare
  169. as equal between PDE and the shared library.
  170. NB: If PLT is PC-relative, we can use the .plt in PIE for
  171. function address. */
  172. if (h->def_regular)
  173. use_plt = false;
  174. else if (htab->pcrel_plt)
  175. use_plt = ! bfd_link_dll (info);
  176. else
  177. use_plt = bfd_link_pde (info);
  178. if (use_plt)
  179. {
  180. if (use_plt_got)
  181. {
  182. /* We need to make a call to the entry of the GOT PLT
  183. instead of regular PLT entry. */
  184. h->root.u.def.section = got_s;
  185. h->root.u.def.value = eh->plt_got.offset;
  186. }
  187. else
  188. {
  189. if (second_s)
  190. {
  191. /* We need to make a call to the entry of the
  192. second PLT instead of regular PLT entry. */
  193. h->root.u.def.section = second_s;
  194. h->root.u.def.value = eh->plt_second.offset;
  195. }
  196. else
  197. {
  198. h->root.u.def.section = s;
  199. h->root.u.def.value = h->plt.offset;
  200. }
  201. }
  202. }
  203. /* Make room for this entry. */
  204. if (use_plt_got)
  205. got_s->size += htab->non_lazy_plt->plt_entry_size;
  206. else
  207. {
  208. s->size += plt_entry_size;
  209. if (second_s)
  210. second_s->size += htab->non_lazy_plt->plt_entry_size;
  211. /* We also need to make an entry in the .got.plt section,
  212. which will be placed in the .got section by the linker
  213. script. */
  214. htab->elf.sgotplt->size += htab->got_entry_size;
  215. /* There should be no PLT relocation against resolved
  216. undefined weak symbol in executable. */
  217. if (!resolved_to_zero)
  218. {
  219. /* We also need to make an entry in the .rel.plt
  220. section. */
  221. htab->elf.srelplt->size += htab->sizeof_reloc;
  222. htab->elf.srelplt->reloc_count++;
  223. }
  224. }
  225. if (htab->elf.target_os == is_vxworks && !bfd_link_pic (info))
  226. {
  227. /* VxWorks has a second set of relocations for each PLT entry
  228. in executables. They go in a separate relocation section,
  229. which is processed by the kernel loader. */
  230. /* There are two relocations for the initial PLT entry: an
  231. R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an
  232. R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */
  233. asection *srelplt2 = htab->srelplt2;
  234. if (h->plt.offset == plt_entry_size)
  235. srelplt2->size += (htab->sizeof_reloc * 2);
  236. /* There are two extra relocations for each subsequent PLT entry:
  237. an R_386_32 relocation for the GOT entry, and an R_386_32
  238. relocation for the PLT entry. */
  239. srelplt2->size += (htab->sizeof_reloc * 2);
  240. }
  241. }
  242. else
  243. {
  244. eh->plt_got.offset = (bfd_vma) -1;
  245. h->plt.offset = (bfd_vma) -1;
  246. h->needs_plt = 0;
  247. }
  248. }
  249. else
  250. {
  251. eh->plt_got.offset = (bfd_vma) -1;
  252. h->plt.offset = (bfd_vma) -1;
  253. h->needs_plt = 0;
  254. }
  255. eh->tlsdesc_got = (bfd_vma) -1;
  256. /* For i386, if R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the
  257. binary, make it a R_386_TLS_LE_32 requiring no TLS entry. For
  258. x86-64, if R_X86_64_GOTTPOFF symbol is now local to the binary,
  259. make it a R_X86_64_TPOFF32 requiring no GOT entry. */
  260. if (h->got.refcount > 0
  261. && bfd_link_executable (info)
  262. && h->dynindx == -1
  263. && (elf_x86_hash_entry (h)->tls_type & GOT_TLS_IE))
  264. h->got.offset = (bfd_vma) -1;
  265. else if (h->got.refcount > 0)
  266. {
  267. asection *s;
  268. bool dyn;
  269. int tls_type = elf_x86_hash_entry (h)->tls_type;
  270. /* Make sure this symbol is output as a dynamic symbol.
  271. Undefined weak syms won't yet be marked as dynamic. */
  272. if (h->dynindx == -1
  273. && !h->forced_local
  274. && !resolved_to_zero
  275. && h->root.type == bfd_link_hash_undefweak)
  276. {
  277. if (! bfd_elf_link_record_dynamic_symbol (info, h))
  278. return false;
  279. }
  280. s = htab->elf.sgot;
  281. if (GOT_TLS_GDESC_P (tls_type))
  282. {
  283. eh->tlsdesc_got = htab->elf.sgotplt->size
  284. - elf_x86_compute_jump_table_size (htab);
  285. htab->elf.sgotplt->size += 2 * htab->got_entry_size;
  286. h->got.offset = (bfd_vma) -2;
  287. }
  288. if (! GOT_TLS_GDESC_P (tls_type)
  289. || GOT_TLS_GD_P (tls_type))
  290. {
  291. h->got.offset = s->size;
  292. s->size += htab->got_entry_size;
  293. /* R_386_TLS_GD and R_X86_64_TLSGD need 2 consecutive GOT
  294. slots. */
  295. if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH)
  296. s->size += htab->got_entry_size;
  297. }
  298. dyn = htab->elf.dynamic_sections_created;
  299. /* R_386_TLS_IE_32 needs one dynamic relocation,
  300. R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation,
  301. (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we
  302. need two), R_386_TLS_GD and R_X86_64_TLSGD need one if local
  303. symbol and two if global. No dynamic relocation against
  304. resolved undefined weak symbol in executable. No dynamic
  305. relocation against non-preemptible absolute symbol. */
  306. if (tls_type == GOT_TLS_IE_BOTH)
  307. htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
  308. else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
  309. || (tls_type & GOT_TLS_IE))
  310. htab->elf.srelgot->size += htab->sizeof_reloc;
  311. else if (GOT_TLS_GD_P (tls_type))
  312. htab->elf.srelgot->size += 2 * htab->sizeof_reloc;
  313. else if (! GOT_TLS_GDESC_P (tls_type)
  314. && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  315. && !resolved_to_zero)
  316. || h->root.type != bfd_link_hash_undefweak)
  317. && ((bfd_link_pic (info)
  318. && !(h->dynindx == -1
  319. && ABS_SYMBOL_P (h)))
  320. || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
  321. htab->elf.srelgot->size += htab->sizeof_reloc;
  322. if (GOT_TLS_GDESC_P (tls_type))
  323. {
  324. htab->elf.srelplt->size += htab->sizeof_reloc;
  325. if (bed->target_id == X86_64_ELF_DATA)
  326. htab->elf.tlsdesc_plt = (bfd_vma) -1;
  327. }
  328. }
  329. else
  330. h->got.offset = (bfd_vma) -1;
  331. if (h->dyn_relocs == NULL)
  332. return true;
  333. /* In the shared -Bsymbolic case, discard space allocated for
  334. dynamic pc-relative relocs against symbols which turn out to be
  335. defined in regular objects. For the normal shared case, discard
  336. space for pc-relative relocs that have become local due to symbol
  337. visibility changes. */
  338. if (bfd_link_pic (info))
  339. {
  340. /* Relocs that use pc_count are those that appear on a call
  341. insn, or certain REL relocs that can generated via assembly.
  342. We want calls to protected symbols to resolve directly to the
  343. function rather than going via the plt. If people want
  344. function pointer comparisons to work as expected then they
  345. should avoid writing weird assembly. */
  346. if (SYMBOL_CALLS_LOCAL (info, h))
  347. {
  348. struct elf_dyn_relocs **pp;
  349. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  350. {
  351. p->count -= p->pc_count;
  352. p->pc_count = 0;
  353. if (p->count == 0)
  354. *pp = p->next;
  355. else
  356. pp = &p->next;
  357. }
  358. }
  359. if (htab->elf.target_os == is_vxworks)
  360. {
  361. struct elf_dyn_relocs **pp;
  362. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  363. {
  364. if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
  365. *pp = p->next;
  366. else
  367. pp = &p->next;
  368. }
  369. }
  370. /* Also discard relocs on undefined weak syms with non-default
  371. visibility or in PIE. */
  372. if (h->dyn_relocs != NULL)
  373. {
  374. if (h->root.type == bfd_link_hash_undefweak)
  375. {
  376. /* Undefined weak symbol is never bound locally in shared
  377. library. */
  378. if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  379. || resolved_to_zero)
  380. {
  381. if (bed->target_id == I386_ELF_DATA
  382. && h->non_got_ref)
  383. {
  384. /* Keep dynamic non-GOT/non-PLT relocation so
  385. that we can branch to 0 without PLT. */
  386. struct elf_dyn_relocs **pp;
  387. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  388. if (p->pc_count == 0)
  389. *pp = p->next;
  390. else
  391. {
  392. /* Remove non-R_386_PC32 relocation. */
  393. p->count = p->pc_count;
  394. pp = &p->next;
  395. }
  396. /* Make sure undefined weak symbols are output
  397. as dynamic symbols in PIEs for dynamic non-GOT
  398. non-PLT reloations. */
  399. if (h->dyn_relocs != NULL
  400. && !bfd_elf_link_record_dynamic_symbol (info, h))
  401. return false;
  402. }
  403. else
  404. h->dyn_relocs = NULL;
  405. }
  406. else if (h->dynindx == -1
  407. && !h->forced_local
  408. && !bfd_elf_link_record_dynamic_symbol (info, h))
  409. return false;
  410. }
  411. else if (bfd_link_executable (info)
  412. && (h->needs_copy || eh->needs_copy)
  413. && h->def_dynamic
  414. && !h->def_regular)
  415. {
  416. /* NB: needs_copy is set only for x86-64. For PIE,
  417. discard space for pc-relative relocs against symbols
  418. which turn out to need copy relocs. */
  419. struct elf_dyn_relocs **pp;
  420. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  421. {
  422. if (p->pc_count != 0)
  423. *pp = p->next;
  424. else
  425. pp = &p->next;
  426. }
  427. }
  428. }
  429. }
  430. else if (ELIMINATE_COPY_RELOCS)
  431. {
  432. /* For the non-shared case, discard space for relocs against
  433. symbols which turn out to need copy relocs or are not
  434. dynamic. Keep dynamic relocations for run-time function
  435. pointer initialization. */
  436. if ((!h->non_got_ref
  437. || (h->root.type == bfd_link_hash_undefweak
  438. && !resolved_to_zero))
  439. && ((h->def_dynamic
  440. && !h->def_regular)
  441. || (htab->elf.dynamic_sections_created
  442. && (h->root.type == bfd_link_hash_undefweak
  443. || h->root.type == bfd_link_hash_undefined))))
  444. {
  445. /* Make sure this symbol is output as a dynamic symbol.
  446. Undefined weak syms won't yet be marked as dynamic. */
  447. if (h->dynindx == -1
  448. && !h->forced_local
  449. && !resolved_to_zero
  450. && h->root.type == bfd_link_hash_undefweak
  451. && ! bfd_elf_link_record_dynamic_symbol (info, h))
  452. return false;
  453. /* If that succeeded, we know we'll be keeping all the
  454. relocs. */
  455. if (h->dynindx != -1)
  456. goto keep;
  457. }
  458. h->dyn_relocs = NULL;
  459. keep: ;
  460. }
  461. /* Finally, allocate space. */
  462. for (p = h->dyn_relocs; p != NULL; p = p->next)
  463. {
  464. asection *sreloc;
  465. if (eh->def_protected
  466. && elf_has_no_copy_on_protected (h->root.u.def.section->owner))
  467. {
  468. /* Disallow copy relocation against non-copyable protected
  469. symbol. */
  470. asection *s = p->sec->output_section;
  471. if (s != NULL && (s->flags & SEC_READONLY) != 0)
  472. {
  473. info->callbacks->einfo
  474. /* xgettext:c-format */
  475. (_("%F%P: %pB: copy relocation against non-copyable "
  476. "protected symbol `%s' in %pB\n"),
  477. p->sec->owner, h->root.root.string,
  478. h->root.u.def.section->owner);
  479. return false;
  480. }
  481. }
  482. sreloc = elf_section_data (p->sec)->sreloc;
  483. BFD_ASSERT (sreloc != NULL);
  484. sreloc->size += p->count * htab->sizeof_reloc;
  485. }
  486. return true;
  487. }
  488. /* Allocate space in .plt, .got and associated reloc sections for
  489. local dynamic relocs. */
  490. static int
  491. elf_x86_allocate_local_dynreloc (void **slot, void *inf)
  492. {
  493. struct elf_link_hash_entry *h
  494. = (struct elf_link_hash_entry *) *slot;
  495. if (h->type != STT_GNU_IFUNC
  496. || !h->def_regular
  497. || !h->ref_regular
  498. || !h->forced_local
  499. || h->root.type != bfd_link_hash_defined)
  500. abort ();
  501. return elf_x86_allocate_dynrelocs (h, inf);
  502. }
  503. /* Find and/or create a hash entry for local symbol. */
  504. struct elf_link_hash_entry *
  505. _bfd_elf_x86_get_local_sym_hash (struct elf_x86_link_hash_table *htab,
  506. bfd *abfd, const Elf_Internal_Rela *rel,
  507. bool create)
  508. {
  509. struct elf_x86_link_hash_entry e, *ret;
  510. asection *sec = abfd->sections;
  511. hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
  512. htab->r_sym (rel->r_info));
  513. void **slot;
  514. e.elf.indx = sec->id;
  515. e.elf.dynstr_index = htab->r_sym (rel->r_info);
  516. slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
  517. create ? INSERT : NO_INSERT);
  518. if (!slot)
  519. return NULL;
  520. if (*slot)
  521. {
  522. ret = (struct elf_x86_link_hash_entry *) *slot;
  523. return &ret->elf;
  524. }
  525. ret = (struct elf_x86_link_hash_entry *)
  526. objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
  527. sizeof (struct elf_x86_link_hash_entry));
  528. if (ret)
  529. {
  530. memset (ret, 0, sizeof (*ret));
  531. ret->elf.indx = sec->id;
  532. ret->elf.dynstr_index = htab->r_sym (rel->r_info);
  533. ret->elf.dynindx = -1;
  534. ret->plt_got.offset = (bfd_vma) -1;
  535. *slot = ret;
  536. }
  537. return &ret->elf;
  538. }
  539. /* Create an entry in a x86 ELF linker hash table. NB: THIS MUST BE IN
  540. SYNC WITH _bfd_elf_link_hash_newfunc. */
  541. struct bfd_hash_entry *
  542. _bfd_x86_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
  543. struct bfd_hash_table *table,
  544. const char *string)
  545. {
  546. /* Allocate the structure if it has not already been allocated by a
  547. subclass. */
  548. if (entry == NULL)
  549. {
  550. entry = (struct bfd_hash_entry *)
  551. bfd_hash_allocate (table,
  552. sizeof (struct elf_x86_link_hash_entry));
  553. if (entry == NULL)
  554. return entry;
  555. }
  556. /* Call the allocation method of the superclass. */
  557. entry = _bfd_link_hash_newfunc (entry, table, string);
  558. if (entry != NULL)
  559. {
  560. struct elf_x86_link_hash_entry *eh
  561. = (struct elf_x86_link_hash_entry *) entry;
  562. struct elf_link_hash_table *htab
  563. = (struct elf_link_hash_table *) table;
  564. memset (&eh->elf.size, 0,
  565. (sizeof (struct elf_x86_link_hash_entry)
  566. - offsetof (struct elf_link_hash_entry, size)));
  567. /* Set local fields. */
  568. eh->elf.indx = -1;
  569. eh->elf.dynindx = -1;
  570. eh->elf.got = htab->init_got_refcount;
  571. eh->elf.plt = htab->init_plt_refcount;
  572. /* Assume that we have been called by a non-ELF symbol reader.
  573. This flag is then reset by the code which reads an ELF input
  574. file. This ensures that a symbol created by a non-ELF symbol
  575. reader will have the flag set correctly. */
  576. eh->elf.non_elf = 1;
  577. eh->plt_second.offset = (bfd_vma) -1;
  578. eh->plt_got.offset = (bfd_vma) -1;
  579. eh->tlsdesc_got = (bfd_vma) -1;
  580. eh->zero_undefweak = 1;
  581. }
  582. return entry;
  583. }
  584. /* Compute a hash of a local hash entry. We use elf_link_hash_entry
  585. for local symbol so that we can handle local STT_GNU_IFUNC symbols
  586. as global symbol. We reuse indx and dynstr_index for local symbol
  587. hash since they aren't used by global symbols in this backend. */
  588. hashval_t
  589. _bfd_x86_elf_local_htab_hash (const void *ptr)
  590. {
  591. struct elf_link_hash_entry *h
  592. = (struct elf_link_hash_entry *) ptr;
  593. return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
  594. }
  595. /* Compare local hash entries. */
  596. int
  597. _bfd_x86_elf_local_htab_eq (const void *ptr1, const void *ptr2)
  598. {
  599. struct elf_link_hash_entry *h1
  600. = (struct elf_link_hash_entry *) ptr1;
  601. struct elf_link_hash_entry *h2
  602. = (struct elf_link_hash_entry *) ptr2;
  603. return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
  604. }
  605. /* Destroy an x86 ELF linker hash table. */
  606. static void
  607. elf_x86_link_hash_table_free (bfd *obfd)
  608. {
  609. struct elf_x86_link_hash_table *htab
  610. = (struct elf_x86_link_hash_table *) obfd->link.hash;
  611. if (htab->loc_hash_table)
  612. htab_delete (htab->loc_hash_table);
  613. if (htab->loc_hash_memory)
  614. objalloc_free ((struct objalloc *) htab->loc_hash_memory);
  615. _bfd_elf_link_hash_table_free (obfd);
  616. }
  617. static bool
  618. elf_i386_is_reloc_section (const char *secname)
  619. {
  620. return startswith (secname, ".rel");
  621. }
  622. static bool
  623. elf_x86_64_is_reloc_section (const char *secname)
  624. {
  625. return startswith (secname, ".rela");
  626. }
  627. /* Create an x86 ELF linker hash table. */
  628. struct bfd_link_hash_table *
  629. _bfd_x86_elf_link_hash_table_create (bfd *abfd)
  630. {
  631. struct elf_x86_link_hash_table *ret;
  632. const struct elf_backend_data *bed;
  633. size_t amt = sizeof (struct elf_x86_link_hash_table);
  634. ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
  635. if (ret == NULL)
  636. return NULL;
  637. bed = get_elf_backend_data (abfd);
  638. if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
  639. _bfd_x86_elf_link_hash_newfunc,
  640. sizeof (struct elf_x86_link_hash_entry),
  641. bed->target_id))
  642. {
  643. free (ret);
  644. return NULL;
  645. }
  646. if (bed->target_id == X86_64_ELF_DATA)
  647. {
  648. ret->is_reloc_section = elf_x86_64_is_reloc_section;
  649. ret->got_entry_size = 8;
  650. ret->pcrel_plt = true;
  651. ret->tls_get_addr = "__tls_get_addr";
  652. ret->relative_r_type = R_X86_64_RELATIVE;
  653. ret->relative_r_name = "R_X86_64_RELATIVE";
  654. ret->elf_append_reloc = elf_append_rela;
  655. ret->elf_write_addend_in_got = _bfd_elf64_write_addend;
  656. }
  657. if (ABI_64_P (abfd))
  658. {
  659. ret->sizeof_reloc = sizeof (Elf64_External_Rela);
  660. ret->pointer_r_type = R_X86_64_64;
  661. ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
  662. ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
  663. ret->elf_write_addend = _bfd_elf64_write_addend;
  664. }
  665. else
  666. {
  667. if (bed->target_id == X86_64_ELF_DATA)
  668. {
  669. ret->sizeof_reloc = sizeof (Elf32_External_Rela);
  670. ret->pointer_r_type = R_X86_64_32;
  671. ret->dynamic_interpreter = ELFX32_DYNAMIC_INTERPRETER;
  672. ret->dynamic_interpreter_size
  673. = sizeof ELFX32_DYNAMIC_INTERPRETER;
  674. ret->elf_write_addend = _bfd_elf32_write_addend;
  675. }
  676. else
  677. {
  678. ret->is_reloc_section = elf_i386_is_reloc_section;
  679. ret->sizeof_reloc = sizeof (Elf32_External_Rel);
  680. ret->got_entry_size = 4;
  681. ret->pcrel_plt = false;
  682. ret->pointer_r_type = R_386_32;
  683. ret->relative_r_type = R_386_RELATIVE;
  684. ret->relative_r_name = "R_386_RELATIVE";
  685. ret->elf_append_reloc = elf_append_rel;
  686. ret->elf_write_addend = _bfd_elf32_write_addend;
  687. ret->elf_write_addend_in_got = _bfd_elf32_write_addend;
  688. ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
  689. ret->dynamic_interpreter_size
  690. = sizeof ELF32_DYNAMIC_INTERPRETER;
  691. ret->tls_get_addr = "___tls_get_addr";
  692. }
  693. }
  694. ret->loc_hash_table = htab_try_create (1024,
  695. _bfd_x86_elf_local_htab_hash,
  696. _bfd_x86_elf_local_htab_eq,
  697. NULL);
  698. ret->loc_hash_memory = objalloc_create ();
  699. if (!ret->loc_hash_table || !ret->loc_hash_memory)
  700. {
  701. elf_x86_link_hash_table_free (abfd);
  702. return NULL;
  703. }
  704. ret->elf.root.hash_table_free = elf_x86_link_hash_table_free;
  705. return &ret->elf.root;
  706. }
  707. /* Sort relocs into address order. */
  708. int
  709. _bfd_x86_elf_compare_relocs (const void *ap, const void *bp)
  710. {
  711. const arelent *a = * (const arelent **) ap;
  712. const arelent *b = * (const arelent **) bp;
  713. if (a->address > b->address)
  714. return 1;
  715. else if (a->address < b->address)
  716. return -1;
  717. else
  718. return 0;
  719. }
  720. /* Mark symbol, NAME, as locally defined by linker if it is referenced
  721. and not defined in a relocatable object file. */
  722. static void
  723. elf_x86_linker_defined (struct bfd_link_info *info, const char *name)
  724. {
  725. struct elf_link_hash_entry *h;
  726. h = elf_link_hash_lookup (elf_hash_table (info), name,
  727. false, false, false);
  728. if (h == NULL)
  729. return;
  730. while (h->root.type == bfd_link_hash_indirect)
  731. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  732. if (h->root.type == bfd_link_hash_new
  733. || h->root.type == bfd_link_hash_undefined
  734. || h->root.type == bfd_link_hash_undefweak
  735. || h->root.type == bfd_link_hash_common
  736. || (!h->def_regular && h->def_dynamic))
  737. {
  738. elf_x86_hash_entry (h)->local_ref = 2;
  739. elf_x86_hash_entry (h)->linker_def = 1;
  740. }
  741. }
  742. /* Hide a linker-defined symbol, NAME, with hidden visibility. */
  743. static void
  744. elf_x86_hide_linker_defined (struct bfd_link_info *info,
  745. const char *name)
  746. {
  747. struct elf_link_hash_entry *h;
  748. h = elf_link_hash_lookup (elf_hash_table (info), name,
  749. false, false, false);
  750. if (h == NULL)
  751. return;
  752. while (h->root.type == bfd_link_hash_indirect)
  753. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  754. if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
  755. || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
  756. _bfd_elf_link_hash_hide_symbol (info, h, true);
  757. }
  758. bool
  759. _bfd_x86_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info)
  760. {
  761. if (!bfd_link_relocatable (info))
  762. {
  763. /* Check for __tls_get_addr reference. */
  764. struct elf_x86_link_hash_table *htab;
  765. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  766. htab = elf_x86_hash_table (info, bed->target_id);
  767. if (htab)
  768. {
  769. struct elf_link_hash_entry *h;
  770. h = elf_link_hash_lookup (elf_hash_table (info),
  771. htab->tls_get_addr,
  772. false, false, false);
  773. if (h != NULL)
  774. {
  775. elf_x86_hash_entry (h)->tls_get_addr = 1;
  776. /* Check the versioned __tls_get_addr symbol. */
  777. while (h->root.type == bfd_link_hash_indirect)
  778. {
  779. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  780. elf_x86_hash_entry (h)->tls_get_addr = 1;
  781. }
  782. }
  783. /* "__ehdr_start" will be defined by linker as a hidden symbol
  784. later if it is referenced and not defined. */
  785. elf_x86_linker_defined (info, "__ehdr_start");
  786. if (bfd_link_executable (info))
  787. {
  788. /* References to __bss_start, _end and _edata should be
  789. locally resolved within executables. */
  790. elf_x86_linker_defined (info, "__bss_start");
  791. elf_x86_linker_defined (info, "_end");
  792. elf_x86_linker_defined (info, "_edata");
  793. }
  794. else
  795. {
  796. /* Hide hidden __bss_start, _end and _edata in shared
  797. libraries. */
  798. elf_x86_hide_linker_defined (info, "__bss_start");
  799. elf_x86_hide_linker_defined (info, "_end");
  800. elf_x86_hide_linker_defined (info, "_edata");
  801. }
  802. }
  803. }
  804. /* Invoke the regular ELF backend linker to do all the work. */
  805. return _bfd_elf_link_check_relocs (abfd, info);
  806. }
  807. /* Look through the relocs for a section before allocation to make the
  808. dynamic reloc section. */
  809. bool
  810. _bfd_x86_elf_check_relocs (bfd *abfd,
  811. struct bfd_link_info *info,
  812. asection *sec,
  813. const Elf_Internal_Rela *relocs)
  814. {
  815. struct elf_x86_link_hash_table *htab;
  816. Elf_Internal_Shdr *symtab_hdr;
  817. struct elf_link_hash_entry **sym_hashes;
  818. const Elf_Internal_Rela *rel;
  819. const Elf_Internal_Rela *rel_end;
  820. asection *sreloc;
  821. const struct elf_backend_data *bed;
  822. bool is_x86_64;
  823. if (bfd_link_relocatable (info))
  824. return true;
  825. bed = get_elf_backend_data (abfd);
  826. htab = elf_x86_hash_table (info, bed->target_id);
  827. if (htab == NULL)
  828. {
  829. sec->check_relocs_failed = 1;
  830. return false;
  831. }
  832. is_x86_64 = bed->target_id == X86_64_ELF_DATA;
  833. symtab_hdr = &elf_symtab_hdr (abfd);
  834. sym_hashes = elf_sym_hashes (abfd);
  835. rel_end = relocs + sec->reloc_count;
  836. for (rel = relocs; rel < rel_end; rel++)
  837. {
  838. unsigned int r_type;
  839. unsigned int r_symndx;
  840. struct elf_link_hash_entry *h;
  841. r_symndx = htab->r_sym (rel->r_info);
  842. r_type = ELF32_R_TYPE (rel->r_info);
  843. if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  844. {
  845. /* xgettext:c-format */
  846. _bfd_error_handler (_("%pB: bad symbol index: %d"),
  847. abfd, r_symndx);
  848. goto error_return;
  849. }
  850. if (r_symndx < symtab_hdr->sh_info)
  851. h = NULL;
  852. else
  853. {
  854. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  855. while (h->root.type == bfd_link_hash_indirect
  856. || h->root.type == bfd_link_hash_warning)
  857. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  858. }
  859. if (X86_NEED_DYNAMIC_RELOC_TYPE_P (is_x86_64, r_type)
  860. && NEED_DYNAMIC_RELOCATION_P (is_x86_64, info, true, h, sec,
  861. r_type, htab->pointer_r_type))
  862. {
  863. /* We may copy these reloc types into the output file.
  864. Create a reloc section in dynobj and make room for
  865. this reloc. */
  866. sreloc = _bfd_elf_make_dynamic_reloc_section
  867. (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
  868. abfd, sec->use_rela_p);
  869. if (sreloc != NULL)
  870. return true;
  871. error_return:
  872. sec->check_relocs_failed = 1;
  873. return false;
  874. }
  875. }
  876. return true;
  877. }
  878. /* Add an entry to the relative reloc record. */
  879. static bool
  880. elf_x86_relative_reloc_record_add
  881. (struct bfd_link_info *info,
  882. struct elf_x86_relative_reloc_data *relative_reloc,
  883. Elf_Internal_Rela *rel, asection *sec,
  884. asection *sym_sec, struct elf_link_hash_entry *h,
  885. Elf_Internal_Sym *sym, bfd_vma offset)
  886. {
  887. bfd_size_type newidx;
  888. if (relative_reloc->data == NULL)
  889. {
  890. relative_reloc->data = bfd_malloc
  891. (sizeof (struct elf_x86_relative_reloc_record));
  892. relative_reloc->count = 0;
  893. relative_reloc->size = 1;
  894. }
  895. newidx = relative_reloc->count++;
  896. if (relative_reloc->count > relative_reloc->size)
  897. {
  898. relative_reloc->size <<= 1;
  899. relative_reloc->data = bfd_realloc
  900. (relative_reloc->data,
  901. (relative_reloc->size
  902. * sizeof (struct elf_x86_relative_reloc_record)));
  903. }
  904. if (relative_reloc->data == NULL)
  905. {
  906. info->callbacks->einfo
  907. /* xgettext:c-format */
  908. (_("%F%P: %pB: failed to allocate relative reloc record\n"),
  909. info->output_bfd);
  910. return false;
  911. }
  912. relative_reloc->data[newidx].rel = *rel;
  913. relative_reloc->data[newidx].sec = sec;
  914. if (h != NULL)
  915. {
  916. /* Set SYM to NULL to indicate a global symbol. */
  917. relative_reloc->data[newidx].sym = NULL;
  918. relative_reloc->data[newidx].u.h = h;
  919. }
  920. else
  921. {
  922. relative_reloc->data[newidx].sym = sym;
  923. relative_reloc->data[newidx].u.sym_sec = sym_sec;
  924. }
  925. relative_reloc->data[newidx].offset = offset;
  926. relative_reloc->data[newidx].address = 0;
  927. return true;
  928. }
  929. /* After input sections have been mapped to output sections and
  930. addresses of output sections are set initiallly, scan input
  931. relocations with the same logic in relocate_section to determine
  932. if a relative relocation should be generated. Save the relative
  933. relocation candidate information for sizing the DT_RELR section
  934. later after all symbols addresses can be determined. */
  935. bool
  936. _bfd_x86_elf_link_relax_section (bfd *abfd ATTRIBUTE_UNUSED,
  937. asection *input_section,
  938. struct bfd_link_info *info,
  939. bool *again)
  940. {
  941. Elf_Internal_Shdr *symtab_hdr;
  942. Elf_Internal_Rela *internal_relocs;
  943. Elf_Internal_Rela *irel, *irelend;
  944. Elf_Internal_Sym *isymbuf = NULL;
  945. struct elf_link_hash_entry **sym_hashes;
  946. const struct elf_backend_data *bed;
  947. struct elf_x86_link_hash_table *htab;
  948. bfd_vma *local_got_offsets;
  949. bool is_x86_64;
  950. bool unaligned_section;
  951. if (bfd_link_relocatable (info))
  952. return true;
  953. /* Assume we're not going to change any sizes, and we'll only need
  954. one pass. */
  955. *again = false;
  956. bed = get_elf_backend_data (abfd);
  957. htab = elf_x86_hash_table (info, bed->target_id);
  958. if (htab == NULL)
  959. return true;
  960. /* Nothing to do if there are no relocations or relative relocations
  961. have been packed. */
  962. if (input_section == htab->elf.srelrdyn
  963. || input_section->relative_reloc_packed
  964. || ((input_section->flags & (SEC_RELOC | SEC_ALLOC))
  965. != (SEC_RELOC | SEC_ALLOC))
  966. || (input_section->flags & SEC_DEBUGGING) != 0
  967. || input_section->reloc_count == 0)
  968. return true;
  969. /* Skip if the section isn't aligned. */
  970. unaligned_section = input_section->alignment_power == 0;
  971. is_x86_64 = bed->target_id == X86_64_ELF_DATA;
  972. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  973. sym_hashes = elf_sym_hashes (abfd);
  974. local_got_offsets = elf_local_got_offsets (abfd);
  975. /* Load the relocations for this section. */
  976. internal_relocs =
  977. _bfd_elf_link_read_relocs (abfd, input_section, NULL,
  978. (Elf_Internal_Rela *) NULL,
  979. info->keep_memory);
  980. if (internal_relocs == NULL)
  981. return false;
  982. irelend = internal_relocs + input_section->reloc_count;
  983. for (irel = internal_relocs; irel < irelend; irel++)
  984. {
  985. unsigned int r_type;
  986. unsigned int r_symndx;
  987. Elf_Internal_Sym *isym;
  988. struct elf_link_hash_entry *h;
  989. struct elf_x86_link_hash_entry *eh;
  990. bfd_vma offset;
  991. bool resolved_to_zero;
  992. bool need_copy_reloc_in_pie;
  993. bool pc32_reloc;
  994. asection *sec;
  995. /* Offset must be a multiple of 2. */
  996. bool unaligned_offset = (irel->r_offset & 1) != 0;
  997. /* True if there is a relative relocation against a dynamic
  998. symbol. */
  999. bool dynamic_relative_reloc_p;
  1000. /* Get the value of the symbol referred to by the reloc. */
  1001. r_symndx = htab->r_sym (irel->r_info);
  1002. r_type = ELF32_R_TYPE (irel->r_info);
  1003. /* Clear the R_X86_64_converted_reloc_bit bit. */
  1004. r_type &= ~R_X86_64_converted_reloc_bit;
  1005. sec = NULL;
  1006. h = NULL;
  1007. dynamic_relative_reloc_p = false;
  1008. if (r_symndx < symtab_hdr->sh_info)
  1009. {
  1010. /* Read this BFD's local symbols. */
  1011. if (isymbuf == NULL)
  1012. {
  1013. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1014. if (isymbuf == NULL)
  1015. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1016. symtab_hdr->sh_info, 0,
  1017. NULL, NULL, NULL);
  1018. if (isymbuf == NULL)
  1019. goto error_return;
  1020. }
  1021. isym = isymbuf + r_symndx;
  1022. switch (isym->st_shndx)
  1023. {
  1024. case SHN_ABS:
  1025. sec = bfd_abs_section_ptr;
  1026. break;
  1027. case SHN_COMMON:
  1028. sec = bfd_com_section_ptr;
  1029. break;
  1030. case SHN_X86_64_LCOMMON:
  1031. if (!is_x86_64)
  1032. abort ();
  1033. sec = &_bfd_elf_large_com_section;
  1034. break;
  1035. default:
  1036. sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1037. break;
  1038. }
  1039. /* Skip relocation against local STT_GNU_IFUNC symbol. */
  1040. if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
  1041. continue;
  1042. eh = (struct elf_x86_link_hash_entry *) h;
  1043. resolved_to_zero = false;
  1044. }
  1045. else
  1046. {
  1047. /* Get H and SEC for GENERATE_DYNAMIC_RELOCATION_P below. */
  1048. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1049. while (h->root.type == bfd_link_hash_indirect
  1050. || h->root.type == bfd_link_hash_warning)
  1051. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1052. if (h->root.type == bfd_link_hash_defined
  1053. || h->root.type == bfd_link_hash_defweak)
  1054. sec = h->root.u.def.section;
  1055. /* Skip relocation against STT_GNU_IFUNC symbol. */
  1056. if (h->type == STT_GNU_IFUNC)
  1057. continue;
  1058. eh = (struct elf_x86_link_hash_entry *) h;
  1059. resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
  1060. /* NB: See how elf_backend_finish_dynamic_symbol is called
  1061. from elf_link_output_extsym. */
  1062. if ((h->dynindx != -1 || h->forced_local)
  1063. && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
  1064. || h->root.type != bfd_link_hash_undefweak)
  1065. || !h->forced_local)
  1066. && h->got.offset != (bfd_vma) -1
  1067. && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
  1068. && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
  1069. && !resolved_to_zero
  1070. && SYMBOL_REFERENCES_LOCAL_P (info, h)
  1071. && SYMBOL_DEFINED_NON_SHARED_P (h))
  1072. dynamic_relative_reloc_p = true;
  1073. isym = NULL;
  1074. }
  1075. if (X86_GOT_TYPE_P (is_x86_64, r_type))
  1076. {
  1077. /* Pack GOT relative relocations. There should be only a
  1078. single R_*_RELATIVE relocation in GOT. */
  1079. if (eh != NULL)
  1080. {
  1081. if (eh->got_relative_reloc_done)
  1082. continue;
  1083. if (!(dynamic_relative_reloc_p
  1084. || (RESOLVED_LOCALLY_P (info, h, htab)
  1085. && GENERATE_RELATIVE_RELOC_P (info, h))))
  1086. continue;
  1087. if (!dynamic_relative_reloc_p)
  1088. eh->no_finish_dynamic_symbol = 1;
  1089. eh->got_relative_reloc_done = 1;
  1090. offset = h->got.offset;
  1091. }
  1092. else
  1093. {
  1094. if (elf_x86_relative_reloc_done (abfd)[r_symndx])
  1095. continue;
  1096. if (!X86_LOCAL_GOT_RELATIVE_RELOC_P (is_x86_64, info,
  1097. isym))
  1098. continue;
  1099. elf_x86_relative_reloc_done (abfd)[r_symndx] = 1;
  1100. offset = local_got_offsets[r_symndx];
  1101. }
  1102. if (!elf_x86_relative_reloc_record_add (info,
  1103. &htab->relative_reloc,
  1104. irel, htab->elf.sgot,
  1105. sec, h, isym, offset))
  1106. goto error_return;
  1107. continue;
  1108. }
  1109. if (is_x86_64
  1110. && irel->r_addend == 0
  1111. && !ABI_64_P (info->output_bfd))
  1112. {
  1113. /* For x32, if addend is zero, treat R_X86_64_64 like
  1114. R_X86_64_32 and R_X86_64_SIZE64 like R_X86_64_SIZE32. */
  1115. if (r_type == R_X86_64_64)
  1116. r_type = R_X86_64_32;
  1117. else if (r_type == R_X86_64_SIZE64)
  1118. r_type = R_X86_64_SIZE32;
  1119. }
  1120. if (!X86_RELATIVE_RELOC_TYPE_P (is_x86_64, r_type))
  1121. continue;
  1122. /* Pack non-GOT relative relocations. */
  1123. if (is_x86_64)
  1124. {
  1125. need_copy_reloc_in_pie =
  1126. (bfd_link_pie (info)
  1127. && h != NULL
  1128. && (h->needs_copy
  1129. || eh->needs_copy
  1130. || (h->root.type == bfd_link_hash_undefined))
  1131. && (X86_PCREL_TYPE_P (true, r_type)
  1132. || X86_SIZE_TYPE_P (true, r_type)));
  1133. pc32_reloc = false;
  1134. }
  1135. else
  1136. {
  1137. need_copy_reloc_in_pie = false;
  1138. pc32_reloc = r_type == R_386_PC32;
  1139. }
  1140. if (GENERATE_DYNAMIC_RELOCATION_P (is_x86_64, info, eh, r_type,
  1141. sec, need_copy_reloc_in_pie,
  1142. resolved_to_zero, pc32_reloc))
  1143. {
  1144. /* When generating a shared object, these relocations
  1145. are copied into the output file to be resolved at run
  1146. time. */
  1147. offset = _bfd_elf_section_offset (info->output_bfd, info,
  1148. input_section,
  1149. irel->r_offset);
  1150. if (offset == (bfd_vma) -1
  1151. || offset == (bfd_vma) -2
  1152. || COPY_INPUT_RELOC_P (is_x86_64, info, h, r_type))
  1153. continue;
  1154. /* This symbol is local, or marked to become local. When
  1155. relocation overflow check is disabled, we convert
  1156. R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
  1157. if (is_x86_64
  1158. && !(r_type == htab->pointer_r_type
  1159. || (r_type == R_X86_64_32
  1160. && htab->params->no_reloc_overflow_check)))
  1161. continue;
  1162. if (!elf_x86_relative_reloc_record_add
  1163. (info,
  1164. ((unaligned_section || unaligned_offset)
  1165. ? &htab->unaligned_relative_reloc
  1166. : &htab->relative_reloc),
  1167. irel, input_section, sec, h, isym, offset))
  1168. goto error_return;
  1169. }
  1170. }
  1171. input_section->relative_reloc_packed = 1;
  1172. return true;
  1173. error_return:
  1174. if ((unsigned char *) isymbuf != symtab_hdr->contents)
  1175. free (isymbuf);
  1176. if (elf_section_data (input_section)->relocs != internal_relocs)
  1177. free (internal_relocs);
  1178. return false;
  1179. }
  1180. /* Add an entry to the 64-bit DT_RELR bitmap. */
  1181. static void
  1182. elf64_dt_relr_bitmap_add
  1183. (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
  1184. uint64_t entry)
  1185. {
  1186. bfd_size_type newidx;
  1187. if (bitmap->u.elf64 == NULL)
  1188. {
  1189. bitmap->u.elf64 = bfd_malloc (sizeof (uint64_t));
  1190. bitmap->count = 0;
  1191. bitmap->size = 1;
  1192. }
  1193. newidx = bitmap->count++;
  1194. if (bitmap->count > bitmap->size)
  1195. {
  1196. bitmap->size <<= 1;
  1197. bitmap->u.elf64 = bfd_realloc (bitmap->u.elf64,
  1198. (bitmap->size * sizeof (uint64_t)));
  1199. }
  1200. if (bitmap->u.elf64 == NULL)
  1201. {
  1202. info->callbacks->einfo
  1203. /* xgettext:c-format */
  1204. (_("%F%P: %pB: failed to allocate 64-bit DT_RELR bitmap\n"),
  1205. info->output_bfd);
  1206. }
  1207. bitmap->u.elf64[newidx] = entry;
  1208. }
  1209. /* Add an entry to the 32-bit DT_RELR bitmap. */
  1210. static void
  1211. elf32_dt_relr_bitmap_add
  1212. (struct bfd_link_info *info, struct elf_dt_relr_bitmap *bitmap,
  1213. uint32_t entry)
  1214. {
  1215. bfd_size_type newidx;
  1216. if (bitmap->u.elf32 == NULL)
  1217. {
  1218. bitmap->u.elf32 = bfd_malloc (sizeof (uint32_t));
  1219. bitmap->count = 0;
  1220. bitmap->size = 1;
  1221. }
  1222. newidx = bitmap->count++;
  1223. if (bitmap->count > bitmap->size)
  1224. {
  1225. bitmap->size <<= 1;
  1226. bitmap->u.elf32 = bfd_realloc (bitmap->u.elf32,
  1227. (bitmap->size * sizeof (uint32_t)));
  1228. }
  1229. if (bitmap->u.elf32 == NULL)
  1230. {
  1231. info->callbacks->einfo
  1232. /* xgettext:c-format */
  1233. (_("%F%P: %pB: failed to allocate 32-bit DT_RELR bitmap\n"),
  1234. info->output_bfd);
  1235. }
  1236. bitmap->u.elf32[newidx] = entry;
  1237. }
  1238. void
  1239. _bfd_elf32_write_addend (bfd *abfd, uint64_t value, void *addr)
  1240. {
  1241. bfd_put_32 (abfd, value, addr);
  1242. }
  1243. void
  1244. _bfd_elf64_write_addend (bfd *abfd, uint64_t value, void *addr)
  1245. {
  1246. bfd_put_64 (abfd, value, addr);
  1247. }
  1248. /* Size or finish relative relocations to determine the run-time
  1249. addresses for DT_RELR bitmap computation later. OUTREL is set
  1250. to NULL in the sizing phase and non-NULL in the finising phase
  1251. where the regular relative relocations will be written out. */
  1252. static void
  1253. elf_x86_size_or_finish_relative_reloc
  1254. (bool is_x86_64, struct bfd_link_info *info,
  1255. struct elf_x86_link_hash_table *htab, bool unaligned,
  1256. Elf_Internal_Rela *outrel)
  1257. {
  1258. unsigned int align_mask;
  1259. bfd_size_type i, count;
  1260. asection *sec, *srel;
  1261. struct elf_link_hash_entry *h;
  1262. bfd_vma offset;
  1263. Elf_Internal_Sym *sym;
  1264. asection *sym_sec;
  1265. asection *sgot = htab->elf.sgot;
  1266. asection *srelgot = htab->elf.srelgot;
  1267. struct elf_x86_relative_reloc_data *relative_reloc;
  1268. if (unaligned)
  1269. {
  1270. align_mask = 0;
  1271. relative_reloc = &htab->unaligned_relative_reloc;
  1272. }
  1273. else
  1274. {
  1275. align_mask = 1;
  1276. relative_reloc = &htab->relative_reloc;
  1277. }
  1278. count = relative_reloc->count;
  1279. for (i = 0; i < count; i++)
  1280. {
  1281. sec = relative_reloc->data[i].sec;
  1282. sym = relative_reloc->data[i].sym;
  1283. /* If SYM is NULL, it must be a global symbol. */
  1284. if (sym == NULL)
  1285. h = relative_reloc->data[i].u.h;
  1286. else
  1287. h = NULL;
  1288. if (is_x86_64)
  1289. {
  1290. bfd_vma relocation;
  1291. /* This function may be called more than once and REL may be
  1292. updated by _bfd_elf_rela_local_sym below. */
  1293. Elf_Internal_Rela rel = relative_reloc->data[i].rel;
  1294. if (h != NULL)
  1295. {
  1296. if (h->root.type == bfd_link_hash_defined
  1297. || h->root.type == bfd_link_hash_defweak)
  1298. {
  1299. sym_sec = h->root.u.def.section;
  1300. relocation = (h->root.u.def.value
  1301. + sym_sec->output_section->vma
  1302. + sym_sec->output_offset);
  1303. }
  1304. else
  1305. {
  1306. /* Allow undefined symbol only at the sizing phase.
  1307. Otherwise skip undefined symbol here. Undefined
  1308. symbol will be reported by relocate_section. */
  1309. if (outrel == NULL)
  1310. relocation = 0;
  1311. else
  1312. continue;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. sym_sec = relative_reloc->data[i].u.sym_sec;
  1318. relocation = _bfd_elf_rela_local_sym
  1319. (info->output_bfd, sym, &sym_sec, &rel);
  1320. }
  1321. if (outrel != NULL)
  1322. {
  1323. outrel->r_addend = relocation;
  1324. if (sec == sgot)
  1325. {
  1326. if (h != NULL && h->needs_plt)
  1327. abort ();
  1328. }
  1329. else
  1330. outrel->r_addend += rel.r_addend;
  1331. /* Write the implicit addend if ALIGN_MASK isn't 0. */
  1332. if (align_mask)
  1333. {
  1334. if (sec == sgot)
  1335. {
  1336. if (relative_reloc->data[i].offset >= sec->size)
  1337. abort ();
  1338. htab->elf_write_addend_in_got
  1339. (info->output_bfd, outrel->r_addend,
  1340. sec->contents + relative_reloc->data[i].offset);
  1341. }
  1342. else
  1343. {
  1344. if (rel.r_offset >= sec->size)
  1345. abort ();
  1346. htab->elf_write_addend
  1347. (info->output_bfd, outrel->r_addend,
  1348. (elf_section_data (sec)->this_hdr.contents
  1349. + rel.r_offset));
  1350. }
  1351. }
  1352. }
  1353. }
  1354. if (sec == sgot)
  1355. srel = srelgot;
  1356. else
  1357. srel = elf_section_data (sec)->sreloc;
  1358. offset = (sec->output_section->vma + sec->output_offset
  1359. + relative_reloc->data[i].offset);
  1360. relative_reloc->data[i].address = offset;
  1361. if (outrel != NULL)
  1362. {
  1363. outrel->r_offset = offset;
  1364. if ((outrel->r_offset & align_mask) != 0)
  1365. abort ();
  1366. if (htab->params->report_relative_reloc)
  1367. _bfd_x86_elf_link_report_relative_reloc
  1368. (info, sec, h, sym, htab->relative_r_name, outrel);
  1369. /* Generate regular relative relocation if ALIGN_MASK is 0. */
  1370. if (align_mask == 0)
  1371. htab->elf_append_reloc (info->output_bfd, srel, outrel);
  1372. }
  1373. }
  1374. }
  1375. /* Compute the DT_RELR section size. Set NEED_PLAYOUT to true if
  1376. the DT_RELR section size has been increased. */
  1377. static void
  1378. elf_x86_compute_dl_relr_bitmap
  1379. (struct bfd_link_info *info, struct elf_x86_link_hash_table *htab,
  1380. bool *need_layout)
  1381. {
  1382. bfd_vma base;
  1383. bfd_size_type i, count, new_count;
  1384. struct elf_x86_relative_reloc_data *relative_reloc =
  1385. &htab->relative_reloc;
  1386. /* Save the old DT_RELR bitmap count. Don't shrink the DT_RELR bitmap
  1387. if the new DT_RELR bitmap count is smaller than the old one. Pad
  1388. with trailing 1s which won't be decoded to more relocations. */
  1389. bfd_size_type dt_relr_bitmap_count = htab->dt_relr_bitmap.count;
  1390. /* Clear the DT_RELR bitmap count. */
  1391. htab->dt_relr_bitmap.count = 0;
  1392. count = relative_reloc->count;
  1393. if (ABI_64_P (info->output_bfd))
  1394. {
  1395. /* Compute the 64-bit DT_RELR bitmap. */
  1396. i = 0;
  1397. while (i < count)
  1398. {
  1399. if ((relative_reloc->data[i].address % 1) != 0)
  1400. abort ();
  1401. elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
  1402. relative_reloc->data[i].address);
  1403. base = relative_reloc->data[i].address + 8;
  1404. i++;
  1405. while (i < count)
  1406. {
  1407. uint64_t bitmap = 0;
  1408. for (; i < count; i++)
  1409. {
  1410. bfd_vma delta = (relative_reloc->data[i].address
  1411. - base);
  1412. /* Stop if it is too far from base. */
  1413. if (delta >= 63 * 8)
  1414. break;
  1415. /* Stop if it isn't a multiple of 8. */
  1416. if ((delta % 8) != 0)
  1417. break;
  1418. bitmap |= 1ULL << (delta / 8);
  1419. }
  1420. if (bitmap == 0)
  1421. break;
  1422. elf64_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
  1423. (bitmap << 1) | 1);
  1424. base += 63 * 8;
  1425. }
  1426. }
  1427. new_count = htab->dt_relr_bitmap.count;
  1428. if (dt_relr_bitmap_count > new_count)
  1429. {
  1430. /* Don't shrink the DT_RELR section size to avoid section
  1431. layout oscillation. Instead, pad the DT_RELR bitmap with
  1432. 1s which do not decode to more relocations. */
  1433. htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
  1434. count = dt_relr_bitmap_count - new_count;
  1435. for (i = 0; i < count; i++)
  1436. htab->dt_relr_bitmap.u.elf64[new_count + i] = 1;
  1437. }
  1438. }
  1439. else
  1440. {
  1441. /* Compute the 32-bit DT_RELR bitmap. */
  1442. i = 0;
  1443. while (i < count)
  1444. {
  1445. if ((relative_reloc->data[i].address % 1) != 0)
  1446. abort ();
  1447. elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
  1448. relative_reloc->data[i].address);
  1449. base = relative_reloc->data[i].address + 4;
  1450. i++;
  1451. while (i < count)
  1452. {
  1453. uint32_t bitmap = 0;
  1454. for (; i < count; i++)
  1455. {
  1456. bfd_vma delta = (relative_reloc->data[i].address
  1457. - base);
  1458. /* Stop if it is too far from base. */
  1459. if (delta >= 31 * 4)
  1460. break;
  1461. /* Stop if it isn't a multiple of 4. */
  1462. if ((delta % 4) != 0)
  1463. break;
  1464. bitmap |= 1ULL << (delta / 4);
  1465. }
  1466. if (bitmap == 0)
  1467. break;
  1468. elf32_dt_relr_bitmap_add (info, &htab->dt_relr_bitmap,
  1469. (bitmap << 1) | 1);
  1470. base += 31 * 4;
  1471. }
  1472. }
  1473. new_count = htab->dt_relr_bitmap.count;
  1474. if (dt_relr_bitmap_count > new_count)
  1475. {
  1476. /* Don't shrink the DT_RELR section size to avoid section
  1477. layout oscillation. Instead, pad the DT_RELR bitmap with
  1478. 1s which do not decode to more relocations. */
  1479. htab->dt_relr_bitmap.count = dt_relr_bitmap_count;
  1480. count = dt_relr_bitmap_count - new_count;
  1481. for (i = 0; i < count; i++)
  1482. htab->dt_relr_bitmap.u.elf32[new_count + i] = 1;
  1483. }
  1484. }
  1485. if (htab->dt_relr_bitmap.count != dt_relr_bitmap_count)
  1486. {
  1487. if (need_layout)
  1488. {
  1489. /* The .relr.dyn section size is changed. Update the section
  1490. size and tell linker to layout sections again. */
  1491. htab->elf.srelrdyn->size =
  1492. (htab->dt_relr_bitmap.count
  1493. * (ABI_64_P (info->output_bfd) ? 8 : 4));
  1494. *need_layout = true;
  1495. }
  1496. else
  1497. info->callbacks->einfo
  1498. /* xgettext:c-format */
  1499. (_("%F%P: %pB: size of compact relative reloc section is "
  1500. "changed: new (%lu) != old (%lu)\n"),
  1501. info->output_bfd, htab->dt_relr_bitmap.count,
  1502. dt_relr_bitmap_count);
  1503. }
  1504. }
  1505. /* Write out the DT_RELR section. */
  1506. static void
  1507. elf_x86_write_dl_relr_bitmap (struct bfd_link_info *info,
  1508. struct elf_x86_link_hash_table *htab)
  1509. {
  1510. asection *sec = htab->elf.srelrdyn;
  1511. bfd_size_type size = sec->size;
  1512. bfd_size_type i;
  1513. unsigned char *contents;
  1514. contents = (unsigned char *) bfd_alloc (sec->owner, size);
  1515. if (contents == NULL)
  1516. info->callbacks->einfo
  1517. /* xgettext:c-format */
  1518. (_("%F%P: %pB: failed to allocate compact relative reloc section\n"),
  1519. info->output_bfd);
  1520. /* Cache the section contents for elf_link_input_bfd. */
  1521. sec->contents = contents;
  1522. if (ABI_64_P (info->output_bfd))
  1523. for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 8)
  1524. bfd_put_64 (info->output_bfd, htab->dt_relr_bitmap.u.elf64[i],
  1525. contents);
  1526. else
  1527. for (i = 0; i < htab->dt_relr_bitmap.count; i++, contents += 4)
  1528. bfd_put_32 (info->output_bfd, htab->dt_relr_bitmap.u.elf32[i],
  1529. contents);
  1530. }
  1531. /* Sort relative relocations by address. */
  1532. static int
  1533. elf_x86_relative_reloc_compare (const void *pa, const void *pb)
  1534. {
  1535. struct elf_x86_relative_reloc_record *a =
  1536. (struct elf_x86_relative_reloc_record *) pa;
  1537. struct elf_x86_relative_reloc_record *b =
  1538. (struct elf_x86_relative_reloc_record *) pb;
  1539. if (a->address < b->address)
  1540. return -1;
  1541. if (a->address > b->address)
  1542. return 1;
  1543. return 0;
  1544. }
  1545. bool
  1546. _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
  1547. bool *need_layout)
  1548. {
  1549. struct elf_x86_link_hash_table *htab;
  1550. const struct elf_backend_data *bed;
  1551. bool is_x86_64;
  1552. bfd_size_type i, count, unaligned_count;
  1553. asection *sec, *srel;
  1554. /* Do nothing for ld -r. */
  1555. if (bfd_link_relocatable (info))
  1556. return true;
  1557. bed = get_elf_backend_data (info->output_bfd);
  1558. htab = elf_x86_hash_table (info, bed->target_id);
  1559. if (htab == NULL)
  1560. return false;
  1561. count = htab->relative_reloc.count;
  1562. unaligned_count = htab->unaligned_relative_reloc.count;
  1563. if (count == 0)
  1564. {
  1565. if (htab->generate_relative_reloc_pass == 0
  1566. && htab->elf.srelrdyn != NULL)
  1567. {
  1568. /* Remove the empty .relr.dyn sections now. */
  1569. if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
  1570. {
  1571. bfd_section_list_remove
  1572. (info->output_bfd, htab->elf.srelrdyn->output_section);
  1573. info->output_bfd->section_count--;
  1574. }
  1575. bfd_section_list_remove (htab->elf.srelrdyn->owner,
  1576. htab->elf.srelrdyn);
  1577. htab->elf.srelrdyn->owner->section_count--;
  1578. }
  1579. if (unaligned_count == 0)
  1580. {
  1581. htab->generate_relative_reloc_pass++;
  1582. return true;
  1583. }
  1584. }
  1585. is_x86_64 = bed->target_id == X86_64_ELF_DATA;
  1586. /* Size relative relocations. */
  1587. if (htab->generate_relative_reloc_pass)
  1588. {
  1589. /* Reset the regular relative relocation count. */
  1590. for (i = 0; i < unaligned_count; i++)
  1591. {
  1592. sec = htab->unaligned_relative_reloc.data[i].sec;
  1593. srel = elf_section_data (sec)->sreloc;
  1594. srel->reloc_count = 0;
  1595. }
  1596. }
  1597. else
  1598. {
  1599. /* Remove the reserved space for compact relative relocations. */
  1600. if (count)
  1601. {
  1602. asection *sgot = htab->elf.sgot;
  1603. asection *srelgot = htab->elf.srelgot;
  1604. for (i = 0; i < count; i++)
  1605. {
  1606. sec = htab->relative_reloc.data[i].sec;
  1607. if (sec == sgot)
  1608. srel = srelgot;
  1609. else
  1610. srel = elf_section_data (sec)->sreloc;
  1611. srel->size -= htab->sizeof_reloc;
  1612. }
  1613. }
  1614. }
  1615. /* Size unaligned relative relocations. */
  1616. if (unaligned_count)
  1617. elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
  1618. true, NULL);
  1619. if (count)
  1620. {
  1621. elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
  1622. false, NULL);
  1623. /* Sort relative relocations by addresses. We only need to
  1624. sort them in the first pass since the relative positions
  1625. won't change. */
  1626. if (htab->generate_relative_reloc_pass == 0)
  1627. qsort (htab->relative_reloc.data, count,
  1628. sizeof (struct elf_x86_relative_reloc_record),
  1629. elf_x86_relative_reloc_compare);
  1630. elf_x86_compute_dl_relr_bitmap (info, htab, need_layout);
  1631. }
  1632. htab->generate_relative_reloc_pass++;
  1633. return true;
  1634. }
  1635. bool
  1636. _bfd_elf_x86_finish_relative_relocs (struct bfd_link_info *info)
  1637. {
  1638. struct elf_x86_link_hash_table *htab;
  1639. const struct elf_backend_data *bed;
  1640. Elf_Internal_Rela outrel;
  1641. bool is_x86_64;
  1642. bfd_size_type count;
  1643. /* Do nothing for ld -r. */
  1644. if (bfd_link_relocatable (info))
  1645. return true;
  1646. bed = get_elf_backend_data (info->output_bfd);
  1647. htab = elf_x86_hash_table (info, bed->target_id);
  1648. if (htab == NULL)
  1649. return false;
  1650. is_x86_64 = bed->target_id == X86_64_ELF_DATA;
  1651. outrel.r_info = htab->r_info (0, htab->relative_r_type);
  1652. if (htab->unaligned_relative_reloc.count)
  1653. elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
  1654. true, &outrel);
  1655. count = htab->relative_reloc.count;
  1656. if (count)
  1657. {
  1658. elf_x86_size_or_finish_relative_reloc (is_x86_64, info, htab,
  1659. false, &outrel);
  1660. elf_x86_compute_dl_relr_bitmap (info, htab, NULL);
  1661. elf_x86_write_dl_relr_bitmap (info, htab);
  1662. }
  1663. return true;
  1664. }
  1665. bool
  1666. _bfd_elf_x86_valid_reloc_p (asection *input_section,
  1667. struct bfd_link_info *info,
  1668. struct elf_x86_link_hash_table *htab,
  1669. const Elf_Internal_Rela *rel,
  1670. struct elf_link_hash_entry *h,
  1671. Elf_Internal_Sym *sym,
  1672. Elf_Internal_Shdr *symtab_hdr,
  1673. bool *no_dynreloc_p)
  1674. {
  1675. bool valid_p = true;
  1676. *no_dynreloc_p = false;
  1677. /* Check If relocation against non-preemptible absolute symbol is
  1678. valid in PIC. FIXME: Can't use SYMBOL_REFERENCES_LOCAL_P since
  1679. it may call _bfd_elf_link_hide_sym_by_version and result in
  1680. ld-elfvers/ vers21 test failure. */
  1681. if (bfd_link_pic (info)
  1682. && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)))
  1683. {
  1684. const struct elf_backend_data *bed;
  1685. unsigned int r_type;
  1686. Elf_Internal_Rela irel;
  1687. /* Skip non-absolute symbol. */
  1688. if (h)
  1689. {
  1690. if (!ABS_SYMBOL_P (h))
  1691. return valid_p;
  1692. }
  1693. else if (sym->st_shndx != SHN_ABS)
  1694. return valid_p;
  1695. bed = get_elf_backend_data (input_section->owner);
  1696. r_type = ELF32_R_TYPE (rel->r_info);
  1697. irel = *rel;
  1698. /* Only allow relocations against absolute symbol, which can be
  1699. resolved as absolute value + addend. GOTPCREL and GOT32
  1700. relocations are allowed since absolute value + addend is
  1701. stored in the GOT slot. */
  1702. if (bed->target_id == X86_64_ELF_DATA)
  1703. {
  1704. r_type &= ~R_X86_64_converted_reloc_bit;
  1705. valid_p = (r_type == R_X86_64_64
  1706. || r_type == R_X86_64_32
  1707. || r_type == R_X86_64_32S
  1708. || r_type == R_X86_64_16
  1709. || r_type == R_X86_64_8
  1710. || r_type == R_X86_64_GOTPCREL
  1711. || r_type == R_X86_64_GOTPCRELX
  1712. || r_type == R_X86_64_REX_GOTPCRELX);
  1713. if (!valid_p)
  1714. {
  1715. unsigned int r_symndx = htab->r_sym (rel->r_info);
  1716. irel.r_info = htab->r_info (r_symndx, r_type);
  1717. }
  1718. }
  1719. else
  1720. valid_p = (r_type == R_386_32
  1721. || r_type == R_386_16
  1722. || r_type == R_386_8
  1723. || r_type == R_386_GOT32
  1724. || r_type == R_386_GOT32X);
  1725. if (valid_p)
  1726. *no_dynreloc_p = true;
  1727. else
  1728. {
  1729. const char *name;
  1730. arelent internal_reloc;
  1731. if (!bed->elf_info_to_howto (input_section->owner,
  1732. &internal_reloc, &irel)
  1733. || internal_reloc.howto == NULL)
  1734. abort ();
  1735. if (h)
  1736. name = h->root.root.string;
  1737. else
  1738. name = bfd_elf_sym_name (input_section->owner, symtab_hdr,
  1739. sym, NULL);
  1740. info->callbacks->einfo
  1741. /* xgettext:c-format */
  1742. (_("%F%P: %pB: relocation %s against absolute symbol "
  1743. "`%s' in section `%pA' is disallowed\n"),
  1744. input_section->owner, internal_reloc.howto->name, name,
  1745. input_section);
  1746. bfd_set_error (bfd_error_bad_value);
  1747. }
  1748. }
  1749. return valid_p;
  1750. }
  1751. /* Set the sizes of the dynamic sections. */
  1752. bool
  1753. _bfd_x86_elf_size_dynamic_sections (bfd *output_bfd,
  1754. struct bfd_link_info *info)
  1755. {
  1756. struct elf_x86_link_hash_table *htab;
  1757. bfd *dynobj;
  1758. asection *s;
  1759. bool relocs;
  1760. bfd *ibfd;
  1761. const struct elf_backend_data *bed
  1762. = get_elf_backend_data (output_bfd);
  1763. htab = elf_x86_hash_table (info, bed->target_id);
  1764. if (htab == NULL)
  1765. return false;
  1766. dynobj = htab->elf.dynobj;
  1767. if (dynobj == NULL)
  1768. abort ();
  1769. /* Set up .got offsets for local syms, and space for local dynamic
  1770. relocs. */
  1771. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1772. {
  1773. bfd_signed_vma *local_got;
  1774. bfd_signed_vma *end_local_got;
  1775. char *local_tls_type;
  1776. bfd_vma *local_tlsdesc_gotent;
  1777. bfd_size_type locsymcount;
  1778. Elf_Internal_Shdr *symtab_hdr;
  1779. asection *srel;
  1780. if (! is_x86_elf (ibfd, htab))
  1781. continue;
  1782. for (s = ibfd->sections; s != NULL; s = s->next)
  1783. {
  1784. struct elf_dyn_relocs *p;
  1785. for (p = ((struct elf_dyn_relocs *)
  1786. elf_section_data (s)->local_dynrel);
  1787. p != NULL;
  1788. p = p->next)
  1789. {
  1790. if (!bfd_is_abs_section (p->sec)
  1791. && bfd_is_abs_section (p->sec->output_section))
  1792. {
  1793. /* Input section has been discarded, either because
  1794. it is a copy of a linkonce section or due to
  1795. linker script /DISCARD/, so we'll be discarding
  1796. the relocs too. */
  1797. }
  1798. else if (htab->elf.target_os == is_vxworks
  1799. && strcmp (p->sec->output_section->name,
  1800. ".tls_vars") == 0)
  1801. {
  1802. /* Relocations in vxworks .tls_vars sections are
  1803. handled specially by the loader. */
  1804. }
  1805. else if (p->count != 0)
  1806. {
  1807. srel = elf_section_data (p->sec)->sreloc;
  1808. srel->size += p->count * htab->sizeof_reloc;
  1809. if ((p->sec->output_section->flags & SEC_READONLY) != 0
  1810. && (info->flags & DF_TEXTREL) == 0)
  1811. {
  1812. info->flags |= DF_TEXTREL;
  1813. if (bfd_link_textrel_check (info))
  1814. /* xgettext:c-format */
  1815. info->callbacks->einfo
  1816. (_("%P: %pB: warning: relocation "
  1817. "in read-only section `%pA'\n"),
  1818. p->sec->owner, p->sec);
  1819. }
  1820. }
  1821. }
  1822. }
  1823. local_got = elf_local_got_refcounts (ibfd);
  1824. if (!local_got)
  1825. continue;
  1826. symtab_hdr = &elf_symtab_hdr (ibfd);
  1827. locsymcount = symtab_hdr->sh_info;
  1828. end_local_got = local_got + locsymcount;
  1829. local_tls_type = elf_x86_local_got_tls_type (ibfd);
  1830. local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
  1831. s = htab->elf.sgot;
  1832. srel = htab->elf.srelgot;
  1833. for (; local_got < end_local_got;
  1834. ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
  1835. {
  1836. *local_tlsdesc_gotent = (bfd_vma) -1;
  1837. if (*local_got > 0)
  1838. {
  1839. if (GOT_TLS_GDESC_P (*local_tls_type))
  1840. {
  1841. *local_tlsdesc_gotent = htab->elf.sgotplt->size
  1842. - elf_x86_compute_jump_table_size (htab);
  1843. htab->elf.sgotplt->size += 2 * htab->got_entry_size;
  1844. *local_got = (bfd_vma) -2;
  1845. }
  1846. if (! GOT_TLS_GDESC_P (*local_tls_type)
  1847. || GOT_TLS_GD_P (*local_tls_type))
  1848. {
  1849. *local_got = s->size;
  1850. s->size += htab->got_entry_size;
  1851. if (GOT_TLS_GD_P (*local_tls_type)
  1852. || *local_tls_type == GOT_TLS_IE_BOTH)
  1853. s->size += htab->got_entry_size;
  1854. }
  1855. if ((bfd_link_pic (info) && *local_tls_type != GOT_ABS)
  1856. || GOT_TLS_GD_ANY_P (*local_tls_type)
  1857. || (*local_tls_type & GOT_TLS_IE))
  1858. {
  1859. if (*local_tls_type == GOT_TLS_IE_BOTH)
  1860. srel->size += 2 * htab->sizeof_reloc;
  1861. else if (GOT_TLS_GD_P (*local_tls_type)
  1862. || ! GOT_TLS_GDESC_P (*local_tls_type))
  1863. srel->size += htab->sizeof_reloc;
  1864. if (GOT_TLS_GDESC_P (*local_tls_type))
  1865. {
  1866. htab->elf.srelplt->size += htab->sizeof_reloc;
  1867. if (bed->target_id == X86_64_ELF_DATA)
  1868. htab->elf.tlsdesc_plt = (bfd_vma) -1;
  1869. }
  1870. }
  1871. }
  1872. else
  1873. *local_got = (bfd_vma) -1;
  1874. }
  1875. }
  1876. if (htab->tls_ld_or_ldm_got.refcount > 0)
  1877. {
  1878. /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM
  1879. or R_X86_64_TLSLD relocs. */
  1880. htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
  1881. htab->elf.sgot->size += 2 * htab->got_entry_size;
  1882. htab->elf.srelgot->size += htab->sizeof_reloc;
  1883. }
  1884. else
  1885. htab->tls_ld_or_ldm_got.offset = -1;
  1886. /* Allocate global sym .plt and .got entries, and space for global
  1887. sym dynamic relocs. */
  1888. elf_link_hash_traverse (&htab->elf, elf_x86_allocate_dynrelocs,
  1889. info);
  1890. /* Allocate .plt and .got entries, and space for local symbols. */
  1891. htab_traverse (htab->loc_hash_table, elf_x86_allocate_local_dynreloc,
  1892. info);
  1893. /* For every jump slot reserved in the sgotplt, reloc_count is
  1894. incremented. However, when we reserve space for TLS descriptors,
  1895. it's not incremented, so in order to compute the space reserved
  1896. for them, it suffices to multiply the reloc count by the jump
  1897. slot size.
  1898. PR ld/13302: We start next_irelative_index at the end of .rela.plt
  1899. so that R_{386,X86_64}_IRELATIVE entries come last. */
  1900. if (htab->elf.srelplt)
  1901. {
  1902. htab->next_tls_desc_index = htab->elf.srelplt->reloc_count;
  1903. htab->sgotplt_jump_table_size
  1904. = elf_x86_compute_jump_table_size (htab);
  1905. htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
  1906. }
  1907. else if (htab->elf.irelplt)
  1908. htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
  1909. if (htab->elf.tlsdesc_plt)
  1910. {
  1911. /* NB: tlsdesc_plt is set only for x86-64. If we're not using
  1912. lazy TLS relocations, don't generate the PLT and GOT entries
  1913. they require. */
  1914. if ((info->flags & DF_BIND_NOW))
  1915. htab->elf.tlsdesc_plt = 0;
  1916. else
  1917. {
  1918. htab->elf.tlsdesc_got = htab->elf.sgot->size;
  1919. htab->elf.sgot->size += htab->got_entry_size;
  1920. /* Reserve room for the initial entry.
  1921. FIXME: we could probably do away with it in this case. */
  1922. if (htab->elf.splt->size == 0)
  1923. htab->elf.splt->size = htab->plt.plt_entry_size;
  1924. htab->elf.tlsdesc_plt = htab->elf.splt->size;
  1925. htab->elf.splt->size += htab->plt.plt_entry_size;
  1926. }
  1927. }
  1928. if (htab->elf.sgotplt)
  1929. {
  1930. /* Don't allocate .got.plt section if there are no GOT nor PLT
  1931. entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */
  1932. if ((htab->elf.hgot == NULL
  1933. || !htab->got_referenced)
  1934. && (htab->elf.sgotplt->size == bed->got_header_size)
  1935. && (htab->elf.splt == NULL
  1936. || htab->elf.splt->size == 0)
  1937. && (htab->elf.sgot == NULL
  1938. || htab->elf.sgot->size == 0)
  1939. && (htab->elf.iplt == NULL
  1940. || htab->elf.iplt->size == 0)
  1941. && (htab->elf.igotplt == NULL
  1942. || htab->elf.igotplt->size == 0))
  1943. {
  1944. htab->elf.sgotplt->size = 0;
  1945. /* Solaris requires to keep _GLOBAL_OFFSET_TABLE_ even if it
  1946. isn't used. */
  1947. if (htab->elf.hgot != NULL
  1948. && htab->elf.target_os != is_solaris)
  1949. {
  1950. /* Remove the unused _GLOBAL_OFFSET_TABLE_ from symbol
  1951. table. */
  1952. htab->elf.hgot->root.type = bfd_link_hash_undefined;
  1953. htab->elf.hgot->root.u.undef.abfd
  1954. = htab->elf.hgot->root.u.def.section->owner;
  1955. htab->elf.hgot->root.linker_def = 0;
  1956. htab->elf.hgot->ref_regular = 0;
  1957. htab->elf.hgot->def_regular = 0;
  1958. }
  1959. }
  1960. }
  1961. if (_bfd_elf_eh_frame_present (info))
  1962. {
  1963. if (htab->plt_eh_frame != NULL
  1964. && htab->elf.splt != NULL
  1965. && htab->elf.splt->size != 0
  1966. && !bfd_is_abs_section (htab->elf.splt->output_section))
  1967. htab->plt_eh_frame->size = htab->plt.eh_frame_plt_size;
  1968. if (htab->plt_got_eh_frame != NULL
  1969. && htab->plt_got != NULL
  1970. && htab->plt_got->size != 0
  1971. && !bfd_is_abs_section (htab->plt_got->output_section))
  1972. htab->plt_got_eh_frame->size
  1973. = htab->non_lazy_plt->eh_frame_plt_size;
  1974. /* Unwind info for the second PLT and .plt.got sections are
  1975. identical. */
  1976. if (htab->plt_second_eh_frame != NULL
  1977. && htab->plt_second != NULL
  1978. && htab->plt_second->size != 0
  1979. && !bfd_is_abs_section (htab->plt_second->output_section))
  1980. htab->plt_second_eh_frame->size
  1981. = htab->non_lazy_plt->eh_frame_plt_size;
  1982. }
  1983. /* We now have determined the sizes of the various dynamic sections.
  1984. Allocate memory for them. */
  1985. relocs = false;
  1986. for (s = dynobj->sections; s != NULL; s = s->next)
  1987. {
  1988. bool strip_section = true;
  1989. if ((s->flags & SEC_LINKER_CREATED) == 0)
  1990. continue;
  1991. /* The .relr.dyn section for compact relative relocation will
  1992. be filled later. */
  1993. if (s == htab->elf.srelrdyn)
  1994. continue;
  1995. if (s == htab->elf.splt
  1996. || s == htab->elf.sgot)
  1997. {
  1998. /* Strip this section if we don't need it; see the
  1999. comment below. */
  2000. /* We'd like to strip these sections if they aren't needed, but if
  2001. we've exported dynamic symbols from them we must leave them.
  2002. It's too late to tell BFD to get rid of the symbols. */
  2003. if (htab->elf.hplt != NULL)
  2004. strip_section = false;
  2005. }
  2006. else if (s == htab->elf.sgotplt
  2007. || s == htab->elf.iplt
  2008. || s == htab->elf.igotplt
  2009. || s == htab->plt_second
  2010. || s == htab->plt_got
  2011. || s == htab->plt_eh_frame
  2012. || s == htab->plt_got_eh_frame
  2013. || s == htab->plt_second_eh_frame
  2014. || s == htab->elf.sdynbss
  2015. || s == htab->elf.sdynrelro)
  2016. {
  2017. /* Strip these too. */
  2018. }
  2019. else if (htab->is_reloc_section (bfd_section_name (s)))
  2020. {
  2021. if (s->size != 0
  2022. && s != htab->elf.srelplt
  2023. && s != htab->srelplt2)
  2024. relocs = true;
  2025. /* We use the reloc_count field as a counter if we need
  2026. to copy relocs into the output file. */
  2027. if (s != htab->elf.srelplt)
  2028. s->reloc_count = 0;
  2029. }
  2030. else
  2031. {
  2032. /* It's not one of our sections, so don't allocate space. */
  2033. continue;
  2034. }
  2035. if (s->size == 0)
  2036. {
  2037. /* If we don't need this section, strip it from the
  2038. output file. This is mostly to handle .rel.bss and
  2039. .rel.plt. We must create both sections in
  2040. create_dynamic_sections, because they must be created
  2041. before the linker maps input sections to output
  2042. sections. The linker does that before
  2043. adjust_dynamic_symbol is called, and it is that
  2044. function which decides whether anything needs to go
  2045. into these sections. */
  2046. if (strip_section)
  2047. s->flags |= SEC_EXCLUDE;
  2048. continue;
  2049. }
  2050. if ((s->flags & SEC_HAS_CONTENTS) == 0)
  2051. continue;
  2052. /* NB: Initially, the iplt section has minimal alignment to
  2053. avoid moving dot of the following section backwards when
  2054. it is empty. Update its section alignment now since it
  2055. is non-empty. */
  2056. if (s == htab->elf.iplt)
  2057. bfd_set_section_alignment (s, htab->plt.iplt_alignment);
  2058. /* Allocate memory for the section contents. We use bfd_zalloc
  2059. here in case unused entries are not reclaimed before the
  2060. section's contents are written out. This should not happen,
  2061. but this way if it does, we get a R_386_NONE or R_X86_64_NONE
  2062. reloc instead of garbage. */
  2063. s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size);
  2064. if (s->contents == NULL)
  2065. return false;
  2066. }
  2067. if (htab->plt_eh_frame != NULL
  2068. && htab->plt_eh_frame->contents != NULL)
  2069. {
  2070. memcpy (htab->plt_eh_frame->contents,
  2071. htab->plt.eh_frame_plt,
  2072. htab->plt_eh_frame->size);
  2073. bfd_put_32 (dynobj, htab->elf.splt->size,
  2074. htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
  2075. }
  2076. if (htab->plt_got_eh_frame != NULL
  2077. && htab->plt_got_eh_frame->contents != NULL)
  2078. {
  2079. memcpy (htab->plt_got_eh_frame->contents,
  2080. htab->non_lazy_plt->eh_frame_plt,
  2081. htab->plt_got_eh_frame->size);
  2082. bfd_put_32 (dynobj, htab->plt_got->size,
  2083. (htab->plt_got_eh_frame->contents
  2084. + PLT_FDE_LEN_OFFSET));
  2085. }
  2086. if (htab->plt_second_eh_frame != NULL
  2087. && htab->plt_second_eh_frame->contents != NULL)
  2088. {
  2089. memcpy (htab->plt_second_eh_frame->contents,
  2090. htab->non_lazy_plt->eh_frame_plt,
  2091. htab->plt_second_eh_frame->size);
  2092. bfd_put_32 (dynobj, htab->plt_second->size,
  2093. (htab->plt_second_eh_frame->contents
  2094. + PLT_FDE_LEN_OFFSET));
  2095. }
  2096. return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
  2097. relocs);
  2098. }
  2099. /* Finish up the x86 dynamic sections. */
  2100. struct elf_x86_link_hash_table *
  2101. _bfd_x86_elf_finish_dynamic_sections (bfd *output_bfd,
  2102. struct bfd_link_info *info)
  2103. {
  2104. struct elf_x86_link_hash_table *htab;
  2105. const struct elf_backend_data *bed;
  2106. bfd *dynobj;
  2107. asection *sdyn;
  2108. bfd_byte *dyncon, *dynconend;
  2109. bfd_size_type sizeof_dyn;
  2110. bed = get_elf_backend_data (output_bfd);
  2111. htab = elf_x86_hash_table (info, bed->target_id);
  2112. if (htab == NULL)
  2113. return htab;
  2114. dynobj = htab->elf.dynobj;
  2115. sdyn = bfd_get_linker_section (dynobj, ".dynamic");
  2116. /* GOT is always created in setup_gnu_properties. But it may not be
  2117. needed. .got.plt section may be needed for static IFUNC. */
  2118. if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
  2119. {
  2120. bfd_vma dynamic_addr;
  2121. if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
  2122. {
  2123. _bfd_error_handler
  2124. (_("discarded output section: `%pA'"), htab->elf.sgotplt);
  2125. return NULL;
  2126. }
  2127. elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
  2128. = htab->got_entry_size;
  2129. dynamic_addr = (sdyn == NULL
  2130. ? (bfd_vma) 0
  2131. : sdyn->output_section->vma + sdyn->output_offset);
  2132. /* Set the first entry in the global offset table to the address
  2133. of the dynamic section. Write GOT[1] and GOT[2], needed for
  2134. the dynamic linker. */
  2135. if (htab->got_entry_size == 8)
  2136. {
  2137. bfd_put_64 (output_bfd, dynamic_addr,
  2138. htab->elf.sgotplt->contents);
  2139. bfd_put_64 (output_bfd, (bfd_vma) 0,
  2140. htab->elf.sgotplt->contents + 8);
  2141. bfd_put_64 (output_bfd, (bfd_vma) 0,
  2142. htab->elf.sgotplt->contents + 8*2);
  2143. }
  2144. else
  2145. {
  2146. bfd_put_32 (output_bfd, dynamic_addr,
  2147. htab->elf.sgotplt->contents);
  2148. bfd_put_32 (output_bfd, 0,
  2149. htab->elf.sgotplt->contents + 4);
  2150. bfd_put_32 (output_bfd, 0,
  2151. htab->elf.sgotplt->contents + 4*2);
  2152. }
  2153. }
  2154. if (!htab->elf.dynamic_sections_created)
  2155. return htab;
  2156. if (sdyn == NULL || htab->elf.sgot == NULL)
  2157. abort ();
  2158. sizeof_dyn = bed->s->sizeof_dyn;
  2159. dyncon = sdyn->contents;
  2160. dynconend = sdyn->contents + sdyn->size;
  2161. for (; dyncon < dynconend; dyncon += sizeof_dyn)
  2162. {
  2163. Elf_Internal_Dyn dyn;
  2164. asection *s;
  2165. (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
  2166. switch (dyn.d_tag)
  2167. {
  2168. default:
  2169. if (htab->elf.target_os == is_vxworks
  2170. && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
  2171. break;
  2172. continue;
  2173. case DT_PLTGOT:
  2174. s = htab->elf.sgotplt;
  2175. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
  2176. break;
  2177. case DT_JMPREL:
  2178. dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
  2179. break;
  2180. case DT_PLTRELSZ:
  2181. s = htab->elf.srelplt->output_section;
  2182. dyn.d_un.d_val = s->size;
  2183. break;
  2184. case DT_TLSDESC_PLT:
  2185. s = htab->elf.splt;
  2186. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
  2187. + htab->elf.tlsdesc_plt;
  2188. break;
  2189. case DT_TLSDESC_GOT:
  2190. s = htab->elf.sgot;
  2191. dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
  2192. + htab->elf.tlsdesc_got;
  2193. break;
  2194. }
  2195. (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
  2196. }
  2197. if (htab->plt_got != NULL && htab->plt_got->size > 0)
  2198. elf_section_data (htab->plt_got->output_section)
  2199. ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
  2200. if (htab->plt_second != NULL && htab->plt_second->size > 0)
  2201. elf_section_data (htab->plt_second->output_section)
  2202. ->this_hdr.sh_entsize = htab->non_lazy_plt->plt_entry_size;
  2203. /* Adjust .eh_frame for .plt section. */
  2204. if (htab->plt_eh_frame != NULL
  2205. && htab->plt_eh_frame->contents != NULL)
  2206. {
  2207. if (htab->elf.splt != NULL
  2208. && htab->elf.splt->size != 0
  2209. && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
  2210. && htab->elf.splt->output_section != NULL
  2211. && htab->plt_eh_frame->output_section != NULL)
  2212. {
  2213. bfd_vma plt_start = htab->elf.splt->output_section->vma;
  2214. bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
  2215. + htab->plt_eh_frame->output_offset
  2216. + PLT_FDE_START_OFFSET;
  2217. bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
  2218. htab->plt_eh_frame->contents
  2219. + PLT_FDE_START_OFFSET);
  2220. }
  2221. if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
  2222. {
  2223. if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
  2224. htab->plt_eh_frame,
  2225. htab->plt_eh_frame->contents))
  2226. return NULL;
  2227. }
  2228. }
  2229. /* Adjust .eh_frame for .plt.got section. */
  2230. if (htab->plt_got_eh_frame != NULL
  2231. && htab->plt_got_eh_frame->contents != NULL)
  2232. {
  2233. if (htab->plt_got != NULL
  2234. && htab->plt_got->size != 0
  2235. && (htab->plt_got->flags & SEC_EXCLUDE) == 0
  2236. && htab->plt_got->output_section != NULL
  2237. && htab->plt_got_eh_frame->output_section != NULL)
  2238. {
  2239. bfd_vma plt_start = htab->plt_got->output_section->vma;
  2240. bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
  2241. + htab->plt_got_eh_frame->output_offset
  2242. + PLT_FDE_START_OFFSET;
  2243. bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
  2244. htab->plt_got_eh_frame->contents
  2245. + PLT_FDE_START_OFFSET);
  2246. }
  2247. if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
  2248. {
  2249. if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
  2250. htab->plt_got_eh_frame,
  2251. htab->plt_got_eh_frame->contents))
  2252. return NULL;
  2253. }
  2254. }
  2255. /* Adjust .eh_frame for the second PLT section. */
  2256. if (htab->plt_second_eh_frame != NULL
  2257. && htab->plt_second_eh_frame->contents != NULL)
  2258. {
  2259. if (htab->plt_second != NULL
  2260. && htab->plt_second->size != 0
  2261. && (htab->plt_second->flags & SEC_EXCLUDE) == 0
  2262. && htab->plt_second->output_section != NULL
  2263. && htab->plt_second_eh_frame->output_section != NULL)
  2264. {
  2265. bfd_vma plt_start = htab->plt_second->output_section->vma;
  2266. bfd_vma eh_frame_start
  2267. = (htab->plt_second_eh_frame->output_section->vma
  2268. + htab->plt_second_eh_frame->output_offset
  2269. + PLT_FDE_START_OFFSET);
  2270. bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
  2271. htab->plt_second_eh_frame->contents
  2272. + PLT_FDE_START_OFFSET);
  2273. }
  2274. if (htab->plt_second_eh_frame->sec_info_type
  2275. == SEC_INFO_TYPE_EH_FRAME)
  2276. {
  2277. if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
  2278. htab->plt_second_eh_frame,
  2279. htab->plt_second_eh_frame->contents))
  2280. return NULL;
  2281. }
  2282. }
  2283. if (htab->elf.sgot && htab->elf.sgot->size > 0)
  2284. elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
  2285. = htab->got_entry_size;
  2286. return htab;
  2287. }
  2288. bool
  2289. _bfd_x86_elf_always_size_sections (bfd *output_bfd,
  2290. struct bfd_link_info *info)
  2291. {
  2292. asection *tls_sec = elf_hash_table (info)->tls_sec;
  2293. if (tls_sec)
  2294. {
  2295. struct elf_link_hash_entry *tlsbase;
  2296. tlsbase = elf_link_hash_lookup (elf_hash_table (info),
  2297. "_TLS_MODULE_BASE_",
  2298. false, false, false);
  2299. if (tlsbase && tlsbase->type == STT_TLS)
  2300. {
  2301. struct elf_x86_link_hash_table *htab;
  2302. struct bfd_link_hash_entry *bh = NULL;
  2303. const struct elf_backend_data *bed
  2304. = get_elf_backend_data (output_bfd);
  2305. htab = elf_x86_hash_table (info, bed->target_id);
  2306. if (htab == NULL)
  2307. return false;
  2308. if (!(_bfd_generic_link_add_one_symbol
  2309. (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
  2310. tls_sec, 0, NULL, false,
  2311. bed->collect, &bh)))
  2312. return false;
  2313. htab->tls_module_base = bh;
  2314. tlsbase = (struct elf_link_hash_entry *)bh;
  2315. tlsbase->def_regular = 1;
  2316. tlsbase->other = STV_HIDDEN;
  2317. tlsbase->root.linker_def = 1;
  2318. (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
  2319. }
  2320. }
  2321. return true;
  2322. }
  2323. void
  2324. _bfd_x86_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
  2325. unsigned int st_other,
  2326. bool definition,
  2327. bool dynamic ATTRIBUTE_UNUSED)
  2328. {
  2329. if (definition)
  2330. {
  2331. struct elf_x86_link_hash_entry *eh
  2332. = (struct elf_x86_link_hash_entry *) h;
  2333. eh->def_protected = ELF_ST_VISIBILITY (st_other) == STV_PROTECTED;
  2334. }
  2335. }
  2336. /* Copy the extra info we tack onto an elf_link_hash_entry. */
  2337. void
  2338. _bfd_x86_elf_copy_indirect_symbol (struct bfd_link_info *info,
  2339. struct elf_link_hash_entry *dir,
  2340. struct elf_link_hash_entry *ind)
  2341. {
  2342. struct elf_x86_link_hash_entry *edir, *eind;
  2343. edir = (struct elf_x86_link_hash_entry *) dir;
  2344. eind = (struct elf_x86_link_hash_entry *) ind;
  2345. if (ind->root.type == bfd_link_hash_indirect
  2346. && dir->got.refcount <= 0)
  2347. {
  2348. edir->tls_type = eind->tls_type;
  2349. eind->tls_type = GOT_UNKNOWN;
  2350. }
  2351. /* Copy gotoff_ref so that elf_i386_adjust_dynamic_symbol will
  2352. generate a R_386_COPY reloc. */
  2353. edir->gotoff_ref |= eind->gotoff_ref;
  2354. edir->zero_undefweak |= eind->zero_undefweak;
  2355. if (ELIMINATE_COPY_RELOCS
  2356. && ind->root.type != bfd_link_hash_indirect
  2357. && dir->dynamic_adjusted)
  2358. {
  2359. /* If called to transfer flags for a weakdef during processing
  2360. of elf_adjust_dynamic_symbol, don't copy non_got_ref.
  2361. We clear it ourselves for ELIMINATE_COPY_RELOCS. */
  2362. if (dir->versioned != versioned_hidden)
  2363. dir->ref_dynamic |= ind->ref_dynamic;
  2364. dir->ref_regular |= ind->ref_regular;
  2365. dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
  2366. dir->needs_plt |= ind->needs_plt;
  2367. dir->pointer_equality_needed |= ind->pointer_equality_needed;
  2368. }
  2369. else
  2370. _bfd_elf_link_hash_copy_indirect (info, dir, ind);
  2371. }
  2372. /* Remove undefined weak symbol from the dynamic symbol table if it
  2373. is resolved to 0. */
  2374. bool
  2375. _bfd_x86_elf_fixup_symbol (struct bfd_link_info *info,
  2376. struct elf_link_hash_entry *h)
  2377. {
  2378. if (h->dynindx != -1
  2379. && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, elf_x86_hash_entry (h)))
  2380. {
  2381. h->dynindx = -1;
  2382. _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
  2383. h->dynstr_index);
  2384. }
  2385. return true;
  2386. }
  2387. /* Change the STT_GNU_IFUNC symbol defined in position-dependent
  2388. executable into the normal function symbol and set its address
  2389. to its PLT entry, which should be resolved by R_*_IRELATIVE at
  2390. run-time. */
  2391. void
  2392. _bfd_x86_elf_link_fixup_ifunc_symbol (struct bfd_link_info *info,
  2393. struct elf_x86_link_hash_table *htab,
  2394. struct elf_link_hash_entry *h,
  2395. Elf_Internal_Sym *sym)
  2396. {
  2397. if (bfd_link_pde (info)
  2398. && h->def_regular
  2399. && h->dynindx != -1
  2400. && h->plt.offset != (bfd_vma) -1
  2401. && h->type == STT_GNU_IFUNC)
  2402. {
  2403. asection *plt_s;
  2404. bfd_vma plt_offset;
  2405. bfd *output_bfd = info->output_bfd;
  2406. if (htab->plt_second)
  2407. {
  2408. struct elf_x86_link_hash_entry *eh
  2409. = (struct elf_x86_link_hash_entry *) h;
  2410. plt_s = htab->plt_second;
  2411. plt_offset = eh->plt_second.offset;
  2412. }
  2413. else
  2414. {
  2415. plt_s = htab->elf.splt;
  2416. plt_offset = h->plt.offset;
  2417. }
  2418. sym->st_size = 0;
  2419. sym->st_info = ELF_ST_INFO (ELF_ST_BIND (sym->st_info), STT_FUNC);
  2420. sym->st_shndx
  2421. = _bfd_elf_section_from_bfd_section (output_bfd,
  2422. plt_s->output_section);
  2423. sym->st_value = (plt_s->output_section->vma
  2424. + plt_s->output_offset + plt_offset);
  2425. }
  2426. }
  2427. /* Report relative relocation. */
  2428. void
  2429. _bfd_x86_elf_link_report_relative_reloc
  2430. (struct bfd_link_info *info, asection *asect,
  2431. struct elf_link_hash_entry *h, Elf_Internal_Sym *sym,
  2432. const char *reloc_name, const void *reloc)
  2433. {
  2434. const char *name;
  2435. bfd *abfd;
  2436. const Elf_Internal_Rela *rel = (const Elf_Internal_Rela *) reloc;
  2437. /* Use the output BFD for linker created sections. */
  2438. if ((asect->flags & SEC_LINKER_CREATED) != 0)
  2439. abfd = info->output_bfd;
  2440. else
  2441. abfd = asect->owner;
  2442. if (h != NULL && h->root.root.string != NULL)
  2443. name = h->root.root.string;
  2444. else
  2445. name = bfd_elf_sym_name (abfd, &elf_symtab_hdr (abfd), sym, NULL);
  2446. if (asect->use_rela_p)
  2447. info->callbacks->einfo
  2448. (_("%pB: %s (offset: 0x%v, info: 0x%v, addend: 0x%v) against "
  2449. "'%s' " "for section '%pA' in %pB\n"),
  2450. info->output_bfd, reloc_name, rel->r_offset, rel->r_info,
  2451. rel->r_addend, name, asect, abfd);
  2452. else
  2453. info->callbacks->einfo
  2454. (_("%pB: %s (offset: 0x%v, info: 0x%v) against '%s' for section "
  2455. "'%pA' in %pB\n"),
  2456. info->output_bfd, reloc_name, rel->r_offset, rel->r_info, name,
  2457. asect, abfd);
  2458. }
  2459. /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
  2460. bool
  2461. _bfd_x86_elf_hash_symbol (struct elf_link_hash_entry *h)
  2462. {
  2463. if (h->plt.offset != (bfd_vma) -1
  2464. && !h->def_regular
  2465. && !h->pointer_equality_needed)
  2466. return false;
  2467. return _bfd_elf_hash_symbol (h);
  2468. }
  2469. /* Adjust a symbol defined by a dynamic object and referenced by a
  2470. regular object. The current definition is in some section of the
  2471. dynamic object, but we're not including those sections. We have to
  2472. change the definition to something the rest of the link can
  2473. understand. */
  2474. bool
  2475. _bfd_x86_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
  2476. struct elf_link_hash_entry *h)
  2477. {
  2478. struct elf_x86_link_hash_table *htab;
  2479. asection *s, *srel;
  2480. struct elf_x86_link_hash_entry *eh;
  2481. struct elf_dyn_relocs *p;
  2482. const struct elf_backend_data *bed
  2483. = get_elf_backend_data (info->output_bfd);
  2484. eh = (struct elf_x86_link_hash_entry *) h;
  2485. /* Clear GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS if it is turned
  2486. on by an input relocatable file and there is a non-GOT/non-PLT
  2487. reference from another relocatable file without it.
  2488. NB: There can be non-GOT reference in data sections in input with
  2489. GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS. */
  2490. if (eh->non_got_ref_without_indirect_extern_access
  2491. && info->indirect_extern_access == 1
  2492. && bfd_link_executable (info))
  2493. {
  2494. unsigned int needed_1;
  2495. info->indirect_extern_access = 0;
  2496. /* Turn off nocopyreloc if implied by indirect_extern_access. */
  2497. if (info->nocopyreloc == 2)
  2498. info->nocopyreloc = 0;
  2499. needed_1 = bfd_h_get_32 (info->output_bfd, info->needed_1_p);
  2500. needed_1 &= ~GNU_PROPERTY_1_NEEDED_INDIRECT_EXTERN_ACCESS;
  2501. bfd_h_put_32 (info->output_bfd, needed_1, info->needed_1_p);
  2502. }
  2503. /* STT_GNU_IFUNC symbol must go through PLT. */
  2504. if (h->type == STT_GNU_IFUNC)
  2505. {
  2506. /* All local STT_GNU_IFUNC references must be treate as local
  2507. calls via local PLT. */
  2508. if (h->ref_regular
  2509. && SYMBOL_CALLS_LOCAL (info, h))
  2510. {
  2511. bfd_size_type pc_count = 0, count = 0;
  2512. struct elf_dyn_relocs **pp;
  2513. eh = (struct elf_x86_link_hash_entry *) h;
  2514. for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
  2515. {
  2516. pc_count += p->pc_count;
  2517. p->count -= p->pc_count;
  2518. p->pc_count = 0;
  2519. count += p->count;
  2520. if (p->count == 0)
  2521. *pp = p->next;
  2522. else
  2523. pp = &p->next;
  2524. }
  2525. if (pc_count || count)
  2526. {
  2527. h->non_got_ref = 1;
  2528. if (pc_count)
  2529. {
  2530. /* Increment PLT reference count only for PC-relative
  2531. references. */
  2532. h->needs_plt = 1;
  2533. if (h->plt.refcount <= 0)
  2534. h->plt.refcount = 1;
  2535. else
  2536. h->plt.refcount += 1;
  2537. }
  2538. }
  2539. /* GOTOFF relocation needs PLT. */
  2540. if (eh->gotoff_ref)
  2541. h->plt.refcount = 1;
  2542. }
  2543. if (h->plt.refcount <= 0)
  2544. {
  2545. h->plt.offset = (bfd_vma) -1;
  2546. h->needs_plt = 0;
  2547. }
  2548. return true;
  2549. }
  2550. /* If this is a function, put it in the procedure linkage table. We
  2551. will fill in the contents of the procedure linkage table later,
  2552. when we know the address of the .got section. */
  2553. if (h->type == STT_FUNC
  2554. || h->needs_plt)
  2555. {
  2556. if (h->plt.refcount <= 0
  2557. || SYMBOL_CALLS_LOCAL (info, h)
  2558. || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2559. && h->root.type == bfd_link_hash_undefweak))
  2560. {
  2561. /* This case can occur if we saw a PLT32 reloc in an input
  2562. file, but the symbol was never referred to by a dynamic
  2563. object, or if all references were garbage collected. In
  2564. such a case, we don't actually need to build a procedure
  2565. linkage table, and we can just do a PC32 reloc instead. */
  2566. h->plt.offset = (bfd_vma) -1;
  2567. h->needs_plt = 0;
  2568. }
  2569. return true;
  2570. }
  2571. else
  2572. /* It's possible that we incorrectly decided a .plt reloc was needed
  2573. * for an R_386_PC32/R_X86_64_PC32 reloc to a non-function sym in
  2574. check_relocs. We can't decide accurately between function and
  2575. non-function syms in check-relocs; Objects loaded later in
  2576. the link may change h->type. So fix it now. */
  2577. h->plt.offset = (bfd_vma) -1;
  2578. /* If this is a weak symbol, and there is a real definition, the
  2579. processor independent code will have arranged for us to see the
  2580. real definition first, and we can just use the same value. */
  2581. if (h->is_weakalias)
  2582. {
  2583. struct elf_link_hash_entry *def = weakdef (h);
  2584. BFD_ASSERT (def->root.type == bfd_link_hash_defined);
  2585. h->root.u.def.section = def->root.u.def.section;
  2586. h->root.u.def.value = def->root.u.def.value;
  2587. if (ELIMINATE_COPY_RELOCS
  2588. || info->nocopyreloc
  2589. || SYMBOL_NO_COPYRELOC (info, eh))
  2590. {
  2591. /* NB: needs_copy is always 0 for i386. */
  2592. h->non_got_ref = def->non_got_ref;
  2593. eh->needs_copy = def->needs_copy;
  2594. }
  2595. return true;
  2596. }
  2597. /* This is a reference to a symbol defined by a dynamic object which
  2598. is not a function. */
  2599. /* If we are creating a shared library, we must presume that the
  2600. only references to the symbol are via the global offset table.
  2601. For such cases we need not do anything here; the relocations will
  2602. be handled correctly by relocate_section. */
  2603. if (!bfd_link_executable (info))
  2604. return true;
  2605. /* If there are no references to this symbol that do not use the
  2606. GOT nor R_386_GOTOFF relocation, we don't need to generate a copy
  2607. reloc. NB: gotoff_ref is always 0 for x86-64. */
  2608. if (!h->non_got_ref && !eh->gotoff_ref)
  2609. return true;
  2610. /* If -z nocopyreloc was given, we won't generate them either. */
  2611. if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
  2612. {
  2613. h->non_got_ref = 0;
  2614. return true;
  2615. }
  2616. htab = elf_x86_hash_table (info, bed->target_id);
  2617. if (htab == NULL)
  2618. return false;
  2619. /* If there aren't any dynamic relocs in read-only sections nor
  2620. R_386_GOTOFF relocation, then we can keep the dynamic relocs and
  2621. avoid the copy reloc. This doesn't work on VxWorks, where we can
  2622. not have dynamic relocations (other than copy and jump slot
  2623. relocations) in an executable. */
  2624. if (ELIMINATE_COPY_RELOCS
  2625. && (bed->target_id == X86_64_ELF_DATA
  2626. || (!eh->gotoff_ref
  2627. && htab->elf.target_os != is_vxworks)))
  2628. {
  2629. /* If we don't find any dynamic relocs in read-only sections,
  2630. then we'll be keeping the dynamic relocs and avoiding the copy
  2631. reloc. */
  2632. if (!_bfd_elf_readonly_dynrelocs (h))
  2633. {
  2634. h->non_got_ref = 0;
  2635. return true;
  2636. }
  2637. }
  2638. /* We must allocate the symbol in our .dynbss section, which will
  2639. become part of the .bss section of the executable. There will be
  2640. an entry for this symbol in the .dynsym section. The dynamic
  2641. object will contain position independent code, so all references
  2642. from the dynamic object to this symbol will go through the global
  2643. offset table. The dynamic linker will use the .dynsym entry to
  2644. determine the address it must put in the global offset table, so
  2645. both the dynamic object and the regular object will refer to the
  2646. same memory location for the variable. */
  2647. /* We must generate a R_386_COPY/R_X86_64_COPY reloc to tell the
  2648. dynamic linker to copy the initial value out of the dynamic object
  2649. and into the runtime process image. */
  2650. if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
  2651. {
  2652. s = htab->elf.sdynrelro;
  2653. srel = htab->elf.sreldynrelro;
  2654. }
  2655. else
  2656. {
  2657. s = htab->elf.sdynbss;
  2658. srel = htab->elf.srelbss;
  2659. }
  2660. if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
  2661. {
  2662. srel->size += htab->sizeof_reloc;
  2663. h->needs_copy = 1;
  2664. }
  2665. return _bfd_elf_adjust_dynamic_copy (info, h, s);
  2666. }
  2667. void
  2668. _bfd_x86_elf_hide_symbol (struct bfd_link_info *info,
  2669. struct elf_link_hash_entry *h,
  2670. bool force_local)
  2671. {
  2672. if (h->root.type == bfd_link_hash_undefweak
  2673. && info->nointerp
  2674. && bfd_link_pie (info))
  2675. {
  2676. /* When there is no dynamic interpreter in PIE, make the undefined
  2677. weak symbol dynamic so that PC relative branch to the undefined
  2678. weak symbol will land to address 0. */
  2679. struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
  2680. if (h->plt.refcount > 0
  2681. || eh->plt_got.refcount > 0)
  2682. return;
  2683. }
  2684. _bfd_elf_link_hash_hide_symbol (info, h, force_local);
  2685. }
  2686. /* Return TRUE if a symbol is referenced locally. It is similar to
  2687. SYMBOL_REFERENCES_LOCAL, but it also checks version script. It
  2688. works in check_relocs. */
  2689. bool
  2690. _bfd_x86_elf_link_symbol_references_local (struct bfd_link_info *info,
  2691. struct elf_link_hash_entry *h)
  2692. {
  2693. struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
  2694. struct elf_x86_link_hash_table *htab
  2695. = (struct elf_x86_link_hash_table *) info->hash;
  2696. if (eh->local_ref > 1)
  2697. return true;
  2698. if (eh->local_ref == 1)
  2699. return false;
  2700. /* Unversioned symbols defined in regular objects can be forced local
  2701. by linker version script. A weak undefined symbol is forced local
  2702. if
  2703. 1. It has non-default visibility. Or
  2704. 2. When building executable, there is no dynamic linker. Or
  2705. 3. or "-z nodynamic-undefined-weak" is used.
  2706. */
  2707. if (SYMBOL_REFERENCES_LOCAL (info, h)
  2708. || (h->root.type == bfd_link_hash_undefweak
  2709. && (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
  2710. || (bfd_link_executable (info)
  2711. && htab->interp == NULL)
  2712. || info->dynamic_undefined_weak == 0))
  2713. || ((h->def_regular || ELF_COMMON_DEF_P (h))
  2714. && info->version_info != NULL
  2715. && _bfd_elf_link_hide_sym_by_version (info, h)))
  2716. {
  2717. eh->local_ref = 2;
  2718. return true;
  2719. }
  2720. eh->local_ref = 1;
  2721. return false;
  2722. }
  2723. /* Return the section that should be marked against GC for a given
  2724. relocation. */
  2725. asection *
  2726. _bfd_x86_elf_gc_mark_hook (asection *sec,
  2727. struct bfd_link_info *info,
  2728. Elf_Internal_Rela *rel,
  2729. struct elf_link_hash_entry *h,
  2730. Elf_Internal_Sym *sym)
  2731. {
  2732. /* Compiler should optimize this out. */
  2733. if (((unsigned int) R_X86_64_GNU_VTINHERIT
  2734. != (unsigned int) R_386_GNU_VTINHERIT)
  2735. || ((unsigned int) R_X86_64_GNU_VTENTRY
  2736. != (unsigned int) R_386_GNU_VTENTRY))
  2737. abort ();
  2738. if (h != NULL)
  2739. switch (ELF32_R_TYPE (rel->r_info))
  2740. {
  2741. case R_X86_64_GNU_VTINHERIT:
  2742. case R_X86_64_GNU_VTENTRY:
  2743. return NULL;
  2744. }
  2745. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  2746. }
  2747. static bfd_vma
  2748. elf_i386_get_plt_got_vma (struct elf_x86_plt *plt_p ATTRIBUTE_UNUSED,
  2749. bfd_vma off,
  2750. bfd_vma offset ATTRIBUTE_UNUSED,
  2751. bfd_vma got_addr)
  2752. {
  2753. return got_addr + off;
  2754. }
  2755. static bfd_vma
  2756. elf_x86_64_get_plt_got_vma (struct elf_x86_plt *plt_p,
  2757. bfd_vma off,
  2758. bfd_vma offset,
  2759. bfd_vma got_addr ATTRIBUTE_UNUSED)
  2760. {
  2761. return plt_p->sec->vma + offset + off + plt_p->plt_got_insn_size;
  2762. }
  2763. static bool
  2764. elf_i386_valid_plt_reloc_p (unsigned int type)
  2765. {
  2766. return (type == R_386_JUMP_SLOT
  2767. || type == R_386_GLOB_DAT
  2768. || type == R_386_IRELATIVE);
  2769. }
  2770. static bool
  2771. elf_x86_64_valid_plt_reloc_p (unsigned int type)
  2772. {
  2773. return (type == R_X86_64_JUMP_SLOT
  2774. || type == R_X86_64_GLOB_DAT
  2775. || type == R_X86_64_IRELATIVE);
  2776. }
  2777. long
  2778. _bfd_x86_elf_get_synthetic_symtab (bfd *abfd,
  2779. long count,
  2780. long relsize,
  2781. bfd_vma got_addr,
  2782. struct elf_x86_plt plts[],
  2783. asymbol **dynsyms,
  2784. asymbol **ret)
  2785. {
  2786. long size, i, n, len;
  2787. int j;
  2788. unsigned int plt_got_offset, plt_entry_size;
  2789. asymbol *s;
  2790. bfd_byte *plt_contents;
  2791. long dynrelcount;
  2792. arelent **dynrelbuf, *p;
  2793. char *names;
  2794. const struct elf_backend_data *bed;
  2795. bfd_vma (*get_plt_got_vma) (struct elf_x86_plt *, bfd_vma, bfd_vma,
  2796. bfd_vma);
  2797. bool (*valid_plt_reloc_p) (unsigned int);
  2798. dynrelbuf = NULL;
  2799. if (count == 0)
  2800. goto bad_return;
  2801. dynrelbuf = (arelent **) bfd_malloc (relsize);
  2802. if (dynrelbuf == NULL)
  2803. goto bad_return;
  2804. dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
  2805. dynsyms);
  2806. if (dynrelcount <= 0)
  2807. goto bad_return;
  2808. /* Sort the relocs by address. */
  2809. qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
  2810. _bfd_x86_elf_compare_relocs);
  2811. size = count * sizeof (asymbol);
  2812. /* Allocate space for @plt suffixes. */
  2813. n = 0;
  2814. for (i = 0; i < dynrelcount; i++)
  2815. {
  2816. p = dynrelbuf[i];
  2817. size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
  2818. if (p->addend != 0)
  2819. size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
  2820. }
  2821. s = *ret = (asymbol *) bfd_zmalloc (size);
  2822. if (s == NULL)
  2823. goto bad_return;
  2824. bed = get_elf_backend_data (abfd);
  2825. if (bed->target_id == X86_64_ELF_DATA)
  2826. {
  2827. get_plt_got_vma = elf_x86_64_get_plt_got_vma;
  2828. valid_plt_reloc_p = elf_x86_64_valid_plt_reloc_p;
  2829. }
  2830. else
  2831. {
  2832. get_plt_got_vma = elf_i386_get_plt_got_vma;
  2833. valid_plt_reloc_p = elf_i386_valid_plt_reloc_p;
  2834. if (got_addr)
  2835. {
  2836. /* Check .got.plt and then .got to get the _GLOBAL_OFFSET_TABLE_
  2837. address. */
  2838. asection *sec = bfd_get_section_by_name (abfd, ".got.plt");
  2839. if (sec != NULL)
  2840. got_addr = sec->vma;
  2841. else
  2842. {
  2843. sec = bfd_get_section_by_name (abfd, ".got");
  2844. if (sec != NULL)
  2845. got_addr = sec->vma;
  2846. }
  2847. if (got_addr == (bfd_vma) -1)
  2848. goto bad_return;
  2849. }
  2850. }
  2851. /* Check for each PLT section. */
  2852. names = (char *) (s + count);
  2853. size = 0;
  2854. n = 0;
  2855. for (j = 0; plts[j].name != NULL; j++)
  2856. if ((plt_contents = plts[j].contents) != NULL)
  2857. {
  2858. long k;
  2859. bfd_vma offset;
  2860. asection *plt;
  2861. struct elf_x86_plt *plt_p = &plts[j];
  2862. plt_got_offset = plt_p->plt_got_offset;
  2863. plt_entry_size = plt_p->plt_entry_size;
  2864. plt = plt_p->sec;
  2865. if ((plt_p->type & plt_lazy))
  2866. {
  2867. /* Skip PLT0 in lazy PLT. */
  2868. k = 1;
  2869. offset = plt_entry_size;
  2870. }
  2871. else
  2872. {
  2873. k = 0;
  2874. offset = 0;
  2875. }
  2876. /* Check each PLT entry against dynamic relocations. */
  2877. for (; k < plt_p->count; k++)
  2878. {
  2879. int off;
  2880. bfd_vma got_vma;
  2881. long min, max, mid;
  2882. /* Get the GOT offset for i386 or the PC-relative offset
  2883. for x86-64, a signed 32-bit integer. */
  2884. off = H_GET_32 (abfd, (plt_contents + offset
  2885. + plt_got_offset));
  2886. got_vma = get_plt_got_vma (plt_p, off, offset, got_addr);
  2887. /* Binary search. */
  2888. p = dynrelbuf[0];
  2889. min = 0;
  2890. max = dynrelcount;
  2891. while ((min + 1) < max)
  2892. {
  2893. arelent *r;
  2894. mid = (min + max) / 2;
  2895. r = dynrelbuf[mid];
  2896. if (got_vma > r->address)
  2897. min = mid;
  2898. else if (got_vma < r->address)
  2899. max = mid;
  2900. else
  2901. {
  2902. p = r;
  2903. break;
  2904. }
  2905. }
  2906. /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
  2907. if (got_vma == p->address
  2908. && p->howto != NULL
  2909. && valid_plt_reloc_p (p->howto->type))
  2910. {
  2911. *s = **p->sym_ptr_ptr;
  2912. /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
  2913. set. Since we are defining a symbol, ensure one
  2914. of them is set. */
  2915. if ((s->flags & BSF_LOCAL) == 0)
  2916. s->flags |= BSF_GLOBAL;
  2917. s->flags |= BSF_SYNTHETIC;
  2918. /* This is no longer a section symbol. */
  2919. s->flags &= ~BSF_SECTION_SYM;
  2920. s->section = plt;
  2921. s->the_bfd = plt->owner;
  2922. s->value = offset;
  2923. s->udata.p = NULL;
  2924. s->name = names;
  2925. len = strlen ((*p->sym_ptr_ptr)->name);
  2926. memcpy (names, (*p->sym_ptr_ptr)->name, len);
  2927. names += len;
  2928. if (p->addend != 0)
  2929. {
  2930. char buf[30], *a;
  2931. memcpy (names, "+0x", sizeof ("+0x") - 1);
  2932. names += sizeof ("+0x") - 1;
  2933. bfd_sprintf_vma (abfd, buf, p->addend);
  2934. for (a = buf; *a == '0'; ++a)
  2935. ;
  2936. size = strlen (a);
  2937. memcpy (names, a, size);
  2938. names += size;
  2939. }
  2940. memcpy (names, "@plt", sizeof ("@plt"));
  2941. names += sizeof ("@plt");
  2942. n++;
  2943. s++;
  2944. /* There should be only one entry in PLT for a given
  2945. symbol. Set howto to NULL after processing a PLT
  2946. entry to guard against corrupted PLT. */
  2947. p->howto = NULL;
  2948. }
  2949. offset += plt_entry_size;
  2950. }
  2951. }
  2952. /* PLT entries with R_386_TLS_DESC relocations are skipped. */
  2953. if (n == 0)
  2954. {
  2955. bad_return:
  2956. count = -1;
  2957. }
  2958. else
  2959. count = n;
  2960. for (j = 0; plts[j].name != NULL; j++)
  2961. free (plts[j].contents);
  2962. free (dynrelbuf);
  2963. return count;
  2964. }
  2965. /* Parse x86 GNU properties. */
  2966. enum elf_property_kind
  2967. _bfd_x86_elf_parse_gnu_properties (bfd *abfd, unsigned int type,
  2968. bfd_byte *ptr, unsigned int datasz)
  2969. {
  2970. elf_property *prop;
  2971. if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
  2972. || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
  2973. || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
  2974. && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
  2975. || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
  2976. && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
  2977. || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
  2978. && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
  2979. {
  2980. if (datasz != 4)
  2981. {
  2982. _bfd_error_handler
  2983. (_("error: %pB: <corrupt x86 property (0x%x) size: 0x%x>"),
  2984. abfd, type, datasz);
  2985. return property_corrupt;
  2986. }
  2987. prop = _bfd_elf_get_property (abfd, type, datasz);
  2988. prop->u.number |= bfd_h_get_32 (abfd, ptr);
  2989. prop->pr_kind = property_number;
  2990. return property_number;
  2991. }
  2992. return property_ignored;
  2993. }
  2994. /* Merge x86 GNU property BPROP with APROP. If APROP isn't NULL,
  2995. return TRUE if APROP is updated. Otherwise, return TRUE if BPROP
  2996. should be merged with ABFD. */
  2997. bool
  2998. _bfd_x86_elf_merge_gnu_properties (struct bfd_link_info *info,
  2999. bfd *abfd ATTRIBUTE_UNUSED,
  3000. bfd *bbfd ATTRIBUTE_UNUSED,
  3001. elf_property *aprop,
  3002. elf_property *bprop)
  3003. {
  3004. unsigned int number, features;
  3005. bool updated = false;
  3006. const struct elf_backend_data *bed;
  3007. struct elf_x86_link_hash_table *htab;
  3008. unsigned int pr_type = aprop != NULL ? aprop->pr_type : bprop->pr_type;
  3009. if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
  3010. || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
  3011. && pr_type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
  3012. {
  3013. if (aprop == NULL || bprop == NULL)
  3014. {
  3015. /* Only one of APROP and BPROP can be NULL. */
  3016. if (aprop != NULL)
  3017. {
  3018. /* Remove this property since the other input file doesn't
  3019. have it. */
  3020. aprop->pr_kind = property_remove;
  3021. updated = true;
  3022. }
  3023. }
  3024. else
  3025. {
  3026. number = aprop->u.number;
  3027. aprop->u.number = number | bprop->u.number;
  3028. updated = number != (unsigned int) aprop->u.number;
  3029. }
  3030. return updated;
  3031. }
  3032. else if (pr_type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
  3033. || (pr_type >= GNU_PROPERTY_X86_UINT32_OR_LO
  3034. && pr_type <= GNU_PROPERTY_X86_UINT32_OR_HI))
  3035. {
  3036. features = 0;
  3037. if (pr_type == GNU_PROPERTY_X86_ISA_1_NEEDED)
  3038. {
  3039. bed = get_elf_backend_data (info->output_bfd);
  3040. htab = elf_x86_hash_table (info, bed->target_id);
  3041. switch (htab->params->isa_level)
  3042. {
  3043. case 0:
  3044. break;
  3045. case 2:
  3046. features = GNU_PROPERTY_X86_ISA_1_V2;
  3047. break;
  3048. case 3:
  3049. features = GNU_PROPERTY_X86_ISA_1_V3;
  3050. break;
  3051. case 4:
  3052. features = GNU_PROPERTY_X86_ISA_1_V4;
  3053. break;
  3054. default:
  3055. abort ();
  3056. }
  3057. }
  3058. if (aprop != NULL && bprop != NULL)
  3059. {
  3060. number = aprop->u.number;
  3061. aprop->u.number = number | bprop->u.number | features;
  3062. /* Remove the property if all bits are empty. */
  3063. if (aprop->u.number == 0)
  3064. {
  3065. aprop->pr_kind = property_remove;
  3066. updated = true;
  3067. }
  3068. else
  3069. updated = number != (unsigned int) aprop->u.number;
  3070. }
  3071. else
  3072. {
  3073. /* Only one of APROP and BPROP can be NULL. */
  3074. if (aprop != NULL)
  3075. {
  3076. aprop->u.number |= features;
  3077. if (aprop->u.number == 0)
  3078. {
  3079. /* Remove APROP if all bits are empty. */
  3080. aprop->pr_kind = property_remove;
  3081. updated = true;
  3082. }
  3083. }
  3084. else
  3085. {
  3086. /* Return TRUE if APROP is NULL and all bits of BPROP
  3087. aren't empty to indicate that BPROP should be added
  3088. to ABFD. */
  3089. bprop->u.number |= features;
  3090. updated = bprop->u.number != 0;
  3091. }
  3092. }
  3093. return updated;
  3094. }
  3095. else if (pr_type >= GNU_PROPERTY_X86_UINT32_AND_LO
  3096. && pr_type <= GNU_PROPERTY_X86_UINT32_AND_HI)
  3097. {
  3098. /* Only one of APROP and BPROP can be NULL:
  3099. 1. APROP & BPROP when both APROP and BPROP aren't NULL.
  3100. 2. If APROP is NULL, remove x86 feature.
  3101. 3. Otherwise, do nothing.
  3102. */
  3103. bed = get_elf_backend_data (info->output_bfd);
  3104. htab = elf_x86_hash_table (info, bed->target_id);
  3105. if (!htab)
  3106. abort ();
  3107. if (aprop != NULL && bprop != NULL)
  3108. {
  3109. number = aprop->u.number;
  3110. aprop->u.number = number & bprop->u.number;
  3111. if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
  3112. {
  3113. features = 0;
  3114. if (htab->params->ibt)
  3115. features = GNU_PROPERTY_X86_FEATURE_1_IBT;
  3116. if (htab->params->shstk)
  3117. features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
  3118. if (htab->params->lam_u48)
  3119. features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
  3120. | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
  3121. else if (htab->params->lam_u57)
  3122. features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
  3123. /* Add GNU_PROPERTY_X86_FEATURE_1_IBT,
  3124. GNU_PROPERTY_X86_FEATURE_1_SHSTK,
  3125. GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
  3126. GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
  3127. aprop->u.number |= features;
  3128. }
  3129. updated = number != (unsigned int) aprop->u.number;
  3130. /* Remove the property if all feature bits are cleared. */
  3131. if (aprop->u.number == 0)
  3132. aprop->pr_kind = property_remove;
  3133. }
  3134. else
  3135. {
  3136. /* There should be no AND properties since some input doesn't
  3137. have them. Set IBT and SHSTK properties for -z ibt and -z
  3138. shstk if needed. */
  3139. features = 0;
  3140. if (pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
  3141. {
  3142. if (htab->params->ibt)
  3143. features = GNU_PROPERTY_X86_FEATURE_1_IBT;
  3144. if (htab->params->shstk)
  3145. features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
  3146. if (htab->params->lam_u48)
  3147. features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
  3148. | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
  3149. else if (htab->params->lam_u57)
  3150. features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
  3151. }
  3152. if (features)
  3153. {
  3154. if (aprop != NULL)
  3155. {
  3156. updated = features != (unsigned int) aprop->u.number;
  3157. aprop->u.number = features;
  3158. }
  3159. else
  3160. {
  3161. updated = true;
  3162. bprop->u.number = features;
  3163. }
  3164. }
  3165. else if (aprop != NULL)
  3166. {
  3167. aprop->pr_kind = property_remove;
  3168. updated = true;
  3169. }
  3170. }
  3171. return updated;
  3172. }
  3173. else
  3174. {
  3175. /* Never should happen. */
  3176. abort ();
  3177. }
  3178. return updated;
  3179. }
  3180. /* Set up x86 GNU properties. Return the first relocatable ELF input
  3181. with GNU properties if found. Otherwise, return NULL. */
  3182. bfd *
  3183. _bfd_x86_elf_link_setup_gnu_properties
  3184. (struct bfd_link_info *info, struct elf_x86_init_table *init_table)
  3185. {
  3186. bool normal_target;
  3187. bool lazy_plt;
  3188. asection *sec, *pltsec;
  3189. bfd *dynobj;
  3190. bool use_ibt_plt;
  3191. unsigned int plt_alignment, features, isa_level;
  3192. struct elf_x86_link_hash_table *htab;
  3193. bfd *pbfd;
  3194. bfd *ebfd = NULL;
  3195. elf_property *prop;
  3196. const struct elf_backend_data *bed;
  3197. unsigned int class_align = ABI_64_P (info->output_bfd) ? 3 : 2;
  3198. unsigned int got_align;
  3199. /* Find a normal input file with GNU property note. */
  3200. for (pbfd = info->input_bfds;
  3201. pbfd != NULL;
  3202. pbfd = pbfd->link.next)
  3203. if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
  3204. && bfd_count_sections (pbfd) != 0)
  3205. {
  3206. ebfd = pbfd;
  3207. if (elf_properties (pbfd) != NULL)
  3208. break;
  3209. }
  3210. bed = get_elf_backend_data (info->output_bfd);
  3211. htab = elf_x86_hash_table (info, bed->target_id);
  3212. if (htab == NULL)
  3213. return pbfd;
  3214. features = 0;
  3215. if (htab->params->ibt)
  3216. {
  3217. features = GNU_PROPERTY_X86_FEATURE_1_IBT;
  3218. htab->params->cet_report &= ~prop_report_ibt;
  3219. }
  3220. if (htab->params->shstk)
  3221. {
  3222. features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
  3223. htab->params->cet_report &= ~prop_report_shstk;
  3224. }
  3225. if (!(htab->params->cet_report & (prop_report_ibt | prop_report_shstk)))
  3226. htab->params->cet_report = prop_report_none;
  3227. if (htab->params->lam_u48)
  3228. {
  3229. features |= (GNU_PROPERTY_X86_FEATURE_1_LAM_U48
  3230. | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
  3231. htab->params->lam_u48_report = prop_report_none;
  3232. htab->params->lam_u57_report = prop_report_none;
  3233. }
  3234. else if (htab->params->lam_u57)
  3235. {
  3236. features |= GNU_PROPERTY_X86_FEATURE_1_LAM_U57;
  3237. htab->params->lam_u57_report = prop_report_none;
  3238. }
  3239. switch (htab->params->isa_level)
  3240. {
  3241. case 0:
  3242. isa_level = 0;
  3243. break;
  3244. case 1:
  3245. isa_level = GNU_PROPERTY_X86_ISA_1_BASELINE;
  3246. break;
  3247. case 2:
  3248. isa_level = GNU_PROPERTY_X86_ISA_1_V2;
  3249. break;
  3250. case 3:
  3251. isa_level = GNU_PROPERTY_X86_ISA_1_V3;
  3252. break;
  3253. case 4:
  3254. isa_level = GNU_PROPERTY_X86_ISA_1_V4;
  3255. break;
  3256. default:
  3257. abort ();
  3258. }
  3259. if (ebfd != NULL)
  3260. {
  3261. prop = NULL;
  3262. if (features)
  3263. {
  3264. /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT,
  3265. GNU_PROPERTY_X86_FEATURE_1_SHSTK,
  3266. GNU_PROPERTY_X86_FEATURE_1_LAM_U48 and
  3267. GNU_PROPERTY_X86_FEATURE_1_LAM_U57. */
  3268. prop = _bfd_elf_get_property (ebfd,
  3269. GNU_PROPERTY_X86_FEATURE_1_AND,
  3270. 4);
  3271. prop->u.number |= features;
  3272. prop->pr_kind = property_number;
  3273. }
  3274. if (isa_level)
  3275. {
  3276. /* If ISA level is set, add GNU_PROPERTY_X86_ISA_1_NEEDED. */
  3277. prop = _bfd_elf_get_property (ebfd,
  3278. GNU_PROPERTY_X86_ISA_1_NEEDED,
  3279. 4);
  3280. prop->u.number |= isa_level;
  3281. prop->pr_kind = property_number;
  3282. }
  3283. /* Create the GNU property note section if needed. */
  3284. if (prop != NULL && pbfd == NULL)
  3285. {
  3286. sec = bfd_make_section_with_flags (ebfd,
  3287. NOTE_GNU_PROPERTY_SECTION_NAME,
  3288. (SEC_ALLOC
  3289. | SEC_LOAD
  3290. | SEC_IN_MEMORY
  3291. | SEC_READONLY
  3292. | SEC_HAS_CONTENTS
  3293. | SEC_DATA));
  3294. if (sec == NULL)
  3295. info->callbacks->einfo (_("%F%P: failed to create GNU property section\n"));
  3296. if (!bfd_set_section_alignment (sec, class_align))
  3297. {
  3298. error_alignment:
  3299. info->callbacks->einfo (_("%F%pA: failed to align section\n"),
  3300. sec);
  3301. }
  3302. elf_section_type (sec) = SHT_NOTE;
  3303. }
  3304. }
  3305. if (htab->params->cet_report
  3306. || htab->params->lam_u48_report
  3307. || htab->params->lam_u57_report)
  3308. {
  3309. /* Report missing IBT, SHSTK and LAM properties. */
  3310. bfd *abfd;
  3311. const char *warning_msg = _("%P: %pB: warning: missing %s\n");
  3312. const char *error_msg = _("%X%P: %pB: error: missing %s\n");
  3313. const char *cet_msg = NULL;
  3314. const char *lam_u48_msg = NULL;
  3315. const char *lam_u57_msg = NULL;
  3316. const char *missing;
  3317. elf_property_list *p;
  3318. bool missing_ibt, missing_shstk;
  3319. bool missing_lam_u48, missing_lam_u57;
  3320. bool check_ibt
  3321. = (htab->params->cet_report
  3322. && (htab->params->cet_report & prop_report_ibt));
  3323. bool check_shstk
  3324. = (htab->params->cet_report
  3325. && (htab->params->cet_report & prop_report_shstk));
  3326. if (htab->params->cet_report)
  3327. {
  3328. if ((htab->params->cet_report & prop_report_warning))
  3329. cet_msg = warning_msg;
  3330. else
  3331. cet_msg = error_msg;
  3332. }
  3333. if (htab->params->lam_u48_report)
  3334. {
  3335. if ((htab->params->lam_u48_report & prop_report_warning))
  3336. lam_u48_msg = warning_msg;
  3337. else
  3338. lam_u48_msg = error_msg;
  3339. }
  3340. if (htab->params->lam_u57_report)
  3341. {
  3342. if ((htab->params->lam_u57_report & prop_report_warning))
  3343. lam_u57_msg = warning_msg;
  3344. else
  3345. lam_u57_msg = error_msg;
  3346. }
  3347. for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  3348. if (!(abfd->flags & (DYNAMIC | BFD_PLUGIN | BFD_LINKER_CREATED))
  3349. && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  3350. {
  3351. for (p = elf_properties (abfd); p; p = p->next)
  3352. if (p->property.pr_type == GNU_PROPERTY_X86_FEATURE_1_AND)
  3353. break;
  3354. missing_ibt = check_ibt;
  3355. missing_shstk = check_shstk;
  3356. missing_lam_u48 = !!lam_u48_msg;
  3357. missing_lam_u57 = !!lam_u57_msg;
  3358. if (p)
  3359. {
  3360. missing_ibt &= !(p->property.u.number
  3361. & GNU_PROPERTY_X86_FEATURE_1_IBT);
  3362. missing_shstk &= !(p->property.u.number
  3363. & GNU_PROPERTY_X86_FEATURE_1_SHSTK);
  3364. missing_lam_u48 &= !(p->property.u.number
  3365. & GNU_PROPERTY_X86_FEATURE_1_LAM_U48);
  3366. missing_lam_u57 &= !(p->property.u.number
  3367. & GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
  3368. }
  3369. if (missing_ibt || missing_shstk)
  3370. {
  3371. if (missing_ibt && missing_shstk)
  3372. missing = _("IBT and SHSTK properties");
  3373. else if (missing_ibt)
  3374. missing = _("IBT property");
  3375. else
  3376. missing = _("SHSTK property");
  3377. info->callbacks->einfo (cet_msg, abfd, missing);
  3378. }
  3379. if (missing_lam_u48)
  3380. {
  3381. missing = _("LAM_U48 property");
  3382. info->callbacks->einfo (lam_u48_msg, abfd, missing);
  3383. }
  3384. if (missing_lam_u57)
  3385. {
  3386. missing = _("LAM_U57 property");
  3387. info->callbacks->einfo (lam_u57_msg, abfd, missing);
  3388. }
  3389. }
  3390. }
  3391. pbfd = _bfd_elf_link_setup_gnu_properties (info);
  3392. htab->r_info = init_table->r_info;
  3393. htab->r_sym = init_table->r_sym;
  3394. if (bfd_link_relocatable (info))
  3395. return pbfd;
  3396. htab->plt0_pad_byte = init_table->plt0_pad_byte;
  3397. use_ibt_plt = htab->params->ibtplt || htab->params->ibt;
  3398. if (!use_ibt_plt && pbfd != NULL)
  3399. {
  3400. /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
  3401. elf_property_list *p;
  3402. /* The property list is sorted in order of type. */
  3403. for (p = elf_properties (pbfd); p; p = p->next)
  3404. {
  3405. if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
  3406. {
  3407. use_ibt_plt = !!(p->property.u.number
  3408. & GNU_PROPERTY_X86_FEATURE_1_IBT);
  3409. break;
  3410. }
  3411. else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
  3412. break;
  3413. }
  3414. }
  3415. dynobj = htab->elf.dynobj;
  3416. /* Set htab->elf.dynobj here so that there is no need to check and
  3417. set it in check_relocs. */
  3418. if (dynobj == NULL)
  3419. {
  3420. if (pbfd != NULL)
  3421. {
  3422. htab->elf.dynobj = pbfd;
  3423. dynobj = pbfd;
  3424. }
  3425. else
  3426. {
  3427. bfd *abfd;
  3428. /* Find a normal input file to hold linker created
  3429. sections. */
  3430. for (abfd = info->input_bfds;
  3431. abfd != NULL;
  3432. abfd = abfd->link.next)
  3433. if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
  3434. && (abfd->flags
  3435. & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0
  3436. && bed->relocs_compatible (abfd->xvec,
  3437. info->output_bfd->xvec))
  3438. {
  3439. htab->elf.dynobj = abfd;
  3440. dynobj = abfd;
  3441. break;
  3442. }
  3443. }
  3444. }
  3445. /* Return if there are no normal input files. */
  3446. if (dynobj == NULL)
  3447. return pbfd;
  3448. /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
  3449. still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
  3450. canonical function address. */
  3451. htab->plt.has_plt0 = 1;
  3452. normal_target = htab->elf.target_os == is_normal;
  3453. if (normal_target)
  3454. {
  3455. if (use_ibt_plt)
  3456. {
  3457. htab->lazy_plt = init_table->lazy_ibt_plt;
  3458. htab->non_lazy_plt = init_table->non_lazy_ibt_plt;
  3459. }
  3460. else
  3461. {
  3462. htab->lazy_plt = init_table->lazy_plt;
  3463. htab->non_lazy_plt = init_table->non_lazy_plt;
  3464. }
  3465. }
  3466. else
  3467. {
  3468. htab->lazy_plt = init_table->lazy_plt;
  3469. htab->non_lazy_plt = NULL;
  3470. }
  3471. pltsec = htab->elf.splt;
  3472. /* If the non-lazy PLT is available, use it for all PLT entries if
  3473. there are no PLT0 or no .plt section. */
  3474. if (htab->non_lazy_plt != NULL
  3475. && (!htab->plt.has_plt0 || pltsec == NULL))
  3476. {
  3477. lazy_plt = false;
  3478. if (bfd_link_pic (info))
  3479. htab->plt.plt_entry = htab->non_lazy_plt->pic_plt_entry;
  3480. else
  3481. htab->plt.plt_entry = htab->non_lazy_plt->plt_entry;
  3482. htab->plt.plt_entry_size = htab->non_lazy_plt->plt_entry_size;
  3483. htab->plt.plt_got_offset = htab->non_lazy_plt->plt_got_offset;
  3484. htab->plt.plt_got_insn_size
  3485. = htab->non_lazy_plt->plt_got_insn_size;
  3486. htab->plt.eh_frame_plt_size
  3487. = htab->non_lazy_plt->eh_frame_plt_size;
  3488. htab->plt.eh_frame_plt = htab->non_lazy_plt->eh_frame_plt;
  3489. }
  3490. else
  3491. {
  3492. lazy_plt = true;
  3493. if (bfd_link_pic (info))
  3494. {
  3495. htab->plt.plt0_entry = htab->lazy_plt->pic_plt0_entry;
  3496. htab->plt.plt_entry = htab->lazy_plt->pic_plt_entry;
  3497. }
  3498. else
  3499. {
  3500. htab->plt.plt0_entry = htab->lazy_plt->plt0_entry;
  3501. htab->plt.plt_entry = htab->lazy_plt->plt_entry;
  3502. }
  3503. htab->plt.plt_entry_size = htab->lazy_plt->plt_entry_size;
  3504. htab->plt.plt_got_offset = htab->lazy_plt->plt_got_offset;
  3505. htab->plt.plt_got_insn_size
  3506. = htab->lazy_plt->plt_got_insn_size;
  3507. htab->plt.eh_frame_plt_size
  3508. = htab->lazy_plt->eh_frame_plt_size;
  3509. htab->plt.eh_frame_plt = htab->lazy_plt->eh_frame_plt;
  3510. }
  3511. if (htab->elf.target_os == is_vxworks
  3512. && !elf_vxworks_create_dynamic_sections (dynobj, info,
  3513. &htab->srelplt2))
  3514. {
  3515. info->callbacks->einfo (_("%F%P: failed to create VxWorks dynamic sections\n"));
  3516. return pbfd;
  3517. }
  3518. /* Since create_dynamic_sections isn't always called, but GOT
  3519. relocations need GOT relocations, create them here so that we
  3520. don't need to do it in check_relocs. */
  3521. if (htab->elf.sgot == NULL
  3522. && !_bfd_elf_create_got_section (dynobj, info))
  3523. info->callbacks->einfo (_("%F%P: failed to create GOT sections\n"));
  3524. got_align = (bed->target_id == X86_64_ELF_DATA) ? 3 : 2;
  3525. /* Align .got and .got.plt sections to their entry size. Do it here
  3526. instead of in create_dynamic_sections so that they are always
  3527. properly aligned even if create_dynamic_sections isn't called. */
  3528. sec = htab->elf.sgot;
  3529. if (!bfd_set_section_alignment (sec, got_align))
  3530. goto error_alignment;
  3531. sec = htab->elf.sgotplt;
  3532. if (!bfd_set_section_alignment (sec, got_align))
  3533. goto error_alignment;
  3534. /* Create the ifunc sections here so that check_relocs can be
  3535. simplified. */
  3536. if (!_bfd_elf_create_ifunc_sections (dynobj, info))
  3537. info->callbacks->einfo (_("%F%P: failed to create ifunc sections\n"));
  3538. plt_alignment = bfd_log2 (htab->plt.plt_entry_size);
  3539. if (pltsec != NULL)
  3540. {
  3541. /* Whe creating executable, set the contents of the .interp
  3542. section to the interpreter. */
  3543. if (bfd_link_executable (info) && !info->nointerp)
  3544. {
  3545. asection *s = bfd_get_linker_section (dynobj, ".interp");
  3546. if (s == NULL)
  3547. abort ();
  3548. s->size = htab->dynamic_interpreter_size;
  3549. s->contents = (unsigned char *) htab->dynamic_interpreter;
  3550. htab->interp = s;
  3551. }
  3552. if (normal_target)
  3553. {
  3554. flagword pltflags = (bed->dynamic_sec_flags
  3555. | SEC_ALLOC
  3556. | SEC_CODE
  3557. | SEC_LOAD
  3558. | SEC_READONLY);
  3559. unsigned int non_lazy_plt_alignment
  3560. = bfd_log2 (htab->non_lazy_plt->plt_entry_size);
  3561. sec = pltsec;
  3562. if (!bfd_set_section_alignment (sec, plt_alignment))
  3563. goto error_alignment;
  3564. /* Create the GOT procedure linkage table. */
  3565. sec = bfd_make_section_anyway_with_flags (dynobj,
  3566. ".plt.got",
  3567. pltflags);
  3568. if (sec == NULL)
  3569. info->callbacks->einfo (_("%F%P: failed to create GOT PLT section\n"));
  3570. if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
  3571. goto error_alignment;
  3572. htab->plt_got = sec;
  3573. if (lazy_plt)
  3574. {
  3575. sec = NULL;
  3576. if (use_ibt_plt)
  3577. {
  3578. /* Create the second PLT for Intel IBT support. IBT
  3579. PLT is needed only for lazy binding. */
  3580. sec = bfd_make_section_anyway_with_flags (dynobj,
  3581. ".plt.sec",
  3582. pltflags);
  3583. if (sec == NULL)
  3584. info->callbacks->einfo (_("%F%P: failed to create IBT-enabled PLT section\n"));
  3585. if (!bfd_set_section_alignment (sec, plt_alignment))
  3586. goto error_alignment;
  3587. }
  3588. else if (htab->params->bndplt && ABI_64_P (dynobj))
  3589. {
  3590. /* Create the second PLT for Intel MPX support. MPX
  3591. PLT is supported only in 64-bit mode and is needed
  3592. only for lazy binding. */
  3593. sec = bfd_make_section_anyway_with_flags (dynobj,
  3594. ".plt.sec",
  3595. pltflags);
  3596. if (sec == NULL)
  3597. info->callbacks->einfo (_("%F%P: failed to create BND PLT section\n"));
  3598. if (!bfd_set_section_alignment (sec, non_lazy_plt_alignment))
  3599. goto error_alignment;
  3600. }
  3601. htab->plt_second = sec;
  3602. }
  3603. }
  3604. if (!info->no_ld_generated_unwind_info)
  3605. {
  3606. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
  3607. | SEC_HAS_CONTENTS | SEC_IN_MEMORY
  3608. | SEC_LINKER_CREATED);
  3609. sec = bfd_make_section_anyway_with_flags (dynobj,
  3610. ".eh_frame",
  3611. flags);
  3612. if (sec == NULL)
  3613. info->callbacks->einfo (_("%F%P: failed to create PLT .eh_frame section\n"));
  3614. if (!bfd_set_section_alignment (sec, class_align))
  3615. goto error_alignment;
  3616. htab->plt_eh_frame = sec;
  3617. if (htab->plt_got != NULL)
  3618. {
  3619. sec = bfd_make_section_anyway_with_flags (dynobj,
  3620. ".eh_frame",
  3621. flags);
  3622. if (sec == NULL)
  3623. info->callbacks->einfo (_("%F%P: failed to create GOT PLT .eh_frame section\n"));
  3624. if (!bfd_set_section_alignment (sec, class_align))
  3625. goto error_alignment;
  3626. htab->plt_got_eh_frame = sec;
  3627. }
  3628. if (htab->plt_second != NULL)
  3629. {
  3630. sec = bfd_make_section_anyway_with_flags (dynobj,
  3631. ".eh_frame",
  3632. flags);
  3633. if (sec == NULL)
  3634. info->callbacks->einfo (_("%F%P: failed to create the second PLT .eh_frame section\n"));
  3635. if (!bfd_set_section_alignment (sec, class_align))
  3636. goto error_alignment;
  3637. htab->plt_second_eh_frame = sec;
  3638. }
  3639. }
  3640. }
  3641. /* The .iplt section is used for IFUNC symbols in static
  3642. executables. */
  3643. sec = htab->elf.iplt;
  3644. if (sec != NULL)
  3645. {
  3646. /* NB: Delay setting its alignment until we know it is non-empty.
  3647. Otherwise an empty iplt section may change vma and lma of the
  3648. following sections, which triggers moving dot of the following
  3649. section backwards, resulting in a warning and section lma not
  3650. being set properly. It later leads to a "File truncated"
  3651. error. */
  3652. if (!bfd_set_section_alignment (sec, 0))
  3653. goto error_alignment;
  3654. htab->plt.iplt_alignment = (normal_target
  3655. ? plt_alignment
  3656. : bed->plt_alignment);
  3657. }
  3658. if (bfd_link_executable (info)
  3659. && !info->nointerp
  3660. && !htab->params->has_dynamic_linker
  3661. && htab->params->static_before_all_inputs)
  3662. {
  3663. /* Report error for dynamic input objects if -static is passed at
  3664. command-line before all input files without --dynamic-linker
  3665. unless --no-dynamic-linker is used. */
  3666. bfd *abfd;
  3667. for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  3668. if ((abfd->flags & DYNAMIC))
  3669. info->callbacks->einfo
  3670. (_("%X%P: attempted static link of dynamic object `%pB'\n"),
  3671. abfd);
  3672. }
  3673. return pbfd;
  3674. }
  3675. /* Fix up x86 GNU properties. */
  3676. void
  3677. _bfd_x86_elf_link_fixup_gnu_properties
  3678. (struct bfd_link_info *info, elf_property_list **listp)
  3679. {
  3680. elf_property_list *p;
  3681. for (p = *listp; p; p = p->next)
  3682. {
  3683. unsigned int type = p->property.pr_type;
  3684. if (type == GNU_PROPERTY_X86_COMPAT_ISA_1_USED
  3685. || type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
  3686. || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
  3687. && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
  3688. || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
  3689. && type <= GNU_PROPERTY_X86_UINT32_OR_HI)
  3690. || (type >= GNU_PROPERTY_X86_UINT32_OR_AND_LO
  3691. && type <= GNU_PROPERTY_X86_UINT32_OR_AND_HI))
  3692. {
  3693. if (p->property.u.number == 0
  3694. && (type == GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED
  3695. || (type >= GNU_PROPERTY_X86_UINT32_AND_LO
  3696. && type <= GNU_PROPERTY_X86_UINT32_AND_HI)
  3697. || (type >= GNU_PROPERTY_X86_UINT32_OR_LO
  3698. && type <= GNU_PROPERTY_X86_UINT32_OR_HI)))
  3699. {
  3700. /* Remove empty property. */
  3701. *listp = p->next;
  3702. continue;
  3703. }
  3704. /* Keep LAM features only for 64-bit output. */
  3705. if (type == GNU_PROPERTY_X86_FEATURE_1_AND
  3706. && !ABI_64_P (info->output_bfd))
  3707. p->property.u.number &= ~(GNU_PROPERTY_X86_FEATURE_1_LAM_U48
  3708. | GNU_PROPERTY_X86_FEATURE_1_LAM_U57);
  3709. listp = &p->next;
  3710. }
  3711. else if (type > GNU_PROPERTY_HIPROC)
  3712. {
  3713. /* The property list is sorted in order of type. */
  3714. break;
  3715. }
  3716. }
  3717. }
  3718. void
  3719. _bfd_elf_linker_x86_set_options (struct bfd_link_info * info,
  3720. struct elf_linker_x86_params *params)
  3721. {
  3722. const struct elf_backend_data *bed
  3723. = get_elf_backend_data (info->output_bfd);
  3724. struct elf_x86_link_hash_table *htab
  3725. = elf_x86_hash_table (info, bed->target_id);
  3726. if (htab != NULL)
  3727. htab->params = params;
  3728. }