1 // symtab.h -- the gold symbol table -*- C++ -*-
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "parameters.h"
32 #include "stringpool.h"
43 template<int size, bool big_endian>
46 template<int size, bool big_endian>
49 class Version_script_info;
55 class Output_symtab_xindex;
57 // The base class of an entry in the symbol table. The symbol table
58 // can have a lot of entries, so we don't want this class to big.
59 // Size dependent fields can be found in the template class
60 // Sized_symbol. Targets may support their own derived classes.
65 // Because we want the class to be small, we don't use any virtual
66 // functions. But because symbols can be defined in different
67 // places, we need to classify them. This enum is the different
68 // sources of symbols we support.
71 // Symbol defined in a relocatable or dynamic input file--this is
72 // the most common case.
74 // Symbol defined in an Output_data, a special section created by
77 // Symbol defined in an Output_segment, with no associated
80 // Symbol value is constant.
84 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
86 enum Segment_offset_base
88 // From the start of the segment.
90 // From the end of the segment.
92 // From the filesz of the segment--i.e., after the loaded bytes
93 // but before the bytes which are allocated but zeroed.
97 // Return the symbol name.
100 { return this->name_; }
102 // Return the (ANSI) demangled version of the name, if
103 // parameters.demangle() is true. Otherwise, return the name. This
104 // is intended to be used only for logging errors, so it's not
107 demangled_name() const;
109 // Return the symbol version. This will return NULL for an
110 // unversioned symbol.
113 { return this->version_; }
115 // Return whether this version is the default for this symbol name
116 // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
117 // meaningful for versioned symbols.
121 gold_assert(this->version_ != NULL);
122 return this->is_def_;
125 // Set that this version is the default for this symbol name.
128 { this->is_def_ = true; }
130 // Return the symbol source.
133 { return this->source_; }
135 // Return the object with which this symbol is associated.
139 gold_assert(this->source_ == FROM_OBJECT);
140 return this->u_.from_object.object;
143 // Return the index of the section in the input relocatable or
144 // dynamic object file.
146 shndx(bool* is_ordinary) const
148 gold_assert(this->source_ == FROM_OBJECT);
149 *is_ordinary = this->is_ordinary_shndx_;
150 return this->u_.from_object.shndx;
153 // Return the output data section with which this symbol is
154 // associated, if the symbol was specially defined with respect to
155 // an output data section.
159 gold_assert(this->source_ == IN_OUTPUT_DATA);
160 return this->u_.in_output_data.output_data;
163 // If this symbol was defined with respect to an output data
164 // section, return whether the value is an offset from end.
166 offset_is_from_end() const
168 gold_assert(this->source_ == IN_OUTPUT_DATA);
169 return this->u_.in_output_data.offset_is_from_end;
172 // Return the output segment with which this symbol is associated,
173 // if the symbol was specially defined with respect to an output
176 output_segment() const
178 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
179 return this->u_.in_output_segment.output_segment;
182 // If this symbol was defined with respect to an output segment,
183 // return the offset base.
187 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
188 return this->u_.in_output_segment.offset_base;
191 // Return the symbol binding.
194 { return this->binding_; }
196 // Return the symbol type.
199 { return this->type_; }
201 // Return the symbol visibility.
204 { return this->visibility_; }
206 // Return the non-visibility part of the st_other field.
209 { return this->nonvis_; }
211 // Return whether this symbol is a forwarder. This will never be
212 // true of a symbol found in the hash table, but may be true of
213 // symbol pointers attached to object files.
216 { return this->is_forwarder_; }
218 // Mark this symbol as a forwarder.
221 { this->is_forwarder_ = true; }
223 // Return whether this symbol has an alias in the weak aliases table
227 { return this->has_alias_; }
229 // Mark this symbol as having an alias.
232 { this->has_alias_ = true; }
234 // Return whether this symbol needs an entry in the dynamic symbol
237 needs_dynsym_entry() const
239 return (this->needs_dynsym_entry_
240 || (this->in_reg() && this->in_dyn()));
243 // Mark this symbol as needing an entry in the dynamic symbol table.
245 set_needs_dynsym_entry()
246 { this->needs_dynsym_entry_ = true; }
248 // Return whether this symbol should be added to the dynamic symbol
251 should_add_dynsym_entry() const;
253 // Return whether this symbol has been seen in a regular object.
256 { return this->in_reg_; }
258 // Mark this symbol as having been seen in a regular object.
261 { this->in_reg_ = true; }
263 // Return whether this symbol has been seen in a dynamic object.
266 { return this->in_dyn_; }
268 // Mark this symbol as having been seen in a dynamic object.
271 { this->in_dyn_ = true; }
273 // Return the index of this symbol in the output file symbol table.
274 // A value of -1U means that this symbol is not going into the
275 // output file. This starts out as zero, and is set to a non-zero
276 // value by Symbol_table::finalize. It is an error to ask for the
277 // symbol table index before it has been set.
281 gold_assert(this->symtab_index_ != 0);
282 return this->symtab_index_;
285 // Set the index of the symbol in the output file symbol table.
287 set_symtab_index(unsigned int index)
289 gold_assert(index != 0);
290 this->symtab_index_ = index;
293 // Return whether this symbol already has an index in the output
294 // file symbol table.
296 has_symtab_index() const
297 { return this->symtab_index_ != 0; }
299 // Return the index of this symbol in the dynamic symbol table. A
300 // value of -1U means that this symbol is not going into the dynamic
301 // symbol table. This starts out as zero, and is set to a non-zero
302 // during Layout::finalize. It is an error to ask for the dynamic
303 // symbol table index before it has been set.
307 gold_assert(this->dynsym_index_ != 0);
308 return this->dynsym_index_;
311 // Set the index of the symbol in the dynamic symbol table.
313 set_dynsym_index(unsigned int index)
315 gold_assert(index != 0);
316 this->dynsym_index_ = index;
319 // Return whether this symbol already has an index in the dynamic
322 has_dynsym_index() const
323 { return this->dynsym_index_ != 0; }
325 // Return whether this symbol has an entry in the GOT section.
326 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
328 has_got_offset(unsigned int got_type) const
329 { return this->got_offsets_.get_offset(got_type) != -1U; }
331 // Return the offset into the GOT section of this symbol.
333 got_offset(unsigned int got_type) const
335 unsigned int got_offset = this->got_offsets_.get_offset(got_type);
336 gold_assert(got_offset != -1U);
340 // Set the GOT offset of this symbol.
342 set_got_offset(unsigned int got_type, unsigned int got_offset)
343 { this->got_offsets_.set_offset(got_type, got_offset); }
345 // Return whether this symbol has an entry in the PLT section.
347 has_plt_offset() const
348 { return this->has_plt_offset_; }
350 // Return the offset into the PLT section of this symbol.
354 gold_assert(this->has_plt_offset());
355 return this->plt_offset_;
358 // Set the PLT offset of this symbol.
360 set_plt_offset(unsigned int plt_offset)
362 this->has_plt_offset_ = true;
363 this->plt_offset_ = plt_offset;
366 // Return whether this dynamic symbol needs a special value in the
367 // dynamic symbol table.
369 needs_dynsym_value() const
370 { return this->needs_dynsym_value_; }
372 // Set that this dynamic symbol needs a special value in the dynamic
375 set_needs_dynsym_value()
377 gold_assert(this->object()->is_dynamic());
378 this->needs_dynsym_value_ = true;
381 // Return true if the final value of this symbol is known at link
384 final_value_is_known() const;
386 // Return whether this is a defined symbol (not undefined or
392 if (this->source_ != FROM_OBJECT)
394 unsigned int shndx = this->shndx(&is_ordinary);
396 ? shndx != elfcpp::SHN_UNDEF
397 : shndx != elfcpp::SHN_COMMON);
400 // Return true if this symbol is from a dynamic object.
402 is_from_dynobj() const
404 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
407 // Return whether this is an undefined symbol.
412 return (this->source_ == FROM_OBJECT
413 && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF
417 // Return whether this is a weak undefined symbol.
419 is_weak_undefined() const
422 return (this->source_ == FROM_OBJECT
423 && this->binding() == elfcpp::STB_WEAK
424 && this->shndx(&is_ordinary) == elfcpp::SHN_UNDEF
428 // Return whether this is an absolute symbol.
433 return (this->source_ == FROM_OBJECT
434 && this->shndx(&is_ordinary) == elfcpp::SHN_ABS
438 // Return whether this is a common symbol.
443 return (this->source_ == FROM_OBJECT
444 && ((this->shndx(&is_ordinary) == elfcpp::SHN_COMMON
446 || this->type_ == elfcpp::STT_COMMON));
449 // Return whether this symbol can be seen outside this object.
451 is_externally_visible() const
453 return (this->visibility_ == elfcpp::STV_DEFAULT
454 || this->visibility_ == elfcpp::STV_PROTECTED);
457 // Return true if this symbol can be preempted by a definition in
458 // another link unit.
460 is_preemptible() const
462 // It doesn't make sense to ask whether a symbol defined in
463 // another object is preemptible.
464 gold_assert(!this->is_from_dynobj());
466 // It doesn't make sense to ask whether an undefined symbol
468 gold_assert(!this->is_undefined());
470 return (this->visibility_ != elfcpp::STV_INTERNAL
471 && this->visibility_ != elfcpp::STV_HIDDEN
472 && this->visibility_ != elfcpp::STV_PROTECTED
473 && !this->is_forced_local_
474 && parameters->options().shared()
475 && !parameters->options().Bsymbolic());
478 // Return true if this symbol is a function that needs a PLT entry.
479 // If the symbol is defined in a dynamic object or if it is subject
480 // to pre-emption, we need to make a PLT entry. If we're doing a
481 // static link, we don't create PLT entries.
483 needs_plt_entry() const
485 return (!parameters->doing_static_link()
486 && this->type() == elfcpp::STT_FUNC
487 && (this->is_from_dynobj()
488 || this->is_undefined()
489 || this->is_preemptible()));
492 // When determining whether a reference to a symbol needs a dynamic
493 // relocation, we need to know several things about the reference.
494 // These flags may be or'ed together.
497 // Reference to the symbol's absolute address.
499 // A non-PIC reference.
505 // Given a direct absolute or pc-relative static relocation against
506 // the global symbol, this function returns whether a dynamic relocation
510 needs_dynamic_reloc(int flags) const
512 // No dynamic relocations in a static link!
513 if (parameters->doing_static_link())
516 // A reference to a weak undefined symbol from an executable should be
517 // statically resolved to 0, and does not need a dynamic relocation.
518 // This matches gnu ld behavior.
519 if (this->is_weak_undefined() && !parameters->options().shared())
522 // A reference to an absolute symbol does not need a dynamic relocation.
523 if (this->is_absolute())
526 // An absolute reference within a position-independent output file
527 // will need a dynamic relocation.
528 if ((flags & ABSOLUTE_REF)
529 && parameters->options().output_is_position_independent())
532 // A function call that can branch to a local PLT entry does not need
533 // a dynamic relocation. A non-pic pc-relative function call in a
534 // shared library cannot use a PLT entry.
535 if ((flags & FUNCTION_CALL)
536 && this->has_plt_offset()
537 && !((flags & NON_PIC_REF) && parameters->options().shared()))
540 // A reference to any PLT entry in a non-position-independent executable
541 // does not need a dynamic relocation.
542 if (!parameters->options().output_is_position_independent()
543 && this->has_plt_offset())
546 // A reference to a symbol defined in a dynamic object or to a
547 // symbol that is preemptible will need a dynamic relocation.
548 if (this->is_from_dynobj()
549 || this->is_undefined()
550 || this->is_preemptible())
553 // For all other cases, return FALSE.
557 // Given a direct absolute static relocation against
558 // the global symbol, where a dynamic relocation is needed, this
559 // function returns whether a relative dynamic relocation can be used.
560 // The caller must determine separately whether the static relocation
561 // is compatible with a relative relocation.
564 can_use_relative_reloc(bool is_function_call) const
566 // A function call that can branch to a local PLT entry can
567 // use a RELATIVE relocation.
568 if (is_function_call && this->has_plt_offset())
571 // A reference to a symbol defined in a dynamic object or to a
572 // symbol that is preemptible can not use a RELATIVE relocaiton.
573 if (this->is_from_dynobj()
574 || this->is_undefined()
575 || this->is_preemptible())
578 // For all other cases, return TRUE.
582 // Return the output section where this symbol is defined. Return
583 // NULL if the symbol has an absolute value.
585 output_section() const;
587 // Set the symbol's output section. This is used for symbols
588 // defined in scripts. This should only be called after the symbol
589 // table has been finalized.
591 set_output_section(Output_section*);
593 // Return whether there should be a warning for references to this
597 { return this->has_warning_; }
599 // Mark this symbol as having a warning.
602 { this->has_warning_ = true; }
604 // Return whether this symbol is defined by a COPY reloc from a
607 is_copied_from_dynobj() const
608 { return this->is_copied_from_dynobj_; }
610 // Mark this symbol as defined by a COPY reloc.
612 set_is_copied_from_dynobj()
613 { this->is_copied_from_dynobj_ = true; }
615 // Return whether this symbol is forced to visibility STB_LOCAL
616 // by a "local:" entry in a version script.
618 is_forced_local() const
619 { return this->is_forced_local_; }
621 // Mark this symbol as forced to STB_LOCAL visibility.
623 set_is_forced_local()
624 { this->is_forced_local_ = true; }
627 // Instances of this class should always be created at a specific
630 { memset(this, 0, sizeof *this); }
632 // Initialize the general fields.
634 init_fields(const char* name, const char* version,
635 elfcpp::STT type, elfcpp::STB binding,
636 elfcpp::STV visibility, unsigned char nonvis);
638 // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
639 // section index, IS_ORDINARY is whether it is a normal section
640 // index rather than a special code.
641 template<int size, bool big_endian>
643 init_base(const char *name, const char* version, Object* object,
644 const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
647 // Initialize fields for an Output_data.
649 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
650 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
652 // Initialize fields for an Output_segment.
654 init_base(const char* name, Output_segment* os, elfcpp::STT type,
655 elfcpp::STB binding, elfcpp::STV visibility,
656 unsigned char nonvis, Segment_offset_base offset_base);
658 // Initialize fields for a constant.
660 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
661 elfcpp::STV visibility, unsigned char nonvis);
663 // Override existing symbol.
664 template<int size, bool big_endian>
666 override_base(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
667 bool is_ordinary, Object* object, const char* version);
669 // Override existing symbol with a special symbol.
671 override_base_with_special(const Symbol* from);
673 // Allocate a common symbol by giving it a location in the output
676 allocate_base_common(Output_data*);
679 Symbol(const Symbol&);
680 Symbol& operator=(const Symbol&);
682 // Symbol name (expected to point into a Stringpool).
684 // Symbol version (expected to point into a Stringpool). This may
686 const char* version_;
690 // This struct is used if SOURCE_ == FROM_OBJECT.
693 // Object in which symbol is defined, or in which it was first
696 // Section number in object_ in which symbol is defined.
700 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
703 // Output_data in which symbol is defined. Before
704 // Layout::finalize the symbol's value is an offset within the
706 Output_data* output_data;
707 // True if the offset is from the end, false if the offset is
708 // from the beginning.
709 bool offset_is_from_end;
712 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
715 // Output_segment in which the symbol is defined. Before
716 // Layout::finalize the symbol's value is an offset.
717 Output_segment* output_segment;
718 // The base to use for the offset before Layout::finalize.
719 Segment_offset_base offset_base;
723 // The index of this symbol in the output file. If the symbol is
724 // not going into the output file, this value is -1U. This field
725 // starts as always holding zero. It is set to a non-zero value by
726 // Symbol_table::finalize.
727 unsigned int symtab_index_;
729 // The index of this symbol in the dynamic symbol table. If the
730 // symbol is not going into the dynamic symbol table, this value is
731 // -1U. This field starts as always holding zero. It is set to a
732 // non-zero value during Layout::finalize.
733 unsigned int dynsym_index_;
735 // If this symbol has an entry in the GOT section (has_got_offset_
736 // is true), this holds the offset from the start of the GOT section.
737 // A symbol may have more than one GOT offset (e.g., when mixing
738 // modules compiled with two different TLS models), but will usually
740 Got_offset_list got_offsets_;
742 // If this symbol has an entry in the PLT section (has_plt_offset_
743 // is true), then this is the offset from the start of the PLT
745 unsigned int plt_offset_;
747 // Symbol type (bits 0 to 3).
748 elfcpp::STT type_ : 4;
749 // Symbol binding (bits 4 to 7).
750 elfcpp::STB binding_ : 4;
751 // Symbol visibility (bits 8 to 9).
752 elfcpp::STV visibility_ : 2;
753 // Rest of symbol st_other field (bits 10 to 15).
754 unsigned int nonvis_ : 6;
755 // The type of symbol (bits 16 to 18).
757 // True if this symbol always requires special target-specific
758 // handling (bit 19).
759 bool is_target_special_ : 1;
760 // True if this is the default version of the symbol (bit 20).
762 // True if this symbol really forwards to another symbol. This is
763 // used when we discover after the fact that two different entries
764 // in the hash table really refer to the same symbol. This will
765 // never be set for a symbol found in the hash table, but may be set
766 // for a symbol found in the list of symbols attached to an Object.
767 // It forwards to the symbol found in the forwarders_ map of
768 // Symbol_table (bit 21).
769 bool is_forwarder_ : 1;
770 // True if the symbol has an alias in the weak_aliases table in
771 // Symbol_table (bit 22).
773 // True if this symbol needs to be in the dynamic symbol table (bit
775 bool needs_dynsym_entry_ : 1;
776 // True if we've seen this symbol in a regular object (bit 24).
778 // True if we've seen this symbol in a dynamic object (bit 25).
780 // True if the symbol has an entry in the PLT section (bit 26).
781 bool has_plt_offset_ : 1;
782 // True if this is a dynamic symbol which needs a special value in
783 // the dynamic symbol table (bit 27).
784 bool needs_dynsym_value_ : 1;
785 // True if there is a warning for this symbol (bit 28).
786 bool has_warning_ : 1;
787 // True if we are using a COPY reloc for this symbol, so that the
788 // real definition lives in a dynamic object (bit 29).
789 bool is_copied_from_dynobj_ : 1;
790 // True if this symbol was forced to local visibility by a version
792 bool is_forced_local_ : 1;
793 // True if the field u_.from_object.shndx is an ordinary section
794 // index, not one of the special codes from SHN_LORESERVE to
796 bool is_ordinary_shndx_ : 1;
799 // The parts of a symbol which are size specific. Using a template
800 // derived class like this helps us use less space on a 32-bit system.
803 class Sized_symbol : public Symbol
806 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
807 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
812 // Initialize fields from an ELF symbol in OBJECT. ST_SHNDX is the
813 // section index, IS_ORDINARY is whether it is a normal section
814 // index rather than a special code.
815 template<bool big_endian>
817 init(const char *name, const char* version, Object* object,
818 const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
821 // Initialize fields for an Output_data.
823 init(const char* name, Output_data*, Value_type value, Size_type symsize,
824 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
825 bool offset_is_from_end);
827 // Initialize fields for an Output_segment.
829 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
830 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
831 Segment_offset_base offset_base);
833 // Initialize fields for a constant.
835 init(const char* name, Value_type value, Size_type symsize,
836 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
838 // Override existing symbol.
839 template<bool big_endian>
841 override(const elfcpp::Sym<size, big_endian>&, unsigned int st_shndx,
842 bool is_ordinary, Object* object, const char* version);
844 // Override existing symbol with a special symbol.
846 override_with_special(const Sized_symbol<size>*);
848 // Return the symbol's value.
851 { return this->value_; }
853 // Return the symbol's size (we can't call this 'size' because that
854 // is a template parameter).
857 { return this->symsize_; }
859 // Set the symbol size. This is used when resolving common symbols.
861 set_symsize(Size_type symsize)
862 { this->symsize_ = symsize; }
864 // Set the symbol value. This is called when we store the final
865 // values of the symbols into the symbol table.
867 set_value(Value_type value)
868 { this->value_ = value; }
870 // Allocate a common symbol by giving it a location in the output
873 allocate_common(Output_data*, Value_type value);
876 Sized_symbol(const Sized_symbol&);
877 Sized_symbol& operator=(const Sized_symbol&);
879 // Symbol value. Before Layout::finalize this is the offset in the
880 // input section. This is set to the final value during
887 // A struct describing a symbol defined by the linker, where the value
888 // of the symbol is defined based on an output section. This is used
889 // for symbols defined by the linker, like "_init_array_start".
891 struct Define_symbol_in_section
895 // The name of the output section with which this symbol should be
896 // associated. If there is no output section with that name, the
897 // symbol will be defined as zero.
898 const char* output_section;
899 // The offset of the symbol within the output section. This is an
900 // offset from the start of the output section, unless start_at_end
901 // is true, in which case this is an offset from the end of the
904 // The size of the symbol.
908 // The symbol binding.
910 // The symbol visibility.
911 elfcpp::STV visibility;
912 // The rest of the st_other field.
913 unsigned char nonvis;
914 // If true, the value field is an offset from the end of the output
916 bool offset_is_from_end;
917 // If true, this symbol is defined only if we see a reference to it.
921 // A struct describing a symbol defined by the linker, where the value
922 // of the symbol is defined based on a segment. This is used for
923 // symbols defined by the linker, like "_end". We describe the
924 // segment with which the symbol should be associated by its
925 // characteristics. If no segment meets these characteristics, the
926 // symbol will be defined as zero. If there is more than one segment
927 // which meets these characteristics, we will use the first one.
929 struct Define_symbol_in_segment
933 // The segment type where the symbol should be defined, typically
935 elfcpp::PT segment_type;
936 // Bitmask of segment flags which must be set.
937 elfcpp::PF segment_flags_set;
938 // Bitmask of segment flags which must be clear.
939 elfcpp::PF segment_flags_clear;
940 // The offset of the symbol within the segment. The offset is
941 // calculated from the position set by offset_base.
943 // The size of the symbol.
947 // The symbol binding.
949 // The symbol visibility.
950 elfcpp::STV visibility;
951 // The rest of the st_other field.
952 unsigned char nonvis;
953 // The base from which we compute the offset.
954 Symbol::Segment_offset_base offset_base;
955 // If true, this symbol is defined only if we see a reference to it.
959 // This class manages warnings. Warnings are a GNU extension. When
960 // we see a section named .gnu.warning.SYM in an object file, and if
961 // we wind using the definition of SYM from that object file, then we
962 // will issue a warning for any relocation against SYM from a
963 // different object file. The text of the warning is the contents of
964 // the section. This is not precisely the definition used by the old
965 // GNU linker; the old GNU linker treated an occurrence of
966 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
967 // would trigger a warning on any reference. However, it was
968 // inconsistent in that a warning in a dynamic object only triggered
969 // if there was no definition in a regular object. This linker is
970 // different in that we only issue a warning if we use the symbol
971 // definition from the same object file as the warning section.
980 // Add a warning for symbol NAME in object OBJ. WARNING is the text
983 add_warning(Symbol_table* symtab, const char* name, Object* obj,
984 const std::string& warning);
986 // For each symbol for which we should give a warning, make a note
989 note_warnings(Symbol_table* symtab);
991 // Issue a warning for a reference to SYM at RELINFO's location.
992 template<int size, bool big_endian>
994 issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
995 size_t relnum, off_t reloffset) const;
998 Warnings(const Warnings&);
999 Warnings& operator=(const Warnings&);
1001 // What we need to know to get the warning text.
1002 struct Warning_location
1004 // The object the warning is in.
1006 // The warning text.
1010 : object(NULL), text()
1014 set(Object* o, const std::string& t)
1021 // A mapping from warning symbol names (canonicalized in
1022 // Symbol_table's namepool_ field) to warning information.
1023 typedef Unordered_map<const char*, Warning_location> Warning_table;
1025 Warning_table warnings_;
1028 // The main linker symbol table.
1033 // COUNT is an estimate of how many symbosl will be inserted in the
1034 // symbol table. It's ok to put 0 if you don't know; a correct
1035 // guess will just save some CPU by reducing hashtable resizes.
1036 Symbol_table(unsigned int count, const Version_script_info& version_script);
1040 // Add COUNT external symbols from the relocatable object RELOBJ to
1041 // the symbol table. SYMS is the symbols, SYMNDX_OFFSET is the
1042 // offset in the symbol table of the first symbol, SYM_NAMES is
1043 // their names, SYM_NAME_SIZE is the size of SYM_NAMES. This sets
1044 // SYMPOINTERS to point to the symbols in the symbol table.
1045 template<int size, bool big_endian>
1047 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
1048 const unsigned char* syms, size_t count,
1049 size_t symndx_offset, const char* sym_names,
1050 size_t sym_name_size,
1051 typename Sized_relobj<size, big_endian>::Symbols*);
1053 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1054 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1055 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1056 // symbol version data.
1057 template<int size, bool big_endian>
1059 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
1060 const unsigned char* syms, size_t count,
1061 const char* sym_names, size_t sym_name_size,
1062 const unsigned char* versym, size_t versym_size,
1063 const std::vector<const char*>*);
1065 // Define a special symbol based on an Output_data. It is a
1066 // multiple definition error if this symbol is already defined.
1068 define_in_output_data(const char* name, const char* version,
1069 Output_data*, uint64_t value, uint64_t symsize,
1070 elfcpp::STT type, elfcpp::STB binding,
1071 elfcpp::STV visibility, unsigned char nonvis,
1072 bool offset_is_from_end, bool only_if_ref);
1074 // Define a special symbol based on an Output_segment. It is a
1075 // multiple definition error if this symbol is already defined.
1077 define_in_output_segment(const char* name, const char* version,
1078 Output_segment*, uint64_t value, uint64_t symsize,
1079 elfcpp::STT type, elfcpp::STB binding,
1080 elfcpp::STV visibility, unsigned char nonvis,
1081 Symbol::Segment_offset_base, bool only_if_ref);
1083 // Define a special symbol with a constant value. It is a multiple
1084 // definition error if this symbol is already defined.
1086 define_as_constant(const char* name, const char* version,
1087 uint64_t value, uint64_t symsize, elfcpp::STT type,
1088 elfcpp::STB binding, elfcpp::STV visibility,
1089 unsigned char nonvis, bool only_if_ref,
1090 bool force_override);
1092 // Define a set of symbols in output sections. If ONLY_IF_REF is
1093 // true, only define them if they are referenced.
1095 define_symbols(const Layout*, int count, const Define_symbol_in_section*,
1098 // Define a set of symbols in output segments. If ONLY_IF_REF is
1099 // true, only defined them if they are referenced.
1101 define_symbols(const Layout*, int count, const Define_symbol_in_segment*,
1104 // Define SYM using a COPY reloc. POSD is the Output_data where the
1105 // symbol should be defined--typically a .dyn.bss section. VALUE is
1106 // the offset within POSD.
1109 define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
1110 typename elfcpp::Elf_types<size>::Elf_Addr);
1112 // Look up a symbol.
1114 lookup(const char*, const char* version = NULL) const;
1116 // Return the real symbol associated with the forwarder symbol FROM.
1118 resolve_forwards(const Symbol* from) const;
1120 // Return the sized version of a symbol in this table.
1123 get_sized_symbol(Symbol*) const;
1126 const Sized_symbol<size>*
1127 get_sized_symbol(const Symbol*) const;
1129 // Return the count of undefined symbols seen.
1131 saw_undefined() const
1132 { return this->saw_undefined_; }
1134 // Allocate the common symbols
1136 allocate_commons(Layout*);
1138 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1141 add_warning(const char* name, Object* obj, const std::string& warning)
1142 { this->warnings_.add_warning(this, name, obj, warning); }
1144 // Canonicalize a symbol name for use in the hash table.
1146 canonicalize_name(const char* name)
1147 { return this->namepool_.add(name, true, NULL); }
1149 // Possibly issue a warning for a reference to SYM at LOCATION which
1151 template<int size, bool big_endian>
1153 issue_warning(const Symbol* sym,
1154 const Relocate_info<size, big_endian>* relinfo,
1155 size_t relnum, off_t reloffset) const
1156 { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
1158 // Check candidate_odr_violations_ to find symbols with the same name
1159 // but apparently different definitions (different source-file/line-no).
1161 detect_odr_violations(const Task*, const char* output_file_name) const;
1163 // SYM is defined using a COPY reloc. Return the dynamic object
1164 // where the original definition was found.
1166 get_copy_source(const Symbol* sym) const;
1168 // Set the dynamic symbol indexes. INDEX is the index of the first
1169 // global dynamic symbol. Pointers to the symbols are stored into
1170 // the vector. The names are stored into the Stringpool. This
1171 // returns an updated dynamic symbol index.
1173 set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*,
1174 Stringpool*, Versions*);
1176 // Finalize the symbol table after we have set the final addresses
1177 // of all the input sections. This sets the final symbol indexes,
1178 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1179 // index of the first global symbol. OFF is the file offset of the
1180 // global symbol table, DYNOFF is the offset of the globals in the
1181 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1182 // global dynamic symbol, and DYNCOUNT is the number of global
1183 // dynamic symbols. This records the parameters, and returns the
1184 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1187 finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
1188 Stringpool* pool, unsigned int *plocal_symcount);
1190 // Write out the global symbols.
1192 write_globals(const Input_objects*, const Stringpool*, const Stringpool*,
1193 Output_symtab_xindex*, Output_symtab_xindex*,
1194 Output_file*) const;
1196 // Write out a section symbol. Return the updated offset.
1198 write_section_symbol(const Output_section*, Output_symtab_xindex*,
1199 Output_file*, off_t) const;
1201 // Dump statistical information to stderr.
1203 print_stats() const;
1205 // Return the version script information.
1206 const Version_script_info&
1207 version_script() const
1208 { return version_script_; }
1211 Symbol_table(const Symbol_table&);
1212 Symbol_table& operator=(const Symbol_table&);
1214 // The type of the list of common symbols.
1215 typedef std::vector<Symbol*> Commons_type;
1217 // Make FROM a forwarder symbol to TO.
1219 make_forwarder(Symbol* from, Symbol* to);
1222 template<int size, bool big_endian>
1224 add_from_object(Object*, const char *name, Stringpool::Key name_key,
1225 const char *version, Stringpool::Key version_key,
1226 bool def, const elfcpp::Sym<size, big_endian>& sym,
1227 unsigned int st_shndx, bool is_ordinary,
1228 unsigned int orig_st_shndx);
1231 template<int size, bool big_endian>
1233 resolve(Sized_symbol<size>* to,
1234 const elfcpp::Sym<size, big_endian>& sym,
1235 unsigned int st_shndx, bool is_ordinary,
1236 unsigned int orig_st_shndx,
1237 Object*, const char* version);
1239 template<int size, bool big_endian>
1241 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
1242 const char* version);
1244 // Record that a symbol is forced to be local by a version script.
1246 force_local(Symbol*);
1248 // Adjust NAME and *NAME_KEY for wrapping.
1250 wrap_symbol(Object* object, const char*, Stringpool::Key* name_key);
1252 // Whether we should override a symbol, based on flags in
1255 should_override(const Symbol*, unsigned int, Object*, bool*);
1257 // Override a symbol.
1258 template<int size, bool big_endian>
1260 override(Sized_symbol<size>* tosym,
1261 const elfcpp::Sym<size, big_endian>& fromsym,
1262 unsigned int st_shndx, bool is_ordinary,
1263 Object* object, const char* version);
1265 // Whether we should override a symbol with a special symbol which
1266 // is automatically defined by the linker.
1268 should_override_with_special(const Symbol*);
1270 // Override a symbol with a special symbol.
1273 override_with_special(Sized_symbol<size>* tosym,
1274 const Sized_symbol<size>* fromsym);
1276 // Record all weak alias sets for a dynamic object.
1279 record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1281 // Define a special symbol.
1282 template<int size, bool big_endian>
1284 define_special_symbol(const char** pname, const char** pversion,
1285 bool only_if_ref, Sized_symbol<size>** poldsym);
1287 // Define a symbol in an Output_data, sized version.
1290 do_define_in_output_data(const char* name, const char* version, Output_data*,
1291 typename elfcpp::Elf_types<size>::Elf_Addr value,
1292 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1293 elfcpp::STT type, elfcpp::STB binding,
1294 elfcpp::STV visibility, unsigned char nonvis,
1295 bool offset_is_from_end, bool only_if_ref);
1297 // Define a symbol in an Output_segment, sized version.
1300 do_define_in_output_segment(
1301 const char* name, const char* version, Output_segment* os,
1302 typename elfcpp::Elf_types<size>::Elf_Addr value,
1303 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1304 elfcpp::STT type, elfcpp::STB binding,
1305 elfcpp::STV visibility, unsigned char nonvis,
1306 Symbol::Segment_offset_base offset_base, bool only_if_ref);
1308 // Define a symbol as a constant, sized version.
1311 do_define_as_constant(
1312 const char* name, const char* version,
1313 typename elfcpp::Elf_types<size>::Elf_Addr value,
1314 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1315 elfcpp::STT type, elfcpp::STB binding,
1316 elfcpp::STV visibility, unsigned char nonvis,
1317 bool only_if_ref, bool force_override);
1319 // Allocate the common symbols, sized version.
1322 do_allocate_commons(Layout*);
1324 // Allocate the common symbols from one list.
1327 do_allocate_commons_list(Layout*, bool is_tls, Commons_type*);
1329 // Implement detect_odr_violations.
1330 template<int size, bool big_endian>
1332 sized_detect_odr_violations() const;
1334 // Finalize symbols specialized for size.
1337 sized_finalize(off_t, Stringpool*, unsigned int*);
1339 // Finalize a symbol. Return whether it should be added to the
1343 sized_finalize_symbol(Symbol*);
1345 // Add a symbol the final symtab by setting its index.
1348 add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
1350 // Write globals specialized for size and endianness.
1351 template<int size, bool big_endian>
1353 sized_write_globals(const Input_objects*, const Stringpool*,
1354 const Stringpool*, Output_symtab_xindex*,
1355 Output_symtab_xindex*, Output_file*) const;
1357 // Write out a symbol to P.
1358 template<int size, bool big_endian>
1360 sized_write_symbol(Sized_symbol<size>*,
1361 typename elfcpp::Elf_types<size>::Elf_Addr value,
1363 const Stringpool*, unsigned char* p) const;
1365 // Possibly warn about an undefined symbol from a dynamic object.
1367 warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const;
1369 // Write out a section symbol, specialized for size and endianness.
1370 template<int size, bool big_endian>
1372 sized_write_section_symbol(const Output_section*, Output_symtab_xindex*,
1373 Output_file*, off_t) const;
1375 // The type of the symbol hash table.
1377 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1379 struct Symbol_table_hash
1382 operator()(const Symbol_table_key&) const;
1385 struct Symbol_table_eq
1388 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1391 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1392 Symbol_table_eq> Symbol_table_type;
1394 // The type of the list of symbols which have been forced local.
1395 typedef std::vector<Symbol*> Forced_locals;
1397 // A map from symbols with COPY relocs to the dynamic objects where
1398 // they are defined.
1399 typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1401 // A map from symbol name (as a pointer into the namepool) to all
1402 // the locations the symbols is (weakly) defined (and certain other
1403 // conditions are met). This map will be used later to detect
1404 // possible One Definition Rule (ODR) violations.
1405 struct Symbol_location
1407 Object* object; // Object where the symbol is defined.
1408 unsigned int shndx; // Section-in-object where the symbol is defined.
1409 off_t offset; // Offset-in-section where the symbol is defined.
1410 bool operator==(const Symbol_location& that) const
1412 return (this->object == that.object
1413 && this->shndx == that.shndx
1414 && this->offset == that.offset);
1418 struct Symbol_location_hash
1420 size_t operator()(const Symbol_location& loc) const
1421 { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1424 typedef Unordered_map<const char*,
1425 Unordered_set<Symbol_location, Symbol_location_hash> >
1428 // We increment this every time we see a new undefined symbol, for
1429 // use in archive groups.
1431 // The index of the first global symbol in the output file.
1432 unsigned int first_global_index_;
1433 // The file offset within the output symtab section where we should
1436 // The number of global symbols we want to write out.
1437 unsigned int output_count_;
1438 // The file offset of the global dynamic symbols, or 0 if none.
1439 off_t dynamic_offset_;
1440 // The index of the first global dynamic symbol.
1441 unsigned int first_dynamic_global_index_;
1442 // The number of global dynamic symbols, or 0 if none.
1443 unsigned int dynamic_count_;
1444 // The symbol hash table.
1445 Symbol_table_type table_;
1446 // A pool of symbol names. This is used for all global symbols.
1447 // Entries in the hash table point into this pool.
1448 Stringpool namepool_;
1449 // Forwarding symbols.
1450 Unordered_map<const Symbol*, Symbol*> forwarders_;
1451 // Weak aliases. A symbol in this list points to the next alias.
1452 // The aliases point to each other in a circular list.
1453 Unordered_map<Symbol*, Symbol*> weak_aliases_;
1454 // We don't expect there to be very many common symbols, so we keep
1455 // a list of them. When we find a common symbol we add it to this
1456 // list. It is possible that by the time we process the list the
1457 // symbol is no longer a common symbol. It may also have become a
1459 Commons_type commons_;
1460 // This is like the commons_ field, except that it holds TLS common
1462 Commons_type tls_commons_;
1463 // A list of symbols which have been forced to be local. We don't
1464 // expect there to be very many of them, so we keep a list of them
1465 // rather than walking the whole table to find them.
1466 Forced_locals forced_locals_;
1467 // Manage symbol warnings.
1469 // Manage potential One Definition Rule (ODR) violations.
1470 Odr_map candidate_odr_violations_;
1472 // When we emit a COPY reloc for a symbol, we define it in an
1473 // Output_data. When it's time to emit version information for it,
1474 // we need to know the dynamic object in which we found the original
1475 // definition. This maps symbols with COPY relocs to the dynamic
1476 // object where they were defined.
1477 Copied_symbol_dynobjs copied_symbol_dynobjs_;
1478 // Information parsed from the version script, if any.
1479 const Version_script_info& version_script_;
1482 // We inline get_sized_symbol for efficiency.
1486 Symbol_table::get_sized_symbol(Symbol* sym) const
1488 gold_assert(size == parameters->target().get_size());
1489 return static_cast<Sized_symbol<size>*>(sym);
1493 const Sized_symbol<size>*
1494 Symbol_table::get_sized_symbol(const Symbol* sym) const
1496 gold_assert(size == parameters->target().get_size());
1497 return static_cast<const Sized_symbol<size>*>(sym);
1500 } // End namespace gold.
1502 #endif // !defined(GOLD_SYMTAB_H)