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