bfd/
[binutils.git] / gold / symtab.h
blobbd417948cd37e3dab78f38b3bba4e8b565233ed4
1 // symtab.h -- the gold symbol table -*- 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 // Symbol_table
24 // The symbol table.
26 #include <string>
27 #include <utility>
28 #include <vector>
30 #include "elfcpp.h"
31 #include "parameters.h"
32 #include "stringpool.h"
33 #include "object.h"
35 #ifndef GOLD_SYMTAB_H
36 #define GOLD_SYMTAB_H
38 namespace gold
41 class Object;
42 class Relobj;
43 template<int size, bool big_endian>
44 class Sized_relobj;
45 class Dynobj;
46 template<int size, bool big_endian>
47 class Sized_dynobj;
48 class Versions;
49 class Version_script_info;
50 class Input_objects;
51 class Output_data;
52 class Output_section;
53 class Output_segment;
54 class Output_file;
56 // The base class of an entry in the symbol table. The symbol table
57 // can have a lot of entries, so we don't want this class to big.
58 // Size dependent fields can be found in the template class
59 // Sized_symbol. Targets may support their own derived classes.
61 class Symbol
63 public:
64 // Because we want the class to be small, we don't use any virtual
65 // functions. But because symbols can be defined in different
66 // places, we need to classify them. This enum is the different
67 // sources of symbols we support.
68 enum Source
70 // Symbol defined in a relocatable or dynamic input file--this is
71 // the most common case.
72 FROM_OBJECT,
73 // Symbol defined in an Output_data, a special section created by
74 // the target.
75 IN_OUTPUT_DATA,
76 // Symbol defined in an Output_segment, with no associated
77 // section.
78 IN_OUTPUT_SEGMENT,
79 // Symbol value is constant.
80 CONSTANT
83 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
84 // the offset means.
85 enum Segment_offset_base
87 // From the start of the segment.
88 SEGMENT_START,
89 // From the end of the segment.
90 SEGMENT_END,
91 // From the filesz of the segment--i.e., after the loaded bytes
92 // but before the bytes which are allocated but zeroed.
93 SEGMENT_BSS
96 // Return the symbol name.
97 const char*
98 name() const
99 { return this->name_; }
101 // Return the (ANSI) demangled version of the name, if
102 // parameters.demangle() is true. Otherwise, return the name. This
103 // is intended to be used only for logging errors, so it's not
104 // super-efficient.
105 std::string
106 demangled_name() const;
108 // Return the symbol version. This will return NULL for an
109 // unversioned symbol.
110 const char*
111 version() const
112 { return this->version_; }
114 // Return whether this version is the default for this symbol name
115 // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
116 // meaningful for versioned symbols.
117 bool
118 is_default() const
120 gold_assert(this->version_ != NULL);
121 return this->is_def_;
124 // Set that this version is the default for this symbol name.
125 void
126 set_is_default()
127 { this->is_def_ = true; }
129 // Return the symbol source.
130 Source
131 source() const
132 { return this->source_; }
134 // Return the object with which this symbol is associated.
135 Object*
136 object() const
138 gold_assert(this->source_ == FROM_OBJECT);
139 return this->u_.from_object.object;
142 // Return the index of the section in the input relocatable or
143 // dynamic object file.
144 unsigned int
145 shndx() const
147 gold_assert(this->source_ == FROM_OBJECT);
148 return this->u_.from_object.shndx;
151 // Return the output data section with which this symbol is
152 // associated, if the symbol was specially defined with respect to
153 // an output data section.
154 Output_data*
155 output_data() const
157 gold_assert(this->source_ == IN_OUTPUT_DATA);
158 return this->u_.in_output_data.output_data;
161 // If this symbol was defined with respect to an output data
162 // section, return whether the value is an offset from end.
163 bool
164 offset_is_from_end() const
166 gold_assert(this->source_ == IN_OUTPUT_DATA);
167 return this->u_.in_output_data.offset_is_from_end;
170 // Return the output segment with which this symbol is associated,
171 // if the symbol was specially defined with respect to an output
172 // segment.
173 Output_segment*
174 output_segment() const
176 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
177 return this->u_.in_output_segment.output_segment;
180 // If this symbol was defined with respect to an output segment,
181 // return the offset base.
182 Segment_offset_base
183 offset_base() const
185 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
186 return this->u_.in_output_segment.offset_base;
189 // Return the symbol binding.
190 elfcpp::STB
191 binding() const
192 { return this->binding_; }
194 // Return the symbol type.
195 elfcpp::STT
196 type() const
197 { return this->type_; }
199 // Return the symbol visibility.
200 elfcpp::STV
201 visibility() const
202 { return this->visibility_; }
204 // Return the non-visibility part of the st_other field.
205 unsigned char
206 nonvis() const
207 { return this->nonvis_; }
209 // Return whether this symbol is a forwarder. This will never be
210 // true of a symbol found in the hash table, but may be true of
211 // symbol pointers attached to object files.
212 bool
213 is_forwarder() const
214 { return this->is_forwarder_; }
216 // Mark this symbol as a forwarder.
217 void
218 set_forwarder()
219 { this->is_forwarder_ = true; }
221 // Return whether this symbol has an alias in the weak aliases table
222 // in Symbol_table.
223 bool
224 has_alias() const
225 { return this->has_alias_; }
227 // Mark this symbol as having an alias.
228 void
229 set_has_alias()
230 { this->has_alias_ = true; }
232 // Return whether this symbol needs an entry in the dynamic symbol
233 // table.
234 bool
235 needs_dynsym_entry() const
237 return (this->needs_dynsym_entry_
238 || (this->in_reg() && this->in_dyn()));
241 // Mark this symbol as needing an entry in the dynamic symbol table.
242 void
243 set_needs_dynsym_entry()
244 { this->needs_dynsym_entry_ = true; }
246 // Return whether this symbol should be added to the dynamic symbol
247 // table.
248 bool
249 should_add_dynsym_entry() const;
251 // Return whether this symbol has been seen in a regular object.
252 bool
253 in_reg() const
254 { return this->in_reg_; }
256 // Mark this symbol as having been seen in a regular object.
257 void
258 set_in_reg()
259 { this->in_reg_ = true; }
261 // Return whether this symbol has been seen in a dynamic object.
262 bool
263 in_dyn() const
264 { return this->in_dyn_; }
266 // Mark this symbol as having been seen in a dynamic object.
267 void
268 set_in_dyn()
269 { this->in_dyn_ = true; }
271 // Return the index of this symbol in the output file symbol table.
272 // A value of -1U means that this symbol is not going into the
273 // output file. This starts out as zero, and is set to a non-zero
274 // value by Symbol_table::finalize. It is an error to ask for the
275 // symbol table index before it has been set.
276 unsigned int
277 symtab_index() const
279 gold_assert(this->symtab_index_ != 0);
280 return this->symtab_index_;
283 // Set the index of the symbol in the output file symbol table.
284 void
285 set_symtab_index(unsigned int index)
287 gold_assert(index != 0);
288 this->symtab_index_ = index;
291 // Return whether this symbol already has an index in the output
292 // file symbol table.
293 bool
294 has_symtab_index() const
295 { return this->symtab_index_ != 0; }
297 // Return the index of this symbol in the dynamic symbol table. A
298 // value of -1U means that this symbol is not going into the dynamic
299 // symbol table. This starts out as zero, and is set to a non-zero
300 // during Layout::finalize. It is an error to ask for the dynamic
301 // symbol table index before it has been set.
302 unsigned int
303 dynsym_index() const
305 gold_assert(this->dynsym_index_ != 0);
306 return this->dynsym_index_;
309 // Set the index of the symbol in the dynamic symbol table.
310 void
311 set_dynsym_index(unsigned int index)
313 gold_assert(index != 0);
314 this->dynsym_index_ = index;
317 // Return whether this symbol already has an index in the dynamic
318 // symbol table.
319 bool
320 has_dynsym_index() const
321 { return this->dynsym_index_ != 0; }
323 // Return whether this symbol has an entry in the GOT section.
324 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
325 bool
326 has_got_offset(unsigned int got_type) const
327 { return this->got_offsets_.get_offset(got_type) != -1U; }
329 // Return the offset into the GOT section of this symbol.
330 unsigned int
331 got_offset(unsigned int got_type) const
333 unsigned int got_offset = this->got_offsets_.get_offset(got_type);
334 gold_assert(got_offset != -1U);
335 return got_offset;
338 // Set the GOT offset of this symbol.
339 void
340 set_got_offset(unsigned int got_type, unsigned int got_offset)
341 { this->got_offsets_.set_offset(got_type, got_offset); }
343 // Return whether this symbol has an entry in the PLT section.
344 bool
345 has_plt_offset() const
346 { return this->has_plt_offset_; }
348 // Return the offset into the PLT section of this symbol.
349 unsigned int
350 plt_offset() const
352 gold_assert(this->has_plt_offset());
353 return this->plt_offset_;
356 // Set the PLT offset of this symbol.
357 void
358 set_plt_offset(unsigned int plt_offset)
360 this->has_plt_offset_ = true;
361 this->plt_offset_ = plt_offset;
364 // Return whether this dynamic symbol needs a special value in the
365 // dynamic symbol table.
366 bool
367 needs_dynsym_value() const
368 { return this->needs_dynsym_value_; }
370 // Set that this dynamic symbol needs a special value in the dynamic
371 // symbol table.
372 void
373 set_needs_dynsym_value()
375 gold_assert(this->object()->is_dynamic());
376 this->needs_dynsym_value_ = true;
379 // Return true if the final value of this symbol is known at link
380 // time.
381 bool
382 final_value_is_known() const;
384 // Return whether this is a defined symbol (not undefined or
385 // common).
386 bool
387 is_defined() const
389 return (this->source_ != FROM_OBJECT
390 || (this->shndx() != elfcpp::SHN_UNDEF
391 && this->shndx() != elfcpp::SHN_COMMON));
394 // Return true if this symbol is from a dynamic object.
395 bool
396 is_from_dynobj() const
398 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
401 // Return whether this is an undefined symbol.
402 bool
403 is_undefined() const
405 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
408 // Return whether this is a common symbol.
409 bool
410 is_common() const
412 return (this->source_ == FROM_OBJECT
413 && (this->shndx() == elfcpp::SHN_COMMON
414 || this->type_ == elfcpp::STT_COMMON));
417 // Return whether this symbol can be seen outside this object.
418 bool
419 is_externally_visible() const
421 return (this->visibility_ == elfcpp::STV_DEFAULT
422 || this->visibility_ == elfcpp::STV_PROTECTED);
425 // Return true if this symbol can be preempted by a definition in
426 // another link unit.
427 bool
428 is_preemptible() const
430 // It doesn't make sense to ask whether a symbol defined in
431 // another object is preemptible.
432 gold_assert(!this->is_from_dynobj());
434 // It doesn't make sense to ask whether an undefined symbol
435 // is preemptible.
436 gold_assert(!this->is_undefined());
438 return (this->visibility_ != elfcpp::STV_INTERNAL
439 && this->visibility_ != elfcpp::STV_HIDDEN
440 && this->visibility_ != elfcpp::STV_PROTECTED
441 && !this->is_forced_local_
442 && parameters->options().shared()
443 && !parameters->options().Bsymbolic());
446 // Return true if this symbol is a function that needs a PLT entry.
447 // If the symbol is defined in a dynamic object or if it is subject
448 // to pre-emption, we need to make a PLT entry. If we're doing a
449 // static link, we don't create PLT entries.
450 bool
451 needs_plt_entry() const
453 return (!parameters->doing_static_link()
454 && this->type() == elfcpp::STT_FUNC
455 && (this->is_from_dynobj()
456 || this->is_undefined()
457 || this->is_preemptible()));
460 // When determining whether a reference to a symbol needs a dynamic
461 // relocation, we need to know several things about the reference.
462 // These flags may be or'ed together.
463 enum Reference_flags
465 // Reference to the symbol's absolute address.
466 ABSOLUTE_REF = 1,
467 // A non-PIC reference.
468 NON_PIC_REF = 2,
469 // A function call.
470 FUNCTION_CALL = 4
473 // Given a direct absolute or pc-relative static relocation against
474 // the global symbol, this function returns whether a dynamic relocation
475 // is needed.
477 bool
478 needs_dynamic_reloc(int flags) const
480 // No dynamic relocations in a static link!
481 if (parameters->doing_static_link())
482 return false;
484 // An absolute reference within a position-independent output file
485 // will need a dynamic relocation.
486 if ((flags & ABSOLUTE_REF)
487 && parameters->options().output_is_position_independent())
488 return true;
490 // A function call that can branch to a local PLT entry does not need
491 // a dynamic relocation. A non-pic pc-relative function call in a
492 // shared library cannot use a PLT entry.
493 if ((flags & FUNCTION_CALL)
494 && this->has_plt_offset()
495 && !((flags & NON_PIC_REF) && parameters->options().shared()))
496 return false;
498 // A reference to any PLT entry in a non-position-independent executable
499 // does not need a dynamic relocation.
500 if (!parameters->options().output_is_position_independent()
501 && this->has_plt_offset())
502 return false;
504 // A reference to a symbol defined in a dynamic object or to a
505 // symbol that is preemptible will need a dynamic relocation.
506 if (this->is_from_dynobj()
507 || this->is_undefined()
508 || this->is_preemptible())
509 return true;
511 // For all other cases, return FALSE.
512 return false;
515 // Given a direct absolute static relocation against
516 // the global symbol, where a dynamic relocation is needed, this
517 // function returns whether a relative dynamic relocation can be used.
518 // The caller must determine separately whether the static relocation
519 // is compatible with a relative relocation.
521 bool
522 can_use_relative_reloc(bool is_function_call) const
524 // A function call that can branch to a local PLT entry can
525 // use a RELATIVE relocation.
526 if (is_function_call && this->has_plt_offset())
527 return true;
529 // A reference to a symbol defined in a dynamic object or to a
530 // symbol that is preemptible can not use a RELATIVE relocaiton.
531 if (this->is_from_dynobj()
532 || this->is_undefined()
533 || this->is_preemptible())
534 return false;
536 // For all other cases, return TRUE.
537 return true;
540 // Return the output section where this symbol is defined. Return
541 // NULL if the symbol has an absolute value.
542 Output_section*
543 output_section() const;
545 // Set the symbol's output section. This is used for symbols
546 // defined in scripts. This should only be called after the symbol
547 // table has been finalized.
548 void
549 set_output_section(Output_section*);
551 // Return whether there should be a warning for references to this
552 // symbol.
553 bool
554 has_warning() const
555 { return this->has_warning_; }
557 // Mark this symbol as having a warning.
558 void
559 set_has_warning()
560 { this->has_warning_ = true; }
562 // Return whether this symbol is defined by a COPY reloc from a
563 // dynamic object.
564 bool
565 is_copied_from_dynobj() const
566 { return this->is_copied_from_dynobj_; }
568 // Mark this symbol as defined by a COPY reloc.
569 void
570 set_is_copied_from_dynobj()
571 { this->is_copied_from_dynobj_ = true; }
573 // Return whether this symbol is forced to visibility STB_LOCAL
574 // by a "local:" entry in a version script.
575 bool
576 is_forced_local() const
577 { return this->is_forced_local_; }
579 // Mark this symbol as forced to STB_LOCAL visibility.
580 void
581 set_is_forced_local()
582 { this->is_forced_local_ = true; }
584 protected:
585 // Instances of this class should always be created at a specific
586 // size.
587 Symbol()
588 { memset(this, 0, sizeof *this); }
590 // Initialize the general fields.
591 void
592 init_fields(const char* name, const char* version,
593 elfcpp::STT type, elfcpp::STB binding,
594 elfcpp::STV visibility, unsigned char nonvis);
596 // Initialize fields from an ELF symbol in OBJECT.
597 template<int size, bool big_endian>
598 void
599 init_base(const char *name, const char* version, Object* object,
600 const elfcpp::Sym<size, big_endian>&);
602 // Initialize fields for an Output_data.
603 void
604 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
605 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
607 // Initialize fields for an Output_segment.
608 void
609 init_base(const char* name, Output_segment* os, elfcpp::STT type,
610 elfcpp::STB binding, elfcpp::STV visibility,
611 unsigned char nonvis, Segment_offset_base offset_base);
613 // Initialize fields for a constant.
614 void
615 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
616 elfcpp::STV visibility, unsigned char nonvis);
618 // Override existing symbol.
619 template<int size, bool big_endian>
620 void
621 override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
622 const char* version);
624 // Override existing symbol with a special symbol.
625 void
626 override_base_with_special(const Symbol* from);
628 // Allocate a common symbol by giving it a location in the output
629 // file.
630 void
631 allocate_base_common(Output_data*);
633 private:
634 Symbol(const Symbol&);
635 Symbol& operator=(const Symbol&);
637 // Symbol name (expected to point into a Stringpool).
638 const char* name_;
639 // Symbol version (expected to point into a Stringpool). This may
640 // be NULL.
641 const char* version_;
643 union
645 // This struct is used if SOURCE_ == FROM_OBJECT.
646 struct
648 // Object in which symbol is defined, or in which it was first
649 // seen.
650 Object* object;
651 // Section number in object_ in which symbol is defined.
652 unsigned int shndx;
653 } from_object;
655 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
656 struct
658 // Output_data in which symbol is defined. Before
659 // Layout::finalize the symbol's value is an offset within the
660 // Output_data.
661 Output_data* output_data;
662 // True if the offset is from the end, false if the offset is
663 // from the beginning.
664 bool offset_is_from_end;
665 } in_output_data;
667 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
668 struct
670 // Output_segment in which the symbol is defined. Before
671 // Layout::finalize the symbol's value is an offset.
672 Output_segment* output_segment;
673 // The base to use for the offset before Layout::finalize.
674 Segment_offset_base offset_base;
675 } in_output_segment;
676 } u_;
678 // The index of this symbol in the output file. If the symbol is
679 // not going into the output file, this value is -1U. This field
680 // starts as always holding zero. It is set to a non-zero value by
681 // Symbol_table::finalize.
682 unsigned int symtab_index_;
684 // The index of this symbol in the dynamic symbol table. If the
685 // symbol is not going into the dynamic symbol table, this value is
686 // -1U. This field starts as always holding zero. It is set to a
687 // non-zero value during Layout::finalize.
688 unsigned int dynsym_index_;
690 // If this symbol has an entry in the GOT section (has_got_offset_
691 // is true), this holds the offset from the start of the GOT section.
692 // A symbol may have more than one GOT offset (e.g., when mixing
693 // modules compiled with two different TLS models), but will usually
694 // have at most one.
695 Got_offset_list got_offsets_;
697 // If this symbol has an entry in the PLT section (has_plt_offset_
698 // is true), then this is the offset from the start of the PLT
699 // section.
700 unsigned int plt_offset_;
702 // Symbol type.
703 elfcpp::STT type_ : 4;
704 // Symbol binding.
705 elfcpp::STB binding_ : 4;
706 // Symbol visibility.
707 elfcpp::STV visibility_ : 2;
708 // Rest of symbol st_other field.
709 unsigned int nonvis_ : 6;
710 // The type of symbol.
711 Source source_ : 3;
712 // True if this symbol always requires special target-specific
713 // handling.
714 bool is_target_special_ : 1;
715 // True if this is the default version of the symbol.
716 bool is_def_ : 1;
717 // True if this symbol really forwards to another symbol. This is
718 // used when we discover after the fact that two different entries
719 // in the hash table really refer to the same symbol. This will
720 // never be set for a symbol found in the hash table, but may be set
721 // for a symbol found in the list of symbols attached to an Object.
722 // It forwards to the symbol found in the forwarders_ map of
723 // Symbol_table.
724 bool is_forwarder_ : 1;
725 // True if the symbol has an alias in the weak_aliases table in
726 // Symbol_table.
727 bool has_alias_ : 1;
728 // True if this symbol needs to be in the dynamic symbol table.
729 bool needs_dynsym_entry_ : 1;
730 // True if we've seen this symbol in a regular object.
731 bool in_reg_ : 1;
732 // True if we've seen this symbol in a dynamic object.
733 bool in_dyn_ : 1;
734 // True if the symbol has an entry in the PLT section.
735 bool has_plt_offset_ : 1;
736 // True if this is a dynamic symbol which needs a special value in
737 // the dynamic symbol table.
738 bool needs_dynsym_value_ : 1;
739 // True if there is a warning for this symbol.
740 bool has_warning_ : 1;
741 // True if we are using a COPY reloc for this symbol, so that the
742 // real definition lives in a dynamic object.
743 bool is_copied_from_dynobj_ : 1;
744 // True if this symbol was forced to local visibility by a version
745 // script.
746 bool is_forced_local_ : 1;
749 // The parts of a symbol which are size specific. Using a template
750 // derived class like this helps us use less space on a 32-bit system.
752 template<int size>
753 class Sized_symbol : public Symbol
755 public:
756 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
757 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
759 Sized_symbol()
762 // Initialize fields from an ELF symbol in OBJECT.
763 template<bool big_endian>
764 void
765 init(const char *name, const char* version, Object* object,
766 const elfcpp::Sym<size, big_endian>&);
768 // Initialize fields for an Output_data.
769 void
770 init(const char* name, Output_data*, Value_type value, Size_type symsize,
771 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
772 bool offset_is_from_end);
774 // Initialize fields for an Output_segment.
775 void
776 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
777 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
778 Segment_offset_base offset_base);
780 // Initialize fields for a constant.
781 void
782 init(const char* name, Value_type value, Size_type symsize,
783 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
785 // Override existing symbol.
786 template<bool big_endian>
787 void
788 override(const elfcpp::Sym<size, big_endian>&, Object* object,
789 const char* version);
791 // Override existing symbol with a special symbol.
792 void
793 override_with_special(const Sized_symbol<size>*);
795 // Return the symbol's value.
796 Value_type
797 value() const
798 { return this->value_; }
800 // Return the symbol's size (we can't call this 'size' because that
801 // is a template parameter).
802 Size_type
803 symsize() const
804 { return this->symsize_; }
806 // Set the symbol size. This is used when resolving common symbols.
807 void
808 set_symsize(Size_type symsize)
809 { this->symsize_ = symsize; }
811 // Set the symbol value. This is called when we store the final
812 // values of the symbols into the symbol table.
813 void
814 set_value(Value_type value)
815 { this->value_ = value; }
817 // Allocate a common symbol by giving it a location in the output
818 // file.
819 void
820 allocate_common(Output_data*, Value_type value);
822 private:
823 Sized_symbol(const Sized_symbol&);
824 Sized_symbol& operator=(const Sized_symbol&);
826 // Symbol value. Before Layout::finalize this is the offset in the
827 // input section. This is set to the final value during
828 // Layout::finalize.
829 Value_type value_;
830 // Symbol size.
831 Size_type symsize_;
834 // A struct describing a symbol defined by the linker, where the value
835 // of the symbol is defined based on an output section. This is used
836 // for symbols defined by the linker, like "_init_array_start".
838 struct Define_symbol_in_section
840 // The symbol name.
841 const char* name;
842 // The name of the output section with which this symbol should be
843 // associated. If there is no output section with that name, the
844 // symbol will be defined as zero.
845 const char* output_section;
846 // The offset of the symbol within the output section. This is an
847 // offset from the start of the output section, unless start_at_end
848 // is true, in which case this is an offset from the end of the
849 // output section.
850 uint64_t value;
851 // The size of the symbol.
852 uint64_t size;
853 // The symbol type.
854 elfcpp::STT type;
855 // The symbol binding.
856 elfcpp::STB binding;
857 // The symbol visibility.
858 elfcpp::STV visibility;
859 // The rest of the st_other field.
860 unsigned char nonvis;
861 // If true, the value field is an offset from the end of the output
862 // section.
863 bool offset_is_from_end;
864 // If true, this symbol is defined only if we see a reference to it.
865 bool only_if_ref;
868 // A struct describing a symbol defined by the linker, where the value
869 // of the symbol is defined based on a segment. This is used for
870 // symbols defined by the linker, like "_end". We describe the
871 // segment with which the symbol should be associated by its
872 // characteristics. If no segment meets these characteristics, the
873 // symbol will be defined as zero. If there is more than one segment
874 // which meets these characteristics, we will use the first one.
876 struct Define_symbol_in_segment
878 // The symbol name.
879 const char* name;
880 // The segment type where the symbol should be defined, typically
881 // PT_LOAD.
882 elfcpp::PT segment_type;
883 // Bitmask of segment flags which must be set.
884 elfcpp::PF segment_flags_set;
885 // Bitmask of segment flags which must be clear.
886 elfcpp::PF segment_flags_clear;
887 // The offset of the symbol within the segment. The offset is
888 // calculated from the position set by offset_base.
889 uint64_t value;
890 // The size of the symbol.
891 uint64_t size;
892 // The symbol type.
893 elfcpp::STT type;
894 // The symbol binding.
895 elfcpp::STB binding;
896 // The symbol visibility.
897 elfcpp::STV visibility;
898 // The rest of the st_other field.
899 unsigned char nonvis;
900 // The base from which we compute the offset.
901 Symbol::Segment_offset_base offset_base;
902 // If true, this symbol is defined only if we see a reference to it.
903 bool only_if_ref;
906 // This class manages warnings. Warnings are a GNU extension. When
907 // we see a section named .gnu.warning.SYM in an object file, and if
908 // we wind using the definition of SYM from that object file, then we
909 // will issue a warning for any relocation against SYM from a
910 // different object file. The text of the warning is the contents of
911 // the section. This is not precisely the definition used by the old
912 // GNU linker; the old GNU linker treated an occurrence of
913 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
914 // would trigger a warning on any reference. However, it was
915 // inconsistent in that a warning in a dynamic object only triggered
916 // if there was no definition in a regular object. This linker is
917 // different in that we only issue a warning if we use the symbol
918 // definition from the same object file as the warning section.
920 class Warnings
922 public:
923 Warnings()
924 : warnings_()
927 // Add a warning for symbol NAME in object OBJ. WARNING is the text
928 // of the warning.
929 void
930 add_warning(Symbol_table* symtab, const char* name, Object* obj,
931 const std::string& warning);
933 // For each symbol for which we should give a warning, make a note
934 // on the symbol.
935 void
936 note_warnings(Symbol_table* symtab);
938 // Issue a warning for a reference to SYM at RELINFO's location.
939 template<int size, bool big_endian>
940 void
941 issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
942 size_t relnum, off_t reloffset) const;
944 private:
945 Warnings(const Warnings&);
946 Warnings& operator=(const Warnings&);
948 // What we need to know to get the warning text.
949 struct Warning_location
951 // The object the warning is in.
952 Object* object;
953 // The warning text.
954 std::string text;
956 Warning_location()
957 : object(NULL), text()
960 void
961 set(Object* o, const std::string& t)
963 this->object = o;
964 this->text = t;
968 // A mapping from warning symbol names (canonicalized in
969 // Symbol_table's namepool_ field) to warning information.
970 typedef Unordered_map<const char*, Warning_location> Warning_table;
972 Warning_table warnings_;
975 // The main linker symbol table.
977 class Symbol_table
979 public:
980 // COUNT is an estimate of how many symbosl will be inserted in the
981 // symbol table. It's ok to put 0 if you don't know; a correct
982 // guess will just save some CPU by reducing hashtable resizes.
983 Symbol_table(unsigned int count, const Version_script_info& version_script);
985 ~Symbol_table();
987 // Add COUNT external symbols from the relocatable object RELOBJ to
988 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
989 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
990 // point to the symbols in the symbol table.
991 template<int size, bool big_endian>
992 void
993 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
994 const unsigned char* syms, size_t count,
995 const char* sym_names, size_t sym_name_size,
996 typename Sized_relobj<size, big_endian>::Symbols*);
998 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
999 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1000 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1001 // symbol version data.
1002 template<int size, bool big_endian>
1003 void
1004 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
1005 const unsigned char* syms, size_t count,
1006 const char* sym_names, size_t sym_name_size,
1007 const unsigned char* versym, size_t versym_size,
1008 const std::vector<const char*>*);
1010 // Define a special symbol based on an Output_data. It is a
1011 // multiple definition error if this symbol is already defined.
1012 Symbol*
1013 define_in_output_data(const char* name, const char* version,
1014 Output_data*, uint64_t value, uint64_t symsize,
1015 elfcpp::STT type, elfcpp::STB binding,
1016 elfcpp::STV visibility, unsigned char nonvis,
1017 bool offset_is_from_end, bool only_if_ref);
1019 // Define a special symbol based on an Output_segment. It is a
1020 // multiple definition error if this symbol is already defined.
1021 Symbol*
1022 define_in_output_segment(const char* name, const char* version,
1023 Output_segment*, uint64_t value, uint64_t symsize,
1024 elfcpp::STT type, elfcpp::STB binding,
1025 elfcpp::STV visibility, unsigned char nonvis,
1026 Symbol::Segment_offset_base, bool only_if_ref);
1028 // Define a special symbol with a constant value. It is a multiple
1029 // definition error if this symbol is already defined.
1030 Symbol*
1031 define_as_constant(const char* name, const char* version,
1032 uint64_t value, uint64_t symsize, elfcpp::STT type,
1033 elfcpp::STB binding, elfcpp::STV visibility,
1034 unsigned char nonvis, bool only_if_ref,
1035 bool force_override);
1037 // Define a set of symbols in output sections. If ONLY_IF_REF is
1038 // true, only define them if they are referenced.
1039 void
1040 define_symbols(const Layout*, int count, const Define_symbol_in_section*,
1041 bool only_if_ref);
1043 // Define a set of symbols in output segments. If ONLY_IF_REF is
1044 // true, only defined them if they are referenced.
1045 void
1046 define_symbols(const Layout*, int count, const Define_symbol_in_segment*,
1047 bool only_if_ref);
1049 // Define SYM using a COPY reloc. POSD is the Output_data where the
1050 // symbol should be defined--typically a .dyn.bss section. VALUE is
1051 // the offset within POSD.
1052 template<int size>
1053 void
1054 define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
1055 typename elfcpp::Elf_types<size>::Elf_Addr);
1057 // Look up a symbol.
1058 Symbol*
1059 lookup(const char*, const char* version = NULL) const;
1061 // Return the real symbol associated with the forwarder symbol FROM.
1062 Symbol*
1063 resolve_forwards(const Symbol* from) const;
1065 // Return the sized version of a symbol in this table.
1066 template<int size>
1067 Sized_symbol<size>*
1068 get_sized_symbol(Symbol*) const;
1070 template<int size>
1071 const Sized_symbol<size>*
1072 get_sized_symbol(const Symbol*) const;
1074 // Return the count of undefined symbols seen.
1076 saw_undefined() const
1077 { return this->saw_undefined_; }
1079 // Allocate the common symbols
1080 void
1081 allocate_commons(const General_options&, Layout*);
1083 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1084 // of the warning.
1085 void
1086 add_warning(const char* name, Object* obj, const std::string& warning)
1087 { this->warnings_.add_warning(this, name, obj, warning); }
1089 // Canonicalize a symbol name for use in the hash table.
1090 const char*
1091 canonicalize_name(const char* name)
1092 { return this->namepool_.add(name, true, NULL); }
1094 // Possibly issue a warning for a reference to SYM at LOCATION which
1095 // is in OBJ.
1096 template<int size, bool big_endian>
1097 void
1098 issue_warning(const Symbol* sym,
1099 const Relocate_info<size, big_endian>* relinfo,
1100 size_t relnum, off_t reloffset) const
1101 { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
1103 // Check candidate_odr_violations_ to find symbols with the same name
1104 // but apparently different definitions (different source-file/line-no).
1105 void
1106 detect_odr_violations(const Task*, const char* output_file_name) const;
1108 // SYM is defined using a COPY reloc. Return the dynamic object
1109 // where the original definition was found.
1110 Dynobj*
1111 get_copy_source(const Symbol* sym) const;
1113 // Set the dynamic symbol indexes. INDEX is the index of the first
1114 // global dynamic symbol. Pointers to the symbols are stored into
1115 // the vector. The names are stored into the Stringpool. This
1116 // returns an updated dynamic symbol index.
1117 unsigned int
1118 set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*,
1119 Stringpool*, Versions*);
1121 // Finalize the symbol table after we have set the final addresses
1122 // of all the input sections. This sets the final symbol indexes,
1123 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1124 // index of the first global symbol. OFF is the file offset of the
1125 // global symbol table, DYNOFF is the offset of the globals in the
1126 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1127 // global dynamic symbol, and DYNCOUNT is the number of global
1128 // dynamic symbols. This records the parameters, and returns the
1129 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1130 // local symbols.
1131 off_t
1132 finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
1133 Stringpool* pool, unsigned int *plocal_symcount);
1135 // Write out the global symbols.
1136 void
1137 write_globals(const Input_objects*, const Stringpool*, const Stringpool*,
1138 Output_file*) const;
1140 // Write out a section symbol. Return the updated offset.
1141 void
1142 write_section_symbol(const Output_section*, Output_file*, off_t) const;
1144 // Dump statistical information to stderr.
1145 void
1146 print_stats() const;
1148 // Return the version script information.
1149 const Version_script_info&
1150 version_script() const
1151 { return version_script_; }
1153 private:
1154 Symbol_table(const Symbol_table&);
1155 Symbol_table& operator=(const Symbol_table&);
1157 // Make FROM a forwarder symbol to TO.
1158 void
1159 make_forwarder(Symbol* from, Symbol* to);
1161 // Add a symbol.
1162 template<int size, bool big_endian>
1163 Sized_symbol<size>*
1164 add_from_object(Object*, const char *name, Stringpool::Key name_key,
1165 const char *version, Stringpool::Key version_key,
1166 bool def, const elfcpp::Sym<size, big_endian>& sym,
1167 const elfcpp::Sym<size, big_endian>& orig_sym);
1169 // Resolve symbols.
1170 template<int size, bool big_endian>
1171 void
1172 resolve(Sized_symbol<size>* to,
1173 const elfcpp::Sym<size, big_endian>& sym,
1174 const elfcpp::Sym<size, big_endian>& orig_sym,
1175 Object*, const char* version);
1177 template<int size, bool big_endian>
1178 void
1179 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
1180 const char* version);
1182 // Record that a symbol is forced to be local by a version script.
1183 void
1184 force_local(Symbol*);
1186 // Whether we should override a symbol, based on flags in
1187 // resolve.cc.
1188 static bool
1189 should_override(const Symbol*, unsigned int, Object*, bool*);
1191 // Override a symbol.
1192 template<int size, bool big_endian>
1193 void
1194 override(Sized_symbol<size>* tosym,
1195 const elfcpp::Sym<size, big_endian>& fromsym,
1196 Object* object, const char* version);
1198 // Whether we should override a symbol with a special symbol which
1199 // is automatically defined by the linker.
1200 static bool
1201 should_override_with_special(const Symbol*);
1203 // Override a symbol with a special symbol.
1204 template<int size>
1205 void
1206 override_with_special(Sized_symbol<size>* tosym,
1207 const Sized_symbol<size>* fromsym);
1209 // Record all weak alias sets for a dynamic object.
1210 template<int size>
1211 void
1212 record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1214 // Define a special symbol.
1215 template<int size, bool big_endian>
1216 Sized_symbol<size>*
1217 define_special_symbol(const char** pname, const char** pversion,
1218 bool only_if_ref, Sized_symbol<size>** poldsym);
1220 // Define a symbol in an Output_data, sized version.
1221 template<int size>
1222 Sized_symbol<size>*
1223 do_define_in_output_data(const char* name, const char* version, Output_data*,
1224 typename elfcpp::Elf_types<size>::Elf_Addr value,
1225 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1226 elfcpp::STT type, elfcpp::STB binding,
1227 elfcpp::STV visibility, unsigned char nonvis,
1228 bool offset_is_from_end, bool only_if_ref);
1230 // Define a symbol in an Output_segment, sized version.
1231 template<int size>
1232 Sized_symbol<size>*
1233 do_define_in_output_segment(
1234 const char* name, const char* version, Output_segment* os,
1235 typename elfcpp::Elf_types<size>::Elf_Addr value,
1236 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1237 elfcpp::STT type, elfcpp::STB binding,
1238 elfcpp::STV visibility, unsigned char nonvis,
1239 Symbol::Segment_offset_base offset_base, bool only_if_ref);
1241 // Define a symbol as a constant, sized version.
1242 template<int size>
1243 Sized_symbol<size>*
1244 do_define_as_constant(
1245 const char* name, const char* version,
1246 typename elfcpp::Elf_types<size>::Elf_Addr value,
1247 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1248 elfcpp::STT type, elfcpp::STB binding,
1249 elfcpp::STV visibility, unsigned char nonvis,
1250 bool only_if_ref, bool force_override);
1252 // Allocate the common symbols, sized version.
1253 template<int size>
1254 void
1255 do_allocate_commons(const General_options&, Layout*);
1257 // Implement detect_odr_violations.
1258 template<int size, bool big_endian>
1259 void
1260 sized_detect_odr_violations() const;
1262 // Finalize symbols specialized for size.
1263 template<int size>
1264 off_t
1265 sized_finalize(off_t, Stringpool*, unsigned int*);
1267 // Finalize a symbol. Return whether it should be added to the
1268 // symbol table.
1269 template<int size>
1270 bool
1271 sized_finalize_symbol(Symbol*);
1273 // Add a symbol the final symtab by setting its index.
1274 template<int size>
1275 void
1276 add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
1278 // Write globals specialized for size and endianness.
1279 template<int size, bool big_endian>
1280 void
1281 sized_write_globals(const Input_objects*, const Stringpool*,
1282 const Stringpool*, Output_file*) const;
1284 // Write out a symbol to P.
1285 template<int size, bool big_endian>
1286 void
1287 sized_write_symbol(Sized_symbol<size>*,
1288 typename elfcpp::Elf_types<size>::Elf_Addr value,
1289 unsigned int shndx,
1290 const Stringpool*, unsigned char* p) const;
1292 // Possibly warn about an undefined symbol from a dynamic object.
1293 void
1294 warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const;
1296 // Write out a section symbol, specialized for size and endianness.
1297 template<int size, bool big_endian>
1298 void
1299 sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1301 // The type of the symbol hash table.
1303 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1305 struct Symbol_table_hash
1307 size_t
1308 operator()(const Symbol_table_key&) const;
1311 struct Symbol_table_eq
1313 bool
1314 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1317 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1318 Symbol_table_eq> Symbol_table_type;
1320 // The type of the list of common symbols.
1321 typedef std::vector<Symbol*> Commons_type;
1323 // The type of the list of symbols which have been forced local.
1324 typedef std::vector<Symbol*> Forced_locals;
1326 // A map from symbols with COPY relocs to the dynamic objects where
1327 // they are defined.
1328 typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1330 // A map from symbol name (as a pointer into the namepool) to all
1331 // the locations the symbols is (weakly) defined (and certain other
1332 // conditions are met). This map will be used later to detect
1333 // possible One Definition Rule (ODR) violations.
1334 struct Symbol_location
1336 Object* object; // Object where the symbol is defined.
1337 unsigned int shndx; // Section-in-object where the symbol is defined.
1338 off_t offset; // Offset-in-section where the symbol is defined.
1339 bool operator==(const Symbol_location& that) const
1341 return (this->object == that.object
1342 && this->shndx == that.shndx
1343 && this->offset == that.offset);
1347 struct Symbol_location_hash
1349 size_t operator()(const Symbol_location& loc) const
1350 { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1353 typedef Unordered_map<const char*,
1354 Unordered_set<Symbol_location, Symbol_location_hash> >
1355 Odr_map;
1357 // We increment this every time we see a new undefined symbol, for
1358 // use in archive groups.
1359 int saw_undefined_;
1360 // The index of the first global symbol in the output file.
1361 unsigned int first_global_index_;
1362 // The file offset within the output symtab section where we should
1363 // write the table.
1364 off_t offset_;
1365 // The number of global symbols we want to write out.
1366 unsigned int output_count_;
1367 // The file offset of the global dynamic symbols, or 0 if none.
1368 off_t dynamic_offset_;
1369 // The index of the first global dynamic symbol.
1370 unsigned int first_dynamic_global_index_;
1371 // The number of global dynamic symbols, or 0 if none.
1372 unsigned int dynamic_count_;
1373 // The symbol hash table.
1374 Symbol_table_type table_;
1375 // A pool of symbol names. This is used for all global symbols.
1376 // Entries in the hash table point into this pool.
1377 Stringpool namepool_;
1378 // Forwarding symbols.
1379 Unordered_map<const Symbol*, Symbol*> forwarders_;
1380 // Weak aliases. A symbol in this list points to the next alias.
1381 // The aliases point to each other in a circular list.
1382 Unordered_map<Symbol*, Symbol*> weak_aliases_;
1383 // We don't expect there to be very many common symbols, so we keep
1384 // a list of them. When we find a common symbol we add it to this
1385 // list. It is possible that by the time we process the list the
1386 // symbol is no longer a common symbol. It may also have become a
1387 // forwarder.
1388 Commons_type commons_;
1389 // A list of symbols which have been forced to be local. We don't
1390 // expect there to be very many of them, so we keep a list of them
1391 // rather than walking the whole table to find them.
1392 Forced_locals forced_locals_;
1393 // Manage symbol warnings.
1394 Warnings warnings_;
1395 // Manage potential One Definition Rule (ODR) violations.
1396 Odr_map candidate_odr_violations_;
1398 // When we emit a COPY reloc for a symbol, we define it in an
1399 // Output_data. When it's time to emit version information for it,
1400 // we need to know the dynamic object in which we found the original
1401 // definition. This maps symbols with COPY relocs to the dynamic
1402 // object where they were defined.
1403 Copied_symbol_dynobjs copied_symbol_dynobjs_;
1404 // Information parsed from the version script, if any.
1405 const Version_script_info& version_script_;
1408 // We inline get_sized_symbol for efficiency.
1410 template<int size>
1411 Sized_symbol<size>*
1412 Symbol_table::get_sized_symbol(Symbol* sym) const
1414 gold_assert(size == parameters->target().get_size());
1415 return static_cast<Sized_symbol<size>*>(sym);
1418 template<int size>
1419 const Sized_symbol<size>*
1420 Symbol_table::get_sized_symbol(const Symbol* sym) const
1422 gold_assert(size == parameters->target().get_size());
1423 return static_cast<const Sized_symbol<size>*>(sym);
1426 } // End namespace gold.
1428 #endif // !defined(GOLD_SYMTAB_H)