i386.cc 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462
  1. // i386.cc -- i386 target support for gold.
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstring>
  19. #include "elfcpp.h"
  20. #include "dwarf.h"
  21. #include "parameters.h"
  22. #include "reloc.h"
  23. #include "i386.h"
  24. #include "object.h"
  25. #include "symtab.h"
  26. #include "layout.h"
  27. #include "output.h"
  28. #include "copy-relocs.h"
  29. #include "target.h"
  30. #include "target-reloc.h"
  31. #include "target-select.h"
  32. #include "tls.h"
  33. #include "freebsd.h"
  34. #include "nacl.h"
  35. #include "gc.h"
  36. namespace
  37. {
  38. using namespace gold;
  39. // A class to handle the .got.plt section.
  40. class Output_data_got_plt_i386 : public Output_section_data_build
  41. {
  42. public:
  43. Output_data_got_plt_i386(Layout* layout)
  44. : Output_section_data_build(4),
  45. layout_(layout)
  46. { }
  47. protected:
  48. // Write out the PLT data.
  49. void
  50. do_write(Output_file*);
  51. // Write to a map file.
  52. void
  53. do_print_to_mapfile(Mapfile* mapfile) const
  54. { mapfile->print_output_data(this, "** GOT PLT"); }
  55. private:
  56. // A pointer to the Layout class, so that we can find the .dynamic
  57. // section when we write out the GOT PLT section.
  58. Layout* layout_;
  59. };
  60. // A class to handle the PLT data.
  61. // This is an abstract base class that handles most of the linker details
  62. // but does not know the actual contents of PLT entries. The derived
  63. // classes below fill in those details.
  64. class Output_data_plt_i386 : public Output_section_data
  65. {
  66. public:
  67. typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
  68. Output_data_plt_i386(Layout*, uint64_t addralign,
  69. Output_data_got_plt_i386*, Output_data_space*);
  70. // Add an entry to the PLT.
  71. void
  72. add_entry(Symbol_table*, Layout*, Symbol* gsym);
  73. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
  74. unsigned int
  75. add_local_ifunc_entry(Symbol_table*, Layout*,
  76. Sized_relobj_file<32, false>* relobj,
  77. unsigned int local_sym_index);
  78. // Return the .rel.plt section data.
  79. Reloc_section*
  80. rel_plt() const
  81. { return this->rel_; }
  82. // Return where the TLS_DESC relocations should go.
  83. Reloc_section*
  84. rel_tls_desc(Layout*);
  85. // Return where the IRELATIVE relocations should go.
  86. Reloc_section*
  87. rel_irelative(Symbol_table*, Layout*);
  88. // Return whether we created a section for IRELATIVE relocations.
  89. bool
  90. has_irelative_section() const
  91. { return this->irelative_rel_ != NULL; }
  92. // Return the number of PLT entries.
  93. unsigned int
  94. entry_count() const
  95. { return this->count_ + this->irelative_count_; }
  96. // Return the offset of the first non-reserved PLT entry.
  97. unsigned int
  98. first_plt_entry_offset()
  99. { return this->get_plt_entry_size(); }
  100. // Return the size of a PLT entry.
  101. unsigned int
  102. get_plt_entry_size() const
  103. { return this->do_get_plt_entry_size(); }
  104. // Return the PLT address to use for a global symbol.
  105. uint64_t
  106. address_for_global(const Symbol*);
  107. // Return the PLT address to use for a local symbol.
  108. uint64_t
  109. address_for_local(const Relobj*, unsigned int symndx);
  110. // Add .eh_frame information for the PLT.
  111. void
  112. add_eh_frame(Layout* layout)
  113. { this->do_add_eh_frame(layout); }
  114. protected:
  115. // Fill the first PLT entry, given the pointer to the PLT section data
  116. // and the runtime address of the GOT.
  117. void
  118. fill_first_plt_entry(unsigned char* pov,
  119. elfcpp::Elf_types<32>::Elf_Addr got_address)
  120. { this->do_fill_first_plt_entry(pov, got_address); }
  121. // Fill a normal PLT entry, given the pointer to the entry's data in the
  122. // section, the runtime address of the GOT, the offset into the GOT of
  123. // the corresponding slot, the offset into the relocation section of the
  124. // corresponding reloc, and the offset of this entry within the whole
  125. // PLT. Return the offset from this PLT entry's runtime address that
  126. // should be used to compute the initial value of the GOT slot.
  127. unsigned int
  128. fill_plt_entry(unsigned char* pov,
  129. elfcpp::Elf_types<32>::Elf_Addr got_address,
  130. unsigned int got_offset,
  131. unsigned int plt_offset,
  132. unsigned int plt_rel_offset)
  133. {
  134. return this->do_fill_plt_entry(pov, got_address, got_offset,
  135. plt_offset, plt_rel_offset);
  136. }
  137. virtual unsigned int
  138. do_get_plt_entry_size() const = 0;
  139. virtual void
  140. do_fill_first_plt_entry(unsigned char* pov,
  141. elfcpp::Elf_types<32>::Elf_Addr got_address) = 0;
  142. virtual unsigned int
  143. do_fill_plt_entry(unsigned char* pov,
  144. elfcpp::Elf_types<32>::Elf_Addr got_address,
  145. unsigned int got_offset,
  146. unsigned int plt_offset,
  147. unsigned int plt_rel_offset) = 0;
  148. virtual void
  149. do_add_eh_frame(Layout*) = 0;
  150. void
  151. do_adjust_output_section(Output_section* os);
  152. // Write to a map file.
  153. void
  154. do_print_to_mapfile(Mapfile* mapfile) const
  155. { mapfile->print_output_data(this, _("** PLT")); }
  156. // The .eh_frame unwind information for the PLT.
  157. // The CIE is common across variants of the PLT format.
  158. static const int plt_eh_frame_cie_size = 16;
  159. static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
  160. private:
  161. // Set the final size.
  162. void
  163. set_final_data_size()
  164. {
  165. this->set_data_size((this->count_ + this->irelative_count_ + 1)
  166. * this->get_plt_entry_size());
  167. }
  168. // Write out the PLT data.
  169. void
  170. do_write(Output_file*);
  171. // We keep a list of global STT_GNU_IFUNC symbols, each with its
  172. // offset in the GOT.
  173. struct Global_ifunc
  174. {
  175. Symbol* sym;
  176. unsigned int got_offset;
  177. };
  178. // We keep a list of local STT_GNU_IFUNC symbols, each with its
  179. // offset in the GOT.
  180. struct Local_ifunc
  181. {
  182. Sized_relobj_file<32, false>* object;
  183. unsigned int local_sym_index;
  184. unsigned int got_offset;
  185. };
  186. // The reloc section.
  187. Reloc_section* rel_;
  188. // The TLS_DESC relocations, if necessary. These must follow the
  189. // regular PLT relocs.
  190. Reloc_section* tls_desc_rel_;
  191. // The IRELATIVE relocations, if necessary. These must follow the
  192. // regular relocatoins and the TLS_DESC relocations.
  193. Reloc_section* irelative_rel_;
  194. // The .got.plt section.
  195. Output_data_got_plt_i386* got_plt_;
  196. // The part of the .got.plt section used for IRELATIVE relocs.
  197. Output_data_space* got_irelative_;
  198. // The number of PLT entries.
  199. unsigned int count_;
  200. // Number of PLT entries with R_386_IRELATIVE relocs. These follow
  201. // the regular PLT entries.
  202. unsigned int irelative_count_;
  203. // Global STT_GNU_IFUNC symbols.
  204. std::vector<Global_ifunc> global_ifuncs_;
  205. // Local STT_GNU_IFUNC symbols.
  206. std::vector<Local_ifunc> local_ifuncs_;
  207. };
  208. // This is an abstract class for the standard PLT layout.
  209. // The derived classes below handle the actual PLT contents
  210. // for the executable (non-PIC) and shared-library (PIC) cases.
  211. // The unwind information is uniform across those two, so it's here.
  212. class Output_data_plt_i386_standard : public Output_data_plt_i386
  213. {
  214. public:
  215. Output_data_plt_i386_standard(Layout* layout,
  216. Output_data_got_plt_i386* got_plt,
  217. Output_data_space* got_irelative)
  218. : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
  219. { }
  220. protected:
  221. virtual unsigned int
  222. do_get_plt_entry_size() const
  223. { return plt_entry_size; }
  224. virtual void
  225. do_add_eh_frame(Layout* layout)
  226. {
  227. layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
  228. plt_eh_frame_fde, plt_eh_frame_fde_size);
  229. }
  230. // The size of an entry in the PLT.
  231. static const int plt_entry_size = 16;
  232. // The .eh_frame unwind information for the PLT.
  233. static const int plt_eh_frame_fde_size = 32;
  234. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  235. };
  236. // Actually fill the PLT contents for an executable (non-PIC).
  237. class Output_data_plt_i386_exec : public Output_data_plt_i386_standard
  238. {
  239. public:
  240. Output_data_plt_i386_exec(Layout* layout,
  241. Output_data_got_plt_i386* got_plt,
  242. Output_data_space* got_irelative)
  243. : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
  244. { }
  245. protected:
  246. virtual void
  247. do_fill_first_plt_entry(unsigned char* pov,
  248. elfcpp::Elf_types<32>::Elf_Addr got_address);
  249. virtual unsigned int
  250. do_fill_plt_entry(unsigned char* pov,
  251. elfcpp::Elf_types<32>::Elf_Addr got_address,
  252. unsigned int got_offset,
  253. unsigned int plt_offset,
  254. unsigned int plt_rel_offset);
  255. private:
  256. // The first entry in the PLT for an executable.
  257. static const unsigned char first_plt_entry[plt_entry_size];
  258. // Other entries in the PLT for an executable.
  259. static const unsigned char plt_entry[plt_entry_size];
  260. };
  261. // Actually fill the PLT contents for a shared library (PIC).
  262. class Output_data_plt_i386_dyn : public Output_data_plt_i386_standard
  263. {
  264. public:
  265. Output_data_plt_i386_dyn(Layout* layout,
  266. Output_data_got_plt_i386* got_plt,
  267. Output_data_space* got_irelative)
  268. : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
  269. { }
  270. protected:
  271. virtual void
  272. do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
  273. virtual unsigned int
  274. do_fill_plt_entry(unsigned char* pov,
  275. elfcpp::Elf_types<32>::Elf_Addr,
  276. unsigned int got_offset,
  277. unsigned int plt_offset,
  278. unsigned int plt_rel_offset);
  279. private:
  280. // The first entry in the PLT for a shared object.
  281. static const unsigned char first_plt_entry[plt_entry_size];
  282. // Other entries in the PLT for a shared object.
  283. static const unsigned char plt_entry[plt_entry_size];
  284. };
  285. // The i386 target class.
  286. // TLS info comes from
  287. // http://people.redhat.com/drepper/tls.pdf
  288. // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
  289. class Target_i386 : public Sized_target<32, false>
  290. {
  291. public:
  292. typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
  293. Target_i386(const Target::Target_info* info = &i386_info)
  294. : Sized_target<32, false>(info),
  295. got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
  296. got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
  297. rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY),
  298. got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
  299. { }
  300. // Process the relocations to determine unreferenced sections for
  301. // garbage collection.
  302. void
  303. gc_process_relocs(Symbol_table* symtab,
  304. Layout* layout,
  305. Sized_relobj_file<32, false>* object,
  306. unsigned int data_shndx,
  307. unsigned int sh_type,
  308. const unsigned char* prelocs,
  309. size_t reloc_count,
  310. Output_section* output_section,
  311. bool needs_special_offset_handling,
  312. size_t local_symbol_count,
  313. const unsigned char* plocal_symbols);
  314. // Scan the relocations to look for symbol adjustments.
  315. void
  316. scan_relocs(Symbol_table* symtab,
  317. Layout* layout,
  318. Sized_relobj_file<32, false>* object,
  319. unsigned int data_shndx,
  320. unsigned int sh_type,
  321. const unsigned char* prelocs,
  322. size_t reloc_count,
  323. Output_section* output_section,
  324. bool needs_special_offset_handling,
  325. size_t local_symbol_count,
  326. const unsigned char* plocal_symbols);
  327. // Finalize the sections.
  328. void
  329. do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
  330. // Return the value to use for a dynamic which requires special
  331. // treatment.
  332. uint64_t
  333. do_dynsym_value(const Symbol*) const;
  334. // Relocate a section.
  335. void
  336. relocate_section(const Relocate_info<32, false>*,
  337. unsigned int sh_type,
  338. const unsigned char* prelocs,
  339. size_t reloc_count,
  340. Output_section* output_section,
  341. bool needs_special_offset_handling,
  342. unsigned char* view,
  343. elfcpp::Elf_types<32>::Elf_Addr view_address,
  344. section_size_type view_size,
  345. const Reloc_symbol_changes*);
  346. // Scan the relocs during a relocatable link.
  347. void
  348. scan_relocatable_relocs(Symbol_table* symtab,
  349. Layout* layout,
  350. Sized_relobj_file<32, false>* object,
  351. unsigned int data_shndx,
  352. unsigned int sh_type,
  353. const unsigned char* prelocs,
  354. size_t reloc_count,
  355. Output_section* output_section,
  356. bool needs_special_offset_handling,
  357. size_t local_symbol_count,
  358. const unsigned char* plocal_symbols,
  359. Relocatable_relocs*);
  360. // Scan the relocs for --emit-relocs.
  361. void
  362. emit_relocs_scan(Symbol_table* symtab,
  363. Layout* layout,
  364. Sized_relobj_file<32, false>* object,
  365. unsigned int data_shndx,
  366. unsigned int sh_type,
  367. const unsigned char* prelocs,
  368. size_t reloc_count,
  369. Output_section* output_section,
  370. bool needs_special_offset_handling,
  371. size_t local_symbol_count,
  372. const unsigned char* plocal_syms,
  373. Relocatable_relocs* rr);
  374. // Emit relocations for a section.
  375. void
  376. relocate_relocs(const Relocate_info<32, false>*,
  377. unsigned int sh_type,
  378. const unsigned char* prelocs,
  379. size_t reloc_count,
  380. Output_section* output_section,
  381. elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
  382. unsigned char* view,
  383. elfcpp::Elf_types<32>::Elf_Addr view_address,
  384. section_size_type view_size,
  385. unsigned char* reloc_view,
  386. section_size_type reloc_view_size);
  387. // Return a string used to fill a code section with nops.
  388. std::string
  389. do_code_fill(section_size_type length) const;
  390. // Return whether SYM is defined by the ABI.
  391. bool
  392. do_is_defined_by_abi(const Symbol* sym) const
  393. { return strcmp(sym->name(), "___tls_get_addr") == 0; }
  394. // Return whether a symbol name implies a local label. The UnixWare
  395. // 2.1 cc generates temporary symbols that start with .X, so we
  396. // recognize them here. FIXME: do other SVR4 compilers also use .X?.
  397. // If so, we should move the .X recognition into
  398. // Target::do_is_local_label_name.
  399. bool
  400. do_is_local_label_name(const char* name) const
  401. {
  402. if (name[0] == '.' && name[1] == 'X')
  403. return true;
  404. return Target::do_is_local_label_name(name);
  405. }
  406. // Return the PLT address to use for a global symbol.
  407. uint64_t
  408. do_plt_address_for_global(const Symbol* gsym) const
  409. { return this->plt_section()->address_for_global(gsym); }
  410. uint64_t
  411. do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
  412. { return this->plt_section()->address_for_local(relobj, symndx); }
  413. // We can tell whether we take the address of a function.
  414. inline bool
  415. do_can_check_for_function_pointers() const
  416. { return true; }
  417. // Return the base for a DW_EH_PE_datarel encoding.
  418. uint64_t
  419. do_ehframe_datarel_base() const;
  420. // Return whether SYM is call to a non-split function.
  421. bool
  422. do_is_call_to_non_split(const Symbol* sym, const unsigned char*,
  423. const unsigned char*, section_size_type) const;
  424. // Adjust -fsplit-stack code which calls non-split-stack code.
  425. void
  426. do_calls_non_split(Relobj* object, unsigned int shndx,
  427. section_offset_type fnoffset, section_size_type fnsize,
  428. const unsigned char* prelocs, size_t reloc_count,
  429. unsigned char* view, section_size_type view_size,
  430. std::string* from, std::string* to) const;
  431. // Return the size of the GOT section.
  432. section_size_type
  433. got_size() const
  434. {
  435. gold_assert(this->got_ != NULL);
  436. return this->got_->data_size();
  437. }
  438. // Return the number of entries in the GOT.
  439. unsigned int
  440. got_entry_count() const
  441. {
  442. if (this->got_ == NULL)
  443. return 0;
  444. return this->got_size() / 4;
  445. }
  446. // Return the number of entries in the PLT.
  447. unsigned int
  448. plt_entry_count() const;
  449. // Return the offset of the first non-reserved PLT entry.
  450. unsigned int
  451. first_plt_entry_offset() const;
  452. // Return the size of each PLT entry.
  453. unsigned int
  454. plt_entry_size() const;
  455. protected:
  456. // Instantiate the plt_ member.
  457. // This chooses the right PLT flavor for an executable or a shared object.
  458. Output_data_plt_i386*
  459. make_data_plt(Layout* layout,
  460. Output_data_got_plt_i386* got_plt,
  461. Output_data_space* got_irelative,
  462. bool dyn)
  463. { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); }
  464. virtual Output_data_plt_i386*
  465. do_make_data_plt(Layout* layout,
  466. Output_data_got_plt_i386* got_plt,
  467. Output_data_space* got_irelative,
  468. bool dyn)
  469. {
  470. if (dyn)
  471. return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative);
  472. else
  473. return new Output_data_plt_i386_exec(layout, got_plt, got_irelative);
  474. }
  475. private:
  476. // The class which scans relocations.
  477. struct Scan
  478. {
  479. static inline int
  480. get_reference_flags(unsigned int r_type);
  481. inline void
  482. local(Symbol_table* symtab, Layout* layout, Target_i386* target,
  483. Sized_relobj_file<32, false>* object,
  484. unsigned int data_shndx,
  485. Output_section* output_section,
  486. const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
  487. const elfcpp::Sym<32, false>& lsym,
  488. bool is_discarded);
  489. inline void
  490. global(Symbol_table* symtab, Layout* layout, Target_i386* target,
  491. Sized_relobj_file<32, false>* object,
  492. unsigned int data_shndx,
  493. Output_section* output_section,
  494. const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
  495. Symbol* gsym);
  496. inline bool
  497. local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
  498. Target_i386* target,
  499. Sized_relobj_file<32, false>* object,
  500. unsigned int data_shndx,
  501. Output_section* output_section,
  502. const elfcpp::Rel<32, false>& reloc,
  503. unsigned int r_type,
  504. const elfcpp::Sym<32, false>& lsym);
  505. inline bool
  506. global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
  507. Target_i386* target,
  508. Sized_relobj_file<32, false>* object,
  509. unsigned int data_shndx,
  510. Output_section* output_section,
  511. const elfcpp::Rel<32, false>& reloc,
  512. unsigned int r_type,
  513. Symbol* gsym);
  514. inline bool
  515. possible_function_pointer_reloc(unsigned int r_type);
  516. bool
  517. reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
  518. unsigned int r_type);
  519. static void
  520. unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
  521. static void
  522. unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
  523. Symbol*);
  524. };
  525. // The class which implements relocation.
  526. class Relocate
  527. {
  528. public:
  529. Relocate()
  530. : skip_call_tls_get_addr_(false),
  531. local_dynamic_type_(LOCAL_DYNAMIC_NONE)
  532. { }
  533. ~Relocate()
  534. {
  535. if (this->skip_call_tls_get_addr_)
  536. {
  537. // FIXME: This needs to specify the location somehow.
  538. gold_error(_("missing expected TLS relocation"));
  539. }
  540. }
  541. // Return whether the static relocation needs to be applied.
  542. inline bool
  543. should_apply_static_reloc(const Sized_symbol<32>* gsym,
  544. unsigned int r_type,
  545. bool is_32bit,
  546. Output_section* output_section);
  547. // Do a relocation. Return false if the caller should not issue
  548. // any warnings about this relocation.
  549. inline bool
  550. relocate(const Relocate_info<32, false>*, unsigned int,
  551. Target_i386*, Output_section*, size_t, const unsigned char*,
  552. const Sized_symbol<32>*, const Symbol_value<32>*,
  553. unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
  554. section_size_type);
  555. private:
  556. // Do a TLS relocation.
  557. inline void
  558. relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
  559. size_t relnum, const elfcpp::Rel<32, false>&,
  560. unsigned int r_type, const Sized_symbol<32>*,
  561. const Symbol_value<32>*,
  562. unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
  563. section_size_type);
  564. // Do a TLS General-Dynamic to Initial-Exec transition.
  565. inline void
  566. tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
  567. const elfcpp::Rel<32, false>&, unsigned int r_type,
  568. elfcpp::Elf_types<32>::Elf_Addr value,
  569. unsigned char* view,
  570. section_size_type view_size);
  571. // Do a TLS General-Dynamic to Local-Exec transition.
  572. inline void
  573. tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
  574. Output_segment* tls_segment,
  575. const elfcpp::Rel<32, false>&, unsigned int r_type,
  576. elfcpp::Elf_types<32>::Elf_Addr value,
  577. unsigned char* view,
  578. section_size_type view_size);
  579. // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
  580. // transition.
  581. inline void
  582. tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
  583. const elfcpp::Rel<32, false>&, unsigned int r_type,
  584. elfcpp::Elf_types<32>::Elf_Addr value,
  585. unsigned char* view,
  586. section_size_type view_size);
  587. // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
  588. // transition.
  589. inline void
  590. tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
  591. Output_segment* tls_segment,
  592. const elfcpp::Rel<32, false>&, unsigned int r_type,
  593. elfcpp::Elf_types<32>::Elf_Addr value,
  594. unsigned char* view,
  595. section_size_type view_size);
  596. // Do a TLS Local-Dynamic to Local-Exec transition.
  597. inline void
  598. tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
  599. Output_segment* tls_segment,
  600. const elfcpp::Rel<32, false>&, unsigned int r_type,
  601. elfcpp::Elf_types<32>::Elf_Addr value,
  602. unsigned char* view,
  603. section_size_type view_size);
  604. // Do a TLS Initial-Exec to Local-Exec transition.
  605. static inline void
  606. tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
  607. Output_segment* tls_segment,
  608. const elfcpp::Rel<32, false>&, unsigned int r_type,
  609. elfcpp::Elf_types<32>::Elf_Addr value,
  610. unsigned char* view,
  611. section_size_type view_size);
  612. // We need to keep track of which type of local dynamic relocation
  613. // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
  614. enum Local_dynamic_type
  615. {
  616. LOCAL_DYNAMIC_NONE,
  617. LOCAL_DYNAMIC_SUN,
  618. LOCAL_DYNAMIC_GNU
  619. };
  620. // This is set if we should skip the next reloc, which should be a
  621. // PLT32 reloc against ___tls_get_addr.
  622. bool skip_call_tls_get_addr_;
  623. // The type of local dynamic relocation we have seen in the section
  624. // being relocated, if any.
  625. Local_dynamic_type local_dynamic_type_;
  626. };
  627. // A class for inquiring about properties of a relocation,
  628. // used while scanning relocs during a relocatable link and
  629. // garbage collection.
  630. class Classify_reloc :
  631. public gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
  632. {
  633. public:
  634. typedef Reloc_types<elfcpp::SHT_REL, 32, false>::Reloc Reltype;
  635. // Return the explicit addend of the relocation (return 0 for SHT_REL).
  636. static elfcpp::Elf_types<32>::Elf_Swxword
  637. get_r_addend(const Reltype*)
  638. { return 0; }
  639. // Return the size of the addend of the relocation (only used for SHT_REL).
  640. static unsigned int
  641. get_size_for_reloc(unsigned int, Relobj*);
  642. };
  643. // Adjust TLS relocation type based on the options and whether this
  644. // is a local symbol.
  645. static tls::Tls_optimization
  646. optimize_tls_reloc(bool is_final, int r_type);
  647. // Check if relocation against this symbol is a candidate for
  648. // conversion from
  649. // mov foo@GOT(%reg), %reg
  650. // to
  651. // lea foo@GOTOFF(%reg), %reg.
  652. static bool
  653. can_convert_mov_to_lea(const Symbol* gsym)
  654. {
  655. gold_assert(gsym != NULL);
  656. return (gsym->type() != elfcpp::STT_GNU_IFUNC
  657. && !gsym->is_undefined ()
  658. && !gsym->is_from_dynobj()
  659. && !gsym->is_preemptible()
  660. && (!parameters->options().shared()
  661. || (gsym->visibility() != elfcpp::STV_DEFAULT
  662. && gsym->visibility() != elfcpp::STV_PROTECTED)
  663. || parameters->options().Bsymbolic())
  664. && strcmp(gsym->name(), "_DYNAMIC") != 0);
  665. }
  666. // Get the GOT section, creating it if necessary.
  667. Output_data_got<32, false>*
  668. got_section(Symbol_table*, Layout*);
  669. // Get the GOT PLT section.
  670. Output_data_got_plt_i386*
  671. got_plt_section() const
  672. {
  673. gold_assert(this->got_plt_ != NULL);
  674. return this->got_plt_;
  675. }
  676. // Get the GOT section for TLSDESC entries.
  677. Output_data_got<32, false>*
  678. got_tlsdesc_section() const
  679. {
  680. gold_assert(this->got_tlsdesc_ != NULL);
  681. return this->got_tlsdesc_;
  682. }
  683. // Create the PLT section.
  684. void
  685. make_plt_section(Symbol_table* symtab, Layout* layout);
  686. // Create a PLT entry for a global symbol.
  687. void
  688. make_plt_entry(Symbol_table*, Layout*, Symbol*);
  689. // Create a PLT entry for a local STT_GNU_IFUNC symbol.
  690. void
  691. make_local_ifunc_plt_entry(Symbol_table*, Layout*,
  692. Sized_relobj_file<32, false>* relobj,
  693. unsigned int local_sym_index);
  694. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  695. void
  696. define_tls_base_symbol(Symbol_table*, Layout*);
  697. // Create a GOT entry for the TLS module index.
  698. unsigned int
  699. got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  700. Sized_relobj_file<32, false>* object);
  701. // Get the PLT section.
  702. Output_data_plt_i386*
  703. plt_section() const
  704. {
  705. gold_assert(this->plt_ != NULL);
  706. return this->plt_;
  707. }
  708. // Get the dynamic reloc section, creating it if necessary.
  709. Reloc_section*
  710. rel_dyn_section(Layout*);
  711. // Get the section to use for TLS_DESC relocations.
  712. Reloc_section*
  713. rel_tls_desc_section(Layout*) const;
  714. // Get the section to use for IRELATIVE relocations.
  715. Reloc_section*
  716. rel_irelative_section(Layout*);
  717. // Add a potential copy relocation.
  718. void
  719. copy_reloc(Symbol_table* symtab, Layout* layout,
  720. Sized_relobj_file<32, false>* object,
  721. unsigned int shndx, Output_section* output_section,
  722. Symbol* sym, const elfcpp::Rel<32, false>& reloc)
  723. {
  724. unsigned int r_type = elfcpp::elf_r_type<32>(reloc.get_r_info());
  725. this->copy_relocs_.copy_reloc(symtab, layout,
  726. symtab->get_sized_symbol<32>(sym),
  727. object, shndx, output_section,
  728. r_type, reloc.get_r_offset(), 0,
  729. this->rel_dyn_section(layout));
  730. }
  731. // Information about this specific target which we pass to the
  732. // general Target structure.
  733. static const Target::Target_info i386_info;
  734. // The types of GOT entries needed for this platform.
  735. // These values are exposed to the ABI in an incremental link.
  736. // Do not renumber existing values without changing the version
  737. // number of the .gnu_incremental_inputs section.
  738. enum Got_type
  739. {
  740. GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
  741. GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
  742. GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
  743. GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
  744. GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
  745. };
  746. // The GOT section.
  747. Output_data_got<32, false>* got_;
  748. // The PLT section.
  749. Output_data_plt_i386* plt_;
  750. // The GOT PLT section.
  751. Output_data_got_plt_i386* got_plt_;
  752. // The GOT section for IRELATIVE relocations.
  753. Output_data_space* got_irelative_;
  754. // The GOT section for TLSDESC relocations.
  755. Output_data_got<32, false>* got_tlsdesc_;
  756. // The _GLOBAL_OFFSET_TABLE_ symbol.
  757. Symbol* global_offset_table_;
  758. // The dynamic reloc section.
  759. Reloc_section* rel_dyn_;
  760. // The section to use for IRELATIVE relocs.
  761. Reloc_section* rel_irelative_;
  762. // Relocs saved to avoid a COPY reloc.
  763. Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
  764. // Offset of the GOT entry for the TLS module index.
  765. unsigned int got_mod_index_offset_;
  766. // True if the _TLS_MODULE_BASE_ symbol has been defined.
  767. bool tls_base_symbol_defined_;
  768. };
  769. const Target::Target_info Target_i386::i386_info =
  770. {
  771. 32, // size
  772. false, // is_big_endian
  773. elfcpp::EM_386, // machine_code
  774. false, // has_make_symbol
  775. false, // has_resolve
  776. true, // has_code_fill
  777. true, // is_default_stack_executable
  778. true, // can_icf_inline_merge_sections
  779. '\0', // wrap_char
  780. "/usr/lib/libc.so.1", // dynamic_linker
  781. 0x08048000, // default_text_segment_address
  782. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  783. 0x1000, // common_pagesize (overridable by -z common-page-size)
  784. false, // isolate_execinstr
  785. 0, // rosegment_gap
  786. elfcpp::SHN_UNDEF, // small_common_shndx
  787. elfcpp::SHN_UNDEF, // large_common_shndx
  788. 0, // small_common_section_flags
  789. 0, // large_common_section_flags
  790. NULL, // attributes_section
  791. NULL, // attributes_vendor
  792. "_start", // entry_symbol_name
  793. 32, // hash_entry_size
  794. elfcpp::SHT_PROGBITS, // unwind_section_type
  795. };
  796. // Get the GOT section, creating it if necessary.
  797. Output_data_got<32, false>*
  798. Target_i386::got_section(Symbol_table* symtab, Layout* layout)
  799. {
  800. if (this->got_ == NULL)
  801. {
  802. gold_assert(symtab != NULL && layout != NULL);
  803. this->got_ = new Output_data_got<32, false>();
  804. // When using -z now, we can treat .got.plt as a relro section.
  805. // Without -z now, it is modified after program startup by lazy
  806. // PLT relocations.
  807. bool is_got_plt_relro = parameters->options().now();
  808. Output_section_order got_order = (is_got_plt_relro
  809. ? ORDER_RELRO
  810. : ORDER_RELRO_LAST);
  811. Output_section_order got_plt_order = (is_got_plt_relro
  812. ? ORDER_RELRO
  813. : ORDER_NON_RELRO_FIRST);
  814. layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
  815. (elfcpp::SHF_ALLOC
  816. | elfcpp::SHF_WRITE),
  817. this->got_, got_order, true);
  818. this->got_plt_ = new Output_data_got_plt_i386(layout);
  819. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  820. (elfcpp::SHF_ALLOC
  821. | elfcpp::SHF_WRITE),
  822. this->got_plt_, got_plt_order,
  823. is_got_plt_relro);
  824. // The first three entries are reserved.
  825. this->got_plt_->set_current_data_size(3 * 4);
  826. if (!is_got_plt_relro)
  827. {
  828. // Those bytes can go into the relro segment.
  829. layout->increase_relro(3 * 4);
  830. }
  831. // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
  832. this->global_offset_table_ =
  833. symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
  834. Symbol_table::PREDEFINED,
  835. this->got_plt_,
  836. 0, 0, elfcpp::STT_OBJECT,
  837. elfcpp::STB_LOCAL,
  838. elfcpp::STV_HIDDEN, 0,
  839. false, false);
  840. // If there are any IRELATIVE relocations, they get GOT entries
  841. // in .got.plt after the jump slot relocations.
  842. this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
  843. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  844. (elfcpp::SHF_ALLOC
  845. | elfcpp::SHF_WRITE),
  846. this->got_irelative_,
  847. got_plt_order, is_got_plt_relro);
  848. // If there are any TLSDESC relocations, they get GOT entries in
  849. // .got.plt after the jump slot entries.
  850. this->got_tlsdesc_ = new Output_data_got<32, false>();
  851. layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
  852. (elfcpp::SHF_ALLOC
  853. | elfcpp::SHF_WRITE),
  854. this->got_tlsdesc_,
  855. got_plt_order, is_got_plt_relro);
  856. }
  857. return this->got_;
  858. }
  859. // Get the dynamic reloc section, creating it if necessary.
  860. Target_i386::Reloc_section*
  861. Target_i386::rel_dyn_section(Layout* layout)
  862. {
  863. if (this->rel_dyn_ == NULL)
  864. {
  865. gold_assert(layout != NULL);
  866. this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
  867. layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
  868. elfcpp::SHF_ALLOC, this->rel_dyn_,
  869. ORDER_DYNAMIC_RELOCS, false);
  870. }
  871. return this->rel_dyn_;
  872. }
  873. // Get the section to use for IRELATIVE relocs, creating it if
  874. // necessary. These go in .rel.dyn, but only after all other dynamic
  875. // relocations. They need to follow the other dynamic relocations so
  876. // that they can refer to global variables initialized by those
  877. // relocs.
  878. Target_i386::Reloc_section*
  879. Target_i386::rel_irelative_section(Layout* layout)
  880. {
  881. if (this->rel_irelative_ == NULL)
  882. {
  883. // Make sure we have already create the dynamic reloc section.
  884. this->rel_dyn_section(layout);
  885. this->rel_irelative_ = new Reloc_section(false);
  886. layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
  887. elfcpp::SHF_ALLOC, this->rel_irelative_,
  888. ORDER_DYNAMIC_RELOCS, false);
  889. gold_assert(this->rel_dyn_->output_section()
  890. == this->rel_irelative_->output_section());
  891. }
  892. return this->rel_irelative_;
  893. }
  894. // Write the first three reserved words of the .got.plt section.
  895. // The remainder of the section is written while writing the PLT
  896. // in Output_data_plt_i386::do_write.
  897. void
  898. Output_data_got_plt_i386::do_write(Output_file* of)
  899. {
  900. // The first entry in the GOT is the address of the .dynamic section
  901. // aka the PT_DYNAMIC segment. The next two entries are reserved.
  902. // We saved space for them when we created the section in
  903. // Target_i386::got_section.
  904. const off_t got_file_offset = this->offset();
  905. gold_assert(this->data_size() >= 12);
  906. unsigned char* const got_view = of->get_output_view(got_file_offset, 12);
  907. Output_section* dynamic = this->layout_->dynamic_section();
  908. uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
  909. elfcpp::Swap<32, false>::writeval(got_view, dynamic_addr);
  910. memset(got_view + 4, 0, 8);
  911. of->write_output_view(got_file_offset, 12, got_view);
  912. }
  913. // Create the PLT section. The ordinary .got section is an argument,
  914. // since we need to refer to the start. We also create our own .got
  915. // section just for PLT entries.
  916. Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
  917. uint64_t addralign,
  918. Output_data_got_plt_i386* got_plt,
  919. Output_data_space* got_irelative)
  920. : Output_section_data(addralign),
  921. tls_desc_rel_(NULL), irelative_rel_(NULL), got_plt_(got_plt),
  922. got_irelative_(got_irelative), count_(0), irelative_count_(0),
  923. global_ifuncs_(), local_ifuncs_()
  924. {
  925. this->rel_ = new Reloc_section(false);
  926. layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
  927. elfcpp::SHF_ALLOC, this->rel_,
  928. ORDER_DYNAMIC_PLT_RELOCS, false);
  929. }
  930. void
  931. Output_data_plt_i386::do_adjust_output_section(Output_section* os)
  932. {
  933. // UnixWare sets the entsize of .plt to 4, and so does the old GNU
  934. // linker, and so do we.
  935. os->set_entsize(4);
  936. }
  937. // Add an entry to the PLT.
  938. void
  939. Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
  940. Symbol* gsym)
  941. {
  942. gold_assert(!gsym->has_plt_offset());
  943. // Every PLT entry needs a reloc.
  944. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  945. && gsym->can_use_relative_reloc(false))
  946. {
  947. gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size());
  948. ++this->irelative_count_;
  949. section_offset_type got_offset =
  950. this->got_irelative_->current_data_size();
  951. this->got_irelative_->set_current_data_size(got_offset + 4);
  952. Reloc_section* rel = this->rel_irelative(symtab, layout);
  953. rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
  954. this->got_irelative_, got_offset);
  955. struct Global_ifunc gi;
  956. gi.sym = gsym;
  957. gi.got_offset = got_offset;
  958. this->global_ifuncs_.push_back(gi);
  959. }
  960. else
  961. {
  962. // When setting the PLT offset we skip the initial reserved PLT
  963. // entry.
  964. gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size());
  965. ++this->count_;
  966. section_offset_type got_offset = this->got_plt_->current_data_size();
  967. // Every PLT entry needs a GOT entry which points back to the
  968. // PLT entry (this will be changed by the dynamic linker,
  969. // normally lazily when the function is called).
  970. this->got_plt_->set_current_data_size(got_offset + 4);
  971. gsym->set_needs_dynsym_entry();
  972. this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
  973. got_offset);
  974. }
  975. // Note that we don't need to save the symbol. The contents of the
  976. // PLT are independent of which symbols are used. The symbols only
  977. // appear in the relocations.
  978. }
  979. // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
  980. // the PLT offset.
  981. unsigned int
  982. Output_data_plt_i386::add_local_ifunc_entry(
  983. Symbol_table* symtab,
  984. Layout* layout,
  985. Sized_relobj_file<32, false>* relobj,
  986. unsigned int local_sym_index)
  987. {
  988. unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
  989. ++this->irelative_count_;
  990. section_offset_type got_offset = this->got_irelative_->current_data_size();
  991. // Every PLT entry needs a GOT entry which points back to the PLT
  992. // entry.
  993. this->got_irelative_->set_current_data_size(got_offset + 4);
  994. // Every PLT entry needs a reloc.
  995. Reloc_section* rel = this->rel_irelative(symtab, layout);
  996. rel->add_symbolless_local_addend(relobj, local_sym_index,
  997. elfcpp::R_386_IRELATIVE,
  998. this->got_irelative_, got_offset);
  999. struct Local_ifunc li;
  1000. li.object = relobj;
  1001. li.local_sym_index = local_sym_index;
  1002. li.got_offset = got_offset;
  1003. this->local_ifuncs_.push_back(li);
  1004. return plt_offset;
  1005. }
  1006. // Return where the TLS_DESC relocations should go, creating it if
  1007. // necessary. These follow the JUMP_SLOT relocations.
  1008. Output_data_plt_i386::Reloc_section*
  1009. Output_data_plt_i386::rel_tls_desc(Layout* layout)
  1010. {
  1011. if (this->tls_desc_rel_ == NULL)
  1012. {
  1013. this->tls_desc_rel_ = new Reloc_section(false);
  1014. layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
  1015. elfcpp::SHF_ALLOC, this->tls_desc_rel_,
  1016. ORDER_DYNAMIC_PLT_RELOCS, false);
  1017. gold_assert(this->tls_desc_rel_->output_section()
  1018. == this->rel_->output_section());
  1019. }
  1020. return this->tls_desc_rel_;
  1021. }
  1022. // Return where the IRELATIVE relocations should go in the PLT. These
  1023. // follow the JUMP_SLOT and TLS_DESC relocations.
  1024. Output_data_plt_i386::Reloc_section*
  1025. Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
  1026. {
  1027. if (this->irelative_rel_ == NULL)
  1028. {
  1029. // Make sure we have a place for the TLS_DESC relocations, in
  1030. // case we see any later on.
  1031. this->rel_tls_desc(layout);
  1032. this->irelative_rel_ = new Reloc_section(false);
  1033. layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
  1034. elfcpp::SHF_ALLOC, this->irelative_rel_,
  1035. ORDER_DYNAMIC_PLT_RELOCS, false);
  1036. gold_assert(this->irelative_rel_->output_section()
  1037. == this->rel_->output_section());
  1038. if (parameters->doing_static_link())
  1039. {
  1040. // A statically linked executable will only have a .rel.plt
  1041. // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
  1042. // symbols. The library will use these symbols to locate
  1043. // the IRELATIVE relocs at program startup time.
  1044. symtab->define_in_output_data("__rel_iplt_start", NULL,
  1045. Symbol_table::PREDEFINED,
  1046. this->irelative_rel_, 0, 0,
  1047. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1048. elfcpp::STV_HIDDEN, 0, false, true);
  1049. symtab->define_in_output_data("__rel_iplt_end", NULL,
  1050. Symbol_table::PREDEFINED,
  1051. this->irelative_rel_, 0, 0,
  1052. elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  1053. elfcpp::STV_HIDDEN, 0, true, true);
  1054. }
  1055. }
  1056. return this->irelative_rel_;
  1057. }
  1058. // Return the PLT address to use for a global symbol.
  1059. uint64_t
  1060. Output_data_plt_i386::address_for_global(const Symbol* gsym)
  1061. {
  1062. uint64_t offset = 0;
  1063. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1064. && gsym->can_use_relative_reloc(false))
  1065. offset = (this->count_ + 1) * this->get_plt_entry_size();
  1066. return this->address() + offset + gsym->plt_offset();
  1067. }
  1068. // Return the PLT address to use for a local symbol. These are always
  1069. // IRELATIVE relocs.
  1070. uint64_t
  1071. Output_data_plt_i386::address_for_local(const Relobj* object,
  1072. unsigned int r_sym)
  1073. {
  1074. return (this->address()
  1075. + (this->count_ + 1) * this->get_plt_entry_size()
  1076. + object->local_plt_offset(r_sym));
  1077. }
  1078. // The first entry in the PLT for an executable.
  1079. const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] =
  1080. {
  1081. 0xff, 0x35, // pushl contents of memory address
  1082. 0, 0, 0, 0, // replaced with address of .got + 4
  1083. 0xff, 0x25, // jmp indirect
  1084. 0, 0, 0, 0, // replaced with address of .got + 8
  1085. 0, 0, 0, 0 // unused
  1086. };
  1087. void
  1088. Output_data_plt_i386_exec::do_fill_first_plt_entry(
  1089. unsigned char* pov,
  1090. elfcpp::Elf_types<32>::Elf_Addr got_address)
  1091. {
  1092. memcpy(pov, first_plt_entry, plt_entry_size);
  1093. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
  1094. elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
  1095. }
  1096. // The first entry in the PLT for a shared object.
  1097. const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] =
  1098. {
  1099. 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
  1100. 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
  1101. 0, 0, 0, 0 // unused
  1102. };
  1103. void
  1104. Output_data_plt_i386_dyn::do_fill_first_plt_entry(
  1105. unsigned char* pov,
  1106. elfcpp::Elf_types<32>::Elf_Addr)
  1107. {
  1108. memcpy(pov, first_plt_entry, plt_entry_size);
  1109. }
  1110. // Subsequent entries in the PLT for an executable.
  1111. const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] =
  1112. {
  1113. 0xff, 0x25, // jmp indirect
  1114. 0, 0, 0, 0, // replaced with address of symbol in .got
  1115. 0x68, // pushl immediate
  1116. 0, 0, 0, 0, // replaced with offset into relocation table
  1117. 0xe9, // jmp relative
  1118. 0, 0, 0, 0 // replaced with offset to start of .plt
  1119. };
  1120. unsigned int
  1121. Output_data_plt_i386_exec::do_fill_plt_entry(
  1122. unsigned char* pov,
  1123. elfcpp::Elf_types<32>::Elf_Addr got_address,
  1124. unsigned int got_offset,
  1125. unsigned int plt_offset,
  1126. unsigned int plt_rel_offset)
  1127. {
  1128. memcpy(pov, plt_entry, plt_entry_size);
  1129. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  1130. got_address + got_offset);
  1131. elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
  1132. elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
  1133. return 6;
  1134. }
  1135. // Subsequent entries in the PLT for a shared object.
  1136. const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] =
  1137. {
  1138. 0xff, 0xa3, // jmp *offset(%ebx)
  1139. 0, 0, 0, 0, // replaced with offset of symbol in .got
  1140. 0x68, // pushl immediate
  1141. 0, 0, 0, 0, // replaced with offset into relocation table
  1142. 0xe9, // jmp relative
  1143. 0, 0, 0, 0 // replaced with offset to start of .plt
  1144. };
  1145. unsigned int
  1146. Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov,
  1147. elfcpp::Elf_types<32>::Elf_Addr,
  1148. unsigned int got_offset,
  1149. unsigned int plt_offset,
  1150. unsigned int plt_rel_offset)
  1151. {
  1152. memcpy(pov, plt_entry, plt_entry_size);
  1153. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
  1154. elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
  1155. elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
  1156. return 6;
  1157. }
  1158. // The .eh_frame unwind information for the PLT.
  1159. const unsigned char
  1160. Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
  1161. {
  1162. 1, // CIE version.
  1163. 'z', // Augmentation: augmentation size included.
  1164. 'R', // Augmentation: FDE encoding included.
  1165. '\0', // End of augmentation string.
  1166. 1, // Code alignment factor.
  1167. 0x7c, // Data alignment factor.
  1168. 8, // Return address column.
  1169. 1, // Augmentation size.
  1170. (elfcpp::DW_EH_PE_pcrel // FDE encoding.
  1171. | elfcpp::DW_EH_PE_sdata4),
  1172. elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
  1173. elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
  1174. elfcpp::DW_CFA_nop, // Align to 16 bytes.
  1175. elfcpp::DW_CFA_nop
  1176. };
  1177. const unsigned char
  1178. Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  1179. {
  1180. 0, 0, 0, 0, // Replaced with offset to .plt.
  1181. 0, 0, 0, 0, // Replaced with size of .plt.
  1182. 0, // Augmentation size.
  1183. elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
  1184. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  1185. elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
  1186. elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
  1187. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  1188. 11, // Block length.
  1189. elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
  1190. elfcpp::DW_OP_breg8, 0, // Push %eip.
  1191. elfcpp::DW_OP_lit15, // Push 0xf.
  1192. elfcpp::DW_OP_and, // & (%eip & 0xf).
  1193. elfcpp::DW_OP_lit11, // Push 0xb.
  1194. elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
  1195. elfcpp::DW_OP_lit2, // Push 2.
  1196. elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
  1197. elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
  1198. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  1199. elfcpp::DW_CFA_nop,
  1200. elfcpp::DW_CFA_nop,
  1201. elfcpp::DW_CFA_nop
  1202. };
  1203. // Write out the PLT. This uses the hand-coded instructions above,
  1204. // and adjusts them as needed. This is all specified by the i386 ELF
  1205. // Processor Supplement.
  1206. void
  1207. Output_data_plt_i386::do_write(Output_file* of)
  1208. {
  1209. const off_t offset = this->offset();
  1210. const section_size_type oview_size =
  1211. convert_to_section_size_type(this->data_size());
  1212. unsigned char* const oview = of->get_output_view(offset, oview_size);
  1213. const off_t got_file_offset = this->got_plt_->offset();
  1214. gold_assert(parameters->incremental_update()
  1215. || (got_file_offset + this->got_plt_->data_size()
  1216. == this->got_irelative_->offset()));
  1217. const section_size_type got_size =
  1218. convert_to_section_size_type(this->got_plt_->data_size()
  1219. + this->got_irelative_->data_size());
  1220. unsigned char* const got_view = of->get_output_view(got_file_offset,
  1221. got_size);
  1222. unsigned char* pov = oview;
  1223. elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
  1224. elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
  1225. this->fill_first_plt_entry(pov, got_address);
  1226. pov += this->get_plt_entry_size();
  1227. // The first three entries in the GOT are reserved, and are written
  1228. // by Output_data_got_plt_i386::do_write.
  1229. unsigned char* got_pov = got_view + 12;
  1230. const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
  1231. unsigned int plt_offset = this->get_plt_entry_size();
  1232. unsigned int plt_rel_offset = 0;
  1233. unsigned int got_offset = 12;
  1234. const unsigned int count = this->count_ + this->irelative_count_;
  1235. for (unsigned int i = 0;
  1236. i < count;
  1237. ++i,
  1238. pov += this->get_plt_entry_size(),
  1239. got_pov += 4,
  1240. plt_offset += this->get_plt_entry_size(),
  1241. plt_rel_offset += rel_size,
  1242. got_offset += 4)
  1243. {
  1244. // Set and adjust the PLT entry itself.
  1245. unsigned int lazy_offset = this->fill_plt_entry(pov,
  1246. got_address,
  1247. got_offset,
  1248. plt_offset,
  1249. plt_rel_offset);
  1250. // Set the entry in the GOT.
  1251. elfcpp::Swap<32, false>::writeval(got_pov,
  1252. plt_address + plt_offset + lazy_offset);
  1253. }
  1254. // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
  1255. // the GOT to point to the actual symbol value, rather than point to
  1256. // the PLT entry. That will let the dynamic linker call the right
  1257. // function when resolving IRELATIVE relocations.
  1258. unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
  1259. for (std::vector<Global_ifunc>::const_iterator p =
  1260. this->global_ifuncs_.begin();
  1261. p != this->global_ifuncs_.end();
  1262. ++p)
  1263. {
  1264. const Sized_symbol<32>* ssym =
  1265. static_cast<const Sized_symbol<32>*>(p->sym);
  1266. elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
  1267. ssym->value());
  1268. }
  1269. for (std::vector<Local_ifunc>::const_iterator p =
  1270. this->local_ifuncs_.begin();
  1271. p != this->local_ifuncs_.end();
  1272. ++p)
  1273. {
  1274. const Symbol_value<32>* psymval =
  1275. p->object->local_symbol(p->local_sym_index);
  1276. elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
  1277. psymval->value(p->object, 0));
  1278. }
  1279. gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
  1280. gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
  1281. of->write_output_view(offset, oview_size, oview);
  1282. of->write_output_view(got_file_offset, got_size, got_view);
  1283. }
  1284. // Create the PLT section.
  1285. void
  1286. Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
  1287. {
  1288. if (this->plt_ == NULL)
  1289. {
  1290. // Create the GOT sections first.
  1291. this->got_section(symtab, layout);
  1292. const bool dyn = parameters->options().output_is_position_independent();
  1293. this->plt_ = this->make_data_plt(layout,
  1294. this->got_plt_,
  1295. this->got_irelative_,
  1296. dyn);
  1297. // Add unwind information if requested.
  1298. if (parameters->options().ld_generated_unwind_info())
  1299. this->plt_->add_eh_frame(layout);
  1300. layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
  1301. (elfcpp::SHF_ALLOC
  1302. | elfcpp::SHF_EXECINSTR),
  1303. this->plt_, ORDER_PLT, false);
  1304. // Make the sh_info field of .rel.plt point to .plt.
  1305. Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
  1306. rel_plt_os->set_info_section(this->plt_->output_section());
  1307. }
  1308. }
  1309. // Create a PLT entry for a global symbol.
  1310. void
  1311. Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
  1312. {
  1313. if (gsym->has_plt_offset())
  1314. return;
  1315. if (this->plt_ == NULL)
  1316. this->make_plt_section(symtab, layout);
  1317. this->plt_->add_entry(symtab, layout, gsym);
  1318. }
  1319. // Make a PLT entry for a local STT_GNU_IFUNC symbol.
  1320. void
  1321. Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
  1322. Sized_relobj_file<32, false>* relobj,
  1323. unsigned int local_sym_index)
  1324. {
  1325. if (relobj->local_has_plt_offset(local_sym_index))
  1326. return;
  1327. if (this->plt_ == NULL)
  1328. this->make_plt_section(symtab, layout);
  1329. unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
  1330. relobj,
  1331. local_sym_index);
  1332. relobj->set_local_plt_offset(local_sym_index, plt_offset);
  1333. }
  1334. // Return the number of entries in the PLT.
  1335. unsigned int
  1336. Target_i386::plt_entry_count() const
  1337. {
  1338. if (this->plt_ == NULL)
  1339. return 0;
  1340. return this->plt_->entry_count();
  1341. }
  1342. // Return the offset of the first non-reserved PLT entry.
  1343. unsigned int
  1344. Target_i386::first_plt_entry_offset() const
  1345. {
  1346. if (this->plt_ == NULL)
  1347. return 0;
  1348. return this->plt_->first_plt_entry_offset();
  1349. }
  1350. // Return the size of each PLT entry.
  1351. unsigned int
  1352. Target_i386::plt_entry_size() const
  1353. {
  1354. if (this->plt_ == NULL)
  1355. return 0;
  1356. return this->plt_->get_plt_entry_size();
  1357. }
  1358. // Get the section to use for TLS_DESC relocations.
  1359. Target_i386::Reloc_section*
  1360. Target_i386::rel_tls_desc_section(Layout* layout) const
  1361. {
  1362. return this->plt_section()->rel_tls_desc(layout);
  1363. }
  1364. // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
  1365. void
  1366. Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
  1367. {
  1368. if (this->tls_base_symbol_defined_)
  1369. return;
  1370. Output_segment* tls_segment = layout->tls_segment();
  1371. if (tls_segment != NULL)
  1372. {
  1373. bool is_exec = parameters->options().output_is_executable();
  1374. symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
  1375. Symbol_table::PREDEFINED,
  1376. tls_segment, 0, 0,
  1377. elfcpp::STT_TLS,
  1378. elfcpp::STB_LOCAL,
  1379. elfcpp::STV_HIDDEN, 0,
  1380. (is_exec
  1381. ? Symbol::SEGMENT_END
  1382. : Symbol::SEGMENT_START),
  1383. true);
  1384. }
  1385. this->tls_base_symbol_defined_ = true;
  1386. }
  1387. // Create a GOT entry for the TLS module index.
  1388. unsigned int
  1389. Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
  1390. Sized_relobj_file<32, false>* object)
  1391. {
  1392. if (this->got_mod_index_offset_ == -1U)
  1393. {
  1394. gold_assert(symtab != NULL && layout != NULL && object != NULL);
  1395. Reloc_section* rel_dyn = this->rel_dyn_section(layout);
  1396. Output_data_got<32, false>* got = this->got_section(symtab, layout);
  1397. unsigned int got_offset = got->add_constant(0);
  1398. rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
  1399. got_offset);
  1400. got->add_constant(0);
  1401. this->got_mod_index_offset_ = got_offset;
  1402. }
  1403. return this->got_mod_index_offset_;
  1404. }
  1405. // Optimize the TLS relocation type based on what we know about the
  1406. // symbol. IS_FINAL is true if the final address of this symbol is
  1407. // known at link time.
  1408. tls::Tls_optimization
  1409. Target_i386::optimize_tls_reloc(bool is_final, int r_type)
  1410. {
  1411. // If we are generating a shared library, then we can't do anything
  1412. // in the linker.
  1413. if (parameters->options().shared())
  1414. return tls::TLSOPT_NONE;
  1415. switch (r_type)
  1416. {
  1417. case elfcpp::R_386_TLS_GD:
  1418. case elfcpp::R_386_TLS_GOTDESC:
  1419. case elfcpp::R_386_TLS_DESC_CALL:
  1420. // These are General-Dynamic which permits fully general TLS
  1421. // access. Since we know that we are generating an executable,
  1422. // we can convert this to Initial-Exec. If we also know that
  1423. // this is a local symbol, we can further switch to Local-Exec.
  1424. if (is_final)
  1425. return tls::TLSOPT_TO_LE;
  1426. return tls::TLSOPT_TO_IE;
  1427. case elfcpp::R_386_TLS_LDM:
  1428. // This is Local-Dynamic, which refers to a local symbol in the
  1429. // dynamic TLS block. Since we know that we generating an
  1430. // executable, we can switch to Local-Exec.
  1431. return tls::TLSOPT_TO_LE;
  1432. case elfcpp::R_386_TLS_LDO_32:
  1433. // Another type of Local-Dynamic relocation.
  1434. return tls::TLSOPT_TO_LE;
  1435. case elfcpp::R_386_TLS_IE:
  1436. case elfcpp::R_386_TLS_GOTIE:
  1437. case elfcpp::R_386_TLS_IE_32:
  1438. // These are Initial-Exec relocs which get the thread offset
  1439. // from the GOT. If we know that we are linking against the
  1440. // local symbol, we can switch to Local-Exec, which links the
  1441. // thread offset into the instruction.
  1442. if (is_final)
  1443. return tls::TLSOPT_TO_LE;
  1444. return tls::TLSOPT_NONE;
  1445. case elfcpp::R_386_TLS_LE:
  1446. case elfcpp::R_386_TLS_LE_32:
  1447. // When we already have Local-Exec, there is nothing further we
  1448. // can do.
  1449. return tls::TLSOPT_NONE;
  1450. default:
  1451. gold_unreachable();
  1452. }
  1453. }
  1454. // Get the Reference_flags for a particular relocation.
  1455. int
  1456. Target_i386::Scan::get_reference_flags(unsigned int r_type)
  1457. {
  1458. switch (r_type)
  1459. {
  1460. case elfcpp::R_386_NONE:
  1461. case elfcpp::R_386_GNU_VTINHERIT:
  1462. case elfcpp::R_386_GNU_VTENTRY:
  1463. case elfcpp::R_386_GOTPC:
  1464. // No symbol reference.
  1465. return 0;
  1466. case elfcpp::R_386_32:
  1467. case elfcpp::R_386_16:
  1468. case elfcpp::R_386_8:
  1469. return Symbol::ABSOLUTE_REF;
  1470. case elfcpp::R_386_PC32:
  1471. case elfcpp::R_386_PC16:
  1472. case elfcpp::R_386_PC8:
  1473. case elfcpp::R_386_GOTOFF:
  1474. return Symbol::RELATIVE_REF;
  1475. case elfcpp::R_386_PLT32:
  1476. return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
  1477. case elfcpp::R_386_GOT32:
  1478. case elfcpp::R_386_GOT32X:
  1479. // Absolute in GOT.
  1480. return Symbol::ABSOLUTE_REF;
  1481. case elfcpp::R_386_TLS_GD: // Global-dynamic
  1482. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
  1483. case elfcpp::R_386_TLS_DESC_CALL:
  1484. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  1485. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  1486. case elfcpp::R_386_TLS_IE: // Initial-exec
  1487. case elfcpp::R_386_TLS_IE_32:
  1488. case elfcpp::R_386_TLS_GOTIE:
  1489. case elfcpp::R_386_TLS_LE: // Local-exec
  1490. case elfcpp::R_386_TLS_LE_32:
  1491. return Symbol::TLS_REF;
  1492. case elfcpp::R_386_COPY:
  1493. case elfcpp::R_386_GLOB_DAT:
  1494. case elfcpp::R_386_JUMP_SLOT:
  1495. case elfcpp::R_386_RELATIVE:
  1496. case elfcpp::R_386_IRELATIVE:
  1497. case elfcpp::R_386_TLS_TPOFF:
  1498. case elfcpp::R_386_TLS_DTPMOD32:
  1499. case elfcpp::R_386_TLS_DTPOFF32:
  1500. case elfcpp::R_386_TLS_TPOFF32:
  1501. case elfcpp::R_386_TLS_DESC:
  1502. case elfcpp::R_386_32PLT:
  1503. case elfcpp::R_386_TLS_GD_32:
  1504. case elfcpp::R_386_TLS_GD_PUSH:
  1505. case elfcpp::R_386_TLS_GD_CALL:
  1506. case elfcpp::R_386_TLS_GD_POP:
  1507. case elfcpp::R_386_TLS_LDM_32:
  1508. case elfcpp::R_386_TLS_LDM_PUSH:
  1509. case elfcpp::R_386_TLS_LDM_CALL:
  1510. case elfcpp::R_386_TLS_LDM_POP:
  1511. case elfcpp::R_386_USED_BY_INTEL_200:
  1512. default:
  1513. // Not expected. We will give an error later.
  1514. return 0;
  1515. }
  1516. }
  1517. // Report an unsupported relocation against a local symbol.
  1518. void
  1519. Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
  1520. unsigned int r_type)
  1521. {
  1522. gold_error(_("%s: unsupported reloc %u against local symbol"),
  1523. object->name().c_str(), r_type);
  1524. }
  1525. // Return whether we need to make a PLT entry for a relocation of a
  1526. // given type against a STT_GNU_IFUNC symbol.
  1527. bool
  1528. Target_i386::Scan::reloc_needs_plt_for_ifunc(
  1529. Sized_relobj_file<32, false>* object,
  1530. unsigned int r_type)
  1531. {
  1532. int flags = Scan::get_reference_flags(r_type);
  1533. if (flags & Symbol::TLS_REF)
  1534. gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
  1535. object->name().c_str(), r_type);
  1536. return flags != 0;
  1537. }
  1538. // Scan a relocation for a local symbol.
  1539. inline void
  1540. Target_i386::Scan::local(Symbol_table* symtab,
  1541. Layout* layout,
  1542. Target_i386* target,
  1543. Sized_relobj_file<32, false>* object,
  1544. unsigned int data_shndx,
  1545. Output_section* output_section,
  1546. const elfcpp::Rel<32, false>& reloc,
  1547. unsigned int r_type,
  1548. const elfcpp::Sym<32, false>& lsym,
  1549. bool is_discarded)
  1550. {
  1551. if (is_discarded)
  1552. return;
  1553. // A local STT_GNU_IFUNC symbol may require a PLT entry.
  1554. if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
  1555. && this->reloc_needs_plt_for_ifunc(object, r_type))
  1556. {
  1557. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1558. target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
  1559. }
  1560. switch (r_type)
  1561. {
  1562. case elfcpp::R_386_NONE:
  1563. case elfcpp::R_386_GNU_VTINHERIT:
  1564. case elfcpp::R_386_GNU_VTENTRY:
  1565. break;
  1566. case elfcpp::R_386_32:
  1567. // If building a shared library (or a position-independent
  1568. // executable), we need to create a dynamic relocation for
  1569. // this location. The relocation applied at link time will
  1570. // apply the link-time value, so we flag the location with
  1571. // an R_386_RELATIVE relocation so the dynamic loader can
  1572. // relocate it easily.
  1573. if (parameters->options().output_is_position_independent())
  1574. {
  1575. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1576. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1577. rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
  1578. output_section, data_shndx,
  1579. reloc.get_r_offset());
  1580. }
  1581. break;
  1582. case elfcpp::R_386_16:
  1583. case elfcpp::R_386_8:
  1584. // If building a shared library (or a position-independent
  1585. // executable), we need to create a dynamic relocation for
  1586. // this location. Because the addend needs to remain in the
  1587. // data section, we need to be careful not to apply this
  1588. // relocation statically.
  1589. if (parameters->options().output_is_position_independent())
  1590. {
  1591. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1592. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1593. if (lsym.get_st_type() != elfcpp::STT_SECTION)
  1594. rel_dyn->add_local(object, r_sym, r_type, output_section,
  1595. data_shndx, reloc.get_r_offset());
  1596. else
  1597. {
  1598. gold_assert(lsym.get_st_value() == 0);
  1599. unsigned int shndx = lsym.get_st_shndx();
  1600. bool is_ordinary;
  1601. shndx = object->adjust_sym_shndx(r_sym, shndx,
  1602. &is_ordinary);
  1603. if (!is_ordinary)
  1604. object->error(_("section symbol %u has bad shndx %u"),
  1605. r_sym, shndx);
  1606. else
  1607. rel_dyn->add_local_section(object, shndx,
  1608. r_type, output_section,
  1609. data_shndx, reloc.get_r_offset());
  1610. }
  1611. }
  1612. break;
  1613. case elfcpp::R_386_PC32:
  1614. case elfcpp::R_386_PC16:
  1615. case elfcpp::R_386_PC8:
  1616. break;
  1617. case elfcpp::R_386_PLT32:
  1618. // Since we know this is a local symbol, we can handle this as a
  1619. // PC32 reloc.
  1620. break;
  1621. case elfcpp::R_386_GOTOFF:
  1622. case elfcpp::R_386_GOTPC:
  1623. // We need a GOT section.
  1624. target->got_section(symtab, layout);
  1625. break;
  1626. case elfcpp::R_386_GOT32:
  1627. case elfcpp::R_386_GOT32X:
  1628. {
  1629. // We need GOT section.
  1630. Output_data_got<32, false>* got = target->got_section(symtab, layout);
  1631. // If the relocation symbol isn't IFUNC,
  1632. // and is local, then we will convert
  1633. // mov foo@GOT(%reg), %reg
  1634. // to
  1635. // lea foo@GOTOFF(%reg), %reg
  1636. // in Relocate::relocate.
  1637. if (reloc.get_r_offset() >= 2
  1638. && lsym.get_st_type() != elfcpp::STT_GNU_IFUNC)
  1639. {
  1640. section_size_type stype;
  1641. const unsigned char* view = object->section_contents(data_shndx,
  1642. &stype, true);
  1643. if (view[reloc.get_r_offset() - 2] == 0x8b)
  1644. break;
  1645. }
  1646. // Otherwise, the symbol requires a GOT entry.
  1647. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1648. // For a STT_GNU_IFUNC symbol we want the PLT offset. That
  1649. // lets function pointers compare correctly with shared
  1650. // libraries. Otherwise we would need an IRELATIVE reloc.
  1651. bool is_new;
  1652. if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
  1653. is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
  1654. else
  1655. is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
  1656. if (is_new)
  1657. {
  1658. // If we are generating a shared object, we need to add a
  1659. // dynamic RELATIVE relocation for this symbol's GOT entry.
  1660. if (parameters->options().output_is_position_independent())
  1661. {
  1662. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1663. unsigned int got_offset =
  1664. object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
  1665. rel_dyn->add_local_relative(object, r_sym,
  1666. elfcpp::R_386_RELATIVE,
  1667. got, got_offset);
  1668. }
  1669. }
  1670. }
  1671. break;
  1672. // These are relocations which should only be seen by the
  1673. // dynamic linker, and should never be seen here.
  1674. case elfcpp::R_386_COPY:
  1675. case elfcpp::R_386_GLOB_DAT:
  1676. case elfcpp::R_386_JUMP_SLOT:
  1677. case elfcpp::R_386_RELATIVE:
  1678. case elfcpp::R_386_IRELATIVE:
  1679. case elfcpp::R_386_TLS_TPOFF:
  1680. case elfcpp::R_386_TLS_DTPMOD32:
  1681. case elfcpp::R_386_TLS_DTPOFF32:
  1682. case elfcpp::R_386_TLS_TPOFF32:
  1683. case elfcpp::R_386_TLS_DESC:
  1684. gold_error(_("%s: unexpected reloc %u in object file"),
  1685. object->name().c_str(), r_type);
  1686. break;
  1687. // These are initial TLS relocs, which are expected when
  1688. // linking.
  1689. case elfcpp::R_386_TLS_GD: // Global-dynamic
  1690. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
  1691. case elfcpp::R_386_TLS_DESC_CALL:
  1692. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  1693. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  1694. case elfcpp::R_386_TLS_IE: // Initial-exec
  1695. case elfcpp::R_386_TLS_IE_32:
  1696. case elfcpp::R_386_TLS_GOTIE:
  1697. case elfcpp::R_386_TLS_LE: // Local-exec
  1698. case elfcpp::R_386_TLS_LE_32:
  1699. {
  1700. bool output_is_shared = parameters->options().shared();
  1701. const tls::Tls_optimization optimized_type
  1702. = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
  1703. switch (r_type)
  1704. {
  1705. case elfcpp::R_386_TLS_GD: // Global-dynamic
  1706. if (optimized_type == tls::TLSOPT_NONE)
  1707. {
  1708. // Create a pair of GOT entries for the module index and
  1709. // dtv-relative offset.
  1710. Output_data_got<32, false>* got
  1711. = target->got_section(symtab, layout);
  1712. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1713. unsigned int shndx = lsym.get_st_shndx();
  1714. bool is_ordinary;
  1715. shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
  1716. if (!is_ordinary)
  1717. object->error(_("local symbol %u has bad shndx %u"),
  1718. r_sym, shndx);
  1719. else
  1720. got->add_local_pair_with_rel(object, r_sym, shndx,
  1721. GOT_TYPE_TLS_PAIR,
  1722. target->rel_dyn_section(layout),
  1723. elfcpp::R_386_TLS_DTPMOD32);
  1724. }
  1725. else if (optimized_type != tls::TLSOPT_TO_LE)
  1726. unsupported_reloc_local(object, r_type);
  1727. break;
  1728. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
  1729. target->define_tls_base_symbol(symtab, layout);
  1730. if (optimized_type == tls::TLSOPT_NONE)
  1731. {
  1732. // Create a double GOT entry with an R_386_TLS_DESC
  1733. // reloc. The R_386_TLS_DESC reloc is resolved
  1734. // lazily, so the GOT entry needs to be in an area in
  1735. // .got.plt, not .got. Call got_section to make sure
  1736. // the section has been created.
  1737. target->got_section(symtab, layout);
  1738. Output_data_got<32, false>* got = target->got_tlsdesc_section();
  1739. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1740. if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
  1741. {
  1742. unsigned int got_offset = got->add_constant(0);
  1743. // The local symbol value is stored in the second
  1744. // GOT entry.
  1745. got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
  1746. // That set the GOT offset of the local symbol to
  1747. // point to the second entry, but we want it to
  1748. // point to the first.
  1749. object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
  1750. got_offset);
  1751. Reloc_section* rt = target->rel_tls_desc_section(layout);
  1752. rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
  1753. }
  1754. }
  1755. else if (optimized_type != tls::TLSOPT_TO_LE)
  1756. unsupported_reloc_local(object, r_type);
  1757. break;
  1758. case elfcpp::R_386_TLS_DESC_CALL:
  1759. break;
  1760. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  1761. if (optimized_type == tls::TLSOPT_NONE)
  1762. {
  1763. // Create a GOT entry for the module index.
  1764. target->got_mod_index_entry(symtab, layout, object);
  1765. }
  1766. else if (optimized_type != tls::TLSOPT_TO_LE)
  1767. unsupported_reloc_local(object, r_type);
  1768. break;
  1769. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  1770. break;
  1771. case elfcpp::R_386_TLS_IE: // Initial-exec
  1772. case elfcpp::R_386_TLS_IE_32:
  1773. case elfcpp::R_386_TLS_GOTIE:
  1774. layout->set_has_static_tls();
  1775. if (optimized_type == tls::TLSOPT_NONE)
  1776. {
  1777. // For the R_386_TLS_IE relocation, we need to create a
  1778. // dynamic relocation when building a shared library.
  1779. if (r_type == elfcpp::R_386_TLS_IE
  1780. && parameters->options().shared())
  1781. {
  1782. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1783. unsigned int r_sym
  1784. = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1785. rel_dyn->add_local_relative(object, r_sym,
  1786. elfcpp::R_386_RELATIVE,
  1787. output_section, data_shndx,
  1788. reloc.get_r_offset());
  1789. }
  1790. // Create a GOT entry for the tp-relative offset.
  1791. Output_data_got<32, false>* got
  1792. = target->got_section(symtab, layout);
  1793. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1794. unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
  1795. ? elfcpp::R_386_TLS_TPOFF32
  1796. : elfcpp::R_386_TLS_TPOFF);
  1797. unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
  1798. ? GOT_TYPE_TLS_OFFSET
  1799. : GOT_TYPE_TLS_NOFFSET);
  1800. got->add_local_with_rel(object, r_sym, got_type,
  1801. target->rel_dyn_section(layout),
  1802. dyn_r_type);
  1803. }
  1804. else if (optimized_type != tls::TLSOPT_TO_LE)
  1805. unsupported_reloc_local(object, r_type);
  1806. break;
  1807. case elfcpp::R_386_TLS_LE: // Local-exec
  1808. case elfcpp::R_386_TLS_LE_32:
  1809. layout->set_has_static_tls();
  1810. if (output_is_shared)
  1811. {
  1812. // We need to create a dynamic relocation.
  1813. gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
  1814. unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
  1815. unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
  1816. ? elfcpp::R_386_TLS_TPOFF32
  1817. : elfcpp::R_386_TLS_TPOFF);
  1818. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1819. rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
  1820. data_shndx, reloc.get_r_offset());
  1821. }
  1822. break;
  1823. default:
  1824. gold_unreachable();
  1825. }
  1826. }
  1827. break;
  1828. case elfcpp::R_386_32PLT:
  1829. case elfcpp::R_386_TLS_GD_32:
  1830. case elfcpp::R_386_TLS_GD_PUSH:
  1831. case elfcpp::R_386_TLS_GD_CALL:
  1832. case elfcpp::R_386_TLS_GD_POP:
  1833. case elfcpp::R_386_TLS_LDM_32:
  1834. case elfcpp::R_386_TLS_LDM_PUSH:
  1835. case elfcpp::R_386_TLS_LDM_CALL:
  1836. case elfcpp::R_386_TLS_LDM_POP:
  1837. case elfcpp::R_386_USED_BY_INTEL_200:
  1838. default:
  1839. unsupported_reloc_local(object, r_type);
  1840. break;
  1841. }
  1842. }
  1843. // Report an unsupported relocation against a global symbol.
  1844. void
  1845. Target_i386::Scan::unsupported_reloc_global(
  1846. Sized_relobj_file<32, false>* object,
  1847. unsigned int r_type,
  1848. Symbol* gsym)
  1849. {
  1850. gold_error(_("%s: unsupported reloc %u against global symbol %s"),
  1851. object->name().c_str(), r_type, gsym->demangled_name().c_str());
  1852. }
  1853. inline bool
  1854. Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
  1855. {
  1856. switch (r_type)
  1857. {
  1858. case elfcpp::R_386_32:
  1859. case elfcpp::R_386_16:
  1860. case elfcpp::R_386_8:
  1861. case elfcpp::R_386_GOTOFF:
  1862. case elfcpp::R_386_GOT32:
  1863. case elfcpp::R_386_GOT32X:
  1864. {
  1865. return true;
  1866. }
  1867. default:
  1868. return false;
  1869. }
  1870. return false;
  1871. }
  1872. inline bool
  1873. Target_i386::Scan::local_reloc_may_be_function_pointer(
  1874. Symbol_table* ,
  1875. Layout* ,
  1876. Target_i386* ,
  1877. Sized_relobj_file<32, false>* ,
  1878. unsigned int ,
  1879. Output_section* ,
  1880. const elfcpp::Rel<32, false>& ,
  1881. unsigned int r_type,
  1882. const elfcpp::Sym<32, false>&)
  1883. {
  1884. return possible_function_pointer_reloc(r_type);
  1885. }
  1886. inline bool
  1887. Target_i386::Scan::global_reloc_may_be_function_pointer(
  1888. Symbol_table* ,
  1889. Layout* ,
  1890. Target_i386* ,
  1891. Sized_relobj_file<32, false>* ,
  1892. unsigned int ,
  1893. Output_section* ,
  1894. const elfcpp::Rel<32, false>& ,
  1895. unsigned int r_type,
  1896. Symbol*)
  1897. {
  1898. return possible_function_pointer_reloc(r_type);
  1899. }
  1900. // Scan a relocation for a global symbol.
  1901. inline void
  1902. Target_i386::Scan::global(Symbol_table* symtab,
  1903. Layout* layout,
  1904. Target_i386* target,
  1905. Sized_relobj_file<32, false>* object,
  1906. unsigned int data_shndx,
  1907. Output_section* output_section,
  1908. const elfcpp::Rel<32, false>& reloc,
  1909. unsigned int r_type,
  1910. Symbol* gsym)
  1911. {
  1912. // A STT_GNU_IFUNC symbol may require a PLT entry.
  1913. if (gsym->type() == elfcpp::STT_GNU_IFUNC
  1914. && this->reloc_needs_plt_for_ifunc(object, r_type))
  1915. target->make_plt_entry(symtab, layout, gsym);
  1916. switch (r_type)
  1917. {
  1918. case elfcpp::R_386_NONE:
  1919. case elfcpp::R_386_GNU_VTINHERIT:
  1920. case elfcpp::R_386_GNU_VTENTRY:
  1921. break;
  1922. case elfcpp::R_386_32:
  1923. case elfcpp::R_386_16:
  1924. case elfcpp::R_386_8:
  1925. {
  1926. // Make a PLT entry if necessary.
  1927. if (gsym->needs_plt_entry())
  1928. {
  1929. target->make_plt_entry(symtab, layout, gsym);
  1930. // Since this is not a PC-relative relocation, we may be
  1931. // taking the address of a function. In that case we need to
  1932. // set the entry in the dynamic symbol table to the address of
  1933. // the PLT entry.
  1934. if (gsym->is_from_dynobj() && !parameters->options().shared())
  1935. gsym->set_needs_dynsym_value();
  1936. }
  1937. // Make a dynamic relocation if necessary.
  1938. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  1939. {
  1940. if (!parameters->options().output_is_position_independent()
  1941. && gsym->may_need_copy_reloc())
  1942. {
  1943. target->copy_reloc(symtab, layout, object,
  1944. data_shndx, output_section, gsym, reloc);
  1945. }
  1946. else if (r_type == elfcpp::R_386_32
  1947. && gsym->type() == elfcpp::STT_GNU_IFUNC
  1948. && gsym->can_use_relative_reloc(false)
  1949. && !gsym->is_from_dynobj()
  1950. && !gsym->is_undefined()
  1951. && !gsym->is_preemptible())
  1952. {
  1953. // Use an IRELATIVE reloc for a locally defined
  1954. // STT_GNU_IFUNC symbol. This makes a function
  1955. // address in a PIE executable match the address in a
  1956. // shared library that it links against.
  1957. Reloc_section* rel_dyn = target->rel_irelative_section(layout);
  1958. rel_dyn->add_symbolless_global_addend(gsym,
  1959. elfcpp::R_386_IRELATIVE,
  1960. output_section,
  1961. object, data_shndx,
  1962. reloc.get_r_offset());
  1963. }
  1964. else if (r_type == elfcpp::R_386_32
  1965. && gsym->can_use_relative_reloc(false))
  1966. {
  1967. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1968. rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
  1969. output_section, object,
  1970. data_shndx, reloc.get_r_offset());
  1971. }
  1972. else
  1973. {
  1974. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  1975. rel_dyn->add_global(gsym, r_type, output_section, object,
  1976. data_shndx, reloc.get_r_offset());
  1977. }
  1978. }
  1979. }
  1980. break;
  1981. case elfcpp::R_386_PC32:
  1982. case elfcpp::R_386_PC16:
  1983. case elfcpp::R_386_PC8:
  1984. {
  1985. // Make a PLT entry if necessary.
  1986. if (gsym->needs_plt_entry())
  1987. {
  1988. // These relocations are used for function calls only in
  1989. // non-PIC code. For a 32-bit relocation in a shared library,
  1990. // we'll need a text relocation anyway, so we can skip the
  1991. // PLT entry and let the dynamic linker bind the call directly
  1992. // to the target. For smaller relocations, we should use a
  1993. // PLT entry to ensure that the call can reach.
  1994. if (!parameters->options().shared()
  1995. || r_type != elfcpp::R_386_PC32)
  1996. target->make_plt_entry(symtab, layout, gsym);
  1997. }
  1998. // Make a dynamic relocation if necessary.
  1999. if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
  2000. {
  2001. if (parameters->options().output_is_executable()
  2002. && gsym->may_need_copy_reloc())
  2003. {
  2004. target->copy_reloc(symtab, layout, object,
  2005. data_shndx, output_section, gsym, reloc);
  2006. }
  2007. else
  2008. {
  2009. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  2010. rel_dyn->add_global(gsym, r_type, output_section, object,
  2011. data_shndx, reloc.get_r_offset());
  2012. }
  2013. }
  2014. }
  2015. break;
  2016. case elfcpp::R_386_GOT32:
  2017. case elfcpp::R_386_GOT32X:
  2018. {
  2019. // The symbol requires a GOT section.
  2020. Output_data_got<32, false>* got = target->got_section(symtab, layout);
  2021. // If we convert this from
  2022. // mov foo@GOT(%reg), %reg
  2023. // to
  2024. // lea foo@GOTOFF(%reg), %reg
  2025. // in Relocate::relocate, then there is nothing to do here.
  2026. if (reloc.get_r_offset() >= 2
  2027. && Target_i386::can_convert_mov_to_lea(gsym))
  2028. {
  2029. section_size_type stype;
  2030. const unsigned char* view = object->section_contents(data_shndx,
  2031. &stype, true);
  2032. if (view[reloc.get_r_offset() - 2] == 0x8b)
  2033. break;
  2034. }
  2035. if (gsym->final_value_is_known())
  2036. {
  2037. // For a STT_GNU_IFUNC symbol we want the PLT address.
  2038. if (gsym->type() == elfcpp::STT_GNU_IFUNC)
  2039. got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  2040. else
  2041. got->add_global(gsym, GOT_TYPE_STANDARD);
  2042. }
  2043. else
  2044. {
  2045. // If this symbol is not fully resolved, we need to add a
  2046. // GOT entry with a dynamic relocation.
  2047. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  2048. // Use a GLOB_DAT rather than a RELATIVE reloc if:
  2049. //
  2050. // 1) The symbol may be defined in some other module.
  2051. //
  2052. // 2) We are building a shared library and this is a
  2053. // protected symbol; using GLOB_DAT means that the dynamic
  2054. // linker can use the address of the PLT in the main
  2055. // executable when appropriate so that function address
  2056. // comparisons work.
  2057. //
  2058. // 3) This is a STT_GNU_IFUNC symbol in position dependent
  2059. // code, again so that function address comparisons work.
  2060. if (gsym->is_from_dynobj()
  2061. || gsym->is_undefined()
  2062. || gsym->is_preemptible()
  2063. || (gsym->visibility() == elfcpp::STV_PROTECTED
  2064. && parameters->options().shared())
  2065. || (gsym->type() == elfcpp::STT_GNU_IFUNC
  2066. && parameters->options().output_is_position_independent()))
  2067. got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
  2068. rel_dyn, elfcpp::R_386_GLOB_DAT);
  2069. else
  2070. {
  2071. // For a STT_GNU_IFUNC symbol we want to write the PLT
  2072. // offset into the GOT, so that function pointer
  2073. // comparisons work correctly.
  2074. bool is_new;
  2075. if (gsym->type() != elfcpp::STT_GNU_IFUNC)
  2076. is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
  2077. else
  2078. {
  2079. is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
  2080. // Tell the dynamic linker to use the PLT address
  2081. // when resolving relocations.
  2082. if (gsym->is_from_dynobj()
  2083. && !parameters->options().shared())
  2084. gsym->set_needs_dynsym_value();
  2085. }
  2086. if (is_new)
  2087. {
  2088. unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
  2089. rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
  2090. got, got_off);
  2091. }
  2092. }
  2093. }
  2094. }
  2095. break;
  2096. case elfcpp::R_386_PLT32:
  2097. // If the symbol is fully resolved, this is just a PC32 reloc.
  2098. // Otherwise we need a PLT entry.
  2099. if (gsym->final_value_is_known())
  2100. break;
  2101. // If building a shared library, we can also skip the PLT entry
  2102. // if the symbol is defined in the output file and is protected
  2103. // or hidden.
  2104. if (gsym->is_defined()
  2105. && !gsym->is_from_dynobj()
  2106. && !gsym->is_preemptible())
  2107. break;
  2108. target->make_plt_entry(symtab, layout, gsym);
  2109. break;
  2110. case elfcpp::R_386_GOTOFF:
  2111. // A GOT-relative reference must resolve locally.
  2112. if (!gsym->is_defined())
  2113. gold_error(_("%s: relocation R_386_GOTOFF against undefined symbol %s"
  2114. " cannot be used when making a shared object"),
  2115. object->name().c_str(), gsym->name());
  2116. else if (gsym->is_from_dynobj())
  2117. gold_error(_("%s: relocation R_386_GOTOFF against external symbol %s"
  2118. " cannot be used when making a shared object"),
  2119. object->name().c_str(), gsym->name());
  2120. else if (gsym->is_preemptible())
  2121. gold_error(_("%s: relocation R_386_GOTOFF against preemptible symbol %s"
  2122. " cannot be used when making a shared object"),
  2123. object->name().c_str(), gsym->name());
  2124. // We need a GOT section.
  2125. target->got_section(symtab, layout);
  2126. break;
  2127. case elfcpp::R_386_GOTPC:
  2128. // We need a GOT section.
  2129. target->got_section(symtab, layout);
  2130. break;
  2131. // These are relocations which should only be seen by the
  2132. // dynamic linker, and should never be seen here.
  2133. case elfcpp::R_386_COPY:
  2134. case elfcpp::R_386_GLOB_DAT:
  2135. case elfcpp::R_386_JUMP_SLOT:
  2136. case elfcpp::R_386_RELATIVE:
  2137. case elfcpp::R_386_IRELATIVE:
  2138. case elfcpp::R_386_TLS_TPOFF:
  2139. case elfcpp::R_386_TLS_DTPMOD32:
  2140. case elfcpp::R_386_TLS_DTPOFF32:
  2141. case elfcpp::R_386_TLS_TPOFF32:
  2142. case elfcpp::R_386_TLS_DESC:
  2143. gold_error(_("%s: unexpected reloc %u in object file"),
  2144. object->name().c_str(), r_type);
  2145. break;
  2146. // These are initial tls relocs, which are expected when
  2147. // linking.
  2148. case elfcpp::R_386_TLS_GD: // Global-dynamic
  2149. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
  2150. case elfcpp::R_386_TLS_DESC_CALL:
  2151. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  2152. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  2153. case elfcpp::R_386_TLS_IE: // Initial-exec
  2154. case elfcpp::R_386_TLS_IE_32:
  2155. case elfcpp::R_386_TLS_GOTIE:
  2156. case elfcpp::R_386_TLS_LE: // Local-exec
  2157. case elfcpp::R_386_TLS_LE_32:
  2158. {
  2159. const bool is_final = gsym->final_value_is_known();
  2160. const tls::Tls_optimization optimized_type
  2161. = Target_i386::optimize_tls_reloc(is_final, r_type);
  2162. switch (r_type)
  2163. {
  2164. case elfcpp::R_386_TLS_GD: // Global-dynamic
  2165. if (optimized_type == tls::TLSOPT_NONE)
  2166. {
  2167. // Create a pair of GOT entries for the module index and
  2168. // dtv-relative offset.
  2169. Output_data_got<32, false>* got
  2170. = target->got_section(symtab, layout);
  2171. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
  2172. target->rel_dyn_section(layout),
  2173. elfcpp::R_386_TLS_DTPMOD32,
  2174. elfcpp::R_386_TLS_DTPOFF32);
  2175. }
  2176. else if (optimized_type == tls::TLSOPT_TO_IE)
  2177. {
  2178. // Create a GOT entry for the tp-relative offset.
  2179. Output_data_got<32, false>* got
  2180. = target->got_section(symtab, layout);
  2181. got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
  2182. target->rel_dyn_section(layout),
  2183. elfcpp::R_386_TLS_TPOFF);
  2184. }
  2185. else if (optimized_type != tls::TLSOPT_TO_LE)
  2186. unsupported_reloc_global(object, r_type, gsym);
  2187. break;
  2188. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
  2189. target->define_tls_base_symbol(symtab, layout);
  2190. if (optimized_type == tls::TLSOPT_NONE)
  2191. {
  2192. // Create a double GOT entry with an R_386_TLS_DESC
  2193. // reloc. The R_386_TLS_DESC reloc is resolved
  2194. // lazily, so the GOT entry needs to be in an area in
  2195. // .got.plt, not .got. Call got_section to make sure
  2196. // the section has been created.
  2197. target->got_section(symtab, layout);
  2198. Output_data_got<32, false>* got = target->got_tlsdesc_section();
  2199. Reloc_section* rt = target->rel_tls_desc_section(layout);
  2200. got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
  2201. elfcpp::R_386_TLS_DESC, 0);
  2202. }
  2203. else if (optimized_type == tls::TLSOPT_TO_IE)
  2204. {
  2205. // Create a GOT entry for the tp-relative offset.
  2206. Output_data_got<32, false>* got
  2207. = target->got_section(symtab, layout);
  2208. got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
  2209. target->rel_dyn_section(layout),
  2210. elfcpp::R_386_TLS_TPOFF);
  2211. }
  2212. else if (optimized_type != tls::TLSOPT_TO_LE)
  2213. unsupported_reloc_global(object, r_type, gsym);
  2214. break;
  2215. case elfcpp::R_386_TLS_DESC_CALL:
  2216. break;
  2217. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  2218. if (optimized_type == tls::TLSOPT_NONE)
  2219. {
  2220. // Create a GOT entry for the module index.
  2221. target->got_mod_index_entry(symtab, layout, object);
  2222. }
  2223. else if (optimized_type != tls::TLSOPT_TO_LE)
  2224. unsupported_reloc_global(object, r_type, gsym);
  2225. break;
  2226. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  2227. break;
  2228. case elfcpp::R_386_TLS_IE: // Initial-exec
  2229. case elfcpp::R_386_TLS_IE_32:
  2230. case elfcpp::R_386_TLS_GOTIE:
  2231. layout->set_has_static_tls();
  2232. if (optimized_type == tls::TLSOPT_NONE)
  2233. {
  2234. // For the R_386_TLS_IE relocation, we need to create a
  2235. // dynamic relocation when building a shared library.
  2236. if (r_type == elfcpp::R_386_TLS_IE
  2237. && parameters->options().shared())
  2238. {
  2239. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  2240. rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
  2241. output_section, object,
  2242. data_shndx,
  2243. reloc.get_r_offset());
  2244. }
  2245. // Create a GOT entry for the tp-relative offset.
  2246. Output_data_got<32, false>* got
  2247. = target->got_section(symtab, layout);
  2248. unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
  2249. ? elfcpp::R_386_TLS_TPOFF32
  2250. : elfcpp::R_386_TLS_TPOFF);
  2251. unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
  2252. ? GOT_TYPE_TLS_OFFSET
  2253. : GOT_TYPE_TLS_NOFFSET);
  2254. got->add_global_with_rel(gsym, got_type,
  2255. target->rel_dyn_section(layout),
  2256. dyn_r_type);
  2257. }
  2258. else if (optimized_type != tls::TLSOPT_TO_LE)
  2259. unsupported_reloc_global(object, r_type, gsym);
  2260. break;
  2261. case elfcpp::R_386_TLS_LE: // Local-exec
  2262. case elfcpp::R_386_TLS_LE_32:
  2263. layout->set_has_static_tls();
  2264. if (parameters->options().shared())
  2265. {
  2266. // We need to create a dynamic relocation.
  2267. unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
  2268. ? elfcpp::R_386_TLS_TPOFF32
  2269. : elfcpp::R_386_TLS_TPOFF);
  2270. Reloc_section* rel_dyn = target->rel_dyn_section(layout);
  2271. rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
  2272. data_shndx, reloc.get_r_offset());
  2273. }
  2274. break;
  2275. default:
  2276. gold_unreachable();
  2277. }
  2278. }
  2279. break;
  2280. case elfcpp::R_386_32PLT:
  2281. case elfcpp::R_386_TLS_GD_32:
  2282. case elfcpp::R_386_TLS_GD_PUSH:
  2283. case elfcpp::R_386_TLS_GD_CALL:
  2284. case elfcpp::R_386_TLS_GD_POP:
  2285. case elfcpp::R_386_TLS_LDM_32:
  2286. case elfcpp::R_386_TLS_LDM_PUSH:
  2287. case elfcpp::R_386_TLS_LDM_CALL:
  2288. case elfcpp::R_386_TLS_LDM_POP:
  2289. case elfcpp::R_386_USED_BY_INTEL_200:
  2290. default:
  2291. unsupported_reloc_global(object, r_type, gsym);
  2292. break;
  2293. }
  2294. }
  2295. // Process relocations for gc.
  2296. void
  2297. Target_i386::gc_process_relocs(Symbol_table* symtab,
  2298. Layout* layout,
  2299. Sized_relobj_file<32, false>* object,
  2300. unsigned int data_shndx,
  2301. unsigned int,
  2302. const unsigned char* prelocs,
  2303. size_t reloc_count,
  2304. Output_section* output_section,
  2305. bool needs_special_offset_handling,
  2306. size_t local_symbol_count,
  2307. const unsigned char* plocal_symbols)
  2308. {
  2309. gold::gc_process_relocs<32, false, Target_i386, Scan, Classify_reloc>(
  2310. symtab,
  2311. layout,
  2312. this,
  2313. object,
  2314. data_shndx,
  2315. prelocs,
  2316. reloc_count,
  2317. output_section,
  2318. needs_special_offset_handling,
  2319. local_symbol_count,
  2320. plocal_symbols);
  2321. }
  2322. // Scan relocations for a section.
  2323. void
  2324. Target_i386::scan_relocs(Symbol_table* symtab,
  2325. Layout* layout,
  2326. Sized_relobj_file<32, false>* object,
  2327. unsigned int data_shndx,
  2328. unsigned int sh_type,
  2329. const unsigned char* prelocs,
  2330. size_t reloc_count,
  2331. Output_section* output_section,
  2332. bool needs_special_offset_handling,
  2333. size_t local_symbol_count,
  2334. const unsigned char* plocal_symbols)
  2335. {
  2336. if (sh_type == elfcpp::SHT_RELA)
  2337. {
  2338. gold_error(_("%s: unsupported RELA reloc section"),
  2339. object->name().c_str());
  2340. return;
  2341. }
  2342. gold::scan_relocs<32, false, Target_i386, Scan, Classify_reloc>(
  2343. symtab,
  2344. layout,
  2345. this,
  2346. object,
  2347. data_shndx,
  2348. prelocs,
  2349. reloc_count,
  2350. output_section,
  2351. needs_special_offset_handling,
  2352. local_symbol_count,
  2353. plocal_symbols);
  2354. }
  2355. // Finalize the sections.
  2356. void
  2357. Target_i386::do_finalize_sections(
  2358. Layout* layout,
  2359. const Input_objects*,
  2360. Symbol_table* symtab)
  2361. {
  2362. const Reloc_section* rel_plt = (this->plt_ == NULL
  2363. ? NULL
  2364. : this->plt_->rel_plt());
  2365. layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
  2366. this->rel_dyn_, true, false);
  2367. // Emit any relocs we saved in an attempt to avoid generating COPY
  2368. // relocs.
  2369. if (this->copy_relocs_.any_saved_relocs())
  2370. this->copy_relocs_.emit(this->rel_dyn_section(layout));
  2371. // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
  2372. // the .got.plt section.
  2373. Symbol* sym = this->global_offset_table_;
  2374. if (sym != NULL)
  2375. {
  2376. uint32_t data_size = this->got_plt_->current_data_size();
  2377. symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
  2378. }
  2379. if (parameters->doing_static_link()
  2380. && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
  2381. {
  2382. // If linking statically, make sure that the __rel_iplt symbols
  2383. // were defined if necessary, even if we didn't create a PLT.
  2384. static const Define_symbol_in_segment syms[] =
  2385. {
  2386. {
  2387. "__rel_iplt_start", // name
  2388. elfcpp::PT_LOAD, // segment_type
  2389. elfcpp::PF_W, // segment_flags_set
  2390. elfcpp::PF(0), // segment_flags_clear
  2391. 0, // value
  2392. 0, // size
  2393. elfcpp::STT_NOTYPE, // type
  2394. elfcpp::STB_GLOBAL, // binding
  2395. elfcpp::STV_HIDDEN, // visibility
  2396. 0, // nonvis
  2397. Symbol::SEGMENT_START, // offset_from_base
  2398. true // only_if_ref
  2399. },
  2400. {
  2401. "__rel_iplt_end", // name
  2402. elfcpp::PT_LOAD, // segment_type
  2403. elfcpp::PF_W, // segment_flags_set
  2404. elfcpp::PF(0), // segment_flags_clear
  2405. 0, // value
  2406. 0, // size
  2407. elfcpp::STT_NOTYPE, // type
  2408. elfcpp::STB_GLOBAL, // binding
  2409. elfcpp::STV_HIDDEN, // visibility
  2410. 0, // nonvis
  2411. Symbol::SEGMENT_START, // offset_from_base
  2412. true // only_if_ref
  2413. }
  2414. };
  2415. symtab->define_symbols(layout, 2, syms,
  2416. layout->script_options()->saw_sections_clause());
  2417. }
  2418. }
  2419. // Return whether a direct absolute static relocation needs to be applied.
  2420. // In cases where Scan::local() or Scan::global() has created
  2421. // a dynamic relocation other than R_386_RELATIVE, the addend
  2422. // of the relocation is carried in the data, and we must not
  2423. // apply the static relocation.
  2424. inline bool
  2425. Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
  2426. unsigned int r_type,
  2427. bool is_32bit,
  2428. Output_section* output_section)
  2429. {
  2430. // If the output section is not allocated, then we didn't call
  2431. // scan_relocs, we didn't create a dynamic reloc, and we must apply
  2432. // the reloc here.
  2433. if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
  2434. return true;
  2435. int ref_flags = Scan::get_reference_flags(r_type);
  2436. // For local symbols, we will have created a non-RELATIVE dynamic
  2437. // relocation only if (a) the output is position independent,
  2438. // (b) the relocation is absolute (not pc- or segment-relative), and
  2439. // (c) the relocation is not 32 bits wide.
  2440. if (gsym == NULL)
  2441. return !(parameters->options().output_is_position_independent()
  2442. && (ref_flags & Symbol::ABSOLUTE_REF)
  2443. && !is_32bit);
  2444. // For global symbols, we use the same helper routines used in the
  2445. // scan pass. If we did not create a dynamic relocation, or if we
  2446. // created a RELATIVE dynamic relocation, we should apply the static
  2447. // relocation.
  2448. bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
  2449. bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
  2450. && gsym->can_use_relative_reloc(ref_flags
  2451. & Symbol::FUNCTION_CALL);
  2452. return !has_dyn || is_rel;
  2453. }
  2454. // Perform a relocation.
  2455. inline bool
  2456. Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
  2457. unsigned int,
  2458. Target_i386* target,
  2459. Output_section* output_section,
  2460. size_t relnum,
  2461. const unsigned char* preloc,
  2462. const Sized_symbol<32>* gsym,
  2463. const Symbol_value<32>* psymval,
  2464. unsigned char* view,
  2465. elfcpp::Elf_types<32>::Elf_Addr address,
  2466. section_size_type view_size)
  2467. {
  2468. const elfcpp::Rel<32, false> rel(preloc);
  2469. unsigned int r_type = elfcpp::elf_r_type<32>(rel.get_r_info());
  2470. if (this->skip_call_tls_get_addr_)
  2471. {
  2472. if ((r_type != elfcpp::R_386_PLT32
  2473. && r_type != elfcpp::R_386_GOT32X
  2474. && r_type != elfcpp::R_386_PC32)
  2475. || gsym == NULL
  2476. || strcmp(gsym->name(), "___tls_get_addr") != 0)
  2477. {
  2478. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2479. _("missing expected TLS relocation"));
  2480. this->skip_call_tls_get_addr_ = false;
  2481. }
  2482. else
  2483. {
  2484. this->skip_call_tls_get_addr_ = false;
  2485. return false;
  2486. }
  2487. }
  2488. if (view == NULL)
  2489. return true;
  2490. const Sized_relobj_file<32, false>* object = relinfo->object;
  2491. // Pick the value to use for symbols defined in shared objects.
  2492. Symbol_value<32> symval;
  2493. if (gsym != NULL
  2494. && gsym->type() == elfcpp::STT_GNU_IFUNC
  2495. && r_type == elfcpp::R_386_32
  2496. && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
  2497. && gsym->can_use_relative_reloc(false)
  2498. && !gsym->is_from_dynobj()
  2499. && !gsym->is_undefined()
  2500. && !gsym->is_preemptible())
  2501. {
  2502. // In this case we are generating a R_386_IRELATIVE reloc. We
  2503. // want to use the real value of the symbol, not the PLT offset.
  2504. }
  2505. else if (gsym != NULL
  2506. && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
  2507. {
  2508. symval.set_output_value(target->plt_address_for_global(gsym));
  2509. psymval = &symval;
  2510. }
  2511. else if (gsym == NULL && psymval->is_ifunc_symbol())
  2512. {
  2513. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  2514. if (object->local_has_plt_offset(r_sym))
  2515. {
  2516. symval.set_output_value(target->plt_address_for_local(object, r_sym));
  2517. psymval = &symval;
  2518. }
  2519. }
  2520. bool baseless;
  2521. switch (r_type)
  2522. {
  2523. case elfcpp::R_386_NONE:
  2524. case elfcpp::R_386_GNU_VTINHERIT:
  2525. case elfcpp::R_386_GNU_VTENTRY:
  2526. break;
  2527. case elfcpp::R_386_32:
  2528. if (should_apply_static_reloc(gsym, r_type, true, output_section))
  2529. Relocate_functions<32, false>::rel32(view, object, psymval);
  2530. break;
  2531. case elfcpp::R_386_PC32:
  2532. if (should_apply_static_reloc(gsym, r_type, true, output_section))
  2533. Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
  2534. break;
  2535. case elfcpp::R_386_16:
  2536. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  2537. Relocate_functions<32, false>::rel16(view, object, psymval);
  2538. break;
  2539. case elfcpp::R_386_PC16:
  2540. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  2541. Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
  2542. break;
  2543. case elfcpp::R_386_8:
  2544. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  2545. Relocate_functions<32, false>::rel8(view, object, psymval);
  2546. break;
  2547. case elfcpp::R_386_PC8:
  2548. if (should_apply_static_reloc(gsym, r_type, false, output_section))
  2549. Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
  2550. break;
  2551. case elfcpp::R_386_PLT32:
  2552. gold_assert(gsym == NULL
  2553. || gsym->has_plt_offset()
  2554. || gsym->final_value_is_known()
  2555. || (gsym->is_defined()
  2556. && !gsym->is_from_dynobj()
  2557. && !gsym->is_preemptible()));
  2558. Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
  2559. break;
  2560. case elfcpp::R_386_GOT32:
  2561. case elfcpp::R_386_GOT32X:
  2562. baseless = (view[-1] & 0xc7) == 0x5;
  2563. // R_386_GOT32 and R_386_GOT32X don't work without base register
  2564. // when generating a position-independent output file.
  2565. if (baseless
  2566. && parameters->options().output_is_position_independent())
  2567. {
  2568. if(gsym)
  2569. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2570. _("unexpected reloc %u against global symbol %s without base register in object file when generating a position-independent output file"),
  2571. r_type, gsym->demangled_name().c_str());
  2572. else
  2573. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2574. _("unexpected reloc %u against local symbol without base register in object file when generating a position-independent output file"),
  2575. r_type);
  2576. }
  2577. // Convert
  2578. // mov foo@GOT(%reg), %reg
  2579. // to
  2580. // lea foo@GOTOFF(%reg), %reg
  2581. // if possible.
  2582. if (rel.get_r_offset() >= 2
  2583. && view[-2] == 0x8b
  2584. && ((gsym == NULL && !psymval->is_ifunc_symbol())
  2585. || (gsym != NULL
  2586. && Target_i386::can_convert_mov_to_lea(gsym))))
  2587. {
  2588. view[-2] = 0x8d;
  2589. elfcpp::Elf_types<32>::Elf_Addr value;
  2590. value = psymval->value(object, 0);
  2591. // Don't subtract the .got.plt section address for baseless
  2592. // addressing.
  2593. if (!baseless)
  2594. value -= target->got_plt_section()->address();
  2595. Relocate_functions<32, false>::rel32(view, value);
  2596. }
  2597. else
  2598. {
  2599. // The GOT pointer points to the end of the GOT section.
  2600. // We need to subtract the size of the GOT section to get
  2601. // the actual offset to use in the relocation.
  2602. unsigned int got_offset = 0;
  2603. if (gsym != NULL)
  2604. {
  2605. gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
  2606. got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
  2607. - target->got_size());
  2608. }
  2609. else
  2610. {
  2611. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  2612. gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
  2613. got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
  2614. - target->got_size());
  2615. }
  2616. // Add the .got.plt section address for baseless addressing.
  2617. if (baseless)
  2618. got_offset += target->got_plt_section()->address();
  2619. Relocate_functions<32, false>::rel32(view, got_offset);
  2620. }
  2621. break;
  2622. case elfcpp::R_386_GOTOFF:
  2623. {
  2624. elfcpp::Elf_types<32>::Elf_Addr reladdr;
  2625. reladdr = target->got_plt_section()->address();
  2626. Relocate_functions<32, false>::pcrel32(view, object, psymval, reladdr);
  2627. }
  2628. break;
  2629. case elfcpp::R_386_GOTPC:
  2630. {
  2631. elfcpp::Elf_types<32>::Elf_Addr value;
  2632. value = target->got_plt_section()->address();
  2633. Relocate_functions<32, false>::pcrel32(view, value, address);
  2634. }
  2635. break;
  2636. case elfcpp::R_386_COPY:
  2637. case elfcpp::R_386_GLOB_DAT:
  2638. case elfcpp::R_386_JUMP_SLOT:
  2639. case elfcpp::R_386_RELATIVE:
  2640. case elfcpp::R_386_IRELATIVE:
  2641. // These are outstanding tls relocs, which are unexpected when
  2642. // linking.
  2643. case elfcpp::R_386_TLS_TPOFF:
  2644. case elfcpp::R_386_TLS_DTPMOD32:
  2645. case elfcpp::R_386_TLS_DTPOFF32:
  2646. case elfcpp::R_386_TLS_TPOFF32:
  2647. case elfcpp::R_386_TLS_DESC:
  2648. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2649. _("unexpected reloc %u in object file"),
  2650. r_type);
  2651. break;
  2652. // These are initial tls relocs, which are expected when
  2653. // linking.
  2654. case elfcpp::R_386_TLS_GD: // Global-dynamic
  2655. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
  2656. case elfcpp::R_386_TLS_DESC_CALL:
  2657. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  2658. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  2659. case elfcpp::R_386_TLS_IE: // Initial-exec
  2660. case elfcpp::R_386_TLS_IE_32:
  2661. case elfcpp::R_386_TLS_GOTIE:
  2662. case elfcpp::R_386_TLS_LE: // Local-exec
  2663. case elfcpp::R_386_TLS_LE_32:
  2664. this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
  2665. view, address, view_size);
  2666. break;
  2667. case elfcpp::R_386_32PLT:
  2668. case elfcpp::R_386_TLS_GD_32:
  2669. case elfcpp::R_386_TLS_GD_PUSH:
  2670. case elfcpp::R_386_TLS_GD_CALL:
  2671. case elfcpp::R_386_TLS_GD_POP:
  2672. case elfcpp::R_386_TLS_LDM_32:
  2673. case elfcpp::R_386_TLS_LDM_PUSH:
  2674. case elfcpp::R_386_TLS_LDM_CALL:
  2675. case elfcpp::R_386_TLS_LDM_POP:
  2676. case elfcpp::R_386_USED_BY_INTEL_200:
  2677. default:
  2678. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2679. _("unsupported reloc %u"),
  2680. r_type);
  2681. break;
  2682. }
  2683. return true;
  2684. }
  2685. // Perform a TLS relocation.
  2686. inline void
  2687. Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
  2688. Target_i386* target,
  2689. size_t relnum,
  2690. const elfcpp::Rel<32, false>& rel,
  2691. unsigned int r_type,
  2692. const Sized_symbol<32>* gsym,
  2693. const Symbol_value<32>* psymval,
  2694. unsigned char* view,
  2695. elfcpp::Elf_types<32>::Elf_Addr,
  2696. section_size_type view_size)
  2697. {
  2698. Output_segment* tls_segment = relinfo->layout->tls_segment();
  2699. const Sized_relobj_file<32, false>* object = relinfo->object;
  2700. elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
  2701. const bool is_final = (gsym == NULL
  2702. ? !parameters->options().shared()
  2703. : gsym->final_value_is_known());
  2704. const tls::Tls_optimization optimized_type
  2705. = Target_i386::optimize_tls_reloc(is_final, r_type);
  2706. switch (r_type)
  2707. {
  2708. case elfcpp::R_386_TLS_GD: // Global-dynamic
  2709. if (optimized_type == tls::TLSOPT_TO_LE)
  2710. {
  2711. if (tls_segment == NULL)
  2712. {
  2713. gold_assert(parameters->errors()->error_count() > 0
  2714. || issue_undefined_symbol_error(gsym));
  2715. return;
  2716. }
  2717. this->tls_gd_to_le(relinfo, relnum, tls_segment,
  2718. rel, r_type, value, view,
  2719. view_size);
  2720. break;
  2721. }
  2722. else
  2723. {
  2724. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  2725. ? GOT_TYPE_TLS_NOFFSET
  2726. : GOT_TYPE_TLS_PAIR);
  2727. unsigned int got_offset;
  2728. if (gsym != NULL)
  2729. {
  2730. gold_assert(gsym->has_got_offset(got_type));
  2731. got_offset = gsym->got_offset(got_type) - target->got_size();
  2732. }
  2733. else
  2734. {
  2735. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  2736. gold_assert(object->local_has_got_offset(r_sym, got_type));
  2737. got_offset = (object->local_got_offset(r_sym, got_type)
  2738. - target->got_size());
  2739. }
  2740. if (optimized_type == tls::TLSOPT_TO_IE)
  2741. {
  2742. this->tls_gd_to_ie(relinfo, relnum, rel, r_type,
  2743. got_offset, view, view_size);
  2744. break;
  2745. }
  2746. else if (optimized_type == tls::TLSOPT_NONE)
  2747. {
  2748. // Relocate the field with the offset of the pair of GOT
  2749. // entries.
  2750. Relocate_functions<32, false>::rel32(view, got_offset);
  2751. break;
  2752. }
  2753. }
  2754. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2755. _("unsupported reloc %u"),
  2756. r_type);
  2757. break;
  2758. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
  2759. case elfcpp::R_386_TLS_DESC_CALL:
  2760. this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
  2761. if (optimized_type == tls::TLSOPT_TO_LE)
  2762. {
  2763. if (tls_segment == NULL)
  2764. {
  2765. gold_assert(parameters->errors()->error_count() > 0
  2766. || issue_undefined_symbol_error(gsym));
  2767. return;
  2768. }
  2769. this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
  2770. rel, r_type, value, view,
  2771. view_size);
  2772. break;
  2773. }
  2774. else
  2775. {
  2776. unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
  2777. ? GOT_TYPE_TLS_NOFFSET
  2778. : GOT_TYPE_TLS_DESC);
  2779. unsigned int got_offset = 0;
  2780. if (r_type == elfcpp::R_386_TLS_GOTDESC
  2781. && optimized_type == tls::TLSOPT_NONE)
  2782. {
  2783. // We created GOT entries in the .got.tlsdesc portion of
  2784. // the .got.plt section, but the offset stored in the
  2785. // symbol is the offset within .got.tlsdesc.
  2786. got_offset = (target->got_size()
  2787. + target->got_plt_section()->data_size());
  2788. }
  2789. if (gsym != NULL)
  2790. {
  2791. gold_assert(gsym->has_got_offset(got_type));
  2792. got_offset += gsym->got_offset(got_type) - target->got_size();
  2793. }
  2794. else
  2795. {
  2796. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  2797. gold_assert(object->local_has_got_offset(r_sym, got_type));
  2798. got_offset += (object->local_got_offset(r_sym, got_type)
  2799. - target->got_size());
  2800. }
  2801. if (optimized_type == tls::TLSOPT_TO_IE)
  2802. {
  2803. this->tls_desc_gd_to_ie(relinfo, relnum, rel, r_type,
  2804. got_offset, view, view_size);
  2805. break;
  2806. }
  2807. else if (optimized_type == tls::TLSOPT_NONE)
  2808. {
  2809. if (r_type == elfcpp::R_386_TLS_GOTDESC)
  2810. {
  2811. // Relocate the field with the offset of the pair of GOT
  2812. // entries.
  2813. Relocate_functions<32, false>::rel32(view, got_offset);
  2814. }
  2815. break;
  2816. }
  2817. }
  2818. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2819. _("unsupported reloc %u"),
  2820. r_type);
  2821. break;
  2822. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  2823. if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
  2824. {
  2825. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2826. _("both SUN and GNU model "
  2827. "TLS relocations"));
  2828. break;
  2829. }
  2830. this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
  2831. if (optimized_type == tls::TLSOPT_TO_LE)
  2832. {
  2833. if (tls_segment == NULL)
  2834. {
  2835. gold_assert(parameters->errors()->error_count() > 0
  2836. || issue_undefined_symbol_error(gsym));
  2837. return;
  2838. }
  2839. this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
  2840. value, view, view_size);
  2841. break;
  2842. }
  2843. else if (optimized_type == tls::TLSOPT_NONE)
  2844. {
  2845. // Relocate the field with the offset of the GOT entry for
  2846. // the module index.
  2847. unsigned int got_offset;
  2848. got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
  2849. - target->got_size());
  2850. Relocate_functions<32, false>::rel32(view, got_offset);
  2851. break;
  2852. }
  2853. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2854. _("unsupported reloc %u"),
  2855. r_type);
  2856. break;
  2857. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  2858. if (optimized_type == tls::TLSOPT_TO_LE)
  2859. {
  2860. // This reloc can appear in debugging sections, in which
  2861. // case we must not convert to local-exec. We decide what
  2862. // to do based on whether the section is marked as
  2863. // containing executable code. That is what the GNU linker
  2864. // does as well.
  2865. elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
  2866. if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
  2867. {
  2868. if (tls_segment == NULL)
  2869. {
  2870. gold_assert(parameters->errors()->error_count() > 0
  2871. || issue_undefined_symbol_error(gsym));
  2872. return;
  2873. }
  2874. value -= tls_segment->memsz();
  2875. }
  2876. }
  2877. Relocate_functions<32, false>::rel32(view, value);
  2878. break;
  2879. case elfcpp::R_386_TLS_IE: // Initial-exec
  2880. case elfcpp::R_386_TLS_GOTIE:
  2881. case elfcpp::R_386_TLS_IE_32:
  2882. if (optimized_type == tls::TLSOPT_TO_LE)
  2883. {
  2884. if (tls_segment == NULL)
  2885. {
  2886. gold_assert(parameters->errors()->error_count() > 0
  2887. || issue_undefined_symbol_error(gsym));
  2888. return;
  2889. }
  2890. Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
  2891. rel, r_type, value, view,
  2892. view_size);
  2893. break;
  2894. }
  2895. else if (optimized_type == tls::TLSOPT_NONE)
  2896. {
  2897. // Relocate the field with the offset of the GOT entry for
  2898. // the tp-relative offset of the symbol.
  2899. unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
  2900. ? GOT_TYPE_TLS_OFFSET
  2901. : GOT_TYPE_TLS_NOFFSET);
  2902. unsigned int got_offset;
  2903. if (gsym != NULL)
  2904. {
  2905. gold_assert(gsym->has_got_offset(got_type));
  2906. got_offset = gsym->got_offset(got_type);
  2907. }
  2908. else
  2909. {
  2910. unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
  2911. gold_assert(object->local_has_got_offset(r_sym, got_type));
  2912. got_offset = object->local_got_offset(r_sym, got_type);
  2913. }
  2914. // For the R_386_TLS_IE relocation, we need to apply the
  2915. // absolute address of the GOT entry.
  2916. if (r_type == elfcpp::R_386_TLS_IE)
  2917. got_offset += target->got_plt_section()->address();
  2918. // All GOT offsets are relative to the end of the GOT.
  2919. got_offset -= target->got_size();
  2920. Relocate_functions<32, false>::rel32(view, got_offset);
  2921. break;
  2922. }
  2923. gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
  2924. _("unsupported reloc %u"),
  2925. r_type);
  2926. break;
  2927. case elfcpp::R_386_TLS_LE: // Local-exec
  2928. // If we're creating a shared library, a dynamic relocation will
  2929. // have been created for this location, so do not apply it now.
  2930. if (!parameters->options().shared())
  2931. {
  2932. if (tls_segment == NULL)
  2933. {
  2934. gold_assert(parameters->errors()->error_count() > 0
  2935. || issue_undefined_symbol_error(gsym));
  2936. return;
  2937. }
  2938. value -= tls_segment->memsz();
  2939. Relocate_functions<32, false>::rel32(view, value);
  2940. }
  2941. break;
  2942. case elfcpp::R_386_TLS_LE_32:
  2943. // If we're creating a shared library, a dynamic relocation will
  2944. // have been created for this location, so do not apply it now.
  2945. if (!parameters->options().shared())
  2946. {
  2947. if (tls_segment == NULL)
  2948. {
  2949. gold_assert(parameters->errors()->error_count() > 0
  2950. || issue_undefined_symbol_error(gsym));
  2951. return;
  2952. }
  2953. value = tls_segment->memsz() - value;
  2954. Relocate_functions<32, false>::rel32(view, value);
  2955. }
  2956. break;
  2957. }
  2958. }
  2959. // Do a relocation in which we convert a TLS General-Dynamic to a
  2960. // Local-Exec.
  2961. inline void
  2962. Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
  2963. size_t relnum,
  2964. Output_segment* tls_segment,
  2965. const elfcpp::Rel<32, false>& rel,
  2966. unsigned int,
  2967. elfcpp::Elf_types<32>::Elf_Addr value,
  2968. unsigned char* view,
  2969. section_size_type view_size)
  2970. {
  2971. // leal foo(,%ebx,1),%eax; call ___tls_get_addr@PLT
  2972. // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
  2973. // leal foo(%ebx),%eax; call ___tls_get_addr@PLT
  2974. // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
  2975. // leal foo(%reg),%eax; call *___tls_get_addr@GOT(%reg)
  2976. // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
  2977. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  2978. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
  2979. unsigned char op1 = view[-1];
  2980. unsigned char op2 = view[-2];
  2981. unsigned char op3 = view[4];
  2982. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  2983. op2 == 0x8d || op2 == 0x04);
  2984. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  2985. op3 == 0xe8 || op3 == 0xff);
  2986. int roff = 5;
  2987. if (op2 == 0x04)
  2988. {
  2989. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
  2990. tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
  2991. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  2992. ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
  2993. memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
  2994. }
  2995. else
  2996. {
  2997. unsigned char reg = op1 & 7;
  2998. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  2999. ((op1 & 0xf8) == 0x80
  3000. && reg != 4
  3001. && reg != 0
  3002. && (op3 == 0xe8 || (view[5] & 0x7) == reg)));
  3003. if (op3 == 0xff
  3004. || (rel.get_r_offset() + 9 < view_size
  3005. && view[9] == 0x90))
  3006. {
  3007. // There is an indirect call or a trailing nop. Use the size
  3008. // byte subl.
  3009. memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
  3010. roff = 6;
  3011. }
  3012. else
  3013. {
  3014. // Use the five byte subl.
  3015. memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
  3016. }
  3017. }
  3018. value = tls_segment->memsz() - value;
  3019. Relocate_functions<32, false>::rel32(view + roff, value);
  3020. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  3021. // We can skip it.
  3022. this->skip_call_tls_get_addr_ = true;
  3023. }
  3024. // Do a relocation in which we convert a TLS General-Dynamic to an
  3025. // Initial-Exec.
  3026. inline void
  3027. Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
  3028. size_t relnum,
  3029. const elfcpp::Rel<32, false>& rel,
  3030. unsigned int,
  3031. elfcpp::Elf_types<32>::Elf_Addr value,
  3032. unsigned char* view,
  3033. section_size_type view_size)
  3034. {
  3035. // leal foo(,%ebx,1),%eax; call ___tls_get_addr@PLT
  3036. // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
  3037. // leal foo(%ebx),%eax; call ___tls_get_addr@PLT; nop
  3038. // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
  3039. // leal foo(%reg),%eax; call *___tls_get_addr@GOT(%reg)
  3040. // ==> movl %gs:0,%eax; addl foo@gotntpoff(%reg),%eax
  3041. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  3042. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
  3043. unsigned char op1 = view[-1];
  3044. unsigned char op2 = view[-2];
  3045. unsigned char op3 = view[4];
  3046. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3047. op2 == 0x8d || op2 == 0x04);
  3048. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3049. op3 == 0xe8 || op3 == 0xff);
  3050. int roff;
  3051. if (op2 == 0x04)
  3052. {
  3053. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
  3054. tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
  3055. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3056. ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
  3057. roff = 5;
  3058. }
  3059. else
  3060. {
  3061. unsigned char reg = op1 & 7;
  3062. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 10);
  3063. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3064. ((op1 & 0xf8) == 0x80
  3065. && reg != 4
  3066. && reg != 0
  3067. && ((op3 == 0xe8 && view[9] == 0x90)
  3068. || (view[5] & 0x7) == reg)));
  3069. roff = 6;
  3070. }
  3071. memcpy(view + roff - 8, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
  3072. Relocate_functions<32, false>::rel32(view + roff, value);
  3073. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  3074. // We can skip it.
  3075. this->skip_call_tls_get_addr_ = true;
  3076. }
  3077. // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
  3078. // General-Dynamic to a Local-Exec.
  3079. inline void
  3080. Target_i386::Relocate::tls_desc_gd_to_le(
  3081. const Relocate_info<32, false>* relinfo,
  3082. size_t relnum,
  3083. Output_segment* tls_segment,
  3084. const elfcpp::Rel<32, false>& rel,
  3085. unsigned int r_type,
  3086. elfcpp::Elf_types<32>::Elf_Addr value,
  3087. unsigned char* view,
  3088. section_size_type view_size)
  3089. {
  3090. if (r_type == elfcpp::R_386_TLS_GOTDESC)
  3091. {
  3092. // leal foo@TLSDESC(%ebx), %eax
  3093. // ==> leal foo@NTPOFF, %eax
  3094. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  3095. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
  3096. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3097. view[-2] == 0x8d && view[-1] == 0x83);
  3098. view[-1] = 0x05;
  3099. value -= tls_segment->memsz();
  3100. Relocate_functions<32, false>::rel32(view, value);
  3101. }
  3102. else
  3103. {
  3104. // call *foo@TLSCALL(%eax)
  3105. // ==> nop; nop
  3106. gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
  3107. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
  3108. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3109. view[0] == 0xff && view[1] == 0x10);
  3110. view[0] = 0x66;
  3111. view[1] = 0x90;
  3112. }
  3113. }
  3114. // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
  3115. // General-Dynamic to an Initial-Exec.
  3116. inline void
  3117. Target_i386::Relocate::tls_desc_gd_to_ie(
  3118. const Relocate_info<32, false>* relinfo,
  3119. size_t relnum,
  3120. const elfcpp::Rel<32, false>& rel,
  3121. unsigned int r_type,
  3122. elfcpp::Elf_types<32>::Elf_Addr value,
  3123. unsigned char* view,
  3124. section_size_type view_size)
  3125. {
  3126. if (r_type == elfcpp::R_386_TLS_GOTDESC)
  3127. {
  3128. // leal foo@TLSDESC(%ebx), %eax
  3129. // ==> movl foo@GOTNTPOFF(%ebx), %eax
  3130. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  3131. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
  3132. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3133. view[-2] == 0x8d && view[-1] == 0x83);
  3134. view[-2] = 0x8b;
  3135. Relocate_functions<32, false>::rel32(view, value);
  3136. }
  3137. else
  3138. {
  3139. // call *foo@TLSCALL(%eax)
  3140. // ==> nop; nop
  3141. gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
  3142. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
  3143. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3144. view[0] == 0xff && view[1] == 0x10);
  3145. view[0] = 0x66;
  3146. view[1] = 0x90;
  3147. }
  3148. }
  3149. // Do a relocation in which we convert a TLS Local-Dynamic to a
  3150. // Local-Exec.
  3151. inline void
  3152. Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
  3153. size_t relnum,
  3154. Output_segment*,
  3155. const elfcpp::Rel<32, false>& rel,
  3156. unsigned int,
  3157. elfcpp::Elf_types<32>::Elf_Addr,
  3158. unsigned char* view,
  3159. section_size_type view_size)
  3160. {
  3161. // leal foo(%ebx), %eax; call ___tls_get_addr@PLT
  3162. // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
  3163. // leal foo(%reg), %eax; call call *___tls_get_addr@GOT(%reg)
  3164. // ==> movl %gs:0,%eax; leal (%esi),%esi
  3165. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  3166. unsigned char op1 = view[-1];
  3167. unsigned char op2 = view[-2];
  3168. unsigned char op3 = view[4];
  3169. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3170. op3 == 0xe8 || op3 == 0xff);
  3171. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size,
  3172. op3 == 0xe8 ? 9 : 10);
  3173. // FIXME: Does this test really always pass?
  3174. tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x8d);
  3175. unsigned char reg = op1 & 7;
  3176. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3177. ((op1 & 0xf8) == 0x80
  3178. && reg != 4
  3179. && reg != 0
  3180. && (op3 == 0xe8 || (view[5] & 0x7) == reg)));
  3181. if (op3 == 0xe8)
  3182. memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
  3183. else
  3184. memcpy(view - 2, "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0\0", 12);
  3185. // The next reloc should be a PLT32 reloc against __tls_get_addr.
  3186. // We can skip it.
  3187. this->skip_call_tls_get_addr_ = true;
  3188. }
  3189. // Do a relocation in which we convert a TLS Initial-Exec to a
  3190. // Local-Exec.
  3191. inline void
  3192. Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
  3193. size_t relnum,
  3194. Output_segment* tls_segment,
  3195. const elfcpp::Rel<32, false>& rel,
  3196. unsigned int r_type,
  3197. elfcpp::Elf_types<32>::Elf_Addr value,
  3198. unsigned char* view,
  3199. section_size_type view_size)
  3200. {
  3201. // We have to actually change the instructions, which means that we
  3202. // need to examine the opcodes to figure out which instruction we
  3203. // are looking at.
  3204. if (r_type == elfcpp::R_386_TLS_IE)
  3205. {
  3206. // movl %gs:XX,%eax ==> movl $YY,%eax
  3207. // movl %gs:XX,%reg ==> movl $YY,%reg
  3208. // addl %gs:XX,%reg ==> addl $YY,%reg
  3209. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
  3210. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
  3211. unsigned char op1 = view[-1];
  3212. if (op1 == 0xa1)
  3213. {
  3214. // movl XX,%eax ==> movl $YY,%eax
  3215. view[-1] = 0xb8;
  3216. }
  3217. else
  3218. {
  3219. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  3220. unsigned char op2 = view[-2];
  3221. if (op2 == 0x8b)
  3222. {
  3223. // movl XX,%reg ==> movl $YY,%reg
  3224. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3225. (op1 & 0xc7) == 0x05);
  3226. view[-2] = 0xc7;
  3227. view[-1] = 0xc0 | ((op1 >> 3) & 7);
  3228. }
  3229. else if (op2 == 0x03)
  3230. {
  3231. // addl XX,%reg ==> addl $YY,%reg
  3232. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3233. (op1 & 0xc7) == 0x05);
  3234. view[-2] = 0x81;
  3235. view[-1] = 0xc0 | ((op1 >> 3) & 7);
  3236. }
  3237. else
  3238. tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
  3239. }
  3240. }
  3241. else
  3242. {
  3243. // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
  3244. // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
  3245. // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
  3246. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
  3247. tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
  3248. unsigned char op1 = view[-1];
  3249. unsigned char op2 = view[-2];
  3250. tls::check_tls(relinfo, relnum, rel.get_r_offset(),
  3251. (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
  3252. if (op2 == 0x8b)
  3253. {
  3254. // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
  3255. view[-2] = 0xc7;
  3256. view[-1] = 0xc0 | ((op1 >> 3) & 7);
  3257. }
  3258. else if (op2 == 0x2b)
  3259. {
  3260. // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
  3261. view[-2] = 0x81;
  3262. view[-1] = 0xe8 | ((op1 >> 3) & 7);
  3263. }
  3264. else if (op2 == 0x03)
  3265. {
  3266. // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
  3267. view[-2] = 0x81;
  3268. view[-1] = 0xc0 | ((op1 >> 3) & 7);
  3269. }
  3270. else
  3271. tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
  3272. }
  3273. value = tls_segment->memsz() - value;
  3274. if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
  3275. value = - value;
  3276. Relocate_functions<32, false>::rel32(view, value);
  3277. }
  3278. // Relocate section data.
  3279. void
  3280. Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
  3281. unsigned int sh_type,
  3282. const unsigned char* prelocs,
  3283. size_t reloc_count,
  3284. Output_section* output_section,
  3285. bool needs_special_offset_handling,
  3286. unsigned char* view,
  3287. elfcpp::Elf_types<32>::Elf_Addr address,
  3288. section_size_type view_size,
  3289. const Reloc_symbol_changes* reloc_symbol_changes)
  3290. {
  3291. gold_assert(sh_type == elfcpp::SHT_REL);
  3292. gold::relocate_section<32, false, Target_i386, Relocate,
  3293. gold::Default_comdat_behavior, Classify_reloc>(
  3294. relinfo,
  3295. this,
  3296. prelocs,
  3297. reloc_count,
  3298. output_section,
  3299. needs_special_offset_handling,
  3300. view,
  3301. address,
  3302. view_size,
  3303. reloc_symbol_changes);
  3304. }
  3305. // Return the size of a relocation while scanning during a relocatable
  3306. // link.
  3307. unsigned int
  3308. Target_i386::Classify_reloc::get_size_for_reloc(
  3309. unsigned int r_type,
  3310. Relobj* object)
  3311. {
  3312. switch (r_type)
  3313. {
  3314. case elfcpp::R_386_NONE:
  3315. case elfcpp::R_386_GNU_VTINHERIT:
  3316. case elfcpp::R_386_GNU_VTENTRY:
  3317. case elfcpp::R_386_TLS_GD: // Global-dynamic
  3318. case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
  3319. case elfcpp::R_386_TLS_DESC_CALL:
  3320. case elfcpp::R_386_TLS_LDM: // Local-dynamic
  3321. case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
  3322. case elfcpp::R_386_TLS_IE: // Initial-exec
  3323. case elfcpp::R_386_TLS_IE_32:
  3324. case elfcpp::R_386_TLS_GOTIE:
  3325. case elfcpp::R_386_TLS_LE: // Local-exec
  3326. case elfcpp::R_386_TLS_LE_32:
  3327. return 0;
  3328. case elfcpp::R_386_32:
  3329. case elfcpp::R_386_PC32:
  3330. case elfcpp::R_386_GOT32:
  3331. case elfcpp::R_386_GOT32X:
  3332. case elfcpp::R_386_PLT32:
  3333. case elfcpp::R_386_GOTOFF:
  3334. case elfcpp::R_386_GOTPC:
  3335. return 4;
  3336. case elfcpp::R_386_16:
  3337. case elfcpp::R_386_PC16:
  3338. return 2;
  3339. case elfcpp::R_386_8:
  3340. case elfcpp::R_386_PC8:
  3341. return 1;
  3342. // These are relocations which should only be seen by the
  3343. // dynamic linker, and should never be seen here.
  3344. case elfcpp::R_386_COPY:
  3345. case elfcpp::R_386_GLOB_DAT:
  3346. case elfcpp::R_386_JUMP_SLOT:
  3347. case elfcpp::R_386_RELATIVE:
  3348. case elfcpp::R_386_IRELATIVE:
  3349. case elfcpp::R_386_TLS_TPOFF:
  3350. case elfcpp::R_386_TLS_DTPMOD32:
  3351. case elfcpp::R_386_TLS_DTPOFF32:
  3352. case elfcpp::R_386_TLS_TPOFF32:
  3353. case elfcpp::R_386_TLS_DESC:
  3354. object->error(_("unexpected reloc %u in object file"), r_type);
  3355. return 0;
  3356. case elfcpp::R_386_32PLT:
  3357. case elfcpp::R_386_TLS_GD_32:
  3358. case elfcpp::R_386_TLS_GD_PUSH:
  3359. case elfcpp::R_386_TLS_GD_CALL:
  3360. case elfcpp::R_386_TLS_GD_POP:
  3361. case elfcpp::R_386_TLS_LDM_32:
  3362. case elfcpp::R_386_TLS_LDM_PUSH:
  3363. case elfcpp::R_386_TLS_LDM_CALL:
  3364. case elfcpp::R_386_TLS_LDM_POP:
  3365. case elfcpp::R_386_USED_BY_INTEL_200:
  3366. default:
  3367. object->error(_("unsupported reloc %u in object file"), r_type);
  3368. return 0;
  3369. }
  3370. }
  3371. // Scan the relocs during a relocatable link.
  3372. void
  3373. Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
  3374. Layout* layout,
  3375. Sized_relobj_file<32, false>* object,
  3376. unsigned int data_shndx,
  3377. unsigned int sh_type,
  3378. const unsigned char* prelocs,
  3379. size_t reloc_count,
  3380. Output_section* output_section,
  3381. bool needs_special_offset_handling,
  3382. size_t local_symbol_count,
  3383. const unsigned char* plocal_symbols,
  3384. Relocatable_relocs* rr)
  3385. {
  3386. typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
  3387. Scan_relocatable_relocs;
  3388. gold_assert(sh_type == elfcpp::SHT_REL);
  3389. gold::scan_relocatable_relocs<32, false, Scan_relocatable_relocs>(
  3390. symtab,
  3391. layout,
  3392. object,
  3393. data_shndx,
  3394. prelocs,
  3395. reloc_count,
  3396. output_section,
  3397. needs_special_offset_handling,
  3398. local_symbol_count,
  3399. plocal_symbols,
  3400. rr);
  3401. }
  3402. // Scan the relocs for --emit-relocs.
  3403. void
  3404. Target_i386::emit_relocs_scan(Symbol_table* symtab,
  3405. Layout* layout,
  3406. Sized_relobj_file<32, false>* object,
  3407. unsigned int data_shndx,
  3408. unsigned int sh_type,
  3409. const unsigned char* prelocs,
  3410. size_t reloc_count,
  3411. Output_section* output_section,
  3412. bool needs_special_offset_handling,
  3413. size_t local_symbol_count,
  3414. const unsigned char* plocal_syms,
  3415. Relocatable_relocs* rr)
  3416. {
  3417. typedef gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
  3418. Classify_reloc;
  3419. typedef gold::Default_emit_relocs_strategy<Classify_reloc>
  3420. Emit_relocs_strategy;
  3421. gold_assert(sh_type == elfcpp::SHT_REL);
  3422. gold::scan_relocatable_relocs<32, false, Emit_relocs_strategy>(
  3423. symtab,
  3424. layout,
  3425. object,
  3426. data_shndx,
  3427. prelocs,
  3428. reloc_count,
  3429. output_section,
  3430. needs_special_offset_handling,
  3431. local_symbol_count,
  3432. plocal_syms,
  3433. rr);
  3434. }
  3435. // Emit relocations for a section.
  3436. void
  3437. Target_i386::relocate_relocs(
  3438. const Relocate_info<32, false>* relinfo,
  3439. unsigned int sh_type,
  3440. const unsigned char* prelocs,
  3441. size_t reloc_count,
  3442. Output_section* output_section,
  3443. elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
  3444. unsigned char* view,
  3445. elfcpp::Elf_types<32>::Elf_Addr view_address,
  3446. section_size_type view_size,
  3447. unsigned char* reloc_view,
  3448. section_size_type reloc_view_size)
  3449. {
  3450. gold_assert(sh_type == elfcpp::SHT_REL);
  3451. gold::relocate_relocs<32, false, Classify_reloc>(
  3452. relinfo,
  3453. prelocs,
  3454. reloc_count,
  3455. output_section,
  3456. offset_in_output_section,
  3457. view,
  3458. view_address,
  3459. view_size,
  3460. reloc_view,
  3461. reloc_view_size);
  3462. }
  3463. // Return the value to use for a dynamic which requires special
  3464. // treatment. This is how we support equality comparisons of function
  3465. // pointers across shared library boundaries, as described in the
  3466. // processor specific ABI supplement.
  3467. uint64_t
  3468. Target_i386::do_dynsym_value(const Symbol* gsym) const
  3469. {
  3470. gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
  3471. return this->plt_address_for_global(gsym);
  3472. }
  3473. // Return a string used to fill a code section with nops to take up
  3474. // the specified length.
  3475. std::string
  3476. Target_i386::do_code_fill(section_size_type length) const
  3477. {
  3478. if (length >= 16)
  3479. {
  3480. // Build a jmp instruction to skip over the bytes.
  3481. unsigned char jmp[5];
  3482. jmp[0] = 0xe9;
  3483. elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
  3484. return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
  3485. + std::string(length - 5, static_cast<char>(0x90)));
  3486. }
  3487. // Nop sequences of various lengths.
  3488. const char nop1[1] = { '\x90' }; // nop
  3489. const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
  3490. const char nop3[3] = { '\x8d', '\x76', '\x00' }; // leal 0(%esi),%esi
  3491. const char nop4[4] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
  3492. '\x00'};
  3493. const char nop5[5] = { '\x90', '\x8d', '\x74', // nop
  3494. '\x26', '\x00' }; // leal 0(%esi,1),%esi
  3495. const char nop6[6] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
  3496. '\x00', '\x00', '\x00' };
  3497. const char nop7[7] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
  3498. '\x00', '\x00', '\x00',
  3499. '\x00' };
  3500. const char nop8[8] = { '\x90', '\x8d', '\xb4', // nop
  3501. '\x26', '\x00', '\x00', // leal 0L(%esi,1),%esi
  3502. '\x00', '\x00' };
  3503. const char nop9[9] = { '\x89', '\xf6', '\x8d', // movl %esi,%esi
  3504. '\xbc', '\x27', '\x00', // leal 0L(%edi,1),%edi
  3505. '\x00', '\x00', '\x00' };
  3506. const char nop10[10] = { '\x8d', '\x76', '\x00', // leal 0(%esi),%esi
  3507. '\x8d', '\xbc', '\x27', // leal 0L(%edi,1),%edi
  3508. '\x00', '\x00', '\x00',
  3509. '\x00' };
  3510. const char nop11[11] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
  3511. '\x00', '\x8d', '\xbc', // leal 0L(%edi,1),%edi
  3512. '\x27', '\x00', '\x00',
  3513. '\x00', '\x00' };
  3514. const char nop12[12] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
  3515. '\x00', '\x00', '\x00', // leal 0L(%edi),%edi
  3516. '\x8d', '\xbf', '\x00',
  3517. '\x00', '\x00', '\x00' };
  3518. const char nop13[13] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
  3519. '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
  3520. '\x8d', '\xbc', '\x27',
  3521. '\x00', '\x00', '\x00',
  3522. '\x00' };
  3523. const char nop14[14] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
  3524. '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
  3525. '\x00', '\x8d', '\xbc',
  3526. '\x27', '\x00', '\x00',
  3527. '\x00', '\x00' };
  3528. const char nop15[15] = { '\xeb', '\x0d', '\x90', // jmp .+15
  3529. '\x90', '\x90', '\x90', // nop,nop,nop,...
  3530. '\x90', '\x90', '\x90',
  3531. '\x90', '\x90', '\x90',
  3532. '\x90', '\x90', '\x90' };
  3533. const char* nops[16] = {
  3534. NULL,
  3535. nop1, nop2, nop3, nop4, nop5, nop6, nop7,
  3536. nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
  3537. };
  3538. return std::string(nops[length], length);
  3539. }
  3540. // Return the value to use for the base of a DW_EH_PE_datarel offset
  3541. // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
  3542. // assembler can not write out the difference between two labels in
  3543. // different sections, so instead of using a pc-relative value they
  3544. // use an offset from the GOT.
  3545. uint64_t
  3546. Target_i386::do_ehframe_datarel_base() const
  3547. {
  3548. gold_assert(this->global_offset_table_ != NULL);
  3549. Symbol* sym = this->global_offset_table_;
  3550. Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
  3551. return ssym->value();
  3552. }
  3553. // Return whether SYM should be treated as a call to a non-split
  3554. // function. We don't want that to be true of a call to a
  3555. // get_pc_thunk function.
  3556. bool
  3557. Target_i386::do_is_call_to_non_split(const Symbol* sym,
  3558. const unsigned char*,
  3559. const unsigned char*,
  3560. section_size_type) const
  3561. {
  3562. return (sym->type() == elfcpp::STT_FUNC
  3563. && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
  3564. }
  3565. // FNOFFSET in section SHNDX in OBJECT is the start of a function
  3566. // compiled with -fsplit-stack. The function calls non-split-stack
  3567. // code. We have to change the function so that it always ensures
  3568. // that it has enough stack space to run some random function.
  3569. void
  3570. Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
  3571. section_offset_type fnoffset,
  3572. section_size_type fnsize,
  3573. const unsigned char*,
  3574. size_t,
  3575. unsigned char* view,
  3576. section_size_type view_size,
  3577. std::string* from,
  3578. std::string* to) const
  3579. {
  3580. // The function starts with a comparison of the stack pointer and a
  3581. // field in the TCB. This is followed by a jump.
  3582. // cmp %gs:NN,%esp
  3583. if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
  3584. && fnsize > 7)
  3585. {
  3586. // We will call __morestack if the carry flag is set after this
  3587. // comparison. We turn the comparison into an stc instruction
  3588. // and some nops.
  3589. view[fnoffset] = '\xf9';
  3590. this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
  3591. }
  3592. // lea NN(%esp),%ecx
  3593. // lea NN(%esp),%edx
  3594. else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
  3595. || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
  3596. && fnsize > 7)
  3597. {
  3598. // This is loading an offset from the stack pointer for a
  3599. // comparison. The offset is negative, so we decrease the
  3600. // offset by the amount of space we need for the stack. This
  3601. // means we will avoid calling __morestack if there happens to
  3602. // be plenty of space on the stack already.
  3603. unsigned char* pval = view + fnoffset + 3;
  3604. uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
  3605. val -= parameters->options().split_stack_adjust_size();
  3606. elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
  3607. }
  3608. else
  3609. {
  3610. if (!object->has_no_split_stack())
  3611. object->error(_("failed to match split-stack sequence at "
  3612. "section %u offset %0zx"),
  3613. shndx, static_cast<size_t>(fnoffset));
  3614. return;
  3615. }
  3616. // We have to change the function so that it calls
  3617. // __morestack_non_split instead of __morestack. The former will
  3618. // allocate additional stack space.
  3619. *from = "__morestack";
  3620. *to = "__morestack_non_split";
  3621. }
  3622. // The selector for i386 object files. Note this is never instantiated
  3623. // directly. It's only used in Target_selector_i386_nacl, below.
  3624. class Target_selector_i386 : public Target_selector_freebsd
  3625. {
  3626. public:
  3627. Target_selector_i386()
  3628. : Target_selector_freebsd(elfcpp::EM_386, 32, false,
  3629. "elf32-i386", "elf32-i386-freebsd",
  3630. "elf_i386")
  3631. { }
  3632. Target*
  3633. do_instantiate_target()
  3634. { return new Target_i386(); }
  3635. };
  3636. // NaCl variant. It uses different PLT contents.
  3637. class Output_data_plt_i386_nacl : public Output_data_plt_i386
  3638. {
  3639. public:
  3640. Output_data_plt_i386_nacl(Layout* layout,
  3641. Output_data_got_plt_i386* got_plt,
  3642. Output_data_space* got_irelative)
  3643. : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
  3644. { }
  3645. protected:
  3646. virtual unsigned int
  3647. do_get_plt_entry_size() const
  3648. { return plt_entry_size; }
  3649. virtual void
  3650. do_add_eh_frame(Layout* layout)
  3651. {
  3652. layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
  3653. plt_eh_frame_fde, plt_eh_frame_fde_size);
  3654. }
  3655. // The size of an entry in the PLT.
  3656. static const int plt_entry_size = 64;
  3657. // The .eh_frame unwind information for the PLT.
  3658. static const int plt_eh_frame_fde_size = 32;
  3659. static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
  3660. };
  3661. class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl
  3662. {
  3663. public:
  3664. Output_data_plt_i386_nacl_exec(Layout* layout,
  3665. Output_data_got_plt_i386* got_plt,
  3666. Output_data_space* got_irelative)
  3667. : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
  3668. { }
  3669. protected:
  3670. virtual void
  3671. do_fill_first_plt_entry(unsigned char* pov,
  3672. elfcpp::Elf_types<32>::Elf_Addr got_address);
  3673. virtual unsigned int
  3674. do_fill_plt_entry(unsigned char* pov,
  3675. elfcpp::Elf_types<32>::Elf_Addr got_address,
  3676. unsigned int got_offset,
  3677. unsigned int plt_offset,
  3678. unsigned int plt_rel_offset);
  3679. private:
  3680. // The first entry in the PLT for an executable.
  3681. static const unsigned char first_plt_entry[plt_entry_size];
  3682. // Other entries in the PLT for an executable.
  3683. static const unsigned char plt_entry[plt_entry_size];
  3684. };
  3685. class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl
  3686. {
  3687. public:
  3688. Output_data_plt_i386_nacl_dyn(Layout* layout,
  3689. Output_data_got_plt_i386* got_plt,
  3690. Output_data_space* got_irelative)
  3691. : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
  3692. { }
  3693. protected:
  3694. virtual void
  3695. do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
  3696. virtual unsigned int
  3697. do_fill_plt_entry(unsigned char* pov,
  3698. elfcpp::Elf_types<32>::Elf_Addr,
  3699. unsigned int got_offset,
  3700. unsigned int plt_offset,
  3701. unsigned int plt_rel_offset);
  3702. private:
  3703. // The first entry in the PLT for a shared object.
  3704. static const unsigned char first_plt_entry[plt_entry_size];
  3705. // Other entries in the PLT for a shared object.
  3706. static const unsigned char plt_entry[plt_entry_size];
  3707. };
  3708. class Target_i386_nacl : public Target_i386
  3709. {
  3710. public:
  3711. Target_i386_nacl()
  3712. : Target_i386(&i386_nacl_info)
  3713. { }
  3714. protected:
  3715. virtual Output_data_plt_i386*
  3716. do_make_data_plt(Layout* layout,
  3717. Output_data_got_plt_i386* got_plt,
  3718. Output_data_space* got_irelative,
  3719. bool dyn)
  3720. {
  3721. if (dyn)
  3722. return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative);
  3723. else
  3724. return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative);
  3725. }
  3726. virtual std::string
  3727. do_code_fill(section_size_type length) const;
  3728. private:
  3729. static const Target::Target_info i386_nacl_info;
  3730. };
  3731. const Target::Target_info Target_i386_nacl::i386_nacl_info =
  3732. {
  3733. 32, // size
  3734. false, // is_big_endian
  3735. elfcpp::EM_386, // machine_code
  3736. false, // has_make_symbol
  3737. false, // has_resolve
  3738. true, // has_code_fill
  3739. true, // is_default_stack_executable
  3740. true, // can_icf_inline_merge_sections
  3741. '\0', // wrap_char
  3742. "/lib/ld-nacl-x86-32.so.1", // dynamic_linker
  3743. 0x20000, // default_text_segment_address
  3744. 0x10000, // abi_pagesize (overridable by -z max-page-size)
  3745. 0x10000, // common_pagesize (overridable by -z common-page-size)
  3746. true, // isolate_execinstr
  3747. 0x10000000, // rosegment_gap
  3748. elfcpp::SHN_UNDEF, // small_common_shndx
  3749. elfcpp::SHN_UNDEF, // large_common_shndx
  3750. 0, // small_common_section_flags
  3751. 0, // large_common_section_flags
  3752. NULL, // attributes_section
  3753. NULL, // attributes_vendor
  3754. "_start", // entry_symbol_name
  3755. 32, // hash_entry_size
  3756. elfcpp::SHT_PROGBITS, // unwind_section_type
  3757. };
  3758. #define NACLMASK 0xe0 // 32-byte alignment mask
  3759. const unsigned char
  3760. Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] =
  3761. {
  3762. 0xff, 0x35, // pushl contents of memory address
  3763. 0, 0, 0, 0, // replaced with address of .got + 4
  3764. 0x8b, 0x0d, // movl contents of address, %ecx
  3765. 0, 0, 0, 0, // replaced with address of .got + 8
  3766. 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
  3767. 0xff, 0xe1, // jmp *%ecx
  3768. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3769. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3770. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3771. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3772. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3773. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3774. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3775. 0x90, 0x90, 0x90, 0x90, 0x90
  3776. };
  3777. void
  3778. Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry(
  3779. unsigned char* pov,
  3780. elfcpp::Elf_types<32>::Elf_Addr got_address)
  3781. {
  3782. memcpy(pov, first_plt_entry, plt_entry_size);
  3783. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
  3784. elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
  3785. }
  3786. // The first entry in the PLT for a shared object.
  3787. const unsigned char
  3788. Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] =
  3789. {
  3790. 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
  3791. 0x8b, 0x4b, 0x08, // mov 0x8(%ebx), %ecx
  3792. 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
  3793. 0xff, 0xe1, // jmp *%ecx
  3794. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3795. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3796. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3797. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3798. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3799. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3800. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3801. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3802. 0x90, 0x90, 0x90, 0x90, 0x90, // nops
  3803. 0x90, 0x90, 0x90, 0x90, 0x90 // nops
  3804. };
  3805. void
  3806. Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry(
  3807. unsigned char* pov,
  3808. elfcpp::Elf_types<32>::Elf_Addr)
  3809. {
  3810. memcpy(pov, first_plt_entry, plt_entry_size);
  3811. }
  3812. // Subsequent entries in the PLT for an executable.
  3813. const unsigned char
  3814. Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] =
  3815. {
  3816. 0x8b, 0x0d, // movl contents of address, %ecx */
  3817. 0, 0, 0, 0, // replaced with address of symbol in .got
  3818. 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
  3819. 0xff, 0xe1, // jmp *%ecx
  3820. // Pad to the next 32-byte boundary with nop instructions.
  3821. 0x90,
  3822. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3823. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3824. // Lazy GOT entries point here (32-byte aligned).
  3825. 0x68, // pushl immediate
  3826. 0, 0, 0, 0, // replaced with offset into relocation table
  3827. 0xe9, // jmp relative
  3828. 0, 0, 0, 0, // replaced with offset to start of .plt
  3829. // Pad to the next 32-byte boundary with nop instructions.
  3830. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3831. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3832. 0x90, 0x90
  3833. };
  3834. unsigned int
  3835. Output_data_plt_i386_nacl_exec::do_fill_plt_entry(
  3836. unsigned char* pov,
  3837. elfcpp::Elf_types<32>::Elf_Addr got_address,
  3838. unsigned int got_offset,
  3839. unsigned int plt_offset,
  3840. unsigned int plt_rel_offset)
  3841. {
  3842. memcpy(pov, plt_entry, plt_entry_size);
  3843. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
  3844. got_address + got_offset);
  3845. elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
  3846. elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
  3847. return 32;
  3848. }
  3849. // Subsequent entries in the PLT for a shared object.
  3850. const unsigned char
  3851. Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] =
  3852. {
  3853. 0x8b, 0x8b, // movl offset(%ebx), %ecx
  3854. 0, 0, 0, 0, // replaced with offset of symbol in .got
  3855. 0x83, 0xe1, 0xe0, // andl $NACLMASK, %ecx
  3856. 0xff, 0xe1, // jmp *%ecx
  3857. // Pad to the next 32-byte boundary with nop instructions.
  3858. 0x90,
  3859. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3860. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3861. // Lazy GOT entries point here (32-byte aligned).
  3862. 0x68, // pushl immediate
  3863. 0, 0, 0, 0, // replaced with offset into relocation table.
  3864. 0xe9, // jmp relative
  3865. 0, 0, 0, 0, // replaced with offset to start of .plt.
  3866. // Pad to the next 32-byte boundary with nop instructions.
  3867. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3868. 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
  3869. 0x90, 0x90
  3870. };
  3871. unsigned int
  3872. Output_data_plt_i386_nacl_dyn::do_fill_plt_entry(
  3873. unsigned char* pov,
  3874. elfcpp::Elf_types<32>::Elf_Addr,
  3875. unsigned int got_offset,
  3876. unsigned int plt_offset,
  3877. unsigned int plt_rel_offset)
  3878. {
  3879. memcpy(pov, plt_entry, plt_entry_size);
  3880. elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
  3881. elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
  3882. elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
  3883. return 32;
  3884. }
  3885. const unsigned char
  3886. Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] =
  3887. {
  3888. 0, 0, 0, 0, // Replaced with offset to .plt.
  3889. 0, 0, 0, 0, // Replaced with size of .plt.
  3890. 0, // Augmentation size.
  3891. elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
  3892. elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
  3893. elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
  3894. elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
  3895. elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
  3896. 13, // Block length.
  3897. elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
  3898. elfcpp::DW_OP_breg8, 0, // Push %eip.
  3899. elfcpp::DW_OP_const1u, 63, // Push 0x3f.
  3900. elfcpp::DW_OP_and, // & (%eip & 0x3f).
  3901. elfcpp::DW_OP_const1u, 37, // Push 0x25.
  3902. elfcpp::DW_OP_ge, // >= ((%eip & 0x3f) >= 0x25)
  3903. elfcpp::DW_OP_lit2, // Push 2.
  3904. elfcpp::DW_OP_shl, // << (((%eip & 0x3f) >= 0x25) << 2)
  3905. elfcpp::DW_OP_plus, // + ((((%eip&0x3f)>=0x25)<<2)+%esp+4
  3906. elfcpp::DW_CFA_nop, // Align to 32 bytes.
  3907. elfcpp::DW_CFA_nop
  3908. };
  3909. // Return a string used to fill a code section with nops.
  3910. // For NaCl, long NOPs are only valid if they do not cross
  3911. // bundle alignment boundaries, so keep it simple with one-byte NOPs.
  3912. std::string
  3913. Target_i386_nacl::do_code_fill(section_size_type length) const
  3914. {
  3915. return std::string(length, static_cast<char>(0x90));
  3916. }
  3917. // The selector for i386-nacl object files.
  3918. class Target_selector_i386_nacl
  3919. : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl>
  3920. {
  3921. public:
  3922. Target_selector_i386_nacl()
  3923. : Target_selector_nacl<Target_selector_i386,
  3924. Target_i386_nacl>("x86-32",
  3925. "elf32-i386-nacl",
  3926. "elf_i386_nacl")
  3927. { }
  3928. };
  3929. Target_selector_i386_nacl target_selector_i386;
  3930. // IAMCU variant. It uses EM_IAMCU, not EM_386.
  3931. class Target_iamcu : public Target_i386
  3932. {
  3933. public:
  3934. Target_iamcu()
  3935. : Target_i386(&iamcu_info)
  3936. { }
  3937. private:
  3938. // Information about this specific target which we pass to the
  3939. // general Target structure.
  3940. static const Target::Target_info iamcu_info;
  3941. };
  3942. const Target::Target_info Target_iamcu::iamcu_info =
  3943. {
  3944. 32, // size
  3945. false, // is_big_endian
  3946. elfcpp::EM_IAMCU, // machine_code
  3947. false, // has_make_symbol
  3948. false, // has_resolve
  3949. true, // has_code_fill
  3950. true, // is_default_stack_executable
  3951. true, // can_icf_inline_merge_sections
  3952. '\0', // wrap_char
  3953. "/usr/lib/libc.so.1", // dynamic_linker
  3954. 0x08048000, // default_text_segment_address
  3955. 0x1000, // abi_pagesize (overridable by -z max-page-size)
  3956. 0x1000, // common_pagesize (overridable by -z common-page-size)
  3957. false, // isolate_execinstr
  3958. 0, // rosegment_gap
  3959. elfcpp::SHN_UNDEF, // small_common_shndx
  3960. elfcpp::SHN_UNDEF, // large_common_shndx
  3961. 0, // small_common_section_flags
  3962. 0, // large_common_section_flags
  3963. NULL, // attributes_section
  3964. NULL, // attributes_vendor
  3965. "_start", // entry_symbol_name
  3966. 32, // hash_entry_size
  3967. elfcpp::SHT_PROGBITS, // unwind_section_type
  3968. };
  3969. class Target_selector_iamcu : public Target_selector
  3970. {
  3971. public:
  3972. Target_selector_iamcu()
  3973. : Target_selector(elfcpp::EM_IAMCU, 32, false, "elf32-iamcu",
  3974. "elf_iamcu")
  3975. { }
  3976. Target*
  3977. do_instantiate_target()
  3978. { return new Target_iamcu(); }
  3979. };
  3980. Target_selector_iamcu target_selector_iamcu;
  3981. } // End anonymous namespace.