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