1 // symtab.h -- the gold symbol table -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "parameters.h"
32 #include "stringpool.h"
44 template<int size
, bool big_endian
>
46 template<int size
, bool big_endian
>
47 class Sized_pluginobj
;
49 template<int size
, bool big_endian
>
52 class Version_script_info
;
58 class Output_symtab_xindex
;
60 // The base class of an entry in the symbol table. The symbol table
61 // can have a lot of entries, so we don't want this class to big.
62 // Size dependent fields can be found in the template class
63 // Sized_symbol. Targets may support their own derived classes.
68 // Because we want the class to be small, we don't use any virtual
69 // functions. But because symbols can be defined in different
70 // places, we need to classify them. This enum is the different
71 // sources of symbols we support.
74 // Symbol defined in a relocatable or dynamic input file--this is
75 // the most common case.
77 // Symbol defined in an Output_data, a special section created by
80 // Symbol defined in an Output_segment, with no associated
83 // Symbol value is constant.
85 // Symbol is undefined.
89 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
91 enum Segment_offset_base
93 // From the start of the segment.
95 // From the end of the segment.
97 // From the filesz of the segment--i.e., after the loaded bytes
98 // but before the bytes which are allocated but zeroed.
102 // Return the symbol name.
105 { return this->name_
; }
107 // Return the (ANSI) demangled version of the name, if
108 // parameters.demangle() is true. Otherwise, return the name. This
109 // is intended to be used only for logging errors, so it's not
112 demangled_name() const;
114 // Return the symbol version. This will return NULL for an
115 // unversioned symbol.
118 { return this->version_
; }
120 // Return whether this version is the default for this symbol name
121 // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
122 // meaningful for versioned symbols.
126 gold_assert(this->version_
!= NULL
);
127 return this->is_def_
;
130 // Set that this version is the default for this symbol name.
133 { this->is_def_
= true; }
135 // Return the symbol source.
138 { return this->source_
; }
140 // Return the object with which this symbol is associated.
144 gold_assert(this->source_
== FROM_OBJECT
);
145 return this->u_
.from_object
.object
;
148 // Return the index of the section in the input relocatable or
149 // dynamic object file.
151 shndx(bool* is_ordinary
) const
153 gold_assert(this->source_
== FROM_OBJECT
);
154 *is_ordinary
= this->is_ordinary_shndx_
;
155 return this->u_
.from_object
.shndx
;
158 // Return the output data section with which this symbol is
159 // associated, if the symbol was specially defined with respect to
160 // an output data section.
164 gold_assert(this->source_
== IN_OUTPUT_DATA
);
165 return this->u_
.in_output_data
.output_data
;
168 // If this symbol was defined with respect to an output data
169 // section, return whether the value is an offset from end.
171 offset_is_from_end() const
173 gold_assert(this->source_
== IN_OUTPUT_DATA
);
174 return this->u_
.in_output_data
.offset_is_from_end
;
177 // Return the output segment with which this symbol is associated,
178 // if the symbol was specially defined with respect to an output
181 output_segment() const
183 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
184 return this->u_
.in_output_segment
.output_segment
;
187 // If this symbol was defined with respect to an output segment,
188 // return the offset base.
192 gold_assert(this->source_
== IN_OUTPUT_SEGMENT
);
193 return this->u_
.in_output_segment
.offset_base
;
196 // Return the symbol binding.
199 { return this->binding_
; }
201 // Return the symbol type.
204 { return this->type_
; }
206 // Return the symbol visibility.
209 { return this->visibility_
; }
211 // Return the non-visibility part of the st_other field.
214 { return this->nonvis_
; }
216 // Return whether this symbol is a forwarder. This will never be
217 // true of a symbol found in the hash table, but may be true of
218 // symbol pointers attached to object files.
221 { return this->is_forwarder_
; }
223 // Mark this symbol as a forwarder.
226 { this->is_forwarder_
= true; }
228 // Return whether this symbol has an alias in the weak aliases table
232 { return this->has_alias_
; }
234 // Mark this symbol as having an alias.
237 { this->has_alias_
= true; }
239 // Return whether this symbol needs an entry in the dynamic symbol
242 needs_dynsym_entry() const
244 return (this->needs_dynsym_entry_
245 || (this->in_reg() && this->in_dyn()));
248 // Mark this symbol as needing an entry in the dynamic symbol table.
250 set_needs_dynsym_entry()
251 { this->needs_dynsym_entry_
= true; }
253 // Return whether this symbol should be added to the dynamic symbol
256 should_add_dynsym_entry() const;
258 // Return whether this symbol has been seen in a regular object.
261 { return this->in_reg_
; }
263 // Mark this symbol as having been seen in a regular object.
266 { this->in_reg_
= true; }
268 // Return whether this symbol has been seen in a dynamic object.
271 { return this->in_dyn_
; }
273 // Mark this symbol as having been seen in a dynamic object.
276 { this->in_dyn_
= true; }
278 // Return whether this symbol has been seen in a real ELF object.
279 // (IN_REG will return TRUE if the symbol has been seen in either
280 // a real ELF object or an object claimed by a plugin.)
283 { return this->in_real_elf_
; }
285 // Mark this symbol as having been seen in a real ELF object.
288 { this->in_real_elf_
= true; }
290 // Return the index of this symbol in the output file symbol table.
291 // A value of -1U means that this symbol is not going into the
292 // output file. This starts out as zero, and is set to a non-zero
293 // value by Symbol_table::finalize. It is an error to ask for the
294 // symbol table index before it has been set.
298 gold_assert(this->symtab_index_
!= 0);
299 return this->symtab_index_
;
302 // Set the index of the symbol in the output file symbol table.
304 set_symtab_index(unsigned int index
)
306 gold_assert(index
!= 0);
307 this->symtab_index_
= index
;
310 // Return whether this symbol already has an index in the output
311 // file symbol table.
313 has_symtab_index() const
314 { return this->symtab_index_
!= 0; }
316 // Return the index of this symbol in the dynamic symbol table. A
317 // value of -1U means that this symbol is not going into the dynamic
318 // symbol table. This starts out as zero, and is set to a non-zero
319 // during Layout::finalize. It is an error to ask for the dynamic
320 // symbol table index before it has been set.
324 gold_assert(this->dynsym_index_
!= 0);
325 return this->dynsym_index_
;
328 // Set the index of the symbol in the dynamic symbol table.
330 set_dynsym_index(unsigned int index
)
332 gold_assert(index
!= 0);
333 this->dynsym_index_
= index
;
336 // Return whether this symbol already has an index in the dynamic
339 has_dynsym_index() const
340 { return this->dynsym_index_
!= 0; }
342 // Return whether this symbol has an entry in the GOT section.
343 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
345 has_got_offset(unsigned int got_type
) const
346 { return this->got_offsets_
.get_offset(got_type
) != -1U; }
348 // Return the offset into the GOT section of this symbol.
350 got_offset(unsigned int got_type
) const
352 unsigned int got_offset
= this->got_offsets_
.get_offset(got_type
);
353 gold_assert(got_offset
!= -1U);
357 // Set the GOT offset of this symbol.
359 set_got_offset(unsigned int got_type
, unsigned int got_offset
)
360 { this->got_offsets_
.set_offset(got_type
, got_offset
); }
362 // Return whether this symbol has an entry in the PLT section.
364 has_plt_offset() const
365 { return this->has_plt_offset_
; }
367 // Return the offset into the PLT section of this symbol.
371 gold_assert(this->has_plt_offset());
372 return this->plt_offset_
;
375 // Set the PLT offset of this symbol.
377 set_plt_offset(unsigned int plt_offset
)
379 this->has_plt_offset_
= true;
380 this->plt_offset_
= plt_offset
;
383 // Return whether this dynamic symbol needs a special value in the
384 // dynamic symbol table.
386 needs_dynsym_value() const
387 { return this->needs_dynsym_value_
; }
389 // Set that this dynamic symbol needs a special value in the dynamic
392 set_needs_dynsym_value()
394 gold_assert(this->object()->is_dynamic());
395 this->needs_dynsym_value_
= true;
398 // Return true if the final value of this symbol is known at link
401 final_value_is_known() const;
403 // Return whether this is a defined symbol (not undefined or
409 if (this->source_
!= FROM_OBJECT
)
410 return this->source_
!= IS_UNDEFINED
;
411 unsigned int shndx
= this->shndx(&is_ordinary
);
413 ? shndx
!= elfcpp::SHN_UNDEF
414 : shndx
!= elfcpp::SHN_COMMON
);
417 // Return true if this symbol is from a dynamic object.
419 is_from_dynobj() const
421 return this->source_
== FROM_OBJECT
&& this->object()->is_dynamic();
424 // Return whether this is an undefined symbol.
429 return ((this->source_
== FROM_OBJECT
430 && this->shndx(&is_ordinary
) == elfcpp::SHN_UNDEF
432 || this->source_
== IS_UNDEFINED
);
435 // Return whether this is a weak undefined symbol.
437 is_weak_undefined() const
438 { return this->is_undefined() && this->binding() == elfcpp::STB_WEAK
; }
440 // Return whether this is an absolute symbol.
445 return ((this->source_
== FROM_OBJECT
446 && this->shndx(&is_ordinary
) == elfcpp::SHN_ABS
448 || this->source_
== IS_CONSTANT
);
451 // Return whether this is a common symbol.
456 return (this->source_
== FROM_OBJECT
457 && ((this->shndx(&is_ordinary
) == elfcpp::SHN_COMMON
459 || this->type_
== elfcpp::STT_COMMON
));
462 // Return whether this symbol can be seen outside this object.
464 is_externally_visible() const
466 return (this->visibility_
== elfcpp::STV_DEFAULT
467 || this->visibility_
== elfcpp::STV_PROTECTED
);
470 // Return true if this symbol can be preempted by a definition in
471 // another link unit.
473 is_preemptible() const
475 // It doesn't make sense to ask whether a symbol defined in
476 // another object is preemptible.
477 gold_assert(!this->is_from_dynobj());
479 // It doesn't make sense to ask whether an undefined symbol
481 gold_assert(!this->is_undefined());
483 // If a symbol does not have default visibility, it can not be
484 // seen outside this link unit and therefore is not preemptible.
485 if (this->visibility_
!= elfcpp::STV_DEFAULT
)
488 // If this symbol has been forced to be a local symbol by a
489 // version script, then it is not visible outside this link unit
490 // and is not preemptible.
491 if (this->is_forced_local_
)
494 // If we are not producing a shared library, then nothing is
496 if (!parameters
->options().shared())
499 // If the user used -Bsymbolic, then nothing is preemptible.
500 if (parameters
->options().Bsymbolic())
503 // If the user used -Bsymbolic-functions, then functions are not
504 // preemptible. We explicitly check for not being STT_OBJECT,
505 // rather than for being STT_FUNC, because that is what the GNU
507 if (this->type() != elfcpp::STT_OBJECT
508 && parameters
->options().Bsymbolic_functions())
511 // Otherwise the symbol is preemptible.
515 // Return true if this symbol is a function that needs a PLT entry.
516 // If the symbol is defined in a dynamic object or if it is subject
517 // to pre-emption, we need to make a PLT entry. If we're doing a
518 // static link, we don't create PLT entries.
520 needs_plt_entry() const
522 return (!parameters
->doing_static_link()
523 && this->type() == elfcpp::STT_FUNC
524 && (this->is_from_dynobj()
525 || this->is_undefined()
526 || this->is_preemptible()));
529 // When determining whether a reference to a symbol needs a dynamic
530 // relocation, we need to know several things about the reference.
531 // These flags may be or'ed together.
534 // Reference to the symbol's absolute address.
536 // A non-PIC reference.
542 // Given a direct absolute or pc-relative static relocation against
543 // the global symbol, this function returns whether a dynamic relocation
547 needs_dynamic_reloc(int flags
) const
549 // No dynamic relocations in a static link!
550 if (parameters
->doing_static_link())
553 // A reference to a weak undefined symbol from an executable should be
554 // statically resolved to 0, and does not need a dynamic relocation.
555 // This matches gnu ld behavior.
556 if (this->is_weak_undefined() && !parameters
->options().shared())
559 // A reference to an absolute symbol does not need a dynamic relocation.
560 if (this->is_absolute())
563 // An absolute reference within a position-independent output file
564 // will need a dynamic relocation.
565 if ((flags
& ABSOLUTE_REF
)
566 && parameters
->options().output_is_position_independent())
569 // A function call that can branch to a local PLT entry does not need
570 // a dynamic relocation. A non-pic pc-relative function call in a
571 // shared library cannot use a PLT entry.
572 if ((flags
& FUNCTION_CALL
)
573 && this->has_plt_offset()
574 && !((flags
& NON_PIC_REF
) && parameters
->options().shared()))
577 // A reference to any PLT entry in a non-position-independent executable
578 // does not need a dynamic relocation.
579 if (!parameters
->options().output_is_position_independent()
580 && this->has_plt_offset())
583 // A reference to a symbol defined in a dynamic object or to a
584 // symbol that is preemptible will need a dynamic relocation.
585 if (this->is_from_dynobj()
586 || this->is_undefined()
587 || this->is_preemptible())
590 // For all other cases, return FALSE.
594 // Whether we should use the PLT offset associated with a symbol for
595 // a relocation. IS_NON_PIC_REFERENCE is true if this is a non-PIC
596 // reloc--the same set of relocs for which we would pass NON_PIC_REF
597 // to the needs_dynamic_reloc function.
600 use_plt_offset(bool is_non_pic_reference
) const
602 // If the symbol doesn't have a PLT offset, then naturally we
603 // don't want to use it.
604 if (!this->has_plt_offset())
607 // If we are going to generate a dynamic relocation, then we will
608 // wind up using that, so no need to use the PLT entry.
609 if (this->needs_dynamic_reloc(FUNCTION_CALL
610 | (is_non_pic_reference
615 // If the symbol is from a dynamic object, we need to use the PLT
617 if (this->is_from_dynobj())
620 // If we are generating a shared object, and this symbol is
621 // undefined or preemptible, we need to use the PLT entry.
622 if (parameters
->options().shared()
623 && (this->is_undefined() || this->is_preemptible()))
626 // If this is a weak undefined symbol, we need to use the PLT
627 // entry; the symbol may be defined by a library loaded at
629 if (this->is_weak_undefined())
632 // Otherwise we can use the regular definition.
636 // Given a direct absolute static relocation against
637 // the global symbol, where a dynamic relocation is needed, this
638 // function returns whether a relative dynamic relocation can be used.
639 // The caller must determine separately whether the static relocation
640 // is compatible with a relative relocation.
643 can_use_relative_reloc(bool is_function_call
) const
645 // A function call that can branch to a local PLT entry can
646 // use a RELATIVE relocation.
647 if (is_function_call
&& this->has_plt_offset())
650 // A reference to a symbol defined in a dynamic object or to a
651 // symbol that is preemptible can not use a RELATIVE relocaiton.
652 if (this->is_from_dynobj()
653 || this->is_undefined()
654 || this->is_preemptible())
657 // For all other cases, return TRUE.
661 // Return the output section where this symbol is defined. Return
662 // NULL if the symbol has an absolute value.
664 output_section() const;
666 // Set the symbol's output section. This is used for symbols
667 // defined in scripts. This should only be called after the symbol
668 // table has been finalized.
670 set_output_section(Output_section
*);
672 // Return whether there should be a warning for references to this
676 { return this->has_warning_
; }
678 // Mark this symbol as having a warning.
681 { this->has_warning_
= true; }
683 // Return whether this symbol is defined by a COPY reloc from a
686 is_copied_from_dynobj() const
687 { return this->is_copied_from_dynobj_
; }
689 // Mark this symbol as defined by a COPY reloc.
691 set_is_copied_from_dynobj()
692 { this->is_copied_from_dynobj_
= true; }
694 // Return whether this symbol is forced to visibility STB_LOCAL
695 // by a "local:" entry in a version script.
697 is_forced_local() const
698 { return this->is_forced_local_
; }
700 // Mark this symbol as forced to STB_LOCAL visibility.
702 set_is_forced_local()
703 { this->is_forced_local_
= true; }
706 // Instances of this class should always be created at a specific
709 { memset(this, 0, sizeof *this); }
711 // Initialize the general fields.
713 init_fields(const char* name
, const char* version
,
714 elfcpp::STT type
, elfcpp::STB binding
,
715 elfcpp::STV visibility
, unsigned char nonvis
);
717 // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
718 // section index, IS_ORDINARY is whether it is a normal section
719 // index rather than a special code.
720 template<int size
, bool big_endian
>
722 init_base_object(const char *name
, const char* version
, Object
* object
,
723 const elfcpp::Sym
<size
, big_endian
>&, unsigned int st_shndx
,
726 // Initialize fields for an Output_data.
728 init_base_output_data(const char* name
, const char* version
, Output_data
*,
729 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
,
730 unsigned char nonvis
, bool offset_is_from_end
);
732 // Initialize fields for an Output_segment.
734 init_base_output_segment(const char* name
, const char* version
,
735 Output_segment
* os
, elfcpp::STT type
,
736 elfcpp::STB binding
, elfcpp::STV visibility
,
737 unsigned char nonvis
,
738 Segment_offset_base offset_base
);
740 // Initialize fields for a constant.
742 init_base_constant(const char* name
, const char* version
, elfcpp::STT type
,
743 elfcpp::STB binding
, elfcpp::STV visibility
,
744 unsigned char nonvis
);
746 // Initialize fields for an undefined symbol.
748 init_base_undefined(const char* name
, const char* version
, elfcpp::STT type
,
749 elfcpp::STB binding
, elfcpp::STV visibility
,
750 unsigned char nonvis
);
752 // Override existing symbol.
753 template<int size
, bool big_endian
>
755 override_base(const elfcpp::Sym
<size
, big_endian
>&, unsigned int st_shndx
,
756 bool is_ordinary
, Object
* object
, const char* version
);
758 // Override existing symbol with a special symbol.
760 override_base_with_special(const Symbol
* from
);
762 // Override symbol version.
764 override_version(const char* version
);
766 // Allocate a common symbol by giving it a location in the output
769 allocate_base_common(Output_data
*);
772 Symbol(const Symbol
&);
773 Symbol
& operator=(const Symbol
&);
775 // Symbol name (expected to point into a Stringpool).
777 // Symbol version (expected to point into a Stringpool). This may
779 const char* version_
;
783 // This struct is used if SOURCE_ == FROM_OBJECT.
786 // Object in which symbol is defined, or in which it was first
789 // Section number in object_ in which symbol is defined.
793 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
796 // Output_data in which symbol is defined. Before
797 // Layout::finalize the symbol's value is an offset within the
799 Output_data
* output_data
;
800 // True if the offset is from the end, false if the offset is
801 // from the beginning.
802 bool offset_is_from_end
;
805 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
808 // Output_segment in which the symbol is defined. Before
809 // Layout::finalize the symbol's value is an offset.
810 Output_segment
* output_segment
;
811 // The base to use for the offset before Layout::finalize.
812 Segment_offset_base offset_base
;
816 // The index of this symbol in the output file. If the symbol is
817 // not going into the output file, this value is -1U. This field
818 // starts as always holding zero. It is set to a non-zero value by
819 // Symbol_table::finalize.
820 unsigned int symtab_index_
;
822 // The index of this symbol in the dynamic symbol table. If the
823 // symbol is not going into the dynamic symbol table, this value is
824 // -1U. This field starts as always holding zero. It is set to a
825 // non-zero value during Layout::finalize.
826 unsigned int dynsym_index_
;
828 // If this symbol has an entry in the GOT section (has_got_offset_
829 // is true), this holds the offset from the start of the GOT section.
830 // A symbol may have more than one GOT offset (e.g., when mixing
831 // modules compiled with two different TLS models), but will usually
833 Got_offset_list got_offsets_
;
835 // If this symbol has an entry in the PLT section (has_plt_offset_
836 // is true), then this is the offset from the start of the PLT
838 unsigned int plt_offset_
;
840 // Symbol type (bits 0 to 3).
841 elfcpp::STT type_
: 4;
842 // Symbol binding (bits 4 to 7).
843 elfcpp::STB binding_
: 4;
844 // Symbol visibility (bits 8 to 9).
845 elfcpp::STV visibility_
: 2;
846 // Rest of symbol st_other field (bits 10 to 15).
847 unsigned int nonvis_
: 6;
848 // The type of symbol (bits 16 to 18).
850 // True if this symbol always requires special target-specific
851 // handling (bit 19).
852 bool is_target_special_
: 1;
853 // True if this is the default version of the symbol (bit 20).
855 // True if this symbol really forwards to another symbol. This is
856 // used when we discover after the fact that two different entries
857 // in the hash table really refer to the same symbol. This will
858 // never be set for a symbol found in the hash table, but may be set
859 // for a symbol found in the list of symbols attached to an Object.
860 // It forwards to the symbol found in the forwarders_ map of
861 // Symbol_table (bit 21).
862 bool is_forwarder_
: 1;
863 // True if the symbol has an alias in the weak_aliases table in
864 // Symbol_table (bit 22).
866 // True if this symbol needs to be in the dynamic symbol table (bit
868 bool needs_dynsym_entry_
: 1;
869 // True if we've seen this symbol in a regular object (bit 24).
871 // True if we've seen this symbol in a dynamic object (bit 25).
873 // True if the symbol has an entry in the PLT section (bit 26).
874 bool has_plt_offset_
: 1;
875 // True if this is a dynamic symbol which needs a special value in
876 // the dynamic symbol table (bit 27).
877 bool needs_dynsym_value_
: 1;
878 // True if there is a warning for this symbol (bit 28).
879 bool has_warning_
: 1;
880 // True if we are using a COPY reloc for this symbol, so that the
881 // real definition lives in a dynamic object (bit 29).
882 bool is_copied_from_dynobj_
: 1;
883 // True if this symbol was forced to local visibility by a version
885 bool is_forced_local_
: 1;
886 // True if the field u_.from_object.shndx is an ordinary section
887 // index, not one of the special codes from SHN_LORESERVE to
888 // SHN_HIRESERVE (bit 31).
889 bool is_ordinary_shndx_
: 1;
890 // True if we've seen this symbol in a real ELF object.
891 bool in_real_elf_
: 1;
894 // The parts of a symbol which are size specific. Using a template
895 // derived class like this helps us use less space on a 32-bit system.
898 class Sized_symbol
: public Symbol
901 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
902 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
907 // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
908 // section index, IS_ORDINARY is whether it is a normal section
909 // index rather than a special code.
910 template<bool big_endian
>
912 init_object(const char *name
, const char* version
, Object
* object
,
913 const elfcpp::Sym
<size
, big_endian
>&, unsigned int st_shndx
,
916 // Initialize fields for an Output_data.
918 init_output_data(const char* name
, const char* version
, Output_data
*,
919 Value_type value
, Size_type symsize
, elfcpp::STT
,
920 elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
921 bool offset_is_from_end
);
923 // Initialize fields for an Output_segment.
925 init_output_segment(const char* name
, const char* version
, Output_segment
*,
926 Value_type value
, Size_type symsize
, elfcpp::STT
,
927 elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
928 Segment_offset_base offset_base
);
930 // Initialize fields for a constant.
932 init_constant(const char* name
, const char* version
, Value_type value
,
933 Size_type symsize
, elfcpp::STT
, elfcpp::STB
, elfcpp::STV
,
934 unsigned char nonvis
);
936 // Initialize fields for an undefined symbol.
938 init_undefined(const char* name
, const char* version
, elfcpp::STT
,
939 elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
941 // Override existing symbol.
942 template<bool big_endian
>
944 override(const elfcpp::Sym
<size
, big_endian
>&, unsigned int st_shndx
,
945 bool is_ordinary
, Object
* object
, const char* version
);
947 // Override existing symbol with a special symbol.
949 override_with_special(const Sized_symbol
<size
>*);
951 // Return the symbol's value.
954 { return this->value_
; }
956 // Return the symbol's size (we can't call this 'size' because that
957 // is a template parameter).
960 { return this->symsize_
; }
962 // Set the symbol size. This is used when resolving common symbols.
964 set_symsize(Size_type symsize
)
965 { this->symsize_
= symsize
; }
967 // Set the symbol value. This is called when we store the final
968 // values of the symbols into the symbol table.
970 set_value(Value_type value
)
971 { this->value_
= value
; }
973 // Allocate a common symbol by giving it a location in the output
976 allocate_common(Output_data
*, Value_type value
);
979 Sized_symbol(const Sized_symbol
&);
980 Sized_symbol
& operator=(const Sized_symbol
&);
982 // Symbol value. Before Layout::finalize this is the offset in the
983 // input section. This is set to the final value during
990 // A struct describing a symbol defined by the linker, where the value
991 // of the symbol is defined based on an output section. This is used
992 // for symbols defined by the linker, like "_init_array_start".
994 struct Define_symbol_in_section
998 // The name of the output section with which this symbol should be
999 // associated. If there is no output section with that name, the
1000 // symbol will be defined as zero.
1001 const char* output_section
;
1002 // The offset of the symbol within the output section. This is an
1003 // offset from the start of the output section, unless start_at_end
1004 // is true, in which case this is an offset from the end of the
1007 // The size of the symbol.
1011 // The symbol binding.
1012 elfcpp::STB binding
;
1013 // The symbol visibility.
1014 elfcpp::STV visibility
;
1015 // The rest of the st_other field.
1016 unsigned char nonvis
;
1017 // If true, the value field is an offset from the end of the output
1019 bool offset_is_from_end
;
1020 // If true, this symbol is defined only if we see a reference to it.
1024 // A struct describing a symbol defined by the linker, where the value
1025 // of the symbol is defined based on a segment. This is used for
1026 // symbols defined by the linker, like "_end". We describe the
1027 // segment with which the symbol should be associated by its
1028 // characteristics. If no segment meets these characteristics, the
1029 // symbol will be defined as zero. If there is more than one segment
1030 // which meets these characteristics, we will use the first one.
1032 struct Define_symbol_in_segment
1036 // The segment type where the symbol should be defined, typically
1038 elfcpp::PT segment_type
;
1039 // Bitmask of segment flags which must be set.
1040 elfcpp::PF segment_flags_set
;
1041 // Bitmask of segment flags which must be clear.
1042 elfcpp::PF segment_flags_clear
;
1043 // The offset of the symbol within the segment. The offset is
1044 // calculated from the position set by offset_base.
1046 // The size of the symbol.
1050 // The symbol binding.
1051 elfcpp::STB binding
;
1052 // The symbol visibility.
1053 elfcpp::STV visibility
;
1054 // The rest of the st_other field.
1055 unsigned char nonvis
;
1056 // The base from which we compute the offset.
1057 Symbol::Segment_offset_base offset_base
;
1058 // If true, this symbol is defined only if we see a reference to it.
1062 // This class manages warnings. Warnings are a GNU extension. When
1063 // we see a section named .gnu.warning.SYM in an object file, and if
1064 // we wind using the definition of SYM from that object file, then we
1065 // will issue a warning for any relocation against SYM from a
1066 // different object file. The text of the warning is the contents of
1067 // the section. This is not precisely the definition used by the old
1068 // GNU linker; the old GNU linker treated an occurrence of
1069 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
1070 // would trigger a warning on any reference. However, it was
1071 // inconsistent in that a warning in a dynamic object only triggered
1072 // if there was no definition in a regular object. This linker is
1073 // different in that we only issue a warning if we use the symbol
1074 // definition from the same object file as the warning section.
1083 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1086 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1087 const std::string
& warning
);
1089 // For each symbol for which we should give a warning, make a note
1092 note_warnings(Symbol_table
* symtab
);
1094 // Issue a warning for a reference to SYM at RELINFO's location.
1095 template<int size
, bool big_endian
>
1097 issue_warning(const Symbol
* sym
, const Relocate_info
<size
, big_endian
>*,
1098 size_t relnum
, off_t reloffset
) const;
1101 Warnings(const Warnings
&);
1102 Warnings
& operator=(const Warnings
&);
1104 // What we need to know to get the warning text.
1105 struct Warning_location
1107 // The object the warning is in.
1109 // The warning text.
1113 : object(NULL
), text()
1117 set(Object
* o
, const std::string
& t
)
1124 // A mapping from warning symbol names (canonicalized in
1125 // Symbol_table's namepool_ field) to warning information.
1126 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
1128 Warning_table warnings_
;
1131 // The main linker symbol table.
1136 // COUNT is an estimate of how many symbosl will be inserted in the
1137 // symbol table. It's ok to put 0 if you don't know; a correct
1138 // guess will just save some CPU by reducing hashtable resizes.
1139 Symbol_table(unsigned int count
, const Version_script_info
& version_script
);
1143 // Add COUNT external symbols from the relocatable object RELOBJ to
1144 // the symbol table. SYMS is the symbols, SYMNDX_OFFSET is the
1145 // offset in the symbol table of the first symbol, SYM_NAMES is
1146 // their names, SYM_NAME_SIZE is the size of SYM_NAMES. This sets
1147 // SYMPOINTERS to point to the symbols in the symbol table. It sets
1148 // *DEFINED to the number of defined symbols.
1149 template<int size
, bool big_endian
>
1151 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
1152 const unsigned char* syms
, size_t count
,
1153 size_t symndx_offset
, const char* sym_names
,
1154 size_t sym_name_size
,
1155 typename Sized_relobj
<size
, big_endian
>::Symbols
*,
1158 // Add one external symbol from the plugin object OBJ to the symbol table.
1159 // Returns a pointer to the resolved symbol in the symbol table.
1160 template<int size
, bool big_endian
>
1162 add_from_pluginobj(Sized_pluginobj
<size
, big_endian
>* obj
,
1163 const char* name
, const char* ver
,
1164 elfcpp::Sym
<size
, big_endian
>* sym
);
1166 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1167 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1168 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1169 // symbol version data.
1170 template<int size
, bool big_endian
>
1172 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
1173 const unsigned char* syms
, size_t count
,
1174 const char* sym_names
, size_t sym_name_size
,
1175 const unsigned char* versym
, size_t versym_size
,
1176 const std::vector
<const char*>*,
1177 typename Sized_relobj
<size
, big_endian
>::Symbols
*,
1180 // Define a special symbol based on an Output_data. It is a
1181 // multiple definition error if this symbol is already defined.
1183 define_in_output_data(const char* name
, const char* version
,
1184 Output_data
*, uint64_t value
, uint64_t symsize
,
1185 elfcpp::STT type
, elfcpp::STB binding
,
1186 elfcpp::STV visibility
, unsigned char nonvis
,
1187 bool offset_is_from_end
, bool only_if_ref
);
1189 // Define a special symbol based on an Output_segment. It is a
1190 // multiple definition error if this symbol is already defined.
1192 define_in_output_segment(const char* name
, const char* version
,
1193 Output_segment
*, uint64_t value
, uint64_t symsize
,
1194 elfcpp::STT type
, elfcpp::STB binding
,
1195 elfcpp::STV visibility
, unsigned char nonvis
,
1196 Symbol::Segment_offset_base
, bool only_if_ref
);
1198 // Define a special symbol with a constant value. It is a multiple
1199 // definition error if this symbol is already defined.
1201 define_as_constant(const char* name
, const char* version
,
1202 uint64_t value
, uint64_t symsize
, elfcpp::STT type
,
1203 elfcpp::STB binding
, elfcpp::STV visibility
,
1204 unsigned char nonvis
, bool only_if_ref
,
1205 bool force_override
);
1207 // Define a set of symbols in output sections. If ONLY_IF_REF is
1208 // true, only define them if they are referenced.
1210 define_symbols(const Layout
*, int count
, const Define_symbol_in_section
*,
1213 // Define a set of symbols in output segments. If ONLY_IF_REF is
1214 // true, only defined them if they are referenced.
1216 define_symbols(const Layout
*, int count
, const Define_symbol_in_segment
*,
1219 // Define SYM using a COPY reloc. POSD is the Output_data where the
1220 // symbol should be defined--typically a .dyn.bss section. VALUE is
1221 // the offset within POSD.
1224 define_with_copy_reloc(Sized_symbol
<size
>* sym
, Output_data
* posd
,
1225 typename
elfcpp::Elf_types
<size
>::Elf_Addr
);
1227 // Look up a symbol.
1229 lookup(const char*, const char* version
= NULL
) const;
1231 // Return the real symbol associated with the forwarder symbol FROM.
1233 resolve_forwards(const Symbol
* from
) const;
1235 // Return the sized version of a symbol in this table.
1238 get_sized_symbol(Symbol
*) const;
1241 const Sized_symbol
<size
>*
1242 get_sized_symbol(const Symbol
*) const;
1244 // Return the count of undefined symbols seen.
1246 saw_undefined() const
1247 { return this->saw_undefined_
; }
1249 // Allocate the common symbols
1251 allocate_commons(Layout
*, Mapfile
*);
1253 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1256 add_warning(const char* name
, Object
* obj
, const std::string
& warning
)
1257 { this->warnings_
.add_warning(this, name
, obj
, warning
); }
1259 // Canonicalize a symbol name for use in the hash table.
1261 canonicalize_name(const char* name
)
1262 { return this->namepool_
.add(name
, true, NULL
); }
1264 // Possibly issue a warning for a reference to SYM at LOCATION which
1266 template<int size
, bool big_endian
>
1268 issue_warning(const Symbol
* sym
,
1269 const Relocate_info
<size
, big_endian
>* relinfo
,
1270 size_t relnum
, off_t reloffset
) const
1271 { this->warnings_
.issue_warning(sym
, relinfo
, relnum
, reloffset
); }
1273 // Check candidate_odr_violations_ to find symbols with the same name
1274 // but apparently different definitions (different source-file/line-no).
1276 detect_odr_violations(const Task
*, const char* output_file_name
) const;
1278 // Add any undefined symbols named on the command line to the symbol
1281 add_undefined_symbols_from_command_line();
1283 // SYM is defined using a COPY reloc. Return the dynamic object
1284 // where the original definition was found.
1286 get_copy_source(const Symbol
* sym
) const;
1288 // Set the dynamic symbol indexes. INDEX is the index of the first
1289 // global dynamic symbol. Pointers to the symbols are stored into
1290 // the vector. The names are stored into the Stringpool. This
1291 // returns an updated dynamic symbol index.
1293 set_dynsym_indexes(unsigned int index
, std::vector
<Symbol
*>*,
1294 Stringpool
*, Versions
*);
1296 // Finalize the symbol table after we have set the final addresses
1297 // of all the input sections. This sets the final symbol indexes,
1298 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1299 // index of the first global symbol. OFF is the file offset of the
1300 // global symbol table, DYNOFF is the offset of the globals in the
1301 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1302 // global dynamic symbol, and DYNCOUNT is the number of global
1303 // dynamic symbols. This records the parameters, and returns the
1304 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1307 finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
, size_t dyncount
,
1308 Stringpool
* pool
, unsigned int *plocal_symcount
);
1310 // Write out the global symbols.
1312 write_globals(const Input_objects
*, const Stringpool
*, const Stringpool
*,
1313 Output_symtab_xindex
*, Output_symtab_xindex
*,
1314 Output_file
*) const;
1316 // Write out a section symbol. Return the updated offset.
1318 write_section_symbol(const Output_section
*, Output_symtab_xindex
*,
1319 Output_file
*, off_t
) const;
1321 // Dump statistical information to stderr.
1323 print_stats() const;
1325 // Return the version script information.
1326 const Version_script_info
&
1327 version_script() const
1328 { return version_script_
; }
1331 Symbol_table(const Symbol_table
&);
1332 Symbol_table
& operator=(const Symbol_table
&);
1334 // The type of the list of common symbols.
1335 typedef std::vector
<Symbol
*> Commons_type
;
1337 // Make FROM a forwarder symbol to TO.
1339 make_forwarder(Symbol
* from
, Symbol
* to
);
1342 template<int size
, bool big_endian
>
1344 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
1345 const char *version
, Stringpool::Key version_key
,
1346 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
,
1347 unsigned int st_shndx
, bool is_ordinary
,
1348 unsigned int orig_st_shndx
);
1351 template<int size
, bool big_endian
>
1353 resolve(Sized_symbol
<size
>* to
,
1354 const elfcpp::Sym
<size
, big_endian
>& sym
,
1355 unsigned int st_shndx
, bool is_ordinary
,
1356 unsigned int orig_st_shndx
,
1357 Object
*, const char* version
);
1359 template<int size
, bool big_endian
>
1361 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
);
1363 // Record that a symbol is forced to be local by a version script.
1365 force_local(Symbol
*);
1367 // Adjust NAME and *NAME_KEY for wrapping.
1369 wrap_symbol(Object
* object
, const char*, Stringpool::Key
* name_key
);
1371 // Whether we should override a symbol, based on flags in
1374 should_override(const Symbol
*, unsigned int, Object
*, bool*);
1376 // Override a symbol.
1377 template<int size
, bool big_endian
>
1379 override(Sized_symbol
<size
>* tosym
,
1380 const elfcpp::Sym
<size
, big_endian
>& fromsym
,
1381 unsigned int st_shndx
, bool is_ordinary
,
1382 Object
* object
, const char* version
);
1384 // Whether we should override a symbol with a special symbol which
1385 // is automatically defined by the linker.
1387 should_override_with_special(const Symbol
*);
1389 // Override a symbol with a special symbol.
1392 override_with_special(Sized_symbol
<size
>* tosym
,
1393 const Sized_symbol
<size
>* fromsym
);
1395 // Record all weak alias sets for a dynamic object.
1398 record_weak_aliases(std::vector
<Sized_symbol
<size
>*>*);
1400 // Define a special symbol.
1401 template<int size
, bool big_endian
>
1403 define_special_symbol(const char** pname
, const char** pversion
,
1404 bool only_if_ref
, Sized_symbol
<size
>** poldsym
);
1406 // Define a symbol in an Output_data, sized version.
1409 do_define_in_output_data(const char* name
, const char* version
, Output_data
*,
1410 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1411 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1412 elfcpp::STT type
, elfcpp::STB binding
,
1413 elfcpp::STV visibility
, unsigned char nonvis
,
1414 bool offset_is_from_end
, bool only_if_ref
);
1416 // Define a symbol in an Output_segment, sized version.
1419 do_define_in_output_segment(
1420 const char* name
, const char* version
, Output_segment
* os
,
1421 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1422 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1423 elfcpp::STT type
, elfcpp::STB binding
,
1424 elfcpp::STV visibility
, unsigned char nonvis
,
1425 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
1427 // Define a symbol as a constant, sized version.
1430 do_define_as_constant(
1431 const char* name
, const char* version
,
1432 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1433 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
1434 elfcpp::STT type
, elfcpp::STB binding
,
1435 elfcpp::STV visibility
, unsigned char nonvis
,
1436 bool only_if_ref
, bool force_override
);
1438 // Add any undefined symbols named on the command line to the symbol
1439 // table, sized version.
1442 do_add_undefined_symbols_from_command_line();
1444 // Allocate the common symbols, sized version.
1447 do_allocate_commons(Layout
*, Mapfile
*);
1449 // Allocate the common symbols from one list.
1452 do_allocate_commons_list(Layout
*, bool is_tls
, Commons_type
*, Mapfile
*);
1454 // Implement detect_odr_violations.
1455 template<int size
, bool big_endian
>
1457 sized_detect_odr_violations() const;
1459 // Finalize symbols specialized for size.
1462 sized_finalize(off_t
, Stringpool
*, unsigned int*);
1464 // Finalize a symbol. Return whether it should be added to the
1468 sized_finalize_symbol(Symbol
*);
1470 // Add a symbol the final symtab by setting its index.
1473 add_to_final_symtab(Symbol
*, Stringpool
*, unsigned int* pindex
, off_t
* poff
);
1475 // Write globals specialized for size and endianness.
1476 template<int size
, bool big_endian
>
1478 sized_write_globals(const Input_objects
*, const Stringpool
*,
1479 const Stringpool
*, Output_symtab_xindex
*,
1480 Output_symtab_xindex
*, Output_file
*) const;
1482 // Write out a symbol to P.
1483 template<int size
, bool big_endian
>
1485 sized_write_symbol(Sized_symbol
<size
>*,
1486 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1488 const Stringpool
*, unsigned char* p
) const;
1490 // Possibly warn about an undefined symbol from a dynamic object.
1492 warn_about_undefined_dynobj_symbol(const Input_objects
*, Symbol
*) const;
1494 // Write out a section symbol, specialized for size and endianness.
1495 template<int size
, bool big_endian
>
1497 sized_write_section_symbol(const Output_section
*, Output_symtab_xindex
*,
1498 Output_file
*, off_t
) const;
1500 // The type of the symbol hash table.
1502 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
1504 struct Symbol_table_hash
1507 operator()(const Symbol_table_key
&) const;
1510 struct Symbol_table_eq
1513 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
1516 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
1517 Symbol_table_eq
> Symbol_table_type
;
1519 // The type of the list of symbols which have been forced local.
1520 typedef std::vector
<Symbol
*> Forced_locals
;
1522 // A map from symbols with COPY relocs to the dynamic objects where
1523 // they are defined.
1524 typedef Unordered_map
<const Symbol
*, Dynobj
*> Copied_symbol_dynobjs
;
1526 // A map from symbol name (as a pointer into the namepool) to all
1527 // the locations the symbols is (weakly) defined (and certain other
1528 // conditions are met). This map will be used later to detect
1529 // possible One Definition Rule (ODR) violations.
1530 struct Symbol_location
1532 Object
* object
; // Object where the symbol is defined.
1533 unsigned int shndx
; // Section-in-object where the symbol is defined.
1534 off_t offset
; // Offset-in-section where the symbol is defined.
1535 bool operator==(const Symbol_location
& that
) const
1537 return (this->object
== that
.object
1538 && this->shndx
== that
.shndx
1539 && this->offset
== that
.offset
);
1543 struct Symbol_location_hash
1545 size_t operator()(const Symbol_location
& loc
) const
1546 { return reinterpret_cast<uintptr_t>(loc
.object
) ^ loc
.offset
^ loc
.shndx
; }
1549 typedef Unordered_map
<const char*,
1550 Unordered_set
<Symbol_location
, Symbol_location_hash
> >
1553 // We increment this every time we see a new undefined symbol, for
1554 // use in archive groups.
1556 // The index of the first global symbol in the output file.
1557 unsigned int first_global_index_
;
1558 // The file offset within the output symtab section where we should
1561 // The number of global symbols we want to write out.
1562 unsigned int output_count_
;
1563 // The file offset of the global dynamic symbols, or 0 if none.
1564 off_t dynamic_offset_
;
1565 // The index of the first global dynamic symbol.
1566 unsigned int first_dynamic_global_index_
;
1567 // The number of global dynamic symbols, or 0 if none.
1568 unsigned int dynamic_count_
;
1569 // The symbol hash table.
1570 Symbol_table_type table_
;
1571 // A pool of symbol names. This is used for all global symbols.
1572 // Entries in the hash table point into this pool.
1573 Stringpool namepool_
;
1574 // Forwarding symbols.
1575 Unordered_map
<const Symbol
*, Symbol
*> forwarders_
;
1576 // Weak aliases. A symbol in this list points to the next alias.
1577 // The aliases point to each other in a circular list.
1578 Unordered_map
<Symbol
*, Symbol
*> weak_aliases_
;
1579 // We don't expect there to be very many common symbols, so we keep
1580 // a list of them. When we find a common symbol we add it to this
1581 // list. It is possible that by the time we process the list the
1582 // symbol is no longer a common symbol. It may also have become a
1584 Commons_type commons_
;
1585 // This is like the commons_ field, except that it holds TLS common
1587 Commons_type tls_commons_
;
1588 // A list of symbols which have been forced to be local. We don't
1589 // expect there to be very many of them, so we keep a list of them
1590 // rather than walking the whole table to find them.
1591 Forced_locals forced_locals_
;
1592 // Manage symbol warnings.
1594 // Manage potential One Definition Rule (ODR) violations.
1595 Odr_map candidate_odr_violations_
;
1597 // When we emit a COPY reloc for a symbol, we define it in an
1598 // Output_data. When it's time to emit version information for it,
1599 // we need to know the dynamic object in which we found the original
1600 // definition. This maps symbols with COPY relocs to the dynamic
1601 // object where they were defined.
1602 Copied_symbol_dynobjs copied_symbol_dynobjs_
;
1603 // Information parsed from the version script, if any.
1604 const Version_script_info
& version_script_
;
1607 // We inline get_sized_symbol for efficiency.
1611 Symbol_table::get_sized_symbol(Symbol
* sym
) const
1613 gold_assert(size
== parameters
->target().get_size());
1614 return static_cast<Sized_symbol
<size
>*>(sym
);
1618 const Sized_symbol
<size
>*
1619 Symbol_table::get_sized_symbol(const Symbol
* sym
) const
1621 gold_assert(size
== parameters
->target().get_size());
1622 return static_cast<const Sized_symbol
<size
>*>(sym
);
1625 } // End namespace gold.
1627 #endif // !defined(GOLD_SYMTAB_H)