Finished layout code.
[external/binutils.git] / gold / symtab.h
1 // symtab.h -- the gold symbol table   -*- C++ -*-
2
3 // Symbol_table
4 //   The symbol table.
5
6 #include <string>
7 #include <utility>
8 #include <cassert>
9
10 #include "elfcpp.h"
11 #include "stringpool.h"
12
13 #ifndef GOLD_SYMTAB_H
14 #define GOLD_SYMTAB_H
15
16 namespace gold
17 {
18
19 class Object;
20
21 template<int size, bool big_endian>
22 class Sized_object;
23
24 // The base class of an entry in the symbol table.  The symbol table
25 // can have a lot of entries, so we don't want this class to big.
26 // Size dependent fields can be found in the template class
27 // Sized_symbol.  Targets may support their own derived classes.
28
29 class Symbol
30 {
31  public:
32   // Return the symbol name.
33   const char*
34   name() const
35   { return this->name_; }
36
37   // Return the symbol version.  This will return NULL for an
38   // unversioned symbol.
39   const char*
40   version() const
41   { return this->version_; }
42
43   // Return the object with which this symbol is associated.
44   Object*
45   object() const
46   { return this->object_; }
47
48   // Return the symbol binding.
49   elfcpp::STB
50   binding() const
51   { return this->binding_; }
52
53   // Return the symbol type.
54   elfcpp::STT
55   type() const
56   { return this->type_; }
57
58   // Return the symbol visibility.
59   elfcpp::STV
60   visibility() const
61   { return this->visibility_; }
62
63   // Return the non-visibility part of the st_other field.
64   unsigned char
65   other() const
66   { return this->other_; }
67
68   // Return the section index.
69   unsigned int
70   shnum() const
71   { return this->shnum_; }
72
73   // Return whether this symbol is a forwarder.  This will never be
74   // true of a symbol found in the hash table, but may be true of
75   // symbol pointers attached to object files.
76   bool
77   is_forwarder() const
78   { return this->is_forwarder_; }
79
80   // Mark this symbol as a forwarder.
81   void
82   set_forwarder()
83   { this->is_forwarder_ = true; }
84
85   // Return whether this symbol was seen in a dynamic object.
86   bool
87   in_dyn() const
88   { return this->in_dyn_; }
89
90   // Mark this symbol as seen in a dynamic object.
91   void
92   set_in_dyn()
93   { this->in_dyn_ = true; }
94
95  protected:
96   // Instances of this class should always be created at a specific
97   // size.
98   Symbol()
99   { }
100
101   // Initialize fields from an ELF symbol in OBJECT.
102   template<int size, bool big_endian>
103   void
104   init_base(const char *name, const char* version, Object* object,
105             const elfcpp::Sym<size, big_endian>&);
106
107   // Override existing symbol.
108   template<int size, bool big_endian>
109   void
110   override_base(const elfcpp::Sym<size, big_endian>&, Object* object);
111
112  private:
113   Symbol(const Symbol&);
114   Symbol& operator=(const Symbol&);
115
116   // Symbol name (expected to point into a Stringpool).
117   const char* name_;
118   // Symbol version (expected to point into a Stringpool).  This may
119   // be NULL.
120   const char* version_;
121   // Object in which symbol is defined, or in which it was first seen.
122   Object* object_;
123   // Section number in object_ in which symbol is defined.
124   unsigned int shnum_;
125   // Symbol type.
126   elfcpp::STT type_ : 4;
127   // Symbol binding.
128   elfcpp::STB binding_ : 4;
129   // Symbol visibility.
130   elfcpp::STV visibility_ : 2;
131   // Rest of symbol st_other field.
132   unsigned int other_ : 6;
133   // True if this symbol always requires special target-specific
134   // handling.
135   bool is_special_ : 1;
136   // True if this is the default version of the symbol.
137   bool is_def_ : 1;
138   // True if this symbol really forwards to another symbol.  This is
139   // used when we discover after the fact that two different entries
140   // in the hash table really refer to the same symbol.  This will
141   // never be set for a symbol found in the hash table, but may be set
142   // for a symbol found in the list of symbols attached to an Object.
143   // It forwards to the symbol found in the forwarders_ map of
144   // Symbol_table.
145   bool is_forwarder_ : 1;
146   // True if we've seen this symbol in a dynamic object.
147   bool in_dyn_ : 1;
148 };
149
150 // The parts of a symbol which are size specific.  Using a template
151 // derived class like this helps us use less space on a 32-bit system.
152
153 template<int size>
154 class Sized_symbol : public Symbol
155 {
156  public:
157   typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
158   typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
159
160   Sized_symbol()
161   { }
162
163   // Initialize fields from an ELF symbol in OBJECT.
164   template<bool big_endian>
165   void
166   init(const char *name, const char* version, Object* object,
167        const elfcpp::Sym<size, big_endian>&);
168
169   // Override existing symbol.
170   template<bool big_endian>
171   void
172   override(const elfcpp::Sym<size, big_endian>&, Object* object);
173
174   // Return the symbol's value.
175   Value_type
176   value() const
177   { return this->value_; }
178
179   // Return the symbol's size (we can't call this 'size' because that
180   // is a template parameter).
181   Size_type
182   symsize() const
183   { return this->size_; }
184
185   // Set the symbol value.  This is called when we store the final
186   // values of the symbols into the symbol table.
187   void
188   set_value(Value_type value)
189   { this->value_ = value; }
190
191  private:
192   Sized_symbol(const Sized_symbol&);
193   Sized_symbol& operator=(const Sized_symbol&);
194
195   // Symbol value.
196   Value_type value_;
197   // Symbol size.
198   Size_type size_;
199 };
200
201 // The main linker symbol table.
202
203 class Symbol_table
204 {
205  public:
206   Symbol_table();
207
208   ~Symbol_table();
209
210   // Add COUNT external symbols from OBJECT to the symbol table.  SYMS
211   // is the symbols, SYM_NAMES is their names, SYM_NAME_SIZE is the
212   // size of SYM_NAMES.  This sets SYMPOINTERS to point to the symbols
213   // in the symbol table.
214   template<int size, bool big_endian>
215   void
216   add_from_object(Sized_object<size, big_endian>* object,
217                   const elfcpp::Sym<size, big_endian>* syms,
218                   size_t count, const char* sym_names, size_t sym_name_size,
219                   Symbol** sympointers);
220
221   // Return the real symbol associated with the forwarder symbol FROM.
222   Symbol*
223   resolve_forwards(Symbol* from) const;
224
225   // Return the size of the symbols in the table.
226   int
227   get_size() const
228   { return this->size_; }
229
230   // Return the sized version of a symbol in this table.
231   template<int size>
232   Sized_symbol<size>*
233   get_sized_symbol(Symbol*) const;
234
235   template<int size>
236   const Sized_symbol<size>*
237   get_sized_symbol(const Symbol*) const;
238
239   // Finalize the symbol table after we have set the final addresses
240   // of all the input sections.  This sets the final symbol values and
241   // adds the names to *POOL.  It records the file offset OFF, and
242   // returns the new file offset.
243   off_t
244   finalize(off_t, Stringpool*);
245
246  private:
247   Symbol_table(const Symbol_table&);
248   Symbol_table& operator=(const Symbol_table&);
249
250   // Set the size of the symbols in the table.
251   void
252   set_size(int size)
253   { this->size_ = size; }
254
255   // Make FROM a forwarder symbol to TO.
256   void
257   make_forwarder(Symbol* from, Symbol* to);
258
259   // Add a symbol.
260   template<int size, bool big_endian>
261   Symbol*
262   add_from_object(Sized_object<size, big_endian>*, const char *name,
263                   const char *version, bool def,
264                   const elfcpp::Sym<size, big_endian>& sym);
265
266   // Resolve symbols.
267   template<int size, bool big_endian>
268   static void
269   resolve(Sized_symbol<size>* to,
270           const elfcpp::Sym<size, big_endian>& sym,
271           Object*);
272
273 #ifdef HAVE_MEMBER_TEMPLATE_SPECIFICATIONS
274   template<int size, bool big_endian>
275   static void
276   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from);
277 #else
278   template<int size>
279   static void
280   resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
281           bool big_endian);
282 #endif
283
284   // Finalize symbols specialized for size.
285   template<int size>
286   off_t
287   sized_finalize(off_t, Stringpool*);
288
289   // The type of the symbol hash table.
290
291   typedef std::pair<const char*, const char*> Symbol_table_key;
292
293   struct Symbol_table_hash
294   {
295     size_t
296     operator()(const Symbol_table_key&) const;
297   };
298
299   struct Symbol_table_eq
300   {
301     bool
302     operator()(const Symbol_table_key&, const Symbol_table_key&) const;
303   };
304
305   typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
306                         Symbol_table_eq> Symbol_table_type;
307
308   // The size of the symbols in the symbol table (32 or 64).
309   int size_;
310
311   // The file offset within the output symtab section where we should
312   // write the table.
313   off_t offset_;
314
315   // The symbol hash table.
316   Symbol_table_type table_;
317
318   // A pool of symbol names.  This is used for all global symbols.
319   // Entries in the hash table point into this pool.
320   Stringpool namepool_;
321
322   // Forwarding symbols.
323   Unordered_map<Symbol*, Symbol*> forwarders_;
324 };
325
326 // We inline get_sized_symbol for efficiency.
327
328 template<int size>
329 Sized_symbol<size>*
330 Symbol_table::get_sized_symbol(Symbol* sym) const
331 {
332   assert(size == this->get_size());
333   return static_cast<Sized_symbol<size>*>(sym);
334 }
335
336 template<int size>
337 const Sized_symbol<size>*
338 Symbol_table::get_sized_symbol(const Symbol* sym) const
339 {
340   assert(size == this->get_size());
341   return static_cast<const Sized_symbol<size>*>(sym);
342 }
343
344 } // End namespace gold.
345
346 #endif // !defined(GOLD_SYMTAB_H)