symtab.cc 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035
  1. // symtab.cc -- the gold symbol table
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstring>
  19. #include <stdint.h>
  20. #include <algorithm>
  21. #include <set>
  22. #include <string>
  23. #include <utility>
  24. #include "demangle.h"
  25. #include "gc.h"
  26. #include "object.h"
  27. #include "dwarf_reader.h"
  28. #include "dynobj.h"
  29. #include "output.h"
  30. #include "target.h"
  31. #include "workqueue.h"
  32. #include "symtab.h"
  33. #include "script.h"
  34. #include "plugin.h"
  35. #include "incremental.h"
  36. namespace gold
  37. {
  38. // Class Symbol.
  39. // Initialize fields in Symbol. This initializes everything except
  40. // u1_, u2_ and source_.
  41. void
  42. Symbol::init_fields(const char* name, const char* version,
  43. elfcpp::STT type, elfcpp::STB binding,
  44. elfcpp::STV visibility, unsigned char nonvis)
  45. {
  46. this->name_ = name;
  47. this->version_ = version;
  48. this->symtab_index_ = 0;
  49. this->dynsym_index_ = 0;
  50. this->got_offsets_.init();
  51. this->plt_offset_ = -1U;
  52. this->type_ = type;
  53. this->binding_ = binding;
  54. this->visibility_ = visibility;
  55. this->nonvis_ = nonvis;
  56. this->is_def_ = false;
  57. this->is_forwarder_ = false;
  58. this->has_alias_ = false;
  59. this->needs_dynsym_entry_ = false;
  60. this->in_reg_ = false;
  61. this->in_dyn_ = false;
  62. this->has_warning_ = false;
  63. this->is_copied_from_dynobj_ = false;
  64. this->is_forced_local_ = false;
  65. this->is_ordinary_shndx_ = false;
  66. this->in_real_elf_ = false;
  67. this->is_defined_in_discarded_section_ = false;
  68. this->undef_binding_set_ = false;
  69. this->undef_binding_weak_ = false;
  70. this->is_predefined_ = false;
  71. this->is_protected_ = false;
  72. this->non_zero_localentry_ = false;
  73. }
  74. // Return the demangled version of the symbol's name, but only
  75. // if the --demangle flag was set.
  76. static std::string
  77. demangle(const char* name)
  78. {
  79. if (!parameters->options().do_demangle())
  80. return name;
  81. // cplus_demangle allocates memory for the result it returns,
  82. // and returns NULL if the name is already demangled.
  83. char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
  84. if (demangled_name == NULL)
  85. return name;
  86. std::string retval(demangled_name);
  87. free(demangled_name);
  88. return retval;
  89. }
  90. std::string
  91. Symbol::demangled_name() const
  92. {
  93. return demangle(this->name());
  94. }
  95. // Initialize the fields in the base class Symbol for SYM in OBJECT.
  96. template<int size, bool big_endian>
  97. void
  98. Symbol::init_base_object(const char* name, const char* version, Object* object,
  99. const elfcpp::Sym<size, big_endian>& sym,
  100. unsigned int st_shndx, bool is_ordinary)
  101. {
  102. this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
  103. sym.get_st_visibility(), sym.get_st_nonvis());
  104. this->u1_.object = object;
  105. this->u2_.shndx = st_shndx;
  106. this->is_ordinary_shndx_ = is_ordinary;
  107. this->source_ = FROM_OBJECT;
  108. this->in_reg_ = !object->is_dynamic();
  109. this->in_dyn_ = object->is_dynamic();
  110. this->in_real_elf_ = object->pluginobj() == NULL;
  111. }
  112. // Initialize the fields in the base class Symbol for a symbol defined
  113. // in an Output_data.
  114. void
  115. Symbol::init_base_output_data(const char* name, const char* version,
  116. Output_data* od, elfcpp::STT type,
  117. elfcpp::STB binding, elfcpp::STV visibility,
  118. unsigned char nonvis, bool offset_is_from_end,
  119. bool is_predefined)
  120. {
  121. this->init_fields(name, version, type, binding, visibility, nonvis);
  122. this->u1_.output_data = od;
  123. this->u2_.offset_is_from_end = offset_is_from_end;
  124. this->source_ = IN_OUTPUT_DATA;
  125. this->in_reg_ = true;
  126. this->in_real_elf_ = true;
  127. this->is_predefined_ = is_predefined;
  128. }
  129. // Initialize the fields in the base class Symbol for a symbol defined
  130. // in an Output_segment.
  131. void
  132. Symbol::init_base_output_segment(const char* name, const char* version,
  133. Output_segment* os, elfcpp::STT type,
  134. elfcpp::STB binding, elfcpp::STV visibility,
  135. unsigned char nonvis,
  136. Segment_offset_base offset_base,
  137. bool is_predefined)
  138. {
  139. this->init_fields(name, version, type, binding, visibility, nonvis);
  140. this->u1_.output_segment = os;
  141. this->u2_.offset_base = offset_base;
  142. this->source_ = IN_OUTPUT_SEGMENT;
  143. this->in_reg_ = true;
  144. this->in_real_elf_ = true;
  145. this->is_predefined_ = is_predefined;
  146. }
  147. // Initialize the fields in the base class Symbol for a symbol defined
  148. // as a constant.
  149. void
  150. Symbol::init_base_constant(const char* name, const char* version,
  151. elfcpp::STT type, elfcpp::STB binding,
  152. elfcpp::STV visibility, unsigned char nonvis,
  153. bool is_predefined)
  154. {
  155. this->init_fields(name, version, type, binding, visibility, nonvis);
  156. this->source_ = IS_CONSTANT;
  157. this->in_reg_ = true;
  158. this->in_real_elf_ = true;
  159. this->is_predefined_ = is_predefined;
  160. }
  161. // Initialize the fields in the base class Symbol for an undefined
  162. // symbol.
  163. void
  164. Symbol::init_base_undefined(const char* name, const char* version,
  165. elfcpp::STT type, elfcpp::STB binding,
  166. elfcpp::STV visibility, unsigned char nonvis)
  167. {
  168. this->init_fields(name, version, type, binding, visibility, nonvis);
  169. this->dynsym_index_ = -1U;
  170. this->source_ = IS_UNDEFINED;
  171. this->in_reg_ = true;
  172. this->in_real_elf_ = true;
  173. }
  174. // Allocate a common symbol in the base.
  175. void
  176. Symbol::allocate_base_common(Output_data* od)
  177. {
  178. gold_assert(this->is_common());
  179. this->source_ = IN_OUTPUT_DATA;
  180. this->u1_.output_data = od;
  181. this->u2_.offset_is_from_end = false;
  182. }
  183. // Initialize the fields in Sized_symbol for SYM in OBJECT.
  184. template<int size>
  185. template<bool big_endian>
  186. void
  187. Sized_symbol<size>::init_object(const char* name, const char* version,
  188. Object* object,
  189. const elfcpp::Sym<size, big_endian>& sym,
  190. unsigned int st_shndx, bool is_ordinary)
  191. {
  192. this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
  193. this->value_ = sym.get_st_value();
  194. this->symsize_ = sym.get_st_size();
  195. }
  196. // Initialize the fields in Sized_symbol for a symbol defined in an
  197. // Output_data.
  198. template<int size>
  199. void
  200. Sized_symbol<size>::init_output_data(const char* name, const char* version,
  201. Output_data* od, Value_type value,
  202. Size_type symsize, elfcpp::STT type,
  203. elfcpp::STB binding,
  204. elfcpp::STV visibility,
  205. unsigned char nonvis,
  206. bool offset_is_from_end,
  207. bool is_predefined)
  208. {
  209. this->init_base_output_data(name, version, od, type, binding, visibility,
  210. nonvis, offset_is_from_end, is_predefined);
  211. this->value_ = value;
  212. this->symsize_ = symsize;
  213. }
  214. // Initialize the fields in Sized_symbol for a symbol defined in an
  215. // Output_segment.
  216. template<int size>
  217. void
  218. Sized_symbol<size>::init_output_segment(const char* name, const char* version,
  219. Output_segment* os, Value_type value,
  220. Size_type symsize, elfcpp::STT type,
  221. elfcpp::STB binding,
  222. elfcpp::STV visibility,
  223. unsigned char nonvis,
  224. Segment_offset_base offset_base,
  225. bool is_predefined)
  226. {
  227. this->init_base_output_segment(name, version, os, type, binding, visibility,
  228. nonvis, offset_base, is_predefined);
  229. this->value_ = value;
  230. this->symsize_ = symsize;
  231. }
  232. // Initialize the fields in Sized_symbol for a symbol defined as a
  233. // constant.
  234. template<int size>
  235. void
  236. Sized_symbol<size>::init_constant(const char* name, const char* version,
  237. Value_type value, Size_type symsize,
  238. elfcpp::STT type, elfcpp::STB binding,
  239. elfcpp::STV visibility, unsigned char nonvis,
  240. bool is_predefined)
  241. {
  242. this->init_base_constant(name, version, type, binding, visibility, nonvis,
  243. is_predefined);
  244. this->value_ = value;
  245. this->symsize_ = symsize;
  246. }
  247. // Initialize the fields in Sized_symbol for an undefined symbol.
  248. template<int size>
  249. void
  250. Sized_symbol<size>::init_undefined(const char* name, const char* version,
  251. Value_type value, elfcpp::STT type,
  252. elfcpp::STB binding, elfcpp::STV visibility,
  253. unsigned char nonvis)
  254. {
  255. this->init_base_undefined(name, version, type, binding, visibility, nonvis);
  256. this->value_ = value;
  257. this->symsize_ = 0;
  258. }
  259. // Return an allocated string holding the symbol's name as
  260. // name@version. This is used for relocatable links.
  261. std::string
  262. Symbol::versioned_name() const
  263. {
  264. gold_assert(this->version_ != NULL);
  265. std::string ret = this->name_;
  266. ret.push_back('@');
  267. if (this->is_def_)
  268. ret.push_back('@');
  269. ret += this->version_;
  270. return ret;
  271. }
  272. // Return true if SHNDX represents a common symbol.
  273. bool
  274. Symbol::is_common_shndx(unsigned int shndx)
  275. {
  276. return (shndx == elfcpp::SHN_COMMON
  277. || shndx == parameters->target().small_common_shndx()
  278. || shndx == parameters->target().large_common_shndx());
  279. }
  280. // Allocate a common symbol.
  281. template<int size>
  282. void
  283. Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
  284. {
  285. this->allocate_base_common(od);
  286. this->value_ = value;
  287. }
  288. // The ""'s around str ensure str is a string literal, so sizeof works.
  289. #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
  290. // Return true if this symbol should be added to the dynamic symbol
  291. // table.
  292. bool
  293. Symbol::should_add_dynsym_entry(Symbol_table* symtab) const
  294. {
  295. // If the symbol is only present on plugin files, the plugin decided we
  296. // don't need it.
  297. if (!this->in_real_elf())
  298. return false;
  299. // If the symbol is used by a dynamic relocation, we need to add it.
  300. if (this->needs_dynsym_entry())
  301. return true;
  302. // If this symbol's section is not added, the symbol need not be added.
  303. // The section may have been GCed. Note that export_dynamic is being
  304. // overridden here. This should not be done for shared objects.
  305. if (parameters->options().gc_sections()
  306. && !parameters->options().shared()
  307. && this->source() == Symbol::FROM_OBJECT
  308. && !this->object()->is_dynamic())
  309. {
  310. Relobj* relobj = static_cast<Relobj*>(this->object());
  311. bool is_ordinary;
  312. unsigned int shndx = this->shndx(&is_ordinary);
  313. if (is_ordinary && shndx != elfcpp::SHN_UNDEF
  314. && !relobj->is_section_included(shndx)
  315. && !symtab->is_section_folded(relobj, shndx))
  316. return false;
  317. }
  318. // If the symbol was forced dynamic in a --dynamic-list file
  319. // or an --export-dynamic-symbol option, add it.
  320. if (!this->is_from_dynobj()
  321. && (parameters->options().in_dynamic_list(this->name())
  322. || parameters->options().is_export_dynamic_symbol(this->name())))
  323. {
  324. if (!this->is_forced_local())
  325. return true;
  326. gold_warning(_("Cannot export local symbol '%s'"),
  327. this->demangled_name().c_str());
  328. return false;
  329. }
  330. // If the symbol was forced local in a version script, do not add it.
  331. if (this->is_forced_local())
  332. return false;
  333. // If dynamic-list-data was specified, add any STT_OBJECT.
  334. if (parameters->options().dynamic_list_data()
  335. && !this->is_from_dynobj()
  336. && this->type() == elfcpp::STT_OBJECT)
  337. return true;
  338. // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
  339. // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
  340. if ((parameters->options().dynamic_list_cpp_new()
  341. || parameters->options().dynamic_list_cpp_typeinfo())
  342. && !this->is_from_dynobj())
  343. {
  344. // TODO(csilvers): We could probably figure out if we're an operator
  345. // new/delete or typeinfo without the need to demangle.
  346. char* demangled_name = cplus_demangle(this->name(),
  347. DMGL_ANSI | DMGL_PARAMS);
  348. if (demangled_name == NULL)
  349. {
  350. // Not a C++ symbol, so it can't satisfy these flags
  351. }
  352. else if (parameters->options().dynamic_list_cpp_new()
  353. && (strprefix(demangled_name, "operator new")
  354. || strprefix(demangled_name, "operator delete")))
  355. {
  356. free(demangled_name);
  357. return true;
  358. }
  359. else if (parameters->options().dynamic_list_cpp_typeinfo()
  360. && (strprefix(demangled_name, "typeinfo name for")
  361. || strprefix(demangled_name, "typeinfo for")))
  362. {
  363. free(demangled_name);
  364. return true;
  365. }
  366. else
  367. free(demangled_name);
  368. }
  369. // If exporting all symbols or building a shared library,
  370. // or the symbol should be globally unique (GNU_UNIQUE),
  371. // and the symbol is defined in a regular object and is
  372. // externally visible, we need to add it.
  373. if ((parameters->options().export_dynamic()
  374. || parameters->options().shared()
  375. || (parameters->options().gnu_unique()
  376. && this->binding() == elfcpp::STB_GNU_UNIQUE))
  377. && !this->is_from_dynobj()
  378. && !this->is_undefined()
  379. && this->is_externally_visible())
  380. return true;
  381. return false;
  382. }
  383. // Return true if the final value of this symbol is known at link
  384. // time.
  385. bool
  386. Symbol::final_value_is_known() const
  387. {
  388. // If we are not generating an executable, then no final values are
  389. // known, since they will change at runtime, with the exception of
  390. // TLS symbols in a position-independent executable.
  391. if ((parameters->options().output_is_position_independent()
  392. || parameters->options().relocatable())
  393. && !(this->type() == elfcpp::STT_TLS
  394. && parameters->options().pie()))
  395. return false;
  396. // If the symbol is not from an object file, and is not undefined,
  397. // then it is defined, and known.
  398. if (this->source_ != FROM_OBJECT)
  399. {
  400. if (this->source_ != IS_UNDEFINED)
  401. return true;
  402. }
  403. else
  404. {
  405. // If the symbol is from a dynamic object, then the final value
  406. // is not known.
  407. if (this->object()->is_dynamic())
  408. return false;
  409. // If the symbol is not undefined (it is defined or common),
  410. // then the final value is known.
  411. if (!this->is_undefined())
  412. return true;
  413. }
  414. // If the symbol is undefined, then whether the final value is known
  415. // depends on whether we are doing a static link. If we are doing a
  416. // dynamic link, then the final value could be filled in at runtime.
  417. // This could reasonably be the case for a weak undefined symbol.
  418. return parameters->doing_static_link();
  419. }
  420. // Return the output section where this symbol is defined.
  421. Output_section*
  422. Symbol::output_section() const
  423. {
  424. switch (this->source_)
  425. {
  426. case FROM_OBJECT:
  427. {
  428. unsigned int shndx = this->u2_.shndx;
  429. if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
  430. {
  431. gold_assert(!this->u1_.object->is_dynamic());
  432. gold_assert(this->u1_.object->pluginobj() == NULL);
  433. Relobj* relobj = static_cast<Relobj*>(this->u1_.object);
  434. return relobj->output_section(shndx);
  435. }
  436. return NULL;
  437. }
  438. case IN_OUTPUT_DATA:
  439. return this->u1_.output_data->output_section();
  440. case IN_OUTPUT_SEGMENT:
  441. case IS_CONSTANT:
  442. case IS_UNDEFINED:
  443. return NULL;
  444. default:
  445. gold_unreachable();
  446. }
  447. }
  448. // Set the symbol's output section. This is used for symbols defined
  449. // in scripts. This should only be called after the symbol table has
  450. // been finalized.
  451. void
  452. Symbol::set_output_section(Output_section* os)
  453. {
  454. switch (this->source_)
  455. {
  456. case FROM_OBJECT:
  457. case IN_OUTPUT_DATA:
  458. gold_assert(this->output_section() == os);
  459. break;
  460. case IS_CONSTANT:
  461. this->source_ = IN_OUTPUT_DATA;
  462. this->u1_.output_data = os;
  463. this->u2_.offset_is_from_end = false;
  464. break;
  465. case IN_OUTPUT_SEGMENT:
  466. case IS_UNDEFINED:
  467. default:
  468. gold_unreachable();
  469. }
  470. }
  471. // Set the symbol's output segment. This is used for pre-defined
  472. // symbols whose segments aren't known until after layout is done
  473. // (e.g., __ehdr_start).
  474. void
  475. Symbol::set_output_segment(Output_segment* os, Segment_offset_base base)
  476. {
  477. gold_assert(this->is_predefined_);
  478. this->source_ = IN_OUTPUT_SEGMENT;
  479. this->u1_.output_segment = os;
  480. this->u2_.offset_base = base;
  481. }
  482. // Set the symbol to undefined. This is used for pre-defined
  483. // symbols whose segments aren't known until after layout is done
  484. // (e.g., __ehdr_start).
  485. void
  486. Symbol::set_undefined()
  487. {
  488. this->source_ = IS_UNDEFINED;
  489. this->is_predefined_ = false;
  490. }
  491. // Class Symbol_table.
  492. Symbol_table::Symbol_table(unsigned int count,
  493. const Version_script_info& version_script)
  494. : saw_undefined_(0), offset_(0), has_gnu_output_(false), table_(count),
  495. namepool_(), forwarders_(), commons_(), tls_commons_(), small_commons_(),
  496. large_commons_(), forced_locals_(), warnings_(),
  497. version_script_(version_script), gc_(NULL), icf_(NULL),
  498. target_symbols_()
  499. {
  500. namepool_.reserve(count);
  501. }
  502. Symbol_table::~Symbol_table()
  503. {
  504. }
  505. // The symbol table key equality function. This is called with
  506. // Stringpool keys.
  507. inline bool
  508. Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
  509. const Symbol_table_key& k2) const
  510. {
  511. return k1.first == k2.first && k1.second == k2.second;
  512. }
  513. bool
  514. Symbol_table::is_section_folded(Relobj* obj, unsigned int shndx) const
  515. {
  516. return (parameters->options().icf_enabled()
  517. && this->icf_->is_section_folded(obj, shndx));
  518. }
  519. // For symbols that have been listed with a -u or --export-dynamic-symbol
  520. // option, add them to the work list to avoid gc'ing them.
  521. void
  522. Symbol_table::gc_mark_undef_symbols(Layout* layout)
  523. {
  524. for (options::String_set::const_iterator p =
  525. parameters->options().undefined_begin();
  526. p != parameters->options().undefined_end();
  527. ++p)
  528. {
  529. const char* name = p->c_str();
  530. Symbol* sym = this->lookup(name);
  531. gold_assert(sym != NULL);
  532. if (sym->source() == Symbol::FROM_OBJECT
  533. && !sym->object()->is_dynamic())
  534. {
  535. this->gc_mark_symbol(sym);
  536. }
  537. }
  538. for (options::String_set::const_iterator p =
  539. parameters->options().export_dynamic_symbol_begin();
  540. p != parameters->options().export_dynamic_symbol_end();
  541. ++p)
  542. {
  543. const char* name = p->c_str();
  544. Symbol* sym = this->lookup(name);
  545. // It's not an error if a symbol named by --export-dynamic-symbol
  546. // is undefined.
  547. if (sym != NULL
  548. && sym->source() == Symbol::FROM_OBJECT
  549. && !sym->object()->is_dynamic())
  550. {
  551. this->gc_mark_symbol(sym);
  552. }
  553. }
  554. for (Script_options::referenced_const_iterator p =
  555. layout->script_options()->referenced_begin();
  556. p != layout->script_options()->referenced_end();
  557. ++p)
  558. {
  559. Symbol* sym = this->lookup(p->c_str());
  560. gold_assert(sym != NULL);
  561. if (sym->source() == Symbol::FROM_OBJECT
  562. && !sym->object()->is_dynamic())
  563. {
  564. this->gc_mark_symbol(sym);
  565. }
  566. }
  567. }
  568. void
  569. Symbol_table::gc_mark_symbol(Symbol* sym)
  570. {
  571. // Add the object and section to the work list.
  572. bool is_ordinary;
  573. unsigned int shndx = sym->shndx(&is_ordinary);
  574. if (is_ordinary && shndx != elfcpp::SHN_UNDEF && !sym->object()->is_dynamic())
  575. {
  576. gold_assert(this->gc_!= NULL);
  577. Relobj* relobj = static_cast<Relobj*>(sym->object());
  578. this->gc_->worklist().push_back(Section_id(relobj, shndx));
  579. }
  580. parameters->target().gc_mark_symbol(this, sym);
  581. }
  582. // When doing garbage collection, keep symbols that have been seen in
  583. // dynamic objects.
  584. inline void
  585. Symbol_table::gc_mark_dyn_syms(Symbol* sym)
  586. {
  587. if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
  588. && !sym->object()->is_dynamic())
  589. this->gc_mark_symbol(sym);
  590. }
  591. // Make TO a symbol which forwards to FROM.
  592. void
  593. Symbol_table::make_forwarder(Symbol* from, Symbol* to)
  594. {
  595. gold_assert(from != to);
  596. gold_assert(!from->is_forwarder() && !to->is_forwarder());
  597. this->forwarders_[from] = to;
  598. from->set_forwarder();
  599. }
  600. // Resolve the forwards from FROM, returning the real symbol.
  601. Symbol*
  602. Symbol_table::resolve_forwards(const Symbol* from) const
  603. {
  604. gold_assert(from->is_forwarder());
  605. Unordered_map<const Symbol*, Symbol*>::const_iterator p =
  606. this->forwarders_.find(from);
  607. gold_assert(p != this->forwarders_.end());
  608. return p->second;
  609. }
  610. // Look up a symbol by name.
  611. Symbol*
  612. Symbol_table::lookup(const char* name, const char* version) const
  613. {
  614. Stringpool::Key name_key;
  615. name = this->namepool_.find(name, &name_key);
  616. if (name == NULL)
  617. return NULL;
  618. Stringpool::Key version_key = 0;
  619. if (version != NULL)
  620. {
  621. version = this->namepool_.find(version, &version_key);
  622. if (version == NULL)
  623. return NULL;
  624. }
  625. Symbol_table_key key(name_key, version_key);
  626. Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
  627. if (p == this->table_.end())
  628. return NULL;
  629. return p->second;
  630. }
  631. // Resolve a Symbol with another Symbol. This is only used in the
  632. // unusual case where there are references to both an unversioned
  633. // symbol and a symbol with a version, and we then discover that that
  634. // version is the default version. Because this is unusual, we do
  635. // this the slow way, by converting back to an ELF symbol.
  636. template<int size, bool big_endian>
  637. void
  638. Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
  639. {
  640. unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
  641. elfcpp::Sym_write<size, big_endian> esym(buf);
  642. // We don't bother to set the st_name or the st_shndx field.
  643. esym.put_st_value(from->value());
  644. esym.put_st_size(from->symsize());
  645. esym.put_st_info(from->binding(), from->type());
  646. esym.put_st_other(from->visibility(), from->nonvis());
  647. bool is_ordinary;
  648. unsigned int shndx = from->shndx(&is_ordinary);
  649. this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
  650. from->version(), true);
  651. if (from->in_reg())
  652. to->set_in_reg();
  653. if (from->in_dyn())
  654. to->set_in_dyn();
  655. if (parameters->options().gc_sections())
  656. this->gc_mark_dyn_syms(to);
  657. }
  658. // Record that a symbol is forced to be local by a version script or
  659. // by visibility.
  660. void
  661. Symbol_table::force_local(Symbol* sym)
  662. {
  663. if (!sym->is_defined() && !sym->is_common())
  664. return;
  665. if (sym->is_forced_local())
  666. {
  667. // We already got this one.
  668. return;
  669. }
  670. sym->set_is_forced_local();
  671. this->forced_locals_.push_back(sym);
  672. }
  673. // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
  674. // is only called for undefined symbols, when at least one --wrap
  675. // option was used.
  676. const char*
  677. Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key)
  678. {
  679. // For some targets, we need to ignore a specific character when
  680. // wrapping, and add it back later.
  681. char prefix = '\0';
  682. if (name[0] == parameters->target().wrap_char())
  683. {
  684. prefix = name[0];
  685. ++name;
  686. }
  687. if (parameters->options().is_wrap(name))
  688. {
  689. // Turn NAME into __wrap_NAME.
  690. std::string s;
  691. if (prefix != '\0')
  692. s += prefix;
  693. s += "__wrap_";
  694. s += name;
  695. // This will give us both the old and new name in NAMEPOOL_, but
  696. // that is OK. Only the versions we need will wind up in the
  697. // real string table in the output file.
  698. return this->namepool_.add(s.c_str(), true, name_key);
  699. }
  700. const char* const real_prefix = "__real_";
  701. const size_t real_prefix_length = strlen(real_prefix);
  702. if (strncmp(name, real_prefix, real_prefix_length) == 0
  703. && parameters->options().is_wrap(name + real_prefix_length))
  704. {
  705. // Turn __real_NAME into NAME.
  706. std::string s;
  707. if (prefix != '\0')
  708. s += prefix;
  709. s += name + real_prefix_length;
  710. return this->namepool_.add(s.c_str(), true, name_key);
  711. }
  712. return name;
  713. }
  714. // This is called when we see a symbol NAME/VERSION, and the symbol
  715. // already exists in the symbol table, and VERSION is marked as being
  716. // the default version. SYM is the NAME/VERSION symbol we just added.
  717. // DEFAULT_IS_NEW is true if this is the first time we have seen the
  718. // symbol NAME/NULL. PDEF points to the entry for NAME/NULL.
  719. template<int size, bool big_endian>
  720. void
  721. Symbol_table::define_default_version(Sized_symbol<size>* sym,
  722. bool default_is_new,
  723. Symbol_table_type::iterator pdef)
  724. {
  725. if (default_is_new)
  726. {
  727. // This is the first time we have seen NAME/NULL. Make
  728. // NAME/NULL point to NAME/VERSION, and mark SYM as the default
  729. // version.
  730. pdef->second = sym;
  731. sym->set_is_default();
  732. }
  733. else if (pdef->second == sym)
  734. {
  735. // NAME/NULL already points to NAME/VERSION. Don't mark the
  736. // symbol as the default if it is not already the default.
  737. }
  738. else
  739. {
  740. // This is the unfortunate case where we already have entries
  741. // for both NAME/VERSION and NAME/NULL. We now see a symbol
  742. // NAME/VERSION where VERSION is the default version. We have
  743. // already resolved this new symbol with the existing
  744. // NAME/VERSION symbol.
  745. // It's possible that NAME/NULL and NAME/VERSION are both
  746. // defined in regular objects. This can only happen if one
  747. // object file defines foo and another defines foo@@ver. This
  748. // is somewhat obscure, but we call it a multiple definition
  749. // error.
  750. // It's possible that NAME/NULL actually has a version, in which
  751. // case it won't be the same as VERSION. This happens with
  752. // ver_test_7.so in the testsuite for the symbol t2_2. We see
  753. // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We
  754. // then see an unadorned t2_2 in an object file and give it
  755. // version VER1 from the version script. This looks like a
  756. // default definition for VER1, so it looks like we should merge
  757. // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's
  758. // not obvious that this is an error, either. So we just punt.
  759. // If one of the symbols has non-default visibility, and the
  760. // other is defined in a shared object, then they are different
  761. // symbols.
  762. // If the two symbols are from different shared objects,
  763. // they are different symbols.
  764. // Otherwise, we just resolve the symbols as though they were
  765. // the same.
  766. if (pdef->second->version() != NULL)
  767. gold_assert(pdef->second->version() != sym->version());
  768. else if (sym->visibility() != elfcpp::STV_DEFAULT
  769. && pdef->second->is_from_dynobj())
  770. ;
  771. else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
  772. && sym->is_from_dynobj())
  773. ;
  774. else if (pdef->second->is_from_dynobj()
  775. && sym->is_from_dynobj()
  776. && pdef->second->is_defined()
  777. && pdef->second->object() != sym->object())
  778. ;
  779. else
  780. {
  781. const Sized_symbol<size>* symdef;
  782. symdef = this->get_sized_symbol<size>(pdef->second);
  783. Symbol_table::resolve<size, big_endian>(sym, symdef);
  784. this->make_forwarder(pdef->second, sym);
  785. pdef->second = sym;
  786. sym->set_is_default();
  787. }
  788. }
  789. }
  790. // Add one symbol from OBJECT to the symbol table. NAME is symbol
  791. // name and VERSION is the version; both are canonicalized. DEF is
  792. // whether this is the default version. ST_SHNDX is the symbol's
  793. // section index; IS_ORDINARY is whether this is a normal section
  794. // rather than a special code.
  795. // If IS_DEFAULT_VERSION is true, then this is the definition of a
  796. // default version of a symbol. That means that any lookup of
  797. // NAME/NULL and any lookup of NAME/VERSION should always return the
  798. // same symbol. This is obvious for references, but in particular we
  799. // want to do this for definitions: overriding NAME/NULL should also
  800. // override NAME/VERSION. If we don't do that, it would be very hard
  801. // to override functions in a shared library which uses versioning.
  802. // We implement this by simply making both entries in the hash table
  803. // point to the same Symbol structure. That is easy enough if this is
  804. // the first time we see NAME/NULL or NAME/VERSION, but it is possible
  805. // that we have seen both already, in which case they will both have
  806. // independent entries in the symbol table. We can't simply change
  807. // the symbol table entry, because we have pointers to the entries
  808. // attached to the object files. So we mark the entry attached to the
  809. // object file as a forwarder, and record it in the forwarders_ map.
  810. // Note that entries in the hash table will never be marked as
  811. // forwarders.
  812. //
  813. // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
  814. // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
  815. // for a special section code. ST_SHNDX may be modified if the symbol
  816. // is defined in a section being discarded.
  817. template<int size, bool big_endian>
  818. Sized_symbol<size>*
  819. Symbol_table::add_from_object(Object* object,
  820. const char* name,
  821. Stringpool::Key name_key,
  822. const char* version,
  823. Stringpool::Key version_key,
  824. bool is_default_version,
  825. const elfcpp::Sym<size, big_endian>& sym,
  826. unsigned int st_shndx,
  827. bool is_ordinary,
  828. unsigned int orig_st_shndx)
  829. {
  830. // Print a message if this symbol is being traced.
  831. if (parameters->options().is_trace_symbol(name))
  832. {
  833. if (orig_st_shndx == elfcpp::SHN_UNDEF)
  834. gold_info(_("%s: reference to %s"), object->name().c_str(), name);
  835. else
  836. gold_info(_("%s: definition of %s"), object->name().c_str(), name);
  837. }
  838. // For an undefined symbol, we may need to adjust the name using
  839. // --wrap.
  840. if (orig_st_shndx == elfcpp::SHN_UNDEF
  841. && parameters->options().any_wrap())
  842. {
  843. const char* wrap_name = this->wrap_symbol(name, &name_key);
  844. if (wrap_name != name)
  845. {
  846. // If we see a reference to malloc with version GLIBC_2.0,
  847. // and we turn it into a reference to __wrap_malloc, then we
  848. // discard the version number. Otherwise the user would be
  849. // required to specify the correct version for
  850. // __wrap_malloc.
  851. version = NULL;
  852. version_key = 0;
  853. name = wrap_name;
  854. }
  855. }
  856. Symbol* const snull = NULL;
  857. std::pair<typename Symbol_table_type::iterator, bool> ins =
  858. this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
  859. snull));
  860. std::pair<typename Symbol_table_type::iterator, bool> insdefault =
  861. std::make_pair(this->table_.end(), false);
  862. if (is_default_version)
  863. {
  864. const Stringpool::Key vnull_key = 0;
  865. insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
  866. vnull_key),
  867. snull));
  868. }
  869. // ins.first: an iterator, which is a pointer to a pair.
  870. // ins.first->first: the key (a pair of name and version).
  871. // ins.first->second: the value (Symbol*).
  872. // ins.second: true if new entry was inserted, false if not.
  873. Sized_symbol<size>* ret = NULL;
  874. bool was_undefined_in_reg;
  875. bool was_common;
  876. if (!ins.second)
  877. {
  878. // We already have an entry for NAME/VERSION.
  879. ret = this->get_sized_symbol<size>(ins.first->second);
  880. gold_assert(ret != NULL);
  881. was_undefined_in_reg = ret->is_undefined() && ret->in_reg();
  882. // Commons from plugins are just placeholders.
  883. was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
  884. this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
  885. version, is_default_version);
  886. if (parameters->options().gc_sections())
  887. this->gc_mark_dyn_syms(ret);
  888. if (is_default_version)
  889. this->define_default_version<size, big_endian>(ret, insdefault.second,
  890. insdefault.first);
  891. else
  892. {
  893. bool dummy;
  894. if (version != NULL
  895. && ret->source() == Symbol::FROM_OBJECT
  896. && ret->object() == object
  897. && is_ordinary
  898. && ret->shndx(&dummy) == st_shndx
  899. && ret->is_default())
  900. {
  901. // We have seen NAME/VERSION already, and marked it as the
  902. // default version, but now we see a definition for
  903. // NAME/VERSION that is not the default version. This can
  904. // happen when the assembler generates two symbols for
  905. // a symbol as a result of a ".symver foo,foo@VER"
  906. // directive. We see the first unversioned symbol and
  907. // we may mark it as the default version (from a
  908. // version script); then we see the second versioned
  909. // symbol and we need to override the first.
  910. // In any other case, the two symbols should have generated
  911. // a multiple definition error.
  912. // (See PR gold/18703.)
  913. ret->set_is_not_default();
  914. const Stringpool::Key vnull_key = 0;
  915. this->table_.erase(std::make_pair(name_key, vnull_key));
  916. }
  917. }
  918. }
  919. else
  920. {
  921. // This is the first time we have seen NAME/VERSION.
  922. gold_assert(ins.first->second == NULL);
  923. if (is_default_version && !insdefault.second)
  924. {
  925. // We already have an entry for NAME/NULL. If we override
  926. // it, then change it to NAME/VERSION.
  927. ret = this->get_sized_symbol<size>(insdefault.first->second);
  928. // If the existing symbol already has a version,
  929. // don't override it with the new symbol.
  930. // This should only happen when the new symbol
  931. // is from a shared library.
  932. if (ret->version() != NULL)
  933. {
  934. if (!object->is_dynamic())
  935. {
  936. gold_warning(_("%s: conflicting default version definition"
  937. " for %s@@%s"),
  938. object->name().c_str(), name, version);
  939. if (ret->source() == Symbol::FROM_OBJECT)
  940. gold_info(_("%s: %s: previous definition of %s@@%s here"),
  941. program_name,
  942. ret->object()->name().c_str(),
  943. name, ret->version());
  944. }
  945. ret = NULL;
  946. is_default_version = false;
  947. }
  948. else
  949. {
  950. was_undefined_in_reg = ret->is_undefined() && ret->in_reg();
  951. // Commons from plugins are just placeholders.
  952. was_common = (ret->is_common()
  953. && ret->object()->pluginobj() == NULL);
  954. this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx,
  955. object, version, is_default_version);
  956. if (parameters->options().gc_sections())
  957. this->gc_mark_dyn_syms(ret);
  958. ins.first->second = ret;
  959. }
  960. }
  961. if (ret == NULL)
  962. {
  963. was_undefined_in_reg = false;
  964. was_common = false;
  965. Sized_target<size, big_endian>* target =
  966. parameters->sized_target<size, big_endian>();
  967. if (!target->has_make_symbol())
  968. ret = new Sized_symbol<size>();
  969. else
  970. {
  971. ret = target->make_symbol(name, sym.get_st_type(), object,
  972. st_shndx, sym.get_st_value());
  973. if (ret == NULL)
  974. {
  975. // This means that we don't want a symbol table
  976. // entry after all.
  977. if (!is_default_version)
  978. this->table_.erase(ins.first);
  979. else
  980. {
  981. this->table_.erase(insdefault.first);
  982. // Inserting INSDEFAULT invalidated INS.
  983. this->table_.erase(std::make_pair(name_key,
  984. version_key));
  985. }
  986. return NULL;
  987. }
  988. }
  989. ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
  990. ins.first->second = ret;
  991. if (is_default_version)
  992. {
  993. // This is the first time we have seen NAME/NULL. Point
  994. // it at the new entry for NAME/VERSION.
  995. gold_assert(insdefault.second);
  996. insdefault.first->second = ret;
  997. }
  998. }
  999. if (is_default_version)
  1000. ret->set_is_default();
  1001. }
  1002. // Record every time we see a new undefined symbol, to speed up archive
  1003. // groups. We only care about symbols undefined in regular objects here
  1004. // because undefined symbols only in dynamic objects should't trigger rescans.
  1005. if (!was_undefined_in_reg && ret->is_undefined() && ret->in_reg())
  1006. {
  1007. ++this->saw_undefined_;
  1008. if (parameters->options().has_plugins())
  1009. parameters->options().plugins()->new_undefined_symbol(ret);
  1010. }
  1011. // Keep track of common symbols, to speed up common symbol
  1012. // allocation. Don't record commons from plugin objects;
  1013. // we need to wait until we see the real symbol in the
  1014. // replacement file.
  1015. if (!was_common && ret->is_common() && ret->object()->pluginobj() == NULL)
  1016. {
  1017. if (ret->type() == elfcpp::STT_TLS)
  1018. this->tls_commons_.push_back(ret);
  1019. else if (!is_ordinary
  1020. && st_shndx == parameters->target().small_common_shndx())
  1021. this->small_commons_.push_back(ret);
  1022. else if (!is_ordinary
  1023. && st_shndx == parameters->target().large_common_shndx())
  1024. this->large_commons_.push_back(ret);
  1025. else
  1026. this->commons_.push_back(ret);
  1027. }
  1028. // If we're not doing a relocatable link, then any symbol with
  1029. // hidden or internal visibility is local.
  1030. if ((ret->visibility() == elfcpp::STV_HIDDEN
  1031. || ret->visibility() == elfcpp::STV_INTERNAL)
  1032. && (ret->binding() == elfcpp::STB_GLOBAL
  1033. || ret->binding() == elfcpp::STB_GNU_UNIQUE
  1034. || ret->binding() == elfcpp::STB_WEAK)
  1035. && !parameters->options().relocatable())
  1036. this->force_local(ret);
  1037. return ret;
  1038. }
  1039. // Add all the symbols in a relocatable object to the hash table.
  1040. template<int size, bool big_endian>
  1041. void
  1042. Symbol_table::add_from_relobj(
  1043. Sized_relobj_file<size, big_endian>* relobj,
  1044. const unsigned char* syms,
  1045. size_t count,
  1046. size_t symndx_offset,
  1047. const char* sym_names,
  1048. size_t sym_name_size,
  1049. typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
  1050. size_t* defined)
  1051. {
  1052. *defined = 0;
  1053. gold_assert(size == parameters->target().get_size());
  1054. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1055. const bool just_symbols = relobj->just_symbols();
  1056. const unsigned char* p = syms;
  1057. for (size_t i = 0; i < count; ++i, p += sym_size)
  1058. {
  1059. (*sympointers)[i] = NULL;
  1060. elfcpp::Sym<size, big_endian> sym(p);
  1061. unsigned int st_name = sym.get_st_name();
  1062. if (st_name >= sym_name_size)
  1063. {
  1064. relobj->error(_("bad global symbol name offset %u at %zu"),
  1065. st_name, i);
  1066. continue;
  1067. }
  1068. const char* name = sym_names + st_name;
  1069. if (!parameters->options().relocatable()
  1070. && name[0] == '_'
  1071. && name[1] == '_'
  1072. && strcmp (name + (name[2] == '_'), "__gnu_lto_slim") == 0)
  1073. gold_info(_("%s: plugin needed to handle lto object"),
  1074. relobj->name().c_str());
  1075. bool is_ordinary;
  1076. unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
  1077. sym.get_st_shndx(),
  1078. &is_ordinary);
  1079. unsigned int orig_st_shndx = st_shndx;
  1080. if (!is_ordinary)
  1081. orig_st_shndx = elfcpp::SHN_UNDEF;
  1082. if (st_shndx != elfcpp::SHN_UNDEF)
  1083. ++*defined;
  1084. // A symbol defined in a section which we are not including must
  1085. // be treated as an undefined symbol.
  1086. bool is_defined_in_discarded_section = false;
  1087. if (st_shndx != elfcpp::SHN_UNDEF
  1088. && is_ordinary
  1089. && !relobj->is_section_included(st_shndx)
  1090. && !this->is_section_folded(relobj, st_shndx))
  1091. {
  1092. st_shndx = elfcpp::SHN_UNDEF;
  1093. is_defined_in_discarded_section = true;
  1094. }
  1095. // In an object file, an '@' in the name separates the symbol
  1096. // name from the version name. If there are two '@' characters,
  1097. // this is the default version.
  1098. const char* ver = strchr(name, '@');
  1099. Stringpool::Key ver_key = 0;
  1100. int namelen = 0;
  1101. // IS_DEFAULT_VERSION: is the version default?
  1102. // IS_FORCED_LOCAL: is the symbol forced local?
  1103. bool is_default_version = false;
  1104. bool is_forced_local = false;
  1105. // FIXME: For incremental links, we don't store version information,
  1106. // so we need to ignore version symbols for now.
  1107. if (parameters->incremental_update() && ver != NULL)
  1108. {
  1109. namelen = ver - name;
  1110. ver = NULL;
  1111. }
  1112. if (ver != NULL)
  1113. {
  1114. // The symbol name is of the form foo@VERSION or foo@@VERSION
  1115. namelen = ver - name;
  1116. ++ver;
  1117. if (*ver == '@')
  1118. {
  1119. is_default_version = true;
  1120. ++ver;
  1121. }
  1122. ver = this->namepool_.add(ver, true, &ver_key);
  1123. }
  1124. // We don't want to assign a version to an undefined symbol,
  1125. // even if it is listed in the version script. FIXME: What
  1126. // about a common symbol?
  1127. else
  1128. {
  1129. namelen = strlen(name);
  1130. if (!this->version_script_.empty()
  1131. && st_shndx != elfcpp::SHN_UNDEF)
  1132. {
  1133. // The symbol name did not have a version, but the
  1134. // version script may assign a version anyway.
  1135. std::string version;
  1136. bool is_global;
  1137. if (this->version_script_.get_symbol_version(name, &version,
  1138. &is_global))
  1139. {
  1140. if (!is_global)
  1141. is_forced_local = true;
  1142. else if (!version.empty())
  1143. {
  1144. ver = this->namepool_.add_with_length(version.c_str(),
  1145. version.length(),
  1146. true,
  1147. &ver_key);
  1148. is_default_version = true;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. elfcpp::Sym<size, big_endian>* psym = &sym;
  1154. unsigned char symbuf[sym_size];
  1155. elfcpp::Sym<size, big_endian> sym2(symbuf);
  1156. if (just_symbols)
  1157. {
  1158. memcpy(symbuf, p, sym_size);
  1159. elfcpp::Sym_write<size, big_endian> sw(symbuf);
  1160. if (orig_st_shndx != elfcpp::SHN_UNDEF
  1161. && is_ordinary
  1162. && relobj->e_type() == elfcpp::ET_REL)
  1163. {
  1164. // Symbol values in relocatable object files are section
  1165. // relative. This is normally what we want, but since here
  1166. // we are converting the symbol to absolute we need to add
  1167. // the section address. The section address in an object
  1168. // file is normally zero, but people can use a linker
  1169. // script to change it.
  1170. sw.put_st_value(sym.get_st_value()
  1171. + relobj->section_address(orig_st_shndx));
  1172. }
  1173. st_shndx = elfcpp::SHN_ABS;
  1174. is_ordinary = false;
  1175. psym = &sym2;
  1176. }
  1177. // Fix up visibility if object has no-export set.
  1178. if (relobj->no_export()
  1179. && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
  1180. {
  1181. // We may have copied symbol already above.
  1182. if (psym != &sym2)
  1183. {
  1184. memcpy(symbuf, p, sym_size);
  1185. psym = &sym2;
  1186. }
  1187. elfcpp::STV visibility = sym2.get_st_visibility();
  1188. if (visibility == elfcpp::STV_DEFAULT
  1189. || visibility == elfcpp::STV_PROTECTED)
  1190. {
  1191. elfcpp::Sym_write<size, big_endian> sw(symbuf);
  1192. unsigned char nonvis = sym2.get_st_nonvis();
  1193. sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
  1194. }
  1195. }
  1196. Stringpool::Key name_key;
  1197. name = this->namepool_.add_with_length(name, namelen, true,
  1198. &name_key);
  1199. Sized_symbol<size>* res;
  1200. res = this->add_from_object(relobj, name, name_key, ver, ver_key,
  1201. is_default_version, *psym, st_shndx,
  1202. is_ordinary, orig_st_shndx);
  1203. if (res == NULL)
  1204. continue;
  1205. if (is_forced_local)
  1206. this->force_local(res);
  1207. // Do not treat this symbol as garbage if this symbol will be
  1208. // exported to the dynamic symbol table. This is true when
  1209. // building a shared library or using --export-dynamic and
  1210. // the symbol is externally visible.
  1211. if (parameters->options().gc_sections()
  1212. && res->is_externally_visible()
  1213. && !res->is_from_dynobj()
  1214. && (parameters->options().shared()
  1215. || parameters->options().export_dynamic()
  1216. || parameters->options().in_dynamic_list(res->name())))
  1217. this->gc_mark_symbol(res);
  1218. if (is_defined_in_discarded_section)
  1219. res->set_is_defined_in_discarded_section();
  1220. (*sympointers)[i] = res;
  1221. }
  1222. }
  1223. // Add a symbol from a plugin-claimed file.
  1224. template<int size, bool big_endian>
  1225. Symbol*
  1226. Symbol_table::add_from_pluginobj(
  1227. Sized_pluginobj<size, big_endian>* obj,
  1228. const char* name,
  1229. const char* ver,
  1230. elfcpp::Sym<size, big_endian>* sym)
  1231. {
  1232. unsigned int st_shndx = sym->get_st_shndx();
  1233. bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
  1234. Stringpool::Key ver_key = 0;
  1235. bool is_default_version = false;
  1236. bool is_forced_local = false;
  1237. if (ver != NULL)
  1238. {
  1239. ver = this->namepool_.add(ver, true, &ver_key);
  1240. }
  1241. // We don't want to assign a version to an undefined symbol,
  1242. // even if it is listed in the version script. FIXME: What
  1243. // about a common symbol?
  1244. else
  1245. {
  1246. if (!this->version_script_.empty()
  1247. && st_shndx != elfcpp::SHN_UNDEF)
  1248. {
  1249. // The symbol name did not have a version, but the
  1250. // version script may assign a version anyway.
  1251. std::string version;
  1252. bool is_global;
  1253. if (this->version_script_.get_symbol_version(name, &version,
  1254. &is_global))
  1255. {
  1256. if (!is_global)
  1257. is_forced_local = true;
  1258. else if (!version.empty())
  1259. {
  1260. ver = this->namepool_.add_with_length(version.c_str(),
  1261. version.length(),
  1262. true,
  1263. &ver_key);
  1264. is_default_version = true;
  1265. }
  1266. }
  1267. }
  1268. }
  1269. Stringpool::Key name_key;
  1270. name = this->namepool_.add(name, true, &name_key);
  1271. Sized_symbol<size>* res;
  1272. res = this->add_from_object(obj, name, name_key, ver, ver_key,
  1273. is_default_version, *sym, st_shndx,
  1274. is_ordinary, st_shndx);
  1275. if (res == NULL)
  1276. return NULL;
  1277. if (is_forced_local)
  1278. this->force_local(res);
  1279. return res;
  1280. }
  1281. // Add all the symbols in a dynamic object to the hash table.
  1282. template<int size, bool big_endian>
  1283. void
  1284. Symbol_table::add_from_dynobj(
  1285. Sized_dynobj<size, big_endian>* dynobj,
  1286. const unsigned char* syms,
  1287. size_t count,
  1288. const char* sym_names,
  1289. size_t sym_name_size,
  1290. const unsigned char* versym,
  1291. size_t versym_size,
  1292. const std::vector<const char*>* version_map,
  1293. typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
  1294. size_t* defined)
  1295. {
  1296. *defined = 0;
  1297. gold_assert(size == parameters->target().get_size());
  1298. if (dynobj->just_symbols())
  1299. {
  1300. gold_error(_("--just-symbols does not make sense with a shared object"));
  1301. return;
  1302. }
  1303. // FIXME: For incremental links, we don't store version information,
  1304. // so we need to ignore version symbols for now.
  1305. if (parameters->incremental_update())
  1306. versym = NULL;
  1307. if (versym != NULL && versym_size / 2 < count)
  1308. {
  1309. dynobj->error(_("too few symbol versions"));
  1310. return;
  1311. }
  1312. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  1313. // We keep a list of all STT_OBJECT symbols, so that we can resolve
  1314. // weak aliases. This is necessary because if the dynamic object
  1315. // provides the same variable under two names, one of which is a
  1316. // weak definition, and the regular object refers to the weak
  1317. // definition, we have to put both the weak definition and the
  1318. // strong definition into the dynamic symbol table. Given a weak
  1319. // definition, the only way that we can find the corresponding
  1320. // strong definition, if any, is to search the symbol table.
  1321. std::vector<Sized_symbol<size>*> object_symbols;
  1322. const unsigned char* p = syms;
  1323. const unsigned char* vs = versym;
  1324. for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
  1325. {
  1326. elfcpp::Sym<size, big_endian> sym(p);
  1327. if (sympointers != NULL)
  1328. (*sympointers)[i] = NULL;
  1329. // Ignore symbols with local binding or that have
  1330. // internal or hidden visibility.
  1331. if (sym.get_st_bind() == elfcpp::STB_LOCAL
  1332. || sym.get_st_visibility() == elfcpp::STV_INTERNAL
  1333. || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
  1334. continue;
  1335. // A protected symbol in a shared library must be treated as a
  1336. // normal symbol when viewed from outside the shared library.
  1337. // Implement this by overriding the visibility here.
  1338. // Likewise, an IFUNC symbol in a shared library must be treated
  1339. // as a normal FUNC symbol.
  1340. elfcpp::Sym<size, big_endian>* psym = &sym;
  1341. unsigned char symbuf[sym_size];
  1342. elfcpp::Sym<size, big_endian> sym2(symbuf);
  1343. if (sym.get_st_visibility() == elfcpp::STV_PROTECTED
  1344. || sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
  1345. {
  1346. memcpy(symbuf, p, sym_size);
  1347. elfcpp::Sym_write<size, big_endian> sw(symbuf);
  1348. if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
  1349. sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
  1350. if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
  1351. sw.put_st_info(sym.get_st_bind(), elfcpp::STT_FUNC);
  1352. psym = &sym2;
  1353. }
  1354. unsigned int st_name = psym->get_st_name();
  1355. if (st_name >= sym_name_size)
  1356. {
  1357. dynobj->error(_("bad symbol name offset %u at %zu"),
  1358. st_name, i);
  1359. continue;
  1360. }
  1361. const char* name = sym_names + st_name;
  1362. bool is_ordinary;
  1363. unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
  1364. &is_ordinary);
  1365. if (st_shndx != elfcpp::SHN_UNDEF)
  1366. ++*defined;
  1367. Sized_symbol<size>* res;
  1368. if (versym == NULL)
  1369. {
  1370. Stringpool::Key name_key;
  1371. name = this->namepool_.add(name, true, &name_key);
  1372. res = this->add_from_object(dynobj, name, name_key, NULL, 0,
  1373. false, *psym, st_shndx, is_ordinary,
  1374. st_shndx);
  1375. }
  1376. else
  1377. {
  1378. // Read the version information.
  1379. unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
  1380. bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
  1381. v &= elfcpp::VERSYM_VERSION;
  1382. // The Sun documentation says that V can be VER_NDX_LOCAL,
  1383. // or VER_NDX_GLOBAL, or a version index. The meaning of
  1384. // VER_NDX_LOCAL is defined as "Symbol has local scope."
  1385. // The old GNU linker will happily generate VER_NDX_LOCAL
  1386. // for an undefined symbol. I don't know what the Sun
  1387. // linker will generate.
  1388. if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
  1389. && st_shndx != elfcpp::SHN_UNDEF)
  1390. {
  1391. // This symbol should not be visible outside the object.
  1392. continue;
  1393. }
  1394. // At this point we are definitely going to add this symbol.
  1395. Stringpool::Key name_key;
  1396. name = this->namepool_.add(name, true, &name_key);
  1397. if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
  1398. || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
  1399. {
  1400. // This symbol does not have a version.
  1401. res = this->add_from_object(dynobj, name, name_key, NULL, 0,
  1402. false, *psym, st_shndx, is_ordinary,
  1403. st_shndx);
  1404. }
  1405. else
  1406. {
  1407. if (v >= version_map->size())
  1408. {
  1409. dynobj->error(_("versym for symbol %zu out of range: %u"),
  1410. i, v);
  1411. continue;
  1412. }
  1413. const char* version = (*version_map)[v];
  1414. if (version == NULL)
  1415. {
  1416. dynobj->error(_("versym for symbol %zu has no name: %u"),
  1417. i, v);
  1418. continue;
  1419. }
  1420. Stringpool::Key version_key;
  1421. version = this->namepool_.add(version, true, &version_key);
  1422. // If this is an absolute symbol, and the version name
  1423. // and symbol name are the same, then this is the
  1424. // version definition symbol. These symbols exist to
  1425. // support using -u to pull in particular versions. We
  1426. // do not want to record a version for them.
  1427. if (st_shndx == elfcpp::SHN_ABS
  1428. && !is_ordinary
  1429. && name_key == version_key)
  1430. res = this->add_from_object(dynobj, name, name_key, NULL, 0,
  1431. false, *psym, st_shndx, is_ordinary,
  1432. st_shndx);
  1433. else
  1434. {
  1435. const bool is_default_version =
  1436. !hidden && st_shndx != elfcpp::SHN_UNDEF;
  1437. res = this->add_from_object(dynobj, name, name_key, version,
  1438. version_key, is_default_version,
  1439. *psym, st_shndx,
  1440. is_ordinary, st_shndx);
  1441. }
  1442. }
  1443. }
  1444. if (res == NULL)
  1445. continue;
  1446. // Note that it is possible that RES was overridden by an
  1447. // earlier object, in which case it can't be aliased here.
  1448. if (st_shndx != elfcpp::SHN_UNDEF
  1449. && is_ordinary
  1450. && psym->get_st_type() == elfcpp::STT_OBJECT
  1451. && res->source() == Symbol::FROM_OBJECT
  1452. && res->object() == dynobj)
  1453. object_symbols.push_back(res);
  1454. // If the symbol has protected visibility in the dynobj,
  1455. // mark it as such if it was not overridden.
  1456. if (res->source() == Symbol::FROM_OBJECT
  1457. && res->object() == dynobj
  1458. && sym.get_st_visibility() == elfcpp::STV_PROTECTED)
  1459. res->set_is_protected();
  1460. if (sympointers != NULL)
  1461. (*sympointers)[i] = res;
  1462. }
  1463. this->record_weak_aliases(&object_symbols);
  1464. }
  1465. // Add a symbol from a incremental object file.
  1466. template<int size, bool big_endian>
  1467. Sized_symbol<size>*
  1468. Symbol_table::add_from_incrobj(
  1469. Object* obj,
  1470. const char* name,
  1471. const char* ver,
  1472. elfcpp::Sym<size, big_endian>* sym)
  1473. {
  1474. unsigned int st_shndx = sym->get_st_shndx();
  1475. bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
  1476. Stringpool::Key ver_key = 0;
  1477. bool is_default_version = false;
  1478. Stringpool::Key name_key;
  1479. name = this->namepool_.add(name, true, &name_key);
  1480. Sized_symbol<size>* res;
  1481. res = this->add_from_object(obj, name, name_key, ver, ver_key,
  1482. is_default_version, *sym, st_shndx,
  1483. is_ordinary, st_shndx);
  1484. return res;
  1485. }
  1486. // This is used to sort weak aliases. We sort them first by section
  1487. // index, then by offset, then by weak ahead of strong.
  1488. template<int size>
  1489. class Weak_alias_sorter
  1490. {
  1491. public:
  1492. bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
  1493. };
  1494. template<int size>
  1495. bool
  1496. Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
  1497. const Sized_symbol<size>* s2) const
  1498. {
  1499. bool is_ordinary;
  1500. unsigned int s1_shndx = s1->shndx(&is_ordinary);
  1501. gold_assert(is_ordinary);
  1502. unsigned int s2_shndx = s2->shndx(&is_ordinary);
  1503. gold_assert(is_ordinary);
  1504. if (s1_shndx != s2_shndx)
  1505. return s1_shndx < s2_shndx;
  1506. if (s1->value() != s2->value())
  1507. return s1->value() < s2->value();
  1508. if (s1->binding() != s2->binding())
  1509. {
  1510. if (s1->binding() == elfcpp::STB_WEAK)
  1511. return true;
  1512. if (s2->binding() == elfcpp::STB_WEAK)
  1513. return false;
  1514. }
  1515. return std::string(s1->name()) < std::string(s2->name());
  1516. }
  1517. // SYMBOLS is a list of object symbols from a dynamic object. Look
  1518. // for any weak aliases, and record them so that if we add the weak
  1519. // alias to the dynamic symbol table, we also add the corresponding
  1520. // strong symbol.
  1521. template<int size>
  1522. void
  1523. Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
  1524. {
  1525. // Sort the vector by section index, then by offset, then by weak
  1526. // ahead of strong.
  1527. std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
  1528. // Walk through the vector. For each weak definition, record
  1529. // aliases.
  1530. for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
  1531. symbols->begin();
  1532. p != symbols->end();
  1533. ++p)
  1534. {
  1535. if ((*p)->binding() != elfcpp::STB_WEAK)
  1536. continue;
  1537. // Build a circular list of weak aliases. Each symbol points to
  1538. // the next one in the circular list.
  1539. Sized_symbol<size>* from_sym = *p;
  1540. typename std::vector<Sized_symbol<size>*>::const_iterator q;
  1541. for (q = p + 1; q != symbols->end(); ++q)
  1542. {
  1543. bool dummy;
  1544. if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
  1545. || (*q)->value() != from_sym->value())
  1546. break;
  1547. this->weak_aliases_[from_sym] = *q;
  1548. from_sym->set_has_alias();
  1549. from_sym = *q;
  1550. }
  1551. if (from_sym != *p)
  1552. {
  1553. this->weak_aliases_[from_sym] = *p;
  1554. from_sym->set_has_alias();
  1555. }
  1556. p = q - 1;
  1557. }
  1558. }
  1559. // Create and return a specially defined symbol. If ONLY_IF_REF is
  1560. // true, then only create the symbol if there is a reference to it.
  1561. // If this does not return NULL, it sets *POLDSYM to the existing
  1562. // symbol if there is one. This sets *RESOLVE_OLDSYM if we should
  1563. // resolve the newly created symbol to the old one. This
  1564. // canonicalizes *PNAME and *PVERSION.
  1565. template<int size, bool big_endian>
  1566. Sized_symbol<size>*
  1567. Symbol_table::define_special_symbol(const char** pname, const char** pversion,
  1568. bool only_if_ref,
  1569. elfcpp::STV visibility,
  1570. Sized_symbol<size>** poldsym,
  1571. bool* resolve_oldsym, bool is_forced_local)
  1572. {
  1573. *resolve_oldsym = false;
  1574. *poldsym = NULL;
  1575. // If the caller didn't give us a version, see if we get one from
  1576. // the version script.
  1577. std::string v;
  1578. bool is_default_version = false;
  1579. if (!is_forced_local && *pversion == NULL)
  1580. {
  1581. bool is_global;
  1582. if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
  1583. {
  1584. if (is_global && !v.empty())
  1585. {
  1586. *pversion = v.c_str();
  1587. // If we get the version from a version script, then we
  1588. // are also the default version.
  1589. is_default_version = true;
  1590. }
  1591. }
  1592. }
  1593. Symbol* oldsym;
  1594. Sized_symbol<size>* sym;
  1595. bool add_to_table = false;
  1596. typename Symbol_table_type::iterator add_loc = this->table_.end();
  1597. bool add_def_to_table = false;
  1598. typename Symbol_table_type::iterator add_def_loc = this->table_.end();
  1599. if (only_if_ref)
  1600. {
  1601. oldsym = this->lookup(*pname, *pversion);
  1602. if (oldsym == NULL && is_default_version)
  1603. oldsym = this->lookup(*pname, NULL);
  1604. if (oldsym == NULL)
  1605. return NULL;
  1606. if (!oldsym->is_undefined())
  1607. {
  1608. // Skip if the old definition is from a regular object.
  1609. if (!oldsym->is_from_dynobj())
  1610. return NULL;
  1611. // If the symbol has hidden or internal visibility, ignore
  1612. // definition and reference from a dynamic object.
  1613. if ((visibility == elfcpp::STV_HIDDEN
  1614. || visibility == elfcpp::STV_INTERNAL)
  1615. && !oldsym->in_reg())
  1616. return NULL;
  1617. }
  1618. *pname = oldsym->name();
  1619. if (is_default_version)
  1620. *pversion = this->namepool_.add(*pversion, true, NULL);
  1621. else
  1622. *pversion = oldsym->version();
  1623. }
  1624. else
  1625. {
  1626. // Canonicalize NAME and VERSION.
  1627. Stringpool::Key name_key;
  1628. *pname = this->namepool_.add(*pname, true, &name_key);
  1629. Stringpool::Key version_key = 0;
  1630. if (*pversion != NULL)
  1631. *pversion = this->namepool_.add(*pversion, true, &version_key);
  1632. Symbol* const snull = NULL;
  1633. std::pair<typename Symbol_table_type::iterator, bool> ins =
  1634. this->table_.insert(std::make_pair(std::make_pair(name_key,
  1635. version_key),
  1636. snull));
  1637. std::pair<typename Symbol_table_type::iterator, bool> insdefault =
  1638. std::make_pair(this->table_.end(), false);
  1639. if (is_default_version)
  1640. {
  1641. const Stringpool::Key vnull = 0;
  1642. insdefault =
  1643. this->table_.insert(std::make_pair(std::make_pair(name_key,
  1644. vnull),
  1645. snull));
  1646. }
  1647. if (!ins.second)
  1648. {
  1649. // We already have a symbol table entry for NAME/VERSION.
  1650. oldsym = ins.first->second;
  1651. gold_assert(oldsym != NULL);
  1652. if (is_default_version)
  1653. {
  1654. Sized_symbol<size>* soldsym =
  1655. this->get_sized_symbol<size>(oldsym);
  1656. this->define_default_version<size, big_endian>(soldsym,
  1657. insdefault.second,
  1658. insdefault.first);
  1659. }
  1660. }
  1661. else
  1662. {
  1663. // We haven't seen this symbol before.
  1664. gold_assert(ins.first->second == NULL);
  1665. add_to_table = true;
  1666. add_loc = ins.first;
  1667. if (is_default_version
  1668. && !insdefault.second
  1669. && insdefault.first->second->version() == NULL)
  1670. {
  1671. // We are adding NAME/VERSION, and it is the default
  1672. // version. We already have an entry for NAME/NULL
  1673. // that does not already have a version.
  1674. oldsym = insdefault.first->second;
  1675. *resolve_oldsym = true;
  1676. }
  1677. else
  1678. {
  1679. oldsym = NULL;
  1680. if (is_default_version)
  1681. {
  1682. add_def_to_table = true;
  1683. add_def_loc = insdefault.first;
  1684. }
  1685. }
  1686. }
  1687. }
  1688. const Target& target = parameters->target();
  1689. if (!target.has_make_symbol())
  1690. sym = new Sized_symbol<size>();
  1691. else
  1692. {
  1693. Sized_target<size, big_endian>* sized_target =
  1694. parameters->sized_target<size, big_endian>();
  1695. sym = sized_target->make_symbol(*pname, elfcpp::STT_NOTYPE,
  1696. NULL, elfcpp::SHN_UNDEF, 0);
  1697. if (sym == NULL)
  1698. return NULL;
  1699. }
  1700. if (add_to_table)
  1701. add_loc->second = sym;
  1702. else
  1703. gold_assert(oldsym != NULL);
  1704. if (add_def_to_table)
  1705. add_def_loc->second = sym;
  1706. *poldsym = this->get_sized_symbol<size>(oldsym);
  1707. return sym;
  1708. }
  1709. // Define a symbol based on an Output_data.
  1710. Symbol*
  1711. Symbol_table::define_in_output_data(const char* name,
  1712. const char* version,
  1713. Defined defined,
  1714. Output_data* od,
  1715. uint64_t value,
  1716. uint64_t symsize,
  1717. elfcpp::STT type,
  1718. elfcpp::STB binding,
  1719. elfcpp::STV visibility,
  1720. unsigned char nonvis,
  1721. bool offset_is_from_end,
  1722. bool only_if_ref)
  1723. {
  1724. if (parameters->target().get_size() == 32)
  1725. {
  1726. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1727. return this->do_define_in_output_data<32>(name, version, defined, od,
  1728. value, symsize, type, binding,
  1729. visibility, nonvis,
  1730. offset_is_from_end,
  1731. only_if_ref);
  1732. #else
  1733. gold_unreachable();
  1734. #endif
  1735. }
  1736. else if (parameters->target().get_size() == 64)
  1737. {
  1738. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1739. return this->do_define_in_output_data<64>(name, version, defined, od,
  1740. value, symsize, type, binding,
  1741. visibility, nonvis,
  1742. offset_is_from_end,
  1743. only_if_ref);
  1744. #else
  1745. gold_unreachable();
  1746. #endif
  1747. }
  1748. else
  1749. gold_unreachable();
  1750. }
  1751. // Define a symbol in an Output_data, sized version.
  1752. template<int size>
  1753. Sized_symbol<size>*
  1754. Symbol_table::do_define_in_output_data(
  1755. const char* name,
  1756. const char* version,
  1757. Defined defined,
  1758. Output_data* od,
  1759. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1760. typename elfcpp::Elf_types<size>::Elf_WXword symsize,
  1761. elfcpp::STT type,
  1762. elfcpp::STB binding,
  1763. elfcpp::STV visibility,
  1764. unsigned char nonvis,
  1765. bool offset_is_from_end,
  1766. bool only_if_ref)
  1767. {
  1768. Sized_symbol<size>* sym;
  1769. Sized_symbol<size>* oldsym;
  1770. bool resolve_oldsym;
  1771. const bool is_forced_local = binding == elfcpp::STB_LOCAL;
  1772. if (parameters->target().is_big_endian())
  1773. {
  1774. #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
  1775. sym = this->define_special_symbol<size, true>(&name, &version,
  1776. only_if_ref,
  1777. visibility,
  1778. &oldsym,
  1779. &resolve_oldsym,
  1780. is_forced_local);
  1781. #else
  1782. gold_unreachable();
  1783. #endif
  1784. }
  1785. else
  1786. {
  1787. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
  1788. sym = this->define_special_symbol<size, false>(&name, &version,
  1789. only_if_ref,
  1790. visibility,
  1791. &oldsym,
  1792. &resolve_oldsym,
  1793. is_forced_local);
  1794. #else
  1795. gold_unreachable();
  1796. #endif
  1797. }
  1798. if (sym == NULL)
  1799. return NULL;
  1800. sym->init_output_data(name, version, od, value, symsize, type, binding,
  1801. visibility, nonvis, offset_is_from_end,
  1802. defined == PREDEFINED);
  1803. if (oldsym == NULL)
  1804. {
  1805. if (is_forced_local || this->version_script_.symbol_is_local(name))
  1806. this->force_local(sym);
  1807. else if (version != NULL)
  1808. sym->set_is_default();
  1809. return sym;
  1810. }
  1811. if (Symbol_table::should_override_with_special(oldsym, type, defined))
  1812. this->override_with_special(oldsym, sym);
  1813. if (resolve_oldsym)
  1814. return sym;
  1815. else
  1816. {
  1817. if (defined == PREDEFINED
  1818. && (is_forced_local || this->version_script_.symbol_is_local(name)))
  1819. this->force_local(oldsym);
  1820. delete sym;
  1821. return oldsym;
  1822. }
  1823. }
  1824. // Define a symbol based on an Output_segment.
  1825. Symbol*
  1826. Symbol_table::define_in_output_segment(const char* name,
  1827. const char* version,
  1828. Defined defined,
  1829. Output_segment* os,
  1830. uint64_t value,
  1831. uint64_t symsize,
  1832. elfcpp::STT type,
  1833. elfcpp::STB binding,
  1834. elfcpp::STV visibility,
  1835. unsigned char nonvis,
  1836. Symbol::Segment_offset_base offset_base,
  1837. bool only_if_ref)
  1838. {
  1839. if (parameters->target().get_size() == 32)
  1840. {
  1841. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1842. return this->do_define_in_output_segment<32>(name, version, defined, os,
  1843. value, symsize, type,
  1844. binding, visibility, nonvis,
  1845. offset_base, only_if_ref);
  1846. #else
  1847. gold_unreachable();
  1848. #endif
  1849. }
  1850. else if (parameters->target().get_size() == 64)
  1851. {
  1852. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1853. return this->do_define_in_output_segment<64>(name, version, defined, os,
  1854. value, symsize, type,
  1855. binding, visibility, nonvis,
  1856. offset_base, only_if_ref);
  1857. #else
  1858. gold_unreachable();
  1859. #endif
  1860. }
  1861. else
  1862. gold_unreachable();
  1863. }
  1864. // Define a symbol in an Output_segment, sized version.
  1865. template<int size>
  1866. Sized_symbol<size>*
  1867. Symbol_table::do_define_in_output_segment(
  1868. const char* name,
  1869. const char* version,
  1870. Defined defined,
  1871. Output_segment* os,
  1872. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1873. typename elfcpp::Elf_types<size>::Elf_WXword symsize,
  1874. elfcpp::STT type,
  1875. elfcpp::STB binding,
  1876. elfcpp::STV visibility,
  1877. unsigned char nonvis,
  1878. Symbol::Segment_offset_base offset_base,
  1879. bool only_if_ref)
  1880. {
  1881. Sized_symbol<size>* sym;
  1882. Sized_symbol<size>* oldsym;
  1883. bool resolve_oldsym;
  1884. const bool is_forced_local = binding == elfcpp::STB_LOCAL;
  1885. if (parameters->target().is_big_endian())
  1886. {
  1887. #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
  1888. sym = this->define_special_symbol<size, true>(&name, &version,
  1889. only_if_ref,
  1890. visibility,
  1891. &oldsym,
  1892. &resolve_oldsym,
  1893. is_forced_local);
  1894. #else
  1895. gold_unreachable();
  1896. #endif
  1897. }
  1898. else
  1899. {
  1900. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
  1901. sym = this->define_special_symbol<size, false>(&name, &version,
  1902. only_if_ref,
  1903. visibility,
  1904. &oldsym,
  1905. &resolve_oldsym,
  1906. is_forced_local);
  1907. #else
  1908. gold_unreachable();
  1909. #endif
  1910. }
  1911. if (sym == NULL)
  1912. return NULL;
  1913. sym->init_output_segment(name, version, os, value, symsize, type, binding,
  1914. visibility, nonvis, offset_base,
  1915. defined == PREDEFINED);
  1916. if (oldsym == NULL)
  1917. {
  1918. if (is_forced_local || this->version_script_.symbol_is_local(name))
  1919. this->force_local(sym);
  1920. else if (version != NULL)
  1921. sym->set_is_default();
  1922. return sym;
  1923. }
  1924. if (Symbol_table::should_override_with_special(oldsym, type, defined))
  1925. this->override_with_special(oldsym, sym);
  1926. if (resolve_oldsym)
  1927. return sym;
  1928. else
  1929. {
  1930. if (is_forced_local || this->version_script_.symbol_is_local(name))
  1931. this->force_local(oldsym);
  1932. delete sym;
  1933. return oldsym;
  1934. }
  1935. }
  1936. // Define a special symbol with a constant value. It is a multiple
  1937. // definition error if this symbol is already defined.
  1938. Symbol*
  1939. Symbol_table::define_as_constant(const char* name,
  1940. const char* version,
  1941. Defined defined,
  1942. uint64_t value,
  1943. uint64_t symsize,
  1944. elfcpp::STT type,
  1945. elfcpp::STB binding,
  1946. elfcpp::STV visibility,
  1947. unsigned char nonvis,
  1948. bool only_if_ref,
  1949. bool force_override)
  1950. {
  1951. if (parameters->target().get_size() == 32)
  1952. {
  1953. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  1954. return this->do_define_as_constant<32>(name, version, defined, value,
  1955. symsize, type, binding,
  1956. visibility, nonvis, only_if_ref,
  1957. force_override);
  1958. #else
  1959. gold_unreachable();
  1960. #endif
  1961. }
  1962. else if (parameters->target().get_size() == 64)
  1963. {
  1964. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  1965. return this->do_define_as_constant<64>(name, version, defined, value,
  1966. symsize, type, binding,
  1967. visibility, nonvis, only_if_ref,
  1968. force_override);
  1969. #else
  1970. gold_unreachable();
  1971. #endif
  1972. }
  1973. else
  1974. gold_unreachable();
  1975. }
  1976. // Define a symbol as a constant, sized version.
  1977. template<int size>
  1978. Sized_symbol<size>*
  1979. Symbol_table::do_define_as_constant(
  1980. const char* name,
  1981. const char* version,
  1982. Defined defined,
  1983. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1984. typename elfcpp::Elf_types<size>::Elf_WXword symsize,
  1985. elfcpp::STT type,
  1986. elfcpp::STB binding,
  1987. elfcpp::STV visibility,
  1988. unsigned char nonvis,
  1989. bool only_if_ref,
  1990. bool force_override)
  1991. {
  1992. Sized_symbol<size>* sym;
  1993. Sized_symbol<size>* oldsym;
  1994. bool resolve_oldsym;
  1995. const bool is_forced_local = binding == elfcpp::STB_LOCAL;
  1996. if (parameters->target().is_big_endian())
  1997. {
  1998. #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
  1999. sym = this->define_special_symbol<size, true>(&name, &version,
  2000. only_if_ref,
  2001. visibility,
  2002. &oldsym,
  2003. &resolve_oldsym,
  2004. is_forced_local);
  2005. #else
  2006. gold_unreachable();
  2007. #endif
  2008. }
  2009. else
  2010. {
  2011. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
  2012. sym = this->define_special_symbol<size, false>(&name, &version,
  2013. only_if_ref,
  2014. visibility,
  2015. &oldsym,
  2016. &resolve_oldsym,
  2017. is_forced_local);
  2018. #else
  2019. gold_unreachable();
  2020. #endif
  2021. }
  2022. if (sym == NULL)
  2023. return NULL;
  2024. sym->init_constant(name, version, value, symsize, type, binding, visibility,
  2025. nonvis, defined == PREDEFINED);
  2026. if (oldsym == NULL)
  2027. {
  2028. // Version symbols are absolute symbols with name == version.
  2029. // We don't want to force them to be local.
  2030. if ((version == NULL
  2031. || name != version
  2032. || value != 0)
  2033. && (is_forced_local || this->version_script_.symbol_is_local(name)))
  2034. this->force_local(sym);
  2035. else if (version != NULL
  2036. && (name != version || value != 0))
  2037. sym->set_is_default();
  2038. return sym;
  2039. }
  2040. if (force_override
  2041. || Symbol_table::should_override_with_special(oldsym, type, defined))
  2042. this->override_with_special(oldsym, sym);
  2043. if (resolve_oldsym)
  2044. return sym;
  2045. else
  2046. {
  2047. if (is_forced_local || this->version_script_.symbol_is_local(name))
  2048. this->force_local(oldsym);
  2049. delete sym;
  2050. return oldsym;
  2051. }
  2052. }
  2053. // Define a set of symbols in output sections.
  2054. void
  2055. Symbol_table::define_symbols(const Layout* layout, int count,
  2056. const Define_symbol_in_section* p,
  2057. bool only_if_ref)
  2058. {
  2059. for (int i = 0; i < count; ++i, ++p)
  2060. {
  2061. Output_section* os = layout->find_output_section(p->output_section);
  2062. if (os != NULL)
  2063. this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
  2064. p->size, p->type, p->binding,
  2065. p->visibility, p->nonvis,
  2066. p->offset_is_from_end,
  2067. only_if_ref || p->only_if_ref);
  2068. else
  2069. this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
  2070. p->type, p->binding, p->visibility, p->nonvis,
  2071. only_if_ref || p->only_if_ref,
  2072. false);
  2073. }
  2074. }
  2075. // Define a set of symbols in output segments.
  2076. void
  2077. Symbol_table::define_symbols(const Layout* layout, int count,
  2078. const Define_symbol_in_segment* p,
  2079. bool only_if_ref)
  2080. {
  2081. for (int i = 0; i < count; ++i, ++p)
  2082. {
  2083. Output_segment* os = layout->find_output_segment(p->segment_type,
  2084. p->segment_flags_set,
  2085. p->segment_flags_clear);
  2086. if (os != NULL)
  2087. this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
  2088. p->size, p->type, p->binding,
  2089. p->visibility, p->nonvis,
  2090. p->offset_base,
  2091. only_if_ref || p->only_if_ref);
  2092. else
  2093. this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
  2094. p->type, p->binding, p->visibility, p->nonvis,
  2095. only_if_ref || p->only_if_ref,
  2096. false);
  2097. }
  2098. }
  2099. // Define CSYM using a COPY reloc. POSD is the Output_data where the
  2100. // symbol should be defined--typically a .dyn.bss section. VALUE is
  2101. // the offset within POSD.
  2102. template<int size>
  2103. void
  2104. Symbol_table::define_with_copy_reloc(
  2105. Sized_symbol<size>* csym,
  2106. Output_data* posd,
  2107. typename elfcpp::Elf_types<size>::Elf_Addr value)
  2108. {
  2109. gold_assert(csym->is_from_dynobj());
  2110. gold_assert(!csym->is_copied_from_dynobj());
  2111. Object* object = csym->object();
  2112. gold_assert(object->is_dynamic());
  2113. Dynobj* dynobj = static_cast<Dynobj*>(object);
  2114. // Our copied variable has to override any variable in a shared
  2115. // library.
  2116. elfcpp::STB binding = csym->binding();
  2117. if (binding == elfcpp::STB_WEAK)
  2118. binding = elfcpp::STB_GLOBAL;
  2119. this->define_in_output_data(csym->name(), csym->version(), COPY,
  2120. posd, value, csym->symsize(),
  2121. csym->type(), binding,
  2122. csym->visibility(), csym->nonvis(),
  2123. false, false);
  2124. csym->set_is_copied_from_dynobj();
  2125. csym->set_needs_dynsym_entry();
  2126. this->copied_symbol_dynobjs_[csym] = dynobj;
  2127. // We have now defined all aliases, but we have not entered them all
  2128. // in the copied_symbol_dynobjs_ map.
  2129. if (csym->has_alias())
  2130. {
  2131. Symbol* sym = csym;
  2132. while (true)
  2133. {
  2134. sym = this->weak_aliases_[sym];
  2135. if (sym == csym)
  2136. break;
  2137. gold_assert(sym->output_data() == posd);
  2138. sym->set_is_copied_from_dynobj();
  2139. this->copied_symbol_dynobjs_[sym] = dynobj;
  2140. }
  2141. }
  2142. }
  2143. // SYM is defined using a COPY reloc. Return the dynamic object where
  2144. // the original definition was found.
  2145. Dynobj*
  2146. Symbol_table::get_copy_source(const Symbol* sym) const
  2147. {
  2148. gold_assert(sym->is_copied_from_dynobj());
  2149. Copied_symbol_dynobjs::const_iterator p =
  2150. this->copied_symbol_dynobjs_.find(sym);
  2151. gold_assert(p != this->copied_symbol_dynobjs_.end());
  2152. return p->second;
  2153. }
  2154. // Add any undefined symbols named on the command line.
  2155. void
  2156. Symbol_table::add_undefined_symbols_from_command_line(Layout* layout)
  2157. {
  2158. if (parameters->options().any_undefined()
  2159. || layout->script_options()->any_unreferenced())
  2160. {
  2161. if (parameters->target().get_size() == 32)
  2162. {
  2163. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  2164. this->do_add_undefined_symbols_from_command_line<32>(layout);
  2165. #else
  2166. gold_unreachable();
  2167. #endif
  2168. }
  2169. else if (parameters->target().get_size() == 64)
  2170. {
  2171. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  2172. this->do_add_undefined_symbols_from_command_line<64>(layout);
  2173. #else
  2174. gold_unreachable();
  2175. #endif
  2176. }
  2177. else
  2178. gold_unreachable();
  2179. }
  2180. }
  2181. template<int size>
  2182. void
  2183. Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout)
  2184. {
  2185. for (options::String_set::const_iterator p =
  2186. parameters->options().undefined_begin();
  2187. p != parameters->options().undefined_end();
  2188. ++p)
  2189. this->add_undefined_symbol_from_command_line<size>(p->c_str());
  2190. for (Script_options::referenced_const_iterator p =
  2191. layout->script_options()->referenced_begin();
  2192. p != layout->script_options()->referenced_end();
  2193. ++p)
  2194. this->add_undefined_symbol_from_command_line<size>(p->c_str());
  2195. }
  2196. template<int size>
  2197. void
  2198. Symbol_table::add_undefined_symbol_from_command_line(const char* name)
  2199. {
  2200. if (this->lookup(name) != NULL)
  2201. return;
  2202. const char* version = NULL;
  2203. Sized_symbol<size>* sym;
  2204. Sized_symbol<size>* oldsym;
  2205. bool resolve_oldsym;
  2206. if (parameters->target().is_big_endian())
  2207. {
  2208. #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
  2209. sym = this->define_special_symbol<size, true>(&name, &version,
  2210. false,
  2211. elfcpp::STV_DEFAULT,
  2212. &oldsym,
  2213. &resolve_oldsym,
  2214. false);
  2215. #else
  2216. gold_unreachable();
  2217. #endif
  2218. }
  2219. else
  2220. {
  2221. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
  2222. sym = this->define_special_symbol<size, false>(&name, &version,
  2223. false,
  2224. elfcpp::STV_DEFAULT,
  2225. &oldsym,
  2226. &resolve_oldsym,
  2227. false);
  2228. #else
  2229. gold_unreachable();
  2230. #endif
  2231. }
  2232. gold_assert(oldsym == NULL);
  2233. sym->init_undefined(name, version, 0, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
  2234. elfcpp::STV_DEFAULT, 0);
  2235. ++this->saw_undefined_;
  2236. }
  2237. // Set the dynamic symbol indexes. INDEX is the index of the first
  2238. // global dynamic symbol. Pointers to the global symbols are stored
  2239. // into the vector SYMS. The names are added to DYNPOOL.
  2240. // This returns an updated dynamic symbol index.
  2241. unsigned int
  2242. Symbol_table::set_dynsym_indexes(unsigned int index,
  2243. unsigned int* pforced_local_count,
  2244. std::vector<Symbol*>* syms,
  2245. Stringpool* dynpool,
  2246. Versions* versions)
  2247. {
  2248. // First process all the symbols which have been forced to be local,
  2249. // as they must appear before all global symbols.
  2250. unsigned int forced_local_count = 0;
  2251. for (Forced_locals::iterator p = this->forced_locals_.begin();
  2252. p != this->forced_locals_.end();
  2253. ++p)
  2254. {
  2255. Symbol* sym = *p;
  2256. gold_assert(sym->is_forced_local());
  2257. if (sym->has_dynsym_index())
  2258. continue;
  2259. if (!sym->should_add_dynsym_entry(this))
  2260. sym->set_dynsym_index(-1U);
  2261. else
  2262. {
  2263. sym->set_dynsym_index(index);
  2264. ++index;
  2265. ++forced_local_count;
  2266. dynpool->add(sym->name(), false, NULL);
  2267. if (sym->type() == elfcpp::STT_GNU_IFUNC)
  2268. this->set_has_gnu_output();
  2269. }
  2270. }
  2271. *pforced_local_count = forced_local_count;
  2272. // Allow a target to set dynsym indexes.
  2273. if (parameters->target().has_custom_set_dynsym_indexes())
  2274. {
  2275. std::vector<Symbol*> dyn_symbols;
  2276. for (Symbol_table_type::iterator p = this->table_.begin();
  2277. p != this->table_.end();
  2278. ++p)
  2279. {
  2280. Symbol* sym = p->second;
  2281. if (sym->is_forced_local())
  2282. continue;
  2283. if (!sym->should_add_dynsym_entry(this))
  2284. sym->set_dynsym_index(-1U);
  2285. else
  2286. {
  2287. dyn_symbols.push_back(sym);
  2288. if (sym->type() == elfcpp::STT_GNU_IFUNC
  2289. || (sym->binding() == elfcpp::STB_GNU_UNIQUE
  2290. && parameters->options().gnu_unique()))
  2291. this->set_has_gnu_output();
  2292. }
  2293. }
  2294. return parameters->target().set_dynsym_indexes(&dyn_symbols, index, syms,
  2295. dynpool, versions, this);
  2296. }
  2297. for (Symbol_table_type::iterator p = this->table_.begin();
  2298. p != this->table_.end();
  2299. ++p)
  2300. {
  2301. Symbol* sym = p->second;
  2302. if (sym->is_forced_local())
  2303. continue;
  2304. // Note that SYM may already have a dynamic symbol index, since
  2305. // some symbols appear more than once in the symbol table, with
  2306. // and without a version.
  2307. if (!sym->should_add_dynsym_entry(this))
  2308. sym->set_dynsym_index(-1U);
  2309. else if (!sym->has_dynsym_index())
  2310. {
  2311. sym->set_dynsym_index(index);
  2312. ++index;
  2313. syms->push_back(sym);
  2314. dynpool->add(sym->name(), false, NULL);
  2315. if (sym->type() == elfcpp::STT_GNU_IFUNC
  2316. || (sym->binding() == elfcpp::STB_GNU_UNIQUE
  2317. && parameters->options().gnu_unique()))
  2318. this->set_has_gnu_output();
  2319. // Record any version information, except those from
  2320. // as-needed libraries not seen to be needed. Note that the
  2321. // is_needed state for such libraries can change in this loop.
  2322. if (sym->version() != NULL)
  2323. {
  2324. if (!sym->is_from_dynobj()
  2325. || !sym->object()->as_needed()
  2326. || sym->object()->is_needed())
  2327. versions->record_version(this, dynpool, sym);
  2328. else
  2329. {
  2330. if (parameters->options().warn_drop_version())
  2331. gold_warning(_("discarding version information for "
  2332. "%s@%s, defined in unused shared library %s "
  2333. "(linked with --as-needed)"),
  2334. sym->name(), sym->version(),
  2335. sym->object()->name().c_str());
  2336. sym->clear_version();
  2337. }
  2338. }
  2339. }
  2340. }
  2341. // Finish up the versions. In some cases this may add new dynamic
  2342. // symbols.
  2343. index = versions->finalize(this, index, syms);
  2344. // Process target-specific symbols.
  2345. for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
  2346. p != this->target_symbols_.end();
  2347. ++p)
  2348. {
  2349. (*p)->set_dynsym_index(index);
  2350. ++index;
  2351. syms->push_back(*p);
  2352. dynpool->add((*p)->name(), false, NULL);
  2353. }
  2354. return index;
  2355. }
  2356. // Set the final values for all the symbols. The index of the first
  2357. // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
  2358. // file offset OFF. Add their names to POOL. Return the new file
  2359. // offset. Update *PLOCAL_SYMCOUNT if necessary. DYNOFF and
  2360. // DYN_GLOBAL_INDEX refer to the start of the symbols that will be
  2361. // written from the global symbol table in Symtab::write_globals(),
  2362. // which will include forced-local symbols. DYN_GLOBAL_INDEX is
  2363. // not necessarily the same as the sh_info field for the .dynsym
  2364. // section, which will point to the first real global symbol.
  2365. off_t
  2366. Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
  2367. size_t dyncount, Stringpool* pool,
  2368. unsigned int* plocal_symcount)
  2369. {
  2370. off_t ret;
  2371. gold_assert(*plocal_symcount != 0);
  2372. this->first_global_index_ = *plocal_symcount;
  2373. this->dynamic_offset_ = dynoff;
  2374. this->first_dynamic_global_index_ = dyn_global_index;
  2375. this->dynamic_count_ = dyncount;
  2376. if (parameters->target().get_size() == 32)
  2377. {
  2378. #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
  2379. ret = this->sized_finalize<32>(off, pool, plocal_symcount);
  2380. #else
  2381. gold_unreachable();
  2382. #endif
  2383. }
  2384. else if (parameters->target().get_size() == 64)
  2385. {
  2386. #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
  2387. ret = this->sized_finalize<64>(off, pool, plocal_symcount);
  2388. #else
  2389. gold_unreachable();
  2390. #endif
  2391. }
  2392. else
  2393. gold_unreachable();
  2394. if (this->has_gnu_output_)
  2395. {
  2396. Target* target = const_cast<Target*>(&parameters->target());
  2397. if (target->osabi() == elfcpp::ELFOSABI_NONE)
  2398. target->set_osabi(elfcpp::ELFOSABI_GNU);
  2399. }
  2400. // Now that we have the final symbol table, we can reliably note
  2401. // which symbols should get warnings.
  2402. this->warnings_.note_warnings(this);
  2403. return ret;
  2404. }
  2405. // SYM is going into the symbol table at *PINDEX. Add the name to
  2406. // POOL, update *PINDEX and *POFF.
  2407. template<int size>
  2408. void
  2409. Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
  2410. unsigned int* pindex, off_t* poff)
  2411. {
  2412. sym->set_symtab_index(*pindex);
  2413. if (sym->version() == NULL || !parameters->options().relocatable())
  2414. pool->add(sym->name(), false, NULL);
  2415. else
  2416. pool->add(sym->versioned_name(), true, NULL);
  2417. ++*pindex;
  2418. *poff += elfcpp::Elf_sizes<size>::sym_size;
  2419. }
  2420. // Set the final value for all the symbols. This is called after
  2421. // Layout::finalize, so all the output sections have their final
  2422. // address.
  2423. template<int size>
  2424. off_t
  2425. Symbol_table::sized_finalize(off_t off, Stringpool* pool,
  2426. unsigned int* plocal_symcount)
  2427. {
  2428. off = align_address(off, size >> 3);
  2429. this->offset_ = off;
  2430. unsigned int index = *plocal_symcount;
  2431. const unsigned int orig_index = index;
  2432. // First do all the symbols which have been forced to be local, as
  2433. // they must appear before all global symbols.
  2434. for (Forced_locals::iterator p = this->forced_locals_.begin();
  2435. p != this->forced_locals_.end();
  2436. ++p)
  2437. {
  2438. Symbol* sym = *p;
  2439. gold_assert(sym->is_forced_local());
  2440. if (this->sized_finalize_symbol<size>(sym))
  2441. {
  2442. this->add_to_final_symtab<size>(sym, pool, &index, &off);
  2443. ++*plocal_symcount;
  2444. if (sym->type() == elfcpp::STT_GNU_IFUNC)
  2445. this->set_has_gnu_output();
  2446. }
  2447. }
  2448. // Now do all the remaining symbols.
  2449. for (Symbol_table_type::iterator p = this->table_.begin();
  2450. p != this->table_.end();
  2451. ++p)
  2452. {
  2453. Symbol* sym = p->second;
  2454. if (this->sized_finalize_symbol<size>(sym))
  2455. {
  2456. this->add_to_final_symtab<size>(sym, pool, &index, &off);
  2457. if (sym->type() == elfcpp::STT_GNU_IFUNC
  2458. || (sym->binding() == elfcpp::STB_GNU_UNIQUE
  2459. && parameters->options().gnu_unique()))
  2460. this->set_has_gnu_output();
  2461. }
  2462. }
  2463. // Now do target-specific symbols.
  2464. for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
  2465. p != this->target_symbols_.end();
  2466. ++p)
  2467. {
  2468. this->add_to_final_symtab<size>(*p, pool, &index, &off);
  2469. }
  2470. this->output_count_ = index - orig_index;
  2471. return off;
  2472. }
  2473. // Compute the final value of SYM and store status in location PSTATUS.
  2474. // During relaxation, this may be called multiple times for a symbol to
  2475. // compute its would-be final value in each relaxation pass.
  2476. template<int size>
  2477. typename Sized_symbol<size>::Value_type
  2478. Symbol_table::compute_final_value(
  2479. const Sized_symbol<size>* sym,
  2480. Compute_final_value_status* pstatus) const
  2481. {
  2482. typedef typename Sized_symbol<size>::Value_type Value_type;
  2483. Value_type value;
  2484. switch (sym->source())
  2485. {
  2486. case Symbol::FROM_OBJECT:
  2487. {
  2488. bool is_ordinary;
  2489. unsigned int shndx = sym->shndx(&is_ordinary);
  2490. if (!is_ordinary
  2491. && shndx != elfcpp::SHN_ABS
  2492. && !Symbol::is_common_shndx(shndx))
  2493. {
  2494. *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
  2495. return 0;
  2496. }
  2497. Object* symobj = sym->object();
  2498. if (symobj->is_dynamic())
  2499. {
  2500. value = 0;
  2501. shndx = elfcpp::SHN_UNDEF;
  2502. }
  2503. else if (symobj->pluginobj() != NULL)
  2504. {
  2505. value = 0;
  2506. shndx = elfcpp::SHN_UNDEF;
  2507. }
  2508. else if (shndx == elfcpp::SHN_UNDEF)
  2509. value = 0;
  2510. else if (!is_ordinary
  2511. && (shndx == elfcpp::SHN_ABS
  2512. || Symbol::is_common_shndx(shndx)))
  2513. value = sym->value();
  2514. else
  2515. {
  2516. Relobj* relobj = static_cast<Relobj*>(symobj);
  2517. Output_section* os = relobj->output_section(shndx);
  2518. if (this->is_section_folded(relobj, shndx))
  2519. {
  2520. gold_assert(os == NULL);
  2521. // Get the os of the section it is folded onto.
  2522. Section_id folded = this->icf_->get_folded_section(relobj,
  2523. shndx);
  2524. gold_assert(folded.first != NULL);
  2525. Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
  2526. unsigned folded_shndx = folded.second;
  2527. os = folded_obj->output_section(folded_shndx);
  2528. gold_assert(os != NULL);
  2529. // Replace (relobj, shndx) with canonical ICF input section.
  2530. shndx = folded_shndx;
  2531. relobj = folded_obj;
  2532. }
  2533. uint64_t secoff64 = relobj->output_section_offset(shndx);
  2534. if (os == NULL)
  2535. {
  2536. bool static_or_reloc = (parameters->doing_static_link() ||
  2537. parameters->options().relocatable());
  2538. gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
  2539. *pstatus = CFVS_NO_OUTPUT_SECTION;
  2540. return 0;
  2541. }
  2542. if (secoff64 == -1ULL)
  2543. {
  2544. // The section needs special handling (e.g., a merge section).
  2545. value = os->output_address(relobj, shndx, sym->value());
  2546. }
  2547. else
  2548. {
  2549. Value_type secoff =
  2550. convert_types<Value_type, uint64_t>(secoff64);
  2551. if (sym->type() == elfcpp::STT_TLS)
  2552. value = sym->value() + os->tls_offset() + secoff;
  2553. else
  2554. value = sym->value() + os->address() + secoff;
  2555. }
  2556. }
  2557. }
  2558. break;
  2559. case Symbol::IN_OUTPUT_DATA:
  2560. {
  2561. Output_data* od = sym->output_data();
  2562. value = sym->value();
  2563. if (sym->type() != elfcpp::STT_TLS)
  2564. value += od->address();
  2565. else
  2566. {
  2567. Output_section* os = od->output_section();
  2568. gold_assert(os != NULL);
  2569. value += os->tls_offset() + (od->address() - os->address());
  2570. }
  2571. if (sym->offset_is_from_end())
  2572. value += od->data_size();
  2573. }
  2574. break;
  2575. case Symbol::IN_OUTPUT_SEGMENT:
  2576. {
  2577. Output_segment* os = sym->output_segment();
  2578. value = sym->value();
  2579. if (sym->type() != elfcpp::STT_TLS)
  2580. value += os->vaddr();
  2581. switch (sym->offset_base())
  2582. {
  2583. case Symbol::SEGMENT_START:
  2584. break;
  2585. case Symbol::SEGMENT_END:
  2586. value += os->memsz();
  2587. break;
  2588. case Symbol::SEGMENT_BSS:
  2589. value += os->filesz();
  2590. break;
  2591. default:
  2592. gold_unreachable();
  2593. }
  2594. }
  2595. break;
  2596. case Symbol::IS_CONSTANT:
  2597. value = sym->value();
  2598. break;
  2599. case Symbol::IS_UNDEFINED:
  2600. value = 0;
  2601. break;
  2602. default:
  2603. gold_unreachable();
  2604. }
  2605. *pstatus = CFVS_OK;
  2606. return value;
  2607. }
  2608. // Finalize the symbol SYM. This returns true if the symbol should be
  2609. // added to the symbol table, false otherwise.
  2610. template<int size>
  2611. bool
  2612. Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
  2613. {
  2614. typedef typename Sized_symbol<size>::Value_type Value_type;
  2615. Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
  2616. // The default version of a symbol may appear twice in the symbol
  2617. // table. We only need to finalize it once.
  2618. if (sym->has_symtab_index())
  2619. return false;
  2620. if (!sym->in_reg())
  2621. {
  2622. gold_assert(!sym->has_symtab_index());
  2623. sym->set_symtab_index(-1U);
  2624. gold_assert(sym->dynsym_index() == -1U);
  2625. return false;
  2626. }
  2627. // If the symbol is only present on plugin files, the plugin decided we
  2628. // don't need it.
  2629. if (!sym->in_real_elf())
  2630. {
  2631. gold_assert(!sym->has_symtab_index());
  2632. sym->set_symtab_index(-1U);
  2633. return false;
  2634. }
  2635. // Compute final symbol value.
  2636. Compute_final_value_status status;
  2637. Value_type value = this->compute_final_value(sym, &status);
  2638. switch (status)
  2639. {
  2640. case CFVS_OK:
  2641. break;
  2642. case CFVS_UNSUPPORTED_SYMBOL_SECTION:
  2643. {
  2644. bool is_ordinary;
  2645. unsigned int shndx = sym->shndx(&is_ordinary);
  2646. gold_error(_("%s: unsupported symbol section 0x%x"),
  2647. sym->demangled_name().c_str(), shndx);
  2648. }
  2649. break;
  2650. case CFVS_NO_OUTPUT_SECTION:
  2651. sym->set_symtab_index(-1U);
  2652. return false;
  2653. default:
  2654. gold_unreachable();
  2655. }
  2656. sym->set_value(value);
  2657. if (parameters->options().strip_all()
  2658. || !parameters->options().should_retain_symbol(sym->name()))
  2659. {
  2660. sym->set_symtab_index(-1U);
  2661. return false;
  2662. }
  2663. return true;
  2664. }
  2665. // Write out the global symbols.
  2666. void
  2667. Symbol_table::write_globals(const Stringpool* sympool,
  2668. const Stringpool* dynpool,
  2669. Output_symtab_xindex* symtab_xindex,
  2670. Output_symtab_xindex* dynsym_xindex,
  2671. Output_file* of) const
  2672. {
  2673. switch (parameters->size_and_endianness())
  2674. {
  2675. #ifdef HAVE_TARGET_32_LITTLE
  2676. case Parameters::TARGET_32_LITTLE:
  2677. this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
  2678. dynsym_xindex, of);
  2679. break;
  2680. #endif
  2681. #ifdef HAVE_TARGET_32_BIG
  2682. case Parameters::TARGET_32_BIG:
  2683. this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
  2684. dynsym_xindex, of);
  2685. break;
  2686. #endif
  2687. #ifdef HAVE_TARGET_64_LITTLE
  2688. case Parameters::TARGET_64_LITTLE:
  2689. this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
  2690. dynsym_xindex, of);
  2691. break;
  2692. #endif
  2693. #ifdef HAVE_TARGET_64_BIG
  2694. case Parameters::TARGET_64_BIG:
  2695. this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
  2696. dynsym_xindex, of);
  2697. break;
  2698. #endif
  2699. default:
  2700. gold_unreachable();
  2701. }
  2702. }
  2703. // Write out the global symbols.
  2704. template<int size, bool big_endian>
  2705. void
  2706. Symbol_table::sized_write_globals(const Stringpool* sympool,
  2707. const Stringpool* dynpool,
  2708. Output_symtab_xindex* symtab_xindex,
  2709. Output_symtab_xindex* dynsym_xindex,
  2710. Output_file* of) const
  2711. {
  2712. const Target& target = parameters->target();
  2713. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2714. const unsigned int output_count = this->output_count_;
  2715. const section_size_type oview_size = output_count * sym_size;
  2716. const unsigned int first_global_index = this->first_global_index_;
  2717. unsigned char* psyms;
  2718. if (this->offset_ == 0 || output_count == 0)
  2719. psyms = NULL;
  2720. else
  2721. psyms = of->get_output_view(this->offset_, oview_size);
  2722. const unsigned int dynamic_count = this->dynamic_count_;
  2723. const section_size_type dynamic_size = dynamic_count * sym_size;
  2724. const unsigned int first_dynamic_global_index =
  2725. this->first_dynamic_global_index_;
  2726. unsigned char* dynamic_view;
  2727. if (this->dynamic_offset_ == 0 || dynamic_count == 0)
  2728. dynamic_view = NULL;
  2729. else
  2730. dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
  2731. for (Symbol_table_type::const_iterator p = this->table_.begin();
  2732. p != this->table_.end();
  2733. ++p)
  2734. {
  2735. Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
  2736. // Possibly warn about unresolved symbols in shared libraries.
  2737. this->warn_about_undefined_dynobj_symbol(sym);
  2738. unsigned int sym_index = sym->symtab_index();
  2739. unsigned int dynsym_index;
  2740. if (dynamic_view == NULL)
  2741. dynsym_index = -1U;
  2742. else
  2743. dynsym_index = sym->dynsym_index();
  2744. if (sym_index == -1U && dynsym_index == -1U)
  2745. {
  2746. // This symbol is not included in the output file.
  2747. continue;
  2748. }
  2749. unsigned int shndx;
  2750. typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
  2751. typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
  2752. elfcpp::STB binding = sym->binding();
  2753. // If --weak-unresolved-symbols is set, change binding of unresolved
  2754. // global symbols to STB_WEAK.
  2755. if (parameters->options().weak_unresolved_symbols()
  2756. && binding == elfcpp::STB_GLOBAL
  2757. && sym->is_undefined())
  2758. binding = elfcpp::STB_WEAK;
  2759. // If --no-gnu-unique is set, change STB_GNU_UNIQUE to STB_GLOBAL.
  2760. if (binding == elfcpp::STB_GNU_UNIQUE
  2761. && !parameters->options().gnu_unique())
  2762. binding = elfcpp::STB_GLOBAL;
  2763. switch (sym->source())
  2764. {
  2765. case Symbol::FROM_OBJECT:
  2766. {
  2767. bool is_ordinary;
  2768. unsigned int in_shndx = sym->shndx(&is_ordinary);
  2769. if (!is_ordinary
  2770. && in_shndx != elfcpp::SHN_ABS
  2771. && !Symbol::is_common_shndx(in_shndx))
  2772. {
  2773. gold_error(_("%s: unsupported symbol section 0x%x"),
  2774. sym->demangled_name().c_str(), in_shndx);
  2775. shndx = in_shndx;
  2776. }
  2777. else
  2778. {
  2779. Object* symobj = sym->object();
  2780. if (symobj->is_dynamic())
  2781. {
  2782. if (sym->needs_dynsym_value())
  2783. dynsym_value = target.dynsym_value(sym);
  2784. shndx = elfcpp::SHN_UNDEF;
  2785. if (sym->is_undef_binding_weak())
  2786. binding = elfcpp::STB_WEAK;
  2787. else
  2788. binding = elfcpp::STB_GLOBAL;
  2789. }
  2790. else if (symobj->pluginobj() != NULL)
  2791. shndx = elfcpp::SHN_UNDEF;
  2792. else if (in_shndx == elfcpp::SHN_UNDEF
  2793. || (!is_ordinary
  2794. && (in_shndx == elfcpp::SHN_ABS
  2795. || Symbol::is_common_shndx(in_shndx))))
  2796. shndx = in_shndx;
  2797. else
  2798. {
  2799. Relobj* relobj = static_cast<Relobj*>(symobj);
  2800. Output_section* os = relobj->output_section(in_shndx);
  2801. if (this->is_section_folded(relobj, in_shndx))
  2802. {
  2803. // This global symbol must be written out even though
  2804. // it is folded.
  2805. // Get the os of the section it is folded onto.
  2806. Section_id folded =
  2807. this->icf_->get_folded_section(relobj, in_shndx);
  2808. gold_assert(folded.first !=NULL);
  2809. Relobj* folded_obj =
  2810. reinterpret_cast<Relobj*>(folded.first);
  2811. os = folded_obj->output_section(folded.second);
  2812. gold_assert(os != NULL);
  2813. }
  2814. gold_assert(os != NULL);
  2815. shndx = os->out_shndx();
  2816. if (shndx >= elfcpp::SHN_LORESERVE)
  2817. {
  2818. if (sym_index != -1U)
  2819. symtab_xindex->add(sym_index, shndx);
  2820. if (dynsym_index != -1U)
  2821. dynsym_xindex->add(dynsym_index, shndx);
  2822. shndx = elfcpp::SHN_XINDEX;
  2823. }
  2824. // In object files symbol values are section
  2825. // relative.
  2826. if (parameters->options().relocatable())
  2827. sym_value -= os->address();
  2828. }
  2829. }
  2830. }
  2831. break;
  2832. case Symbol::IN_OUTPUT_DATA:
  2833. {
  2834. Output_data* od = sym->output_data();
  2835. shndx = od->out_shndx();
  2836. if (shndx >= elfcpp::SHN_LORESERVE)
  2837. {
  2838. if (sym_index != -1U)
  2839. symtab_xindex->add(sym_index, shndx);
  2840. if (dynsym_index != -1U)
  2841. dynsym_xindex->add(dynsym_index, shndx);
  2842. shndx = elfcpp::SHN_XINDEX;
  2843. }
  2844. // In object files symbol values are section
  2845. // relative.
  2846. if (parameters->options().relocatable())
  2847. {
  2848. Output_section* os = od->output_section();
  2849. gold_assert(os != NULL);
  2850. sym_value -= os->address();
  2851. }
  2852. }
  2853. break;
  2854. case Symbol::IN_OUTPUT_SEGMENT:
  2855. {
  2856. Output_segment* oseg = sym->output_segment();
  2857. Output_section* osect = oseg->first_section();
  2858. if (osect == NULL)
  2859. shndx = elfcpp::SHN_ABS;
  2860. else
  2861. shndx = osect->out_shndx();
  2862. }
  2863. break;
  2864. case Symbol::IS_CONSTANT:
  2865. shndx = elfcpp::SHN_ABS;
  2866. break;
  2867. case Symbol::IS_UNDEFINED:
  2868. shndx = elfcpp::SHN_UNDEF;
  2869. break;
  2870. default:
  2871. gold_unreachable();
  2872. }
  2873. if (sym_index != -1U)
  2874. {
  2875. sym_index -= first_global_index;
  2876. gold_assert(sym_index < output_count);
  2877. unsigned char* ps = psyms + (sym_index * sym_size);
  2878. this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
  2879. binding, sympool, ps);
  2880. }
  2881. if (dynsym_index != -1U)
  2882. {
  2883. dynsym_index -= first_dynamic_global_index;
  2884. gold_assert(dynsym_index < dynamic_count);
  2885. unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
  2886. this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
  2887. binding, dynpool, pd);
  2888. // Allow a target to adjust dynamic symbol value.
  2889. parameters->target().adjust_dyn_symbol(sym, pd);
  2890. }
  2891. }
  2892. // Write the target-specific symbols.
  2893. for (std::vector<Symbol*>::const_iterator p = this->target_symbols_.begin();
  2894. p != this->target_symbols_.end();
  2895. ++p)
  2896. {
  2897. Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(*p);
  2898. unsigned int sym_index = sym->symtab_index();
  2899. unsigned int dynsym_index;
  2900. if (dynamic_view == NULL)
  2901. dynsym_index = -1U;
  2902. else
  2903. dynsym_index = sym->dynsym_index();
  2904. unsigned int shndx;
  2905. switch (sym->source())
  2906. {
  2907. case Symbol::IS_CONSTANT:
  2908. shndx = elfcpp::SHN_ABS;
  2909. break;
  2910. case Symbol::IS_UNDEFINED:
  2911. shndx = elfcpp::SHN_UNDEF;
  2912. break;
  2913. default:
  2914. gold_unreachable();
  2915. }
  2916. if (sym_index != -1U)
  2917. {
  2918. sym_index -= first_global_index;
  2919. gold_assert(sym_index < output_count);
  2920. unsigned char* ps = psyms + (sym_index * sym_size);
  2921. this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
  2922. sym->binding(), sympool,
  2923. ps);
  2924. }
  2925. if (dynsym_index != -1U)
  2926. {
  2927. dynsym_index -= first_dynamic_global_index;
  2928. gold_assert(dynsym_index < dynamic_count);
  2929. unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
  2930. this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
  2931. sym->binding(), dynpool,
  2932. pd);
  2933. }
  2934. }
  2935. of->write_output_view(this->offset_, oview_size, psyms);
  2936. if (dynamic_view != NULL)
  2937. of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
  2938. }
  2939. // Write out the symbol SYM, in section SHNDX, to P. POOL is the
  2940. // strtab holding the name.
  2941. template<int size, bool big_endian>
  2942. void
  2943. Symbol_table::sized_write_symbol(
  2944. Sized_symbol<size>* sym,
  2945. typename elfcpp::Elf_types<size>::Elf_Addr value,
  2946. unsigned int shndx,
  2947. elfcpp::STB binding,
  2948. const Stringpool* pool,
  2949. unsigned char* p) const
  2950. {
  2951. elfcpp::Sym_write<size, big_endian> osym(p);
  2952. if (sym->version() == NULL || !parameters->options().relocatable())
  2953. osym.put_st_name(pool->get_offset(sym->name()));
  2954. else
  2955. osym.put_st_name(pool->get_offset(sym->versioned_name()));
  2956. osym.put_st_value(value);
  2957. // Use a symbol size of zero for undefined symbols from shared libraries.
  2958. if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
  2959. osym.put_st_size(0);
  2960. else
  2961. osym.put_st_size(sym->symsize());
  2962. elfcpp::STT type = sym->type();
  2963. gold_assert(type != elfcpp::STT_GNU_IFUNC || !sym->is_from_dynobj());
  2964. // A version script may have overridden the default binding.
  2965. if (sym->is_forced_local())
  2966. osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
  2967. else
  2968. osym.put_st_info(elfcpp::elf_st_info(binding, type));
  2969. osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
  2970. osym.put_st_shndx(shndx);
  2971. }
  2972. // Check for unresolved symbols in shared libraries. This is
  2973. // controlled by the --allow-shlib-undefined option.
  2974. // We only warn about libraries for which we have seen all the
  2975. // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
  2976. // which were not seen in this link. If we didn't see a DT_NEEDED
  2977. // entry, we aren't going to be able to reliably report whether the
  2978. // symbol is undefined.
  2979. // We also don't warn about libraries found in a system library
  2980. // directory (e.g., /lib or /usr/lib); we assume that those libraries
  2981. // are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
  2982. // can have undefined references satisfied by ld-linux.so.
  2983. inline void
  2984. Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
  2985. {
  2986. bool dummy;
  2987. if (sym->source() == Symbol::FROM_OBJECT
  2988. && sym->object()->is_dynamic()
  2989. && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
  2990. && sym->binding() != elfcpp::STB_WEAK
  2991. && !parameters->options().allow_shlib_undefined()
  2992. && !parameters->target().is_defined_by_abi(sym)
  2993. && !sym->object()->is_in_system_directory())
  2994. {
  2995. // A very ugly cast.
  2996. Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
  2997. if (!dynobj->has_unknown_needed_entries())
  2998. gold_undefined_symbol(sym);
  2999. }
  3000. }
  3001. // Write out a section symbol. Return the update offset.
  3002. void
  3003. Symbol_table::write_section_symbol(const Output_section* os,
  3004. Output_symtab_xindex* symtab_xindex,
  3005. Output_file* of,
  3006. off_t offset) const
  3007. {
  3008. switch (parameters->size_and_endianness())
  3009. {
  3010. #ifdef HAVE_TARGET_32_LITTLE
  3011. case Parameters::TARGET_32_LITTLE:
  3012. this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
  3013. offset);
  3014. break;
  3015. #endif
  3016. #ifdef HAVE_TARGET_32_BIG
  3017. case Parameters::TARGET_32_BIG:
  3018. this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
  3019. offset);
  3020. break;
  3021. #endif
  3022. #ifdef HAVE_TARGET_64_LITTLE
  3023. case Parameters::TARGET_64_LITTLE:
  3024. this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
  3025. offset);
  3026. break;
  3027. #endif
  3028. #ifdef HAVE_TARGET_64_BIG
  3029. case Parameters::TARGET_64_BIG:
  3030. this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
  3031. offset);
  3032. break;
  3033. #endif
  3034. default:
  3035. gold_unreachable();
  3036. }
  3037. }
  3038. // Write out a section symbol, specialized for size and endianness.
  3039. template<int size, bool big_endian>
  3040. void
  3041. Symbol_table::sized_write_section_symbol(const Output_section* os,
  3042. Output_symtab_xindex* symtab_xindex,
  3043. Output_file* of,
  3044. off_t offset) const
  3045. {
  3046. const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  3047. unsigned char* pov = of->get_output_view(offset, sym_size);
  3048. elfcpp::Sym_write<size, big_endian> osym(pov);
  3049. osym.put_st_name(0);
  3050. if (parameters->options().relocatable())
  3051. osym.put_st_value(0);
  3052. else
  3053. osym.put_st_value(os->address());
  3054. osym.put_st_size(0);
  3055. osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
  3056. elfcpp::STT_SECTION));
  3057. osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
  3058. unsigned int shndx = os->out_shndx();
  3059. if (shndx >= elfcpp::SHN_LORESERVE)
  3060. {
  3061. symtab_xindex->add(os->symtab_index(), shndx);
  3062. shndx = elfcpp::SHN_XINDEX;
  3063. }
  3064. osym.put_st_shndx(shndx);
  3065. of->write_output_view(offset, sym_size, pov);
  3066. }
  3067. // Print statistical information to stderr. This is used for --stats.
  3068. void
  3069. Symbol_table::print_stats() const
  3070. {
  3071. #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
  3072. fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
  3073. program_name, this->table_.size(), this->table_.bucket_count());
  3074. #else
  3075. fprintf(stderr, _("%s: symbol table entries: %zu\n"),
  3076. program_name, this->table_.size());
  3077. #endif
  3078. this->namepool_.print_stats("symbol table stringpool");
  3079. }
  3080. // We check for ODR violations by looking for symbols with the same
  3081. // name for which the debugging information reports that they were
  3082. // defined in disjoint source locations. When comparing the source
  3083. // location, we consider instances with the same base filename to be
  3084. // the same. This is because different object files/shared libraries
  3085. // can include the same header file using different paths, and
  3086. // different optimization settings can make the line number appear to
  3087. // be a couple lines off, and we don't want to report an ODR violation
  3088. // in those cases.
  3089. // This struct is used to compare line information, as returned by
  3090. // Dwarf_line_info::one_addr2line. It implements a < comparison
  3091. // operator used with std::sort.
  3092. struct Odr_violation_compare
  3093. {
  3094. bool
  3095. operator()(const std::string& s1, const std::string& s2) const
  3096. {
  3097. // Inputs should be of the form "dirname/filename:linenum" where
  3098. // "dirname/" is optional. We want to compare just the filename:linenum.
  3099. // Find the last '/' in each string.
  3100. std::string::size_type s1begin = s1.rfind('/');
  3101. std::string::size_type s2begin = s2.rfind('/');
  3102. // If there was no '/' in a string, start at the beginning.
  3103. if (s1begin == std::string::npos)
  3104. s1begin = 0;
  3105. if (s2begin == std::string::npos)
  3106. s2begin = 0;
  3107. return s1.compare(s1begin, std::string::npos,
  3108. s2, s2begin, std::string::npos) < 0;
  3109. }
  3110. };
  3111. // Returns all of the lines attached to LOC, not just the one the
  3112. // instruction actually came from.
  3113. std::vector<std::string>
  3114. Symbol_table::linenos_from_loc(const Task* task,
  3115. const Symbol_location& loc)
  3116. {
  3117. // We need to lock the object in order to read it. This
  3118. // means that we have to run in a singleton Task. If we
  3119. // want to run this in a general Task for better
  3120. // performance, we will need one Task for object, plus
  3121. // appropriate locking to ensure that we don't conflict with
  3122. // other uses of the object. Also note, one_addr2line is not
  3123. // currently thread-safe.
  3124. Task_lock_obj<Object> tl(task, loc.object);
  3125. std::vector<std::string> result;
  3126. Symbol_location code_loc = loc;
  3127. parameters->target().function_location(&code_loc);
  3128. // 16 is the size of the object-cache that one_addr2line should use.
  3129. std::string canonical_result = Dwarf_line_info::one_addr2line(
  3130. code_loc.object, code_loc.shndx, code_loc.offset, 16, &result);
  3131. if (!canonical_result.empty())
  3132. result.push_back(canonical_result);
  3133. return result;
  3134. }
  3135. // OutputIterator that records if it was ever assigned to. This
  3136. // allows it to be used with std::set_intersection() to check for
  3137. // intersection rather than computing the intersection.
  3138. struct Check_intersection
  3139. {
  3140. Check_intersection()
  3141. : value_(false)
  3142. {}
  3143. bool had_intersection() const
  3144. { return this->value_; }
  3145. Check_intersection& operator++()
  3146. { return *this; }
  3147. Check_intersection& operator*()
  3148. { return *this; }
  3149. template<typename T>
  3150. Check_intersection& operator=(const T&)
  3151. {
  3152. this->value_ = true;
  3153. return *this;
  3154. }
  3155. private:
  3156. bool value_;
  3157. };
  3158. // Check candidate_odr_violations_ to find symbols with the same name
  3159. // but apparently different definitions (different source-file/line-no
  3160. // for each line assigned to the first instruction).
  3161. void
  3162. Symbol_table::detect_odr_violations(const Task* task,
  3163. const char* output_file_name) const
  3164. {
  3165. for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
  3166. it != candidate_odr_violations_.end();
  3167. ++it)
  3168. {
  3169. const char* const symbol_name = it->first;
  3170. std::string first_object_name;
  3171. std::vector<std::string> first_object_linenos;
  3172. Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
  3173. locs = it->second.begin();
  3174. const Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
  3175. locs_end = it->second.end();
  3176. for (; locs != locs_end && first_object_linenos.empty(); ++locs)
  3177. {
  3178. // Save the line numbers from the first definition to
  3179. // compare to the other definitions. Ideally, we'd compare
  3180. // every definition to every other, but we don't want to
  3181. // take O(N^2) time to do this. This shortcut may cause
  3182. // false negatives that appear or disappear depending on the
  3183. // link order, but it won't cause false positives.
  3184. first_object_name = locs->object->name();
  3185. first_object_linenos = this->linenos_from_loc(task, *locs);
  3186. }
  3187. if (first_object_linenos.empty())
  3188. continue;
  3189. // Sort by Odr_violation_compare to make std::set_intersection work.
  3190. std::string first_object_canonical_result = first_object_linenos.back();
  3191. std::sort(first_object_linenos.begin(), first_object_linenos.end(),
  3192. Odr_violation_compare());
  3193. for (; locs != locs_end; ++locs)
  3194. {
  3195. std::vector<std::string> linenos =
  3196. this->linenos_from_loc(task, *locs);
  3197. // linenos will be empty if we couldn't parse the debug info.
  3198. if (linenos.empty())
  3199. continue;
  3200. // Sort by Odr_violation_compare to make std::set_intersection work.
  3201. gold_assert(!linenos.empty());
  3202. std::string second_object_canonical_result = linenos.back();
  3203. std::sort(linenos.begin(), linenos.end(), Odr_violation_compare());
  3204. Check_intersection intersection_result =
  3205. std::set_intersection(first_object_linenos.begin(),
  3206. first_object_linenos.end(),
  3207. linenos.begin(),
  3208. linenos.end(),
  3209. Check_intersection(),
  3210. Odr_violation_compare());
  3211. if (!intersection_result.had_intersection())
  3212. {
  3213. gold_warning(_("while linking %s: symbol '%s' defined in "
  3214. "multiple places (possible ODR violation):"),
  3215. output_file_name, demangle(symbol_name).c_str());
  3216. // This only prints one location from each definition,
  3217. // which may not be the location we expect to intersect
  3218. // with another definition. We could print the whole
  3219. // set of locations, but that seems too verbose.
  3220. fprintf(stderr, _(" %s from %s\n"),
  3221. first_object_canonical_result.c_str(),
  3222. first_object_name.c_str());
  3223. fprintf(stderr, _(" %s from %s\n"),
  3224. second_object_canonical_result.c_str(),
  3225. locs->object->name().c_str());
  3226. // Only print one broken pair, to avoid needing to
  3227. // compare against a list of the disjoint definition
  3228. // locations we've found so far. (If we kept comparing
  3229. // against just the first one, we'd get a lot of
  3230. // redundant complaints about the second definition
  3231. // location.)
  3232. break;
  3233. }
  3234. }
  3235. }
  3236. // We only call one_addr2line() in this function, so we can clear its cache.
  3237. Dwarf_line_info::clear_addr2line_cache();
  3238. }
  3239. // Warnings functions.
  3240. // Add a new warning.
  3241. void
  3242. Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
  3243. const std::string& warning)
  3244. {
  3245. name = symtab->canonicalize_name(name);
  3246. this->warnings_[name].set(obj, warning);
  3247. }
  3248. // Look through the warnings and mark the symbols for which we should
  3249. // warn. This is called during Layout::finalize when we know the
  3250. // sources for all the symbols.
  3251. void
  3252. Warnings::note_warnings(Symbol_table* symtab)
  3253. {
  3254. for (Warning_table::iterator p = this->warnings_.begin();
  3255. p != this->warnings_.end();
  3256. ++p)
  3257. {
  3258. Symbol* sym = symtab->lookup(p->first, NULL);
  3259. if (sym != NULL
  3260. && sym->source() == Symbol::FROM_OBJECT
  3261. && sym->object() == p->second.object)
  3262. sym->set_has_warning();
  3263. }
  3264. }
  3265. // Issue a warning. This is called when we see a relocation against a
  3266. // symbol for which has a warning.
  3267. template<int size, bool big_endian>
  3268. void
  3269. Warnings::issue_warning(const Symbol* sym,
  3270. const Relocate_info<size, big_endian>* relinfo,
  3271. size_t relnum, off_t reloffset) const
  3272. {
  3273. gold_assert(sym->has_warning());
  3274. // We don't want to issue a warning for a relocation against the
  3275. // symbol in the same object file in which the symbol is defined.
  3276. if (sym->object() == relinfo->object)
  3277. return;
  3278. Warning_table::const_iterator p = this->warnings_.find(sym->name());
  3279. gold_assert(p != this->warnings_.end());
  3280. gold_warning_at_location(relinfo, relnum, reloffset,
  3281. "%s", p->second.text.c_str());
  3282. }
  3283. // Instantiate the templates we need. We could use the configure
  3284. // script to restrict this to only the ones needed for implemented
  3285. // targets.
  3286. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  3287. template
  3288. void
  3289. Sized_symbol<32>::allocate_common(Output_data*, Value_type);
  3290. #endif
  3291. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  3292. template
  3293. void
  3294. Sized_symbol<64>::allocate_common(Output_data*, Value_type);
  3295. #endif
  3296. #ifdef HAVE_TARGET_32_LITTLE
  3297. template
  3298. void
  3299. Symbol_table::add_from_relobj<32, false>(
  3300. Sized_relobj_file<32, false>* relobj,
  3301. const unsigned char* syms,
  3302. size_t count,
  3303. size_t symndx_offset,
  3304. const char* sym_names,
  3305. size_t sym_name_size,
  3306. Sized_relobj_file<32, false>::Symbols* sympointers,
  3307. size_t* defined);
  3308. #endif
  3309. #ifdef HAVE_TARGET_32_BIG
  3310. template
  3311. void
  3312. Symbol_table::add_from_relobj<32, true>(
  3313. Sized_relobj_file<32, true>* relobj,
  3314. const unsigned char* syms,
  3315. size_t count,
  3316. size_t symndx_offset,
  3317. const char* sym_names,
  3318. size_t sym_name_size,
  3319. Sized_relobj_file<32, true>::Symbols* sympointers,
  3320. size_t* defined);
  3321. #endif
  3322. #ifdef HAVE_TARGET_64_LITTLE
  3323. template
  3324. void
  3325. Symbol_table::add_from_relobj<64, false>(
  3326. Sized_relobj_file<64, false>* relobj,
  3327. const unsigned char* syms,
  3328. size_t count,
  3329. size_t symndx_offset,
  3330. const char* sym_names,
  3331. size_t sym_name_size,
  3332. Sized_relobj_file<64, false>::Symbols* sympointers,
  3333. size_t* defined);
  3334. #endif
  3335. #ifdef HAVE_TARGET_64_BIG
  3336. template
  3337. void
  3338. Symbol_table::add_from_relobj<64, true>(
  3339. Sized_relobj_file<64, true>* relobj,
  3340. const unsigned char* syms,
  3341. size_t count,
  3342. size_t symndx_offset,
  3343. const char* sym_names,
  3344. size_t sym_name_size,
  3345. Sized_relobj_file<64, true>::Symbols* sympointers,
  3346. size_t* defined);
  3347. #endif
  3348. #ifdef HAVE_TARGET_32_LITTLE
  3349. template
  3350. Symbol*
  3351. Symbol_table::add_from_pluginobj<32, false>(
  3352. Sized_pluginobj<32, false>* obj,
  3353. const char* name,
  3354. const char* ver,
  3355. elfcpp::Sym<32, false>* sym);
  3356. #endif
  3357. #ifdef HAVE_TARGET_32_BIG
  3358. template
  3359. Symbol*
  3360. Symbol_table::add_from_pluginobj<32, true>(
  3361. Sized_pluginobj<32, true>* obj,
  3362. const char* name,
  3363. const char* ver,
  3364. elfcpp::Sym<32, true>* sym);
  3365. #endif
  3366. #ifdef HAVE_TARGET_64_LITTLE
  3367. template
  3368. Symbol*
  3369. Symbol_table::add_from_pluginobj<64, false>(
  3370. Sized_pluginobj<64, false>* obj,
  3371. const char* name,
  3372. const char* ver,
  3373. elfcpp::Sym<64, false>* sym);
  3374. #endif
  3375. #ifdef HAVE_TARGET_64_BIG
  3376. template
  3377. Symbol*
  3378. Symbol_table::add_from_pluginobj<64, true>(
  3379. Sized_pluginobj<64, true>* obj,
  3380. const char* name,
  3381. const char* ver,
  3382. elfcpp::Sym<64, true>* sym);
  3383. #endif
  3384. #ifdef HAVE_TARGET_32_LITTLE
  3385. template
  3386. void
  3387. Symbol_table::add_from_dynobj<32, false>(
  3388. Sized_dynobj<32, false>* dynobj,
  3389. const unsigned char* syms,
  3390. size_t count,
  3391. const char* sym_names,
  3392. size_t sym_name_size,
  3393. const unsigned char* versym,
  3394. size_t versym_size,
  3395. const std::vector<const char*>* version_map,
  3396. Sized_relobj_file<32, false>::Symbols* sympointers,
  3397. size_t* defined);
  3398. #endif
  3399. #ifdef HAVE_TARGET_32_BIG
  3400. template
  3401. void
  3402. Symbol_table::add_from_dynobj<32, true>(
  3403. Sized_dynobj<32, true>* dynobj,
  3404. const unsigned char* syms,
  3405. size_t count,
  3406. const char* sym_names,
  3407. size_t sym_name_size,
  3408. const unsigned char* versym,
  3409. size_t versym_size,
  3410. const std::vector<const char*>* version_map,
  3411. Sized_relobj_file<32, true>::Symbols* sympointers,
  3412. size_t* defined);
  3413. #endif
  3414. #ifdef HAVE_TARGET_64_LITTLE
  3415. template
  3416. void
  3417. Symbol_table::add_from_dynobj<64, false>(
  3418. Sized_dynobj<64, false>* dynobj,
  3419. const unsigned char* syms,
  3420. size_t count,
  3421. const char* sym_names,
  3422. size_t sym_name_size,
  3423. const unsigned char* versym,
  3424. size_t versym_size,
  3425. const std::vector<const char*>* version_map,
  3426. Sized_relobj_file<64, false>::Symbols* sympointers,
  3427. size_t* defined);
  3428. #endif
  3429. #ifdef HAVE_TARGET_64_BIG
  3430. template
  3431. void
  3432. Symbol_table::add_from_dynobj<64, true>(
  3433. Sized_dynobj<64, true>* dynobj,
  3434. const unsigned char* syms,
  3435. size_t count,
  3436. const char* sym_names,
  3437. size_t sym_name_size,
  3438. const unsigned char* versym,
  3439. size_t versym_size,
  3440. const std::vector<const char*>* version_map,
  3441. Sized_relobj_file<64, true>::Symbols* sympointers,
  3442. size_t* defined);
  3443. #endif
  3444. #ifdef HAVE_TARGET_32_LITTLE
  3445. template
  3446. Sized_symbol<32>*
  3447. Symbol_table::add_from_incrobj(
  3448. Object* obj,
  3449. const char* name,
  3450. const char* ver,
  3451. elfcpp::Sym<32, false>* sym);
  3452. #endif
  3453. #ifdef HAVE_TARGET_32_BIG
  3454. template
  3455. Sized_symbol<32>*
  3456. Symbol_table::add_from_incrobj(
  3457. Object* obj,
  3458. const char* name,
  3459. const char* ver,
  3460. elfcpp::Sym<32, true>* sym);
  3461. #endif
  3462. #ifdef HAVE_TARGET_64_LITTLE
  3463. template
  3464. Sized_symbol<64>*
  3465. Symbol_table::add_from_incrobj(
  3466. Object* obj,
  3467. const char* name,
  3468. const char* ver,
  3469. elfcpp::Sym<64, false>* sym);
  3470. #endif
  3471. #ifdef HAVE_TARGET_64_BIG
  3472. template
  3473. Sized_symbol<64>*
  3474. Symbol_table::add_from_incrobj(
  3475. Object* obj,
  3476. const char* name,
  3477. const char* ver,
  3478. elfcpp::Sym<64, true>* sym);
  3479. #endif
  3480. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  3481. template
  3482. void
  3483. Symbol_table::define_with_copy_reloc<32>(
  3484. Sized_symbol<32>* sym,
  3485. Output_data* posd,
  3486. elfcpp::Elf_types<32>::Elf_Addr value);
  3487. #endif
  3488. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  3489. template
  3490. void
  3491. Symbol_table::define_with_copy_reloc<64>(
  3492. Sized_symbol<64>* sym,
  3493. Output_data* posd,
  3494. elfcpp::Elf_types<64>::Elf_Addr value);
  3495. #endif
  3496. #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
  3497. template
  3498. void
  3499. Sized_symbol<32>::init_output_data(const char* name, const char* version,
  3500. Output_data* od, Value_type value,
  3501. Size_type symsize, elfcpp::STT type,
  3502. elfcpp::STB binding,
  3503. elfcpp::STV visibility,
  3504. unsigned char nonvis,
  3505. bool offset_is_from_end,
  3506. bool is_predefined);
  3507. template
  3508. void
  3509. Sized_symbol<32>::init_constant(const char* name, const char* version,
  3510. Value_type value, Size_type symsize,
  3511. elfcpp::STT type, elfcpp::STB binding,
  3512. elfcpp::STV visibility, unsigned char nonvis,
  3513. bool is_predefined);
  3514. template
  3515. void
  3516. Sized_symbol<32>::init_undefined(const char* name, const char* version,
  3517. Value_type value, elfcpp::STT type,
  3518. elfcpp::STB binding, elfcpp::STV visibility,
  3519. unsigned char nonvis);
  3520. #endif
  3521. #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
  3522. template
  3523. void
  3524. Sized_symbol<64>::init_output_data(const char* name, const char* version,
  3525. Output_data* od, Value_type value,
  3526. Size_type symsize, elfcpp::STT type,
  3527. elfcpp::STB binding,
  3528. elfcpp::STV visibility,
  3529. unsigned char nonvis,
  3530. bool offset_is_from_end,
  3531. bool is_predefined);
  3532. template
  3533. void
  3534. Sized_symbol<64>::init_constant(const char* name, const char* version,
  3535. Value_type value, Size_type symsize,
  3536. elfcpp::STT type, elfcpp::STB binding,
  3537. elfcpp::STV visibility, unsigned char nonvis,
  3538. bool is_predefined);
  3539. template
  3540. void
  3541. Sized_symbol<64>::init_undefined(const char* name, const char* version,
  3542. Value_type value, elfcpp::STT type,
  3543. elfcpp::STB binding, elfcpp::STV visibility,
  3544. unsigned char nonvis);
  3545. #endif
  3546. #ifdef HAVE_TARGET_32_LITTLE
  3547. template
  3548. void
  3549. Warnings::issue_warning<32, false>(const Symbol* sym,
  3550. const Relocate_info<32, false>* relinfo,
  3551. size_t relnum, off_t reloffset) const;
  3552. #endif
  3553. #ifdef HAVE_TARGET_32_BIG
  3554. template
  3555. void
  3556. Warnings::issue_warning<32, true>(const Symbol* sym,
  3557. const Relocate_info<32, true>* relinfo,
  3558. size_t relnum, off_t reloffset) const;
  3559. #endif
  3560. #ifdef HAVE_TARGET_64_LITTLE
  3561. template
  3562. void
  3563. Warnings::issue_warning<64, false>(const Symbol* sym,
  3564. const Relocate_info<64, false>* relinfo,
  3565. size_t relnum, off_t reloffset) const;
  3566. #endif
  3567. #ifdef HAVE_TARGET_64_BIG
  3568. template
  3569. void
  3570. Warnings::issue_warning<64, true>(const Symbol* sym,
  3571. const Relocate_info<64, true>* relinfo,
  3572. size_t relnum, off_t reloffset) const;
  3573. #endif
  3574. } // End namespace gold.