1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 // Compute the ELF hash code for a string.
77 elf_hash(const char*);
79 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
80 // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
81 // number of local dynamic symbols, which is the index of the first
82 // dynamic gobal symbol.
84 create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
85 unsigned int local_dynsym_count
,
86 unsigned char** pphash
,
87 unsigned int* phashlen
);
89 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
90 // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
91 // of local dynamic symbols, which is the index of the first dynamic
94 create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
95 unsigned int local_dynsym_count
,
96 unsigned char** pphash
, unsigned int* phashlen
);
99 // Set the DT_SONAME string.
101 set_soname_string(const char* s
)
102 { this->soname_
.assign(s
); }
104 // Add an entry to the list of DT_NEEDED strings.
106 add_needed(const char* s
)
107 { this->needed_
.push_back(std::string(s
)); }
110 // Compute the GNU hash code for a string.
112 gnu_hash(const char*);
114 // Compute the number of hash buckets to use.
116 compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
117 bool for_gnu_hash_table
);
119 // Sized version of create_elf_hash_table.
120 template<bool big_endian
>
122 sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
123 const std::vector
<uint32_t>& chain
,
124 unsigned char* phash
,
125 unsigned int hashlen
);
127 // Sized version of create_gnu_hash_table.
128 template<int size
, bool big_endian
>
130 sized_create_gnu_hash_table(const std::vector
<Symbol
*>& hashed_dynsyms
,
131 const std::vector
<uint32_t>& dynsym_hashvals
,
132 unsigned int unhashed_dynsym_count
,
133 unsigned char** pphash
,
134 unsigned int* phashlen
);
136 // Values for the has_unknown_needed_entries_ field.
139 UNKNOWN_NEEDED_UNSET
,
144 // The DT_SONAME name, if any.
146 // The list of DT_NEEDED entries.
148 // Whether this dynamic object has any DT_NEEDED entries not seen
150 Unknown_needed unknown_needed_
;
153 // A dynamic object, size and endian specific version.
155 template<int size
, bool big_endian
>
156 class Sized_dynobj
: public Dynobj
159 typedef typename Sized_relobj
<size
, big_endian
>::Symbols Symbols
;
161 Sized_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
162 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
164 // Set up the object file based on TARGET.
170 do_read_symbols(Read_symbols_data
*);
172 // Lay out the input sections.
174 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
176 // Add the symbols to the symbol table.
178 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*);
180 Archive::Should_include
181 do_should_include_member(Symbol_table
* symtab
, Layout
*, Read_symbols_data
*,
184 // Get the size of a section.
186 do_section_size(unsigned int shndx
)
187 { return this->elf_file_
.section_size(shndx
); }
189 // Get the name of a section.
191 do_section_name(unsigned int shndx
)
192 { return this->elf_file_
.section_name(shndx
); }
194 // Return a view of the contents of a section. Set *PLEN to the
197 do_section_contents(unsigned int shndx
)
198 { return this->elf_file_
.section_contents(shndx
); }
200 // Return section flags.
202 do_section_flags(unsigned int shndx
)
203 { return this->elf_file_
.section_flags(shndx
); }
205 // Not used for dynobj.
207 do_section_entsize(unsigned int )
208 { gold_unreachable(); }
210 // Return section address.
212 do_section_address(unsigned int shndx
)
213 { return this->elf_file_
.section_addr(shndx
); }
215 // Return section type.
217 do_section_type(unsigned int shndx
)
218 { return this->elf_file_
.section_type(shndx
); }
220 // Return the section link field.
222 do_section_link(unsigned int shndx
)
223 { return this->elf_file_
.section_link(shndx
); }
225 // Return the section link field.
227 do_section_info(unsigned int shndx
)
228 { return this->elf_file_
.section_info(shndx
); }
230 // Return the section alignment.
232 do_section_addralign(unsigned int shndx
)
233 { return this->elf_file_
.section_addralign(shndx
); }
235 // Return the Xindex structure to use.
237 do_initialize_xindex();
239 // Get symbol counts.
241 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const;
243 // Get the global symbols.
245 do_get_global_symbols() const
246 { return this->symbols_
; }
250 typedef Sized_dynobj
<size
, big_endian
> This
;
251 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
252 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
253 static const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
254 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
255 typedef elfcpp::Dyn
<size
, big_endian
> Dyn
;
257 // Adjust a section index if necessary.
259 adjust_shndx(unsigned int shndx
)
261 if (shndx
>= elfcpp::SHN_LORESERVE
)
262 shndx
+= this->elf_file_
.large_shndx_offset();
266 // Find the dynamic symbol table and the version sections, given the
269 find_dynsym_sections(const unsigned char* pshdrs
,
270 unsigned int* pversym_shndx
,
271 unsigned int* pverdef_shndx
,
272 unsigned int* pverneed_shndx
,
273 unsigned int* pdynamic_shndx
);
275 // Read the dynamic symbol section SHNDX.
277 read_dynsym_section(const unsigned char* pshdrs
, unsigned int shndx
,
278 elfcpp::SHT type
, unsigned int link
,
279 File_view
** view
, section_size_type
* view_size
,
280 unsigned int* view_info
);
282 // Read the dynamic tags.
284 read_dynamic(const unsigned char* pshdrs
, unsigned int dynamic_shndx
,
285 unsigned int strtab_shndx
, const unsigned char* strtabu
,
288 // Mapping from version number to version name.
289 typedef std::vector
<const char*> Version_map
;
291 // Create the version map.
293 make_version_map(Read_symbols_data
* sd
, Version_map
*) const;
295 // Add version definitions to the version map.
297 make_verdef_map(Read_symbols_data
* sd
, Version_map
*) const;
299 // Add version references to the version map.
301 make_verneed_map(Read_symbols_data
* sd
, Version_map
*) const;
303 // Add an entry to the version map.
305 set_version_map(Version_map
*, unsigned int ndx
, const char* name
) const;
307 // General access to the ELF file.
308 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
309 // The section index of the dynamic symbol table.
310 unsigned int dynsym_shndx_
;
311 // The entries in the symbol table for the symbols. We only keep
312 // this if we need it to print symbol information.
314 // Number of defined symbols.
315 size_t defined_count_
;
318 // A base class for Verdef and Verneed_version which just handles the
319 // version index which will be stored in the SHT_GNU_versym section.
332 // Return the version index.
336 gold_assert(this->index_
!= -1U);
340 // Set the version index.
342 set_index(unsigned int index
)
344 gold_assert(this->index_
== -1U);
345 this->index_
= index
;
348 // Clear the weak flag in a version definition.
353 Version_base(const Version_base
&);
354 Version_base
& operator=(const Version_base
&);
356 // The index of the version definition or reference.
360 // This class handles a version being defined in the file we are
363 class Verdef
: public Version_base
366 Verdef(const char* name
, const std::vector
<std::string
>& deps
,
367 bool is_base
, bool is_weak
, bool is_info
, bool is_symbol_created
)
368 : name_(name
), deps_(deps
), is_base_(is_base
), is_weak_(is_weak
),
369 is_info_(is_info
), is_symbol_created_(is_symbol_created
)
372 // Return the version name.
375 { return this->name_
; }
377 // Return the number of dependencies.
379 count_dependencies() const
380 { return this->deps_
.size(); }
382 // Add a dependency to this version. The NAME should be
383 // canonicalized in the dynamic Stringpool.
385 add_dependency(const char* name
)
386 { this->deps_
.push_back(name
); }
388 // Return whether this definition is weak.
391 { return this->is_weak_
; }
393 // Clear the weak flag.
396 { this->is_weak_
= false; }
398 // Return whether this definition is informational.
401 { return this->is_info_
; }
403 // Return whether a version symbol has been created for this
406 is_symbol_created() const
407 { return this->is_symbol_created_
; }
409 // Write contents to buffer.
410 template<int size
, bool big_endian
>
412 write(const Stringpool
*, bool is_last
, unsigned char*) const;
415 Verdef(const Verdef
&);
416 Verdef
& operator=(const Verdef
&);
418 // The type of the list of version dependencies. Each dependency
419 // should be canonicalized in the dynamic Stringpool.
420 typedef std::vector
<std::string
> Deps
;
422 // The name of this version. This should be canonicalized in the
423 // dynamic Stringpool.
425 // A list of other versions which this version depends upon.
427 // Whether this is the base version.
429 // Whether this version is weak.
431 // Whether this version is informational.
433 // Whether a version symbol has been created.
434 bool is_symbol_created_
;
437 // A referened version. This will be associated with a filename by
440 class Verneed_version
: public Version_base
443 Verneed_version(const char* version
)
447 // Return the version name.
450 { return this->version_
; }
452 // Clear the weak flag. This is invalid for a reference.
455 { gold_unreachable(); }
458 Verneed_version(const Verneed_version
&);
459 Verneed_version
& operator=(const Verneed_version
&);
461 const char* version_
;
464 // Version references in a single dynamic object.
469 Verneed(const char* filename
)
470 : filename_(filename
), need_versions_()
475 // Return the file name.
478 { return this->filename_
; }
480 // Return the number of versions.
482 count_versions() const
483 { return this->need_versions_
.size(); }
485 // Add a version name. The name should be canonicalized in the
486 // dynamic Stringpool. If the name is already present, this does
489 add_name(const char* name
);
491 // Set the version indexes, starting at INDEX. Return the updated
494 finalize(unsigned int index
);
496 // Write contents to buffer.
497 template<int size
, bool big_endian
>
499 write(const Stringpool
*, bool is_last
, unsigned char*) const;
502 Verneed(const Verneed
&);
503 Verneed
& operator=(const Verneed
&);
505 // The type of the list of version names. Each name should be
506 // canonicalized in the dynamic Stringpool.
507 typedef std::vector
<Verneed_version
*> Need_versions
;
509 // The filename of the dynamic object. This should be
510 // canonicalized in the dynamic Stringpool.
511 const char* filename_
;
512 // The list of version names.
513 Need_versions need_versions_
;
516 // This class handles version definitions and references which go into
522 Versions(const Version_script_info
&, Stringpool
*);
526 // SYM is going into the dynamic symbol table and has a version.
527 // Record the appropriate version information.
529 record_version(const Symbol_table
* symtab
, Stringpool
*, const Symbol
* sym
);
531 // Set the version indexes. DYNSYM_INDEX is the index we should use
532 // for the next dynamic symbol. We add new dynamic symbols to SYMS
533 // and return an updated DYNSYM_INDEX.
535 finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
536 std::vector
<Symbol
*>* syms
);
538 // Return whether there are any version definitions.
541 { return !this->defs_
.empty(); }
543 // Return whether there are any version references.
546 { return !this->needs_
.empty(); }
548 // Build an allocated buffer holding the contents of the symbol
549 // version section (.gnu.version).
550 template<int size
, bool big_endian
>
552 symbol_section_contents(const Symbol_table
*, const Stringpool
*,
553 unsigned int local_symcount
,
554 const std::vector
<Symbol
*>& syms
,
555 unsigned char**, unsigned int*) const;
557 // Build an allocated buffer holding the contents of the version
558 // definition section (.gnu.version_d).
559 template<int size
, bool big_endian
>
561 def_section_contents(const Stringpool
*, unsigned char**,
562 unsigned int* psize
, unsigned int* pentries
) const;
564 // Build an allocated buffer holding the contents of the version
565 // reference section (.gnu.version_r).
566 template<int size
, bool big_endian
>
568 need_section_contents(const Stringpool
*, unsigned char**,
569 unsigned int* psize
, unsigned int* pentries
) const;
571 const Version_script_info
&
572 version_script() const
573 { return this->version_script_
; }
576 Versions(const Versions
&);
577 Versions
& operator=(const Versions
&);
579 // The type of the list of version definitions.
580 typedef std::vector
<Verdef
*> Defs
;
582 // The type of the list of version references.
583 typedef std::vector
<Verneed
*> Needs
;
585 // Handle a symbol SYM defined with version VERSION.
587 add_def(const Symbol
* sym
, const char* version
, Stringpool::Key
);
589 // Add a reference to version NAME in file FILENAME.
591 add_need(Stringpool
*, const char* filename
, const char* name
,
594 // Get the dynamic object to use for SYM.
596 get_dynobj_for_sym(const Symbol_table
*, const Symbol
* sym
) const;
598 // Return the version index to use for SYM.
600 version_index(const Symbol_table
*, const Stringpool
*,
601 const Symbol
* sym
) const;
603 // Define the base version of a shared library.
605 define_base_version(Stringpool
* dynpool
);
607 // We keep a hash table mapping canonicalized name/version pairs to
609 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Key
;
611 struct Version_table_hash
614 operator()(const Key
& k
) const
615 { return k
.first
+ k
.second
; }
618 struct Version_table_eq
621 operator()(const Key
& k1
, const Key
& k2
) const
622 { return k1
.first
== k2
.first
&& k1
.second
== k2
.second
; }
625 typedef Unordered_map
<Key
, Version_base
*, Version_table_hash
,
626 Version_table_eq
> Version_table
;
628 // The version definitions.
630 // The version references.
632 // The mapping from a canonicalized version/filename pair to a
633 // version index. The filename may be NULL.
634 Version_table version_table_
;
635 // Whether the version indexes have been set.
637 // Contents of --version-script, if passed, or NULL.
638 const Version_script_info
& version_script_
;
639 // Whether we need to insert a base version. This is only used for
640 // shared libaries and is cleared when the base version is defined.
641 bool needs_base_version_
;
644 } // End namespace gold.
646 #endif // !defined(GOLD_DYNOBJ_H)