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;
56 // The base class of an entry in the symbol table. The symbol table
57 // can have a lot of entries, so we don't want this class to big.
58 // Size dependent fields can be found in the template class
59 // Sized_symbol. Targets may support their own derived classes.
64 // Because we want the class to be small, we don't use any virtual
65 // functions. But because symbols can be defined in different
66 // places, we need to classify them. This enum is the different
67 // sources of symbols we support.
70 // Symbol defined in a relocatable or dynamic input file--this is
71 // the most common case.
73 // Symbol defined in an Output_data, a special section created by
76 // Symbol defined in an Output_segment, with no associated
79 // Symbol value is constant.
83 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
85 enum Segment_offset_base
87 // From the start of the segment.
89 // From the end of the segment.
91 // From the filesz of the segment--i.e., after the loaded bytes
92 // but before the bytes which are allocated but zeroed.
96 // Return the symbol name.
99 { return this->name_; }
101 // Return the (ANSI) demangled version of the name, if
102 // parameters.demangle() is true. Otherwise, return the name. This
103 // is intended to be used only for logging errors, so it's not
106 demangled_name() const;
108 // Return the symbol version. This will return NULL for an
109 // unversioned symbol.
112 { return this->version_; }
114 // Return whether this version is the default for this symbol name
115 // (eg, "foo@@V2" is a default version; "foo@V1" is not). Only
116 // meaningful for versioned symbols.
120 gold_assert(this->version_ != NULL);
121 return this->is_def_;
124 // Set that this version is the default for this symbol name.
127 { this->is_def_ = true; }
129 // Return the symbol source.
132 { return this->source_; }
134 // Return the object with which this symbol is associated.
138 gold_assert(this->source_ == FROM_OBJECT);
139 return this->u_.from_object.object;
142 // Return the index of the section in the input relocatable or
143 // dynamic object file.
147 gold_assert(this->source_ == FROM_OBJECT);
148 return this->u_.from_object.shndx;
151 // Return the output data section with which this symbol is
152 // associated, if the symbol was specially defined with respect to
153 // an output data section.
157 gold_assert(this->source_ == IN_OUTPUT_DATA);
158 return this->u_.in_output_data.output_data;
161 // If this symbol was defined with respect to an output data
162 // section, return whether the value is an offset from end.
164 offset_is_from_end() const
166 gold_assert(this->source_ == IN_OUTPUT_DATA);
167 return this->u_.in_output_data.offset_is_from_end;
170 // Return the output segment with which this symbol is associated,
171 // if the symbol was specially defined with respect to an output
174 output_segment() const
176 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
177 return this->u_.in_output_segment.output_segment;
180 // If this symbol was defined with respect to an output segment,
181 // return the offset base.
185 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
186 return this->u_.in_output_segment.offset_base;
189 // Return the symbol binding.
192 { return this->binding_; }
194 // Return the symbol type.
197 { return this->type_; }
199 // Return the symbol visibility.
202 { return this->visibility_; }
204 // Return the non-visibility part of the st_other field.
207 { return this->nonvis_; }
209 // Return whether this symbol is a forwarder. This will never be
210 // true of a symbol found in the hash table, but may be true of
211 // symbol pointers attached to object files.
214 { return this->is_forwarder_; }
216 // Mark this symbol as a forwarder.
219 { this->is_forwarder_ = true; }
221 // Return whether this symbol has an alias in the weak aliases table
225 { return this->has_alias_; }
227 // Mark this symbol as having an alias.
230 { this->has_alias_ = true; }
232 // Return whether this symbol needs an entry in the dynamic symbol
235 needs_dynsym_entry() const
237 return (this->needs_dynsym_entry_
238 || (this->in_reg() && this->in_dyn()));
241 // Mark this symbol as needing an entry in the dynamic symbol table.
243 set_needs_dynsym_entry()
244 { this->needs_dynsym_entry_ = true; }
246 // Return whether this symbol should be added to the dynamic symbol
249 should_add_dynsym_entry() const;
251 // Return whether this symbol has been seen in a regular object.
254 { return this->in_reg_; }
256 // Mark this symbol as having been seen in a regular object.
259 { this->in_reg_ = true; }
261 // Return whether this symbol has been seen in a dynamic object.
264 { return this->in_dyn_; }
266 // Mark this symbol as having been seen in a dynamic object.
269 { this->in_dyn_ = true; }
271 // Return the index of this symbol in the output file symbol table.
272 // A value of -1U means that this symbol is not going into the
273 // output file. This starts out as zero, and is set to a non-zero
274 // value by Symbol_table::finalize. It is an error to ask for the
275 // symbol table index before it has been set.
279 gold_assert(this->symtab_index_ != 0);
280 return this->symtab_index_;
283 // Set the index of the symbol in the output file symbol table.
285 set_symtab_index(unsigned int index)
287 gold_assert(index != 0);
288 this->symtab_index_ = index;
291 // Return whether this symbol already has an index in the output
292 // file symbol table.
294 has_symtab_index() const
295 { return this->symtab_index_ != 0; }
297 // Return the index of this symbol in the dynamic symbol table. A
298 // value of -1U means that this symbol is not going into the dynamic
299 // symbol table. This starts out as zero, and is set to a non-zero
300 // during Layout::finalize. It is an error to ask for the dynamic
301 // symbol table index before it has been set.
305 gold_assert(this->dynsym_index_ != 0);
306 return this->dynsym_index_;
309 // Set the index of the symbol in the dynamic symbol table.
311 set_dynsym_index(unsigned int index)
313 gold_assert(index != 0);
314 this->dynsym_index_ = index;
317 // Return whether this symbol already has an index in the dynamic
320 has_dynsym_index() const
321 { return this->dynsym_index_ != 0; }
323 // Return whether this symbol has an entry in the GOT section.
324 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
326 has_got_offset(unsigned int got_type) const
327 { return this->got_offsets_.get_offset(got_type) != -1U; }
329 // Return the offset into the GOT section of this symbol.
331 got_offset(unsigned int got_type) const
333 unsigned int got_offset = this->got_offsets_.get_offset(got_type);
334 gold_assert(got_offset != -1U);
338 // Set the GOT offset of this symbol.
340 set_got_offset(unsigned int got_type, unsigned int got_offset)
341 { this->got_offsets_.set_offset(got_type, got_offset); }
343 // Return whether this symbol has an entry in the PLT section.
345 has_plt_offset() const
346 { return this->has_plt_offset_; }
348 // Return the offset into the PLT section of this symbol.
352 gold_assert(this->has_plt_offset());
353 return this->plt_offset_;
356 // Set the PLT offset of this symbol.
358 set_plt_offset(unsigned int plt_offset)
360 this->has_plt_offset_ = true;
361 this->plt_offset_ = plt_offset;
364 // Return whether this dynamic symbol needs a special value in the
365 // dynamic symbol table.
367 needs_dynsym_value() const
368 { return this->needs_dynsym_value_; }
370 // Set that this dynamic symbol needs a special value in the dynamic
373 set_needs_dynsym_value()
375 gold_assert(this->object()->is_dynamic());
376 this->needs_dynsym_value_ = true;
379 // Return true if the final value of this symbol is known at link
382 final_value_is_known() const;
384 // Return whether this is a defined symbol (not undefined or
389 return (this->source_ != FROM_OBJECT
390 || (this->shndx() != elfcpp::SHN_UNDEF
391 && this->shndx() != elfcpp::SHN_COMMON));
394 // Return true if this symbol is from a dynamic object.
396 is_from_dynobj() const
398 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
401 // Return whether this is an undefined symbol.
405 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
408 // Return whether this is a weak undefined symbol.
410 is_weak_undefined() const
412 return (this->source_ == FROM_OBJECT
413 && this->binding() == elfcpp::STB_WEAK
414 && this->shndx() == elfcpp::SHN_UNDEF);
417 // Return whether this is an absolute symbol.
421 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_ABS;
424 // Return whether this is a common symbol.
428 return (this->source_ == FROM_OBJECT
429 && (this->shndx() == elfcpp::SHN_COMMON
430 || this->type_ == elfcpp::STT_COMMON));
433 // Return whether this symbol can be seen outside this object.
435 is_externally_visible() const
437 return (this->visibility_ == elfcpp::STV_DEFAULT
438 || this->visibility_ == elfcpp::STV_PROTECTED);
441 // Return true if this symbol can be preempted by a definition in
442 // another link unit.
444 is_preemptible() const
446 // It doesn't make sense to ask whether a symbol defined in
447 // another object is preemptible.
448 gold_assert(!this->is_from_dynobj());
450 // It doesn't make sense to ask whether an undefined symbol
452 gold_assert(!this->is_undefined());
454 return (this->visibility_ != elfcpp::STV_INTERNAL
455 && this->visibility_ != elfcpp::STV_HIDDEN
456 && this->visibility_ != elfcpp::STV_PROTECTED
457 && !this->is_forced_local_
458 && parameters->options().shared()
459 && !parameters->options().Bsymbolic());
462 // Return true if this symbol is a function that needs a PLT entry.
463 // If the symbol is defined in a dynamic object or if it is subject
464 // to pre-emption, we need to make a PLT entry. If we're doing a
465 // static link, we don't create PLT entries.
467 needs_plt_entry() const
469 return (!parameters->doing_static_link()
470 && this->type() == elfcpp::STT_FUNC
471 && (this->is_from_dynobj()
472 || this->is_undefined()
473 || this->is_preemptible()));
476 // When determining whether a reference to a symbol needs a dynamic
477 // relocation, we need to know several things about the reference.
478 // These flags may be or'ed together.
481 // Reference to the symbol's absolute address.
483 // A non-PIC reference.
489 // Given a direct absolute or pc-relative static relocation against
490 // the global symbol, this function returns whether a dynamic relocation
494 needs_dynamic_reloc(int flags) const
496 // No dynamic relocations in a static link!
497 if (parameters->doing_static_link())
500 // A reference to a weak undefined symbol from an executable should be
501 // statically resolved to 0, and does not need a dynamic relocation.
502 // This matches gnu ld behavior.
503 if (this->is_weak_undefined() && !parameters->options().shared())
506 // A reference to an absolute symbol does not need a dynamic relocation.
507 if (this->is_absolute())
510 // An absolute reference within a position-independent output file
511 // will need a dynamic relocation.
512 if ((flags & ABSOLUTE_REF)
513 && parameters->options().output_is_position_independent())
516 // A function call that can branch to a local PLT entry does not need
517 // a dynamic relocation. A non-pic pc-relative function call in a
518 // shared library cannot use a PLT entry.
519 if ((flags & FUNCTION_CALL)
520 && this->has_plt_offset()
521 && !((flags & NON_PIC_REF) && parameters->options().shared()))
524 // A reference to any PLT entry in a non-position-independent executable
525 // does not need a dynamic relocation.
526 if (!parameters->options().output_is_position_independent()
527 && this->has_plt_offset())
530 // A reference to a symbol defined in a dynamic object or to a
531 // symbol that is preemptible will need a dynamic relocation.
532 if (this->is_from_dynobj()
533 || this->is_undefined()
534 || this->is_preemptible())
537 // For all other cases, return FALSE.
541 // Given a direct absolute static relocation against
542 // the global symbol, where a dynamic relocation is needed, this
543 // function returns whether a relative dynamic relocation can be used.
544 // The caller must determine separately whether the static relocation
545 // is compatible with a relative relocation.
548 can_use_relative_reloc(bool is_function_call) const
550 // A function call that can branch to a local PLT entry can
551 // use a RELATIVE relocation.
552 if (is_function_call && this->has_plt_offset())
555 // A reference to a symbol defined in a dynamic object or to a
556 // symbol that is preemptible can not use a RELATIVE relocaiton.
557 if (this->is_from_dynobj()
558 || this->is_undefined()
559 || this->is_preemptible())
562 // For all other cases, return TRUE.
566 // Return the output section where this symbol is defined. Return
567 // NULL if the symbol has an absolute value.
569 output_section() const;
571 // Set the symbol's output section. This is used for symbols
572 // defined in scripts. This should only be called after the symbol
573 // table has been finalized.
575 set_output_section(Output_section*);
577 // Return whether there should be a warning for references to this
581 { return this->has_warning_; }
583 // Mark this symbol as having a warning.
586 { this->has_warning_ = true; }
588 // Return whether this symbol is defined by a COPY reloc from a
591 is_copied_from_dynobj() const
592 { return this->is_copied_from_dynobj_; }
594 // Mark this symbol as defined by a COPY reloc.
596 set_is_copied_from_dynobj()
597 { this->is_copied_from_dynobj_ = true; }
599 // Return whether this symbol is forced to visibility STB_LOCAL
600 // by a "local:" entry in a version script.
602 is_forced_local() const
603 { return this->is_forced_local_; }
605 // Mark this symbol as forced to STB_LOCAL visibility.
607 set_is_forced_local()
608 { this->is_forced_local_ = true; }
611 // Instances of this class should always be created at a specific
614 { memset(this, 0, sizeof *this); }
616 // Initialize the general fields.
618 init_fields(const char* name, const char* version,
619 elfcpp::STT type, elfcpp::STB binding,
620 elfcpp::STV visibility, unsigned char nonvis);
622 // Initialize fields from an ELF symbol in OBJECT.
623 template<int size, bool big_endian>
625 init_base(const char *name, const char* version, Object* object,
626 const elfcpp::Sym<size, big_endian>&);
628 // Initialize fields for an Output_data.
630 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
631 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
633 // Initialize fields for an Output_segment.
635 init_base(const char* name, Output_segment* os, elfcpp::STT type,
636 elfcpp::STB binding, elfcpp::STV visibility,
637 unsigned char nonvis, Segment_offset_base offset_base);
639 // Initialize fields for a constant.
641 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
642 elfcpp::STV visibility, unsigned char nonvis);
644 // Override existing symbol.
645 template<int size, bool big_endian>
647 override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
648 const char* version);
650 // Override existing symbol with a special symbol.
652 override_base_with_special(const Symbol* from);
654 // Allocate a common symbol by giving it a location in the output
657 allocate_base_common(Output_data*);
660 Symbol(const Symbol&);
661 Symbol& operator=(const Symbol&);
663 // Symbol name (expected to point into a Stringpool).
665 // Symbol version (expected to point into a Stringpool). This may
667 const char* version_;
671 // This struct is used if SOURCE_ == FROM_OBJECT.
674 // Object in which symbol is defined, or in which it was first
677 // Section number in object_ in which symbol is defined.
681 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
684 // Output_data in which symbol is defined. Before
685 // Layout::finalize the symbol's value is an offset within the
687 Output_data* output_data;
688 // True if the offset is from the end, false if the offset is
689 // from the beginning.
690 bool offset_is_from_end;
693 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
696 // Output_segment in which the symbol is defined. Before
697 // Layout::finalize the symbol's value is an offset.
698 Output_segment* output_segment;
699 // The base to use for the offset before Layout::finalize.
700 Segment_offset_base offset_base;
704 // The index of this symbol in the output file. If the symbol is
705 // not going into the output file, this value is -1U. This field
706 // starts as always holding zero. It is set to a non-zero value by
707 // Symbol_table::finalize.
708 unsigned int symtab_index_;
710 // The index of this symbol in the dynamic symbol table. If the
711 // symbol is not going into the dynamic symbol table, this value is
712 // -1U. This field starts as always holding zero. It is set to a
713 // non-zero value during Layout::finalize.
714 unsigned int dynsym_index_;
716 // If this symbol has an entry in the GOT section (has_got_offset_
717 // is true), this holds the offset from the start of the GOT section.
718 // A symbol may have more than one GOT offset (e.g., when mixing
719 // modules compiled with two different TLS models), but will usually
721 Got_offset_list got_offsets_;
723 // If this symbol has an entry in the PLT section (has_plt_offset_
724 // is true), then this is the offset from the start of the PLT
726 unsigned int plt_offset_;
729 elfcpp::STT type_ : 4;
731 elfcpp::STB binding_ : 4;
732 // Symbol visibility.
733 elfcpp::STV visibility_ : 2;
734 // Rest of symbol st_other field.
735 unsigned int nonvis_ : 6;
736 // The type of symbol.
738 // True if this symbol always requires special target-specific
740 bool is_target_special_ : 1;
741 // True if this is the default version of the symbol.
743 // True if this symbol really forwards to another symbol. This is
744 // used when we discover after the fact that two different entries
745 // in the hash table really refer to the same symbol. This will
746 // never be set for a symbol found in the hash table, but may be set
747 // for a symbol found in the list of symbols attached to an Object.
748 // It forwards to the symbol found in the forwarders_ map of
750 bool is_forwarder_ : 1;
751 // True if the symbol has an alias in the weak_aliases table in
754 // True if this symbol needs to be in the dynamic symbol table.
755 bool needs_dynsym_entry_ : 1;
756 // True if we've seen this symbol in a regular object.
758 // True if we've seen this symbol in a dynamic object.
760 // True if the symbol has an entry in the PLT section.
761 bool has_plt_offset_ : 1;
762 // True if this is a dynamic symbol which needs a special value in
763 // the dynamic symbol table.
764 bool needs_dynsym_value_ : 1;
765 // True if there is a warning for this symbol.
766 bool has_warning_ : 1;
767 // True if we are using a COPY reloc for this symbol, so that the
768 // real definition lives in a dynamic object.
769 bool is_copied_from_dynobj_ : 1;
770 // True if this symbol was forced to local visibility by a version
772 bool is_forced_local_ : 1;
775 // The parts of a symbol which are size specific. Using a template
776 // derived class like this helps us use less space on a 32-bit system.
779 class Sized_symbol : public Symbol
782 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
783 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
788 // Initialize fields from an ELF symbol in OBJECT.
789 template<bool big_endian>
791 init(const char *name, const char* version, Object* object,
792 const elfcpp::Sym<size, big_endian>&);
794 // Initialize fields for an Output_data.
796 init(const char* name, Output_data*, Value_type value, Size_type symsize,
797 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
798 bool offset_is_from_end);
800 // Initialize fields for an Output_segment.
802 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
803 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
804 Segment_offset_base offset_base);
806 // Initialize fields for a constant.
808 init(const char* name, Value_type value, Size_type symsize,
809 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
811 // Override existing symbol.
812 template<bool big_endian>
814 override(const elfcpp::Sym<size, big_endian>&, Object* object,
815 const char* version);
817 // Override existing symbol with a special symbol.
819 override_with_special(const Sized_symbol<size>*);
821 // Return the symbol's value.
824 { return this->value_; }
826 // Return the symbol's size (we can't call this 'size' because that
827 // is a template parameter).
830 { return this->symsize_; }
832 // Set the symbol size. This is used when resolving common symbols.
834 set_symsize(Size_type symsize)
835 { this->symsize_ = symsize; }
837 // Set the symbol value. This is called when we store the final
838 // values of the symbols into the symbol table.
840 set_value(Value_type value)
841 { this->value_ = value; }
843 // Allocate a common symbol by giving it a location in the output
846 allocate_common(Output_data*, Value_type value);
849 Sized_symbol(const Sized_symbol&);
850 Sized_symbol& operator=(const Sized_symbol&);
852 // Symbol value. Before Layout::finalize this is the offset in the
853 // input section. This is set to the final value during
860 // A struct describing a symbol defined by the linker, where the value
861 // of the symbol is defined based on an output section. This is used
862 // for symbols defined by the linker, like "_init_array_start".
864 struct Define_symbol_in_section
868 // The name of the output section with which this symbol should be
869 // associated. If there is no output section with that name, the
870 // symbol will be defined as zero.
871 const char* output_section;
872 // The offset of the symbol within the output section. This is an
873 // offset from the start of the output section, unless start_at_end
874 // is true, in which case this is an offset from the end of the
877 // The size of the symbol.
881 // The symbol binding.
883 // The symbol visibility.
884 elfcpp::STV visibility;
885 // The rest of the st_other field.
886 unsigned char nonvis;
887 // If true, the value field is an offset from the end of the output
889 bool offset_is_from_end;
890 // If true, this symbol is defined only if we see a reference to it.
894 // A struct describing a symbol defined by the linker, where the value
895 // of the symbol is defined based on a segment. This is used for
896 // symbols defined by the linker, like "_end". We describe the
897 // segment with which the symbol should be associated by its
898 // characteristics. If no segment meets these characteristics, the
899 // symbol will be defined as zero. If there is more than one segment
900 // which meets these characteristics, we will use the first one.
902 struct Define_symbol_in_segment
906 // The segment type where the symbol should be defined, typically
908 elfcpp::PT segment_type;
909 // Bitmask of segment flags which must be set.
910 elfcpp::PF segment_flags_set;
911 // Bitmask of segment flags which must be clear.
912 elfcpp::PF segment_flags_clear;
913 // The offset of the symbol within the segment. The offset is
914 // calculated from the position set by offset_base.
916 // The size of the symbol.
920 // The symbol binding.
922 // The symbol visibility.
923 elfcpp::STV visibility;
924 // The rest of the st_other field.
925 unsigned char nonvis;
926 // The base from which we compute the offset.
927 Symbol::Segment_offset_base offset_base;
928 // If true, this symbol is defined only if we see a reference to it.
932 // This class manages warnings. Warnings are a GNU extension. When
933 // we see a section named .gnu.warning.SYM in an object file, and if
934 // we wind using the definition of SYM from that object file, then we
935 // will issue a warning for any relocation against SYM from a
936 // different object file. The text of the warning is the contents of
937 // the section. This is not precisely the definition used by the old
938 // GNU linker; the old GNU linker treated an occurrence of
939 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
940 // would trigger a warning on any reference. However, it was
941 // inconsistent in that a warning in a dynamic object only triggered
942 // if there was no definition in a regular object. This linker is
943 // different in that we only issue a warning if we use the symbol
944 // definition from the same object file as the warning section.
953 // Add a warning for symbol NAME in object OBJ. WARNING is the text
956 add_warning(Symbol_table* symtab, const char* name, Object* obj,
957 const std::string& warning);
959 // For each symbol for which we should give a warning, make a note
962 note_warnings(Symbol_table* symtab);
964 // Issue a warning for a reference to SYM at RELINFO's location.
965 template<int size, bool big_endian>
967 issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
968 size_t relnum, off_t reloffset) const;
971 Warnings(const Warnings&);
972 Warnings& operator=(const Warnings&);
974 // What we need to know to get the warning text.
975 struct Warning_location
977 // The object the warning is in.
983 : object(NULL), text()
987 set(Object* o, const std::string& t)
994 // A mapping from warning symbol names (canonicalized in
995 // Symbol_table's namepool_ field) to warning information.
996 typedef Unordered_map<const char*, Warning_location> Warning_table;
998 Warning_table warnings_;
1001 // The main linker symbol table.
1006 // COUNT is an estimate of how many symbosl will be inserted in the
1007 // symbol table. It's ok to put 0 if you don't know; a correct
1008 // guess will just save some CPU by reducing hashtable resizes.
1009 Symbol_table(unsigned int count, const Version_script_info& version_script);
1013 // Add COUNT external symbols from the relocatable object RELOBJ to
1014 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
1015 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
1016 // point to the symbols in the symbol table.
1017 template<int size, bool big_endian>
1019 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
1020 const unsigned char* syms, size_t count,
1021 const char* sym_names, size_t sym_name_size,
1022 typename Sized_relobj<size, big_endian>::Symbols*);
1024 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1025 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1026 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1027 // symbol version data.
1028 template<int size, bool big_endian>
1030 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
1031 const unsigned char* syms, size_t count,
1032 const char* sym_names, size_t sym_name_size,
1033 const unsigned char* versym, size_t versym_size,
1034 const std::vector<const char*>*);
1036 // Define a special symbol based on an Output_data. It is a
1037 // multiple definition error if this symbol is already defined.
1039 define_in_output_data(const char* name, const char* version,
1040 Output_data*, uint64_t value, uint64_t symsize,
1041 elfcpp::STT type, elfcpp::STB binding,
1042 elfcpp::STV visibility, unsigned char nonvis,
1043 bool offset_is_from_end, bool only_if_ref);
1045 // Define a special symbol based on an Output_segment. It is a
1046 // multiple definition error if this symbol is already defined.
1048 define_in_output_segment(const char* name, const char* version,
1049 Output_segment*, uint64_t value, uint64_t symsize,
1050 elfcpp::STT type, elfcpp::STB binding,
1051 elfcpp::STV visibility, unsigned char nonvis,
1052 Symbol::Segment_offset_base, bool only_if_ref);
1054 // Define a special symbol with a constant value. It is a multiple
1055 // definition error if this symbol is already defined.
1057 define_as_constant(const char* name, const char* version,
1058 uint64_t value, uint64_t symsize, elfcpp::STT type,
1059 elfcpp::STB binding, elfcpp::STV visibility,
1060 unsigned char nonvis, bool only_if_ref,
1061 bool force_override);
1063 // Define a set of symbols in output sections. If ONLY_IF_REF is
1064 // true, only define them if they are referenced.
1066 define_symbols(const Layout*, int count, const Define_symbol_in_section*,
1069 // Define a set of symbols in output segments. If ONLY_IF_REF is
1070 // true, only defined them if they are referenced.
1072 define_symbols(const Layout*, int count, const Define_symbol_in_segment*,
1075 // Define SYM using a COPY reloc. POSD is the Output_data where the
1076 // symbol should be defined--typically a .dyn.bss section. VALUE is
1077 // the offset within POSD.
1080 define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
1081 typename elfcpp::Elf_types<size>::Elf_Addr);
1083 // Look up a symbol.
1085 lookup(const char*, const char* version = NULL) const;
1087 // Return the real symbol associated with the forwarder symbol FROM.
1089 resolve_forwards(const Symbol* from) const;
1091 // Return the sized version of a symbol in this table.
1094 get_sized_symbol(Symbol*) const;
1097 const Sized_symbol<size>*
1098 get_sized_symbol(const Symbol*) const;
1100 // Return the count of undefined symbols seen.
1102 saw_undefined() const
1103 { return this->saw_undefined_; }
1105 // Allocate the common symbols
1107 allocate_commons(const General_options&, Layout*);
1109 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1112 add_warning(const char* name, Object* obj, const std::string& warning)
1113 { this->warnings_.add_warning(this, name, obj, warning); }
1115 // Canonicalize a symbol name for use in the hash table.
1117 canonicalize_name(const char* name)
1118 { return this->namepool_.add(name, true, NULL); }
1120 // Possibly issue a warning for a reference to SYM at LOCATION which
1122 template<int size, bool big_endian>
1124 issue_warning(const Symbol* sym,
1125 const Relocate_info<size, big_endian>* relinfo,
1126 size_t relnum, off_t reloffset) const
1127 { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
1129 // Check candidate_odr_violations_ to find symbols with the same name
1130 // but apparently different definitions (different source-file/line-no).
1132 detect_odr_violations(const Task*, const char* output_file_name) const;
1134 // SYM is defined using a COPY reloc. Return the dynamic object
1135 // where the original definition was found.
1137 get_copy_source(const Symbol* sym) const;
1139 // Set the dynamic symbol indexes. INDEX is the index of the first
1140 // global dynamic symbol. Pointers to the symbols are stored into
1141 // the vector. The names are stored into the Stringpool. This
1142 // returns an updated dynamic symbol index.
1144 set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*,
1145 Stringpool*, Versions*);
1147 // Finalize the symbol table after we have set the final addresses
1148 // of all the input sections. This sets the final symbol indexes,
1149 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1150 // index of the first global symbol. OFF is the file offset of the
1151 // global symbol table, DYNOFF is the offset of the globals in the
1152 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1153 // global dynamic symbol, and DYNCOUNT is the number of global
1154 // dynamic symbols. This records the parameters, and returns the
1155 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1158 finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
1159 Stringpool* pool, unsigned int *plocal_symcount);
1161 // Write out the global symbols.
1163 write_globals(const Input_objects*, const Stringpool*, const Stringpool*,
1164 Output_file*) const;
1166 // Write out a section symbol. Return the updated offset.
1168 write_section_symbol(const Output_section*, Output_file*, off_t) const;
1170 // Dump statistical information to stderr.
1172 print_stats() const;
1174 // Return the version script information.
1175 const Version_script_info&
1176 version_script() const
1177 { return version_script_; }
1180 Symbol_table(const Symbol_table&);
1181 Symbol_table& operator=(const Symbol_table&);
1183 // Make FROM a forwarder symbol to TO.
1185 make_forwarder(Symbol* from, Symbol* to);
1188 template<int size, bool big_endian>
1190 add_from_object(Object*, const char *name, Stringpool::Key name_key,
1191 const char *version, Stringpool::Key version_key,
1192 bool def, const elfcpp::Sym<size, big_endian>& sym,
1193 const elfcpp::Sym<size, big_endian>& orig_sym);
1196 template<int size, bool big_endian>
1198 resolve(Sized_symbol<size>* to,
1199 const elfcpp::Sym<size, big_endian>& sym,
1200 const elfcpp::Sym<size, big_endian>& orig_sym,
1201 Object*, const char* version);
1203 template<int size, bool big_endian>
1205 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
1206 const char* version);
1208 // Record that a symbol is forced to be local by a version script.
1210 force_local(Symbol*);
1212 // Adjust NAME and *NAME_KEY for wrapping.
1214 wrap_symbol(Object* object, const char*, Stringpool::Key* name_key);
1216 // Whether we should override a symbol, based on flags in
1219 should_override(const Symbol*, unsigned int, Object*, bool*);
1221 // Override a symbol.
1222 template<int size, bool big_endian>
1224 override(Sized_symbol<size>* tosym,
1225 const elfcpp::Sym<size, big_endian>& fromsym,
1226 Object* object, const char* version);
1228 // Whether we should override a symbol with a special symbol which
1229 // is automatically defined by the linker.
1231 should_override_with_special(const Symbol*);
1233 // Override a symbol with a special symbol.
1236 override_with_special(Sized_symbol<size>* tosym,
1237 const Sized_symbol<size>* fromsym);
1239 // Record all weak alias sets for a dynamic object.
1242 record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1244 // Define a special symbol.
1245 template<int size, bool big_endian>
1247 define_special_symbol(const char** pname, const char** pversion,
1248 bool only_if_ref, Sized_symbol<size>** poldsym);
1250 // Define a symbol in an Output_data, sized version.
1253 do_define_in_output_data(const char* name, const char* version, Output_data*,
1254 typename elfcpp::Elf_types<size>::Elf_Addr value,
1255 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1256 elfcpp::STT type, elfcpp::STB binding,
1257 elfcpp::STV visibility, unsigned char nonvis,
1258 bool offset_is_from_end, bool only_if_ref);
1260 // Define a symbol in an Output_segment, sized version.
1263 do_define_in_output_segment(
1264 const char* name, const char* version, Output_segment* os,
1265 typename elfcpp::Elf_types<size>::Elf_Addr value,
1266 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1267 elfcpp::STT type, elfcpp::STB binding,
1268 elfcpp::STV visibility, unsigned char nonvis,
1269 Symbol::Segment_offset_base offset_base, bool only_if_ref);
1271 // Define a symbol as a constant, sized version.
1274 do_define_as_constant(
1275 const char* name, const char* version,
1276 typename elfcpp::Elf_types<size>::Elf_Addr value,
1277 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1278 elfcpp::STT type, elfcpp::STB binding,
1279 elfcpp::STV visibility, unsigned char nonvis,
1280 bool only_if_ref, bool force_override);
1282 // Allocate the common symbols, sized version.
1285 do_allocate_commons(const General_options&, Layout*);
1287 // Implement detect_odr_violations.
1288 template<int size, bool big_endian>
1290 sized_detect_odr_violations() const;
1292 // Finalize symbols specialized for size.
1295 sized_finalize(off_t, Stringpool*, unsigned int*);
1297 // Finalize a symbol. Return whether it should be added to the
1301 sized_finalize_symbol(Symbol*);
1303 // Add a symbol the final symtab by setting its index.
1306 add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
1308 // Write globals specialized for size and endianness.
1309 template<int size, bool big_endian>
1311 sized_write_globals(const Input_objects*, const Stringpool*,
1312 const Stringpool*, Output_file*) const;
1314 // Write out a symbol to P.
1315 template<int size, bool big_endian>
1317 sized_write_symbol(Sized_symbol<size>*,
1318 typename elfcpp::Elf_types<size>::Elf_Addr value,
1320 const Stringpool*, unsigned char* p) const;
1322 // Possibly warn about an undefined symbol from a dynamic object.
1324 warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const;
1326 // Write out a section symbol, specialized for size and endianness.
1327 template<int size, bool big_endian>
1329 sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1331 // The type of the symbol hash table.
1333 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1335 struct Symbol_table_hash
1338 operator()(const Symbol_table_key&) const;
1341 struct Symbol_table_eq
1344 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1347 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1348 Symbol_table_eq> Symbol_table_type;
1350 // The type of the list of common symbols.
1351 typedef std::vector<Symbol*> Commons_type;
1353 // The type of the list of symbols which have been forced local.
1354 typedef std::vector<Symbol*> Forced_locals;
1356 // A map from symbols with COPY relocs to the dynamic objects where
1357 // they are defined.
1358 typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1360 // A map from symbol name (as a pointer into the namepool) to all
1361 // the locations the symbols is (weakly) defined (and certain other
1362 // conditions are met). This map will be used later to detect
1363 // possible One Definition Rule (ODR) violations.
1364 struct Symbol_location
1366 Object* object; // Object where the symbol is defined.
1367 unsigned int shndx; // Section-in-object where the symbol is defined.
1368 off_t offset; // Offset-in-section where the symbol is defined.
1369 bool operator==(const Symbol_location& that) const
1371 return (this->object == that.object
1372 && this->shndx == that.shndx
1373 && this->offset == that.offset);
1377 struct Symbol_location_hash
1379 size_t operator()(const Symbol_location& loc) const
1380 { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1383 typedef Unordered_map<const char*,
1384 Unordered_set<Symbol_location, Symbol_location_hash> >
1387 // We increment this every time we see a new undefined symbol, for
1388 // use in archive groups.
1390 // The index of the first global symbol in the output file.
1391 unsigned int first_global_index_;
1392 // The file offset within the output symtab section where we should
1395 // The number of global symbols we want to write out.
1396 unsigned int output_count_;
1397 // The file offset of the global dynamic symbols, or 0 if none.
1398 off_t dynamic_offset_;
1399 // The index of the first global dynamic symbol.
1400 unsigned int first_dynamic_global_index_;
1401 // The number of global dynamic symbols, or 0 if none.
1402 unsigned int dynamic_count_;
1403 // The symbol hash table.
1404 Symbol_table_type table_;
1405 // A pool of symbol names. This is used for all global symbols.
1406 // Entries in the hash table point into this pool.
1407 Stringpool namepool_;
1408 // Forwarding symbols.
1409 Unordered_map<const Symbol*, Symbol*> forwarders_;
1410 // Weak aliases. A symbol in this list points to the next alias.
1411 // The aliases point to each other in a circular list.
1412 Unordered_map<Symbol*, Symbol*> weak_aliases_;
1413 // We don't expect there to be very many common symbols, so we keep
1414 // a list of them. When we find a common symbol we add it to this
1415 // list. It is possible that by the time we process the list the
1416 // symbol is no longer a common symbol. It may also have become a
1418 Commons_type commons_;
1419 // A list of symbols which have been forced to be local. We don't
1420 // expect there to be very many of them, so we keep a list of them
1421 // rather than walking the whole table to find them.
1422 Forced_locals forced_locals_;
1423 // Manage symbol warnings.
1425 // Manage potential One Definition Rule (ODR) violations.
1426 Odr_map candidate_odr_violations_;
1428 // When we emit a COPY reloc for a symbol, we define it in an
1429 // Output_data. When it's time to emit version information for it,
1430 // we need to know the dynamic object in which we found the original
1431 // definition. This maps symbols with COPY relocs to the dynamic
1432 // object where they were defined.
1433 Copied_symbol_dynobjs copied_symbol_dynobjs_;
1434 // Information parsed from the version script, if any.
1435 const Version_script_info& version_script_;
1438 // We inline get_sized_symbol for efficiency.
1442 Symbol_table::get_sized_symbol(Symbol* sym) const
1444 gold_assert(size == parameters->target().get_size());
1445 return static_cast<Sized_symbol<size>*>(sym);
1449 const Sized_symbol<size>*
1450 Symbol_table::get_sized_symbol(const Symbol* sym) const
1452 gold_assert(size == parameters->target().get_size());
1453 return static_cast<const Sized_symbol<size>*>(sym);
1456 } // End namespace gold.
1458 #endif // !defined(GOLD_SYMTAB_H)