Generate version information.
[external/binutils.git] / gold / symtab.h
1 // symtab.h -- the gold symbol table   -*- C++ -*-
2
3 // Symbol_table
4 //   The symbol table.
5
6 #include <string>
7 #include <utility>
8 #include <vector>
9
10 #include "elfcpp.h"
11 #include "stringpool.h"
12 #include "object.h"
13
14 #ifndef GOLD_SYMTAB_H
15 #define GOLD_SYMTAB_H
16
17 namespace gold
18 {
19
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;
33
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.
38
39 class Symbol
40 {
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
47   {
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
59   };
60
61   // When the source is IN_OUTPUT_SEGMENT, we need to describe what
62   // the offset means.
63   enum Segment_offset_base
64   {
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
72   };
73
74   // Return the symbol name.
75   const char*
76   name() const
77   { return this->name_; }
78
79   // Return the symbol version.  This will return NULL for an
80   // unversioned symbol.
81   const char*
82   version() const
83   { return this->version_; }
84
85   // Return the symbol source.
86   Source
87   source() const
88   { return this->source_; }
89
90   // Return the object with which this symbol is associated.
91   Object*
92   object() const
93   {
94     gold_assert(this->source_ == FROM_OBJECT);
95     return this->u_.from_object.object;
96   }
97
98   // Return the index of the section in the input relocatable or
99   // dynamic object file.
100   unsigned int
101   shndx() const
102   {
103     gold_assert(this->source_ == FROM_OBJECT);
104     return this->u_.from_object.shndx;
105   }
106
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
112   {
113     gold_assert(this->source_ == IN_OUTPUT_DATA);
114     return this->u_.in_output_data.output_data;
115   }
116
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
121   {
122     gold_assert(this->source_ == IN_OUTPUT_DATA);
123     return this->u_.in_output_data.offset_is_from_end;
124   }
125
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
131   {
132     gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
133     return this->u_.in_output_segment.output_segment;
134   }
135
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
140   {
141     gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
142     return this->u_.in_output_segment.offset_base;
143   }
144
145   // Return the symbol binding.
146   elfcpp::STB
147   binding() const
148   { return this->binding_; }
149
150   // Return the symbol type.
151   elfcpp::STT
152   type() const
153   { return this->type_; }
154
155   // Return the symbol visibility.
156   elfcpp::STV
157   visibility() const
158   { return this->visibility_; }
159
160   // Return the non-visibility part of the st_other field.
161   unsigned char
162   nonvis() const
163   { return this->nonvis_; }
164
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_; }
171
172   // Mark this symbol as a forwarder.
173   void
174   set_forwarder()
175   { this->is_forwarder_ = true; }
176
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_; }
182
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; }
187
188   // Mark this symbol as having been seen in a dynamic object.
189   void
190   set_in_dyn()
191   { this->in_dyn_ = true; }
192
193   // Return the index of this symbol in the output file symbol table.
194   // A value of -1U means that this symbol is not going into the
195   // output file.  This starts out as zero, and is set to a non-zero
196   // value by Symbol_table::finalize.  It is an error to ask for the
197   // symbol table index before it has been set.
198   unsigned int
199   symtab_index() const
200   {
201     gold_assert(this->symtab_index_ != 0);
202     return this->symtab_index_;
203   }
204
205   // Set the index of the symbol in the output file symbol table.
206   void
207   set_symtab_index(unsigned int index)
208   {
209     gold_assert(index != 0);
210     this->symtab_index_ = index;
211   }
212
213   // Return whether this symbol already has an index in the output
214   // file symbol table.
215   bool
216   has_symtab_index() const
217   { return this->symtab_index_ != 0; }
218
219   // Return the index of this symbol in the dynamic symbol table.  A
220   // value of -1U means that this symbol is not going into the dynamic
221   // symbol table.  This starts out as zero, and is set to a non-zero
222   // during Layout::finalize.  It is an error to ask for the dynamic
223   // symbol table index before it has been set.
224   unsigned int
225   dynsym_index() const
226   {
227     gold_assert(this->dynsym_index_ != 0);
228     return this->dynsym_index_;
229   }
230
231   // Set the index of the symbol in the dynamic symbol table.
232   void
233   set_dynsym_index(unsigned int index)
234   {
235     gold_assert(index != 0);
236     this->dynsym_index_ = index;
237   }
238
239   // Return whether this symbol already has an index in the dynamic
240   // symbol table.
241   bool
242   has_dynsym_index() const
243   { return this->dynsym_index_ != 0; }
244
245   // Return whether this symbol has an entry in the GOT section.
246   bool
247   has_got_offset() const
248   { return this->has_got_offset_; }
249
250   // Return the offset into the GOT section of this symbol.
251   unsigned int
252   got_offset() const
253   {
254     gold_assert(this->has_got_offset());
255     return this->got_offset_;
256   }
257
258   // Set the GOT offset of this symbol.
259   void
260   set_got_offset(unsigned int got_offset)
261   {
262     this->has_got_offset_ = true;
263     this->got_offset_ = got_offset;
264   }
265
266   // Return whether this symbol has an entry in the PLT section.
267   bool
268   has_plt_offset() const
269   { return this->has_plt_offset_; }
270
271   // Return the offset into the PLT section of this symbol.
272   unsigned int
273   plt_offset() const
274   {
275     gold_assert(this->has_plt_offset());
276     return this->plt_offset_;
277   }
278
279   // Set the PLT offset of this symbol.
280   void
281   set_plt_offset(unsigned int plt_offset)
282   {
283     this->has_plt_offset_ = true;
284     this->plt_offset_ = plt_offset;
285   }
286
287   // Return true if the final value of this symbol is known at link
288   // time.
289   bool
290   final_value_is_known(const General_options* options) const
291   {
292     if (options->is_shared())
293       return false;
294     return this->source_ != FROM_OBJECT || !this->object()->is_dynamic();
295   }
296
297   // Return whether this is a defined symbol (not undefined or
298   // common).
299   bool
300   is_defined() const
301   {
302     return (this->source_ != FROM_OBJECT
303             || (this->shndx() != elfcpp::SHN_UNDEF
304                 && this->shndx() != elfcpp::SHN_COMMON));
305   }
306
307   // Return true if this symbol is from a dynamic object.
308   bool
309   is_from_dynobj() const
310   {
311     return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
312   }
313
314   // Return whether this is an undefined symbol.
315   bool
316   is_undefined() const
317   {
318     return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
319   }
320
321   // Return whether this is a common symbol.
322   bool
323   is_common() const
324   {
325     return (this->source_ == FROM_OBJECT
326             && (this->shndx() == elfcpp::SHN_COMMON
327                 || this->type_ == elfcpp::STT_COMMON));
328   }
329
330   // Return whether there should be a warning for references to this
331   // symbol.
332   bool
333   has_warning() const
334   { return this->has_warning_; }
335
336   // Mark this symbol as having a warning.
337   void
338   set_has_warning()
339   { this->has_warning_ = true; }
340
341  protected:
342   // Instances of this class should always be created at a specific
343   // size.
344   Symbol()
345   { memset(this, 0, sizeof *this); }
346
347   // Initialize the general fields.
348   void
349   init_fields(const char* name, const char* version,
350               elfcpp::STT type, elfcpp::STB binding,
351               elfcpp::STV visibility, unsigned char nonvis);
352
353   // Initialize fields from an ELF symbol in OBJECT.
354   template<int size, bool big_endian>
355   void
356   init_base(const char *name, const char* version, Object* object,
357             const elfcpp::Sym<size, big_endian>&);
358
359   // Initialize fields for an Output_data.
360   void
361   init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
362             elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
363
364   // Initialize fields for an Output_segment.
365   void
366   init_base(const char* name, Output_segment* os, elfcpp::STT type,
367             elfcpp::STB binding, elfcpp::STV visibility,
368             unsigned char nonvis, Segment_offset_base offset_base);
369
370   // Initialize fields for a constant.
371   void
372   init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
373             elfcpp::STV visibility, unsigned char nonvis);
374
375   // Override existing symbol.
376   template<int size, bool big_endian>
377   void
378   override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
379                 const char* version);
380
381  private:
382   Symbol(const Symbol&);
383   Symbol& operator=(const Symbol&);
384
385   // Symbol name (expected to point into a Stringpool).
386   const char* name_;
387   // Symbol version (expected to point into a Stringpool).  This may
388   // be NULL.
389   const char* version_;
390
391   union
392   {
393     // This struct is used if SOURCE_ == FROM_OBJECT.
394     struct
395     {
396       // Object in which symbol is defined, or in which it was first
397       // seen.
398       Object* object;
399       // Section number in object_ in which symbol is defined.
400       unsigned int shndx;
401     } from_object;
402
403     // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
404     struct
405     {
406       // Output_data in which symbol is defined.  Before
407       // Layout::finalize the symbol's value is an offset within the
408       // Output_data.
409       Output_data* output_data;
410       // True if the offset is from the end, false if the offset is
411       // from the beginning.
412       bool offset_is_from_end;
413     } in_output_data;
414
415     // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
416     struct
417     {
418       // Output_segment in which the symbol is defined.  Before
419       // Layout::finalize the symbol's value is an offset.
420       Output_segment* output_segment;
421       // The base to use for the offset before Layout::finalize.
422       Segment_offset_base offset_base;
423     } in_output_segment;
424   } u_;
425
426   // The index of this symbol in the output file.  If the symbol is
427   // not going into the output file, this value is -1U.  This field
428   // starts as always holding zero.  It is set to a non-zero value by
429   // Symbol_table::finalize.
430   unsigned int symtab_index_;
431
432   // The index of this symbol in the dynamic symbol table.  If the
433   // symbol is not going into the dynamic symbol table, this value is
434   // -1U.  This field starts as always holding zero.  It is set to a
435   // non-zero value during Layout::finalize.
436   unsigned int dynsym_index_;
437
438   // If this symbol has an entry in the GOT section (has_got_offset_
439   // is true), this is the offset from the start of the GOT section.
440   unsigned int got_offset_;
441
442   // If this symbol has an entry in the PLT section (has_plt_offset_
443   // is true), then this is the offset from the start of the PLT
444   // section.
445   unsigned int plt_offset_;
446
447   // Symbol type.
448   elfcpp::STT type_ : 4;
449   // Symbol binding.
450   elfcpp::STB binding_ : 4;
451   // Symbol visibility.
452   elfcpp::STV visibility_ : 2;
453   // Rest of symbol st_other field.
454   unsigned int nonvis_ : 6;
455   // The type of symbol.
456   Source source_ : 3;
457   // True if this symbol always requires special target-specific
458   // handling.
459   bool is_target_special_ : 1;
460   // True if this is the default version of the symbol.
461   bool is_def_ : 1;
462   // True if this symbol really forwards to another symbol.  This is
463   // used when we discover after the fact that two different entries
464   // in the hash table really refer to the same symbol.  This will
465   // never be set for a symbol found in the hash table, but may be set
466   // for a symbol found in the list of symbols attached to an Object.
467   // It forwards to the symbol found in the forwarders_ map of
468   // Symbol_table.
469   bool is_forwarder_ : 1;
470   // True if this symbol needs to be in the dynamic symbol table.
471   bool needs_dynsym_entry_ : 1;
472   // True if we've seen this symbol in a dynamic object.
473   bool in_dyn_ : 1;
474   // True if the symbol has an entry in the GOT section.
475   bool has_got_offset_ : 1;
476   // True if the symbol has an entry in the PLT section.
477   bool has_plt_offset_ : 1;
478   // True if there is a warning for this symbol.
479   bool has_warning_ : 1;
480 };
481
482 // The parts of a symbol which are size specific.  Using a template
483 // derived class like this helps us use less space on a 32-bit system.
484
485 template<int size>
486 class Sized_symbol : public Symbol
487 {
488  public:
489   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
490   typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
491
492   Sized_symbol()
493   { }
494
495   // Initialize fields from an ELF symbol in OBJECT.
496   template<bool big_endian>
497   void
498   init(const char *name, const char* version, Object* object,
499        const elfcpp::Sym<size, big_endian>&);
500
501   // Initialize fields for an Output_data.
502   void
503   init(const char* name, Output_data*, Value_type value, Size_type symsize,
504        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
505        bool offset_is_from_end);
506
507   // Initialize fields for an Output_segment.
508   void
509   init(const char* name, Output_segment*, Value_type value, Size_type symsize,
510        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
511        Segment_offset_base offset_base);
512
513   // Initialize fields for a constant.
514   void
515   init(const char* name, Value_type value, Size_type symsize,
516        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
517
518   // Override existing symbol.
519   template<bool big_endian>
520   void
521   override(const elfcpp::Sym<size, big_endian>&, Object* object,
522            const char* version);
523
524   // Return the symbol's value.
525   Value_type
526   value() const
527   { return this->value_; }
528
529   // Return the symbol's size (we can't call this 'size' because that
530   // is a template parameter).
531   Size_type
532   symsize() const
533   { return this->symsize_; }
534
535   // Set the symbol size.  This is used when resolving common symbols.
536   void
537   set_symsize(Size_type symsize)
538   { this->symsize_ = symsize; }
539
540   // Set the symbol value.  This is called when we store the final
541   // values of the symbols into the symbol table.
542   void
543   set_value(Value_type value)
544   { this->value_ = value; }
545
546  private:
547   Sized_symbol(const Sized_symbol&);
548   Sized_symbol& operator=(const Sized_symbol&);
549
550   // Symbol value.  Before Layout::finalize this is the offset in the
551   // input section.  This is set to the final value during
552   // Layout::finalize.
553   Value_type value_;
554   // Symbol size.
555   Size_type symsize_;
556 };
557
558 // A struct describing a symbol defined by the linker, where the value
559 // of the symbol is defined based on an output section.  This is used
560 // for symbols defined by the linker, like "_init_array_start".
561
562 struct Define_symbol_in_section
563 {
564   // The symbol name.
565   const char* name;
566   // The name of the output section with which this symbol should be
567   // associated.  If there is no output section with that name, the
568   // symbol will be defined as zero.
569   const char* output_section;
570   // The offset of the symbol within the output section.  This is an
571   // offset from the start of the output section, unless start_at_end
572   // is true, in which case this is an offset from the end of the
573   // output section.
574   uint64_t value;
575   // The size of the symbol.
576   uint64_t size;
577   // The symbol type.
578   elfcpp::STT type;
579   // The symbol binding.
580   elfcpp::STB binding;
581   // The symbol visibility.
582   elfcpp::STV visibility;
583   // The rest of the st_other field.
584   unsigned char nonvis;
585   // If true, the value field is an offset from the end of the output
586   // section.
587   bool offset_is_from_end;
588   // If true, this symbol is defined only if we see a reference to it.
589   bool only_if_ref;
590 };
591
592 // A struct describing a symbol defined by the linker, where the value
593 // of the symbol is defined based on a segment.  This is used for
594 // symbols defined by the linker, like "_end".  We describe the
595 // segment with which the symbol should be associated by its
596 // characteristics.  If no segment meets these characteristics, the
597 // symbol will be defined as zero.  If there is more than one segment
598 // which meets these characteristics, we will use the first one.
599
600 struct Define_symbol_in_segment
601 {
602   // The symbol name.
603   const char* name;
604   // The segment type where the symbol should be defined, typically
605   // PT_LOAD.
606   elfcpp::PT segment_type;
607   // Bitmask of segment flags which must be set.
608   elfcpp::PF segment_flags_set;
609   // Bitmask of segment flags which must be clear.
610   elfcpp::PF segment_flags_clear;
611   // The offset of the symbol within the segment.  The offset is
612   // calculated from the position set by offset_base.
613   uint64_t value;
614   // The size of the symbol.
615   uint64_t size;
616   // The symbol type.
617   elfcpp::STT type;
618   // The symbol binding.
619   elfcpp::STB binding;
620   // The symbol visibility.
621   elfcpp::STV visibility;
622   // The rest of the st_other field.
623   unsigned char nonvis;
624   // The base from which we compute the offset.
625   Symbol::Segment_offset_base offset_base;
626   // If true, this symbol is defined only if we see a reference to it.
627   bool only_if_ref;
628 };
629
630 // This class manages warnings.  Warnings are a GNU extension.  When
631 // we see a section named .gnu.warning.SYM in an object file, and if
632 // we wind using the definition of SYM from that object file, then we
633 // will issue a warning for any relocation against SYM from a
634 // different object file.  The text of the warning is the contents of
635 // the section.  This is not precisely the definition used by the old
636 // GNU linker; the old GNU linker treated an occurrence of
637 // .gnu.warning.SYM as defining a warning symbol.  A warning symbol
638 // would trigger a warning on any reference.  However, it was
639 // inconsistent in that a warning in a dynamic object only triggered
640 // if there was no definition in a regular object.  This linker is
641 // different in that we only issue a warning if we use the symbol
642 // definition from the same object file as the warning section.
643
644 class Warnings
645 {
646  public:
647   Warnings()
648     : warnings_()
649   { }
650
651   // Add a warning for symbol NAME in section SHNDX in object OBJ.
652   void
653   add_warning(Symbol_table* symtab, const char* name, Object* obj,
654               unsigned int shndx);
655
656   // For each symbol for which we should give a warning, make a note
657   // on the symbol.
658   void
659   note_warnings(Symbol_table* symtab);
660
661   // Issue a warning for a reference to SYM at LOCATION.
662   void
663   issue_warning(const Symbol* sym, const std::string& location) const;
664
665  private:
666   Warnings(const Warnings&);
667   Warnings& operator=(const Warnings&);
668
669   // What we need to know to get the warning text.
670   struct Warning_location
671   {
672     // The object the warning is in.
673     Object* object;
674     // The index of the warning section.
675     unsigned int shndx;
676     // The warning text if we have already loaded it.
677     std::string text;
678
679     Warning_location()
680       : object(NULL), shndx(0), text()
681     { }
682
683     void
684     set(Object* o, unsigned int s)
685     {
686       this->object = o;
687       this->shndx = s;
688     }
689
690     void
691     set_text(const char* t, off_t l)
692     { this->text.assign(t, l); }
693   };
694
695   // A mapping from warning symbol names (canonicalized in
696   // Symbol_table's namepool_ field) to 
697   typedef Unordered_map<const char*, Warning_location> Warning_table;
698
699   Warning_table warnings_;
700 };
701
702 // The main linker symbol table.
703
704 class Symbol_table
705 {
706  public:
707   Symbol_table();
708
709   ~Symbol_table();
710
711   // Add COUNT external symbols from the relocatable object RELOBJ to
712   // the symbol table.  SYMS is the symbols, SYM_NAMES is their names,
713   // SYM_NAME_SIZE is the size of SYM_NAMES.  This sets SYMPOINTERS to
714   // point to the symbols in the symbol table.
715   template<int size, bool big_endian>
716   void
717   add_from_relobj(Sized_relobj<size, big_endian>* relobj,
718                   const unsigned char* syms, size_t count,
719                   const char* sym_names, size_t sym_name_size,
720                   Symbol** sympointers);
721
722   // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
723   // symbol table.  SYMS is the symbols.  SYM_NAMES is their names.
724   // SYM_NAME_SIZE is the size of SYM_NAMES.  The other parameters are
725   // symbol version data.
726   template<int size, bool big_endian>
727   void
728   add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
729                   const unsigned char* syms, size_t count,
730                   const char* sym_names, size_t sym_name_size,
731                   const unsigned char* versym, size_t versym_size,
732                   const std::vector<const char*>*);
733
734   // Define a special symbol based on an Output_data.  It is a
735   // multiple definition error if this symbol is already defined.
736   Symbol*
737   define_in_output_data(const Target*, const char* name, const char* version,
738                         Output_data*, uint64_t value, uint64_t symsize,
739                         elfcpp::STT type, elfcpp::STB binding,
740                         elfcpp::STV visibility, unsigned char nonvis,
741                         bool offset_is_from_end, bool only_if_ref);
742
743   // Define a special symbol based on an Output_segment.  It is a
744   // multiple definition error if this symbol is already defined.
745   Symbol*
746   define_in_output_segment(const Target*, const char* name,
747                            const char* version, Output_segment*,
748                            uint64_t value, uint64_t symsize,
749                            elfcpp::STT type, elfcpp::STB binding,
750                            elfcpp::STV visibility, unsigned char nonvis,
751                            Symbol::Segment_offset_base, bool only_if_ref);
752
753   // Define a special symbol with a constant value.  It is a multiple
754   // definition error if this symbol is already defined.
755   Symbol*
756   define_as_constant(const Target*, const char* name, const char* version,
757                      uint64_t value, uint64_t symsize, elfcpp::STT type,
758                      elfcpp::STB binding, elfcpp::STV visibility,
759                      unsigned char nonvis, bool only_if_ref);
760
761   // Define a set of symbols in output sections.
762   void
763   define_symbols(const Layout*, const Target*, int count,
764                  const Define_symbol_in_section*);
765
766   // Define a set of symbols in output segments.
767   void
768   define_symbols(const Layout*, const Target*, int count,
769                  const Define_symbol_in_segment*);  
770
771   // Look up a symbol.
772   Symbol*
773   lookup(const char*, const char* version = NULL) const;
774
775   // Return the real symbol associated with the forwarder symbol FROM.
776   Symbol*
777   resolve_forwards(const Symbol* from) const;
778
779   // Return the bitsize (32 or 64) of the symbols in the table.
780   int
781   get_size() const
782   { return this->size_; }
783
784   // Return the sized version of a symbol in this table.
785   template<int size>
786   Sized_symbol<size>*
787   get_sized_symbol(Symbol* ACCEPT_SIZE) const;
788
789   template<int size>
790   const Sized_symbol<size>*
791   get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
792
793   // Return the count of undefined symbols seen.
794   int
795   saw_undefined() const
796   { return this->saw_undefined_; }
797
798   // Allocate the common symbols
799   void
800   allocate_commons(const General_options&, Layout*);
801
802   // Add a warning for symbol NAME in section SHNDX in object OBJ.
803   void
804   add_warning(const char* name, Object* obj, unsigned int shndx)
805   { this->warnings_.add_warning(this, name, obj, shndx); }
806
807   // Canonicalize a symbol name for use in the hash table.
808   const char*
809   canonicalize_name(const char* name)
810   { return this->namepool_.add(name, NULL); }
811
812   // Possibly issue a warning for a reference to SYM at LOCATION which
813   // is in OBJ.
814   void
815   issue_warning(const Symbol* sym, const std::string& location) const
816   { this->warnings_.issue_warning(sym, location); }
817
818   // Set the dynamic symbol indexes.  INDEX is the index of the first
819   // global dynamic symbol.  Pointers to the symbols are stored into
820   // the vector.  The names are stored into the Stringpool.  This
821   // returns an updated dynamic symbol index.
822   unsigned int
823   set_dynsym_indexes(const General_options*, const Target*, unsigned int index,
824                      std::vector<Symbol*>*, Stringpool*, Versions*);
825
826   // Finalize the symbol table after we have set the final addresses
827   // of all the input sections.  This sets the final symbol indexes,
828   // values and adds the names to *POOL.  INDEX is the index of the
829   // first global symbol.  OFF is the file offset of the global symbol
830   // table, DYNOFF is the offset of the globals in the dynamic symbol
831   // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
832   // symbol, and DYNCOUNT is the number of global dynamic symbols.
833   // This records the parameters, and returns the new file offset.
834   off_t
835   finalize(unsigned int index, off_t off, off_t dynoff,
836            size_t dyn_global_index, size_t dyncount, Stringpool* pool);
837
838   // Write out the global symbols.
839   void
840   write_globals(const Target*, const Stringpool*, const Stringpool*,
841                 Output_file*) const;
842
843   // Write out a section symbol.  Return the updated offset.
844   void
845   write_section_symbol(const Target*, const Output_section*, Output_file*,
846                        off_t) const;
847
848  private:
849   Symbol_table(const Symbol_table&);
850   Symbol_table& operator=(const Symbol_table&);
851
852   // Set the size (32 or 64) of the symbols in the table.
853   void
854   set_size(int size)
855   { this->size_ = size; }
856
857   // Make FROM a forwarder symbol to TO.
858   void
859   make_forwarder(Symbol* from, Symbol* to);
860
861   // Add a symbol.
862   template<int size, bool big_endian>
863   Symbol*
864   add_from_object(Object*, const char *name, Stringpool::Key name_key,
865                   const char *version, Stringpool::Key version_key,
866                   bool def, const elfcpp::Sym<size, big_endian>& sym);
867
868   // Resolve symbols.
869   template<int size, bool big_endian>
870   static void
871   resolve(Sized_symbol<size>* to,
872           const elfcpp::Sym<size, big_endian>& sym,
873           Object*, const char* version);
874
875   template<int size, bool big_endian>
876   static void
877   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
878           const char* version ACCEPT_SIZE_ENDIAN);
879
880   // Define a special symbol.
881   template<int size, bool big_endian>
882   Sized_symbol<size>*
883   define_special_symbol(const Target* target, const char* name,
884                         const char* version, bool only_if_ref
885                         ACCEPT_SIZE_ENDIAN);
886
887   // Define a symbol in an Output_data, sized version.
888   template<int size>
889   Sized_symbol<size>*
890   do_define_in_output_data(const Target*, const char* name,
891                            const char* version, Output_data*,
892                            typename elfcpp::Elf_types<size>::Elf_Addr value,
893                            typename elfcpp::Elf_types<size>::Elf_WXword ssize,
894                            elfcpp::STT type, elfcpp::STB binding,
895                            elfcpp::STV visibility, unsigned char nonvis,
896                            bool offset_is_from_end, bool only_if_ref);
897
898   // Define a symbol in an Output_segment, sized version.
899   template<int size>
900   Sized_symbol<size>*
901   do_define_in_output_segment(
902     const Target*, const char* name, const char* version, Output_segment* os,
903     typename elfcpp::Elf_types<size>::Elf_Addr value,
904     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
905     elfcpp::STT type, elfcpp::STB binding,
906     elfcpp::STV visibility, unsigned char nonvis,
907     Symbol::Segment_offset_base offset_base, bool only_if_ref);
908
909   // Define a symbol as a constant, sized version.
910   template<int size>
911   Sized_symbol<size>*
912   do_define_as_constant(
913     const Target*, const char* name, const char* version,
914     typename elfcpp::Elf_types<size>::Elf_Addr value,
915     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
916     elfcpp::STT type, elfcpp::STB binding,
917     elfcpp::STV visibility, unsigned char nonvis,
918     bool only_if_ref);
919
920   // Allocate the common symbols, sized version.
921   template<int size>
922   void
923   do_allocate_commons(const General_options&, Layout*);
924
925   // Finalize symbols specialized for size.
926   template<int size>
927   off_t
928   sized_finalize(unsigned int, off_t, Stringpool*);
929
930   // Write globals specialized for size and endianness.
931   template<int size, bool big_endian>
932   void
933   sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
934                       Output_file*) const;
935
936   // Write out a symbol to P.
937   template<int size, bool big_endian>
938   void
939   sized_write_symbol(Sized_symbol<size>*, unsigned int shndx,
940                      const Stringpool*, unsigned char* p
941                      ACCEPT_SIZE_ENDIAN) const;
942
943   // Write out a section symbol, specialized for size and endianness.
944   template<int size, bool big_endian>
945   void
946   sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
947
948   // The type of the symbol hash table.
949
950   typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
951
952   struct Symbol_table_hash
953   {
954     size_t
955     operator()(const Symbol_table_key&) const;
956   };
957
958   struct Symbol_table_eq
959   {
960     bool
961     operator()(const Symbol_table_key&, const Symbol_table_key&) const;
962   };
963
964   typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
965                         Symbol_table_eq> Symbol_table_type;
966
967   // The type of the list of common symbols.
968
969   typedef std::vector<Symbol*> Commons_type;
970
971   // The size of the symbols in the symbol table (32 or 64).
972   int size_;
973
974   // We increment this every time we see a new undefined symbol, for
975   // use in archive groups.
976   int saw_undefined_;
977
978   // The index of the first global symbol in the output file.
979   unsigned int first_global_index_;
980
981   // The file offset within the output symtab section where we should
982   // write the table.
983   off_t offset_;
984
985   // The number of global symbols we want to write out.
986   size_t output_count_;
987
988   // The file offset of the global dynamic symbols, or 0 if none.
989   off_t dynamic_offset_;
990
991   // The index of the first global dynamic symbol.
992   unsigned int first_dynamic_global_index_;
993
994   // The number of global dynamic symbols, or 0 if none.
995   off_t dynamic_count_;
996
997   // The symbol hash table.
998   Symbol_table_type table_;
999
1000   // A pool of symbol names.  This is used for all global symbols.
1001   // Entries in the hash table point into this pool.
1002   Stringpool namepool_;
1003
1004   // Forwarding symbols.
1005   Unordered_map<const Symbol*, Symbol*> forwarders_;
1006
1007   // We don't expect there to be very many common symbols, so we keep
1008   // a list of them.  When we find a common symbol we add it to this
1009   // list.  It is possible that by the time we process the list the
1010   // symbol is no longer a common symbol.  It may also have become a
1011   // forwarder.
1012   Commons_type commons_;
1013
1014   // Manage symbol warnings.
1015   Warnings warnings_;
1016 };
1017
1018 // We inline get_sized_symbol for efficiency.
1019
1020 template<int size>
1021 Sized_symbol<size>*
1022 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1023 {
1024   gold_assert(size == this->get_size());
1025   return static_cast<Sized_symbol<size>*>(sym);
1026 }
1027
1028 template<int size>
1029 const Sized_symbol<size>*
1030 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1031 {
1032   gold_assert(size == this->get_size());
1033   return static_cast<const Sized_symbol<size>*>(sym);
1034 }
1035
1036 } // End namespace gold.
1037
1038 #endif // !defined(GOLD_SYMTAB_H)