1 // symtab.h -- the gold symbol table -*- C++ -*-
3 // Copyright 2006, 2007 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.
31 #include "parameters.h"
32 #include "stringpool.h"
43 template<int size
, bool big_endian
>
46 template<int size
, bool big_endian
>
49 class Version_script_info
;
57 // The base class of an entry in the symbol table. The symbol table
58 // can have a lot of entries, so we don't want this class to big.
59 // Size dependent fields can be found in the template class
60 // Sized_symbol. Targets may support their own derived classes.
65 // Because we want the class to be small, we don't use any virtual
66 // functions. But because symbols can be defined in different
67 // places, we need to classify them. This enum is the different
68 // sources of symbols we support.
71 // Symbol defined in a relocatable or dynamic input file--this is
72 // the most common case.
74 // Symbol defined in an Output_data, a special section created by
77 // Symbol defined in an Output_segment, with no associated
80 // Symbol value is constant.
84 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
86 enum Segment_offset_base
88 // From the start of the segment.
90 // From the end of the segment.
92 // From the filesz of the segment--i.e., after the loaded bytes
93 // but before the bytes which are allocated but zeroed.
97 // Return the symbol name.
100 { return this->name_
; }
102 // Return the (ANSI) demangled version of the name, if
103 // parameters.demangle() is true. Otherwise, return the name. This
104 // is intended to be used only for logging errors, so it's not
107 demangled_name() const;
109 // Return the symbol version. This will return NULL for an
110 // unversioned symbol.
113 { return this->version_
; }
115 // Return whether this version is the default for this symbol name
116 // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
117 // meaningful for versioned symbols.
121 gold_assert(this->version_
!= NULL
);
122 return this->is_def_
;
125 // Set whether this version is the default for this symbol name.
127 set_is_default(bool def
)
128 { this->is_def_
= def
; }
130 // Return the symbol source.
133 { return this->source_
; }
135 // Return the object with which this symbol is associated.
139 gold_assert(this->source_
== FROM_OBJECT
);
140 return this->u_
.from_object
.object
;
143 // Return the index of the section in the input relocatable or
144 // dynamic object file.
148 gold_assert(this->source_
== FROM_OBJECT
);
149 return this->u_
.from_object
.shndx
;
152 // Return the output data section with which this symbol is
153 // associated, if the symbol was specially defined with respect to
154 // an output data section.
158 gold_assert(this->source_
== IN_OUTPUT_DATA
);
159 return this->u_
.in_output_data
.output_data
;
162 // If this symbol was defined with respect to an output data
163 // section, return whether the value is an offset from end.
165 offset_is_from_end() const
167 gold_assert(this->source_
== IN_OUTPUT_DATA
);
168 return this->u_
.in_output_data
.offset_is_from_end
;
171 // Return the output segment with which this symbol is associated,
172 // if the symbol was specially defined with respect to an output
175 output_segment() const
177 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
178 return this->u_
.in_output_segment
.output_segment
;
181 // If this symbol was defined with respect to an output segment,
182 // return the offset base.
186 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
187 return this->u_
.in_output_segment
.offset_base
;
190 // Return the symbol binding.
193 { return this->binding_
; }
195 // Return the symbol type.
198 { return this->type_
; }
200 // Return the symbol visibility.
203 { return this->visibility_
; }
205 // Return the non-visibility part of the st_other field.
208 { return this->nonvis_
; }
210 // Return whether this symbol is a forwarder. This will never be
211 // true of a symbol found in the hash table, but may be true of
212 // symbol pointers attached to object files.
215 { return this->is_forwarder_
; }
217 // Mark this symbol as a forwarder.
220 { this->is_forwarder_
= true; }
222 // Return whether this symbol has an alias in the weak aliases table
226 { return this->has_alias_
; }
228 // Mark this symbol as having an alias.
231 { this->has_alias_
= true; }
233 // Return whether this symbol needs an entry in the dynamic symbol
236 needs_dynsym_entry() const
238 return (this->needs_dynsym_entry_
239 || (this->in_reg() && this->in_dyn()));
242 // Mark this symbol as needing an entry in the dynamic symbol table.
244 set_needs_dynsym_entry()
245 { this->needs_dynsym_entry_
= true; }
247 // Return whether this symbol should be added to the dynamic symbol
250 should_add_dynsym_entry() const;
252 // Return whether this symbol has been seen in a regular object.
255 { return this->in_reg_
; }
257 // Mark this symbol as having been seen in a regular object.
260 { this->in_reg_
= true; }
262 // Return whether this symbol has been seen in a dynamic object.
265 { return this->in_dyn_
; }
267 // Mark this symbol as having been seen in a dynamic object.
270 { this->in_dyn_
= true; }
272 // Return the index of this symbol in the output file symbol table.
273 // A value of -1U means that this symbol is not going into the
274 // output file. This starts out as zero, and is set to a non-zero
275 // value by Symbol_table::finalize. It is an error to ask for the
276 // symbol table index before it has been set.
280 gold_assert(this->symtab_index_
!= 0);
281 return this->symtab_index_
;
284 // Set the index of the symbol in the output file symbol table.
286 set_symtab_index(unsigned int index
)
288 gold_assert(index
!= 0);
289 this->symtab_index_
= index
;
292 // Return whether this symbol already has an index in the output
293 // file symbol table.
295 has_symtab_index() const
296 { return this->symtab_index_
!= 0; }
298 // Return the index of this symbol in the dynamic symbol table. A
299 // value of -1U means that this symbol is not going into the dynamic
300 // symbol table. This starts out as zero, and is set to a non-zero
301 // during Layout::finalize. It is an error to ask for the dynamic
302 // symbol table index before it has been set.
306 gold_assert(this->dynsym_index_
!= 0);
307 return this->dynsym_index_
;
310 // Set the index of the symbol in the dynamic symbol table.
312 set_dynsym_index(unsigned int index
)
314 gold_assert(index
!= 0);
315 this->dynsym_index_
= index
;
318 // Return whether this symbol already has an index in the dynamic
321 has_dynsym_index() const
322 { return this->dynsym_index_
!= 0; }
324 // Return whether this symbol has an entry in the GOT section.
325 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
327 has_got_offset() const
328 { return this->has_got_offset_
; }
330 // Return the offset into the GOT section of this symbol.
334 gold_assert(this->has_got_offset());
335 return this->got_offset_
;
338 // Set the GOT offset of this symbol.
340 set_got_offset(unsigned int got_offset
)
342 this->has_got_offset_
= true;
343 this->got_offset_
= got_offset
;
346 // Return whether this TLS symbol has an entry in the GOT section for
347 // its module index or, if NEED_PAIR is true, has a pair of entries
348 // for its module index and dtv-relative offset.
350 has_tls_got_offset(bool need_pair
) const
352 return (this->has_tls_mod_got_offset_
353 && (!need_pair
|| this->has_tls_pair_got_offset_
));
356 // Return the offset into the GOT section for this symbol's TLS module
357 // index or, if NEED_PAIR is true, for the pair of entries for the
358 // module index and dtv-relative offset.
360 tls_got_offset(bool need_pair
) const
362 gold_assert(this->has_tls_got_offset(need_pair
));
363 return this->tls_mod_got_offset_
;
366 // Set the GOT offset of this symbol.
368 set_tls_got_offset(unsigned int got_offset
, bool have_pair
)
370 this->has_tls_mod_got_offset_
= true;
371 this->has_tls_pair_got_offset_
= have_pair
;
372 this->tls_mod_got_offset_
= got_offset
;
375 // Return whether this symbol has an entry in the PLT section.
377 has_plt_offset() const
378 { return this->has_plt_offset_
; }
380 // Return the offset into the PLT section of this symbol.
384 gold_assert(this->has_plt_offset());
385 return this->plt_offset_
;
388 // Set the PLT offset of this symbol.
390 set_plt_offset(unsigned int plt_offset
)
392 this->has_plt_offset_
= true;
393 this->plt_offset_
= plt_offset
;
396 // Return whether this dynamic symbol needs a special value in the
397 // dynamic symbol table.
399 needs_dynsym_value() const
400 { return this->needs_dynsym_value_
; }
402 // Set that this dynamic symbol needs a special value in the dynamic
405 set_needs_dynsym_value()
407 gold_assert(this->object()->is_dynamic());
408 this->needs_dynsym_value_
= true;
411 // Return true if the final value of this symbol is known at link
414 final_value_is_known() const;
416 // Return whether this is a defined symbol (not undefined or
421 return (this->source_
!= FROM_OBJECT
422 || (this->shndx() != elfcpp::SHN_UNDEF
423 && this->shndx() != elfcpp::SHN_COMMON
));
426 // Return true if this symbol is from a dynamic object.
428 is_from_dynobj() const
430 return this->source_
== FROM_OBJECT
&& this->object()->is_dynamic();
433 // Return whether this is an undefined symbol.
437 return this->source_
== FROM_OBJECT
&& this->shndx() == elfcpp::SHN_UNDEF
;
440 // Return whether this is a common symbol.
444 return (this->source_
== FROM_OBJECT
445 && (this->shndx() == elfcpp::SHN_COMMON
446 || this->type_
== elfcpp::STT_COMMON
));
449 // Return whether this symbol can be seen outside this object.
451 is_externally_visible() const
453 return (this->visibility_
== elfcpp::STV_DEFAULT
454 || this->visibility_
== elfcpp::STV_PROTECTED
);
457 // Return true if this symbol can be preempted by a definition in
458 // another link unit.
460 is_preemptible() const
462 // It doesn't make sense to ask whether a symbol defined in
463 // another object is preemptible.
464 gold_assert(!this->is_from_dynobj());
466 return (this->visibility_
!= elfcpp::STV_INTERNAL
467 && this->visibility_
!= elfcpp::STV_HIDDEN
468 && this->visibility_
!= elfcpp::STV_PROTECTED
469 && parameters
->output_is_shared()
470 && !parameters
->symbolic());
473 // Return true if this symbol is a function that needs a PLT entry.
474 // If the symbol is defined in a dynamic object or if it is subject
475 // to pre-emption, we need to make a PLT entry.
477 needs_plt_entry() const
479 return (this->type() == elfcpp::STT_FUNC
480 && (this->is_from_dynobj() || this->is_preemptible()));
483 // When determining whether a reference to a symbol needs a dynamic
484 // relocation, we need to know several things about the reference.
485 // These flags may be or'ed together.
488 // Reference to the symbol's absolute address.
490 // A non-PIC reference.
496 // Given a direct absolute or pc-relative static relocation against
497 // the global symbol, this function returns whether a dynamic relocation
501 needs_dynamic_reloc(int flags
) const
503 // An absolute reference within a position-independent output file
504 // will need a dynamic relocation.
505 if ((flags
& ABSOLUTE_REF
)
506 && parameters
->output_is_position_independent())
509 // A function call that can branch to a local PLT entry does not need
510 // a dynamic relocation.
511 if ((flags
& FUNCTION_CALL
) && this->has_plt_offset())
514 // A non-pic pc-relative function call in a shared library whose target
515 // is defined in the same load module does not need a dynamic relocation.
516 // Even if the target is preemptible, we will bind directly, since we
517 // cannot use a PLT entry in this case.
518 if ((flags
& FUNCTION_CALL
)
519 && (flags
& NON_PIC_REF
)
520 && this->is_defined()
521 && parameters
->output_is_shared())
524 // A reference to any PLT entry in a non-position-independent executable
525 // does not need a dynamic relocation.
526 if (!parameters
->output_is_position_independent()
527 && this->has_plt_offset())
530 // A reference to a symbol defined in a dynamic object or to a
531 // symbol that is preemptible will need a dynamic relocation.
532 if (this->is_from_dynobj() || this->is_preemptible())
535 // For all other cases, return FALSE.
539 // Given a direct absolute static relocation against
540 // the global symbol, where a dynamic relocation is needed, this
541 // function returns whether a relative dynamic relocation can be used.
542 // The caller must determine separately whether the static relocation
543 // is compatible with a relative relocation.
546 can_use_relative_reloc(bool is_function_call
) const
548 // A function call that can branch to a local PLT entry can
549 // use a RELATIVE relocation.
550 if (is_function_call
&& this->has_plt_offset())
553 // A reference to a symbol defined in a dynamic object or to a
554 // symbol that is preemptible can not use a RELATIVE relocaiton.
555 if (this->is_from_dynobj() || this->is_preemptible())
558 // For all other cases, return TRUE.
562 // Return whether there should be a warning for references to this
566 { return this->has_warning_
; }
568 // Mark this symbol as having a warning.
571 { this->has_warning_
= true; }
573 // Return whether this symbol is defined by a COPY reloc from a
576 is_copied_from_dynobj() const
577 { return this->is_copied_from_dynobj_
; }
579 // Mark this symbol as defined by a COPY reloc.
581 set_is_copied_from_dynobj()
582 { this->is_copied_from_dynobj_
= true; }
585 // Instances of this class should always be created at a specific
588 { memset(this, 0, sizeof *this); }
590 // Initialize the general fields.
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
>
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.
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.
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.
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
>
621 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
622 const char* version
);
624 // Override existing symbol with a special symbol.
626 override_base_with_special(const Symbol
* from
);
628 // Allocate a common symbol by giving it a location in the output
631 allocate_base_common(Output_data
*);
634 Symbol(const Symbol
&);
635 Symbol
& operator=(const Symbol
&);
637 // Symbol name (expected to point into a Stringpool).
639 // Symbol version (expected to point into a Stringpool). This may
641 const char* version_
;
645 // This struct is used if SOURCE_ == FROM_OBJECT.
648 // Object in which symbol is defined, or in which it was first
651 // Section number in object_ in which symbol is defined.
655 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
658 // Output_data in which symbol is defined. Before
659 // Layout::finalize the symbol's value is an offset within the
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
;
667 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
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
;
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 is the offset from the start of the GOT section.
692 // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
693 // serves as the GOT offset for the GOT entry that holds its
694 // TP-relative offset.
695 unsigned int got_offset_
;
697 // If this is a TLS symbol and has an entry in the GOT section
698 // for a module index or a pair of entries (module index,
699 // dtv-relative offset), these are the offsets from the start
700 // of the GOT section.
701 unsigned int tls_mod_got_offset_
;
702 unsigned int tls_pair_got_offset_
;
704 // If this symbol has an entry in the PLT section (has_plt_offset_
705 // is true), then this is the offset from the start of the PLT
707 unsigned int plt_offset_
;
710 elfcpp::STT type_
: 4;
712 elfcpp::STB binding_
: 4;
713 // Symbol visibility.
714 elfcpp::STV visibility_
: 2;
715 // Rest of symbol st_other field.
716 unsigned int nonvis_
: 6;
717 // The type of symbol.
719 // True if this symbol always requires special target-specific
721 bool is_target_special_
: 1;
722 // True if this is the default version of the symbol.
724 // True if this symbol really forwards to another symbol. This is
725 // used when we discover after the fact that two different entries
726 // in the hash table really refer to the same symbol. This will
727 // never be set for a symbol found in the hash table, but may be set
728 // for a symbol found in the list of symbols attached to an Object.
729 // It forwards to the symbol found in the forwarders_ map of
731 bool is_forwarder_
: 1;
732 // True if the symbol has an alias in the weak_aliases table in
735 // True if this symbol needs to be in the dynamic symbol table.
736 bool needs_dynsym_entry_
: 1;
737 // True if we've seen this symbol in a regular object.
739 // True if we've seen this symbol in a dynamic object.
741 // True if the symbol has an entry in the GOT section.
742 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
743 bool has_got_offset_
: 1;
744 // True if the symbol has an entry in the GOT section for its
746 bool has_tls_mod_got_offset_
: 1;
747 // True if the symbol has a pair of entries in the GOT section for its
748 // module index and dtv-relative offset.
749 bool has_tls_pair_got_offset_
: 1;
750 // True if the symbol has an entry in the PLT section.
751 bool has_plt_offset_
: 1;
752 // True if this is a dynamic symbol which needs a special value in
753 // the dynamic symbol table.
754 bool needs_dynsym_value_
: 1;
755 // True if there is a warning for this symbol.
756 bool has_warning_
: 1;
757 // True if we are using a COPY reloc for this symbol, so that the
758 // real definition lives in a dynamic object.
759 bool is_copied_from_dynobj_
: 1;
762 // The parts of a symbol which are size specific. Using a template
763 // derived class like this helps us use less space on a 32-bit system.
766 class Sized_symbol
: public Symbol
769 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
770 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
775 // Initialize fields from an ELF symbol in OBJECT.
776 template<bool big_endian
>
778 init(const char *name
, const char* version
, Object
* object
,
779 const elfcpp::Sym
<size
, big_endian
>&);
781 // Initialize fields for an Output_data.
783 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
784 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
785 bool offset_is_from_end
);
787 // Initialize fields for an Output_segment.
789 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
790 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
791 Segment_offset_base offset_base
);
793 // Initialize fields for a constant.
795 init(const char* name
, Value_type value
, Size_type symsize
,
796 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
798 // Override existing symbol.
799 template<bool big_endian
>
801 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
802 const char* version
);
804 // Override existing symbol with a special symbol.
806 override_with_special(const Sized_symbol
<size
>*);
808 // Return the symbol's value.
811 { return this->value_
; }
813 // Return the symbol's size (we can't call this 'size' because that
814 // is a template parameter).
817 { return this->symsize_
; }
819 // Set the symbol size. This is used when resolving common symbols.
821 set_symsize(Size_type symsize
)
822 { this->symsize_
= symsize
; }
824 // Set the symbol value. This is called when we store the final
825 // values of the symbols into the symbol table.
827 set_value(Value_type value
)
828 { this->value_
= value
; }
830 // Allocate a common symbol by giving it a location in the output
833 allocate_common(Output_data
*, Value_type value
);
836 Sized_symbol(const Sized_symbol
&);
837 Sized_symbol
& operator=(const Sized_symbol
&);
839 // Symbol value. Before Layout::finalize this is the offset in the
840 // input section. This is set to the final value during
847 // A struct describing a symbol defined by the linker, where the value
848 // of the symbol is defined based on an output section. This is used
849 // for symbols defined by the linker, like "_init_array_start".
851 struct Define_symbol_in_section
855 // The name of the output section with which this symbol should be
856 // associated. If there is no output section with that name, the
857 // symbol will be defined as zero.
858 const char* output_section
;
859 // The offset of the symbol within the output section. This is an
860 // offset from the start of the output section, unless start_at_end
861 // is true, in which case this is an offset from the end of the
864 // The size of the symbol.
868 // The symbol binding.
870 // The symbol visibility.
871 elfcpp::STV visibility
;
872 // The rest of the st_other field.
873 unsigned char nonvis
;
874 // If true, the value field is an offset from the end of the output
876 bool offset_is_from_end
;
877 // If true, this symbol is defined only if we see a reference to it.
881 // A struct describing a symbol defined by the linker, where the value
882 // of the symbol is defined based on a segment. This is used for
883 // symbols defined by the linker, like "_end". We describe the
884 // segment with which the symbol should be associated by its
885 // characteristics. If no segment meets these characteristics, the
886 // symbol will be defined as zero. If there is more than one segment
887 // which meets these characteristics, we will use the first one.
889 struct Define_symbol_in_segment
893 // The segment type where the symbol should be defined, typically
895 elfcpp::PT segment_type
;
896 // Bitmask of segment flags which must be set.
897 elfcpp::PF segment_flags_set
;
898 // Bitmask of segment flags which must be clear.
899 elfcpp::PF segment_flags_clear
;
900 // The offset of the symbol within the segment. The offset is
901 // calculated from the position set by offset_base.
903 // The size of the symbol.
907 // The symbol binding.
909 // The symbol visibility.
910 elfcpp::STV visibility
;
911 // The rest of the st_other field.
912 unsigned char nonvis
;
913 // The base from which we compute the offset.
914 Symbol::Segment_offset_base offset_base
;
915 // If true, this symbol is defined only if we see a reference to it.
919 // This class manages warnings. Warnings are a GNU extension. When
920 // we see a section named .gnu.warning.SYM in an object file, and if
921 // we wind using the definition of SYM from that object file, then we
922 // will issue a warning for any relocation against SYM from a
923 // different object file. The text of the warning is the contents of
924 // the section. This is not precisely the definition used by the old
925 // GNU linker; the old GNU linker treated an occurrence of
926 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
927 // would trigger a warning on any reference. However, it was
928 // inconsistent in that a warning in a dynamic object only triggered
929 // if there was no definition in a regular object. This linker is
930 // different in that we only issue a warning if we use the symbol
931 // definition from the same object file as the warning section.
940 // Add a warning for symbol NAME in object OBJ. WARNING is the text
943 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
944 const std::string
& warning
);
946 // For each symbol for which we should give a warning, make a note
949 note_warnings(Symbol_table
* symtab
);
951 // Issue a warning for a reference to SYM at RELINFO's location.
952 template<int size
, bool big_endian
>
954 issue_warning(const Symbol
* sym
, const Relocate_info
<size
, big_endian
>*,
955 size_t relnum
, off_t reloffset
) const;
958 Warnings(const Warnings
&);
959 Warnings
& operator=(const Warnings
&);
961 // What we need to know to get the warning text.
962 struct Warning_location
964 // The object the warning is in.
970 : object(NULL
), text()
974 set(Object
* o
, const std::string
& t
)
981 // A mapping from warning symbol names (canonicalized in
982 // Symbol_table's namepool_ field) to warning information.
983 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
985 Warning_table warnings_
;
988 // The main linker symbol table.
993 // COUNT is an estimate of how many symbosl will be inserted in the
994 // symbol table. It's ok to put 0 if you don't know; a correct
995 // guess will just save some CPU by reducing hashtable resizes.
996 Symbol_table(unsigned int count
, const Version_script_info
& version_script
);
1000 // Add COUNT external symbols from the relocatable object RELOBJ to
1001 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
1002 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
1003 // point to the symbols in the symbol table.
1004 template<int size
, bool big_endian
>
1006 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
1007 const unsigned char* syms
, size_t count
,
1008 const char* sym_names
, size_t sym_name_size
,
1009 typename Sized_relobj
<size
, big_endian
>::Symbols
*);
1011 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1012 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1013 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1014 // symbol version data.
1015 template<int size
, bool big_endian
>
1017 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
1018 const unsigned char* syms
, size_t count
,
1019 const char* sym_names
, size_t sym_name_size
,
1020 const unsigned char* versym
, size_t versym_size
,
1021 const std::vector
<const char*>*);
1023 // Define a special symbol based on an Output_data. It is a
1024 // multiple definition error if this symbol is already defined.
1026 define_in_output_data(const Target
*, const char* name
, const char* version
,
1027 Output_data
*, uint64_t value
, uint64_t symsize
,
1028 elfcpp::STT type
, elfcpp::STB binding
,
1029 elfcpp::STV visibility
, unsigned char nonvis
,
1030 bool offset_is_from_end
, bool only_if_ref
);
1032 // Define a special symbol based on an Output_segment. It is a
1033 // multiple definition error if this symbol is already defined.
1035 define_in_output_segment(const Target
*, const char* name
,
1036 const char* version
, Output_segment
*,
1037 uint64_t value
, uint64_t symsize
,
1038 elfcpp::STT type
, elfcpp::STB binding
,
1039 elfcpp::STV visibility
, unsigned char nonvis
,
1040 Symbol::Segment_offset_base
, bool only_if_ref
);
1042 // Define a special symbol with a constant value. It is a multiple
1043 // definition error if this symbol is already defined.
1045 define_as_constant(const Target
*, const char* name
, const char* version
,
1046 uint64_t value
, uint64_t symsize
, elfcpp::STT type
,
1047 elfcpp::STB binding
, elfcpp::STV visibility
,
1048 unsigned char nonvis
, bool only_if_ref
);
1050 // Define a set of symbols in output sections.
1052 define_symbols(const Layout
*, const Target
*, int count
,
1053 const Define_symbol_in_section
*);
1055 // Define a set of symbols in output segments.
1057 define_symbols(const Layout
*, const Target
*, int count
,
1058 const Define_symbol_in_segment
*);
1060 // Define SYM using a COPY reloc. POSD is the Output_data where the
1061 // symbol should be defined--typically a .dyn.bss section. VALUE is
1062 // the offset within POSD.
1065 define_with_copy_reloc(const Target
*, Sized_symbol
<size
>* sym
,
1067 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
1069 // Look up a symbol.
1071 lookup(const char*, const char* version
= NULL
) const;
1073 // Return the real symbol associated with the forwarder symbol FROM.
1075 resolve_forwards(const Symbol
* from
) const;
1077 // Return the sized version of a symbol in this table.
1080 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
1083 const Sized_symbol
<size
>*
1084 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
1086 // Return the count of undefined symbols seen.
1088 saw_undefined() const
1089 { return this->saw_undefined_
; }
1091 // Allocate the common symbols
1093 allocate_commons(const General_options
&, Layout
*);
1095 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1098 add_warning(const char* name
, Object
* obj
, const std::string
& warning
)
1099 { this->warnings_
.add_warning(this, name
, obj
, warning
); }
1101 // Canonicalize a symbol name for use in the hash table.
1103 canonicalize_name(const char* name
)
1104 { return this->namepool_
.add(name
, true, NULL
); }
1106 // Possibly issue a warning for a reference to SYM at LOCATION which
1108 template<int size
, bool big_endian
>
1110 issue_warning(const Symbol
* sym
,
1111 const Relocate_info
<size
, big_endian
>* relinfo
,
1112 size_t relnum
, off_t reloffset
) const
1113 { this->warnings_
.issue_warning(sym
, relinfo
, relnum
, reloffset
); }
1115 // Check candidate_odr_violations_ to find symbols with the same name
1116 // but apparently different definitions (different source-file/line-no).
1118 detect_odr_violations(const Task
*, const char* output_file_name
) const;
1120 // SYM is defined using a COPY reloc. Return the dynamic object
1121 // where the original definition was found.
1123 get_copy_source(const Symbol
* sym
) const;
1125 // Set the dynamic symbol indexes. INDEX is the index of the first
1126 // global dynamic symbol. Pointers to the symbols are stored into
1127 // the vector. The names are stored into the Stringpool. This
1128 // returns an updated dynamic symbol index.
1130 set_dynsym_indexes(const Target
*, unsigned int index
,
1131 std::vector
<Symbol
*>*, Stringpool
*, Versions
*);
1133 // Finalize the symbol table after we have set the final addresses
1134 // of all the input sections. This sets the final symbol indexes,
1135 // values and adds the names to *POOL. INDEX is the index of the
1136 // first global symbol. OFF is the file offset of the global symbol
1137 // table, DYNOFF is the offset of the globals in the dynamic symbol
1138 // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
1139 // symbol, and DYNCOUNT is the number of global dynamic symbols.
1140 // This records the parameters, and returns the new file offset.
1142 finalize(unsigned int index
, off_t off
, off_t dynoff
,
1143 size_t dyn_global_index
, size_t dyncount
, Stringpool
* pool
);
1145 // Write out the global symbols.
1147 write_globals(const Input_objects
*, const Stringpool
*, const Stringpool
*,
1148 Output_file
*) const;
1150 // Write out a section symbol. Return the updated offset.
1152 write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1154 // Dump statistical information to stderr.
1156 print_stats() const;
1158 // Return the version script information.
1159 const Version_script_info
&
1160 version_script() const
1161 { return version_script_
; }
1164 Symbol_table(const Symbol_table
&);
1165 Symbol_table
& operator=(const Symbol_table
&);
1167 // Make FROM a forwarder symbol to TO.
1169 make_forwarder(Symbol
* from
, Symbol
* to
);
1172 template<int size
, bool big_endian
>
1174 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
1175 const char *version
, Stringpool::Key version_key
,
1176 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
,
1177 const elfcpp::Sym
<size
, big_endian
>& orig_sym
);
1180 template<int size
, bool big_endian
>
1182 resolve(Sized_symbol
<size
>* to
,
1183 const elfcpp::Sym
<size
, big_endian
>& sym
,
1184 const elfcpp::Sym
<size
, big_endian
>& orig_sym
,
1185 Object
*, const char* version
);
1187 template<int size
, bool big_endian
>
1189 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
1190 const char* version ACCEPT_SIZE_ENDIAN
);
1192 // Whether we should override a symbol, based on flags in
1195 should_override(const Symbol
*, unsigned int, Object
*, bool*);
1197 // Override a symbol.
1198 template<int size
, bool big_endian
>
1200 override(Sized_symbol
<size
>* tosym
,
1201 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
1202 Object
* object
, const char* version
);
1204 // Whether we should override a symbol with a special symbol which
1205 // is automatically defined by the linker.
1207 should_override_with_special(const Symbol
*);
1209 // Override a symbol with a special symbol.
1212 override_with_special(Sized_symbol
<size
>* tosym
,
1213 const Sized_symbol
<size
>* fromsym
);
1215 // Record all weak alias sets for a dynamic object.
1218 record_weak_aliases(std::vector
<Sized_symbol
<size
>*>*);
1220 // Define a special symbol.
1221 template<int size
, bool big_endian
>
1223 define_special_symbol(const Target
* target
, const char** pname
,
1224 const char** pversion
, bool only_if_ref
,
1225 Sized_symbol
<size
>** poldsym ACCEPT_SIZE_ENDIAN
);
1227 // Define a symbol in an Output_data, sized version.
1230 do_define_in_output_data(const Target
*, const char* name
,
1231 const char* version
, Output_data
*,
1232 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1233 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1234 elfcpp::STT type
, elfcpp::STB binding
,
1235 elfcpp::STV visibility
, unsigned char nonvis
,
1236 bool offset_is_from_end
, bool only_if_ref
);
1238 // Define a symbol in an Output_segment, sized version.
1241 do_define_in_output_segment(
1242 const Target
*, const char* name
, const char* version
, Output_segment
* os
,
1243 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1244 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1245 elfcpp::STT type
, elfcpp::STB binding
,
1246 elfcpp::STV visibility
, unsigned char nonvis
,
1247 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
1249 // Define a symbol as a constant, sized version.
1252 do_define_as_constant(
1253 const Target
*, const char* name
, const char* version
,
1254 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1255 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1256 elfcpp::STT type
, elfcpp::STB binding
,
1257 elfcpp::STV visibility
, unsigned char nonvis
,
1260 // Allocate the common symbols, sized version.
1263 do_allocate_commons(const General_options
&, Layout
*);
1265 // Implement detect_odr_violations.
1266 template<int size
, bool big_endian
>
1268 sized_detect_odr_violations() const;
1270 // Finalize symbols specialized for size.
1273 sized_finalize(unsigned int, off_t
, Stringpool
*);
1275 // Write globals specialized for size and endianness.
1276 template<int size
, bool big_endian
>
1278 sized_write_globals(const Input_objects
*, const Stringpool
*,
1279 const Stringpool
*, Output_file
*) const;
1281 // Write out a symbol to P.
1282 template<int size
, bool big_endian
>
1284 sized_write_symbol(Sized_symbol
<size
>*,
1285 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1287 const Stringpool
*, unsigned char* p
1288 ACCEPT_SIZE_ENDIAN
) const;
1290 // Possibly warn about an undefined symbol from a dynamic object.
1292 warn_about_undefined_dynobj_symbol(const Input_objects
*, Symbol
*) const;
1294 // Write out a section symbol, specialized for size and endianness.
1295 template<int size
, bool big_endian
>
1297 sized_write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1299 // The type of the symbol hash table.
1301 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
1303 struct Symbol_table_hash
1306 operator()(const Symbol_table_key
&) const;
1309 struct Symbol_table_eq
1312 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
1315 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
1316 Symbol_table_eq
> Symbol_table_type
;
1318 // The type of the list of common symbols.
1319 typedef std::vector
<Symbol
*> Commons_type
;
1321 // A map from symbols with COPY relocs to the dynamic objects where
1322 // they are defined.
1323 typedef Unordered_map
<const Symbol
*, Dynobj
*> Copied_symbol_dynobjs
;
1325 // A map from symbol name (as a pointer into the namepool) to all
1326 // the locations the symbols is (weakly) defined (and certain other
1327 // conditions are met). This map will be used later to detect
1328 // possible One Definition Rule (ODR) violations.
1329 struct Symbol_location
1331 Object
* object
; // Object where the symbol is defined.
1332 unsigned int shndx
; // Section-in-object where the symbol is defined.
1333 off_t offset
; // Offset-in-section where the symbol is defined.
1334 bool operator==(const Symbol_location
& that
) const
1336 return (this->object
== that
.object
1337 && this->shndx
== that
.shndx
1338 && this->offset
== that
.offset
);
1342 struct Symbol_location_hash
1344 size_t operator()(const Symbol_location
& loc
) const
1345 { return reinterpret_cast<uintptr_t>(loc
.object
) ^ loc
.offset
^ loc
.shndx
; }
1348 typedef Unordered_map
<const char*,
1349 Unordered_set
<Symbol_location
, Symbol_location_hash
> >
1352 // We increment this every time we see a new undefined symbol, for
1353 // use in archive groups.
1355 // The index of the first global symbol in the output file.
1356 unsigned int first_global_index_
;
1357 // The file offset within the output symtab section where we should
1360 // The number of global symbols we want to write out.
1361 size_t output_count_
;
1362 // The file offset of the global dynamic symbols, or 0 if none.
1363 off_t dynamic_offset_
;
1364 // The index of the first global dynamic symbol.
1365 unsigned int first_dynamic_global_index_
;
1366 // The number of global dynamic symbols, or 0 if none.
1367 off_t dynamic_count_
;
1368 // The symbol hash table.
1369 Symbol_table_type table_
;
1370 // A pool of symbol names. This is used for all global symbols.
1371 // Entries in the hash table point into this pool.
1372 Stringpool namepool_
;
1373 // Forwarding symbols.
1374 Unordered_map
<const Symbol
*, Symbol
*> forwarders_
;
1375 // Weak aliases. A symbol in this list points to the next alias.
1376 // The aliases point to each other in a circular list.
1377 Unordered_map
<Symbol
*, Symbol
*> weak_aliases_
;
1378 // We don't expect there to be very many common symbols, so we keep
1379 // a list of them. When we find a common symbol we add it to this
1380 // list. It is possible that by the time we process the list the
1381 // symbol is no longer a common symbol. It may also have become a
1383 Commons_type commons_
;
1384 // Manage symbol warnings.
1386 // Manage potential One Definition Rule (ODR) violations.
1387 Odr_map candidate_odr_violations_
;
1389 // When we emit a COPY reloc for a symbol, we define it in an
1390 // Output_data. When it's time to emit version information for it,
1391 // we need to know the dynamic object in which we found the original
1392 // definition. This maps symbols with COPY relocs to the dynamic
1393 // object where they were defined.
1394 Copied_symbol_dynobjs copied_symbol_dynobjs_
;
1395 // Information parsed from the version script, if any.
1396 const Version_script_info
& version_script_
;
1399 // We inline get_sized_symbol for efficiency.
1403 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
1405 gold_assert(size
== parameters
->get_size());
1406 return static_cast<Sized_symbol
<size
>*>(sym
);
1410 const Sized_symbol
<size
>*
1411 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
1413 gold_assert(size
== parameters
->get_size());
1414 return static_cast<const Sized_symbol
<size
>*>(sym
);
1417 } // End namespace gold.
1419 #endif // !defined(GOLD_SYMTAB_H)