i386.cc 135 KB

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