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
;
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 whether this version is the default for this symbol name.
126 set_is_default(bool def
)
127 { this->is_def_
= def
; }
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
->output_is_shared()
474 && !parameters
->symbolic());
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
->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
->output_is_shared()))
529 // A reference to any PLT entry in a non-position-independent executable
530 // does not need a dynamic relocation.
531 if (!parameters
->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 whether this symbol currently has an absolute value.
573 value_is_absolute() const;
575 // Return whether there should be a warning for references to this
579 { return this->has_warning_
; }
581 // Mark this symbol as having a warning.
584 { this->has_warning_
= true; }
586 // Return whether this symbol is defined by a COPY reloc from a
589 is_copied_from_dynobj() const
590 { return this->is_copied_from_dynobj_
; }
592 // Mark this symbol as defined by a COPY reloc.
594 set_is_copied_from_dynobj()
595 { this->is_copied_from_dynobj_
= true; }
597 // Return whether this symbol is forced to visibility STB_LOCAL
598 // by a "local:" entry in a version script.
600 is_forced_local() const
601 { return this->is_forced_local_
; }
603 // Mark this symbol as forced to STB_LOCAL visibility.
605 set_is_forced_local()
606 { this->is_forced_local_
= true; }
609 // Instances of this class should always be created at a specific
612 { memset(this, 0, sizeof *this); }
614 // Initialize the general fields.
616 init_fields(const char* name
, const char* version
,
617 elfcpp::STT type
, elfcpp::STB binding
,
618 elfcpp::STV visibility
, unsigned char nonvis
);
620 // Initialize fields from an ELF symbol in OBJECT.
621 template<int size
, bool big_endian
>
623 init_base(const char *name
, const char* version
, Object
* object
,
624 const elfcpp::Sym
<size
, big_endian
>&);
626 // Initialize fields for an Output_data.
628 init_base(const char* name
, Output_data
*, elfcpp::STT
, elfcpp::STB
,
629 elfcpp::STV
, unsigned char nonvis
, bool offset_is_from_end
);
631 // Initialize fields for an Output_segment.
633 init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
634 elfcpp::STB binding
, elfcpp::STV visibility
,
635 unsigned char nonvis
, Segment_offset_base offset_base
);
637 // Initialize fields for a constant.
639 init_base(const char* name
, elfcpp::STT type
, elfcpp::STB binding
,
640 elfcpp::STV visibility
, unsigned char nonvis
);
642 // Override existing symbol.
643 template<int size
, bool big_endian
>
645 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
646 const char* version
);
648 // Override existing symbol with a special symbol.
650 override_base_with_special(const Symbol
* from
);
652 // Allocate a common symbol by giving it a location in the output
655 allocate_base_common(Output_data
*);
658 Symbol(const Symbol
&);
659 Symbol
& operator=(const Symbol
&);
661 // Symbol name (expected to point into a Stringpool).
663 // Symbol version (expected to point into a Stringpool). This may
665 const char* version_
;
669 // This struct is used if SOURCE_ == FROM_OBJECT.
672 // Object in which symbol is defined, or in which it was first
675 // Section number in object_ in which symbol is defined.
679 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
682 // Output_data in which symbol is defined. Before
683 // Layout::finalize the symbol's value is an offset within the
685 Output_data
* output_data
;
686 // True if the offset is from the end, false if the offset is
687 // from the beginning.
688 bool offset_is_from_end
;
691 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
694 // Output_segment in which the symbol is defined. Before
695 // Layout::finalize the symbol's value is an offset.
696 Output_segment
* output_segment
;
697 // The base to use for the offset before Layout::finalize.
698 Segment_offset_base offset_base
;
702 // The index of this symbol in the output file. If the symbol is
703 // not going into the output file, this value is -1U. This field
704 // starts as always holding zero. It is set to a non-zero value by
705 // Symbol_table::finalize.
706 unsigned int symtab_index_
;
708 // The index of this symbol in the dynamic symbol table. If the
709 // symbol is not going into the dynamic symbol table, this value is
710 // -1U. This field starts as always holding zero. It is set to a
711 // non-zero value during Layout::finalize.
712 unsigned int dynsym_index_
;
714 // If this symbol has an entry in the GOT section (has_got_offset_
715 // is true), this is the offset from the start of the GOT section.
716 // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
717 // serves as the GOT offset for the GOT entry that holds its
718 // TP-relative offset.
719 unsigned int got_offset_
;
721 // If this is a TLS symbol and has an entry in the GOT section
722 // for a module index or a pair of entries (module index,
723 // dtv-relative offset), these are the offsets from the start
724 // of the GOT section.
725 unsigned int tls_mod_got_offset_
;
726 unsigned int tls_pair_got_offset_
;
728 // If this symbol has an entry in the PLT section (has_plt_offset_
729 // is true), then this is the offset from the start of the PLT
731 unsigned int plt_offset_
;
734 elfcpp::STT type_
: 4;
736 elfcpp::STB binding_
: 4;
737 // Symbol visibility.
738 elfcpp::STV visibility_
: 2;
739 // Rest of symbol st_other field.
740 unsigned int nonvis_
: 6;
741 // The type of symbol.
743 // True if this symbol always requires special target-specific
745 bool is_target_special_
: 1;
746 // True if this is the default version of the symbol.
748 // True if this symbol really forwards to another symbol. This is
749 // used when we discover after the fact that two different entries
750 // in the hash table really refer to the same symbol. This will
751 // never be set for a symbol found in the hash table, but may be set
752 // for a symbol found in the list of symbols attached to an Object.
753 // It forwards to the symbol found in the forwarders_ map of
755 bool is_forwarder_
: 1;
756 // True if the symbol has an alias in the weak_aliases table in
759 // True if this symbol needs to be in the dynamic symbol table.
760 bool needs_dynsym_entry_
: 1;
761 // True if we've seen this symbol in a regular object.
763 // True if we've seen this symbol in a dynamic object.
765 // True if the symbol has an entry in the GOT section.
766 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
767 bool has_got_offset_
: 1;
768 // True if the symbol has an entry in the GOT section for its
770 bool has_tls_mod_got_offset_
: 1;
771 // True if the symbol has a pair of entries in the GOT section for its
772 // module index and dtv-relative offset.
773 bool has_tls_pair_got_offset_
: 1;
774 // True if the symbol has an entry in the PLT section.
775 bool has_plt_offset_
: 1;
776 // True if this is a dynamic symbol which needs a special value in
777 // the dynamic symbol table.
778 bool needs_dynsym_value_
: 1;
779 // True if there is a warning for this symbol.
780 bool has_warning_
: 1;
781 // True if we are using a COPY reloc for this symbol, so that the
782 // real definition lives in a dynamic object.
783 bool is_copied_from_dynobj_
: 1;
784 // True if this symbol was forced to local visibility by a version
786 bool is_forced_local_
: 1;
789 // The parts of a symbol which are size specific. Using a template
790 // derived class like this helps us use less space on a 32-bit system.
793 class Sized_symbol
: public Symbol
796 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
797 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
802 // Initialize fields from an ELF symbol in OBJECT.
803 template<bool big_endian
>
805 init(const char *name
, const char* version
, Object
* object
,
806 const elfcpp::Sym
<size
, big_endian
>&);
808 // Initialize fields for an Output_data.
810 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
811 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
812 bool offset_is_from_end
);
814 // Initialize fields for an Output_segment.
816 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
817 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
818 Segment_offset_base offset_base
);
820 // Initialize fields for a constant.
822 init(const char* name
, Value_type value
, Size_type symsize
,
823 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
825 // Override existing symbol.
826 template<bool big_endian
>
828 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
829 const char* version
);
831 // Override existing symbol with a special symbol.
833 override_with_special(const Sized_symbol
<size
>*);
835 // Return the symbol's value.
838 { return this->value_
; }
840 // Return the symbol's size (we can't call this 'size' because that
841 // is a template parameter).
844 { return this->symsize_
; }
846 // Set the symbol size. This is used when resolving common symbols.
848 set_symsize(Size_type symsize
)
849 { this->symsize_
= symsize
; }
851 // Set the symbol value. This is called when we store the final
852 // values of the symbols into the symbol table.
854 set_value(Value_type value
)
855 { this->value_
= value
; }
857 // Allocate a common symbol by giving it a location in the output
860 allocate_common(Output_data
*, Value_type value
);
863 Sized_symbol(const Sized_symbol
&);
864 Sized_symbol
& operator=(const Sized_symbol
&);
866 // Symbol value. Before Layout::finalize this is the offset in the
867 // input section. This is set to the final value during
874 // A struct describing a symbol defined by the linker, where the value
875 // of the symbol is defined based on an output section. This is used
876 // for symbols defined by the linker, like "_init_array_start".
878 struct Define_symbol_in_section
882 // The name of the output section with which this symbol should be
883 // associated. If there is no output section with that name, the
884 // symbol will be defined as zero.
885 const char* output_section
;
886 // The offset of the symbol within the output section. This is an
887 // offset from the start of the output section, unless start_at_end
888 // is true, in which case this is an offset from the end of the
891 // The size of the symbol.
895 // The symbol binding.
897 // The symbol visibility.
898 elfcpp::STV visibility
;
899 // The rest of the st_other field.
900 unsigned char nonvis
;
901 // If true, the value field is an offset from the end of the output
903 bool offset_is_from_end
;
904 // If true, this symbol is defined only if we see a reference to it.
908 // A struct describing a symbol defined by the linker, where the value
909 // of the symbol is defined based on a segment. This is used for
910 // symbols defined by the linker, like "_end". We describe the
911 // segment with which the symbol should be associated by its
912 // characteristics. If no segment meets these characteristics, the
913 // symbol will be defined as zero. If there is more than one segment
914 // which meets these characteristics, we will use the first one.
916 struct Define_symbol_in_segment
920 // The segment type where the symbol should be defined, typically
922 elfcpp::PT segment_type
;
923 // Bitmask of segment flags which must be set.
924 elfcpp::PF segment_flags_set
;
925 // Bitmask of segment flags which must be clear.
926 elfcpp::PF segment_flags_clear
;
927 // The offset of the symbol within the segment. The offset is
928 // calculated from the position set by offset_base.
930 // The size of the symbol.
934 // The symbol binding.
936 // The symbol visibility.
937 elfcpp::STV visibility
;
938 // The rest of the st_other field.
939 unsigned char nonvis
;
940 // The base from which we compute the offset.
941 Symbol::Segment_offset_base offset_base
;
942 // If true, this symbol is defined only if we see a reference to it.
946 // This class manages warnings. Warnings are a GNU extension. When
947 // we see a section named .gnu.warning.SYM in an object file, and if
948 // we wind using the definition of SYM from that object file, then we
949 // will issue a warning for any relocation against SYM from a
950 // different object file. The text of the warning is the contents of
951 // the section. This is not precisely the definition used by the old
952 // GNU linker; the old GNU linker treated an occurrence of
953 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
954 // would trigger a warning on any reference. However, it was
955 // inconsistent in that a warning in a dynamic object only triggered
956 // if there was no definition in a regular object. This linker is
957 // different in that we only issue a warning if we use the symbol
958 // definition from the same object file as the warning section.
967 // Add a warning for symbol NAME in object OBJ. WARNING is the text
970 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
971 const std::string
& warning
);
973 // For each symbol for which we should give a warning, make a note
976 note_warnings(Symbol_table
* symtab
);
978 // Issue a warning for a reference to SYM at RELINFO's location.
979 template<int size
, bool big_endian
>
981 issue_warning(const Symbol
* sym
, const Relocate_info
<size
, big_endian
>*,
982 size_t relnum
, off_t reloffset
) const;
985 Warnings(const Warnings
&);
986 Warnings
& operator=(const Warnings
&);
988 // What we need to know to get the warning text.
989 struct Warning_location
991 // The object the warning is in.
997 : object(NULL
), text()
1001 set(Object
* o
, const std::string
& t
)
1008 // A mapping from warning symbol names (canonicalized in
1009 // Symbol_table's namepool_ field) to warning information.
1010 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
1012 Warning_table warnings_
;
1015 // The main linker symbol table.
1020 // COUNT is an estimate of how many symbosl will be inserted in the
1021 // symbol table. It's ok to put 0 if you don't know; a correct
1022 // guess will just save some CPU by reducing hashtable resizes.
1023 Symbol_table(unsigned int count
, const Version_script_info
& version_script
);
1027 // Add COUNT external symbols from the relocatable object RELOBJ to
1028 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
1029 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
1030 // point to the symbols in the symbol table.
1031 template<int size
, bool big_endian
>
1033 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
1034 const unsigned char* syms
, size_t count
,
1035 const char* sym_names
, size_t sym_name_size
,
1036 typename Sized_relobj
<size
, big_endian
>::Symbols
*);
1038 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1039 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1040 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1041 // symbol version data.
1042 template<int size
, bool big_endian
>
1044 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
1045 const unsigned char* syms
, size_t count
,
1046 const char* sym_names
, size_t sym_name_size
,
1047 const unsigned char* versym
, size_t versym_size
,
1048 const std::vector
<const char*>*);
1050 // Define a special symbol based on an Output_data. It is a
1051 // multiple definition error if this symbol is already defined.
1053 define_in_output_data(const char* name
, const char* version
,
1054 Output_data
*, uint64_t value
, uint64_t symsize
,
1055 elfcpp::STT type
, elfcpp::STB binding
,
1056 elfcpp::STV visibility
, unsigned char nonvis
,
1057 bool offset_is_from_end
, bool only_if_ref
);
1059 // Define a special symbol based on an Output_segment. It is a
1060 // multiple definition error if this symbol is already defined.
1062 define_in_output_segment(const char* name
, const char* version
,
1063 Output_segment
*, uint64_t value
, uint64_t symsize
,
1064 elfcpp::STT type
, elfcpp::STB binding
,
1065 elfcpp::STV visibility
, unsigned char nonvis
,
1066 Symbol::Segment_offset_base
, bool only_if_ref
);
1068 // Define a special symbol with a constant value. It is a multiple
1069 // definition error if this symbol is already defined.
1071 define_as_constant(const char* name
, const char* version
,
1072 uint64_t value
, uint64_t symsize
, elfcpp::STT type
,
1073 elfcpp::STB binding
, elfcpp::STV visibility
,
1074 unsigned char nonvis
, bool only_if_ref
);
1076 // Define a set of symbols in output sections. If ONLY_IF_REF is
1077 // true, only define them if they are referenced.
1079 define_symbols(const Layout
*, int count
, const Define_symbol_in_section
*,
1082 // Define a set of symbols in output segments. If ONLY_IF_REF is
1083 // true, only defined them if they are referenced.
1085 define_symbols(const Layout
*, int count
, const Define_symbol_in_segment
*,
1088 // Define SYM using a COPY reloc. POSD is the Output_data where the
1089 // symbol should be defined--typically a .dyn.bss section. VALUE is
1090 // the offset within POSD.
1093 define_with_copy_reloc(Sized_symbol
<size
>* sym
, Output_data
* posd
,
1094 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
1096 // Look up a symbol.
1098 lookup(const char*, const char* version
= NULL
) const;
1100 // Return the real symbol associated with the forwarder symbol FROM.
1102 resolve_forwards(const Symbol
* from
) const;
1104 // Return the sized version of a symbol in this table.
1107 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
1110 const Sized_symbol
<size
>*
1111 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
1113 // Return the count of undefined symbols seen.
1115 saw_undefined() const
1116 { return this->saw_undefined_
; }
1118 // Allocate the common symbols
1120 allocate_commons(const General_options
&, Layout
*);
1122 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1125 add_warning(const char* name
, Object
* obj
, const std::string
& warning
)
1126 { this->warnings_
.add_warning(this, name
, obj
, warning
); }
1128 // Canonicalize a symbol name for use in the hash table.
1130 canonicalize_name(const char* name
)
1131 { return this->namepool_
.add(name
, true, NULL
); }
1133 // Possibly issue a warning for a reference to SYM at LOCATION which
1135 template<int size
, bool big_endian
>
1137 issue_warning(const Symbol
* sym
,
1138 const Relocate_info
<size
, big_endian
>* relinfo
,
1139 size_t relnum
, off_t reloffset
) const
1140 { this->warnings_
.issue_warning(sym
, relinfo
, relnum
, reloffset
); }
1142 // Check candidate_odr_violations_ to find symbols with the same name
1143 // but apparently different definitions (different source-file/line-no).
1145 detect_odr_violations(const Task
*, const char* output_file_name
) const;
1147 // SYM is defined using a COPY reloc. Return the dynamic object
1148 // where the original definition was found.
1150 get_copy_source(const Symbol
* sym
) const;
1152 // Set the dynamic symbol indexes. INDEX is the index of the first
1153 // global dynamic symbol. Pointers to the symbols are stored into
1154 // the vector. The names are stored into the Stringpool. This
1155 // returns an updated dynamic symbol index.
1157 set_dynsym_indexes(unsigned int index
, std::vector
<Symbol
*>*,
1158 Stringpool
*, Versions
*);
1160 // Finalize the symbol table after we have set the final addresses
1161 // of all the input sections. This sets the final symbol indexes,
1162 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1163 // index of the first global symbol. OFF is the file offset of the
1164 // global symbol table, DYNOFF is the offset of the globals in the
1165 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1166 // global dynamic symbol, and DYNCOUNT is the number of global
1167 // dynamic symbols. This records the parameters, and returns the
1168 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1171 finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
, size_t dyncount
,
1172 Stringpool
* pool
, unsigned int *plocal_symcount
);
1174 // Write out the global symbols.
1176 write_globals(const Input_objects
*, const Stringpool
*, const Stringpool
*,
1177 Output_file
*) const;
1179 // Write out a section symbol. Return the updated offset.
1181 write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1183 // Dump statistical information to stderr.
1185 print_stats() const;
1187 // Return the version script information.
1188 const Version_script_info
&
1189 version_script() const
1190 { return version_script_
; }
1193 Symbol_table(const Symbol_table
&);
1194 Symbol_table
& operator=(const Symbol_table
&);
1196 // Make FROM a forwarder symbol to TO.
1198 make_forwarder(Symbol
* from
, Symbol
* to
);
1201 template<int size
, bool big_endian
>
1203 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
1204 const char *version
, Stringpool::Key version_key
,
1205 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
,
1206 const elfcpp::Sym
<size
, big_endian
>& orig_sym
);
1209 template<int size
, bool big_endian
>
1211 resolve(Sized_symbol
<size
>* to
,
1212 const elfcpp::Sym
<size
, big_endian
>& sym
,
1213 const elfcpp::Sym
<size
, big_endian
>& orig_sym
,
1214 Object
*, const char* version
);
1216 template<int size
, bool big_endian
>
1218 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
1219 const char* version ACCEPT_SIZE_ENDIAN
);
1221 // Record that a symbol is forced to be local by a version script.
1223 force_local(Symbol
*);
1225 // Whether we should override a symbol, based on flags in
1228 should_override(const Symbol
*, unsigned int, Object
*, bool*);
1230 // Override a symbol.
1231 template<int size
, bool big_endian
>
1233 override(Sized_symbol
<size
>* tosym
,
1234 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
1235 Object
* object
, const char* version
);
1237 // Whether we should override a symbol with a special symbol which
1238 // is automatically defined by the linker.
1240 should_override_with_special(const Symbol
*);
1242 // Override a symbol with a special symbol.
1245 override_with_special(Sized_symbol
<size
>* tosym
,
1246 const Sized_symbol
<size
>* fromsym
);
1248 // Record all weak alias sets for a dynamic object.
1251 record_weak_aliases(std::vector
<Sized_symbol
<size
>*>*);
1253 // Define a special symbol.
1254 template<int size
, bool big_endian
>
1256 define_special_symbol(const char** pname
, const char** pversion
,
1257 bool only_if_ref
, Sized_symbol
<size
>** poldsym
1258 ACCEPT_SIZE_ENDIAN
);
1260 // Define a symbol in an Output_data, sized version.
1263 do_define_in_output_data(const char* name
, const char* version
, Output_data
*,
1264 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1265 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1266 elfcpp::STT type
, elfcpp::STB binding
,
1267 elfcpp::STV visibility
, unsigned char nonvis
,
1268 bool offset_is_from_end
, bool only_if_ref
);
1270 // Define a symbol in an Output_segment, sized version.
1273 do_define_in_output_segment(
1274 const char* name
, const char* version
, Output_segment
* os
,
1275 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1276 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1277 elfcpp::STT type
, elfcpp::STB binding
,
1278 elfcpp::STV visibility
, unsigned char nonvis
,
1279 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
1281 // Define a symbol as a constant, sized version.
1284 do_define_as_constant(
1285 const char* name
, const char* version
,
1286 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1287 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1288 elfcpp::STT type
, elfcpp::STB binding
,
1289 elfcpp::STV visibility
, unsigned char nonvis
,
1292 // Allocate the common symbols, sized version.
1295 do_allocate_commons(const General_options
&, Layout
*);
1297 // Implement detect_odr_violations.
1298 template<int size
, bool big_endian
>
1300 sized_detect_odr_violations() const;
1302 // Finalize symbols specialized for size.
1305 sized_finalize(off_t
, Stringpool
*, unsigned int*);
1307 // Finalize a symbol. Return whether it should be added to the
1311 sized_finalize_symbol(Symbol
*);
1313 // Add a symbol the final symtab by setting its index.
1316 add_to_final_symtab(Symbol
*, Stringpool
*, unsigned int* pindex
, off_t
* poff
);
1318 // Write globals specialized for size and endianness.
1319 template<int size
, bool big_endian
>
1321 sized_write_globals(const Input_objects
*, const Stringpool
*,
1322 const Stringpool
*, Output_file
*) const;
1324 // Write out a symbol to P.
1325 template<int size
, bool big_endian
>
1327 sized_write_symbol(Sized_symbol
<size
>*,
1328 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1330 const Stringpool
*, unsigned char* p
1331 ACCEPT_SIZE_ENDIAN
) const;
1333 // Possibly warn about an undefined symbol from a dynamic object.
1335 warn_about_undefined_dynobj_symbol(const Input_objects
*, Symbol
*) const;
1337 // Write out a section symbol, specialized for size and endianness.
1338 template<int size
, bool big_endian
>
1340 sized_write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1342 // The type of the symbol hash table.
1344 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
1346 struct Symbol_table_hash
1349 operator()(const Symbol_table_key
&) const;
1352 struct Symbol_table_eq
1355 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
1358 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
1359 Symbol_table_eq
> Symbol_table_type
;
1361 // The type of the list of common symbols.
1362 typedef std::vector
<Symbol
*> Commons_type
;
1364 // The type of the list of symbols which have been forced local.
1365 typedef std::vector
<Symbol
*> Forced_locals
;
1367 // A map from symbols with COPY relocs to the dynamic objects where
1368 // they are defined.
1369 typedef Unordered_map
<const Symbol
*, Dynobj
*> Copied_symbol_dynobjs
;
1371 // A map from symbol name (as a pointer into the namepool) to all
1372 // the locations the symbols is (weakly) defined (and certain other
1373 // conditions are met). This map will be used later to detect
1374 // possible One Definition Rule (ODR) violations.
1375 struct Symbol_location
1377 Object
* object
; // Object where the symbol is defined.
1378 unsigned int shndx
; // Section-in-object where the symbol is defined.
1379 off_t offset
; // Offset-in-section where the symbol is defined.
1380 bool operator==(const Symbol_location
& that
) const
1382 return (this->object
== that
.object
1383 && this->shndx
== that
.shndx
1384 && this->offset
== that
.offset
);
1388 struct Symbol_location_hash
1390 size_t operator()(const Symbol_location
& loc
) const
1391 { return reinterpret_cast<uintptr_t>(loc
.object
) ^ loc
.offset
^ loc
.shndx
; }
1394 typedef Unordered_map
<const char*,
1395 Unordered_set
<Symbol_location
, Symbol_location_hash
> >
1398 // We increment this every time we see a new undefined symbol, for
1399 // use in archive groups.
1401 // The index of the first global symbol in the output file.
1402 unsigned int first_global_index_
;
1403 // The file offset within the output symtab section where we should
1406 // The number of global symbols we want to write out.
1407 unsigned int output_count_
;
1408 // The file offset of the global dynamic symbols, or 0 if none.
1409 off_t dynamic_offset_
;
1410 // The index of the first global dynamic symbol.
1411 unsigned int first_dynamic_global_index_
;
1412 // The number of global dynamic symbols, or 0 if none.
1413 unsigned int dynamic_count_
;
1414 // The symbol hash table.
1415 Symbol_table_type table_
;
1416 // A pool of symbol names. This is used for all global symbols.
1417 // Entries in the hash table point into this pool.
1418 Stringpool namepool_
;
1419 // Forwarding symbols.
1420 Unordered_map
<const Symbol
*, Symbol
*> forwarders_
;
1421 // Weak aliases. A symbol in this list points to the next alias.
1422 // The aliases point to each other in a circular list.
1423 Unordered_map
<Symbol
*, Symbol
*> weak_aliases_
;
1424 // We don't expect there to be very many common symbols, so we keep
1425 // a list of them. When we find a common symbol we add it to this
1426 // list. It is possible that by the time we process the list the
1427 // symbol is no longer a common symbol. It may also have become a
1429 Commons_type commons_
;
1430 // A list of symbols which have been forced to be local. We don't
1431 // expect there to be very many of them, so we keep a list of them
1432 // rather than walking the whole table to find them.
1433 Forced_locals forced_locals_
;
1434 // Manage symbol warnings.
1436 // Manage potential One Definition Rule (ODR) violations.
1437 Odr_map candidate_odr_violations_
;
1439 // When we emit a COPY reloc for a symbol, we define it in an
1440 // Output_data. When it's time to emit version information for it,
1441 // we need to know the dynamic object in which we found the original
1442 // definition. This maps symbols with COPY relocs to the dynamic
1443 // object where they were defined.
1444 Copied_symbol_dynobjs copied_symbol_dynobjs_
;
1445 // Information parsed from the version script, if any.
1446 const Version_script_info
& version_script_
;
1449 // We inline get_sized_symbol for efficiency.
1453 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
1455 gold_assert(size
== parameters
->get_size());
1456 return static_cast<Sized_symbol
<size
>*>(sym
);
1460 const Sized_symbol
<size
>*
1461 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
1463 gold_assert(size
== parameters
->get_size());
1464 return static_cast<const Sized_symbol
<size
>*>(sym
);
1467 } // End namespace gold.
1469 #endif // !defined(GOLD_SYMTAB_H)