Add an option for Stringpools to not copy strings.
[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 Output_data;
50 class Output_section;
51 class Output_segment;
52 class Output_file;
53 class Target;
54
55 // The base class of an entry in the symbol table.  The symbol table
56 // can have a lot of entries, so we don't want this class to big.
57 // Size dependent fields can be found in the template class
58 // Sized_symbol.  Targets may support their own derived classes.
59
60 class Symbol
61 {
62  public:
63   // Because we want the class to be small, we don't use any virtual
64   // functions.  But because symbols can be defined in different
65   // places, we need to classify them.  This enum is the different
66   // sources of symbols we support.
67   enum Source
68   {
69     // Symbol defined in a relocatable or dynamic input file--this is
70     // the most common case.
71     FROM_OBJECT,
72     // Symbol defined in an Output_data, a special section created by
73     // the target.
74     IN_OUTPUT_DATA,
75     // Symbol defined in an Output_segment, with no associated
76     // section.
77     IN_OUTPUT_SEGMENT,
78     // Symbol value is constant.
79     CONSTANT
80   };
81
82   // When the source is IN_OUTPUT_SEGMENT, we need to describe what
83   // the offset means.
84   enum Segment_offset_base
85   {
86     // From the start of the segment.
87     SEGMENT_START,
88     // From the end of the segment.
89     SEGMENT_END,
90     // From the filesz of the segment--i.e., after the loaded bytes
91     // but before the bytes which are allocated but zeroed.
92     SEGMENT_BSS
93   };
94
95   // Return the symbol name.
96   const char*
97   name() const
98   { return this->name_; }
99
100   // Return the symbol version.  This will return NULL for an
101   // unversioned symbol.
102   const char*
103   version() const
104   { return this->version_; }
105
106   // Return the symbol source.
107   Source
108   source() const
109   { return this->source_; }
110
111   // Return the object with which this symbol is associated.
112   Object*
113   object() const
114   {
115     gold_assert(this->source_ == FROM_OBJECT);
116     return this->u_.from_object.object;
117   }
118
119   // Return the index of the section in the input relocatable or
120   // dynamic object file.
121   unsigned int
122   shndx() const
123   {
124     gold_assert(this->source_ == FROM_OBJECT);
125     return this->u_.from_object.shndx;
126   }
127
128   // Return the output data section with which this symbol is
129   // associated, if the symbol was specially defined with respect to
130   // an output data section.
131   Output_data*
132   output_data() const
133   {
134     gold_assert(this->source_ == IN_OUTPUT_DATA);
135     return this->u_.in_output_data.output_data;
136   }
137
138   // If this symbol was defined with respect to an output data
139   // section, return whether the value is an offset from end.
140   bool
141   offset_is_from_end() const
142   {
143     gold_assert(this->source_ == IN_OUTPUT_DATA);
144     return this->u_.in_output_data.offset_is_from_end;
145   }
146
147   // Return the output segment with which this symbol is associated,
148   // if the symbol was specially defined with respect to an output
149   // segment.
150   Output_segment*
151   output_segment() const
152   {
153     gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
154     return this->u_.in_output_segment.output_segment;
155   }
156
157   // If this symbol was defined with respect to an output segment,
158   // return the offset base.
159   Segment_offset_base
160   offset_base() const
161   {
162     gold_assert(this->source_ == IN_OUTPUT_SEGMENT);
163     return this->u_.in_output_segment.offset_base;
164   }
165
166   // Return the symbol binding.
167   elfcpp::STB
168   binding() const
169   { return this->binding_; }
170
171   // Return the symbol type.
172   elfcpp::STT
173   type() const
174   { return this->type_; }
175
176   // Return the symbol visibility.
177   elfcpp::STV
178   visibility() const
179   { return this->visibility_; }
180
181   // Return the non-visibility part of the st_other field.
182   unsigned char
183   nonvis() const
184   { return this->nonvis_; }
185
186   // Return whether this symbol is a forwarder.  This will never be
187   // true of a symbol found in the hash table, but may be true of
188   // symbol pointers attached to object files.
189   bool
190   is_forwarder() const
191   { return this->is_forwarder_; }
192
193   // Mark this symbol as a forwarder.
194   void
195   set_forwarder()
196   { this->is_forwarder_ = true; }
197
198   // Return whether this symbol needs an entry in the dynamic symbol
199   // table.
200   bool
201   needs_dynsym_entry() const
202   {
203     return (this->needs_dynsym_entry_
204             || (this->in_reg() && this->in_dyn()));
205   }
206
207   // Mark this symbol as needing an entry in the dynamic symbol table.
208   void
209   set_needs_dynsym_entry()
210   { this->needs_dynsym_entry_ = true; }
211
212   // Return whether this symbol has been seen in a regular object.
213   bool
214   in_reg() const
215   { return this->in_reg_; }
216
217   // Mark this symbol as having been seen in a regular object.
218   void
219   set_in_reg()
220   { this->in_reg_ = true; }
221
222   // Return whether this symbol has been seen in a dynamic object.
223   bool
224   in_dyn() const
225   { return this->in_dyn_; }
226
227   // Mark this symbol as having been seen in a dynamic object.
228   void
229   set_in_dyn()
230   { this->in_dyn_ = true; }
231
232   // Return the index of this symbol in the output file symbol table.
233   // A value of -1U means that this symbol is not going into the
234   // output file.  This starts out as zero, and is set to a non-zero
235   // value by Symbol_table::finalize.  It is an error to ask for the
236   // symbol table index before it has been set.
237   unsigned int
238   symtab_index() const
239   {
240     gold_assert(this->symtab_index_ != 0);
241     return this->symtab_index_;
242   }
243
244   // Set the index of the symbol in the output file symbol table.
245   void
246   set_symtab_index(unsigned int index)
247   {
248     gold_assert(index != 0);
249     this->symtab_index_ = index;
250   }
251
252   // Return whether this symbol already has an index in the output
253   // file symbol table.
254   bool
255   has_symtab_index() const
256   { return this->symtab_index_ != 0; }
257
258   // Return the index of this symbol in the dynamic symbol table.  A
259   // value of -1U means that this symbol is not going into the dynamic
260   // symbol table.  This starts out as zero, and is set to a non-zero
261   // during Layout::finalize.  It is an error to ask for the dynamic
262   // symbol table index before it has been set.
263   unsigned int
264   dynsym_index() const
265   {
266     gold_assert(this->dynsym_index_ != 0);
267     return this->dynsym_index_;
268   }
269
270   // Set the index of the symbol in the dynamic symbol table.
271   void
272   set_dynsym_index(unsigned int index)
273   {
274     gold_assert(index != 0);
275     this->dynsym_index_ = index;
276   }
277
278   // Return whether this symbol already has an index in the dynamic
279   // symbol table.
280   bool
281   has_dynsym_index() const
282   { return this->dynsym_index_ != 0; }
283
284   // Return whether this symbol has an entry in the GOT section.
285   bool
286   has_got_offset() const
287   { return this->has_got_offset_; }
288
289   // Return the offset into the GOT section of this symbol.
290   unsigned int
291   got_offset() const
292   {
293     gold_assert(this->has_got_offset());
294     return this->got_offset_;
295   }
296
297   // Set the GOT offset of this symbol.
298   void
299   set_got_offset(unsigned int got_offset)
300   {
301     this->has_got_offset_ = true;
302     this->got_offset_ = got_offset;
303   }
304
305   // Return whether this symbol has an entry in the PLT section.
306   bool
307   has_plt_offset() const
308   { return this->has_plt_offset_; }
309
310   // Return the offset into the PLT section of this symbol.
311   unsigned int
312   plt_offset() const
313   {
314     gold_assert(this->has_plt_offset());
315     return this->plt_offset_;
316   }
317
318   // Set the PLT offset of this symbol.
319   void
320   set_plt_offset(unsigned int plt_offset)
321   {
322     this->has_plt_offset_ = true;
323     this->plt_offset_ = plt_offset;
324   }
325
326   // Return whether this dynamic symbol needs a special value in the
327   // dynamic symbol table.
328   bool
329   needs_dynsym_value() const
330   { return this->needs_dynsym_value_; }
331
332   // Set that this dynamic symbol needs a special value in the dynamic
333   // symbol table.
334   void
335   set_needs_dynsym_value()
336   {
337     gold_assert(this->object()->is_dynamic());
338     this->needs_dynsym_value_ = true;
339   }
340
341   // Return true if the final value of this symbol is known at link
342   // time.
343   bool
344   final_value_is_known() const;
345
346   // Return whether this is a defined symbol (not undefined or
347   // common).
348   bool
349   is_defined() const
350   {
351     return (this->source_ != FROM_OBJECT
352             || (this->shndx() != elfcpp::SHN_UNDEF
353                 && this->shndx() != elfcpp::SHN_COMMON));
354   }
355
356   // Return true if this symbol is from a dynamic object.
357   bool
358   is_from_dynobj() const
359   {
360     return this->source_ == FROM_OBJECT && this->object()->is_dynamic();
361   }
362
363   // Return whether this is an undefined symbol.
364   bool
365   is_undefined() const
366   {
367     return this->source_ == FROM_OBJECT && this->shndx() == elfcpp::SHN_UNDEF;
368   }
369
370   // Return whether this is a common symbol.
371   bool
372   is_common() const
373   {
374     return (this->source_ == FROM_OBJECT
375             && (this->shndx() == elfcpp::SHN_COMMON
376                 || this->type_ == elfcpp::STT_COMMON));
377   }
378
379   // Return whether this symbol can be seen outside this object.
380   bool
381   is_externally_visible() const
382   {
383     return (this->visibility_ == elfcpp::STV_DEFAULT
384             || this->visibility_ == elfcpp::STV_PROTECTED);
385   }
386
387   // Return whether there should be a warning for references to this
388   // symbol.
389   bool
390   has_warning() const
391   { return this->has_warning_; }
392
393   // Mark this symbol as having a warning.
394   void
395   set_has_warning()
396   { this->has_warning_ = true; }
397
398  protected:
399   // Instances of this class should always be created at a specific
400   // size.
401   Symbol()
402   { memset(this, 0, sizeof *this); }
403
404   // Initialize the general fields.
405   void
406   init_fields(const char* name, const char* version,
407               elfcpp::STT type, elfcpp::STB binding,
408               elfcpp::STV visibility, unsigned char nonvis);
409
410   // Initialize fields from an ELF symbol in OBJECT.
411   template<int size, bool big_endian>
412   void
413   init_base(const char *name, const char* version, Object* object,
414             const elfcpp::Sym<size, big_endian>&);
415
416   // Initialize fields for an Output_data.
417   void
418   init_base(const char* name, Output_data*, elfcpp::STT, elfcpp::STB,
419             elfcpp::STV, unsigned char nonvis, bool offset_is_from_end);
420
421   // Initialize fields for an Output_segment.
422   void
423   init_base(const char* name, Output_segment* os, elfcpp::STT type,
424             elfcpp::STB binding, elfcpp::STV visibility,
425             unsigned char nonvis, Segment_offset_base offset_base);
426
427   // Initialize fields for a constant.
428   void
429   init_base(const char* name, elfcpp::STT type, elfcpp::STB binding,
430             elfcpp::STV visibility, unsigned char nonvis);
431
432   // Override existing symbol.
433   template<int size, bool big_endian>
434   void
435   override_base(const elfcpp::Sym<size, big_endian>&, Object* object,
436                 const char* version);
437
438   // Override existing symbol with a special symbol.
439   void
440   override_base_with_special(const Symbol* from);
441
442  private:
443   Symbol(const Symbol&);
444   Symbol& operator=(const Symbol&);
445
446   // Symbol name (expected to point into a Stringpool).
447   const char* name_;
448   // Symbol version (expected to point into a Stringpool).  This may
449   // be NULL.
450   const char* version_;
451
452   union
453   {
454     // This struct is used if SOURCE_ == FROM_OBJECT.
455     struct
456     {
457       // Object in which symbol is defined, or in which it was first
458       // seen.
459       Object* object;
460       // Section number in object_ in which symbol is defined.
461       unsigned int shndx;
462     } from_object;
463
464     // This struct is used if SOURCE_ == IN_OUTPUT_DATA.
465     struct
466     {
467       // Output_data in which symbol is defined.  Before
468       // Layout::finalize the symbol's value is an offset within the
469       // Output_data.
470       Output_data* output_data;
471       // True if the offset is from the end, false if the offset is
472       // from the beginning.
473       bool offset_is_from_end;
474     } in_output_data;
475
476     // This struct is used if SOURCE_ == IN_OUTPUT_SEGMENT.
477     struct
478     {
479       // Output_segment in which the symbol is defined.  Before
480       // Layout::finalize the symbol's value is an offset.
481       Output_segment* output_segment;
482       // The base to use for the offset before Layout::finalize.
483       Segment_offset_base offset_base;
484     } in_output_segment;
485   } u_;
486
487   // The index of this symbol in the output file.  If the symbol is
488   // not going into the output file, this value is -1U.  This field
489   // starts as always holding zero.  It is set to a non-zero value by
490   // Symbol_table::finalize.
491   unsigned int symtab_index_;
492
493   // The index of this symbol in the dynamic symbol table.  If the
494   // symbol is not going into the dynamic symbol table, this value is
495   // -1U.  This field starts as always holding zero.  It is set to a
496   // non-zero value during Layout::finalize.
497   unsigned int dynsym_index_;
498
499   // If this symbol has an entry in the GOT section (has_got_offset_
500   // is true), this is the offset from the start of the GOT section.
501   unsigned int got_offset_;
502
503   // If this symbol has an entry in the PLT section (has_plt_offset_
504   // is true), then this is the offset from the start of the PLT
505   // section.
506   unsigned int plt_offset_;
507
508   // Symbol type.
509   elfcpp::STT type_ : 4;
510   // Symbol binding.
511   elfcpp::STB binding_ : 4;
512   // Symbol visibility.
513   elfcpp::STV visibility_ : 2;
514   // Rest of symbol st_other field.
515   unsigned int nonvis_ : 6;
516   // The type of symbol.
517   Source source_ : 3;
518   // True if this symbol always requires special target-specific
519   // handling.
520   bool is_target_special_ : 1;
521   // True if this is the default version of the symbol.
522   bool is_def_ : 1;
523   // True if this symbol really forwards to another symbol.  This is
524   // used when we discover after the fact that two different entries
525   // in the hash table really refer to the same symbol.  This will
526   // never be set for a symbol found in the hash table, but may be set
527   // for a symbol found in the list of symbols attached to an Object.
528   // It forwards to the symbol found in the forwarders_ map of
529   // Symbol_table.
530   bool is_forwarder_ : 1;
531   // True if this symbol needs to be in the dynamic symbol table.
532   bool needs_dynsym_entry_ : 1;
533   // True if we've seen this symbol in a regular object.
534   bool in_reg_ : 1;
535   // True if we've seen this symbol in a dynamic object.
536   bool in_dyn_ : 1;
537   // True if the symbol has an entry in the GOT section.
538   bool has_got_offset_ : 1;
539   // True if the symbol has an entry in the PLT section.
540   bool has_plt_offset_ : 1;
541   // True if this is a dynamic symbol which needs a special value in
542   // the dynamic symbol table.
543   bool needs_dynsym_value_ : 1;
544   // True if there is a warning for this symbol.
545   bool has_warning_ : 1;
546 };
547
548 // The parts of a symbol which are size specific.  Using a template
549 // derived class like this helps us use less space on a 32-bit system.
550
551 template<int size>
552 class Sized_symbol : public Symbol
553 {
554  public:
555   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
556   typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
557
558   Sized_symbol()
559   { }
560
561   // Initialize fields from an ELF symbol in OBJECT.
562   template<bool big_endian>
563   void
564   init(const char *name, const char* version, Object* object,
565        const elfcpp::Sym<size, big_endian>&);
566
567   // Initialize fields for an Output_data.
568   void
569   init(const char* name, Output_data*, Value_type value, Size_type symsize,
570        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
571        bool offset_is_from_end);
572
573   // Initialize fields for an Output_segment.
574   void
575   init(const char* name, Output_segment*, Value_type value, Size_type symsize,
576        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis,
577        Segment_offset_base offset_base);
578
579   // Initialize fields for a constant.
580   void
581   init(const char* name, Value_type value, Size_type symsize,
582        elfcpp::STT, elfcpp::STB, elfcpp::STV, unsigned char nonvis);
583
584   // Override existing symbol.
585   template<bool big_endian>
586   void
587   override(const elfcpp::Sym<size, big_endian>&, Object* object,
588            const char* version);
589
590   // Override existing symbol with a special symbol.
591   void
592   override_with_special(const Sized_symbol<size>*);
593
594   // Return the symbol's value.
595   Value_type
596   value() const
597   { return this->value_; }
598
599   // Return the symbol's size (we can't call this 'size' because that
600   // is a template parameter).
601   Size_type
602   symsize() const
603   { return this->symsize_; }
604
605   // Set the symbol size.  This is used when resolving common symbols.
606   void
607   set_symsize(Size_type symsize)
608   { this->symsize_ = symsize; }
609
610   // Set the symbol value.  This is called when we store the final
611   // values of the symbols into the symbol table.
612   void
613   set_value(Value_type value)
614   { this->value_ = value; }
615
616  private:
617   Sized_symbol(const Sized_symbol&);
618   Sized_symbol& operator=(const Sized_symbol&);
619
620   // Symbol value.  Before Layout::finalize this is the offset in the
621   // input section.  This is set to the final value during
622   // Layout::finalize.
623   Value_type value_;
624   // Symbol size.
625   Size_type symsize_;
626 };
627
628 // A struct describing a symbol defined by the linker, where the value
629 // of the symbol is defined based on an output section.  This is used
630 // for symbols defined by the linker, like "_init_array_start".
631
632 struct Define_symbol_in_section
633 {
634   // The symbol name.
635   const char* name;
636   // The name of the output section with which this symbol should be
637   // associated.  If there is no output section with that name, the
638   // symbol will be defined as zero.
639   const char* output_section;
640   // The offset of the symbol within the output section.  This is an
641   // offset from the start of the output section, unless start_at_end
642   // is true, in which case this is an offset from the end of the
643   // output section.
644   uint64_t value;
645   // The size of the symbol.
646   uint64_t size;
647   // The symbol type.
648   elfcpp::STT type;
649   // The symbol binding.
650   elfcpp::STB binding;
651   // The symbol visibility.
652   elfcpp::STV visibility;
653   // The rest of the st_other field.
654   unsigned char nonvis;
655   // If true, the value field is an offset from the end of the output
656   // section.
657   bool offset_is_from_end;
658   // If true, this symbol is defined only if we see a reference to it.
659   bool only_if_ref;
660 };
661
662 // A struct describing a symbol defined by the linker, where the value
663 // of the symbol is defined based on a segment.  This is used for
664 // symbols defined by the linker, like "_end".  We describe the
665 // segment with which the symbol should be associated by its
666 // characteristics.  If no segment meets these characteristics, the
667 // symbol will be defined as zero.  If there is more than one segment
668 // which meets these characteristics, we will use the first one.
669
670 struct Define_symbol_in_segment
671 {
672   // The symbol name.
673   const char* name;
674   // The segment type where the symbol should be defined, typically
675   // PT_LOAD.
676   elfcpp::PT segment_type;
677   // Bitmask of segment flags which must be set.
678   elfcpp::PF segment_flags_set;
679   // Bitmask of segment flags which must be clear.
680   elfcpp::PF segment_flags_clear;
681   // The offset of the symbol within the segment.  The offset is
682   // calculated from the position set by offset_base.
683   uint64_t value;
684   // The size of the symbol.
685   uint64_t size;
686   // The symbol type.
687   elfcpp::STT type;
688   // The symbol binding.
689   elfcpp::STB binding;
690   // The symbol visibility.
691   elfcpp::STV visibility;
692   // The rest of the st_other field.
693   unsigned char nonvis;
694   // The base from which we compute the offset.
695   Symbol::Segment_offset_base offset_base;
696   // If true, this symbol is defined only if we see a reference to it.
697   bool only_if_ref;
698 };
699
700 // This class manages warnings.  Warnings are a GNU extension.  When
701 // we see a section named .gnu.warning.SYM in an object file, and if
702 // we wind using the definition of SYM from that object file, then we
703 // will issue a warning for any relocation against SYM from a
704 // different object file.  The text of the warning is the contents of
705 // the section.  This is not precisely the definition used by the old
706 // GNU linker; the old GNU linker treated an occurrence of
707 // .gnu.warning.SYM as defining a warning symbol.  A warning symbol
708 // would trigger a warning on any reference.  However, it was
709 // inconsistent in that a warning in a dynamic object only triggered
710 // if there was no definition in a regular object.  This linker is
711 // different in that we only issue a warning if we use the symbol
712 // definition from the same object file as the warning section.
713
714 class Warnings
715 {
716  public:
717   Warnings()
718     : warnings_()
719   { }
720
721   // Add a warning for symbol NAME in section SHNDX in object OBJ.
722   void
723   add_warning(Symbol_table* symtab, const char* name, Object* obj,
724               unsigned int shndx);
725
726   // For each symbol for which we should give a warning, make a note
727   // on the symbol.
728   void
729   note_warnings(Symbol_table* symtab);
730
731   // Issue a warning for a reference to SYM at LOCATION.
732   void
733   issue_warning(const Symbol* sym, const std::string& location) const;
734
735  private:
736   Warnings(const Warnings&);
737   Warnings& operator=(const Warnings&);
738
739   // What we need to know to get the warning text.
740   struct Warning_location
741   {
742     // The object the warning is in.
743     Object* object;
744     // The index of the warning section.
745     unsigned int shndx;
746     // The warning text if we have already loaded it.
747     std::string text;
748
749     Warning_location()
750       : object(NULL), shndx(0), text()
751     { }
752
753     void
754     set(Object* o, unsigned int s)
755     {
756       this->object = o;
757       this->shndx = s;
758     }
759
760     void
761     set_text(const char* t, off_t l)
762     { this->text.assign(t, l); }
763   };
764
765   // A mapping from warning symbol names (canonicalized in
766   // Symbol_table's namepool_ field) to 
767   typedef Unordered_map<const char*, Warning_location> Warning_table;
768
769   Warning_table warnings_;
770 };
771
772 // The main linker symbol table.
773
774 class Symbol_table
775 {
776  public:
777   Symbol_table();
778
779   ~Symbol_table();
780
781   // Add COUNT external symbols from the relocatable object RELOBJ to
782   // the symbol table.  SYMS is the symbols, SYM_NAMES is their names,
783   // SYM_NAME_SIZE is the size of SYM_NAMES.  This sets SYMPOINTERS to
784   // point to the symbols in the symbol table.
785   template<int size, bool big_endian>
786   void
787   add_from_relobj(Sized_relobj<size, big_endian>* relobj,
788                   const unsigned char* syms, size_t count,
789                   const char* sym_names, size_t sym_name_size,
790                   Symbol** sympointers);
791
792   // Add COUNT dynamic symbols from the dynamic object DYNOBJ to the
793   // symbol table.  SYMS is the symbols.  SYM_NAMES is their names.
794   // SYM_NAME_SIZE is the size of SYM_NAMES.  The other parameters are
795   // symbol version data.
796   template<int size, bool big_endian>
797   void
798   add_from_dynobj(Sized_dynobj<size, big_endian>* dynobj,
799                   const unsigned char* syms, size_t count,
800                   const char* sym_names, size_t sym_name_size,
801                   const unsigned char* versym, size_t versym_size,
802                   const std::vector<const char*>*);
803
804   // Define a special symbol based on an Output_data.  It is a
805   // multiple definition error if this symbol is already defined.
806   Symbol*
807   define_in_output_data(const Target*, const char* name, const char* version,
808                         Output_data*, uint64_t value, uint64_t symsize,
809                         elfcpp::STT type, elfcpp::STB binding,
810                         elfcpp::STV visibility, unsigned char nonvis,
811                         bool offset_is_from_end, bool only_if_ref);
812
813   // Define a special symbol based on an Output_segment.  It is a
814   // multiple definition error if this symbol is already defined.
815   Symbol*
816   define_in_output_segment(const Target*, const char* name,
817                            const char* version, Output_segment*,
818                            uint64_t value, uint64_t symsize,
819                            elfcpp::STT type, elfcpp::STB binding,
820                            elfcpp::STV visibility, unsigned char nonvis,
821                            Symbol::Segment_offset_base, bool only_if_ref);
822
823   // Define a special symbol with a constant value.  It is a multiple
824   // definition error if this symbol is already defined.
825   Symbol*
826   define_as_constant(const Target*, const char* name, const char* version,
827                      uint64_t value, uint64_t symsize, elfcpp::STT type,
828                      elfcpp::STB binding, elfcpp::STV visibility,
829                      unsigned char nonvis, bool only_if_ref);
830
831   // Define a set of symbols in output sections.
832   void
833   define_symbols(const Layout*, const Target*, int count,
834                  const Define_symbol_in_section*);
835
836   // Define a set of symbols in output segments.
837   void
838   define_symbols(const Layout*, const Target*, int count,
839                  const Define_symbol_in_segment*);  
840
841   // Look up a symbol.
842   Symbol*
843   lookup(const char*, const char* version = NULL) const;
844
845   // Return the real symbol associated with the forwarder symbol FROM.
846   Symbol*
847   resolve_forwards(const Symbol* from) const;
848
849   // Return the sized version of a symbol in this table.
850   template<int size>
851   Sized_symbol<size>*
852   get_sized_symbol(Symbol* ACCEPT_SIZE) const;
853
854   template<int size>
855   const Sized_symbol<size>*
856   get_sized_symbol(const Symbol* ACCEPT_SIZE) const;
857
858   // Return the count of undefined symbols seen.
859   int
860   saw_undefined() const
861   { return this->saw_undefined_; }
862
863   // Allocate the common symbols
864   void
865   allocate_commons(const General_options&, Layout*);
866
867   // Add a warning for symbol NAME in section SHNDX in object OBJ.
868   void
869   add_warning(const char* name, Object* obj, unsigned int shndx)
870   { this->warnings_.add_warning(this, name, obj, shndx); }
871
872   // Canonicalize a symbol name for use in the hash table.
873   const char*
874   canonicalize_name(const char* name)
875   { return this->namepool_.add(name, true, NULL); }
876
877   // Possibly issue a warning for a reference to SYM at LOCATION which
878   // is in OBJ.
879   void
880   issue_warning(const Symbol* sym, const std::string& location) const
881   { this->warnings_.issue_warning(sym, location); }
882
883   // Set the dynamic symbol indexes.  INDEX is the index of the first
884   // global dynamic symbol.  Pointers to the symbols are stored into
885   // the vector.  The names are stored into the Stringpool.  This
886   // returns an updated dynamic symbol index.
887   unsigned int
888   set_dynsym_indexes(const General_options*, const Target*, unsigned int index,
889                      std::vector<Symbol*>*, Stringpool*, Versions*);
890
891   // Finalize the symbol table after we have set the final addresses
892   // of all the input sections.  This sets the final symbol indexes,
893   // values and adds the names to *POOL.  INDEX is the index of the
894   // first global symbol.  OFF is the file offset of the global symbol
895   // table, DYNOFF is the offset of the globals in the dynamic symbol
896   // table, DYN_GLOBAL_INDEX is the index of the first global dynamic
897   // symbol, and DYNCOUNT is the number of global dynamic symbols.
898   // This records the parameters, and returns the new file offset.
899   off_t
900   finalize(unsigned int index, off_t off, off_t dynoff,
901            size_t dyn_global_index, size_t dyncount, Stringpool* pool);
902
903   // Write out the global symbols.
904   void
905   write_globals(const Target*, const Stringpool*, const Stringpool*,
906                 Output_file*) const;
907
908   // Write out a section symbol.  Return the updated offset.
909   void
910   write_section_symbol(const Output_section*, Output_file*, off_t) const;
911
912  private:
913   Symbol_table(const Symbol_table&);
914   Symbol_table& operator=(const Symbol_table&);
915
916   // Make FROM a forwarder symbol to TO.
917   void
918   make_forwarder(Symbol* from, Symbol* to);
919
920   // Add a symbol.
921   template<int size, bool big_endian>
922   Symbol*
923   add_from_object(Object*, const char *name, Stringpool::Key name_key,
924                   const char *version, Stringpool::Key version_key,
925                   bool def, const elfcpp::Sym<size, big_endian>& sym);
926
927   // Resolve symbols.
928   template<int size, bool big_endian>
929   static void
930   resolve(Sized_symbol<size>* to,
931           const elfcpp::Sym<size, big_endian>& sym,
932           Object*, const char* version);
933
934   template<int size, bool big_endian>
935   static void
936   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
937           const char* version ACCEPT_SIZE_ENDIAN);
938
939   // Whether we should override a symbol, based on flags in
940   // resolve.cc.
941   static bool
942   should_override(const Symbol*, unsigned int, bool*);
943
944   // Whether we should override a symbol with a special symbol which
945   // is automatically defined by the linker.
946   static bool
947   should_override_with_special(const Symbol*);
948
949   // Define a special symbol.
950   template<int size, bool big_endian>
951   Sized_symbol<size>*
952   define_special_symbol(const Target* target, const char** pname,
953                         const char** pversion, bool only_if_ref,
954                         Sized_symbol<size>** poldsym ACCEPT_SIZE_ENDIAN);
955
956   // Define a symbol in an Output_data, sized version.
957   template<int size>
958   Sized_symbol<size>*
959   do_define_in_output_data(const Target*, const char* name,
960                            const char* version, Output_data*,
961                            typename elfcpp::Elf_types<size>::Elf_Addr value,
962                            typename elfcpp::Elf_types<size>::Elf_WXword ssize,
963                            elfcpp::STT type, elfcpp::STB binding,
964                            elfcpp::STV visibility, unsigned char nonvis,
965                            bool offset_is_from_end, bool only_if_ref);
966
967   // Define a symbol in an Output_segment, sized version.
968   template<int size>
969   Sized_symbol<size>*
970   do_define_in_output_segment(
971     const Target*, const char* name, const char* version, Output_segment* os,
972     typename elfcpp::Elf_types<size>::Elf_Addr value,
973     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
974     elfcpp::STT type, elfcpp::STB binding,
975     elfcpp::STV visibility, unsigned char nonvis,
976     Symbol::Segment_offset_base offset_base, bool only_if_ref);
977
978   // Define a symbol as a constant, sized version.
979   template<int size>
980   Sized_symbol<size>*
981   do_define_as_constant(
982     const Target*, const char* name, const char* version,
983     typename elfcpp::Elf_types<size>::Elf_Addr value,
984     typename elfcpp::Elf_types<size>::Elf_WXword ssize,
985     elfcpp::STT type, elfcpp::STB binding,
986     elfcpp::STV visibility, unsigned char nonvis,
987     bool only_if_ref);
988
989   // Allocate the common symbols, sized version.
990   template<int size>
991   void
992   do_allocate_commons(const General_options&, Layout*);
993
994   // Finalize symbols specialized for size.
995   template<int size>
996   off_t
997   sized_finalize(unsigned int, off_t, Stringpool*);
998
999   // Write globals specialized for size and endianness.
1000   template<int size, bool big_endian>
1001   void
1002   sized_write_globals(const Target*, const Stringpool*, const Stringpool*,
1003                       Output_file*) const;
1004
1005   // Write out a symbol to P.
1006   template<int size, bool big_endian>
1007   void
1008   sized_write_symbol(Sized_symbol<size>*,
1009                      typename elfcpp::Elf_types<size>::Elf_Addr value,
1010                      unsigned int shndx,
1011                      const Stringpool*, unsigned char* p
1012                      ACCEPT_SIZE_ENDIAN) const;
1013
1014   // Write out a section symbol, specialized for size and endianness.
1015   template<int size, bool big_endian>
1016   void
1017   sized_write_section_symbol(const Output_section*, Output_file*, off_t) const;
1018
1019   // The type of the symbol hash table.
1020
1021   typedef std::pair<Stringpool::Key, Stringpool::Key> Symbol_table_key;
1022
1023   struct Symbol_table_hash
1024   {
1025     size_t
1026     operator()(const Symbol_table_key&) const;
1027   };
1028
1029   struct Symbol_table_eq
1030   {
1031     bool
1032     operator()(const Symbol_table_key&, const Symbol_table_key&) const;
1033   };
1034
1035   typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
1036                         Symbol_table_eq> Symbol_table_type;
1037
1038   // The type of the list of common symbols.
1039
1040   typedef std::vector<Symbol*> Commons_type;
1041
1042   // We increment this every time we see a new undefined symbol, for
1043   // use in archive groups.
1044   int saw_undefined_;
1045
1046   // The index of the first global symbol in the output file.
1047   unsigned int first_global_index_;
1048
1049   // The file offset within the output symtab section where we should
1050   // write the table.
1051   off_t offset_;
1052
1053   // The number of global symbols we want to write out.
1054   size_t output_count_;
1055
1056   // The file offset of the global dynamic symbols, or 0 if none.
1057   off_t dynamic_offset_;
1058
1059   // The index of the first global dynamic symbol.
1060   unsigned int first_dynamic_global_index_;
1061
1062   // The number of global dynamic symbols, or 0 if none.
1063   off_t dynamic_count_;
1064
1065   // The symbol hash table.
1066   Symbol_table_type table_;
1067
1068   // A pool of symbol names.  This is used for all global symbols.
1069   // Entries in the hash table point into this pool.
1070   Stringpool namepool_;
1071
1072   // Forwarding symbols.
1073   Unordered_map<const Symbol*, Symbol*> forwarders_;
1074
1075   // We don't expect there to be very many common symbols, so we keep
1076   // a list of them.  When we find a common symbol we add it to this
1077   // list.  It is possible that by the time we process the list the
1078   // symbol is no longer a common symbol.  It may also have become a
1079   // forwarder.
1080   Commons_type commons_;
1081
1082   // Manage symbol warnings.
1083   Warnings warnings_;
1084 };
1085
1086 // We inline get_sized_symbol for efficiency.
1087
1088 template<int size>
1089 Sized_symbol<size>*
1090 Symbol_table::get_sized_symbol(Symbol* sym ACCEPT_SIZE) const
1091 {
1092   gold_assert(size == parameters->get_size());
1093   return static_cast<Sized_symbol<size>*>(sym);
1094 }
1095
1096 template<int size>
1097 const Sized_symbol<size>*
1098 Symbol_table::get_sized_symbol(const Symbol* sym ACCEPT_SIZE) const
1099 {
1100   gold_assert(size == parameters->get_size());
1101   return static_cast<const Sized_symbol<size>*>(sym);
1102 }
1103
1104 } // End namespace gold.
1105
1106 #endif // !defined(GOLD_SYMTAB_H)