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