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