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