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)
517 relobj->error(_("bad global symbol name offset %u at %zu"),
522 const char* name = sym_names + st_name;
524 // A symbol defined in a section which we are not including must
525 // be treated as an undefined symbol.
526 unsigned char symbuf[sym_size];
527 elfcpp::Sym<size, big_endian> sym2(symbuf);
528 unsigned int st_shndx = psym->get_st_shndx();
529 if (st_shndx != elfcpp::SHN_UNDEF
530 && st_shndx < elfcpp::SHN_LORESERVE
531 && !relobj->is_section_included(st_shndx))
533 memcpy(symbuf, p, sym_size);
534 elfcpp::Sym_write<size, big_endian> sw(symbuf);
535 sw.put_st_shndx(elfcpp::SHN_UNDEF);
539 // In an object file, an '@' in the name separates the symbol
540 // name from the version name. If there are two '@' characters,
541 // this is the default version.
542 const char* ver = strchr(name, '@');
547 Stringpool::Key name_key;
548 name = this->namepool_.add(name, true, &name_key);
549 res = this->add_from_object(relobj, name, name_key, NULL, 0,
554 Stringpool::Key name_key;
555 name = this->namepool_.add_prefix(name, ver - name, &name_key);
565 Stringpool::Key ver_key;
566 ver = this->namepool_.add(ver, true, &ver_key);
568 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
572 *sympointers++ = res;
576 // Add all the symbols in a dynamic object to the hash table.
578 template<int size, bool big_endian>
580 Symbol_table::add_from_dynobj(
581 Sized_dynobj<size, big_endian>* dynobj,
582 const unsigned char* syms,
584 const char* sym_names,
585 size_t sym_name_size,
586 const unsigned char* versym,
588 const std::vector<const char*>* version_map)
590 gold_assert(size == dynobj->target()->get_size());
591 gold_assert(size == parameters->get_size());
593 if (versym != NULL && versym_size / 2 < count)
595 dynobj->error(_("too few symbol versions"));
599 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
601 const unsigned char* p = syms;
602 const unsigned char* vs = versym;
603 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
605 elfcpp::Sym<size, big_endian> sym(p);
607 // Ignore symbols with local binding.
608 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
611 unsigned int st_name = sym.get_st_name();
612 if (st_name >= sym_name_size)
614 dynobj->error(_("bad symbol name offset %u at %zu"),
619 const char* name = sym_names + st_name;
623 Stringpool::Key name_key;
624 name = this->namepool_.add(name, true, &name_key);
625 this->add_from_object(dynobj, name, name_key, NULL, 0,
630 // Read the version information.
632 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
634 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
635 v &= elfcpp::VERSYM_VERSION;
637 // The Sun documentation says that V can be VER_NDX_LOCAL, or
638 // VER_NDX_GLOBAL, or a version index. The meaning of
639 // VER_NDX_LOCAL is defined as "Symbol has local scope." The
640 // old GNU linker will happily generate VER_NDX_LOCAL for an
641 // undefined symbol. I don't know what the Sun linker will
644 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
645 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
647 // This symbol should not be visible outside the object.
651 // At this point we are definitely going to add this symbol.
652 Stringpool::Key name_key;
653 name = this->namepool_.add(name, true, &name_key);
655 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
656 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
658 // This symbol does not have a version.
659 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
663 if (v >= version_map->size())
665 dynobj->error(_("versym for symbol %zu out of range: %u"), i, v);
669 const char* version = (*version_map)[v];
672 dynobj->error(_("versym for symbol %zu has no name: %u"), i, v);
676 Stringpool::Key version_key;
677 version = this->namepool_.add(version, true, &version_key);
679 // If this is an absolute symbol, and the version name and
680 // symbol name are the same, then this is the version definition
681 // symbol. These symbols exist to support using -u to pull in
682 // particular versions. We do not want to record a version for
684 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
686 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
690 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
692 this->add_from_object(dynobj, name, name_key, version, version_key,
697 // Create and return a specially defined symbol. If ONLY_IF_REF is
698 // true, then only create the symbol if there is a reference to it.
699 // If this does not return NULL, it sets *POLDSYM to the existing
700 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
702 template<int size, bool big_endian>
704 Symbol_table::define_special_symbol(const Target* target, const char** pname,
705 const char** pversion, bool only_if_ref,
706 Sized_symbol<size>** poldsym
710 Sized_symbol<size>* sym;
711 bool add_to_table = false;
712 typename Symbol_table_type::iterator add_loc = this->table_.end();
716 oldsym = this->lookup(*pname, *pversion);
717 if (oldsym == NULL || !oldsym->is_undefined())
720 *pname = oldsym->name();
721 *pversion = oldsym->version();
725 // Canonicalize NAME and VERSION.
726 Stringpool::Key name_key;
727 *pname = this->namepool_.add(*pname, true, &name_key);
729 Stringpool::Key version_key = 0;
730 if (*pversion != NULL)
731 *pversion = this->namepool_.add(*pversion, true, &version_key);
733 Symbol* const snull = NULL;
734 std::pair<typename Symbol_table_type::iterator, bool> ins =
735 this->table_.insert(std::make_pair(std::make_pair(name_key,
741 // We already have a symbol table entry for NAME/VERSION.
742 oldsym = ins.first->second;
743 gold_assert(oldsym != NULL);
747 // We haven't seen this symbol before.
748 gold_assert(ins.first->second == NULL);
755 if (!target->has_make_symbol())
756 sym = new Sized_symbol<size>();
759 gold_assert(target->get_size() == size);
760 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
761 typedef Sized_target<size, big_endian> My_target;
762 const My_target* sized_target =
763 static_cast<const My_target*>(target);
764 sym = sized_target->make_symbol();
770 add_loc->second = sym;
772 gold_assert(oldsym != NULL);
774 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
780 // Define a symbol based on an Output_data.
783 Symbol_table::define_in_output_data(const Target* target, const char* name,
784 const char* version, Output_data* od,
785 uint64_t value, uint64_t symsize,
786 elfcpp::STT type, elfcpp::STB binding,
787 elfcpp::STV visibility,
788 unsigned char nonvis,
789 bool offset_is_from_end,
792 if (parameters->get_size() == 32)
794 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
795 return this->do_define_in_output_data<32>(target, name, version, od,
796 value, symsize, type, binding,
804 else if (parameters->get_size() == 64)
806 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
807 return this->do_define_in_output_data<64>(target, name, version, od,
808 value, symsize, type, binding,
820 // Define a symbol in an Output_data, sized version.
824 Symbol_table::do_define_in_output_data(
825 const Target* target,
829 typename elfcpp::Elf_types<size>::Elf_Addr value,
830 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
833 elfcpp::STV visibility,
834 unsigned char nonvis,
835 bool offset_is_from_end,
838 Sized_symbol<size>* sym;
839 Sized_symbol<size>* oldsym;
841 if (parameters->is_big_endian())
843 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
844 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
845 target, &name, &version, only_if_ref, &oldsym
846 SELECT_SIZE_ENDIAN(size, true));
853 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
854 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
855 target, &name, &version, only_if_ref, &oldsym
856 SELECT_SIZE_ENDIAN(size, false));
865 gold_assert(version == NULL || oldsym != NULL);
866 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
870 && Symbol_table::should_override_with_special(oldsym))
871 oldsym->override_with_special(sym);
876 // Define a symbol based on an Output_segment.
879 Symbol_table::define_in_output_segment(const Target* target, const char* name,
880 const char* version, Output_segment* os,
881 uint64_t value, uint64_t symsize,
882 elfcpp::STT type, elfcpp::STB binding,
883 elfcpp::STV visibility,
884 unsigned char nonvis,
885 Symbol::Segment_offset_base offset_base,
888 if (parameters->get_size() == 32)
890 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
891 return this->do_define_in_output_segment<32>(target, name, version, os,
892 value, symsize, type,
893 binding, visibility, nonvis,
894 offset_base, only_if_ref);
899 else if (parameters->get_size() == 64)
901 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
902 return this->do_define_in_output_segment<64>(target, name, version, os,
903 value, symsize, type,
904 binding, visibility, nonvis,
905 offset_base, only_if_ref);
914 // Define a symbol in an Output_segment, sized version.
918 Symbol_table::do_define_in_output_segment(
919 const Target* target,
923 typename elfcpp::Elf_types<size>::Elf_Addr value,
924 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
927 elfcpp::STV visibility,
928 unsigned char nonvis,
929 Symbol::Segment_offset_base offset_base,
932 Sized_symbol<size>* sym;
933 Sized_symbol<size>* oldsym;
935 if (parameters->is_big_endian())
937 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
938 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
939 target, &name, &version, only_if_ref, &oldsym
940 SELECT_SIZE_ENDIAN(size, true));
947 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
948 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
949 target, &name, &version, only_if_ref, &oldsym
950 SELECT_SIZE_ENDIAN(size, false));
959 gold_assert(version == NULL || oldsym != NULL);
960 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
964 && Symbol_table::should_override_with_special(oldsym))
965 oldsym->override_with_special(sym);
970 // Define a special symbol with a constant value. It is a multiple
971 // definition error if this symbol is already defined.
974 Symbol_table::define_as_constant(const Target* target, const char* name,
975 const char* version, uint64_t value,
976 uint64_t symsize, elfcpp::STT type,
977 elfcpp::STB binding, elfcpp::STV visibility,
978 unsigned char nonvis, bool only_if_ref)
980 if (parameters->get_size() == 32)
982 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
983 return this->do_define_as_constant<32>(target, name, version, value,
984 symsize, type, binding,
985 visibility, nonvis, only_if_ref);
990 else if (parameters->get_size() == 64)
992 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
993 return this->do_define_as_constant<64>(target, name, version, value,
994 symsize, type, binding,
995 visibility, nonvis, only_if_ref);
1004 // Define a symbol as a constant, sized version.
1008 Symbol_table::do_define_as_constant(
1009 const Target* target,
1011 const char* version,
1012 typename elfcpp::Elf_types<size>::Elf_Addr value,
1013 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1015 elfcpp::STB binding,
1016 elfcpp::STV visibility,
1017 unsigned char nonvis,
1020 Sized_symbol<size>* sym;
1021 Sized_symbol<size>* oldsym;
1023 if (parameters->is_big_endian())
1025 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1026 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1027 target, &name, &version, only_if_ref, &oldsym
1028 SELECT_SIZE_ENDIAN(size, true));
1035 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1036 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1037 target, &name, &version, only_if_ref, &oldsym
1038 SELECT_SIZE_ENDIAN(size, false));
1047 gold_assert(version == NULL || oldsym != NULL);
1048 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1051 && Symbol_table::should_override_with_special(oldsym))
1052 oldsym->override_with_special(sym);
1057 // Define a set of symbols in output sections.
1060 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1061 int count, const Define_symbol_in_section* p)
1063 for (int i = 0; i < count; ++i, ++p)
1065 Output_section* os = layout->find_output_section(p->output_section);
1067 this->define_in_output_data(target, p->name, NULL, os, p->value,
1068 p->size, p->type, p->binding,
1069 p->visibility, p->nonvis,
1070 p->offset_is_from_end, p->only_if_ref);
1072 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1073 p->binding, p->visibility, p->nonvis,
1078 // Define a set of symbols in output segments.
1081 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1082 int count, const Define_symbol_in_segment* p)
1084 for (int i = 0; i < count; ++i, ++p)
1086 Output_segment* os = layout->find_output_segment(p->segment_type,
1087 p->segment_flags_set,
1088 p->segment_flags_clear);
1090 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1091 p->size, p->type, p->binding,
1092 p->visibility, p->nonvis,
1093 p->offset_base, p->only_if_ref);
1095 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1096 p->binding, p->visibility, p->nonvis,
1101 // Set the dynamic symbol indexes. INDEX is the index of the first
1102 // global dynamic symbol. Pointers to the symbols are stored into the
1103 // vector SYMS. The names are added to DYNPOOL. This returns an
1104 // updated dynamic symbol index.
1107 Symbol_table::set_dynsym_indexes(const General_options* options,
1108 const Target* target,
1110 std::vector<Symbol*>* syms,
1111 Stringpool* dynpool,
1114 for (Symbol_table_type::iterator p = this->table_.begin();
1115 p != this->table_.end();
1118 Symbol* sym = p->second;
1120 // Note that SYM may already have a dynamic symbol index, since
1121 // some symbols appear more than once in the symbol table, with
1122 // and without a version.
1124 if (!sym->needs_dynsym_entry()
1125 && (!options->export_dynamic()
1127 || !sym->is_externally_visible()))
1128 sym->set_dynsym_index(-1U);
1129 else if (!sym->has_dynsym_index())
1131 sym->set_dynsym_index(index);
1133 syms->push_back(sym);
1134 dynpool->add(sym->name(), false, NULL);
1136 // Record any version information.
1137 if (sym->version() != NULL)
1138 versions->record_version(options, dynpool, sym);
1142 // Finish up the versions. In some cases this may add new dynamic
1144 index = versions->finalize(target, this, index, syms);
1149 // Set the final values for all the symbols. The index of the first
1150 // global symbol in the output file is INDEX. Record the file offset
1151 // OFF. Add their names to POOL. Return the new file offset.
1154 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1155 size_t dyn_global_index, size_t dyncount,
1160 gold_assert(index != 0);
1161 this->first_global_index_ = index;
1163 this->dynamic_offset_ = dynoff;
1164 this->first_dynamic_global_index_ = dyn_global_index;
1165 this->dynamic_count_ = dyncount;
1167 if (parameters->get_size() == 32)
1169 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1170 ret = this->sized_finalize<32>(index, off, pool);
1175 else if (parameters->get_size() == 64)
1177 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1178 ret = this->sized_finalize<64>(index, off, pool);
1186 // Now that we have the final symbol table, we can reliably note
1187 // which symbols should get warnings.
1188 this->warnings_.note_warnings(this);
1193 // Set the final value for all the symbols. This is called after
1194 // Layout::finalize, so all the output sections have their final
1199 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1201 off = align_address(off, size >> 3);
1202 this->offset_ = off;
1204 size_t orig_index = index;
1206 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1207 for (Symbol_table_type::iterator p = this->table_.begin();
1208 p != this->table_.end();
1211 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1213 // FIXME: Here we need to decide which symbols should go into
1214 // the output file, based on --strip.
1216 // The default version of a symbol may appear twice in the
1217 // symbol table. We only need to finalize it once.
1218 if (sym->has_symtab_index())
1223 gold_assert(!sym->has_symtab_index());
1224 sym->set_symtab_index(-1U);
1225 gold_assert(sym->dynsym_index() == -1U);
1229 typename Sized_symbol<size>::Value_type value;
1231 switch (sym->source())
1233 case Symbol::FROM_OBJECT:
1235 unsigned int shndx = sym->shndx();
1237 // FIXME: We need some target specific support here.
1238 if (shndx >= elfcpp::SHN_LORESERVE
1239 && shndx != elfcpp::SHN_ABS)
1241 gold_error(_("%s: unsupported symbol section 0x%x"),
1242 sym->name(), shndx);
1243 shndx = elfcpp::SHN_UNDEF;
1246 Object* symobj = sym->object();
1247 if (symobj->is_dynamic())
1250 shndx = elfcpp::SHN_UNDEF;
1252 else if (shndx == elfcpp::SHN_UNDEF)
1254 else if (shndx == elfcpp::SHN_ABS)
1255 value = sym->value();
1258 Relobj* relobj = static_cast<Relobj*>(symobj);
1260 Output_section* os = relobj->output_section(shndx, &secoff);
1264 sym->set_symtab_index(-1U);
1265 gold_assert(sym->dynsym_index() == -1U);
1269 value = sym->value() + os->address() + secoff;
1274 case Symbol::IN_OUTPUT_DATA:
1276 Output_data* od = sym->output_data();
1277 value = sym->value() + od->address();
1278 if (sym->offset_is_from_end())
1279 value += od->data_size();
1283 case Symbol::IN_OUTPUT_SEGMENT:
1285 Output_segment* os = sym->output_segment();
1286 value = sym->value() + os->vaddr();
1287 switch (sym->offset_base())
1289 case Symbol::SEGMENT_START:
1291 case Symbol::SEGMENT_END:
1292 value += os->memsz();
1294 case Symbol::SEGMENT_BSS:
1295 value += os->filesz();
1303 case Symbol::CONSTANT:
1304 value = sym->value();
1311 sym->set_value(value);
1313 if (parameters->strip_all())
1314 sym->set_symtab_index(-1U);
1317 sym->set_symtab_index(index);
1318 pool->add(sym->name(), false, NULL);
1324 this->output_count_ = index - orig_index;
1329 // Write out the global symbols.
1332 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1333 const Stringpool* dynpool, Output_file* of) const
1335 if (parameters->get_size() == 32)
1337 if (parameters->is_big_endian())
1339 #ifdef HAVE_TARGET_32_BIG
1340 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1347 #ifdef HAVE_TARGET_32_LITTLE
1348 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1354 else if (parameters->get_size() == 64)
1356 if (parameters->is_big_endian())
1358 #ifdef HAVE_TARGET_64_BIG
1359 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1366 #ifdef HAVE_TARGET_64_LITTLE
1367 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1377 // Write out the global symbols.
1379 template<int size, bool big_endian>
1381 Symbol_table::sized_write_globals(const Target* target,
1382 const Stringpool* sympool,
1383 const Stringpool* dynpool,
1384 Output_file* of) const
1386 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1387 unsigned int index = this->first_global_index_;
1388 const off_t oview_size = this->output_count_ * sym_size;
1389 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1391 unsigned int dynamic_count = this->dynamic_count_;
1392 off_t dynamic_size = dynamic_count * sym_size;
1393 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1394 unsigned char* dynamic_view;
1395 if (this->dynamic_offset_ == 0)
1396 dynamic_view = NULL;
1398 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1400 unsigned char* ps = psyms;
1401 for (Symbol_table_type::const_iterator p = this->table_.begin();
1402 p != this->table_.end();
1405 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1407 unsigned int sym_index = sym->symtab_index();
1408 unsigned int dynsym_index;
1409 if (dynamic_view == NULL)
1412 dynsym_index = sym->dynsym_index();
1414 if (sym_index == -1U && dynsym_index == -1U)
1416 // This symbol is not included in the output file.
1420 if (sym_index == index)
1422 else if (sym_index != -1U)
1424 // We have already seen this symbol, because it has a
1426 gold_assert(sym_index < index);
1427 if (dynsym_index == -1U)
1433 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1434 switch (sym->source())
1436 case Symbol::FROM_OBJECT:
1438 unsigned int in_shndx = sym->shndx();
1440 // FIXME: We need some target specific support here.
1441 if (in_shndx >= elfcpp::SHN_LORESERVE
1442 && in_shndx != elfcpp::SHN_ABS)
1444 gold_error(_("%s: unsupported symbol section 0x%x"),
1445 sym->name(), in_shndx);
1450 Object* symobj = sym->object();
1451 if (symobj->is_dynamic())
1453 if (sym->needs_dynsym_value())
1454 value = target->dynsym_value(sym);
1455 shndx = elfcpp::SHN_UNDEF;
1457 else if (in_shndx == elfcpp::SHN_UNDEF
1458 || in_shndx == elfcpp::SHN_ABS)
1462 Relobj* relobj = static_cast<Relobj*>(symobj);
1464 Output_section* os = relobj->output_section(in_shndx,
1466 gold_assert(os != NULL);
1467 shndx = os->out_shndx();
1473 case Symbol::IN_OUTPUT_DATA:
1474 shndx = sym->output_data()->out_shndx();
1477 case Symbol::IN_OUTPUT_SEGMENT:
1478 shndx = elfcpp::SHN_ABS;
1481 case Symbol::CONSTANT:
1482 shndx = elfcpp::SHN_ABS;
1489 if (sym_index != -1U)
1491 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1492 sym, sym->value(), shndx, sympool, ps
1493 SELECT_SIZE_ENDIAN(size, big_endian));
1497 if (dynsym_index != -1U)
1499 dynsym_index -= first_dynamic_global_index;
1500 gold_assert(dynsym_index < dynamic_count);
1501 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1502 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1503 sym, value, shndx, dynpool, pd
1504 SELECT_SIZE_ENDIAN(size, big_endian));
1508 gold_assert(ps - psyms == oview_size);
1510 of->write_output_view(this->offset_, oview_size, psyms);
1511 if (dynamic_view != NULL)
1512 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1515 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1516 // strtab holding the name.
1518 template<int size, bool big_endian>
1520 Symbol_table::sized_write_symbol(
1521 Sized_symbol<size>* sym,
1522 typename elfcpp::Elf_types<size>::Elf_Addr value,
1524 const Stringpool* pool,
1526 ACCEPT_SIZE_ENDIAN) const
1528 elfcpp::Sym_write<size, big_endian> osym(p);
1529 osym.put_st_name(pool->get_offset(sym->name()));
1530 osym.put_st_value(value);
1531 osym.put_st_size(sym->symsize());
1532 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1533 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1534 osym.put_st_shndx(shndx);
1537 // Write out a section symbol. Return the update offset.
1540 Symbol_table::write_section_symbol(const Output_section *os,
1544 if (parameters->get_size() == 32)
1546 if (parameters->is_big_endian())
1548 #ifdef HAVE_TARGET_32_BIG
1549 this->sized_write_section_symbol<32, true>(os, of, offset);
1556 #ifdef HAVE_TARGET_32_LITTLE
1557 this->sized_write_section_symbol<32, false>(os, of, offset);
1563 else if (parameters->get_size() == 64)
1565 if (parameters->is_big_endian())
1567 #ifdef HAVE_TARGET_64_BIG
1568 this->sized_write_section_symbol<64, true>(os, of, offset);
1575 #ifdef HAVE_TARGET_64_LITTLE
1576 this->sized_write_section_symbol<64, false>(os, of, offset);
1586 // Write out a section symbol, specialized for size and endianness.
1588 template<int size, bool big_endian>
1590 Symbol_table::sized_write_section_symbol(const Output_section* os,
1594 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1596 unsigned char* pov = of->get_output_view(offset, sym_size);
1598 elfcpp::Sym_write<size, big_endian> osym(pov);
1599 osym.put_st_name(0);
1600 osym.put_st_value(os->address());
1601 osym.put_st_size(0);
1602 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1603 elfcpp::STT_SECTION));
1604 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1605 osym.put_st_shndx(os->out_shndx());
1607 of->write_output_view(offset, sym_size, pov);
1610 // Warnings functions.
1612 // Add a new warning.
1615 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1618 name = symtab->canonicalize_name(name);
1619 this->warnings_[name].set(obj, shndx);
1622 // Look through the warnings and mark the symbols for which we should
1623 // warn. This is called during Layout::finalize when we know the
1624 // sources for all the symbols.
1627 Warnings::note_warnings(Symbol_table* symtab)
1629 for (Warning_table::iterator p = this->warnings_.begin();
1630 p != this->warnings_.end();
1633 Symbol* sym = symtab->lookup(p->first, NULL);
1635 && sym->source() == Symbol::FROM_OBJECT
1636 && sym->object() == p->second.object)
1638 sym->set_has_warning();
1640 // Read the section contents to get the warning text. It
1641 // would be nicer if we only did this if we have to actually
1642 // issue a warning. Unfortunately, warnings are issued as
1643 // we relocate sections. That means that we can not lock
1644 // the object then, as we might try to issue the same
1645 // warning multiple times simultaneously.
1647 Task_locker_obj<Object> tl(*p->second.object);
1648 const unsigned char* c;
1650 c = p->second.object->section_contents(p->second.shndx, &len,
1652 p->second.set_text(reinterpret_cast<const char*>(c), len);
1658 // Issue a warning. This is called when we see a relocation against a
1659 // symbol for which has a warning.
1661 template<int size, bool big_endian>
1663 Warnings::issue_warning(const Symbol* sym,
1664 const Relocate_info<size, big_endian>* relinfo,
1665 size_t relnum, off_t reloffset) const
1667 gold_assert(sym->has_warning());
1668 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1669 gold_assert(p != this->warnings_.end());
1670 gold_warning_at_location(relinfo, relnum, reloffset,
1671 "%s", p->second.text.c_str());
1674 // Instantiate the templates we need. We could use the configure
1675 // script to restrict this to only the ones needed for implemented
1678 #ifdef HAVE_TARGET_32_LITTLE
1681 Symbol_table::add_from_relobj<32, false>(
1682 Sized_relobj<32, false>* relobj,
1683 const unsigned char* syms,
1685 const char* sym_names,
1686 size_t sym_name_size,
1687 Symbol** sympointers);
1690 #ifdef HAVE_TARGET_32_BIG
1693 Symbol_table::add_from_relobj<32, true>(
1694 Sized_relobj<32, true>* relobj,
1695 const unsigned char* syms,
1697 const char* sym_names,
1698 size_t sym_name_size,
1699 Symbol** sympointers);
1702 #ifdef HAVE_TARGET_64_LITTLE
1705 Symbol_table::add_from_relobj<64, false>(
1706 Sized_relobj<64, false>* relobj,
1707 const unsigned char* syms,
1709 const char* sym_names,
1710 size_t sym_name_size,
1711 Symbol** sympointers);
1714 #ifdef HAVE_TARGET_64_BIG
1717 Symbol_table::add_from_relobj<64, true>(
1718 Sized_relobj<64, true>* relobj,
1719 const unsigned char* syms,
1721 const char* sym_names,
1722 size_t sym_name_size,
1723 Symbol** sympointers);
1726 #ifdef HAVE_TARGET_32_LITTLE
1729 Symbol_table::add_from_dynobj<32, false>(
1730 Sized_dynobj<32, false>* dynobj,
1731 const unsigned char* syms,
1733 const char* sym_names,
1734 size_t sym_name_size,
1735 const unsigned char* versym,
1737 const std::vector<const char*>* version_map);
1740 #ifdef HAVE_TARGET_32_BIG
1743 Symbol_table::add_from_dynobj<32, true>(
1744 Sized_dynobj<32, true>* dynobj,
1745 const unsigned char* syms,
1747 const char* sym_names,
1748 size_t sym_name_size,
1749 const unsigned char* versym,
1751 const std::vector<const char*>* version_map);
1754 #ifdef HAVE_TARGET_64_LITTLE
1757 Symbol_table::add_from_dynobj<64, false>(
1758 Sized_dynobj<64, false>* dynobj,
1759 const unsigned char* syms,
1761 const char* sym_names,
1762 size_t sym_name_size,
1763 const unsigned char* versym,
1765 const std::vector<const char*>* version_map);
1768 #ifdef HAVE_TARGET_64_BIG
1771 Symbol_table::add_from_dynobj<64, true>(
1772 Sized_dynobj<64, true>* dynobj,
1773 const unsigned char* syms,
1775 const char* sym_names,
1776 size_t sym_name_size,
1777 const unsigned char* versym,
1779 const std::vector<const char*>* version_map);
1782 #ifdef HAVE_TARGET_32_LITTLE
1785 Warnings::issue_warning<32, false>(const Symbol* sym,
1786 const Relocate_info<32, false>* relinfo,
1787 size_t relnum, off_t reloffset) const;
1790 #ifdef HAVE_TARGET_32_BIG
1793 Warnings::issue_warning<32, true>(const Symbol* sym,
1794 const Relocate_info<32, true>* relinfo,
1795 size_t relnum, off_t reloffset) const;
1798 #ifdef HAVE_TARGET_64_LITTLE
1801 Warnings::issue_warning<64, false>(const Symbol* sym,
1802 const Relocate_info<64, false>* relinfo,
1803 size_t relnum, off_t reloffset) const;
1806 #ifdef HAVE_TARGET_64_BIG
1809 Warnings::issue_warning<64, true>(const Symbol* sym,
1810 const Relocate_info<64, true>* relinfo,
1811 size_t relnum, off_t reloffset) const;
1815 } // End namespace gold.