*** empty log message ***
[deliverable/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"
dbe717ef 11#include "dynobj.h"
75f65a3e 12#include "output.h"
61ba1cf9 13#include "target.h"
645f8123 14#include "workqueue.h"
14bfc3f5
ILT
15#include "symtab.h"
16
17namespace gold
18{
19
20// Class Symbol.
21
ead1e424
ILT
22// Initialize fields in Symbol. This initializes everything except u_
23// and source_.
14bfc3f5 24
14bfc3f5 25void
ead1e424
ILT
26Symbol::init_fields(const char* name, const char* version,
27 elfcpp::STT type, elfcpp::STB binding,
28 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
29{
30 this->name_ = name;
31 this->version_ = version;
ead1e424
ILT
32 this->got_offset_ = 0;
33 this->type_ = type;
34 this->binding_ = binding;
35 this->visibility_ = visibility;
36 this->nonvis_ = nonvis;
37 this->is_target_special_ = false;
1564db8d
ILT
38 this->is_def_ = false;
39 this->is_forwarder_ = false;
ead1e424
ILT
40 this->in_dyn_ = false;
41 this->has_got_offset_ = false;
f6ce93d6 42 this->has_warning_ = false;
ead1e424
ILT
43}
44
45// Initialize the fields in the base class Symbol for SYM in OBJECT.
46
47template<int size, bool big_endian>
48void
49Symbol::init_base(const char* name, const char* version, Object* object,
50 const elfcpp::Sym<size, big_endian>& sym)
51{
52 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
53 sym.get_st_visibility(), sym.get_st_nonvis());
54 this->u_.from_object.object = object;
55 // FIXME: Handle SHN_XINDEX.
56 this->u_.from_object.shnum = sym.get_st_shndx();
57 this->source_ = FROM_OBJECT;
1564db8d 58 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
59}
60
ead1e424
ILT
61// Initialize the fields in the base class Symbol for a symbol defined
62// in an Output_data.
63
64void
65Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
66 elfcpp::STB binding, elfcpp::STV visibility,
67 unsigned char nonvis, bool offset_is_from_end)
68{
69 this->init_fields(name, NULL, type, binding, visibility, nonvis);
70 this->u_.in_output_data.output_data = od;
71 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
72 this->source_ = IN_OUTPUT_DATA;
73}
74
75// Initialize the fields in the base class Symbol for a symbol defined
76// in an Output_segment.
77
78void
79Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
80 elfcpp::STB binding, elfcpp::STV visibility,
81 unsigned char nonvis, Segment_offset_base offset_base)
82{
83 this->init_fields(name, NULL, type, binding, visibility, nonvis);
84 this->u_.in_output_segment.output_segment = os;
85 this->u_.in_output_segment.offset_base = offset_base;
86 this->source_ = IN_OUTPUT_SEGMENT;
87}
88
89// Initialize the fields in the base class Symbol for a symbol defined
90// as a constant.
91
92void
93Symbol::init_base(const char* name, elfcpp::STT type,
94 elfcpp::STB binding, elfcpp::STV visibility,
95 unsigned char nonvis)
96{
97 this->init_fields(name, NULL, type, binding, visibility, nonvis);
98 this->source_ = CONSTANT;
99}
100
101// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
102
103template<int size>
104template<bool big_endian>
105void
106Sized_symbol<size>::init(const char* name, const char* version, Object* object,
107 const elfcpp::Sym<size, big_endian>& sym)
108{
109 this->init_base(name, version, object, sym);
110 this->value_ = sym.get_st_value();
ead1e424
ILT
111 this->symsize_ = sym.get_st_size();
112}
113
114// Initialize the fields in Sized_symbol for a symbol defined in an
115// Output_data.
116
117template<int size>
118void
119Sized_symbol<size>::init(const char* name, Output_data* od,
120 Value_type value, Size_type symsize,
121 elfcpp::STT type, elfcpp::STB binding,
122 elfcpp::STV visibility, unsigned char nonvis,
123 bool offset_is_from_end)
124{
125 this->init_base(name, od, type, binding, visibility, nonvis,
126 offset_is_from_end);
127 this->value_ = value;
128 this->symsize_ = symsize;
129}
130
131// Initialize the fields in Sized_symbol for a symbol defined in an
132// Output_segment.
133
134template<int size>
135void
136Sized_symbol<size>::init(const char* name, Output_segment* os,
137 Value_type value, Size_type symsize,
138 elfcpp::STT type, elfcpp::STB binding,
139 elfcpp::STV visibility, unsigned char nonvis,
140 Segment_offset_base offset_base)
141{
142 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
143 this->value_ = value;
144 this->symsize_ = symsize;
145}
146
147// Initialize the fields in Sized_symbol for a symbol defined as a
148// constant.
149
150template<int size>
151void
152Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
153 elfcpp::STT type, elfcpp::STB binding,
154 elfcpp::STV visibility, unsigned char nonvis)
155{
156 this->init_base(name, type, binding, visibility, nonvis);
157 this->value_ = value;
158 this->symsize_ = symsize;
14bfc3f5
ILT
159}
160
161// Class Symbol_table.
162
163Symbol_table::Symbol_table()
ead1e424 164 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
f6ce93d6 165 forwarders_(), commons_(), warnings_()
14bfc3f5
ILT
166{
167}
168
169Symbol_table::~Symbol_table()
170{
171}
172
173// The hash function. The key is always canonicalized, so we use a
174// simple combination of the pointers.
175
176size_t
177Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
178{
f0641a0b 179 return key.first ^ key.second;
14bfc3f5
ILT
180}
181
182// The symbol table key equality function. This is only called with
183// canonicalized name and version strings, so we can use pointer
184// comparison.
185
186bool
187Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
188 const Symbol_table_key& k2) const
189{
190 return k1.first == k2.first && k1.second == k2.second;
191}
192
193// Make TO a symbol which forwards to FROM.
194
195void
196Symbol_table::make_forwarder(Symbol* from, Symbol* to)
197{
dbe717ef 198 assert(from != to);
14bfc3f5
ILT
199 assert(!from->is_forwarder() && !to->is_forwarder());
200 this->forwarders_[from] = to;
201 from->set_forwarder();
202}
203
61ba1cf9
ILT
204// Resolve the forwards from FROM, returning the real symbol.
205
14bfc3f5
ILT
206Symbol*
207Symbol_table::resolve_forwards(Symbol* from) const
208{
209 assert(from->is_forwarder());
210 Unordered_map<Symbol*, Symbol*>::const_iterator p =
211 this->forwarders_.find(from);
212 assert(p != this->forwarders_.end());
213 return p->second;
214}
215
61ba1cf9
ILT
216// Look up a symbol by name.
217
218Symbol*
219Symbol_table::lookup(const char* name, const char* version) const
220{
f0641a0b
ILT
221 Stringpool::Key name_key;
222 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
223 if (name == NULL)
224 return NULL;
f0641a0b
ILT
225
226 Stringpool::Key version_key = 0;
61ba1cf9
ILT
227 if (version != NULL)
228 {
f0641a0b 229 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
230 if (version == NULL)
231 return NULL;
232 }
233
f0641a0b 234 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
235 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
236 if (p == this->table_.end())
237 return NULL;
238 return p->second;
239}
240
14bfc3f5
ILT
241// Resolve a Symbol with another Symbol. This is only used in the
242// unusual case where there are references to both an unversioned
243// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
244// version is the default version. Because this is unusual, we do
245// this the slow way, by converting back to an ELF symbol.
14bfc3f5 246
1564db8d 247template<int size, bool big_endian>
14bfc3f5 248void
5482377d
ILT
249Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
250 ACCEPT_SIZE_ENDIAN)
14bfc3f5 251{
1564db8d
ILT
252 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
253 elfcpp::Sym_write<size, big_endian> esym(buf);
254 // We don't bother to set the st_name field.
255 esym.put_st_value(from->value());
256 esym.put_st_size(from->symsize());
257 esym.put_st_info(from->binding(), from->type());
ead1e424 258 esym.put_st_other(from->visibility(), from->nonvis());
1564db8d
ILT
259 esym.put_st_shndx(from->shnum());
260 Symbol_table::resolve(to, esym.sym(), from->object());
14bfc3f5
ILT
261}
262
263// Add one symbol from OBJECT to the symbol table. NAME is symbol
264// name and VERSION is the version; both are canonicalized. DEF is
265// whether this is the default version.
266
267// If DEF is true, then this is the definition of a default version of
268// a symbol. That means that any lookup of NAME/NULL and any lookup
269// of NAME/VERSION should always return the same symbol. This is
270// obvious for references, but in particular we want to do this for
271// definitions: overriding NAME/NULL should also override
272// NAME/VERSION. If we don't do that, it would be very hard to
273// override functions in a shared library which uses versioning.
274
275// We implement this by simply making both entries in the hash table
276// point to the same Symbol structure. That is easy enough if this is
277// the first time we see NAME/NULL or NAME/VERSION, but it is possible
278// that we have seen both already, in which case they will both have
279// independent entries in the symbol table. We can't simply change
280// the symbol table entry, because we have pointers to the entries
281// attached to the object files. So we mark the entry attached to the
282// object file as a forwarder, and record it in the forwarders_ map.
283// Note that entries in the hash table will never be marked as
284// forwarders.
285
286template<int size, bool big_endian>
287Symbol*
f6ce93d6 288Symbol_table::add_from_object(Object* object,
14bfc3f5 289 const char *name,
f0641a0b
ILT
290 Stringpool::Key name_key,
291 const char *version,
292 Stringpool::Key version_key,
293 bool def,
14bfc3f5
ILT
294 const elfcpp::Sym<size, big_endian>& sym)
295{
296 Symbol* const snull = NULL;
297 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
298 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
299 snull));
14bfc3f5
ILT
300
301 std::pair<typename Symbol_table_type::iterator, bool> insdef =
302 std::make_pair(this->table_.end(), false);
303 if (def)
304 {
f0641a0b
ILT
305 const Stringpool::Key vnull_key = 0;
306 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
307 vnull_key),
14bfc3f5
ILT
308 snull));
309 }
310
311 // ins.first: an iterator, which is a pointer to a pair.
312 // ins.first->first: the key (a pair of name and version).
313 // ins.first->second: the value (Symbol*).
314 // ins.second: true if new entry was inserted, false if not.
315
1564db8d 316 Sized_symbol<size>* ret;
ead1e424
ILT
317 bool was_undefined;
318 bool was_common;
14bfc3f5
ILT
319 if (!ins.second)
320 {
321 // We already have an entry for NAME/VERSION.
593f47df
ILT
322 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
323 SELECT_SIZE(size));
14bfc3f5 324 assert(ret != NULL);
ead1e424
ILT
325
326 was_undefined = ret->is_undefined();
327 was_common = ret->is_common();
328
14bfc3f5
ILT
329 Symbol_table::resolve(ret, sym, object);
330
331 if (def)
332 {
333 if (insdef.second)
334 {
335 // This is the first time we have seen NAME/NULL. Make
336 // NAME/NULL point to NAME/VERSION.
337 insdef.first->second = ret;
338 }
dbe717ef 339 else if (insdef.first->second != ret)
14bfc3f5
ILT
340 {
341 // This is the unfortunate case where we already have
342 // entries for both NAME/VERSION and NAME/NULL.
274e99f9 343 const Sized_symbol<size>* sym2;
593f47df 344 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d
ILT
345 insdef.first->second
346 SELECT_SIZE(size));
593f47df 347 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
5482377d 348 ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian));
14bfc3f5
ILT
349 this->make_forwarder(insdef.first->second, ret);
350 insdef.first->second = ret;
351 }
352 }
353 }
354 else
355 {
356 // This is the first time we have seen NAME/VERSION.
357 assert(ins.first->second == NULL);
ead1e424
ILT
358
359 was_undefined = false;
360 was_common = false;
361
14bfc3f5
ILT
362 if (def && !insdef.second)
363 {
364 // We already have an entry for NAME/NULL. Make
365 // NAME/VERSION point to it.
593f47df
ILT
366 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
367 insdef.first->second
368 SELECT_SIZE(size));
14bfc3f5
ILT
369 Symbol_table::resolve(ret, sym, object);
370 ins.first->second = ret;
371 }
372 else
373 {
f6ce93d6
ILT
374 Sized_target<size, big_endian>* target =
375 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
376 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
1564db8d
ILT
377 if (!target->has_make_symbol())
378 ret = new Sized_symbol<size>();
379 else
14bfc3f5 380 {
1564db8d
ILT
381 ret = target->make_symbol();
382 if (ret == NULL)
14bfc3f5
ILT
383 {
384 // This means that we don't want a symbol table
385 // entry after all.
386 if (!def)
387 this->table_.erase(ins.first);
388 else
389 {
390 this->table_.erase(insdef.first);
391 // Inserting insdef invalidated ins.
f0641a0b
ILT
392 this->table_.erase(std::make_pair(name_key,
393 version_key));
14bfc3f5
ILT
394 }
395 return NULL;
396 }
397 }
14bfc3f5 398
1564db8d
ILT
399 ret->init(name, version, object, sym);
400
14bfc3f5
ILT
401 ins.first->second = ret;
402 if (def)
403 {
404 // This is the first time we have seen NAME/NULL. Point
405 // it at the new entry for NAME/VERSION.
406 assert(insdef.second);
407 insdef.first->second = ret;
408 }
409 }
410 }
411
ead1e424
ILT
412 // Record every time we see a new undefined symbol, to speed up
413 // archive groups.
414 if (!was_undefined && ret->is_undefined())
415 ++this->saw_undefined_;
416
417 // Keep track of common symbols, to speed up common symbol
418 // allocation.
419 if (!was_common && ret->is_common())
420 this->commons_.push_back(ret);
421
14bfc3f5
ILT
422 return ret;
423}
424
f6ce93d6 425// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
426
427template<int size, bool big_endian>
428void
dbe717ef
ILT
429Symbol_table::add_from_relobj(
430 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 431 const unsigned char* syms,
14bfc3f5
ILT
432 size_t count,
433 const char* sym_names,
434 size_t sym_name_size,
435 Symbol** sympointers)
436{
437 // We take the size from the first object we see.
438 if (this->get_size() == 0)
439 this->set_size(size);
440
dbe717ef 441 if (size != this->get_size() || size != relobj->target()->get_size())
14bfc3f5
ILT
442 {
443 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
dbe717ef 444 program_name, relobj->name().c_str());
14bfc3f5
ILT
445 gold_exit(false);
446 }
447
a783673b
ILT
448 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
449
f6ce93d6 450 const unsigned char* p = syms;
a783673b 451 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
452 {
453 elfcpp::Sym<size, big_endian> sym(p);
a783673b 454 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 455
a783673b 456 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
457 if (st_name >= sym_name_size)
458 {
54dc6425
ILT
459 fprintf(stderr,
460 _("%s: %s: bad global symbol name offset %u at %lu\n"),
dbe717ef 461 program_name, relobj->name().c_str(), st_name,
14bfc3f5
ILT
462 static_cast<unsigned long>(i));
463 gold_exit(false);
464 }
465
dbe717ef
ILT
466 const char* name = sym_names + st_name;
467
a783673b
ILT
468 // A symbol defined in a section which we are not including must
469 // be treated as an undefined symbol.
470 unsigned char symbuf[sym_size];
471 elfcpp::Sym<size, big_endian> sym2(symbuf);
472 unsigned int st_shndx = psym->get_st_shndx();
473 if (st_shndx != elfcpp::SHN_UNDEF
474 && st_shndx < elfcpp::SHN_LORESERVE
dbe717ef 475 && !relobj->is_section_included(st_shndx))
a783673b
ILT
476 {
477 memcpy(symbuf, p, sym_size);
478 elfcpp::Sym_write<size, big_endian> sw(symbuf);
479 sw.put_st_shndx(elfcpp::SHN_UNDEF);
480 psym = &sym2;
481 }
482
14bfc3f5
ILT
483 // In an object file, an '@' in the name separates the symbol
484 // name from the version name. If there are two '@' characters,
485 // this is the default version.
486 const char* ver = strchr(name, '@');
487
488 Symbol* res;
489 if (ver == NULL)
490 {
f0641a0b
ILT
491 Stringpool::Key name_key;
492 name = this->namepool_.add(name, &name_key);
dbe717ef 493 res = this->add_from_object(relobj, name, name_key, NULL, 0,
f0641a0b 494 false, *psym);
14bfc3f5
ILT
495 }
496 else
497 {
f0641a0b
ILT
498 Stringpool::Key name_key;
499 name = this->namepool_.add(name, ver - name, &name_key);
500
14bfc3f5
ILT
501 bool def = false;
502 ++ver;
503 if (*ver == '@')
504 {
505 def = true;
506 ++ver;
507 }
f0641a0b
ILT
508
509 Stringpool::Key ver_key;
510 ver = this->namepool_.add(ver, &ver_key);
511
dbe717ef 512 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
f0641a0b 513 def, *psym);
14bfc3f5
ILT
514 }
515
516 *sympointers++ = res;
14bfc3f5
ILT
517 }
518}
519
dbe717ef
ILT
520// Add all the symbols in a dynamic object to the hash table.
521
522template<int size, bool big_endian>
523void
524Symbol_table::add_from_dynobj(
525 Sized_dynobj<size, big_endian>* dynobj,
526 const unsigned char* syms,
527 size_t count,
528 const char* sym_names,
529 size_t sym_name_size,
530 const unsigned char* versym,
531 size_t versym_size,
532 const std::vector<const char*>* version_map)
533{
534 // We take the size from the first object we see.
535 if (this->get_size() == 0)
536 this->set_size(size);
537
538 if (size != this->get_size() || size != dynobj->target()->get_size())
539 {
540 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
541 program_name, dynobj->name().c_str());
542 gold_exit(false);
543 }
544
545 if (versym != NULL && versym_size / 2 < count)
546 {
547 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
548 program_name, dynobj->name().c_str());
549 gold_exit(false);
550 }
551
552 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
553
554 const unsigned char* p = syms;
555 const unsigned char* vs = versym;
556 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
557 {
558 elfcpp::Sym<size, big_endian> sym(p);
559
560 // Ignore symbols with local binding.
561 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
562 continue;
563
564 unsigned int st_name = sym.get_st_name();
565 if (st_name >= sym_name_size)
566 {
567 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
568 program_name, dynobj->name().c_str(), st_name,
569 static_cast<unsigned long>(i));
570 gold_exit(false);
571 }
572
573 const char* name = sym_names + st_name;
574
575 if (versym == NULL)
576 {
577 Stringpool::Key name_key;
578 name = this->namepool_.add(name, &name_key);
579 this->add_from_object(dynobj, name, name_key, NULL, 0,
580 false, sym);
581 continue;
582 }
583
584 // Read the version information.
585
586 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
587
588 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
589 v &= elfcpp::VERSYM_VERSION;
590
591 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL))
592 {
593 // This symbol should not be visible outside the object.
594 continue;
595 }
596
597 // At this point we are definitely going to add this symbol.
598 Stringpool::Key name_key;
599 name = this->namepool_.add(name, &name_key);
600
601 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
602 {
603 // This symbol does not have a version.
604 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
605 continue;
606 }
607
608 if (v >= version_map->size())
609 {
610 fprintf(stderr,
611 _("%s: %s: versym for symbol %zu out of range: %u\n"),
612 program_name, dynobj->name().c_str(), i, v);
613 gold_exit(false);
614 }
615
616 const char* version = (*version_map)[v];
617 if (version == NULL)
618 {
619 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
620 program_name, dynobj->name().c_str(), i, v);
621 gold_exit(false);
622 }
623
624 Stringpool::Key version_key;
625 version = this->namepool_.add(version, &version_key);
626
627 // If this is an absolute symbol, and the version name and
628 // symbol name are the same, then this is the version definition
629 // symbol. These symbols exist to support using -u to pull in
630 // particular versions. We do not want to record a version for
631 // them.
632 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
633 {
634 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
635 continue;
636 }
637
638 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
639
640 this->add_from_object(dynobj, name, name_key, version, version_key,
641 def, sym);
642 }
643}
644
ead1e424
ILT
645// Create and return a specially defined symbol. If ONLY_IF_REF is
646// true, then only create the symbol if there is a reference to it.
647
648template<int size, bool big_endian>
649Sized_symbol<size>*
650Symbol_table::define_special_symbol(Target* target, const char* name,
593f47df
ILT
651 bool only_if_ref
652 ACCEPT_SIZE_ENDIAN)
ead1e424
ILT
653{
654 assert(this->size_ == size);
655
656 Symbol* oldsym;
657 Sized_symbol<size>* sym;
658
659 if (only_if_ref)
660 {
661 oldsym = this->lookup(name, NULL);
f6ce93d6 662 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424
ILT
663 return NULL;
664 sym = NULL;
665
666 // Canonicalize NAME.
667 name = oldsym->name();
668 }
669 else
670 {
671 // Canonicalize NAME.
f0641a0b
ILT
672 Stringpool::Key name_key;
673 name = this->namepool_.add(name, &name_key);
ead1e424
ILT
674
675 Symbol* const snull = NULL;
f0641a0b 676 const Stringpool::Key ver_key = 0;
ead1e424 677 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b 678 this->table_.insert(std::make_pair(std::make_pair(name_key, ver_key),
ead1e424
ILT
679 snull));
680
681 if (!ins.second)
682 {
683 // We already have a symbol table entry for NAME.
684 oldsym = ins.first->second;
685 assert(oldsym != NULL);
686 sym = NULL;
687 }
688 else
689 {
690 // We haven't seen this symbol before.
691 assert(ins.first->second == NULL);
692
693 if (!target->has_make_symbol())
694 sym = new Sized_symbol<size>();
695 else
696 {
697 assert(target->get_size() == size);
698 assert(target->is_big_endian() ? big_endian : !big_endian);
699 typedef Sized_target<size, big_endian> My_target;
700 My_target* sized_target = static_cast<My_target*>(target);
701 sym = sized_target->make_symbol();
702 if (sym == NULL)
703 return NULL;
704 }
705
706 ins.first->second = sym;
707 oldsym = NULL;
708 }
709 }
710
711 if (oldsym != NULL)
712 {
713 assert(sym == NULL);
714
593f47df
ILT
715 sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
716 SELECT_SIZE(size));
ead1e424
ILT
717 assert(sym->source() == Symbol::FROM_OBJECT);
718 const int old_shnum = sym->shnum();
719 if (old_shnum != elfcpp::SHN_UNDEF
720 && old_shnum != elfcpp::SHN_COMMON
721 && !sym->object()->is_dynamic())
722 {
723 fprintf(stderr, "%s: linker defined: multiple definition of %s\n",
724 program_name, name);
725 // FIXME: Report old location. Record that we have seen an
726 // error.
727 return NULL;
728 }
729
730 // Our new definition is going to override the old reference.
731 }
732
733 return sym;
734}
735
736// Define a symbol based on an Output_data.
737
738void
739Symbol_table::define_in_output_data(Target* target, const char* name,
740 Output_data* od,
741 uint64_t value, uint64_t symsize,
742 elfcpp::STT type, elfcpp::STB binding,
743 elfcpp::STV visibility,
744 unsigned char nonvis,
745 bool offset_is_from_end,
746 bool only_if_ref)
747{
748 assert(target->get_size() == this->size_);
749 if (this->size_ == 32)
750 this->do_define_in_output_data<32>(target, name, od, value, symsize,
751 type, binding, visibility, nonvis,
752 offset_is_from_end, only_if_ref);
753 else if (this->size_ == 64)
754 this->do_define_in_output_data<64>(target, name, od, value, symsize,
755 type, binding, visibility, nonvis,
756 offset_is_from_end, only_if_ref);
757 else
758 abort();
759}
760
761// Define a symbol in an Output_data, sized version.
762
763template<int size>
764void
765Symbol_table::do_define_in_output_data(
766 Target* target,
767 const char* name,
768 Output_data* od,
769 typename elfcpp::Elf_types<size>::Elf_Addr value,
770 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
771 elfcpp::STT type,
772 elfcpp::STB binding,
773 elfcpp::STV visibility,
774 unsigned char nonvis,
775 bool offset_is_from_end,
776 bool only_if_ref)
777{
778 Sized_symbol<size>* sym;
779
780 if (target->is_big_endian())
593f47df
ILT
781 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
782 target, name, only_if_ref
783 SELECT_SIZE_ENDIAN(size, true));
ead1e424 784 else
593f47df
ILT
785 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
786 target, name, only_if_ref
787 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
788
789 if (sym == NULL)
790 return;
791
792 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
793 offset_is_from_end);
794}
795
796// Define a symbol based on an Output_segment.
797
798void
799Symbol_table::define_in_output_segment(Target* target, const char* name,
800 Output_segment* os,
801 uint64_t value, uint64_t symsize,
802 elfcpp::STT type, elfcpp::STB binding,
803 elfcpp::STV visibility,
804 unsigned char nonvis,
805 Symbol::Segment_offset_base offset_base,
806 bool only_if_ref)
807{
808 assert(target->get_size() == this->size_);
809 if (this->size_ == 32)
810 this->do_define_in_output_segment<32>(target, name, os, value, symsize,
811 type, binding, visibility, nonvis,
812 offset_base, only_if_ref);
813 else if (this->size_ == 64)
814 this->do_define_in_output_segment<64>(target, name, os, value, symsize,
815 type, binding, visibility, nonvis,
816 offset_base, only_if_ref);
817 else
818 abort();
819}
820
821// Define a symbol in an Output_segment, sized version.
822
823template<int size>
824void
825Symbol_table::do_define_in_output_segment(
826 Target* target,
827 const char* name,
828 Output_segment* os,
829 typename elfcpp::Elf_types<size>::Elf_Addr value,
830 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
831 elfcpp::STT type,
832 elfcpp::STB binding,
833 elfcpp::STV visibility,
834 unsigned char nonvis,
835 Symbol::Segment_offset_base offset_base,
836 bool only_if_ref)
837{
838 Sized_symbol<size>* sym;
839
840 if (target->is_big_endian())
593f47df
ILT
841 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
842 target, name, only_if_ref
843 SELECT_SIZE_ENDIAN(size, true));
ead1e424 844 else
593f47df
ILT
845 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
846 target, name, only_if_ref
847 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
848
849 if (sym == NULL)
850 return;
851
852 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
853 offset_base);
854}
855
856// Define a special symbol with a constant value. It is a multiple
857// definition error if this symbol is already defined.
858
859void
860Symbol_table::define_as_constant(Target* target, const char* name,
861 uint64_t value, uint64_t symsize,
862 elfcpp::STT type, elfcpp::STB binding,
863 elfcpp::STV visibility, unsigned char nonvis,
864 bool only_if_ref)
865{
866 assert(target->get_size() == this->size_);
867 if (this->size_ == 32)
868 this->do_define_as_constant<32>(target, name, value, symsize,
869 type, binding, visibility, nonvis,
870 only_if_ref);
871 else if (this->size_ == 64)
872 this->do_define_as_constant<64>(target, name, value, symsize,
873 type, binding, visibility, nonvis,
874 only_if_ref);
875 else
876 abort();
877}
878
879// Define a symbol as a constant, sized version.
880
881template<int size>
882void
883Symbol_table::do_define_as_constant(
884 Target* target,
885 const char* name,
886 typename elfcpp::Elf_types<size>::Elf_Addr value,
887 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
888 elfcpp::STT type,
889 elfcpp::STB binding,
890 elfcpp::STV visibility,
891 unsigned char nonvis,
892 bool only_if_ref)
893{
894 Sized_symbol<size>* sym;
895
896 if (target->is_big_endian())
593f47df
ILT
897 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
898 target, name, only_if_ref
899 SELECT_SIZE_ENDIAN(size, true));
ead1e424 900 else
593f47df
ILT
901 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
902 target, name, only_if_ref
903 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
904
905 if (sym == NULL)
906 return;
907
908 sym->init(name, value, symsize, type, binding, visibility, nonvis);
909}
910
911// Define a set of symbols in output sections.
912
913void
914Symbol_table::define_symbols(const Layout* layout, Target* target, int count,
915 const Define_symbol_in_section* p)
916{
917 for (int i = 0; i < count; ++i, ++p)
918 {
919 Output_section* os = layout->find_output_section(p->output_section);
920 if (os != NULL)
921 this->define_in_output_data(target, p->name, os, p->value, p->size,
922 p->type, p->binding, p->visibility,
923 p->nonvis, p->offset_is_from_end,
924 p->only_if_ref);
925 else
926 this->define_as_constant(target, p->name, 0, p->size, p->type,
927 p->binding, p->visibility, p->nonvis,
928 p->only_if_ref);
929 }
930}
931
932// Define a set of symbols in output segments.
933
934void
935Symbol_table::define_symbols(const Layout* layout, Target* target, int count,
936 const Define_symbol_in_segment* p)
937{
938 for (int i = 0; i < count; ++i, ++p)
939 {
940 Output_segment* os = layout->find_output_segment(p->segment_type,
941 p->segment_flags_set,
942 p->segment_flags_clear);
943 if (os != NULL)
944 this->define_in_output_segment(target, p->name, os, p->value, p->size,
945 p->type, p->binding, p->visibility,
946 p->nonvis, p->offset_base,
947 p->only_if_ref);
948 else
949 this->define_as_constant(target, p->name, 0, p->size, p->type,
950 p->binding, p->visibility, p->nonvis,
951 p->only_if_ref);
952 }
953}
954
75f65a3e
ILT
955// Set the final values for all the symbols. Record the file offset
956// OFF. Add their names to POOL. Return the new file offset.
54dc6425 957
75f65a3e
ILT
958off_t
959Symbol_table::finalize(off_t off, Stringpool* pool)
54dc6425 960{
f6ce93d6
ILT
961 off_t ret;
962
75f65a3e 963 if (this->size_ == 32)
f6ce93d6 964 ret = this->sized_finalize<32>(off, pool);
61ba1cf9 965 else if (this->size_ == 64)
f6ce93d6 966 ret = this->sized_finalize<64>(off, pool);
61ba1cf9
ILT
967 else
968 abort();
f6ce93d6
ILT
969
970 // Now that we have the final symbol table, we can reliably note
971 // which symbols should get warnings.
972 this->warnings_.note_warnings(this);
973
974 return ret;
75f65a3e
ILT
975}
976
ead1e424
ILT
977// Set the final value for all the symbols. This is called after
978// Layout::finalize, so all the output sections have their final
979// address.
75f65a3e
ILT
980
981template<int size>
982off_t
983Symbol_table::sized_finalize(off_t off, Stringpool* pool)
984{
ead1e424 985 off = align_address(off, size >> 3);
75f65a3e
ILT
986 this->offset_ = off;
987
988 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
989 Symbol_table_type::iterator p = this->table_.begin();
61ba1cf9 990 size_t count = 0;
75f65a3e 991 while (p != this->table_.end())
54dc6425 992 {
75f65a3e 993 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 994
75f65a3e
ILT
995 // FIXME: Here we need to decide which symbols should go into
996 // the output file.
997
ead1e424 998 typename Sized_symbol<size>::Value_type value;
75f65a3e 999
ead1e424 1000 switch (sym->source())
75f65a3e 1001 {
ead1e424
ILT
1002 case Symbol::FROM_OBJECT:
1003 {
1004 unsigned int shnum = sym->shnum();
1005
1006 // FIXME: We need some target specific support here.
1007 if (shnum >= elfcpp::SHN_LORESERVE
1008 && shnum != elfcpp::SHN_ABS)
1009 {
1010 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1011 program_name, sym->name(), shnum);
1012 gold_exit(false);
1013 }
1014
f6ce93d6
ILT
1015 Object* symobj = sym->object();
1016 if (symobj->is_dynamic())
1017 {
1018 value = 0;
1019 shnum = elfcpp::SHN_UNDEF;
1020 }
1021 else if (shnum == elfcpp::SHN_UNDEF)
ead1e424
ILT
1022 value = 0;
1023 else if (shnum == elfcpp::SHN_ABS)
1024 value = sym->value();
1025 else
1026 {
f6ce93d6 1027 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1028 off_t secoff;
f6ce93d6 1029 Output_section* os = relobj->output_section(shnum, &secoff);
ead1e424
ILT
1030
1031 if (os == NULL)
1032 {
1033 // We should be able to erase this symbol from the
1034 // symbol table, but at least with gcc 4.0.2
1035 // std::unordered_map::erase doesn't appear to return
1036 // the new iterator.
1037 // p = this->table_.erase(p);
1038 ++p;
1039 continue;
1040 }
1041
1042 value = sym->value() + os->address() + secoff;
1043 }
1044 }
1045 break;
1046
1047 case Symbol::IN_OUTPUT_DATA:
1048 {
1049 Output_data* od = sym->output_data();
1050 value = sym->value() + od->address();
1051 if (sym->offset_is_from_end())
1052 value += od->data_size();
1053 }
1054 break;
1055
1056 case Symbol::IN_OUTPUT_SEGMENT:
1057 {
1058 Output_segment* os = sym->output_segment();
1059 value = sym->value() + os->vaddr();
1060 switch (sym->offset_base())
1061 {
1062 case Symbol::SEGMENT_START:
1063 break;
1064 case Symbol::SEGMENT_END:
1065 value += os->memsz();
1066 break;
1067 case Symbol::SEGMENT_BSS:
1068 value += os->filesz();
1069 break;
1070 default:
1071 abort();
1072 }
1073 }
1074 break;
1075
1076 case Symbol::CONSTANT:
1077 value = sym->value();
1078 break;
1079
1080 default:
1081 abort();
54dc6425 1082 }
ead1e424
ILT
1083
1084 sym->set_value(value);
f0641a0b 1085 pool->add(sym->name(), NULL);
ead1e424
ILT
1086 ++count;
1087 off += sym_size;
1088 ++p;
54dc6425 1089 }
75f65a3e 1090
61ba1cf9
ILT
1091 this->output_count_ = count;
1092
75f65a3e 1093 return off;
54dc6425
ILT
1094}
1095
61ba1cf9
ILT
1096// Write out the global symbols.
1097
1098void
1099Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1100 Output_file* of) const
1101{
1102 if (this->size_ == 32)
1103 {
1104 if (target->is_big_endian())
1105 this->sized_write_globals<32, true>(target, sympool, of);
1106 else
1107 this->sized_write_globals<32, false>(target, sympool, of);
1108 }
1109 else if (this->size_ == 64)
1110 {
1111 if (target->is_big_endian())
1112 this->sized_write_globals<64, true>(target, sympool, of);
1113 else
1114 this->sized_write_globals<64, false>(target, sympool, of);
1115 }
1116 else
1117 abort();
1118}
1119
1120// Write out the global symbols.
1121
1122template<int size, bool big_endian>
1123void
1124Symbol_table::sized_write_globals(const Target*,
1125 const Stringpool* sympool,
1126 Output_file* of) const
1127{
1128 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1129 unsigned char* psyms = of->get_output_view(this->offset_,
1130 this->output_count_ * sym_size);
1131 unsigned char* ps = psyms;
1132 for (Symbol_table_type::const_iterator p = this->table_.begin();
1133 p != this->table_.end();
1134 ++p)
1135 {
1136 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1137
ead1e424
ILT
1138 unsigned int shndx;
1139 switch (sym->source())
1140 {
1141 case Symbol::FROM_OBJECT:
1142 {
1143 unsigned int shnum = sym->shnum();
1144
1145 // FIXME: We need some target specific support here.
1146 if (shnum >= elfcpp::SHN_LORESERVE
1147 && shnum != elfcpp::SHN_ABS)
1148 {
1149 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1150 program_name, sym->name(), sym->shnum());
1151 gold_exit(false);
1152 }
1153
f6ce93d6
ILT
1154 Object* symobj = sym->object();
1155 if (symobj->is_dynamic())
1156 {
1157 // FIXME.
1158 shndx = elfcpp::SHN_UNDEF;
1159 }
1160 else if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS)
ead1e424
ILT
1161 shndx = shnum;
1162 else
1163 {
f6ce93d6 1164 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1165 off_t secoff;
f6ce93d6 1166 Output_section* os = relobj->output_section(shnum, &secoff);
ead1e424
ILT
1167 if (os == NULL)
1168 continue;
1169
1170 shndx = os->out_shndx();
1171 }
1172 }
1173 break;
1174
1175 case Symbol::IN_OUTPUT_DATA:
1176 shndx = sym->output_data()->out_shndx();
1177 break;
1178
1179 case Symbol::IN_OUTPUT_SEGMENT:
1180 shndx = elfcpp::SHN_ABS;
1181 break;
1182
1183 case Symbol::CONSTANT:
1184 shndx = elfcpp::SHN_ABS;
1185 break;
1186
1187 default:
1188 abort();
1189 }
61ba1cf9
ILT
1190
1191 elfcpp::Sym_write<size, big_endian> osym(ps);
1192 osym.put_st_name(sympool->get_offset(sym->name()));
1193 osym.put_st_value(sym->value());
1194 osym.put_st_size(sym->symsize());
1195 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
ead1e424
ILT
1196 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
1197 sym->nonvis()));
1198 osym.put_st_shndx(shndx);
61ba1cf9
ILT
1199
1200 ps += sym_size;
1201 }
1202
1203 of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms);
1204}
1205
f6ce93d6
ILT
1206// Warnings functions.
1207
1208// Add a new warning.
1209
1210void
1211Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1212 unsigned int shndx)
1213{
1214 name = symtab->canonicalize_name(name);
1215 this->warnings_[name].set(obj, shndx);
1216}
1217
1218// Look through the warnings and mark the symbols for which we should
1219// warn. This is called during Layout::finalize when we know the
1220// sources for all the symbols.
1221
1222void
1223Warnings::note_warnings(Symbol_table* symtab)
1224{
1225 for (Warning_table::iterator p = this->warnings_.begin();
1226 p != this->warnings_.end();
1227 ++p)
1228 {
1229 Symbol* sym = symtab->lookup(p->first, NULL);
1230 if (sym != NULL
1231 && sym->source() == Symbol::FROM_OBJECT
1232 && sym->object() == p->second.object)
1233 {
1234 sym->set_has_warning();
1235
1236 // Read the section contents to get the warning text. It
1237 // would be nicer if we only did this if we have to actually
1238 // issue a warning. Unfortunately, warnings are issued as
1239 // we relocate sections. That means that we can not lock
1240 // the object then, as we might try to issue the same
1241 // warning multiple times simultaneously.
645f8123
ILT
1242 {
1243 Task_locker_obj<Object> tl(*p->second.object);
1244 const unsigned char* c;
1245 off_t len;
1246 c = p->second.object->section_contents(p->second.shndx, &len);
1247 p->second.set_text(reinterpret_cast<const char*>(c), len);
1248 }
f6ce93d6
ILT
1249 }
1250 }
1251}
1252
1253// Issue a warning. This is called when we see a relocation against a
1254// symbol for which has a warning.
1255
1256void
1257Warnings::issue_warning(Symbol* sym, const std::string& location) const
1258{
1259 assert(sym->has_warning());
1260 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1261 assert(p != this->warnings_.end());
1262 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1263 p->second.text.c_str());
1264}
1265
14bfc3f5
ILT
1266// Instantiate the templates we need. We could use the configure
1267// script to restrict this to only the ones needed for implemented
1268// targets.
1269
1270template
1271void
dbe717ef
ILT
1272Symbol_table::add_from_relobj<32, true>(
1273 Sized_relobj<32, true>* relobj,
f6ce93d6 1274 const unsigned char* syms,
14bfc3f5
ILT
1275 size_t count,
1276 const char* sym_names,
1277 size_t sym_name_size,
1278 Symbol** sympointers);
1279
1280template
1281void
dbe717ef
ILT
1282Symbol_table::add_from_relobj<32, false>(
1283 Sized_relobj<32, false>* relobj,
f6ce93d6 1284 const unsigned char* syms,
14bfc3f5
ILT
1285 size_t count,
1286 const char* sym_names,
1287 size_t sym_name_size,
1288 Symbol** sympointers);
1289
1290template
1291void
dbe717ef
ILT
1292Symbol_table::add_from_relobj<64, true>(
1293 Sized_relobj<64, true>* relobj,
f6ce93d6 1294 const unsigned char* syms,
14bfc3f5
ILT
1295 size_t count,
1296 const char* sym_names,
1297 size_t sym_name_size,
1298 Symbol** sympointers);
1299
1300template
1301void
dbe717ef
ILT
1302Symbol_table::add_from_relobj<64, false>(
1303 Sized_relobj<64, false>* relobj,
f6ce93d6 1304 const unsigned char* syms,
14bfc3f5
ILT
1305 size_t count,
1306 const char* sym_names,
1307 size_t sym_name_size,
1308 Symbol** sympointers);
1309
dbe717ef
ILT
1310template
1311void
1312Symbol_table::add_from_dynobj<32, true>(
1313 Sized_dynobj<32, true>* dynobj,
1314 const unsigned char* syms,
1315 size_t count,
1316 const char* sym_names,
1317 size_t sym_name_size,
1318 const unsigned char* versym,
1319 size_t versym_size,
1320 const std::vector<const char*>* version_map);
1321
1322template
1323void
1324Symbol_table::add_from_dynobj<32, false>(
1325 Sized_dynobj<32, false>* dynobj,
1326 const unsigned char* syms,
1327 size_t count,
1328 const char* sym_names,
1329 size_t sym_name_size,
1330 const unsigned char* versym,
1331 size_t versym_size,
1332 const std::vector<const char*>* version_map);
1333
1334template
1335void
1336Symbol_table::add_from_dynobj<64, true>(
1337 Sized_dynobj<64, true>* dynobj,
1338 const unsigned char* syms,
1339 size_t count,
1340 const char* sym_names,
1341 size_t sym_name_size,
1342 const unsigned char* versym,
1343 size_t versym_size,
1344 const std::vector<const char*>* version_map);
1345
1346template
1347void
1348Symbol_table::add_from_dynobj<64, false>(
1349 Sized_dynobj<64, false>* dynobj,
1350 const unsigned char* syms,
1351 size_t count,
1352 const char* sym_names,
1353 size_t sym_name_size,
1354 const unsigned char* versym,
1355 size_t versym_size,
1356 const std::vector<const char*>* version_map);
1357
14bfc3f5 1358} // End namespace gold.
This page took 0.086075 seconds and 4 git commands to generate.