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