include:
[deliverable/binutils-gdb.git] / gold / dynobj.h
1 // dynobj.h -- dynamic object support for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #ifndef GOLD_DYNOBJ_H
24 #define GOLD_DYNOBJ_H
25
26 #include <vector>
27
28 #include "stringpool.h"
29 #include "object.h"
30
31 namespace gold
32 {
33
34 class Version_script_info;
35
36 // A dynamic object (ET_DYN). This is an abstract base class itself.
37 // The implementations is the template class Sized_dynobj.
38
39 class Dynobj : public Object
40 {
41 public:
42 // We keep a list of all the DT_NEEDED entries we find.
43 typedef std::vector<std::string> Needed;
44
45 Dynobj(const std::string& name, Input_file* input_file, off_t offset = 0);
46
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(); }
51
52 // Return the list of DT_NEEDED strings.
53 const Needed&
54 needed() const
55 { return this->needed_; }
56
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
61 {
62 gold_assert(this->unknown_needed_ != UNKNOWN_NEEDED_UNSET);
63 return this->unknown_needed_ == UNKNOWN_NEEDED_TRUE;
64 }
65
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)
70 {
71 gold_assert(this->unknown_needed_ == UNKNOWN_NEEDED_UNSET);
72 this->unknown_needed_ = set ? UNKNOWN_NEEDED_TRUE : UNKNOWN_NEEDED_FALSE;
73 }
74
75 // Compute the ELF hash code for a string.
76 static uint32_t
77 elf_hash(const char*);
78
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);
88
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);
97
98 protected:
99 // Set the DT_SONAME string.
100 void
101 set_soname_string(const char* s)
102 { this->soname_.assign(s); }
103
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)); }
108
109 private:
110 // Compute the GNU hash code for a string.
111 static uint32_t
112 gnu_hash(const char*);
113
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);
118
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);
126
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);
135
136 // Values for the has_unknown_needed_entries_ field.
137 enum Unknown_needed
138 {
139 UNKNOWN_NEEDED_UNSET,
140 UNKNOWN_NEEDED_TRUE,
141 UNKNOWN_NEEDED_FALSE
142 };
143
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_;
151 };
152
153 // A dynamic object, size and endian specific version.
154
155 template<int size, bool big_endian>
156 class Sized_dynobj : public Dynobj
157 {
158 public:
159 typedef typename Sized_relobj<size, big_endian>::Symbols Symbols;
160
161 Sized_dynobj(const std::string& name, Input_file* input_file, off_t offset,
162 const typename elfcpp::Ehdr<size, big_endian>&);
163
164 // Set up the object file based on TARGET.
165 void
166 setup();
167
168 // Read the symbols.
169 void
170 do_read_symbols(Read_symbols_data*);
171
172 // Lay out the input sections.
173 void
174 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
175
176 // Add the symbols to the symbol table.
177 void
178 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
179
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); }
184
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); }
189
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); }
195
196 // Return section flags.
197 uint64_t
198 do_section_flags(unsigned int shndx)
199 { return this->elf_file_.section_flags(shndx); }
200
201 // Not used for dynobj.
202 uint64_t
203 do_section_entsize(unsigned int )
204 { gold_unreachable(); }
205
206 // Return section address.
207 uint64_t
208 do_section_address(unsigned int shndx)
209 { return this->elf_file_.section_addr(shndx); }
210
211 // Return section type.
212 unsigned int
213 do_section_type(unsigned int shndx)
214 { return this->elf_file_.section_type(shndx); }
215
216 // Return the section link field.
217 unsigned int
218 do_section_link(unsigned int shndx)
219 { return this->elf_file_.section_link(shndx); }
220
221 // Return the section link field.
222 unsigned int
223 do_section_info(unsigned int shndx)
224 { return this->elf_file_.section_info(shndx); }
225
226 // Return the section alignment.
227 uint64_t
228 do_section_addralign(unsigned int shndx)
229 { return this->elf_file_.section_addralign(shndx); }
230
231 // Return the Xindex structure to use.
232 Xindex*
233 do_initialize_xindex();
234
235 // Get symbol counts.
236 void
237 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
238
239 // Get the global symbols.
240 const Symbols*
241 do_get_global_symbols() const
242 { return this->symbols_; }
243
244 private:
245 // For convenience.
246 typedef Sized_dynobj<size, big_endian> This;
247 static const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
248 static const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
249 static const int dyn_size = elfcpp::Elf_sizes<size>::dyn_size;
250 typedef elfcpp::Shdr<size, big_endian> Shdr;
251 typedef elfcpp::Dyn<size, big_endian> Dyn;
252
253 // Adjust a section index if necessary.
254 unsigned int
255 adjust_shndx(unsigned int shndx)
256 {
257 if (shndx >= elfcpp::SHN_LORESERVE)
258 shndx += this->elf_file_.large_shndx_offset();
259 return shndx;
260 }
261
262 // Find the dynamic symbol table and the version sections, given the
263 // section headers.
264 void
265 find_dynsym_sections(const unsigned char* pshdrs,
266 unsigned int* pversym_shndx,
267 unsigned int* pverdef_shndx,
268 unsigned int* pverneed_shndx,
269 unsigned int* pdynamic_shndx);
270
271 // Read the dynamic symbol section SHNDX.
272 void
273 read_dynsym_section(const unsigned char* pshdrs, unsigned int shndx,
274 elfcpp::SHT type, unsigned int link,
275 File_view** view, section_size_type* view_size,
276 unsigned int* view_info);
277
278 // Read the dynamic tags.
279 void
280 read_dynamic(const unsigned char* pshdrs, unsigned int dynamic_shndx,
281 unsigned int strtab_shndx, const unsigned char* strtabu,
282 off_t strtab_size);
283
284 // Mapping from version number to version name.
285 typedef std::vector<const char*> Version_map;
286
287 // Create the version map.
288 void
289 make_version_map(Read_symbols_data* sd, Version_map*) const;
290
291 // Add version definitions to the version map.
292 void
293 make_verdef_map(Read_symbols_data* sd, Version_map*) const;
294
295 // Add version references to the version map.
296 void
297 make_verneed_map(Read_symbols_data* sd, Version_map*) const;
298
299 // Add an entry to the version map.
300 void
301 set_version_map(Version_map*, unsigned int ndx, const char* name) const;
302
303 // General access to the ELF file.
304 elfcpp::Elf_file<size, big_endian, Object> elf_file_;
305 // The section index of the dynamic symbol table.
306 unsigned int dynsym_shndx_;
307 // The entries in the symbol table for the symbols. We only keep
308 // this if we need it to print symbol information.
309 Symbols* symbols_;
310 // Number of defined symbols.
311 size_t defined_count_;
312 };
313
314 // A base class for Verdef and Verneed_version which just handles the
315 // version index which will be stored in the SHT_GNU_versym section.
316
317 class Version_base
318 {
319 public:
320 Version_base()
321 : index_(-1U)
322 { }
323
324 virtual
325 ~Version_base()
326 { }
327
328 // Return the version index.
329 unsigned int
330 index() const
331 {
332 gold_assert(this->index_ != -1U);
333 return this->index_;
334 }
335
336 // Set the version index.
337 void
338 set_index(unsigned int index)
339 {
340 gold_assert(this->index_ == -1U);
341 this->index_ = index;
342 }
343
344 // Clear the weak flag in a version definition.
345 virtual void
346 clear_weak() = 0;
347
348 private:
349 Version_base(const Version_base&);
350 Version_base& operator=(const Version_base&);
351
352 // The index of the version definition or reference.
353 unsigned int index_;
354 };
355
356 // This class handles a version being defined in the file we are
357 // generating.
358
359 class Verdef : public Version_base
360 {
361 public:
362 Verdef(const char* name, const std::vector<std::string>& deps,
363 bool is_base, bool is_weak, bool is_info, bool is_symbol_created)
364 : name_(name), deps_(deps), is_base_(is_base), is_weak_(is_weak),
365 is_info_(is_info), is_symbol_created_(is_symbol_created)
366 { }
367
368 // Return the version name.
369 const char*
370 name() const
371 { return this->name_; }
372
373 // Return the number of dependencies.
374 unsigned int
375 count_dependencies() const
376 { return this->deps_.size(); }
377
378 // Add a dependency to this version. The NAME should be
379 // canonicalized in the dynamic Stringpool.
380 void
381 add_dependency(const char* name)
382 { this->deps_.push_back(name); }
383
384 // Return whether this definition is weak.
385 bool
386 is_weak() const
387 { return this->is_weak_; }
388
389 // Clear the weak flag.
390 void
391 clear_weak()
392 { this->is_weak_ = false; }
393
394 // Return whether this definition is informational.
395 bool
396 is_info() const
397 { return this->is_info_; }
398
399 // Return whether a version symbol has been created for this
400 // definition.
401 bool
402 is_symbol_created() const
403 { return this->is_symbol_created_; }
404
405 // Write contents to buffer.
406 template<int size, bool big_endian>
407 unsigned char*
408 write(const Stringpool*, bool is_last, unsigned char*) const;
409
410 private:
411 Verdef(const Verdef&);
412 Verdef& operator=(const Verdef&);
413
414 // The type of the list of version dependencies. Each dependency
415 // should be canonicalized in the dynamic Stringpool.
416 typedef std::vector<std::string> Deps;
417
418 // The name of this version. This should be canonicalized in the
419 // dynamic Stringpool.
420 const char* name_;
421 // A list of other versions which this version depends upon.
422 Deps deps_;
423 // Whether this is the base version.
424 bool is_base_;
425 // Whether this version is weak.
426 bool is_weak_;
427 // Whether this version is informational.
428 bool is_info_;
429 // Whether a version symbol has been created.
430 bool is_symbol_created_;
431 };
432
433 // A referened version. This will be associated with a filename by
434 // Verneed.
435
436 class Verneed_version : public Version_base
437 {
438 public:
439 Verneed_version(const char* version)
440 : version_(version)
441 { }
442
443 // Return the version name.
444 const char*
445 version() const
446 { return this->version_; }
447
448 // Clear the weak flag. This is invalid for a reference.
449 void
450 clear_weak()
451 { gold_unreachable(); }
452
453 private:
454 Verneed_version(const Verneed_version&);
455 Verneed_version& operator=(const Verneed_version&);
456
457 const char* version_;
458 };
459
460 // Version references in a single dynamic object.
461
462 class Verneed
463 {
464 public:
465 Verneed(const char* filename)
466 : filename_(filename), need_versions_()
467 { }
468
469 ~Verneed();
470
471 // Return the file name.
472 const char*
473 filename() const
474 { return this->filename_; }
475
476 // Return the number of versions.
477 unsigned int
478 count_versions() const
479 { return this->need_versions_.size(); }
480
481 // Add a version name. The name should be canonicalized in the
482 // dynamic Stringpool. If the name is already present, this does
483 // nothing.
484 Verneed_version*
485 add_name(const char* name);
486
487 // Set the version indexes, starting at INDEX. Return the updated
488 // INDEX.
489 unsigned int
490 finalize(unsigned int index);
491
492 // Write contents to buffer.
493 template<int size, bool big_endian>
494 unsigned char*
495 write(const Stringpool*, bool is_last, unsigned char*) const;
496
497 private:
498 Verneed(const Verneed&);
499 Verneed& operator=(const Verneed&);
500
501 // The type of the list of version names. Each name should be
502 // canonicalized in the dynamic Stringpool.
503 typedef std::vector<Verneed_version*> Need_versions;
504
505 // The filename of the dynamic object. This should be
506 // canonicalized in the dynamic Stringpool.
507 const char* filename_;
508 // The list of version names.
509 Need_versions need_versions_;
510 };
511
512 // This class handles version definitions and references which go into
513 // the output file.
514
515 class Versions
516 {
517 public:
518 Versions(const Version_script_info&, Stringpool*);
519
520 ~Versions();
521
522 // SYM is going into the dynamic symbol table and has a version.
523 // Record the appropriate version information.
524 void
525 record_version(const Symbol_table* symtab, Stringpool*, const Symbol* sym);
526
527 // Set the version indexes. DYNSYM_INDEX is the index we should use
528 // for the next dynamic symbol. We add new dynamic symbols to SYMS
529 // and return an updated DYNSYM_INDEX.
530 unsigned int
531 finalize(Symbol_table* symtab, unsigned int dynsym_index,
532 std::vector<Symbol*>* syms);
533
534 // Return whether there are any version definitions.
535 bool
536 any_defs() const
537 { return !this->defs_.empty(); }
538
539 // Return whether there are any version references.
540 bool
541 any_needs() const
542 { return !this->needs_.empty(); }
543
544 // Build an allocated buffer holding the contents of the symbol
545 // version section (.gnu.version).
546 template<int size, bool big_endian>
547 void
548 symbol_section_contents(const Symbol_table*, const Stringpool*,
549 unsigned int local_symcount,
550 const std::vector<Symbol*>& syms,
551 unsigned char**, unsigned int*) const;
552
553 // Build an allocated buffer holding the contents of the version
554 // definition section (.gnu.version_d).
555 template<int size, bool big_endian>
556 void
557 def_section_contents(const Stringpool*, unsigned char**,
558 unsigned int* psize, unsigned int* pentries) const;
559
560 // Build an allocated buffer holding the contents of the version
561 // reference section (.gnu.version_r).
562 template<int size, bool big_endian>
563 void
564 need_section_contents(const Stringpool*, unsigned char**,
565 unsigned int* psize, unsigned int* pentries) const;
566
567 const Version_script_info&
568 version_script() const
569 { return this->version_script_; }
570
571 private:
572 Versions(const Versions&);
573 Versions& operator=(const Versions&);
574
575 // The type of the list of version definitions.
576 typedef std::vector<Verdef*> Defs;
577
578 // The type of the list of version references.
579 typedef std::vector<Verneed*> Needs;
580
581 // Handle a symbol SYM defined with version VERSION.
582 void
583 add_def(const Symbol* sym, const char* version, Stringpool::Key);
584
585 // Add a reference to version NAME in file FILENAME.
586 void
587 add_need(Stringpool*, const char* filename, const char* name,
588 Stringpool::Key);
589
590 // Get the dynamic object to use for SYM.
591 Dynobj*
592 get_dynobj_for_sym(const Symbol_table*, const Symbol* sym) const;
593
594 // Return the version index to use for SYM.
595 unsigned int
596 version_index(const Symbol_table*, const Stringpool*,
597 const Symbol* sym) const;
598
599 // Define the base version of a shared library.
600 void
601 define_base_version(Stringpool* dynpool);
602
603 // We keep a hash table mapping canonicalized name/version pairs to
604 // a version base.
605 typedef std::pair<Stringpool::Key, Stringpool::Key> Key;
606
607 struct Version_table_hash
608 {
609 size_t
610 operator()(const Key& k) const
611 { return k.first + k.second; }
612 };
613
614 struct Version_table_eq
615 {
616 bool
617 operator()(const Key& k1, const Key& k2) const
618 { return k1.first == k2.first && k1.second == k2.second; }
619 };
620
621 typedef Unordered_map<Key, Version_base*, Version_table_hash,
622 Version_table_eq> Version_table;
623
624 // The version definitions.
625 Defs defs_;
626 // The version references.
627 Needs needs_;
628 // The mapping from a canonicalized version/filename pair to a
629 // version index. The filename may be NULL.
630 Version_table version_table_;
631 // Whether the version indexes have been set.
632 bool is_finalized_;
633 // Contents of --version-script, if passed, or NULL.
634 const Version_script_info& version_script_;
635 // Whether we need to insert a base version. This is only used for
636 // shared libaries and is cleared when the base version is defined.
637 bool needs_base_version_;
638 };
639
640 } // End namespace gold.
641
642 #endif // !defined(GOLD_DYNOBJ_H)
This page took 0.054576 seconds and 5 git commands to generate.