1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
3 // Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "stringpool.h"
34 class Version_script_info;
36 // A dynamic object (ET_DYN). This is an abstract base class itself.
37 // The implementations is the template class Sized_dynobj.
39 class Dynobj : public Object
42 // We keep a list of all the DT_NEEDED entries we find.
43 typedef std::vector<std::string> Needed;
45 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
47 // Return the name to use in a DT_NEEDED entry for this object.
50 { return this->soname_.c_str(); }
52 // Return the list of DT_NEEDED strings.
55 { return this->needed_; }
57 // Return whether this dynamic object has any DT_NEEDED entries
58 // which were not seen during the link.
60 has_unknown_needed_entries() const
62 gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET);
63 return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE;
66 // Set whether this dynamic object has any DT_NEEDED entries which
67 // were not seen during the link.
69 set_has_unknown_needed_entries(bool set)
71 gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET);
72 this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE;
75 // Return the word size of the object file.
78 { gold_unreachable(); }
80 // Return TRUE if this is a big-endian object file.
83 { gold_unreachable(); }
85 // Compute the ELF hash code for a string.
87 elf_hash(const char*);
89 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
90 // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
91 // number of local dynamic symbols, which is the index of the first
92 // dynamic gobal symbol.
94 create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
95 unsigned int local_dynsym_count,
96 unsigned char** pphash,
97 unsigned int* phashlen);
99 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
100 // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
101 // of local dynamic symbols, which is the index of the first dynamic
104 create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
105 unsigned int local_dynsym_count,
106 unsigned char** pphash, unsigned int* phashlen);
109 // Return a pointer to this object.
114 // Set the DT_SONAME string.
116 set_soname_string(const char* s)
117 { this->soname_.assign(s); }
119 // Add an entry to the list of DT_NEEDED strings.
121 add_needed(const char* s)
122 { this->needed_.push_back(std::string(s)); }
125 // Compute the GNU hash code for a string.
127 gnu_hash(const char*);
129 // Compute the number of hash buckets to use.
131 compute_bucket_count(const std::vector<uint32_t>& hashcodes,
132 bool for_gnu_hash_table);
134 // Sized version of create_elf_hash_table.
135 template<int size, bool big_endian>
137 sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
138 const std::vector<uint32_t>& chain,
139 unsigned char* phash,
140 unsigned int hashlen);
142 // Sized version of create_gnu_hash_table.
143 template<int size, bool big_endian>
145 sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
146 const std::vector<uint32_t>& dynsym_hashvals,
147 unsigned int unhashed_dynsym_count,
148 unsigned char** pphash,
149 unsigned int* phashlen);
151 // Values for the has_unknown_needed_entries_ field.
154 UNKNOWN_NEEDED_UNSET,
159 // The DT_SONAME name, if any.
161 // The list of DT_NEEDED entries.
163 // Whether this dynamic object has any DT_NEEDED entries not seen
165 Unknown_needed unknown_needed_;
168 // A dynamic object, size and endian specific version.
170 template<int size, bool big_endian>
171 class Sized_dynobj : public Dynobj
174 typedef typename Sized_relobj_file<size, big_endian>::Symbols Symbols;
176 Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
177 const typename elfcpp::Ehdr<size, big_endian>&);
179 // Set up the object file based on TARGET.
185 do_read_symbols(Read_symbols_data*);
187 // Lay out the input sections.
189 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
191 // Add the symbols to the symbol table.
193 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
195 Archive::Should_include
196 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
199 // Iterate over global symbols, calling a visitor class V for each.
201 do_for_all_global_symbols(Read_symbols_data* sd,
202 Library_base::Symbol_visitor_base* v);
204 // Iterate over local symbols, calling a visitor class V for each GOT offset
205 // associated with a local symbol.
207 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
209 // Get the size of a section.
211 do_section_size(unsigned int shndx)
212 { return this->elf_file_.section_size(shndx); }
214 // Get the name of a section.
216 do_section_name(unsigned int shndx) const
217 { return this->elf_file_.section_name(shndx); }
219 // Return a view of the contents of a section. Set *PLEN to the
222 do_section_contents(unsigned int shndx, section_size_type* plen,
225 Location loc(this->elf_file_.section_contents(shndx));
226 *plen = convert_to_section_size_type(loc.data_size);
229 static const unsigned char empty[1] = { '\0' };
232 return this->get_view(loc.file_offset, *plen, true, cache);
235 // Return section flags.
237 do_section_flags(unsigned int shndx)
238 { return this->elf_file_.section_flags(shndx); }
240 // Not used for dynobj.
242 do_section_entsize(unsigned int )
243 { gold_unreachable(); }
245 // Return section address.
247 do_section_address(unsigned int shndx)
248 { return this->elf_file_.section_addr(shndx); }
250 // Return section type.
252 do_section_type(unsigned int shndx)
253 { return this->elf_file_.section_type(shndx); }
255 // Return the section link field.
257 do_section_link(unsigned int shndx)
258 { return this->elf_file_.section_link(shndx); }
260 // Return the section link field.
262 do_section_info(unsigned int shndx)
263 { return this->elf_file_.section_info(shndx); }
265 // Return the section alignment.
267 do_section_addralign(unsigned int shndx)
268 { return this->elf_file_.section_addralign(shndx); }
270 // Return the Xindex structure to use.
272 do_initialize_xindex();
274 // Get symbol counts.
276 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
278 // Get the global symbols.
280 do_get_global_symbols() const
281 { return this->symbols_; }
284 // Read the symbols. This is common code for all target-specific
285 // overrides of do_read_symbols().
287 base_read_symbols(Read_symbols_data*);
291 typedef Sized_dynobj<size, big_endian> This;
292 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
293 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
294 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
295 typedef elfcpp::Shdr<size, big_endian> Shdr;
296 typedef elfcpp::Dyn<size, big_endian> Dyn;
298 // Adjust a section index if necessary.
300 adjust_shndx(unsigned int shndx)
302 if (shndx >= elfcpp::SHN_LORESERVE)
303 shndx += this->elf_file_.large_shndx_offset();
307 // Find the dynamic symbol table and the version sections, given the
310 find_dynsym_sections(const unsigned char* pshdrs,
311 unsigned int* pversym_shndx,
312 unsigned int* pverdef_shndx,
313 unsigned int* pverneed_shndx,
314 unsigned int* pdynamic_shndx);
316 // Read the dynamic symbol section SHNDX.
318 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
319 elfcpp::SHT type, unsigned int link,
320 File_view** view, section_size_type* view_size,
321 unsigned int* view_info);
323 // Read the dynamic tags.
325 read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
326 unsigned int strtab_shndx, const unsigned char* strtabu,
329 // Mapping from version number to version name.
330 typedef std::vector<const char*> Version_map;
332 // Create the version map.
334 make_version_map(Read_symbols_data* sd, Version_map*) const;
336 // Add version definitions to the version map.
338 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
340 // Add version references to the version map.
342 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
344 // Add an entry to the version map.
346 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
348 // General access to the ELF file.
349 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
350 // The section index of the dynamic symbol table.
351 unsigned int dynsym_shndx_;
352 // The entries in the symbol table for the symbols. We only keep
353 // this if we need it to print symbol information.
355 // Number of defined symbols.
356 size_t defined_count_;
359 // A base class for Verdef and Verneed_version which just handles the
360 // version index which will be stored in the SHT_GNU_versym section.
373 // Return the version index.
377 gold_assert(this->index_ != -1U);
381 // Set the version index.
383 set_index(unsigned int index)
385 gold_assert(this->index_ == -1U);
386 this->index_ = index;
389 // Clear the weak flag in a version definition.
394 Version_base(const Version_base&);
395 Version_base& operator=(const Version_base&);
397 // The index of the version definition or reference.
401 // This class handles a version being defined in the file we are
404 class Verdef : public Version_base
407 Verdef(const char* name, const std::vector<std::string>& deps,
408 bool is_base, bool is_weak, bool is_info, bool is_symbol_created)
409 : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
410 is_info_(is_info), is_symbol_created_(is_symbol_created)
413 // Return the version name.
416 { return this->name_; }
418 // Return the number of dependencies.
420 count_dependencies() const
421 { return this->deps_.size(); }
423 // Add a dependency to this version. The NAME should be
424 // canonicalized in the dynamic Stringpool.
426 add_dependency(const char* name)
427 { this->deps_.push_back(name); }
429 // Return whether this definition is weak.
432 { return this->is_weak_; }
434 // Clear the weak flag.
437 { this->is_weak_ = false; }
439 // Return whether this definition is informational.
442 { return this->is_info_; }
444 // Return whether a version symbol has been created for this
447 is_symbol_created() const
448 { return this->is_symbol_created_; }
450 // Write contents to buffer.
451 template<int size, bool big_endian>
453 write(const Stringpool*, bool is_last, unsigned char*) const;
456 Verdef(const Verdef&);
457 Verdef& operator=(const Verdef&);
459 // The type of the list of version dependencies. Each dependency
460 // should be canonicalized in the dynamic Stringpool.
461 typedef std::vector<std::string> Deps;
463 // The name of this version. This should be canonicalized in the
464 // dynamic Stringpool.
466 // A list of other versions which this version depends upon.
468 // Whether this is the base version.
470 // Whether this version is weak.
472 // Whether this version is informational.
474 // Whether a version symbol has been created.
475 bool is_symbol_created_;
478 // A referened version. This will be associated with a filename by
481 class Verneed_version : public Version_base
484 Verneed_version(const char* version)
488 // Return the version name.
491 { return this->version_; }
493 // Clear the weak flag. This is invalid for a reference.
496 { gold_unreachable(); }
499 Verneed_version(const Verneed_version&);
500 Verneed_version& operator=(const Verneed_version&);
502 const char* version_;
505 // Version references in a single dynamic object.
510 Verneed(const char* filename)
511 : filename_(filename), need_versions_()
516 // Return the file name.
519 { return this->filename_; }
521 // Return the number of versions.
523 count_versions() const
524 { return this->need_versions_.size(); }
526 // Add a version name. The name should be canonicalized in the
527 // dynamic Stringpool. If the name is already present, this does
530 add_name(const char* name);
532 // Set the version indexes, starting at INDEX. Return the updated
535 finalize(unsigned int index);
537 // Write contents to buffer.
538 template<int size, bool big_endian>
540 write(const Stringpool*, bool is_last, unsigned char*) const;
543 Verneed(const Verneed&);
544 Verneed& operator=(const Verneed&);
546 // The type of the list of version names. Each name should be
547 // canonicalized in the dynamic Stringpool.
548 typedef std::vector<Verneed_version*> Need_versions;
550 // The filename of the dynamic object. This should be
551 // canonicalized in the dynamic Stringpool.
552 const char* filename_;
553 // The list of version names.
554 Need_versions need_versions_;
557 // This class handles version definitions and references which go into
563 Versions(const Version_script_info&, Stringpool*);
567 // SYM is going into the dynamic symbol table and has a version.
568 // Record the appropriate version information.
570 record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
572 // Set the version indexes. DYNSYM_INDEX is the index we should use
573 // for the next dynamic symbol. We add new dynamic symbols to SYMS
574 // and return an updated DYNSYM_INDEX.
576 finalize(Symbol_table* symtab, unsigned int dynsym_index,
577 std::vector<Symbol*>* syms);
579 // Return whether there are any version definitions.
582 { return !this->defs_.empty(); }
584 // Return whether there are any version references.
587 { return !this->needs_.empty(); }
589 // Build an allocated buffer holding the contents of the symbol
590 // version section (.gnu.version).
591 template<int size, bool big_endian>
593 symbol_section_contents(const Symbol_table*, const Stringpool*,
594 unsigned int local_symcount,
595 const std::vector<Symbol*>& syms,
596 unsigned char**, unsigned int*) const;
598 // Build an allocated buffer holding the contents of the version
599 // definition section (.gnu.version_d).
600 template<int size, bool big_endian>
602 def_section_contents(const Stringpool*, unsigned char**,
603 unsigned int* psize, unsigned int* pentries) const;
605 // Build an allocated buffer holding the contents of the version
606 // reference section (.gnu.version_r).
607 template<int size, bool big_endian>
609 need_section_contents(const Stringpool*, unsigned char**,
610 unsigned int* psize, unsigned int* pentries) const;
612 const Version_script_info&
613 version_script() const
614 { return this->version_script_; }
617 Versions(const Versions&);
618 Versions& operator=(const Versions&);
620 // The type of the list of version definitions.
621 typedef std::vector<Verdef*> Defs;
623 // The type of the list of version references.
624 typedef std::vector<Verneed*> Needs;
626 // Handle a symbol SYM defined with version VERSION.
628 add_def(Stringpool*, const Symbol* sym, const char* version,
631 // Add a reference to version NAME in file FILENAME.
633 add_need(Stringpool*, const char* filename, const char* name,
636 // Get the dynamic object to use for SYM.
638 get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
640 // Return the version index to use for SYM.
642 version_index(const Symbol_table*, const Stringpool*,
643 const Symbol* sym) const;
645 // Define the base version of a shared library.
647 define_base_version(Stringpool* dynpool);
649 // We keep a hash table mapping canonicalized name/version pairs to
651 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
653 struct Version_table_hash
656 operator()(const Key& k) const
657 { return k.first + k.second; }
660 struct Version_table_eq
663 operator()(const Key& k1, const Key& k2) const
664 { return k1.first == k2.first && k1.second == k2.second; }
667 typedef Unordered_map<Key, Version_base*, Version_table_hash,
668 Version_table_eq> Version_table;
670 // The version definitions.
672 // The version references.
674 // The mapping from a canonicalized version/filename pair to a
675 // version index. The filename may be NULL.
676 Version_table version_table_;
677 // Whether the version indexes have been set.
679 // Contents of --version-script, if passed, or NULL.
680 const Version_script_info& version_script_;
681 // Whether we need to insert a base version. This is only used for
682 // shared libraries and is cleared when the base version is defined.
683 bool needs_base_version_;
686 } // End namespace gold.
688 #endif // !defined(GOLD_DYNOBJ_H)