]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
* ser-mingw.c (free_pipe_state, pipe_wait_handle): Update
[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
1564db8d 136template<int size, bool big_endian>
14bfc3f5 137void
5482377d
ILT
138Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
139 ACCEPT_SIZE_ENDIAN)
14bfc3f5 140{
1564db8d
ILT
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());
14bfc3f5
ILT
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
175template<int size, bool big_endian>
176Symbol*
177Symbol_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
1564db8d 200 Sized_symbol<size>* ret;
14bfc3f5
ILT
201 if (!ins.second)
202 {
203 // We already have an entry for NAME/VERSION.
5482377d
ILT
204 ret = this->get_sized_symbol SELECT_SIZE_NAME (ins.first->second
205 SELECT_SIZE(size));
14bfc3f5
ILT
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.
274e99f9 221 const Sized_symbol<size>* sym2;
5482377d
ILT
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));
14bfc3f5
ILT
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.
5482377d
ILT
240 ret = this->get_sized_symbol SELECT_SIZE_NAME (insdef.first->second
241 SELECT_SIZE(size));
14bfc3f5
ILT
242 Symbol_table::resolve(ret, sym, object);
243 ins.first->second = ret;
244 }
245 else
246 {
14bfc3f5 247 Sized_target<size, big_endian>* target = object->sized_target();
1564db8d
ILT
248 if (!target->has_make_symbol())
249 ret = new Sized_symbol<size>();
250 else
14bfc3f5 251 {
1564db8d
ILT
252 ret = target->make_symbol();
253 if (ret == NULL)
14bfc3f5
ILT
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 }
14bfc3f5 268
1564db8d
ILT
269 ret->init(name, version, object, sym);
270
14bfc3f5
ILT
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
287template<int size, bool big_endian>
288void
289Symbol_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 {
54dc6425
ILT
316 fprintf(stderr,
317 _("%s: %s: bad global symbol name offset %u at %lu\n"),
14bfc3f5
ILT
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
75f65a3e
ILT
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.
54dc6425 358
75f65a3e
ILT
359off_t
360Symbol_table::finalize(off_t off, Stringpool* pool)
54dc6425 361{
75f65a3e
ILT
362 if (this->size_ == 32)
363 return this->sized_finalize<32>(off, pool);
61ba1cf9 364 else if (this->size_ == 64)
75f65a3e 365 return this->sized_finalize<64>(off, pool);
61ba1cf9
ILT
366 else
367 abort();
75f65a3e
ILT
368}
369
370// Set the final value for all the symbols.
371
372template<int size>
373off_t
374Symbol_table::sized_finalize(off_t off, Stringpool* pool)
375{
61ba1cf9 376 off = (off + (size >> 3) - 1) & ~ ((size >> 3) - 1);
75f65a3e
ILT
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();
61ba1cf9 381 size_t count = 0;
75f65a3e 382 while (p != this->table_.end())
54dc6425 383 {
75f65a3e 384 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 385
75f65a3e
ILT
386 // FIXME: Here we need to decide which symbols should go into
387 // the output file.
388
61ba1cf9
ILT
389 // FIXME: This is wrong.
390 if (sym->shnum() >= elfcpp::SHN_LORESERVE)
391 {
392 ++p;
393 continue;
394 }
395
75f65a3e
ILT
396 const Object::Map_to_output* mo =
397 sym->object()->section_output_info(sym->shnum());
398
399 if (mo->output_section == NULL)
54dc6425 400 {
75f65a3e
ILT
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 {
61ba1cf9
ILT
410 sym->set_value(sym->value()
411 + mo->output_section->address()
412 + mo->offset);
75f65a3e
ILT
413 pool->add(sym->name());
414 ++p;
61ba1cf9 415 ++count;
75f65a3e 416 off += sym_size;
54dc6425 417 }
54dc6425 418 }
75f65a3e 419
61ba1cf9
ILT
420 this->output_count_ = count;
421
75f65a3e 422 return off;
54dc6425
ILT
423}
424
61ba1cf9
ILT
425// Write out the global symbols.
426
427void
428Symbol_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
451template<int size, bool big_endian>
452void
453Symbol_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
14bfc3f5
ILT
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
497template
498void
499Symbol_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
507template
508void
509Symbol_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
517template
518void
519Symbol_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
527template
528void
529Symbol_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.