dynobj.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. // dynobj.h -- dynamic object support for 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_DYNOBJ_H
  18. #define GOLD_DYNOBJ_H
  19. #include <vector>
  20. #include "stringpool.h"
  21. #include "object.h"
  22. namespace gold
  23. {
  24. class Version_script_info;
  25. // A dynamic object (ET_DYN). This is an abstract base class itself.
  26. // The implementations is the template class Sized_dynobj.
  27. class Dynobj : public Object
  28. {
  29. public:
  30. // We keep a list of all the DT_NEEDED entries we find.
  31. typedef std::vector<std::string> Needed;
  32. Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
  33. // Return the name to use in a DT_NEEDED entry for this object.
  34. const char*
  35. soname() const
  36. { return this->soname_.c_str(); }
  37. // Return the list of DT_NEEDED strings.
  38. const Needed&
  39. needed() const
  40. { return this->needed_; }
  41. // Return whether this dynamic object has any DT_NEEDED entries
  42. // which were not seen during the link.
  43. bool
  44. has_unknown_needed_entries() const
  45. {
  46. gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET);
  47. return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE;
  48. }
  49. // Set whether this dynamic object has any DT_NEEDED entries which
  50. // were not seen during the link.
  51. void
  52. set_has_unknown_needed_entries(bool set)
  53. {
  54. gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET);
  55. this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE;
  56. }
  57. // Return the word size of the object file.
  58. int
  59. elfsize() const
  60. { gold_unreachable(); }
  61. // Return TRUE if this is a big-endian object file.
  62. bool
  63. is_big_endian() const
  64. { gold_unreachable(); }
  65. // Compute the ELF hash code for a string.
  66. static uint32_t
  67. elf_hash(const char*);
  68. // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
  69. // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
  70. // number of local dynamic symbols, which is the index of the first
  71. // dynamic gobal symbol.
  72. static void
  73. create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
  74. unsigned int local_dynsym_count,
  75. unsigned char** pphash,
  76. unsigned int* phashlen);
  77. // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
  78. // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
  79. // of local dynamic symbols, which is the index of the first dynamic
  80. // gobal symbol.
  81. static void
  82. create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
  83. unsigned int local_dynsym_count,
  84. unsigned char** pphash, unsigned int* phashlen);
  85. protected:
  86. // Return a pointer to this object.
  87. virtual Dynobj*
  88. do_dynobj()
  89. { return this; }
  90. // Set the DT_SONAME string.
  91. void
  92. set_soname_string(const char* s)
  93. { this->soname_.assign(s); }
  94. // Add an entry to the list of DT_NEEDED strings.
  95. void
  96. add_needed(const char* s)
  97. { this->needed_.push_back(std::string(s)); }
  98. private:
  99. // Compute the GNU hash code for a string.
  100. static uint32_t
  101. gnu_hash(const char*);
  102. // Compute the number of hash buckets to use.
  103. static unsigned int
  104. compute_bucket_count(const std::vector<uint32_t>& hashcodes,
  105. bool for_gnu_hash_table);
  106. // Sized version of create_elf_hash_table.
  107. template<bool big_endian>
  108. static void
  109. sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
  110. const std::vector<uint32_t>& chain,
  111. unsigned char* phash,
  112. unsigned int hashlen);
  113. // Sized version of create_gnu_hash_table.
  114. template<int size, bool big_endian>
  115. static void
  116. sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
  117. const std::vector<uint32_t>& dynsym_hashvals,
  118. unsigned int unhashed_dynsym_count,
  119. unsigned char** pphash,
  120. unsigned int* phashlen);
  121. // Values for the has_unknown_needed_entries_ field.
  122. enum Unknown_needed
  123. {
  124. UNKNOWN_NEEDED_UNSET,
  125. UNKNOWN_NEEDED_TRUE,
  126. UNKNOWN_NEEDED_FALSE
  127. };
  128. // The DT_SONAME name, if any.
  129. std::string soname_;
  130. // The list of DT_NEEDED entries.
  131. Needed needed_;
  132. // Whether this dynamic object has any DT_NEEDED entries not seen
  133. // during the link.
  134. Unknown_needed unknown_needed_;
  135. };
  136. // A dynamic object, size and endian specific version.
  137. template<int size, bool big_endian>
  138. class Sized_dynobj : public Dynobj
  139. {
  140. public:
  141. typedef typename Sized_relobj_file<size, big_endian>::Symbols Symbols;
  142. Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
  143. const typename elfcpp::Ehdr<size, big_endian>&);
  144. // Set up the object file based on TARGET.
  145. void
  146. setup();
  147. // Read the symbols.
  148. void
  149. do_read_symbols(Read_symbols_data*);
  150. // Lay out the input sections.
  151. void
  152. do_layout(Symbol_table*, Layout*, Read_symbols_data*);
  153. // Add the symbols to the symbol table.
  154. void
  155. do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
  156. Archive::Should_include
  157. do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
  158. std::string* why);
  159. // Iterate over global symbols, calling a visitor class V for each.
  160. void
  161. do_for_all_global_symbols(Read_symbols_data* sd,
  162. Library_base::Symbol_visitor_base* v);
  163. // Iterate over local symbols, calling a visitor class V for each GOT offset
  164. // associated with a local symbol.
  165. void
  166. do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
  167. // Get the size of a section.
  168. uint64_t
  169. do_section_size(unsigned int shndx)
  170. { return this->elf_file_.section_size(shndx); }
  171. // Get the name of a section.
  172. std::string
  173. do_section_name(unsigned int shndx) const
  174. { return this->elf_file_.section_name(shndx); }
  175. // Return a view of the contents of a section. Set *PLEN to the
  176. // size.
  177. const unsigned char*
  178. do_section_contents(unsigned int shndx, section_size_type* plen,
  179. bool cache)
  180. {
  181. Location loc(this->elf_file_.section_contents(shndx));
  182. *plen = convert_to_section_size_type(loc.data_size);
  183. if (*plen == 0)
  184. {
  185. static const unsigned char empty[1] = { '\0' };
  186. return empty;
  187. }
  188. return this->get_view(loc.file_offset, *plen, true, cache);
  189. }
  190. // Return section flags.
  191. uint64_t
  192. do_section_flags(unsigned int shndx)
  193. { return this->elf_file_.section_flags(shndx); }
  194. // Not used for dynobj.
  195. uint64_t
  196. do_section_entsize(unsigned int )
  197. { gold_unreachable(); }
  198. // Return section address.
  199. uint64_t
  200. do_section_address(unsigned int shndx)
  201. { return this->elf_file_.section_addr(shndx); }
  202. // Return section type.
  203. unsigned int
  204. do_section_type(unsigned int shndx)
  205. { return this->elf_file_.section_type(shndx); }
  206. // Return the section link field.
  207. unsigned int
  208. do_section_link(unsigned int shndx)
  209. { return this->elf_file_.section_link(shndx); }
  210. // Return the section link field.
  211. unsigned int
  212. do_section_info(unsigned int shndx)
  213. { return this->elf_file_.section_info(shndx); }
  214. // Return the section alignment.
  215. uint64_t
  216. do_section_addralign(unsigned int shndx)
  217. { return this->elf_file_.section_addralign(shndx); }
  218. // Return the Xindex structure to use.
  219. Xindex*
  220. do_initialize_xindex();
  221. // Get symbol counts.
  222. void
  223. do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
  224. // Get the global symbols.
  225. const Symbols*
  226. do_get_global_symbols() const
  227. { return this->symbols_; }
  228. protected:
  229. // Read the symbols. This is common code for all target-specific
  230. // overrides of do_read_symbols().
  231. void
  232. base_read_symbols(Read_symbols_data*);
  233. private:
  234. // For convenience.
  235. typedef Sized_dynobj<size, big_endian> This;
  236. static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
  237. static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
  238. static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
  239. typedef elfcpp::Shdr<size, big_endian> Shdr;
  240. typedef elfcpp::Dyn<size, big_endian> Dyn;
  241. // Adjust a section index if necessary.
  242. unsigned int
  243. adjust_shndx(unsigned int shndx)
  244. {
  245. if (shndx >= elfcpp::SHN_LORESERVE)
  246. shndx += this->elf_file_.large_shndx_offset();
  247. return shndx;
  248. }
  249. // Find the dynamic symbol table and the version sections, given the
  250. // section headers.
  251. void
  252. find_dynsym_sections(const unsigned char* pshdrs,
  253. unsigned int* pversym_shndx,
  254. unsigned int* pverdef_shndx,
  255. unsigned int* pverneed_shndx,
  256. unsigned int* pdynamic_shndx);
  257. // Read the dynamic symbol section SHNDX.
  258. void
  259. read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
  260. elfcpp::SHT type, unsigned int link,
  261. File_view** view, section_size_type* view_size,
  262. unsigned int* view_info);
  263. // Read the dynamic tags.
  264. void
  265. read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
  266. unsigned int strtab_shndx, const unsigned char* strtabu,
  267. off_t strtab_size);
  268. // Mapping from version number to version name.
  269. typedef std::vector<const char*> Version_map;
  270. // Create the version map.
  271. void
  272. make_version_map(Read_symbols_data* sd, Version_map*) const;
  273. // Add version definitions to the version map.
  274. void
  275. make_verdef_map(Read_symbols_data* sd, Version_map*) const;
  276. // Add version references to the version map.
  277. void
  278. make_verneed_map(Read_symbols_data* sd, Version_map*) const;
  279. // Add an entry to the version map.
  280. void
  281. set_version_map(Version_map*, unsigned int ndx, const char* name) const;
  282. // General access to the ELF file.
  283. elfcpp::Elf_file<size, big_endian, Object> elf_file_;
  284. // The section index of the dynamic symbol table.
  285. unsigned int dynsym_shndx_;
  286. // The entries in the symbol table for the symbols. We only keep
  287. // this if we need it to print symbol information.
  288. Symbols* symbols_;
  289. // Number of defined symbols.
  290. size_t defined_count_;
  291. };
  292. // A base class for Verdef and Verneed_version which just handles the
  293. // version index which will be stored in the SHT_GNU_versym section.
  294. class Version_base
  295. {
  296. public:
  297. Version_base()
  298. : index_(-1U)
  299. { }
  300. virtual
  301. ~Version_base()
  302. { }
  303. // Return the version index.
  304. unsigned int
  305. index() const
  306. {
  307. gold_assert(this->index_ != -1U);
  308. return this->index_;
  309. }
  310. // Set the version index.
  311. void
  312. set_index(unsigned int index)
  313. {
  314. gold_assert(this->index_ == -1U);
  315. this->index_ = index;
  316. }
  317. // Clear the weak flag in a version definition.
  318. virtual void
  319. clear_weak() = 0;
  320. private:
  321. Version_base(const Version_base&);
  322. Version_base& operator=(const Version_base&);
  323. // The index of the version definition or reference.
  324. unsigned int index_;
  325. };
  326. // This class handles a version being defined in the file we are
  327. // generating.
  328. class Verdef : public Version_base
  329. {
  330. public:
  331. Verdef(const char* name, const std::vector<std::string>& deps,
  332. bool is_base, bool is_weak, bool is_info, bool is_symbol_created)
  333. : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
  334. is_info_(is_info), is_symbol_created_(is_symbol_created)
  335. { }
  336. // Return the version name.
  337. const char*
  338. name() const
  339. { return this->name_; }
  340. // Return the number of dependencies.
  341. unsigned int
  342. count_dependencies() const
  343. { return this->deps_.size(); }
  344. // Add a dependency to this version. The NAME should be
  345. // canonicalized in the dynamic Stringpool.
  346. void
  347. add_dependency(const char* name)
  348. { this->deps_.push_back(name); }
  349. // Return whether this definition is weak.
  350. bool
  351. is_weak() const
  352. { return this->is_weak_; }
  353. // Clear the weak flag.
  354. void
  355. clear_weak()
  356. { this->is_weak_ = false; }
  357. // Return whether this definition is informational.
  358. bool
  359. is_info() const
  360. { return this->is_info_; }
  361. // Return whether a version symbol has been created for this
  362. // definition.
  363. bool
  364. is_symbol_created() const
  365. { return this->is_symbol_created_; }
  366. // Write contents to buffer.
  367. template<int size, bool big_endian>
  368. unsigned char*
  369. write(const Stringpool*, bool is_last, unsigned char*) const;
  370. private:
  371. Verdef(const Verdef&);
  372. Verdef& operator=(const Verdef&);
  373. // The type of the list of version dependencies. Each dependency
  374. // should be canonicalized in the dynamic Stringpool.
  375. typedef std::vector<std::string> Deps;
  376. // The name of this version. This should be canonicalized in the
  377. // dynamic Stringpool.
  378. const char* name_;
  379. // A list of other versions which this version depends upon.
  380. Deps deps_;
  381. // Whether this is the base version.
  382. bool is_base_;
  383. // Whether this version is weak.
  384. bool is_weak_;
  385. // Whether this version is informational.
  386. bool is_info_;
  387. // Whether a version symbol has been created.
  388. bool is_symbol_created_;
  389. };
  390. // A referened version. This will be associated with a filename by
  391. // Verneed.
  392. class Verneed_version : public Version_base
  393. {
  394. public:
  395. Verneed_version(const char* version)
  396. : version_(version)
  397. { }
  398. // Return the version name.
  399. const char*
  400. version() const
  401. { return this->version_; }
  402. // Clear the weak flag. This is invalid for a reference.
  403. void
  404. clear_weak()
  405. { gold_unreachable(); }
  406. private:
  407. Verneed_version(const Verneed_version&);
  408. Verneed_version& operator=(const Verneed_version&);
  409. const char* version_;
  410. };
  411. // Version references in a single dynamic object.
  412. class Verneed
  413. {
  414. public:
  415. Verneed(const char* filename)
  416. : filename_(filename), need_versions_()
  417. { }
  418. ~Verneed();
  419. // Return the file name.
  420. const char*
  421. filename() const
  422. { return this->filename_; }
  423. // Return the number of versions.
  424. unsigned int
  425. count_versions() const
  426. { return this->need_versions_.size(); }
  427. // Add a version name. The name should be canonicalized in the
  428. // dynamic Stringpool. If the name is already present, this does
  429. // nothing.
  430. Verneed_version*
  431. add_name(const char* name);
  432. // Set the version indexes, starting at INDEX. Return the updated
  433. // INDEX.
  434. unsigned int
  435. finalize(unsigned int index);
  436. // Write contents to buffer.
  437. template<int size, bool big_endian>
  438. unsigned char*
  439. write(const Stringpool*, bool is_last, unsigned char*) const;
  440. private:
  441. Verneed(const Verneed&);
  442. Verneed& operator=(const Verneed&);
  443. // The type of the list of version names. Each name should be
  444. // canonicalized in the dynamic Stringpool.
  445. typedef std::vector<Verneed_version*> Need_versions;
  446. // The filename of the dynamic object. This should be
  447. // canonicalized in the dynamic Stringpool.
  448. const char* filename_;
  449. // The list of version names.
  450. Need_versions need_versions_;
  451. };
  452. // This class handles version definitions and references which go into
  453. // the output file.
  454. class Versions
  455. {
  456. public:
  457. Versions(const Version_script_info&, Stringpool*);
  458. ~Versions();
  459. // SYM is going into the dynamic symbol table and has a version.
  460. // Record the appropriate version information.
  461. void
  462. record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
  463. // Set the version indexes. DYNSYM_INDEX is the index we should use
  464. // for the next dynamic symbol. We add new dynamic symbols to SYMS
  465. // and return an updated DYNSYM_INDEX.
  466. unsigned int
  467. finalize(Symbol_table* symtab, unsigned int dynsym_index,
  468. std::vector<Symbol*>* syms);
  469. // Return whether there are any version definitions.
  470. bool
  471. any_defs() const
  472. { return !this->defs_.empty(); }
  473. // Return whether there are any version references.
  474. bool
  475. any_needs() const
  476. { return !this->needs_.empty(); }
  477. // Build an allocated buffer holding the contents of the symbol
  478. // version section (.gnu.version).
  479. template<int size, bool big_endian>
  480. void
  481. symbol_section_contents(const Symbol_table*, const Stringpool*,
  482. unsigned int local_symcount,
  483. const std::vector<Symbol*>& syms,
  484. unsigned char**, unsigned int*) const;
  485. // Build an allocated buffer holding the contents of the version
  486. // definition section (.gnu.version_d).
  487. template<int size, bool big_endian>
  488. void
  489. def_section_contents(const Stringpool*, unsigned char**,
  490. unsigned int* psize, unsigned int* pentries) const;
  491. // Build an allocated buffer holding the contents of the version
  492. // reference section (.gnu.version_r).
  493. template<int size, bool big_endian>
  494. void
  495. need_section_contents(const Stringpool*, unsigned char**,
  496. unsigned int* psize, unsigned int* pentries) const;
  497. const Version_script_info&
  498. version_script() const
  499. { return this->version_script_; }
  500. private:
  501. Versions(const Versions&);
  502. Versions& operator=(const Versions&);
  503. // The type of the list of version definitions.
  504. typedef std::vector<Verdef*> Defs;
  505. // The type of the list of version references.
  506. typedef std::vector<Verneed*> Needs;
  507. // Handle a symbol SYM defined with version VERSION.
  508. void
  509. add_def(Stringpool*, const Symbol* sym, const char* version,
  510. Stringpool::Key);
  511. // Add a reference to version NAME in file FILENAME.
  512. void
  513. add_need(Stringpool*, const char* filename, const char* name,
  514. Stringpool::Key);
  515. // Get the dynamic object to use for SYM.
  516. Dynobj*
  517. get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
  518. // Return the version index to use for SYM.
  519. unsigned int
  520. version_index(const Symbol_table*, const Stringpool*,
  521. const Symbol* sym) const;
  522. // Define the base version of a shared library.
  523. void
  524. define_base_version(Stringpool* dynpool);
  525. // We keep a hash table mapping canonicalized name/version pairs to
  526. // a version base.
  527. typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
  528. struct Version_table_hash
  529. {
  530. size_t
  531. operator()(const Key& k) const
  532. { return k.first + k.second; }
  533. };
  534. struct Version_table_eq
  535. {
  536. bool
  537. operator()(const Key& k1, const Key& k2) const
  538. { return k1.first == k2.first && k1.second == k2.second; }
  539. };
  540. typedef Unordered_map<Key, Version_base*, Version_table_hash,
  541. Version_table_eq> Version_table;
  542. // The version definitions.
  543. Defs defs_;
  544. // The version references.
  545. Needs needs_;
  546. // The mapping from a canonicalized version/filename pair to a
  547. // version index. The filename may be NULL.
  548. Version_table version_table_;
  549. // Whether the version indexes have been set.
  550. bool is_finalized_;
  551. // Contents of --version-script, if passed, or NULL.
  552. const Version_script_info& version_script_;
  553. // Whether we need to insert a base version. This is only used for
  554. // shared libraries and is cleared when the base version is defined.
  555. bool needs_base_version_;
  556. };
  557. } // End namespace gold.
  558. #endif // !defined(GOLD_DYNOBJ_H)