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