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