From Cary Coutant: Improve i386 shared library TLS support.
[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 Input_objects;
50 class Output_data;
51 class Output_section;
52 class Output_segment;
53 class Output_file;
54 class Target;
55
56 // The base class of an entry in the symbol table.  The symbol table
57 // can have a lot of entries, so we don't want this class to big.
58 // Size dependent fields can be found in the template class
59 // Sized_symbol.  Targets may support their own derived classes.
60
61 class Symbol
62 {
63  public:
64   // Because we want the class to be small, we don't use any virtual
65   // functions.  But because symbols can be defined in different
66   // places, we need to classify them.  This enum is the different
67   // sources of symbols we support.
68   enum Source
69   {
70     // Symbol defined in a relocatable or dynamic input file--this is
71     // the most common case.
72     FROM_OBJECT,
73     // Symbol defined in an Output_data, a special section created by
74     // the target.
75     IN_OUTPUT_DATA,
76     // Symbol defined in an Output_segment, with no associated
77     // section.
78     IN_OUTPUT_SEGMENT,
79     // Symbol value is constant.
80     CONSTANT
81   };
82
83   // When the source is IN_OUTPUT_SEGMENT, we need to describe what
84   // the offset means.
85   enum Segment_offset_base
86   {
87     // From the start of the segment.
88     SEGMENT_START,
89     // From the end of the segment.
90     SEGMENT_END,
91     // From the filesz of the segment--i.e., after the loaded bytes
92     // but before the bytes which are allocated but zeroed.
93     SEGMENT_BSS
94   };
95
96   // Return the symbol name.
97   const char*
98   name() const
99   { return this->name_; }
100
101   // Return the symbol version.  This will return NULL for an
102   // unversioned symbol.
103   const char*
104   version() const
105   { return this->version_; }
106
107   // Return the symbol source.
108   Source
109   source() const
110   { return this->source_; }
111
112   // Return the object with which this symbol is associated.
113   Object*
114   object() const
115   {
116     gold_assert(this->source_ == FROM_OBJECT);
117     return this->u_.from_object.object;
118   }
119
120   // Return the index of the section in the input relocatable or
121   // dynamic object file.
122   unsigned int
123   shndx() const
124   {
125     gold_assert(this->source_ == FROM_OBJECT);
126     return this->u_.from_object.shndx;
127   }
128
129   // Return the output data section with which this symbol is
130   // associated, if the symbol was specially defined with respect to
131   // an output data section.
132   Output_data*
133   output_data() const
134   {
135     gold_assert(this->source_ == IN_OUTPUT_DATA);
136     return this->u_.in_output_data.output_data;
137   }
138
139   // If this symbol was defined with respect to an output data
140   // section, return whether the value is an offset from end.
141   bool
142   offset_is_from_end() const
143   {
144     gold_assert(this->source_ == IN_OUTPUT_DATA);
145     return this->u_.in_output_data.offset_is_from_end;
146   }
147
148   // Return the output segment with which this symbol is associated,
149   // if the symbol was specially defined with respect to an output
150   // segment.
151   Output_segment*
152   output_segment() const
153   {
154     gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
155     return this->u_.in_output_segment.output_segment;
156   }
157
158   // If this symbol was defined with respect to an output segment,
159   // return the offset base.
160   Segment_offset_base
161   offset_base() const
162   {
163     gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
164     return this->u_.in_output_segment.offset_base;
165   }
166
167   // Return the symbol binding.
168   elfcpp::STB
169   binding() const
170   { return this->binding_; }
171
172   // Return the symbol type.
173   elfcpp::STT
174   type() const
175   { return this->type_; }
176
177   // Return the symbol visibility.
178   elfcpp::STV
179   visibility() const
180   { return this->visibility_; }
181
182   // Return the non-visibility part of the st_other field.
183   unsigned char
184   nonvis() const
185   { return this->nonvis_; }
186
187   // Return whether this symbol is a forwarder.  This will never be
188   // true of a symbol found in the hash table, but may be true of
189   // symbol pointers attached to object files.
190   bool
191   is_forwarder() const
192   { return this->is_forwarder_; }
193
194   // Mark this symbol as a forwarder.
195   void
196   set_forwarder()
197   { this->is_forwarder_ = true; }
198
199   // Return whether this symbol has an alias in the weak aliases table
200   // in Symbol_table.
201   bool
202   has_alias() const
203   { return this->has_alias_; }
204
205   // Mark this symbol as having an alias.
206   void
207   set_has_alias()
208   { this->has_alias_ = true; }
209
210   // Return whether this symbol needs an entry in the dynamic symbol
211   // table.
212   bool
213   needs_dynsym_entry() const
214   {
215     return (this->needs_dynsym_entry_
216             || (this->in_reg() && this->in_dyn()));
217   }
218
219   // Mark this symbol as needing an entry in the dynamic symbol table.
220   void
221   set_needs_dynsym_entry()
222   { this->needs_dynsym_entry_ = true; }
223
224   // Return whether this symbol should be added to the dynamic symbol
225   // table.
226   bool
227   should_add_dynsym_entry() const;
228
229   // Return whether this symbol has been seen in a regular object.
230   bool
231   in_reg() const
232   { return this->in_reg_; }
233
234   // Mark this symbol as having been seen in a regular object.
235   void
236   set_in_reg()
237   { this->in_reg_ = true; }
238
239   // Return whether this symbol has been seen in a dynamic object.
240   bool
241   in_dyn() const
242   { return this->in_dyn_; }
243
244   // Mark this symbol as having been seen in a dynamic object.
245   void
246   set_in_dyn()
247   { this->in_dyn_ = true; }
248
249   // Return the index of this symbol in the output file symbol table.
250   // A value of -1U means that this symbol is not going into the
251   // output file.  This starts out as zero, and is set to a non-zero
252   // value by Symbol_table::finalize.  It is an error to ask for the
253   // symbol table index before it has been set.
254   unsigned int
255   symtab_index() const
256   {
257     gold_assert(this->symtab_index_ != 0);
258     return this->symtab_index_;
259   }
260
261   // Set the index of the symbol in the output file symbol table.
262   void
263   set_symtab_index(unsigned int index)
264   {
265     gold_assert(index != 0);
266     this->symtab_index_ = index;
267   }
268
269   // Return whether this symbol already has an index in the output
270   // file symbol table.
271   bool
272   has_symtab_index() const
273   { return this->symtab_index_ != 0; }
274
275   // Return the index of this symbol in the dynamic symbol table.  A
276   // value of -1U means that this symbol is not going into the dynamic
277   // symbol table.  This starts out as zero, and is set to a non-zero
278   // during Layout::finalize.  It is an error to ask for the dynamic
279   // symbol table index before it has been set.
280   unsigned int
281   dynsym_index() const
282   {
283     gold_assert(this->dynsym_index_ != 0);
284     return this->dynsym_index_;
285   }
286
287   // Set the index of the symbol in the dynamic symbol table.
288   void
289   set_dynsym_index(unsigned int index)
290   {
291     gold_assert(index != 0);
292     this->dynsym_index_ = index;
293   }
294
295   // Return whether this symbol already has an index in the dynamic
296   // symbol table.
297   bool
298   has_dynsym_index() const
299   { return this->dynsym_index_ != 0; }
300
301   // Return whether this symbol has an entry in the GOT section.
302   // For a TLS symbol, this GOT entry will hold its tp-relative offset.
303   bool
304   has_got_offset() const
305   { return this->has_got_offset_; }
306
307   // Return the offset into the GOT section of this symbol.
308   unsigned int
309   got_offset() const
310   {
311     gold_assert(this->has_got_offset());
312     return this->got_offset_;
313   }
314
315   // Set the GOT offset of this symbol.
316   void
317   set_got_offset(unsigned int got_offset)
318   {
319     this->has_got_offset_ = true;
320     this->got_offset_ = got_offset;
321   }
322
323   // Return whether this TLS symbol has an entry in the GOT section for
324   // its module index or, if NEED_PAIR is true, has a pair of entries
325   // for its module index and dtv-relative offset.
326   bool
327   has_tls_got_offset(bool need_pair) const
328   {
329     return (this->has_tls_mod_got_offset_
330             && (!need_pair || this->has_tls_pair_got_offset_));
331   }
332
333   // Return the offset into the GOT section for this symbol's TLS module
334   // index or, if NEED_PAIR is true, for the pair of entries for the
335   // module index and dtv-relative offset.
336   unsigned int
337   tls_got_offset(bool need_pair) const
338   {
339     gold_assert(this->has_tls_got_offset(need_pair));
340     return this->tls_mod_got_offset_;
341   }
342
343   // Set the GOT offset of this symbol.
344   void
345   set_tls_got_offset(unsigned int got_offset, bool have_pair)
346   {
347     this->has_tls_mod_got_offset_ = true;
348     this->has_tls_pair_got_offset_ = have_pair;
349     this->tls_mod_got_offset_ = got_offset;
350   }
351
352   // Return whether this symbol has an entry in the PLT section.
353   bool
354   has_plt_offset() const
355   { return this->has_plt_offset_; }
356
357   // Return the offset into the PLT section of this symbol.
358   unsigned int
359   plt_offset() const
360   {
361     gold_assert(this->has_plt_offset());
362     return this->plt_offset_;
363   }
364
365   // Set the PLT offset of this symbol.
366   void
367   set_plt_offset(unsigned int plt_offset)
368   {
369     this->has_plt_offset_ = true;
370     this->plt_offset_ = plt_offset;
371   }
372
373   // Return whether this dynamic symbol needs a special value in the
374   // dynamic symbol table.
375   bool
376   needs_dynsym_value() const
377   { return this->needs_dynsym_value_; }
378
379   // Set that this dynamic symbol needs a special value in the dynamic
380   // symbol table.
381   void
382   set_needs_dynsym_value()
383   {
384     gold_assert(this->object()->is_dynamic());
385     this->needs_dynsym_value_ = true;
386   }
387
388   // Return true if the final value of this symbol is known at link
389   // time.
390   bool
391   final_value_is_known() const;
392
393   // Return whether this is a defined symbol (not undefined or
394   // common).
395   bool
396   is_defined() const
397   {
398     return (this->source_ != FROM_OBJECT
399             || (this->shndx() != elfcpp::SHN_UNDEF
400                 && this->shndx() != elfcpp::SHN_COMMON));
401   }
402
403   // Return true if this symbol is from a dynamic object.
404   bool
405   is_from_dynobj() const
406   {
407     return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
408   }
409
410   // Return whether this is an undefined symbol.
411   bool
412   is_undefined() const
413   {
414     return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
415   }
416
417   // Return whether this is a common symbol.
418   bool
419   is_common() const
420   {
421     return (this->source_ == FROM_OBJECT
422             && (this->shndx() == elfcpp::SHN_COMMON
423                 || this->type_ == elfcpp::STT_COMMON));
424   }
425
426   // Return whether this symbol can be seen outside this object.
427   bool
428   is_externally_visible() const
429   {
430     return (this->visibility_ == elfcpp::STV_DEFAULT
431             || this->visibility_ == elfcpp::STV_PROTECTED);
432   }
433
434   // Return true if this symbol can be preempted by a definition in
435   // another link unit.
436   bool
437   is_preemptible() const
438   {
439     // It doesn't make sense to ask whether a symbol defined in
440     // another object is preemptible.
441     gold_assert(!this->is_from_dynobj());
442
443     return (this->visibility_ != elfcpp::STV_INTERNAL
444             && this->visibility_ != elfcpp::STV_HIDDEN
445             && this->visibility_ != elfcpp::STV_PROTECTED
446             && parameters->output_is_shared()
447             && !parameters->symbolic());
448   }
449
450   // Return true if this symbol is a function that needs a PLT entry.
451   // If the symbol is defined in a dynamic object or if it is subject
452   // to pre-emption, we need to make a PLT entry.
453   bool
454   needs_plt_entry() const
455   {
456     return (this->type() == elfcpp::STT_FUNC
457             && (this->is_from_dynobj() || this->is_preemptible()));
458   }
459
460   // Given a direct absolute or pc-relative static relocation against
461   // the global symbol, this function returns whether a dynamic relocation
462   // is needed.
463
464   bool
465   needs_dynamic_reloc(bool is_absolute_ref, bool is_function_call) const
466   {
467     // An absolute reference within a position-independent output file
468     // will need a dynamic relocaion.
469     if (is_absolute_ref && parameters->output_is_position_independent())
470       return true;
471
472     // A function call that can branch to a local PLT entry does not need
473     // a dynamic relocation.
474     if (is_function_call && this->has_plt_offset())
475       return false;
476
477     // A reference to any PLT entry in a non-position-independent executable
478     // does not need a dynamic relocation.
479     if (!parameters->output_is_position_independent()
480         && this->has_plt_offset())
481       return false;
482
483     // A reference to a symbol defined in a dynamic object or to a
484     // symbol that is preemptible will need a dynamic relocation.
485     if (this->is_from_dynobj() || this->is_preemptible())
486       return true;
487
488     // For all other cases, return FALSE.
489     return false;
490   }
491
492   // Given a direct absolute static relocation against
493   // the global symbol, where a dynamic relocation is needed, this
494   // function returns whether a relative dynamic relocation can be used.
495   // The caller must determine separately whether the static relocation
496   // is compatible with a relative relocation.
497
498   bool
499   can_use_relative_reloc(bool is_function_call) const
500   {
501     // A function call that can branch to a local PLT entry can
502     // use a RELATIVE relocation.
503     if (is_function_call && this->has_plt_offset())
504       return true;
505
506     // A reference to a symbol defined in a dynamic object or to a
507     // symbol that is preemptible can not use a RELATIVE relocaiton.
508     if (this->is_from_dynobj() || this->is_preemptible())
509       return false;
510
511     // For all other cases, return TRUE.
512     return true;
513   }
514
515   // Return whether there should be a warning for references to this
516   // symbol.
517   bool
518   has_warning() const
519   { return this->has_warning_; }
520
521   // Mark this symbol as having a warning.
522   void
523   set_has_warning()
524   { this->has_warning_ = true; }
525
526   // Return whether this symbol is defined by a COPY reloc from a
527   // dynamic object.
528   bool
529   is_copied_from_dynobj() const
530   { return this->is_copied_from_dynobj_; }
531
532   // Mark this symbol as defined by a COPY reloc.
533   void
534   set_is_copied_from_dynobj()
535   { this->is_copied_from_dynobj_ = true; }
536
537   // Mark this symbol as needing its value written to the GOT even when
538   // the value is subject to dynamic relocation (e.g., when the target
539   // uses a RELATIVE relocation for the GOT entry).
540   void
541   set_needs_value_in_got()
542   { this->needs_value_in_got_ = true; }
543
544   // Return whether this symbol needs its value written to the GOT even
545   // when the value is subject to dynamic relocation.
546   bool
547   needs_value_in_got() const
548   { return this->needs_value_in_got_; }
549
550  protected:
551   // Instances of this class should always be created at a specific
552   // size.
553   Symbol()
554   { memset(this, 0, sizeof *this); }
555
556   // Initialize the general fields.
557   void
558   init_fields(const char* name, const char* version,
559               elfcpp::STT type, elfcpp::STB binding,
560               elfcpp::STV visibility, unsigned char nonvis);
561
562   // Initialize fields from an ELF symbol in OBJECT.
563   template<int size, bool big_endian>
564   void
565   init_base(const char *name, const char* version, Object* object,
566             const elfcpp::Sym<size, big_endian>&);
567
568   // Initialize fields for an Output_data.
569   void
570   init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
571             elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
572
573   // Initialize fields for an Output_segment.
574   void
575   init_base(const char* name, Output_segment* os, elfcpp::STT type,
576             elfcpp::STB binding, elfcpp::STV visibility,
577             unsigned char nonvis, Segment_offset_base offset_base);
578
579   // Initialize fields for a constant.
580   void
581   init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
582             elfcpp::STV visibility, unsigned char nonvis);
583
584   // Override existing symbol.
585   template<int size, bool big_endian>
586   void
587   override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
588                 const char* version);
589
590   // Override existing symbol with a special symbol.
591   void
592   override_base_with_special(const Symbol* from);
593
594  private:
595   Symbol(const Symbol&);
596   Symbol& operator=(const Symbol&);
597
598   // Symbol name (expected to point into a Stringpool).
599   const char* name_;
600   // Symbol version (expected to point into a Stringpool).  This may
601   // be NULL.
602   const char* version_;
603
604   union
605   {
606     // This struct is used if SOURCE_ == FROM_OBJECT.
607     struct
608     {
609       // Object in which symbol is defined, or in which it was first
610       // seen.
611       Object* object;
612       // Section number in object_ in which symbol is defined.
613       unsigned int shndx;
614     } from_object;
615
616     // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
617     struct
618     {
619       // Output_data in which symbol is defined.  Before
620       // Layout::finalize the symbol's value is an offset within the
621       // Output_data.
622       Output_data* output_data;
623       // True if the offset is from the end, false if the offset is
624       // from the beginning.
625       bool offset_is_from_end;
626     } in_output_data;
627
628     // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
629     struct
630     {
631       // Output_segment in which the symbol is defined.  Before
632       // Layout::finalize the symbol's value is an offset.
633       Output_segment* output_segment;
634       // The base to use for the offset before Layout::finalize.
635       Segment_offset_base offset_base;
636     } in_output_segment;
637   } u_;
638
639   // The index of this symbol in the output file.  If the symbol is
640   // not going into the output file, this value is -1U.  This field
641   // starts as always holding zero.  It is set to a non-zero value by
642   // Symbol_table::finalize.
643   unsigned int symtab_index_;
644
645   // The index of this symbol in the dynamic symbol table.  If the
646   // symbol is not going into the dynamic symbol table, this value is
647   // -1U.  This field starts as always holding zero.  It is set to a
648   // non-zero value during Layout::finalize.
649   unsigned int dynsym_index_;
650
651   // If this symbol has an entry in the GOT section (has_got_offset_
652   // is true), this is the offset from the start of the GOT section.
653   // For a TLS symbol, if has_tls_tpoff_got_offset_ is true, this
654   // serves as the GOT offset for the GOT entry that holds its
655   // TP-relative offset.
656   unsigned int got_offset_;
657
658   // If this is a TLS symbol and has an entry in the GOT section
659   // for a module index or a pair of entries (module index,
660   // dtv-relative offset), these are the offsets from the start
661   // of the GOT section.
662   unsigned int tls_mod_got_offset_;
663   unsigned int tls_pair_got_offset_;
664
665   // If this symbol has an entry in the PLT section (has_plt_offset_
666   // is true), then this is the offset from the start of the PLT
667   // section.
668   unsigned int plt_offset_;
669
670   // Symbol type.
671   elfcpp::STT type_ : 4;
672   // Symbol binding.
673   elfcpp::STB binding_ : 4;
674   // Symbol visibility.
675   elfcpp::STV visibility_ : 2;
676   // Rest of symbol st_other field.
677   unsigned int nonvis_ : 6;
678   // The type of symbol.
679   Source source_ : 3;
680   // True if this symbol always requires special target-specific
681   // handling.
682   bool is_target_special_ : 1;
683   // True if this is the default version of the symbol.
684   bool is_def_ : 1;
685   // True if this symbol really forwards to another symbol.  This is
686   // used when we discover after the fact that two different entries
687   // in the hash table really refer to the same symbol.  This will
688   // never be set for a symbol found in the hash table, but may be set
689   // for a symbol found in the list of symbols attached to an Object.
690   // It forwards to the symbol found in the forwarders_ map of
691   // Symbol_table.
692   bool is_forwarder_ : 1;
693   // True if the symbol has an alias in the weak_aliases table in
694   // Symbol_table.
695   bool has_alias_ : 1;
696   // True if this symbol needs to be in the dynamic symbol table.
697   bool needs_dynsym_entry_ : 1;
698   // True if we've seen this symbol in a regular object.
699   bool in_reg_ : 1;
700   // True if we've seen this symbol in a dynamic object.
701   bool in_dyn_ : 1;
702   // True if the symbol has an entry in the GOT section.
703   // For a TLS symbol, this GOT entry will hold its tp-relative offset.
704   bool has_got_offset_ : 1;
705   // True if the symbol has an entry in the GOT section for its
706   // module index.
707   bool has_tls_mod_got_offset_ : 1;
708   // True if the symbol has a pair of entries in the GOT section for its
709   // module index and dtv-relative offset.
710   bool has_tls_pair_got_offset_ : 1;
711   // True if the symbol has an entry in the PLT section.
712   bool has_plt_offset_ : 1;
713   // True if this is a dynamic symbol which needs a special value in
714   // the dynamic symbol table.
715   bool needs_dynsym_value_ : 1;
716   // True if there is a warning for this symbol.
717   bool has_warning_ : 1;
718   // True if we are using a COPY reloc for this symbol, so that the
719   // real definition lives in a dynamic object.
720   bool is_copied_from_dynobj_ : 1;
721   // True if the static value should be written to the GOT even
722   // when the final value is subject to dynamic relocation.
723   bool needs_value_in_got_ : 1;
724 };
725
726 // The parts of a symbol which are size specific.  Using a template
727 // derived class like this helps us use less space on a 32-bit system.
728
729 template<int size>
730 class Sized_symbol : public Symbol
731 {
732  public:
733   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
734   typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
735
736   Sized_symbol()
737   { }
738
739   // Initialize fields from an ELF symbol in OBJECT.
740   template<bool big_endian>
741   void
742   init(const char *name, const char* version, Object* object,
743        const elfcpp::Sym<size, big_endian>&);
744
745   // Initialize fields for an Output_data.
746   void
747   init(const char* name, Output_data*, Value_type value, Size_type symsize,
748        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
749        bool offset_is_from_end);
750
751   // Initialize fields for an Output_segment.
752   void
753   init(const char* name, Output_segment*, Value_type value, Size_type symsize,
754        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
755        Segment_offset_base offset_base);
756
757   // Initialize fields for a constant.
758   void
759   init(const char* name, Value_type value, Size_type symsize,
760        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
761
762   // Override existing symbol.
763   template<bool big_endian>
764   void
765   override(const elfcpp::Sym<size, big_endian>&, Object* object,
766            const char* version);
767
768   // Override existing symbol with a special symbol.
769   void
770   override_with_special(const Sized_symbol<size>*);
771
772   // Return the symbol's value.
773   Value_type
774   value() const
775   { return this->value_; }
776
777   // Return the symbol's size (we can't call this 'size' because that
778   // is a template parameter).
779   Size_type
780   symsize() const
781   { return this->symsize_; }
782
783   // Set the symbol size.  This is used when resolving common symbols.
784   void
785   set_symsize(Size_type symsize)
786   { this->symsize_ = symsize; }
787
788   // Set the symbol value.  This is called when we store the final
789   // values of the symbols into the symbol table.
790   void
791   set_value(Value_type value)
792   { this->value_ = value; }
793
794  private:
795   Sized_symbol(const Sized_symbol&);
796   Sized_symbol& operator=(const Sized_symbol&);
797
798   // Symbol value.  Before Layout::finalize this is the offset in the
799   // input section.  This is set to the final value during
800   // Layout::finalize.
801   Value_type value_;
802   // Symbol size.
803   Size_type symsize_;
804 };
805
806 // A struct describing a symbol defined by the linker, where the value
807 // of the symbol is defined based on an output section.  This is used
808 // for symbols defined by the linker, like "_init_array_start".
809
810 struct Define_symbol_in_section
811 {
812   // The symbol name.
813   const char* name;
814   // The name of the output section with which this symbol should be
815   // associated.  If there is no output section with that name, the
816   // symbol will be defined as zero.
817   const char* output_section;
818   // The offset of the symbol within the output section.  This is an
819   // offset from the start of the output section, unless start_at_end
820   // is true, in which case this is an offset from the end of the
821   // output section.
822   uint64_t value;
823   // The size of the symbol.
824   uint64_t size;
825   // The symbol type.
826   elfcpp::STT type;
827   // The symbol binding.
828   elfcpp::STB binding;
829   // The symbol visibility.
830   elfcpp::STV visibility;
831   // The rest of the st_other field.
832   unsigned char nonvis;
833   // If true, the value field is an offset from the end of the output
834   // section.
835   bool offset_is_from_end;
836   // If true, this symbol is defined only if we see a reference to it.
837   bool only_if_ref;
838 };
839
840 // A struct describing a symbol defined by the linker, where the value
841 // of the symbol is defined based on a segment.  This is used for
842 // symbols defined by the linker, like "_end".  We describe the
843 // segment with which the symbol should be associated by its
844 // characteristics.  If no segment meets these characteristics, the
845 // symbol will be defined as zero.  If there is more than one segment
846 // which meets these characteristics, we will use the first one.
847
848 struct Define_symbol_in_segment
849 {
850   // The symbol name.
851   const char* name;
852   // The segment type where the symbol should be defined, typically
853   // PT_LOAD.
854   elfcpp::PT segment_type;
855   // Bitmask of segment flags which must be set.
856   elfcpp::PF segment_flags_set;
857   // Bitmask of segment flags which must be clear.
858   elfcpp::PF segment_flags_clear;
859   // The offset of the symbol within the segment.  The offset is
860   // calculated from the position set by offset_base.
861   uint64_t value;
862   // The size of the symbol.
863   uint64_t size;
864   // The symbol type.
865   elfcpp::STT type;
866   // The symbol binding.
867   elfcpp::STB binding;
868   // The symbol visibility.
869   elfcpp::STV visibility;
870   // The rest of the st_other field.
871   unsigned char nonvis;
872   // The base from which we compute the offset.
873   Symbol::Segment_offset_base offset_base;
874   // If true, this symbol is defined only if we see a reference to it.
875   bool only_if_ref;
876 };
877
878 // This class manages warnings.  Warnings are a GNU extension.  When
879 // we see a section named .gnu.warning.SYM in an object file, and if
880 // we wind using the definition of SYM from that object file, then we
881 // will issue a warning for any relocation against SYM from a
882 // different object file.  The text of the warning is the contents of
883 // the section.  This is not precisely the definition used by the old
884 // GNU linker; the old GNU linker treated an occurrence of
885 // .gnu.warning.SYM as defining a warning symbol.  A warning symbol
886 // would trigger a warning on any reference.  However, it was
887 // inconsistent in that a warning in a dynamic object only triggered
888 // if there was no definition in a regular object.  This linker is
889 // different in that we only issue a warning if we use the symbol
890 // definition from the same object file as the warning section.
891
892 class Warnings
893 {
894  public:
895   Warnings()
896     : warnings_()
897   { }
898
899   // Add a warning for symbol NAME in section SHNDX in object OBJ.
900   void
901   add_warning(Symbol_table* symtab, const char* name, Object* obj,
902               unsigned int shndx);
903
904   // For each symbol for which we should give a warning, make a note
905   // on the symbol.
906   void
907   note_warnings(Symbol_table* symtab);
908
909   // Issue a warning for a reference to SYM at RELINFO's location.
910   template<int size, bool big_endian>
911   void
912   issue_warning(const Symbol* sym, const Relocate_info<size, big_endian>*,
913                 size_t relnum, off_t reloffset) const;
914
915  private:
916   Warnings(const Warnings&);
917   Warnings& operator=(const Warnings&);
918
919   // What we need to know to get the warning text.
920   struct Warning_location
921   {
922     // The object the warning is in.
923     Object* object;
924     // The index of the warning section.
925     unsigned int shndx;
926     // The warning text if we have already loaded it.
927     std::string text;
928
929     Warning_location()
930       : object(NULL), shndx(0), text()
931     { }
932
933     void
934     set(Object* o, unsigned int s)
935     {
936       this->object = o;
937       this->shndx = s;
938     }
939
940     void
941     set_text(const char* t, off_t l)
942     { this->text.assign(t, l); }
943   };
944
945   // A mapping from warning symbol names (canonicalized in
946   // Symbol_table's namepool_ field) to warning information.
947   typedef Unordered_map<const char*, Warning_location> Warning_table;
948
949   Warning_table warnings_;
950 };
951
952 // The main linker symbol table.
953
954 class Symbol_table
955 {
956  public:
957   Symbol_table();
958
959   ~Symbol_table();
960
961   // Add COUNT external symbols from the relocatable object RELOBJ to
962   // the symbol table.  SYMS is the symbols, SYM_NAMES is their names,
963   // SYM_NAME_SIZE is the size of SYM_NAMES.  This sets SYMPOINTERS to
964   // point to the symbols in the symbol table.
965   template<int size, bool big_endian>
966   void
967   add_from_relobj(Sized_relobj<size, big_endian>* relobj,
968                   const unsigned char* syms, size_t count,
969                   const char* sym_names, size_t sym_name_size,
970                   typename Sized_relobj<size, big_endian>::Symbols*);
971
972   // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
973   // symbol table.  SYMS is the symbols.  SYM_NAMES is their names.
974   // SYM_NAME_SIZE is the size of SYM_NAMES.  The other parameters are
975   // symbol version data.
976   template<int size, bool big_endian>
977   void
978   add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
979                   const unsigned char* syms, size_t count,
980                   const char* sym_names, size_t sym_name_size,
981                   const unsigned char* versym, size_t versym_size,
982                   const std::vector<const char*>*);
983
984   // Define a special symbol based on an Output_data.  It is a
985   // multiple definition error if this symbol is already defined.
986   Symbol*
987   define_in_output_data(const Target*, const char* name, const char* version,
988                         Output_data*, uint64_t value, uint64_t symsize,
989                         elfcpp::STT type, elfcpp::STB binding,
990                         elfcpp::STV visibility, unsigned char nonvis,
991                         bool offset_is_from_end, bool only_if_ref);
992
993   // Define a special symbol based on an Output_segment.  It is a
994   // multiple definition error if this symbol is already defined.
995   Symbol*
996   define_in_output_segment(const Target*, const char* name,
997                            const char* version, Output_segment*,
998                            uint64_t value, uint64_t symsize,
999                            elfcpp::STT type, elfcpp::STB binding,
1000                            elfcpp::STV visibility, unsigned char nonvis,
1001                            Symbol::Segment_offset_base, bool only_if_ref);
1002
1003   // Define a special symbol with a constant value.  It is a multiple
1004   // definition error if this symbol is already defined.
1005   Symbol*
1006   define_as_constant(const Target*, const char* name, const char* version,
1007                      uint64_t value, uint64_t symsize, elfcpp::STT type,
1008                      elfcpp::STB binding, elfcpp::STV visibility,
1009                      unsigned char nonvis, bool only_if_ref);
1010
1011   // Define a set of symbols in output sections.
1012   void
1013   define_symbols(const Layout*, const Target*, int count,
1014                  const Define_symbol_in_section*);
1015
1016   // Define a set of symbols in output segments.
1017   void
1018   define_symbols(const Layout*, const Target*, int count,
1019                  const Define_symbol_in_segment*);
1020
1021   // Define SYM using a COPY reloc.  POSD is the Output_data where the
1022   // symbol should be defined--typically a .dyn.bss section.  VALUE is
1023   // the offset within POSD.
1024   template<int size>
1025   void
1026   define_with_copy_reloc(const Target*, Sized_symbol<size>* sym,
1027                          Output_data* posd, uint64_t value);
1028
1029   // Look up a symbol.
1030   Symbol*
1031   lookup(const char*, const char* version = NULL) const;
1032
1033   // Return the real symbol associated with the forwarder symbol FROM.
1034   Symbol*
1035   resolve_forwards(const Symbol* from) const;
1036
1037   // Return the sized version of a symbol in this table.
1038   template<int size>
1039   Sized_symbol<size>*
1040   get_sized_symbol(Symbol* ACCEPT_SIZE) const;
1041
1042   template<int size>
1043   const Sized_symbol<size>*
1044   get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
1045
1046   // Return the count of undefined symbols seen.
1047   int
1048   saw_undefined() const
1049   { return this->saw_undefined_; }
1050
1051   // Allocate the common symbols
1052   void
1053   allocate_commons(const General_options&, Layout*);
1054
1055   // Add a warning for symbol NAME in section SHNDX in object OBJ.
1056   void
1057   add_warning(const char* name, Object* obj, unsigned int shndx)
1058   { this->warnings_.add_warning(this, name, obj, shndx); }
1059
1060   // Canonicalize a symbol name for use in the hash table.
1061   const char*
1062   canonicalize_name(const char* name)
1063   { return this->namepool_.add(name, true, NULL); }
1064
1065   // Possibly issue a warning for a reference to SYM at LOCATION which
1066   // is in OBJ.
1067   template<int size, bool big_endian>
1068   void
1069   issue_warning(const Symbol* sym,
1070                 const Relocate_info<size, big_endian>* relinfo,
1071                 size_t relnum, off_t reloffset) const
1072   { this->warnings_.issue_warning(sym, relinfo, relnum, reloffset); }
1073
1074   // Check candidate_odr_violations_ to find symbols with the same name
1075   // but apparently different definitions (different source-file/line-no).
1076   void
1077   detect_odr_violations(const char* output_file_name) const;
1078
1079   // SYM is defined using a COPY reloc.  Return the dynamic object
1080   // where the original definition was found.
1081   Dynobj*
1082   get_copy_source(const Symbol* sym) const;
1083
1084   // Set the dynamic symbol indexes.  INDEX is the index of the first
1085   // global dynamic symbol.  Pointers to the symbols are stored into
1086   // the vector.  The names are stored into the Stringpool.  This
1087   // returns an updated dynamic symbol index.
1088   unsigned int
1089   set_dynsym_indexes(const Target*, unsigned int index,
1090                      std::vector<Symbol*>*, Stringpool*, Versions*);
1091
1092   // Finalize the symbol table after we have set the final addresses
1093   // of all the input sections.  This sets the final symbol indexes,
1094   // values and adds the names to *POOL.  INDEX is the index of the
1095   // first global symbol.  OFF is the file offset of the global symbol
1096   // table, DYNOFF is the offset of the globals in the dynamic symbol
1097   // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
1098   // symbol, and DYNCOUNT is the number of global dynamic symbols.
1099   // This records the parameters, and returns the new file offset.
1100   off_t
1101   finalize(unsigned int index, off_t off, off_t dynoff,
1102            size_t dyn_global_index, size_t dyncount, Stringpool* pool);
1103
1104   // Write out the global symbols.
1105   void
1106   write_globals(const Input_objects*, const Stringpool*, const Stringpool*,
1107                 Output_file*) const;
1108
1109   // Write out a section symbol.  Return the updated offset.
1110   void
1111   write_section_symbol(const Output_section*, Output_file*, off_t) const;
1112
1113  private:
1114   Symbol_table(const Symbol_table&);
1115   Symbol_table& operator=(const Symbol_table&);
1116
1117   // Make FROM a forwarder symbol to TO.
1118   void
1119   make_forwarder(Symbol* from, Symbol* to);
1120
1121   // Add a symbol.
1122   template<int size, bool big_endian>
1123   Sized_symbol<size>*
1124   add_from_object(Object*, const char *name, Stringpool::Key name_key,
1125                   const char *version, Stringpool::Key version_key,
1126                   bool def, const elfcpp::Sym<size, big_endian>& sym,
1127                   const elfcpp::Sym<size, big_endian>& orig_sym);
1128
1129   // Resolve symbols.
1130   template<int size, bool big_endian>
1131   void
1132   resolve(Sized_symbol<size>* to,
1133           const elfcpp::Sym<size, big_endian>& sym,
1134           const elfcpp::Sym<size, big_endian>& orig_sym,
1135           Object*, const char* version);
1136
1137   template<int size, bool big_endian>
1138   void
1139   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
1140           const char* version ACCEPT_SIZE_ENDIAN);
1141
1142   // Whether we should override a symbol, based on flags in
1143   // resolve.cc.
1144   static bool
1145   should_override(const Symbol*, unsigned int, Object*, bool*);
1146
1147   // Override a symbol.
1148   template<int size, bool big_endian>
1149   void
1150   override(Sized_symbol<size>* tosym,
1151            const elfcpp::Sym<size, big_endian>& fromsym,
1152            Object* object, const char* version);
1153
1154   // Whether we should override a symbol with a special symbol which
1155   // is automatically defined by the linker.
1156   static bool
1157   should_override_with_special(const Symbol*);
1158
1159   // Override a symbol with a special symbol.
1160   template<int size>
1161   void
1162   override_with_special(Sized_symbol<size>* tosym,
1163                         const Sized_symbol<size>* fromsym);
1164
1165   // Record all weak alias sets for a dynamic object.
1166   template<int size>
1167   void
1168   record_weak_aliases(std::vector<Sized_symbol<size>*>*);
1169
1170   // Define a special symbol.
1171   template<int size, bool big_endian>
1172   Sized_symbol<size>*
1173   define_special_symbol(const Target* target, const char** pname,
1174                         const char** pversion, bool only_if_ref,
1175                         Sized_symbol<size>** poldsym ACCEPT_SIZE_ENDIAN);
1176
1177   // Define a symbol in an Output_data, sized version.
1178   template<int size>
1179   Sized_symbol<size>*
1180   do_define_in_output_data(const Target*, const char* name,
1181                            const char* version, Output_data*,
1182                            typename elfcpp::Elf_types<size>::Elf_Addr value,
1183                            typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1184                            elfcpp::STT type, elfcpp::STB binding,
1185                            elfcpp::STV visibility, unsigned char nonvis,
1186                            bool offset_is_from_end, bool only_if_ref);
1187
1188   // Define a symbol in an Output_segment, sized version.
1189   template<int size>
1190   Sized_symbol<size>*
1191   do_define_in_output_segment(
1192     const Target*, const char* name, const char* version, Output_segment* os,
1193     typename elfcpp::Elf_types<size>::Elf_Addr value,
1194     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1195     elfcpp::STT type, elfcpp::STB binding,
1196     elfcpp::STV visibility, unsigned char nonvis,
1197     Symbol::Segment_offset_base offset_base, bool only_if_ref);
1198
1199   // Define a symbol as a constant, sized version.
1200   template<int size>
1201   Sized_symbol<size>*
1202   do_define_as_constant(
1203     const Target*, const char* name, const char* version,
1204     typename elfcpp::Elf_types<size>::Elf_Addr value,
1205     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
1206     elfcpp::STT type, elfcpp::STB binding,
1207     elfcpp::STV visibility, unsigned char nonvis,
1208     bool only_if_ref);
1209
1210   // Allocate the common symbols, sized version.
1211   template<int size>
1212   void
1213   do_allocate_commons(const General_options&, Layout*);
1214
1215   // Implement detect_odr_violations.
1216   template<int size, bool big_endian>
1217   void
1218   sized_detect_odr_violations() const;
1219
1220   // Finalize symbols specialized for size.
1221   template<int size>
1222   off_t
1223   sized_finalize(unsigned int, off_t, Stringpool*);
1224
1225   // Write globals specialized for size and endianness.
1226   template<int size, bool big_endian>
1227   void
1228   sized_write_globals(const Input_objects*, const Stringpool*,
1229                       const Stringpool*, Output_file*) const;
1230
1231   // Write out a symbol to P.
1232   template<int size, bool big_endian>
1233   void
1234   sized_write_symbol(Sized_symbol<size>*,
1235                      typename elfcpp::Elf_types<size>::Elf_Addr value,
1236                      unsigned int shndx,
1237                      const Stringpool*, unsigned char* p
1238                      ACCEPT_SIZE_ENDIAN) const;
1239
1240   // Possibly warn about an undefined symbol from a dynamic object.
1241   void
1242   warn_about_undefined_dynobj_symbol(const Input_objects*, Symbol*) const;
1243
1244   // Write out a section symbol, specialized for size and endianness.
1245   template<int size, bool big_endian>
1246   void
1247   sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1248
1249   // The type of the symbol hash table.
1250
1251   typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1252
1253   struct Symbol_table_hash
1254   {
1255     size_t
1256     operator()(const Symbol_table_key&) const;
1257   };
1258
1259   struct Symbol_table_eq
1260   {
1261     bool
1262     operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1263   };
1264
1265   typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1266                         Symbol_table_eq> Symbol_table_type;
1267
1268   // The type of the list of common symbols.
1269   typedef std::vector<Symbol*> Commons_type;
1270
1271   // A map from symbols with COPY relocs to the dynamic objects where
1272   // they are defined.
1273   typedef Unordered_map<const Symbol*, Dynobj*> Copied_symbol_dynobjs;
1274
1275   // A map from symbol name (as a pointer into the namepool) to all
1276   // the locations the symbols is (weakly) defined (and certain other
1277   // conditions are met).  This map will be used later to detect
1278   // possible One Definition Rule (ODR) violations.
1279   struct Symbol_location
1280   {
1281     Object* object;         // Object where the symbol is defined.
1282     unsigned int shndx;     // Section-in-object where the symbol is defined.
1283     off_t offset;           // Offset-in-section where the symbol is defined.
1284     bool operator==(const Symbol_location& that) const
1285     {
1286       return (this->object == that.object
1287               && this->shndx == that.shndx
1288               && this->offset == that.offset);
1289     }
1290   };
1291
1292   struct Symbol_location_hash
1293   {
1294     size_t operator()(const Symbol_location& loc) const
1295     { return reinterpret_cast<uintptr_t>(loc.object) ^ loc.offset ^ loc.shndx; }
1296   };
1297
1298   typedef Unordered_map<const char*,
1299                         Unordered_set<Symbol_location, Symbol_location_hash> >
1300   Odr_map;
1301
1302   // We increment this every time we see a new undefined symbol, for
1303   // use in archive groups.
1304   int saw_undefined_;
1305   // The index of the first global symbol in the output file.
1306   unsigned int first_global_index_;
1307   // The file offset within the output symtab section where we should
1308   // write the table.
1309   off_t offset_;
1310   // The number of global symbols we want to write out.
1311   size_t output_count_;
1312   // The file offset of the global dynamic symbols, or 0 if none.
1313   off_t dynamic_offset_;
1314   // The index of the first global dynamic symbol.
1315   unsigned int first_dynamic_global_index_;
1316   // The number of global dynamic symbols, or 0 if none.
1317   off_t dynamic_count_;
1318   // The symbol hash table.
1319   Symbol_table_type table_;
1320   // A pool of symbol names.  This is used for all global symbols.
1321   // Entries in the hash table point into this pool.
1322   Stringpool namepool_;
1323   // Forwarding symbols.
1324   Unordered_map<const Symbol*, Symbol*> forwarders_;
1325   // Weak aliases.  A symbol in this list points to the next alias.
1326   // The aliases point to each other in a circular list.
1327   Unordered_map<Symbol*, Symbol*> weak_aliases_;
1328   // We don't expect there to be very many common symbols, so we keep
1329   // a list of them.  When we find a common symbol we add it to this
1330   // list.  It is possible that by the time we process the list the
1331   // symbol is no longer a common symbol.  It may also have become a
1332   // forwarder.
1333   Commons_type commons_;
1334   // Manage symbol warnings.
1335   Warnings warnings_;
1336   // Manage potential One Definition Rule (ODR) violations.
1337   Odr_map candidate_odr_violations_;
1338
1339   // When we emit a COPY reloc for a symbol, we define it in an
1340   // Output_data.  When it's time to emit version information for it,
1341   // we need to know the dynamic object in which we found the original
1342   // definition.  This maps symbols with COPY relocs to the dynamic
1343   // object where they were defined.
1344   Copied_symbol_dynobjs copied_symbol_dynobjs_;
1345 };
1346
1347 // We inline get_sized_symbol for efficiency.
1348
1349 template<int size>
1350 Sized_symbol<size>*
1351 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1352 {
1353   gold_assert(size == parameters->get_size());
1354   return static_cast<Sized_symbol<size>*>(sym);
1355 }
1356
1357 template<int size>
1358 const Sized_symbol<size>*
1359 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1360 {
1361   gold_assert(size == parameters->get_size());
1362   return static_cast<const Sized_symbol<size>*>(sym);
1363 }
1364
1365 } // End namespace gold.
1366
1367 #endif // !defined(GOLD_SYMTAB_H)