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
>
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 symbol version. This will return NULL for an
102 // unversioned symbol.
105 { return this->version_
; }
107 // Return the symbol source.
110 { return this->source_
; }
112 // Return the object with which this symbol is associated.
116 gold_assert(this->source_
== FROM_OBJECT
);
117 return this->u_
.from_object
.object
;
120 // Return the index of the section in the input relocatable or
121 // dynamic object file.
125 gold_assert(this->source_
== FROM_OBJECT
);
126 return this->u_
.from_object
.shndx
;
129 // Return the output data section with which this symbol is
130 // associated, if the symbol was specially defined with respect to
131 // an output data section.
135 gold_assert(this->source_
== IN_OUTPUT_DATA
);
136 return this->u_
.in_output_data
.output_data
;
139 // If this symbol was defined with respect to an output data
140 // section, return whether the value is an offset from end.
142 offset_is_from_end() const
144 gold_assert(this->source_
== IN_OUTPUT_DATA
);
145 return this->u_
.in_output_data
.offset_is_from_end
;
148 // Return the output segment with which this symbol is associated,
149 // if the symbol was specially defined with respect to an output
152 output_segment() const
154 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
155 return this->u_
.in_output_segment
.output_segment
;
158 // If this symbol was defined with respect to an output segment,
159 // return the offset base.
163 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
164 return this->u_
.in_output_segment
.offset_base
;
167 // Return the symbol binding.
170 { return this->binding_
; }
172 // Return the symbol type.
175 { return this->type_
; }
177 // Return the symbol visibility.
180 { return this->visibility_
; }
182 // Return the non-visibility part of the st_other field.
185 { return this->nonvis_
; }
187 // Return whether this symbol is a forwarder. This will never be
188 // true of a symbol found in the hash table, but may be true of
189 // symbol pointers attached to object files.
192 { return this->is_forwarder_
; }
194 // Mark this symbol as a forwarder.
197 { this->is_forwarder_
= true; }
199 // Return whether this symbol has an alias in the weak aliases table
203 { return this->has_alias_
; }
205 // Mark this symbol as having an alias.
208 { this->has_alias_
= true; }
210 // Return whether this symbol needs an entry in the dynamic symbol
213 needs_dynsym_entry() const
215 return (this->needs_dynsym_entry_
216 || (this->in_reg() && this->in_dyn()));
219 // Mark this symbol as needing an entry in the dynamic symbol table.
221 set_needs_dynsym_entry()
222 { this->needs_dynsym_entry_
= true; }
224 // Return whether this symbol should be added to the dynamic symbol
227 should_add_dynsym_entry() const;
229 // Return whether this symbol has been seen in a regular object.
232 { return this->in_reg_
; }
234 // Mark this symbol as having been seen in a regular object.
237 { this->in_reg_
= true; }
239 // Return whether this symbol has been seen in a dynamic object.
242 { return this->in_dyn_
; }
244 // Mark this symbol as having been seen in a dynamic object.
247 { this->in_dyn_
= true; }
249 // Return the index of this symbol in the output file symbol table.
250 // A value of -1U means that this symbol is not going into the
251 // output file. This starts out as zero, and is set to a non-zero
252 // value by Symbol_table::finalize. It is an error to ask for the
253 // symbol table index before it has been set.
257 gold_assert(this->symtab_index_
!= 0);
258 return this->symtab_index_
;
261 // Set the index of the symbol in the output file symbol table.
263 set_symtab_index(unsigned int index
)
265 gold_assert(index
!= 0);
266 this->symtab_index_
= index
;
269 // Return whether this symbol already has an index in the output
270 // file symbol table.
272 has_symtab_index() const
273 { return this->symtab_index_
!= 0; }
275 // Return the index of this symbol in the dynamic symbol table. A
276 // value of -1U means that this symbol is not going into the dynamic
277 // symbol table. This starts out as zero, and is set to a non-zero
278 // during Layout::finalize. It is an error to ask for the dynamic
279 // symbol table index before it has been set.
283 gold_assert(this->dynsym_index_
!= 0);
284 return this->dynsym_index_
;
287 // Set the index of the symbol in the dynamic symbol table.
289 set_dynsym_index(unsigned int index
)
291 gold_assert(index
!= 0);
292 this->dynsym_index_
= index
;
295 // Return whether this symbol already has an index in the dynamic
298 has_dynsym_index() const
299 { return this->dynsym_index_
!= 0; }
301 // Return whether this symbol has an entry in the GOT section.
302 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
304 has_got_offset() const
305 { return this->has_got_offset_
; }
307 // Return the offset into the GOT section of this symbol.
311 gold_assert(this->has_got_offset());
312 return this->got_offset_
;
315 // Set the GOT offset of this symbol.
317 set_got_offset(unsigned int got_offset
)
319 this->has_got_offset_
= true;
320 this->got_offset_
= got_offset
;
323 // Return whether this TLS symbol has an entry in the GOT section for
324 // its module index or, if NEED_PAIR is true, has a pair of entries
325 // for its module index and dtv-relative offset.
327 has_tls_got_offset(bool need_pair
) const
329 return (this->has_tls_mod_got_offset_
330 && (!need_pair
|| this->has_tls_pair_got_offset_
));
333 // Return the offset into the GOT section for this symbol's TLS module
334 // index or, if NEED_PAIR is true, for the pair of entries for the
335 // module index and dtv-relative offset.
337 tls_got_offset(bool need_pair
) const
339 gold_assert(this->has_tls_got_offset(need_pair
));
340 return this->tls_mod_got_offset_
;
343 // Set the GOT offset of this symbol.
345 set_tls_got_offset(unsigned int got_offset
, bool have_pair
)
347 this->has_tls_mod_got_offset_
= true;
348 this->has_tls_pair_got_offset_
= have_pair
;
349 this->tls_mod_got_offset_
= got_offset
;
352 // Return whether this symbol has an entry in the PLT section.
354 has_plt_offset() const
355 { return this->has_plt_offset_
; }
357 // Return the offset into the PLT section of this symbol.
361 gold_assert(this->has_plt_offset());
362 return this->plt_offset_
;
365 // Set the PLT offset of this symbol.
367 set_plt_offset(unsigned int plt_offset
)
369 this->has_plt_offset_
= true;
370 this->plt_offset_
= plt_offset
;
373 // Return whether this dynamic symbol needs a special value in the
374 // dynamic symbol table.
376 needs_dynsym_value() const
377 { return this->needs_dynsym_value_
; }
379 // Set that this dynamic symbol needs a special value in the dynamic
382 set_needs_dynsym_value()
384 gold_assert(this->object()->is_dynamic());
385 this->needs_dynsym_value_
= true;
388 // Return true if the final value of this symbol is known at link
391 final_value_is_known() const;
393 // Return whether this is a defined symbol (not undefined or
398 return (this->source_
!= FROM_OBJECT
399 || (this->shndx() != elfcpp::SHN_UNDEF
400 && this->shndx() != elfcpp::SHN_COMMON
));
403 // Return true if this symbol is from a dynamic object.
405 is_from_dynobj() const
407 return this->source_
== FROM_OBJECT
&& this->object()->is_dynamic();
410 // Return whether this is an undefined symbol.
414 return this->source_
== FROM_OBJECT
&& this->shndx() == elfcpp::SHN_UNDEF
;
417 // Return whether this is a common symbol.
421 return (this->source_
== FROM_OBJECT
422 && (this->shndx() == elfcpp::SHN_COMMON
423 || this->type_
== elfcpp::STT_COMMON
));
426 // Return whether this symbol can be seen outside this object.
428 is_externally_visible() const
430 return (this->visibility_
== elfcpp::STV_DEFAULT
431 || this->visibility_
== elfcpp::STV_PROTECTED
);
434 // Return true if this symbol can be preempted by a definition in
435 // another link unit.
437 is_preemptible() const
439 // It doesn't make sense to ask whether a symbol defined in
440 // another object is preemptible.
441 gold_assert(!this->is_from_dynobj());
443 return (this->visibility_
!= elfcpp::STV_INTERNAL
444 && this->visibility_
!= elfcpp::STV_HIDDEN
445 && this->visibility_
!= elfcpp::STV_PROTECTED
446 && parameters
->output_is_shared()
447 && !parameters
->symbolic());
450 // Return true if this symbol is a function that needs a PLT entry.
451 // If the symbol is defined in a dynamic object or if it is subject
452 // to pre-emption, we need to make a PLT entry.
454 needs_plt_entry() const
456 return (this->type() == elfcpp::STT_FUNC
457 && (this->is_from_dynobj() || this->is_preemptible()));
460 // Given a direct absolute or pc-relative static relocation against
461 // the global symbol, this function returns whether a dynamic relocation
465 needs_dynamic_reloc(bool is_absolute_ref
, bool is_function_call
) const
467 // An absolute reference within a position-independent output file
468 // will need a dynamic relocaion.
469 if (is_absolute_ref
&& parameters
->output_is_position_independent())
472 // A function call that can branch to a local PLT entry does not need
473 // a dynamic relocation.
474 if (is_function_call
&& this->has_plt_offset())
477 // A reference to any PLT entry in a non-position-independent executable
478 // does not need a dynamic relocation.
479 if (!parameters
->output_is_position_independent()
480 && this->has_plt_offset())
483 // A reference to a symbol defined in a dynamic object or to a
484 // symbol that is preemptible will need a dynamic relocation.
485 if (this->is_from_dynobj() || this->is_preemptible())
488 // For all other cases, return FALSE.
492 // Given a direct absolute static relocation against
493 // the global symbol, where a dynamic relocation is needed, this
494 // function returns whether a relative dynamic relocation can be used.
495 // The caller must determine separately whether the static relocation
496 // is compatible with a relative relocation.
499 can_use_relative_reloc(bool is_function_call
) const
501 // A function call that can branch to a local PLT entry can
502 // use a RELATIVE relocation.
503 if (is_function_call
&& this->has_plt_offset())
506 // A reference to a symbol defined in a dynamic object or to a
507 // symbol that is preemptible can not use a RELATIVE relocaiton.
508 if (this->is_from_dynobj() || this->is_preemptible())
511 // For all other cases, return TRUE.
515 // Return whether there should be a warning for references to this
519 { return this->has_warning_
; }
521 // Mark this symbol as having a warning.
524 { this->has_warning_
= true; }
526 // Return whether this symbol is defined by a COPY reloc from a
529 is_copied_from_dynobj() const
530 { return this->is_copied_from_dynobj_
; }
532 // Mark this symbol as defined by a COPY reloc.
534 set_is_copied_from_dynobj()
535 { this->is_copied_from_dynobj_
= true; }
537 // Mark this symbol as needing its value written to the GOT even when
538 // the value is subject to dynamic relocation (e.g., when the target
539 // uses a RELATIVE relocation for the GOT entry).
541 set_needs_value_in_got()
542 { this->needs_value_in_got_
= true; }
544 // Return whether this symbol needs its value written to the GOT even
545 // when the value is subject to dynamic relocation.
547 needs_value_in_got() const
548 { return this->needs_value_in_got_
; }
551 // Instances of this class should always be created at a specific
554 { memset(this, 0, sizeof *this); }
556 // Initialize the general fields.
558 init_fields(const char* name
, const char* version
,
559 elfcpp::STT type
, elfcpp::STB binding
,
560 elfcpp::STV visibility
, unsigned char nonvis
);
562 // Initialize fields from an ELF symbol in OBJECT.
563 template<int size
, bool big_endian
>
565 init_base(const char *name
, const char* version
, Object
* object
,
566 const elfcpp::Sym
<size
, big_endian
>&);
568 // Initialize fields for an Output_data.
570 init_base(const char* name
, Output_data
*, elfcpp::STT
, elfcpp::STB
,
571 elfcpp::STV
, unsigned char nonvis
, bool offset_is_from_end
);
573 // Initialize fields for an Output_segment.
575 init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
576 elfcpp::STB binding
, elfcpp::STV visibility
,
577 unsigned char nonvis
, Segment_offset_base offset_base
);
579 // Initialize fields for a constant.
581 init_base(const char* name
, elfcpp::STT type
, elfcpp::STB binding
,
582 elfcpp::STV visibility
, unsigned char nonvis
);
584 // Override existing symbol.
585 template<int size
, bool big_endian
>
587 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
588 const char* version
);
590 // Override existing symbol with a special symbol.
592 override_base_with_special(const Symbol
* from
);
595 Symbol(const Symbol
&);
596 Symbol
& operator=(const Symbol
&);
598 // Symbol name (expected to point into a Stringpool).
600 // Symbol version (expected to point into a Stringpool). This may
602 const char* version_
;
606 // This struct is used if SOURCE_ == FROM_OBJECT.
609 // Object in which symbol is defined, or in which it was first
612 // Section number in object_ in which symbol is defined.
616 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
619 // Output_data in which symbol is defined. Before
620 // Layout::finalize the symbol's value is an offset within the
622 Output_data
* output_data
;
623 // True if the offset is from the end, false if the offset is
624 // from the beginning.
625 bool offset_is_from_end
;
628 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
631 // Output_segment in which the symbol is defined. Before
632 // Layout::finalize the symbol's value is an offset.
633 Output_segment
* output_segment
;
634 // The base to use for the offset before Layout::finalize.
635 Segment_offset_base offset_base
;
639 // The index of this symbol in the output file. If the symbol is
640 // not going into the output file, this value is -1U. This field
641 // starts as always holding zero. It is set to a non-zero value by
642 // Symbol_table::finalize.
643 unsigned int symtab_index_
;
645 // The index of this symbol in the dynamic symbol table. If the
646 // symbol is not going into the dynamic symbol table, this value is
647 // -1U. This field starts as always holding zero. It is set to a
648 // non-zero value during Layout::finalize.
649 unsigned int dynsym_index_
;
651 // If this symbol has an entry in the GOT section (has_got_offset_
652 // is true), this is the offset from the start of the GOT section.
653 // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
654 // serves as the GOT offset for the GOT entry that holds its
655 // TP-relative offset.
656 unsigned int got_offset_
;
658 // If this is a TLS symbol and has an entry in the GOT section
659 // for a module index or a pair of entries (module index,
660 // dtv-relative offset), these are the offsets from the start
661 // of the GOT section.
662 unsigned int tls_mod_got_offset_
;
663 unsigned int tls_pair_got_offset_
;
665 // If this symbol has an entry in the PLT section (has_plt_offset_
666 // is true), then this is the offset from the start of the PLT
668 unsigned int plt_offset_
;
671 elfcpp::STT type_
: 4;
673 elfcpp::STB binding_
: 4;
674 // Symbol visibility.
675 elfcpp::STV visibility_
: 2;
676 // Rest of symbol st_other field.
677 unsigned int nonvis_
: 6;
678 // The type of symbol.
680 // True if this symbol always requires special target-specific
682 bool is_target_special_
: 1;
683 // True if this is the default version of the symbol.
685 // True if this symbol really forwards to another symbol. This is
686 // used when we discover after the fact that two different entries
687 // in the hash table really refer to the same symbol. This will
688 // never be set for a symbol found in the hash table, but may be set
689 // for a symbol found in the list of symbols attached to an Object.
690 // It forwards to the symbol found in the forwarders_ map of
692 bool is_forwarder_
: 1;
693 // True if the symbol has an alias in the weak_aliases table in
696 // True if this symbol needs to be in the dynamic symbol table.
697 bool needs_dynsym_entry_
: 1;
698 // True if we've seen this symbol in a regular object.
700 // True if we've seen this symbol in a dynamic object.
702 // True if the symbol has an entry in the GOT section.
703 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
704 bool has_got_offset_
: 1;
705 // True if the symbol has an entry in the GOT section for its
707 bool has_tls_mod_got_offset_
: 1;
708 // True if the symbol has a pair of entries in the GOT section for its
709 // module index and dtv-relative offset.
710 bool has_tls_pair_got_offset_
: 1;
711 // True if the symbol has an entry in the PLT section.
712 bool has_plt_offset_
: 1;
713 // True if this is a dynamic symbol which needs a special value in
714 // the dynamic symbol table.
715 bool needs_dynsym_value_
: 1;
716 // True if there is a warning for this symbol.
717 bool has_warning_
: 1;
718 // True if we are using a COPY reloc for this symbol, so that the
719 // real definition lives in a dynamic object.
720 bool is_copied_from_dynobj_
: 1;
721 // True if the static value should be written to the GOT even
722 // when the final value is subject to dynamic relocation.
723 bool needs_value_in_got_
: 1;
726 // The parts of a symbol which are size specific. Using a template
727 // derived class like this helps us use less space on a 32-bit system.
730 class Sized_symbol
: public Symbol
733 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
734 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
739 // Initialize fields from an ELF symbol in OBJECT.
740 template<bool big_endian
>
742 init(const char *name
, const char* version
, Object
* object
,
743 const elfcpp::Sym
<size
, big_endian
>&);
745 // Initialize fields for an Output_data.
747 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
748 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
749 bool offset_is_from_end
);
751 // Initialize fields for an Output_segment.
753 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
754 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
755 Segment_offset_base offset_base
);
757 // Initialize fields for a constant.
759 init(const char* name
, Value_type value
, Size_type symsize
,
760 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
762 // Override existing symbol.
763 template<bool big_endian
>
765 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
766 const char* version
);
768 // Override existing symbol with a special symbol.
770 override_with_special(const Sized_symbol
<size
>*);
772 // Return the symbol's value.
775 { return this->value_
; }
777 // Return the symbol's size (we can't call this 'size' because that
778 // is a template parameter).
781 { return this->symsize_
; }
783 // Set the symbol size. This is used when resolving common symbols.
785 set_symsize(Size_type symsize
)
786 { this->symsize_
= symsize
; }
788 // Set the symbol value. This is called when we store the final
789 // values of the symbols into the symbol table.
791 set_value(Value_type value
)
792 { this->value_
= value
; }
795 Sized_symbol(const Sized_symbol
&);
796 Sized_symbol
& operator=(const Sized_symbol
&);
798 // Symbol value. Before Layout::finalize this is the offset in the
799 // input section. This is set to the final value during
806 // A struct describing a symbol defined by the linker, where the value
807 // of the symbol is defined based on an output section. This is used
808 // for symbols defined by the linker, like "_init_array_start".
810 struct Define_symbol_in_section
814 // The name of the output section with which this symbol should be
815 // associated. If there is no output section with that name, the
816 // symbol will be defined as zero.
817 const char* output_section
;
818 // The offset of the symbol within the output section. This is an
819 // offset from the start of the output section, unless start_at_end
820 // is true, in which case this is an offset from the end of the
823 // The size of the symbol.
827 // The symbol binding.
829 // The symbol visibility.
830 elfcpp::STV visibility
;
831 // The rest of the st_other field.
832 unsigned char nonvis
;
833 // If true, the value field is an offset from the end of the output
835 bool offset_is_from_end
;
836 // If true, this symbol is defined only if we see a reference to it.
840 // A struct describing a symbol defined by the linker, where the value
841 // of the symbol is defined based on a segment. This is used for
842 // symbols defined by the linker, like "_end". We describe the
843 // segment with which the symbol should be associated by its
844 // characteristics. If no segment meets these characteristics, the
845 // symbol will be defined as zero. If there is more than one segment
846 // which meets these characteristics, we will use the first one.
848 struct Define_symbol_in_segment
852 // The segment type where the symbol should be defined, typically
854 elfcpp::PT segment_type
;
855 // Bitmask of segment flags which must be set.
856 elfcpp::PF segment_flags_set
;
857 // Bitmask of segment flags which must be clear.
858 elfcpp::PF segment_flags_clear
;
859 // The offset of the symbol within the segment. The offset is
860 // calculated from the position set by offset_base.
862 // The size of the symbol.
866 // The symbol binding.
868 // The symbol visibility.
869 elfcpp::STV visibility
;
870 // The rest of the st_other field.
871 unsigned char nonvis
;
872 // The base from which we compute the offset.
873 Symbol::Segment_offset_base offset_base
;
874 // If true, this symbol is defined only if we see a reference to it.
878 // This class manages warnings. Warnings are a GNU extension. When
879 // we see a section named .gnu.warning.SYM in an object file, and if
880 // we wind using the definition of SYM from that object file, then we
881 // will issue a warning for any relocation against SYM from a
882 // different object file. The text of the warning is the contents of
883 // the section. This is not precisely the definition used by the old
884 // GNU linker; the old GNU linker treated an occurrence of
885 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
886 // would trigger a warning on any reference. However, it was
887 // inconsistent in that a warning in a dynamic object only triggered
888 // if there was no definition in a regular object. This linker is
889 // different in that we only issue a warning if we use the symbol
890 // definition from the same object file as the warning section.
899 // Add a warning for symbol NAME in section SHNDX in object OBJ.
901 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
904 // For each symbol for which we should give a warning, make a note
907 note_warnings(Symbol_table
* symtab
);
909 // Issue a warning for a reference to SYM at RELINFO's location.
910 template<int size
, bool big_endian
>
912 issue_warning(const Symbol
* sym
, const Relocate_info
<size
, big_endian
>*,
913 size_t relnum
, off_t reloffset
) const;
916 Warnings(const Warnings
&);
917 Warnings
& operator=(const Warnings
&);
919 // What we need to know to get the warning text.
920 struct Warning_location
922 // The object the warning is in.
924 // The index of the warning section.
926 // The warning text if we have already loaded it.
930 : object(NULL
), shndx(0), text()
934 set(Object
* o
, unsigned int s
)
941 set_text(const char* t
, off_t l
)
942 { this->text
.assign(t
, l
); }
945 // A mapping from warning symbol names (canonicalized in
946 // Symbol_table's namepool_ field) to warning information.
947 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
949 Warning_table warnings_
;
952 // The main linker symbol table.
961 // Add COUNT external symbols from the relocatable object RELOBJ to
962 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
963 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
964 // point to the symbols in the symbol table.
965 template<int size
, bool big_endian
>
967 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
968 const unsigned char* syms
, size_t count
,
969 const char* sym_names
, size_t sym_name_size
,
970 typename Sized_relobj
<size
, big_endian
>::Symbols
*);
972 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
973 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
974 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
975 // symbol version data.
976 template<int size
, bool big_endian
>
978 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
979 const unsigned char* syms
, size_t count
,
980 const char* sym_names
, size_t sym_name_size
,
981 const unsigned char* versym
, size_t versym_size
,
982 const std::vector
<const char*>*);
984 // Define a special symbol based on an Output_data. It is a
985 // multiple definition error if this symbol is already defined.
987 define_in_output_data(const Target
*, const char* name
, const char* version
,
988 Output_data
*, uint64_t value
, uint64_t symsize
,
989 elfcpp::STT type
, elfcpp::STB binding
,
990 elfcpp::STV visibility
, unsigned char nonvis
,
991 bool offset_is_from_end
, bool only_if_ref
);
993 // Define a special symbol based on an Output_segment. It is a
994 // multiple definition error if this symbol is already defined.
996 define_in_output_segment(const Target
*, const char* name
,
997 const char* version
, Output_segment
*,
998 uint64_t value
, uint64_t symsize
,
999 elfcpp::STT type
, elfcpp::STB binding
,
1000 elfcpp::STV visibility
, unsigned char nonvis
,
1001 Symbol::Segment_offset_base
, bool only_if_ref
);
1003 // Define a special symbol with a constant value. It is a multiple
1004 // definition error if this symbol is already defined.
1006 define_as_constant(const Target
*, const char* name
, const char* version
,
1007 uint64_t value
, uint64_t symsize
, elfcpp::STT type
,
1008 elfcpp::STB binding
, elfcpp::STV visibility
,
1009 unsigned char nonvis
, bool only_if_ref
);
1011 // Define a set of symbols in output sections.
1013 define_symbols(const Layout
*, const Target
*, int count
,
1014 const Define_symbol_in_section
*);
1016 // Define a set of symbols in output segments.
1018 define_symbols(const Layout
*, const Target
*, int count
,
1019 const Define_symbol_in_segment
*);
1021 // Define SYM using a COPY reloc. POSD is the Output_data where the
1022 // symbol should be defined--typically a .dyn.bss section. VALUE is
1023 // the offset within POSD.
1026 define_with_copy_reloc(const Target
*, Sized_symbol
<size
>* sym
,
1027 Output_data
* posd
, uint64_t value
);
1029 // Look up a symbol.
1031 lookup(const char*, const char* version
= NULL
) const;
1033 // Return the real symbol associated with the forwarder symbol FROM.
1035 resolve_forwards(const Symbol
* from
) const;
1037 // Return the sized version of a symbol in this table.
1040 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
1043 const Sized_symbol
<size
>*
1044 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
1046 // Return the count of undefined symbols seen.
1048 saw_undefined() const
1049 { return this->saw_undefined_
; }
1051 // Allocate the common symbols
1053 allocate_commons(const General_options
&, Layout
*);
1055 // Add a warning for symbol NAME in section SHNDX in object OBJ.
1057 add_warning(const char* name
, Object
* obj
, unsigned int shndx
)
1058 { this->warnings_
.add_warning(this, name
, obj
, shndx
); }
1060 // Canonicalize a symbol name for use in the hash table.
1062 canonicalize_name(const char* name
)
1063 { return this->namepool_
.add(name
, true, NULL
); }
1065 // Possibly issue a warning for a reference to SYM at LOCATION which
1067 template<int size
, bool big_endian
>
1069 issue_warning(const Symbol
* sym
,
1070 const Relocate_info
<size
, big_endian
>* relinfo
,
1071 size_t relnum
, off_t reloffset
) const
1072 { this->warnings_
.issue_warning(sym
, relinfo
, relnum
, reloffset
); }
1074 // Check candidate_odr_violations_ to find symbols with the same name
1075 // but apparently different definitions (different source-file/line-no).
1077 detect_odr_violations(const char* output_file_name
) const;
1079 // SYM is defined using a COPY reloc. Return the dynamic object
1080 // where the original definition was found.
1082 get_copy_source(const Symbol
* sym
) const;
1084 // Set the dynamic symbol indexes. INDEX is the index of the first
1085 // global dynamic symbol. Pointers to the symbols are stored into
1086 // the vector. The names are stored into the Stringpool. This
1087 // returns an updated dynamic symbol index.
1089 set_dynsym_indexes(const Target
*, unsigned int index
,
1090 std::vector
<Symbol
*>*, Stringpool
*, Versions
*);
1092 // Finalize the symbol table after we have set the final addresses
1093 // of all the input sections. This sets the final symbol indexes,
1094 // values and adds the names to *POOL. INDEX is the index of the
1095 // first global symbol. OFF is the file offset of the global symbol
1096 // table, DYNOFF is the offset of the globals in the dynamic symbol
1097 // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
1098 // symbol, and DYNCOUNT is the number of global dynamic symbols.
1099 // This records the parameters, and returns the new file offset.
1101 finalize(unsigned int index
, off_t off
, off_t dynoff
,
1102 size_t dyn_global_index
, size_t dyncount
, Stringpool
* pool
);
1104 // Write out the global symbols.
1106 write_globals(const Input_objects
*, const Stringpool
*, const Stringpool
*,
1107 Output_file
*) const;
1109 // Write out a section symbol. Return the updated offset.
1111 write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1114 Symbol_table(const Symbol_table
&);
1115 Symbol_table
& operator=(const Symbol_table
&);
1117 // Make FROM a forwarder symbol to TO.
1119 make_forwarder(Symbol
* from
, Symbol
* to
);
1122 template<int size
, bool big_endian
>
1124 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
1125 const char *version
, Stringpool::Key version_key
,
1126 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
,
1127 const elfcpp::Sym
<size
, big_endian
>& orig_sym
);
1130 template<int size
, bool big_endian
>
1132 resolve(Sized_symbol
<size
>* to
,
1133 const elfcpp::Sym
<size
, big_endian
>& sym
,
1134 const elfcpp::Sym
<size
, big_endian
>& orig_sym
,
1135 Object
*, const char* version
);
1137 template<int size
, bool big_endian
>
1139 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
1140 const char* version ACCEPT_SIZE_ENDIAN
);
1142 // Whether we should override a symbol, based on flags in
1145 should_override(const Symbol
*, unsigned int, Object
*, bool*);
1147 // Override a symbol.
1148 template<int size
, bool big_endian
>
1150 override(Sized_symbol
<size
>* tosym
,
1151 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
1152 Object
* object
, const char* version
);
1154 // Whether we should override a symbol with a special symbol which
1155 // is automatically defined by the linker.
1157 should_override_with_special(const Symbol
*);
1159 // Override a symbol with a special symbol.
1162 override_with_special(Sized_symbol
<size
>* tosym
,
1163 const Sized_symbol
<size
>* fromsym
);
1165 // Record all weak alias sets for a dynamic object.
1168 record_weak_aliases(std::vector
<Sized_symbol
<size
>*>*);
1170 // Define a special symbol.
1171 template<int size
, bool big_endian
>
1173 define_special_symbol(const Target
* target
, const char** pname
,
1174 const char** pversion
, bool only_if_ref
,
1175 Sized_symbol
<size
>** poldsym ACCEPT_SIZE_ENDIAN
);
1177 // Define a symbol in an Output_data, sized version.
1180 do_define_in_output_data(const Target
*, const char* name
,
1181 const char* version
, Output_data
*,
1182 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1183 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1184 elfcpp::STT type
, elfcpp::STB binding
,
1185 elfcpp::STV visibility
, unsigned char nonvis
,
1186 bool offset_is_from_end
, bool only_if_ref
);
1188 // Define a symbol in an Output_segment, sized version.
1191 do_define_in_output_segment(
1192 const Target
*, const char* name
, const char* version
, Output_segment
* os
,
1193 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1194 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1195 elfcpp::STT type
, elfcpp::STB binding
,
1196 elfcpp::STV visibility
, unsigned char nonvis
,
1197 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
1199 // Define a symbol as a constant, sized version.
1202 do_define_as_constant(
1203 const Target
*, const char* name
, const char* version
,
1204 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1205 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1206 elfcpp::STT type
, elfcpp::STB binding
,
1207 elfcpp::STV visibility
, unsigned char nonvis
,
1210 // Allocate the common symbols, sized version.
1213 do_allocate_commons(const General_options
&, Layout
*);
1215 // Implement detect_odr_violations.
1216 template<int size
, bool big_endian
>
1218 sized_detect_odr_violations() const;
1220 // Finalize symbols specialized for size.
1223 sized_finalize(unsigned int, off_t
, Stringpool
*);
1225 // Write globals specialized for size and endianness.
1226 template<int size
, bool big_endian
>
1228 sized_write_globals(const Input_objects
*, const Stringpool
*,
1229 const Stringpool
*, Output_file
*) const;
1231 // Write out a symbol to P.
1232 template<int size
, bool big_endian
>
1234 sized_write_symbol(Sized_symbol
<size
>*,
1235 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1237 const Stringpool
*, unsigned char* p
1238 ACCEPT_SIZE_ENDIAN
) const;
1240 // Possibly warn about an undefined symbol from a dynamic object.
1242 warn_about_undefined_dynobj_symbol(const Input_objects
*, Symbol
*) const;
1244 // Write out a section symbol, specialized for size and endianness.
1245 template<int size
, bool big_endian
>
1247 sized_write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1249 // The type of the symbol hash table.
1251 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
1253 struct Symbol_table_hash
1256 operator()(const Symbol_table_key
&) const;
1259 struct Symbol_table_eq
1262 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
1265 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
1266 Symbol_table_eq
> Symbol_table_type
;
1268 // The type of the list of common symbols.
1269 typedef std::vector
<Symbol
*> Commons_type
;
1271 // A map from symbols with COPY relocs to the dynamic objects where
1272 // they are defined.
1273 typedef Unordered_map
<const Symbol
*, Dynobj
*> Copied_symbol_dynobjs
;
1275 // A map from symbol name (as a pointer into the namepool) to all
1276 // the locations the symbols is (weakly) defined (and certain other
1277 // conditions are met). This map will be used later to detect
1278 // possible One Definition Rule (ODR) violations.
1279 struct Symbol_location
1281 Object
* object
; // Object where the symbol is defined.
1282 unsigned int shndx
; // Section-in-object where the symbol is defined.
1283 off_t offset
; // Offset-in-section where the symbol is defined.
1284 bool operator==(const Symbol_location
& that
) const
1286 return (this->object
== that
.object
1287 && this->shndx
== that
.shndx
1288 && this->offset
== that
.offset
);
1292 struct Symbol_location_hash
1294 size_t operator()(const Symbol_location
& loc
) const
1295 { return reinterpret_cast<uintptr_t>(loc
.object
) ^ loc
.offset
^ loc
.shndx
; }
1298 typedef Unordered_map
<const char*,
1299 Unordered_set
<Symbol_location
, Symbol_location_hash
> >
1302 // We increment this every time we see a new undefined symbol, for
1303 // use in archive groups.
1305 // The index of the first global symbol in the output file.
1306 unsigned int first_global_index_
;
1307 // The file offset within the output symtab section where we should
1310 // The number of global symbols we want to write out.
1311 size_t output_count_
;
1312 // The file offset of the global dynamic symbols, or 0 if none.
1313 off_t dynamic_offset_
;
1314 // The index of the first global dynamic symbol.
1315 unsigned int first_dynamic_global_index_
;
1316 // The number of global dynamic symbols, or 0 if none.
1317 off_t dynamic_count_
;
1318 // The symbol hash table.
1319 Symbol_table_type table_
;
1320 // A pool of symbol names. This is used for all global symbols.
1321 // Entries in the hash table point into this pool.
1322 Stringpool namepool_
;
1323 // Forwarding symbols.
1324 Unordered_map
<const Symbol
*, Symbol
*> forwarders_
;
1325 // Weak aliases. A symbol in this list points to the next alias.
1326 // The aliases point to each other in a circular list.
1327 Unordered_map
<Symbol
*, Symbol
*> weak_aliases_
;
1328 // We don't expect there to be very many common symbols, so we keep
1329 // a list of them. When we find a common symbol we add it to this
1330 // list. It is possible that by the time we process the list the
1331 // symbol is no longer a common symbol. It may also have become a
1333 Commons_type commons_
;
1334 // Manage symbol warnings.
1336 // Manage potential One Definition Rule (ODR) violations.
1337 Odr_map candidate_odr_violations_
;
1339 // When we emit a COPY reloc for a symbol, we define it in an
1340 // Output_data. When it's time to emit version information for it,
1341 // we need to know the dynamic object in which we found the original
1342 // definition. This maps symbols with COPY relocs to the dynamic
1343 // object where they were defined.
1344 Copied_symbol_dynobjs copied_symbol_dynobjs_
;
1347 // We inline get_sized_symbol for efficiency.
1351 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
1353 gold_assert(size
== parameters
->get_size());
1354 return static_cast<Sized_symbol
<size
>*>(sym
);
1358 const Sized_symbol
<size
>*
1359 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
1361 gold_assert(size
== parameters
->get_size());
1362 return static_cast<const Sized_symbol
<size
>*>(sym
);
1365 } // End namespace gold.
1367 #endif // !defined(GOLD_SYMTAB_H)