More section layout code.
[binutils.git] / gold / symtab.h
blobf5d2a7be24a841044285b3513248220f3fd693c2
1 // symtab.h -- the gold symbol table -*- C++ -*-
3 // Symbol_table
4 // The symbol table.
6 #include <string>
7 #include <utility>
8 #include <cassert>
10 #include "elfcpp.h"
11 #include "stringpool.h"
13 #ifndef GOLD_SYMTAB_H
14 #define GOLD_SYMTAB_H
16 namespace gold
19 class Object;
21 template<int size, bool big_endian>
22 class Sized_object;
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.
29 class Symbol
31 public:
32 // Return the symbol name.
33 const char*
34 name() const
35 { return this->name_; }
37 // Return the symbol version. This will return NULL for an
38 // unversioned symbol.
39 const char*
40 version() const
41 { return this->version_; }
43 // Return the object with which this symbol is associated.
44 Object*
45 object() const
46 { return this->object_; }
48 // Return the symbol binding.
49 elfcpp::STB
50 binding() const
51 { return this->binding_; }
53 // Return the symbol type.
54 elfcpp::STT
55 type() const
56 { return this->type_; }
58 // Return the symbol visibility.
59 elfcpp::STV
60 visibility() const
61 { return this->visibility_; }
63 // Return the non-visibility part of the st_other field.
64 unsigned char
65 other() const
66 { return this->other_; }
68 // Return the section index.
69 unsigned int
70 shnum() const
71 { return this->shnum_; }
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_; }
80 // Mark this symbol as a forwarder.
81 void
82 set_forwarder()
83 { this->is_forwarder_ = true; }
85 // Return whether this symbol was seen in a dynamic object.
86 bool
87 in_dyn() const
88 { return this->in_dyn_; }
90 // Mark this symbol as seen in a dynamic object.
91 void
92 set_in_dyn()
93 { this->in_dyn_ = true; }
95 protected:
96 // Instances of this class should always be created at a specific
97 // size.
98 Symbol()
99 { }
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>&);
107 // Override existing symbol.
108 template<int size, bool big_endian>
109 void
110 override_base(const elfcpp::Sym<size, big_endian>&, Object* object);
112 private:
113 Symbol(const Symbol&);
114 Symbol& operator=(const Symbol&);
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;
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.
153 template<int size>
154 class Sized_symbol : public Symbol
156 public:
157 typedef typename elfcpp::Elf_types<size>::Elf_Addr Value_type;
158 typedef typename elfcpp::Elf_types<size>::Elf_WXword Size_type;
160 Sized_symbol()
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>&);
169 // Override existing symbol.
170 template<bool big_endian>
171 void
172 override(const elfcpp::Sym<size, big_endian>&, Object* object);
174 // Return the symbol's value.
175 Value_type
176 value() const
177 { return this->value_; }
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_; }
185 private:
186 Sized_symbol(const Sized_symbol&);
187 Sized_symbol& operator=(const Sized_symbol&);
189 // Symbol value.
190 Value_type value_;
191 // Symbol size.
192 Size_type size_;
195 // The main linker symbol table.
197 class Symbol_table
199 public:
200 Symbol_table();
202 ~Symbol_table();
204 // Add COUNT external symbols from OBJECT to the symbol table. SYMS
205 // is the symbols, SYM_NAMES is their names, SYM_NAME_SIZE is the
206 // size of SYM_NAMES. This sets SYMPOINTERS to point to the symbols
207 // in the symbol table.
208 template<int size, bool big_endian>
209 void
210 add_from_object(Sized_object<size, big_endian>* object,
211 const elfcpp::Sym<size, big_endian>* syms,
212 size_t count, const char* sym_names, size_t sym_name_size,
213 Symbol** sympointers);
215 // Return the real symbol associated with the forwarder symbol FROM.
216 Symbol*
217 resolve_forwards(Symbol* from) const;
219 // Return the size of the symbols in the table.
221 get_size() const
222 { return this->size_; }
224 // Return the sized version of a symbol in this table.
225 template<int size>
226 Sized_symbol<size>*
227 get_sized_symbol(Symbol*) const;
229 template<int size>
230 const Sized_symbol<size>*
231 get_sized_symbol(const Symbol*) const;
233 // Record the names of the local symbols for an object.
234 template<int size, bool big_endian>
235 void
236 add_local_symbol_names(Sized_object<size, big_endian>* object,
237 const elfcpp::Sym<size, big_endian>* syms,
238 size_t count, const char* sym_names,
239 size_t sym_name_size);
241 private:
242 Symbol_table(const Symbol_table&);
243 Symbol_table& operator=(const Symbol_table&);
245 // Set the size of the symbols in the table.
246 void
247 set_size(int size)
248 { this->size_ = size; }
250 // Make FROM a forwarder symbol to TO.
251 void
252 make_forwarder(Symbol* from, Symbol* to);
254 // Add a symbol.
255 template<int size, bool big_endian>
256 Symbol*
257 add_from_object(Sized_object<size, big_endian>*, const char *name,
258 const char *version, bool def,
259 const elfcpp::Sym<size, big_endian>& sym);
261 // Resolve symbols.
262 template<int size, bool big_endian>
263 static void
264 resolve(Sized_symbol<size>* to,
265 const elfcpp::Sym<size, big_endian>& sym,
266 Object*);
268 template<int size, bool big_endian>
269 static void
270 resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from);
272 // The type of the symbol hash table.
274 typedef std::pair<const char*, const char*> Symbol_table_key;
276 struct Symbol_table_hash
278 size_t
279 operator()(const Symbol_table_key&) const;
282 struct Symbol_table_eq
284 bool
285 operator()(const Symbol_table_key&, const Symbol_table_key&) const;
288 typedef Unordered_map<Symbol_table_key, Symbol*, Symbol_table_hash,
289 Symbol_table_eq> Symbol_table_type;
291 // The size of the symbols in the symbol table (32 or 64).
292 int size_;
294 // The symbol hash table.
295 Symbol_table_type table_;
297 // A pool of symbol names. This is used for all global symbols.
298 // Entries in the hash table point into this pool.
299 Stringpool namepool_;
301 // A pool of symbol names to go into the output file. This is used
302 // for all symbols, global and local, but only the names of symbols
303 // which will definitely be output are added to this pool.
304 Stringpool output_pool_;
306 // Forwarding symbols.
307 Unordered_map<Symbol*, Symbol*> forwarders_;
310 // We inline get_sized_symbol for efficiency.
312 template<int size>
313 Sized_symbol<size>*
314 Symbol_table::get_sized_symbol(Symbol* sym) const
316 assert(size == this->get_size());
317 return static_cast<Sized_symbol<size>*>(sym);
320 template<int size>
321 const Sized_symbol<size>*
322 Symbol_table::get_sized_symbol(const Symbol* sym) const
324 assert(size == this->get_size());
325 return static_cast<const Sized_symbol<size>*>(sym);
328 } // End namespace gold.
330 #endif // !defined(GOLD_SYMTAB_H)