1 // symtab.h -- the gold symbol table -*- C++ -*-
12 #include "stringpool.h"
26 template<int size, bool big_endian>
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 an input file--this is the most common case.
45 // Symbol defined in an Output_data, a special section created by
48 // Symbol defined in an Output_segment, with no associated
51 // Symbol value is constant.
55 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
57 enum Segment_offset_base
59 // From the start of the segment.
61 // From the end of the segment.
63 // From the filesz of the segment--i.e., after the loaded bytes
64 // but before the bytes which are allocated but zeroed.
68 // Return the symbol name.
71 { return this->name_; }
73 // Return the symbol version. This will return NULL for an
74 // unversioned symbol.
77 { return this->version_; }
79 // Return the symbol source.
82 { return this->source_; }
84 // Return the object with which this symbol is associated.
88 assert(this->source_ == FROM_OBJECT);
89 return this->u_.from_object.object;
92 // Return the index of the section in the input object file.
96 assert(this->source_ == FROM_OBJECT);
97 return this->u_.from_object.shnum;
100 // Return the output data section with which this symbol is
101 // associated, if the symbol was specially defined with respect to
102 // an output data section.
106 assert(this->source_ == IN_OUTPUT_DATA);
107 return this->u_.in_output_data.output_data;
110 // If this symbol was defined with respect to an output data
111 // section, return whether the value is an offset from end.
113 offset_is_from_end() const
115 assert(this->source_ == IN_OUTPUT_DATA);
116 return this->u_.in_output_data.offset_is_from_end;
119 // Return the output segment with which this symbol is associated,
120 // if the symbol was specially defined with respect to an output
123 output_segment() const
125 assert(this->source_ == IN_OUTPUT_SEGMENT);
126 return this->u_.in_output_segment.output_segment;
129 // If this symbol was defined with respect to an output segment,
130 // return the offset base.
134 assert(this->source_ == IN_OUTPUT_SEGMENT);
135 return this->u_.in_output_segment.offset_base;
138 // Return the symbol binding.
141 { return this->binding_; }
143 // Return the symbol type.
146 { return this->type_; }
148 // Return the symbol visibility.
151 { return this->visibility_; }
153 // Return the non-visibility part of the st_other field.
156 { return this->nonvis_; }
158 // Return whether this symbol is a forwarder. This will never be
159 // true of a symbol found in the hash table, but may be true of
160 // symbol pointers attached to object files.
163 { return this->is_forwarder_; }
165 // Mark this symbol as a forwarder.
168 { this->is_forwarder_ = true; }
170 // Return whether this symbol was seen in a dynamic object.
173 { return this->in_dyn_; }
175 // Mark this symbol as seen in a dynamic object.
178 { this->in_dyn_ = true; }
180 // Return whether this symbol has an entry in the GOT section.
182 has_got_offset() const
183 { return this->has_got_offset_; }
185 // Return the offset into the GOT section of this symbol.
189 assert(this->has_got_offset());
190 return this->got_offset_;
193 // Set the GOT offset of this symbol.
195 set_got_offset(unsigned int got_offset)
197 this->has_got_offset_ = true;
198 this->got_offset_ = got_offset;
201 // Return whether this symbol is resolved locally. This is always
202 // true when linking statically. It is true for a symbol defined in
203 // this object when using -Bsymbolic. It is true for a symbol
204 // marked local in a version file. FIXME: This needs to be
207 is_resolved_locally() const
208 { return !this->in_dyn_; }
210 // Return whether this is an undefined symbol.
214 return this->source_ == FROM_OBJECT && this->shnum() == elfcpp::SHN_UNDEF;
217 // Return whether this is a common symbol.
221 return this->source_ == FROM_OBJECT && this->shnum() == elfcpp::SHN_COMMON;
225 // Instances of this class should always be created at a specific
230 // Initialize the general fields.
232 init_fields(const char* name, const char* version,
233 elfcpp::STT type, elfcpp::STB binding,
234 elfcpp::STV visibility, unsigned char nonvis);
236 // Initialize fields from an ELF symbol in OBJECT.
237 template<int size, bool big_endian>
239 init_base(const char *name, const char* version, Object* object,
240 const elfcpp::Sym<size, big_endian>&);
242 // Initialize fields for an Output_data.
244 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
245 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
247 // Initialize fields for an Output_segment.
249 init_base(const char* name, Output_segment* os, elfcpp::STT type,
250 elfcpp::STB binding, elfcpp::STV visibility,
251 unsigned char nonvis, Segment_offset_base offset_base);
253 // Initialize fields for a constant.
255 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
256 elfcpp::STV visibility, unsigned char nonvis);
258 // Override existing symbol.
259 template<int size, bool big_endian>
261 override_base(const elfcpp::Sym<size, big_endian>&, Object* object);
264 Symbol(const Symbol&);
265 Symbol& operator=(const Symbol&);
267 // Symbol name (expected to point into a Stringpool).
269 // Symbol version (expected to point into a Stringpool). This may
271 const char* version_;
275 // This struct is used if SOURCE_ == FROM_OBJECT.
278 // Object in which symbol is defined, or in which it was first
281 // Section number in object_ in which symbol is defined.
285 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
288 // Output_data in which symbol is defined. Before
289 // Layout::finalize the symbol's value is an offset within the
291 Output_data* output_data;
292 // True if the offset is from the end, false if the offset is
293 // from the beginning.
294 bool offset_is_from_end;
297 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
300 // Output_segment in which the symbol is defined. Before
301 // Layout::finalize the symbol's value is an offset.
302 Output_segment* output_segment;
303 // The base to use for the offset before Layout::finalize.
304 Segment_offset_base offset_base;
308 // If this symbol has an entry in the GOT section (has_got_offset_
309 // is true), this is the offset.
310 unsigned int got_offset_;
312 elfcpp::STT type_ : 4;
314 elfcpp::STB binding_ : 4;
315 // Symbol visibility.
316 elfcpp::STV visibility_ : 2;
317 // Rest of symbol st_other field.
318 unsigned int nonvis_ : 6;
319 // The type of symbol.
321 // True if this symbol always requires special target-specific
323 bool is_target_special_ : 1;
324 // True if this is the default version of the symbol.
326 // True if this symbol really forwards to another symbol. This is
327 // used when we discover after the fact that two different entries
328 // in the hash table really refer to the same symbol. This will
329 // never be set for a symbol found in the hash table, but may be set
330 // for a symbol found in the list of symbols attached to an Object.
331 // It forwards to the symbol found in the forwarders_ map of
333 bool is_forwarder_ : 1;
334 // True if we've seen this symbol in a dynamic object.
336 // True if the symbol has an entry in the GOT section.
337 bool has_got_offset_ : 1;
340 // The parts of a symbol which are size specific. Using a template
341 // derived class like this helps us use less space on a 32-bit system.
344 class Sized_symbol : public Symbol
347 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
348 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
353 // Initialize fields from an ELF symbol in OBJECT.
354 template<bool big_endian>
356 init(const char *name, const char* version, Object* object,
357 const elfcpp::Sym<size, big_endian>&);
359 // Initialize fields for an Output_data.
361 init(const char* name, Output_data*, Value_type value, Size_type symsize,
362 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
363 bool offset_is_from_end);
365 // Initialize fields for an Output_segment.
367 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
368 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
369 Segment_offset_base offset_base);
371 // Initialize fields for a constant.
373 init(const char* name, Value_type value, Size_type symsize,
374 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
376 // Override existing symbol.
377 template<bool big_endian>
379 override(const elfcpp::Sym<size, big_endian>&, Object* object);
381 // Return the symbol's value.
384 { return this->value_; }
386 // Return the symbol's size (we can't call this 'size' because that
387 // is a template parameter).
390 { return this->symsize_; }
392 // Set the symbol size. This is used when resolving common symbols.
394 set_symsize(Size_type symsize)
395 { this->symsize_ = symsize; }
397 // Set the symbol value. This is called when we store the final
398 // values of the symbols into the symbol table.
400 set_value(Value_type value)
401 { this->value_ = value; }
404 Sized_symbol(const Sized_symbol&);
405 Sized_symbol& operator=(const Sized_symbol&);
407 // Symbol value. Before Layout::finalize this is the offset in the
408 // input section. This is set to the final value during
415 // A struct describing a symbol defined by the linker, where the value
416 // of the symbol is defined based on an output section. This is used
417 // for symbols defined by the linker, like "_init_array_start".
419 struct Define_symbol_in_section
423 // The name of the output section with which this symbol should be
424 // associated. If there is no output section with that name, the
425 // symbol will be defined as zero.
426 const char* output_section;
427 // The offset of the symbol within the output section. This is an
428 // offset from the start of the output section, unless start_at_end
429 // is true, in which case this is an offset from the end of the
432 // The size of the symbol.
436 // The symbol binding.
438 // The symbol visibility.
439 elfcpp::STV visibility;
440 // The rest of the st_other field.
441 unsigned char nonvis;
442 // If true, the value field is an offset from the end of the output
444 bool offset_is_from_end;
445 // If true, this symbol is defined only if we see a reference to it.
449 // A struct describing a symbol defined by the linker, where the value
450 // of the symbol is defined based on a segment. This is used for
451 // symbols defined by the linker, like "_end". We describe the
452 // segment with which the symbol should be associated by its
453 // characteristics. If no segment meets these characteristics, the
454 // symbol will be defined as zero. If there is more than one segment
455 // which meets these characteristics, we will use the first one.
457 struct Define_symbol_in_segment
461 // The segment type where the symbol should be defined, typically
463 elfcpp::PT segment_type;
464 // Bitmask of segment flags which must be set.
465 elfcpp::PF segment_flags_set;
466 // Bitmask of segment flags which must be clear.
467 elfcpp::PF segment_flags_clear;
468 // The offset of the symbol within the segment. The offset is
469 // calculated from the position set by offset_base.
471 // The size of the symbol.
475 // The symbol binding.
477 // The symbol visibility.
478 elfcpp::STV visibility;
479 // The rest of the st_other field.
480 unsigned char nonvis;
481 // The base from which we compute the offset.
482 Symbol::Segment_offset_base offset_base;
483 // If true, this symbol is defined only if we see a reference to it.
487 // The main linker symbol table.
496 // Add COUNT external symbols from OBJECT to the symbol table. SYMS
497 // is the symbols, SYM_NAMES is their names, SYM_NAME_SIZE is the
498 // size of SYM_NAMES. This sets SYMPOINTERS to point to the symbols
499 // in the symbol table.
500 template<int size, bool big_endian>
502 add_from_object(Sized_object<size, big_endian>* object,
503 const elfcpp::Sym<size, big_endian>* syms,
504 size_t count, const char* sym_names, size_t sym_name_size,
505 Symbol** sympointers);
507 // Define a special symbol.
508 template<int size, bool big_endian>
510 define_special_symbol(Target* target, const char* name, bool only_if_ref
513 // Define a special symbol based on an Output_data. It is a
514 // multiple definition error if this symbol is already defined.
516 define_in_output_data(Target*, const char* name, Output_data*,
517 uint64_t value, uint64_t symsize,
518 elfcpp::STT type, elfcpp::STB binding,
519 elfcpp::STV visibility, unsigned char nonvis,
520 bool offset_is_from_end, bool only_if_ref);
522 // Define a special symbol based on an Output_segment. It is a
523 // multiple definition error if this symbol is already defined.
525 define_in_output_segment(Target*, const char* name, Output_segment*,
526 uint64_t value, uint64_t symsize,
527 elfcpp::STT type, elfcpp::STB binding,
528 elfcpp::STV visibility, unsigned char nonvis,
529 Symbol::Segment_offset_base, bool only_if_ref);
531 // Define a special symbol with a constant value. It is a multiple
532 // definition error if this symbol is already defined.
534 define_as_constant(Target*, const char* name, uint64_t value,
535 uint64_t symsize, elfcpp::STT type, elfcpp::STB binding,
536 elfcpp::STV visibility, unsigned char nonvis,
539 // Define a set of symbols in output sections.
541 define_symbols(const Layout*, Target*, int count,
542 const Define_symbol_in_section*);
544 // Define a set of symbols in output segments.
546 define_symbols(const Layout*, Target*, int count,
547 const Define_symbol_in_segment*);
551 lookup(const char*, const char* version = NULL) const;
553 // Return the real symbol associated with the forwarder symbol FROM.
555 resolve_forwards(Symbol* from) const;
557 // Return the size of the symbols in the table.
560 { return this->size_; }
562 // Return the sized version of a symbol in this table.
565 get_sized_symbol(Symbol* ACCEPT_SIZE) const;
568 const Sized_symbol<size>*
569 get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
571 // Return the count of undefined symbols seen.
573 saw_undefined() const
574 { return this->saw_undefined_; }
576 // Allocate the common symbols
578 allocate_commons(const General_options&, Layout*);
580 // Finalize the symbol table after we have set the final addresses
581 // of all the input sections. This sets the final symbol values and
582 // adds the names to *POOL. It records the file offset OFF, and
583 // returns the new file offset.
585 finalize(off_t, Stringpool*);
587 // Write out the global symbols.
589 write_globals(const Target*, const Stringpool*, Output_file*) const;
592 Symbol_table(const Symbol_table&);
593 Symbol_table& operator=(const Symbol_table&);
595 // Set the size of the symbols in the table.
598 { this->size_ = size; }
600 // Make FROM a forwarder symbol to TO.
602 make_forwarder(Symbol* from, Symbol* to);
605 template<int size, bool big_endian>
607 add_from_object(Sized_object<size, big_endian>*, const char *name,
608 const char *version, bool def,
609 const elfcpp::Sym<size, big_endian>& sym);
612 template<int size, bool big_endian>
614 resolve(Sized_symbol<size>* to,
615 const elfcpp::Sym<size, big_endian>& sym,
618 template<int size, bool big_endian>
620 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
623 // Define a symbol in an Output_data, sized version.
626 do_define_in_output_data(Target*, const char* name, Output_data*,
627 typename elfcpp::Elf_types<size>::Elf_Addr value,
628 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
629 elfcpp::STT type, elfcpp::STB binding,
630 elfcpp::STV visibility, unsigned char nonvis,
631 bool offset_is_from_end, bool only_if_ref);
633 // Define a symbol in an Output_segment, sized version.
636 do_define_in_output_segment(
637 Target*, const char* name, Output_segment* os,
638 typename elfcpp::Elf_types<size>::Elf_Addr value,
639 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
640 elfcpp::STT type, elfcpp::STB binding,
641 elfcpp::STV visibility, unsigned char nonvis,
642 Symbol::Segment_offset_base offset_base, bool only_if_ref);
644 // Define a symbol as a constant, sized version.
647 do_define_as_constant(
648 Target*, const char* name,
649 typename elfcpp::Elf_types<size>::Elf_Addr value,
650 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
651 elfcpp::STT type, elfcpp::STB binding,
652 elfcpp::STV visibility, unsigned char nonvis,
655 // Allocate the common symbols, sized version.
658 do_allocate_commons(const General_options&, Layout*);
660 // Finalize symbols specialized for size.
663 sized_finalize(off_t, Stringpool*);
665 // Write globals specialized for size and endianness.
666 template<int size, bool big_endian>
668 sized_write_globals(const Target*, const Stringpool*, Output_file*) const;
670 // The type of the symbol hash table.
672 typedef std::pair<const char*, const char*> Symbol_table_key;
674 struct Symbol_table_hash
677 operator()(const Symbol_table_key&) const;
680 struct Symbol_table_eq
683 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
686 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
687 Symbol_table_eq> Symbol_table_type;
689 // The type of the list of common symbols.
691 typedef std::vector<Symbol*> Commons_type;
693 // The size of the symbols in the symbol table (32 or 64).
696 // We increment this every time we see a new undefined symbol, for
697 // use in archive groups.
700 // The file offset within the output symtab section where we should
704 // The number of global symbols we want to write out.
705 size_t output_count_;
707 // The symbol hash table.
708 Symbol_table_type table_;
710 // A pool of symbol names. This is used for all global symbols.
711 // Entries in the hash table point into this pool.
712 Stringpool namepool_;
714 // Forwarding symbols.
715 Unordered_map<Symbol*, Symbol*> forwarders_;
717 // We don't expect there to be very many common symbols, so we keep
718 // a list of them. When we find a common symbol we add it to this
719 // list. It is possible that by the time we process the list the
720 // symbol is no longer a common symbol. It may also have become a
722 Commons_type commons_;
725 // We inline get_sized_symbol for efficiency.
729 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
731 assert(size == this->get_size());
732 return static_cast<Sized_symbol<size>*>(sym);
736 const Sized_symbol<size>*
737 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
739 assert(size == this->get_size());
740 return static_cast<const Sized_symbol<size>*>(sym);
743 } // End namespace gold.
745 #endif // !defined(GOLD_SYMTAB_H)