symtab.h 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  1. // symtab.h -- the gold symbol table -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. // Symbol_table
  18. // The symbol table.
  19. #ifndef GOLD_SYMTAB_H
  20. #define GOLD_SYMTAB_H
  21. #include <string>
  22. #include <utility>
  23. #include <vector>
  24. #include "elfcpp.h"
  25. #include "parameters.h"
  26. #include "stringpool.h"
  27. #include "object.h"
  28. namespace gold
  29. {
  30. class Mapfile;
  31. class Object;
  32. class Relobj;
  33. template<int size, bool big_endian>
  34. class Sized_relobj_file;
  35. template<int size, bool big_endian>
  36. class Sized_pluginobj;
  37. class Dynobj;
  38. template<int size, bool big_endian>
  39. class Sized_dynobj;
  40. template<int size, bool big_endian>
  41. class Sized_incrobj;
  42. class Versions;
  43. class Version_script_info;
  44. class Input_objects;
  45. class Output_data;
  46. class Output_section;
  47. class Output_segment;
  48. class Output_file;
  49. class Output_symtab_xindex;
  50. class Garbage_collection;
  51. class Icf;
  52. // The base class of an entry in the symbol table. The symbol table
  53. // can have a lot of entries, so we don't want this class too big.
  54. // Size dependent fields can be found in the template class
  55. // Sized_symbol. Targets may support their own derived classes.
  56. class Symbol
  57. {
  58. public:
  59. // Because we want the class to be small, we don't use any virtual
  60. // functions. But because symbols can be defined in different
  61. // places, we need to classify them. This enum is the different
  62. // sources of symbols we support.
  63. enum Source
  64. {
  65. // Symbol defined in a relocatable or dynamic input file--this is
  66. // the most common case.
  67. FROM_OBJECT,
  68. // Symbol defined in an Output_data, a special section created by
  69. // the target.
  70. IN_OUTPUT_DATA,
  71. // Symbol defined in an Output_segment, with no associated
  72. // section.
  73. IN_OUTPUT_SEGMENT,
  74. // Symbol value is constant.
  75. IS_CONSTANT,
  76. // Symbol is undefined.
  77. IS_UNDEFINED
  78. };
  79. // When the source is IN_OUTPUT_SEGMENT, we need to describe what
  80. // the offset means.
  81. enum Segment_offset_base
  82. {
  83. // From the start of the segment.
  84. SEGMENT_START,
  85. // From the end of the segment.
  86. SEGMENT_END,
  87. // From the filesz of the segment--i.e., after the loaded bytes
  88. // but before the bytes which are allocated but zeroed.
  89. SEGMENT_BSS
  90. };
  91. // Return the symbol name.
  92. const char*
  93. name() const
  94. { return this->name_; }
  95. // Return the (ANSI) demangled version of the name, if
  96. // parameters.demangle() is true. Otherwise, return the name. This
  97. // is intended to be used only for logging errors, so it's not
  98. // super-efficient.
  99. std::string
  100. demangled_name() const;
  101. // Return the symbol version. This will return NULL for an
  102. // unversioned symbol.
  103. const char*
  104. version() const
  105. { return this->version_; }
  106. void
  107. clear_version()
  108. { this->version_ = NULL; }
  109. // Return whether this version is the default for this symbol name
  110. // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
  111. // meaningful for versioned symbols.
  112. bool
  113. is_default() const
  114. {
  115. gold_assert(this->version_ != NULL);
  116. return this->is_def_;
  117. }
  118. // Set that this version is the default for this symbol name.
  119. void
  120. set_is_default()
  121. { this->is_def_ = true; }
  122. // Set that this version is not the default for this symbol name.
  123. void
  124. set_is_not_default()
  125. { this->is_def_ = false; }
  126. // Return the symbol's name as name@version (or name@@version).
  127. std::string
  128. versioned_name() const;
  129. // Return the symbol source.
  130. Source
  131. source() const
  132. { return this->source_; }
  133. // Return the object with which this symbol is associated.
  134. Object*
  135. object() const
  136. {
  137. gold_assert(this->source_ == FROM_OBJECT);
  138. return this->u1_.object;
  139. }
  140. // Return the index of the section in the input relocatable or
  141. // dynamic object file.
  142. unsigned int
  143. shndx(bool* is_ordinary) const
  144. {
  145. gold_assert(this->source_ == FROM_OBJECT);
  146. *is_ordinary = this->is_ordinary_shndx_;
  147. return this->u2_.shndx;
  148. }
  149. // Return the output data section with which this symbol is
  150. // associated, if the symbol was specially defined with respect to
  151. // an output data section.
  152. Output_data*
  153. output_data() const
  154. {
  155. gold_assert(this->source_ == IN_OUTPUT_DATA);
  156. return this->u1_.output_data;
  157. }
  158. // If this symbol was defined with respect to an output data
  159. // section, return whether the value is an offset from end.
  160. bool
  161. offset_is_from_end() const
  162. {
  163. gold_assert(this->source_ == IN_OUTPUT_DATA);
  164. return this->u2_.offset_is_from_end;
  165. }
  166. // Return the output segment with which this symbol is associated,
  167. // if the symbol was specially defined with respect to an output
  168. // segment.
  169. Output_segment*
  170. output_segment() const
  171. {
  172. gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
  173. return this->u1_.output_segment;
  174. }
  175. // If this symbol was defined with respect to an output segment,
  176. // return the offset base.
  177. Segment_offset_base
  178. offset_base() const
  179. {
  180. gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
  181. return this->u2_.offset_base;
  182. }
  183. // Return the symbol binding.
  184. elfcpp::STB
  185. binding() const
  186. { return this->binding_; }
  187. // Return the symbol type.
  188. elfcpp::STT
  189. type() const
  190. { return this->type_; }
  191. // Set the symbol type.
  192. void
  193. set_type(elfcpp::STT type)
  194. { this->type_ = type; }
  195. // Return true for function symbol.
  196. bool
  197. is_func() const
  198. {
  199. return (this->type_ == elfcpp::STT_FUNC
  200. || this->type_ == elfcpp::STT_GNU_IFUNC);
  201. }
  202. // Return the symbol visibility.
  203. elfcpp::STV
  204. visibility() const
  205. { return this->visibility_; }
  206. // Set the visibility.
  207. void
  208. set_visibility(elfcpp::STV visibility)
  209. { this->visibility_ = visibility; }
  210. // Override symbol visibility.
  211. void
  212. override_visibility(elfcpp::STV);
  213. // Set whether the symbol was originally a weak undef or a regular undef
  214. // when resolved by a dynamic def or by a special symbol.
  215. inline void
  216. set_undef_binding(elfcpp::STB bind)
  217. {
  218. if (!this->undef_binding_set_ || this->undef_binding_weak_)
  219. {
  220. this->undef_binding_weak_ = bind == elfcpp::STB_WEAK;
  221. this->undef_binding_set_ = true;
  222. }
  223. }
  224. // Return TRUE if a weak undef was resolved by a dynamic def or
  225. // by a special symbol.
  226. inline bool
  227. is_undef_binding_weak() const
  228. { return this->undef_binding_weak_; }
  229. // Return the non-visibility part of the st_other field.
  230. unsigned char
  231. nonvis() const
  232. { return this->nonvis_; }
  233. // Set the non-visibility part of the st_other field.
  234. void
  235. set_nonvis(unsigned int nonvis)
  236. { this->nonvis_ = nonvis; }
  237. // Return whether this symbol is a forwarder. This will never be
  238. // true of a symbol found in the hash table, but may be true of
  239. // symbol pointers attached to object files.
  240. bool
  241. is_forwarder() const
  242. { return this->is_forwarder_; }
  243. // Mark this symbol as a forwarder.
  244. void
  245. set_forwarder()
  246. { this->is_forwarder_ = true; }
  247. // Return whether this symbol has an alias in the weak aliases table
  248. // in Symbol_table.
  249. bool
  250. has_alias() const
  251. { return this->has_alias_; }
  252. // Mark this symbol as having an alias.
  253. void
  254. set_has_alias()
  255. { this->has_alias_ = true; }
  256. // Return whether this symbol needs an entry in the dynamic symbol
  257. // table.
  258. bool
  259. needs_dynsym_entry() const
  260. {
  261. return (this->needs_dynsym_entry_
  262. || (this->in_reg()
  263. && this->in_dyn()
  264. && this->is_externally_visible()));
  265. }
  266. // Mark this symbol as needing an entry in the dynamic symbol table.
  267. void
  268. set_needs_dynsym_entry()
  269. { this->needs_dynsym_entry_ = true; }
  270. // Return whether this symbol should be added to the dynamic symbol
  271. // table.
  272. bool
  273. should_add_dynsym_entry(Symbol_table*) const;
  274. // Return whether this symbol has been seen in a regular object.
  275. bool
  276. in_reg() const
  277. { return this->in_reg_; }
  278. // Mark this symbol as having been seen in a regular object.
  279. void
  280. set_in_reg()
  281. { this->in_reg_ = true; }
  282. // Forget this symbol was seen in a regular object.
  283. void
  284. clear_in_reg()
  285. { this->in_reg_ = false; }
  286. // Return whether this symbol has been seen in a dynamic object.
  287. bool
  288. in_dyn() const
  289. { return this->in_dyn_; }
  290. // Mark this symbol as having been seen in a dynamic object.
  291. void
  292. set_in_dyn()
  293. { this->in_dyn_ = true; }
  294. // Return whether this symbol is defined in a dynamic object.
  295. bool
  296. from_dyn() const
  297. { return this->source_ == FROM_OBJECT && this->object()->is_dynamic(); }
  298. // Return whether this symbol has been seen in a real ELF object.
  299. // (IN_REG will return TRUE if the symbol has been seen in either
  300. // a real ELF object or an object claimed by a plugin.)
  301. bool
  302. in_real_elf() const
  303. { return this->in_real_elf_; }
  304. // Mark this symbol as having been seen in a real ELF object.
  305. void
  306. set_in_real_elf()
  307. { this->in_real_elf_ = true; }
  308. // Return whether this symbol was defined in a section that was
  309. // discarded from the link. This is used to control some error
  310. // reporting.
  311. bool
  312. is_defined_in_discarded_section() const
  313. { return this->is_defined_in_discarded_section_; }
  314. // Mark this symbol as having been defined in a discarded section.
  315. void
  316. set_is_defined_in_discarded_section()
  317. { this->is_defined_in_discarded_section_ = true; }
  318. // Return the index of this symbol in the output file symbol table.
  319. // A value of -1U means that this symbol is not going into the
  320. // output file. This starts out as zero, and is set to a non-zero
  321. // value by Symbol_table::finalize. It is an error to ask for the
  322. // symbol table index before it has been set.
  323. unsigned int
  324. symtab_index() const
  325. {
  326. gold_assert(this->symtab_index_ != 0);
  327. return this->symtab_index_;
  328. }
  329. // Set the index of the symbol in the output file symbol table.
  330. void
  331. set_symtab_index(unsigned int index)
  332. {
  333. gold_assert(index != 0);
  334. this->symtab_index_ = index;
  335. }
  336. // Return whether this symbol already has an index in the output
  337. // file symbol table.
  338. bool
  339. has_symtab_index() const
  340. { return this->symtab_index_ != 0; }
  341. // Return the index of this symbol in the dynamic symbol table. A
  342. // value of -1U means that this symbol is not going into the dynamic
  343. // symbol table. This starts out as zero, and is set to a non-zero
  344. // during Layout::finalize. It is an error to ask for the dynamic
  345. // symbol table index before it has been set.
  346. unsigned int
  347. dynsym_index() const
  348. {
  349. gold_assert(this->dynsym_index_ != 0);
  350. return this->dynsym_index_;
  351. }
  352. // Set the index of the symbol in the dynamic symbol table.
  353. void
  354. set_dynsym_index(unsigned int index)
  355. {
  356. gold_assert(index != 0);
  357. this->dynsym_index_ = index;
  358. }
  359. // Return whether this symbol already has an index in the dynamic
  360. // symbol table.
  361. bool
  362. has_dynsym_index() const
  363. { return this->dynsym_index_ != 0; }
  364. // Return whether this symbol has an entry in the GOT section.
  365. // For a TLS symbol, this GOT entry will hold its tp-relative offset.
  366. bool
  367. has_got_offset(unsigned int got_type, uint64_t addend = 0) const
  368. { return this->got_offsets_.get_offset(got_type, addend) != -1U; }
  369. // Return the offset into the GOT section of this symbol.
  370. unsigned int
  371. got_offset(unsigned int got_type, uint64_t addend = 0) const
  372. {
  373. unsigned int got_offset = this->got_offsets_.get_offset(got_type, addend);
  374. gold_assert(got_offset != -1U);
  375. return got_offset;
  376. }
  377. // Set the GOT offset of this symbol.
  378. void
  379. set_got_offset(unsigned int got_type, unsigned int got_offset,
  380. uint64_t addend = 0)
  381. { this->got_offsets_.set_offset(got_type, got_offset, addend); }
  382. // Return the GOT offset list.
  383. const Got_offset_list*
  384. got_offset_list() const
  385. { return this->got_offsets_.get_list(); }
  386. // Return whether this symbol has an entry in the PLT section.
  387. bool
  388. has_plt_offset() const
  389. { return this->plt_offset_ != -1U; }
  390. // Return the offset into the PLT section of this symbol.
  391. unsigned int
  392. plt_offset() const
  393. {
  394. gold_assert(this->has_plt_offset());
  395. return this->plt_offset_;
  396. }
  397. // Set the PLT offset of this symbol.
  398. void
  399. set_plt_offset(unsigned int plt_offset)
  400. {
  401. gold_assert(plt_offset != -1U);
  402. this->plt_offset_ = plt_offset;
  403. }
  404. // Return whether this dynamic symbol needs a special value in the
  405. // dynamic symbol table.
  406. bool
  407. needs_dynsym_value() const
  408. { return this->needs_dynsym_value_; }
  409. // Set that this dynamic symbol needs a special value in the dynamic
  410. // symbol table.
  411. void
  412. set_needs_dynsym_value()
  413. {
  414. gold_assert(this->object()->is_dynamic());
  415. this->needs_dynsym_value_ = true;
  416. }
  417. // Return true if the final value of this symbol is known at link
  418. // time.
  419. bool
  420. final_value_is_known() const;
  421. // Return true if SHNDX represents a common symbol. This depends on
  422. // the target.
  423. static bool
  424. is_common_shndx(unsigned int shndx);
  425. // Return whether this is a defined symbol (not undefined or
  426. // common).
  427. bool
  428. is_defined() const
  429. {
  430. bool is_ordinary;
  431. if (this->source_ != FROM_OBJECT)
  432. return this->source_ != IS_UNDEFINED;
  433. unsigned int shndx = this->shndx(&is_ordinary);
  434. return (is_ordinary
  435. ? shndx != elfcpp::SHN_UNDEF
  436. : !Symbol::is_common_shndx(shndx));
  437. }
  438. // Return true if this symbol is from a dynamic object.
  439. bool
  440. is_from_dynobj() const
  441. {
  442. return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
  443. }
  444. // Return whether this is a placeholder symbol from a plugin object.
  445. bool
  446. is_placeholder() const
  447. {
  448. return this->source_ == FROM_OBJECT && this->object()->pluginobj() != NULL;
  449. }
  450. // Return whether this is an undefined symbol.
  451. bool
  452. is_undefined() const
  453. {
  454. bool is_ordinary;
  455. return ((this->source_ == FROM_OBJECT
  456. && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF
  457. && is_ordinary)
  458. || this->source_ == IS_UNDEFINED);
  459. }
  460. // Return whether this is a weak undefined symbol.
  461. bool
  462. is_weak_undefined() const
  463. {
  464. return (this->is_undefined()
  465. && (this->binding() == elfcpp::STB_WEAK
  466. || this->is_undef_binding_weak()
  467. || parameters->options().weak_unresolved_symbols()));
  468. }
  469. // Return whether this is a strong undefined symbol.
  470. bool
  471. is_strong_undefined() const
  472. {
  473. return (this->is_undefined()
  474. && this->binding() != elfcpp::STB_WEAK
  475. && !this->is_undef_binding_weak()
  476. && !parameters->options().weak_unresolved_symbols());
  477. }
  478. // Return whether this is an absolute symbol.
  479. bool
  480. is_absolute() const
  481. {
  482. bool is_ordinary;
  483. return ((this->source_ == FROM_OBJECT
  484. && this->shndx(&is_ordinary) == elfcpp::SHN_ABS
  485. && !is_ordinary)
  486. || this->source_ == IS_CONSTANT);
  487. }
  488. // Return whether this is a common symbol.
  489. bool
  490. is_common() const
  491. {
  492. if (this->source_ != FROM_OBJECT)
  493. return false;
  494. bool is_ordinary;
  495. unsigned int shndx = this->shndx(&is_ordinary);
  496. return !is_ordinary && Symbol::is_common_shndx(shndx);
  497. }
  498. // Return whether this symbol can be seen outside this object.
  499. bool
  500. is_externally_visible() const
  501. {
  502. return ((this->visibility_ == elfcpp::STV_DEFAULT
  503. || this->visibility_ == elfcpp::STV_PROTECTED)
  504. && !this->is_forced_local_);
  505. }
  506. // Return true if this symbol can be preempted by a definition in
  507. // another link unit.
  508. bool
  509. is_preemptible() const
  510. {
  511. // It doesn't make sense to ask whether a symbol defined in
  512. // another object is preemptible.
  513. gold_assert(!this->is_from_dynobj());
  514. // It doesn't make sense to ask whether an undefined symbol
  515. // is preemptible.
  516. gold_assert(!this->is_undefined());
  517. // If a symbol does not have default visibility, it can not be
  518. // seen outside this link unit and therefore is not preemptible.
  519. if (this->visibility_ != elfcpp::STV_DEFAULT)
  520. return false;
  521. // If this symbol has been forced to be a local symbol by a
  522. // version script, then it is not visible outside this link unit
  523. // and is not preemptible.
  524. if (this->is_forced_local_)
  525. return false;
  526. // If we are not producing a shared library, then nothing is
  527. // preemptible.
  528. if (!parameters->options().shared())
  529. return false;
  530. // If the symbol was named in a --dynamic-list script, it is preemptible.
  531. if (parameters->options().in_dynamic_list(this->name()))
  532. return true;
  533. // If the user used -Bsymbolic, then nothing (else) is preemptible.
  534. if (parameters->options().Bsymbolic())
  535. return false;
  536. // If the user used -Bsymbolic-functions, then functions are not
  537. // preemptible. We explicitly check for not being STT_OBJECT,
  538. // rather than for being STT_FUNC, because that is what the GNU
  539. // linker does.
  540. if (this->type() != elfcpp::STT_OBJECT
  541. && parameters->options().Bsymbolic_functions())
  542. return false;
  543. // Otherwise the symbol is preemptible.
  544. return true;
  545. }
  546. // Return true if this symbol is a function that needs a PLT entry.
  547. bool
  548. needs_plt_entry() const
  549. {
  550. // An undefined symbol from an executable does not need a PLT entry.
  551. if (this->is_undefined() && !parameters->options().shared())
  552. return false;
  553. // An STT_GNU_IFUNC symbol always needs a PLT entry, even when
  554. // doing a static link.
  555. if (this->type() == elfcpp::STT_GNU_IFUNC)
  556. return true;
  557. // We only need a PLT entry for a function.
  558. if (!this->is_func())
  559. return false;
  560. // If we're doing a static link or a -pie link, we don't create
  561. // PLT entries.
  562. if (parameters->doing_static_link()
  563. || parameters->options().pie())
  564. return false;
  565. // We need a PLT entry if the function is defined in a dynamic
  566. // object, or is undefined when building a shared object, or if it
  567. // is subject to pre-emption.
  568. return (this->is_from_dynobj()
  569. || this->is_undefined()
  570. || this->is_preemptible());
  571. }
  572. // When determining whether a reference to a symbol needs a dynamic
  573. // relocation, we need to know several things about the reference.
  574. // These flags may be or'ed together. 0 means that the symbol
  575. // isn't referenced at all.
  576. enum Reference_flags
  577. {
  578. // A reference to the symbol's absolute address. This includes
  579. // references that cause an absolute address to be stored in the GOT.
  580. ABSOLUTE_REF = 1,
  581. // A reference that calculates the offset of the symbol from some
  582. // anchor point, such as the PC or GOT.
  583. RELATIVE_REF = 2,
  584. // A TLS-related reference.
  585. TLS_REF = 4,
  586. // A reference that can always be treated as a function call.
  587. FUNCTION_CALL = 8,
  588. // When set, says that dynamic relocations are needed even if a
  589. // symbol has a plt entry.
  590. FUNC_DESC_ABI = 16,
  591. };
  592. // Given a direct absolute or pc-relative static relocation against
  593. // the global symbol, this function returns whether a dynamic relocation
  594. // is needed.
  595. bool
  596. needs_dynamic_reloc(int flags) const
  597. {
  598. // No dynamic relocations in a static link!
  599. if (parameters->doing_static_link())
  600. return false;
  601. // A reference to an undefined symbol from an executable should be
  602. // statically resolved to 0, and does not need a dynamic relocation.
  603. // This matches gnu ld behavior.
  604. if (this->is_undefined() && !parameters->options().shared())
  605. return false;
  606. // A reference to an absolute symbol does not need a dynamic relocation.
  607. if (this->is_absolute())
  608. return false;
  609. // An absolute reference within a position-independent output file
  610. // will need a dynamic relocation.
  611. if ((flags & ABSOLUTE_REF)
  612. && parameters->options().output_is_position_independent())
  613. return true;
  614. // A function call that can branch to a local PLT entry does not need
  615. // a dynamic relocation.
  616. if ((flags & FUNCTION_CALL) && this->has_plt_offset())
  617. return false;
  618. // A reference to any PLT entry in a non-position-independent executable
  619. // does not need a dynamic relocation.
  620. if (!(flags & FUNC_DESC_ABI)
  621. && !parameters->options().output_is_position_independent()
  622. && this->has_plt_offset())
  623. return false;
  624. // A reference to a symbol defined in a dynamic object or to a
  625. // symbol that is preemptible will need a dynamic relocation.
  626. if (this->is_from_dynobj()
  627. || this->is_undefined()
  628. || this->is_preemptible())
  629. return true;
  630. // For all other cases, return FALSE.
  631. return false;
  632. }
  633. // Whether we should use the PLT offset associated with a symbol for
  634. // a relocation. FLAGS is a set of Reference_flags.
  635. bool
  636. use_plt_offset(int flags) const
  637. {
  638. // If the symbol doesn't have a PLT offset, then naturally we
  639. // don't want to use it.
  640. if (!this->has_plt_offset())
  641. return false;
  642. // For a STT_GNU_IFUNC symbol we always have to use the PLT entry.
  643. if (this->type() == elfcpp::STT_GNU_IFUNC)
  644. return true;
  645. // If we are going to generate a dynamic relocation, then we will
  646. // wind up using that, so no need to use the PLT entry.
  647. if (this->needs_dynamic_reloc(flags))
  648. return false;
  649. // If the symbol is from a dynamic object, we need to use the PLT
  650. // entry.
  651. if (this->is_from_dynobj())
  652. return true;
  653. // If we are generating a shared object, and this symbol is
  654. // undefined or preemptible, we need to use the PLT entry.
  655. if (parameters->options().shared()
  656. && (this->is_undefined() || this->is_preemptible()))
  657. return true;
  658. // If this is a call to a weak undefined symbol, we need to use
  659. // the PLT entry; the symbol may be defined by a library loaded
  660. // at runtime.
  661. if ((flags & FUNCTION_CALL) && this->is_weak_undefined())
  662. return true;
  663. // Otherwise we can use the regular definition.
  664. return false;
  665. }
  666. // Given a direct absolute static relocation against
  667. // the global symbol, where a dynamic relocation is needed, this
  668. // function returns whether a relative dynamic relocation can be used.
  669. // The caller must determine separately whether the static relocation
  670. // is compatible with a relative relocation.
  671. bool
  672. can_use_relative_reloc(bool is_function_call) const
  673. {
  674. // A function call that can branch to a local PLT entry can
  675. // use a RELATIVE relocation.
  676. if (is_function_call && this->has_plt_offset())
  677. return true;
  678. // A reference to a symbol defined in a dynamic object or to a
  679. // symbol that is preemptible can not use a RELATIVE relocation.
  680. if (this->is_from_dynobj()
  681. || this->is_undefined()
  682. || this->is_preemptible())
  683. return false;
  684. // For all other cases, return TRUE.
  685. return true;
  686. }
  687. // Return the output section where this symbol is defined. Return
  688. // NULL if the symbol has an absolute value.
  689. Output_section*
  690. output_section() const;
  691. // Set the symbol's output section. This is used for symbols
  692. // defined in scripts. This should only be called after the symbol
  693. // table has been finalized.
  694. void
  695. set_output_section(Output_section*);
  696. // Set the symbol's output segment. This is used for pre-defined
  697. // symbols whose segments aren't known until after layout is done
  698. // (e.g., __ehdr_start).
  699. void
  700. set_output_segment(Output_segment*, Segment_offset_base);
  701. // Set the symbol to undefined. This is used for pre-defined
  702. // symbols whose segments aren't known until after layout is done
  703. // (e.g., __ehdr_start).
  704. void
  705. set_undefined();
  706. // Return whether there should be a warning for references to this
  707. // symbol.
  708. bool
  709. has_warning() const
  710. { return this->has_warning_; }
  711. // Mark this symbol as having a warning.
  712. void
  713. set_has_warning()
  714. { this->has_warning_ = true; }
  715. // Return whether this symbol is defined by a COPY reloc from a
  716. // dynamic object.
  717. bool
  718. is_copied_from_dynobj() const
  719. { return this->is_copied_from_dynobj_; }
  720. // Mark this symbol as defined by a COPY reloc.
  721. void
  722. set_is_copied_from_dynobj()
  723. { this->is_copied_from_dynobj_ = true; }
  724. // Return whether this symbol is forced to visibility STB_LOCAL
  725. // by a "local:" entry in a version script.
  726. bool
  727. is_forced_local() const
  728. { return this->is_forced_local_; }
  729. // Mark this symbol as forced to STB_LOCAL visibility.
  730. void
  731. set_is_forced_local()
  732. { this->is_forced_local_ = true; }
  733. // Return true if this may need a COPY relocation.
  734. // References from an executable object to non-function symbols
  735. // defined in a dynamic object may need a COPY relocation.
  736. bool
  737. may_need_copy_reloc() const
  738. {
  739. return (parameters->options().copyreloc()
  740. && this->is_from_dynobj()
  741. && !this->is_func());
  742. }
  743. // Return true if this symbol was predefined by the linker.
  744. bool
  745. is_predefined() const
  746. { return this->is_predefined_; }
  747. // Return true if this is a C++ vtable symbol.
  748. bool
  749. is_cxx_vtable() const
  750. { return is_prefix_of("_ZTV", this->name_); }
  751. // Return true if this symbol is protected in a shared object.
  752. // This is not the same as checking if visibility() == elfcpp::STV_PROTECTED,
  753. // because the visibility_ field reflects the symbol's visibility from
  754. // outside the shared object.
  755. bool
  756. is_protected() const
  757. { return this->is_protected_; }
  758. // Mark this symbol as protected in a shared object.
  759. void
  760. set_is_protected()
  761. { this->is_protected_ = true; }
  762. // Return state of PowerPC64 ELFv2 specific flag.
  763. bool
  764. non_zero_localentry() const
  765. { return this->non_zero_localentry_; }
  766. // Set PowerPC64 ELFv2 specific flag.
  767. void
  768. set_non_zero_localentry()
  769. { this->non_zero_localentry_ = true; }
  770. // Completely override existing symbol. Everything bar name_,
  771. // version_, and is_forced_local_ flag are copied. version_ is
  772. // cleared if from->version_ is clear. Returns true if this symbol
  773. // should be forced local.
  774. bool
  775. clone(const Symbol* from);
  776. protected:
  777. // Instances of this class should always be created at a specific
  778. // size.
  779. Symbol()
  780. { memset(static_cast<void*>(this), 0, sizeof *this); }
  781. // Initialize the general fields.
  782. void
  783. init_fields(const char* name, const char* version,
  784. elfcpp::STT type, elfcpp::STB binding,
  785. elfcpp::STV visibility, unsigned char nonvis);
  786. // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
  787. // section index, IS_ORDINARY is whether it is a normal section
  788. // index rather than a special code.
  789. template<int size, bool big_endian>
  790. void
  791. init_base_object(const char* name, const char* version, Object* object,
  792. const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
  793. bool is_ordinary);
  794. // Initialize fields for an Output_data.
  795. void
  796. init_base_output_data(const char* name, const char* version, Output_data*,
  797. elfcpp::STT, elfcpp::STB, elfcpp::STV,
  798. unsigned char nonvis, bool offset_is_from_end,
  799. bool is_predefined);
  800. // Initialize fields for an Output_segment.
  801. void
  802. init_base_output_segment(const char* name, const char* version,
  803. Output_segment* os, elfcpp::STT type,
  804. elfcpp::STB binding, elfcpp::STV visibility,
  805. unsigned char nonvis,
  806. Segment_offset_base offset_base,
  807. bool is_predefined);
  808. // Initialize fields for a constant.
  809. void
  810. init_base_constant(const char* name, const char* version, elfcpp::STT type,
  811. elfcpp::STB binding, elfcpp::STV visibility,
  812. unsigned char nonvis, bool is_predefined);
  813. // Initialize fields for an undefined symbol.
  814. void
  815. init_base_undefined(const char* name, const char* version, elfcpp::STT type,
  816. elfcpp::STB binding, elfcpp::STV visibility,
  817. unsigned char nonvis);
  818. // Override existing symbol.
  819. template<int size, bool big_endian>
  820. void
  821. override_base(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
  822. bool is_ordinary, Object* object, const char* version);
  823. // Override existing symbol with a special symbol.
  824. void
  825. override_base_with_special(const Symbol* from);
  826. // Override symbol version.
  827. void
  828. override_version(const char* version);
  829. // Allocate a common symbol by giving it a location in the output
  830. // file.
  831. void
  832. allocate_base_common(Output_data*);
  833. private:
  834. Symbol(const Symbol&);
  835. Symbol& operator=(const Symbol&);
  836. // Symbol name (expected to point into a Stringpool).
  837. const char* name_;
  838. // Symbol version (expected to point into a Stringpool). This may
  839. // be NULL.
  840. const char* version_;
  841. union
  842. {
  843. // This is used if SOURCE_ == FROM_OBJECT.
  844. // Object in which symbol is defined, or in which it was first
  845. // seen.
  846. Object* object;
  847. // This is used if SOURCE_ == IN_OUTPUT_DATA.
  848. // Output_data in which symbol is defined. Before
  849. // Layout::finalize the symbol's value is an offset within the
  850. // Output_data.
  851. Output_data* output_data;
  852. // This is used if SOURCE_ == IN_OUTPUT_SEGMENT.
  853. // Output_segment in which the symbol is defined. Before
  854. // Layout::finalize the symbol's value is an offset.
  855. Output_segment* output_segment;
  856. } u1_;
  857. union
  858. {
  859. // This is used if SOURCE_ == FROM_OBJECT.
  860. // Section number in object in which symbol is defined.
  861. unsigned int shndx;
  862. // This is used if SOURCE_ == IN_OUTPUT_DATA.
  863. // True if the offset is from the end, false if the offset is
  864. // from the beginning.
  865. bool offset_is_from_end;
  866. // This is used if SOURCE_ == IN_OUTPUT_SEGMENT.
  867. // The base to use for the offset before Layout::finalize.
  868. Segment_offset_base offset_base;
  869. } u2_;
  870. // The index of this symbol in the output file. If the symbol is
  871. // not going into the output file, this value is -1U. This field
  872. // starts as always holding zero. It is set to a non-zero value by
  873. // Symbol_table::finalize.
  874. unsigned int symtab_index_;
  875. // The index of this symbol in the dynamic symbol table. If the
  876. // symbol is not going into the dynamic symbol table, this value is
  877. // -1U. This field starts as always holding zero. It is set to a
  878. // non-zero value during Layout::finalize.
  879. unsigned int dynsym_index_;
  880. // If this symbol has an entry in the PLT section, then this is the
  881. // offset from the start of the PLT section. This is -1U if there
  882. // is no PLT entry.
  883. unsigned int plt_offset_;
  884. // The GOT section entries for this symbol. A symbol may have more
  885. // than one GOT offset (e.g., when mixing modules compiled with two
  886. // different TLS models), but will usually have at most one.
  887. Got_offset_list got_offsets_;
  888. // Symbol type (bits 0 to 3).
  889. elfcpp::STT type_ : 4;
  890. // Symbol binding (bits 4 to 7).
  891. elfcpp::STB binding_ : 4;
  892. // Symbol visibility (bits 8 to 9).
  893. elfcpp::STV visibility_ : 2;
  894. // Rest of symbol st_other field (bits 10 to 15).
  895. unsigned int nonvis_ : 6;
  896. // The type of symbol (bits 16 to 18).
  897. Source source_ : 3;
  898. // True if this is the default version of the symbol (bit 19).
  899. bool is_def_ : 1;
  900. // True if this symbol really forwards to another symbol. This is
  901. // used when we discover after the fact that two different entries
  902. // in the hash table really refer to the same symbol. This will
  903. // never be set for a symbol found in the hash table, but may be set
  904. // for a symbol found in the list of symbols attached to an Object.
  905. // It forwards to the symbol found in the forwarders_ map of
  906. // Symbol_table (bit 20).
  907. bool is_forwarder_ : 1;
  908. // True if the symbol has an alias in the weak_aliases table in
  909. // Symbol_table (bit 21).
  910. bool has_alias_ : 1;
  911. // True if this symbol needs to be in the dynamic symbol table (bit
  912. // 22).
  913. bool needs_dynsym_entry_ : 1;
  914. // True if we've seen this symbol in a regular object (bit 23).
  915. bool in_reg_ : 1;
  916. // True if we've seen this symbol in a dynamic object (bit 24).
  917. bool in_dyn_ : 1;
  918. // True if this is a dynamic symbol which needs a special value in
  919. // the dynamic symbol table (bit 25).
  920. bool needs_dynsym_value_ : 1;
  921. // True if there is a warning for this symbol (bit 26).
  922. bool has_warning_ : 1;
  923. // True if we are using a COPY reloc for this symbol, so that the
  924. // real definition lives in a dynamic object (bit 27).
  925. bool is_copied_from_dynobj_ : 1;
  926. // True if this symbol was forced to local visibility by a version
  927. // script (bit 28).
  928. bool is_forced_local_ : 1;
  929. // True if the field u2_.shndx is an ordinary section
  930. // index, not one of the special codes from SHN_LORESERVE to
  931. // SHN_HIRESERVE (bit 29).
  932. bool is_ordinary_shndx_ : 1;
  933. // True if we've seen this symbol in a "real" ELF object (bit 30).
  934. // If the symbol has been seen in a relocatable, non-IR, object file,
  935. // it's known to be referenced from outside the IR. A reference from
  936. // a dynamic object doesn't count as a "real" ELF, and we'll simply
  937. // mark the symbol as "visible" from outside the IR. The compiler
  938. // can use this distinction to guide its handling of COMDAT symbols.
  939. bool in_real_elf_ : 1;
  940. // True if this symbol is defined in a section which was discarded
  941. // (bit 31).
  942. bool is_defined_in_discarded_section_ : 1;
  943. // True if UNDEF_BINDING_WEAK_ has been set (bit 32).
  944. bool undef_binding_set_ : 1;
  945. // True if this symbol was a weak undef resolved by a dynamic def
  946. // or by a special symbol (bit 33).
  947. bool undef_binding_weak_ : 1;
  948. // True if this symbol is a predefined linker symbol (bit 34).
  949. bool is_predefined_ : 1;
  950. // True if this symbol has protected visibility in a shared object (bit 35).
  951. // The visibility_ field will be STV_DEFAULT in this case because we
  952. // must treat it as such from outside the shared object.
  953. bool is_protected_ : 1;
  954. // Used by PowerPC64 ELFv2 to track st_other localentry (bit 36).
  955. bool non_zero_localentry_ : 1;
  956. };
  957. // The parts of a symbol which are size specific. Using a template
  958. // derived class like this helps us use less space on a 32-bit system.
  959. template<int size>
  960. class Sized_symbol : public Symbol
  961. {
  962. public:
  963. typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
  964. typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
  965. Sized_symbol()
  966. { }
  967. // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
  968. // section index, IS_ORDINARY is whether it is a normal section
  969. // index rather than a special code.
  970. template<bool big_endian>
  971. void
  972. init_object(const char* name, const char* version, Object* object,
  973. const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
  974. bool is_ordinary);
  975. // Initialize fields for an Output_data.
  976. void
  977. init_output_data(const char* name, const char* version, Output_data*,
  978. Value_type value, Size_type symsize, elfcpp::STT,
  979. elfcpp::STB, elfcpp::STV, unsigned char nonvis,
  980. bool offset_is_from_end, bool is_predefined);
  981. // Initialize fields for an Output_segment.
  982. void
  983. init_output_segment(const char* name, const char* version, Output_segment*,
  984. Value_type value, Size_type symsize, elfcpp::STT,
  985. elfcpp::STB, elfcpp::STV, unsigned char nonvis,
  986. Segment_offset_base offset_base, bool is_predefined);
  987. // Initialize fields for a constant.
  988. void
  989. init_constant(const char* name, const char* version, Value_type value,
  990. Size_type symsize, elfcpp::STT, elfcpp::STB, elfcpp::STV,
  991. unsigned char nonvis, bool is_predefined);
  992. // Initialize fields for an undefined symbol.
  993. void
  994. init_undefined(const char* name, const char* version, Value_type value,
  995. elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
  996. // Override existing symbol.
  997. template<bool big_endian>
  998. void
  999. override(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
  1000. bool is_ordinary, Object* object, const char* version);
  1001. // Override existing symbol with a special symbol.
  1002. void
  1003. override_with_special(const Sized_symbol<size>*);
  1004. // Return the symbol's value.
  1005. Value_type
  1006. value() const
  1007. { return this->value_; }
  1008. // Return the symbol's size (we can't call this 'size' because that
  1009. // is a template parameter).
  1010. Size_type
  1011. symsize() const
  1012. { return this->symsize_; }
  1013. // Set the symbol size. This is used when resolving common symbols.
  1014. void
  1015. set_symsize(Size_type symsize)
  1016. { this->symsize_ = symsize; }
  1017. // Set the symbol value. This is called when we store the final
  1018. // values of the symbols into the symbol table.
  1019. void
  1020. set_value(Value_type value)
  1021. { this->value_ = value; }
  1022. // Allocate a common symbol by giving it a location in the output
  1023. // file.
  1024. void
  1025. allocate_common(Output_data*, Value_type value);
  1026. // Completely override existing symbol. Everything bar name_,
  1027. // version_, and is_forced_local_ flag are copied. version_ is
  1028. // cleared if from->version_ is clear. Returns true if this symbol
  1029. // should be forced local.
  1030. bool
  1031. clone(const Sized_symbol<size>* from);
  1032. private:
  1033. Sized_symbol(const Sized_symbol&);
  1034. Sized_symbol& operator=(const Sized_symbol&);
  1035. // Symbol value. Before Layout::finalize this is the offset in the
  1036. // input section. This is set to the final value during
  1037. // Layout::finalize.
  1038. Value_type value_;
  1039. // Symbol size.
  1040. Size_type symsize_;
  1041. };
  1042. // A struct describing a symbol defined by the linker, where the value
  1043. // of the symbol is defined based on an output section. This is used
  1044. // for symbols defined by the linker, like "_init_array_start".
  1045. struct Define_symbol_in_section
  1046. {
  1047. // The symbol name.
  1048. const char* name;
  1049. // The name of the output section with which this symbol should be
  1050. // associated. If there is no output section with that name, the
  1051. // symbol will be defined as zero.
  1052. const char* output_section;
  1053. // The offset of the symbol within the output section. This is an
  1054. // offset from the start of the output section, unless start_at_end
  1055. // is true, in which case this is an offset from the end of the
  1056. // output section.
  1057. uint64_t value;
  1058. // The size of the symbol.
  1059. uint64_t size;
  1060. // The symbol type.
  1061. elfcpp::STT type;
  1062. // The symbol binding.
  1063. elfcpp::STB binding;
  1064. // The symbol visibility.
  1065. elfcpp::STV visibility;
  1066. // The rest of the st_other field.
  1067. unsigned char nonvis;
  1068. // If true, the value field is an offset from the end of the output
  1069. // section.
  1070. bool offset_is_from_end;
  1071. // If true, this symbol is defined only if we see a reference to it.
  1072. bool only_if_ref;
  1073. };
  1074. // A struct describing a symbol defined by the linker, where the value
  1075. // of the symbol is defined based on a segment. This is used for
  1076. // symbols defined by the linker, like "_end". We describe the
  1077. // segment with which the symbol should be associated by its
  1078. // characteristics. If no segment meets these characteristics, the
  1079. // symbol will be defined as zero. If there is more than one segment
  1080. // which meets these characteristics, we will use the first one.
  1081. struct Define_symbol_in_segment
  1082. {
  1083. // The symbol name.
  1084. const char* name;
  1085. // The segment type where the symbol should be defined, typically
  1086. // PT_LOAD.
  1087. elfcpp::PT segment_type;
  1088. // Bitmask of segment flags which must be set.
  1089. elfcpp::PF segment_flags_set;
  1090. // Bitmask of segment flags which must be clear.
  1091. elfcpp::PF segment_flags_clear;
  1092. // The offset of the symbol within the segment. The offset is
  1093. // calculated from the position set by offset_base.
  1094. uint64_t value;
  1095. // The size of the symbol.
  1096. uint64_t size;
  1097. // The symbol type.
  1098. elfcpp::STT type;
  1099. // The symbol binding.
  1100. elfcpp::STB binding;
  1101. // The symbol visibility.
  1102. elfcpp::STV visibility;
  1103. // The rest of the st_other field.
  1104. unsigned char nonvis;
  1105. // The base from which we compute the offset.
  1106. Symbol::Segment_offset_base offset_base;
  1107. // If true, this symbol is defined only if we see a reference to it.
  1108. bool only_if_ref;
  1109. };
  1110. // Specify an object/section/offset location. Used by ODR code.
  1111. struct Symbol_location
  1112. {
  1113. // Object where the symbol is defined.
  1114. Object* object;
  1115. // Section-in-object where the symbol is defined.
  1116. unsigned int shndx;
  1117. // For relocatable objects, offset-in-section where the symbol is defined.
  1118. // For dynamic objects, address where the symbol is defined.
  1119. off_t offset;
  1120. bool operator==(const Symbol_location& that) const
  1121. {
  1122. return (this->object == that.object
  1123. && this->shndx == that.shndx
  1124. && this->offset == that.offset);
  1125. }
  1126. };
  1127. // A map from symbol name (as a pointer into the namepool) to all
  1128. // the locations the symbols is (weakly) defined (and certain other
  1129. // conditions are met). This map will be used later to detect
  1130. // possible One Definition Rule (ODR) violations.
  1131. struct Symbol_location_hash
  1132. {
  1133. size_t operator()(const Symbol_location& loc) const
  1134. { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
  1135. };
  1136. // This class manages warnings. Warnings are a GNU extension. When
  1137. // we see a section named .gnu.warning.SYM in an object file, and if
  1138. // we wind using the definition of SYM from that object file, then we
  1139. // will issue a warning for any relocation against SYM from a
  1140. // different object file. The text of the warning is the contents of
  1141. // the section. This is not precisely the definition used by the old
  1142. // GNU linker; the old GNU linker treated an occurrence of
  1143. // .gnu.warning.SYM as defining a warning symbol. A warning symbol
  1144. // would trigger a warning on any reference. However, it was
  1145. // inconsistent in that a warning in a dynamic object only triggered
  1146. // if there was no definition in a regular object. This linker is
  1147. // different in that we only issue a warning if we use the symbol
  1148. // definition from the same object file as the warning section.
  1149. class Warnings
  1150. {
  1151. public:
  1152. Warnings()
  1153. : warnings_()
  1154. { }
  1155. // Add a warning for symbol NAME in object OBJ. WARNING is the text
  1156. // of the warning.
  1157. void
  1158. add_warning(Symbol_table* symtab, const char* name, Object* obj,
  1159. const std::string& warning);
  1160. // For each symbol for which we should give a warning, make a note
  1161. // on the symbol.
  1162. void
  1163. note_warnings(Symbol_table* symtab);
  1164. // Issue a warning for a reference to SYM at RELINFO's location.
  1165. template<int size, bool big_endian>
  1166. void
  1167. issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
  1168. size_t relnum, off_t reloffset) const;
  1169. private:
  1170. Warnings(const Warnings&);
  1171. Warnings& operator=(const Warnings&);
  1172. // What we need to know to get the warning text.
  1173. struct Warning_location
  1174. {
  1175. // The object the warning is in.
  1176. Object* object;
  1177. // The warning text.
  1178. std::string text;
  1179. Warning_location()
  1180. : object(NULL), text()
  1181. { }
  1182. void
  1183. set(Object* o, const std::string& t)
  1184. {
  1185. this->object = o;
  1186. this->text = t;
  1187. }
  1188. };
  1189. // A mapping from warning symbol names (canonicalized in
  1190. // Symbol_table's namepool_ field) to warning information.
  1191. typedef Unordered_map<const char*, Warning_location> Warning_table;
  1192. Warning_table warnings_;
  1193. };
  1194. // The main linker symbol table.
  1195. class Symbol_table
  1196. {
  1197. public:
  1198. // The different places where a symbol definition can come from.
  1199. enum Defined
  1200. {
  1201. // Defined in an object file--the normal case.
  1202. OBJECT,
  1203. // Defined for a COPY reloc.
  1204. COPY,
  1205. // Defined on the command line using --defsym.
  1206. DEFSYM,
  1207. // Defined (so to speak) on the command line using -u.
  1208. UNDEFINED,
  1209. // Defined in a linker script.
  1210. SCRIPT,
  1211. // Predefined by the linker.
  1212. PREDEFINED,
  1213. // Defined by the linker during an incremental base link, but not
  1214. // a predefined symbol (e.g., common, defined in script).
  1215. INCREMENTAL_BASE,
  1216. };
  1217. // The order in which we sort common symbols.
  1218. enum Sort_commons_order
  1219. {
  1220. SORT_COMMONS_BY_SIZE_DESCENDING,
  1221. SORT_COMMONS_BY_ALIGNMENT_DESCENDING,
  1222. SORT_COMMONS_BY_ALIGNMENT_ASCENDING
  1223. };
  1224. // COUNT is an estimate of how many symbols will be inserted in the
  1225. // symbol table. It's ok to put 0 if you don't know; a correct
  1226. // guess will just save some CPU by reducing hashtable resizes.
  1227. Symbol_table(unsigned int count, const Version_script_info& version_script);
  1228. ~Symbol_table();
  1229. void
  1230. set_icf(Icf* icf)
  1231. { this->icf_ = icf;}
  1232. Icf*
  1233. icf() const
  1234. { return this->icf_; }
  1235. // Returns true if ICF determined that this is a duplicate section.
  1236. bool
  1237. is_section_folded(Relobj* obj, unsigned int shndx) const;
  1238. void
  1239. set_gc(Garbage_collection* gc)
  1240. { this->gc_ = gc; }
  1241. Garbage_collection*
  1242. gc() const
  1243. { return this->gc_; }
  1244. // During garbage collection, this keeps undefined symbols.
  1245. void
  1246. gc_mark_undef_symbols(Layout*);
  1247. // This tells garbage collection that this symbol is referenced.
  1248. void
  1249. gc_mark_symbol(Symbol* sym);
  1250. // During garbage collection, this keeps sections that correspond to
  1251. // symbols seen in dynamic objects.
  1252. inline void
  1253. gc_mark_dyn_syms(Symbol* sym);
  1254. // Add COUNT external symbols from the relocatable object RELOBJ to
  1255. // the symbol table. SYMS is the symbols, SYMNDX_OFFSET is the
  1256. // offset in the symbol table of the first symbol, SYM_NAMES is
  1257. // their names, SYM_NAME_SIZE is the size of SYM_NAMES. This sets
  1258. // SYMPOINTERS to point to the symbols in the symbol table. It sets
  1259. // *DEFINED to the number of defined symbols.
  1260. template<int size, bool big_endian>
  1261. void
  1262. add_from_relobj(Sized_relobj_file<size, big_endian>* relobj,
  1263. const unsigned char* syms, size_t count,
  1264. size_t symndx_offset, const char* sym_names,
  1265. size_t sym_name_size,
  1266. typename Sized_relobj_file<size, big_endian>::Symbols*,
  1267. size_t* defined);
  1268. // Add one external symbol from the plugin object OBJ to the symbol table.
  1269. // Returns a pointer to the resolved symbol in the symbol table.
  1270. template<int size, bool big_endian>
  1271. Symbol*
  1272. add_from_pluginobj(Sized_pluginobj<size, big_endian>* obj,
  1273. const char* name, const char* ver,
  1274. elfcpp::Sym<size, big_endian>* sym);
  1275. // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
  1276. // symbol table. SYMS is the symbols. SYM_NAMES is their names.
  1277. // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
  1278. // symbol version data.
  1279. template<int size, bool big_endian>
  1280. void
  1281. add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
  1282. const unsigned char* syms, size_t count,
  1283. const char* sym_names, size_t sym_name_size,
  1284. const unsigned char* versym, size_t versym_size,
  1285. const std::vector<const char*>*,
  1286. typename Sized_relobj_file<size, big_endian>::Symbols*,
  1287. size_t* defined);
  1288. // Add one external symbol from the incremental object OBJ to the symbol
  1289. // table. Returns a pointer to the resolved symbol in the symbol table.
  1290. template<int size, bool big_endian>
  1291. Sized_symbol<size>*
  1292. add_from_incrobj(Object* obj, const char* name,
  1293. const char* ver, elfcpp::Sym<size, big_endian>* sym);
  1294. // Define a special symbol based on an Output_data. It is a
  1295. // multiple definition error if this symbol is already defined.
  1296. Symbol*
  1297. define_in_output_data(const char* name, const char* version, Defined,
  1298. Output_data*, uint64_t value, uint64_t symsize,
  1299. elfcpp::STT type, elfcpp::STB binding,
  1300. elfcpp::STV visibility, unsigned char nonvis,
  1301. bool offset_is_from_end, bool only_if_ref);
  1302. // Define a special symbol based on an Output_segment. It is a
  1303. // multiple definition error if this symbol is already defined.
  1304. Symbol*
  1305. define_in_output_segment(const char* name, const char* version, Defined,
  1306. Output_segment*, uint64_t value, uint64_t symsize,
  1307. elfcpp::STT type, elfcpp::STB binding,
  1308. elfcpp::STV visibility, unsigned char nonvis,
  1309. Symbol::Segment_offset_base, bool only_if_ref);
  1310. // Define a special symbol with a constant value. It is a multiple
  1311. // definition error if this symbol is already defined.
  1312. Symbol*
  1313. define_as_constant(const char* name, const char* version, Defined,
  1314. uint64_t value, uint64_t symsize, elfcpp::STT type,
  1315. elfcpp::STB binding, elfcpp::STV visibility,
  1316. unsigned char nonvis, bool only_if_ref,
  1317. bool force_override);
  1318. // Define a set of symbols in output sections. If ONLY_IF_REF is
  1319. // true, only define them if they are referenced.
  1320. void
  1321. define_symbols(const Layout*, int count, const Define_symbol_in_section*,
  1322. bool only_if_ref);
  1323. // Define a set of symbols in output segments. If ONLY_IF_REF is
  1324. // true, only defined them if they are referenced.
  1325. void
  1326. define_symbols(const Layout*, int count, const Define_symbol_in_segment*,
  1327. bool only_if_ref);
  1328. // Add a target-specific global symbol.
  1329. // (Used by SPARC backend to add STT_SPARC_REGISTER symbols.)
  1330. void
  1331. add_target_global_symbol(Symbol* sym)
  1332. { this->target_symbols_.push_back(sym); }
  1333. // Define SYM using a COPY reloc. POSD is the Output_data where the
  1334. // symbol should be defined--typically a .dyn.bss section. VALUE is
  1335. // the offset within POSD.
  1336. template<int size>
  1337. void
  1338. define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
  1339. typename elfcpp::Elf_types<size>::Elf_Addr);
  1340. // Look up a symbol.
  1341. Symbol*
  1342. lookup(const char*, const char* version = NULL) const;
  1343. // Return the real symbol associated with the forwarder symbol FROM.
  1344. Symbol*
  1345. resolve_forwards(const Symbol* from) const;
  1346. // Return the sized version of a symbol in this table.
  1347. template<int size>
  1348. Sized_symbol<size>*
  1349. get_sized_symbol(Symbol*) const;
  1350. template<int size>
  1351. const Sized_symbol<size>*
  1352. get_sized_symbol(const Symbol*) const;
  1353. // Return the count of undefined symbols seen.
  1354. size_t
  1355. saw_undefined() const
  1356. { return this->saw_undefined_; }
  1357. void
  1358. set_has_gnu_output()
  1359. { this->has_gnu_output_ = true; }
  1360. // Allocate the common symbols
  1361. void
  1362. allocate_commons(Layout*, Mapfile*);
  1363. // Add a warning for symbol NAME in object OBJ. WARNING is the text
  1364. // of the warning.
  1365. void
  1366. add_warning(const char* name, Object* obj, const std::string& warning)
  1367. { this->warnings_.add_warning(this, name, obj, warning); }
  1368. // Canonicalize a symbol name for use in the hash table.
  1369. const char*
  1370. canonicalize_name(const char* name)
  1371. { return this->namepool_.add(name, true, NULL); }
  1372. // Possibly issue a warning for a reference to SYM at LOCATION which
  1373. // is in OBJ.
  1374. template<int size, bool big_endian>
  1375. void
  1376. issue_warning(const Symbol* sym,
  1377. const Relocate_info<size, big_endian>* relinfo,
  1378. size_t relnum, off_t reloffset) const
  1379. { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
  1380. // Check candidate_odr_violations_ to find symbols with the same name
  1381. // but apparently different definitions (different source-file/line-no).
  1382. void
  1383. detect_odr_violations(const Task*, const char* output_file_name) const;
  1384. // Add any undefined symbols named on the command line to the symbol
  1385. // table.
  1386. void
  1387. add_undefined_symbols_from_command_line(Layout*);
  1388. // SYM is defined using a COPY reloc. Return the dynamic object
  1389. // where the original definition was found.
  1390. Dynobj*
  1391. get_copy_source(const Symbol* sym) const;
  1392. // Set the dynamic symbol indexes. INDEX is the index of the first
  1393. // global dynamic symbol. Return the count of forced-local symbols in
  1394. // *PFORCED_LOCAL_COUNT. Pointers to the symbols are stored into
  1395. // the vector. The names are stored into the Stringpool. This
  1396. // returns an updated dynamic symbol index.
  1397. unsigned int
  1398. set_dynsym_indexes(unsigned int index, unsigned int* pforced_local_count,
  1399. std::vector<Symbol*>*, Stringpool*, Versions*);
  1400. // Finalize the symbol table after we have set the final addresses
  1401. // of all the input sections. This sets the final symbol indexes,
  1402. // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
  1403. // index of the first global symbol. OFF is the file offset of the
  1404. // global symbol table, DYNOFF is the offset of the globals in the
  1405. // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
  1406. // global dynamic symbol, and DYNCOUNT is the number of global
  1407. // dynamic symbols. This records the parameters, and returns the
  1408. // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
  1409. // local symbols.
  1410. off_t
  1411. finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
  1412. Stringpool* pool, unsigned int* plocal_symcount);
  1413. // Set the final file offset of the symbol table.
  1414. void
  1415. set_file_offset(off_t off)
  1416. { this->offset_ = off; }
  1417. // Status code of Symbol_table::compute_final_value.
  1418. enum Compute_final_value_status
  1419. {
  1420. // No error.
  1421. CFVS_OK,
  1422. // Unsupported symbol section.
  1423. CFVS_UNSUPPORTED_SYMBOL_SECTION,
  1424. // No output section.
  1425. CFVS_NO_OUTPUT_SECTION
  1426. };
  1427. // Compute the final value of SYM and store status in location PSTATUS.
  1428. // During relaxation, this may be called multiple times for a symbol to
  1429. // compute its would-be final value in each relaxation pass.
  1430. template<int size>
  1431. typename Sized_symbol<size>::Value_type
  1432. compute_final_value(const Sized_symbol<size>* sym,
  1433. Compute_final_value_status* pstatus) const;
  1434. // Return the index of the first global symbol.
  1435. unsigned int
  1436. first_global_index() const
  1437. { return this->first_global_index_; }
  1438. // Return the total number of symbols in the symbol table.
  1439. unsigned int
  1440. output_count() const
  1441. { return this->output_count_; }
  1442. // Write out the global symbols.
  1443. void
  1444. write_globals(const Stringpool*, const Stringpool*,
  1445. Output_symtab_xindex*, Output_symtab_xindex*,
  1446. Output_file*) const;
  1447. // Write out a section symbol. Return the updated offset.
  1448. void
  1449. write_section_symbol(const Output_section*, Output_symtab_xindex*,
  1450. Output_file*, off_t) const;
  1451. // Loop over all symbols, applying the function F to each.
  1452. template<int size, typename F>
  1453. void
  1454. for_all_symbols(F f) const
  1455. {
  1456. for (Symbol_table_type::const_iterator p = this->table_.begin();
  1457. p != this->table_.end();
  1458. ++p)
  1459. {
  1460. Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
  1461. f(sym);
  1462. }
  1463. }
  1464. // Dump statistical information to stderr.
  1465. void
  1466. print_stats() const;
  1467. // Return the version script information.
  1468. const Version_script_info&
  1469. version_script() const
  1470. { return version_script_; }
  1471. // Completely override existing symbol.
  1472. template<int size>
  1473. void
  1474. clone(Sized_symbol<size>* to, const Sized_symbol<size>* from)
  1475. {
  1476. if (to->clone(from))
  1477. this->force_local(to);
  1478. }
  1479. private:
  1480. Symbol_table(const Symbol_table&);
  1481. Symbol_table& operator=(const Symbol_table&);
  1482. // The type of the list of common symbols.
  1483. typedef std::vector<Symbol*> Commons_type;
  1484. // The type of the symbol hash table.
  1485. typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
  1486. // The hash function. The key values are Stringpool keys.
  1487. struct Symbol_table_hash
  1488. {
  1489. inline size_t
  1490. operator()(const Symbol_table_key& key) const
  1491. {
  1492. return key.first ^ key.second;
  1493. }
  1494. };
  1495. struct Symbol_table_eq
  1496. {
  1497. bool
  1498. operator()(const Symbol_table_key&, const Symbol_table_key&) const;
  1499. };
  1500. typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
  1501. Symbol_table_eq> Symbol_table_type;
  1502. typedef Unordered_map<const char*,
  1503. Unordered_set<Symbol_location, Symbol_location_hash> >
  1504. Odr_map;
  1505. // Make FROM a forwarder symbol to TO.
  1506. void
  1507. make_forwarder(Symbol* from, Symbol* to);
  1508. // Add a symbol.
  1509. template<int size, bool big_endian>
  1510. Sized_symbol<size>*
  1511. add_from_object(Object*, const char* name, Stringpool::Key name_key,
  1512. const char* version, Stringpool::Key version_key,
  1513. bool def, const elfcpp::Sym<size, big_endian>& sym,
  1514. unsigned int st_shndx, bool is_ordinary,
  1515. unsigned int orig_st_shndx);
  1516. // Define a default symbol.
  1517. template<int size, bool big_endian>
  1518. void
  1519. define_default_version(Sized_symbol<size>*, bool,
  1520. Symbol_table_type::iterator);
  1521. // Resolve symbols.
  1522. template<int size, bool big_endian>
  1523. void
  1524. resolve(Sized_symbol<size>* to,
  1525. const elfcpp::Sym<size, big_endian>& sym,
  1526. unsigned int st_shndx, bool is_ordinary,
  1527. unsigned int orig_st_shndx,
  1528. Object*, const char* version,
  1529. bool is_default_version);
  1530. template<int size, bool big_endian>
  1531. void
  1532. resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from);
  1533. // Record that a symbol is forced to be local by a version script or
  1534. // by visibility.
  1535. void
  1536. force_local(Symbol*);
  1537. // Adjust NAME and *NAME_KEY for wrapping.
  1538. const char*
  1539. wrap_symbol(const char* name, Stringpool::Key* name_key);
  1540. // Whether we should override a symbol, based on flags in
  1541. // resolve.cc.
  1542. static bool
  1543. should_override(const Symbol*, unsigned int, elfcpp::STT, Defined,
  1544. Object*, bool*, bool*, bool);
  1545. // Report a problem in symbol resolution.
  1546. static void
  1547. report_resolve_problem(bool is_error, const char* msg, const Symbol* to,
  1548. Defined, Object* object);
  1549. // Override a symbol.
  1550. template<int size, bool big_endian>
  1551. void
  1552. override(Sized_symbol<size>* tosym,
  1553. const elfcpp::Sym<size, big_endian>& fromsym,
  1554. unsigned int st_shndx, bool is_ordinary,
  1555. Object* object, const char* version);
  1556. // Whether we should override a symbol with a special symbol which
  1557. // is automatically defined by the linker.
  1558. static bool
  1559. should_override_with_special(const Symbol*, elfcpp::STT, Defined);
  1560. // Override a symbol with a special symbol.
  1561. template<int size>
  1562. void
  1563. override_with_special(Sized_symbol<size>* tosym,
  1564. const Sized_symbol<size>* fromsym);
  1565. // Record all weak alias sets for a dynamic object.
  1566. template<int size>
  1567. void
  1568. record_weak_aliases(std::vector<Sized_symbol<size>*>*);
  1569. // Define a special symbol.
  1570. template<int size, bool big_endian>
  1571. Sized_symbol<size>*
  1572. define_special_symbol(const char** pname, const char** pversion,
  1573. bool only_if_ref, elfcpp::STV visibility,
  1574. Sized_symbol<size>** poldsym,
  1575. bool* resolve_oldsym, bool is_forced_local);
  1576. // Define a symbol in an Output_data, sized version.
  1577. template<int size>
  1578. Sized_symbol<size>*
  1579. do_define_in_output_data(const char* name, const char* version, Defined,
  1580. Output_data*,
  1581. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1582. typename elfcpp::Elf_types<size>::Elf_WXword ssize,
  1583. elfcpp::STT type, elfcpp::STB binding,
  1584. elfcpp::STV visibility, unsigned char nonvis,
  1585. bool offset_is_from_end, bool only_if_ref);
  1586. // Define a symbol in an Output_segment, sized version.
  1587. template<int size>
  1588. Sized_symbol<size>*
  1589. do_define_in_output_segment(
  1590. const char* name, const char* version, Defined, Output_segment* os,
  1591. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1592. typename elfcpp::Elf_types<size>::Elf_WXword ssize,
  1593. elfcpp::STT type, elfcpp::STB binding,
  1594. elfcpp::STV visibility, unsigned char nonvis,
  1595. Symbol::Segment_offset_base offset_base, bool only_if_ref);
  1596. // Define a symbol as a constant, sized version.
  1597. template<int size>
  1598. Sized_symbol<size>*
  1599. do_define_as_constant(
  1600. const char* name, const char* version, Defined,
  1601. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1602. typename elfcpp::Elf_types<size>::Elf_WXword ssize,
  1603. elfcpp::STT type, elfcpp::STB binding,
  1604. elfcpp::STV visibility, unsigned char nonvis,
  1605. bool only_if_ref, bool force_override);
  1606. // Add any undefined symbols named on the command line to the symbol
  1607. // table, sized version.
  1608. template<int size>
  1609. void
  1610. do_add_undefined_symbols_from_command_line(Layout*);
  1611. // Add one undefined symbol.
  1612. template<int size>
  1613. void
  1614. add_undefined_symbol_from_command_line(const char* name);
  1615. // Types of common symbols.
  1616. enum Commons_section_type
  1617. {
  1618. COMMONS_NORMAL,
  1619. COMMONS_TLS,
  1620. COMMONS_SMALL,
  1621. COMMONS_LARGE
  1622. };
  1623. // Allocate the common symbols, sized version.
  1624. template<int size>
  1625. void
  1626. do_allocate_commons(Layout*, Mapfile*, Sort_commons_order);
  1627. // Allocate the common symbols from one list.
  1628. template<int size>
  1629. void
  1630. do_allocate_commons_list(Layout*, Commons_section_type, Commons_type*,
  1631. Mapfile*, Sort_commons_order);
  1632. // Returns all of the lines attached to LOC, not just the one the
  1633. // instruction actually came from. This helps the ODR checker avoid
  1634. // false positives.
  1635. static std::vector<std::string>
  1636. linenos_from_loc(const Task* task, const Symbol_location& loc);
  1637. // Implement detect_odr_violations.
  1638. template<int size, bool big_endian>
  1639. void
  1640. sized_detect_odr_violations() const;
  1641. // Finalize symbols specialized for size.
  1642. template<int size>
  1643. off_t
  1644. sized_finalize(off_t, Stringpool*, unsigned int*);
  1645. // Finalize a symbol. Return whether it should be added to the
  1646. // symbol table.
  1647. template<int size>
  1648. bool
  1649. sized_finalize_symbol(Symbol*);
  1650. // Add a symbol the final symtab by setting its index.
  1651. template<int size>
  1652. void
  1653. add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
  1654. // Write globals specialized for size and endianness.
  1655. template<int size, bool big_endian>
  1656. void
  1657. sized_write_globals(const Stringpool*, const Stringpool*,
  1658. Output_symtab_xindex*, Output_symtab_xindex*,
  1659. Output_file*) const;
  1660. // Write out a symbol to P.
  1661. template<int size, bool big_endian>
  1662. void
  1663. sized_write_symbol(Sized_symbol<size>*,
  1664. typename elfcpp::Elf_types<size>::Elf_Addr value,
  1665. unsigned int shndx, elfcpp::STB,
  1666. const Stringpool*, unsigned char* p) const;
  1667. // Possibly warn about an undefined symbol from a dynamic object.
  1668. void
  1669. warn_about_undefined_dynobj_symbol(Symbol*) const;
  1670. // Write out a section symbol, specialized for size and endianness.
  1671. template<int size, bool big_endian>
  1672. void
  1673. sized_write_section_symbol(const Output_section*, Output_symtab_xindex*,
  1674. Output_file*, off_t) const;
  1675. // The type of the list of symbols which have been forced local.
  1676. typedef std::vector<Symbol*> Forced_locals;
  1677. // A map from symbols with COPY relocs to the dynamic objects where
  1678. // they are defined.
  1679. typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
  1680. // We increment this every time we see a new undefined symbol, for
  1681. // use in archive groups.
  1682. size_t saw_undefined_;
  1683. // The index of the first global symbol in the output file.
  1684. unsigned int first_global_index_;
  1685. // The file offset within the output symtab section where we should
  1686. // write the table.
  1687. off_t offset_;
  1688. // The number of global symbols we want to write out.
  1689. unsigned int output_count_;
  1690. // The file offset of the global dynamic symbols, or 0 if none.
  1691. off_t dynamic_offset_;
  1692. // The index of the first global dynamic symbol (including
  1693. // forced-local symbols).
  1694. unsigned int first_dynamic_global_index_;
  1695. // The number of global dynamic symbols (including forced-local symbols),
  1696. // or 0 if none.
  1697. unsigned int dynamic_count_;
  1698. // Set if a STT_GNU_IFUNC or STB_GNU_UNIQUE symbol will be output.
  1699. bool has_gnu_output_;
  1700. // The symbol hash table.
  1701. Symbol_table_type table_;
  1702. // A pool of symbol names. This is used for all global symbols.
  1703. // Entries in the hash table point into this pool.
  1704. Stringpool namepool_;
  1705. // Forwarding symbols.
  1706. Unordered_map<const Symbol*, Symbol*> forwarders_;
  1707. // Weak aliases. A symbol in this list points to the next alias.
  1708. // The aliases point to each other in a circular list.
  1709. Unordered_map<Symbol*, Symbol*> weak_aliases_;
  1710. // We don't expect there to be very many common symbols, so we keep
  1711. // a list of them. When we find a common symbol we add it to this
  1712. // list. It is possible that by the time we process the list the
  1713. // symbol is no longer a common symbol. It may also have become a
  1714. // forwarder.
  1715. Commons_type commons_;
  1716. // This is like the commons_ field, except that it holds TLS common
  1717. // symbols.
  1718. Commons_type tls_commons_;
  1719. // This is for small common symbols.
  1720. Commons_type small_commons_;
  1721. // This is for large common symbols.
  1722. Commons_type large_commons_;
  1723. // A list of symbols which have been forced to be local. We don't
  1724. // expect there to be very many of them, so we keep a list of them
  1725. // rather than walking the whole table to find them.
  1726. Forced_locals forced_locals_;
  1727. // Manage symbol warnings.
  1728. Warnings warnings_;
  1729. // Manage potential One Definition Rule (ODR) violations.
  1730. Odr_map candidate_odr_violations_;
  1731. // When we emit a COPY reloc for a symbol, we define it in an
  1732. // Output_data. When it's time to emit version information for it,
  1733. // we need to know the dynamic object in which we found the original
  1734. // definition. This maps symbols with COPY relocs to the dynamic
  1735. // object where they were defined.
  1736. Copied_symbol_dynobjs copied_symbol_dynobjs_;
  1737. // Information parsed from the version script, if any.
  1738. const Version_script_info& version_script_;
  1739. Garbage_collection* gc_;
  1740. Icf* icf_;
  1741. // Target-specific symbols, if any.
  1742. std::vector<Symbol*> target_symbols_;
  1743. };
  1744. // We inline get_sized_symbol for efficiency.
  1745. template<int size>
  1746. Sized_symbol<size>*
  1747. Symbol_table::get_sized_symbol(Symbol* sym) const
  1748. {
  1749. gold_assert(size == parameters->target().get_size());
  1750. return static_cast<Sized_symbol<size>*>(sym);
  1751. }
  1752. template<int size>
  1753. const Sized_symbol<size>*
  1754. Symbol_table::get_sized_symbol(const Symbol* sym) const
  1755. {
  1756. gold_assert(size == parameters->target().get_size());
  1757. return static_cast<const Sized_symbol<size>*>(sym);
  1758. }
  1759. } // End namespace gold.
  1760. #endif // !defined(GOLD_SYMTAB_H)