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