1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
3 // Copyright 2006, 2007 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 General_options;
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 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0)
43 : Object(name, input_file, true, offset), soname_()
46 // Return the name to use in a DT_NEEDED entry for this object.
50 // Compute the ELF hash code for a string.
52 elf_hash(const char*);
54 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
55 // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
56 // number of local dynamic symbols, which is the index of the first
57 // dynamic gobal symbol.
59 create_elf_hash_table(const std::vector<Symbol*>& dynsyms,
60 unsigned int local_dynsym_count,
61 unsigned char** pphash,
62 unsigned int* phashlen);
64 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
65 // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
66 // of local dynamic symbols, which is the index of the first dynamic
69 create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
70 unsigned int local_dynsym_count,
71 unsigned char** pphash, unsigned int* phashlen);
74 // Set the DT_SONAME string.
76 set_soname_string(const char* s)
77 { this->soname_.assign(s); }
80 // Compute the GNU hash code for a string.
82 gnu_hash(const char*);
84 // Compute the number of hash buckets to use.
86 compute_bucket_count(const std::vector<uint32_t>& hashcodes,
87 bool for_gnu_hash_table);
89 // Sized version of create_elf_hash_table.
90 template<bool big_endian>
92 sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
93 const std::vector<uint32_t>& chain,
95 unsigned int hashlen);
97 // Sized version of create_gnu_hash_table.
98 template<int size, bool big_endian>
100 sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
101 const std::vector<uint32_t>& dynsym_hashvals,
102 unsigned int unhashed_dynsym_count,
103 unsigned char** pphash,
104 unsigned int* phashlen);
106 // The DT_SONAME name, if any.
110 // A dynamic object, size and endian specific version.
112 template<int size, bool big_endian>
113 class Sized_dynobj : public Dynobj
116 Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
117 const typename elfcpp::Ehdr<size, big_endian>&);
119 // Set up the object file based on the ELF header.
121 setup(const typename elfcpp::Ehdr<size, big_endian>&);
125 do_read_symbols(Read_symbols_data*);
127 // Lay out the input sections.
129 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
131 // Add the symbols to the symbol table.
133 do_add_symbols(Symbol_table*, Read_symbols_data*);
135 // Get the name of a section.
137 do_section_name(unsigned int shndx)
138 { return this->elf_file_.section_name(shndx); }
140 // Return a view of the contents of a section. Set *PLEN to the
143 do_section_contents(unsigned int shndx)
144 { return this->elf_file_.section_contents(shndx); }
146 // Return section flags.
148 do_section_flags(unsigned int shndx)
149 { return this->elf_file_.section_flags(shndx); }
153 typedef Sized_dynobj<size, big_endian> This;
154 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
155 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
156 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
157 typedef elfcpp::Shdr<size, big_endian> Shdr;
158 typedef elfcpp::Dyn<size, big_endian> Dyn;
160 // Find the dynamic symbol table and the version sections, given the
163 find_dynsym_sections(const unsigned char* pshdrs,
164 unsigned int* pdynshm_shndx,
165 unsigned int* pversym_shndx,
166 unsigned int* pverdef_shndx,
167 unsigned int* pverneed_shndx,
168 unsigned int* pdynamic_shndx);
170 // Read the dynamic symbol section SHNDX.
172 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
173 elfcpp::SHT type, unsigned int link,
174 File_view** view, off_t* view_size,
175 unsigned int* view_info);
177 // Set the SONAME from the SHT_DYNAMIC section at DYNAMIC_SHNDX.
178 // The STRTAB parameters may have the relevant string table.
180 set_soname(const unsigned char* pshdrs, unsigned int dynamic_shndx,
181 unsigned int strtab_shndx, const unsigned char* strtabu,
184 // Mapping from version number to version name.
185 typedef std::vector<const char*> Version_map;
187 // Create the version map.
189 make_version_map(Read_symbols_data* sd, Version_map*) const;
191 // Add version definitions to the version map.
193 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
195 // Add version references to the version map.
197 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
199 // Add an entry to the version map.
201 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
203 // General access to the ELF file.
204 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
207 // A base class for Verdef and Verneed_version which just handles the
208 // version index which will be stored in the SHT_GNU_versym section.
221 // Return the version index.
225 gold_assert(this->index_ != -1U);
229 // Set the version index.
231 set_index(unsigned int index)
233 gold_assert(this->index_ == -1U);
234 this->index_ = index;
237 // Clear the weak flag in a version definition.
242 Version_base(const Version_base&);
243 Version_base& operator=(const Version_base&);
245 // The index of the version definition or reference.
249 // This class handles a version being defined in the file we are
252 class Verdef : public Version_base
255 Verdef(const char* name, bool is_base, bool is_weak, bool is_symbol_created)
256 : name_(name), deps_(), is_base_(is_base), is_weak_(is_weak),
257 is_symbol_created_(is_symbol_created)
260 // Return the version name.
263 { return this->name_; }
265 // Return the number of dependencies.
267 count_dependencies() const
268 { return this->deps_.size(); }
270 // Add a dependency to this version. The NAME should be
271 // canonicalized in the dynamic Stringpool.
273 add_dependency(const char* name)
274 { this->deps_.push_back(name); }
276 // Return whether this definition is weak.
279 { return this->is_weak_; }
281 // Clear the weak flag.
284 { this->is_weak_ = false; }
286 // Return whether a version symbol has been created for this
289 is_symbol_created() const
290 { return this->is_symbol_created_; }
292 // Write contents to buffer.
293 template<int size, bool big_endian>
295 write(const Stringpool*, bool is_last, unsigned char*
296 ACCEPT_SIZE_ENDIAN) const;
299 Verdef(const Verdef&);
300 Verdef& operator=(const Verdef&);
302 // The type of the list of version dependencies. Each dependency
303 // should be canonicalized in the dynamic Stringpool.
304 typedef std::vector<const char*> Deps;
306 // The name of this version. This should be canonicalized in the
307 // dynamic Stringpool.
309 // A list of other versions which this version depends upon.
311 // Whether this is the base version.
313 // Whether this version is weak.
315 // Whether a version symbol has been created.
316 bool is_symbol_created_;
319 // A referened version. This will be associated with a filename by
322 class Verneed_version : public Version_base
325 Verneed_version(const char* version)
329 // Return the version name.
332 { return this->version_; }
334 // Clear the weak flag. This is invalid for a reference.
337 { gold_unreachable(); }
340 Verneed_version(const Verneed_version&);
341 Verneed_version& operator=(const Verneed_version&);
343 const char* version_;
346 // Version references in a single dynamic object.
351 Verneed(const char* filename)
352 : filename_(filename), need_versions_()
357 // Return the file name.
360 { return this->filename_; }
362 // Return the number of versions.
364 count_versions() const
365 { return this->need_versions_.size(); }
367 // Add a version name. The name should be canonicalized in the
368 // dynamic Stringpool. If the name is already present, this does
371 add_name(const char* name);
373 // Set the version indexes, starting at INDEX. Return the updated
376 finalize(unsigned int index);
378 // Write contents to buffer.
379 template<int size, bool big_endian>
381 write(const Stringpool*, bool is_last, unsigned char*
382 ACCEPT_SIZE_ENDIAN) const;
385 Verneed(const Verneed&);
386 Verneed& operator=(const Verneed&);
388 // The type of the list of version names. Each name should be
389 // canonicalized in the dynamic Stringpool.
390 typedef std::vector<Verneed_version*> Need_versions;
392 // The filename of the dynamic object. This should be
393 // canonicalized in the dynamic Stringpool.
394 const char* filename_;
395 // The list of version names.
396 Need_versions need_versions_;
399 // This class handles version definitions and references which go into
406 : defs_(), needs_(), version_table_(), is_finalized_(false)
411 // SYM is going into the dynamic symbol table and has a version.
412 // Record the appropriate version information.
414 record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
416 // Set the version indexes. DYNSYM_INDEX is the index we should use
417 // for the next dynamic symbol. We add new dynamic symbols to SYMS
418 // and return an updated DYNSYM_INDEX.
420 finalize(const Target*, Symbol_table* symtab, unsigned int dynsym_index,
421 std::vector<Symbol*>* syms);
423 // Return whether there are any version definitions.
426 { return !this->defs_.empty(); }
428 // Return whether there are any version references.
431 { return !this->needs_.empty(); }
433 // Build an allocated buffer holding the contents of the symbol
434 // version section (.gnu.version).
435 template<int size, bool big_endian>
437 symbol_section_contents(const Symbol_table*, const Stringpool*,
438 unsigned int local_symcount,
439 const std::vector<Symbol*>& syms,
440 unsigned char**, unsigned int*
441 ACCEPT_SIZE_ENDIAN) const;
443 // Build an allocated buffer holding the contents of the version
444 // definition section (.gnu.version_d).
445 template<int size, bool big_endian>
447 def_section_contents(const Stringpool*, unsigned char**,
448 unsigned int* psize, unsigned int* pentries
449 ACCEPT_SIZE_ENDIAN) const;
451 // Build an allocated buffer holding the contents of the version
452 // reference section (.gnu.version_r).
453 template<int size, bool big_endian>
455 need_section_contents(const Stringpool*, unsigned char**,
456 unsigned int* psize, unsigned int* pentries
457 ACCEPT_SIZE_ENDIAN) const;
460 // The type of the list of version definitions.
461 typedef std::vector<Verdef*> Defs;
463 // The type of the list of version references.
464 typedef std::vector<Verneed*> Needs;
466 // Handle a symbol SYM defined with version VERSION.
468 add_def(const Symbol* sym, const char* version, Stringpool::Key);
470 // Add a reference to version NAME in file FILENAME.
472 add_need(Stringpool*, const char* filename, const char* name,
475 // Get the dynamic object to use for SYM.
477 get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
479 // Return the version index to use for SYM.
481 version_index(const Symbol_table*, const Stringpool*,
482 const Symbol* sym) const;
484 // We keep a hash table mapping canonicalized name/version pairs to
486 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
488 struct Version_table_hash
491 operator()(const Key& k) const
492 { return k.first + k.second; }
495 struct Version_table_eq
498 operator()(const Key& k1, const Key& k2) const
499 { return k1.first == k2.first && k1.second == k2.second; }
502 typedef Unordered_map<Key, Version_base*, Version_table_hash,
503 Version_table_eq> Version_table;
505 // The version definitions.
507 // The version references.
509 // The mapping from a canonicalized version/filename pair to a
510 // version index. The filename may be NULL.
511 Version_table version_table_;
512 // Whether the version indexes have been set.
516 } // End namespace gold.
518 #endif // !defined(GOLD_DYNOBJ_H)