Generate version information.
[binutils.git] / gold / symtab.cc
blob5b61152652d568abacd90cc708a25ef2b8b8ef32
1 // symtab.cc -- the gold symbol table
3 #include "gold.h"
5 #include <stdint.h>
6 #include <string>
7 #include <utility>
9 #include "object.h"
10 #include "dynobj.h"
11 #include "output.h"
12 #include "target.h"
13 #include "workqueue.h"
14 #include "symtab.h"
16 namespace gold
19 // Class Symbol.
21 // Initialize fields in Symbol. This initializes everything except u_
22 // and source_.
24 void
25 Symbol::init_fields(const char* name, const char* version,
26 elfcpp::STT type, elfcpp::STB binding,
27 elfcpp::STV visibility, unsigned char nonvis)
29 this->name_ = name;
30 this->version_ = version;
31 this->symtab_index_ = 0;
32 this->dynsym_index_ = 0;
33 this->got_offset_ = 0;
34 this->type_ = type;
35 this->binding_ = binding;
36 this->visibility_ = visibility;
37 this->nonvis_ = nonvis;
38 this->is_target_special_ = false;
39 this->is_def_ = false;
40 this->is_forwarder_ = false;
41 this->needs_dynsym_entry_ = false;
42 this->in_dyn_ = false;
43 this->has_got_offset_ = false;
44 this->has_warning_ = false;
47 // Initialize the fields in the base class Symbol for SYM in OBJECT.
49 template<int size, bool big_endian>
50 void
51 Symbol::init_base(const char* name, const char* version, Object* object,
52 const elfcpp::Sym<size, big_endian>& sym)
54 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
55 sym.get_st_visibility(), sym.get_st_nonvis());
56 this->u_.from_object.object = object;
57 // FIXME: Handle SHN_XINDEX.
58 this->u_.from_object.shndx = sym.get_st_shndx();
59 this->source_ = FROM_OBJECT;
60 this->in_dyn_ = object->is_dynamic();
63 // Initialize the fields in the base class Symbol for a symbol defined
64 // in an Output_data.
66 void
67 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
68 elfcpp::STB binding, elfcpp::STV visibility,
69 unsigned char nonvis, bool offset_is_from_end)
71 this->init_fields(name, NULL, type, binding, visibility, nonvis);
72 this->u_.in_output_data.output_data = od;
73 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
74 this->source_ = IN_OUTPUT_DATA;
77 // Initialize the fields in the base class Symbol for a symbol defined
78 // in an Output_segment.
80 void
81 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
82 elfcpp::STB binding, elfcpp::STV visibility,
83 unsigned char nonvis, Segment_offset_base offset_base)
85 this->init_fields(name, NULL, type, binding, visibility, nonvis);
86 this->u_.in_output_segment.output_segment = os;
87 this->u_.in_output_segment.offset_base = offset_base;
88 this->source_ = IN_OUTPUT_SEGMENT;
91 // Initialize the fields in the base class Symbol for a symbol defined
92 // as a constant.
94 void
95 Symbol::init_base(const char* name, elfcpp::STT type,
96 elfcpp::STB binding, elfcpp::STV visibility,
97 unsigned char nonvis)
99 this->init_fields(name, NULL, type, binding, visibility, nonvis);
100 this->source_ = CONSTANT;
103 // Initialize the fields in Sized_symbol for SYM in OBJECT.
105 template<int size>
106 template<bool big_endian>
107 void
108 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
109 const elfcpp::Sym<size, big_endian>& sym)
111 this->init_base(name, version, object, sym);
112 this->value_ = sym.get_st_value();
113 this->symsize_ = sym.get_st_size();
116 // Initialize the fields in Sized_symbol for a symbol defined in an
117 // Output_data.
119 template<int size>
120 void
121 Sized_symbol<size>::init(const char* name, Output_data* od,
122 Value_type value, Size_type symsize,
123 elfcpp::STT type, elfcpp::STB binding,
124 elfcpp::STV visibility, unsigned char nonvis,
125 bool offset_is_from_end)
127 this->init_base(name, od, type, binding, visibility, nonvis,
128 offset_is_from_end);
129 this->value_ = value;
130 this->symsize_ = symsize;
133 // Initialize the fields in Sized_symbol for a symbol defined in an
134 // Output_segment.
136 template<int size>
137 void
138 Sized_symbol<size>::init(const char* name, Output_segment* os,
139 Value_type value, Size_type symsize,
140 elfcpp::STT type, elfcpp::STB binding,
141 elfcpp::STV visibility, unsigned char nonvis,
142 Segment_offset_base offset_base)
144 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
145 this->value_ = value;
146 this->symsize_ = symsize;
149 // Initialize the fields in Sized_symbol for a symbol defined as a
150 // constant.
152 template<int size>
153 void
154 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
155 elfcpp::STT type, elfcpp::STB binding,
156 elfcpp::STV visibility, unsigned char nonvis)
158 this->init_base(name, type, binding, visibility, nonvis);
159 this->value_ = value;
160 this->symsize_ = symsize;
163 // Class Symbol_table.
165 Symbol_table::Symbol_table()
166 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
167 forwarders_(), commons_(), warnings_()
171 Symbol_table::~Symbol_table()
175 // The hash function. The key is always canonicalized, so we use a
176 // simple combination of the pointers.
178 size_t
179 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
181 return key.first ^ key.second;
184 // The symbol table key equality function. This is only called with
185 // canonicalized name and version strings, so we can use pointer
186 // comparison.
188 bool
189 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
190 const Symbol_table_key& k2) const
192 return k1.first == k2.first && k1.second == k2.second;
195 // Make TO a symbol which forwards to FROM.
197 void
198 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
200 gold_assert(from != to);
201 gold_assert(!from->is_forwarder() && !to->is_forwarder());
202 this->forwarders_[from] = to;
203 from->set_forwarder();
206 // Resolve the forwards from FROM, returning the real symbol.
208 Symbol*
209 Symbol_table::resolve_forwards(const Symbol* from) const
211 gold_assert(from->is_forwarder());
212 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
213 this->forwarders_.find(from);
214 gold_assert(p != this->forwarders_.end());
215 return p->second;
218 // Look up a symbol by name.
220 Symbol*
221 Symbol_table::lookup(const char* name, const char* version) const
223 Stringpool::Key name_key;
224 name = this->namepool_.find(name, &name_key);
225 if (name == NULL)
226 return NULL;
228 Stringpool::Key version_key = 0;
229 if (version != NULL)
231 version = this->namepool_.find(version, &version_key);
232 if (version == NULL)
233 return NULL;
236 Symbol_table_key key(name_key, version_key);
237 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
238 if (p == this->table_.end())
239 return NULL;
240 return p->second;
243 // Resolve a Symbol with another Symbol. This is only used in the
244 // unusual case where there are references to both an unversioned
245 // symbol and a symbol with a version, and we then discover that that
246 // version is the default version. Because this is unusual, we do
247 // this the slow way, by converting back to an ELF symbol.
249 template<int size, bool big_endian>
250 void
251 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
252 const char* version ACCEPT_SIZE_ENDIAN)
254 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
255 elfcpp::Sym_write<size, big_endian> esym(buf);
256 // We don't bother to set the st_name field.
257 esym.put_st_value(from->value());
258 esym.put_st_size(from->symsize());
259 esym.put_st_info(from->binding(), from->type());
260 esym.put_st_other(from->visibility(), from->nonvis());
261 esym.put_st_shndx(from->shndx());
262 Symbol_table::resolve(to, esym.sym(), from->object(), version);
265 // Add one symbol from OBJECT to the symbol table. NAME is symbol
266 // name and VERSION is the version; both are canonicalized. DEF is
267 // whether this is the default version.
269 // If DEF is true, then this is the definition of a default version of
270 // a symbol. That means that any lookup of NAME/NULL and any lookup
271 // of NAME/VERSION should always return the same symbol. This is
272 // obvious for references, but in particular we want to do this for
273 // definitions: overriding NAME/NULL should also override
274 // NAME/VERSION. If we don't do that, it would be very hard to
275 // override functions in a shared library which uses versioning.
277 // We implement this by simply making both entries in the hash table
278 // point to the same Symbol structure. That is easy enough if this is
279 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
280 // that we have seen both already, in which case they will both have
281 // independent entries in the symbol table. We can't simply change
282 // the symbol table entry, because we have pointers to the entries
283 // attached to the object files. So we mark the entry attached to the
284 // object file as a forwarder, and record it in the forwarders_ map.
285 // Note that entries in the hash table will never be marked as
286 // forwarders.
288 template<int size, bool big_endian>
289 Symbol*
290 Symbol_table::add_from_object(Object* object,
291 const char *name,
292 Stringpool::Key name_key,
293 const char *version,
294 Stringpool::Key version_key,
295 bool def,
296 const elfcpp::Sym<size, big_endian>& sym)
298 Symbol* const snull = NULL;
299 std::pair<typename Symbol_table_type::iterator, bool> ins =
300 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
301 snull));
303 std::pair<typename Symbol_table_type::iterator, bool> insdef =
304 std::make_pair(this->table_.end(), false);
305 if (def)
307 const Stringpool::Key vnull_key = 0;
308 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
309 vnull_key),
310 snull));
313 // ins.first: an iterator, which is a pointer to a pair.
314 // ins.first->first: the key (a pair of name and version).
315 // ins.first->second: the value (Symbol*).
316 // ins.second: true if new entry was inserted, false if not.
318 Sized_symbol<size>* ret;
319 bool was_undefined;
320 bool was_common;
321 if (!ins.second)
323 // We already have an entry for NAME/VERSION.
324 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
325 SELECT_SIZE(size));
326 gold_assert(ret != NULL);
328 was_undefined = ret->is_undefined();
329 was_common = ret->is_common();
331 Symbol_table::resolve(ret, sym, object, version);
333 if (def)
335 if (insdef.second)
337 // This is the first time we have seen NAME/NULL. Make
338 // NAME/NULL point to NAME/VERSION.
339 insdef.first->second = ret;
341 else if (insdef.first->second != ret)
343 // This is the unfortunate case where we already have
344 // entries for both NAME/VERSION and NAME/NULL.
345 const Sized_symbol<size>* sym2;
346 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
347 insdef.first->second
348 SELECT_SIZE(size));
349 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
350 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
351 this->make_forwarder(insdef.first->second, ret);
352 insdef.first->second = ret;
356 else
358 // This is the first time we have seen NAME/VERSION.
359 gold_assert(ins.first->second == NULL);
361 was_undefined = false;
362 was_common = false;
364 if (def && !insdef.second)
366 // We already have an entry for NAME/NULL. If we override
367 // it, then change it to NAME/VERSION.
368 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
369 insdef.first->second
370 SELECT_SIZE(size));
371 Symbol_table::resolve(ret, sym, object, version);
372 ins.first->second = ret;
374 else
376 Sized_target<size, big_endian>* target =
377 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
378 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
379 if (!target->has_make_symbol())
380 ret = new Sized_symbol<size>();
381 else
383 ret = target->make_symbol();
384 if (ret == NULL)
386 // This means that we don't want a symbol table
387 // entry after all.
388 if (!def)
389 this->table_.erase(ins.first);
390 else
392 this->table_.erase(insdef.first);
393 // Inserting insdef invalidated ins.
394 this->table_.erase(std::make_pair(name_key,
395 version_key));
397 return NULL;
401 ret->init(name, version, object, sym);
403 ins.first->second = ret;
404 if (def)
406 // This is the first time we have seen NAME/NULL. Point
407 // it at the new entry for NAME/VERSION.
408 gold_assert(insdef.second);
409 insdef.first->second = ret;
414 // Record every time we see a new undefined symbol, to speed up
415 // archive groups.
416 if (!was_undefined && ret->is_undefined())
417 ++this->saw_undefined_;
419 // Keep track of common symbols, to speed up common symbol
420 // allocation.
421 if (!was_common && ret->is_common())
422 this->commons_.push_back(ret);
424 return ret;
427 // Add all the symbols in a relocatable object to the hash table.
429 template<int size, bool big_endian>
430 void
431 Symbol_table::add_from_relobj(
432 Sized_relobj<size, big_endian>* relobj,
433 const unsigned char* syms,
434 size_t count,
435 const char* sym_names,
436 size_t sym_name_size,
437 Symbol** sympointers)
439 // We take the size from the first object we see.
440 if (this->get_size() == 0)
441 this->set_size(size);
443 if (size != this->get_size() || size != relobj->target()->get_size())
445 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
446 program_name, relobj->name().c_str());
447 gold_exit(false);
450 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
452 const unsigned char* p = syms;
453 for (size_t i = 0; i < count; ++i, p += sym_size)
455 elfcpp::Sym<size, big_endian> sym(p);
456 elfcpp::Sym<size, big_endian>* psym = &sym;
458 unsigned int st_name = psym->get_st_name();
459 if (st_name >= sym_name_size)
461 fprintf(stderr,
462 _("%s: %s: bad global symbol name offset %u at %lu\n"),
463 program_name, relobj->name().c_str(), st_name,
464 static_cast<unsigned long>(i));
465 gold_exit(false);
468 const char* name = sym_names + st_name;
470 // A symbol defined in a section which we are not including must
471 // be treated as an undefined symbol.
472 unsigned char symbuf[sym_size];
473 elfcpp::Sym<size, big_endian> sym2(symbuf);
474 unsigned int st_shndx = psym->get_st_shndx();
475 if (st_shndx != elfcpp::SHN_UNDEF
476 && st_shndx < elfcpp::SHN_LORESERVE
477 && !relobj->is_section_included(st_shndx))
479 memcpy(symbuf, p, sym_size);
480 elfcpp::Sym_write<size, big_endian> sw(symbuf);
481 sw.put_st_shndx(elfcpp::SHN_UNDEF);
482 psym = &sym2;
485 // In an object file, an '@' in the name separates the symbol
486 // name from the version name. If there are two '@' characters,
487 // this is the default version.
488 const char* ver = strchr(name, '@');
490 Symbol* res;
491 if (ver == NULL)
493 Stringpool::Key name_key;
494 name = this->namepool_.add(name, &name_key);
495 res = this->add_from_object(relobj, name, name_key, NULL, 0,
496 false, *psym);
498 else
500 Stringpool::Key name_key;
501 name = this->namepool_.add(name, ver - name, &name_key);
503 bool def = false;
504 ++ver;
505 if (*ver == '@')
507 def = true;
508 ++ver;
511 Stringpool::Key ver_key;
512 ver = this->namepool_.add(ver, &ver_key);
514 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
515 def, *psym);
518 *sympointers++ = res;
522 // Add all the symbols in a dynamic object to the hash table.
524 template<int size, bool big_endian>
525 void
526 Symbol_table::add_from_dynobj(
527 Sized_dynobj<size, big_endian>* dynobj,
528 const unsigned char* syms,
529 size_t count,
530 const char* sym_names,
531 size_t sym_name_size,
532 const unsigned char* versym,
533 size_t versym_size,
534 const std::vector<const char*>* version_map)
536 // We take the size from the first object we see.
537 if (this->get_size() == 0)
538 this->set_size(size);
540 if (size != this->get_size() || size != dynobj->target()->get_size())
542 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
543 program_name, dynobj->name().c_str());
544 gold_exit(false);
547 if (versym != NULL && versym_size / 2 < count)
549 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
550 program_name, dynobj->name().c_str());
551 gold_exit(false);
554 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
556 const unsigned char* p = syms;
557 const unsigned char* vs = versym;
558 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
560 elfcpp::Sym<size, big_endian> sym(p);
562 // Ignore symbols with local binding.
563 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
564 continue;
566 unsigned int st_name = sym.get_st_name();
567 if (st_name >= sym_name_size)
569 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
570 program_name, dynobj->name().c_str(), st_name,
571 static_cast<unsigned long>(i));
572 gold_exit(false);
575 const char* name = sym_names + st_name;
577 if (versym == NULL)
579 Stringpool::Key name_key;
580 name = this->namepool_.add(name, &name_key);
581 this->add_from_object(dynobj, name, name_key, NULL, 0,
582 false, sym);
583 continue;
586 // Read the version information.
588 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
590 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
591 v &= elfcpp::VERSYM_VERSION;
593 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL))
595 // This symbol should not be visible outside the object.
596 continue;
599 // At this point we are definitely going to add this symbol.
600 Stringpool::Key name_key;
601 name = this->namepool_.add(name, &name_key);
603 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
605 // This symbol does not have a version.
606 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
607 continue;
610 if (v >= version_map->size())
612 fprintf(stderr,
613 _("%s: %s: versym for symbol %zu out of range: %u\n"),
614 program_name, dynobj->name().c_str(), i, v);
615 gold_exit(false);
618 const char* version = (*version_map)[v];
619 if (version == NULL)
621 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
622 program_name, dynobj->name().c_str(), i, v);
623 gold_exit(false);
626 Stringpool::Key version_key;
627 version = this->namepool_.add(version, &version_key);
629 // If this is an absolute symbol, and the version name and
630 // symbol name are the same, then this is the version definition
631 // symbol. These symbols exist to support using -u to pull in
632 // particular versions. We do not want to record a version for
633 // them.
634 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
636 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
637 continue;
640 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
642 this->add_from_object(dynobj, name, name_key, version, version_key,
643 def, sym);
647 // Create and return a specially defined symbol. If ONLY_IF_REF is
648 // true, then only create the symbol if there is a reference to it.
650 template<int size, bool big_endian>
651 Sized_symbol<size>*
652 Symbol_table::define_special_symbol(const Target* target, const char* name,
653 const char* version, bool only_if_ref
654 ACCEPT_SIZE_ENDIAN)
656 gold_assert(this->size_ == size);
658 Symbol* oldsym;
659 Sized_symbol<size>* sym;
661 if (only_if_ref)
663 oldsym = this->lookup(name, version);
664 if (oldsym == NULL || !oldsym->is_undefined())
665 return NULL;
666 sym = NULL;
668 // Canonicalize NAME and VERSION.
669 name = oldsym->name();
670 version = oldsym->version();
672 else
674 // Canonicalize NAME and VERSION.
675 Stringpool::Key name_key;
676 name = this->namepool_.add(name, &name_key);
678 Stringpool::Key version_key = 0;
679 if (version != NULL)
680 version = this->namepool_.add(version, &version_key);
682 Symbol* const snull = NULL;
683 std::pair<typename Symbol_table_type::iterator, bool> ins =
684 this->table_.insert(std::make_pair(std::make_pair(name_key,
685 version_key),
686 snull));
688 if (!ins.second)
690 // We already have a symbol table entry for NAME/VERSION.
691 oldsym = ins.first->second;
692 gold_assert(oldsym != NULL);
693 sym = NULL;
695 else
697 // We haven't seen this symbol before.
698 gold_assert(ins.first->second == NULL);
700 if (!target->has_make_symbol())
701 sym = new Sized_symbol<size>();
702 else
704 gold_assert(target->get_size() == size);
705 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
706 typedef Sized_target<size, big_endian> My_target;
707 const My_target* sized_target =
708 static_cast<const My_target*>(target);
709 sym = sized_target->make_symbol();
710 if (sym == NULL)
711 return NULL;
714 ins.first->second = sym;
715 oldsym = NULL;
719 if (oldsym != NULL)
721 gold_assert(sym == NULL);
723 sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
724 SELECT_SIZE(size));
725 gold_assert(sym->source() == Symbol::FROM_OBJECT);
726 const int old_shndx = sym->shndx();
727 if (old_shndx != elfcpp::SHN_UNDEF
728 && old_shndx != elfcpp::SHN_COMMON
729 && !sym->object()->is_dynamic())
731 fprintf(stderr, "%s: linker defined: multiple definition of %s\n",
732 program_name, name);
733 // FIXME: Report old location. Record that we have seen an
734 // error.
735 return NULL;
738 // Our new definition is going to override the old reference.
741 return sym;
744 // Define a symbol based on an Output_data.
746 Symbol*
747 Symbol_table::define_in_output_data(const Target* target, const char* name,
748 const char* version, Output_data* od,
749 uint64_t value, uint64_t symsize,
750 elfcpp::STT type, elfcpp::STB binding,
751 elfcpp::STV visibility,
752 unsigned char nonvis,
753 bool offset_is_from_end,
754 bool only_if_ref)
756 gold_assert(target->get_size() == this->size_);
757 if (this->size_ == 32)
758 return this->do_define_in_output_data<32>(target, name, version, od, value,
759 symsize, type, binding,
760 visibility, nonvis,
761 offset_is_from_end, only_if_ref);
762 else if (this->size_ == 64)
763 return this->do_define_in_output_data<64>(target, name, version, od, value,
764 symsize, type, binding,
765 visibility, nonvis,
766 offset_is_from_end, only_if_ref);
767 else
768 gold_unreachable();
771 // Define a symbol in an Output_data, sized version.
773 template<int size>
774 Sized_symbol<size>*
775 Symbol_table::do_define_in_output_data(
776 const Target* target,
777 const char* name,
778 const char* version,
779 Output_data* od,
780 typename elfcpp::Elf_types<size>::Elf_Addr value,
781 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
782 elfcpp::STT type,
783 elfcpp::STB binding,
784 elfcpp::STV visibility,
785 unsigned char nonvis,
786 bool offset_is_from_end,
787 bool only_if_ref)
789 Sized_symbol<size>* sym;
791 if (target->is_big_endian())
792 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
793 target, name, version, only_if_ref
794 SELECT_SIZE_ENDIAN(size, true));
795 else
796 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
797 target, name, version, only_if_ref
798 SELECT_SIZE_ENDIAN(size, false));
800 if (sym == NULL)
801 return NULL;
803 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
804 offset_is_from_end);
806 return sym;
809 // Define a symbol based on an Output_segment.
811 Symbol*
812 Symbol_table::define_in_output_segment(const Target* target, const char* name,
813 const char* version, Output_segment* os,
814 uint64_t value, uint64_t symsize,
815 elfcpp::STT type, elfcpp::STB binding,
816 elfcpp::STV visibility,
817 unsigned char nonvis,
818 Symbol::Segment_offset_base offset_base,
819 bool only_if_ref)
821 gold_assert(target->get_size() == this->size_);
822 if (this->size_ == 32)
823 return this->do_define_in_output_segment<32>(target, name, version, os,
824 value, symsize, type, binding,
825 visibility, nonvis,
826 offset_base, only_if_ref);
827 else if (this->size_ == 64)
828 return this->do_define_in_output_segment<64>(target, name, version, os,
829 value, symsize, type, binding,
830 visibility, nonvis,
831 offset_base, only_if_ref);
832 else
833 gold_unreachable();
836 // Define a symbol in an Output_segment, sized version.
838 template<int size>
839 Sized_symbol<size>*
840 Symbol_table::do_define_in_output_segment(
841 const Target* target,
842 const char* name,
843 const char* version,
844 Output_segment* os,
845 typename elfcpp::Elf_types<size>::Elf_Addr value,
846 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
847 elfcpp::STT type,
848 elfcpp::STB binding,
849 elfcpp::STV visibility,
850 unsigned char nonvis,
851 Symbol::Segment_offset_base offset_base,
852 bool only_if_ref)
854 Sized_symbol<size>* sym;
856 if (target->is_big_endian())
857 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
858 target, name, version, only_if_ref
859 SELECT_SIZE_ENDIAN(size, true));
860 else
861 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
862 target, name, version, only_if_ref
863 SELECT_SIZE_ENDIAN(size, false));
865 if (sym == NULL)
866 return NULL;
868 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
869 offset_base);
871 return sym;
874 // Define a special symbol with a constant value. It is a multiple
875 // definition error if this symbol is already defined.
877 Symbol*
878 Symbol_table::define_as_constant(const Target* target, const char* name,
879 const char* version, uint64_t value,
880 uint64_t symsize, elfcpp::STT type,
881 elfcpp::STB binding, elfcpp::STV visibility,
882 unsigned char nonvis, bool only_if_ref)
884 gold_assert(target->get_size() == this->size_);
885 if (this->size_ == 32)
886 return this->do_define_as_constant<32>(target, name, version, value,
887 symsize, type, binding, visibility,
888 nonvis, only_if_ref);
889 else if (this->size_ == 64)
890 return this->do_define_as_constant<64>(target, name, version, value,
891 symsize, type, binding, visibility,
892 nonvis, only_if_ref);
893 else
894 gold_unreachable();
897 // Define a symbol as a constant, sized version.
899 template<int size>
900 Sized_symbol<size>*
901 Symbol_table::do_define_as_constant(
902 const Target* target,
903 const char* name,
904 const char* version,
905 typename elfcpp::Elf_types<size>::Elf_Addr value,
906 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
907 elfcpp::STT type,
908 elfcpp::STB binding,
909 elfcpp::STV visibility,
910 unsigned char nonvis,
911 bool only_if_ref)
913 Sized_symbol<size>* sym;
915 if (target->is_big_endian())
916 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
917 target, name, version, only_if_ref
918 SELECT_SIZE_ENDIAN(size, true));
919 else
920 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
921 target, name, version, only_if_ref
922 SELECT_SIZE_ENDIAN(size, false));
924 if (sym == NULL)
925 return NULL;
927 sym->init(name, value, symsize, type, binding, visibility, nonvis);
929 return sym;
932 // Define a set of symbols in output sections.
934 void
935 Symbol_table::define_symbols(const Layout* layout, const Target* target,
936 int count, const Define_symbol_in_section* p)
938 for (int i = 0; i < count; ++i, ++p)
940 Output_section* os = layout->find_output_section(p->output_section);
941 if (os != NULL)
942 this->define_in_output_data(target, p->name, NULL, os, p->value,
943 p->size, p->type, p->binding,
944 p->visibility, p->nonvis,
945 p->offset_is_from_end, p->only_if_ref);
946 else
947 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
948 p->binding, p->visibility, p->nonvis,
949 p->only_if_ref);
953 // Define a set of symbols in output segments.
955 void
956 Symbol_table::define_symbols(const Layout* layout, const Target* target,
957 int count, const Define_symbol_in_segment* p)
959 for (int i = 0; i < count; ++i, ++p)
961 Output_segment* os = layout->find_output_segment(p->segment_type,
962 p->segment_flags_set,
963 p->segment_flags_clear);
964 if (os != NULL)
965 this->define_in_output_segment(target, p->name, NULL, os, p->value,
966 p->size, p->type, p->binding,
967 p->visibility, p->nonvis,
968 p->offset_base, p->only_if_ref);
969 else
970 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
971 p->binding, p->visibility, p->nonvis,
972 p->only_if_ref);
976 // Set the dynamic symbol indexes. INDEX is the index of the first
977 // global dynamic symbol. Pointers to the symbols are stored into the
978 // vector SYMS. The names are added to DYNPOOL. This returns an
979 // updated dynamic symbol index.
981 unsigned int
982 Symbol_table::set_dynsym_indexes(const General_options* options,
983 const Target* target,
984 unsigned int index,
985 std::vector<Symbol*>* syms,
986 Stringpool* dynpool,
987 Versions* versions)
989 for (Symbol_table_type::iterator p = this->table_.begin();
990 p != this->table_.end();
991 ++p)
993 Symbol* sym = p->second;
995 // Note that SYM may already have a dynamic symbol index, since
996 // some symbols appear more than once in the symbol table, with
997 // and without a version.
999 if (!sym->needs_dynsym_entry())
1000 sym->set_dynsym_index(-1U);
1001 else if (!sym->has_dynsym_index())
1003 sym->set_dynsym_index(index);
1004 ++index;
1005 syms->push_back(sym);
1006 dynpool->add(sym->name(), NULL);
1008 // Record any version information.
1009 if (sym->version() != NULL)
1010 versions->record_version(options, dynpool, sym);
1014 // Finish up the versions. In some cases this may add new dynamic
1015 // symbols.
1016 index = versions->finalize(target, this, index, syms);
1018 return index;
1021 // Set the final values for all the symbols. The index of the first
1022 // global symbol in the output file is INDEX. Record the file offset
1023 // OFF. Add their names to POOL. Return the new file offset.
1025 off_t
1026 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1027 size_t dyn_global_index, size_t dyncount,
1028 Stringpool* pool)
1030 off_t ret;
1032 gold_assert(index != 0);
1033 this->first_global_index_ = index;
1035 this->dynamic_offset_ = dynoff;
1036 this->first_dynamic_global_index_ = dyn_global_index;
1037 this->dynamic_count_ = dyncount;
1039 if (this->size_ == 32)
1040 ret = this->sized_finalize<32>(index, off, pool);
1041 else if (this->size_ == 64)
1042 ret = this->sized_finalize<64>(index, off, pool);
1043 else
1044 gold_unreachable();
1046 // Now that we have the final symbol table, we can reliably note
1047 // which symbols should get warnings.
1048 this->warnings_.note_warnings(this);
1050 return ret;
1053 // Set the final value for all the symbols. This is called after
1054 // Layout::finalize, so all the output sections have their final
1055 // address.
1057 template<int size>
1058 off_t
1059 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1061 off = align_address(off, size >> 3);
1062 this->offset_ = off;
1064 size_t orig_index = index;
1066 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1067 for (Symbol_table_type::iterator p = this->table_.begin();
1068 p != this->table_.end();
1069 ++p)
1071 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1073 // FIXME: Here we need to decide which symbols should go into
1074 // the output file, based on --strip.
1076 // The default version of a symbol may appear twice in the
1077 // symbol table. We only need to finalize it once.
1078 if (sym->has_symtab_index())
1079 continue;
1081 typename Sized_symbol<size>::Value_type value;
1083 switch (sym->source())
1085 case Symbol::FROM_OBJECT:
1087 unsigned int shndx = sym->shndx();
1089 // FIXME: We need some target specific support here.
1090 if (shndx >= elfcpp::SHN_LORESERVE
1091 && shndx != elfcpp::SHN_ABS)
1093 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1094 program_name, sym->name(), shndx);
1095 gold_exit(false);
1098 Object* symobj = sym->object();
1099 if (symobj->is_dynamic())
1101 value = 0;
1102 shndx = elfcpp::SHN_UNDEF;
1104 else if (shndx == elfcpp::SHN_UNDEF)
1105 value = 0;
1106 else if (shndx == elfcpp::SHN_ABS)
1107 value = sym->value();
1108 else
1110 Relobj* relobj = static_cast<Relobj*>(symobj);
1111 off_t secoff;
1112 Output_section* os = relobj->output_section(shndx, &secoff);
1114 if (os == NULL)
1116 sym->set_symtab_index(-1U);
1117 gold_assert(sym->dynsym_index() == -1U);
1118 continue;
1121 value = sym->value() + os->address() + secoff;
1124 break;
1126 case Symbol::IN_OUTPUT_DATA:
1128 Output_data* od = sym->output_data();
1129 value = sym->value() + od->address();
1130 if (sym->offset_is_from_end())
1131 value += od->data_size();
1133 break;
1135 case Symbol::IN_OUTPUT_SEGMENT:
1137 Output_segment* os = sym->output_segment();
1138 value = sym->value() + os->vaddr();
1139 switch (sym->offset_base())
1141 case Symbol::SEGMENT_START:
1142 break;
1143 case Symbol::SEGMENT_END:
1144 value += os->memsz();
1145 break;
1146 case Symbol::SEGMENT_BSS:
1147 value += os->filesz();
1148 break;
1149 default:
1150 gold_unreachable();
1153 break;
1155 case Symbol::CONSTANT:
1156 value = sym->value();
1157 break;
1159 default:
1160 gold_unreachable();
1163 sym->set_value(value);
1164 sym->set_symtab_index(index);
1165 pool->add(sym->name(), NULL);
1166 ++index;
1167 off += sym_size;
1170 this->output_count_ = index - orig_index;
1172 return off;
1175 // Write out the global symbols.
1177 void
1178 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1179 const Stringpool* dynpool, Output_file* of) const
1181 if (this->size_ == 32)
1183 if (target->is_big_endian())
1184 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1185 else
1186 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1188 else if (this->size_ == 64)
1190 if (target->is_big_endian())
1191 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1192 else
1193 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1195 else
1196 gold_unreachable();
1199 // Write out the global symbols.
1201 template<int size, bool big_endian>
1202 void
1203 Symbol_table::sized_write_globals(const Target*,
1204 const Stringpool* sympool,
1205 const Stringpool* dynpool,
1206 Output_file* of) const
1208 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1209 unsigned int index = this->first_global_index_;
1210 const off_t oview_size = this->output_count_ * sym_size;
1211 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1213 unsigned int dynamic_count = this->dynamic_count_;
1214 off_t dynamic_size = dynamic_count * sym_size;
1215 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1216 unsigned char* dynamic_view;
1217 if (this->dynamic_offset_ == 0)
1218 dynamic_view = NULL;
1219 else
1220 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1222 unsigned char* ps = psyms;
1223 for (Symbol_table_type::const_iterator p = this->table_.begin();
1224 p != this->table_.end();
1225 ++p)
1227 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1229 unsigned int sym_index = sym->symtab_index();
1230 unsigned int dynsym_index;
1231 if (dynamic_view == NULL)
1232 dynsym_index = -1U;
1233 else
1234 dynsym_index = sym->dynsym_index();
1236 if (sym_index == -1U && dynsym_index == -1U)
1238 // This symbol is not included in the output file.
1239 continue;
1242 if (sym_index == index)
1243 ++index;
1244 else if (sym_index != -1U)
1246 // We have already seen this symbol, because it has a
1247 // default version.
1248 gold_assert(sym_index < index);
1249 if (dynsym_index == -1U)
1250 continue;
1251 sym_index = -1U;
1254 unsigned int shndx;
1255 switch (sym->source())
1257 case Symbol::FROM_OBJECT:
1259 unsigned int in_shndx = sym->shndx();
1261 // FIXME: We need some target specific support here.
1262 if (in_shndx >= elfcpp::SHN_LORESERVE
1263 && in_shndx != elfcpp::SHN_ABS)
1265 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1266 program_name, sym->name(), in_shndx);
1267 gold_exit(false);
1270 Object* symobj = sym->object();
1271 if (symobj->is_dynamic())
1273 // FIXME.
1274 shndx = elfcpp::SHN_UNDEF;
1276 else if (in_shndx == elfcpp::SHN_UNDEF
1277 || in_shndx == elfcpp::SHN_ABS)
1278 shndx = in_shndx;
1279 else
1281 Relobj* relobj = static_cast<Relobj*>(symobj);
1282 off_t secoff;
1283 Output_section* os = relobj->output_section(in_shndx, &secoff);
1284 gold_assert(os != NULL);
1285 shndx = os->out_shndx();
1288 break;
1290 case Symbol::IN_OUTPUT_DATA:
1291 shndx = sym->output_data()->out_shndx();
1292 break;
1294 case Symbol::IN_OUTPUT_SEGMENT:
1295 shndx = elfcpp::SHN_ABS;
1296 break;
1298 case Symbol::CONSTANT:
1299 shndx = elfcpp::SHN_ABS;
1300 break;
1302 default:
1303 gold_unreachable();
1306 if (sym_index != -1U)
1308 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1309 sym, shndx, sympool, ps
1310 SELECT_SIZE_ENDIAN(size, big_endian));
1311 ps += sym_size;
1314 if (dynsym_index != -1U)
1316 dynsym_index -= first_dynamic_global_index;
1317 gold_assert(dynsym_index < dynamic_count);
1318 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1319 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1320 sym, shndx, dynpool, pd
1321 SELECT_SIZE_ENDIAN(size, big_endian));
1325 gold_assert(ps - psyms == oview_size);
1327 of->write_output_view(this->offset_, oview_size, psyms);
1328 if (dynamic_view != NULL)
1329 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1332 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1333 // strtab holding the name.
1335 template<int size, bool big_endian>
1336 void
1337 Symbol_table::sized_write_symbol(Sized_symbol<size>* sym,
1338 unsigned int shndx,
1339 const Stringpool* pool,
1340 unsigned char* p
1341 ACCEPT_SIZE_ENDIAN) const
1343 elfcpp::Sym_write<size, big_endian> osym(p);
1344 osym.put_st_name(pool->get_offset(sym->name()));
1345 osym.put_st_value(sym->value());
1346 osym.put_st_size(sym->symsize());
1347 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1348 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1349 osym.put_st_shndx(shndx);
1352 // Write out a section symbol. Return the update offset.
1354 void
1355 Symbol_table::write_section_symbol(const Target* target,
1356 const Output_section *os,
1357 Output_file* of,
1358 off_t offset) const
1360 if (this->size_ == 32)
1362 if (target->is_big_endian())
1363 this->sized_write_section_symbol<32, true>(os, of, offset);
1364 else
1365 this->sized_write_section_symbol<32, false>(os, of, offset);
1367 else if (this->size_ == 64)
1369 if (target->is_big_endian())
1370 this->sized_write_section_symbol<64, true>(os, of, offset);
1371 else
1372 this->sized_write_section_symbol<64, false>(os, of, offset);
1374 else
1375 gold_unreachable();
1378 // Write out a section symbol, specialized for size and endianness.
1380 template<int size, bool big_endian>
1381 void
1382 Symbol_table::sized_write_section_symbol(const Output_section* os,
1383 Output_file* of,
1384 off_t offset) const
1386 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1388 unsigned char* pov = of->get_output_view(offset, sym_size);
1390 elfcpp::Sym_write<size, big_endian> osym(pov);
1391 osym.put_st_name(0);
1392 osym.put_st_value(os->address());
1393 osym.put_st_size(0);
1394 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1395 elfcpp::STT_SECTION));
1396 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1397 osym.put_st_shndx(os->out_shndx());
1399 of->write_output_view(offset, sym_size, pov);
1402 // Warnings functions.
1404 // Add a new warning.
1406 void
1407 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1408 unsigned int shndx)
1410 name = symtab->canonicalize_name(name);
1411 this->warnings_[name].set(obj, shndx);
1414 // Look through the warnings and mark the symbols for which we should
1415 // warn. This is called during Layout::finalize when we know the
1416 // sources for all the symbols.
1418 void
1419 Warnings::note_warnings(Symbol_table* symtab)
1421 for (Warning_table::iterator p = this->warnings_.begin();
1422 p != this->warnings_.end();
1423 ++p)
1425 Symbol* sym = symtab->lookup(p->first, NULL);
1426 if (sym != NULL
1427 && sym->source() == Symbol::FROM_OBJECT
1428 && sym->object() == p->second.object)
1430 sym->set_has_warning();
1432 // Read the section contents to get the warning text. It
1433 // would be nicer if we only did this if we have to actually
1434 // issue a warning. Unfortunately, warnings are issued as
1435 // we relocate sections. That means that we can not lock
1436 // the object then, as we might try to issue the same
1437 // warning multiple times simultaneously.
1439 Task_locker_obj<Object> tl(*p->second.object);
1440 const unsigned char* c;
1441 off_t len;
1442 c = p->second.object->section_contents(p->second.shndx, &len);
1443 p->second.set_text(reinterpret_cast<const char*>(c), len);
1449 // Issue a warning. This is called when we see a relocation against a
1450 // symbol for which has a warning.
1452 void
1453 Warnings::issue_warning(const Symbol* sym, const std::string& location) const
1455 gold_assert(sym->has_warning());
1456 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1457 gold_assert(p != this->warnings_.end());
1458 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1459 p->second.text.c_str());
1462 // Instantiate the templates we need. We could use the configure
1463 // script to restrict this to only the ones needed for implemented
1464 // targets.
1466 template
1467 void
1468 Symbol_table::add_from_relobj<32, true>(
1469 Sized_relobj<32, true>* relobj,
1470 const unsigned char* syms,
1471 size_t count,
1472 const char* sym_names,
1473 size_t sym_name_size,
1474 Symbol** sympointers);
1476 template
1477 void
1478 Symbol_table::add_from_relobj<32, false>(
1479 Sized_relobj<32, false>* relobj,
1480 const unsigned char* syms,
1481 size_t count,
1482 const char* sym_names,
1483 size_t sym_name_size,
1484 Symbol** sympointers);
1486 template
1487 void
1488 Symbol_table::add_from_relobj<64, true>(
1489 Sized_relobj<64, true>* relobj,
1490 const unsigned char* syms,
1491 size_t count,
1492 const char* sym_names,
1493 size_t sym_name_size,
1494 Symbol** sympointers);
1496 template
1497 void
1498 Symbol_table::add_from_relobj<64, false>(
1499 Sized_relobj<64, false>* relobj,
1500 const unsigned char* syms,
1501 size_t count,
1502 const char* sym_names,
1503 size_t sym_name_size,
1504 Symbol** sympointers);
1506 template
1507 void
1508 Symbol_table::add_from_dynobj<32, true>(
1509 Sized_dynobj<32, true>* dynobj,
1510 const unsigned char* syms,
1511 size_t count,
1512 const char* sym_names,
1513 size_t sym_name_size,
1514 const unsigned char* versym,
1515 size_t versym_size,
1516 const std::vector<const char*>* version_map);
1518 template
1519 void
1520 Symbol_table::add_from_dynobj<32, false>(
1521 Sized_dynobj<32, false>* dynobj,
1522 const unsigned char* syms,
1523 size_t count,
1524 const char* sym_names,
1525 size_t sym_name_size,
1526 const unsigned char* versym,
1527 size_t versym_size,
1528 const std::vector<const char*>* version_map);
1530 template
1531 void
1532 Symbol_table::add_from_dynobj<64, true>(
1533 Sized_dynobj<64, true>* dynobj,
1534 const unsigned char* syms,
1535 size_t count,
1536 const char* sym_names,
1537 size_t sym_name_size,
1538 const unsigned char* versym,
1539 size_t versym_size,
1540 const std::vector<const char*>* version_map);
1542 template
1543 void
1544 Symbol_table::add_from_dynobj<64, false>(
1545 Sized_dynobj<64, false>* dynobj,
1546 const unsigned char* syms,
1547 size_t count,
1548 const char* sym_names,
1549 size_t sym_name_size,
1550 const unsigned char* versym,
1551 size_t versym_size,
1552 const std::vector<const char*>* version_map);
1554 } // End namespace gold.