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