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