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