binutils/
[binutils.git] / gold / symtab.h
blobcba97d0bf9ec1a932f032a9ec92cf0d5d76dab84
1 // symtab.h -- the gold symbol table -*- C++ -*-
3 // Symbol_table
4 // The symbol table.
6 #include <string>
7 #include <utility>
8 #include <vector>
10 #include "elfcpp.h"
11 #include "stringpool.h"
12 #include "object.h"
14 #ifndef GOLD_SYMTAB_H
15 #define GOLD_SYMTAB_H
17 namespace gold
20 class Object;
21 class Relobj;
22 template<int size, bool big_endian>
23 class Sized_relobj;
24 class Dynobj;
25 template<int size, bool big_endian>
26 class Sized_dynobj;
27 class Versions;
28 class Output_data;
29 class Output_section;
30 class Output_segment;
31 class Output_file;
32 class Target;
34 // The base class of an entry in the symbol table. The symbol table
35 // can have a lot of entries, so we don't want this class to big.
36 // Size dependent fields can be found in the template class
37 // Sized_symbol. Targets may support their own derived classes.
39 class Symbol
41 public:
42 // Because we want the class to be small, we don't use any virtual
43 // functions. But because symbols can be defined in different
44 // places, we need to classify them. This enum is the different
45 // sources of symbols we support.
46 enum Source
48 // Symbol defined in a relocatable or dynamic input file--this is
49 // the most common case.
50 FROM_OBJECT,
51 // Symbol defined in an Output_data, a special section created by
52 // the target.
53 IN_OUTPUT_DATA,
54 // Symbol defined in an Output_segment, with no associated
55 // section.
56 IN_OUTPUT_SEGMENT,
57 // Symbol value is constant.
58 CONSTANT
61 // When the source is IN_OUTPUT_SEGMENT, we need to describe what
62 // the offset means.
63 enum Segment_offset_base
65 // From the start of the segment.
66 SEGMENT_START,
67 // From the end of the segment.
68 SEGMENT_END,
69 // From the filesz of the segment--i.e., after the loaded bytes
70 // but before the bytes which are allocated but zeroed.
71 SEGMENT_BSS
74 // Return the symbol name.
75 const char*
76 name() const
77 { return this->name_; }
79 // Return the symbol version. This will return NULL for an
80 // unversioned symbol.
81 const char*
82 version() const
83 { return this->version_; }
85 // Return the symbol source.
86 Source
87 source() const
88 { return this->source_; }
90 // Return the object with which this symbol is associated.
91 Object*
92 object() const
94 gold_assert(this->source_ == FROM_OBJECT);
95 return this->u_.from_object.object;
98 // Return the index of the section in the input relocatable or
99 // dynamic object file.
100 unsigned int
101 shndx() const
103 gold_assert(this->source_ == FROM_OBJECT);
104 return this->u_.from_object.shndx;
107 // Return the output data section with which this symbol is
108 // associated, if the symbol was specially defined with respect to
109 // an output data section.
110 Output_data*
111 output_data() const
113 gold_assert(this->source_ == IN_OUTPUT_DATA);
114 return this->u_.in_output_data.output_data;
117 // If this symbol was defined with respect to an output data
118 // section, return whether the value is an offset from end.
119 bool
120 offset_is_from_end() const
122 gold_assert(this->source_ == IN_OUTPUT_DATA);
123 return this->u_.in_output_data.offset_is_from_end;
126 // Return the output segment with which this symbol is associated,
127 // if the symbol was specially defined with respect to an output
128 // segment.
129 Output_segment*
130 output_segment() const
132 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
133 return this->u_.in_output_segment.output_segment;
136 // If this symbol was defined with respect to an output segment,
137 // return the offset base.
138 Segment_offset_base
139 offset_base() const
141 gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
142 return this->u_.in_output_segment.offset_base;
145 // Return the symbol binding.
146 elfcpp::STB
147 binding() const
148 { return this->binding_; }
150 // Return the symbol type.
151 elfcpp::STT
152 type() const
153 { return this->type_; }
155 // Return the symbol visibility.
156 elfcpp::STV
157 visibility() const
158 { return this->visibility_; }
160 // Return the non-visibility part of the st_other field.
161 unsigned char
162 nonvis() const
163 { return this->nonvis_; }
165 // Return whether this symbol is a forwarder. This will never be
166 // true of a symbol found in the hash table, but may be true of
167 // symbol pointers attached to object files.
168 bool
169 is_forwarder() const
170 { return this->is_forwarder_; }
172 // Mark this symbol as a forwarder.
173 void
174 set_forwarder()
175 { this->is_forwarder_ = true; }
177 // Return whether this symbol needs an entry in the dynamic symbol
178 // table.
179 bool
180 needs_dynsym_entry() const
181 { return this->needs_dynsym_entry_; }
183 // Mark this symbol as needing an entry in the dynamic symbol table.
184 void
185 set_needs_dynsym_entry()
186 { this->needs_dynsym_entry_ = true; }
188 // Return whether this symbol has been seen in a regular object.
189 bool
190 in_reg() const
191 { return this->in_reg_; }
193 // Mark this symbol as having been seen in a regular object.
194 void
195 set_in_reg()
196 { this->in_reg_ = true; }
198 // Mark this symbol as having been seen in a dynamic object.
199 void
200 set_in_dyn()
201 { this->in_dyn_ = true; }
203 // Return the index of this symbol in the output file symbol table.
204 // A value of -1U means that this symbol is not going into the
205 // output file. This starts out as zero, and is set to a non-zero
206 // value by Symbol_table::finalize. It is an error to ask for the
207 // symbol table index before it has been set.
208 unsigned int
209 symtab_index() const
211 gold_assert(this->symtab_index_ != 0);
212 return this->symtab_index_;
215 // Set the index of the symbol in the output file symbol table.
216 void
217 set_symtab_index(unsigned int index)
219 gold_assert(index != 0);
220 this->symtab_index_ = index;
223 // Return whether this symbol already has an index in the output
224 // file symbol table.
225 bool
226 has_symtab_index() const
227 { return this->symtab_index_ != 0; }
229 // Return the index of this symbol in the dynamic symbol table. A
230 // value of -1U means that this symbol is not going into the dynamic
231 // symbol table. This starts out as zero, and is set to a non-zero
232 // during Layout::finalize. It is an error to ask for the dynamic
233 // symbol table index before it has been set.
234 unsigned int
235 dynsym_index() const
237 gold_assert(this->dynsym_index_ != 0);
238 return this->dynsym_index_;
241 // Set the index of the symbol in the dynamic symbol table.
242 void
243 set_dynsym_index(unsigned int index)
245 gold_assert(index != 0);
246 this->dynsym_index_ = index;
249 // Return whether this symbol already has an index in the dynamic
250 // symbol table.
251 bool
252 has_dynsym_index() const
253 { return this->dynsym_index_ != 0; }
255 // Return whether this symbol has an entry in the GOT section.
256 bool
257 has_got_offset() const
258 { return this->has_got_offset_; }
260 // Return the offset into the GOT section of this symbol.
261 unsigned int
262 got_offset() const
264 gold_assert(this->has_got_offset());
265 return this->got_offset_;
268 // Set the GOT offset of this symbol.
269 void
270 set_got_offset(unsigned int got_offset)
272 this->has_got_offset_ = true;
273 this->got_offset_ = got_offset;
276 // Return whether this symbol has an entry in the PLT section.
277 bool
278 has_plt_offset() const
279 { return this->has_plt_offset_; }
281 // Return the offset into the PLT section of this symbol.
282 unsigned int
283 plt_offset() const
285 gold_assert(this->has_plt_offset());
286 return this->plt_offset_;
289 // Set the PLT offset of this symbol.
290 void
291 set_plt_offset(unsigned int plt_offset)
293 this->has_plt_offset_ = true;
294 this->plt_offset_ = plt_offset;
297 // Return true if the final value of this symbol is known at link
298 // time.
299 bool
300 final_value_is_known(const General_options* options) const
302 if (options->is_shared())
303 return false;
304 return this->source_ != FROM_OBJECT || !this->object()->is_dynamic();
307 // Return whether this is a defined symbol (not undefined or
308 // common).
309 bool
310 is_defined() const
312 return (this->source_ != FROM_OBJECT
313 || (this->shndx() != elfcpp::SHN_UNDEF
314 && this->shndx() != elfcpp::SHN_COMMON));
317 // Return true if this symbol is from a dynamic object.
318 bool
319 is_from_dynobj() const
321 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
324 // Return whether this is an undefined symbol.
325 bool
326 is_undefined() const
328 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
331 // Return whether this is a common symbol.
332 bool
333 is_common() const
335 return (this->source_ == FROM_OBJECT
336 && (this->shndx() == elfcpp::SHN_COMMON
337 || this->type_ == elfcpp::STT_COMMON));
340 // Return whether this symbol can be seen outside this object.
341 bool
342 is_externally_visible() const
344 return (this->visibility_ == elfcpp::STV_DEFAULT
345 || this->visibility_ == elfcpp::STV_PROTECTED);
348 // Return whether there should be a warning for references to this
349 // symbol.
350 bool
351 has_warning() const
352 { return this->has_warning_; }
354 // Mark this symbol as having a warning.
355 void
356 set_has_warning()
357 { this->has_warning_ = true; }
359 protected:
360 // Instances of this class should always be created at a specific
361 // size.
362 Symbol()
363 { memset(this, 0, sizeof *this); }
365 // Initialize the general fields.
366 void
367 init_fields(const char* name, const char* version,
368 elfcpp::STT type, elfcpp::STB binding,
369 elfcpp::STV visibility, unsigned char nonvis);
371 // Initialize fields from an ELF symbol in OBJECT.
372 template<int size, bool big_endian>
373 void
374 init_base(const char *name, const char* version, Object* object,
375 const elfcpp::Sym<size, big_endian>&);
377 // Initialize fields for an Output_data.
378 void
379 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
380 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
382 // Initialize fields for an Output_segment.
383 void
384 init_base(const char* name, Output_segment* os, elfcpp::STT type,
385 elfcpp::STB binding, elfcpp::STV visibility,
386 unsigned char nonvis, Segment_offset_base offset_base);
388 // Initialize fields for a constant.
389 void
390 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
391 elfcpp::STV visibility, unsigned char nonvis);
393 // Override existing symbol.
394 template<int size, bool big_endian>
395 void
396 override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
397 const char* version);
399 private:
400 Symbol(const Symbol&);
401 Symbol& operator=(const Symbol&);
403 // Symbol name (expected to point into a Stringpool).
404 const char* name_;
405 // Symbol version (expected to point into a Stringpool). This may
406 // be NULL.
407 const char* version_;
409 union
411 // This struct is used if SOURCE_ == FROM_OBJECT.
412 struct
414 // Object in which symbol is defined, or in which it was first
415 // seen.
416 Object* object;
417 // Section number in object_ in which symbol is defined.
418 unsigned int shndx;
419 } from_object;
421 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
422 struct
424 // Output_data in which symbol is defined. Before
425 // Layout::finalize the symbol's value is an offset within the
426 // Output_data.
427 Output_data* output_data;
428 // True if the offset is from the end, false if the offset is
429 // from the beginning.
430 bool offset_is_from_end;
431 } in_output_data;
433 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
434 struct
436 // Output_segment in which the symbol is defined. Before
437 // Layout::finalize the symbol's value is an offset.
438 Output_segment* output_segment;
439 // The base to use for the offset before Layout::finalize.
440 Segment_offset_base offset_base;
441 } in_output_segment;
442 } u_;
444 // The index of this symbol in the output file. If the symbol is
445 // not going into the output file, this value is -1U. This field
446 // starts as always holding zero. It is set to a non-zero value by
447 // Symbol_table::finalize.
448 unsigned int symtab_index_;
450 // The index of this symbol in the dynamic symbol table. If the
451 // symbol is not going into the dynamic symbol table, this value is
452 // -1U. This field starts as always holding zero. It is set to a
453 // non-zero value during Layout::finalize.
454 unsigned int dynsym_index_;
456 // If this symbol has an entry in the GOT section (has_got_offset_
457 // is true), this is the offset from the start of the GOT section.
458 unsigned int got_offset_;
460 // If this symbol has an entry in the PLT section (has_plt_offset_
461 // is true), then this is the offset from the start of the PLT
462 // section.
463 unsigned int plt_offset_;
465 // Symbol type.
466 elfcpp::STT type_ : 4;
467 // Symbol binding.
468 elfcpp::STB binding_ : 4;
469 // Symbol visibility.
470 elfcpp::STV visibility_ : 2;
471 // Rest of symbol st_other field.
472 unsigned int nonvis_ : 6;
473 // The type of symbol.
474 Source source_ : 3;
475 // True if this symbol always requires special target-specific
476 // handling.
477 bool is_target_special_ : 1;
478 // True if this is the default version of the symbol.
479 bool is_def_ : 1;
480 // True if this symbol really forwards to another symbol. This is
481 // used when we discover after the fact that two different entries
482 // in the hash table really refer to the same symbol. This will
483 // never be set for a symbol found in the hash table, but may be set
484 // for a symbol found in the list of symbols attached to an Object.
485 // It forwards to the symbol found in the forwarders_ map of
486 // Symbol_table.
487 bool is_forwarder_ : 1;
488 // True if this symbol needs to be in the dynamic symbol table.
489 bool needs_dynsym_entry_ : 1;
490 // True if we've seen this symbol in a regular object.
491 bool in_reg_ : 1;
492 // True if we've seen this symbol in a dynamic object.
493 bool in_dyn_ : 1;
494 // True if the symbol has an entry in the GOT section.
495 bool has_got_offset_ : 1;
496 // True if the symbol has an entry in the PLT section.
497 bool has_plt_offset_ : 1;
498 // True if there is a warning for this symbol.
499 bool has_warning_ : 1;
502 // The parts of a symbol which are size specific. Using a template
503 // derived class like this helps us use less space on a 32-bit system.
505 template<int size>
506 class Sized_symbol : public Symbol
508 public:
509 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
510 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
512 Sized_symbol()
515 // Initialize fields from an ELF symbol in OBJECT.
516 template<bool big_endian>
517 void
518 init(const char *name, const char* version, Object* object,
519 const elfcpp::Sym<size, big_endian>&);
521 // Initialize fields for an Output_data.
522 void
523 init(const char* name, Output_data*, Value_type value, Size_type symsize,
524 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
525 bool offset_is_from_end);
527 // Initialize fields for an Output_segment.
528 void
529 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
530 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
531 Segment_offset_base offset_base);
533 // Initialize fields for a constant.
534 void
535 init(const char* name, Value_type value, Size_type symsize,
536 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
538 // Override existing symbol.
539 template<bool big_endian>
540 void
541 override(const elfcpp::Sym<size, big_endian>&, Object* object,
542 const char* version);
544 // Return the symbol's value.
545 Value_type
546 value() const
547 { return this->value_; }
549 // Return the symbol's size (we can't call this 'size' because that
550 // is a template parameter).
551 Size_type
552 symsize() const
553 { return this->symsize_; }
555 // Set the symbol size. This is used when resolving common symbols.
556 void
557 set_symsize(Size_type symsize)
558 { this->symsize_ = symsize; }
560 // Set the symbol value. This is called when we store the final
561 // values of the symbols into the symbol table.
562 void
563 set_value(Value_type value)
564 { this->value_ = value; }
566 private:
567 Sized_symbol(const Sized_symbol&);
568 Sized_symbol& operator=(const Sized_symbol&);
570 // Symbol value. Before Layout::finalize this is the offset in the
571 // input section. This is set to the final value during
572 // Layout::finalize.
573 Value_type value_;
574 // Symbol size.
575 Size_type symsize_;
578 // A struct describing a symbol defined by the linker, where the value
579 // of the symbol is defined based on an output section. This is used
580 // for symbols defined by the linker, like "_init_array_start".
582 struct Define_symbol_in_section
584 // The symbol name.
585 const char* name;
586 // The name of the output section with which this symbol should be
587 // associated. If there is no output section with that name, the
588 // symbol will be defined as zero.
589 const char* output_section;
590 // The offset of the symbol within the output section. This is an
591 // offset from the start of the output section, unless start_at_end
592 // is true, in which case this is an offset from the end of the
593 // output section.
594 uint64_t value;
595 // The size of the symbol.
596 uint64_t size;
597 // The symbol type.
598 elfcpp::STT type;
599 // The symbol binding.
600 elfcpp::STB binding;
601 // The symbol visibility.
602 elfcpp::STV visibility;
603 // The rest of the st_other field.
604 unsigned char nonvis;
605 // If true, the value field is an offset from the end of the output
606 // section.
607 bool offset_is_from_end;
608 // If true, this symbol is defined only if we see a reference to it.
609 bool only_if_ref;
612 // A struct describing a symbol defined by the linker, where the value
613 // of the symbol is defined based on a segment. This is used for
614 // symbols defined by the linker, like "_end". We describe the
615 // segment with which the symbol should be associated by its
616 // characteristics. If no segment meets these characteristics, the
617 // symbol will be defined as zero. If there is more than one segment
618 // which meets these characteristics, we will use the first one.
620 struct Define_symbol_in_segment
622 // The symbol name.
623 const char* name;
624 // The segment type where the symbol should be defined, typically
625 // PT_LOAD.
626 elfcpp::PT segment_type;
627 // Bitmask of segment flags which must be set.
628 elfcpp::PF segment_flags_set;
629 // Bitmask of segment flags which must be clear.
630 elfcpp::PF segment_flags_clear;
631 // The offset of the symbol within the segment. The offset is
632 // calculated from the position set by offset_base.
633 uint64_t value;
634 // The size of the symbol.
635 uint64_t size;
636 // The symbol type.
637 elfcpp::STT type;
638 // The symbol binding.
639 elfcpp::STB binding;
640 // The symbol visibility.
641 elfcpp::STV visibility;
642 // The rest of the st_other field.
643 unsigned char nonvis;
644 // The base from which we compute the offset.
645 Symbol::Segment_offset_base offset_base;
646 // If true, this symbol is defined only if we see a reference to it.
647 bool only_if_ref;
650 // This class manages warnings. Warnings are a GNU extension. When
651 // we see a section named .gnu.warning.SYM in an object file, and if
652 // we wind using the definition of SYM from that object file, then we
653 // will issue a warning for any relocation against SYM from a
654 // different object file. The text of the warning is the contents of
655 // the section. This is not precisely the definition used by the old
656 // GNU linker; the old GNU linker treated an occurrence of
657 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
658 // would trigger a warning on any reference. However, it was
659 // inconsistent in that a warning in a dynamic object only triggered
660 // if there was no definition in a regular object. This linker is
661 // different in that we only issue a warning if we use the symbol
662 // definition from the same object file as the warning section.
664 class Warnings
666 public:
667 Warnings()
668 : warnings_()
671 // Add a warning for symbol NAME in section SHNDX in object OBJ.
672 void
673 add_warning(Symbol_table* symtab, const char* name, Object* obj,
674 unsigned int shndx);
676 // For each symbol for which we should give a warning, make a note
677 // on the symbol.
678 void
679 note_warnings(Symbol_table* symtab);
681 // Issue a warning for a reference to SYM at LOCATION.
682 void
683 issue_warning(const Symbol* sym, const std::string& location) const;
685 private:
686 Warnings(const Warnings&);
687 Warnings& operator=(const Warnings&);
689 // What we need to know to get the warning text.
690 struct Warning_location
692 // The object the warning is in.
693 Object* object;
694 // The index of the warning section.
695 unsigned int shndx;
696 // The warning text if we have already loaded it.
697 std::string text;
699 Warning_location()
700 : object(NULL), shndx(0), text()
703 void
704 set(Object* o, unsigned int s)
706 this->object = o;
707 this->shndx = s;
710 void
711 set_text(const char* t, off_t l)
712 { this->text.assign(t, l); }
715 // A mapping from warning symbol names (canonicalized in
716 // Symbol_table's namepool_ field) to
717 typedef Unordered_map<const char*, Warning_location> Warning_table;
719 Warning_table warnings_;
722 // The main linker symbol table.
724 class Symbol_table
726 public:
727 Symbol_table();
729 ~Symbol_table();
731 // Add COUNT external symbols from the relocatable object RELOBJ to
732 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
733 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
734 // point to the symbols in the symbol table.
735 template<int size, bool big_endian>
736 void
737 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
738 const unsigned char* syms, size_t count,
739 const char* sym_names, size_t sym_name_size,
740 Symbol** sympointers);
742 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
743 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
744 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
745 // symbol version data.
746 template<int size, bool big_endian>
747 void
748 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
749 const unsigned char* syms, size_t count,
750 const char* sym_names, size_t sym_name_size,
751 const unsigned char* versym, size_t versym_size,
752 const std::vector<const char*>*);
754 // Define a special symbol based on an Output_data. It is a
755 // multiple definition error if this symbol is already defined.
756 Symbol*
757 define_in_output_data(const Target*, const char* name, const char* version,
758 Output_data*, uint64_t value, uint64_t symsize,
759 elfcpp::STT type, elfcpp::STB binding,
760 elfcpp::STV visibility, unsigned char nonvis,
761 bool offset_is_from_end, bool only_if_ref);
763 // Define a special symbol based on an Output_segment. It is a
764 // multiple definition error if this symbol is already defined.
765 Symbol*
766 define_in_output_segment(const Target*, const char* name,
767 const char* version, Output_segment*,
768 uint64_t value, uint64_t symsize,
769 elfcpp::STT type, elfcpp::STB binding,
770 elfcpp::STV visibility, unsigned char nonvis,
771 Symbol::Segment_offset_base, bool only_if_ref);
773 // Define a special symbol with a constant value. It is a multiple
774 // definition error if this symbol is already defined.
775 Symbol*
776 define_as_constant(const Target*, const char* name, const char* version,
777 uint64_t value, uint64_t symsize, elfcpp::STT type,
778 elfcpp::STB binding, elfcpp::STV visibility,
779 unsigned char nonvis, bool only_if_ref);
781 // Define a set of symbols in output sections.
782 void
783 define_symbols(const Layout*, const Target*, int count,
784 const Define_symbol_in_section*);
786 // Define a set of symbols in output segments.
787 void
788 define_symbols(const Layout*, const Target*, int count,
789 const Define_symbol_in_segment*);
791 // Look up a symbol.
792 Symbol*
793 lookup(const char*, const char* version = NULL) const;
795 // Return the real symbol associated with the forwarder symbol FROM.
796 Symbol*
797 resolve_forwards(const Symbol* from) const;
799 // Return the bitsize (32 or 64) of the symbols in the table.
801 get_size() const
802 { return this->size_; }
804 // Return the sized version of a symbol in this table.
805 template<int size>
806 Sized_symbol<size>*
807 get_sized_symbol(Symbol* ACCEPT_SIZE) const;
809 template<int size>
810 const Sized_symbol<size>*
811 get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
813 // Return the count of undefined symbols seen.
815 saw_undefined() const
816 { return this->saw_undefined_; }
818 // Allocate the common symbols
819 void
820 allocate_commons(const General_options&, Layout*);
822 // Add a warning for symbol NAME in section SHNDX in object OBJ.
823 void
824 add_warning(const char* name, Object* obj, unsigned int shndx)
825 { this->warnings_.add_warning(this, name, obj, shndx); }
827 // Canonicalize a symbol name for use in the hash table.
828 const char*
829 canonicalize_name(const char* name)
830 { return this->namepool_.add(name, NULL); }
832 // Possibly issue a warning for a reference to SYM at LOCATION which
833 // is in OBJ.
834 void
835 issue_warning(const Symbol* sym, const std::string& location) const
836 { this->warnings_.issue_warning(sym, location); }
838 // Set the dynamic symbol indexes. INDEX is the index of the first
839 // global dynamic symbol. Pointers to the symbols are stored into
840 // the vector. The names are stored into the Stringpool. This
841 // returns an updated dynamic symbol index.
842 unsigned int
843 set_dynsym_indexes(const General_options*, const Target*, unsigned int index,
844 std::vector<Symbol*>*, Stringpool*, Versions*);
846 // Finalize the symbol table after we have set the final addresses
847 // of all the input sections. This sets the final symbol indexes,
848 // values and adds the names to *POOL. INDEX is the index of the
849 // first global symbol. OFF is the file offset of the global symbol
850 // table, DYNOFF is the offset of the globals in the dynamic symbol
851 // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
852 // symbol, and DYNCOUNT is the number of global dynamic symbols.
853 // This records the parameters, and returns the new file offset.
854 off_t
855 finalize(unsigned int index, off_t off, off_t dynoff,
856 size_t dyn_global_index, size_t dyncount, Stringpool* pool);
858 // Write out the global symbols.
859 void
860 write_globals(const Target*, const Stringpool*, const Stringpool*,
861 Output_file*) const;
863 // Write out a section symbol. Return the updated offset.
864 void
865 write_section_symbol(const Target*, const Output_section*, Output_file*,
866 off_t) const;
868 private:
869 Symbol_table(const Symbol_table&);
870 Symbol_table& operator=(const Symbol_table&);
872 // Set the size (32 or 64) of the symbols in the table.
873 void
874 set_size(int size)
875 { this->size_ = size; }
877 // Make FROM a forwarder symbol to TO.
878 void
879 make_forwarder(Symbol* from, Symbol* to);
881 // Add a symbol.
882 template<int size, bool big_endian>
883 Symbol*
884 add_from_object(Object*, const char *name, Stringpool::Key name_key,
885 const char *version, Stringpool::Key version_key,
886 bool def, const elfcpp::Sym<size, big_endian>& sym);
888 // Resolve symbols.
889 template<int size, bool big_endian>
890 static void
891 resolve(Sized_symbol<size>* to,
892 const elfcpp::Sym<size, big_endian>& sym,
893 Object*, const char* version);
895 template<int size, bool big_endian>
896 static void
897 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
898 const char* version ACCEPT_SIZE_ENDIAN);
900 // Define a special symbol.
901 template<int size, bool big_endian>
902 Sized_symbol<size>*
903 define_special_symbol(const Target* target, const char* name,
904 const char* version, bool only_if_ref
905 ACCEPT_SIZE_ENDIAN);
907 // Define a symbol in an Output_data, sized version.
908 template<int size>
909 Sized_symbol<size>*
910 do_define_in_output_data(const Target*, const char* name,
911 const char* version, Output_data*,
912 typename elfcpp::Elf_types<size>::Elf_Addr value,
913 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
914 elfcpp::STT type, elfcpp::STB binding,
915 elfcpp::STV visibility, unsigned char nonvis,
916 bool offset_is_from_end, bool only_if_ref);
918 // Define a symbol in an Output_segment, sized version.
919 template<int size>
920 Sized_symbol<size>*
921 do_define_in_output_segment(
922 const Target*, const char* name, const char* version, Output_segment* os,
923 typename elfcpp::Elf_types<size>::Elf_Addr value,
924 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
925 elfcpp::STT type, elfcpp::STB binding,
926 elfcpp::STV visibility, unsigned char nonvis,
927 Symbol::Segment_offset_base offset_base, bool only_if_ref);
929 // Define a symbol as a constant, sized version.
930 template<int size>
931 Sized_symbol<size>*
932 do_define_as_constant(
933 const Target*, const char* name, const char* version,
934 typename elfcpp::Elf_types<size>::Elf_Addr value,
935 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
936 elfcpp::STT type, elfcpp::STB binding,
937 elfcpp::STV visibility, unsigned char nonvis,
938 bool only_if_ref);
940 // Allocate the common symbols, sized version.
941 template<int size>
942 void
943 do_allocate_commons(const General_options&, Layout*);
945 // Finalize symbols specialized for size.
946 template<int size>
947 off_t
948 sized_finalize(unsigned int, off_t, Stringpool*);
950 // Write globals specialized for size and endianness.
951 template<int size, bool big_endian>
952 void
953 sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
954 Output_file*) const;
956 // Write out a symbol to P.
957 template<int size, bool big_endian>
958 void
959 sized_write_symbol(Sized_symbol<size>*, unsigned int shndx,
960 const Stringpool*, unsigned char* p
961 ACCEPT_SIZE_ENDIAN) const;
963 // Write out a section symbol, specialized for size and endianness.
964 template<int size, bool big_endian>
965 void
966 sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
968 // The type of the symbol hash table.
970 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
972 struct Symbol_table_hash
974 size_t
975 operator()(const Symbol_table_key&) const;
978 struct Symbol_table_eq
980 bool
981 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
984 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
985 Symbol_table_eq> Symbol_table_type;
987 // The type of the list of common symbols.
989 typedef std::vector<Symbol*> Commons_type;
991 // The size of the symbols in the symbol table (32 or 64).
992 int size_;
994 // We increment this every time we see a new undefined symbol, for
995 // use in archive groups.
996 int saw_undefined_;
998 // The index of the first global symbol in the output file.
999 unsigned int first_global_index_;
1001 // The file offset within the output symtab section where we should
1002 // write the table.
1003 off_t offset_;
1005 // The number of global symbols we want to write out.
1006 size_t output_count_;
1008 // The file offset of the global dynamic symbols, or 0 if none.
1009 off_t dynamic_offset_;
1011 // The index of the first global dynamic symbol.
1012 unsigned int first_dynamic_global_index_;
1014 // The number of global dynamic symbols, or 0 if none.
1015 off_t dynamic_count_;
1017 // The symbol hash table.
1018 Symbol_table_type table_;
1020 // A pool of symbol names. This is used for all global symbols.
1021 // Entries in the hash table point into this pool.
1022 Stringpool namepool_;
1024 // Forwarding symbols.
1025 Unordered_map<const Symbol*, Symbol*> forwarders_;
1027 // We don't expect there to be very many common symbols, so we keep
1028 // a list of them. When we find a common symbol we add it to this
1029 // list. It is possible that by the time we process the list the
1030 // symbol is no longer a common symbol. It may also have become a
1031 // forwarder.
1032 Commons_type commons_;
1034 // Manage symbol warnings.
1035 Warnings warnings_;
1038 // We inline get_sized_symbol for efficiency.
1040 template<int size>
1041 Sized_symbol<size>*
1042 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1044 gold_assert(size == this->get_size());
1045 return static_cast<Sized_symbol<size>*>(sym);
1048 template<int size>
1049 const Sized_symbol<size>*
1050 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1052 gold_assert(size == this->get_size());
1053 return static_cast<const Sized_symbol<size>*>(sym);
1056 } // End namespace gold.
1058 #endif // !defined(GOLD_SYMTAB_H)