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