1 // symtab.h -- the gold symbol table -*- C++ -*-
12 #include "stringpool.h"
23 template<int size, bool big_endian>
26 template<int size, bool big_endian>
33 // The base class of an entry in the symbol table. The symbol table
34 // can have a lot of entries, so we don't want this class to big.
35 // Size dependent fields can be found in the template class
36 // Sized_symbol. Targets may support their own derived classes.
41 // Because we want the class to be small, we don't use any virtual
42 // functions. But because symbols can be defined in different
43 // places, we need to classify them. This enum is the different
44 // sources of symbols we support.
47 // Symbol defined in a relocatable or dynamic input file--this is
48 // the most common case.
50 // Symbol defined in an Output_data, a special section created by
53 // Symbol defined in an Output_segment, with no associated
56 // Symbol value is constant.
60 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
62 enum Segment_offset_base
64 // From the start of the segment.
66 // From the end of the segment.
68 // From the filesz of the segment--i.e., after the loaded bytes
69 // but before the bytes which are allocated but zeroed.
73 // Return the symbol name.
76 { return this->name_; }
78 // Return the symbol version. This will return NULL for an
79 // unversioned symbol.
82 { return this->version_; }
84 // Return the symbol source.
87 { return this->source_; }
89 // Return the object with which this symbol is associated.
93 assert(this->source_ == FROM_OBJECT);
94 return this->u_.from_object.object;
97 // Return the index of the section in the input relocatable or
98 // dynamic object file.
102 assert(this->source_ == FROM_OBJECT);
103 return this->u_.from_object.shnum;
106 // Return the output data section with which this symbol is
107 // associated, if the symbol was specially defined with respect to
108 // an output data section.
112 assert(this->source_ == IN_OUTPUT_DATA);
113 return this->u_.in_output_data.output_data;
116 // If this symbol was defined with respect to an output data
117 // section, return whether the value is an offset from end.
119 offset_is_from_end() const
121 assert(this->source_ == IN_OUTPUT_DATA);
122 return this->u_.in_output_data.offset_is_from_end;
125 // Return the output segment with which this symbol is associated,
126 // if the symbol was specially defined with respect to an output
129 output_segment() const
131 assert(this->source_ == IN_OUTPUT_SEGMENT);
132 return this->u_.in_output_segment.output_segment;
135 // If this symbol was defined with respect to an output segment,
136 // return the offset base.
140 assert(this->source_ == IN_OUTPUT_SEGMENT);
141 return this->u_.in_output_segment.offset_base;
144 // Return the symbol binding.
147 { return this->binding_; }
149 // Return the symbol type.
152 { return this->type_; }
154 // Return the symbol visibility.
157 { return this->visibility_; }
159 // Return the non-visibility part of the st_other field.
162 { return this->nonvis_; }
164 // Return whether this symbol is a forwarder. This will never be
165 // true of a symbol found in the hash table, but may be true of
166 // symbol pointers attached to object files.
169 { return this->is_forwarder_; }
171 // Mark this symbol as a forwarder.
174 { this->is_forwarder_ = true; }
176 // Return whether this symbol needs an entry in the dynamic symbol
179 needs_dynsym_entry() const
180 { return this->needs_dynsym_entry_; }
182 // Mark this symbol as needing an entry in the dynamic symbol table.
184 set_needs_dynsym_entry()
185 { this->needs_dynsym_entry_ = true; }
187 // Return whether this symbol was ever seen in a dynamic object.
190 { return this->in_dyn_; }
192 // Mark this symbol as having been seen in a dynamic object.
195 { this->in_dyn_ = true; }
197 // Return the index of this symbol in the output file symbol table.
198 // A value of -1U means that this symbol is not going into the
199 // output file. This starts out as zero, and is set to a non-zero
200 // value by Symbol_table::finalize. It is an error to ask for the
201 // symbol table index before it has been set.
205 assert(this->symtab_index_ != 0);
206 return this->symtab_index_;
209 // Set the index of the symbol in the output file symbol table.
211 set_symtab_index(unsigned int index)
214 this->symtab_index_ = index;
217 // Return the index of this symbol in the dynamic symbol table. A
218 // value of -1U means that this symbol is not going into the dynamic
219 // symbol table. This starts out as zero, and is set to a non-zero
220 // during Layout::finalize. It is an error to ask for the dynamic
221 // symbol table index before it has been set.
225 assert(this->dynsym_index_ != 0);
226 return this->dynsym_index_;
229 // Set the index of the symbol in the dynamic symbol table.
231 set_dynsym_index(unsigned int index)
234 this->dynsym_index_ = index;
237 // Return whether this symbol has an entry in the GOT section.
239 has_got_offset() const
240 { return this->has_got_offset_; }
242 // Return the offset into the GOT section of this symbol.
246 assert(this->has_got_offset());
247 return this->got_offset_;
250 // Set the GOT offset of this symbol.
252 set_got_offset(unsigned int got_offset)
254 this->has_got_offset_ = true;
255 this->got_offset_ = got_offset;
258 // Return whether this symbol is resolved locally. This is always
259 // true when linking statically. It is true for a symbol defined in
260 // this object when using -Bsymbolic. It is true for a symbol
261 // marked local in a version file. FIXME: This needs to be
264 is_resolved_locally() const
265 { return !this->in_dyn_; }
267 // Return whether this is a defined symbol (not undefined or
272 return (this->source_ != FROM_OBJECT
273 || (this->u_.from_object.shnum != elfcpp::SHN_UNDEF
274 && this->u_.from_object.shnum != elfcpp::SHN_COMMON));
277 // Return whether this is an undefined symbol.
281 return this->source_ == FROM_OBJECT && this->shnum() == elfcpp::SHN_UNDEF;
284 // Return whether this is a common symbol.
288 return (this->source_ == FROM_OBJECT
289 && (this->u_.from_object.shnum == elfcpp::SHN_COMMON
290 || this->type_ == elfcpp::STT_COMMON));
293 // Return whether there should be a warning for references to this
297 { return this->has_warning_; }
299 // Mark this symbol as having a warning.
302 { this->has_warning_ = true; }
305 // Instances of this class should always be created at a specific
308 { memset(this, 0, sizeof *this); }
310 // Initialize the general fields.
312 init_fields(const char* name, const char* version,
313 elfcpp::STT type, elfcpp::STB binding,
314 elfcpp::STV visibility, unsigned char nonvis);
316 // Initialize fields from an ELF symbol in OBJECT.
317 template<int size, bool big_endian>
319 init_base(const char *name, const char* version, Object* object,
320 const elfcpp::Sym<size, big_endian>&);
322 // Initialize fields for an Output_data.
324 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
325 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
327 // Initialize fields for an Output_segment.
329 init_base(const char* name, Output_segment* os, elfcpp::STT type,
330 elfcpp::STB binding, elfcpp::STV visibility,
331 unsigned char nonvis, Segment_offset_base offset_base);
333 // Initialize fields for a constant.
335 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
336 elfcpp::STV visibility, unsigned char nonvis);
338 // Override existing symbol.
339 template<int size, bool big_endian>
341 override_base(const elfcpp::Sym<size, big_endian>&, Object* object);
344 Symbol(const Symbol&);
345 Symbol& operator=(const Symbol&);
347 // Symbol name (expected to point into a Stringpool).
349 // Symbol version (expected to point into a Stringpool). This may
351 const char* version_;
355 // This struct is used if SOURCE_ == FROM_OBJECT.
358 // Object in which symbol is defined, or in which it was first
361 // Section number in object_ in which symbol is defined.
365 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
368 // Output_data in which symbol is defined. Before
369 // Layout::finalize the symbol's value is an offset within the
371 Output_data* output_data;
372 // True if the offset is from the end, false if the offset is
373 // from the beginning.
374 bool offset_is_from_end;
377 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
380 // Output_segment in which the symbol is defined. Before
381 // Layout::finalize the symbol's value is an offset.
382 Output_segment* output_segment;
383 // The base to use for the offset before Layout::finalize.
384 Segment_offset_base offset_base;
388 // The index of this symbol in the output file. If the symbol is
389 // not going into the output file, this value is -1U. This field
390 // starts as always holding zero. It is set to a non-zero value by
391 // Symbol_table::finalize.
392 unsigned int symtab_index_;
394 // The index of this symbol in the dynamic symbol table. If the
395 // symbol is not going into the dynamic symbol table, this value is
396 // -1U. This field starts as always holding zero. It is set to a
397 // non-zero value during Layout::finalize.
398 unsigned int dynsym_index_;
400 // If this symbol has an entry in the GOT section (has_got_offset_
401 // is true), this is the offset from the start of the GOT section.
402 unsigned int got_offset_;
405 elfcpp::STT type_ : 4;
407 elfcpp::STB binding_ : 4;
408 // Symbol visibility.
409 elfcpp::STV visibility_ : 2;
410 // Rest of symbol st_other field.
411 unsigned int nonvis_ : 6;
412 // The type of symbol.
414 // True if this symbol always requires special target-specific
416 bool is_target_special_ : 1;
417 // True if this is the default version of the symbol.
419 // True if this symbol really forwards to another symbol. This is
420 // used when we discover after the fact that two different entries
421 // in the hash table really refer to the same symbol. This will
422 // never be set for a symbol found in the hash table, but may be set
423 // for a symbol found in the list of symbols attached to an Object.
424 // It forwards to the symbol found in the forwarders_ map of
426 bool is_forwarder_ : 1;
427 // True if this symbol needs to be in the dynamic symbol table.
428 bool needs_dynsym_entry_ : 1;
429 // True if we've seen this symbol in a dynamic object.
431 // True if the symbol has an entry in the GOT section.
432 bool has_got_offset_ : 1;
433 // True if there is a warning for this symbol.
434 bool has_warning_ : 1;
437 // The parts of a symbol which are size specific. Using a template
438 // derived class like this helps us use less space on a 32-bit system.
441 class Sized_symbol : public Symbol
444 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
445 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
450 // Initialize fields from an ELF symbol in OBJECT.
451 template<bool big_endian>
453 init(const char *name, const char* version, Object* object,
454 const elfcpp::Sym<size, big_endian>&);
456 // Initialize fields for an Output_data.
458 init(const char* name, Output_data*, Value_type value, Size_type symsize,
459 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
460 bool offset_is_from_end);
462 // Initialize fields for an Output_segment.
464 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
465 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
466 Segment_offset_base offset_base);
468 // Initialize fields for a constant.
470 init(const char* name, Value_type value, Size_type symsize,
471 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
473 // Override existing symbol.
474 template<bool big_endian>
476 override(const elfcpp::Sym<size, big_endian>&, Object* object);
478 // Return the symbol's value.
481 { return this->value_; }
483 // Return the symbol's size (we can't call this 'size' because that
484 // is a template parameter).
487 { return this->symsize_; }
489 // Set the symbol size. This is used when resolving common symbols.
491 set_symsize(Size_type symsize)
492 { this->symsize_ = symsize; }
494 // Set the symbol value. This is called when we store the final
495 // values of the symbols into the symbol table.
497 set_value(Value_type value)
498 { this->value_ = value; }
501 Sized_symbol(const Sized_symbol&);
502 Sized_symbol& operator=(const Sized_symbol&);
504 // Symbol value. Before Layout::finalize this is the offset in the
505 // input section. This is set to the final value during
512 // A struct describing a symbol defined by the linker, where the value
513 // of the symbol is defined based on an output section. This is used
514 // for symbols defined by the linker, like "_init_array_start".
516 struct Define_symbol_in_section
520 // The name of the output section with which this symbol should be
521 // associated. If there is no output section with that name, the
522 // symbol will be defined as zero.
523 const char* output_section;
524 // The offset of the symbol within the output section. This is an
525 // offset from the start of the output section, unless start_at_end
526 // is true, in which case this is an offset from the end of the
529 // The size of the symbol.
533 // The symbol binding.
535 // The symbol visibility.
536 elfcpp::STV visibility;
537 // The rest of the st_other field.
538 unsigned char nonvis;
539 // If true, the value field is an offset from the end of the output
541 bool offset_is_from_end;
542 // If true, this symbol is defined only if we see a reference to it.
546 // A struct describing a symbol defined by the linker, where the value
547 // of the symbol is defined based on a segment. This is used for
548 // symbols defined by the linker, like "_end". We describe the
549 // segment with which the symbol should be associated by its
550 // characteristics. If no segment meets these characteristics, the
551 // symbol will be defined as zero. If there is more than one segment
552 // which meets these characteristics, we will use the first one.
554 struct Define_symbol_in_segment
558 // The segment type where the symbol should be defined, typically
560 elfcpp::PT segment_type;
561 // Bitmask of segment flags which must be set.
562 elfcpp::PF segment_flags_set;
563 // Bitmask of segment flags which must be clear.
564 elfcpp::PF segment_flags_clear;
565 // The offset of the symbol within the segment. The offset is
566 // calculated from the position set by offset_base.
568 // The size of the symbol.
572 // The symbol binding.
574 // The symbol visibility.
575 elfcpp::STV visibility;
576 // The rest of the st_other field.
577 unsigned char nonvis;
578 // The base from which we compute the offset.
579 Symbol::Segment_offset_base offset_base;
580 // If true, this symbol is defined only if we see a reference to it.
584 // This class manages warnings. Warnings are a GNU extension. When
585 // we see a section named .gnu.warning.SYM in an object file, and if
586 // we wind using the definition of SYM from that object file, then we
587 // will issue a warning for any relocation against SYM from a
588 // different object file. The text of the warning is the contents of
589 // the section. This is not precisely the definition used by the old
590 // GNU linker; the old GNU linker treated an occurrence of
591 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
592 // would trigger a warning on any reference. However, it was
593 // inconsistent in that a warning in a dynamic object only triggered
594 // if there was no definition in a regular object. This linker is
595 // different in that we only issue a warning if we use the symbol
596 // definition from the same object file as the warning section.
605 // Add a warning for symbol NAME in section SHNDX in object OBJ.
607 add_warning(Symbol_table* symtab, const char* name, Object* obj,
610 // For each symbol for which we should give a warning, make a note
613 note_warnings(Symbol_table* symtab);
615 // Issue a warning for a reference to SYM at LOCATION.
617 issue_warning(const Symbol* sym, const std::string& location) const;
620 Warnings(const Warnings&);
621 Warnings& operator=(const Warnings&);
623 // What we need to know to get the warning text.
624 struct Warning_location
626 // The object the warning is in.
628 // The index of the warning section.
630 // The warning text if we have already loaded it.
634 : object(NULL), shndx(0), text()
638 set(Object* o, unsigned int s)
645 set_text(const char* t, off_t l)
646 { this->text.assign(t, l); }
649 // A mapping from warning symbol names (canonicalized in
650 // Symbol_table's namepool_ field) to
651 typedef Unordered_map<const char*, Warning_location> Warning_table;
653 Warning_table warnings_;
656 // The main linker symbol table.
665 // Add COUNT external symbols from the relocatable object RELOBJ to
666 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
667 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
668 // point to the symbols in the symbol table.
669 template<int size, bool big_endian>
671 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
672 const unsigned char* syms, size_t count,
673 const char* sym_names, size_t sym_name_size,
674 Symbol** sympointers);
676 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
677 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
678 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
679 // symbol version data.
680 template<int size, bool big_endian>
682 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
683 const unsigned char* syms, size_t count,
684 const char* sym_names, size_t sym_name_size,
685 const unsigned char* versym, size_t versym_size,
686 const std::vector<const char*>*);
688 // Define a special symbol.
689 template<int size, bool big_endian>
691 define_special_symbol(Target* target, const char* name, bool only_if_ref
694 // Define a special symbol based on an Output_data. It is a
695 // multiple definition error if this symbol is already defined.
697 define_in_output_data(Target*, const char* name, Output_data*,
698 uint64_t value, uint64_t symsize,
699 elfcpp::STT type, elfcpp::STB binding,
700 elfcpp::STV visibility, unsigned char nonvis,
701 bool offset_is_from_end, bool only_if_ref);
703 // Define a special symbol based on an Output_segment. It is a
704 // multiple definition error if this symbol is already defined.
706 define_in_output_segment(Target*, const char* name, Output_segment*,
707 uint64_t value, uint64_t symsize,
708 elfcpp::STT type, elfcpp::STB binding,
709 elfcpp::STV visibility, unsigned char nonvis,
710 Symbol::Segment_offset_base, bool only_if_ref);
712 // Define a special symbol with a constant value. It is a multiple
713 // definition error if this symbol is already defined.
715 define_as_constant(Target*, const char* name, uint64_t value,
716 uint64_t symsize, elfcpp::STT type, elfcpp::STB binding,
717 elfcpp::STV visibility, unsigned char nonvis,
720 // Define a set of symbols in output sections.
722 define_symbols(const Layout*, Target*, int count,
723 const Define_symbol_in_section*);
725 // Define a set of symbols in output segments.
727 define_symbols(const Layout*, Target*, int count,
728 const Define_symbol_in_segment*);
732 lookup(const char*, const char* version = NULL) const;
734 // Return the real symbol associated with the forwarder symbol FROM.
736 resolve_forwards(const Symbol* from) const;
738 // Return the size of the symbols in the table.
741 { return this->size_; }
743 // Return the sized version of a symbol in this table.
746 get_sized_symbol(Symbol* ACCEPT_SIZE) const;
749 const Sized_symbol<size>*
750 get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
752 // Return the count of undefined symbols seen.
754 saw_undefined() const
755 { return this->saw_undefined_; }
757 // Allocate the common symbols
759 allocate_commons(const General_options&, Layout*);
761 // Add a warning for symbol NAME in section SHNDX in object OBJ.
763 add_warning(const char* name, Object* obj, unsigned int shndx)
764 { this->warnings_.add_warning(this, name, obj, shndx); }
766 // Canonicalize a symbol name for use in the hash table.
768 canonicalize_name(const char* name)
769 { return this->namepool_.add(name, NULL); }
771 // Possibly issue a warning for a reference to SYM at LOCATION which
774 issue_warning(const Symbol* sym, const std::string& location) const
775 { this->warnings_.issue_warning(sym, location); }
777 // Finalize the symbol table after we have set the final addresses
778 // of all the input sections. This sets the final symbol indexes,
779 // values and adds the names to *POOL. INDEX is the index of the
780 // first global symbol. This records the file offset OFF, and
781 // returns the new file offset.
783 finalize(unsigned int index, off_t off, Stringpool* pool);
785 // Write out the global symbols.
787 write_globals(const Target*, const Stringpool*, Output_file*) const;
790 Symbol_table(const Symbol_table&);
791 Symbol_table& operator=(const Symbol_table&);
793 // Set the size of the symbols in the table.
796 { this->size_ = size; }
798 // Make FROM a forwarder symbol to TO.
800 make_forwarder(Symbol* from, Symbol* to);
803 template<int size, bool big_endian>
805 add_from_object(Object*, const char *name, Stringpool::Key name_key,
806 const char *version, Stringpool::Key version_key,
807 bool def, const elfcpp::Sym<size, big_endian>& sym);
810 template<int size, bool big_endian>
812 resolve(Sized_symbol<size>* to,
813 const elfcpp::Sym<size, big_endian>& sym,
816 template<int size, bool big_endian>
818 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
821 // Define a symbol in an Output_data, sized version.
824 do_define_in_output_data(Target*, const char* name, Output_data*,
825 typename elfcpp::Elf_types<size>::Elf_Addr value,
826 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
827 elfcpp::STT type, elfcpp::STB binding,
828 elfcpp::STV visibility, unsigned char nonvis,
829 bool offset_is_from_end, bool only_if_ref);
831 // Define a symbol in an Output_segment, sized version.
834 do_define_in_output_segment(
835 Target*, const char* name, Output_segment* os,
836 typename elfcpp::Elf_types<size>::Elf_Addr value,
837 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
838 elfcpp::STT type, elfcpp::STB binding,
839 elfcpp::STV visibility, unsigned char nonvis,
840 Symbol::Segment_offset_base offset_base, bool only_if_ref);
842 // Define a symbol as a constant, sized version.
845 do_define_as_constant(
846 Target*, const char* name,
847 typename elfcpp::Elf_types<size>::Elf_Addr value,
848 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
849 elfcpp::STT type, elfcpp::STB binding,
850 elfcpp::STV visibility, unsigned char nonvis,
853 // Allocate the common symbols, sized version.
856 do_allocate_commons(const General_options&, Layout*);
858 // Finalize symbols specialized for size.
861 sized_finalize(unsigned int, off_t, Stringpool*);
863 // Write globals specialized for size and endianness.
864 template<int size, bool big_endian>
866 sized_write_globals(const Target*, const Stringpool*, Output_file*) const;
868 // The type of the symbol hash table.
870 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
872 struct Symbol_table_hash
875 operator()(const Symbol_table_key&) const;
878 struct Symbol_table_eq
881 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
884 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
885 Symbol_table_eq> Symbol_table_type;
887 // The type of the list of common symbols.
889 typedef std::vector<Symbol*> Commons_type;
891 // The size of the symbols in the symbol table (32 or 64).
894 // We increment this every time we see a new undefined symbol, for
895 // use in archive groups.
898 // The index of the first global symbol in the output file.
899 unsigned int first_global_index_;
901 // The file offset within the output symtab section where we should
905 // The number of global symbols we want to write out.
906 size_t output_count_;
908 // The symbol hash table.
909 Symbol_table_type table_;
911 // A pool of symbol names. This is used for all global symbols.
912 // Entries in the hash table point into this pool.
913 Stringpool namepool_;
915 // Forwarding symbols.
916 Unordered_map<const Symbol*, Symbol*> forwarders_;
918 // We don't expect there to be very many common symbols, so we keep
919 // a list of them. When we find a common symbol we add it to this
920 // list. It is possible that by the time we process the list the
921 // symbol is no longer a common symbol. It may also have become a
923 Commons_type commons_;
925 // Manage symbol warnings.
929 // We inline get_sized_symbol for efficiency.
933 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
935 assert(size == this->get_size());
936 return static_cast<Sized_symbol<size>*>(sym);
940 const Sized_symbol<size>*
941 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
943 assert(size == this->get_size());
944 return static_cast<const Sized_symbol<size>*>(sym);
947 } // End namespace gold.
949 #endif // !defined(GOLD_SYMTAB_H)