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