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 was ever seen in a dynamic object.
179 { return this->in_dyn_
; }
181 // Mark this symbol as having been seen in a dynamic object.
184 { this->in_dyn_
= true; }
186 // Return whether this symbol has an entry in the GOT section.
188 has_got_offset() const
189 { return this->has_got_offset_
; }
191 // Return the offset into the GOT section of this symbol.
195 assert(this->has_got_offset());
196 return this->got_offset_
;
199 // Set the GOT offset of this symbol.
201 set_got_offset(unsigned int got_offset
)
203 this->has_got_offset_
= true;
204 this->got_offset_
= got_offset
;
207 // Return whether this symbol is resolved locally. This is always
208 // true when linking statically. It is true for a symbol defined in
209 // this object when using -Bsymbolic. It is true for a symbol
210 // marked local in a version file. FIXME: This needs to be
213 is_resolved_locally() const
214 { return !this->in_dyn_
; }
216 // Return whether this is a defined symbol (not undefined or
221 return (this->source_
!= FROM_OBJECT
222 || (this->u_
.from_object
.shnum
!= elfcpp::SHN_UNDEF
223 && this->u_
.from_object
.shnum
!= elfcpp::SHN_COMMON
));
226 // Return whether this is an undefined symbol.
230 return this->source_
== FROM_OBJECT
&& this->shnum() == elfcpp::SHN_UNDEF
;
233 // Return whether this is a common symbol.
237 return (this->source_
== FROM_OBJECT
238 && (this->u_
.from_object
.shnum
== elfcpp::SHN_COMMON
239 || this->type_
== elfcpp::STT_COMMON
));
242 // Return whether there should be a warning for references to this
246 { return this->has_warning_
; }
248 // Mark this symbol as having a warning.
251 { this->has_warning_
= true; }
254 // Instances of this class should always be created at a specific
257 { memset(this, 0, sizeof *this); }
259 // Initialize the general fields.
261 init_fields(const char* name
, const char* version
,
262 elfcpp::STT type
, elfcpp::STB binding
,
263 elfcpp::STV visibility
, unsigned char nonvis
);
265 // Initialize fields from an ELF symbol in OBJECT.
266 template<int size
, bool big_endian
>
268 init_base(const char *name
, const char* version
, Object
* object
,
269 const elfcpp::Sym
<size
, big_endian
>&);
271 // Initialize fields for an Output_data.
273 init_base(const char* name
, Output_data
*, elfcpp::STT
, elfcpp::STB
,
274 elfcpp::STV
, unsigned char nonvis
, bool offset_is_from_end
);
276 // Initialize fields for an Output_segment.
278 init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
279 elfcpp::STB binding
, elfcpp::STV visibility
,
280 unsigned char nonvis
, Segment_offset_base offset_base
);
282 // Initialize fields for a constant.
284 init_base(const char* name
, elfcpp::STT type
, elfcpp::STB binding
,
285 elfcpp::STV visibility
, unsigned char nonvis
);
287 // Override existing symbol.
288 template<int size
, bool big_endian
>
290 override_base(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
293 Symbol(const Symbol
&);
294 Symbol
& operator=(const Symbol
&);
296 // Symbol name (expected to point into a Stringpool).
298 // Symbol version (expected to point into a Stringpool). This may
300 const char* version_
;
304 // This struct is used if SOURCE_ == FROM_OBJECT.
307 // Object in which symbol is defined, or in which it was first
310 // Section number in object_ in which symbol is defined.
314 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
317 // Output_data in which symbol is defined. Before
318 // Layout::finalize the symbol's value is an offset within the
320 Output_data
* output_data
;
321 // True if the offset is from the end, false if the offset is
322 // from the beginning.
323 bool offset_is_from_end
;
326 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
329 // Output_segment in which the symbol is defined. Before
330 // Layout::finalize the symbol's value is an offset.
331 Output_segment
* output_segment
;
332 // The base to use for the offset before Layout::finalize.
333 Segment_offset_base offset_base
;
337 // If this symbol has an entry in the GOT section (has_got_offset_
338 // is true), this is the offset.
339 unsigned int got_offset_
;
341 elfcpp::STT type_
: 4;
343 elfcpp::STB binding_
: 4;
344 // Symbol visibility.
345 elfcpp::STV visibility_
: 2;
346 // Rest of symbol st_other field.
347 unsigned int nonvis_
: 6;
348 // The type of symbol.
350 // True if this symbol always requires special target-specific
352 bool is_target_special_
: 1;
353 // True if this is the default version of the symbol.
355 // True if this symbol really forwards to another symbol. This is
356 // used when we discover after the fact that two different entries
357 // in the hash table really refer to the same symbol. This will
358 // never be set for a symbol found in the hash table, but may be set
359 // for a symbol found in the list of symbols attached to an Object.
360 // It forwards to the symbol found in the forwarders_ map of
362 bool is_forwarder_
: 1;
363 // True if we've seen this symbol in a dynamic object.
365 // True if the symbol has an entry in the GOT section.
366 bool has_got_offset_
: 1;
367 // True if there is a warning for this symbol.
368 bool has_warning_
: 1;
371 // The parts of a symbol which are size specific. Using a template
372 // derived class like this helps us use less space on a 32-bit system.
375 class Sized_symbol
: public Symbol
378 typedef typename
elfcpp::Elf_types
<size
>::Elf_Addr Value_type
;
379 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Size_type
;
384 // Initialize fields from an ELF symbol in OBJECT.
385 template<bool big_endian
>
387 init(const char *name
, const char* version
, Object
* object
,
388 const elfcpp::Sym
<size
, big_endian
>&);
390 // Initialize fields for an Output_data.
392 init(const char* name
, Output_data
*, Value_type value
, Size_type symsize
,
393 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
394 bool offset_is_from_end
);
396 // Initialize fields for an Output_segment.
398 init(const char* name
, Output_segment
*, Value_type value
, Size_type symsize
,
399 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
,
400 Segment_offset_base offset_base
);
402 // Initialize fields for a constant.
404 init(const char* name
, Value_type value
, Size_type symsize
,
405 elfcpp::STT
, elfcpp::STB
, elfcpp::STV
, unsigned char nonvis
);
407 // Override existing symbol.
408 template<bool big_endian
>
410 override(const elfcpp::Sym
<size
, big_endian
>&, Object
* object
);
412 // Return the symbol's value.
415 { return this->value_
; }
417 // Return the symbol's size (we can't call this 'size' because that
418 // is a template parameter).
421 { return this->symsize_
; }
423 // Set the symbol size. This is used when resolving common symbols.
425 set_symsize(Size_type symsize
)
426 { this->symsize_
= symsize
; }
428 // Set the symbol value. This is called when we store the final
429 // values of the symbols into the symbol table.
431 set_value(Value_type value
)
432 { this->value_
= value
; }
435 Sized_symbol(const Sized_symbol
&);
436 Sized_symbol
& operator=(const Sized_symbol
&);
438 // Symbol value. Before Layout::finalize this is the offset in the
439 // input section. This is set to the final value during
446 // A struct describing a symbol defined by the linker, where the value
447 // of the symbol is defined based on an output section. This is used
448 // for symbols defined by the linker, like "_init_array_start".
450 struct Define_symbol_in_section
454 // The name of the output section with which this symbol should be
455 // associated. If there is no output section with that name, the
456 // symbol will be defined as zero.
457 const char* output_section
;
458 // The offset of the symbol within the output section. This is an
459 // offset from the start of the output section, unless start_at_end
460 // is true, in which case this is an offset from the end of the
463 // The size of the symbol.
467 // The symbol binding.
469 // The symbol visibility.
470 elfcpp::STV visibility
;
471 // The rest of the st_other field.
472 unsigned char nonvis
;
473 // If true, the value field is an offset from the end of the output
475 bool offset_is_from_end
;
476 // If true, this symbol is defined only if we see a reference to it.
480 // A struct describing a symbol defined by the linker, where the value
481 // of the symbol is defined based on a segment. This is used for
482 // symbols defined by the linker, like "_end". We describe the
483 // segment with which the symbol should be associated by its
484 // characteristics. If no segment meets these characteristics, the
485 // symbol will be defined as zero. If there is more than one segment
486 // which meets these characteristics, we will use the first one.
488 struct Define_symbol_in_segment
492 // The segment type where the symbol should be defined, typically
494 elfcpp::PT segment_type
;
495 // Bitmask of segment flags which must be set.
496 elfcpp::PF segment_flags_set
;
497 // Bitmask of segment flags which must be clear.
498 elfcpp::PF segment_flags_clear
;
499 // The offset of the symbol within the segment. The offset is
500 // calculated from the position set by offset_base.
502 // The size of the symbol.
506 // The symbol binding.
508 // The symbol visibility.
509 elfcpp::STV visibility
;
510 // The rest of the st_other field.
511 unsigned char nonvis
;
512 // The base from which we compute the offset.
513 Symbol::Segment_offset_base offset_base
;
514 // If true, this symbol is defined only if we see a reference to it.
518 // This class manages warnings. Warnings are a GNU extension. When
519 // we see a section named .gnu.warning.SYM in an object file, and if
520 // we wind using the definition of SYM from that object file, then we
521 // will issue a warning for any relocation against SYM from a
522 // different object file. The text of the warning is the contents of
523 // the section. This is not precisely the definition used by the old
524 // GNU linker; the old GNU linker treated an occurrence of
525 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
526 // would trigger a warning on any reference. However, it was
527 // inconsistent in that a warning in a dynamic object only triggered
528 // if there was no definition in a regular object. This linker is
529 // different in that we only issue a warning if we use the symbol
530 // definition from the same object file as the warning section.
539 // Add a warning for symbol NAME in section SHNDX in object OBJ.
541 add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
544 // For each symbol for which we should give a warning, make a note
547 note_warnings(Symbol_table
* symtab
);
549 // Issue a warning for a reference to SYM at LOCATION.
551 issue_warning(Symbol
* sym
, const std::string
& location
) const;
554 Warnings(const Warnings
&);
555 Warnings
& operator=(const Warnings
&);
557 // What we need to know to get the warning text.
558 struct Warning_location
560 // The object the warning is in.
562 // The index of the warning section.
564 // The warning text if we have already loaded it.
568 : object(NULL
), shndx(0), text()
572 set(Object
* o
, unsigned int s
)
579 set_text(const char* t
, off_t l
)
580 { this->text
.assign(t
, l
); }
583 // A mapping from warning symbol names (canonicalized in
584 // Symbol_table's namepool_ field) to
585 typedef Unordered_map
<const char*, Warning_location
> Warning_table
;
587 Warning_table warnings_
;
590 // The main linker symbol table.
599 // Add COUNT external symbols from the relocatable object RELOBJ to
600 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
601 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
602 // point to the symbols in the symbol table.
603 template<int size
, bool big_endian
>
605 add_from_relobj(Sized_relobj
<size
, big_endian
>* relobj
,
606 const unsigned char* syms
, size_t count
,
607 const char* sym_names
, size_t sym_name_size
,
608 Symbol
** sympointers
);
610 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
611 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
612 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
613 // symbol version data.
614 template<int size
, bool big_endian
>
616 add_from_dynobj(Sized_dynobj
<size
, big_endian
>* dynobj
,
617 const unsigned char* syms
, size_t count
,
618 const char* sym_names
, size_t sym_name_size
,
619 const unsigned char* versym
, size_t versym_size
,
620 const std::vector
<const char*>*);
622 // Define a special symbol.
623 template<int size
, bool big_endian
>
625 define_special_symbol(Target
* target
, const char* name
, bool only_if_ref
628 // Define a special symbol based on an Output_data. It is a
629 // multiple definition error if this symbol is already defined.
631 define_in_output_data(Target
*, const char* name
, Output_data
*,
632 uint64_t value
, uint64_t symsize
,
633 elfcpp::STT type
, elfcpp::STB binding
,
634 elfcpp::STV visibility
, unsigned char nonvis
,
635 bool offset_is_from_end
, bool only_if_ref
);
637 // Define a special symbol based on an Output_segment. It is a
638 // multiple definition error if this symbol is already defined.
640 define_in_output_segment(Target
*, const char* name
, Output_segment
*,
641 uint64_t value
, uint64_t symsize
,
642 elfcpp::STT type
, elfcpp::STB binding
,
643 elfcpp::STV visibility
, unsigned char nonvis
,
644 Symbol::Segment_offset_base
, bool only_if_ref
);
646 // Define a special symbol with a constant value. It is a multiple
647 // definition error if this symbol is already defined.
649 define_as_constant(Target
*, const char* name
, uint64_t value
,
650 uint64_t symsize
, elfcpp::STT type
, elfcpp::STB binding
,
651 elfcpp::STV visibility
, unsigned char nonvis
,
654 // Define a set of symbols in output sections.
656 define_symbols(const Layout
*, Target
*, int count
,
657 const Define_symbol_in_section
*);
659 // Define a set of symbols in output segments.
661 define_symbols(const Layout
*, Target
*, int count
,
662 const Define_symbol_in_segment
*);
666 lookup(const char*, const char* version
= NULL
) const;
668 // Return the real symbol associated with the forwarder symbol FROM.
670 resolve_forwards(Symbol
* from
) const;
672 // Return the size of the symbols in the table.
675 { return this->size_
; }
677 // Return the sized version of a symbol in this table.
680 get_sized_symbol(Symbol
* ACCEPT_SIZE
) const;
683 const Sized_symbol
<size
>*
684 get_sized_symbol(const Symbol
* ACCEPT_SIZE
) const;
686 // Return the count of undefined symbols seen.
688 saw_undefined() const
689 { return this->saw_undefined_
; }
691 // Allocate the common symbols
693 allocate_commons(const General_options
&, Layout
*);
695 // Add a warning for symbol NAME in section SHNDX in object OBJ.
697 add_warning(const char* name
, Object
* obj
, unsigned int shndx
)
698 { this->warnings_
.add_warning(this, name
, obj
, shndx
); }
700 // Canonicalize a symbol name for use in the hash table.
702 canonicalize_name(const char* name
)
703 { return this->namepool_
.add(name
, NULL
); }
705 // Possibly issue a warning for a reference to SYM at LOCATION which
708 issue_warning(Symbol
* sym
, const std::string
& location
) const
709 { this->warnings_
.issue_warning(sym
, location
); }
711 // Finalize the symbol table after we have set the final addresses
712 // of all the input sections. This sets the final symbol values and
713 // adds the names to *POOL. It records the file offset OFF, and
714 // returns the new file offset.
716 finalize(off_t
, Stringpool
*);
718 // Write out the global symbols.
720 write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
723 Symbol_table(const Symbol_table
&);
724 Symbol_table
& operator=(const Symbol_table
&);
726 // Set the size of the symbols in the table.
729 { this->size_
= size
; }
731 // Make FROM a forwarder symbol to TO.
733 make_forwarder(Symbol
* from
, Symbol
* to
);
736 template<int size
, bool big_endian
>
738 add_from_object(Object
*, const char *name
, Stringpool::Key name_key
,
739 const char *version
, Stringpool::Key version_key
,
740 bool def
, const elfcpp::Sym
<size
, big_endian
>& sym
);
743 template<int size
, bool big_endian
>
745 resolve(Sized_symbol
<size
>* to
,
746 const elfcpp::Sym
<size
, big_endian
>& sym
,
749 template<int size
, bool big_endian
>
751 resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
754 // Define a symbol in an Output_data, sized version.
757 do_define_in_output_data(Target
*, const char* name
, Output_data
*,
758 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
759 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
760 elfcpp::STT type
, elfcpp::STB binding
,
761 elfcpp::STV visibility
, unsigned char nonvis
,
762 bool offset_is_from_end
, bool only_if_ref
);
764 // Define a symbol in an Output_segment, sized version.
767 do_define_in_output_segment(
768 Target
*, const char* name
, Output_segment
* os
,
769 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
770 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
771 elfcpp::STT type
, elfcpp::STB binding
,
772 elfcpp::STV visibility
, unsigned char nonvis
,
773 Symbol::Segment_offset_base offset_base
, bool only_if_ref
);
775 // Define a symbol as a constant, sized version.
778 do_define_as_constant(
779 Target
*, const char* name
,
780 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
781 typename
elfcpp::Elf_types
<size
>::Elf_WXword ssize
,
782 elfcpp::STT type
, elfcpp::STB binding
,
783 elfcpp::STV visibility
, unsigned char nonvis
,
786 // Allocate the common symbols, sized version.
789 do_allocate_commons(const General_options
&, Layout
*);
791 // Finalize symbols specialized for size.
794 sized_finalize(off_t
, Stringpool
*);
796 // Write globals specialized for size and endianness.
797 template<int size
, bool big_endian
>
799 sized_write_globals(const Target
*, const Stringpool
*, Output_file
*) const;
801 // The type of the symbol hash table.
803 typedef std::pair
<Stringpool::Key
, Stringpool::Key
> Symbol_table_key
;
805 struct Symbol_table_hash
808 operator()(const Symbol_table_key
&) const;
811 struct Symbol_table_eq
814 operator()(const Symbol_table_key
&, const Symbol_table_key
&) const;
817 typedef Unordered_map
<Symbol_table_key
, Symbol
*, Symbol_table_hash
,
818 Symbol_table_eq
> Symbol_table_type
;
820 // The type of the list of common symbols.
822 typedef std::vector
<Symbol
*> Commons_type
;
824 // The size of the symbols in the symbol table (32 or 64).
827 // We increment this every time we see a new undefined symbol, for
828 // use in archive groups.
831 // The file offset within the output symtab section where we should
835 // The number of global symbols we want to write out.
836 size_t output_count_
;
838 // The symbol hash table.
839 Symbol_table_type table_
;
841 // A pool of symbol names. This is used for all global symbols.
842 // Entries in the hash table point into this pool.
843 Stringpool namepool_
;
845 // Forwarding symbols.
846 Unordered_map
<Symbol
*, Symbol
*> forwarders_
;
848 // We don't expect there to be very many common symbols, so we keep
849 // a list of them. When we find a common symbol we add it to this
850 // list. It is possible that by the time we process the list the
851 // symbol is no longer a common symbol. It may also have become a
853 Commons_type commons_
;
855 // Manage symbol warnings.
859 // We inline get_sized_symbol for efficiency.
863 Symbol_table::get_sized_symbol(Symbol
* sym ACCEPT_SIZE
) const
865 assert(size
== this->get_size());
866 return static_cast<Sized_symbol
<size
>*>(sym
);
870 const Sized_symbol
<size
>*
871 Symbol_table::get_sized_symbol(const Symbol
* sym ACCEPT_SIZE
) const
873 assert(size
== this->get_size());
874 return static_cast<const Sized_symbol
<size
>*>(sym
);
877 } // End namespace gold.
879 #endif // !defined(GOLD_SYMTAB_H)