Rework stringpool and hash tables so that we always generate the same
[binutils.git] / gold / symtab.cc
blob054e07fba5e90cef3881612b3cd11ffd373c91c6
1 // symtab.cc -- the gold symbol table
3 #include "gold.h"
5 #include <cassert>
6 #include <stdint.h>
7 #include <string>
8 #include <utility>
10 #include "object.h"
11 #include "output.h"
12 #include "target.h"
13 #include "symtab.h"
15 namespace gold
18 // Class Symbol.
20 // Initialize fields in Symbol. This initializes everything except u_
21 // and source_.
23 void
24 Symbol::init_fields(const char* name, const char* version,
25 elfcpp::STT type, elfcpp::STB binding,
26 elfcpp::STV visibility, unsigned char nonvis)
28 this->name_ = name;
29 this->version_ = version;
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;
36 this->is_def_ = false;
37 this->is_forwarder_ = false;
38 this->in_dyn_ = false;
39 this->has_got_offset_ = false;
40 this->has_warning_ = false;
43 // Initialize the fields in the base class Symbol for SYM in OBJECT.
45 template<int size, bool big_endian>
46 void
47 Symbol::init_base(const char* name, const char* version, Object* object,
48 const elfcpp::Sym<size, big_endian>& sym)
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;
56 this->in_dyn_ = object->is_dynamic();
59 // Initialize the fields in the base class Symbol for a symbol defined
60 // in an Output_data.
62 void
63 Symbol::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)
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;
73 // Initialize the fields in the base class Symbol for a symbol defined
74 // in an Output_segment.
76 void
77 Symbol::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)
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;
87 // Initialize the fields in the base class Symbol for a symbol defined
88 // as a constant.
90 void
91 Symbol::init_base(const char* name, elfcpp::STT type,
92 elfcpp::STB binding, elfcpp::STV visibility,
93 unsigned char nonvis)
95 this->init_fields(name, NULL, type, binding, visibility, nonvis);
96 this->source_ = CONSTANT;
99 // Initialize the fields in Sized_symbol for SYM in OBJECT.
101 template<int size>
102 template<bool big_endian>
103 void
104 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
105 const elfcpp::Sym<size, big_endian>& sym)
107 this->init_base(name, version, object, sym);
108 this->value_ = sym.get_st_value();
109 this->symsize_ = sym.get_st_size();
112 // Initialize the fields in Sized_symbol for a symbol defined in an
113 // Output_data.
115 template<int size>
116 void
117 Sized_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)
123 this->init_base(name, od, type, binding, visibility, nonvis,
124 offset_is_from_end);
125 this->value_ = value;
126 this->symsize_ = symsize;
129 // Initialize the fields in Sized_symbol for a symbol defined in an
130 // Output_segment.
132 template<int size>
133 void
134 Sized_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)
140 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
141 this->value_ = value;
142 this->symsize_ = symsize;
145 // Initialize the fields in Sized_symbol for a symbol defined as a
146 // constant.
148 template<int size>
149 void
150 Sized_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)
154 this->init_base(name, type, binding, visibility, nonvis);
155 this->value_ = value;
156 this->symsize_ = symsize;
159 // Class Symbol_table.
161 Symbol_table::Symbol_table()
162 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
163 forwarders_(), commons_(), warnings_()
167 Symbol_table::~Symbol_table()
171 // The hash function. The key is always canonicalized, so we use a
172 // simple combination of the pointers.
174 size_t
175 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
177 return key.first ^ key.second;
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.
184 bool
185 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
186 const Symbol_table_key& k2) const
188 return k1.first == k2.first && k1.second == k2.second;
191 // Make TO a symbol which forwards to FROM.
193 void
194 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
196 assert(!from->is_forwarder() && !to->is_forwarder());
197 this->forwarders_[from] = to;
198 from->set_forwarder();
201 // Resolve the forwards from FROM, returning the real symbol.
203 Symbol*
204 Symbol_table::resolve_forwards(Symbol* from) const
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;
213 // Look up a symbol by name.
215 Symbol*
216 Symbol_table::lookup(const char* name, const char* version) const
218 Stringpool::Key name_key;
219 name = this->namepool_.find(name, &name_key);
220 if (name == NULL)
221 return NULL;
223 Stringpool::Key version_key = 0;
224 if (version != NULL)
226 version = this->namepool_.find(version, &version_key);
227 if (version == NULL)
228 return NULL;
231 Symbol_table_key key(name_key, version_key);
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;
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
241 // version is the default version. Because this is unusual, we do
242 // this the slow way, by converting back to an ELF symbol.
244 template<int size, bool big_endian>
245 void
246 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
247 ACCEPT_SIZE_ENDIAN)
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());
255 esym.put_st_other(from->visibility(), from->nonvis());
256 esym.put_st_shndx(from->shnum());
257 Symbol_table::resolve(to, esym.sym(), from->object());
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.
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.
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.
283 template<int size, bool big_endian>
284 Symbol*
285 Symbol_table::add_from_object(Object* object,
286 const char *name,
287 Stringpool::Key name_key,
288 const char *version,
289 Stringpool::Key version_key,
290 bool def,
291 const elfcpp::Sym<size, big_endian>& sym)
293 Symbol* const snull = NULL;
294 std::pair<typename Symbol_table_type::iterator, bool> ins =
295 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
296 snull));
298 std::pair<typename Symbol_table_type::iterator, bool> insdef =
299 std::make_pair(this->table_.end(), false);
300 if (def)
302 const Stringpool::Key vnull_key = 0;
303 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
304 vnull_key),
305 snull));
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.
313 Sized_symbol<size>* ret;
314 bool was_undefined;
315 bool was_common;
316 if (!ins.second)
318 // We already have an entry for NAME/VERSION.
319 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
320 SELECT_SIZE(size));
321 assert(ret != NULL);
323 was_undefined = ret->is_undefined();
324 was_common = ret->is_common();
326 Symbol_table::resolve(ret, sym, object);
328 if (def)
330 if (insdef.second)
332 // This is the first time we have seen NAME/NULL. Make
333 // NAME/NULL point to NAME/VERSION.
334 insdef.first->second = ret;
336 else
338 // This is the unfortunate case where we already have
339 // entries for both NAME/VERSION and NAME/NULL.
340 const Sized_symbol<size>* sym2;
341 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
342 insdef.first->second
343 SELECT_SIZE(size));
344 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
345 ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian));
346 this->make_forwarder(insdef.first->second, ret);
347 insdef.first->second = ret;
351 else
353 // This is the first time we have seen NAME/VERSION.
354 assert(ins.first->second == NULL);
356 was_undefined = false;
357 was_common = false;
359 if (def && !insdef.second)
361 // We already have an entry for NAME/NULL. Make
362 // NAME/VERSION point to it.
363 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
364 insdef.first->second
365 SELECT_SIZE(size));
366 Symbol_table::resolve(ret, sym, object);
367 ins.first->second = ret;
369 else
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));
374 if (!target->has_make_symbol())
375 ret = new Sized_symbol<size>();
376 else
378 ret = target->make_symbol();
379 if (ret == NULL)
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
387 this->table_.erase(insdef.first);
388 // Inserting insdef invalidated ins.
389 this->table_.erase(std::make_pair(name_key,
390 version_key));
392 return NULL;
396 ret->init(name, version, object, sym);
398 ins.first->second = ret;
399 if (def)
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;
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_;
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);
419 return ret;
422 // Add all the symbols in a relocatable object to the hash table.
424 template<int size, bool big_endian>
425 void
426 Symbol_table::add_from_object(
427 Relobj* object,
428 const unsigned char* syms,
429 size_t count,
430 const char* sym_names,
431 size_t sym_name_size,
432 Symbol** sympointers)
434 // We take the size from the first object we see.
435 if (this->get_size() == 0)
436 this->set_size(size);
438 if (size != this->get_size() || size != object->target()->get_size())
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);
445 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
447 const unsigned char* p = syms;
448 for (size_t i = 0; i < count; ++i, p += sym_size)
450 elfcpp::Sym<size, big_endian> sym(p);
451 elfcpp::Sym<size, big_endian>* psym = &sym;
453 unsigned int st_name = psym->get_st_name();
454 if (st_name >= sym_name_size)
456 fprintf(stderr,
457 _("%s: %s: bad global symbol name offset %u at %lu\n"),
458 program_name, object->name().c_str(), st_name,
459 static_cast<unsigned long>(i));
460 gold_exit(false);
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))
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;
478 const char* name = sym_names + st_name;
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, '@');
485 Symbol* res;
486 if (ver == NULL)
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);
493 else
495 Stringpool::Key name_key;
496 name = this->namepool_.add(name, ver - name, &name_key);
498 bool def = false;
499 ++ver;
500 if (*ver == '@')
502 def = true;
503 ++ver;
506 Stringpool::Key ver_key;
507 ver = this->namepool_.add(ver, &ver_key);
509 res = this->add_from_object(object, name, name_key, ver, ver_key,
510 def, *psym);
513 *sympointers++ = res;
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.
520 template<int size, bool big_endian>
521 Sized_symbol<size>*
522 Symbol_table::define_special_symbol(Target* target, const char* name,
523 bool only_if_ref
524 ACCEPT_SIZE_ENDIAN)
526 assert(this->size_ == size);
528 Symbol* oldsym;
529 Sized_symbol<size>* sym;
531 if (only_if_ref)
533 oldsym = this->lookup(name, NULL);
534 if (oldsym == NULL || !oldsym->is_undefined())
535 return NULL;
536 sym = NULL;
538 // Canonicalize NAME.
539 name = oldsym->name();
541 else
543 // Canonicalize NAME.
544 Stringpool::Key name_key;
545 name = this->namepool_.add(name, &name_key);
547 Symbol* const snull = NULL;
548 const Stringpool::Key ver_key = 0;
549 std::pair<typename Symbol_table_type::iterator, bool> ins =
550 this->table_.insert(std::make_pair(std::make_pair(name_key, ver_key),
551 snull));
553 if (!ins.second)
555 // We already have a symbol table entry for NAME.
556 oldsym = ins.first->second;
557 assert(oldsym != NULL);
558 sym = NULL;
560 else
562 // We haven't seen this symbol before.
563 assert(ins.first->second == NULL);
565 if (!target->has_make_symbol())
566 sym = new Sized_symbol<size>();
567 else
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;
578 ins.first->second = sym;
579 oldsym = NULL;
583 if (oldsym != NULL)
585 assert(sym == NULL);
587 sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
588 SELECT_SIZE(size));
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())
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;
602 // Our new definition is going to override the old reference.
605 return sym;
608 // Define a symbol based on an Output_data.
610 void
611 Symbol_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)
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();
633 // Define a symbol in an Output_data, sized version.
635 template<int size>
636 void
637 Symbol_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)
650 Sized_symbol<size>* sym;
652 if (target->is_big_endian())
653 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
654 target, name, only_if_ref
655 SELECT_SIZE_ENDIAN(size, true));
656 else
657 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
658 target, name, only_if_ref
659 SELECT_SIZE_ENDIAN(size, false));
661 if (sym == NULL)
662 return;
664 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
665 offset_is_from_end);
668 // Define a symbol based on an Output_segment.
670 void
671 Symbol_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)
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();
693 // Define a symbol in an Output_segment, sized version.
695 template<int size>
696 void
697 Symbol_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)
710 Sized_symbol<size>* sym;
712 if (target->is_big_endian())
713 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
714 target, name, only_if_ref
715 SELECT_SIZE_ENDIAN(size, true));
716 else
717 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
718 target, name, only_if_ref
719 SELECT_SIZE_ENDIAN(size, false));
721 if (sym == NULL)
722 return;
724 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
725 offset_base);
728 // Define a special symbol with a constant value. It is a multiple
729 // definition error if this symbol is already defined.
731 void
732 Symbol_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)
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();
751 // Define a symbol as a constant, sized version.
753 template<int size>
754 void
755 Symbol_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)
766 Sized_symbol<size>* sym;
768 if (target->is_big_endian())
769 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
770 target, name, only_if_ref
771 SELECT_SIZE_ENDIAN(size, true));
772 else
773 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
774 target, name, only_if_ref
775 SELECT_SIZE_ENDIAN(size, false));
777 if (sym == NULL)
778 return;
780 sym->init(name, value, symsize, type, binding, visibility, nonvis);
783 // Define a set of symbols in output sections.
785 void
786 Symbol_table::define_symbols(const Layout* layout, Target* target, int count,
787 const Define_symbol_in_section* p)
789 for (int i = 0; i < count; ++i, ++p)
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);
804 // Define a set of symbols in output segments.
806 void
807 Symbol_table::define_symbols(const Layout* layout, Target* target, int count,
808 const Define_symbol_in_segment* p)
810 for (int i = 0; i < count; ++i, ++p)
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);
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.
830 off_t
831 Symbol_table::finalize(off_t off, Stringpool* pool)
833 off_t ret;
835 if (this->size_ == 32)
836 ret = this->sized_finalize<32>(off, pool);
837 else if (this->size_ == 64)
838 ret = this->sized_finalize<64>(off, pool);
839 else
840 abort();
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);
846 return ret;
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.
853 template<int size>
854 off_t
855 Symbol_table::sized_finalize(off_t off, Stringpool* pool)
857 off = align_address(off, size >> 3);
858 this->offset_ = off;
860 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
861 Symbol_table_type::iterator p = this->table_.begin();
862 size_t count = 0;
863 while (p != this->table_.end())
865 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
867 // FIXME: Here we need to decide which symbols should go into
868 // the output file.
870 typename Sized_symbol<size>::Value_type value;
872 switch (sym->source())
874 case Symbol::FROM_OBJECT:
876 unsigned int shnum = sym->shnum();
878 // FIXME: We need some target specific support here.
879 if (shnum >= elfcpp::SHN_LORESERVE
880 && shnum != elfcpp::SHN_ABS)
882 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
883 program_name, sym->name(), shnum);
884 gold_exit(false);
887 Object* symobj = sym->object();
888 if (symobj->is_dynamic())
890 value = 0;
891 shnum = elfcpp::SHN_UNDEF;
893 else if (shnum == elfcpp::SHN_UNDEF)
894 value = 0;
895 else if (shnum == elfcpp::SHN_ABS)
896 value = sym->value();
897 else
899 Relobj* relobj = static_cast<Relobj*>(symobj);
900 off_t secoff;
901 Output_section* os = relobj->output_section(shnum, &secoff);
903 if (os == NULL)
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;
914 value = sym->value() + os->address() + secoff;
917 break;
919 case Symbol::IN_OUTPUT_DATA:
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();
926 break;
928 case Symbol::IN_OUTPUT_SEGMENT:
930 Output_segment* os = sym->output_segment();
931 value = sym->value() + os->vaddr();
932 switch (sym->offset_base())
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();
946 break;
948 case Symbol::CONSTANT:
949 value = sym->value();
950 break;
952 default:
953 abort();
956 sym->set_value(value);
957 pool->add(sym->name(), NULL);
958 ++count;
959 off += sym_size;
960 ++p;
963 this->output_count_ = count;
965 return off;
968 // Write out the global symbols.
970 void
971 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
972 Output_file* of) const
974 if (this->size_ == 32)
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);
981 else if (this->size_ == 64)
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);
988 else
989 abort();
992 // Write out the global symbols.
994 template<int size, bool big_endian>
995 void
996 Symbol_table::sized_write_globals(const Target*,
997 const Stringpool* sympool,
998 Output_file* of) const
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)
1008 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1010 unsigned int shndx;
1011 switch (sym->source())
1013 case Symbol::FROM_OBJECT:
1015 unsigned int shnum = sym->shnum();
1017 // FIXME: We need some target specific support here.
1018 if (shnum >= elfcpp::SHN_LORESERVE
1019 && shnum != elfcpp::SHN_ABS)
1021 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1022 program_name, sym->name(), sym->shnum());
1023 gold_exit(false);
1026 Object* symobj = sym->object();
1027 if (symobj->is_dynamic())
1029 // FIXME.
1030 shndx = elfcpp::SHN_UNDEF;
1032 else if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS)
1033 shndx = shnum;
1034 else
1036 Relobj* relobj = static_cast<Relobj*>(symobj);
1037 off_t secoff;
1038 Output_section* os = relobj->output_section(shnum, &secoff);
1039 if (os == NULL)
1040 continue;
1042 shndx = os->out_shndx();
1045 break;
1047 case Symbol::IN_OUTPUT_DATA:
1048 shndx = sym->output_data()->out_shndx();
1049 break;
1051 case Symbol::IN_OUTPUT_SEGMENT:
1052 shndx = elfcpp::SHN_ABS;
1053 break;
1055 case Symbol::CONSTANT:
1056 shndx = elfcpp::SHN_ABS;
1057 break;
1059 default:
1060 abort();
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()));
1068 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
1069 sym->nonvis()));
1070 osym.put_st_shndx(shndx);
1072 ps += sym_size;
1075 of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms);
1078 // Warnings functions.
1080 // Add a new warning.
1082 void
1083 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1084 unsigned int shndx)
1086 name = symtab->canonicalize_name(name);
1087 this->warnings_[name].set(obj, shndx);
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.
1094 void
1095 Warnings::note_warnings(Symbol_table* symtab)
1097 for (Warning_table::iterator p = this->warnings_.begin();
1098 p != this->warnings_.end();
1099 ++p)
1101 Symbol* sym = symtab->lookup(p->first, NULL);
1102 if (sym != NULL
1103 && sym->source() == Symbol::FROM_OBJECT
1104 && sym->object() == p->second.object)
1106 sym->set_has_warning();
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);
1122 // Issue a warning. This is called when we see a relocation against a
1123 // symbol for which has a warning.
1125 void
1126 Warnings::issue_warning(Symbol* sym, const std::string& location) const
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());
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.
1139 template
1140 void
1141 Symbol_table::add_from_object<32, true>(
1142 Relobj* object,
1143 const unsigned char* syms,
1144 size_t count,
1145 const char* sym_names,
1146 size_t sym_name_size,
1147 Symbol** sympointers);
1149 template
1150 void
1151 Symbol_table::add_from_object<32, false>(
1152 Relobj* object,
1153 const unsigned char* syms,
1154 size_t count,
1155 const char* sym_names,
1156 size_t sym_name_size,
1157 Symbol** sympointers);
1159 template
1160 void
1161 Symbol_table::add_from_object<64, true>(
1162 Relobj* object,
1163 const unsigned char* syms,
1164 size_t count,
1165 const char* sym_names,
1166 size_t sym_name_size,
1167 Symbol** sympointers);
1169 template
1170 void
1171 Symbol_table::add_from_object<64, false>(
1172 Relobj* object,
1173 const unsigned char* syms,
1174 size_t count,
1175 const char* sym_names,
1176 size_t sym_name_size,
1177 Symbol** sympointers);
1179 } // End namespace gold.