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 (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 the symbol source.
117 { return this->source_
; }
119 // Return the object with which this symbol is associated.
123 gold_assert(this->source_
== FROM_OBJECT
);
124 return this->u_
.from_object
.object
;
127 // Return the index of the section in the input relocatable or
128 // dynamic object file.
132 gold_assert(this->source_
== FROM_OBJECT
);
133 return this->u_
.from_object
.shndx
;
136 // Return the output data section with which this symbol is
137 // associated, if the symbol was specially defined with respect to
138 // an output data section.
142 gold_assert(this->source_
== IN_OUTPUT_DATA
);
143 return this->u_
.in_output_data
.output_data
;
146 // If this symbol was defined with respect to an output data
147 // section, return whether the value is an offset from end.
149 offset_is_from_end() const
151 gold_assert(this->source_
== IN_OUTPUT_DATA
);
152 return this->u_
.in_output_data
.offset_is_from_end
;
155 // Return the output segment with which this symbol is associated,
156 // if the symbol was specially defined with respect to an output
159 output_segment() const
161 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
162 return this->u_
.in_output_segment
.output_segment
;
165 // If this symbol was defined with respect to an output segment,
166 // return the offset base.
170 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
171 return this->u_
.in_output_segment
.offset_base
;
174 // Return the symbol binding.
177 { return this->binding_
; }
179 // Return the symbol type.
182 { return this->type_
; }
184 // Return the symbol visibility.
187 { return this->visibility_
; }
189 // Return the non-visibility part of the st_other field.
192 { return this->nonvis_
; }
194 // Return whether this symbol is a forwarder. This will never be
195 // true of a symbol found in the hash table, but may be true of
196 // symbol pointers attached to object files.
199 { return this->is_forwarder_
; }
201 // Mark this symbol as a forwarder.
204 { this->is_forwarder_
= true; }
206 // Return whether this symbol has an alias in the weak aliases table
210 { return this->has_alias_
; }
212 // Mark this symbol as having an alias.
215 { this->has_alias_
= true; }
217 // Return whether this symbol needs an entry in the dynamic symbol
220 needs_dynsym_entry() const
222 return (this->needs_dynsym_entry_
223 || (this->in_reg() && this->in_dyn()));
226 // Mark this symbol as needing an entry in the dynamic symbol table.
228 set_needs_dynsym_entry()
229 { this->needs_dynsym_entry_
= true; }
231 // Return whether this symbol should be added to the dynamic symbol
234 should_add_dynsym_entry() const;
236 // Return whether this symbol has been seen in a regular object.
239 { return this->in_reg_
; }
241 // Mark this symbol as having been seen in a regular object.
244 { this->in_reg_
= true; }
246 // Return whether this symbol has been seen in a dynamic object.
249 { return this->in_dyn_
; }
251 // Mark this symbol as having been seen in a dynamic object.
254 { this->in_dyn_
= true; }
256 // Return the index of this symbol in the output file symbol table.
257 // A value of -1U means that this symbol is not going into the
258 // output file. This starts out as zero, and is set to a non-zero
259 // value by Symbol_table::finalize. It is an error to ask for the
260 // symbol table index before it has been set.
264 gold_assert(this->symtab_index_
!= 0);
265 return this->symtab_index_
;
268 // Set the index of the symbol in the output file symbol table.
270 set_symtab_index(unsigned int index
)
272 gold_assert(index
!= 0);
273 this->symtab_index_
= index
;
276 // Return whether this symbol already has an index in the output
277 // file symbol table.
279 has_symtab_index() const
280 { return this->symtab_index_
!= 0; }
282 // Return the index of this symbol in the dynamic symbol table. A
283 // value of -1U means that this symbol is not going into the dynamic
284 // symbol table. This starts out as zero, and is set to a non-zero
285 // during Layout::finalize. It is an error to ask for the dynamic
286 // symbol table index before it has been set.
290 gold_assert(this->dynsym_index_
!= 0);
291 return this->dynsym_index_
;
294 // Set the index of the symbol in the dynamic symbol table.
296 set_dynsym_index(unsigned int index
)
298 gold_assert(index
!= 0);
299 this->dynsym_index_
= index
;
302 // Return whether this symbol already has an index in the dynamic
305 has_dynsym_index() const
306 { return this->dynsym_index_
!= 0; }
308 // Return whether this symbol has an entry in the GOT section.
309 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
311 has_got_offset() const
312 { return this->has_got_offset_
; }
314 // Return the offset into the GOT section of this symbol.
318 gold_assert(this->has_got_offset());
319 return this->got_offset_
;
322 // Set the GOT offset of this symbol.
324 set_got_offset(unsigned int got_offset
)
326 this->has_got_offset_
= true;
327 this->got_offset_
= got_offset
;
330 // Return whether this TLS symbol has an entry in the GOT section for
331 // its module index or, if NEED_PAIR is true, has a pair of entries
332 // for its module index and dtv-relative offset.
334 has_tls_got_offset(bool need_pair
) const
336 return (this->has_tls_mod_got_offset_
337 && (!need_pair
|| this->has_tls_pair_got_offset_
));
340 // Return the offset into the GOT section for this symbol's TLS module
341 // index or, if NEED_PAIR is true, for the pair of entries for the
342 // module index and dtv-relative offset.
344 tls_got_offset(bool need_pair
) const
346 gold_assert(this->has_tls_got_offset(need_pair
));
347 return this->tls_mod_got_offset_
;
350 // Set the GOT offset of this symbol.
352 set_tls_got_offset(unsigned int got_offset
, bool have_pair
)
354 this->has_tls_mod_got_offset_
= true;
355 this->has_tls_pair_got_offset_
= have_pair
;
356 this->tls_mod_got_offset_
= got_offset
;
359 // Return whether this symbol has an entry in the PLT section.
361 has_plt_offset() const
362 { return this->has_plt_offset_
; }
364 // Return the offset into the PLT section of this symbol.
368 gold_assert(this->has_plt_offset());
369 return this->plt_offset_
;
372 // Set the PLT offset of this symbol.
374 set_plt_offset(unsigned int plt_offset
)
376 this->has_plt_offset_
= true;
377 this->plt_offset_
= plt_offset
;
380 // Return whether this dynamic symbol needs a special value in the
381 // dynamic symbol table.
383 needs_dynsym_value() const
384 { return this->needs_dynsym_value_
; }
386 // Set that this dynamic symbol needs a special value in the dynamic
389 set_needs_dynsym_value()
391 gold_assert(this->object()->is_dynamic());
392 this->needs_dynsym_value_
= true;
395 // Return true if the final value of this symbol is known at link
398 final_value_is_known() const;
400 // Return whether this is a defined symbol (not undefined or
405 return (this->source_
!= FROM_OBJECT
406 || (this->shndx() != elfcpp::SHN_UNDEF
407 && this->shndx() != elfcpp::SHN_COMMON
));
410 // Return true if this symbol is from a dynamic object.
412 is_from_dynobj() const
414 return this->source_
== FROM_OBJECT
&& this->object()->is_dynamic();
417 // Return whether this is an undefined symbol.
421 return this->source_
== FROM_OBJECT
&& this->shndx() == elfcpp::SHN_UNDEF
;
424 // Return whether this is a common symbol.
428 return (this->source_
== FROM_OBJECT
429 && (this->shndx() == elfcpp::SHN_COMMON
430 || this->type_
== elfcpp::STT_COMMON
));
433 // Return whether this symbol can be seen outside this object.
435 is_externally_visible() const
437 return (this->visibility_
== elfcpp::STV_DEFAULT
438 || this->visibility_
== elfcpp::STV_PROTECTED
);
441 // Return true if this symbol can be preempted by a definition in
442 // another link unit.
444 is_preemptible() const
446 // It doesn't make sense to ask whether a symbol defined in
447 // another object is preemptible.
448 gold_assert(!this->is_from_dynobj());
450 return (this->visibility_
!= elfcpp::STV_INTERNAL
451 && this->visibility_
!= elfcpp::STV_HIDDEN
452 && this->visibility_
!= elfcpp::STV_PROTECTED
453 && parameters
->output_is_shared()
454 && !parameters
->symbolic());
457 // Return true if this symbol is a function that needs a PLT entry.
458 // If the symbol is defined in a dynamic object or if it is subject
459 // to pre-emption, we need to make a PLT entry.
461 needs_plt_entry() const
463 return (this->type() == elfcpp::STT_FUNC
464 && (this->is_from_dynobj() || this->is_preemptible()));
467 // Given a direct absolute or pc-relative static relocation against
468 // the global symbol, this function returns whether a dynamic relocation
472 needs_dynamic_reloc(bool is_absolute_ref
, bool is_function_call
) const
474 // An absolute reference within a position-independent output file
475 // will need a dynamic relocaion.
476 if (is_absolute_ref
&& parameters
->output_is_position_independent())
479 // A function call that can branch to a local PLT entry does not need
480 // a dynamic relocation.
481 if (is_function_call
&& this->has_plt_offset())
484 // A reference to any PLT entry in a non-position-independent executable
485 // does not need a dynamic relocation.
486 if (!parameters
->output_is_position_independent()
487 && this->has_plt_offset())
490 // A reference to a symbol defined in a dynamic object or to a
491 // symbol that is preemptible will need a dynamic relocation.
492 if (this->is_from_dynobj() || this->is_preemptible())
495 // For all other cases, return FALSE.
499 // Given a direct absolute static relocation against
500 // the global symbol, where a dynamic relocation is needed, this
501 // function returns whether a relative dynamic relocation can be used.
502 // The caller must determine separately whether the static relocation
503 // is compatible with a relative relocation.
506 can_use_relative_reloc(bool is_function_call
) const
508 // A function call that can branch to a local PLT entry can
509 // use a RELATIVE relocation.
510 if (is_function_call
&& this->has_plt_offset())
513 // A reference to a symbol defined in a dynamic object or to a
514 // symbol that is preemptible can not use a RELATIVE relocaiton.
515 if (this->is_from_dynobj() || this->is_preemptible())
518 // For all other cases, return TRUE.
522 // Return whether there should be a warning for references to this
526 { return this->has_warning_
; }
528 // Mark this symbol as having a warning.
531 { this->has_warning_
= true; }
533 // Return whether this symbol is defined by a COPY reloc from a
536 is_copied_from_dynobj() const
537 { return this->is_copied_from_dynobj_
; }
539 // Mark this symbol as defined by a COPY reloc.
541 set_is_copied_from_dynobj()
542 { this->is_copied_from_dynobj_
= true; }
544 // Mark this symbol as needing its value written to the GOT even when
545 // the value is subject to dynamic relocation (e.g., when the target
546 // uses a RELATIVE relocation for the GOT entry).
548 set_needs_value_in_got()
549 { this->needs_value_in_got_
= true; }
551 // Return whether this symbol needs its value written to the GOT even
552 // when the value is subject to dynamic relocation.
554 needs_value_in_got() const
555 { return this->needs_value_in_got_
; }
558 // Instances of this class should always be created at a specific
561 { memset(this, 0, sizeof *this); }
563 // Initialize the general fields.
565 init_fields(const char* name
, const char* version
,
566 elfcpp::STT type
, elfcpp::STB binding
,
567 elfcpp::STV visibility
, unsigned char nonvis
);
569 // Initialize fields from an ELF symbol in OBJECT.
570 template<int size
, bool big_endian
>
572 init_base(const char *name
, const char* version
, Object
* object
,
573 const elfcpp::Sym
<size
, big_endian
>&);
575 // Initialize fields for an Output_data.
577 init_base(const char* name
, Output_data
*, elfcpp::STT
, elfcpp::STB
,
578 elfcpp::STV
, unsigned char nonvis
, bool offset_is_from_end
);
580 // Initialize fields for an Output_segment.
582 init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
583 elfcpp::STB binding
, elfcpp::STV visibility
,
584 unsigned char nonvis
, Segment_offset_base offset_base
);
586 // Initialize fields for a constant.
588 init_base(const char* name
, elfcpp::STT type
, elfcpp::STB binding
,
589 elfcpp::STV visibility
, unsigned char nonvis
);
591 // Override existing symbol.
592 template<int size
, bool big_endian
>
594 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
595 const char* version
);
597 // Override existing symbol with a special symbol.
599 override_base_with_special(const Symbol
* from
);
602 Symbol(const Symbol
&);
603 Symbol
& operator=(const Symbol
&);
605 // Symbol name (expected to point into a Stringpool).
607 // Symbol version (expected to point into a Stringpool). This may
609 const char* version_
;
613 // This struct is used if SOURCE_ == FROM_OBJECT.
616 // Object in which symbol is defined, or in which it was first
619 // Section number in object_ in which symbol is defined.
623 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
626 // Output_data in which symbol is defined. Before
627 // Layout::finalize the symbol's value is an offset within the
629 Output_data
* output_data
;
630 // True if the offset is from the end, false if the offset is
631 // from the beginning.
632 bool offset_is_from_end
;
635 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
638 // Output_segment in which the symbol is defined. Before
639 // Layout::finalize the symbol's value is an offset.
640 Output_segment
* output_segment
;
641 // The base to use for the offset before Layout::finalize.
642 Segment_offset_base offset_base
;
646 // The index of this symbol in the output file. If the symbol is
647 // not going into the output file, this value is -1U. This field
648 // starts as always holding zero. It is set to a non-zero value by
649 // Symbol_table::finalize.
650 unsigned int symtab_index_
;
652 // The index of this symbol in the dynamic symbol table. If the
653 // symbol is not going into the dynamic symbol table, this value is
654 // -1U. This field starts as always holding zero. It is set to a
655 // non-zero value during Layout::finalize.
656 unsigned int dynsym_index_
;
658 // If this symbol has an entry in the GOT section (has_got_offset_
659 // is true), this is the offset from the start of the GOT section.
660 // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
661 // serves as the GOT offset for the GOT entry that holds its
662 // TP-relative offset.
663 unsigned int got_offset_
;
665 // If this is a TLS symbol and has an entry in the GOT section
666 // for a module index or a pair of entries (module index,
667 // dtv-relative offset), these are the offsets from the start
668 // of the GOT section.
669 unsigned int tls_mod_got_offset_
;
670 unsigned int tls_pair_got_offset_
;
672 // If this symbol has an entry in the PLT section (has_plt_offset_
673 // is true), then this is the offset from the start of the PLT
675 unsigned int plt_offset_
;
678 elfcpp::STT type_
: 4;
680 elfcpp::STB binding_
: 4;
681 // Symbol visibility.
682 elfcpp::STV visibility_
: 2;
683 // Rest of symbol st_other field.
684 unsigned int nonvis_
: 6;
685 // The type of symbol.
687 // True if this symbol always requires special target-specific
689 bool is_target_special_
: 1;
690 // True if this is the default version of the symbol.
692 // True if this symbol really forwards to another symbol. This is
693 // used when we discover after the fact that two different entries
694 // in the hash table really refer to the same symbol. This will
695 // never be set for a symbol found in the hash table, but may be set
696 // for a symbol found in the list of symbols attached to an Object.
697 // It forwards to the symbol found in the forwarders_ map of
699 bool is_forwarder_
: 1;
700 // True if the symbol has an alias in the weak_aliases table in
703 // True if this symbol needs to be in the dynamic symbol table.
704 bool needs_dynsym_entry_
: 1;
705 // True if we've seen this symbol in a regular object.
707 // True if we've seen this symbol in a dynamic object.
709 // True if the symbol has an entry in the GOT section.
710 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
711 bool has_got_offset_
: 1;
712 // True if the symbol has an entry in the GOT section for its
714 bool has_tls_mod_got_offset_
: 1;
715 // True if the symbol has a pair of entries in the GOT section for its
716 // module index and dtv-relative offset.
717 bool has_tls_pair_got_offset_
: 1;
718 // True if the symbol has an entry in the PLT section.
719 bool has_plt_offset_
: 1;
720 // True if this is a dynamic symbol which needs a special value in
721 // the dynamic symbol table.
722 bool needs_dynsym_value_
: 1;
723 // True if there is a warning for this symbol.
724 bool has_warning_
: 1;
725 // True if we are using a COPY reloc for this symbol, so that the
726 // real definition lives in a dynamic object.
727 bool is_copied_from_dynobj_
: 1;
728 // True if the static value should be written to the GOT even
729 // when the final value is subject to dynamic relocation.
730 bool needs_value_in_got_
: 1;
733 // The parts of a symbol which are size specific. Using a template
734 // derived class like this helps us use less space on a 32-bit system.
737 class Sized_symbol
: public Symbol
740 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
741 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
746 // Initialize fields from an ELF symbol in OBJECT.
747 template<bool big_endian
>
749 init(const char *name
, const char* version
, Object
* object
,
750 const elfcpp::Sym
<size
, big_endian
>&);
752 // Initialize fields for an Output_data.
754 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
755 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
756 bool offset_is_from_end
);
758 // Initialize fields for an Output_segment.
760 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
761 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
762 Segment_offset_base offset_base
);
764 // Initialize fields for a constant.
766 init(const char* name
, Value_type value
, Size_type symsize
,
767 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
769 // Override existing symbol.
770 template<bool big_endian
>
772 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
,
773 const char* version
);
775 // Override existing symbol with a special symbol.
777 override_with_special(const Sized_symbol
<size
>*);
779 // Return the symbol's value.
782 { return this->value_
; }
784 // Return the symbol's size (we can't call this 'size' because that
785 // is a template parameter).
788 { return this->symsize_
; }
790 // Set the symbol size. This is used when resolving common symbols.
792 set_symsize(Size_type symsize
)
793 { this->symsize_
= symsize
; }
795 // Set the symbol value. This is called when we store the final
796 // values of the symbols into the symbol table.
798 set_value(Value_type value
)
799 { this->value_
= value
; }
802 Sized_symbol(const Sized_symbol
&);
803 Sized_symbol
& operator=(const Sized_symbol
&);
805 // Symbol value. Before Layout::finalize this is the offset in the
806 // input section. This is set to the final value during
813 // A struct describing a symbol defined by the linker, where the value
814 // of the symbol is defined based on an output section. This is used
815 // for symbols defined by the linker, like "_init_array_start".
817 struct Define_symbol_in_section
821 // The name of the output section with which this symbol should be
822 // associated. If there is no output section with that name, the
823 // symbol will be defined as zero.
824 const char* output_section
;
825 // The offset of the symbol within the output section. This is an
826 // offset from the start of the output section, unless start_at_end
827 // is true, in which case this is an offset from the end of the
830 // The size of the symbol.
834 // The symbol binding.
836 // The symbol visibility.
837 elfcpp::STV visibility
;
838 // The rest of the st_other field.
839 unsigned char nonvis
;
840 // If true, the value field is an offset from the end of the output
842 bool offset_is_from_end
;
843 // If true, this symbol is defined only if we see a reference to it.
847 // A struct describing a symbol defined by the linker, where the value
848 // of the symbol is defined based on a segment. This is used for
849 // symbols defined by the linker, like "_end". We describe the
850 // segment with which the symbol should be associated by its
851 // characteristics. If no segment meets these characteristics, the
852 // symbol will be defined as zero. If there is more than one segment
853 // which meets these characteristics, we will use the first one.
855 struct Define_symbol_in_segment
859 // The segment type where the symbol should be defined, typically
861 elfcpp::PT segment_type
;
862 // Bitmask of segment flags which must be set.
863 elfcpp::PF segment_flags_set
;
864 // Bitmask of segment flags which must be clear.
865 elfcpp::PF segment_flags_clear
;
866 // The offset of the symbol within the segment. The offset is
867 // calculated from the position set by offset_base.
869 // The size of the symbol.
873 // The symbol binding.
875 // The symbol visibility.
876 elfcpp::STV visibility
;
877 // The rest of the st_other field.
878 unsigned char nonvis
;
879 // The base from which we compute the offset.
880 Symbol::Segment_offset_base offset_base
;
881 // If true, this symbol is defined only if we see a reference to it.
885 // This class manages warnings. Warnings are a GNU extension. When
886 // we see a section named .gnu.warning.SYM in an object file, and if
887 // we wind using the definition of SYM from that object file, then we
888 // will issue a warning for any relocation against SYM from a
889 // different object file. The text of the warning is the contents of
890 // the section. This is not precisely the definition used by the old
891 // GNU linker; the old GNU linker treated an occurrence of
892 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
893 // would trigger a warning on any reference. However, it was
894 // inconsistent in that a warning in a dynamic object only triggered
895 // if there was no definition in a regular object. This linker is
896 // different in that we only issue a warning if we use the symbol
897 // definition from the same object file as the warning section.
906 // Add a warning for symbol NAME in section SHNDX in object OBJ.
908 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
911 // For each symbol for which we should give a warning, make a note
914 note_warnings(Symbol_table
* symtab
);
916 // Issue a warning for a reference to SYM at RELINFO's location.
917 template<int size
, bool big_endian
>
919 issue_warning(const Symbol
* sym
, const Relocate_info
<size
, big_endian
>*,
920 size_t relnum
, off_t reloffset
) const;
923 Warnings(const Warnings
&);
924 Warnings
& operator=(const Warnings
&);
926 // What we need to know to get the warning text.
927 struct Warning_location
929 // The object the warning is in.
931 // The index of the warning section.
933 // The warning text if we have already loaded it.
937 : object(NULL
), shndx(0), text()
941 set(Object
* o
, unsigned int s
)
948 set_text(const char* t
, off_t l
)
949 { this->text
.assign(t
, l
); }
952 // A mapping from warning symbol names (canonicalized in
953 // Symbol_table's namepool_ field) to warning information.
954 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
956 Warning_table warnings_
;
959 // The main linker symbol table.
968 // Add COUNT external symbols from the relocatable object RELOBJ to
969 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
970 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
971 // point to the symbols in the symbol table.
972 template<int size
, bool big_endian
>
974 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
975 const unsigned char* syms
, size_t count
,
976 const char* sym_names
, size_t sym_name_size
,
977 typename Sized_relobj
<size
, big_endian
>::Symbols
*);
979 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
980 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
981 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
982 // symbol version data.
983 template<int size
, bool big_endian
>
985 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
986 const unsigned char* syms
, size_t count
,
987 const char* sym_names
, size_t sym_name_size
,
988 const unsigned char* versym
, size_t versym_size
,
989 const std::vector
<const char*>*);
991 // Define a special symbol based on an Output_data. It is a
992 // multiple definition error if this symbol is already defined.
994 define_in_output_data(const Target
*, const char* name
, const char* version
,
995 Output_data
*, uint64_t value
, uint64_t symsize
,
996 elfcpp::STT type
, elfcpp::STB binding
,
997 elfcpp::STV visibility
, unsigned char nonvis
,
998 bool offset_is_from_end
, bool only_if_ref
);
1000 // Define a special symbol based on an Output_segment. It is a
1001 // multiple definition error if this symbol is already defined.
1003 define_in_output_segment(const Target
*, const char* name
,
1004 const char* version
, Output_segment
*,
1005 uint64_t value
, uint64_t symsize
,
1006 elfcpp::STT type
, elfcpp::STB binding
,
1007 elfcpp::STV visibility
, unsigned char nonvis
,
1008 Symbol::Segment_offset_base
, bool only_if_ref
);
1010 // Define a special symbol with a constant value. It is a multiple
1011 // definition error if this symbol is already defined.
1013 define_as_constant(const Target
*, const char* name
, const char* version
,
1014 uint64_t value
, uint64_t symsize
, elfcpp::STT type
,
1015 elfcpp::STB binding
, elfcpp::STV visibility
,
1016 unsigned char nonvis
, bool only_if_ref
);
1018 // Define a set of symbols in output sections.
1020 define_symbols(const Layout
*, const Target
*, int count
,
1021 const Define_symbol_in_section
*);
1023 // Define a set of symbols in output segments.
1025 define_symbols(const Layout
*, const Target
*, int count
,
1026 const Define_symbol_in_segment
*);
1028 // Define SYM using a COPY reloc. POSD is the Output_data where the
1029 // symbol should be defined--typically a .dyn.bss section. VALUE is
1030 // the offset within POSD.
1033 define_with_copy_reloc(const Target
*, Sized_symbol
<size
>* sym
,
1034 Output_data
* posd
, uint64_t value
);
1036 // Look up a symbol.
1038 lookup(const char*, const char* version
= NULL
) const;
1040 // Return the real symbol associated with the forwarder symbol FROM.
1042 resolve_forwards(const Symbol
* from
) const;
1044 // Return the sized version of a symbol in this table.
1047 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
1050 const Sized_symbol
<size
>*
1051 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
1053 // Return the count of undefined symbols seen.
1055 saw_undefined() const
1056 { return this->saw_undefined_
; }
1058 // Allocate the common symbols
1060 allocate_commons(const General_options
&, Layout
*);
1062 // Add a warning for symbol NAME in section SHNDX in object OBJ.
1064 add_warning(const char* name
, Object
* obj
, unsigned int shndx
)
1065 { this->warnings_
.add_warning(this, name
, obj
, shndx
); }
1067 // Canonicalize a symbol name for use in the hash table.
1069 canonicalize_name(const char* name
)
1070 { return this->namepool_
.add(name
, true, NULL
); }
1072 // Possibly issue a warning for a reference to SYM at LOCATION which
1074 template<int size
, bool big_endian
>
1076 issue_warning(const Symbol
* sym
,
1077 const Relocate_info
<size
, big_endian
>* relinfo
,
1078 size_t relnum
, off_t reloffset
) const
1079 { this->warnings_
.issue_warning(sym
, relinfo
, relnum
, reloffset
); }
1081 // Check candidate_odr_violations_ to find symbols with the same name
1082 // but apparently different definitions (different source-file/line-no).
1084 detect_odr_violations(const char* output_file_name
) const;
1086 // SYM is defined using a COPY reloc. Return the dynamic object
1087 // where the original definition was found.
1089 get_copy_source(const Symbol
* sym
) const;
1091 // Set the dynamic symbol indexes. INDEX is the index of the first
1092 // global dynamic symbol. Pointers to the symbols are stored into
1093 // the vector. The names are stored into the Stringpool. This
1094 // returns an updated dynamic symbol index.
1096 set_dynsym_indexes(const Target
*, unsigned int index
,
1097 std::vector
<Symbol
*>*, Stringpool
*, Versions
*);
1099 // Finalize the symbol table after we have set the final addresses
1100 // of all the input sections. This sets the final symbol indexes,
1101 // values and adds the names to *POOL. INDEX is the index of the
1102 // first global symbol. OFF is the file offset of the global symbol
1103 // table, DYNOFF is the offset of the globals in the dynamic symbol
1104 // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
1105 // symbol, and DYNCOUNT is the number of global dynamic symbols.
1106 // This records the parameters, and returns the new file offset.
1108 finalize(unsigned int index
, off_t off
, off_t dynoff
,
1109 size_t dyn_global_index
, size_t dyncount
, Stringpool
* pool
);
1111 // Write out the global symbols.
1113 write_globals(const Input_objects
*, const Stringpool
*, const Stringpool
*,
1114 Output_file
*) const;
1116 // Write out a section symbol. Return the updated offset.
1118 write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1121 Symbol_table(const Symbol_table
&);
1122 Symbol_table
& operator=(const Symbol_table
&);
1124 // Make FROM a forwarder symbol to TO.
1126 make_forwarder(Symbol
* from
, Symbol
* to
);
1129 template<int size
, bool big_endian
>
1131 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
1132 const char *version
, Stringpool::Key version_key
,
1133 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
,
1134 const elfcpp::Sym
<size
, big_endian
>& orig_sym
);
1137 template<int size
, bool big_endian
>
1139 resolve(Sized_symbol
<size
>* to
,
1140 const elfcpp::Sym
<size
, big_endian
>& sym
,
1141 const elfcpp::Sym
<size
, big_endian
>& orig_sym
,
1142 Object
*, const char* version
);
1144 template<int size
, bool big_endian
>
1146 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
1147 const char* version ACCEPT_SIZE_ENDIAN
);
1149 // Whether we should override a symbol, based on flags in
1152 should_override(const Symbol
*, unsigned int, Object
*, bool*);
1154 // Override a symbol.
1155 template<int size
, bool big_endian
>
1157 override(Sized_symbol
<size
>* tosym
,
1158 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
1159 Object
* object
, const char* version
);
1161 // Whether we should override a symbol with a special symbol which
1162 // is automatically defined by the linker.
1164 should_override_with_special(const Symbol
*);
1166 // Override a symbol with a special symbol.
1169 override_with_special(Sized_symbol
<size
>* tosym
,
1170 const Sized_symbol
<size
>* fromsym
);
1172 // Record all weak alias sets for a dynamic object.
1175 record_weak_aliases(std::vector
<Sized_symbol
<size
>*>*);
1177 // Define a special symbol.
1178 template<int size
, bool big_endian
>
1180 define_special_symbol(const Target
* target
, const char** pname
,
1181 const char** pversion
, bool only_if_ref
,
1182 Sized_symbol
<size
>** poldsym ACCEPT_SIZE_ENDIAN
);
1184 // Define a symbol in an Output_data, sized version.
1187 do_define_in_output_data(const Target
*, const char* name
,
1188 const char* version
, Output_data
*,
1189 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1190 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1191 elfcpp::STT type
, elfcpp::STB binding
,
1192 elfcpp::STV visibility
, unsigned char nonvis
,
1193 bool offset_is_from_end
, bool only_if_ref
);
1195 // Define a symbol in an Output_segment, sized version.
1198 do_define_in_output_segment(
1199 const Target
*, const char* name
, const char* version
, Output_segment
* os
,
1200 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1201 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1202 elfcpp::STT type
, elfcpp::STB binding
,
1203 elfcpp::STV visibility
, unsigned char nonvis
,
1204 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
1206 // Define a symbol as a constant, sized version.
1209 do_define_as_constant(
1210 const Target
*, const char* name
, const char* version
,
1211 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1212 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1213 elfcpp::STT type
, elfcpp::STB binding
,
1214 elfcpp::STV visibility
, unsigned char nonvis
,
1217 // Allocate the common symbols, sized version.
1220 do_allocate_commons(const General_options
&, Layout
*);
1222 // Implement detect_odr_violations.
1223 template<int size
, bool big_endian
>
1225 sized_detect_odr_violations() const;
1227 // Finalize symbols specialized for size.
1230 sized_finalize(unsigned int, off_t
, Stringpool
*);
1232 // Write globals specialized for size and endianness.
1233 template<int size
, bool big_endian
>
1235 sized_write_globals(const Input_objects
*, const Stringpool
*,
1236 const Stringpool
*, Output_file
*) const;
1238 // Write out a symbol to P.
1239 template<int size
, bool big_endian
>
1241 sized_write_symbol(Sized_symbol
<size
>*,
1242 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1244 const Stringpool
*, unsigned char* p
1245 ACCEPT_SIZE_ENDIAN
) const;
1247 // Possibly warn about an undefined symbol from a dynamic object.
1249 warn_about_undefined_dynobj_symbol(const Input_objects
*, Symbol
*) const;
1251 // Write out a section symbol, specialized for size and endianness.
1252 template<int size
, bool big_endian
>
1254 sized_write_section_symbol(const Output_section
*, Output_file
*, off_t
) const;
1256 // The type of the symbol hash table.
1258 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
1260 struct Symbol_table_hash
1263 operator()(const Symbol_table_key
&) const;
1266 struct Symbol_table_eq
1269 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
1272 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
1273 Symbol_table_eq
> Symbol_table_type
;
1275 // The type of the list of common symbols.
1276 typedef std::vector
<Symbol
*> Commons_type
;
1278 // A map from symbols with COPY relocs to the dynamic objects where
1279 // they are defined.
1280 typedef Unordered_map
<const Symbol
*, Dynobj
*> Copied_symbol_dynobjs
;
1282 // A map from symbol name (as a pointer into the namepool) to all
1283 // the locations the symbols is (weakly) defined (and certain other
1284 // conditions are met). This map will be used later to detect
1285 // possible One Definition Rule (ODR) violations.
1286 struct Symbol_location
1288 Object
* object
; // Object where the symbol is defined.
1289 unsigned int shndx
; // Section-in-object where the symbol is defined.
1290 off_t offset
; // Offset-in-section where the symbol is defined.
1291 bool operator==(const Symbol_location
& that
) const
1293 return (this->object
== that
.object
1294 && this->shndx
== that
.shndx
1295 && this->offset
== that
.offset
);
1299 struct Symbol_location_hash
1301 size_t operator()(const Symbol_location
& loc
) const
1302 { return reinterpret_cast<uintptr_t>(loc
.object
) ^ loc
.offset
^ loc
.shndx
; }
1305 typedef Unordered_map
<const char*,
1306 Unordered_set
<Symbol_location
, Symbol_location_hash
> >
1309 // We increment this every time we see a new undefined symbol, for
1310 // use in archive groups.
1312 // The index of the first global symbol in the output file.
1313 unsigned int first_global_index_
;
1314 // The file offset within the output symtab section where we should
1317 // The number of global symbols we want to write out.
1318 size_t output_count_
;
1319 // The file offset of the global dynamic symbols, or 0 if none.
1320 off_t dynamic_offset_
;
1321 // The index of the first global dynamic symbol.
1322 unsigned int first_dynamic_global_index_
;
1323 // The number of global dynamic symbols, or 0 if none.
1324 off_t dynamic_count_
;
1325 // The symbol hash table.
1326 Symbol_table_type table_
;
1327 // A pool of symbol names. This is used for all global symbols.
1328 // Entries in the hash table point into this pool.
1329 Stringpool namepool_
;
1330 // Forwarding symbols.
1331 Unordered_map
<const Symbol
*, Symbol
*> forwarders_
;
1332 // Weak aliases. A symbol in this list points to the next alias.
1333 // The aliases point to each other in a circular list.
1334 Unordered_map
<Symbol
*, Symbol
*> weak_aliases_
;
1335 // We don't expect there to be very many common symbols, so we keep
1336 // a list of them. When we find a common symbol we add it to this
1337 // list. It is possible that by the time we process the list the
1338 // symbol is no longer a common symbol. It may also have become a
1340 Commons_type commons_
;
1341 // Manage symbol warnings.
1343 // Manage potential One Definition Rule (ODR) violations.
1344 Odr_map candidate_odr_violations_
;
1346 // When we emit a COPY reloc for a symbol, we define it in an
1347 // Output_data. When it's time to emit version information for it,
1348 // we need to know the dynamic object in which we found the original
1349 // definition. This maps symbols with COPY relocs to the dynamic
1350 // object where they were defined.
1351 Copied_symbol_dynobjs copied_symbol_dynobjs_
;
1354 // We inline get_sized_symbol for efficiency.
1358 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
1360 gold_assert(size
== parameters
->get_size());
1361 return static_cast<Sized_symbol
<size
>*>(sym
);
1365 const Sized_symbol
<size
>*
1366 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
1368 gold_assert(size
== parameters
->get_size());
1369 return static_cast<const Sized_symbol
<size
>*>(sym
);
1372 } // End namespace gold.
1374 #endif // !defined(GOLD_SYMTAB_H)