* config/tc-arm.c (do_t_add_sub_w): Fixed constraints.
[binutils.git] / gold / dynobj.h
bloba15e2a43238dc1d447dece1818fc79eb201ae128
1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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 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
41 public:
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.
48 const char*
49 soname() const
50 { return this->soname_.c_str(); }
52 // Return the list of DT_NEEDED strings.
53 const Needed&
54 needed() const
55 { return this->needed_; }
57 // Return whether this dynamic object has any DT_NEEDED entries
58 // which were not seen during the link.
59 bool
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.
68 void
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.
76 static uint32_t
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.
83 static void
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
92 // gobal symbol.
93 static void
94 create_gnu_hash_table(const std::vector<Symbol*>& dynsyms,
95 unsigned int local_dynsym_count,
96 unsigned char** pphash, unsigned int* phashlen);
98 protected:
99 // Set the DT_SONAME string.
100 void
101 set_soname_string(const char* s)
102 { this->soname_.assign(s); }
104 // Add an entry to the list of DT_NEEDED strings.
105 void
106 add_needed(const char* s)
107 { this->needed_.push_back(std::string(s)); }
109 private:
110 // Compute the GNU hash code for a string.
111 static uint32_t
112 gnu_hash(const char*);
114 // Compute the number of hash buckets to use.
115 static unsigned int
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>
121 static void
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>
129 static void
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.
137 enum Unknown_needed
139 UNKNOWN_NEEDED_UNSET,
140 UNKNOWN_NEEDED_TRUE,
141 UNKNOWN_NEEDED_FALSE
144 // The DT_SONAME name, if any.
145 std::string soname_;
146 // The list of DT_NEEDED entries.
147 Needed needed_;
148 // Whether this dynamic object has any DT_NEEDED entries not seen
149 // during the link.
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
158 public:
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 the ELF header.
165 void
166 setup(const typename elfcpp::Ehdr<size, big_endian>&);
168 // Read the symbols.
169 void
170 do_read_symbols(Read_symbols_data*);
172 // Lay out the input sections.
173 void
174 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
176 // Add the symbols to the symbol table.
177 void
178 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
180 // Get the size of a section.
181 uint64_t
182 do_section_size(unsigned int shndx)
183 { return this->elf_file_.section_size(shndx); }
185 // Get the name of a section.
186 std::string
187 do_section_name(unsigned int shndx)
188 { return this->elf_file_.section_name(shndx); }
190 // Return a view of the contents of a section. Set *PLEN to the
191 // size.
192 Object::Location
193 do_section_contents(unsigned int shndx)
194 { return this->elf_file_.section_contents(shndx); }
196 // Return section flags.
197 uint64_t
198 do_section_flags(unsigned int shndx)
199 { return this->elf_file_.section_flags(shndx); }
201 // Not used for dynobj.
202 uint64_t
203 do_section_entsize(unsigned int )
204 { gold_unreachable(); }
206 // Return section address.
207 uint64_t
208 do_section_address(unsigned int shndx)
209 { return this->elf_file_.section_addr(shndx); }
211 // Return section type.
212 unsigned int
213 do_section_type(unsigned int shndx)
214 { return this->elf_file_.section_type(shndx); }
216 // Return the section link field.
217 unsigned int
218 do_section_link(unsigned int shndx)
219 { return this->elf_file_.section_link(shndx); }
221 // Return the section link field.
222 unsigned int
223 do_section_info(unsigned int shndx)
224 { return this->elf_file_.section_info(shndx); }
226 // Return the section alignment.
227 uint64_t
228 do_section_addralign(unsigned int shndx)
229 { return this->elf_file_.section_addralign(shndx); }
231 // Return the Xindex structure to use.
232 Xindex*
233 do_initialize_xindex();
235 // Get symbol counts.
236 void
237 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
239 private:
240 // For convenience.
241 typedef Sized_dynobj<size, big_endian> This;
242 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
243 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
244 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
245 typedef elfcpp::Shdr<size, big_endian> Shdr;
246 typedef elfcpp::Dyn<size, big_endian> Dyn;
248 // Adjust a section index if necessary.
249 unsigned int
250 adjust_shndx(unsigned int shndx)
252 if (shndx >= elfcpp::SHN_LORESERVE)
253 shndx += this->elf_file_.large_shndx_offset();
254 return shndx;
257 // Find the dynamic symbol table and the version sections, given the
258 // section headers.
259 void
260 find_dynsym_sections(const unsigned char* pshdrs,
261 unsigned int* pversym_shndx,
262 unsigned int* pverdef_shndx,
263 unsigned int* pverneed_shndx,
264 unsigned int* pdynamic_shndx);
266 // Read the dynamic symbol section SHNDX.
267 void
268 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
269 elfcpp::SHT type, unsigned int link,
270 File_view** view, section_size_type* view_size,
271 unsigned int* view_info);
273 // Read the dynamic tags.
274 void
275 read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
276 unsigned int strtab_shndx, const unsigned char* strtabu,
277 off_t strtab_size);
279 // Mapping from version number to version name.
280 typedef std::vector<const char*> Version_map;
282 // Create the version map.
283 void
284 make_version_map(Read_symbols_data* sd, Version_map*) const;
286 // Add version definitions to the version map.
287 void
288 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
290 // Add version references to the version map.
291 void
292 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
294 // Add an entry to the version map.
295 void
296 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
298 // General access to the ELF file.
299 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
300 // The section index of the dynamic symbol table.
301 unsigned int dynsym_shndx_;
302 // The entries in the symbol table for the symbols. We only keep
303 // this if we need it to print symbol information.
304 Symbols* symbols_;
305 // Number of defined symbols.
306 size_t defined_count_;
309 // A base class for Verdef and Verneed_version which just handles the
310 // version index which will be stored in the SHT_GNU_versym section.
312 class Version_base
314 public:
315 Version_base()
316 : index_(-1U)
319 virtual
320 ~Version_base()
323 // Return the version index.
324 unsigned int
325 index() const
327 gold_assert(this->index_ != -1U);
328 return this->index_;
331 // Set the version index.
332 void
333 set_index(unsigned int index)
335 gold_assert(this->index_ == -1U);
336 this->index_ = index;
339 // Clear the weak flag in a version definition.
340 virtual void
341 clear_weak() = 0;
343 private:
344 Version_base(const Version_base&);
345 Version_base& operator=(const Version_base&);
347 // The index of the version definition or reference.
348 unsigned int index_;
351 // This class handles a version being defined in the file we are
352 // generating.
354 class Verdef : public Version_base
356 public:
357 Verdef(const char* name, const std::vector<std::string>& deps,
358 bool is_base, bool is_weak, bool is_symbol_created)
359 : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
360 is_symbol_created_(is_symbol_created)
363 // Return the version name.
364 const char*
365 name() const
366 { return this->name_; }
368 // Return the number of dependencies.
369 unsigned int
370 count_dependencies() const
371 { return this->deps_.size(); }
373 // Add a dependency to this version. The NAME should be
374 // canonicalized in the dynamic Stringpool.
375 void
376 add_dependency(const char* name)
377 { this->deps_.push_back(name); }
379 // Return whether this definition is weak.
380 bool
381 is_weak() const
382 { return this->is_weak_; }
384 // Clear the weak flag.
385 void
386 clear_weak()
387 { this->is_weak_ = false; }
389 // Return whether a version symbol has been created for this
390 // definition.
391 bool
392 is_symbol_created() const
393 { return this->is_symbol_created_; }
395 // Write contents to buffer.
396 template<int size, bool big_endian>
397 unsigned char*
398 write(const Stringpool*, bool is_last, unsigned char*) const;
400 private:
401 Verdef(const Verdef&);
402 Verdef& operator=(const Verdef&);
404 // The type of the list of version dependencies. Each dependency
405 // should be canonicalized in the dynamic Stringpool.
406 typedef std::vector<std::string> Deps;
408 // The name of this version. This should be canonicalized in the
409 // dynamic Stringpool.
410 const char* name_;
411 // A list of other versions which this version depends upon.
412 Deps deps_;
413 // Whether this is the base version.
414 bool is_base_;
415 // Whether this version is weak.
416 bool is_weak_;
417 // Whether a version symbol has been created.
418 bool is_symbol_created_;
421 // A referened version. This will be associated with a filename by
422 // Verneed.
424 class Verneed_version : public Version_base
426 public:
427 Verneed_version(const char* version)
428 : version_(version)
431 // Return the version name.
432 const char*
433 version() const
434 { return this->version_; }
436 // Clear the weak flag. This is invalid for a reference.
437 void
438 clear_weak()
439 { gold_unreachable(); }
441 private:
442 Verneed_version(const Verneed_version&);
443 Verneed_version& operator=(const Verneed_version&);
445 const char* version_;
448 // Version references in a single dynamic object.
450 class Verneed
452 public:
453 Verneed(const char* filename)
454 : filename_(filename), need_versions_()
457 ~Verneed();
459 // Return the file name.
460 const char*
461 filename() const
462 { return this->filename_; }
464 // Return the number of versions.
465 unsigned int
466 count_versions() const
467 { return this->need_versions_.size(); }
469 // Add a version name. The name should be canonicalized in the
470 // dynamic Stringpool. If the name is already present, this does
471 // nothing.
472 Verneed_version*
473 add_name(const char* name);
475 // Set the version indexes, starting at INDEX. Return the updated
476 // INDEX.
477 unsigned int
478 finalize(unsigned int index);
480 // Write contents to buffer.
481 template<int size, bool big_endian>
482 unsigned char*
483 write(const Stringpool*, bool is_last, unsigned char*) const;
485 private:
486 Verneed(const Verneed&);
487 Verneed& operator=(const Verneed&);
489 // The type of the list of version names. Each name should be
490 // canonicalized in the dynamic Stringpool.
491 typedef std::vector<Verneed_version*> Need_versions;
493 // The filename of the dynamic object. This should be
494 // canonicalized in the dynamic Stringpool.
495 const char* filename_;
496 // The list of version names.
497 Need_versions need_versions_;
500 // This class handles version definitions and references which go into
501 // the output file.
503 class Versions
505 public:
506 Versions(const Version_script_info&, Stringpool*);
508 ~Versions();
510 // SYM is going into the dynamic symbol table and has a version.
511 // Record the appropriate version information.
512 void
513 record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
515 // Set the version indexes. DYNSYM_INDEX is the index we should use
516 // for the next dynamic symbol. We add new dynamic symbols to SYMS
517 // and return an updated DYNSYM_INDEX.
518 unsigned int
519 finalize(Symbol_table* symtab, unsigned int dynsym_index,
520 std::vector<Symbol*>* syms);
522 // Return whether there are any version definitions.
523 bool
524 any_defs() const
525 { return !this->defs_.empty(); }
527 // Return whether there are any version references.
528 bool
529 any_needs() const
530 { return !this->needs_.empty(); }
532 // Build an allocated buffer holding the contents of the symbol
533 // version section (.gnu.version).
534 template<int size, bool big_endian>
535 void
536 symbol_section_contents(const Symbol_table*, const Stringpool*,
537 unsigned int local_symcount,
538 const std::vector<Symbol*>& syms,
539 unsigned char**, unsigned int*) const;
541 // Build an allocated buffer holding the contents of the version
542 // definition section (.gnu.version_d).
543 template<int size, bool big_endian>
544 void
545 def_section_contents(const Stringpool*, unsigned char**,
546 unsigned int* psize, unsigned int* pentries) const;
548 // Build an allocated buffer holding the contents of the version
549 // reference section (.gnu.version_r).
550 template<int size, bool big_endian>
551 void
552 need_section_contents(const Stringpool*, unsigned char**,
553 unsigned int* psize, unsigned int* pentries) const;
555 const Version_script_info&
556 version_script() const
557 { return this->version_script_; }
559 private:
560 Versions(const Versions&);
561 Versions& operator=(const Versions&);
563 // The type of the list of version definitions.
564 typedef std::vector<Verdef*> Defs;
566 // The type of the list of version references.
567 typedef std::vector<Verneed*> Needs;
569 // Handle a symbol SYM defined with version VERSION.
570 void
571 add_def(const Symbol* sym, const char* version, Stringpool::Key);
573 // Add a reference to version NAME in file FILENAME.
574 void
575 add_need(Stringpool*, const char* filename, const char* name,
576 Stringpool::Key);
578 // Get the dynamic object to use for SYM.
579 Dynobj*
580 get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
582 // Return the version index to use for SYM.
583 unsigned int
584 version_index(const Symbol_table*, const Stringpool*,
585 const Symbol* sym) const;
587 // We keep a hash table mapping canonicalized name/version pairs to
588 // a version base.
589 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
591 struct Version_table_hash
593 size_t
594 operator()(const Key& k) const
595 { return k.first + k.second; }
598 struct Version_table_eq
600 bool
601 operator()(const Key& k1, const Key& k2) const
602 { return k1.first == k2.first && k1.second == k2.second; }
605 typedef Unordered_map<Key, Version_base*, Version_table_hash,
606 Version_table_eq> Version_table;
608 // The version definitions.
609 Defs defs_;
610 // The version references.
611 Needs needs_;
612 // The mapping from a canonicalized version/filename pair to a
613 // version index. The filename may be NULL.
614 Version_table version_table_;
615 // Whether the version indexes have been set.
616 bool is_finalized_;
617 // Contents of --version-script, if passed, or NULL.
618 const Version_script_info& version_script_;
621 } // End namespace gold.
623 #endif // !defined(GOLD_DYNOBJ_H)