Fix versions of copied symbols.
[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 should be added to the dynamic symbol
224   // table.
225   bool
226   should_add_dynsym_entry() const;
227
228   // Return whether this symbol has been seen in a regular object.
229   bool
230   in_reg() const
231   { return this->in_reg_; }
232
233   // Mark this symbol as having been seen in a regular object.
234   void
235   set_in_reg()
236   { this->in_reg_ = true; }
237
238   // Return whether this symbol has been seen in a dynamic object.
239   bool
240   in_dyn() const
241   { return this->in_dyn_; }
242
243   // Mark this symbol as having been seen in a dynamic object.
244   void
245   set_in_dyn()
246   { this->in_dyn_ = true; }
247
248   // Return the index of this symbol in the output file symbol table.
249   // A value of -1U means that this symbol is not going into the
250   // output file.  This starts out as zero, and is set to a non-zero
251   // value by Symbol_table::finalize.  It is an error to ask for the
252   // symbol table index before it has been set.
253   unsigned int
254   symtab_index() const
255   {
256     gold_assert(this->symtab_index_ != 0);
257     return this->symtab_index_;
258   }
259
260   // Set the index of the symbol in the output file symbol table.
261   void
262   set_symtab_index(unsigned int index)
263   {
264     gold_assert(index != 0);
265     this->symtab_index_ = index;
266   }
267
268   // Return whether this symbol already has an index in the output
269   // file symbol table.
270   bool
271   has_symtab_index() const
272   { return this->symtab_index_ != 0; }
273
274   // Return the index of this symbol in the dynamic symbol table.  A
275   // value of -1U means that this symbol is not going into the dynamic
276   // symbol table.  This starts out as zero, and is set to a non-zero
277   // during Layout::finalize.  It is an error to ask for the dynamic
278   // symbol table index before it has been set.
279   unsigned int
280   dynsym_index() const
281   {
282     gold_assert(this->dynsym_index_ != 0);
283     return this->dynsym_index_;
284   }
285
286   // Set the index of the symbol in the dynamic symbol table.
287   void
288   set_dynsym_index(unsigned int index)
289   {
290     gold_assert(index != 0);
291     this->dynsym_index_ = index;
292   }
293
294   // Return whether this symbol already has an index in the dynamic
295   // symbol table.
296   bool
297   has_dynsym_index() const
298   { return this->dynsym_index_ != 0; }
299
300   // Return whether this symbol has an entry in the GOT section.
301   bool
302   has_got_offset() const
303   { return this->has_got_offset_; }
304
305   // Return the offset into the GOT section of this symbol.
306   unsigned int
307   got_offset() const
308   {
309     gold_assert(this->has_got_offset());
310     return this->got_offset_;
311   }
312
313   // Set the GOT offset of this symbol.
314   void
315   set_got_offset(unsigned int got_offset)
316   {
317     this->has_got_offset_ = true;
318     this->got_offset_ = got_offset;
319   }
320
321   // Return whether this symbol has an entry in the PLT section.
322   bool
323   has_plt_offset() const
324   { return this->has_plt_offset_; }
325
326   // Return the offset into the PLT section of this symbol.
327   unsigned int
328   plt_offset() const
329   {
330     gold_assert(this->has_plt_offset());
331     return this->plt_offset_;
332   }
333
334   // Set the PLT offset of this symbol.
335   void
336   set_plt_offset(unsigned int plt_offset)
337   {
338     this->has_plt_offset_ = true;
339     this->plt_offset_ = plt_offset;
340   }
341
342   // Return whether this dynamic symbol needs a special value in the
343   // dynamic symbol table.
344   bool
345   needs_dynsym_value() const
346   { return this->needs_dynsym_value_; }
347
348   // Set that this dynamic symbol needs a special value in the dynamic
349   // symbol table.
350   void
351   set_needs_dynsym_value()
352   {
353     gold_assert(this->object()->is_dynamic());
354     this->needs_dynsym_value_ = true;
355   }
356
357   // Return true if the final value of this symbol is known at link
358   // time.
359   bool
360   final_value_is_known() const;
361
362   // Return whether this is a defined symbol (not undefined or
363   // common).
364   bool
365   is_defined() const
366   {
367     return (this->source_ != FROM_OBJECT
368             || (this->shndx() != elfcpp::SHN_UNDEF
369                 && this->shndx() != elfcpp::SHN_COMMON));
370   }
371
372   // Return true if this symbol is from a dynamic object.
373   bool
374   is_from_dynobj() const
375   {
376     return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
377   }
378
379   // Return whether this is an undefined symbol.
380   bool
381   is_undefined() const
382   {
383     return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
384   }
385
386   // Return whether this is a common symbol.
387   bool
388   is_common() const
389   {
390     return (this->source_ == FROM_OBJECT
391             && (this->shndx() == elfcpp::SHN_COMMON
392                 || this->type_ == elfcpp::STT_COMMON));
393   }
394
395   // Return whether this symbol can be seen outside this object.
396   bool
397   is_externally_visible() const
398   {
399     return (this->visibility_ == elfcpp::STV_DEFAULT
400             || this->visibility_ == elfcpp::STV_PROTECTED);
401   }
402
403   // Return true if this symbol can be preempted by a definition in
404   // another link unit.
405   bool
406   is_preemptible() const
407   {
408     return (this->visibility_ != elfcpp::STV_INTERNAL
409             && this->visibility_ != elfcpp::STV_HIDDEN
410             && this->visibility_ != elfcpp::STV_PROTECTED
411             && !parameters->symbolic());
412   }
413
414   // Return whether there should be a warning for references to this
415   // symbol.
416   bool
417   has_warning() const
418   { return this->has_warning_; }
419
420   // Mark this symbol as having a warning.
421   void
422   set_has_warning()
423   { this->has_warning_ = true; }
424
425   // Return whether this symbol is defined by a COPY reloc from a
426   // dynamic object.
427   bool
428   is_copied_from_dynobj() const
429   { return this->is_copied_from_dynobj_; }
430
431   // Mark this symbol as defined by a COPY reloc.
432   void
433   set_is_copied_from_dynobj()
434   { this->is_copied_from_dynobj_ = true; }
435
436  protected:
437   // Instances of this class should always be created at a specific
438   // size.
439   Symbol()
440   { memset(this, 0, sizeof *this); }
441
442   // Initialize the general fields.
443   void
444   init_fields(const char* name, const char* version,
445               elfcpp::STT type, elfcpp::STB binding,
446               elfcpp::STV visibility, unsigned char nonvis);
447
448   // Initialize fields from an ELF symbol in OBJECT.
449   template<int size, bool big_endian>
450   void
451   init_base(const char *name, const char* version, Object* object,
452             const elfcpp::Sym<size, big_endian>&);
453
454   // Initialize fields for an Output_data.
455   void
456   init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
457             elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
458
459   // Initialize fields for an Output_segment.
460   void
461   init_base(const char* name, Output_segment* os, elfcpp::STT type,
462             elfcpp::STB binding, elfcpp::STV visibility,
463             unsigned char nonvis, Segment_offset_base offset_base);
464
465   // Initialize fields for a constant.
466   void
467   init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
468             elfcpp::STV visibility, unsigned char nonvis);
469
470   // Override existing symbol.
471   template<int size, bool big_endian>
472   void
473   override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
474                 const char* version);
475
476   // Override existing symbol with a special symbol.
477   void
478   override_base_with_special(const Symbol* from);
479
480  private:
481   Symbol(const Symbol&);
482   Symbol& operator=(const Symbol&);
483
484   // Symbol name (expected to point into a Stringpool).
485   const char* name_;
486   // Symbol version (expected to point into a Stringpool).  This may
487   // be NULL.
488   const char* version_;
489
490   union
491   {
492     // This struct is used if SOURCE_ == FROM_OBJECT.
493     struct
494     {
495       // Object in which symbol is defined, or in which it was first
496       // seen.
497       Object* object;
498       // Section number in object_ in which symbol is defined.
499       unsigned int shndx;
500     } from_object;
501
502     // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
503     struct
504     {
505       // Output_data in which symbol is defined.  Before
506       // Layout::finalize the symbol's value is an offset within the
507       // Output_data.
508       Output_data* output_data;
509       // True if the offset is from the end, false if the offset is
510       // from the beginning.
511       bool offset_is_from_end;
512     } in_output_data;
513
514     // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
515     struct
516     {
517       // Output_segment in which the symbol is defined.  Before
518       // Layout::finalize the symbol's value is an offset.
519       Output_segment* output_segment;
520       // The base to use for the offset before Layout::finalize.
521       Segment_offset_base offset_base;
522     } in_output_segment;
523   } u_;
524
525   // The index of this symbol in the output file.  If the symbol is
526   // not going into the output file, this value is -1U.  This field
527   // starts as always holding zero.  It is set to a non-zero value by
528   // Symbol_table::finalize.
529   unsigned int symtab_index_;
530
531   // The index of this symbol in the dynamic symbol table.  If the
532   // symbol is not going into the dynamic symbol table, this value is
533   // -1U.  This field starts as always holding zero.  It is set to a
534   // non-zero value during Layout::finalize.
535   unsigned int dynsym_index_;
536
537   // If this symbol has an entry in the GOT section (has_got_offset_
538   // is true), this is the offset from the start of the GOT section.
539   unsigned int got_offset_;
540
541   // If this symbol has an entry in the PLT section (has_plt_offset_
542   // is true), then this is the offset from the start of the PLT
543   // section.
544   unsigned int plt_offset_;
545
546   // Symbol type.
547   elfcpp::STT type_ : 4;
548   // Symbol binding.
549   elfcpp::STB binding_ : 4;
550   // Symbol visibility.
551   elfcpp::STV visibility_ : 2;
552   // Rest of symbol st_other field.
553   unsigned int nonvis_ : 6;
554   // The type of symbol.
555   Source source_ : 3;
556   // True if this symbol always requires special target-specific
557   // handling.
558   bool is_target_special_ : 1;
559   // True if this is the default version of the symbol.
560   bool is_def_ : 1;
561   // True if this symbol really forwards to another symbol.  This is
562   // used when we discover after the fact that two different entries
563   // in the hash table really refer to the same symbol.  This will
564   // never be set for a symbol found in the hash table, but may be set
565   // for a symbol found in the list of symbols attached to an Object.
566   // It forwards to the symbol found in the forwarders_ map of
567   // Symbol_table.
568   bool is_forwarder_ : 1;
569   // True if the symbol has an alias in the weak_aliases table in
570   // Symbol_table.
571   bool has_alias_ : 1;
572   // True if this symbol needs to be in the dynamic symbol table.
573   bool needs_dynsym_entry_ : 1;
574   // True if we've seen this symbol in a regular object.
575   bool in_reg_ : 1;
576   // True if we've seen this symbol in a dynamic object.
577   bool in_dyn_ : 1;
578   // True if the symbol has an entry in the GOT section.
579   bool has_got_offset_ : 1;
580   // True if the symbol has an entry in the PLT section.
581   bool has_plt_offset_ : 1;
582   // True if this is a dynamic symbol which needs a special value in
583   // the dynamic symbol table.
584   bool needs_dynsym_value_ : 1;
585   // True if there is a warning for this symbol.
586   bool has_warning_ : 1;
587   // True if we are using a COPY reloc for this symbol, so that the
588   // real definition lives in a dynamic object.
589   bool is_copied_from_dynobj_ : 1;
590 };
591
592 // The parts of a symbol which are size specific.  Using a template
593 // derived class like this helps us use less space on a 32-bit system.
594
595 template<int size>
596 class Sized_symbol : public Symbol
597 {
598  public:
599   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
600   typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
601
602   Sized_symbol()
603   { }
604
605   // Initialize fields from an ELF symbol in OBJECT.
606   template<bool big_endian>
607   void
608   init(const char *name, const char* version, Object* object,
609        const elfcpp::Sym<size, big_endian>&);
610
611   // Initialize fields for an Output_data.
612   void
613   init(const char* name, Output_data*, Value_type value, Size_type symsize,
614        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
615        bool offset_is_from_end);
616
617   // Initialize fields for an Output_segment.
618   void
619   init(const char* name, Output_segment*, Value_type value, Size_type symsize,
620        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
621        Segment_offset_base offset_base);
622
623   // Initialize fields for a constant.
624   void
625   init(const char* name, Value_type value, Size_type symsize,
626        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
627
628   // Override existing symbol.
629   template<bool big_endian>
630   void
631   override(const elfcpp::Sym<size, big_endian>&, Object* object,
632            const char* version);
633
634   // Override existing symbol with a special symbol.
635   void
636   override_with_special(const Sized_symbol<size>*);
637
638   // Return the symbol's value.
639   Value_type
640   value() const
641   { return this->value_; }
642
643   // Return the symbol's size (we can't call this 'size' because that
644   // is a template parameter).
645   Size_type
646   symsize() const
647   { return this->symsize_; }
648
649   // Set the symbol size.  This is used when resolving common symbols.
650   void
651   set_symsize(Size_type symsize)
652   { this->symsize_ = symsize; }
653
654   // Set the symbol value.  This is called when we store the final
655   // values of the symbols into the symbol table.
656   void
657   set_value(Value_type value)
658   { this->value_ = value; }
659
660  private:
661   Sized_symbol(const Sized_symbol&);
662   Sized_symbol& operator=(const Sized_symbol&);
663
664   // Symbol value.  Before Layout::finalize this is the offset in the
665   // input section.  This is set to the final value during
666   // Layout::finalize.
667   Value_type value_;
668   // Symbol size.
669   Size_type symsize_;
670 };
671
672 // A struct describing a symbol defined by the linker, where the value
673 // of the symbol is defined based on an output section.  This is used
674 // for symbols defined by the linker, like "_init_array_start".
675
676 struct Define_symbol_in_section
677 {
678   // The symbol name.
679   const char* name;
680   // The name of the output section with which this symbol should be
681   // associated.  If there is no output section with that name, the
682   // symbol will be defined as zero.
683   const char* output_section;
684   // The offset of the symbol within the output section.  This is an
685   // offset from the start of the output section, unless start_at_end
686   // is true, in which case this is an offset from the end of the
687   // output section.
688   uint64_t value;
689   // The size of the symbol.
690   uint64_t size;
691   // The symbol type.
692   elfcpp::STT type;
693   // The symbol binding.
694   elfcpp::STB binding;
695   // The symbol visibility.
696   elfcpp::STV visibility;
697   // The rest of the st_other field.
698   unsigned char nonvis;
699   // If true, the value field is an offset from the end of the output
700   // section.
701   bool offset_is_from_end;
702   // If true, this symbol is defined only if we see a reference to it.
703   bool only_if_ref;
704 };
705
706 // A struct describing a symbol defined by the linker, where the value
707 // of the symbol is defined based on a segment.  This is used for
708 // symbols defined by the linker, like "_end".  We describe the
709 // segment with which the symbol should be associated by its
710 // characteristics.  If no segment meets these characteristics, the
711 // symbol will be defined as zero.  If there is more than one segment
712 // which meets these characteristics, we will use the first one.
713
714 struct Define_symbol_in_segment
715 {
716   // The symbol name.
717   const char* name;
718   // The segment type where the symbol should be defined, typically
719   // PT_LOAD.
720   elfcpp::PT segment_type;
721   // Bitmask of segment flags which must be set.
722   elfcpp::PF segment_flags_set;
723   // Bitmask of segment flags which must be clear.
724   elfcpp::PF segment_flags_clear;
725   // The offset of the symbol within the segment.  The offset is
726   // calculated from the position set by offset_base.
727   uint64_t value;
728   // The size of the symbol.
729   uint64_t size;
730   // The symbol type.
731   elfcpp::STT type;
732   // The symbol binding.
733   elfcpp::STB binding;
734   // The symbol visibility.
735   elfcpp::STV visibility;
736   // The rest of the st_other field.
737   unsigned char nonvis;
738   // The base from which we compute the offset.
739   Symbol::Segment_offset_base offset_base;
740   // If true, this symbol is defined only if we see a reference to it.
741   bool only_if_ref;
742 };
743
744 // This class manages warnings.  Warnings are a GNU extension.  When
745 // we see a section named .gnu.warning.SYM in an object file, and if
746 // we wind using the definition of SYM from that object file, then we
747 // will issue a warning for any relocation against SYM from a
748 // different object file.  The text of the warning is the contents of
749 // the section.  This is not precisely the definition used by the old
750 // GNU linker; the old GNU linker treated an occurrence of
751 // .gnu.warning.SYM as defining a warning symbol.  A warning symbol
752 // would trigger a warning on any reference.  However, it was
753 // inconsistent in that a warning in a dynamic object only triggered
754 // if there was no definition in a regular object.  This linker is
755 // different in that we only issue a warning if we use the symbol
756 // definition from the same object file as the warning section.
757
758 class Warnings
759 {
760  public:
761   Warnings()
762     : warnings_()
763   { }
764
765   // Add a warning for symbol NAME in section SHNDX in object OBJ.
766   void
767   add_warning(Symbol_table* symtab, const char* name, Object* obj,
768               unsigned int shndx);
769
770   // For each symbol for which we should give a warning, make a note
771   // on the symbol.
772   void
773   note_warnings(Symbol_table* symtab);
774
775   // Issue a warning for a reference to SYM at RELINFO's location.
776   template<int size, bool big_endian>
777   void
778   issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
779                 size_t relnum, off_t reloffset) const;
780
781  private:
782   Warnings(const Warnings&);
783   Warnings& operator=(const Warnings&);
784
785   // What we need to know to get the warning text.
786   struct Warning_location
787   {
788     // The object the warning is in.
789     Object* object;
790     // The index of the warning section.
791     unsigned int shndx;
792     // The warning text if we have already loaded it.
793     std::string text;
794
795     Warning_location()
796       : object(NULL), shndx(0), text()
797     { }
798
799     void
800     set(Object* o, unsigned int s)
801     {
802       this->object = o;
803       this->shndx = s;
804     }
805
806     void
807     set_text(const char* t, off_t l)
808     { this->text.assign(t, l); }
809   };
810
811   // A mapping from warning symbol names (canonicalized in
812   // Symbol_table's namepool_ field) to 
813   typedef Unordered_map<const char*, Warning_location> Warning_table;
814
815   Warning_table warnings_;
816 };
817
818 // The main linker symbol table.
819
820 class Symbol_table
821 {
822  public:
823   Symbol_table();
824
825   ~Symbol_table();
826
827   // Add COUNT external symbols from the relocatable object RELOBJ to
828   // the symbol table.  SYMS is the symbols, SYM_NAMES is their names,
829   // SYM_NAME_SIZE is the size of SYM_NAMES.  This sets SYMPOINTERS to
830   // point to the symbols in the symbol table.
831   template<int size, bool big_endian>
832   void
833   add_from_relobj(Sized_relobj<size, big_endian>* relobj,
834                   const unsigned char* syms, size_t count,
835                   const char* sym_names, size_t sym_name_size,
836                   Symbol** sympointers);
837
838   // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
839   // symbol table.  SYMS is the symbols.  SYM_NAMES is their names.
840   // SYM_NAME_SIZE is the size of SYM_NAMES.  The other parameters are
841   // symbol version data.
842   template<int size, bool big_endian>
843   void
844   add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
845                   const unsigned char* syms, size_t count,
846                   const char* sym_names, size_t sym_name_size,
847                   const unsigned char* versym, size_t versym_size,
848                   const std::vector<const char*>*);
849
850   // Define a special symbol based on an Output_data.  It is a
851   // multiple definition error if this symbol is already defined.
852   Symbol*
853   define_in_output_data(const Target*, const char* name, const char* version,
854                         Output_data*, uint64_t value, uint64_t symsize,
855                         elfcpp::STT type, elfcpp::STB binding,
856                         elfcpp::STV visibility, unsigned char nonvis,
857                         bool offset_is_from_end, bool only_if_ref);
858
859   // Define a special symbol based on an Output_segment.  It is a
860   // multiple definition error if this symbol is already defined.
861   Symbol*
862   define_in_output_segment(const Target*, const char* name,
863                            const char* version, Output_segment*,
864                            uint64_t value, uint64_t symsize,
865                            elfcpp::STT type, elfcpp::STB binding,
866                            elfcpp::STV visibility, unsigned char nonvis,
867                            Symbol::Segment_offset_base, bool only_if_ref);
868
869   // Define a special symbol with a constant value.  It is a multiple
870   // definition error if this symbol is already defined.
871   Symbol*
872   define_as_constant(const Target*, const char* name, const char* version,
873                      uint64_t value, uint64_t symsize, elfcpp::STT type,
874                      elfcpp::STB binding, elfcpp::STV visibility,
875                      unsigned char nonvis, bool only_if_ref);
876
877   // Define a set of symbols in output sections.
878   void
879   define_symbols(const Layout*, const Target*, int count,
880                  const Define_symbol_in_section*);
881
882   // Define a set of symbols in output segments.
883   void
884   define_symbols(const Layout*, const Target*, int count,
885                  const Define_symbol_in_segment*);  
886
887   // Define SYM using a COPY reloc.  POSD is the Output_data where the
888   // symbol should be defined--typically a .dyn.bss section.  VALUE is
889   // the offset within POSD.
890   template<int size>
891   void
892   define_with_copy_reloc(const Target*, Sized_symbol<size>* sym,
893                          Output_data* posd, uint64_t value);
894
895   // Look up a symbol.
896   Symbol*
897   lookup(const char*, const char* version = NULL) const;
898
899   // Return the real symbol associated with the forwarder symbol FROM.
900   Symbol*
901   resolve_forwards(const Symbol* from) const;
902
903   // Return the sized version of a symbol in this table.
904   template<int size>
905   Sized_symbol<size>*
906   get_sized_symbol(Symbol* ACCEPT_SIZE) const;
907
908   template<int size>
909   const Sized_symbol<size>*
910   get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
911
912   // Return the count of undefined symbols seen.
913   int
914   saw_undefined() const
915   { return this->saw_undefined_; }
916
917   // Allocate the common symbols
918   void
919   allocate_commons(const General_options&, Layout*);
920
921   // Add a warning for symbol NAME in section SHNDX in object OBJ.
922   void
923   add_warning(const char* name, Object* obj, unsigned int shndx)
924   { this->warnings_.add_warning(this, name, obj, shndx); }
925
926   // Canonicalize a symbol name for use in the hash table.
927   const char*
928   canonicalize_name(const char* name)
929   { return this->namepool_.add(name, true, NULL); }
930
931   // Possibly issue a warning for a reference to SYM at LOCATION which
932   // is in OBJ.
933   template<int size, bool big_endian>
934   void
935   issue_warning(const Symbol* sym,
936                 const Relocate_info<size, big_endian>* relinfo,
937                 size_t relnum, off_t reloffset) const
938   { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
939
940   // SYM is defined using a COPY reloc.  Return the dynamic object
941   // where the original definition was found.
942   Dynobj*
943   get_copy_source(const Symbol* sym) const;
944
945   // Set the dynamic symbol indexes.  INDEX is the index of the first
946   // global dynamic symbol.  Pointers to the symbols are stored into
947   // the vector.  The names are stored into the Stringpool.  This
948   // returns an updated dynamic symbol index.
949   unsigned int
950   set_dynsym_indexes(const General_options*, const Target*, unsigned int index,
951                      std::vector<Symbol*>*, Stringpool*, Versions*);
952
953   // Finalize the symbol table after we have set the final addresses
954   // of all the input sections.  This sets the final symbol indexes,
955   // values and adds the names to *POOL.  INDEX is the index of the
956   // first global symbol.  OFF is the file offset of the global symbol
957   // table, DYNOFF is the offset of the globals in the dynamic symbol
958   // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
959   // symbol, and DYNCOUNT is the number of global dynamic symbols.
960   // This records the parameters, and returns the new file offset.
961   off_t
962   finalize(unsigned int index, off_t off, off_t dynoff,
963            size_t dyn_global_index, size_t dyncount, Stringpool* pool);
964
965   // Write out the global symbols.
966   void
967   write_globals(const Target*, const Stringpool*, const Stringpool*,
968                 Output_file*) const;
969
970   // Write out a section symbol.  Return the updated offset.
971   void
972   write_section_symbol(const Output_section*, Output_file*, off_t) const;
973
974  private:
975   Symbol_table(const Symbol_table&);
976   Symbol_table& operator=(const Symbol_table&);
977
978   // Make FROM a forwarder symbol to TO.
979   void
980   make_forwarder(Symbol* from, Symbol* to);
981
982   // Add a symbol.
983   template<int size, bool big_endian>
984   Sized_symbol<size>*
985   add_from_object(Object*, const char *name, Stringpool::Key name_key,
986                   const char *version, Stringpool::Key version_key,
987                   bool def, const elfcpp::Sym<size, big_endian>& sym);
988
989   // Resolve symbols.
990   template<int size, bool big_endian>
991   void
992   resolve(Sized_symbol<size>* to,
993           const elfcpp::Sym<size, big_endian>& sym,
994           Object*, const char* version);
995
996   template<int size, bool big_endian>
997   void
998   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
999           const char* version ACCEPT_SIZE_ENDIAN);
1000
1001   // Whether we should override a symbol, based on flags in
1002   // resolve.cc.
1003   static bool
1004   should_override(const Symbol*, unsigned int, Object*, bool*);
1005
1006   // Override a symbol.
1007   template<int size, bool big_endian>
1008   void
1009   override(Sized_symbol<size>* tosym,
1010            const elfcpp::Sym<size, big_endian>& fromsym,
1011            Object* object, const char* version);
1012
1013   // Whether we should override a symbol with a special symbol which
1014   // is automatically defined by the linker.
1015   static bool
1016   should_override_with_special(const Symbol*);
1017
1018   // Override a symbol with a special symbol.
1019   template<int size>
1020   void
1021   override_with_special(Sized_symbol<size>* tosym,
1022                         const Sized_symbol<size>* fromsym);
1023
1024   // Record all weak alias sets for a dynamic object.
1025   template<int size>
1026   void
1027   record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1028
1029   // Define a special symbol.
1030   template<int size, bool big_endian>
1031   Sized_symbol<size>*
1032   define_special_symbol(const Target* target, const char** pname,
1033                         const char** pversion, bool only_if_ref,
1034                         Sized_symbol<size>** poldsym ACCEPT_SIZE_ENDIAN);
1035
1036   // Define a symbol in an Output_data, sized version.
1037   template<int size>
1038   Sized_symbol<size>*
1039   do_define_in_output_data(const Target*, const char* name,
1040                            const char* version, Output_data*,
1041                            typename elfcpp::Elf_types<size>::Elf_Addr value,
1042                            typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1043                            elfcpp::STT type, elfcpp::STB binding,
1044                            elfcpp::STV visibility, unsigned char nonvis,
1045                            bool offset_is_from_end, bool only_if_ref);
1046
1047   // Define a symbol in an Output_segment, sized version.
1048   template<int size>
1049   Sized_symbol<size>*
1050   do_define_in_output_segment(
1051     const Target*, const char* name, const char* version, Output_segment* os,
1052     typename elfcpp::Elf_types<size>::Elf_Addr value,
1053     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1054     elfcpp::STT type, elfcpp::STB binding,
1055     elfcpp::STV visibility, unsigned char nonvis,
1056     Symbol::Segment_offset_base offset_base, bool only_if_ref);
1057
1058   // Define a symbol as a constant, sized version.
1059   template<int size>
1060   Sized_symbol<size>*
1061   do_define_as_constant(
1062     const Target*, const char* name, const char* version,
1063     typename elfcpp::Elf_types<size>::Elf_Addr value,
1064     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1065     elfcpp::STT type, elfcpp::STB binding,
1066     elfcpp::STV visibility, unsigned char nonvis,
1067     bool only_if_ref);
1068
1069   // Allocate the common symbols, sized version.
1070   template<int size>
1071   void
1072   do_allocate_commons(const General_options&, Layout*);
1073
1074   // Finalize symbols specialized for size.
1075   template<int size>
1076   off_t
1077   sized_finalize(unsigned int, off_t, Stringpool*);
1078
1079   // Write globals specialized for size and endianness.
1080   template<int size, bool big_endian>
1081   void
1082   sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
1083                       Output_file*) const;
1084
1085   // Write out a symbol to P.
1086   template<int size, bool big_endian>
1087   void
1088   sized_write_symbol(Sized_symbol<size>*,
1089                      typename elfcpp::Elf_types<size>::Elf_Addr value,
1090                      unsigned int shndx,
1091                      const Stringpool*, unsigned char* p
1092                      ACCEPT_SIZE_ENDIAN) const;
1093
1094   // Write out a section symbol, specialized for size and endianness.
1095   template<int size, bool big_endian>
1096   void
1097   sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1098
1099   // The type of the symbol hash table.
1100
1101   typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1102
1103   struct Symbol_table_hash
1104   {
1105     size_t
1106     operator()(const Symbol_table_key&) const;
1107   };
1108
1109   struct Symbol_table_eq
1110   {
1111     bool
1112     operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1113   };
1114
1115   typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1116                         Symbol_table_eq> Symbol_table_type;
1117
1118   // The type of the list of common symbols.
1119   typedef std::vector<Symbol*> Commons_type;
1120
1121   // A map from symbols with COPY relocs to the dynamic objects where
1122   // they are defined.
1123   typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1124
1125   // We increment this every time we see a new undefined symbol, for
1126   // use in archive groups.
1127   int saw_undefined_;
1128   // The index of the first global symbol in the output file.
1129   unsigned int first_global_index_;
1130   // The file offset within the output symtab section where we should
1131   // write the table.
1132   off_t offset_;
1133   // The number of global symbols we want to write out.
1134   size_t output_count_;
1135   // The file offset of the global dynamic symbols, or 0 if none.
1136   off_t dynamic_offset_;
1137   // The index of the first global dynamic symbol.
1138   unsigned int first_dynamic_global_index_;
1139   // The number of global dynamic symbols, or 0 if none.
1140   off_t dynamic_count_;
1141   // The symbol hash table.
1142   Symbol_table_type table_;
1143   // A pool of symbol names.  This is used for all global symbols.
1144   // Entries in the hash table point into this pool.
1145   Stringpool namepool_;
1146   // Forwarding symbols.
1147   Unordered_map<const Symbol*, Symbol*> forwarders_;
1148   // Weak aliases.  A symbol in this list points to the next alias.
1149   // The aliases point to each other in a circular list.
1150   Unordered_map<Symbol*, Symbol*> weak_aliases_;
1151   // We don't expect there to be very many common symbols, so we keep
1152   // a list of them.  When we find a common symbol we add it to this
1153   // list.  It is possible that by the time we process the list the
1154   // symbol is no longer a common symbol.  It may also have become a
1155   // forwarder.
1156   Commons_type commons_;
1157   // Manage symbol warnings.
1158   Warnings warnings_;
1159   // When we emit a COPY reloc for a symbol, we define it in an
1160   // Output_data.  When it's time to emit version information for it,
1161   // we need to know the dynamic object in which we found the original
1162   // definition.  This maps symbols with COPY relocs to the dynamic
1163   // object where they were defined.
1164   Copied_symbol_dynobjs copied_symbol_dynobjs_;
1165 };
1166
1167 // We inline get_sized_symbol for efficiency.
1168
1169 template<int size>
1170 Sized_symbol<size>*
1171 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1172 {
1173   gold_assert(size == parameters->get_size());
1174   return static_cast<Sized_symbol<size>*>(sym);
1175 }
1176
1177 template<int size>
1178 const Sized_symbol<size>*
1179 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1180 {
1181   gold_assert(size == parameters->get_size());
1182   return static_cast<const Sized_symbol<size>*>(sym);
1183 }
1184
1185 } // End namespace gold.
1186
1187 #endif // !defined(GOLD_SYMTAB_H)