1 // symtab.h -- the gold symbol table -*- C++ -*-
12 #include "stringpool.h"
29 // The base class of an entry in the symbol table. The symbol table
30 // can have a lot of entries, so we don't want this class to big.
31 // Size dependent fields can be found in the template class
32 // Sized_symbol. Targets may support their own derived classes.
37 // Because we want the class to be small, we don't use any virtual
38 // functions. But because symbols can be defined in different
39 // places, we need to classify them. This enum is the different
40 // sources of symbols we support.
43 // Symbol defined in a relocatable or dynamic input file--this is
44 // the most common case.
46 // Symbol defined in an Output_data, a special section created by
49 // Symbol defined in an Output_segment, with no associated
52 // Symbol value is constant.
56 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
58 enum Segment_offset_base
60 // From the start of the segment.
62 // From the end of the segment.
64 // From the filesz of the segment--i.e., after the loaded bytes
65 // but before the bytes which are allocated but zeroed.
69 // Return the symbol name.
72 { return this->name_
; }
74 // Return the symbol version. This will return NULL for an
75 // unversioned symbol.
78 { return this->version_
; }
80 // Return the symbol source.
83 { return this->source_
; }
85 // Return the object with which this symbol is associated.
89 assert(this->source_
== FROM_OBJECT
);
90 return this->u_
.from_object
.object
;
93 // Return the index of the section in the input relocatable or
94 // dynamic object file.
98 assert(this->source_
== FROM_OBJECT
);
99 return this->u_
.from_object
.shnum
;
102 // Return the output data section with which this symbol is
103 // associated, if the symbol was specially defined with respect to
104 // an output data section.
108 assert(this->source_
== IN_OUTPUT_DATA
);
109 return this->u_
.in_output_data
.output_data
;
112 // If this symbol was defined with respect to an output data
113 // section, return whether the value is an offset from end.
115 offset_is_from_end() const
117 assert(this->source_
== IN_OUTPUT_DATA
);
118 return this->u_
.in_output_data
.offset_is_from_end
;
121 // Return the output segment with which this symbol is associated,
122 // if the symbol was specially defined with respect to an output
125 output_segment() const
127 assert(this->source_
== IN_OUTPUT_SEGMENT
);
128 return this->u_
.in_output_segment
.output_segment
;
131 // If this symbol was defined with respect to an output segment,
132 // return the offset base.
136 assert(this->source_
== IN_OUTPUT_SEGMENT
);
137 return this->u_
.in_output_segment
.offset_base
;
140 // Return the symbol binding.
143 { return this->binding_
; }
145 // Return the symbol type.
148 { return this->type_
; }
150 // Return the symbol visibility.
153 { return this->visibility_
; }
155 // Return the non-visibility part of the st_other field.
158 { return this->nonvis_
; }
160 // Return whether this symbol is a forwarder. This will never be
161 // true of a symbol found in the hash table, but may be true of
162 // symbol pointers attached to object files.
165 { return this->is_forwarder_
; }
167 // Mark this symbol as a forwarder.
170 { this->is_forwarder_
= true; }
172 // Return whether this symbol was ever seen in a dynamic object.
175 { return this->in_dyn_
; }
177 // Mark this symbol as having been seen in a dynamic object.
180 { this->in_dyn_
= true; }
182 // Return whether this symbol has an entry in the GOT section.
184 has_got_offset() const
185 { return this->has_got_offset_
; }
187 // Return the offset into the GOT section of this symbol.
191 assert(this->has_got_offset());
192 return this->got_offset_
;
195 // Set the GOT offset of this symbol.
197 set_got_offset(unsigned int got_offset
)
199 this->has_got_offset_
= true;
200 this->got_offset_
= got_offset
;
203 // Return whether this symbol is resolved locally. This is always
204 // true when linking statically. It is true for a symbol defined in
205 // this object when using -Bsymbolic. It is true for a symbol
206 // marked local in a version file. FIXME: This needs to be
209 is_resolved_locally() const
210 { return !this->in_dyn_
; }
212 // Return whether this is a defined symbol (not undefined or
217 return (this->source_
!= FROM_OBJECT
218 || (this->u_
.from_object
.shnum
!= elfcpp::SHN_UNDEF
219 && this->u_
.from_object
.shnum
!= elfcpp::SHN_COMMON
));
222 // Return whether this is an undefined symbol.
226 return this->source_
== FROM_OBJECT
&& this->shnum() == elfcpp::SHN_UNDEF
;
229 // Return whether this is a common symbol.
233 return (this->source_
== FROM_OBJECT
234 && (this->u_
.from_object
.shnum
== elfcpp::SHN_COMMON
235 || this->type_
== elfcpp::STT_COMMON
));
238 // Return whether there should be a warning for references to this
242 { return this->has_warning_
; }
244 // Mark this symbol as having a warning.
247 { this->has_warning_
= true; }
250 // Instances of this class should always be created at a specific
253 { memset(this, 0, sizeof *this); }
255 // Initialize the general fields.
257 init_fields(const char* name
, const char* version
,
258 elfcpp::STT type
, elfcpp::STB binding
,
259 elfcpp::STV visibility
, unsigned char nonvis
);
261 // Initialize fields from an ELF symbol in OBJECT.
262 template<int size
, bool big_endian
>
264 init_base(const char *name
, const char* version
, Object
* object
,
265 const elfcpp::Sym
<size
, big_endian
>&);
267 // Initialize fields for an Output_data.
269 init_base(const char* name
, Output_data
*, elfcpp::STT
, elfcpp::STB
,
270 elfcpp::STV
, unsigned char nonvis
, bool offset_is_from_end
);
272 // Initialize fields for an Output_segment.
274 init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
275 elfcpp::STB binding
, elfcpp::STV visibility
,
276 unsigned char nonvis
, Segment_offset_base offset_base
);
278 // Initialize fields for a constant.
280 init_base(const char* name
, elfcpp::STT type
, elfcpp::STB binding
,
281 elfcpp::STV visibility
, unsigned char nonvis
);
283 // Override existing symbol.
284 template<int size
, bool big_endian
>
286 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
289 Symbol(const Symbol
&);
290 Symbol
& operator=(const Symbol
&);
292 // Symbol name (expected to point into a Stringpool).
294 // Symbol version (expected to point into a Stringpool). This may
296 const char* version_
;
300 // This struct is used if SOURCE_ == FROM_OBJECT.
303 // Object in which symbol is defined, or in which it was first
306 // Section number in object_ in which symbol is defined.
310 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
313 // Output_data in which symbol is defined. Before
314 // Layout::finalize the symbol's value is an offset within the
316 Output_data
* output_data
;
317 // True if the offset is from the end, false if the offset is
318 // from the beginning.
319 bool offset_is_from_end
;
322 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
325 // Output_segment in which the symbol is defined. Before
326 // Layout::finalize the symbol's value is an offset.
327 Output_segment
* output_segment
;
328 // The base to use for the offset before Layout::finalize.
329 Segment_offset_base offset_base
;
333 // If this symbol has an entry in the GOT section (has_got_offset_
334 // is true), this is the offset.
335 unsigned int got_offset_
;
337 elfcpp::STT type_
: 4;
339 elfcpp::STB binding_
: 4;
340 // Symbol visibility.
341 elfcpp::STV visibility_
: 2;
342 // Rest of symbol st_other field.
343 unsigned int nonvis_
: 6;
344 // The type of symbol.
346 // True if this symbol always requires special target-specific
348 bool is_target_special_
: 1;
349 // True if this is the default version of the symbol.
351 // True if this symbol really forwards to another symbol. This is
352 // used when we discover after the fact that two different entries
353 // in the hash table really refer to the same symbol. This will
354 // never be set for a symbol found in the hash table, but may be set
355 // for a symbol found in the list of symbols attached to an Object.
356 // It forwards to the symbol found in the forwarders_ map of
358 bool is_forwarder_
: 1;
359 // True if we've seen this symbol in a dynamic object.
361 // True if the symbol has an entry in the GOT section.
362 bool has_got_offset_
: 1;
363 // True if there is a warning for this symbol.
364 bool has_warning_
: 1;
367 // The parts of a symbol which are size specific. Using a template
368 // derived class like this helps us use less space on a 32-bit system.
371 class Sized_symbol
: public Symbol
374 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
375 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
380 // Initialize fields from an ELF symbol in OBJECT.
381 template<bool big_endian
>
383 init(const char *name
, const char* version
, Object
* object
,
384 const elfcpp::Sym
<size
, big_endian
>&);
386 // Initialize fields for an Output_data.
388 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
389 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
390 bool offset_is_from_end
);
392 // Initialize fields for an Output_segment.
394 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
395 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
396 Segment_offset_base offset_base
);
398 // Initialize fields for a constant.
400 init(const char* name
, Value_type value
, Size_type symsize
,
401 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
403 // Override existing symbol.
404 template<bool big_endian
>
406 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
408 // Return the symbol's value.
411 { return this->value_
; }
413 // Return the symbol's size (we can't call this 'size' because that
414 // is a template parameter).
417 { return this->symsize_
; }
419 // Set the symbol size. This is used when resolving common symbols.
421 set_symsize(Size_type symsize
)
422 { this->symsize_
= symsize
; }
424 // Set the symbol value. This is called when we store the final
425 // values of the symbols into the symbol table.
427 set_value(Value_type value
)
428 { this->value_
= value
; }
431 Sized_symbol(const Sized_symbol
&);
432 Sized_symbol
& operator=(const Sized_symbol
&);
434 // Symbol value. Before Layout::finalize this is the offset in the
435 // input section. This is set to the final value during
442 // A struct describing a symbol defined by the linker, where the value
443 // of the symbol is defined based on an output section. This is used
444 // for symbols defined by the linker, like "_init_array_start".
446 struct Define_symbol_in_section
450 // The name of the output section with which this symbol should be
451 // associated. If there is no output section with that name, the
452 // symbol will be defined as zero.
453 const char* output_section
;
454 // The offset of the symbol within the output section. This is an
455 // offset from the start of the output section, unless start_at_end
456 // is true, in which case this is an offset from the end of the
459 // The size of the symbol.
463 // The symbol binding.
465 // The symbol visibility.
466 elfcpp::STV visibility
;
467 // The rest of the st_other field.
468 unsigned char nonvis
;
469 // If true, the value field is an offset from the end of the output
471 bool offset_is_from_end
;
472 // If true, this symbol is defined only if we see a reference to it.
476 // A struct describing a symbol defined by the linker, where the value
477 // of the symbol is defined based on a segment. This is used for
478 // symbols defined by the linker, like "_end". We describe the
479 // segment with which the symbol should be associated by its
480 // characteristics. If no segment meets these characteristics, the
481 // symbol will be defined as zero. If there is more than one segment
482 // which meets these characteristics, we will use the first one.
484 struct Define_symbol_in_segment
488 // The segment type where the symbol should be defined, typically
490 elfcpp::PT segment_type
;
491 // Bitmask of segment flags which must be set.
492 elfcpp::PF segment_flags_set
;
493 // Bitmask of segment flags which must be clear.
494 elfcpp::PF segment_flags_clear
;
495 // The offset of the symbol within the segment. The offset is
496 // calculated from the position set by offset_base.
498 // The size of the symbol.
502 // The symbol binding.
504 // The symbol visibility.
505 elfcpp::STV visibility
;
506 // The rest of the st_other field.
507 unsigned char nonvis
;
508 // The base from which we compute the offset.
509 Symbol::Segment_offset_base offset_base
;
510 // If true, this symbol is defined only if we see a reference to it.
514 // This class manages warnings. Warnings are a GNU extension. When
515 // we see a section named .gnu.warning.SYM in an object file, and if
516 // we wind using the definition of SYM from that object file, then we
517 // will issue a warning for any relocation against SYM from a
518 // different object file. The text of the warning is the contents of
519 // the section. This is not precisely the definition used by the old
520 // GNU linker; the old GNU linker treated an occurrence of
521 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
522 // would trigger a warning on any reference. However, it was
523 // inconsistent in that a warning in a dynamic object only triggered
524 // if there was no definition in a regular object. This linker is
525 // different in that we only issue a warning if we use the symbol
526 // definition from the same object file as the warning section.
535 // Add a warning for symbol NAME in section SHNDX in object OBJ.
537 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
540 // For each symbol for which we should give a warning, make a note
543 note_warnings(Symbol_table
* symtab
);
545 // Issue a warning for a reference to SYM at LOCATION.
547 issue_warning(Symbol
* sym
, const std::string
& location
) const;
550 Warnings(const Warnings
&);
551 Warnings
& operator=(const Warnings
&);
553 // What we need to know to get the warning text.
554 struct Warning_location
556 // The object the warning is in.
558 // The index of the warning section.
560 // The warning text if we have already loaded it.
564 : object(NULL
), shndx(0), text()
568 set(Object
* o
, unsigned int s
)
575 set_text(const char* t
, off_t l
)
576 { this->text
.assign(t
, l
); }
579 // A mapping from warning symbol names (canonicalized in
580 // Symbol_table's namepool_ field) to
581 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
583 Warning_table warnings_
;
586 // The main linker symbol table.
595 // Add COUNT external symbols from the relocatable object OBJECT to
596 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
597 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
598 // point to the symbols in the symbol table.
599 template<int size
, bool big_endian
>
601 add_from_object(Relobj
* object
, const unsigned char* syms
,
602 size_t count
, const char* sym_names
, size_t sym_name_size
,
603 Symbol
** sympointers
);
605 // Define a special symbol.
606 template<int size
, bool big_endian
>
608 define_special_symbol(Target
* target
, const char* name
, bool only_if_ref
611 // Define a special symbol based on an Output_data. It is a
612 // multiple definition error if this symbol is already defined.
614 define_in_output_data(Target
*, const char* name
, Output_data
*,
615 uint64_t value
, uint64_t symsize
,
616 elfcpp::STT type
, elfcpp::STB binding
,
617 elfcpp::STV visibility
, unsigned char nonvis
,
618 bool offset_is_from_end
, bool only_if_ref
);
620 // Define a special symbol based on an Output_segment. It is a
621 // multiple definition error if this symbol is already defined.
623 define_in_output_segment(Target
*, const char* name
, Output_segment
*,
624 uint64_t value
, uint64_t symsize
,
625 elfcpp::STT type
, elfcpp::STB binding
,
626 elfcpp::STV visibility
, unsigned char nonvis
,
627 Symbol::Segment_offset_base
, bool only_if_ref
);
629 // Define a special symbol with a constant value. It is a multiple
630 // definition error if this symbol is already defined.
632 define_as_constant(Target
*, const char* name
, uint64_t value
,
633 uint64_t symsize
, elfcpp::STT type
, elfcpp::STB binding
,
634 elfcpp::STV visibility
, unsigned char nonvis
,
637 // Define a set of symbols in output sections.
639 define_symbols(const Layout
*, Target
*, int count
,
640 const Define_symbol_in_section
*);
642 // Define a set of symbols in output segments.
644 define_symbols(const Layout
*, Target
*, int count
,
645 const Define_symbol_in_segment
*);
649 lookup(const char*, const char* version
= NULL
) const;
651 // Return the real symbol associated with the forwarder symbol FROM.
653 resolve_forwards(Symbol
* from
) const;
655 // Return the size of the symbols in the table.
658 { return this->size_
; }
660 // Return the sized version of a symbol in this table.
663 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
666 const Sized_symbol
<size
>*
667 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
669 // Return the count of undefined symbols seen.
671 saw_undefined() const
672 { return this->saw_undefined_
; }
674 // Allocate the common symbols
676 allocate_commons(const General_options
&, Layout
*);
678 // Add a warning for symbol NAME in section SHNDX in object OBJ.
680 add_warning(const char* name
, Object
* obj
, unsigned int shndx
)
681 { this->warnings_
.add_warning(this, name
, obj
, shndx
); }
683 // Canonicalize a symbol name for use in the hash table.
685 canonicalize_name(const char* name
)
686 { return this->namepool_
.add(name
, NULL
); }
688 // Possibly issue a warning for a reference to SYM at LOCATION which
691 issue_warning(Symbol
* sym
, const std::string
& location
) const
692 { this->warnings_
.issue_warning(sym
, location
); }
694 // Finalize the symbol table after we have set the final addresses
695 // of all the input sections. This sets the final symbol values and
696 // adds the names to *POOL. It records the file offset OFF, and
697 // returns the new file offset.
699 finalize(off_t
, Stringpool
*);
701 // Write out the global symbols.
703 write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
706 Symbol_table(const Symbol_table
&);
707 Symbol_table
& operator=(const Symbol_table
&);
709 // Set the size of the symbols in the table.
712 { this->size_
= size
; }
714 // Make FROM a forwarder symbol to TO.
716 make_forwarder(Symbol
* from
, Symbol
* to
);
719 template<int size
, bool big_endian
>
721 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
722 const char *version
, Stringpool::Key version_key
,
723 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
);
726 template<int size
, bool big_endian
>
728 resolve(Sized_symbol
<size
>* to
,
729 const elfcpp::Sym
<size
, big_endian
>& sym
,
732 template<int size
, bool big_endian
>
734 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
737 // Define a symbol in an Output_data, sized version.
740 do_define_in_output_data(Target
*, const char* name
, Output_data
*,
741 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
742 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
743 elfcpp::STT type
, elfcpp::STB binding
,
744 elfcpp::STV visibility
, unsigned char nonvis
,
745 bool offset_is_from_end
, bool only_if_ref
);
747 // Define a symbol in an Output_segment, sized version.
750 do_define_in_output_segment(
751 Target
*, const char* name
, Output_segment
* os
,
752 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
753 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
754 elfcpp::STT type
, elfcpp::STB binding
,
755 elfcpp::STV visibility
, unsigned char nonvis
,
756 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
758 // Define a symbol as a constant, sized version.
761 do_define_as_constant(
762 Target
*, const char* name
,
763 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
764 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
765 elfcpp::STT type
, elfcpp::STB binding
,
766 elfcpp::STV visibility
, unsigned char nonvis
,
769 // Allocate the common symbols, sized version.
772 do_allocate_commons(const General_options
&, Layout
*);
774 // Finalize symbols specialized for size.
777 sized_finalize(off_t
, Stringpool
*);
779 // Write globals specialized for size and endianness.
780 template<int size
, bool big_endian
>
782 sized_write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
784 // The type of the symbol hash table.
786 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
788 struct Symbol_table_hash
791 operator()(const Symbol_table_key
&) const;
794 struct Symbol_table_eq
797 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
800 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
801 Symbol_table_eq
> Symbol_table_type
;
803 // The type of the list of common symbols.
805 typedef std::vector
<Symbol
*> Commons_type
;
807 // The size of the symbols in the symbol table (32 or 64).
810 // We increment this every time we see a new undefined symbol, for
811 // use in archive groups.
814 // The file offset within the output symtab section where we should
818 // The number of global symbols we want to write out.
819 size_t output_count_
;
821 // The symbol hash table.
822 Symbol_table_type table_
;
824 // A pool of symbol names. This is used for all global symbols.
825 // Entries in the hash table point into this pool.
826 Stringpool namepool_
;
828 // Forwarding symbols.
829 Unordered_map
<Symbol
*, Symbol
*> forwarders_
;
831 // We don't expect there to be very many common symbols, so we keep
832 // a list of them. When we find a common symbol we add it to this
833 // list. It is possible that by the time we process the list the
834 // symbol is no longer a common symbol. It may also have become a
836 Commons_type commons_
;
838 // Manage symbol warnings.
842 // We inline get_sized_symbol for efficiency.
846 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
848 assert(size
== this->get_size());
849 return static_cast<Sized_symbol
<size
>*>(sym
);
853 const Sized_symbol
<size
>*
854 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
856 assert(size
== this->get_size());
857 return static_cast<const Sized_symbol
<size
>*>(sym
);
860 } // End namespace gold.
862 #endif // !defined(GOLD_SYMTAB_H)