object.h 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090
  1. // object.h -- support for an object file for linking in gold -*- C++ -*-
  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. #ifndef GOLD_OBJECT_H
  18. #define GOLD_OBJECT_H
  19. #include <string>
  20. #include <vector>
  21. #include "elfcpp.h"
  22. #include "elfcpp_file.h"
  23. #include "fileread.h"
  24. #include "target.h"
  25. #include "archive.h"
  26. namespace gold
  27. {
  28. class General_options;
  29. class Task;
  30. class Cref;
  31. class Layout;
  32. class Kept_section;
  33. class Output_data;
  34. class Output_section;
  35. class Output_section_data;
  36. class Output_file;
  37. class Output_symtab_xindex;
  38. class Pluginobj;
  39. class Dynobj;
  40. class Object_merge_map;
  41. class Relocatable_relocs;
  42. struct Symbols_data;
  43. template<typename Stringpool_char>
  44. class Stringpool_template;
  45. // Data to pass from read_symbols() to add_symbols().
  46. struct Read_symbols_data
  47. {
  48. Read_symbols_data()
  49. : section_headers(NULL), section_names(NULL), symbols(NULL),
  50. symbol_names(NULL), versym(NULL), verdef(NULL), verneed(NULL)
  51. { }
  52. ~Read_symbols_data();
  53. // Section headers.
  54. File_view* section_headers;
  55. // Section names.
  56. File_view* section_names;
  57. // Size of section name data in bytes.
  58. section_size_type section_names_size;
  59. // Symbol data.
  60. File_view* symbols;
  61. // Size of symbol data in bytes.
  62. section_size_type symbols_size;
  63. // Offset of external symbols within symbol data. This structure
  64. // sometimes contains only external symbols, in which case this will
  65. // be zero. Sometimes it contains all symbols.
  66. section_offset_type external_symbols_offset;
  67. // Symbol names.
  68. File_view* symbol_names;
  69. // Size of symbol name data in bytes.
  70. section_size_type symbol_names_size;
  71. // Version information. This is only used on dynamic objects.
  72. // Version symbol data (from SHT_GNU_versym section).
  73. File_view* versym;
  74. section_size_type versym_size;
  75. // Version definition data (from SHT_GNU_verdef section).
  76. File_view* verdef;
  77. section_size_type verdef_size;
  78. unsigned int verdef_info;
  79. // Needed version data (from SHT_GNU_verneed section).
  80. File_view* verneed;
  81. section_size_type verneed_size;
  82. unsigned int verneed_info;
  83. };
  84. // Information used to print error messages.
  85. struct Symbol_location_info
  86. {
  87. std::string source_file;
  88. std::string enclosing_symbol_name;
  89. elfcpp::STT enclosing_symbol_type;
  90. };
  91. // Data about a single relocation section. This is read in
  92. // read_relocs and processed in scan_relocs.
  93. struct Section_relocs
  94. {
  95. Section_relocs()
  96. : contents(NULL)
  97. { }
  98. ~Section_relocs()
  99. { delete this->contents; }
  100. // Index of reloc section.
  101. unsigned int reloc_shndx;
  102. // Index of section that relocs apply to.
  103. unsigned int data_shndx;
  104. // Contents of reloc section.
  105. File_view* contents;
  106. // Reloc section type.
  107. unsigned int sh_type;
  108. // Number of reloc entries.
  109. size_t reloc_count;
  110. // Output section.
  111. Output_section* output_section;
  112. // Whether this section has special handling for offsets.
  113. bool needs_special_offset_handling;
  114. // Whether the data section is allocated (has the SHF_ALLOC flag set).
  115. bool is_data_section_allocated;
  116. };
  117. // Relocations in an object file. This is read in read_relocs and
  118. // processed in scan_relocs.
  119. struct Read_relocs_data
  120. {
  121. Read_relocs_data()
  122. : local_symbols(NULL)
  123. { }
  124. ~Read_relocs_data()
  125. { delete this->local_symbols; }
  126. typedef std::vector<Section_relocs> Relocs_list;
  127. // The relocations.
  128. Relocs_list relocs;
  129. // The local symbols.
  130. File_view* local_symbols;
  131. };
  132. // The Xindex class manages section indexes for objects with more than
  133. // 0xff00 sections.
  134. class Xindex
  135. {
  136. public:
  137. Xindex(int large_shndx_offset)
  138. : large_shndx_offset_(large_shndx_offset), symtab_xindex_()
  139. { }
  140. // Initialize the symtab_xindex_ array, given the object and the
  141. // section index of the symbol table to use.
  142. template<int size, bool big_endian>
  143. void
  144. initialize_symtab_xindex(Object*, unsigned int symtab_shndx);
  145. // Read in the symtab_xindex_ array, given its section index.
  146. // PSHDRS may optionally point to the section headers.
  147. template<int size, bool big_endian>
  148. void
  149. read_symtab_xindex(Object*, unsigned int xindex_shndx,
  150. const unsigned char* pshdrs);
  151. // Symbol SYMNDX in OBJECT has a section of SHN_XINDEX; return the
  152. // real section index.
  153. unsigned int
  154. sym_xindex_to_shndx(Object* object, unsigned int symndx);
  155. private:
  156. // The type of the array giving the real section index for symbols
  157. // whose st_shndx field holds SHN_XINDEX.
  158. typedef std::vector<unsigned int> Symtab_xindex;
  159. // Adjust a section index if necessary. This should only be called
  160. // for ordinary section indexes.
  161. unsigned int
  162. adjust_shndx(unsigned int shndx)
  163. {
  164. if (shndx >= elfcpp::SHN_LORESERVE)
  165. shndx += this->large_shndx_offset_;
  166. return shndx;
  167. }
  168. // Adjust to apply to large section indexes.
  169. int large_shndx_offset_;
  170. // The data from the SHT_SYMTAB_SHNDX section.
  171. Symtab_xindex symtab_xindex_;
  172. };
  173. // A GOT offset list. A symbol may have more than one GOT offset
  174. // (e.g., when mixing modules compiled with two different TLS models),
  175. // but will usually have at most one. GOT_TYPE identifies the type of
  176. // GOT entry; its values are specific to each target.
  177. class Got_offset_list
  178. {
  179. public:
  180. Got_offset_list()
  181. : got_type_(-1U), got_offset_(0), addend_(0), got_next_(NULL)
  182. { }
  183. Got_offset_list(unsigned int got_type, unsigned int got_offset,
  184. uint64_t addend)
  185. : got_type_(got_type), got_offset_(got_offset), addend_(addend),
  186. got_next_(NULL)
  187. { }
  188. ~Got_offset_list()
  189. {
  190. if (this->got_next_ != NULL)
  191. {
  192. delete this->got_next_;
  193. this->got_next_ = NULL;
  194. }
  195. }
  196. // Initialize the fields to their default values.
  197. void
  198. init()
  199. {
  200. this->got_type_ = -1U;
  201. this->got_offset_ = 0;
  202. this->addend_ = 0;
  203. this->got_next_ = NULL;
  204. }
  205. // Set the offset for the GOT entry of type GOT_TYPE.
  206. void
  207. set_offset(unsigned int got_type, unsigned int got_offset, uint64_t addend)
  208. {
  209. if (this->got_type_ == -1U)
  210. {
  211. this->got_type_ = got_type;
  212. this->got_offset_ = got_offset;
  213. this->addend_ = addend;
  214. }
  215. else
  216. {
  217. for (Got_offset_list* g = this; g != NULL; g = g->got_next_)
  218. {
  219. if (g->got_type_ == got_type && g->addend_ == addend)
  220. {
  221. g->got_offset_ = got_offset;
  222. return;
  223. }
  224. }
  225. Got_offset_list* g = new Got_offset_list(got_type, got_offset, addend);
  226. g->got_next_ = this->got_next_;
  227. this->got_next_ = g;
  228. }
  229. }
  230. // Return the offset for a GOT entry of type GOT_TYPE.
  231. unsigned int
  232. get_offset(unsigned int got_type, uint64_t addend) const
  233. {
  234. for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
  235. {
  236. if (g->got_type_ == got_type && g->addend_ == addend)
  237. return g->got_offset_;
  238. }
  239. return -1U;
  240. }
  241. // Return a pointer to the list, or NULL if the list is empty.
  242. const Got_offset_list*
  243. get_list() const
  244. {
  245. if (this->got_type_ == -1U)
  246. return NULL;
  247. return this;
  248. }
  249. // Abstract visitor class for iterating over GOT offsets.
  250. class Visitor
  251. {
  252. public:
  253. Visitor()
  254. { }
  255. virtual
  256. ~Visitor()
  257. { }
  258. virtual void
  259. visit(unsigned int, unsigned int, uint64_t) = 0;
  260. };
  261. // Loop over all GOT offset entries, calling a visitor class V for each.
  262. void
  263. for_all_got_offsets(Visitor* v) const
  264. {
  265. if (this->got_type_ == -1U)
  266. return;
  267. for (const Got_offset_list* g = this; g != NULL; g = g->got_next_)
  268. v->visit(g->got_type_, g->got_offset_, g->addend_);
  269. }
  270. private:
  271. unsigned int got_type_;
  272. unsigned int got_offset_;
  273. uint64_t addend_;
  274. Got_offset_list* got_next_;
  275. };
  276. // The Local_got_entry_key used to index the GOT offsets for local
  277. // non-TLS symbols, and tp-relative offsets for TLS symbols.
  278. class Local_got_entry_key
  279. {
  280. public:
  281. Local_got_entry_key(unsigned int symndx)
  282. : symndx_(symndx)
  283. {}
  284. // Whether this equals to another Local_got_entry_key.
  285. bool
  286. eq(const Local_got_entry_key& key) const
  287. {
  288. return this->symndx_ == key.symndx_;
  289. }
  290. // Compute a hash value for this using 64-bit FNV-1a hash.
  291. size_t
  292. hash_value() const
  293. {
  294. uint64_t h = 14695981039346656037ULL; // FNV offset basis.
  295. uint64_t prime = 1099511628211ULL;
  296. h = (h ^ static_cast<uint64_t>(this->symndx_)) * prime;
  297. return h;
  298. }
  299. // Functors for associative containers.
  300. struct equal_to
  301. {
  302. bool
  303. operator()(const Local_got_entry_key& key1,
  304. const Local_got_entry_key& key2) const
  305. { return key1.eq(key2); }
  306. };
  307. struct hash
  308. {
  309. size_t
  310. operator()(const Local_got_entry_key& key) const
  311. { return key.hash_value(); }
  312. };
  313. private:
  314. // The local symbol index.
  315. unsigned int symndx_;
  316. };
  317. // Type for mapping section index to uncompressed size and contents.
  318. struct Compressed_section_info
  319. {
  320. section_size_type size;
  321. elfcpp::Elf_Xword flag;
  322. uint64_t addralign;
  323. const unsigned char* contents;
  324. };
  325. typedef std::map<unsigned int, Compressed_section_info> Compressed_section_map;
  326. template<int size, bool big_endian>
  327. Compressed_section_map*
  328. build_compressed_section_map(const unsigned char* pshdrs, unsigned int shnum,
  329. const char* names, section_size_type names_size,
  330. Object* obj, bool decompress_if_needed);
  331. // Osabi represents the EI_OSABI field from the ELF header.
  332. class Osabi
  333. {
  334. public:
  335. Osabi(unsigned char ei_osabi)
  336. : ei_osabi_(static_cast<elfcpp::ELFOSABI>(ei_osabi))
  337. { }
  338. bool
  339. has_shf_retain(elfcpp::Elf_Xword sh_flags) const
  340. {
  341. switch (this->ei_osabi_)
  342. {
  343. case elfcpp::ELFOSABI_GNU:
  344. case elfcpp::ELFOSABI_FREEBSD:
  345. return (sh_flags & elfcpp::SHF_GNU_RETAIN) != 0;
  346. default:
  347. break;
  348. }
  349. return false;
  350. }
  351. elfcpp::Elf_Xword
  352. ignored_sh_flags() const
  353. {
  354. switch (this->ei_osabi_)
  355. {
  356. case elfcpp::ELFOSABI_GNU:
  357. case elfcpp::ELFOSABI_FREEBSD:
  358. return elfcpp::SHF_GNU_RETAIN;
  359. default:
  360. break;
  361. }
  362. return 0;
  363. }
  364. private:
  365. elfcpp::ELFOSABI ei_osabi_;
  366. };
  367. // Object is an abstract base class which represents either a 32-bit
  368. // or a 64-bit input object. This can be a regular object file
  369. // (ET_REL) or a shared object (ET_DYN).
  370. class Object
  371. {
  372. public:
  373. typedef std::vector<Symbol*> Symbols;
  374. // NAME is the name of the object as we would report it to the user
  375. // (e.g., libfoo.a(bar.o) if this is in an archive. INPUT_FILE is
  376. // used to read the file. OFFSET is the offset within the input
  377. // file--0 for a .o or .so file, something else for a .a file.
  378. Object(const std::string& name, Input_file* input_file, bool is_dynamic,
  379. off_t offset = 0)
  380. : name_(name), input_file_(input_file), offset_(offset), shnum_(-1U),
  381. is_dynamic_(is_dynamic), is_needed_(false), uses_split_stack_(false),
  382. has_no_split_stack_(false), no_export_(false),
  383. is_in_system_directory_(false), as_needed_(false), xindex_(NULL),
  384. compressed_sections_(NULL)
  385. {
  386. if (input_file != NULL)
  387. {
  388. input_file->file().add_object();
  389. this->is_in_system_directory_ = input_file->is_in_system_directory();
  390. this->as_needed_ = input_file->options().as_needed();
  391. }
  392. }
  393. virtual ~Object()
  394. {
  395. if (this->input_file_ != NULL)
  396. this->input_file_->file().remove_object();
  397. }
  398. // Return the name of the object as we would report it to the user.
  399. const std::string&
  400. name() const
  401. { return this->name_; }
  402. // Get the offset into the file.
  403. off_t
  404. offset() const
  405. { return this->offset_; }
  406. // Return whether this is a dynamic object.
  407. bool
  408. is_dynamic() const
  409. { return this->is_dynamic_; }
  410. // Return the word size of the object file.
  411. virtual int elfsize() const = 0;
  412. // Return TRUE if this is a big-endian object file.
  413. virtual bool is_big_endian() const = 0;
  414. // Return whether this object is needed--true if it is a dynamic
  415. // object which defines some symbol referenced by a regular object.
  416. // We keep the flag here rather than in Dynobj for convenience when
  417. // setting it.
  418. bool
  419. is_needed() const
  420. { return this->is_needed_; }
  421. // Record that this object is needed.
  422. void
  423. set_is_needed()
  424. { this->is_needed_ = true; }
  425. // Return whether this object was compiled with -fsplit-stack.
  426. bool
  427. uses_split_stack() const
  428. { return this->uses_split_stack_; }
  429. // Return whether this object contains any functions compiled with
  430. // the no_split_stack attribute.
  431. bool
  432. has_no_split_stack() const
  433. { return this->has_no_split_stack_; }
  434. // Returns NULL for Objects that are not dynamic objects. This method
  435. // is overridden in the Dynobj class.
  436. Dynobj*
  437. dynobj()
  438. { return this->do_dynobj(); }
  439. // Returns NULL for Objects that are not plugin objects. This method
  440. // is overridden in the Pluginobj class.
  441. Pluginobj*
  442. pluginobj()
  443. { return this->do_pluginobj(); }
  444. // Get the file. We pass on const-ness.
  445. Input_file*
  446. input_file()
  447. {
  448. gold_assert(this->input_file_ != NULL);
  449. return this->input_file_;
  450. }
  451. const Input_file*
  452. input_file() const
  453. {
  454. gold_assert(this->input_file_ != NULL);
  455. return this->input_file_;
  456. }
  457. // Lock the underlying file.
  458. void
  459. lock(const Task* t)
  460. {
  461. if (this->input_file_ != NULL)
  462. this->input_file_->file().lock(t);
  463. }
  464. // Unlock the underlying file.
  465. void
  466. unlock(const Task* t)
  467. {
  468. if (this->input_file_ != NULL)
  469. this->input_file()->file().unlock(t);
  470. }
  471. // Return whether the underlying file is locked.
  472. bool
  473. is_locked() const
  474. { return this->input_file_ != NULL && this->input_file_->file().is_locked(); }
  475. // Return the token, so that the task can be queued.
  476. Task_token*
  477. token()
  478. {
  479. if (this->input_file_ == NULL)
  480. return NULL;
  481. return this->input_file()->file().token();
  482. }
  483. // Release the underlying file.
  484. void
  485. release()
  486. {
  487. if (this->input_file_ != NULL)
  488. this->input_file()->file().release();
  489. }
  490. // Return whether we should just read symbols from this file.
  491. bool
  492. just_symbols() const
  493. { return this->input_file()->just_symbols(); }
  494. // Return whether this is an incremental object.
  495. bool
  496. is_incremental() const
  497. { return this->do_is_incremental(); }
  498. // Return the last modified time of the file.
  499. Timespec
  500. get_mtime()
  501. { return this->do_get_mtime(); }
  502. // Get the number of sections.
  503. unsigned int
  504. shnum() const
  505. { return this->shnum_; }
  506. // Return a view of the contents of a section. Set *PLEN to the
  507. // size. CACHE is a hint as in File_read::get_view.
  508. const unsigned char*
  509. section_contents(unsigned int shndx, section_size_type* plen, bool cache);
  510. // Adjust a symbol's section index as needed. SYMNDX is the index
  511. // of the symbol and SHNDX is the symbol's section from
  512. // get_st_shndx. This returns the section index. It sets
  513. // *IS_ORDINARY to indicate whether this is a normal section index,
  514. // rather than a special code between SHN_LORESERVE and
  515. // SHN_HIRESERVE.
  516. unsigned int
  517. adjust_sym_shndx(unsigned int symndx, unsigned int shndx, bool* is_ordinary)
  518. {
  519. if (shndx < elfcpp::SHN_LORESERVE)
  520. *is_ordinary = true;
  521. else if (shndx == elfcpp::SHN_XINDEX)
  522. {
  523. if (this->xindex_ == NULL)
  524. this->xindex_ = this->do_initialize_xindex();
  525. shndx = this->xindex_->sym_xindex_to_shndx(this, symndx);
  526. *is_ordinary = true;
  527. }
  528. else
  529. *is_ordinary = false;
  530. return shndx;
  531. }
  532. // Return the size of a section given a section index.
  533. uint64_t
  534. section_size(unsigned int shndx)
  535. { return this->do_section_size(shndx); }
  536. // Return the name of a section given a section index.
  537. std::string
  538. section_name(unsigned int shndx) const
  539. { return this->do_section_name(shndx); }
  540. // Return the section flags given a section index.
  541. uint64_t
  542. section_flags(unsigned int shndx)
  543. { return this->do_section_flags(shndx); }
  544. // Return the section entsize given a section index.
  545. uint64_t
  546. section_entsize(unsigned int shndx)
  547. { return this->do_section_entsize(shndx); }
  548. // Return the section address given a section index.
  549. uint64_t
  550. section_address(unsigned int shndx)
  551. { return this->do_section_address(shndx); }
  552. // Return the section type given a section index.
  553. unsigned int
  554. section_type(unsigned int shndx)
  555. { return this->do_section_type(shndx); }
  556. // Return the section link field given a section index.
  557. unsigned int
  558. section_link(unsigned int shndx)
  559. { return this->do_section_link(shndx); }
  560. // Return the section info field given a section index.
  561. unsigned int
  562. section_info(unsigned int shndx)
  563. { return this->do_section_info(shndx); }
  564. // Return the required section alignment given a section index.
  565. uint64_t
  566. section_addralign(unsigned int shndx)
  567. { return this->do_section_addralign(shndx); }
  568. // Return the output section given a section index.
  569. Output_section*
  570. output_section(unsigned int shndx) const
  571. { return this->do_output_section(shndx); }
  572. // Given a section index, return its address.
  573. // The return value will be -1U if the section is specially mapped,
  574. // such as a merge section.
  575. uint64_t
  576. output_section_address(unsigned int shndx)
  577. { return this->do_output_section_address(shndx); }
  578. // Given a section index, return the offset in the Output_section.
  579. // The return value will be -1U if the section is specially mapped,
  580. // such as a merge section.
  581. uint64_t
  582. output_section_offset(unsigned int shndx) const
  583. { return this->do_output_section_offset(shndx); }
  584. // Read the symbol information.
  585. void
  586. read_symbols(Read_symbols_data* sd)
  587. { return this->do_read_symbols(sd); }
  588. // Pass sections which should be included in the link to the Layout
  589. // object, and record where the sections go in the output file.
  590. void
  591. layout(Symbol_table* symtab, Layout* layout, Read_symbols_data* sd)
  592. { this->do_layout(symtab, layout, sd); }
  593. // Add symbol information to the global symbol table.
  594. void
  595. add_symbols(Symbol_table* symtab, Read_symbols_data* sd, Layout *layout)
  596. { this->do_add_symbols(symtab, sd, layout); }
  597. // Add symbol information to the global symbol table.
  598. Archive::Should_include
  599. should_include_member(Symbol_table* symtab, Layout* layout,
  600. Read_symbols_data* sd, std::string* why)
  601. { return this->do_should_include_member(symtab, layout, sd, why); }
  602. // Iterate over global symbols, calling a visitor class V for each.
  603. void
  604. for_all_global_symbols(Read_symbols_data* sd,
  605. Library_base::Symbol_visitor_base* v)
  606. { return this->do_for_all_global_symbols(sd, v); }
  607. // Iterate over local symbols, calling a visitor class V for each GOT offset
  608. // associated with a local symbol.
  609. void
  610. for_all_local_got_entries(Got_offset_list::Visitor* v) const
  611. { this->do_for_all_local_got_entries(v); }
  612. // Functions and types for the elfcpp::Elf_file interface. This
  613. // permit us to use Object as the File template parameter for
  614. // elfcpp::Elf_file.
  615. // The View class is returned by view. It must support a single
  616. // method, data(). This is trivial, because get_view does what we
  617. // need.
  618. class View
  619. {
  620. public:
  621. View(const unsigned char* p)
  622. : p_(p)
  623. { }
  624. const unsigned char*
  625. data() const
  626. { return this->p_; }
  627. private:
  628. const unsigned char* p_;
  629. };
  630. // Return a View.
  631. View
  632. view(off_t file_offset, section_size_type data_size)
  633. { return View(this->get_view(file_offset, data_size, true, true)); }
  634. // Report an error.
  635. void
  636. error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
  637. // A location in the file.
  638. struct Location
  639. {
  640. off_t file_offset;
  641. off_t data_size;
  642. Location(off_t fo, section_size_type ds)
  643. : file_offset(fo), data_size(ds)
  644. { }
  645. };
  646. // Get a View given a Location.
  647. View view(Location loc)
  648. { return View(this->get_view(loc.file_offset, loc.data_size, true, true)); }
  649. // Get a view into the underlying file.
  650. const unsigned char*
  651. get_view(off_t start, section_size_type size, bool aligned, bool cache)
  652. {
  653. return this->input_file()->file().get_view(this->offset_, start, size,
  654. aligned, cache);
  655. }
  656. // Get a lasting view into the underlying file.
  657. File_view*
  658. get_lasting_view(off_t start, section_size_type size, bool aligned,
  659. bool cache)
  660. {
  661. return this->input_file()->file().get_lasting_view(this->offset_, start,
  662. size, aligned, cache);
  663. }
  664. // Read data from the underlying file.
  665. void
  666. read(off_t start, section_size_type size, void* p)
  667. { this->input_file()->file().read(start + this->offset_, size, p); }
  668. // Read multiple data from the underlying file.
  669. void
  670. read_multiple(const File_read::Read_multiple& rm)
  671. { this->input_file()->file().read_multiple(this->offset_, rm); }
  672. // Stop caching views in the underlying file.
  673. void
  674. clear_view_cache_marks()
  675. {
  676. if (this->input_file_ != NULL)
  677. this->input_file_->file().clear_view_cache_marks();
  678. }
  679. // Get the number of global symbols defined by this object, and the
  680. // number of the symbols whose final definition came from this
  681. // object.
  682. void
  683. get_global_symbol_counts(const Symbol_table* symtab, size_t* defined,
  684. size_t* used) const
  685. { this->do_get_global_symbol_counts(symtab, defined, used); }
  686. // Get the symbols defined in this object.
  687. const Symbols*
  688. get_global_symbols() const
  689. { return this->do_get_global_symbols(); }
  690. // Set flag that this object was found in a system directory.
  691. void
  692. set_is_in_system_directory()
  693. { this->is_in_system_directory_ = true; }
  694. // Return whether this object was found in a system directory.
  695. bool
  696. is_in_system_directory() const
  697. { return this->is_in_system_directory_; }
  698. // Set flag that this object was linked with --as-needed.
  699. void
  700. set_as_needed()
  701. { this->as_needed_ = true; }
  702. // Clear flag that this object was linked with --as-needed.
  703. void
  704. clear_as_needed()
  705. { this->as_needed_ = false; }
  706. // Return whether this object was linked with --as-needed.
  707. bool
  708. as_needed() const
  709. { return this->as_needed_; }
  710. // Return whether we found this object by searching a directory.
  711. bool
  712. searched_for() const
  713. { return this->input_file()->will_search_for(); }
  714. bool
  715. no_export() const
  716. { return this->no_export_; }
  717. void
  718. set_no_export(bool value)
  719. { this->no_export_ = value; }
  720. bool
  721. section_is_compressed(unsigned int shndx,
  722. section_size_type* uncompressed_size,
  723. elfcpp::Elf_Xword* palign = NULL) const
  724. {
  725. if (this->compressed_sections_ == NULL)
  726. return false;
  727. Compressed_section_map::const_iterator p =
  728. this->compressed_sections_->find(shndx);
  729. if (p != this->compressed_sections_->end())
  730. {
  731. if (uncompressed_size != NULL)
  732. *uncompressed_size = p->second.size;
  733. if (palign != NULL)
  734. *palign = p->second.addralign;
  735. return true;
  736. }
  737. return false;
  738. }
  739. // Return a view of the decompressed contents of a section. Set *PLEN
  740. // to the size. Set *IS_NEW to true if the contents need to be freed
  741. // by the caller.
  742. const unsigned char*
  743. decompressed_section_contents(unsigned int shndx, section_size_type* plen,
  744. bool* is_cached, uint64_t* palign = NULL);
  745. // Discard any buffers of decompressed sections. This is done
  746. // at the end of the Add_symbols task.
  747. void
  748. discard_decompressed_sections();
  749. // Return the index of the first incremental relocation for symbol SYMNDX.
  750. unsigned int
  751. get_incremental_reloc_base(unsigned int symndx) const
  752. { return this->do_get_incremental_reloc_base(symndx); }
  753. // Return the number of incremental relocations for symbol SYMNDX.
  754. unsigned int
  755. get_incremental_reloc_count(unsigned int symndx) const
  756. { return this->do_get_incremental_reloc_count(symndx); }
  757. // Return the output view for section SHNDX.
  758. unsigned char*
  759. get_output_view(unsigned int shndx, section_size_type* plen) const
  760. { return this->do_get_output_view(shndx, plen); }
  761. protected:
  762. // Returns NULL for Objects that are not dynamic objects. This method
  763. // is overridden in the Dynobj class.
  764. virtual Dynobj*
  765. do_dynobj()
  766. { return NULL; }
  767. // Returns NULL for Objects that are not plugin objects. This method
  768. // is overridden in the Pluginobj class.
  769. virtual Pluginobj*
  770. do_pluginobj()
  771. { return NULL; }
  772. // Return TRUE if this is an incremental (unchanged) input file.
  773. // We return FALSE by default; the incremental object classes
  774. // override this method.
  775. virtual bool
  776. do_is_incremental() const
  777. { return false; }
  778. // Return the last modified time of the file. This method may be
  779. // overridden for subclasses that don't use an actual file (e.g.,
  780. // Incremental objects).
  781. virtual Timespec
  782. do_get_mtime()
  783. { return this->input_file()->file().get_mtime(); }
  784. // Read the symbols--implemented by child class.
  785. virtual void
  786. do_read_symbols(Read_symbols_data*) = 0;
  787. // Lay out sections--implemented by child class.
  788. virtual void
  789. do_layout(Symbol_table*, Layout*, Read_symbols_data*) = 0;
  790. // Add symbol information to the global symbol table--implemented by
  791. // child class.
  792. virtual void
  793. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*) = 0;
  794. virtual Archive::Should_include
  795. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  796. std::string* why) = 0;
  797. // Iterate over global symbols, calling a visitor class V for each.
  798. virtual void
  799. do_for_all_global_symbols(Read_symbols_data* sd,
  800. Library_base::Symbol_visitor_base* v) = 0;
  801. // Iterate over local symbols, calling a visitor class V for each GOT offset
  802. // associated with a local symbol.
  803. virtual void
  804. do_for_all_local_got_entries(Got_offset_list::Visitor* v) const = 0;
  805. // Return the location of the contents of a section. Implemented by
  806. // child class.
  807. virtual const unsigned char*
  808. do_section_contents(unsigned int shndx, section_size_type* plen,
  809. bool cache) = 0;
  810. // Get the size of a section--implemented by child class.
  811. virtual uint64_t
  812. do_section_size(unsigned int shndx) = 0;
  813. // Get the name of a section--implemented by child class.
  814. virtual std::string
  815. do_section_name(unsigned int shndx) const = 0;
  816. // Get section flags--implemented by child class.
  817. virtual uint64_t
  818. do_section_flags(unsigned int shndx) = 0;
  819. // Get section entsize--implemented by child class.
  820. virtual uint64_t
  821. do_section_entsize(unsigned int shndx) = 0;
  822. // Get section address--implemented by child class.
  823. virtual uint64_t
  824. do_section_address(unsigned int shndx) = 0;
  825. // Get section type--implemented by child class.
  826. virtual unsigned int
  827. do_section_type(unsigned int shndx) = 0;
  828. // Get section link field--implemented by child class.
  829. virtual unsigned int
  830. do_section_link(unsigned int shndx) = 0;
  831. // Get section info field--implemented by child class.
  832. virtual unsigned int
  833. do_section_info(unsigned int shndx) = 0;
  834. // Get section alignment--implemented by child class.
  835. virtual uint64_t
  836. do_section_addralign(unsigned int shndx) = 0;
  837. // Return the output section given a section index--implemented
  838. // by child class.
  839. virtual Output_section*
  840. do_output_section(unsigned int) const
  841. { gold_unreachable(); }
  842. // Get the address of a section--implemented by child class.
  843. virtual uint64_t
  844. do_output_section_address(unsigned int)
  845. { gold_unreachable(); }
  846. // Get the offset of a section--implemented by child class.
  847. virtual uint64_t
  848. do_output_section_offset(unsigned int) const
  849. { gold_unreachable(); }
  850. // Return the Xindex structure to use.
  851. virtual Xindex*
  852. do_initialize_xindex() = 0;
  853. // Implement get_global_symbol_counts--implemented by child class.
  854. virtual void
  855. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const = 0;
  856. virtual const Symbols*
  857. do_get_global_symbols() const = 0;
  858. // Set the number of sections.
  859. void
  860. set_shnum(int shnum)
  861. { this->shnum_ = shnum; }
  862. // Functions used by both Sized_relobj_file and Sized_dynobj.
  863. // Read the section data into a Read_symbols_data object.
  864. template<int size, bool big_endian>
  865. void
  866. read_section_data(elfcpp::Elf_file<size, big_endian, Object>*,
  867. Read_symbols_data*);
  868. // Find the section header with the given NAME. If HDR is non-NULL
  869. // then it is a section header returned from a previous call to this
  870. // function and the next section header with the same name will be
  871. // returned.
  872. template<int size, bool big_endian>
  873. const unsigned char*
  874. find_shdr(const unsigned char* pshdrs, const char* name,
  875. const char* names, section_size_type names_size,
  876. const unsigned char* hdr) const;
  877. // Let the child class initialize the xindex object directly.
  878. void
  879. set_xindex(Xindex* xindex)
  880. {
  881. gold_assert(this->xindex_ == NULL);
  882. this->xindex_ = xindex;
  883. }
  884. // If NAME is the name of a special .gnu.warning section, arrange
  885. // for the warning to be issued. SHNDX is the section index.
  886. // Return whether it is a warning section.
  887. bool
  888. handle_gnu_warning_section(const char* name, unsigned int shndx,
  889. Symbol_table*);
  890. // If NAME is the name of the special section which indicates that
  891. // this object was compiled with -fsplit-stack, mark it accordingly,
  892. // and return true. Otherwise return false.
  893. bool
  894. handle_split_stack_section(const char* name);
  895. // Discard any buffers of decompressed sections. This is done
  896. // at the end of the Add_symbols task.
  897. virtual void
  898. do_discard_decompressed_sections()
  899. { }
  900. // Return the index of the first incremental relocation for symbol SYMNDX--
  901. // implemented by child class.
  902. virtual unsigned int
  903. do_get_incremental_reloc_base(unsigned int) const
  904. { gold_unreachable(); }
  905. // Return the number of incremental relocations for symbol SYMNDX--
  906. // implemented by child class.
  907. virtual unsigned int
  908. do_get_incremental_reloc_count(unsigned int) const
  909. { gold_unreachable(); }
  910. // Return the output view for a section.
  911. virtual unsigned char*
  912. do_get_output_view(unsigned int, section_size_type*) const
  913. { gold_unreachable(); }
  914. void
  915. set_compressed_sections(Compressed_section_map* compressed_sections)
  916. { this->compressed_sections_ = compressed_sections; }
  917. Compressed_section_map*
  918. compressed_sections()
  919. { return this->compressed_sections_; }
  920. private:
  921. // This class may not be copied.
  922. Object(const Object&);
  923. Object& operator=(const Object&);
  924. // Name of object as printed to user.
  925. std::string name_;
  926. // For reading the file.
  927. Input_file* input_file_;
  928. // Offset within the file--0 for an object file, non-0 for an
  929. // archive.
  930. off_t offset_;
  931. // Number of input sections.
  932. unsigned int shnum_;
  933. // Whether this is a dynamic object.
  934. bool is_dynamic_ : 1;
  935. // Whether this object is needed. This is only set for dynamic
  936. // objects, and means that the object defined a symbol which was
  937. // used by a reference from a regular object.
  938. bool is_needed_ : 1;
  939. // Whether this object was compiled with -fsplit-stack.
  940. bool uses_split_stack_ : 1;
  941. // Whether this object contains any functions compiled with the
  942. // no_split_stack attribute.
  943. bool has_no_split_stack_ : 1;
  944. // True if exclude this object from automatic symbol export.
  945. // This is used only for archive objects.
  946. bool no_export_ : 1;
  947. // True if the object was found in a system directory.
  948. bool is_in_system_directory_ : 1;
  949. // True if the object was linked with --as-needed.
  950. bool as_needed_ : 1;
  951. // Many sections for objects with more than SHN_LORESERVE sections.
  952. Xindex* xindex_;
  953. // For compressed debug sections, map section index to uncompressed size
  954. // and contents.
  955. Compressed_section_map* compressed_sections_;
  956. };
  957. // A regular object (ET_REL). This is an abstract base class itself.
  958. // The implementation is the template class Sized_relobj_file.
  959. class Relobj : public Object
  960. {
  961. public:
  962. Relobj(const std::string& name, Input_file* input_file, off_t offset = 0)
  963. : Object(name, input_file, false, offset),
  964. output_sections_(),
  965. map_to_relocatable_relocs_(NULL),
  966. object_merge_map_(NULL),
  967. relocs_must_follow_section_writes_(false),
  968. sd_(NULL),
  969. reloc_counts_(NULL),
  970. reloc_bases_(NULL),
  971. first_dyn_reloc_(0),
  972. dyn_reloc_count_(0)
  973. { }
  974. // During garbage collection, the Read_symbols_data pass for
  975. // each object is stored as layout needs to be done after
  976. // reloc processing.
  977. Symbols_data*
  978. get_symbols_data()
  979. { return this->sd_; }
  980. // Decides which section names have to be included in the worklist
  981. // as roots.
  982. bool
  983. is_section_name_included(const char* name);
  984. void
  985. copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd,
  986. unsigned int section_header_size);
  987. void
  988. set_symbols_data(Symbols_data* sd)
  989. { this->sd_ = sd; }
  990. // During garbage collection, the Read_relocs pass for all objects
  991. // is done before scanning the relocs. In that case, this->rd_ is
  992. // used to store the information from Read_relocs for each object.
  993. // This data is also used to compute the list of relevant sections.
  994. Read_relocs_data*
  995. get_relocs_data()
  996. { return this->rd_; }
  997. void
  998. set_relocs_data(Read_relocs_data* rd)
  999. { this->rd_ = rd; }
  1000. virtual bool
  1001. is_output_section_offset_invalid(unsigned int shndx) const = 0;
  1002. // Read the relocs.
  1003. void
  1004. read_relocs(Read_relocs_data* rd)
  1005. { return this->do_read_relocs(rd); }
  1006. // Process the relocs, during garbage collection only.
  1007. void
  1008. gc_process_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
  1009. { return this->do_gc_process_relocs(symtab, layout, rd); }
  1010. // Scan the relocs and adjust the symbol table.
  1011. void
  1012. scan_relocs(Symbol_table* symtab, Layout* layout, Read_relocs_data* rd)
  1013. { return this->do_scan_relocs(symtab, layout, rd); }
  1014. // Return the value of the local symbol whose index is SYMNDX, plus
  1015. // ADDEND. ADDEND is passed in so that we can correctly handle the
  1016. // section symbol for a merge section.
  1017. uint64_t
  1018. local_symbol_value(unsigned int symndx, uint64_t addend) const
  1019. { return this->do_local_symbol_value(symndx, addend); }
  1020. // Return the PLT offset for a local symbol. It is an error to call
  1021. // this if it doesn't have one.
  1022. unsigned int
  1023. local_plt_offset(unsigned int symndx) const
  1024. { return this->do_local_plt_offset(symndx); }
  1025. // Return whether there is a GOT entry of type GOT_TYPE for the
  1026. // local symbol SYMNDX with given ADDEND.
  1027. bool
  1028. local_has_got_offset(unsigned int symndx, unsigned int got_type,
  1029. uint64_t addend = 0) const
  1030. { return this->do_local_has_got_offset(symndx, got_type, addend); }
  1031. // Return the GOT offset of the GOT entry with type GOT_TYPE for the
  1032. // local symbol SYMNDX with given ADDEND. It is an error to call
  1033. // this function if the symbol does not have such a GOT entry.
  1034. unsigned int
  1035. local_got_offset(unsigned int symndx, unsigned int got_type,
  1036. uint64_t addend = 0) const
  1037. { return this->do_local_got_offset(symndx, got_type, addend); }
  1038. // Set the GOT offset for a GOT entry with type GOT_TYPE for the
  1039. // local symbol SYMNDX with ADDEND to GOT_OFFSET. Create such an
  1040. // entry if none exists.
  1041. void
  1042. set_local_got_offset(unsigned int symndx, unsigned int got_type,
  1043. unsigned int got_offset, uint64_t addend = 0)
  1044. { this->do_set_local_got_offset(symndx, got_type, got_offset, addend); }
  1045. // Return whether the local symbol SYMNDX is a TLS symbol.
  1046. bool
  1047. local_is_tls(unsigned int symndx) const
  1048. { return this->do_local_is_tls(symndx); }
  1049. // The number of local symbols in the input symbol table.
  1050. virtual unsigned int
  1051. local_symbol_count() const
  1052. { return this->do_local_symbol_count(); }
  1053. // The number of local symbols in the output symbol table.
  1054. virtual unsigned int
  1055. output_local_symbol_count() const
  1056. { return this->do_output_local_symbol_count(); }
  1057. // The file offset for local symbols in the output symbol table.
  1058. virtual off_t
  1059. local_symbol_offset() const
  1060. { return this->do_local_symbol_offset(); }
  1061. // Initial local symbol processing: count the number of local symbols
  1062. // in the output symbol table and dynamic symbol table; add local symbol
  1063. // names to *POOL and *DYNPOOL.
  1064. void
  1065. count_local_symbols(Stringpool_template<char>* pool,
  1066. Stringpool_template<char>* dynpool)
  1067. { return this->do_count_local_symbols(pool, dynpool); }
  1068. // Set the values of the local symbols, set the output symbol table
  1069. // indexes for the local variables, and set the offset where local
  1070. // symbol information will be stored. Returns the new local symbol index.
  1071. unsigned int
  1072. finalize_local_symbols(unsigned int index, off_t off, Symbol_table* symtab)
  1073. { return this->do_finalize_local_symbols(index, off, symtab); }
  1074. // Set the output dynamic symbol table indexes for the local variables.
  1075. unsigned int
  1076. set_local_dynsym_indexes(unsigned int index)
  1077. { return this->do_set_local_dynsym_indexes(index); }
  1078. // Set the offset where local dynamic symbol information will be stored.
  1079. unsigned int
  1080. set_local_dynsym_offset(off_t off)
  1081. { return this->do_set_local_dynsym_offset(off); }
  1082. // Record a dynamic relocation against an input section from this object.
  1083. void
  1084. add_dyn_reloc(unsigned int index)
  1085. {
  1086. if (this->dyn_reloc_count_ == 0)
  1087. this->first_dyn_reloc_ = index;
  1088. ++this->dyn_reloc_count_;
  1089. }
  1090. // Return the index of the first dynamic relocation.
  1091. unsigned int
  1092. first_dyn_reloc() const
  1093. { return this->first_dyn_reloc_; }
  1094. // Return the count of dynamic relocations.
  1095. unsigned int
  1096. dyn_reloc_count() const
  1097. { return this->dyn_reloc_count_; }
  1098. // Relocate the input sections and write out the local symbols.
  1099. void
  1100. relocate(const Symbol_table* symtab, const Layout* layout, Output_file* of)
  1101. { return this->do_relocate(symtab, layout, of); }
  1102. // Return whether an input section is being included in the link.
  1103. bool
  1104. is_section_included(unsigned int shndx) const
  1105. {
  1106. gold_assert(shndx < this->output_sections_.size());
  1107. return this->output_sections_[shndx] != NULL;
  1108. }
  1109. // The output section of the input section with index SHNDX.
  1110. // This is only used currently to remove a section from the link in
  1111. // relaxation.
  1112. void
  1113. set_output_section(unsigned int shndx, Output_section* os)
  1114. {
  1115. gold_assert(shndx < this->output_sections_.size());
  1116. this->output_sections_[shndx] = os;
  1117. }
  1118. // Set the offset of an input section within its output section.
  1119. void
  1120. set_section_offset(unsigned int shndx, uint64_t off)
  1121. { this->do_set_section_offset(shndx, off); }
  1122. // Return true if we need to wait for output sections to be written
  1123. // before we can apply relocations. This is true if the object has
  1124. // any relocations for sections which require special handling, such
  1125. // as the exception frame section.
  1126. bool
  1127. relocs_must_follow_section_writes() const
  1128. { return this->relocs_must_follow_section_writes_; }
  1129. Object_merge_map*
  1130. get_or_create_merge_map();
  1131. template<int size>
  1132. void
  1133. initialize_input_to_output_map(unsigned int shndx,
  1134. typename elfcpp::Elf_types<size>::Elf_Addr starting_address,
  1135. Unordered_map<section_offset_type,
  1136. typename elfcpp::Elf_types<size>::Elf_Addr>* output_address) const;
  1137. void
  1138. add_merge_mapping(Output_section_data *output_data,
  1139. unsigned int shndx, section_offset_type offset,
  1140. section_size_type length,
  1141. section_offset_type output_offset);
  1142. bool
  1143. merge_output_offset(unsigned int shndx, section_offset_type offset,
  1144. section_offset_type *poutput) const;
  1145. const Output_section_data*
  1146. find_merge_section(unsigned int shndx) const;
  1147. // Record the relocatable reloc info for an input reloc section.
  1148. void
  1149. set_relocatable_relocs(unsigned int reloc_shndx, Relocatable_relocs* rr)
  1150. {
  1151. gold_assert(reloc_shndx < this->shnum());
  1152. (*this->map_to_relocatable_relocs_)[reloc_shndx] = rr;
  1153. }
  1154. // Get the relocatable reloc info for an input reloc section.
  1155. Relocatable_relocs*
  1156. relocatable_relocs(unsigned int reloc_shndx)
  1157. {
  1158. gold_assert(reloc_shndx < this->shnum());
  1159. return (*this->map_to_relocatable_relocs_)[reloc_shndx];
  1160. }
  1161. // Layout sections whose layout was deferred while waiting for
  1162. // input files from a plugin.
  1163. void
  1164. layout_deferred_sections(Layout* layout)
  1165. { this->do_layout_deferred_sections(layout); }
  1166. // Return the index of the first incremental relocation for symbol SYMNDX.
  1167. virtual unsigned int
  1168. do_get_incremental_reloc_base(unsigned int symndx) const
  1169. { return this->reloc_bases_[symndx]; }
  1170. // Return the number of incremental relocations for symbol SYMNDX.
  1171. virtual unsigned int
  1172. do_get_incremental_reloc_count(unsigned int symndx) const
  1173. { return this->reloc_counts_[symndx]; }
  1174. // Return the word size of the object file.
  1175. int
  1176. elfsize() const
  1177. { return this->do_elfsize(); }
  1178. // Return TRUE if this is a big-endian object file.
  1179. bool
  1180. is_big_endian() const
  1181. { return this->do_is_big_endian(); }
  1182. protected:
  1183. // The output section to be used for each input section, indexed by
  1184. // the input section number. The output section is NULL if the
  1185. // input section is to be discarded.
  1186. typedef std::vector<Output_section*> Output_sections;
  1187. // Read the relocs--implemented by child class.
  1188. virtual void
  1189. do_read_relocs(Read_relocs_data*) = 0;
  1190. // Process the relocs--implemented by child class.
  1191. virtual void
  1192. do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
  1193. // Scan the relocs--implemented by child class.
  1194. virtual void
  1195. do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*) = 0;
  1196. // Return the value of a local symbol.
  1197. virtual uint64_t
  1198. do_local_symbol_value(unsigned int symndx, uint64_t addend) const = 0;
  1199. // Return the PLT offset of a local symbol.
  1200. virtual unsigned int
  1201. do_local_plt_offset(unsigned int symndx) const = 0;
  1202. // Return whether a local symbol plus addend has a GOT offset
  1203. // of a given type.
  1204. virtual bool
  1205. do_local_has_got_offset(unsigned int symndx,
  1206. unsigned int got_type, uint64_t addend) const = 0;
  1207. // Return the GOT offset of a given type of a local symbol plus addend.
  1208. virtual unsigned int
  1209. do_local_got_offset(unsigned int symndx, unsigned int got_type,
  1210. uint64_t addend) const = 0;
  1211. // Set the GOT offset with a given type for a local symbol plus addend.
  1212. virtual void
  1213. do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
  1214. unsigned int got_offset, uint64_t addend) = 0;
  1215. // Return whether local symbol SYMNDX is a TLS symbol.
  1216. virtual bool
  1217. do_local_is_tls(unsigned int symndx) const = 0;
  1218. // Return the number of local symbols--implemented by child class.
  1219. virtual unsigned int
  1220. do_local_symbol_count() const = 0;
  1221. // Return the number of output local symbols--implemented by child class.
  1222. virtual unsigned int
  1223. do_output_local_symbol_count() const = 0;
  1224. // Return the file offset for local symbols--implemented by child class.
  1225. virtual off_t
  1226. do_local_symbol_offset() const = 0;
  1227. // Count local symbols--implemented by child class.
  1228. virtual void
  1229. do_count_local_symbols(Stringpool_template<char>*,
  1230. Stringpool_template<char>*) = 0;
  1231. // Finalize the local symbols. Set the output symbol table indexes
  1232. // for the local variables, and set the offset where local symbol
  1233. // information will be stored.
  1234. virtual unsigned int
  1235. do_finalize_local_symbols(unsigned int, off_t, Symbol_table*) = 0;
  1236. // Set the output dynamic symbol table indexes for the local variables.
  1237. virtual unsigned int
  1238. do_set_local_dynsym_indexes(unsigned int) = 0;
  1239. // Set the offset where local dynamic symbol information will be stored.
  1240. virtual unsigned int
  1241. do_set_local_dynsym_offset(off_t) = 0;
  1242. // Relocate the input sections and write out the local
  1243. // symbols--implemented by child class.
  1244. virtual void
  1245. do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of) = 0;
  1246. // Set the offset of a section--implemented by child class.
  1247. virtual void
  1248. do_set_section_offset(unsigned int shndx, uint64_t off) = 0;
  1249. // Layout sections whose layout was deferred while waiting for
  1250. // input files from a plugin--implemented by child class.
  1251. virtual void
  1252. do_layout_deferred_sections(Layout*) = 0;
  1253. // Given a section index, return the corresponding Output_section.
  1254. // The return value will be NULL if the section is not included in
  1255. // the link.
  1256. Output_section*
  1257. do_output_section(unsigned int shndx) const
  1258. {
  1259. gold_assert(shndx < this->output_sections_.size());
  1260. return this->output_sections_[shndx];
  1261. }
  1262. // Return the vector mapping input sections to output sections.
  1263. Output_sections&
  1264. output_sections()
  1265. { return this->output_sections_; }
  1266. const Output_sections&
  1267. output_sections() const
  1268. { return this->output_sections_; }
  1269. // Set the size of the relocatable relocs array.
  1270. void
  1271. size_relocatable_relocs()
  1272. {
  1273. this->map_to_relocatable_relocs_ =
  1274. new std::vector<Relocatable_relocs*>(this->shnum());
  1275. }
  1276. // Record that we must wait for the output sections to be written
  1277. // before applying relocations.
  1278. void
  1279. set_relocs_must_follow_section_writes()
  1280. { this->relocs_must_follow_section_writes_ = true; }
  1281. // Allocate the array for counting incremental relocations.
  1282. void
  1283. allocate_incremental_reloc_counts()
  1284. {
  1285. unsigned int nsyms = this->do_get_global_symbols()->size();
  1286. this->reloc_counts_ = new unsigned int[nsyms];
  1287. gold_assert(this->reloc_counts_ != NULL);
  1288. memset(this->reloc_counts_, 0, nsyms * sizeof(unsigned int));
  1289. }
  1290. // Record a relocation in this object referencing global symbol SYMNDX.
  1291. // Used for tracking incremental link information.
  1292. void
  1293. count_incremental_reloc(unsigned int symndx)
  1294. {
  1295. unsigned int nsyms = this->do_get_global_symbols()->size();
  1296. gold_assert(symndx < nsyms);
  1297. gold_assert(this->reloc_counts_ != NULL);
  1298. ++this->reloc_counts_[symndx];
  1299. }
  1300. // Finalize the incremental relocation information.
  1301. void
  1302. finalize_incremental_relocs(Layout* layout, bool clear_counts);
  1303. // Return the index of the next relocation to be written for global symbol
  1304. // SYMNDX. Only valid after finalize_incremental_relocs() has been called.
  1305. unsigned int
  1306. next_incremental_reloc_index(unsigned int symndx)
  1307. {
  1308. unsigned int nsyms = this->do_get_global_symbols()->size();
  1309. gold_assert(this->reloc_counts_ != NULL);
  1310. gold_assert(this->reloc_bases_ != NULL);
  1311. gold_assert(symndx < nsyms);
  1312. unsigned int counter = this->reloc_counts_[symndx]++;
  1313. return this->reloc_bases_[symndx] + counter;
  1314. }
  1315. // Return the word size of the object file--
  1316. // implemented by child class.
  1317. virtual int
  1318. do_elfsize() const = 0;
  1319. // Return TRUE if this is a big-endian object file--
  1320. // implemented by child class.
  1321. virtual bool
  1322. do_is_big_endian() const = 0;
  1323. private:
  1324. // Mapping from input sections to output section.
  1325. Output_sections output_sections_;
  1326. // Mapping from input section index to the information recorded for
  1327. // the relocations. This is only used for a relocatable link.
  1328. std::vector<Relocatable_relocs*>* map_to_relocatable_relocs_;
  1329. // Mappings for merge sections. This is managed by the code in the
  1330. // Merge_map class.
  1331. Object_merge_map* object_merge_map_;
  1332. // Whether we need to wait for output sections to be written before
  1333. // we can apply relocations.
  1334. bool relocs_must_follow_section_writes_;
  1335. // Used to store the relocs data computed by the Read_relocs pass.
  1336. // Used during garbage collection of unused sections.
  1337. Read_relocs_data* rd_;
  1338. // Used to store the symbols data computed by the Read_symbols pass.
  1339. // Again used during garbage collection when laying out referenced
  1340. // sections.
  1341. gold::Symbols_data* sd_;
  1342. // Per-symbol counts of relocations, for incremental links.
  1343. unsigned int* reloc_counts_;
  1344. // Per-symbol base indexes of relocations, for incremental links.
  1345. unsigned int* reloc_bases_;
  1346. // Index of the first dynamic relocation for this object.
  1347. unsigned int first_dyn_reloc_;
  1348. // Count of dynamic relocations for this object.
  1349. unsigned int dyn_reloc_count_;
  1350. };
  1351. // This class is used to handle relocations against a section symbol
  1352. // in an SHF_MERGE section. For such a symbol, we need to know the
  1353. // addend of the relocation before we can determine the final value.
  1354. // The addend gives us the location in the input section, and we can
  1355. // determine how it is mapped to the output section. For a
  1356. // non-section symbol, we apply the addend to the final value of the
  1357. // symbol; that is done in finalize_local_symbols, and does not use
  1358. // this class.
  1359. template<int size>
  1360. class Merged_symbol_value
  1361. {
  1362. public:
  1363. typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
  1364. // We use a hash table to map offsets in the input section to output
  1365. // addresses.
  1366. typedef Unordered_map<section_offset_type, Value> Output_addresses;
  1367. Merged_symbol_value(Value input_value, Value output_start_address)
  1368. : input_value_(input_value), output_start_address_(output_start_address),
  1369. output_addresses_()
  1370. { }
  1371. // Initialize the hash table.
  1372. void
  1373. initialize_input_to_output_map(const Relobj*, unsigned int input_shndx);
  1374. // Release the hash table to save space.
  1375. void
  1376. free_input_to_output_map()
  1377. { this->output_addresses_.clear(); }
  1378. // Get the output value corresponding to an addend. The object and
  1379. // input section index are passed in because the caller will have
  1380. // them; otherwise we could store them here.
  1381. Value
  1382. value(const Relobj* object, unsigned int input_shndx, Value addend) const
  1383. {
  1384. // This is a relocation against a section symbol. ADDEND is the
  1385. // offset in the section. The result should be the start of some
  1386. // merge area. If the object file wants something else, it should
  1387. // use a regular symbol rather than a section symbol.
  1388. // Unfortunately, PR 6658 shows a case in which the object file
  1389. // refers to the section symbol, but uses a negative ADDEND to
  1390. // compensate for a PC relative reloc. We can't handle the
  1391. // general case. However, we can handle the special case of a
  1392. // negative addend, by assuming that it refers to the start of the
  1393. // section. Of course, that means that we have to guess when
  1394. // ADDEND is negative. It is normal to see a 32-bit value here
  1395. // even when the template parameter size is 64, as 64-bit object
  1396. // file formats have 32-bit relocations. We know this is a merge
  1397. // section, so we know it has to fit into memory. So we assume
  1398. // that we won't see a value larger than a large 32-bit unsigned
  1399. // value. This will break objects with very very large merge
  1400. // sections; they probably break in other ways anyhow.
  1401. Value input_offset = this->input_value_;
  1402. if (addend < 0xffffff00)
  1403. {
  1404. input_offset += addend;
  1405. addend = 0;
  1406. }
  1407. typename Output_addresses::const_iterator p =
  1408. this->output_addresses_.find(input_offset);
  1409. if (p != this->output_addresses_.end())
  1410. return p->second + addend;
  1411. return (this->value_from_output_section(object, input_shndx, input_offset)
  1412. + addend);
  1413. }
  1414. private:
  1415. // Get the output value for an input offset if we couldn't find it
  1416. // in the hash table.
  1417. Value
  1418. value_from_output_section(const Relobj*, unsigned int input_shndx,
  1419. Value input_offset) const;
  1420. // The value of the section symbol in the input file. This is
  1421. // normally zero, but could in principle be something else.
  1422. Value input_value_;
  1423. // The start address of this merged section in the output file.
  1424. Value output_start_address_;
  1425. // A hash table which maps offsets in the input section to output
  1426. // addresses. This only maps specific offsets, not all offsets.
  1427. Output_addresses output_addresses_;
  1428. };
  1429. // This POD class is holds the value of a symbol. This is used for
  1430. // local symbols, and for all symbols during relocation processing.
  1431. // For special sections, such as SHF_MERGE sections, this calls a
  1432. // function to get the final symbol value.
  1433. template<int size>
  1434. class Symbol_value
  1435. {
  1436. public:
  1437. typedef typename elfcpp::Elf_types<size>::Elf_Addr Value;
  1438. Symbol_value()
  1439. : output_symtab_index_(0), output_dynsym_index_(-1U), input_shndx_(0),
  1440. is_ordinary_shndx_(false), is_section_symbol_(false),
  1441. is_tls_symbol_(false), is_ifunc_symbol_(false), has_output_value_(true)
  1442. { this->u_.value = 0; }
  1443. ~Symbol_value()
  1444. {
  1445. if (!this->has_output_value_)
  1446. delete this->u_.merged_symbol_value;
  1447. }
  1448. // Get the value of this symbol. OBJECT is the object in which this
  1449. // symbol is defined, and ADDEND is an addend to add to the value.
  1450. template<bool big_endian>
  1451. Value
  1452. value(const Sized_relobj_file<size, big_endian>* object, Value addend) const
  1453. {
  1454. if (this->has_output_value_)
  1455. return this->u_.value + addend;
  1456. else
  1457. {
  1458. gold_assert(this->is_ordinary_shndx_);
  1459. return this->u_.merged_symbol_value->value(object, this->input_shndx_,
  1460. addend);
  1461. }
  1462. }
  1463. // Set the value of this symbol in the output symbol table.
  1464. void
  1465. set_output_value(Value value)
  1466. { this->u_.value = value; }
  1467. // For a section symbol in a merged section, we need more
  1468. // information.
  1469. void
  1470. set_merged_symbol_value(Merged_symbol_value<size>* msv)
  1471. {
  1472. gold_assert(this->is_section_symbol_);
  1473. this->has_output_value_ = false;
  1474. this->u_.merged_symbol_value = msv;
  1475. }
  1476. // Initialize the input to output map for a section symbol in a
  1477. // merged section. We also initialize the value of a non-section
  1478. // symbol in a merged section.
  1479. void
  1480. initialize_input_to_output_map(const Relobj* object)
  1481. {
  1482. if (!this->has_output_value_)
  1483. {
  1484. gold_assert(this->is_section_symbol_ && this->is_ordinary_shndx_);
  1485. Merged_symbol_value<size>* msv = this->u_.merged_symbol_value;
  1486. msv->initialize_input_to_output_map(object, this->input_shndx_);
  1487. }
  1488. }
  1489. // Free the input to output map for a section symbol in a merged
  1490. // section.
  1491. void
  1492. free_input_to_output_map()
  1493. {
  1494. if (!this->has_output_value_)
  1495. this->u_.merged_symbol_value->free_input_to_output_map();
  1496. }
  1497. // Set the value of the symbol from the input file. This is only
  1498. // called by count_local_symbols, to communicate the value to
  1499. // finalize_local_symbols.
  1500. void
  1501. set_input_value(Value value)
  1502. { this->u_.value = value; }
  1503. // Return the input value. This is only called by
  1504. // finalize_local_symbols and (in special cases) relocate_section.
  1505. Value
  1506. input_value() const
  1507. { return this->u_.value; }
  1508. // Return whether we have set the index in the output symbol table
  1509. // yet.
  1510. bool
  1511. is_output_symtab_index_set() const
  1512. {
  1513. return (this->output_symtab_index_ != 0
  1514. && this->output_symtab_index_ != -2U);
  1515. }
  1516. // Return whether this symbol may be discarded from the normal
  1517. // symbol table.
  1518. bool
  1519. may_be_discarded_from_output_symtab() const
  1520. {
  1521. gold_assert(!this->is_output_symtab_index_set());
  1522. return this->output_symtab_index_ != -2U;
  1523. }
  1524. // Return whether this symbol has an entry in the output symbol
  1525. // table.
  1526. bool
  1527. has_output_symtab_entry() const
  1528. {
  1529. gold_assert(this->is_output_symtab_index_set());
  1530. return this->output_symtab_index_ != -1U;
  1531. }
  1532. // Return the index in the output symbol table.
  1533. unsigned int
  1534. output_symtab_index() const
  1535. {
  1536. gold_assert(this->is_output_symtab_index_set()
  1537. && this->output_symtab_index_ != -1U);
  1538. return this->output_symtab_index_;
  1539. }
  1540. // Set the index in the output symbol table.
  1541. void
  1542. set_output_symtab_index(unsigned int i)
  1543. {
  1544. gold_assert(!this->is_output_symtab_index_set());
  1545. gold_assert(i != 0 && i != -1U && i != -2U);
  1546. this->output_symtab_index_ = i;
  1547. }
  1548. // Record that this symbol should not go into the output symbol
  1549. // table.
  1550. void
  1551. set_no_output_symtab_entry()
  1552. {
  1553. gold_assert(this->output_symtab_index_ == 0);
  1554. this->output_symtab_index_ = -1U;
  1555. }
  1556. // Record that this symbol must go into the output symbol table,
  1557. // because it there is a relocation that uses it.
  1558. void
  1559. set_must_have_output_symtab_entry()
  1560. {
  1561. gold_assert(!this->is_output_symtab_index_set());
  1562. this->output_symtab_index_ = -2U;
  1563. }
  1564. // Set the index in the output dynamic symbol table.
  1565. void
  1566. set_needs_output_dynsym_entry()
  1567. {
  1568. gold_assert(!this->is_section_symbol());
  1569. this->output_dynsym_index_ = 0;
  1570. }
  1571. // Return whether this symbol should go into the dynamic symbol
  1572. // table.
  1573. bool
  1574. needs_output_dynsym_entry() const
  1575. {
  1576. return this->output_dynsym_index_ != -1U;
  1577. }
  1578. // Return whether this symbol has an entry in the dynamic symbol
  1579. // table.
  1580. bool
  1581. has_output_dynsym_entry() const
  1582. {
  1583. gold_assert(this->output_dynsym_index_ != 0);
  1584. return this->output_dynsym_index_ != -1U;
  1585. }
  1586. // Record that this symbol should go into the dynamic symbol table.
  1587. void
  1588. set_output_dynsym_index(unsigned int i)
  1589. {
  1590. gold_assert(this->output_dynsym_index_ == 0);
  1591. gold_assert(i != 0 && i != -1U);
  1592. this->output_dynsym_index_ = i;
  1593. }
  1594. // Return the index in the output dynamic symbol table.
  1595. unsigned int
  1596. output_dynsym_index() const
  1597. {
  1598. gold_assert(this->output_dynsym_index_ != 0
  1599. && this->output_dynsym_index_ != -1U);
  1600. return this->output_dynsym_index_;
  1601. }
  1602. // Set the index of the input section in the input file.
  1603. void
  1604. set_input_shndx(unsigned int i, bool is_ordinary)
  1605. {
  1606. this->input_shndx_ = i;
  1607. // input_shndx_ field is a bitfield, so make sure that the value
  1608. // fits.
  1609. gold_assert(this->input_shndx_ == i);
  1610. this->is_ordinary_shndx_ = is_ordinary;
  1611. }
  1612. // Return the index of the input section in the input file.
  1613. unsigned int
  1614. input_shndx(bool* is_ordinary) const
  1615. {
  1616. *is_ordinary = this->is_ordinary_shndx_;
  1617. return this->input_shndx_;
  1618. }
  1619. // Whether this is a section symbol.
  1620. bool
  1621. is_section_symbol() const
  1622. { return this->is_section_symbol_; }
  1623. // Record that this is a section symbol.
  1624. void
  1625. set_is_section_symbol()
  1626. {
  1627. gold_assert(!this->needs_output_dynsym_entry());
  1628. this->is_section_symbol_ = true;
  1629. }
  1630. // Record that this is a TLS symbol.
  1631. void
  1632. set_is_tls_symbol()
  1633. { this->is_tls_symbol_ = true; }
  1634. // Return true if this is a TLS symbol.
  1635. bool
  1636. is_tls_symbol() const
  1637. { return this->is_tls_symbol_; }
  1638. // Record that this is an IFUNC symbol.
  1639. void
  1640. set_is_ifunc_symbol()
  1641. { this->is_ifunc_symbol_ = true; }
  1642. // Return true if this is an IFUNC symbol.
  1643. bool
  1644. is_ifunc_symbol() const
  1645. { return this->is_ifunc_symbol_; }
  1646. // Return true if this has output value.
  1647. bool
  1648. has_output_value() const
  1649. { return this->has_output_value_; }
  1650. private:
  1651. // The index of this local symbol in the output symbol table. This
  1652. // will be 0 if no value has been assigned yet, and the symbol may
  1653. // be omitted. This will be -1U if the symbol should not go into
  1654. // the symbol table. This will be -2U if the symbol must go into
  1655. // the symbol table, but no index has been assigned yet.
  1656. unsigned int output_symtab_index_;
  1657. // The index of this local symbol in the dynamic symbol table. This
  1658. // will be -1U if the symbol should not go into the symbol table.
  1659. unsigned int output_dynsym_index_;
  1660. // The section index in the input file in which this symbol is
  1661. // defined.
  1662. unsigned int input_shndx_ : 27;
  1663. // Whether the section index is an ordinary index, not a special
  1664. // value.
  1665. bool is_ordinary_shndx_ : 1;
  1666. // Whether this is a STT_SECTION symbol.
  1667. bool is_section_symbol_ : 1;
  1668. // Whether this is a STT_TLS symbol.
  1669. bool is_tls_symbol_ : 1;
  1670. // Whether this is a STT_GNU_IFUNC symbol.
  1671. bool is_ifunc_symbol_ : 1;
  1672. // Whether this symbol has a value for the output file. This is
  1673. // normally set to true during Layout::finalize, by
  1674. // finalize_local_symbols. It will be false for a section symbol in
  1675. // a merge section, as for such symbols we can not determine the
  1676. // value to use in a relocation until we see the addend.
  1677. bool has_output_value_ : 1;
  1678. union
  1679. {
  1680. // This is used if has_output_value_ is true. Between
  1681. // count_local_symbols and finalize_local_symbols, this is the
  1682. // value in the input file. After finalize_local_symbols, it is
  1683. // the value in the output file.
  1684. Value value;
  1685. // This is used if has_output_value_ is false. It points to the
  1686. // information we need to get the value for a merge section.
  1687. Merged_symbol_value<size>* merged_symbol_value;
  1688. } u_;
  1689. };
  1690. // This type is used to modify relocations for -fsplit-stack. It is
  1691. // indexed by relocation index, and means that the relocation at that
  1692. // index should use the symbol from the vector, rather than the one
  1693. // indicated by the relocation.
  1694. class Reloc_symbol_changes
  1695. {
  1696. public:
  1697. Reloc_symbol_changes(size_t count)
  1698. : vec_(count, NULL)
  1699. { }
  1700. void
  1701. set(size_t i, Symbol* sym)
  1702. { this->vec_[i] = sym; }
  1703. const Symbol*
  1704. operator[](size_t i) const
  1705. { return this->vec_[i]; }
  1706. private:
  1707. std::vector<Symbol*> vec_;
  1708. };
  1709. // Abstract base class for a regular object file, either a real object file
  1710. // or an incremental (unchanged) object. This is size and endian specific.
  1711. template<int size, bool big_endian>
  1712. class Sized_relobj : public Relobj
  1713. {
  1714. public:
  1715. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1716. typedef Relobj::Symbols Symbols;
  1717. static const Address invalid_address = static_cast<Address>(0) - 1;
  1718. Sized_relobj(const std::string& name, Input_file* input_file)
  1719. : Relobj(name, input_file), local_got_offsets_(), section_offsets_()
  1720. { }
  1721. Sized_relobj(const std::string& name, Input_file* input_file,
  1722. off_t offset)
  1723. : Relobj(name, input_file, offset), local_got_offsets_(), section_offsets_()
  1724. { }
  1725. ~Sized_relobj()
  1726. { }
  1727. // If this is a regular object, return a pointer to the Sized_relobj_file
  1728. // object. Otherwise, return NULL.
  1729. virtual Sized_relobj_file<size, big_endian>*
  1730. sized_relobj()
  1731. { return NULL; }
  1732. const virtual Sized_relobj_file<size, big_endian>*
  1733. sized_relobj() const
  1734. { return NULL; }
  1735. // Checks if the offset of input section SHNDX within its output
  1736. // section is invalid.
  1737. bool
  1738. is_output_section_offset_invalid(unsigned int shndx) const
  1739. { return this->get_output_section_offset(shndx) == invalid_address; }
  1740. // Get the offset of input section SHNDX within its output section.
  1741. // This is -1 if the input section requires a special mapping, such
  1742. // as a merge section. The output section can be found in the
  1743. // output_sections_ field of the parent class Relobj.
  1744. Address
  1745. get_output_section_offset(unsigned int shndx) const
  1746. {
  1747. gold_assert(shndx < this->section_offsets_.size());
  1748. return this->section_offsets_[shndx];
  1749. }
  1750. // Iterate over local symbols, calling a visitor class V for each GOT offset
  1751. // associated with a local symbol.
  1752. void
  1753. do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
  1754. protected:
  1755. typedef Relobj::Output_sections Output_sections;
  1756. // Clear the local symbol information.
  1757. void
  1758. clear_got_offsets()
  1759. { this->local_got_offsets_.clear(); }
  1760. // Return the vector of section offsets.
  1761. std::vector<Address>&
  1762. section_offsets()
  1763. { return this->section_offsets_; }
  1764. // Get the address of an output section.
  1765. uint64_t
  1766. do_output_section_address(unsigned int shndx);
  1767. // Get the offset of a section.
  1768. uint64_t
  1769. do_output_section_offset(unsigned int shndx) const
  1770. {
  1771. Address off = this->get_output_section_offset(shndx);
  1772. if (off == invalid_address)
  1773. return -1ULL;
  1774. return off;
  1775. }
  1776. // Set the offset of a section.
  1777. void
  1778. do_set_section_offset(unsigned int shndx, uint64_t off)
  1779. {
  1780. gold_assert(shndx < this->section_offsets_.size());
  1781. this->section_offsets_[shndx] =
  1782. (off == static_cast<uint64_t>(-1)
  1783. ? invalid_address
  1784. : convert_types<Address, uint64_t>(off));
  1785. }
  1786. // Return whether the local symbol SYMNDX plus ADDEND has a GOT offset
  1787. // of type GOT_TYPE.
  1788. bool
  1789. do_local_has_got_offset(unsigned int symndx, unsigned int got_type,
  1790. uint64_t addend) const
  1791. {
  1792. Local_got_entry_key key(symndx);
  1793. Local_got_offsets::const_iterator p =
  1794. this->local_got_offsets_.find(key);
  1795. return (p != this->local_got_offsets_.end()
  1796. && p->second->get_offset(got_type, addend) != -1U);
  1797. }
  1798. // Return the GOT offset of type GOT_TYPE of the local symbol
  1799. // SYMNDX plus ADDEND.
  1800. unsigned int
  1801. do_local_got_offset(unsigned int symndx, unsigned int got_type,
  1802. uint64_t addend) const
  1803. {
  1804. Local_got_entry_key key(symndx);
  1805. Local_got_offsets::const_iterator p =
  1806. this->local_got_offsets_.find(key);
  1807. gold_assert(p != this->local_got_offsets_.end());
  1808. unsigned int off = p->second->get_offset(got_type, addend);
  1809. gold_assert(off != -1U);
  1810. return off;
  1811. }
  1812. // Set the GOT offset with type GOT_TYPE of the local symbol SYMNDX
  1813. // plus ADDEND to GOT_OFFSET.
  1814. void
  1815. do_set_local_got_offset(unsigned int symndx, unsigned int got_type,
  1816. unsigned int got_offset, uint64_t addend)
  1817. {
  1818. Local_got_entry_key key(symndx);
  1819. Local_got_offsets::const_iterator p =
  1820. this->local_got_offsets_.find(key);
  1821. if (p != this->local_got_offsets_.end())
  1822. p->second->set_offset(got_type, got_offset, addend);
  1823. else
  1824. {
  1825. Got_offset_list* g = new Got_offset_list(got_type, got_offset, addend);
  1826. std::pair<Local_got_offsets::iterator, bool> ins =
  1827. this->local_got_offsets_.insert(std::make_pair(key, g));
  1828. gold_assert(ins.second);
  1829. }
  1830. }
  1831. // Return the word size of the object file.
  1832. virtual int
  1833. do_elfsize() const
  1834. { return size; }
  1835. // Return TRUE if this is a big-endian object file.
  1836. virtual bool
  1837. do_is_big_endian() const
  1838. { return big_endian; }
  1839. private:
  1840. // The GOT offsets of local symbols. This map also stores GOT offsets
  1841. // for tp-relative offsets for TLS symbols.
  1842. typedef Unordered_map<Local_got_entry_key, Got_offset_list*,
  1843. Local_got_entry_key::hash,
  1844. Local_got_entry_key::equal_to> Local_got_offsets;
  1845. // GOT offsets for local non-TLS symbols, and tp-relative offsets
  1846. // for TLS symbols, indexed by local got entry key class.
  1847. Local_got_offsets local_got_offsets_;
  1848. // For each input section, the offset of the input section in its
  1849. // output section. This is INVALID_ADDRESS if the input section requires a
  1850. // special mapping.
  1851. std::vector<Address> section_offsets_;
  1852. };
  1853. // A regular object file. This is size and endian specific.
  1854. template<int size, bool big_endian>
  1855. class Sized_relobj_file : public Sized_relobj<size, big_endian>
  1856. {
  1857. public:
  1858. typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
  1859. typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
  1860. typedef std::vector<Symbol_value<size> > Local_values;
  1861. static const Address invalid_address = static_cast<Address>(0) - 1;
  1862. enum Compute_final_local_value_status
  1863. {
  1864. // No error.
  1865. CFLV_OK,
  1866. // An error occurred.
  1867. CFLV_ERROR,
  1868. // The local symbol has no output section.
  1869. CFLV_DISCARDED
  1870. };
  1871. Sized_relobj_file(const std::string& name,
  1872. Input_file* input_file,
  1873. off_t offset,
  1874. const typename elfcpp::Ehdr<size, big_endian>&);
  1875. ~Sized_relobj_file();
  1876. // Set up the object file based on TARGET.
  1877. void
  1878. setup()
  1879. { this->do_setup(); }
  1880. // Return a pointer to the Sized_relobj_file object.
  1881. Sized_relobj_file<size, big_endian>*
  1882. sized_relobj()
  1883. { return this; }
  1884. const Sized_relobj_file<size, big_endian>*
  1885. sized_relobj() const
  1886. { return this; }
  1887. // Return the ELF file type.
  1888. int
  1889. e_type() const
  1890. { return this->e_type_; }
  1891. // Return the EI_OSABI.
  1892. const Osabi&
  1893. osabi() const
  1894. { return this->osabi_; }
  1895. // Return the number of symbols. This is only valid after
  1896. // Object::add_symbols has been called.
  1897. unsigned int
  1898. symbol_count() const
  1899. { return this->local_symbol_count_ + this->symbols_.size(); }
  1900. // If SYM is the index of a global symbol in the object file's
  1901. // symbol table, return the Symbol object. Otherwise, return NULL.
  1902. Symbol*
  1903. global_symbol(unsigned int sym) const
  1904. {
  1905. if (sym >= this->local_symbol_count_)
  1906. {
  1907. gold_assert(sym - this->local_symbol_count_ < this->symbols_.size());
  1908. return this->symbols_[sym - this->local_symbol_count_];
  1909. }
  1910. return NULL;
  1911. }
  1912. // Return the section index of symbol SYM. Set *VALUE to its value
  1913. // in the object file. Set *IS_ORDINARY if this is an ordinary
  1914. // section index, not a special code between SHN_LORESERVE and
  1915. // SHN_HIRESERVE. Note that for a symbol which is not defined in
  1916. // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
  1917. // it will not return the final value of the symbol in the link.
  1918. unsigned int
  1919. symbol_section_and_value(unsigned int sym, Address* value, bool* is_ordinary);
  1920. // Return a pointer to the Symbol_value structure which holds the
  1921. // value of a local symbol.
  1922. const Symbol_value<size>*
  1923. local_symbol(unsigned int sym) const
  1924. {
  1925. gold_assert(sym < this->local_values_.size());
  1926. return &this->local_values_[sym];
  1927. }
  1928. // Return the index of local symbol SYM in the ordinary symbol
  1929. // table. A value of -1U means that the symbol is not being output.
  1930. unsigned int
  1931. symtab_index(unsigned int sym) const
  1932. {
  1933. gold_assert(sym < this->local_values_.size());
  1934. return this->local_values_[sym].output_symtab_index();
  1935. }
  1936. // Return the index of local symbol SYM in the dynamic symbol
  1937. // table. A value of -1U means that the symbol is not being output.
  1938. unsigned int
  1939. dynsym_index(unsigned int sym) const
  1940. {
  1941. gold_assert(sym < this->local_values_.size());
  1942. return this->local_values_[sym].output_dynsym_index();
  1943. }
  1944. // Return the input section index of local symbol SYM.
  1945. unsigned int
  1946. local_symbol_input_shndx(unsigned int sym, bool* is_ordinary) const
  1947. {
  1948. gold_assert(sym < this->local_values_.size());
  1949. return this->local_values_[sym].input_shndx(is_ordinary);
  1950. }
  1951. // Record that local symbol SYM must be in the output symbol table.
  1952. void
  1953. set_must_have_output_symtab_entry(unsigned int sym)
  1954. {
  1955. gold_assert(sym < this->local_values_.size());
  1956. this->local_values_[sym].set_must_have_output_symtab_entry();
  1957. }
  1958. // Record that local symbol SYM needs a dynamic symbol entry.
  1959. void
  1960. set_needs_output_dynsym_entry(unsigned int sym)
  1961. {
  1962. gold_assert(sym < this->local_values_.size());
  1963. this->local_values_[sym].set_needs_output_dynsym_entry();
  1964. }
  1965. // Return whether the local symbol SYMNDX has a PLT offset.
  1966. bool
  1967. local_has_plt_offset(unsigned int symndx) const;
  1968. // Set the PLT offset of the local symbol SYMNDX.
  1969. void
  1970. set_local_plt_offset(unsigned int symndx, unsigned int plt_offset);
  1971. // Adjust this local symbol value. Return false if the symbol
  1972. // should be discarded from the output file.
  1973. bool
  1974. adjust_local_symbol(Symbol_value<size>* lv) const
  1975. { return this->do_adjust_local_symbol(lv); }
  1976. // Return the name of the symbol that spans the given offset in the
  1977. // specified section in this object. This is used only for error
  1978. // messages and is not particularly efficient.
  1979. bool
  1980. get_symbol_location_info(unsigned int shndx, off_t offset,
  1981. Symbol_location_info* info);
  1982. // Look for a kept section corresponding to the given discarded section,
  1983. // and return its output address. This is used only for relocations in
  1984. // debugging sections.
  1985. Address
  1986. map_to_kept_section(unsigned int shndx, std::string& section_name,
  1987. bool* found) const;
  1988. // Look for a kept section corresponding to the given discarded section,
  1989. // and return its object file.
  1990. Relobj*
  1991. find_kept_section_object(unsigned int shndx, unsigned int* symndx_p) const;
  1992. // Return the name of symbol SYMNDX.
  1993. const char*
  1994. get_symbol_name(unsigned int symndx);
  1995. // Compute final local symbol value. R_SYM is the local symbol index.
  1996. // LV_IN points to a local symbol value containing the input value.
  1997. // LV_OUT points to a local symbol value storing the final output value,
  1998. // which must not be a merged symbol value since before calling this
  1999. // method to avoid memory leak. SYMTAB points to a symbol table.
  2000. //
  2001. // The method returns a status code at return. If the return status is
  2002. // CFLV_OK, *LV_OUT contains the final value. If the return status is
  2003. // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
  2004. // *LV_OUT is not modified.
  2005. Compute_final_local_value_status
  2006. compute_final_local_value(unsigned int r_sym,
  2007. const Symbol_value<size>* lv_in,
  2008. Symbol_value<size>* lv_out,
  2009. const Symbol_table* symtab);
  2010. // Return true if the layout for this object was deferred.
  2011. bool is_deferred_layout() const
  2012. { return this->is_deferred_layout_; }
  2013. protected:
  2014. typedef typename Sized_relobj<size, big_endian>::Output_sections
  2015. Output_sections;
  2016. // Set up.
  2017. virtual void
  2018. do_setup();
  2019. // Read the symbols.
  2020. void
  2021. do_read_symbols(Read_symbols_data*);
  2022. // Read the symbols. This is common code for all target-specific
  2023. // overrides of do_read_symbols.
  2024. void
  2025. base_read_symbols(Read_symbols_data*);
  2026. // Return the value of a local symbol.
  2027. uint64_t
  2028. do_local_symbol_value(unsigned int symndx, uint64_t addend) const
  2029. {
  2030. const Symbol_value<size>* symval = this->local_symbol(symndx);
  2031. return symval->value(this, addend);
  2032. }
  2033. // Return the PLT offset for a local symbol. It is an error to call
  2034. // this if it doesn't have one.
  2035. unsigned int
  2036. do_local_plt_offset(unsigned int symndx) const;
  2037. // Return whether local symbol SYMNDX is a TLS symbol.
  2038. bool
  2039. do_local_is_tls(unsigned int symndx) const
  2040. { return this->local_symbol(symndx)->is_tls_symbol(); }
  2041. // Return the number of local symbols.
  2042. unsigned int
  2043. do_local_symbol_count() const
  2044. { return this->local_symbol_count_; }
  2045. // Return the number of local symbols in the output symbol table.
  2046. unsigned int
  2047. do_output_local_symbol_count() const
  2048. { return this->output_local_symbol_count_; }
  2049. // Return the number of local symbols in the output symbol table.
  2050. off_t
  2051. do_local_symbol_offset() const
  2052. { return this->local_symbol_offset_; }
  2053. // Lay out the input sections.
  2054. void
  2055. do_layout(Symbol_table*, Layout*, Read_symbols_data*);
  2056. // Layout sections whose layout was deferred while waiting for
  2057. // input files from a plugin.
  2058. void
  2059. do_layout_deferred_sections(Layout*);
  2060. // Add the symbols to the symbol table.
  2061. void
  2062. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
  2063. Archive::Should_include
  2064. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  2065. std::string* why);
  2066. // Iterate over global symbols, calling a visitor class V for each.
  2067. void
  2068. do_for_all_global_symbols(Read_symbols_data* sd,
  2069. Library_base::Symbol_visitor_base* v);
  2070. // Read the relocs.
  2071. void
  2072. do_read_relocs(Read_relocs_data*);
  2073. // Process the relocs to find list of referenced sections. Used only
  2074. // during garbage collection.
  2075. void
  2076. do_gc_process_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  2077. // Scan the relocs and adjust the symbol table.
  2078. void
  2079. do_scan_relocs(Symbol_table*, Layout*, Read_relocs_data*);
  2080. // Count the local symbols.
  2081. void
  2082. do_count_local_symbols(Stringpool_template<char>*,
  2083. Stringpool_template<char>*);
  2084. // Finalize the local symbols.
  2085. unsigned int
  2086. do_finalize_local_symbols(unsigned int, off_t, Symbol_table*);
  2087. // Set the offset where local dynamic symbol information will be stored.
  2088. unsigned int
  2089. do_set_local_dynsym_indexes(unsigned int);
  2090. // Set the offset where local dynamic symbol information will be stored.
  2091. unsigned int
  2092. do_set_local_dynsym_offset(off_t);
  2093. // Relocate the input sections and write out the local symbols.
  2094. void
  2095. do_relocate(const Symbol_table* symtab, const Layout*, Output_file* of);
  2096. // Get the size of a section.
  2097. uint64_t
  2098. do_section_size(unsigned int shndx)
  2099. { return this->elf_file_.section_size(shndx); }
  2100. // Get the name of a section.
  2101. std::string
  2102. do_section_name(unsigned int shndx) const
  2103. { return this->elf_file_.section_name(shndx); }
  2104. // Return the location of the contents of a section.
  2105. const unsigned char*
  2106. do_section_contents(unsigned int shndx, section_size_type* plen,
  2107. bool cache)
  2108. {
  2109. Object::Location loc(this->elf_file_.section_contents(shndx));
  2110. *plen = convert_to_section_size_type(loc.data_size);
  2111. if (*plen == 0)
  2112. {
  2113. static const unsigned char empty[1] = { '\0' };
  2114. return empty;
  2115. }
  2116. return this->get_view(loc.file_offset, *plen, true, cache);
  2117. }
  2118. // Return section flags.
  2119. uint64_t
  2120. do_section_flags(unsigned int shndx);
  2121. // Return section entsize.
  2122. uint64_t
  2123. do_section_entsize(unsigned int shndx);
  2124. // Return section address.
  2125. uint64_t
  2126. do_section_address(unsigned int shndx)
  2127. { return this->elf_file_.section_addr(shndx); }
  2128. // Return section type.
  2129. unsigned int
  2130. do_section_type(unsigned int shndx)
  2131. { return this->elf_file_.section_type(shndx); }
  2132. // Return the section link field.
  2133. unsigned int
  2134. do_section_link(unsigned int shndx)
  2135. { return this->elf_file_.section_link(shndx); }
  2136. // Return the section info field.
  2137. unsigned int
  2138. do_section_info(unsigned int shndx)
  2139. { return this->elf_file_.section_info(shndx); }
  2140. // Return the section alignment.
  2141. uint64_t
  2142. do_section_addralign(unsigned int shndx)
  2143. { return this->elf_file_.section_addralign(shndx); }
  2144. // Return the Xindex structure to use.
  2145. Xindex*
  2146. do_initialize_xindex();
  2147. // Get symbol counts.
  2148. void
  2149. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
  2150. // Get the global symbols.
  2151. const Symbols*
  2152. do_get_global_symbols() const
  2153. { return &this->symbols_; }
  2154. // Adjust a section index if necessary.
  2155. unsigned int
  2156. adjust_shndx(unsigned int shndx)
  2157. {
  2158. if (shndx >= elfcpp::SHN_LORESERVE)
  2159. shndx += this->elf_file_.large_shndx_offset();
  2160. return shndx;
  2161. }
  2162. // Initialize input to output maps for section symbols in merged
  2163. // sections.
  2164. void
  2165. initialize_input_to_output_maps();
  2166. // Free the input to output maps for section symbols in merged
  2167. // sections.
  2168. void
  2169. free_input_to_output_maps();
  2170. // Return symbol table section index.
  2171. unsigned int
  2172. symtab_shndx() const
  2173. { return this->symtab_shndx_; }
  2174. // Allow a child class to access the ELF file.
  2175. elfcpp::Elf_file<size, big_endian, Object>*
  2176. elf_file()
  2177. { return &this->elf_file_; }
  2178. // Allow a child class to access the local values.
  2179. Local_values*
  2180. local_values()
  2181. { return &this->local_values_; }
  2182. // Views and sizes when relocating.
  2183. struct View_size
  2184. {
  2185. unsigned char* view;
  2186. typename elfcpp::Elf_types<size>::Elf_Addr address;
  2187. off_t offset;
  2188. section_size_type view_size;
  2189. bool is_input_output_view;
  2190. bool is_postprocessing_view;
  2191. bool is_ctors_reverse_view;
  2192. };
  2193. typedef std::vector<View_size> Views;
  2194. // Stash away info for a number of special sections.
  2195. // Return true if any of the sections found require local symbols to be read.
  2196. virtual bool
  2197. do_find_special_sections(Read_symbols_data* sd);
  2198. // This may be overriden by a child class.
  2199. virtual void
  2200. do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
  2201. const unsigned char* pshdrs, Output_file* of,
  2202. Views* pviews);
  2203. // Relocate section data for a range of sections.
  2204. void
  2205. relocate_section_range(const Symbol_table* symtab, const Layout* layout,
  2206. const unsigned char* pshdrs, Output_file* of,
  2207. Views* pviews, unsigned int start_shndx,
  2208. unsigned int end_shndx);
  2209. // Adjust this local symbol value. Return false if the symbol
  2210. // should be discarded from the output file.
  2211. virtual bool
  2212. do_adjust_local_symbol(Symbol_value<size>*) const
  2213. { return true; }
  2214. // Allow a child to set output local symbol count.
  2215. void
  2216. set_output_local_symbol_count(unsigned int value)
  2217. { this->output_local_symbol_count_ = value; }
  2218. // Return the output view for a section.
  2219. unsigned char*
  2220. do_get_output_view(unsigned int, section_size_type*) const;
  2221. private:
  2222. // For convenience.
  2223. typedef Sized_relobj_file<size, big_endian> This;
  2224. static const int ehdr_size = elfcpp::Elf_sizes<size>::ehdr_size;
  2225. static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  2226. static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  2227. typedef elfcpp::Shdr<size, big_endian> Shdr;
  2228. typedef elfcpp::Shdr_write<size, big_endian> Shdr_write;
  2229. // To keep track of discarded comdat sections, we need to map a member
  2230. // section index to the object and section index of the corresponding
  2231. // kept section.
  2232. struct Kept_comdat_section
  2233. {
  2234. Kept_comdat_section(uint64_t a_sh_size, Kept_section* a_kept_section,
  2235. unsigned int a_symndx, bool a_is_comdat)
  2236. : sh_size(a_sh_size), kept_section(a_kept_section),
  2237. symndx (a_symndx), is_comdat(a_is_comdat)
  2238. { }
  2239. uint64_t sh_size; // Section size
  2240. Kept_section* kept_section; // Kept section info
  2241. unsigned int symndx; // Index of key symbol
  2242. bool is_comdat; // True if comdat group, false if linkonce
  2243. };
  2244. typedef std::map<unsigned int, Kept_comdat_section>
  2245. Kept_comdat_section_table;
  2246. // Find the SHT_SYMTAB section, given the section headers.
  2247. void
  2248. find_symtab(const unsigned char* pshdrs);
  2249. // Return whether SHDR has the right flags for a GNU style exception
  2250. // frame section.
  2251. bool
  2252. check_eh_frame_flags(const elfcpp::Shdr<size, big_endian>* shdr) const;
  2253. // Return whether there is a section named .eh_frame which might be
  2254. // a GNU style exception frame section.
  2255. bool
  2256. find_eh_frame(const unsigned char* pshdrs, const char* names,
  2257. section_size_type names_size) const;
  2258. // Whether to include a section group in the link.
  2259. bool
  2260. include_section_group(Symbol_table*, Layout*, unsigned int, const char*,
  2261. const unsigned char*, const char*, section_size_type,
  2262. std::vector<bool>*);
  2263. // Whether to include a linkonce section in the link.
  2264. bool
  2265. include_linkonce_section(Layout*, unsigned int, const char*,
  2266. const elfcpp::Shdr<size, big_endian>&);
  2267. // Layout an input section.
  2268. void
  2269. layout_section(Layout* layout, unsigned int shndx, const char* name,
  2270. const typename This::Shdr& shdr, unsigned int sh_type,
  2271. unsigned int reloc_shndx, unsigned int reloc_type);
  2272. // Layout an input .eh_frame section.
  2273. void
  2274. layout_eh_frame_section(Layout* layout, const unsigned char* symbols_data,
  2275. section_size_type symbols_size,
  2276. const unsigned char* symbol_names_data,
  2277. section_size_type symbol_names_size,
  2278. unsigned int shndx, const typename This::Shdr&,
  2279. unsigned int reloc_shndx, unsigned int reloc_type);
  2280. // Layout an input .note.gnu.property section.
  2281. void
  2282. layout_gnu_property_section(Layout* layout, unsigned int shndx);
  2283. // Write section data to the output file. Record the views and
  2284. // sizes in VIEWS for use when relocating.
  2285. void
  2286. write_sections(const Layout*, const unsigned char* pshdrs, Output_file*,
  2287. Views*);
  2288. // Relocate the sections in the output file.
  2289. void
  2290. relocate_sections(const Symbol_table* symtab, const Layout* layout,
  2291. const unsigned char* pshdrs, Output_file* of,
  2292. Views* pviews)
  2293. { this->do_relocate_sections(symtab, layout, pshdrs, of, pviews); }
  2294. // Reverse the words in a section. Used for .ctors sections mapped
  2295. // to .init_array sections.
  2296. void
  2297. reverse_words(unsigned char*, section_size_type);
  2298. // Scan the input relocations for --emit-relocs.
  2299. void
  2300. emit_relocs_scan(Symbol_table*, Layout*, const unsigned char* plocal_syms,
  2301. const Read_relocs_data::Relocs_list::iterator&);
  2302. // Scan the input relocations for --emit-relocs, templatized on the
  2303. // type of the relocation section.
  2304. template<int sh_type>
  2305. void
  2306. emit_relocs_scan_reltype(Symbol_table*, Layout*,
  2307. const unsigned char* plocal_syms,
  2308. const Read_relocs_data::Relocs_list::iterator&,
  2309. Relocatable_relocs*);
  2310. // Scan the input relocations for --incremental.
  2311. void
  2312. incremental_relocs_scan(const Read_relocs_data::Relocs_list::iterator&);
  2313. // Scan the input relocations for --incremental, templatized on the
  2314. // type of the relocation section.
  2315. template<int sh_type>
  2316. void
  2317. incremental_relocs_scan_reltype(
  2318. const Read_relocs_data::Relocs_list::iterator&);
  2319. void
  2320. incremental_relocs_write(const Relocate_info<size, big_endian>*,
  2321. unsigned int sh_type,
  2322. const unsigned char* prelocs,
  2323. size_t reloc_count,
  2324. Output_section*,
  2325. Address output_offset,
  2326. Output_file*);
  2327. template<int sh_type>
  2328. void
  2329. incremental_relocs_write_reltype(const Relocate_info<size, big_endian>*,
  2330. const unsigned char* prelocs,
  2331. size_t reloc_count,
  2332. Output_section*,
  2333. Address output_offset,
  2334. Output_file*);
  2335. // A type shared by split_stack_adjust_reltype and find_functions.
  2336. typedef std::map<section_offset_type, section_size_type> Function_offsets;
  2337. // Check for -fsplit-stack routines calling non-split-stack routines.
  2338. void
  2339. split_stack_adjust(const Symbol_table*, const unsigned char* pshdrs,
  2340. unsigned int sh_type, unsigned int shndx,
  2341. const unsigned char* prelocs, size_t reloc_count,
  2342. unsigned char* view, section_size_type view_size,
  2343. Reloc_symbol_changes** reloc_map,
  2344. const Sized_target<size, big_endian>* target);
  2345. template<int sh_type>
  2346. void
  2347. split_stack_adjust_reltype(const Symbol_table*, const unsigned char* pshdrs,
  2348. unsigned int shndx, const unsigned char* prelocs,
  2349. size_t reloc_count, unsigned char* view,
  2350. section_size_type view_size,
  2351. Reloc_symbol_changes** reloc_map,
  2352. const Sized_target<size, big_endian>* target);
  2353. // Find all functions in a section.
  2354. void
  2355. find_functions(const unsigned char* pshdrs, unsigned int shndx,
  2356. Function_offsets*);
  2357. // Write out the local symbols.
  2358. void
  2359. write_local_symbols(Output_file*,
  2360. const Stringpool_template<char>*,
  2361. const Stringpool_template<char>*,
  2362. Output_symtab_xindex*,
  2363. Output_symtab_xindex*,
  2364. off_t);
  2365. // Record a mapping from discarded section SHNDX to the corresponding
  2366. // kept section.
  2367. void
  2368. set_kept_comdat_section(unsigned int shndx, bool is_comdat,
  2369. unsigned int symndx, uint64_t sh_size,
  2370. Kept_section* kept_section)
  2371. {
  2372. Kept_comdat_section kept(sh_size, kept_section, symndx, is_comdat);
  2373. this->kept_comdat_sections_.insert(std::make_pair(shndx, kept));
  2374. }
  2375. // Find the kept section corresponding to the discarded section
  2376. // SHNDX. Return true if found.
  2377. bool
  2378. get_kept_comdat_section(unsigned int shndx, bool* is_comdat,
  2379. unsigned int *symndx, uint64_t* sh_size,
  2380. Kept_section** kept_section) const
  2381. {
  2382. typename Kept_comdat_section_table::const_iterator p =
  2383. this->kept_comdat_sections_.find(shndx);
  2384. if (p == this->kept_comdat_sections_.end())
  2385. return false;
  2386. *is_comdat = p->second.is_comdat;
  2387. *symndx = p->second.symndx;
  2388. *sh_size = p->second.sh_size;
  2389. *kept_section = p->second.kept_section;
  2390. return true;
  2391. }
  2392. // Compute final local symbol value. R_SYM is the local symbol index.
  2393. // LV_IN points to a local symbol value containing the input value.
  2394. // LV_OUT points to a local symbol value storing the final output value,
  2395. // which must not be a merged symbol value since before calling this
  2396. // method to avoid memory leak. RELOCATABLE indicates whether we are
  2397. // linking a relocatable output. OUT_SECTIONS is an array of output
  2398. // sections. OUT_OFFSETS is an array of offsets of the sections. SYMTAB
  2399. // points to a symbol table.
  2400. //
  2401. // The method returns a status code at return. If the return status is
  2402. // CFLV_OK, *LV_OUT contains the final value. If the return status is
  2403. // CFLV_ERROR, *LV_OUT is 0. If the return status is CFLV_DISCARDED,
  2404. // *LV_OUT is not modified.
  2405. inline Compute_final_local_value_status
  2406. compute_final_local_value_internal(unsigned int r_sym,
  2407. const Symbol_value<size>* lv_in,
  2408. Symbol_value<size>* lv_out,
  2409. bool relocatable,
  2410. const Output_sections& out_sections,
  2411. const std::vector<Address>& out_offsets,
  2412. const Symbol_table* symtab);
  2413. // The PLT offsets of local symbols.
  2414. typedef Unordered_map<unsigned int, unsigned int> Local_plt_offsets;
  2415. // Saved information for sections whose layout was deferred.
  2416. struct Deferred_layout
  2417. {
  2418. static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  2419. Deferred_layout(unsigned int shndx, const char* name,
  2420. unsigned int sh_type,
  2421. const unsigned char* pshdr,
  2422. unsigned int reloc_shndx, unsigned int reloc_type)
  2423. : name_(name), shndx_(shndx), reloc_shndx_(reloc_shndx),
  2424. reloc_type_(reloc_type)
  2425. {
  2426. typename This::Shdr_write shdr(this->shdr_data_);
  2427. memcpy(this->shdr_data_, pshdr, shdr_size);
  2428. shdr.put_sh_type(sh_type);
  2429. }
  2430. std::string name_;
  2431. unsigned int shndx_;
  2432. unsigned int reloc_shndx_;
  2433. unsigned int reloc_type_;
  2434. unsigned char shdr_data_[shdr_size];
  2435. };
  2436. // General access to the ELF file.
  2437. elfcpp::Elf_file<size, big_endian, Object> elf_file_;
  2438. // The EI_OSABI.
  2439. const Osabi osabi_;
  2440. // Type of ELF file (ET_REL or ET_EXEC). ET_EXEC files are allowed
  2441. // as input files only for the --just-symbols option.
  2442. int e_type_;
  2443. // Index of SHT_SYMTAB section.
  2444. unsigned int symtab_shndx_;
  2445. // The number of local symbols.
  2446. unsigned int local_symbol_count_;
  2447. // The number of local symbols which go into the output file.
  2448. unsigned int output_local_symbol_count_;
  2449. // The number of local symbols which go into the output file's dynamic
  2450. // symbol table.
  2451. unsigned int output_local_dynsym_count_;
  2452. // The entries in the symbol table for the external symbols.
  2453. Symbols symbols_;
  2454. // Number of symbols defined in object file itself.
  2455. size_t defined_count_;
  2456. // File offset for local symbols (relative to start of symbol table).
  2457. off_t local_symbol_offset_;
  2458. // File offset for local dynamic symbols (absolute).
  2459. off_t local_dynsym_offset_;
  2460. // Values of local symbols.
  2461. Local_values local_values_;
  2462. // PLT offsets for local symbols.
  2463. Local_plt_offsets local_plt_offsets_;
  2464. // Table mapping discarded comdat sections to corresponding kept sections.
  2465. Kept_comdat_section_table kept_comdat_sections_;
  2466. // Whether this object has a GNU style .eh_frame section.
  2467. bool has_eh_frame_;
  2468. // True if the layout of this object was deferred, waiting for plugin
  2469. // replacement files.
  2470. bool is_deferred_layout_;
  2471. // The list of sections whose layout was deferred.
  2472. std::vector<Deferred_layout> deferred_layout_;
  2473. // The list of relocation sections whose layout was deferred.
  2474. std::vector<Deferred_layout> deferred_layout_relocs_;
  2475. // Pointer to the list of output views; valid only during do_relocate().
  2476. const Views* output_views_;
  2477. };
  2478. // A class to manage the list of all objects.
  2479. class Input_objects
  2480. {
  2481. public:
  2482. Input_objects()
  2483. : relobj_list_(), dynobj_list_(), sonames_(), cref_(NULL)
  2484. { }
  2485. // The type of the list of input relocateable objects.
  2486. typedef std::vector<Relobj*> Relobj_list;
  2487. typedef Relobj_list::const_iterator Relobj_iterator;
  2488. // The type of the list of input dynamic objects.
  2489. typedef std::vector<Dynobj*> Dynobj_list;
  2490. typedef Dynobj_list::const_iterator Dynobj_iterator;
  2491. // Add an object to the list. Return true if all is well, or false
  2492. // if this object should be ignored.
  2493. bool
  2494. add_object(Object*);
  2495. // Start processing an archive.
  2496. void
  2497. archive_start(Archive*);
  2498. // Stop processing an archive.
  2499. void
  2500. archive_stop(Archive*);
  2501. // For each dynamic object, check whether we've seen all of its
  2502. // explicit dependencies.
  2503. void
  2504. check_dynamic_dependencies() const;
  2505. // Return whether an object was found in the system library
  2506. // directory.
  2507. bool
  2508. found_in_system_library_directory(const Object*) const;
  2509. // Print symbol counts.
  2510. void
  2511. print_symbol_counts(const Symbol_table*) const;
  2512. // Print a cross reference table.
  2513. void
  2514. print_cref(const Symbol_table*, FILE*) const;
  2515. // Iterate over all regular objects.
  2516. Relobj_iterator
  2517. relobj_begin() const
  2518. { return this->relobj_list_.begin(); }
  2519. Relobj_iterator
  2520. relobj_end() const
  2521. { return this->relobj_list_.end(); }
  2522. // Iterate over all dynamic objects.
  2523. Dynobj_iterator
  2524. dynobj_begin() const
  2525. { return this->dynobj_list_.begin(); }
  2526. Dynobj_iterator
  2527. dynobj_end() const
  2528. { return this->dynobj_list_.end(); }
  2529. // Return whether we have seen any dynamic objects.
  2530. bool
  2531. any_dynamic() const
  2532. { return !this->dynobj_list_.empty(); }
  2533. // Return the number of non dynamic objects.
  2534. int
  2535. number_of_relobjs() const
  2536. { return this->relobj_list_.size(); }
  2537. // Return the number of input objects.
  2538. int
  2539. number_of_input_objects() const
  2540. { return this->relobj_list_.size() + this->dynobj_list_.size(); }
  2541. private:
  2542. Input_objects(const Input_objects&);
  2543. Input_objects& operator=(const Input_objects&);
  2544. // The list of ordinary objects included in the link.
  2545. Relobj_list relobj_list_;
  2546. // The list of dynamic objects included in the link.
  2547. Dynobj_list dynobj_list_;
  2548. // SONAMEs that we have seen.
  2549. Unordered_map<std::string, Object*> sonames_;
  2550. // Manage cross-references if requested.
  2551. Cref* cref_;
  2552. };
  2553. // Some of the information we pass to the relocation routines. We
  2554. // group this together to avoid passing a dozen different arguments.
  2555. template<int size, bool big_endian>
  2556. struct Relocate_info
  2557. {
  2558. // Symbol table.
  2559. const Symbol_table* symtab;
  2560. // Layout.
  2561. const Layout* layout;
  2562. // Object being relocated.
  2563. Sized_relobj_file<size, big_endian>* object;
  2564. // Section index of relocation section.
  2565. unsigned int reloc_shndx;
  2566. // Section header of relocation section.
  2567. const unsigned char* reloc_shdr;
  2568. // Info about how relocs should be handled
  2569. Relocatable_relocs* rr;
  2570. // Section index of section being relocated.
  2571. unsigned int data_shndx;
  2572. // Section header of data section.
  2573. const unsigned char* data_shdr;
  2574. // Return a string showing the location of a relocation. This is
  2575. // only used for error messages.
  2576. std::string
  2577. location(size_t relnum, off_t reloffset) const;
  2578. };
  2579. // This is used to represent a section in an object and is used as the
  2580. // key type for various section maps.
  2581. typedef std::pair<Relobj*, unsigned int> Section_id;
  2582. // This is similar to Section_id but is used when the section
  2583. // pointers are const.
  2584. typedef std::pair<const Relobj*, unsigned int> Const_section_id;
  2585. // The hash value is based on the address of an object in memory during
  2586. // linking. It is okay to use this for looking up sections but never use
  2587. // this in an unordered container that we want to traverse in a repeatable
  2588. // manner.
  2589. struct Section_id_hash
  2590. {
  2591. size_t operator()(const Section_id& loc) const
  2592. { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
  2593. };
  2594. struct Const_section_id_hash
  2595. {
  2596. size_t operator()(const Const_section_id& loc) const
  2597. { return reinterpret_cast<uintptr_t>(loc.first) ^ loc.second; }
  2598. };
  2599. // Return whether INPUT_FILE contains an ELF object start at file
  2600. // offset OFFSET. This sets *START to point to a view of the start of
  2601. // the file. It sets *READ_SIZE to the number of bytes in the view.
  2602. extern bool
  2603. is_elf_object(Input_file* input_file, off_t offset,
  2604. const unsigned char** start, int* read_size);
  2605. // Return an Object appropriate for the input file. P is BYTES long,
  2606. // and holds the ELF header. If PUNCONFIGURED is not NULL, then if
  2607. // this sees an object the linker is not configured to support, it
  2608. // sets *PUNCONFIGURED to true and returns NULL without giving an
  2609. // error message.
  2610. extern Object*
  2611. make_elf_object(const std::string& name, Input_file*,
  2612. off_t offset, const unsigned char* p,
  2613. section_offset_type bytes, bool* punconfigured);
  2614. } // end namespace gold
  2615. #endif // !defined(GOLD_OBJECT_H)