1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
33 #include "workqueue.h"
41 // Initialize fields in Symbol. This initializes everything except u_
45 Symbol::init_fields(const char* name, const char* version,
46 elfcpp::STT type, elfcpp::STB binding,
47 elfcpp::STV visibility, unsigned char nonvis)
50 this->version_ = version;
51 this->symtab_index_ = 0;
52 this->dynsym_index_ = 0;
53 this->got_offset_ = 0;
54 this->plt_offset_ = 0;
56 this->binding_ = binding;
57 this->visibility_ = visibility;
58 this->nonvis_ = nonvis;
59 this->is_target_special_ = false;
60 this->is_def_ = false;
61 this->is_forwarder_ = false;
62 this->needs_dynsym_entry_ = false;
63 this->in_reg_ = false;
64 this->in_dyn_ = false;
65 this->has_got_offset_ = false;
66 this->has_plt_offset_ = false;
67 this->has_warning_ = false;
70 // Initialize the fields in the base class Symbol for SYM in OBJECT.
72 template<int size, bool big_endian>
74 Symbol::init_base(const char* name, const char* version, Object* object,
75 const elfcpp::Sym<size, big_endian>& sym)
77 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
78 sym.get_st_visibility(), sym.get_st_nonvis());
79 this->u_.from_object.object = object;
80 // FIXME: Handle SHN_XINDEX.
81 this->u_.from_object.shndx = sym.get_st_shndx();
82 this->source_ = FROM_OBJECT;
83 this->in_reg_ = !object->is_dynamic();
84 this->in_dyn_ = object->is_dynamic();
87 // Initialize the fields in the base class Symbol for a symbol defined
91 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
92 elfcpp::STB binding, elfcpp::STV visibility,
93 unsigned char nonvis, bool offset_is_from_end)
95 this->init_fields(name, NULL, type, binding, visibility, nonvis);
96 this->u_.in_output_data.output_data = od;
97 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
98 this->source_ = IN_OUTPUT_DATA;
102 // Initialize the fields in the base class Symbol for a symbol defined
103 // in an Output_segment.
106 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
107 elfcpp::STB binding, elfcpp::STV visibility,
108 unsigned char nonvis, Segment_offset_base offset_base)
110 this->init_fields(name, NULL, type, binding, visibility, nonvis);
111 this->u_.in_output_segment.output_segment = os;
112 this->u_.in_output_segment.offset_base = offset_base;
113 this->source_ = IN_OUTPUT_SEGMENT;
114 this->in_reg_ = true;
117 // Initialize the fields in the base class Symbol for a symbol defined
121 Symbol::init_base(const char* name, elfcpp::STT type,
122 elfcpp::STB binding, elfcpp::STV visibility,
123 unsigned char nonvis)
125 this->init_fields(name, NULL, type, binding, visibility, nonvis);
126 this->source_ = CONSTANT;
127 this->in_reg_ = true;
130 // Initialize the fields in Sized_symbol for SYM in OBJECT.
133 template<bool big_endian>
135 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
136 const elfcpp::Sym<size, big_endian>& sym)
138 this->init_base(name, version, object, sym);
139 this->value_ = sym.get_st_value();
140 this->symsize_ = sym.get_st_size();
143 // Initialize the fields in Sized_symbol for a symbol defined in an
148 Sized_symbol<size>::init(const char* name, Output_data* od,
149 Value_type value, Size_type symsize,
150 elfcpp::STT type, elfcpp::STB binding,
151 elfcpp::STV visibility, unsigned char nonvis,
152 bool offset_is_from_end)
154 this->init_base(name, od, type, binding, visibility, nonvis,
156 this->value_ = value;
157 this->symsize_ = symsize;
160 // Initialize the fields in Sized_symbol for a symbol defined in an
165 Sized_symbol<size>::init(const char* name, Output_segment* os,
166 Value_type value, Size_type symsize,
167 elfcpp::STT type, elfcpp::STB binding,
168 elfcpp::STV visibility, unsigned char nonvis,
169 Segment_offset_base offset_base)
171 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
172 this->value_ = value;
173 this->symsize_ = symsize;
176 // Initialize the fields in Sized_symbol for a symbol defined as a
181 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
182 elfcpp::STT type, elfcpp::STB binding,
183 elfcpp::STV visibility, unsigned char nonvis)
185 this->init_base(name, type, binding, visibility, nonvis);
186 this->value_ = value;
187 this->symsize_ = symsize;
190 // Return true if the final value of this symbol is known at link
194 Symbol::final_value_is_known() const
196 // If we are not generating an executable, then no final values are
197 // known, since they will change at runtime.
198 if (!parameters->output_is_executable())
201 // If the symbol is not from an object file, then it is defined, and
203 if (this->source_ != FROM_OBJECT)
206 // If the symbol is from a dynamic object, then the final value is
208 if (this->object()->is_dynamic())
211 // If the symbol is not undefined (it is defined or common), then
212 // the final value is known.
213 if (!this->is_undefined())
216 // If the symbol is undefined, then whether the final value is known
217 // depends on whether we are doing a static link. If we are doing a
218 // dynamic link, then the final value could be filled in at runtime.
219 // This could reasonably be the case for a weak undefined symbol.
220 return parameters->doing_static_link();
223 // Class Symbol_table.
225 Symbol_table::Symbol_table()
226 : saw_undefined_(0), offset_(0), table_(), namepool_(),
227 forwarders_(), commons_(), warnings_()
231 Symbol_table::~Symbol_table()
235 // The hash function. The key is always canonicalized, so we use a
236 // simple combination of the pointers.
239 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
241 return key.first ^ key.second;
244 // The symbol table key equality function. This is only called with
245 // canonicalized name and version strings, so we can use pointer
249 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
250 const Symbol_table_key& k2) const
252 return k1.first == k2.first && k1.second == k2.second;
255 // Make TO a symbol which forwards to FROM.
258 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
260 gold_assert(from != to);
261 gold_assert(!from->is_forwarder() && !to->is_forwarder());
262 this->forwarders_[from] = to;
263 from->set_forwarder();
266 // Resolve the forwards from FROM, returning the real symbol.
269 Symbol_table::resolve_forwards(const Symbol* from) const
271 gold_assert(from->is_forwarder());
272 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
273 this->forwarders_.find(from);
274 gold_assert(p != this->forwarders_.end());
278 // Look up a symbol by name.
281 Symbol_table::lookup(const char* name, const char* version) const
283 Stringpool::Key name_key;
284 name = this->namepool_.find(name, &name_key);
288 Stringpool::Key version_key = 0;
291 version = this->namepool_.find(version, &version_key);
296 Symbol_table_key key(name_key, version_key);
297 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
298 if (p == this->table_.end())
303 // Resolve a Symbol with another Symbol. This is only used in the
304 // unusual case where there are references to both an unversioned
305 // symbol and a symbol with a version, and we then discover that that
306 // version is the default version. Because this is unusual, we do
307 // this the slow way, by converting back to an ELF symbol.
309 template<int size, bool big_endian>
311 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
312 const char* version ACCEPT_SIZE_ENDIAN)
314 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
315 elfcpp::Sym_write<size, big_endian> esym(buf);
316 // We don't bother to set the st_name field.
317 esym.put_st_value(from->value());
318 esym.put_st_size(from->symsize());
319 esym.put_st_info(from->binding(), from->type());
320 esym.put_st_other(from->visibility(), from->nonvis());
321 esym.put_st_shndx(from->shndx());
322 Symbol_table::resolve(to, esym.sym(), from->object(), version);
329 // Add one symbol from OBJECT to the symbol table. NAME is symbol
330 // name and VERSION is the version; both are canonicalized. DEF is
331 // whether this is the default version.
333 // If DEF is true, then this is the definition of a default version of
334 // a symbol. That means that any lookup of NAME/NULL and any lookup
335 // of NAME/VERSION should always return the same symbol. This is
336 // obvious for references, but in particular we want to do this for
337 // definitions: overriding NAME/NULL should also override
338 // NAME/VERSION. If we don't do that, it would be very hard to
339 // override functions in a shared library which uses versioning.
341 // We implement this by simply making both entries in the hash table
342 // point to the same Symbol structure. That is easy enough if this is
343 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
344 // that we have seen both already, in which case they will both have
345 // independent entries in the symbol table. We can't simply change
346 // the symbol table entry, because we have pointers to the entries
347 // attached to the object files. So we mark the entry attached to the
348 // object file as a forwarder, and record it in the forwarders_ map.
349 // Note that entries in the hash table will never be marked as
352 template<int size, bool big_endian>
354 Symbol_table::add_from_object(Object* object,
356 Stringpool::Key name_key,
358 Stringpool::Key version_key,
360 const elfcpp::Sym<size, big_endian>& sym)
362 Symbol* const snull = NULL;
363 std::pair<typename Symbol_table_type::iterator, bool> ins =
364 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
367 std::pair<typename Symbol_table_type::iterator, bool> insdef =
368 std::make_pair(this->table_.end(), false);
371 const Stringpool::Key vnull_key = 0;
372 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
377 // ins.first: an iterator, which is a pointer to a pair.
378 // ins.first->first: the key (a pair of name and version).
379 // ins.first->second: the value (Symbol*).
380 // ins.second: true if new entry was inserted, false if not.
382 Sized_symbol<size>* ret;
387 // We already have an entry for NAME/VERSION.
388 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
390 gold_assert(ret != NULL);
392 was_undefined = ret->is_undefined();
393 was_common = ret->is_common();
395 Symbol_table::resolve(ret, sym, object, version);
401 // This is the first time we have seen NAME/NULL. Make
402 // NAME/NULL point to NAME/VERSION.
403 insdef.first->second = ret;
405 else if (insdef.first->second != ret)
407 // This is the unfortunate case where we already have
408 // entries for both NAME/VERSION and NAME/NULL.
409 const Sized_symbol<size>* sym2;
410 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
413 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
414 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
415 this->make_forwarder(insdef.first->second, ret);
416 insdef.first->second = ret;
422 // This is the first time we have seen NAME/VERSION.
423 gold_assert(ins.first->second == NULL);
425 was_undefined = false;
428 if (def && !insdef.second)
430 // We already have an entry for NAME/NULL. If we override
431 // it, then change it to NAME/VERSION.
432 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
435 Symbol_table::resolve(ret, sym, object, version);
436 ins.first->second = ret;
440 Sized_target<size, big_endian>* target =
441 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
442 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
443 if (!target->has_make_symbol())
444 ret = new Sized_symbol<size>();
447 ret = target->make_symbol();
450 // This means that we don't want a symbol table
453 this->table_.erase(ins.first);
456 this->table_.erase(insdef.first);
457 // Inserting insdef invalidated ins.
458 this->table_.erase(std::make_pair(name_key,
465 ret->init(name, version, object, sym);
467 ins.first->second = ret;
470 // This is the first time we have seen NAME/NULL. Point
471 // it at the new entry for NAME/VERSION.
472 gold_assert(insdef.second);
473 insdef.first->second = ret;
478 // Record every time we see a new undefined symbol, to speed up
480 if (!was_undefined && ret->is_undefined())
481 ++this->saw_undefined_;
483 // Keep track of common symbols, to speed up common symbol
485 if (!was_common && ret->is_common())
486 this->commons_.push_back(ret);
491 // Add all the symbols in a relocatable object to the hash table.
493 template<int size, bool big_endian>
495 Symbol_table::add_from_relobj(
496 Sized_relobj<size, big_endian>* relobj,
497 const unsigned char* syms,
499 const char* sym_names,
500 size_t sym_name_size,
501 Symbol** sympointers)
503 gold_assert(size == relobj->target()->get_size());
504 gold_assert(size == parameters->get_size());
506 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
508 const unsigned char* p = syms;
509 for (size_t i = 0; i < count; ++i, p += sym_size)
511 elfcpp::Sym<size, big_endian> sym(p);
512 elfcpp::Sym<size, big_endian>* psym = &sym;
514 unsigned int st_name = psym->get_st_name();
515 if (st_name >= sym_name_size)
518 _("%s: %s: bad global symbol name offset %u at %lu\n"),
519 program_name, relobj->name().c_str(), st_name,
520 static_cast<unsigned long>(i));
524 const char* name = sym_names + st_name;
526 // A symbol defined in a section which we are not including must
527 // be treated as an undefined symbol.
528 unsigned char symbuf[sym_size];
529 elfcpp::Sym<size, big_endian> sym2(symbuf);
530 unsigned int st_shndx = psym->get_st_shndx();
531 if (st_shndx != elfcpp::SHN_UNDEF
532 && st_shndx < elfcpp::SHN_LORESERVE
533 && !relobj->is_section_included(st_shndx))
535 memcpy(symbuf, p, sym_size);
536 elfcpp::Sym_write<size, big_endian> sw(symbuf);
537 sw.put_st_shndx(elfcpp::SHN_UNDEF);
541 // In an object file, an '@' in the name separates the symbol
542 // name from the version name. If there are two '@' characters,
543 // this is the default version.
544 const char* ver = strchr(name, '@');
549 Stringpool::Key name_key;
550 name = this->namepool_.add(name, &name_key);
551 res = this->add_from_object(relobj, name, name_key, NULL, 0,
556 Stringpool::Key name_key;
557 name = this->namepool_.add(name, ver - name, &name_key);
567 Stringpool::Key ver_key;
568 ver = this->namepool_.add(ver, &ver_key);
570 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
574 *sympointers++ = res;
578 // Add all the symbols in a dynamic object to the hash table.
580 template<int size, bool big_endian>
582 Symbol_table::add_from_dynobj(
583 Sized_dynobj<size, big_endian>* dynobj,
584 const unsigned char* syms,
586 const char* sym_names,
587 size_t sym_name_size,
588 const unsigned char* versym,
590 const std::vector<const char*>* version_map)
592 gold_assert(size == dynobj->target()->get_size());
593 gold_assert(size == parameters->get_size());
595 if (versym != NULL && versym_size / 2 < count)
597 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
598 program_name, dynobj->name().c_str());
602 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
604 const unsigned char* p = syms;
605 const unsigned char* vs = versym;
606 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
608 elfcpp::Sym<size, big_endian> sym(p);
610 // Ignore symbols with local binding.
611 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
614 unsigned int st_name = sym.get_st_name();
615 if (st_name >= sym_name_size)
617 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
618 program_name, dynobj->name().c_str(), st_name,
619 static_cast<unsigned long>(i));
623 const char* name = sym_names + st_name;
627 Stringpool::Key name_key;
628 name = this->namepool_.add(name, &name_key);
629 this->add_from_object(dynobj, name, name_key, NULL, 0,
634 // Read the version information.
636 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
638 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
639 v &= elfcpp::VERSYM_VERSION;
641 // The Sun documentation says that V can be VER_NDX_LOCAL, or
642 // VER_NDX_GLOBAL, or a version index. The meaning of
643 // VER_NDX_LOCAL is defined as "Symbol has local scope." The
644 // old GNU linker will happily generate VER_NDX_LOCAL for an
645 // undefined symbol. I don't know what the Sun linker will
648 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
649 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
651 // This symbol should not be visible outside the object.
655 // At this point we are definitely going to add this symbol.
656 Stringpool::Key name_key;
657 name = this->namepool_.add(name, &name_key);
659 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
660 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
662 // This symbol does not have a version.
663 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
667 if (v >= version_map->size())
670 _("%s: %s: versym for symbol %zu out of range: %u\n"),
671 program_name, dynobj->name().c_str(), i, v);
675 const char* version = (*version_map)[v];
678 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
679 program_name, dynobj->name().c_str(), i, v);
683 Stringpool::Key version_key;
684 version = this->namepool_.add(version, &version_key);
686 // If this is an absolute symbol, and the version name and
687 // symbol name are the same, then this is the version definition
688 // symbol. These symbols exist to support using -u to pull in
689 // particular versions. We do not want to record a version for
691 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
693 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
697 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
699 this->add_from_object(dynobj, name, name_key, version, version_key,
704 // Create and return a specially defined symbol. If ONLY_IF_REF is
705 // true, then only create the symbol if there is a reference to it.
706 // If this does not return NULL, it sets *POLDSYM to the existing
707 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
709 template<int size, bool big_endian>
711 Symbol_table::define_special_symbol(const Target* target, const char** pname,
712 const char** pversion, bool only_if_ref,
713 Sized_symbol<size>** poldsym
717 Sized_symbol<size>* sym;
718 bool add_to_table = false;
719 typename Symbol_table_type::iterator add_loc = this->table_.end();
723 oldsym = this->lookup(*pname, *pversion);
724 if (oldsym == NULL || !oldsym->is_undefined())
727 *pname = oldsym->name();
728 *pversion = oldsym->version();
732 // Canonicalize NAME and VERSION.
733 Stringpool::Key name_key;
734 *pname = this->namepool_.add(*pname, &name_key);
736 Stringpool::Key version_key = 0;
737 if (*pversion != NULL)
738 *pversion = this->namepool_.add(*pversion, &version_key);
740 Symbol* const snull = NULL;
741 std::pair<typename Symbol_table_type::iterator, bool> ins =
742 this->table_.insert(std::make_pair(std::make_pair(name_key,
748 // We already have a symbol table entry for NAME/VERSION.
749 oldsym = ins.first->second;
750 gold_assert(oldsym != NULL);
754 // We haven't seen this symbol before.
755 gold_assert(ins.first->second == NULL);
762 if (!target->has_make_symbol())
763 sym = new Sized_symbol<size>();
766 gold_assert(target->get_size() == size);
767 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
768 typedef Sized_target<size, big_endian> My_target;
769 const My_target* sized_target =
770 static_cast<const My_target*>(target);
771 sym = sized_target->make_symbol();
777 add_loc->second = sym;
779 gold_assert(oldsym != NULL);
781 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
787 // Define a symbol based on an Output_data.
790 Symbol_table::define_in_output_data(const Target* target, const char* name,
791 const char* version, Output_data* od,
792 uint64_t value, uint64_t symsize,
793 elfcpp::STT type, elfcpp::STB binding,
794 elfcpp::STV visibility,
795 unsigned char nonvis,
796 bool offset_is_from_end,
799 if (parameters->get_size() == 32)
801 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
802 return this->do_define_in_output_data<32>(target, name, version, od,
803 value, symsize, type, binding,
811 else if (parameters->get_size() == 64)
813 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
814 return this->do_define_in_output_data<64>(target, name, version, od,
815 value, symsize, type, binding,
827 // Define a symbol in an Output_data, sized version.
831 Symbol_table::do_define_in_output_data(
832 const Target* target,
836 typename elfcpp::Elf_types<size>::Elf_Addr value,
837 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
840 elfcpp::STV visibility,
841 unsigned char nonvis,
842 bool offset_is_from_end,
845 Sized_symbol<size>* sym;
846 Sized_symbol<size>* oldsym;
848 if (parameters->is_big_endian())
850 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
851 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
852 target, &name, &version, only_if_ref, &oldsym
853 SELECT_SIZE_ENDIAN(size, true));
860 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
861 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
862 target, &name, &version, only_if_ref, &oldsym
863 SELECT_SIZE_ENDIAN(size, false));
872 gold_assert(version == NULL || oldsym != NULL);
873 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
877 && Symbol_table::should_override_with_special(oldsym))
878 oldsym->override_with_special(sym);
883 // Define a symbol based on an Output_segment.
886 Symbol_table::define_in_output_segment(const Target* target, const char* name,
887 const char* version, Output_segment* os,
888 uint64_t value, uint64_t symsize,
889 elfcpp::STT type, elfcpp::STB binding,
890 elfcpp::STV visibility,
891 unsigned char nonvis,
892 Symbol::Segment_offset_base offset_base,
895 if (parameters->get_size() == 32)
897 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
898 return this->do_define_in_output_segment<32>(target, name, version, os,
899 value, symsize, type,
900 binding, visibility, nonvis,
901 offset_base, only_if_ref);
906 else if (parameters->get_size() == 64)
908 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
909 return this->do_define_in_output_segment<64>(target, name, version, os,
910 value, symsize, type,
911 binding, visibility, nonvis,
912 offset_base, only_if_ref);
921 // Define a symbol in an Output_segment, sized version.
925 Symbol_table::do_define_in_output_segment(
926 const Target* target,
930 typename elfcpp::Elf_types<size>::Elf_Addr value,
931 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
934 elfcpp::STV visibility,
935 unsigned char nonvis,
936 Symbol::Segment_offset_base offset_base,
939 Sized_symbol<size>* sym;
940 Sized_symbol<size>* oldsym;
942 if (parameters->is_big_endian())
944 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
945 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
946 target, &name, &version, only_if_ref, &oldsym
947 SELECT_SIZE_ENDIAN(size, true));
954 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
955 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
956 target, &name, &version, only_if_ref, &oldsym
957 SELECT_SIZE_ENDIAN(size, false));
966 gold_assert(version == NULL || oldsym != NULL);
967 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
971 && Symbol_table::should_override_with_special(oldsym))
972 oldsym->override_with_special(sym);
977 // Define a special symbol with a constant value. It is a multiple
978 // definition error if this symbol is already defined.
981 Symbol_table::define_as_constant(const Target* target, const char* name,
982 const char* version, uint64_t value,
983 uint64_t symsize, elfcpp::STT type,
984 elfcpp::STB binding, elfcpp::STV visibility,
985 unsigned char nonvis, bool only_if_ref)
987 if (parameters->get_size() == 32)
989 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
990 return this->do_define_as_constant<32>(target, name, version, value,
991 symsize, type, binding,
992 visibility, nonvis, only_if_ref);
997 else if (parameters->get_size() == 64)
999 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1000 return this->do_define_as_constant<64>(target, name, version, value,
1001 symsize, type, binding,
1002 visibility, nonvis, only_if_ref);
1011 // Define a symbol as a constant, sized version.
1015 Symbol_table::do_define_as_constant(
1016 const Target* target,
1018 const char* version,
1019 typename elfcpp::Elf_types<size>::Elf_Addr value,
1020 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1022 elfcpp::STB binding,
1023 elfcpp::STV visibility,
1024 unsigned char nonvis,
1027 Sized_symbol<size>* sym;
1028 Sized_symbol<size>* oldsym;
1030 if (parameters->is_big_endian())
1032 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1033 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1034 target, &name, &version, only_if_ref, &oldsym
1035 SELECT_SIZE_ENDIAN(size, true));
1042 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1043 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1044 target, &name, &version, only_if_ref, &oldsym
1045 SELECT_SIZE_ENDIAN(size, false));
1054 gold_assert(version == NULL || oldsym != NULL);
1055 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1058 && Symbol_table::should_override_with_special(oldsym))
1059 oldsym->override_with_special(sym);
1064 // Define a set of symbols in output sections.
1067 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1068 int count, const Define_symbol_in_section* p)
1070 for (int i = 0; i < count; ++i, ++p)
1072 Output_section* os = layout->find_output_section(p->output_section);
1074 this->define_in_output_data(target, p->name, NULL, os, p->value,
1075 p->size, p->type, p->binding,
1076 p->visibility, p->nonvis,
1077 p->offset_is_from_end, p->only_if_ref);
1079 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1080 p->binding, p->visibility, p->nonvis,
1085 // Define a set of symbols in output segments.
1088 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1089 int count, const Define_symbol_in_segment* p)
1091 for (int i = 0; i < count; ++i, ++p)
1093 Output_segment* os = layout->find_output_segment(p->segment_type,
1094 p->segment_flags_set,
1095 p->segment_flags_clear);
1097 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1098 p->size, p->type, p->binding,
1099 p->visibility, p->nonvis,
1100 p->offset_base, p->only_if_ref);
1102 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1103 p->binding, p->visibility, p->nonvis,
1108 // Set the dynamic symbol indexes. INDEX is the index of the first
1109 // global dynamic symbol. Pointers to the symbols are stored into the
1110 // vector SYMS. The names are added to DYNPOOL. This returns an
1111 // updated dynamic symbol index.
1114 Symbol_table::set_dynsym_indexes(const General_options* options,
1115 const Target* target,
1117 std::vector<Symbol*>* syms,
1118 Stringpool* dynpool,
1121 for (Symbol_table_type::iterator p = this->table_.begin();
1122 p != this->table_.end();
1125 Symbol* sym = p->second;
1127 // Note that SYM may already have a dynamic symbol index, since
1128 // some symbols appear more than once in the symbol table, with
1129 // and without a version.
1131 if (!sym->needs_dynsym_entry()
1132 && (!options->export_dynamic()
1134 || !sym->is_externally_visible()))
1135 sym->set_dynsym_index(-1U);
1136 else if (!sym->has_dynsym_index())
1138 sym->set_dynsym_index(index);
1140 syms->push_back(sym);
1141 dynpool->add(sym->name(), NULL);
1143 // Record any version information.
1144 if (sym->version() != NULL)
1145 versions->record_version(options, dynpool, sym);
1149 // Finish up the versions. In some cases this may add new dynamic
1151 index = versions->finalize(target, this, index, syms);
1156 // Set the final values for all the symbols. The index of the first
1157 // global symbol in the output file is INDEX. Record the file offset
1158 // OFF. Add their names to POOL. Return the new file offset.
1161 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1162 size_t dyn_global_index, size_t dyncount,
1167 gold_assert(index != 0);
1168 this->first_global_index_ = index;
1170 this->dynamic_offset_ = dynoff;
1171 this->first_dynamic_global_index_ = dyn_global_index;
1172 this->dynamic_count_ = dyncount;
1174 if (parameters->get_size() == 32)
1176 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1177 ret = this->sized_finalize<32>(index, off, pool);
1182 else if (parameters->get_size() == 64)
1184 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1185 ret = this->sized_finalize<64>(index, off, pool);
1193 // Now that we have the final symbol table, we can reliably note
1194 // which symbols should get warnings.
1195 this->warnings_.note_warnings(this);
1200 // Set the final value for all the symbols. This is called after
1201 // Layout::finalize, so all the output sections have their final
1206 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1208 off = align_address(off, size >> 3);
1209 this->offset_ = off;
1211 size_t orig_index = index;
1213 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1214 for (Symbol_table_type::iterator p = this->table_.begin();
1215 p != this->table_.end();
1218 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1220 // FIXME: Here we need to decide which symbols should go into
1221 // the output file, based on --strip.
1223 // The default version of a symbol may appear twice in the
1224 // symbol table. We only need to finalize it once.
1225 if (sym->has_symtab_index())
1230 gold_assert(!sym->has_symtab_index());
1231 sym->set_symtab_index(-1U);
1232 gold_assert(sym->dynsym_index() == -1U);
1236 typename Sized_symbol<size>::Value_type value;
1238 switch (sym->source())
1240 case Symbol::FROM_OBJECT:
1242 unsigned int shndx = sym->shndx();
1244 // FIXME: We need some target specific support here.
1245 if (shndx >= elfcpp::SHN_LORESERVE
1246 && shndx != elfcpp::SHN_ABS)
1248 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1249 program_name, sym->name(), shndx);
1253 Object* symobj = sym->object();
1254 if (symobj->is_dynamic())
1257 shndx = elfcpp::SHN_UNDEF;
1259 else if (shndx == elfcpp::SHN_UNDEF)
1261 else if (shndx == elfcpp::SHN_ABS)
1262 value = sym->value();
1265 Relobj* relobj = static_cast<Relobj*>(symobj);
1267 Output_section* os = relobj->output_section(shndx, &secoff);
1271 sym->set_symtab_index(-1U);
1272 gold_assert(sym->dynsym_index() == -1U);
1276 value = sym->value() + os->address() + secoff;
1281 case Symbol::IN_OUTPUT_DATA:
1283 Output_data* od = sym->output_data();
1284 value = sym->value() + od->address();
1285 if (sym->offset_is_from_end())
1286 value += od->data_size();
1290 case Symbol::IN_OUTPUT_SEGMENT:
1292 Output_segment* os = sym->output_segment();
1293 value = sym->value() + os->vaddr();
1294 switch (sym->offset_base())
1296 case Symbol::SEGMENT_START:
1298 case Symbol::SEGMENT_END:
1299 value += os->memsz();
1301 case Symbol::SEGMENT_BSS:
1302 value += os->filesz();
1310 case Symbol::CONSTANT:
1311 value = sym->value();
1318 sym->set_value(value);
1320 if (parameters->strip_all())
1321 sym->set_symtab_index(-1U);
1324 sym->set_symtab_index(index);
1325 pool->add(sym->name(), NULL);
1331 this->output_count_ = index - orig_index;
1336 // Write out the global symbols.
1339 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1340 const Stringpool* dynpool, Output_file* of) const
1342 if (parameters->get_size() == 32)
1344 if (parameters->is_big_endian())
1346 #ifdef HAVE_TARGET_32_BIG
1347 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1354 #ifdef HAVE_TARGET_32_LITTLE
1355 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1361 else if (parameters->get_size() == 64)
1363 if (parameters->is_big_endian())
1365 #ifdef HAVE_TARGET_64_BIG
1366 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1373 #ifdef HAVE_TARGET_64_LITTLE
1374 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1384 // Write out the global symbols.
1386 template<int size, bool big_endian>
1388 Symbol_table::sized_write_globals(const Target* target,
1389 const Stringpool* sympool,
1390 const Stringpool* dynpool,
1391 Output_file* of) const
1393 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1394 unsigned int index = this->first_global_index_;
1395 const off_t oview_size = this->output_count_ * sym_size;
1396 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1398 unsigned int dynamic_count = this->dynamic_count_;
1399 off_t dynamic_size = dynamic_count * sym_size;
1400 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1401 unsigned char* dynamic_view;
1402 if (this->dynamic_offset_ == 0)
1403 dynamic_view = NULL;
1405 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1407 unsigned char* ps = psyms;
1408 for (Symbol_table_type::const_iterator p = this->table_.begin();
1409 p != this->table_.end();
1412 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1414 unsigned int sym_index = sym->symtab_index();
1415 unsigned int dynsym_index;
1416 if (dynamic_view == NULL)
1419 dynsym_index = sym->dynsym_index();
1421 if (sym_index == -1U && dynsym_index == -1U)
1423 // This symbol is not included in the output file.
1427 if (sym_index == index)
1429 else if (sym_index != -1U)
1431 // We have already seen this symbol, because it has a
1433 gold_assert(sym_index < index);
1434 if (dynsym_index == -1U)
1440 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1441 switch (sym->source())
1443 case Symbol::FROM_OBJECT:
1445 unsigned int in_shndx = sym->shndx();
1447 // FIXME: We need some target specific support here.
1448 if (in_shndx >= elfcpp::SHN_LORESERVE
1449 && in_shndx != elfcpp::SHN_ABS)
1451 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1452 program_name, sym->name(), in_shndx);
1456 Object* symobj = sym->object();
1457 if (symobj->is_dynamic())
1459 if (sym->needs_dynsym_value())
1460 value = target->dynsym_value(sym);
1461 shndx = elfcpp::SHN_UNDEF;
1463 else if (in_shndx == elfcpp::SHN_UNDEF
1464 || in_shndx == elfcpp::SHN_ABS)
1468 Relobj* relobj = static_cast<Relobj*>(symobj);
1470 Output_section* os = relobj->output_section(in_shndx, &secoff);
1471 gold_assert(os != NULL);
1472 shndx = os->out_shndx();
1477 case Symbol::IN_OUTPUT_DATA:
1478 shndx = sym->output_data()->out_shndx();
1481 case Symbol::IN_OUTPUT_SEGMENT:
1482 shndx = elfcpp::SHN_ABS;
1485 case Symbol::CONSTANT:
1486 shndx = elfcpp::SHN_ABS;
1493 if (sym_index != -1U)
1495 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1496 sym, sym->value(), shndx, sympool, ps
1497 SELECT_SIZE_ENDIAN(size, big_endian));
1501 if (dynsym_index != -1U)
1503 dynsym_index -= first_dynamic_global_index;
1504 gold_assert(dynsym_index < dynamic_count);
1505 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1506 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1507 sym, value, shndx, dynpool, pd
1508 SELECT_SIZE_ENDIAN(size, big_endian));
1512 gold_assert(ps - psyms == oview_size);
1514 of->write_output_view(this->offset_, oview_size, psyms);
1515 if (dynamic_view != NULL)
1516 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1519 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1520 // strtab holding the name.
1522 template<int size, bool big_endian>
1524 Symbol_table::sized_write_symbol(
1525 Sized_symbol<size>* sym,
1526 typename elfcpp::Elf_types<size>::Elf_Addr value,
1528 const Stringpool* pool,
1530 ACCEPT_SIZE_ENDIAN) const
1532 elfcpp::Sym_write<size, big_endian> osym(p);
1533 osym.put_st_name(pool->get_offset(sym->name()));
1534 osym.put_st_value(value);
1535 osym.put_st_size(sym->symsize());
1536 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1537 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1538 osym.put_st_shndx(shndx);
1541 // Write out a section symbol. Return the update offset.
1544 Symbol_table::write_section_symbol(const Output_section *os,
1548 if (parameters->get_size() == 32)
1550 if (parameters->is_big_endian())
1552 #ifdef HAVE_TARGET_32_BIG
1553 this->sized_write_section_symbol<32, true>(os, of, offset);
1560 #ifdef HAVE_TARGET_32_LITTLE
1561 this->sized_write_section_symbol<32, false>(os, of, offset);
1567 else if (parameters->get_size() == 64)
1569 if (parameters->is_big_endian())
1571 #ifdef HAVE_TARGET_64_BIG
1572 this->sized_write_section_symbol<64, true>(os, of, offset);
1579 #ifdef HAVE_TARGET_64_LITTLE
1580 this->sized_write_section_symbol<64, false>(os, of, offset);
1590 // Write out a section symbol, specialized for size and endianness.
1592 template<int size, bool big_endian>
1594 Symbol_table::sized_write_section_symbol(const Output_section* os,
1598 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1600 unsigned char* pov = of->get_output_view(offset, sym_size);
1602 elfcpp::Sym_write<size, big_endian> osym(pov);
1603 osym.put_st_name(0);
1604 osym.put_st_value(os->address());
1605 osym.put_st_size(0);
1606 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1607 elfcpp::STT_SECTION));
1608 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1609 osym.put_st_shndx(os->out_shndx());
1611 of->write_output_view(offset, sym_size, pov);
1614 // Warnings functions.
1616 // Add a new warning.
1619 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1622 name = symtab->canonicalize_name(name);
1623 this->warnings_[name].set(obj, shndx);
1626 // Look through the warnings and mark the symbols for which we should
1627 // warn. This is called during Layout::finalize when we know the
1628 // sources for all the symbols.
1631 Warnings::note_warnings(Symbol_table* symtab)
1633 for (Warning_table::iterator p = this->warnings_.begin();
1634 p != this->warnings_.end();
1637 Symbol* sym = symtab->lookup(p->first, NULL);
1639 && sym->source() == Symbol::FROM_OBJECT
1640 && sym->object() == p->second.object)
1642 sym->set_has_warning();
1644 // Read the section contents to get the warning text. It
1645 // would be nicer if we only did this if we have to actually
1646 // issue a warning. Unfortunately, warnings are issued as
1647 // we relocate sections. That means that we can not lock
1648 // the object then, as we might try to issue the same
1649 // warning multiple times simultaneously.
1651 Task_locker_obj<Object> tl(*p->second.object);
1652 const unsigned char* c;
1654 c = p->second.object->section_contents(p->second.shndx, &len,
1656 p->second.set_text(reinterpret_cast<const char*>(c), len);
1662 // Issue a warning. This is called when we see a relocation against a
1663 // symbol for which has a warning.
1666 Warnings::issue_warning(const Symbol* sym, const std::string& location) const
1668 gold_assert(sym->has_warning());
1669 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1670 gold_assert(p != this->warnings_.end());
1671 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1672 p->second.text.c_str());
1675 // Instantiate the templates we need. We could use the configure
1676 // script to restrict this to only the ones needed for implemented
1679 #ifdef HAVE_TARGET_32_LITTLE
1682 Symbol_table::add_from_relobj<32, false>(
1683 Sized_relobj<32, false>* relobj,
1684 const unsigned char* syms,
1686 const char* sym_names,
1687 size_t sym_name_size,
1688 Symbol** sympointers);
1691 #ifdef HAVE_TARGET_32_BIG
1694 Symbol_table::add_from_relobj<32, true>(
1695 Sized_relobj<32, true>* relobj,
1696 const unsigned char* syms,
1698 const char* sym_names,
1699 size_t sym_name_size,
1700 Symbol** sympointers);
1703 #ifdef HAVE_TARGET_64_LITTLE
1706 Symbol_table::add_from_relobj<64, false>(
1707 Sized_relobj<64, false>* relobj,
1708 const unsigned char* syms,
1710 const char* sym_names,
1711 size_t sym_name_size,
1712 Symbol** sympointers);
1715 #ifdef HAVE_TARGET_64_BIG
1718 Symbol_table::add_from_relobj<64, true>(
1719 Sized_relobj<64, true>* relobj,
1720 const unsigned char* syms,
1722 const char* sym_names,
1723 size_t sym_name_size,
1724 Symbol** sympointers);
1727 #ifdef HAVE_TARGET_32_LITTLE
1730 Symbol_table::add_from_dynobj<32, false>(
1731 Sized_dynobj<32, false>* dynobj,
1732 const unsigned char* syms,
1734 const char* sym_names,
1735 size_t sym_name_size,
1736 const unsigned char* versym,
1738 const std::vector<const char*>* version_map);
1741 #ifdef HAVE_TARGET_32_BIG
1744 Symbol_table::add_from_dynobj<32, true>(
1745 Sized_dynobj<32, true>* dynobj,
1746 const unsigned char* syms,
1748 const char* sym_names,
1749 size_t sym_name_size,
1750 const unsigned char* versym,
1752 const std::vector<const char*>* version_map);
1755 #ifdef HAVE_TARGET_64_LITTLE
1758 Symbol_table::add_from_dynobj<64, false>(
1759 Sized_dynobj<64, false>* dynobj,
1760 const unsigned char* syms,
1762 const char* sym_names,
1763 size_t sym_name_size,
1764 const unsigned char* versym,
1766 const std::vector<const char*>* version_map);
1769 #ifdef HAVE_TARGET_64_BIG
1772 Symbol_table::add_from_dynobj<64, true>(
1773 Sized_dynobj<64, true>* dynobj,
1774 const unsigned char* syms,
1776 const char* sym_names,
1777 size_t sym_name_size,
1778 const unsigned char* versym,
1780 const std::vector<const char*>* version_map);
1783 } // End namespace gold.