1 // symtab.h -- the gold symbol table -*- C++ -*-
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "parameters.h"
32 #include "stringpool.h"
43 template<int size, bool big_endian>
46 template<int size, bool big_endian>
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 whether this version is the default for this symbol name.
126 set_is_default(bool def)
127 { this->is_def_ = def; }
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() const
327 { return this->has_got_offset_; }
329 // Return the offset into the GOT section of this symbol.
333 gold_assert(this->has_got_offset());
334 return this->got_offset_;
337 // Set the GOT offset of this symbol.
339 set_got_offset(unsigned int got_offset)
341 this->has_got_offset_ = true;
342 this->got_offset_ = got_offset;
345 // Return whether this TLS symbol has an entry in the GOT section for
346 // its module index or, if NEED_PAIR is true, has a pair of entries
347 // for its module index and dtv-relative offset.
349 has_tls_got_offset(bool need_pair) const
351 return (this->has_tls_mod_got_offset_
352 && (!need_pair || this->has_tls_pair_got_offset_));
355 // Return the offset into the GOT section for this symbol's TLS module
356 // index or, if NEED_PAIR is true, for the pair of entries for the
357 // module index and dtv-relative offset.
359 tls_got_offset(bool need_pair) const
361 gold_assert(this->has_tls_got_offset(need_pair));
362 return this->tls_mod_got_offset_;
365 // Set the GOT offset of this symbol.
367 set_tls_got_offset(unsigned int got_offset, bool have_pair)
369 this->has_tls_mod_got_offset_ = true;
370 this->has_tls_pair_got_offset_ = have_pair;
371 this->tls_mod_got_offset_ = got_offset;
374 // Return whether this symbol has an entry in the PLT section.
376 has_plt_offset() const
377 { return this->has_plt_offset_; }
379 // Return the offset into the PLT section of this symbol.
383 gold_assert(this->has_plt_offset());
384 return this->plt_offset_;
387 // Set the PLT offset of this symbol.
389 set_plt_offset(unsigned int plt_offset)
391 this->has_plt_offset_ = true;
392 this->plt_offset_ = plt_offset;
395 // Return whether this dynamic symbol needs a special value in the
396 // dynamic symbol table.
398 needs_dynsym_value() const
399 { return this->needs_dynsym_value_; }
401 // Set that this dynamic symbol needs a special value in the dynamic
404 set_needs_dynsym_value()
406 gold_assert(this->object()->is_dynamic());
407 this->needs_dynsym_value_ = true;
410 // Return true if the final value of this symbol is known at link
413 final_value_is_known() const;
415 // Return whether this is a defined symbol (not undefined or
420 return (this->source_ != FROM_OBJECT
421 || (this->shndx() != elfcpp::SHN_UNDEF
422 && this->shndx() != elfcpp::SHN_COMMON));
425 // Return true if this symbol is from a dynamic object.
427 is_from_dynobj() const
429 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
432 // Return whether this is an undefined symbol.
436 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
439 // Return whether this is a common symbol.
443 return (this->source_ == FROM_OBJECT
444 && (this->shndx() == elfcpp::SHN_COMMON
445 || this->type_ == elfcpp::STT_COMMON));
448 // Return whether this symbol can be seen outside this object.
450 is_externally_visible() const
452 return (this->visibility_ == elfcpp::STV_DEFAULT
453 || this->visibility_ == elfcpp::STV_PROTECTED);
456 // Return true if this symbol can be preempted by a definition in
457 // another link unit.
459 is_preemptible() const
461 // It doesn't make sense to ask whether a symbol defined in
462 // another object is preemptible.
463 gold_assert(!this->is_from_dynobj());
465 return (this->visibility_ != elfcpp::STV_INTERNAL
466 && this->visibility_ != elfcpp::STV_HIDDEN
467 && this->visibility_ != elfcpp::STV_PROTECTED
468 && !this->is_forced_local_
469 && parameters->output_is_shared()
470 && !parameters->symbolic());
473 // Return true if this symbol is a function that needs a PLT entry.
474 // If the symbol is defined in a dynamic object or if it is subject
475 // to pre-emption, we need to make a PLT entry.
477 needs_plt_entry() const
479 return (this->type() == elfcpp::STT_FUNC
480 && (this->is_from_dynobj() || this->is_preemptible()));
483 // When determining whether a reference to a symbol needs a dynamic
484 // relocation, we need to know several things about the reference.
485 // These flags may be or'ed together.
488 // Reference to the symbol's absolute address.
490 // A non-PIC reference.
496 // Given a direct absolute or pc-relative static relocation against
497 // the global symbol, this function returns whether a dynamic relocation
501 needs_dynamic_reloc(int flags) const
503 // An absolute reference within a position-independent output file
504 // will need a dynamic relocation.
505 if ((flags & ABSOLUTE_REF)
506 && parameters->output_is_position_independent())
509 // A function call that can branch to a local PLT entry does not need
510 // a dynamic relocation. A non-pic pc-relative function call in a
511 // shared library cannot use a PLT entry.
512 if ((flags & FUNCTION_CALL)
513 && this->has_plt_offset()
514 && !((flags & NON_PIC_REF) && parameters->output_is_shared()))
517 // A reference to any PLT entry in a non-position-independent executable
518 // does not need a dynamic relocation.
519 if (!parameters->output_is_position_independent()
520 && this->has_plt_offset())
523 // A reference to a symbol defined in a dynamic object or to a
524 // symbol that is preemptible will need a dynamic relocation.
525 if (this->is_from_dynobj() || this->is_preemptible())
528 // For all other cases, return FALSE.
532 // Given a direct absolute static relocation against
533 // the global symbol, where a dynamic relocation is needed, this
534 // function returns whether a relative dynamic relocation can be used.
535 // The caller must determine separately whether the static relocation
536 // is compatible with a relative relocation.
539 can_use_relative_reloc(bool is_function_call) const
541 // A function call that can branch to a local PLT entry can
542 // use a RELATIVE relocation.
543 if (is_function_call && this->has_plt_offset())
546 // A reference to a symbol defined in a dynamic object or to a
547 // symbol that is preemptible can not use a RELATIVE relocaiton.
548 if (this->is_from_dynobj() || this->is_preemptible())
551 // For all other cases, return TRUE.
555 // Return whether there should be a warning for references to this
559 { return this->has_warning_; }
561 // Mark this symbol as having a warning.
564 { this->has_warning_ = true; }
566 // Return whether this symbol is defined by a COPY reloc from a
569 is_copied_from_dynobj() const
570 { return this->is_copied_from_dynobj_; }
572 // Mark this symbol as defined by a COPY reloc.
574 set_is_copied_from_dynobj()
575 { this->is_copied_from_dynobj_ = true; }
577 // Return whether this symbol is forced to visibility STB_LOCAL
578 // by a "local:" entry in a version script.
580 is_forced_local() const
581 { return this->is_forced_local_; }
583 // Mark this symbol as forced to STB_LOCAL visibility.
585 set_is_forced_local()
586 { this->is_forced_local_ = true; }
589 // Instances of this class should always be created at a specific
592 { memset(this, 0, sizeof *this); }
594 // Initialize the general fields.
596 init_fields(const char* name, const char* version,
597 elfcpp::STT type, elfcpp::STB binding,
598 elfcpp::STV visibility, unsigned char nonvis);
600 // Initialize fields from an ELF symbol in OBJECT.
601 template<int size, bool big_endian>
603 init_base(const char *name, const char* version, Object* object,
604 const elfcpp::Sym<size, big_endian>&);
606 // Initialize fields for an Output_data.
608 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
609 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
611 // Initialize fields for an Output_segment.
613 init_base(const char* name, Output_segment* os, elfcpp::STT type,
614 elfcpp::STB binding, elfcpp::STV visibility,
615 unsigned char nonvis, Segment_offset_base offset_base);
617 // Initialize fields for a constant.
619 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
620 elfcpp::STV visibility, unsigned char nonvis);
622 // Override existing symbol.
623 template<int size, bool big_endian>
625 override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
626 const char* version);
628 // Override existing symbol with a special symbol.
630 override_base_with_special(const Symbol* from);
632 // Allocate a common symbol by giving it a location in the output
635 allocate_base_common(Output_data*);
638 Symbol(const Symbol&);
639 Symbol& operator=(const Symbol&);
641 // Symbol name (expected to point into a Stringpool).
643 // Symbol version (expected to point into a Stringpool). This may
645 const char* version_;
649 // This struct is used if SOURCE_ == FROM_OBJECT.
652 // Object in which symbol is defined, or in which it was first
655 // Section number in object_ in which symbol is defined.
659 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
662 // Output_data in which symbol is defined. Before
663 // Layout::finalize the symbol's value is an offset within the
665 Output_data* output_data;
666 // True if the offset is from the end, false if the offset is
667 // from the beginning.
668 bool offset_is_from_end;
671 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
674 // Output_segment in which the symbol is defined. Before
675 // Layout::finalize the symbol's value is an offset.
676 Output_segment* output_segment;
677 // The base to use for the offset before Layout::finalize.
678 Segment_offset_base offset_base;
682 // The index of this symbol in the output file. If the symbol is
683 // not going into the output file, this value is -1U. This field
684 // starts as always holding zero. It is set to a non-zero value by
685 // Symbol_table::finalize.
686 unsigned int symtab_index_;
688 // The index of this symbol in the dynamic symbol table. If the
689 // symbol is not going into the dynamic symbol table, this value is
690 // -1U. This field starts as always holding zero. It is set to a
691 // non-zero value during Layout::finalize.
692 unsigned int dynsym_index_;
694 // If this symbol has an entry in the GOT section (has_got_offset_
695 // is true), this is the offset from the start of the GOT section.
696 // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
697 // serves as the GOT offset for the GOT entry that holds its
698 // TP-relative offset.
699 unsigned int got_offset_;
701 // If this is a TLS symbol and has an entry in the GOT section
702 // for a module index or a pair of entries (module index,
703 // dtv-relative offset), these are the offsets from the start
704 // of the GOT section.
705 unsigned int tls_mod_got_offset_;
706 unsigned int tls_pair_got_offset_;
708 // If this symbol has an entry in the PLT section (has_plt_offset_
709 // is true), then this is the offset from the start of the PLT
711 unsigned int plt_offset_;
714 elfcpp::STT type_ : 4;
716 elfcpp::STB binding_ : 4;
717 // Symbol visibility.
718 elfcpp::STV visibility_ : 2;
719 // Rest of symbol st_other field.
720 unsigned int nonvis_ : 6;
721 // The type of symbol.
723 // True if this symbol always requires special target-specific
725 bool is_target_special_ : 1;
726 // True if this is the default version of the symbol.
728 // True if this symbol really forwards to another symbol. This is
729 // used when we discover after the fact that two different entries
730 // in the hash table really refer to the same symbol. This will
731 // never be set for a symbol found in the hash table, but may be set
732 // for a symbol found in the list of symbols attached to an Object.
733 // It forwards to the symbol found in the forwarders_ map of
735 bool is_forwarder_ : 1;
736 // True if the symbol has an alias in the weak_aliases table in
739 // True if this symbol needs to be in the dynamic symbol table.
740 bool needs_dynsym_entry_ : 1;
741 // True if we've seen this symbol in a regular object.
743 // True if we've seen this symbol in a dynamic object.
745 // True if the symbol has an entry in the GOT section.
746 // For a TLS symbol, this GOT entry will hold its tp-relative offset.
747 bool has_got_offset_ : 1;
748 // True if the symbol has an entry in the GOT section for its
750 bool has_tls_mod_got_offset_ : 1;
751 // True if the symbol has a pair of entries in the GOT section for its
752 // module index and dtv-relative offset.
753 bool has_tls_pair_got_offset_ : 1;
754 // True if the symbol has an entry in the PLT section.
755 bool has_plt_offset_ : 1;
756 // True if this is a dynamic symbol which needs a special value in
757 // the dynamic symbol table.
758 bool needs_dynsym_value_ : 1;
759 // True if there is a warning for this symbol.
760 bool has_warning_ : 1;
761 // True if we are using a COPY reloc for this symbol, so that the
762 // real definition lives in a dynamic object.
763 bool is_copied_from_dynobj_ : 1;
764 // True if this symbol was forced to local visibility by a version
766 bool is_forced_local_ : 1;
769 // The parts of a symbol which are size specific. Using a template
770 // derived class like this helps us use less space on a 32-bit system.
773 class Sized_symbol : public Symbol
776 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
777 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
782 // Initialize fields from an ELF symbol in OBJECT.
783 template<bool big_endian>
785 init(const char *name, const char* version, Object* object,
786 const elfcpp::Sym<size, big_endian>&);
788 // Initialize fields for an Output_data.
790 init(const char* name, Output_data*, Value_type value, Size_type symsize,
791 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
792 bool offset_is_from_end);
794 // Initialize fields for an Output_segment.
796 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
797 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
798 Segment_offset_base offset_base);
800 // Initialize fields for a constant.
802 init(const char* name, Value_type value, Size_type symsize,
803 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
805 // Override existing symbol.
806 template<bool big_endian>
808 override(const elfcpp::Sym<size, big_endian>&, Object* object,
809 const char* version);
811 // Override existing symbol with a special symbol.
813 override_with_special(const Sized_symbol<size>*);
815 // Return the symbol's value.
818 { return this->value_; }
820 // Return the symbol's size (we can't call this 'size' because that
821 // is a template parameter).
824 { return this->symsize_; }
826 // Set the symbol size. This is used when resolving common symbols.
828 set_symsize(Size_type symsize)
829 { this->symsize_ = symsize; }
831 // Set the symbol value. This is called when we store the final
832 // values of the symbols into the symbol table.
834 set_value(Value_type value)
835 { this->value_ = value; }
837 // Allocate a common symbol by giving it a location in the output
840 allocate_common(Output_data*, Value_type value);
843 Sized_symbol(const Sized_symbol&);
844 Sized_symbol& operator=(const Sized_symbol&);
846 // Symbol value. Before Layout::finalize this is the offset in the
847 // input section. This is set to the final value during
854 // A struct describing a symbol defined by the linker, where the value
855 // of the symbol is defined based on an output section. This is used
856 // for symbols defined by the linker, like "_init_array_start".
858 struct Define_symbol_in_section
862 // The name of the output section with which this symbol should be
863 // associated. If there is no output section with that name, the
864 // symbol will be defined as zero.
865 const char* output_section;
866 // The offset of the symbol within the output section. This is an
867 // offset from the start of the output section, unless start_at_end
868 // is true, in which case this is an offset from the end of the
871 // The size of the symbol.
875 // The symbol binding.
877 // The symbol visibility.
878 elfcpp::STV visibility;
879 // The rest of the st_other field.
880 unsigned char nonvis;
881 // If true, the value field is an offset from the end of the output
883 bool offset_is_from_end;
884 // If true, this symbol is defined only if we see a reference to it.
888 // A struct describing a symbol defined by the linker, where the value
889 // of the symbol is defined based on a segment. This is used for
890 // symbols defined by the linker, like "_end". We describe the
891 // segment with which the symbol should be associated by its
892 // characteristics. If no segment meets these characteristics, the
893 // symbol will be defined as zero. If there is more than one segment
894 // which meets these characteristics, we will use the first one.
896 struct Define_symbol_in_segment
900 // The segment type where the symbol should be defined, typically
902 elfcpp::PT segment_type;
903 // Bitmask of segment flags which must be set.
904 elfcpp::PF segment_flags_set;
905 // Bitmask of segment flags which must be clear.
906 elfcpp::PF segment_flags_clear;
907 // The offset of the symbol within the segment. The offset is
908 // calculated from the position set by offset_base.
910 // The size of the symbol.
914 // The symbol binding.
916 // The symbol visibility.
917 elfcpp::STV visibility;
918 // The rest of the st_other field.
919 unsigned char nonvis;
920 // The base from which we compute the offset.
921 Symbol::Segment_offset_base offset_base;
922 // If true, this symbol is defined only if we see a reference to it.
926 // This class manages warnings. Warnings are a GNU extension. When
927 // we see a section named .gnu.warning.SYM in an object file, and if
928 // we wind using the definition of SYM from that object file, then we
929 // will issue a warning for any relocation against SYM from a
930 // different object file. The text of the warning is the contents of
931 // the section. This is not precisely the definition used by the old
932 // GNU linker; the old GNU linker treated an occurrence of
933 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
934 // would trigger a warning on any reference. However, it was
935 // inconsistent in that a warning in a dynamic object only triggered
936 // if there was no definition in a regular object. This linker is
937 // different in that we only issue a warning if we use the symbol
938 // definition from the same object file as the warning section.
947 // Add a warning for symbol NAME in object OBJ. WARNING is the text
950 add_warning(Symbol_table* symtab, const char* name, Object* obj,
951 const std::string& warning);
953 // For each symbol for which we should give a warning, make a note
956 note_warnings(Symbol_table* symtab);
958 // Issue a warning for a reference to SYM at RELINFO's location.
959 template<int size, bool big_endian>
961 issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
962 size_t relnum, off_t reloffset) const;
965 Warnings(const Warnings&);
966 Warnings& operator=(const Warnings&);
968 // What we need to know to get the warning text.
969 struct Warning_location
971 // The object the warning is in.
977 : object(NULL), text()
981 set(Object* o, const std::string& t)
988 // A mapping from warning symbol names (canonicalized in
989 // Symbol_table's namepool_ field) to warning information.
990 typedef Unordered_map<const char*, Warning_location> Warning_table;
992 Warning_table warnings_;
995 // The main linker symbol table.
1000 // COUNT is an estimate of how many symbosl will be inserted in the
1001 // symbol table. It's ok to put 0 if you don't know; a correct
1002 // guess will just save some CPU by reducing hashtable resizes.
1003 Symbol_table(unsigned int count, const Version_script_info& version_script);
1007 // Add COUNT external symbols from the relocatable object RELOBJ to
1008 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
1009 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
1010 // point to the symbols in the symbol table.
1011 template<int size, bool big_endian>
1013 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
1014 const unsigned char* syms, size_t count,
1015 const char* sym_names, size_t sym_name_size,
1016 typename Sized_relobj<size, big_endian>::Symbols*);
1018 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
1019 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
1020 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
1021 // symbol version data.
1022 template<int size, bool big_endian>
1024 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
1025 const unsigned char* syms, size_t count,
1026 const char* sym_names, size_t sym_name_size,
1027 const unsigned char* versym, size_t versym_size,
1028 const std::vector<const char*>*);
1030 // Define a special symbol based on an Output_data. It is a
1031 // multiple definition error if this symbol is already defined.
1033 define_in_output_data(const char* name, const char* version,
1034 Output_data*, uint64_t value, uint64_t symsize,
1035 elfcpp::STT type, elfcpp::STB binding,
1036 elfcpp::STV visibility, unsigned char nonvis,
1037 bool offset_is_from_end, bool only_if_ref);
1039 // Define a special symbol based on an Output_segment. It is a
1040 // multiple definition error if this symbol is already defined.
1042 define_in_output_segment(const char* name, const char* version,
1043 Output_segment*, uint64_t value, uint64_t symsize,
1044 elfcpp::STT type, elfcpp::STB binding,
1045 elfcpp::STV visibility, unsigned char nonvis,
1046 Symbol::Segment_offset_base, bool only_if_ref);
1048 // Define a special symbol with a constant value. It is a multiple
1049 // definition error if this symbol is already defined.
1051 define_as_constant(const char* name, const char* version,
1052 uint64_t value, uint64_t symsize, elfcpp::STT type,
1053 elfcpp::STB binding, elfcpp::STV visibility,
1054 unsigned char nonvis, bool only_if_ref);
1056 // Define a set of symbols in output sections.
1058 define_symbols(const Layout*, int count, const Define_symbol_in_section*);
1060 // Define a set of symbols in output segments.
1062 define_symbols(const Layout*, int count, const Define_symbol_in_segment*);
1064 // Define SYM using a COPY reloc. POSD is the Output_data where the
1065 // symbol should be defined--typically a .dyn.bss section. VALUE is
1066 // the offset within POSD.
1069 define_with_copy_reloc(Sized_symbol<size>* sym, Output_data* posd,
1070 typename elfcpp::Elf_types<size>::Elf_Addr);
1072 // Look up a symbol.
1074 lookup(const char*, const char* version = NULL) const;
1076 // Return the real symbol associated with the forwarder symbol FROM.
1078 resolve_forwards(const Symbol* from) const;
1080 // Return the sized version of a symbol in this table.
1083 get_sized_symbol(Symbol* ACCEPT_SIZE) const;
1086 const Sized_symbol<size>*
1087 get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
1089 // Return the count of undefined symbols seen.
1091 saw_undefined() const
1092 { return this->saw_undefined_; }
1094 // Allocate the common symbols
1096 allocate_commons(const General_options&, Layout*);
1098 // Add a warning for symbol NAME in object OBJ. WARNING is the text
1101 add_warning(const char* name, Object* obj, const std::string& warning)
1102 { this->warnings_.add_warning(this, name, obj, warning); }
1104 // Canonicalize a symbol name for use in the hash table.
1106 canonicalize_name(const char* name)
1107 { return this->namepool_.add(name, true, NULL); }
1109 // Possibly issue a warning for a reference to SYM at LOCATION which
1111 template<int size, bool big_endian>
1113 issue_warning(const Symbol* sym,
1114 const Relocate_info<size, big_endian>* relinfo,
1115 size_t relnum, off_t reloffset) const
1116 { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
1118 // Check candidate_odr_violations_ to find symbols with the same name
1119 // but apparently different definitions (different source-file/line-no).
1121 detect_odr_violations(const Task*, const char* output_file_name) const;
1123 // SYM is defined using a COPY reloc. Return the dynamic object
1124 // where the original definition was found.
1126 get_copy_source(const Symbol* sym) const;
1128 // Set the dynamic symbol indexes. INDEX is the index of the first
1129 // global dynamic symbol. Pointers to the symbols are stored into
1130 // the vector. The names are stored into the Stringpool. This
1131 // returns an updated dynamic symbol index.
1133 set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*,
1134 Stringpool*, Versions*);
1136 // Finalize the symbol table after we have set the final addresses
1137 // of all the input sections. This sets the final symbol indexes,
1138 // values and adds the names to *POOL. *PLOCAL_SYMCOUNT is the
1139 // index of the first global symbol. OFF is the file offset of the
1140 // global symbol table, DYNOFF is the offset of the globals in the
1141 // dynamic symbol table, DYN_GLOBAL_INDEX is the index of the first
1142 // global dynamic symbol, and DYNCOUNT is the number of global
1143 // dynamic symbols. This records the parameters, and returns the
1144 // new file offset. It updates *PLOCAL_SYMCOUNT if it created any
1147 finalize(off_t off, off_t dynoff, size_t dyn_global_index, size_t dyncount,
1148 Stringpool* pool, unsigned int *plocal_symcount);
1150 // Write out the global symbols.
1152 write_globals(const Input_objects*, const Stringpool*, const Stringpool*,
1153 Output_file*) const;
1155 // Write out a section symbol. Return the updated offset.
1157 write_section_symbol(const Output_section*, Output_file*, off_t) const;
1159 // Dump statistical information to stderr.
1161 print_stats() const;
1163 // Return the version script information.
1164 const Version_script_info&
1165 version_script() const
1166 { return version_script_; }
1169 Symbol_table(const Symbol_table&);
1170 Symbol_table& operator=(const Symbol_table&);
1172 // Make FROM a forwarder symbol to TO.
1174 make_forwarder(Symbol* from, Symbol* to);
1177 template<int size, bool big_endian>
1179 add_from_object(Object*, const char *name, Stringpool::Key name_key,
1180 const char *version, Stringpool::Key version_key,
1181 bool def, const elfcpp::Sym<size, big_endian>& sym,
1182 const elfcpp::Sym<size, big_endian>& orig_sym);
1185 template<int size, bool big_endian>
1187 resolve(Sized_symbol<size>* to,
1188 const elfcpp::Sym<size, big_endian>& sym,
1189 const elfcpp::Sym<size, big_endian>& orig_sym,
1190 Object*, const char* version);
1192 template<int size, bool big_endian>
1194 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
1195 const char* version ACCEPT_SIZE_ENDIAN);
1197 // Record that a symbol is forced to be local by a version script.
1199 force_local(Symbol*);
1201 // Whether we should override a symbol, based on flags in
1204 should_override(const Symbol*, unsigned int, Object*, bool*);
1206 // Override a symbol.
1207 template<int size, bool big_endian>
1209 override(Sized_symbol<size>* tosym,
1210 const elfcpp::Sym<size, big_endian>& fromsym,
1211 Object* object, const char* version);
1213 // Whether we should override a symbol with a special symbol which
1214 // is automatically defined by the linker.
1216 should_override_with_special(const Symbol*);
1218 // Override a symbol with a special symbol.
1221 override_with_special(Sized_symbol<size>* tosym,
1222 const Sized_symbol<size>* fromsym);
1224 // Record all weak alias sets for a dynamic object.
1227 record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1229 // Define a special symbol.
1230 template<int size, bool big_endian>
1232 define_special_symbol(const char** pname, const char** pversion,
1233 bool only_if_ref, Sized_symbol<size>** poldsym
1234 ACCEPT_SIZE_ENDIAN);
1236 // Define a symbol in an Output_data, sized version.
1239 do_define_in_output_data(const char* name, const char* version, Output_data*,
1240 typename elfcpp::Elf_types<size>::Elf_Addr value,
1241 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1242 elfcpp::STT type, elfcpp::STB binding,
1243 elfcpp::STV visibility, unsigned char nonvis,
1244 bool offset_is_from_end, bool only_if_ref);
1246 // Define a symbol in an Output_segment, sized version.
1249 do_define_in_output_segment(
1250 const char* name, const char* version, Output_segment* os,
1251 typename elfcpp::Elf_types<size>::Elf_Addr value,
1252 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1253 elfcpp::STT type, elfcpp::STB binding,
1254 elfcpp::STV visibility, unsigned char nonvis,
1255 Symbol::Segment_offset_base offset_base, bool only_if_ref);
1257 // Define a symbol as a constant, sized version.
1260 do_define_as_constant(
1261 const char* name, const char* version,
1262 typename elfcpp::Elf_types<size>::Elf_Addr value,
1263 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1264 elfcpp::STT type, elfcpp::STB binding,
1265 elfcpp::STV visibility, unsigned char nonvis,
1268 // Allocate the common symbols, sized version.
1271 do_allocate_commons(const General_options&, Layout*);
1273 // Implement detect_odr_violations.
1274 template<int size, bool big_endian>
1276 sized_detect_odr_violations() const;
1278 // Finalize symbols specialized for size.
1281 sized_finalize(off_t, Stringpool*, unsigned int*);
1283 // Finalize a symbol. Return whether it should be added to the
1287 sized_finalize_symbol(Symbol*);
1289 // Add a symbol the final symtab by setting its index.
1292 add_to_final_symtab(Symbol*, Stringpool*, unsigned int* pindex, off_t* poff);
1294 // Write globals specialized for size and endianness.
1295 template<int size, bool big_endian>
1297 sized_write_globals(const Input_objects*, const Stringpool*,
1298 const Stringpool*, Output_file*) const;
1300 // Write out a symbol to P.
1301 template<int size, bool big_endian>
1303 sized_write_symbol(Sized_symbol<size>*,
1304 typename elfcpp::Elf_types<size>::Elf_Addr value,
1306 const Stringpool*, unsigned char* p
1307 ACCEPT_SIZE_ENDIAN) const;
1309 // Possibly warn about an undefined symbol from a dynamic object.
1311 warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const;
1313 // Write out a section symbol, specialized for size and endianness.
1314 template<int size, bool big_endian>
1316 sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1318 // The type of the symbol hash table.
1320 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1322 struct Symbol_table_hash
1325 operator()(const Symbol_table_key&) const;
1328 struct Symbol_table_eq
1331 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1334 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1335 Symbol_table_eq> Symbol_table_type;
1337 // The type of the list of common symbols.
1338 typedef std::vector<Symbol*> Commons_type;
1340 // The type of the list of symbols which have been forced local.
1341 typedef std::vector<Symbol*> Forced_locals;
1343 // A map from symbols with COPY relocs to the dynamic objects where
1344 // they are defined.
1345 typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1347 // A map from symbol name (as a pointer into the namepool) to all
1348 // the locations the symbols is (weakly) defined (and certain other
1349 // conditions are met). This map will be used later to detect
1350 // possible One Definition Rule (ODR) violations.
1351 struct Symbol_location
1353 Object* object; // Object where the symbol is defined.
1354 unsigned int shndx; // Section-in-object where the symbol is defined.
1355 off_t offset; // Offset-in-section where the symbol is defined.
1356 bool operator==(const Symbol_location& that) const
1358 return (this->object == that.object
1359 && this->shndx == that.shndx
1360 && this->offset == that.offset);
1364 struct Symbol_location_hash
1366 size_t operator()(const Symbol_location& loc) const
1367 { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1370 typedef Unordered_map<const char*,
1371 Unordered_set<Symbol_location, Symbol_location_hash> >
1374 // We increment this every time we see a new undefined symbol, for
1375 // use in archive groups.
1377 // The index of the first global symbol in the output file.
1378 unsigned int first_global_index_;
1379 // The file offset within the output symtab section where we should
1382 // The number of global symbols we want to write out.
1383 unsigned int output_count_;
1384 // The file offset of the global dynamic symbols, or 0 if none.
1385 off_t dynamic_offset_;
1386 // The index of the first global dynamic symbol.
1387 unsigned int first_dynamic_global_index_;
1388 // The number of global dynamic symbols, or 0 if none.
1389 unsigned int dynamic_count_;
1390 // The symbol hash table.
1391 Symbol_table_type table_;
1392 // A pool of symbol names. This is used for all global symbols.
1393 // Entries in the hash table point into this pool.
1394 Stringpool namepool_;
1395 // Forwarding symbols.
1396 Unordered_map<const Symbol*, Symbol*> forwarders_;
1397 // Weak aliases. A symbol in this list points to the next alias.
1398 // The aliases point to each other in a circular list.
1399 Unordered_map<Symbol*, Symbol*> weak_aliases_;
1400 // We don't expect there to be very many common symbols, so we keep
1401 // a list of them. When we find a common symbol we add it to this
1402 // list. It is possible that by the time we process the list the
1403 // symbol is no longer a common symbol. It may also have become a
1405 Commons_type commons_;
1406 // A list of symbols which have been forced to be local. We don't
1407 // expect there to be very many of them, so we keep a list of them
1408 // rather than walking the whole table to find them.
1409 Forced_locals forced_locals_;
1410 // Manage symbol warnings.
1412 // Manage potential One Definition Rule (ODR) violations.
1413 Odr_map candidate_odr_violations_;
1415 // When we emit a COPY reloc for a symbol, we define it in an
1416 // Output_data. When it's time to emit version information for it,
1417 // we need to know the dynamic object in which we found the original
1418 // definition. This maps symbols with COPY relocs to the dynamic
1419 // object where they were defined.
1420 Copied_symbol_dynobjs copied_symbol_dynobjs_;
1421 // Information parsed from the version script, if any.
1422 const Version_script_info& version_script_;
1425 // We inline get_sized_symbol for efficiency.
1429 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1431 gold_assert(size == parameters->get_size());
1432 return static_cast<Sized_symbol<size>*>(sym);
1436 const Sized_symbol<size>*
1437 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1439 gold_assert(size == parameters->get_size());
1440 return static_cast<const Sized_symbol<size>*>(sym);
1443 } // End namespace gold.
1445 #endif // !defined(GOLD_SYMTAB_H)