Split Object into Dynobj and Relobj, incorporate elfcpp swapping changes.
[binutils.git] / gold / symtab.cc
blob091144a75ca5ae135c9ff674d823c0503122fd04
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 (reinterpret_cast<size_t>(key.first)
178 ^ reinterpret_cast<size_t>(key.second));
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.
185 bool
186 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
187 const Symbol_table_key& k2) const
189 return k1.first == k2.first && k1.second == k2.second;
192 // Make TO a symbol which forwards to FROM.
194 void
195 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
197 assert(!from->is_forwarder() && !to->is_forwarder());
198 this->forwarders_[from] = to;
199 from->set_forwarder();
202 // Resolve the forwards from FROM, returning the real symbol.
204 Symbol*
205 Symbol_table::resolve_forwards(Symbol* from) const
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;
214 // Look up a symbol by name.
216 Symbol*
217 Symbol_table::lookup(const char* name, const char* version) const
219 name = this->namepool_.find(name);
220 if (name == NULL)
221 return NULL;
222 if (version != NULL)
224 version = this->namepool_.find(version);
225 if (version == NULL)
226 return NULL;
229 Symbol_table_key key(name, version);
230 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
231 if (p == this->table_.end())
232 return NULL;
233 return p->second;
236 // Resolve a Symbol with another Symbol. This is only used in the
237 // unusual case where there are references to both an unversioned
238 // symbol and a symbol with a version, and we then discover that that
239 // version is the default version. Because this is unusual, we do
240 // this the slow way, by converting back to an ELF symbol.
242 template<int size, bool big_endian>
243 void
244 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from
245 ACCEPT_SIZE_ENDIAN)
247 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
248 elfcpp::Sym_write<size, big_endian> esym(buf);
249 // We don't bother to set the st_name field.
250 esym.put_st_value(from->value());
251 esym.put_st_size(from->symsize());
252 esym.put_st_info(from->binding(), from->type());
253 esym.put_st_other(from->visibility(), from->nonvis());
254 esym.put_st_shndx(from->shnum());
255 Symbol_table::resolve(to, esym.sym(), from->object());
258 // Add one symbol from OBJECT to the symbol table. NAME is symbol
259 // name and VERSION is the version; both are canonicalized. DEF is
260 // whether this is the default version.
262 // If DEF is true, then this is the definition of a default version of
263 // a symbol. That means that any lookup of NAME/NULL and any lookup
264 // of NAME/VERSION should always return the same symbol. This is
265 // obvious for references, but in particular we want to do this for
266 // definitions: overriding NAME/NULL should also override
267 // NAME/VERSION. If we don't do that, it would be very hard to
268 // override functions in a shared library which uses versioning.
270 // We implement this by simply making both entries in the hash table
271 // point to the same Symbol structure. That is easy enough if this is
272 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
273 // that we have seen both already, in which case they will both have
274 // independent entries in the symbol table. We can't simply change
275 // the symbol table entry, because we have pointers to the entries
276 // attached to the object files. So we mark the entry attached to the
277 // object file as a forwarder, and record it in the forwarders_ map.
278 // Note that entries in the hash table will never be marked as
279 // forwarders.
281 template<int size, bool big_endian>
282 Symbol*
283 Symbol_table::add_from_object(Object* object,
284 const char *name,
285 const char *version, bool def,
286 const elfcpp::Sym<size, big_endian>& sym)
288 Symbol* const snull = NULL;
289 std::pair<typename Symbol_table_type::iterator, bool> ins =
290 this->table_.insert(std::make_pair(std::make_pair(name, version), snull));
292 std::pair<typename Symbol_table_type::iterator, bool> insdef =
293 std::make_pair(this->table_.end(), false);
294 if (def)
296 const char* const vnull = NULL;
297 insdef = this->table_.insert(std::make_pair(std::make_pair(name, vnull),
298 snull));
301 // ins.first: an iterator, which is a pointer to a pair.
302 // ins.first->first: the key (a pair of name and version).
303 // ins.first->second: the value (Symbol*).
304 // ins.second: true if new entry was inserted, false if not.
306 Sized_symbol<size>* ret;
307 bool was_undefined;
308 bool was_common;
309 if (!ins.second)
311 // We already have an entry for NAME/VERSION.
312 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
313 SELECT_SIZE(size));
314 assert(ret != NULL);
316 was_undefined = ret->is_undefined();
317 was_common = ret->is_common();
319 Symbol_table::resolve(ret, sym, object);
321 if (def)
323 if (insdef.second)
325 // This is the first time we have seen NAME/NULL. Make
326 // NAME/NULL point to NAME/VERSION.
327 insdef.first->second = ret;
329 else
331 // This is the unfortunate case where we already have
332 // entries for both NAME/VERSION and NAME/NULL.
333 const Sized_symbol<size>* sym2;
334 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
335 insdef.first->second
336 SELECT_SIZE(size));
337 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
338 ret, sym2 SELECT_SIZE_ENDIAN(size, big_endian));
339 this->make_forwarder(insdef.first->second, ret);
340 insdef.first->second = ret;
344 else
346 // This is the first time we have seen NAME/VERSION.
347 assert(ins.first->second == NULL);
349 was_undefined = false;
350 was_common = false;
352 if (def && !insdef.second)
354 // We already have an entry for NAME/NULL. Make
355 // NAME/VERSION point to it.
356 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
357 insdef.first->second
358 SELECT_SIZE(size));
359 Symbol_table::resolve(ret, sym, object);
360 ins.first->second = ret;
362 else
364 Sized_target<size, big_endian>* target =
365 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
366 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
367 if (!target->has_make_symbol())
368 ret = new Sized_symbol<size>();
369 else
371 ret = target->make_symbol();
372 if (ret == NULL)
374 // This means that we don't want a symbol table
375 // entry after all.
376 if (!def)
377 this->table_.erase(ins.first);
378 else
380 this->table_.erase(insdef.first);
381 // Inserting insdef invalidated ins.
382 this->table_.erase(std::make_pair(name, version));
384 return NULL;
388 ret->init(name, version, object, sym);
390 ins.first->second = ret;
391 if (def)
393 // This is the first time we have seen NAME/NULL. Point
394 // it at the new entry for NAME/VERSION.
395 assert(insdef.second);
396 insdef.first->second = ret;
401 // Record every time we see a new undefined symbol, to speed up
402 // archive groups.
403 if (!was_undefined && ret->is_undefined())
404 ++this->saw_undefined_;
406 // Keep track of common symbols, to speed up common symbol
407 // allocation.
408 if (!was_common && ret->is_common())
409 this->commons_.push_back(ret);
411 return ret;
414 // Add all the symbols in a relocatable object to the hash table.
416 template<int size, bool big_endian>
417 void
418 Symbol_table::add_from_object(
419 Relobj* object,
420 const unsigned char* syms,
421 size_t count,
422 const char* sym_names,
423 size_t sym_name_size,
424 Symbol** sympointers)
426 // We take the size from the first object we see.
427 if (this->get_size() == 0)
428 this->set_size(size);
430 if (size != this->get_size() || size != object->target()->get_size())
432 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
433 program_name, object->name().c_str());
434 gold_exit(false);
437 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
439 const unsigned char* p = syms;
440 for (size_t i = 0; i < count; ++i, p += sym_size)
442 elfcpp::Sym<size, big_endian> sym(p);
443 elfcpp::Sym<size, big_endian>* psym = &sym;
445 unsigned int st_name = psym->get_st_name();
446 if (st_name >= sym_name_size)
448 fprintf(stderr,
449 _("%s: %s: bad global symbol name offset %u at %lu\n"),
450 program_name, object->name().c_str(), st_name,
451 static_cast<unsigned long>(i));
452 gold_exit(false);
455 // A symbol defined in a section which we are not including must
456 // be treated as an undefined symbol.
457 unsigned char symbuf[sym_size];
458 elfcpp::Sym<size, big_endian> sym2(symbuf);
459 unsigned int st_shndx = psym->get_st_shndx();
460 if (st_shndx != elfcpp::SHN_UNDEF
461 && st_shndx < elfcpp::SHN_LORESERVE
462 && !object->is_section_included(st_shndx))
464 memcpy(symbuf, p, sym_size);
465 elfcpp::Sym_write<size, big_endian> sw(symbuf);
466 sw.put_st_shndx(elfcpp::SHN_UNDEF);
467 psym = &sym2;
470 const char* name = sym_names + st_name;
472 // In an object file, an '@' in the name separates the symbol
473 // name from the version name. If there are two '@' characters,
474 // this is the default version.
475 const char* ver = strchr(name, '@');
477 Symbol* res;
478 if (ver == NULL)
480 name = this->namepool_.add(name);
481 res = this->add_from_object(object, name, NULL, false, *psym);
483 else
485 name = this->namepool_.add(name, ver - name);
486 bool def = false;
487 ++ver;
488 if (*ver == '@')
490 def = true;
491 ++ver;
493 ver = this->namepool_.add(ver);
494 res = this->add_from_object(object, name, ver, def, *psym);
497 *sympointers++ = res;
501 // Create and return a specially defined symbol. If ONLY_IF_REF is
502 // true, then only create the symbol if there is a reference to it.
504 template<int size, bool big_endian>
505 Sized_symbol<size>*
506 Symbol_table::define_special_symbol(Target* target, const char* name,
507 bool only_if_ref
508 ACCEPT_SIZE_ENDIAN)
510 assert(this->size_ == size);
512 Symbol* oldsym;
513 Sized_symbol<size>* sym;
515 if (only_if_ref)
517 oldsym = this->lookup(name, NULL);
518 if (oldsym == NULL || !oldsym->is_undefined())
519 return NULL;
520 sym = NULL;
522 // Canonicalize NAME.
523 name = oldsym->name();
525 else
527 // Canonicalize NAME.
528 name = this->namepool_.add(name);
530 Symbol* const snull = NULL;
531 const char* const vnull = NULL;
532 std::pair<typename Symbol_table_type::iterator, bool> ins =
533 this->table_.insert(std::make_pair(std::make_pair(name, vnull),
534 snull));
536 if (!ins.second)
538 // We already have a symbol table entry for NAME.
539 oldsym = ins.first->second;
540 assert(oldsym != NULL);
541 sym = NULL;
543 else
545 // We haven't seen this symbol before.
546 assert(ins.first->second == NULL);
548 if (!target->has_make_symbol())
549 sym = new Sized_symbol<size>();
550 else
552 assert(target->get_size() == size);
553 assert(target->is_big_endian() ? big_endian : !big_endian);
554 typedef Sized_target<size, big_endian> My_target;
555 My_target* sized_target = static_cast<My_target*>(target);
556 sym = sized_target->make_symbol();
557 if (sym == NULL)
558 return NULL;
561 ins.first->second = sym;
562 oldsym = NULL;
566 if (oldsym != NULL)
568 assert(sym == NULL);
570 sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
571 SELECT_SIZE(size));
572 assert(sym->source() == Symbol::FROM_OBJECT);
573 const int old_shnum = sym->shnum();
574 if (old_shnum != elfcpp::SHN_UNDEF
575 && old_shnum != elfcpp::SHN_COMMON
576 && !sym->object()->is_dynamic())
578 fprintf(stderr, "%s: linker defined: multiple definition of %s\n",
579 program_name, name);
580 // FIXME: Report old location. Record that we have seen an
581 // error.
582 return NULL;
585 // Our new definition is going to override the old reference.
588 return sym;
591 // Define a symbol based on an Output_data.
593 void
594 Symbol_table::define_in_output_data(Target* target, const char* name,
595 Output_data* od,
596 uint64_t value, uint64_t symsize,
597 elfcpp::STT type, elfcpp::STB binding,
598 elfcpp::STV visibility,
599 unsigned char nonvis,
600 bool offset_is_from_end,
601 bool only_if_ref)
603 assert(target->get_size() == this->size_);
604 if (this->size_ == 32)
605 this->do_define_in_output_data<32>(target, name, od, value, symsize,
606 type, binding, visibility, nonvis,
607 offset_is_from_end, only_if_ref);
608 else if (this->size_ == 64)
609 this->do_define_in_output_data<64>(target, name, od, value, symsize,
610 type, binding, visibility, nonvis,
611 offset_is_from_end, only_if_ref);
612 else
613 abort();
616 // Define a symbol in an Output_data, sized version.
618 template<int size>
619 void
620 Symbol_table::do_define_in_output_data(
621 Target* target,
622 const char* name,
623 Output_data* od,
624 typename elfcpp::Elf_types<size>::Elf_Addr value,
625 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
626 elfcpp::STT type,
627 elfcpp::STB binding,
628 elfcpp::STV visibility,
629 unsigned char nonvis,
630 bool offset_is_from_end,
631 bool only_if_ref)
633 Sized_symbol<size>* sym;
635 if (target->is_big_endian())
636 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
637 target, name, only_if_ref
638 SELECT_SIZE_ENDIAN(size, true));
639 else
640 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
641 target, name, only_if_ref
642 SELECT_SIZE_ENDIAN(size, false));
644 if (sym == NULL)
645 return;
647 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
648 offset_is_from_end);
651 // Define a symbol based on an Output_segment.
653 void
654 Symbol_table::define_in_output_segment(Target* target, const char* name,
655 Output_segment* os,
656 uint64_t value, uint64_t symsize,
657 elfcpp::STT type, elfcpp::STB binding,
658 elfcpp::STV visibility,
659 unsigned char nonvis,
660 Symbol::Segment_offset_base offset_base,
661 bool only_if_ref)
663 assert(target->get_size() == this->size_);
664 if (this->size_ == 32)
665 this->do_define_in_output_segment<32>(target, name, os, value, symsize,
666 type, binding, visibility, nonvis,
667 offset_base, only_if_ref);
668 else if (this->size_ == 64)
669 this->do_define_in_output_segment<64>(target, name, os, value, symsize,
670 type, binding, visibility, nonvis,
671 offset_base, only_if_ref);
672 else
673 abort();
676 // Define a symbol in an Output_segment, sized version.
678 template<int size>
679 void
680 Symbol_table::do_define_in_output_segment(
681 Target* target,
682 const char* name,
683 Output_segment* os,
684 typename elfcpp::Elf_types<size>::Elf_Addr value,
685 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
686 elfcpp::STT type,
687 elfcpp::STB binding,
688 elfcpp::STV visibility,
689 unsigned char nonvis,
690 Symbol::Segment_offset_base offset_base,
691 bool only_if_ref)
693 Sized_symbol<size>* sym;
695 if (target->is_big_endian())
696 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
697 target, name, only_if_ref
698 SELECT_SIZE_ENDIAN(size, true));
699 else
700 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
701 target, name, only_if_ref
702 SELECT_SIZE_ENDIAN(size, false));
704 if (sym == NULL)
705 return;
707 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
708 offset_base);
711 // Define a special symbol with a constant value. It is a multiple
712 // definition error if this symbol is already defined.
714 void
715 Symbol_table::define_as_constant(Target* target, const char* name,
716 uint64_t value, uint64_t symsize,
717 elfcpp::STT type, elfcpp::STB binding,
718 elfcpp::STV visibility, unsigned char nonvis,
719 bool only_if_ref)
721 assert(target->get_size() == this->size_);
722 if (this->size_ == 32)
723 this->do_define_as_constant<32>(target, name, value, symsize,
724 type, binding, visibility, nonvis,
725 only_if_ref);
726 else if (this->size_ == 64)
727 this->do_define_as_constant<64>(target, name, value, symsize,
728 type, binding, visibility, nonvis,
729 only_if_ref);
730 else
731 abort();
734 // Define a symbol as a constant, sized version.
736 template<int size>
737 void
738 Symbol_table::do_define_as_constant(
739 Target* target,
740 const char* name,
741 typename elfcpp::Elf_types<size>::Elf_Addr value,
742 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
743 elfcpp::STT type,
744 elfcpp::STB binding,
745 elfcpp::STV visibility,
746 unsigned char nonvis,
747 bool only_if_ref)
749 Sized_symbol<size>* sym;
751 if (target->is_big_endian())
752 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
753 target, name, only_if_ref
754 SELECT_SIZE_ENDIAN(size, true));
755 else
756 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
757 target, name, only_if_ref
758 SELECT_SIZE_ENDIAN(size, false));
760 if (sym == NULL)
761 return;
763 sym->init(name, value, symsize, type, binding, visibility, nonvis);
766 // Define a set of symbols in output sections.
768 void
769 Symbol_table::define_symbols(const Layout* layout, Target* target, int count,
770 const Define_symbol_in_section* p)
772 for (int i = 0; i < count; ++i, ++p)
774 Output_section* os = layout->find_output_section(p->output_section);
775 if (os != NULL)
776 this->define_in_output_data(target, p->name, os, p->value, p->size,
777 p->type, p->binding, p->visibility,
778 p->nonvis, p->offset_is_from_end,
779 p->only_if_ref);
780 else
781 this->define_as_constant(target, p->name, 0, p->size, p->type,
782 p->binding, p->visibility, p->nonvis,
783 p->only_if_ref);
787 // Define a set of symbols in output segments.
789 void
790 Symbol_table::define_symbols(const Layout* layout, Target* target, int count,
791 const Define_symbol_in_segment* p)
793 for (int i = 0; i < count; ++i, ++p)
795 Output_segment* os = layout->find_output_segment(p->segment_type,
796 p->segment_flags_set,
797 p->segment_flags_clear);
798 if (os != NULL)
799 this->define_in_output_segment(target, p->name, os, p->value, p->size,
800 p->type, p->binding, p->visibility,
801 p->nonvis, p->offset_base,
802 p->only_if_ref);
803 else
804 this->define_as_constant(target, p->name, 0, p->size, p->type,
805 p->binding, p->visibility, p->nonvis,
806 p->only_if_ref);
810 // Set the final values for all the symbols. Record the file offset
811 // OFF. Add their names to POOL. Return the new file offset.
813 off_t
814 Symbol_table::finalize(off_t off, Stringpool* pool)
816 off_t ret;
818 if (this->size_ == 32)
819 ret = this->sized_finalize<32>(off, pool);
820 else if (this->size_ == 64)
821 ret = this->sized_finalize<64>(off, pool);
822 else
823 abort();
825 // Now that we have the final symbol table, we can reliably note
826 // which symbols should get warnings.
827 this->warnings_.note_warnings(this);
829 return ret;
832 // Set the final value for all the symbols. This is called after
833 // Layout::finalize, so all the output sections have their final
834 // address.
836 template<int size>
837 off_t
838 Symbol_table::sized_finalize(off_t off, Stringpool* pool)
840 off = align_address(off, size >> 3);
841 this->offset_ = off;
843 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
844 Symbol_table_type::iterator p = this->table_.begin();
845 size_t count = 0;
846 while (p != this->table_.end())
848 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
850 // FIXME: Here we need to decide which symbols should go into
851 // the output file.
853 typename Sized_symbol<size>::Value_type value;
855 switch (sym->source())
857 case Symbol::FROM_OBJECT:
859 unsigned int shnum = sym->shnum();
861 // FIXME: We need some target specific support here.
862 if (shnum >= elfcpp::SHN_LORESERVE
863 && shnum != elfcpp::SHN_ABS)
865 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
866 program_name, sym->name(), shnum);
867 gold_exit(false);
870 Object* symobj = sym->object();
871 if (symobj->is_dynamic())
873 value = 0;
874 shnum = elfcpp::SHN_UNDEF;
876 else if (shnum == elfcpp::SHN_UNDEF)
877 value = 0;
878 else if (shnum == elfcpp::SHN_ABS)
879 value = sym->value();
880 else
882 Relobj* relobj = static_cast<Relobj*>(symobj);
883 off_t secoff;
884 Output_section* os = relobj->output_section(shnum, &secoff);
886 if (os == NULL)
888 // We should be able to erase this symbol from the
889 // symbol table, but at least with gcc 4.0.2
890 // std::unordered_map::erase doesn't appear to return
891 // the new iterator.
892 // p = this->table_.erase(p);
893 ++p;
894 continue;
897 value = sym->value() + os->address() + secoff;
900 break;
902 case Symbol::IN_OUTPUT_DATA:
904 Output_data* od = sym->output_data();
905 value = sym->value() + od->address();
906 if (sym->offset_is_from_end())
907 value += od->data_size();
909 break;
911 case Symbol::IN_OUTPUT_SEGMENT:
913 Output_segment* os = sym->output_segment();
914 value = sym->value() + os->vaddr();
915 switch (sym->offset_base())
917 case Symbol::SEGMENT_START:
918 break;
919 case Symbol::SEGMENT_END:
920 value += os->memsz();
921 break;
922 case Symbol::SEGMENT_BSS:
923 value += os->filesz();
924 break;
925 default:
926 abort();
929 break;
931 case Symbol::CONSTANT:
932 value = sym->value();
933 break;
935 default:
936 abort();
939 sym->set_value(value);
940 pool->add(sym->name());
941 ++count;
942 off += sym_size;
943 ++p;
946 this->output_count_ = count;
948 return off;
951 // Write out the global symbols.
953 void
954 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
955 Output_file* of) const
957 if (this->size_ == 32)
959 if (target->is_big_endian())
960 this->sized_write_globals<32, true>(target, sympool, of);
961 else
962 this->sized_write_globals<32, false>(target, sympool, of);
964 else if (this->size_ == 64)
966 if (target->is_big_endian())
967 this->sized_write_globals<64, true>(target, sympool, of);
968 else
969 this->sized_write_globals<64, false>(target, sympool, of);
971 else
972 abort();
975 // Write out the global symbols.
977 template<int size, bool big_endian>
978 void
979 Symbol_table::sized_write_globals(const Target*,
980 const Stringpool* sympool,
981 Output_file* of) const
983 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
984 unsigned char* psyms = of->get_output_view(this->offset_,
985 this->output_count_ * sym_size);
986 unsigned char* ps = psyms;
987 for (Symbol_table_type::const_iterator p = this->table_.begin();
988 p != this->table_.end();
989 ++p)
991 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
993 unsigned int shndx;
994 switch (sym->source())
996 case Symbol::FROM_OBJECT:
998 unsigned int shnum = sym->shnum();
1000 // FIXME: We need some target specific support here.
1001 if (shnum >= elfcpp::SHN_LORESERVE
1002 && shnum != elfcpp::SHN_ABS)
1004 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1005 program_name, sym->name(), sym->shnum());
1006 gold_exit(false);
1009 Object* symobj = sym->object();
1010 if (symobj->is_dynamic())
1012 // FIXME.
1013 shndx = elfcpp::SHN_UNDEF;
1015 else if (shnum == elfcpp::SHN_UNDEF || shnum == elfcpp::SHN_ABS)
1016 shndx = shnum;
1017 else
1019 Relobj* relobj = static_cast<Relobj*>(symobj);
1020 off_t secoff;
1021 Output_section* os = relobj->output_section(shnum, &secoff);
1022 if (os == NULL)
1023 continue;
1025 shndx = os->out_shndx();
1028 break;
1030 case Symbol::IN_OUTPUT_DATA:
1031 shndx = sym->output_data()->out_shndx();
1032 break;
1034 case Symbol::IN_OUTPUT_SEGMENT:
1035 shndx = elfcpp::SHN_ABS;
1036 break;
1038 case Symbol::CONSTANT:
1039 shndx = elfcpp::SHN_ABS;
1040 break;
1042 default:
1043 abort();
1046 elfcpp::Sym_write<size, big_endian> osym(ps);
1047 osym.put_st_name(sympool->get_offset(sym->name()));
1048 osym.put_st_value(sym->value());
1049 osym.put_st_size(sym->symsize());
1050 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1051 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
1052 sym->nonvis()));
1053 osym.put_st_shndx(shndx);
1055 ps += sym_size;
1058 of->write_output_view(this->offset_, this->output_count_ * sym_size, psyms);
1061 // Warnings functions.
1063 // Add a new warning.
1065 void
1066 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1067 unsigned int shndx)
1069 name = symtab->canonicalize_name(name);
1070 this->warnings_[name].set(obj, shndx);
1073 // Look through the warnings and mark the symbols for which we should
1074 // warn. This is called during Layout::finalize when we know the
1075 // sources for all the symbols.
1077 void
1078 Warnings::note_warnings(Symbol_table* symtab)
1080 for (Warning_table::iterator p = this->warnings_.begin();
1081 p != this->warnings_.end();
1082 ++p)
1084 Symbol* sym = symtab->lookup(p->first, NULL);
1085 if (sym != NULL
1086 && sym->source() == Symbol::FROM_OBJECT
1087 && sym->object() == p->second.object)
1089 sym->set_has_warning();
1091 // Read the section contents to get the warning text. It
1092 // would be nicer if we only did this if we have to actually
1093 // issue a warning. Unfortunately, warnings are issued as
1094 // we relocate sections. That means that we can not lock
1095 // the object then, as we might try to issue the same
1096 // warning multiple times simultaneously.
1097 const unsigned char* c;
1098 off_t len;
1099 c = p->second.object->section_contents(p->second.shndx, &len);
1100 p->second.set_text(reinterpret_cast<const char*>(c), len);
1105 // Issue a warning. This is called when we see a relocation against a
1106 // symbol for which has a warning.
1108 void
1109 Warnings::issue_warning(Symbol* sym, const std::string& location) const
1111 assert(sym->has_warning());
1112 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1113 assert(p != this->warnings_.end());
1114 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1115 p->second.text.c_str());
1118 // Instantiate the templates we need. We could use the configure
1119 // script to restrict this to only the ones needed for implemented
1120 // targets.
1122 template
1123 void
1124 Symbol_table::add_from_object<32, true>(
1125 Relobj* object,
1126 const unsigned char* syms,
1127 size_t count,
1128 const char* sym_names,
1129 size_t sym_name_size,
1130 Symbol** sympointers);
1132 template
1133 void
1134 Symbol_table::add_from_object<32, false>(
1135 Relobj* object,
1136 const unsigned char* syms,
1137 size_t count,
1138 const char* sym_names,
1139 size_t sym_name_size,
1140 Symbol** sympointers);
1142 template
1143 void
1144 Symbol_table::add_from_object<64, true>(
1145 Relobj* object,
1146 const unsigned char* syms,
1147 size_t count,
1148 const char* sym_names,
1149 size_t sym_name_size,
1150 Symbol** sympointers);
1152 template
1153 void
1154 Symbol_table::add_from_object<64, false>(
1155 Relobj* object,
1156 const unsigned char* syms,
1157 size_t count,
1158 const char* sym_names,
1159 size_t sym_name_size,
1160 Symbol** sympointers);
1162 } // End namespace gold.