Clean up HAVE_MEMBER_TEMPLATE_SPECIFICATIONS somewhat.
[binutils-gdb.git] / gold / symtab.cc
1 // symtab.cc -- the gold symbol table
2
3 #include "gold.h"
4
5 #include <cassert>
6 #include <stdint.h>
7 #include <string>
8 #include <utility>
9
10 #include "object.h"
11 #include "output.h"
12 #include "target.h"
13 #include "symtab.h"
14
15 namespace gold
16 {
17
18 // Class Symbol.
19
20 // Initialize the fields in the base class Symbol.
21
22 template<int size, bool big_endian>
23 void
24 Symbol::init_base(const char* name, const char* version, Object* object,
25 const elfcpp::Sym<size, big_endian>& sym)
26 {
27 this->name_ = name;
28 this->version_ = version;
29 this->object_ = object;
30 this->shnum_ = sym.get_st_shndx(); // FIXME: Handle SHN_XINDEX.
31 this->type_ = sym.get_st_type();
32 this->binding_ = sym.get_st_bind();
33 this->visibility_ = sym.get_st_visibility();
34 this->other_ = sym.get_st_nonvis();
35 this->is_special_ = false;
36 this->is_def_ = false;
37 this->is_forwarder_ = false;
38 this->in_dyn_ = object->is_dynamic();
39 }
40
41 // Initialize the fields in Sized_symbol.
42
43 template<int size>
44 template<bool big_endian>
45 void
46 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
47 const elfcpp::Sym<size, big_endian>& sym)
48 {
49 this->init_base(name, version, object, sym);
50 this->value_ = sym.get_st_value();
51 this->size_ = sym.get_st_size();
52 }
53
54 // Class Symbol_table.
55
56 Symbol_table::Symbol_table()
57 : size_(0), offset_(0), table_(), namepool_(), forwarders_()
58 {
59 }
60
61 Symbol_table::~Symbol_table()
62 {
63 }
64
65 // The hash function. The key is always canonicalized, so we use a
66 // simple combination of the pointers.
67
68 size_t
69 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
70 {
71 return (reinterpret_cast<size_t>(key.first)
72 ^ reinterpret_cast<size_t>(key.second));
73 }
74
75 // The symbol table key equality function. This is only called with
76 // canonicalized name and version strings, so we can use pointer
77 // comparison.
78
79 bool
80 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
81 const Symbol_table_key& k2) const
82 {
83 return k1.first == k2.first && k1.second == k2.second;
84 }
85
86 // Make TO a symbol which forwards to FROM.
87
88 void
89 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
90 {
91 assert(!from->is_forwarder() && !to->is_forwarder());
92 this->forwarders_[from] = to;
93 from->set_forwarder();
94 }
95
96 // Resolve the forwards from FROM, returning the real symbol.
97
98 Symbol*
99 Symbol_table::resolve_forwards(Symbol* from) const
100 {
101 assert(from->is_forwarder());
102 Unordered_map<Symbol*, Symbol*>::const_iterator p =
103 this->forwarders_.find(from);
104 assert(p != this->forwarders_.end());
105 return p->second;
106 }
107
108 // Look up a symbol by name.
109
110 Symbol*
111 Symbol_table::lookup(const char* name, const char* version) const
112 {
113 name = this->namepool_.find(name);
114 if (name == NULL)
115 return NULL;
116 if (version != NULL)
117 {
118 version = this->namepool_.find(version);
119 if (version == NULL)
120 return NULL;
121 }
122
123 Symbol_table_key key(name, version);
124 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
125 if (p == this->table_.end())
126 return NULL;
127 return p->second;
128 }
129
130 // Resolve a Symbol with another Symbol. This is only used in the
131 // unusual case where there are references to both an unversioned
132 // symbol and a symbol with a version, and we then discover that that
133 // version is the default version. Because this is unusual, we do
134 // this the slow way, by converting back to an ELF symbol.
135
136 template<int size, bool big_endian>
137 void
138 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
139 ACCEPT_SIZE_ENDIAN)
140 {
141 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
142 elfcpp::Sym_write<size, big_endian> esym(buf);
143 // We don't bother to set the st_name field.
144 esym.put_st_value(from->value());
145 esym.put_st_size(from->symsize());
146 esym.put_st_info(from->binding(), from->type());
147 esym.put_st_other(from->visibility(), from->other());
148 esym.put_st_shndx(from->shnum());
149 Symbol_table::resolve(to, esym.sym(), from->object());
150 }
151
152 // Add one symbol from OBJECT to the symbol table. NAME is symbol
153 // name and VERSION is the version; both are canonicalized. DEF is
154 // whether this is the default version.
155
156 // If DEF is true, then this is the definition of a default version of
157 // a symbol. That means that any lookup of NAME/NULL and any lookup
158 // of NAME/VERSION should always return the same symbol. This is
159 // obvious for references, but in particular we want to do this for
160 // definitions: overriding NAME/NULL should also override
161 // NAME/VERSION. If we don't do that, it would be very hard to
162 // override functions in a shared library which uses versioning.
163
164 // We implement this by simply making both entries in the hash table
165 // point to the same Symbol structure. That is easy enough if this is
166 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
167 // that we have seen both already, in which case they will both have
168 // independent entries in the symbol table. We can't simply change
169 // the symbol table entry, because we have pointers to the entries
170 // attached to the object files. So we mark the entry attached to the
171 // object file as a forwarder, and record it in the forwarders_ map.
172 // Note that entries in the hash table will never be marked as
173 // forwarders.
174
175 template<int size, bool big_endian>
176 Symbol*
177 Symbol_table::add_from_object(Sized_object<size, big_endian>* object,
178 const char *name,
179 const char *version, bool def,
180 const elfcpp::Sym<size, big_endian>& sym)
181 {
182 Symbol* const snull = NULL;
183 std::pair<typename Symbol_table_type::iterator, bool> ins =
184 this->table_.insert(std::make_pair(std::make_pair(name, version), snull));
185
186 std::pair<typename Symbol_table_type::iterator, bool> insdef =
187 std::make_pair(this->table_.end(), false);
188 if (def)
189 {
190 const char* const vnull = NULL;
191 insdef = this->table_.insert(std::make_pair(std::make_pair(name, vnull),
192 snull));
193 }
194
195 // ins.first: an iterator, which is a pointer to a pair.
196 // ins.first->first: the key (a pair of name and version).
197 // ins.first->second: the value (Symbol*).
198 // ins.second: true if new entry was inserted, false if not.
199
200 Sized_symbol<size>* ret;
201 if (!ins.second)
202 {
203 // We already have an entry for NAME/VERSION.
204 ret = this->get_sized_symbol SELECT_SIZE_NAME (ins.first->second
205 SELECT_SIZE(size));
206 assert(ret != NULL);
207 Symbol_table::resolve(ret, sym, object);
208
209 if (def)
210 {
211 if (insdef.second)
212 {
213 // This is the first time we have seen NAME/NULL. Make
214 // NAME/NULL point to NAME/VERSION.
215 insdef.first->second = ret;
216 }
217 else
218 {
219 // This is the unfortunate case where we already have
220 // entries for both NAME/VERSION and NAME/NULL.
221 const Sized_symbol<size>* sym2;
222 sym2 = this->get_sized_symbol SELECT_SIZE_NAME (
223 insdef.first->second
224 SELECT_SIZE(size));
225 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME (
226 ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian));
227 this->make_forwarder(insdef.first->second, ret);
228 insdef.first->second = ret;
229 }
230 }
231 }
232 else
233 {
234 // This is the first time we have seen NAME/VERSION.
235 assert(ins.first->second == NULL);
236 if (def && !insdef.second)
237 {
238 // We already have an entry for NAME/NULL. Make
239 // NAME/VERSION point to it.
240 ret = this->get_sized_symbol SELECT_SIZE_NAME (insdef.first->second
241 SELECT_SIZE(size));
242 Symbol_table::resolve(ret, sym, object);
243 ins.first->second = ret;
244 }
245 else
246 {
247 Sized_target<size, big_endian>* target = object->sized_target();
248 if (!target->has_make_symbol())
249 ret = new Sized_symbol<size>();
250 else
251 {
252 ret = target->make_symbol();
253 if (ret == NULL)
254 {
255 // This means that we don't want a symbol table
256 // entry after all.
257 if (!def)
258 this->table_.erase(ins.first);
259 else
260 {
261 this->table_.erase(insdef.first);
262 // Inserting insdef invalidated ins.
263 this->table_.erase(std::make_pair(name, version));
264 }
265 return NULL;
266 }
267 }
268
269 ret->init(name, version, object, sym);
270
271 ins.first->second = ret;
272 if (def)
273 {
274 // This is the first time we have seen NAME/NULL. Point
275 // it at the new entry for NAME/VERSION.
276 assert(insdef.second);
277 insdef.first->second = ret;
278 }
279 }
280 }
281
282 return ret;
283 }
284
285 // Add all the symbols in an object to the hash table.
286
287 template<int size, bool big_endian>
288 void
289 Symbol_table::add_from_object(
290 Sized_object<size, big_endian>* object,
291 const elfcpp::Sym<size, big_endian>* syms,
292 size_t count,
293 const char* sym_names,
294 size_t sym_name_size,
295 Symbol** sympointers)
296 {
297 // We take the size from the first object we see.
298 if (this->get_size() == 0)
299 this->set_size(size);
300
301 if (size != this->get_size() || size != object->target()->get_size())
302 {
303 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
304 program_name, object->name().c_str());
305 gold_exit(false);
306 }
307
308 const unsigned char* p = reinterpret_cast<const unsigned char*>(syms);
309 for (size_t i = 0; i < count; ++i)
310 {
311 elfcpp::Sym<size, big_endian> sym(p);
312
313 unsigned int st_name = sym.get_st_name();
314 if (st_name >= sym_name_size)
315 {
316 fprintf(stderr,
317 _("%s: %s: bad global symbol name offset %u at %lu\n"),
318 program_name, object->name().c_str(), st_name,
319 static_cast<unsigned long>(i));
320 gold_exit(false);
321 }
322
323 const char* name = sym_names + st_name;
324
325 // In an object file, an '@' in the name separates the symbol
326 // name from the version name. If there are two '@' characters,
327 // this is the default version.
328 const char* ver = strchr(name, '@');
329
330 Symbol* res;
331 if (ver == NULL)
332 {
333 name = this->namepool_.add(name);
334 res = this->add_from_object(object, name, NULL, false, sym);
335 }
336 else
337 {
338 name = this->namepool_.add(name, ver - name);
339 bool def = false;
340 ++ver;
341 if (*ver == '@')
342 {
343 def = true;
344 ++ver;
345 }
346 ver = this->namepool_.add(ver);
347 res = this->add_from_object(object, name, ver, def, sym);
348 }
349
350 *sympointers++ = res;
351
352 p += elfcpp::Elf_sizes<size>::sym_size;
353 }
354 }
355
356 // Set the final values for all the symbols. Record the file offset
357 // OFF. Add their names to POOL. Return the new file offset.
358
359 off_t
360 Symbol_table::finalize(off_t off, Stringpool* pool)
361 {
362 if (this->size_ == 32)
363 return this->sized_finalize<32>(off, pool);
364 else if (this->size_ == 64)
365 return this->sized_finalize<64>(off, pool);
366 else
367 abort();
368 }
369
370 // Set the final value for all the symbols.
371
372 template<int size>
373 off_t
374 Symbol_table::sized_finalize(off_t off, Stringpool* pool)
375 {
376 off = (off + (size >> 3) - 1) & ~ ((size >> 3) - 1);
377 this->offset_ = off;
378
379 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
380 Symbol_table_type::iterator p = this->table_.begin();
381 size_t count = 0;
382 while (p != this->table_.end())
383 {
384 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
385
386 // FIXME: Here we need to decide which symbols should go into
387 // the output file.
388
389 // FIXME: This is wrong.
390 if (sym->shnum() >= elfcpp::SHN_LORESERVE)
391 {
392 ++p;
393 continue;
394 }
395
396 const Object::Map_to_output* mo =
397 sym->object()->section_output_info(sym->shnum());
398
399 if (mo->output_section == NULL)
400 {
401 // We should be able to erase this symbol from the symbol
402 // table, but at least with gcc 4.0.2
403 // std::unordered_map::erase doesn't appear to return the
404 // new iterator.
405 // p = this->table_.erase(p);
406 ++p;
407 }
408 else
409 {
410 sym->set_value(sym->value()
411 + mo->output_section->address()
412 + mo->offset);
413 pool->add(sym->name());
414 ++p;
415 ++count;
416 off += sym_size;
417 }
418 }
419
420 this->output_count_ = count;
421
422 return off;
423 }
424
425 // Write out the global symbols.
426
427 void
428 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
429 Output_file* of) const
430 {
431 if (this->size_ == 32)
432 {
433 if (target->is_big_endian())
434 this->sized_write_globals<32, true>(target, sympool, of);
435 else
436 this->sized_write_globals<32, false>(target, sympool, of);
437 }
438 else if (this->size_ == 64)
439 {
440 if (target->is_big_endian())
441 this->sized_write_globals<64, true>(target, sympool, of);
442 else
443 this->sized_write_globals<64, false>(target, sympool, of);
444 }
445 else
446 abort();
447 }
448
449 // Write out the global symbols.
450
451 template<int size, bool big_endian>
452 void
453 Symbol_table::sized_write_globals(const Target*,
454 const Stringpool* sympool,
455 Output_file* of) const
456 {
457 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
458 unsigned char* psyms = of->get_output_view(this->offset_,
459 this->output_count_ * sym_size);
460 unsigned char* ps = psyms;
461 for (Symbol_table_type::const_iterator p = this->table_.begin();
462 p != this->table_.end();
463 ++p)
464 {
465 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
466
467 // FIXME: This repeats sized_finalize().
468
469 // FIXME: This is wrong.
470 if (sym->shnum() >= elfcpp::SHN_LORESERVE)
471 continue;
472
473 const Object::Map_to_output* mo =
474 sym->object()->section_output_info(sym->shnum());
475
476 if (mo->output_section == NULL)
477 continue;
478
479 elfcpp::Sym_write<size, big_endian> osym(ps);
480 osym.put_st_name(sympool->get_offset(sym->name()));
481 osym.put_st_value(sym->value());
482 osym.put_st_size(sym->symsize());
483 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
484 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->other()));
485 osym.put_st_shndx(mo->output_section->shndx());
486
487 ps += sym_size;
488 }
489
490 of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms);
491 }
492
493 // Instantiate the templates we need. We could use the configure
494 // script to restrict this to only the ones needed for implemented
495 // targets.
496
497 template
498 void
499 Symbol_table::add_from_object<32, true>(
500 Sized_object<32, true>* object,
501 const elfcpp::Sym<32, true>* syms,
502 size_t count,
503 const char* sym_names,
504 size_t sym_name_size,
505 Symbol** sympointers);
506
507 template
508 void
509 Symbol_table::add_from_object<32, false>(
510 Sized_object<32, false>* object,
511 const elfcpp::Sym<32, false>* syms,
512 size_t count,
513 const char* sym_names,
514 size_t sym_name_size,
515 Symbol** sympointers);
516
517 template
518 void
519 Symbol_table::add_from_object<64, true>(
520 Sized_object<64, true>* object,
521 const elfcpp::Sym<64, true>* syms,
522 size_t count,
523 const char* sym_names,
524 size_t sym_name_size,
525 Symbol** sympointers);
526
527 template
528 void
529 Symbol_table::add_from_object<64, false>(
530 Sized_object<64, false>* object,
531 const elfcpp::Sym<64, false>* syms,
532 size_t count,
533 const char* sym_names,
534 size_t sym_name_size,
535 Symbol** sympointers);
536
537 } // End namespace gold.