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
); }
151 // Return the section link field.
153 do_section_link(unsigned int shndx
)
154 { return this->elf_file_
.section_link(shndx
); }
158 typedef Sized_dynobj
<size
, big_endian
> This
;
159 static const int shdr_size
= elfcpp::Elf_sizes
<size
>::shdr_size
;
160 static const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
161 static const int dyn_size
= elfcpp::Elf_sizes
<size
>::dyn_size
;
162 typedef elfcpp::Shdr
<size
, big_endian
> Shdr
;
163 typedef elfcpp::Dyn
<size
, big_endian
> Dyn
;
165 // Find the dynamic symbol table and the version sections, given the
168 find_dynsym_sections(const unsigned char* pshdrs
,
169 unsigned int* pdynshm_shndx
,
170 unsigned int* pversym_shndx
,
171 unsigned int* pverdef_shndx
,
172 unsigned int* pverneed_shndx
,
173 unsigned int* pdynamic_shndx
);
175 // Read the dynamic symbol section SHNDX.
177 read_dynsym_section(const unsigned char* pshdrs
, unsigned int shndx
,
178 elfcpp::SHT type
, unsigned int link
,
179 File_view
** view
, off_t
* view_size
,
180 unsigned int* view_info
);
182 // Set the SONAME from the SHT_DYNAMIC section at DYNAMIC_SHNDX.
183 // The STRTAB parameters may have the relevant string table.
185 set_soname(const unsigned char* pshdrs
, unsigned int dynamic_shndx
,
186 unsigned int strtab_shndx
, const unsigned char* strtabu
,
189 // Mapping from version number to version name.
190 typedef std::vector
<const char*> Version_map
;
192 // Create the version map.
194 make_version_map(Read_symbols_data
* sd
, Version_map
*) const;
196 // Add version definitions to the version map.
198 make_verdef_map(Read_symbols_data
* sd
, Version_map
*) const;
200 // Add version references to the version map.
202 make_verneed_map(Read_symbols_data
* sd
, Version_map
*) const;
204 // Add an entry to the version map.
206 set_version_map(Version_map
*, unsigned int ndx
, const char* name
) const;
208 // General access to the ELF file.
209 elfcpp::Elf_file
<size
, big_endian
, Object
> elf_file_
;
212 // A base class for Verdef and Verneed_version which just handles the
213 // version index which will be stored in the SHT_GNU_versym section.
226 // Return the version index.
230 gold_assert(this->index_
!= -1U);
234 // Set the version index.
236 set_index(unsigned int index
)
238 gold_assert(this->index_
== -1U);
239 this->index_
= index
;
242 // Clear the weak flag in a version definition.
247 Version_base(const Version_base
&);
248 Version_base
& operator=(const Version_base
&);
250 // The index of the version definition or reference.
254 // This class handles a version being defined in the file we are
257 class Verdef
: public Version_base
260 Verdef(const char* name
, bool is_base
, bool is_weak
, bool is_symbol_created
)
261 : name_(name
), deps_(), is_base_(is_base
), is_weak_(is_weak
),
262 is_symbol_created_(is_symbol_created
)
265 // Return the version name.
268 { return this->name_
; }
270 // Return the number of dependencies.
272 count_dependencies() const
273 { return this->deps_
.size(); }
275 // Add a dependency to this version. The NAME should be
276 // canonicalized in the dynamic Stringpool.
278 add_dependency(const char* name
)
279 { this->deps_
.push_back(name
); }
281 // Return whether this definition is weak.
284 { return this->is_weak_
; }
286 // Clear the weak flag.
289 { this->is_weak_
= false; }
291 // Return whether a version symbol has been created for this
294 is_symbol_created() const
295 { return this->is_symbol_created_
; }
297 // Write contents to buffer.
298 template<int size
, bool big_endian
>
300 write(const Stringpool
*, bool is_last
, unsigned char*
301 ACCEPT_SIZE_ENDIAN
) const;
304 Verdef(const Verdef
&);
305 Verdef
& operator=(const Verdef
&);
307 // The type of the list of version dependencies. Each dependency
308 // should be canonicalized in the dynamic Stringpool.
309 typedef std::vector
<const char*> Deps
;
311 // The name of this version. This should be canonicalized in the
312 // dynamic Stringpool.
314 // A list of other versions which this version depends upon.
316 // Whether this is the base version.
318 // Whether this version is weak.
320 // Whether a version symbol has been created.
321 bool is_symbol_created_
;
324 // A referened version. This will be associated with a filename by
327 class Verneed_version
: public Version_base
330 Verneed_version(const char* version
)
334 // Return the version name.
337 { return this->version_
; }
339 // Clear the weak flag. This is invalid for a reference.
342 { gold_unreachable(); }
345 Verneed_version(const Verneed_version
&);
346 Verneed_version
& operator=(const Verneed_version
&);
348 const char* version_
;
351 // Version references in a single dynamic object.
356 Verneed(const char* filename
)
357 : filename_(filename
), need_versions_()
362 // Return the file name.
365 { return this->filename_
; }
367 // Return the number of versions.
369 count_versions() const
370 { return this->need_versions_
.size(); }
372 // Add a version name. The name should be canonicalized in the
373 // dynamic Stringpool. If the name is already present, this does
376 add_name(const char* name
);
378 // Set the version indexes, starting at INDEX. Return the updated
381 finalize(unsigned int index
);
383 // Write contents to buffer.
384 template<int size
, bool big_endian
>
386 write(const Stringpool
*, bool is_last
, unsigned char*
387 ACCEPT_SIZE_ENDIAN
) const;
390 Verneed(const Verneed
&);
391 Verneed
& operator=(const Verneed
&);
393 // The type of the list of version names. Each name should be
394 // canonicalized in the dynamic Stringpool.
395 typedef std::vector
<Verneed_version
*> Need_versions
;
397 // The filename of the dynamic object. This should be
398 // canonicalized in the dynamic Stringpool.
399 const char* filename_
;
400 // The list of version names.
401 Need_versions need_versions_
;
404 // This class handles version definitions and references which go into
411 : defs_(), needs_(), version_table_(), is_finalized_(false)
416 // SYM is going into the dynamic symbol table and has a version.
417 // Record the appropriate version information.
419 record_version(const Symbol_table
* symtab
, Stringpool
*, const Symbol
* sym
);
421 // Set the version indexes. DYNSYM_INDEX is the index we should use
422 // for the next dynamic symbol. We add new dynamic symbols to SYMS
423 // and return an updated DYNSYM_INDEX.
425 finalize(const Target
*, Symbol_table
* symtab
, unsigned int dynsym_index
,
426 std::vector
<Symbol
*>* syms
);
428 // Return whether there are any version definitions.
431 { return !this->defs_
.empty(); }
433 // Return whether there are any version references.
436 { return !this->needs_
.empty(); }
438 // Build an allocated buffer holding the contents of the symbol
439 // version section (.gnu.version).
440 template<int size
, bool big_endian
>
442 symbol_section_contents(const Symbol_table
*, const Stringpool
*,
443 unsigned int local_symcount
,
444 const std::vector
<Symbol
*>& syms
,
445 unsigned char**, unsigned int*
446 ACCEPT_SIZE_ENDIAN
) const;
448 // Build an allocated buffer holding the contents of the version
449 // definition section (.gnu.version_d).
450 template<int size
, bool big_endian
>
452 def_section_contents(const Stringpool
*, unsigned char**,
453 unsigned int* psize
, unsigned int* pentries
454 ACCEPT_SIZE_ENDIAN
) const;
456 // Build an allocated buffer holding the contents of the version
457 // reference section (.gnu.version_r).
458 template<int size
, bool big_endian
>
460 need_section_contents(const Stringpool
*, unsigned char**,
461 unsigned int* psize
, unsigned int* pentries
462 ACCEPT_SIZE_ENDIAN
) const;
465 // The type of the list of version definitions.
466 typedef std::vector
<Verdef
*> Defs
;
468 // The type of the list of version references.
469 typedef std::vector
<Verneed
*> Needs
;
471 // Handle a symbol SYM defined with version VERSION.
473 add_def(const Symbol
* sym
, const char* version
, Stringpool::Key
);
475 // Add a reference to version NAME in file FILENAME.
477 add_need(Stringpool
*, const char* filename
, const char* name
,
480 // Get the dynamic object to use for SYM.
482 get_dynobj_for_sym(const Symbol_table
*, const Symbol
* sym
) const;
484 // Return the version index to use for SYM.
486 version_index(const Symbol_table
*, const Stringpool
*,
487 const Symbol
* sym
) const;
489 // We keep a hash table mapping canonicalized name/version pairs to
491 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Key
;
493 struct Version_table_hash
496 operator()(const Key
& k
) const
497 { return k
.first
+ k
.second
; }
500 struct Version_table_eq
503 operator()(const Key
& k1
, const Key
& k2
) const
504 { return k1
.first
== k2
.first
&& k1
.second
== k2
.second
; }
507 typedef Unordered_map
<Key
, Version_base
*, Version_table_hash
,
508 Version_table_eq
> Version_table
;
510 // The version definitions.
512 // The version references.
514 // The mapping from a canonicalized version/filename pair to a
515 // version index. The filename may be NULL.
516 Version_table version_table_
;
517 // Whether the version indexes have been set.
521 } // End namespace gold.
523 #endif // !defined(GOLD_DYNOBJ_H)