object.h 89 KB

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