Add constructor test.
[binutils.git] / gold / dynobj.h
blob189bf682319dbf1ebc214be18ab4ced83bba98c0
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.
23 #ifndef GOLD_DYNOBJ_H
24 #define GOLD_DYNOBJ_H
26 #include <vector>
28 #include "stringpool.h"
29 #include "object.h"
31 namespace gold
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
41 public:
42 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0)
43 : Object(name, input_file, true, offset), soname_()
44 { }
46 // Return the name to use in a DT_NEEDED entry for this object.
47 const char*
48 soname() const;
50 // Compute the ELF hash code for a string.
51 static uint32_t
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.
58 static void
59 create_elf_hash_table(const Target*, 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
67 // gobal symbol.
68 static void
69 create_gnu_hash_table(const Target*, const std::vector<Symbol*>& dynsyms,
70 unsigned int local_dynsym_count,
71 unsigned char** pphash, unsigned int* phashlen);
73 protected:
74 // Set the DT_SONAME string.
75 void
76 set_soname_string(const char* s)
77 { this->soname_.assign(s); }
79 private:
80 // Compute the GNU hash code for a string.
81 static uint32_t
82 gnu_hash(const char*);
84 // Compute the number of hash buckets to use.
85 static unsigned int
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>
91 static void
92 sized_create_elf_hash_table(const std::vector<uint32_t>& bucket,
93 const std::vector<uint32_t>& chain,
94 unsigned char* phash,
95 unsigned int hashlen);
97 // Sized version of create_gnu_hash_table.
98 template<int size, bool big_endian>
99 static void
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.
107 std::string soname_;
110 // A dynamic object, size and endian specific version.
112 template<int size, bool big_endian>
113 class Sized_dynobj : public Dynobj
115 public:
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.
120 void
121 setup(const typename elfcpp::Ehdr<size, big_endian>&);
123 // Read the symbols.
124 void
125 do_read_symbols(Read_symbols_data*);
127 // Lay out the input sections.
128 void
129 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
131 // Add the symbols to the symbol table.
132 void
133 do_add_symbols(Symbol_table*, Read_symbols_data*);
135 // Get the name of a section.
136 std::string
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
141 // size.
142 Object::Location
143 do_section_contents(unsigned int shndx)
144 { return this->elf_file_.section_contents(shndx); }
146 // Return section flags.
147 uint64_t
148 do_section_flags(unsigned int shndx)
149 { return this->elf_file_.section_flags(shndx); }
151 private:
152 // For convenience.
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
161 // section headers.
162 void
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.
171 void
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.
179 void
180 set_soname(const unsigned char* pshdrs, unsigned int dynamic_shndx,
181 unsigned int strtab_shndx, const unsigned char* strtabu,
182 off_t strtab_size);
184 // Mapping from version number to version name.
185 typedef std::vector<const char*> Version_map;
187 // Create the version map.
188 void
189 make_version_map(Read_symbols_data* sd, Version_map*) const;
191 // Add version definitions to the version map.
192 void
193 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
195 // Add version references to the version map.
196 void
197 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
199 // Add an entry to the version map.
200 void
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.
210 class Version_base
212 public:
213 Version_base()
214 : index_(-1U)
217 virtual
218 ~Version_base()
221 // Return the version index.
222 unsigned int
223 index() const
225 gold_assert(this->index_ != -1U);
226 return this->index_;
229 // Set the version index.
230 void
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.
238 virtual void
239 clear_weak() = 0;
241 private:
242 Version_base(const Version_base&);
243 Version_base& operator=(const Version_base&);
245 // The index of the version definition or reference.
246 unsigned int index_;
249 // This class handles a version being defined in the file we are
250 // generating.
252 class Verdef : public Version_base
254 public:
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.
261 const char*
262 name() const
263 { return this->name_; }
265 // Return the number of dependencies.
266 unsigned int
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.
272 void
273 add_dependency(const char* name)
274 { this->deps_.push_back(name); }
276 // Return whether this definition is weak.
277 bool
278 is_weak() const
279 { return this->is_weak_; }
281 // Clear the weak flag.
282 void
283 clear_weak()
284 { this->is_weak_ = false; }
286 // Return whether a version symbol has been created for this
287 // definition.
288 bool
289 is_symbol_created() const
290 { return this->is_symbol_created_; }
292 // Write contents to buffer.
293 template<int size, bool big_endian>
294 unsigned char*
295 write(const Stringpool*, bool is_last, unsigned char*
296 ACCEPT_SIZE_ENDIAN) const;
298 private:
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.
308 const char* name_;
309 // A list of other versions which this version depends upon.
310 Deps deps_;
311 // Whether this is the base version.
312 bool is_base_;
313 // Whether this version is weak.
314 bool 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
320 // Verneed.
322 class Verneed_version : public Version_base
324 public:
325 Verneed_version(const char* version)
326 : version_(version)
329 // Return the version name.
330 const char*
331 version() const
332 { return this->version_; }
334 // Clear the weak flag. This is invalid for a reference.
335 void
336 clear_weak()
337 { gold_unreachable(); }
339 private:
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.
348 class Verneed
350 public:
351 Verneed(const char* filename)
352 : filename_(filename), need_versions_()
355 ~Verneed();
357 // Return the file name.
358 const char*
359 filename() const
360 { return this->filename_; }
362 // Return the number of versions.
363 unsigned int
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
369 // nothing.
370 Verneed_version*
371 add_name(const char* name);
373 // Set the version indexes, starting at INDEX. Return the updated
374 // INDEX.
375 unsigned int
376 finalize(unsigned int index);
378 // Write contents to buffer.
379 template<int size, bool big_endian>
380 unsigned char*
381 write(const Stringpool*, bool is_last, unsigned char*
382 ACCEPT_SIZE_ENDIAN) const;
384 private:
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
400 // the output file.
402 class Versions
404 public:
405 Versions()
406 : defs_(), needs_(), version_table_(), is_finalized_(false)
409 ~Versions();
411 // SYM is going into the dynamic symbol table and has a version.
412 // Record the appropriate version information.
413 void
414 record_version(const General_options*, 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.
419 unsigned int
420 finalize(const Target*, Symbol_table* symtab, unsigned int dynsym_index,
421 std::vector<Symbol*>* syms);
423 // Return whether there are any version definitions.
424 bool
425 any_defs() const
426 { return !this->defs_.empty(); }
428 // Return whether there are any version references.
429 bool
430 any_needs() const
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>
436 void
437 symbol_section_contents(const Stringpool*, unsigned int local_symcount,
438 const std::vector<Symbol*>& syms,
439 unsigned char**, unsigned int*
440 ACCEPT_SIZE_ENDIAN) const;
442 // Build an allocated buffer holding the contents of the version
443 // definition section (.gnu.version_d).
444 template<int size, bool big_endian>
445 void
446 def_section_contents(const Stringpool*, unsigned char**,
447 unsigned int* psize, unsigned int* pentries
448 ACCEPT_SIZE_ENDIAN) const;
450 // Build an allocated buffer holding the contents of the version
451 // reference section (.gnu.version_r).
452 template<int size, bool big_endian>
453 void
454 need_section_contents(const Stringpool*, unsigned char**,
455 unsigned int* psize, unsigned int* pentries
456 ACCEPT_SIZE_ENDIAN) const;
458 private:
459 // The type of the list of version definitions.
460 typedef std::vector<Verdef*> Defs;
462 // The type of the list of version references.
463 typedef std::vector<Verneed*> Needs;
465 // Handle a symbol SYM defined with version VERSION.
466 void
467 add_def(const General_options*, const Symbol* sym, const char* version,
468 Stringpool::Key);
470 // Add a reference to version NAME in file FILENAME.
471 void
472 add_need(Stringpool*, const char* filename, const char* name,
473 Stringpool::Key);
475 // Return the version index to use for SYM.
476 unsigned int
477 version_index(const Stringpool*, const Symbol* sym) const;
479 // We keep a hash table mapping canonicalized name/version pairs to
480 // a version base.
481 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
483 struct Version_table_hash
485 size_t
486 operator()(const Key& k) const
487 { return k.first + k.second; }
490 struct Version_table_eq
492 bool
493 operator()(const Key& k1, const Key& k2) const
494 { return k1.first == k2.first && k1.second == k2.second; }
497 typedef Unordered_map<Key, Version_base*, Version_table_hash,
498 Version_table_eq> Version_table;
500 // The version definitions.
501 Defs defs_;
502 // The version references.
503 Needs needs_;
504 // The mapping from a canonicalized version/filename pair to a
505 // version index. The filename may be NULL.
506 Version_table version_table_;
507 // Whether the version indexes have been set.
508 bool is_finalized_;
511 } // End namespace gold.
513 #endif // !defined(GOLD_DYNOBJ_H)