OSDN Git Service

Can now dynamically link hello, world.
[pf3gnuchains/pf3gnuchains3x.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   shndx() const
101   {
102     gold_assert(this->source_ == FROM_OBJECT);
103     return this->u_.from_object.shndx;
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 already has an index in the dynamic
239   // symbol table.
240   bool
241   has_dynsym_index() const
242   { return this->dynsym_index_ != 0; }
243
244   // Return whether this symbol has an entry in the GOT section.
245   bool
246   has_got_offset() const
247   { return this->has_got_offset_; }
248
249   // Return the offset into the GOT section of this symbol.
250   unsigned int
251   got_offset() const
252   {
253     gold_assert(this->has_got_offset());
254     return this->got_offset_;
255   }
256
257   // Set the GOT offset of this symbol.
258   void
259   set_got_offset(unsigned int got_offset)
260   {
261     this->has_got_offset_ = true;
262     this->got_offset_ = got_offset;
263   }
264
265   // Return whether this symbol has an entry in the PLT section.
266   bool
267   has_plt_offset() const
268   { return this->has_plt_offset_; }
269
270   // Return the offset into the PLT section of this symbol.
271   unsigned int
272   plt_offset() const
273   {
274     gold_assert(this->has_plt_offset());
275     return this->plt_offset_;
276   }
277
278   // Set the PLT offset of this symbol.
279   void
280   set_plt_offset(unsigned int plt_offset)
281   {
282     this->has_plt_offset_ = true;
283     this->plt_offset_ = plt_offset;
284   }
285
286   // Return true if the final value of this symbol is known at link
287   // time.
288   bool
289   final_value_is_known(const General_options* options) const
290   {
291     if (options->is_shared())
292       return false;
293     return this->source_ != FROM_OBJECT || !this->object()->is_dynamic();
294   }
295
296   // Return whether this is a defined symbol (not undefined or
297   // common).
298   bool
299   is_defined() const
300   {
301     return (this->source_ != FROM_OBJECT
302             || (this->shndx() != elfcpp::SHN_UNDEF
303                 && this->shndx() != elfcpp::SHN_COMMON));
304   }
305
306   // Return whether this symbol is defined in a dynamic object.
307   bool
308   is_defined_in_dynobj() const
309   {
310     return (this->source_ == FROM_OBJECT
311             && this->object()->is_dynamic()
312             && this->is_defined());
313   }
314
315   // Return whether this is an undefined symbol.
316   bool
317   is_undefined() const
318   {
319     return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
320   }
321
322   // Return whether this is a common symbol.
323   bool
324   is_common() const
325   {
326     return (this->source_ == FROM_OBJECT
327             && (this->shndx() == elfcpp::SHN_COMMON
328                 || this->type_ == elfcpp::STT_COMMON));
329   }
330
331   // Return whether there should be a warning for references to this
332   // symbol.
333   bool
334   has_warning() const
335   { return this->has_warning_; }
336
337   // Mark this symbol as having a warning.
338   void
339   set_has_warning()
340   { this->has_warning_ = true; }
341
342  protected:
343   // Instances of this class should always be created at a specific
344   // size.
345   Symbol()
346   { memset(this, 0, sizeof *this); }
347
348   // Initialize the general fields.
349   void
350   init_fields(const char* name, const char* version,
351               elfcpp::STT type, elfcpp::STB binding,
352               elfcpp::STV visibility, unsigned char nonvis);
353
354   // Initialize fields from an ELF symbol in OBJECT.
355   template<int size, bool big_endian>
356   void
357   init_base(const char *name, const char* version, Object* object,
358             const elfcpp::Sym<size, big_endian>&);
359
360   // Initialize fields for an Output_data.
361   void
362   init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
363             elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
364
365   // Initialize fields for an Output_segment.
366   void
367   init_base(const char* name, Output_segment* os, elfcpp::STT type,
368             elfcpp::STB binding, elfcpp::STV visibility,
369             unsigned char nonvis, Segment_offset_base offset_base);
370
371   // Initialize fields for a constant.
372   void
373   init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
374             elfcpp::STV visibility, unsigned char nonvis);
375
376   // Override existing symbol.
377   template<int size, bool big_endian>
378   void
379   override_base(const elfcpp::Sym<size, big_endian>&, Object* object);
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
523   // Return the symbol's value.
524   Value_type
525   value() const
526   { return this->value_; }
527
528   // Return the symbol's size (we can't call this 'size' because that
529   // is a template parameter).
530   Size_type
531   symsize() const
532   { return this->symsize_; }
533
534   // Set the symbol size.  This is used when resolving common symbols.
535   void
536   set_symsize(Size_type symsize)
537   { this->symsize_ = symsize; }
538
539   // Set the symbol value.  This is called when we store the final
540   // values of the symbols into the symbol table.
541   void
542   set_value(Value_type value)
543   { this->value_ = value; }
544
545  private:
546   Sized_symbol(const Sized_symbol&);
547   Sized_symbol& operator=(const Sized_symbol&);
548
549   // Symbol value.  Before Layout::finalize this is the offset in the
550   // input section.  This is set to the final value during
551   // Layout::finalize.
552   Value_type value_;
553   // Symbol size.
554   Size_type symsize_;
555 };
556
557 // A struct describing a symbol defined by the linker, where the value
558 // of the symbol is defined based on an output section.  This is used
559 // for symbols defined by the linker, like "_init_array_start".
560
561 struct Define_symbol_in_section
562 {
563   // The symbol name.
564   const char* name;
565   // The name of the output section with which this symbol should be
566   // associated.  If there is no output section with that name, the
567   // symbol will be defined as zero.
568   const char* output_section;
569   // The offset of the symbol within the output section.  This is an
570   // offset from the start of the output section, unless start_at_end
571   // is true, in which case this is an offset from the end of the
572   // output section.
573   uint64_t value;
574   // The size of the symbol.
575   uint64_t size;
576   // The symbol type.
577   elfcpp::STT type;
578   // The symbol binding.
579   elfcpp::STB binding;
580   // The symbol visibility.
581   elfcpp::STV visibility;
582   // The rest of the st_other field.
583   unsigned char nonvis;
584   // If true, the value field is an offset from the end of the output
585   // section.
586   bool offset_is_from_end;
587   // If true, this symbol is defined only if we see a reference to it.
588   bool only_if_ref;
589 };
590
591 // A struct describing a symbol defined by the linker, where the value
592 // of the symbol is defined based on a segment.  This is used for
593 // symbols defined by the linker, like "_end".  We describe the
594 // segment with which the symbol should be associated by its
595 // characteristics.  If no segment meets these characteristics, the
596 // symbol will be defined as zero.  If there is more than one segment
597 // which meets these characteristics, we will use the first one.
598
599 struct Define_symbol_in_segment
600 {
601   // The symbol name.
602   const char* name;
603   // The segment type where the symbol should be defined, typically
604   // PT_LOAD.
605   elfcpp::PT segment_type;
606   // Bitmask of segment flags which must be set.
607   elfcpp::PF segment_flags_set;
608   // Bitmask of segment flags which must be clear.
609   elfcpp::PF segment_flags_clear;
610   // The offset of the symbol within the segment.  The offset is
611   // calculated from the position set by offset_base.
612   uint64_t value;
613   // The size of the symbol.
614   uint64_t size;
615   // The symbol type.
616   elfcpp::STT type;
617   // The symbol binding.
618   elfcpp::STB binding;
619   // The symbol visibility.
620   elfcpp::STV visibility;
621   // The rest of the st_other field.
622   unsigned char nonvis;
623   // The base from which we compute the offset.
624   Symbol::Segment_offset_base offset_base;
625   // If true, this symbol is defined only if we see a reference to it.
626   bool only_if_ref;
627 };
628
629 // This class manages warnings.  Warnings are a GNU extension.  When
630 // we see a section named .gnu.warning.SYM in an object file, and if
631 // we wind using the definition of SYM from that object file, then we
632 // will issue a warning for any relocation against SYM from a
633 // different object file.  The text of the warning is the contents of
634 // the section.  This is not precisely the definition used by the old
635 // GNU linker; the old GNU linker treated an occurrence of
636 // .gnu.warning.SYM as defining a warning symbol.  A warning symbol
637 // would trigger a warning on any reference.  However, it was
638 // inconsistent in that a warning in a dynamic object only triggered
639 // if there was no definition in a regular object.  This linker is
640 // different in that we only issue a warning if we use the symbol
641 // definition from the same object file as the warning section.
642
643 class Warnings
644 {
645  public:
646   Warnings()
647     : warnings_()
648   { }
649
650   // Add a warning for symbol NAME in section SHNDX in object OBJ.
651   void
652   add_warning(Symbol_table* symtab, const char* name, Object* obj,
653               unsigned int shndx);
654
655   // For each symbol for which we should give a warning, make a note
656   // on the symbol.
657   void
658   note_warnings(Symbol_table* symtab);
659
660   // Issue a warning for a reference to SYM at LOCATION.
661   void
662   issue_warning(const Symbol* sym, const std::string& location) const;
663
664  private:
665   Warnings(const Warnings&);
666   Warnings& operator=(const Warnings&);
667
668   // What we need to know to get the warning text.
669   struct Warning_location
670   {
671     // The object the warning is in.
672     Object* object;
673     // The index of the warning section.
674     unsigned int shndx;
675     // The warning text if we have already loaded it.
676     std::string text;
677
678     Warning_location()
679       : object(NULL), shndx(0), text()
680     { }
681
682     void
683     set(Object* o, unsigned int s)
684     {
685       this->object = o;
686       this->shndx = s;
687     }
688
689     void
690     set_text(const char* t, off_t l)
691     { this->text.assign(t, l); }
692   };
693
694   // A mapping from warning symbol names (canonicalized in
695   // Symbol_table's namepool_ field) to 
696   typedef Unordered_map<const char*, Warning_location> Warning_table;
697
698   Warning_table warnings_;
699 };
700
701 // The main linker symbol table.
702
703 class Symbol_table
704 {
705  public:
706   Symbol_table();
707
708   ~Symbol_table();
709
710   // Add COUNT external symbols from the relocatable object RELOBJ to
711   // the symbol table.  SYMS is the symbols, SYM_NAMES is their names,
712   // SYM_NAME_SIZE is the size of SYM_NAMES.  This sets SYMPOINTERS to
713   // point to the symbols in the symbol table.
714   template<int size, bool big_endian>
715   void
716   add_from_relobj(Sized_relobj<size, big_endian>* relobj,
717                   const unsigned char* syms, size_t count,
718                   const char* sym_names, size_t sym_name_size,
719                   Symbol** sympointers);
720
721   // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
722   // symbol table.  SYMS is the symbols.  SYM_NAMES is their names.
723   // SYM_NAME_SIZE is the size of SYM_NAMES.  The other parameters are
724   // symbol version data.
725   template<int size, bool big_endian>
726   void
727   add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
728                   const unsigned char* syms, size_t count,
729                   const char* sym_names, size_t sym_name_size,
730                   const unsigned char* versym, size_t versym_size,
731                   const std::vector<const char*>*);
732
733   // Define a special symbol.
734   template<int size, bool big_endian>
735   Sized_symbol<size>*
736   define_special_symbol(Target* target, const char* name, bool only_if_ref
737                         ACCEPT_SIZE_ENDIAN);
738
739   // Define a special symbol based on an Output_data.  It is a
740   // multiple definition error if this symbol is already defined.
741   void
742   define_in_output_data(Target*, const char* name, Output_data*,
743                         uint64_t value, uint64_t symsize,
744                         elfcpp::STT type, elfcpp::STB binding,
745                         elfcpp::STV visibility, unsigned char nonvis,
746                         bool offset_is_from_end, bool only_if_ref);
747
748   // Define a special symbol based on an Output_segment.  It is a
749   // multiple definition error if this symbol is already defined.
750   void
751   define_in_output_segment(Target*, const char* name, Output_segment*,
752                            uint64_t value, uint64_t symsize,
753                            elfcpp::STT type, elfcpp::STB binding,
754                            elfcpp::STV visibility, unsigned char nonvis,
755                            Symbol::Segment_offset_base, bool only_if_ref);
756
757   // Define a special symbol with a constant value.  It is a multiple
758   // definition error if this symbol is already defined.
759   void
760   define_as_constant(Target*, const char* name, uint64_t value,
761                      uint64_t symsize, elfcpp::STT type, elfcpp::STB binding,
762                      elfcpp::STV visibility, unsigned char nonvis,
763                      bool only_if_ref);
764
765   // Define a set of symbols in output sections.
766   void
767   define_symbols(const Layout*, Target*, int count,
768                  const Define_symbol_in_section*);
769
770   // Define a set of symbols in output segments.
771   void
772   define_symbols(const Layout*, Target*, int count,
773                  const Define_symbol_in_segment*);  
774
775   // Look up a symbol.
776   Symbol*
777   lookup(const char*, const char* version = NULL) const;
778
779   // Return the real symbol associated with the forwarder symbol FROM.
780   Symbol*
781   resolve_forwards(const Symbol* from) const;
782
783   // Return the bitsize (32 or 64) of the symbols in the table.
784   int
785   get_size() const
786   { return this->size_; }
787
788   // Return the sized version of a symbol in this table.
789   template<int size>
790   Sized_symbol<size>*
791   get_sized_symbol(Symbol* ACCEPT_SIZE) const;
792
793   template<int size>
794   const Sized_symbol<size>*
795   get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
796
797   // Return the count of undefined symbols seen.
798   int
799   saw_undefined() const
800   { return this->saw_undefined_; }
801
802   // Allocate the common symbols
803   void
804   allocate_commons(const General_options&, Layout*);
805
806   // Add a warning for symbol NAME in section SHNDX in object OBJ.
807   void
808   add_warning(const char* name, Object* obj, unsigned int shndx)
809   { this->warnings_.add_warning(this, name, obj, shndx); }
810
811   // Canonicalize a symbol name for use in the hash table.
812   const char*
813   canonicalize_name(const char* name)
814   { return this->namepool_.add(name, NULL); }
815
816   // Possibly issue a warning for a reference to SYM at LOCATION which
817   // is in OBJ.
818   void
819   issue_warning(const Symbol* sym, const std::string& location) const
820   { this->warnings_.issue_warning(sym, location); }
821
822   // Set the dynamic symbol indexes.  INDEX is the index of the first
823   // global dynamic symbol.  Pointers to the symbols are stored into
824   // the vector.  The names are stored into the Stringpool.  This
825   // returns an updated dynamic symbol index.
826   unsigned int
827   set_dynsym_indexes(unsigned int index, std::vector<Symbol*>*,
828                      Stringpool*);
829
830   // Finalize the symbol table after we have set the final addresses
831   // of all the input sections.  This sets the final symbol indexes,
832   // values and adds the names to *POOL.  INDEX is the index of the
833   // first global symbol.  OFF is the file offset of the global symbol
834   // table, DYNOFF is the offset of the globals in the dynamic symbol
835   // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
836   // symbol, and DYNCOUNT is the number of global dynamic symbols.
837   // This records the parameters, and returns the new file offset.
838   off_t
839   finalize(unsigned int index, off_t off, off_t dynoff,
840            size_t dyn_global_index, size_t dyncount, Stringpool* pool);
841
842   // Write out the global symbols.
843   void
844   write_globals(const Target*, const Stringpool*, const Stringpool*,
845                 Output_file*) const;
846
847   // Write out a section symbol.  Return the updated offset.
848   void
849   write_section_symbol(const Target*, const Output_section*, Output_file*,
850                        off_t) const;
851
852  private:
853   Symbol_table(const Symbol_table&);
854   Symbol_table& operator=(const Symbol_table&);
855
856   // Set the size (32 or 64) of the symbols in the table.
857   void
858   set_size(int size)
859   { this->size_ = size; }
860
861   // Make FROM a forwarder symbol to TO.
862   void
863   make_forwarder(Symbol* from, Symbol* to);
864
865   // Add a symbol.
866   template<int size, bool big_endian>
867   Symbol*
868   add_from_object(Object*, const char *name, Stringpool::Key name_key,
869                   const char *version, Stringpool::Key version_key,
870                   bool def, const elfcpp::Sym<size, big_endian>& sym);
871
872   // Resolve symbols.
873   template<int size, bool big_endian>
874   static void
875   resolve(Sized_symbol<size>* to,
876           const elfcpp::Sym<size, big_endian>& sym,
877           Object*);
878
879   template<int size, bool big_endian>
880   static void
881   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
882           ACCEPT_SIZE_ENDIAN);
883
884   // Define a symbol in an Output_data, sized version.
885   template<int size>
886   void
887   do_define_in_output_data(Target*, const char* name, Output_data*,
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                            bool offset_is_from_end, bool only_if_ref);
893
894   // Define a symbol in an Output_segment, sized version.
895   template<int size>
896   void
897   do_define_in_output_segment(
898     Target*, const char* name, Output_segment* os,
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     Symbol::Segment_offset_base offset_base, bool only_if_ref);
904
905   // Define a symbol as a constant, sized version.
906   template<int size>
907   void
908   do_define_as_constant(
909     Target*, const char* name,
910     typename elfcpp::Elf_types<size>::Elf_Addr value,
911     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
912     elfcpp::STT type, elfcpp::STB binding,
913     elfcpp::STV visibility, unsigned char nonvis,
914     bool only_if_ref);
915
916   // Allocate the common symbols, sized version.
917   template<int size>
918   void
919   do_allocate_commons(const General_options&, Layout*);
920
921   // Finalize symbols specialized for size.
922   template<int size>
923   off_t
924   sized_finalize(unsigned int, off_t, Stringpool*);
925
926   // Write globals specialized for size and endianness.
927   template<int size, bool big_endian>
928   void
929   sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
930                       Output_file*) const;
931
932   // Write out a symbol to P.
933   template<int size, bool big_endian>
934   void
935   sized_write_symbol(Sized_symbol<size>*, unsigned int shndx,
936                      const Stringpool*, unsigned char* p) const;
937
938   // Write out a section symbol, specialized for size and endianness.
939   template<int size, bool big_endian>
940   void
941   sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
942
943   // The type of the symbol hash table.
944
945   typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
946
947   struct Symbol_table_hash
948   {
949     size_t
950     operator()(const Symbol_table_key&) const;
951   };
952
953   struct Symbol_table_eq
954   {
955     bool
956     operator()(const Symbol_table_key&, const Symbol_table_key&) const;
957   };
958
959   typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
960                         Symbol_table_eq> Symbol_table_type;
961
962   // The type of the list of common symbols.
963
964   typedef std::vector<Symbol*> Commons_type;
965
966   // The size of the symbols in the symbol table (32 or 64).
967   int size_;
968
969   // We increment this every time we see a new undefined symbol, for
970   // use in archive groups.
971   int saw_undefined_;
972
973   // The index of the first global symbol in the output file.
974   unsigned int first_global_index_;
975
976   // The file offset within the output symtab section where we should
977   // write the table.
978   off_t offset_;
979
980   // The number of global symbols we want to write out.
981   size_t output_count_;
982
983   // The file offset of the global dynamic symbols, or 0 if none.
984   off_t dynamic_offset_;
985
986   // The index of the first global dynamic symbol.
987   unsigned int first_dynamic_global_index_;
988
989   // The number of global dynamic symbols, or 0 if none.
990   off_t dynamic_count_;
991
992   // The symbol hash table.
993   Symbol_table_type table_;
994
995   // A pool of symbol names.  This is used for all global symbols.
996   // Entries in the hash table point into this pool.
997   Stringpool namepool_;
998
999   // Forwarding symbols.
1000   Unordered_map<const Symbol*, Symbol*> forwarders_;
1001
1002   // We don't expect there to be very many common symbols, so we keep
1003   // a list of them.  When we find a common symbol we add it to this
1004   // list.  It is possible that by the time we process the list the
1005   // symbol is no longer a common symbol.  It may also have become a
1006   // forwarder.
1007   Commons_type commons_;
1008
1009   // Manage symbol warnings.
1010   Warnings warnings_;
1011 };
1012
1013 // We inline get_sized_symbol for efficiency.
1014
1015 template<int size>
1016 Sized_symbol<size>*
1017 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1018 {
1019   gold_assert(size == this->get_size());
1020   return static_cast<Sized_symbol<size>*>(sym);
1021 }
1022
1023 template<int size>
1024 const Sized_symbol<size>*
1025 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1026 {
1027   gold_assert(size == this->get_size());
1028   return static_cast<const Sized_symbol<size>*>(sym);
1029 }
1030
1031 } // End namespace gold.
1032
1033 #endif // !defined(GOLD_SYMTAB_H)