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
;
35 class Version_script_info
;
37 // A dynamic object (ET_DYN). This is an abstract base class itself.
38 // The implementations is the template class Sized_dynobj.
40 class Dynobj
: public Object
43 // We keep a list of all the DT_NEEDED entries we find.
44 typedef std::vector
<std::string
> Needed
;
46 Dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
= 0);
48 // Return the name to use in a DT_NEEDED entry for this object.
51 { return this->soname_
.c_str(); }
53 // Return the list of DT_NEEDED strings.
56 { return this->needed_
; }
58 // Return whether this dynamic object has any DT_NEEDED entries
59 // which were not seen during the link.
61 has_unknown_needed_entries() const
63 gold_assert(this->unknown_needed_
!= UNKNOWN_NEEDED_UNSET
);
64 return this->unknown_needed_
== UNKNOWN_NEEDED_TRUE
;
67 // Set whether this dynamic object has any DT_NEEDED entries which
68 // were not seen during the link.
70 set_has_unknown_needed_entries(bool set
)
72 gold_assert(this->unknown_needed_
== UNKNOWN_NEEDED_UNSET
);
73 this->unknown_needed_
= set
? UNKNOWN_NEEDED_TRUE
: UNKNOWN_NEEDED_FALSE
;
76 // Compute the ELF hash code for a string.
78 elf_hash(const char*);
80 // Create a standard ELF hash table, setting *PPHASH and *PHASHLEN.
81 // DYNSYMS is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the
82 // number of local dynamic symbols, which is the index of the first
83 // dynamic gobal symbol.
85 create_elf_hash_table(const std::vector
<Symbol
*>& dynsyms
,
86 unsigned int local_dynsym_count
,
87 unsigned char** pphash
,
88 unsigned int* phashlen
);
90 // Create a GNU hash table, setting *PPHASH and *PHASHLEN. DYNSYMS
91 // is the global dynamic symbols. LOCAL_DYNSYM_COUNT is the number
92 // of local dynamic symbols, which is the index of the first dynamic
95 create_gnu_hash_table(const std::vector
<Symbol
*>& dynsyms
,
96 unsigned int local_dynsym_count
,
97 unsigned char** pphash
, unsigned int* phashlen
);
100 // Set the DT_SONAME string.
102 set_soname_string(const char* s
)
103 { this->soname_
.assign(s
); }
105 // Add an entry to the list of DT_NEEDED strings.
107 add_needed(const char* s
)
108 { this->needed_
.push_back(std::string(s
)); }
111 // Compute the GNU hash code for a string.
113 gnu_hash(const char*);
115 // Compute the number of hash buckets to use.
117 compute_bucket_count(const std::vector
<uint32_t>& hashcodes
,
118 bool for_gnu_hash_table
);
120 // Sized version of create_elf_hash_table.
121 template<bool big_endian
>
123 sized_create_elf_hash_table(const std::vector
<uint32_t>& bucket
,
124 const std::vector
<uint32_t>& chain
,
125 unsigned char* phash
,
126 unsigned int hashlen
);
128 // Sized version of create_gnu_hash_table.
129 template<int size
, bool big_endian
>
131 sized_create_gnu_hash_table(const std::vector
<Symbol
*>& hashed_dynsyms
,
132 const std::vector
<uint32_t>& dynsym_hashvals
,
133 unsigned int unhashed_dynsym_count
,
134 unsigned char** pphash
,
135 unsigned int* phashlen
);
137 // Values for the has_unknown_needed_entries_ field.
140 UNKNOWN_NEEDED_UNSET
,
145 // The DT_SONAME name, if any.
147 // The list of DT_NEEDED entries.
149 // Whether this dynamic object has any DT_NEEDED entries not seen
151 Unknown_needed unknown_needed_
;
154 // A dynamic object, size and endian specific version.
156 template<int size
, bool big_endian
>
157 class Sized_dynobj
: public Dynobj
160 Sized_dynobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
161 const typename
elfcpp::Ehdr
<size
, big_endian
>&);
163 // Set up the object file based on the ELF header.
165 setup(const typename
elfcpp::Ehdr
<size
, big_endian
>&);
169 do_read_symbols(Read_symbols_data
*);
171 // Lay out the input sections.
173 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
175 // Add the symbols to the symbol table.
177 do_add_symbols(Symbol_table
*, Read_symbols_data
*);
179 // Get the size of a section.
181 do_section_size(unsigned int shndx
)
182 { return this->elf_file_
.section_size(shndx
); }
184 // Get the name of a section.
186 do_section_name(unsigned int shndx
)
187 { return this->elf_file_
.section_name(shndx
); }
189 // Return a view of the contents of a section. Set *PLEN to the
192 do_section_contents(unsigned int shndx
)
193 { return this->elf_file_
.section_contents(shndx
); }
195 // Return section flags.
197 do_section_flags(unsigned int shndx
)
198 { return this->elf_file_
.section_flags(shndx
); }
200 // Return section type.
202 do_section_type(unsigned int shndx
)
203 { return this->elf_file_
.section_type(shndx
); }
205 // Return the section link field.
207 do_section_link(unsigned int shndx
)
208 { return this->elf_file_
.section_link(shndx
); }
210 // Return the section link field.
212 do_section_info(unsigned int shndx
)
213 { return this->elf_file_
.section_info(shndx
); }
215 // Return the section alignment.
217 do_section_addralign(unsigned int shndx
)
218 { return this->elf_file_
.section_addralign(shndx
); }
222 typedef Sized_dynobj
<size
, big_endian
> This
;
223 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
224 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
225 static const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
226 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
227 typedef elfcpp::Dyn
<size
, big_endian
> Dyn
;
229 // Find the dynamic symbol table and the version sections, given the
232 find_dynsym_sections(const unsigned char* pshdrs
,
233 unsigned int* pdynshm_shndx
,
234 unsigned int* pversym_shndx
,
235 unsigned int* pverdef_shndx
,
236 unsigned int* pverneed_shndx
,
237 unsigned int* pdynamic_shndx
);
239 // Read the dynamic symbol section SHNDX.
241 read_dynsym_section(const unsigned char* pshdrs
, unsigned int shndx
,
242 elfcpp::SHT type
, unsigned int link
,
243 File_view
** view
, section_size_type
* view_size
,
244 unsigned int* view_info
);
246 // Read the dynamic tags.
248 read_dynamic(const unsigned char* pshdrs
, unsigned int dynamic_shndx
,
249 unsigned int strtab_shndx
, const unsigned char* strtabu
,
252 // Mapping from version number to version name.
253 typedef std::vector
<const char*> Version_map
;
255 // Create the version map.
257 make_version_map(Read_symbols_data
* sd
, Version_map
*) const;
259 // Add version definitions to the version map.
261 make_verdef_map(Read_symbols_data
* sd
, Version_map
*) const;
263 // Add version references to the version map.
265 make_verneed_map(Read_symbols_data
* sd
, Version_map
*) const;
267 // Add an entry to the version map.
269 set_version_map(Version_map
*, unsigned int ndx
, const char* name
) const;
271 // General access to the ELF file.
272 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
275 // A base class for Verdef and Verneed_version which just handles the
276 // version index which will be stored in the SHT_GNU_versym section.
289 // Return the version index.
293 gold_assert(this->index_
!= -1U);
297 // Set the version index.
299 set_index(unsigned int index
)
301 gold_assert(this->index_
== -1U);
302 this->index_
= index
;
305 // Clear the weak flag in a version definition.
310 Version_base(const Version_base
&);
311 Version_base
& operator=(const Version_base
&);
313 // The index of the version definition or reference.
317 // This class handles a version being defined in the file we are
320 class Verdef
: public Version_base
323 Verdef(const char* name
, const std::vector
<std::string
>& deps
,
324 bool is_base
, bool is_weak
, bool is_symbol_created
)
325 : name_(name
), deps_(deps
), is_base_(is_base
), is_weak_(is_weak
),
326 is_symbol_created_(is_symbol_created
)
329 // Return the version name.
332 { return this->name_
; }
334 // Return the number of dependencies.
336 count_dependencies() const
337 { return this->deps_
.size(); }
339 // Add a dependency to this version. The NAME should be
340 // canonicalized in the dynamic Stringpool.
342 add_dependency(const char* name
)
343 { this->deps_
.push_back(name
); }
345 // Return whether this definition is weak.
348 { return this->is_weak_
; }
350 // Clear the weak flag.
353 { this->is_weak_
= false; }
355 // Return whether a version symbol has been created for this
358 is_symbol_created() const
359 { return this->is_symbol_created_
; }
361 // Write contents to buffer.
362 template<int size
, bool big_endian
>
364 write(const Stringpool
*, bool is_last
, unsigned char*
365 ACCEPT_SIZE_ENDIAN
) const;
368 Verdef(const Verdef
&);
369 Verdef
& operator=(const Verdef
&);
371 // The type of the list of version dependencies. Each dependency
372 // should be canonicalized in the dynamic Stringpool.
373 typedef std::vector
<std::string
> Deps
;
375 // The name of this version. This should be canonicalized in the
376 // dynamic Stringpool.
378 // A list of other versions which this version depends upon.
380 // Whether this is the base version.
382 // Whether this version is weak.
384 // Whether a version symbol has been created.
385 bool is_symbol_created_
;
388 // A referened version. This will be associated with a filename by
391 class Verneed_version
: public Version_base
394 Verneed_version(const char* version
)
398 // Return the version name.
401 { return this->version_
; }
403 // Clear the weak flag. This is invalid for a reference.
406 { gold_unreachable(); }
409 Verneed_version(const Verneed_version
&);
410 Verneed_version
& operator=(const Verneed_version
&);
412 const char* version_
;
415 // Version references in a single dynamic object.
420 Verneed(const char* filename
)
421 : filename_(filename
), need_versions_()
426 // Return the file name.
429 { return this->filename_
; }
431 // Return the number of versions.
433 count_versions() const
434 { return this->need_versions_
.size(); }
436 // Add a version name. The name should be canonicalized in the
437 // dynamic Stringpool. If the name is already present, this does
440 add_name(const char* name
);
442 // Set the version indexes, starting at INDEX. Return the updated
445 finalize(unsigned int index
);
447 // Write contents to buffer.
448 template<int size
, bool big_endian
>
450 write(const Stringpool
*, bool is_last
, unsigned char*
451 ACCEPT_SIZE_ENDIAN
) const;
454 Verneed(const Verneed
&);
455 Verneed
& operator=(const Verneed
&);
457 // The type of the list of version names. Each name should be
458 // canonicalized in the dynamic Stringpool.
459 typedef std::vector
<Verneed_version
*> Need_versions
;
461 // The filename of the dynamic object. This should be
462 // canonicalized in the dynamic Stringpool.
463 const char* filename_
;
464 // The list of version names.
465 Need_versions need_versions_
;
468 // This class handles version definitions and references which go into
474 Versions(const General_options
&, Stringpool
*);
478 // SYM is going into the dynamic symbol table and has a version.
479 // Record the appropriate version information.
481 record_version(const Symbol_table
* symtab
, Stringpool
*, const Symbol
* sym
);
483 // Set the version indexes. DYNSYM_INDEX is the index we should use
484 // for the next dynamic symbol. We add new dynamic symbols to SYMS
485 // and return an updated DYNSYM_INDEX.
487 finalize(Symbol_table
* symtab
, unsigned int dynsym_index
,
488 std::vector
<Symbol
*>* syms
);
490 // Return whether there are any version definitions.
493 { return !this->defs_
.empty(); }
495 // Return whether there are any version references.
498 { return !this->needs_
.empty(); }
500 // Build an allocated buffer holding the contents of the symbol
501 // version section (.gnu.version).
502 template<int size
, bool big_endian
>
504 symbol_section_contents(const Symbol_table
*, const Stringpool
*,
505 unsigned int local_symcount
,
506 const std::vector
<Symbol
*>& syms
,
507 unsigned char**, unsigned int*
508 ACCEPT_SIZE_ENDIAN
) const;
510 // Build an allocated buffer holding the contents of the version
511 // definition section (.gnu.version_d).
512 template<int size
, bool big_endian
>
514 def_section_contents(const Stringpool
*, unsigned char**,
515 unsigned int* psize
, unsigned int* pentries
516 ACCEPT_SIZE_ENDIAN
) const;
518 // Build an allocated buffer holding the contents of the version
519 // reference section (.gnu.version_r).
520 template<int size
, bool big_endian
>
522 need_section_contents(const Stringpool
*, unsigned char**,
523 unsigned int* psize
, unsigned int* pentries
524 ACCEPT_SIZE_ENDIAN
) const;
526 const Version_script_info
&
527 version_script() const
528 { return this->version_script_
; }
531 Versions(const Versions
&);
532 Versions
& operator=(const Versions
&);
534 // The type of the list of version definitions.
535 typedef std::vector
<Verdef
*> Defs
;
537 // The type of the list of version references.
538 typedef std::vector
<Verneed
*> Needs
;
540 // Handle a symbol SYM defined with version VERSION.
542 add_def(const Symbol
* sym
, const char* version
, Stringpool::Key
);
544 // Add a reference to version NAME in file FILENAME.
546 add_need(Stringpool
*, const char* filename
, const char* name
,
549 // Get the dynamic object to use for SYM.
551 get_dynobj_for_sym(const Symbol_table
*, const Symbol
* sym
) const;
553 // Return the version index to use for SYM.
555 version_index(const Symbol_table
*, const Stringpool
*,
556 const Symbol
* sym
) const;
558 // We keep a hash table mapping canonicalized name/version pairs to
560 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Key
;
562 struct Version_table_hash
565 operator()(const Key
& k
) const
566 { return k
.first
+ k
.second
; }
569 struct Version_table_eq
572 operator()(const Key
& k1
, const Key
& k2
) const
573 { return k1
.first
== k2
.first
&& k1
.second
== k2
.second
; }
576 typedef Unordered_map
<Key
, Version_base
*, Version_table_hash
,
577 Version_table_eq
> Version_table
;
579 // The version definitions.
581 // The version references.
583 // The mapping from a canonicalized version/filename pair to a
584 // version index. The filename may be NULL.
585 Version_table version_table_
;
586 // Whether the version indexes have been set.
588 // Contents of --version-script, if passed, or NULL.
589 const Version_script_info
& version_script_
;
592 } // End namespace gold.
594 #endif // !defined(GOLD_DYNOBJ_H)