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