1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
8 #include "stringpool.h"
14 class General_options;
16 // A dynamic object (ET_DYN). This is an abstract base class itself.
17 // The implementations is the template class Sized_dynobj.
19 class Dynobj : public Object
22 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0)
23 : Object(name, input_file, true, offset), soname_()
26 // Return the name to use in a DT_NEEDED entry for this object.
30 // Compute the ELF hash code for a string.
32 elf_hash(const char*);
34 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
35 // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
36 // number of local dynamic symbols, which is the index of the first
37 // dynamic gobal symbol.
39 create_elf_hash_table(const Target*, const std::vector<Symbol*>& dynsyms,
40 unsigned int local_dynsym_count,
41 unsigned char** pphash,
42 unsigned int* phashlen);
44 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
45 // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
46 // of local dynamic symbols, which is the index of the first dynamic
49 create_gnu_hash_table(const Target*, const std::vector<Symbol*>& dynsyms,
50 unsigned int local_dynsym_count,
51 unsigned char** pphash, unsigned int* phashlen);
54 // Set the DT_SONAME string.
56 set_soname_string(const char* s)
57 { this->soname_.assign(s); }
60 // Compute the GNU hash code for a string.
62 gnu_hash(const char*);
64 // Compute the number of hash buckets to use.
66 compute_bucket_count(const std::vector<uint32_t>& hashcodes,
67 bool for_gnu_hash_table);
69 // Sized version of create_elf_hash_table.
70 template<bool big_endian>
72 sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
73 const std::vector<uint32_t>& chain,
75 unsigned int hashlen);
77 // Sized version of create_gnu_hash_table.
78 template<int size, bool big_endian>
80 sized_create_gnu_hash_table(const std::vector<Symbol*>& hashed_dynsyms,
81 const std::vector<uint32_t>& dynsym_hashvals,
82 unsigned int unhashed_dynsym_count,
83 unsigned char** pphash,
84 unsigned int* phashlen);
86 // The DT_SONAME name, if any.
90 // A dynamic object, size and endian specific version.
92 template<int size, bool big_endian>
93 class Sized_dynobj : public Dynobj
96 Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
97 const typename elfcpp::Ehdr<size, big_endian>&);
99 // Set up the object file based on the ELF header.
101 setup(const typename elfcpp::Ehdr<size, big_endian>&);
105 do_read_symbols(Read_symbols_data*);
107 // Lay out the input sections.
109 do_layout(const General_options&, Symbol_table*, Layout*,
112 // Add the symbols to the symbol table.
114 do_add_symbols(Symbol_table*, Read_symbols_data*);
116 // Get the name of a section.
118 do_section_name(unsigned int shndx)
119 { return this->elf_file_.section_name(shndx); }
121 // Return a view of the contents of a section. Set *PLEN to the
124 do_section_contents(unsigned int shndx)
125 { return this->elf_file_.section_contents(shndx); }
127 // Return section flags.
129 do_section_flags(unsigned int shndx)
130 { return this->elf_file_.section_flags(shndx); }
134 typedef Sized_dynobj<size, big_endian> This;
135 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
136 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
137 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
138 typedef elfcpp::Shdr<size, big_endian> Shdr;
139 typedef elfcpp::Dyn<size, big_endian> Dyn;
141 // Find the dynamic symbol table and the version sections, given the
144 find_dynsym_sections(const unsigned char* pshdrs,
145 unsigned int* pdynshm_shndx,
146 unsigned int* pversym_shndx,
147 unsigned int* pverdef_shndx,
148 unsigned int* pverneed_shndx,
149 unsigned int* pdynamic_shndx);
151 // Read the dynamic symbol section SHNDX.
153 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
154 elfcpp::SHT type, unsigned int link,
155 File_view** view, off_t* view_size,
156 unsigned int* view_info);
158 // Set the SONAME from the SHT_DYNAMIC section at DYNAMIC_SHNDX.
159 // The STRTAB parameters may have the relevant string table.
161 set_soname(const unsigned char* pshdrs, unsigned int dynamic_shndx,
162 unsigned int strtab_shndx, const unsigned char* strtabu,
165 // Mapping from version number to version name.
166 typedef std::vector<const char*> Version_map;
168 // Create the version map.
170 make_version_map(Read_symbols_data* sd, Version_map*) const;
172 // Add version definitions to the version map.
174 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
176 // Add version references to the version map.
178 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
180 // Add an entry to the version map.
182 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
184 // General access to the ELF file.
185 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
188 // A base class for Verdef and Verneed_version which just handles the
189 // version index which will be stored in the SHT_GNU_versym section.
202 // Return the version index.
206 gold_assert(this->index_ != -1U);
210 // Set the version index.
212 set_index(unsigned int index)
214 gold_assert(this->index_ == -1U);
215 this->index_ = index;
218 // Clear the weak flag in a version definition.
223 Version_base(const Version_base&);
224 Version_base& operator=(const Version_base&);
226 // The index of the version definition or reference.
230 // This class handles a version being defined in the file we are
233 class Verdef : public Version_base
236 Verdef(const char* name, bool is_base, bool is_weak, bool is_symbol_created)
237 : name_(name), deps_(), is_base_(is_base), is_weak_(is_weak),
238 is_symbol_created_(is_symbol_created)
241 // Return the version name.
244 { return this->name_; }
246 // Return the number of dependencies.
248 count_dependencies() const
249 { return this->deps_.size(); }
251 // Add a dependency to this version. The NAME should be
252 // canonicalized in the dynamic Stringpool.
254 add_dependency(const char* name)
255 { this->deps_.push_back(name); }
257 // Return whether this definition is weak.
260 { return this->is_weak_; }
262 // Clear the weak flag.
265 { this->is_weak_ = false; }
267 // Return whether a version symbol has been created for this
270 is_symbol_created() const
271 { return this->is_symbol_created_; }
273 // Write contents to buffer.
274 template<int size, bool big_endian>
276 write(const Stringpool*, bool is_last, unsigned char*
277 ACCEPT_SIZE_ENDIAN) const;
280 Verdef(const Verdef&);
281 Verdef& operator=(const Verdef&);
283 // The type of the list of version dependencies. Each dependency
284 // should be canonicalized in the dynamic Stringpool.
285 typedef std::vector<const char*> Deps;
287 // The name of this version. This should be canonicalized in the
288 // dynamic Stringpool.
290 // A list of other versions which this version depends upon.
292 // Whether this is the base version.
294 // Whether this version is weak.
296 // Whether a version symbol has been created.
297 bool is_symbol_created_;
300 // A referened version. This will be associated with a filename by
303 class Verneed_version : public Version_base
306 Verneed_version(const char* version)
310 // Return the version name.
313 { return this->version_; }
315 // Clear the weak flag. This is invalid for a reference.
318 { gold_unreachable(); }
321 Verneed_version(const Verneed_version&);
322 Verneed_version& operator=(const Verneed_version&);
324 const char* version_;
327 // Version references in a single dynamic object.
332 Verneed(const char* filename)
333 : filename_(filename), need_versions_()
338 // Return the file name.
341 { return this->filename_; }
343 // Return the number of versions.
345 count_versions() const
346 { return this->need_versions_.size(); }
348 // Add a version name. The name should be canonicalized in the
349 // dynamic Stringpool. If the name is already present, this does
352 add_name(const char* name);
354 // Set the version indexes, starting at INDEX. Return the updated
357 finalize(unsigned int index);
359 // Write contents to buffer.
360 template<int size, bool big_endian>
362 write(const Stringpool*, bool is_last, unsigned char*
363 ACCEPT_SIZE_ENDIAN) const;
366 Verneed(const Verneed&);
367 Verneed& operator=(const Verneed&);
369 // The type of the list of version names. Each name should be
370 // canonicalized in the dynamic Stringpool.
371 typedef std::vector<Verneed_version*> Need_versions;
373 // The filename of the dynamic object. This should be
374 // canonicalized in the dynamic Stringpool.
375 const char* filename_;
376 // The list of version names.
377 Need_versions need_versions_;
380 // This class handles version definitions and references which go into
387 : defs_(), needs_(), version_table_(), is_finalized_(false)
392 // SYM is going into the dynamic symbol table and has a version.
393 // Record the appropriate version information.
395 record_version(const General_options*, Stringpool*, const Symbol* sym);
397 // Set the version indexes. DYNSYM_INDEX is the index we should use
398 // for the next dynamic symbol. We add new dynamic symbols to SYMS
399 // and return an updated DYNSYM_INDEX.
401 finalize(const Target*, Symbol_table* symtab, unsigned int dynsym_index,
402 std::vector<Symbol*>* syms);
404 // Return whether there are any version definitions.
407 { return !this->defs_.empty(); }
409 // Return whether there are any version references.
412 { return !this->needs_.empty(); }
414 // Build an allocated buffer holding the contents of the symbol
415 // version section (.gnu.version).
416 template<int size, bool big_endian>
418 symbol_section_contents(const General_options*, const Stringpool*,
419 unsigned int local_symcount,
420 const std::vector<Symbol*>& syms,
421 unsigned char**, unsigned int*
422 ACCEPT_SIZE_ENDIAN) const;
424 // Build an allocated buffer holding the contents of the version
425 // definition section (.gnu.version_d).
426 template<int size, bool big_endian>
428 def_section_contents(const Stringpool*, unsigned char**,
429 unsigned int* psize, unsigned int* pentries
430 ACCEPT_SIZE_ENDIAN) const;
432 // Build an allocated buffer holding the contents of the version
433 // reference section (.gnu.version_r).
434 template<int size, bool big_endian>
436 need_section_contents(const Stringpool*, unsigned char**,
437 unsigned int* psize, unsigned int* pentries
438 ACCEPT_SIZE_ENDIAN) const;
441 // The type of the list of version definitions.
442 typedef std::vector<Verdef*> Defs;
444 // The type of the list of version references.
445 typedef std::vector<Verneed*> Needs;
447 // Handle a symbol SYM defined with version VERSION.
449 add_def(const General_options*, const Symbol* sym, const char* version,
452 // Add a reference to version NAME in file FILENAME.
454 add_need(Stringpool*, const char* filename, const char* name,
457 // Return the version index to use for SYM.
459 version_index(const General_options*, const Stringpool*,
460 const Symbol* sym) const;
462 // We keep a hash table mapping canonicalized name/version pairs to
464 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
466 struct Version_table_hash
469 operator()(const Key& k) const
470 { return k.first + k.second; }
473 struct Version_table_eq
476 operator()(const Key& k1, const Key& k2) const
477 { return k1.first == k2.first && k1.second == k2.second; }
480 typedef Unordered_map<Key, Version_base*, Version_table_hash,
481 Version_table_eq> Version_table;
483 // The version definitions.
485 // The version references.
487 // The mapping from a canonicalized version/filename pair to a
488 // version index. The filename may be NULL.
489 Version_table version_table_;
490 // Whether the version indexes have been set.
494 } // End namespace gold.
496 #endif // !defined(GOLD_DYNOBJ_H)