* readelf.c (do_archive_index): New.
[binutils.git] / gold / symtab.h
blob816afb3884a8380053882a8c53fbed1124cff834
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
182 return (this->needs_dynsym_entry_
183 || (this->in_reg() && this->in_dyn()));
186 // Mark this symbol as needing an entry in the dynamic symbol table.
187 void
188 set_needs_dynsym_entry()
189 { this->needs_dynsym_entry_ = true; }
191 // Return whether this symbol has been seen in a regular object.
192 bool
193 in_reg() const
194 { return this->in_reg_; }
196 // Mark this symbol as having been seen in a regular object.
197 void
198 set_in_reg()
199 { this->in_reg_ = true; }
201 // Return whether this symbol has been seen in a dynamic object.
202 bool
203 in_dyn() const
204 { return this->in_dyn_; }
206 // Mark this symbol as having been seen in a dynamic object.
207 void
208 set_in_dyn()
209 { this->in_dyn_ = true; }
211 // Return the index of this symbol in the output file symbol table.
212 // A value of -1U means that this symbol is not going into the
213 // output file. This starts out as zero, and is set to a non-zero
214 // value by Symbol_table::finalize. It is an error to ask for the
215 // symbol table index before it has been set.
216 unsigned int
217 symtab_index() const
219 gold_assert(this->symtab_index_ != 0);
220 return this->symtab_index_;
223 // Set the index of the symbol in the output file symbol table.
224 void
225 set_symtab_index(unsigned int index)
227 gold_assert(index != 0);
228 this->symtab_index_ = index;
231 // Return whether this symbol already has an index in the output
232 // file symbol table.
233 bool
234 has_symtab_index() const
235 { return this->symtab_index_ != 0; }
237 // Return the index of this symbol in the dynamic symbol table. A
238 // value of -1U means that this symbol is not going into the dynamic
239 // symbol table. This starts out as zero, and is set to a non-zero
240 // during Layout::finalize. It is an error to ask for the dynamic
241 // symbol table index before it has been set.
242 unsigned int
243 dynsym_index() const
245 gold_assert(this->dynsym_index_ != 0);
246 return this->dynsym_index_;
249 // Set the index of the symbol in the dynamic symbol table.
250 void
251 set_dynsym_index(unsigned int index)
253 gold_assert(index != 0);
254 this->dynsym_index_ = index;
257 // Return whether this symbol already has an index in the dynamic
258 // symbol table.
259 bool
260 has_dynsym_index() const
261 { return this->dynsym_index_ != 0; }
263 // Return whether this symbol has an entry in the GOT section.
264 bool
265 has_got_offset() const
266 { return this->has_got_offset_; }
268 // Return the offset into the GOT section of this symbol.
269 unsigned int
270 got_offset() const
272 gold_assert(this->has_got_offset());
273 return this->got_offset_;
276 // Set the GOT offset of this symbol.
277 void
278 set_got_offset(unsigned int got_offset)
280 this->has_got_offset_ = true;
281 this->got_offset_ = got_offset;
284 // Return whether this symbol has an entry in the PLT section.
285 bool
286 has_plt_offset() const
287 { return this->has_plt_offset_; }
289 // Return the offset into the PLT section of this symbol.
290 unsigned int
291 plt_offset() const
293 gold_assert(this->has_plt_offset());
294 return this->plt_offset_;
297 // Set the PLT offset of this symbol.
298 void
299 set_plt_offset(unsigned int plt_offset)
301 this->has_plt_offset_ = true;
302 this->plt_offset_ = plt_offset;
305 // Return true if the final value of this symbol is known at link
306 // time.
307 bool
308 final_value_is_known(const General_options* options) const
310 if (options->is_shared())
311 return false;
312 return this->source_ != FROM_OBJECT || !this->object()->is_dynamic();
315 // Return whether this is a defined symbol (not undefined or
316 // common).
317 bool
318 is_defined() const
320 return (this->source_ != FROM_OBJECT
321 || (this->shndx() != elfcpp::SHN_UNDEF
322 && this->shndx() != elfcpp::SHN_COMMON));
325 // Return true if this symbol is from a dynamic object.
326 bool
327 is_from_dynobj() const
329 return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
332 // Return whether this is an undefined symbol.
333 bool
334 is_undefined() const
336 return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
339 // Return whether this is a common symbol.
340 bool
341 is_common() const
343 return (this->source_ == FROM_OBJECT
344 && (this->shndx() == elfcpp::SHN_COMMON
345 || this->type_ == elfcpp::STT_COMMON));
348 // Return whether this symbol can be seen outside this object.
349 bool
350 is_externally_visible() const
352 return (this->visibility_ == elfcpp::STV_DEFAULT
353 || this->visibility_ == elfcpp::STV_PROTECTED);
356 // Return whether there should be a warning for references to this
357 // symbol.
358 bool
359 has_warning() const
360 { return this->has_warning_; }
362 // Mark this symbol as having a warning.
363 void
364 set_has_warning()
365 { this->has_warning_ = true; }
367 protected:
368 // Instances of this class should always be created at a specific
369 // size.
370 Symbol()
371 { memset(this, 0, sizeof *this); }
373 // Initialize the general fields.
374 void
375 init_fields(const char* name, const char* version,
376 elfcpp::STT type, elfcpp::STB binding,
377 elfcpp::STV visibility, unsigned char nonvis);
379 // Initialize fields from an ELF symbol in OBJECT.
380 template<int size, bool big_endian>
381 void
382 init_base(const char *name, const char* version, Object* object,
383 const elfcpp::Sym<size, big_endian>&);
385 // Initialize fields for an Output_data.
386 void
387 init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
388 elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
390 // Initialize fields for an Output_segment.
391 void
392 init_base(const char* name, Output_segment* os, elfcpp::STT type,
393 elfcpp::STB binding, elfcpp::STV visibility,
394 unsigned char nonvis, Segment_offset_base offset_base);
396 // Initialize fields for a constant.
397 void
398 init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
399 elfcpp::STV visibility, unsigned char nonvis);
401 // Override existing symbol.
402 template<int size, bool big_endian>
403 void
404 override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
405 const char* version);
407 private:
408 Symbol(const Symbol&);
409 Symbol& operator=(const Symbol&);
411 // Symbol name (expected to point into a Stringpool).
412 const char* name_;
413 // Symbol version (expected to point into a Stringpool). This may
414 // be NULL.
415 const char* version_;
417 union
419 // This struct is used if SOURCE_ == FROM_OBJECT.
420 struct
422 // Object in which symbol is defined, or in which it was first
423 // seen.
424 Object* object;
425 // Section number in object_ in which symbol is defined.
426 unsigned int shndx;
427 } from_object;
429 // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
430 struct
432 // Output_data in which symbol is defined. Before
433 // Layout::finalize the symbol's value is an offset within the
434 // Output_data.
435 Output_data* output_data;
436 // True if the offset is from the end, false if the offset is
437 // from the beginning.
438 bool offset_is_from_end;
439 } in_output_data;
441 // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
442 struct
444 // Output_segment in which the symbol is defined. Before
445 // Layout::finalize the symbol's value is an offset.
446 Output_segment* output_segment;
447 // The base to use for the offset before Layout::finalize.
448 Segment_offset_base offset_base;
449 } in_output_segment;
450 } u_;
452 // The index of this symbol in the output file. If the symbol is
453 // not going into the output file, this value is -1U. This field
454 // starts as always holding zero. It is set to a non-zero value by
455 // Symbol_table::finalize.
456 unsigned int symtab_index_;
458 // The index of this symbol in the dynamic symbol table. If the
459 // symbol is not going into the dynamic symbol table, this value is
460 // -1U. This field starts as always holding zero. It is set to a
461 // non-zero value during Layout::finalize.
462 unsigned int dynsym_index_;
464 // If this symbol has an entry in the GOT section (has_got_offset_
465 // is true), this is the offset from the start of the GOT section.
466 unsigned int got_offset_;
468 // If this symbol has an entry in the PLT section (has_plt_offset_
469 // is true), then this is the offset from the start of the PLT
470 // section.
471 unsigned int plt_offset_;
473 // Symbol type.
474 elfcpp::STT type_ : 4;
475 // Symbol binding.
476 elfcpp::STB binding_ : 4;
477 // Symbol visibility.
478 elfcpp::STV visibility_ : 2;
479 // Rest of symbol st_other field.
480 unsigned int nonvis_ : 6;
481 // The type of symbol.
482 Source source_ : 3;
483 // True if this symbol always requires special target-specific
484 // handling.
485 bool is_target_special_ : 1;
486 // True if this is the default version of the symbol.
487 bool is_def_ : 1;
488 // True if this symbol really forwards to another symbol. This is
489 // used when we discover after the fact that two different entries
490 // in the hash table really refer to the same symbol. This will
491 // never be set for a symbol found in the hash table, but may be set
492 // for a symbol found in the list of symbols attached to an Object.
493 // It forwards to the symbol found in the forwarders_ map of
494 // Symbol_table.
495 bool is_forwarder_ : 1;
496 // True if this symbol needs to be in the dynamic symbol table.
497 bool needs_dynsym_entry_ : 1;
498 // True if we've seen this symbol in a regular object.
499 bool in_reg_ : 1;
500 // True if we've seen this symbol in a dynamic object.
501 bool in_dyn_ : 1;
502 // True if the symbol has an entry in the GOT section.
503 bool has_got_offset_ : 1;
504 // True if the symbol has an entry in the PLT section.
505 bool has_plt_offset_ : 1;
506 // True if there is a warning for this symbol.
507 bool has_warning_ : 1;
510 // The parts of a symbol which are size specific. Using a template
511 // derived class like this helps us use less space on a 32-bit system.
513 template<int size>
514 class Sized_symbol : public Symbol
516 public:
517 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
518 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
520 Sized_symbol()
523 // Initialize fields from an ELF symbol in OBJECT.
524 template<bool big_endian>
525 void
526 init(const char *name, const char* version, Object* object,
527 const elfcpp::Sym<size, big_endian>&);
529 // Initialize fields for an Output_data.
530 void
531 init(const char* name, Output_data*, Value_type value, Size_type symsize,
532 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
533 bool offset_is_from_end);
535 // Initialize fields for an Output_segment.
536 void
537 init(const char* name, Output_segment*, Value_type value, Size_type symsize,
538 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
539 Segment_offset_base offset_base);
541 // Initialize fields for a constant.
542 void
543 init(const char* name, Value_type value, Size_type symsize,
544 elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
546 // Override existing symbol.
547 template<bool big_endian>
548 void
549 override(const elfcpp::Sym<size, big_endian>&, Object* object,
550 const char* version);
552 // Return the symbol's value.
553 Value_type
554 value() const
555 { return this->value_; }
557 // Return the symbol's size (we can't call this 'size' because that
558 // is a template parameter).
559 Size_type
560 symsize() const
561 { return this->symsize_; }
563 // Set the symbol size. This is used when resolving common symbols.
564 void
565 set_symsize(Size_type symsize)
566 { this->symsize_ = symsize; }
568 // Set the symbol value. This is called when we store the final
569 // values of the symbols into the symbol table.
570 void
571 set_value(Value_type value)
572 { this->value_ = value; }
574 private:
575 Sized_symbol(const Sized_symbol&);
576 Sized_symbol& operator=(const Sized_symbol&);
578 // Symbol value. Before Layout::finalize this is the offset in the
579 // input section. This is set to the final value during
580 // Layout::finalize.
581 Value_type value_;
582 // Symbol size.
583 Size_type symsize_;
586 // A struct describing a symbol defined by the linker, where the value
587 // of the symbol is defined based on an output section. This is used
588 // for symbols defined by the linker, like "_init_array_start".
590 struct Define_symbol_in_section
592 // The symbol name.
593 const char* name;
594 // The name of the output section with which this symbol should be
595 // associated. If there is no output section with that name, the
596 // symbol will be defined as zero.
597 const char* output_section;
598 // The offset of the symbol within the output section. This is an
599 // offset from the start of the output section, unless start_at_end
600 // is true, in which case this is an offset from the end of the
601 // output section.
602 uint64_t value;
603 // The size of the symbol.
604 uint64_t size;
605 // The symbol type.
606 elfcpp::STT type;
607 // The symbol binding.
608 elfcpp::STB binding;
609 // The symbol visibility.
610 elfcpp::STV visibility;
611 // The rest of the st_other field.
612 unsigned char nonvis;
613 // If true, the value field is an offset from the end of the output
614 // section.
615 bool offset_is_from_end;
616 // If true, this symbol is defined only if we see a reference to it.
617 bool only_if_ref;
620 // A struct describing a symbol defined by the linker, where the value
621 // of the symbol is defined based on a segment. This is used for
622 // symbols defined by the linker, like "_end". We describe the
623 // segment with which the symbol should be associated by its
624 // characteristics. If no segment meets these characteristics, the
625 // symbol will be defined as zero. If there is more than one segment
626 // which meets these characteristics, we will use the first one.
628 struct Define_symbol_in_segment
630 // The symbol name.
631 const char* name;
632 // The segment type where the symbol should be defined, typically
633 // PT_LOAD.
634 elfcpp::PT segment_type;
635 // Bitmask of segment flags which must be set.
636 elfcpp::PF segment_flags_set;
637 // Bitmask of segment flags which must be clear.
638 elfcpp::PF segment_flags_clear;
639 // The offset of the symbol within the segment. The offset is
640 // calculated from the position set by offset_base.
641 uint64_t value;
642 // The size of the symbol.
643 uint64_t size;
644 // The symbol type.
645 elfcpp::STT type;
646 // The symbol binding.
647 elfcpp::STB binding;
648 // The symbol visibility.
649 elfcpp::STV visibility;
650 // The rest of the st_other field.
651 unsigned char nonvis;
652 // The base from which we compute the offset.
653 Symbol::Segment_offset_base offset_base;
654 // If true, this symbol is defined only if we see a reference to it.
655 bool only_if_ref;
658 // This class manages warnings. Warnings are a GNU extension. When
659 // we see a section named .gnu.warning.SYM in an object file, and if
660 // we wind using the definition of SYM from that object file, then we
661 // will issue a warning for any relocation against SYM from a
662 // different object file. The text of the warning is the contents of
663 // the section. This is not precisely the definition used by the old
664 // GNU linker; the old GNU linker treated an occurrence of
665 // .gnu.warning.SYM as defining a warning symbol. A warning symbol
666 // would trigger a warning on any reference. However, it was
667 // inconsistent in that a warning in a dynamic object only triggered
668 // if there was no definition in a regular object. This linker is
669 // different in that we only issue a warning if we use the symbol
670 // definition from the same object file as the warning section.
672 class Warnings
674 public:
675 Warnings()
676 : warnings_()
679 // Add a warning for symbol NAME in section SHNDX in object OBJ.
680 void
681 add_warning(Symbol_table* symtab, const char* name, Object* obj,
682 unsigned int shndx);
684 // For each symbol for which we should give a warning, make a note
685 // on the symbol.
686 void
687 note_warnings(Symbol_table* symtab);
689 // Issue a warning for a reference to SYM at LOCATION.
690 void
691 issue_warning(const Symbol* sym, const std::string& location) const;
693 private:
694 Warnings(const Warnings&);
695 Warnings& operator=(const Warnings&);
697 // What we need to know to get the warning text.
698 struct Warning_location
700 // The object the warning is in.
701 Object* object;
702 // The index of the warning section.
703 unsigned int shndx;
704 // The warning text if we have already loaded it.
705 std::string text;
707 Warning_location()
708 : object(NULL), shndx(0), text()
711 void
712 set(Object* o, unsigned int s)
714 this->object = o;
715 this->shndx = s;
718 void
719 set_text(const char* t, off_t l)
720 { this->text.assign(t, l); }
723 // A mapping from warning symbol names (canonicalized in
724 // Symbol_table's namepool_ field) to
725 typedef Unordered_map<const char*, Warning_location> Warning_table;
727 Warning_table warnings_;
730 // The main linker symbol table.
732 class Symbol_table
734 public:
735 Symbol_table();
737 ~Symbol_table();
739 // Add COUNT external symbols from the relocatable object RELOBJ to
740 // the symbol table. SYMS is the symbols, SYM_NAMES is their names,
741 // SYM_NAME_SIZE is the size of SYM_NAMES. This sets SYMPOINTERS to
742 // point to the symbols in the symbol table.
743 template<int size, bool big_endian>
744 void
745 add_from_relobj(Sized_relobj<size, big_endian>* relobj,
746 const unsigned char* syms, size_t count,
747 const char* sym_names, size_t sym_name_size,
748 Symbol** sympointers);
750 // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
751 // symbol table. SYMS is the symbols. SYM_NAMES is their names.
752 // SYM_NAME_SIZE is the size of SYM_NAMES. The other parameters are
753 // symbol version data.
754 template<int size, bool big_endian>
755 void
756 add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
757 const unsigned char* syms, size_t count,
758 const char* sym_names, size_t sym_name_size,
759 const unsigned char* versym, size_t versym_size,
760 const std::vector<const char*>*);
762 // Define a special symbol based on an Output_data. It is a
763 // multiple definition error if this symbol is already defined.
764 Symbol*
765 define_in_output_data(const Target*, const char* name, const char* version,
766 Output_data*, uint64_t value, uint64_t symsize,
767 elfcpp::STT type, elfcpp::STB binding,
768 elfcpp::STV visibility, unsigned char nonvis,
769 bool offset_is_from_end, bool only_if_ref);
771 // Define a special symbol based on an Output_segment. It is a
772 // multiple definition error if this symbol is already defined.
773 Symbol*
774 define_in_output_segment(const Target*, const char* name,
775 const char* version, Output_segment*,
776 uint64_t value, uint64_t symsize,
777 elfcpp::STT type, elfcpp::STB binding,
778 elfcpp::STV visibility, unsigned char nonvis,
779 Symbol::Segment_offset_base, bool only_if_ref);
781 // Define a special symbol with a constant value. It is a multiple
782 // definition error if this symbol is already defined.
783 Symbol*
784 define_as_constant(const Target*, const char* name, const char* version,
785 uint64_t value, uint64_t symsize, elfcpp::STT type,
786 elfcpp::STB binding, elfcpp::STV visibility,
787 unsigned char nonvis, bool only_if_ref);
789 // Define a set of symbols in output sections.
790 void
791 define_symbols(const Layout*, const Target*, int count,
792 const Define_symbol_in_section*);
794 // Define a set of symbols in output segments.
795 void
796 define_symbols(const Layout*, const Target*, int count,
797 const Define_symbol_in_segment*);
799 // Look up a symbol.
800 Symbol*
801 lookup(const char*, const char* version = NULL) const;
803 // Return the real symbol associated with the forwarder symbol FROM.
804 Symbol*
805 resolve_forwards(const Symbol* from) const;
807 // Return the bitsize (32 or 64) of the symbols in the table.
809 get_size() const
810 { return this->size_; }
812 // Return the sized version of a symbol in this table.
813 template<int size>
814 Sized_symbol<size>*
815 get_sized_symbol(Symbol* ACCEPT_SIZE) const;
817 template<int size>
818 const Sized_symbol<size>*
819 get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
821 // Return the count of undefined symbols seen.
823 saw_undefined() const
824 { return this->saw_undefined_; }
826 // Allocate the common symbols
827 void
828 allocate_commons(const General_options&, Layout*);
830 // Add a warning for symbol NAME in section SHNDX in object OBJ.
831 void
832 add_warning(const char* name, Object* obj, unsigned int shndx)
833 { this->warnings_.add_warning(this, name, obj, shndx); }
835 // Canonicalize a symbol name for use in the hash table.
836 const char*
837 canonicalize_name(const char* name)
838 { return this->namepool_.add(name, NULL); }
840 // Possibly issue a warning for a reference to SYM at LOCATION which
841 // is in OBJ.
842 void
843 issue_warning(const Symbol* sym, const std::string& location) const
844 { this->warnings_.issue_warning(sym, location); }
846 // Set the dynamic symbol indexes. INDEX is the index of the first
847 // global dynamic symbol. Pointers to the symbols are stored into
848 // the vector. The names are stored into the Stringpool. This
849 // returns an updated dynamic symbol index.
850 unsigned int
851 set_dynsym_indexes(const General_options*, const Target*, unsigned int index,
852 std::vector<Symbol*>*, Stringpool*, Versions*);
854 // Finalize the symbol table after we have set the final addresses
855 // of all the input sections. This sets the final symbol indexes,
856 // values and adds the names to *POOL. INDEX is the index of the
857 // first global symbol. OFF is the file offset of the global symbol
858 // table, DYNOFF is the offset of the globals in the dynamic symbol
859 // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
860 // symbol, and DYNCOUNT is the number of global dynamic symbols.
861 // This records the parameters, and returns the new file offset.
862 off_t
863 finalize(unsigned int index, off_t off, off_t dynoff,
864 size_t dyn_global_index, size_t dyncount, Stringpool* pool);
866 // Write out the global symbols.
867 void
868 write_globals(const Target*, const Stringpool*, const Stringpool*,
869 Output_file*) const;
871 // Write out a section symbol. Return the updated offset.
872 void
873 write_section_symbol(const Target*, const Output_section*, Output_file*,
874 off_t) const;
876 private:
877 Symbol_table(const Symbol_table&);
878 Symbol_table& operator=(const Symbol_table&);
880 // Set the size (32 or 64) of the symbols in the table.
881 void
882 set_size(int size)
883 { this->size_ = size; }
885 // Make FROM a forwarder symbol to TO.
886 void
887 make_forwarder(Symbol* from, Symbol* to);
889 // Add a symbol.
890 template<int size, bool big_endian>
891 Symbol*
892 add_from_object(Object*, const char *name, Stringpool::Key name_key,
893 const char *version, Stringpool::Key version_key,
894 bool def, const elfcpp::Sym<size, big_endian>& sym);
896 // Resolve symbols.
897 template<int size, bool big_endian>
898 static void
899 resolve(Sized_symbol<size>* to,
900 const elfcpp::Sym<size, big_endian>& sym,
901 Object*, const char* version);
903 template<int size, bool big_endian>
904 static void
905 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
906 const char* version ACCEPT_SIZE_ENDIAN);
908 // Define a special symbol.
909 template<int size, bool big_endian>
910 Sized_symbol<size>*
911 define_special_symbol(const Target* target, const char* name,
912 const char* version, bool only_if_ref
913 ACCEPT_SIZE_ENDIAN);
915 // Define a symbol in an Output_data, sized version.
916 template<int size>
917 Sized_symbol<size>*
918 do_define_in_output_data(const Target*, const char* name,
919 const char* version, Output_data*,
920 typename elfcpp::Elf_types<size>::Elf_Addr value,
921 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
922 elfcpp::STT type, elfcpp::STB binding,
923 elfcpp::STV visibility, unsigned char nonvis,
924 bool offset_is_from_end, bool only_if_ref);
926 // Define a symbol in an Output_segment, sized version.
927 template<int size>
928 Sized_symbol<size>*
929 do_define_in_output_segment(
930 const Target*, const char* name, const char* version, Output_segment* os,
931 typename elfcpp::Elf_types<size>::Elf_Addr value,
932 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
933 elfcpp::STT type, elfcpp::STB binding,
934 elfcpp::STV visibility, unsigned char nonvis,
935 Symbol::Segment_offset_base offset_base, bool only_if_ref);
937 // Define a symbol as a constant, sized version.
938 template<int size>
939 Sized_symbol<size>*
940 do_define_as_constant(
941 const Target*, const char* name, const char* version,
942 typename elfcpp::Elf_types<size>::Elf_Addr value,
943 typename elfcpp::Elf_types<size>::Elf_WXword ssize,
944 elfcpp::STT type, elfcpp::STB binding,
945 elfcpp::STV visibility, unsigned char nonvis,
946 bool only_if_ref);
948 // Allocate the common symbols, sized version.
949 template<int size>
950 void
951 do_allocate_commons(const General_options&, Layout*);
953 // Finalize symbols specialized for size.
954 template<int size>
955 off_t
956 sized_finalize(unsigned int, off_t, Stringpool*);
958 // Write globals specialized for size and endianness.
959 template<int size, bool big_endian>
960 void
961 sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
962 Output_file*) const;
964 // Write out a symbol to P.
965 template<int size, bool big_endian>
966 void
967 sized_write_symbol(Sized_symbol<size>*, unsigned int shndx,
968 const Stringpool*, unsigned char* p
969 ACCEPT_SIZE_ENDIAN) const;
971 // Write out a section symbol, specialized for size and endianness.
972 template<int size, bool big_endian>
973 void
974 sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
976 // The type of the symbol hash table.
978 typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
980 struct Symbol_table_hash
982 size_t
983 operator()(const Symbol_table_key&) const;
986 struct Symbol_table_eq
988 bool
989 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
992 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
993 Symbol_table_eq> Symbol_table_type;
995 // The type of the list of common symbols.
997 typedef std::vector<Symbol*> Commons_type;
999 // The size of the symbols in the symbol table (32 or 64).
1000 int size_;
1002 // We increment this every time we see a new undefined symbol, for
1003 // use in archive groups.
1004 int saw_undefined_;
1006 // The index of the first global symbol in the output file.
1007 unsigned int first_global_index_;
1009 // The file offset within the output symtab section where we should
1010 // write the table.
1011 off_t offset_;
1013 // The number of global symbols we want to write out.
1014 size_t output_count_;
1016 // The file offset of the global dynamic symbols, or 0 if none.
1017 off_t dynamic_offset_;
1019 // The index of the first global dynamic symbol.
1020 unsigned int first_dynamic_global_index_;
1022 // The number of global dynamic symbols, or 0 if none.
1023 off_t dynamic_count_;
1025 // The symbol hash table.
1026 Symbol_table_type table_;
1028 // A pool of symbol names. This is used for all global symbols.
1029 // Entries in the hash table point into this pool.
1030 Stringpool namepool_;
1032 // Forwarding symbols.
1033 Unordered_map<const Symbol*, Symbol*> forwarders_;
1035 // We don't expect there to be very many common symbols, so we keep
1036 // a list of them. When we find a common symbol we add it to this
1037 // list. It is possible that by the time we process the list the
1038 // symbol is no longer a common symbol. It may also have become a
1039 // forwarder.
1040 Commons_type commons_;
1042 // Manage symbol warnings.
1043 Warnings warnings_;
1046 // We inline get_sized_symbol for efficiency.
1048 template<int size>
1049 Sized_symbol<size>*
1050 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1052 gold_assert(size == this->get_size());
1053 return static_cast<Sized_symbol<size>*>(sym);
1056 template<int size>
1057 const Sized_symbol<size>*
1058 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1060 gold_assert(size == this->get_size());
1061 return static_cast<const Sized_symbol<size>*>(sym);
1064 } // End namespace gold.
1066 #endif // !defined(GOLD_SYMTAB_H)