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.
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
;
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.
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.
70 // Symbol defined in a relocatable or dynamic input file--this is
71 // the most common case.
73 // Symbol defined in an Output_data, a special section created by
76 // Symbol defined in an Output_segment, with no associated
79 // Symbol value is constant.
83 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
85 enum Segment_offset_base
87 // From the start of the segment.
89 // From the end of the segment.
91 // From the filesz of the segment--i.e., after the loaded bytes
92 // but before the bytes which are allocated but zeroed.
96 // Return the symbol name.
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
106 demangled_name() const;
108 // Return the symbol version. This will return NULL for an
109 // unversioned symbol.
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.
120 gold_assert(this->version_
!= NULL
);
121 return this->is_def_
;
124 // Set that this version is the default for this symbol name.
127 { this->is_def_
= true; }
129 // Return the symbol source.
132 { return this->source_
; }
134 // Return the object with which this symbol is associated.
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.
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.
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.
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
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.
185 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
186 return this->u_
.in_output_segment
.offset_base
;
189 // Return the symbol binding.
192 { return this->binding_
; }
194 // Return the symbol type.
197 { return this->type_
; }
199 // Return the symbol visibility.
202 { return this->visibility_
; }
204 // Return the non-visibility part of the st_other field.
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.
214 { return this->is_forwarder_
; }
216 // Mark this symbol as a forwarder.
219 { this->is_forwarder_
= true; }
221 // Return whether this symbol has an alias in the weak aliases table
225 { return this->has_alias_
; }
227 // Mark this symbol as having an alias.
230 { this->has_alias_
= true; }
232 // Return whether this symbol needs an entry in the dynamic symbol
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.
243 set_needs_dynsym_entry()
244 { this->needs_dynsym_entry_
= true; }
246 // Return whether this symbol should be added to the dynamic symbol
249 should_add_dynsym_entry() const;
251 // Return whether this symbol has been seen in a regular object.
254 { return this->in_reg_
; }
256 // Mark this symbol as having been seen in a regular object.
259 { this->in_reg_
= true; }
261 // Return whether this symbol has been seen in a dynamic object.
264 { return this->in_dyn_
; }
266 // Mark this symbol as having been seen in a dynamic object.
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.
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.
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.
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.
305 gold_assert(this->dynsym_index_
!= 0);
306 return this->dynsym_index_
;
309 // Set the index of the symbol in the dynamic symbol table.
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
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.
326 has_got_offset() const
327 { return this->has_got_offset_
; }
329 // Return the offset into the GOT section of this symbol.
333 gold_assert(this->has_got_offset());
334 return this->got_offset_
;
337 // Set the GOT offset of this symbol.
339 set_got_offset(unsigned int got_offset
)
341 this->has_got_offset_
= true;
342 this->got_offset_
= got_offset
;
345 // Return whether this TLS symbol has an entry in the GOT section for
346 // its module index or, if NEED_PAIR is true, has a pair of entries
347 // for its module index and dtv-relative offset.
349 has_tls_got_offset(bool need_pair
) const
351 return (this->has_tls_mod_got_offset_
352 && (!need_pair
|| this->has_tls_pair_got_offset_
));
355 // Return the offset into the GOT section for this symbol's TLS module
356 // index or, if NEED_PAIR is true, for the pair of entries for the
357 // module index and dtv-relative offset.
359 tls_got_offset(bool need_pair
) const
361 gold_assert(this->has_tls_got_offset(need_pair
));
362 return this->tls_mod_got_offset_
;
365 // Set the GOT offset of this symbol.
367 set_tls_got_offset(unsigned int got_offset
, bool have_pair
)
369 this->has_tls_mod_got_offset_
= true;
370 this->has_tls_pair_got_offset_
= have_pair
;
371 this->tls_mod_got_offset_
= got_offset
;
374 // Return whether this symbol has an entry in the PLT section.
376 has_plt_offset() const
377 { return this->has_plt_offset_
; }
379 // Return the offset into the PLT section of this symbol.
383 gold_assert(this->has_plt_offset());
384 return this->plt_offset_
;
387 // Set the PLT offset of this symbol.
389 set_plt_offset(unsigned int plt_offset
)
391 this->has_plt_offset_
= true;
392 this->plt_offset_
= plt_offset
;
395 // Return whether this dynamic symbol needs a special value in the
396 // dynamic symbol table.
398 needs_dynsym_value() const
399 { return this->needs_dynsym_value_
; }
401 // Set that this dynamic symbol needs a special value in the dynamic
404 set_needs_dynsym_value()
406 gold_assert(this->object()->is_dynamic());
407 this->needs_dynsym_value_
= true;
410 // Return true if the final value of this symbol is known at link
413 final_value_is_known() const;
415 // Return whether this is a defined symbol (not undefined or
420 return (this->source_
!= FROM_OBJECT
421 || (this->shndx() != elfcpp::SHN_UNDEF
422 && this->shndx() != elfcpp::SHN_COMMON
));
425 // Return true if this symbol is from a dynamic object.
427 is_from_dynobj() const
429 return this->source_
== FROM_OBJECT
&& this->object()->is_dynamic();
432 // Return whether this is an undefined symbol.
436 return this->source_
== FROM_OBJECT
&& this->shndx() == elfcpp::SHN_UNDEF
;
439 // Return whether this is a common symbol.
443 return (this->source_
== FROM_OBJECT
444 && (this->shndx() == elfcpp::SHN_COMMON
445 || this->type_
== elfcpp::STT_COMMON
));
448 // Return whether this symbol can be seen outside this object.
450 is_externally_visible() const
452 return (this->visibility_
== elfcpp::STV_DEFAULT
453 || this->visibility_
== elfcpp::STV_PROTECTED
);
456 // Return true if this symbol can be preempted by a definition in
457 // another link unit.
459 is_preemptible() const
461 // It doesn't make sense to ask whether a symbol defined in
462 // another object is preemptible.
463 gold_assert(!this->is_from_dynobj());
465 // It doesn't make sense to ask whether an undefined symbol
467 gold_assert(!this->is_undefined());
469 return (this->visibility_
!= elfcpp::STV_INTERNAL
470 && this->visibility_
!= elfcpp::STV_HIDDEN
471 && this->visibility_
!= elfcpp::STV_PROTECTED
472 && !this->is_forced_local_
473 && parameters
->options().shared()
474 && !parameters
->options().Bsymbolic());
477 // Return true if this symbol is a function that needs a PLT entry.
478 // If the symbol is defined in a dynamic object or if it is subject
479 // to pre-emption, we need to make a PLT entry. If we're doing a
480 // static link, we don't create PLT entries.
482 needs_plt_entry() const
484 return (!parameters
->doing_static_link()
485 && this->type() == elfcpp::STT_FUNC
486 && (this->is_from_dynobj()
487 || this->is_undefined()
488 || this->is_preemptible()));
491 // When determining whether a reference to a symbol needs a dynamic
492 // relocation, we need to know several things about the reference.
493 // These flags may be or'ed together.
496 // Reference to the symbol's absolute address.
498 // A non-PIC reference.
504 // Given a direct absolute or pc-relative static relocation against
505 // the global symbol, this function returns whether a dynamic relocation
509 needs_dynamic_reloc(int flags
) const
511 // No dynamic relocations in a static link!
512 if (parameters
->doing_static_link())
515 // An absolute reference within a position-independent output file
516 // will need a dynamic relocation.
517 if ((flags
& ABSOLUTE_REF
)
518 && parameters
->options().output_is_position_independent())
521 // A function call that can branch to a local PLT entry does not need
522 // a dynamic relocation. A non-pic pc-relative function call in a
523 // shared library cannot use a PLT entry.
524 if ((flags
& FUNCTION_CALL
)
525 && this->has_plt_offset()
526 && !((flags
& NON_PIC_REF
) && parameters
->options().shared()))
529 // A reference to any PLT entry in a non-position-independent executable
530 // does not need a dynamic relocation.
531 if (!parameters
->options().output_is_position_independent()
532 && this->has_plt_offset())
535 // A reference to a symbol defined in a dynamic object or to a
536 // symbol that is preemptible will need a dynamic relocation.
537 if (this->is_from_dynobj()
538 || this->is_undefined()
539 || this->is_preemptible())
542 // For all other cases, return FALSE.
546 // Given a direct absolute static relocation against
547 // the global symbol, where a dynamic relocation is needed, this
548 // function returns whether a relative dynamic relocation can be used.
549 // The caller must determine separately whether the static relocation
550 // is compatible with a relative relocation.
553 can_use_relative_reloc(bool is_function_call
) const
555 // A function call that can branch to a local PLT entry can
556 // use a RELATIVE relocation.
557 if (is_function_call
&& this->has_plt_offset())
560 // A reference to a symbol defined in a dynamic object or to a
561 // symbol that is preemptible can not use a RELATIVE relocaiton.
562 if (this->is_from_dynobj()
563 || this->is_undefined()
564 || this->is_preemptible())
567 // For all other cases, return TRUE.
571 // Return the output section where this symbol is defined. Return
572 // NULL if the symbol has an absolute value.
574 output_section() const;
576 // Set the symbol's output section. This is used for symbols
577 // defined in scripts. This should only be called after the symbol
578 // table has been finalized.
580 set_output_section(Output_section
*);
582 // Return whether there should be a warning for references to this
586 { return this->has_warning_
; }
588 // Mark this symbol as having a warning.
591 { this->has_warning_
= true; }
593 // Return whether this symbol is defined by a COPY reloc from a
596 is_copied_from_dynobj() const
597 { return this->is_copied_from_dynobj_
; }
599 // Mark this symbol as defined by a COPY reloc.
601 set_is_copied_from_dynobj()
602 { this->is_copied_from_dynobj_
= true; }
604 // Return whether this symbol is forced to visibility STB_LOCAL
605 // by a "local:" entry in a version script.
607 is_forced_local() const
608 { return this->is_forced_local_
; }
610 // Mark this symbol as forced to STB_LOCAL visibility.
612 set_is_forced_local()
613 { this->is_forced_local_
= true; }
616 // Instances of this class should always be created at a specific
619 { memset(this, 0, sizeof *this); }
621 // Initialize the general fields.
623 init_fields(const char* name
, const char* version
,
624 elfcpp::STT type
, elfcpp::STB binding
,
625 elfcpp::STV visibility
, unsigned char nonvis
);
627 // Initialize fields from an ELF symbol in OBJECT.
628 template<int size
, bool big_endian
>
630 init_base(const char *name
, const char* version
, Object
* object
,
631 const elfcpp::Sym
<size
, big_endian
>&);
633 // Initialize fields for an Output_data.
635 init_base(const char* name
, Output_data
*, elfcpp::STT
, elfcpp::STB
,
636 elfcpp::STV
, unsigned char nonvis
, bool offset_is_from_end
);
638 // Initialize fields for an Output_segment.
640 init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
641 elfcpp::STB binding
, elfcpp::STV visibility
,
642 unsigned char nonvis
, Segment_offset_base offset_base
);
644 // Initialize fields for a constant.
646 init_base(const char* name
, elfcpp::STT type
, elfcpp::STB binding
,
647 elfcpp::STV visibility
, unsigned char nonvis
);
649 // Override existing symbol.
650 template<int size
, bool big_endian
>
652 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
653 const char* version
);
655 // Override existing symbol with a special symbol.
657 override_base_with_special(const Symbol
* from
);
659 // Allocate a common symbol by giving it a location in the output
662 allocate_base_common(Output_data
*);
665 Symbol(const Symbol
&);
666 Symbol
& operator=(const Symbol
&);
668 // Symbol name (expected to point into a Stringpool).
670 // Symbol version (expected to point into a Stringpool). This may
672 const char* version_
;
676 // This struct is used if SOURCE_ == FROM_OBJECT.
679 // Object in which symbol is defined, or in which it was first
682 // Section number in object_ in which symbol is defined.
686 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
689 // Output_data in which symbol is defined. Before
690 // Layout::finalize the symbol's value is an offset within the
692 Output_data
* output_data
;
693 // True if the offset is from the end, false if the offset is
694 // from the beginning.
695 bool offset_is_from_end
;
698 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
701 // Output_segment in which the symbol is defined. Before
702 // Layout::finalize the symbol's value is an offset.
703 Output_segment
* output_segment
;
704 // The base to use for the offset before Layout::finalize.
705 Segment_offset_base offset_base
;
709 // The index of this symbol in the output file. If the symbol is
710 // not going into the output file, this value is -1U. This field
711 // starts as always holding zero. It is set to a non-zero value by
712 // Symbol_table::finalize.
713 unsigned int symtab_index_
;
715 // The index of this symbol in the dynamic symbol table. If the
716 // symbol is not going into the dynamic symbol table, this value is
717 // -1U. This field starts as always holding zero. It is set to a
718 // non-zero value during Layout::finalize.
719 unsigned int dynsym_index_
;
721 // If this symbol has an entry in the GOT section (has_got_offset_
722 // is true), this is the offset from the start of the GOT section.
723 // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
724 // serves as the GOT offset for the GOT entry that holds its
725 // TP-relative offset.
726 unsigned int got_offset_
;
728 // If this is a TLS symbol and has an entry in the GOT section
729 // for a module index or a pair of entries (module index,
730 // dtv-relative offset), these are the offsets from the start
731 // of the GOT section.
732 unsigned int tls_mod_got_offset_
;
733 unsigned int tls_pair_got_offset_
;
735 // If this symbol has an entry in the PLT section (has_plt_offset_
736 // is true), then this is the offset from the start of the PLT
738 unsigned int plt_offset_
;
741 elfcpp::STT type_
: 4;
743 elfcpp::STB binding_
: 4;
744 // Symbol visibility.
745 elfcpp::STV visibility_
: 2;
746 // Rest of symbol st_other field.
747 unsigned int nonvis_
: 6;
748 // The type of symbol.
750 // True if this symbol always requires special target-specific
752 bool is_target_special_
: 1;
753 // True if this is the default version of the symbol.
755 // True if this symbol really forwards to another symbol. This is
756 // used when we discover after the fact that two different entries
757 // in the hash table really refer to the same symbol. This will
758 // never be set for a symbol found in the hash table, but may be set
759 // for a symbol found in the list of symbols attached to an Object.
760 // It forwards to the symbol found in the forwarders_ map of
762 bool is_forwarder_
: 1;
763 // True if the symbol has an alias in the weak_aliases table in
766 // True if this symbol needs to be in the dynamic symbol table.
767 bool needs_dynsym_entry_
: 1;
768 // True if we've seen this symbol in a regular object.
770 // True if we've seen this symbol in a dynamic object.
772 // True if the symbol has an entry in the GOT section.
773 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
774 bool has_got_offset_
: 1;
775 // True if the symbol has an entry in the GOT section for its
777 bool has_tls_mod_got_offset_
: 1;
778 // True if the symbol has a pair of entries in the GOT section for its
779 // module index and dtv-relative offset.
780 bool has_tls_pair_got_offset_
: 1;
781 // True if the symbol has an entry in the PLT section.
782 bool has_plt_offset_
: 1;
783 // True if this is a dynamic symbol which needs a special value in
784 // the dynamic symbol table.
785 bool needs_dynsym_value_
: 1;
786 // True if there is a warning for this symbol.
787 bool has_warning_
: 1;
788 // True if we are using a COPY reloc for this symbol, so that the
789 // real definition lives in a dynamic object.
790 bool is_copied_from_dynobj_
: 1;
791 // True if this symbol was forced to local visibility by a version
793 bool is_forced_local_
: 1;
796 // The parts of a symbol which are size specific. Using a template
797 // derived class like this helps us use less space on a 32-bit system.
800 class Sized_symbol
: public Symbol
803 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
804 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
809 // Initialize fields from an ELF symbol in OBJECT.
810 template<bool big_endian
>
812 init(const char *name
, const char* version
, Object
* object
,
813 const elfcpp::Sym
<size
, big_endian
>&);
815 // Initialize fields for an Output_data.
817 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
818 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
819 bool offset_is_from_end
);
821 // Initialize fields for an Output_segment.
823 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
824 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
825 Segment_offset_base offset_base
);
827 // Initialize fields for a constant.
829 init(const char* name
, Value_type value
, Size_type symsize
,
830 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
832 // Override existing symbol.
833 template<bool big_endian
>
835 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
836 const char* version
);
838 // Override existing symbol with a special symbol.
840 override_with_special(const Sized_symbol
<size
>*);
842 // Return the symbol's value.
845 { return this->value_
; }
847 // Return the symbol's size (we can't call this 'size' because that
848 // is a template parameter).
851 { return this->symsize_
; }
853 // Set the symbol size. This is used when resolving common symbols.
855 set_symsize(Size_type symsize
)
856 { this->symsize_
= symsize
; }
858 // Set the symbol value. This is called when we store the final
859 // values of the symbols into the symbol table.
861 set_value(Value_type value
)
862 { this->value_
= value
; }
864 // Allocate a common symbol by giving it a location in the output
867 allocate_common(Output_data
*, Value_type value
);
870 Sized_symbol(const Sized_symbol
&);
871 Sized_symbol
& operator=(const Sized_symbol
&);
873 // Symbol value. Before Layout::finalize this is the offset in the
874 // input section. This is set to the final value during
881 // A struct describing a symbol defined by the linker, where the value
882 // of the symbol is defined based on an output section. This is used
883 // for symbols defined by the linker, like "_init_array_start".
885 struct Define_symbol_in_section
889 // The name of the output section with which this symbol should be
890 // associated. If there is no output section with that name, the
891 // symbol will be defined as zero.
892 const char* output_section
;
893 // The offset of the symbol within the output section. This is an
894 // offset from the start of the output section, unless start_at_end
895 // is true, in which case this is an offset from the end of the
898 // The size of the symbol.
902 // The symbol binding.
904 // The symbol visibility.
905 elfcpp::STV visibility
;
906 // The rest of the st_other field.
907 unsigned char nonvis
;
908 // If true, the value field is an offset from the end of the output
910 bool offset_is_from_end
;
911 // If true, this symbol is defined only if we see a reference to it.
915 // A struct describing a symbol defined by the linker, where the value
916 // of the symbol is defined based on a segment. This is used for
917 // symbols defined by the linker, like "_end". We describe the
918 // segment with which the symbol should be associated by its
919 // characteristics. If no segment meets these characteristics, the
920 // symbol will be defined as zero. If there is more than one segment
921 // which meets these characteristics, we will use the first one.
923 struct Define_symbol_in_segment
927 // The segment type where the symbol should be defined, typically
929 elfcpp::PT segment_type
;
930 // Bitmask of segment flags which must be set.
931 elfcpp::PF segment_flags_set
;
932 // Bitmask of segment flags which must be clear.
933 elfcpp::PF segment_flags_clear
;
934 // The offset of the symbol within the segment. The offset is
935 // calculated from the position set by offset_base.
937 // The size of the symbol.
941 // The symbol binding.
943 // The symbol visibility.
944 elfcpp::STV visibility
;
945 // The rest of the st_other field.
946 unsigned char nonvis
;
947 // The base from which we compute the offset.
948 Symbol::Segment_offset_base offset_base
;
949 // If true, this symbol is defined only if we see a reference to it.
953 // This class manages warnings. Warnings are a GNU extension. When
954 // we see a section named .gnu.warning.SYM in an object file, and if
955 // we wind using the definition of SYM from that object file, then we
956 // will issue a warning for any relocation against SYM from a
957 // different object file. The text of the warning is the contents of
958 // the section. This is not precisely the definition used by the old
959 // GNU linker; the old GNU linker treated an occurrence of
960 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
961 // would trigger a warning on any reference. However, it was
962 // inconsistent in that a warning in a dynamic object only triggered
963 // if there was no definition in a regular object. This linker is
964 // different in that we only issue a warning if we use the symbol
965 // definition from the same object file as the warning section.
974 // Add a warning for symbol NAME in object OBJ. WARNING is the text
977 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
978 const std::string
& warning
);
980 // For each symbol for which we should give a warning, make a note
983 note_warnings(Symbol_table
* symtab
);
985 // Issue a warning for a reference to SYM at RELINFO's location.
986 template<int size
, bool big_endian
>
988 issue_warning(const Symbol
* sym
, const Relocate_info
<size
, big_endian
>*,
989 size_t relnum
, off_t reloffset
) const;
992 Warnings(const Warnings
&);
993 Warnings
& operator=(const Warnings
&);
995 // What we need to know to get the warning text.
996 struct Warning_location
998 // The object the warning is in.
1000 // The warning text.
1004 : object(NULL
), text()
1008 set(Object
* o
, const std::string
& t
)
1015 // A mapping from warning symbol names (canonicalized in
1016 // Symbol_table's namepool_ field) to warning information.
1017 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
1019 Warning_table warnings_
;
1022 // The main linker symbol table.
1027 // COUNT is an estimate of how many symbosl will be inserted in the
1028 // symbol table. It's ok to put 0 if you don't know; a correct
1029 // guess will just save some CPU by reducing hashtable resizes.
1030 Symbol_table(unsigned int count
, const Version_script_info
& version_script
);
1034 // Add COUNT external symbols from the relocatable object RELOBJ to
1035 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
1036 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
1037 // point to the symbols in the symbol table.
1038 template<int size
, bool big_endian
>
1040 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
1041 const unsigned char* syms
, size_t count
,
1042 const char* sym_names
, size_t sym_name_size
,
1043 typename Sized_relobj
<size
, big_endian
>::Symbols
*);
1045 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1046 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1047 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1048 // symbol version data.
1049 template<int size
, bool big_endian
>
1051 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
1052 const unsigned char* syms
, size_t count
,
1053 const char* sym_names
, size_t sym_name_size
,
1054 const unsigned char* versym
, size_t versym_size
,
1055 const std::vector
<const char*>*);
1057 // Define a special symbol based on an Output_data. It is a
1058 // multiple definition error if this symbol is already defined.
1060 define_in_output_data(const char* name
, const char* version
,
1061 Output_data
*, uint64_t value
, uint64_t symsize
,
1062 elfcpp::STT type
, elfcpp::STB binding
,
1063 elfcpp::STV visibility
, unsigned char nonvis
,
1064 bool offset_is_from_end
, bool only_if_ref
);
1066 // Define a special symbol based on an Output_segment. It is a
1067 // multiple definition error if this symbol is already defined.
1069 define_in_output_segment(const char* name
, const char* version
,
1070 Output_segment
*, uint64_t value
, uint64_t symsize
,
1071 elfcpp::STT type
, elfcpp::STB binding
,
1072 elfcpp::STV visibility
, unsigned char nonvis
,
1073 Symbol::Segment_offset_base
, bool only_if_ref
);
1075 // Define a special symbol with a constant value. It is a multiple
1076 // definition error if this symbol is already defined.
1078 define_as_constant(const char* name
, const char* version
,
1079 uint64_t value
, uint64_t symsize
, elfcpp::STT type
,
1080 elfcpp::STB binding
, elfcpp::STV visibility
,
1081 unsigned char nonvis
, bool only_if_ref
,
1082 bool force_override
);
1084 // Define a set of symbols in output sections. If ONLY_IF_REF is
1085 // true, only define them if they are referenced.
1087 define_symbols(const Layout
*, int count
, const Define_symbol_in_section
*,
1090 // Define a set of symbols in output segments. If ONLY_IF_REF is
1091 // true, only defined them if they are referenced.
1093 define_symbols(const Layout
*, int count
, const Define_symbol_in_segment
*,
1096 // Define SYM using a COPY reloc. POSD is the Output_data where the
1097 // symbol should be defined--typically a .dyn.bss section. VALUE is
1098 // the offset within POSD.
1101 define_with_copy_reloc(Sized_symbol
<size
>* sym
, Output_data
* posd
,
1102 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
1104 // Look up a symbol.
1106 lookup(const char*, const char* version
= NULL
) const;
1108 // Return the real symbol associated with the forwarder symbol FROM.
1110 resolve_forwards(const Symbol
* from
) const;
1112 // Return the sized version of a symbol in this table.
1115 get_sized_symbol(Symbol
*) const;
1118 const Sized_symbol
<size
>*
1119 get_sized_symbol(const Symbol
*) const;
1121 // Return the count of undefined symbols seen.
1123 saw_undefined() const
1124 { return this->saw_undefined_
; }
1126 // Allocate the common symbols
1128 allocate_commons(const General_options
&, Layout
*);
1130 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1133 add_warning(const char* name
, Object
* obj
, const std::string
& warning
)
1134 { this->warnings_
.add_warning(this, name
, obj
, warning
); }
1136 // Canonicalize a symbol name for use in the hash table.
1138 canonicalize_name(const char* name
)
1139 { return this->namepool_
.add(name
, true, NULL
); }
1141 // Possibly issue a warning for a reference to SYM at LOCATION which
1143 template<int size
, bool big_endian
>
1145 issue_warning(const Symbol
* sym
,
1146 const Relocate_info
<size
, big_endian
>* relinfo
,
1147 size_t relnum
, off_t reloffset
) const
1148 { this->warnings_
.issue_warning(sym
, relinfo
, relnum
, reloffset
); }
1150 // Check candidate_odr_violations_ to find symbols with the same name
1151 // but apparently different definitions (different source-file/line-no).
1153 detect_odr_violations(const Task
*, const char* output_file_name
) const;
1155 // SYM is defined using a COPY reloc. Return the dynamic object
1156 // where the original definition was found.
1158 get_copy_source(const Symbol
* sym
) const;
1160 // Set the dynamic symbol indexes. INDEX is the index of the first
1161 // global dynamic symbol. Pointers to the symbols are stored into
1162 // the vector. The names are stored into the Stringpool. This
1163 // returns an updated dynamic symbol index.
1165 set_dynsym_indexes(unsigned int index
, std::vector
<Symbol
*>*,
1166 Stringpool
*, Versions
*);
1168 // Finalize the symbol table after we have set the final addresses
1169 // of all the input sections. This sets the final symbol indexes,
1170 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1171 // index of the first global symbol. OFF is the file offset of the
1172 // global symbol table, DYNOFF is the offset of the globals in the
1173 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1174 // global dynamic symbol, and DYNCOUNT is the number of global
1175 // dynamic symbols. This records the parameters, and returns the
1176 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1179 finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
, size_t dyncount
,
1180 Stringpool
* pool
, unsigned int *plocal_symcount
);
1182 // Write out the global symbols.
1184 write_globals(const Input_objects
*, const Stringpool
*, const Stringpool
*,
1185 Output_file
*) const;
1187 // Write out a section symbol. Return the updated offset.
1189 write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1191 // Dump statistical information to stderr.
1193 print_stats() const;
1195 // Return the version script information.
1196 const Version_script_info
&
1197 version_script() const
1198 { return version_script_
; }
1201 Symbol_table(const Symbol_table
&);
1202 Symbol_table
& operator=(const Symbol_table
&);
1204 // Make FROM a forwarder symbol to TO.
1206 make_forwarder(Symbol
* from
, Symbol
* to
);
1209 template<int size
, bool big_endian
>
1211 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
1212 const char *version
, Stringpool::Key version_key
,
1213 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
,
1214 const elfcpp::Sym
<size
, big_endian
>& orig_sym
);
1217 template<int size
, bool big_endian
>
1219 resolve(Sized_symbol
<size
>* to
,
1220 const elfcpp::Sym
<size
, big_endian
>& sym
,
1221 const elfcpp::Sym
<size
, big_endian
>& orig_sym
,
1222 Object
*, const char* version
);
1224 template<int size
, bool big_endian
>
1226 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
1227 const char* version
);
1229 // Record that a symbol is forced to be local by a version script.
1231 force_local(Symbol
*);
1233 // Whether we should override a symbol, based on flags in
1236 should_override(const Symbol
*, unsigned int, Object
*, bool*);
1238 // Override a symbol.
1239 template<int size
, bool big_endian
>
1241 override(Sized_symbol
<size
>* tosym
,
1242 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
1243 Object
* object
, const char* version
);
1245 // Whether we should override a symbol with a special symbol which
1246 // is automatically defined by the linker.
1248 should_override_with_special(const Symbol
*);
1250 // Override a symbol with a special symbol.
1253 override_with_special(Sized_symbol
<size
>* tosym
,
1254 const Sized_symbol
<size
>* fromsym
);
1256 // Record all weak alias sets for a dynamic object.
1259 record_weak_aliases(std::vector
<Sized_symbol
<size
>*>*);
1261 // Define a special symbol.
1262 template<int size
, bool big_endian
>
1264 define_special_symbol(const char** pname
, const char** pversion
,
1265 bool only_if_ref
, Sized_symbol
<size
>** poldsym
);
1267 // Define a symbol in an Output_data, sized version.
1270 do_define_in_output_data(const char* name
, const char* version
, Output_data
*,
1271 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1272 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1273 elfcpp::STT type
, elfcpp::STB binding
,
1274 elfcpp::STV visibility
, unsigned char nonvis
,
1275 bool offset_is_from_end
, bool only_if_ref
);
1277 // Define a symbol in an Output_segment, sized version.
1280 do_define_in_output_segment(
1281 const char* name
, const char* version
, Output_segment
* os
,
1282 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1283 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1284 elfcpp::STT type
, elfcpp::STB binding
,
1285 elfcpp::STV visibility
, unsigned char nonvis
,
1286 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
1288 // Define a symbol as a constant, sized version.
1291 do_define_as_constant(
1292 const char* name
, const char* version
,
1293 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1294 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1295 elfcpp::STT type
, elfcpp::STB binding
,
1296 elfcpp::STV visibility
, unsigned char nonvis
,
1297 bool only_if_ref
, bool force_override
);
1299 // Allocate the common symbols, sized version.
1302 do_allocate_commons(const General_options
&, Layout
*);
1304 // Implement detect_odr_violations.
1305 template<int size
, bool big_endian
>
1307 sized_detect_odr_violations() const;
1309 // Finalize symbols specialized for size.
1312 sized_finalize(off_t
, Stringpool
*, unsigned int*);
1314 // Finalize a symbol. Return whether it should be added to the
1318 sized_finalize_symbol(Symbol
*);
1320 // Add a symbol the final symtab by setting its index.
1323 add_to_final_symtab(Symbol
*, Stringpool
*, unsigned int* pindex
, off_t
* poff
);
1325 // Write globals specialized for size and endianness.
1326 template<int size
, bool big_endian
>
1328 sized_write_globals(const Input_objects
*, const Stringpool
*,
1329 const Stringpool
*, Output_file
*) const;
1331 // Write out a symbol to P.
1332 template<int size
, bool big_endian
>
1334 sized_write_symbol(Sized_symbol
<size
>*,
1335 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1337 const Stringpool
*, unsigned char* p
) const;
1339 // Possibly warn about an undefined symbol from a dynamic object.
1341 warn_about_undefined_dynobj_symbol(const Input_objects
*, Symbol
*) const;
1343 // Write out a section symbol, specialized for size and endianness.
1344 template<int size
, bool big_endian
>
1346 sized_write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1348 // The type of the symbol hash table.
1350 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
1352 struct Symbol_table_hash
1355 operator()(const Symbol_table_key
&) const;
1358 struct Symbol_table_eq
1361 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
1364 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
1365 Symbol_table_eq
> Symbol_table_type
;
1367 // The type of the list of common symbols.
1368 typedef std::vector
<Symbol
*> Commons_type
;
1370 // The type of the list of symbols which have been forced local.
1371 typedef std::vector
<Symbol
*> Forced_locals
;
1373 // A map from symbols with COPY relocs to the dynamic objects where
1374 // they are defined.
1375 typedef Unordered_map
<const Symbol
*, Dynobj
*> Copied_symbol_dynobjs
;
1377 // A map from symbol name (as a pointer into the namepool) to all
1378 // the locations the symbols is (weakly) defined (and certain other
1379 // conditions are met). This map will be used later to detect
1380 // possible One Definition Rule (ODR) violations.
1381 struct Symbol_location
1383 Object
* object
; // Object where the symbol is defined.
1384 unsigned int shndx
; // Section-in-object where the symbol is defined.
1385 off_t offset
; // Offset-in-section where the symbol is defined.
1386 bool operator==(const Symbol_location
& that
) const
1388 return (this->object
== that
.object
1389 && this->shndx
== that
.shndx
1390 && this->offset
== that
.offset
);
1394 struct Symbol_location_hash
1396 size_t operator()(const Symbol_location
& loc
) const
1397 { return reinterpret_cast<uintptr_t>(loc
.object
) ^ loc
.offset
^ loc
.shndx
; }
1400 typedef Unordered_map
<const char*,
1401 Unordered_set
<Symbol_location
, Symbol_location_hash
> >
1404 // We increment this every time we see a new undefined symbol, for
1405 // use in archive groups.
1407 // The index of the first global symbol in the output file.
1408 unsigned int first_global_index_
;
1409 // The file offset within the output symtab section where we should
1412 // The number of global symbols we want to write out.
1413 unsigned int output_count_
;
1414 // The file offset of the global dynamic symbols, or 0 if none.
1415 off_t dynamic_offset_
;
1416 // The index of the first global dynamic symbol.
1417 unsigned int first_dynamic_global_index_
;
1418 // The number of global dynamic symbols, or 0 if none.
1419 unsigned int dynamic_count_
;
1420 // The symbol hash table.
1421 Symbol_table_type table_
;
1422 // A pool of symbol names. This is used for all global symbols.
1423 // Entries in the hash table point into this pool.
1424 Stringpool namepool_
;
1425 // Forwarding symbols.
1426 Unordered_map
<const Symbol
*, Symbol
*> forwarders_
;
1427 // Weak aliases. A symbol in this list points to the next alias.
1428 // The aliases point to each other in a circular list.
1429 Unordered_map
<Symbol
*, Symbol
*> weak_aliases_
;
1430 // We don't expect there to be very many common symbols, so we keep
1431 // a list of them. When we find a common symbol we add it to this
1432 // list. It is possible that by the time we process the list the
1433 // symbol is no longer a common symbol. It may also have become a
1435 Commons_type commons_
;
1436 // A list of symbols which have been forced to be local. We don't
1437 // expect there to be very many of them, so we keep a list of them
1438 // rather than walking the whole table to find them.
1439 Forced_locals forced_locals_
;
1440 // Manage symbol warnings.
1442 // Manage potential One Definition Rule (ODR) violations.
1443 Odr_map candidate_odr_violations_
;
1445 // When we emit a COPY reloc for a symbol, we define it in an
1446 // Output_data. When it's time to emit version information for it,
1447 // we need to know the dynamic object in which we found the original
1448 // definition. This maps symbols with COPY relocs to the dynamic
1449 // object where they were defined.
1450 Copied_symbol_dynobjs copied_symbol_dynobjs_
;
1451 // Information parsed from the version script, if any.
1452 const Version_script_info
& version_script_
;
1455 // We inline get_sized_symbol for efficiency.
1459 Symbol_table::get_sized_symbol(Symbol
* sym
) const
1461 gold_assert(size
== parameters
->target().get_size());
1462 return static_cast<Sized_symbol
<size
>*>(sym
);
1466 const Sized_symbol
<size
>*
1467 Symbol_table::get_sized_symbol(const Symbol
* sym
) const
1469 gold_assert(size
== parameters
->target().get_size());
1470 return static_cast<const Sized_symbol
<size
>*>(sym
);
1473 } // End namespace gold.
1475 #endif // !defined(GOLD_SYMTAB_H)