1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008 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.
32 #include "dwarf_reader.h"
36 #include "workqueue.h"
44 // Initialize fields in Symbol. This initializes everything except u_
48 Symbol::init_fields(const char* name, const char* version,
49 elfcpp::STT type, elfcpp::STB binding,
50 elfcpp::STV visibility, unsigned char nonvis)
53 this->version_ = version;
54 this->symtab_index_ = 0;
55 this->dynsym_index_ = 0;
56 this->got_offset_ = 0;
57 this->plt_offset_ = 0;
59 this->binding_ = binding;
60 this->visibility_ = visibility;
61 this->nonvis_ = nonvis;
62 this->is_target_special_ = false;
63 this->is_def_ = false;
64 this->is_forwarder_ = false;
65 this->has_alias_ = false;
66 this->needs_dynsym_entry_ = false;
67 this->in_reg_ = false;
68 this->in_dyn_ = false;
69 this->has_got_offset_ = false;
70 this->has_plt_offset_ = false;
71 this->has_warning_ = false;
72 this->is_copied_from_dynobj_ = false;
75 // Return the demangled version of the symbol's name, but only
76 // if the --demangle flag was set.
79 demangle(const char* name)
81 if (!parameters->demangle())
84 // cplus_demangle allocates memory for the result it returns,
85 // and returns NULL if the name is already demangled.
86 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
87 if (demangled_name == NULL)
90 std::string retval(demangled_name);
96 Symbol::demangled_name() const
98 return demangle(this->name());
101 // Initialize the fields in the base class Symbol for SYM in OBJECT.
103 template<int size, bool big_endian>
105 Symbol::init_base(const char* name, const char* version, Object* object,
106 const elfcpp::Sym<size, big_endian>& sym)
108 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
109 sym.get_st_visibility(), sym.get_st_nonvis());
110 this->u_.from_object.object = object;
111 // FIXME: Handle SHN_XINDEX.
112 this->u_.from_object.shndx = sym.get_st_shndx();
113 this->source_ = FROM_OBJECT;
114 this->in_reg_ = !object->is_dynamic();
115 this->in_dyn_ = object->is_dynamic();
118 // Initialize the fields in the base class Symbol for a symbol defined
119 // in an Output_data.
122 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
123 elfcpp::STB binding, elfcpp::STV visibility,
124 unsigned char nonvis, bool offset_is_from_end)
126 this->init_fields(name, NULL, type, binding, visibility, nonvis);
127 this->u_.in_output_data.output_data = od;
128 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
129 this->source_ = IN_OUTPUT_DATA;
130 this->in_reg_ = true;
133 // Initialize the fields in the base class Symbol for a symbol defined
134 // in an Output_segment.
137 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
138 elfcpp::STB binding, elfcpp::STV visibility,
139 unsigned char nonvis, Segment_offset_base offset_base)
141 this->init_fields(name, NULL, type, binding, visibility, nonvis);
142 this->u_.in_output_segment.output_segment = os;
143 this->u_.in_output_segment.offset_base = offset_base;
144 this->source_ = IN_OUTPUT_SEGMENT;
145 this->in_reg_ = true;
148 // Initialize the fields in the base class Symbol for a symbol defined
152 Symbol::init_base(const char* name, elfcpp::STT type,
153 elfcpp::STB binding, elfcpp::STV visibility,
154 unsigned char nonvis)
156 this->init_fields(name, NULL, type, binding, visibility, nonvis);
157 this->source_ = CONSTANT;
158 this->in_reg_ = true;
161 // Allocate a common symbol in the base.
164 Symbol::allocate_base_common(Output_data* od)
166 gold_assert(this->is_common());
167 this->source_ = IN_OUTPUT_DATA;
168 this->u_.in_output_data.output_data = od;
169 this->u_.in_output_data.offset_is_from_end = false;
172 // Initialize the fields in Sized_symbol for SYM in OBJECT.
175 template<bool big_endian>
177 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
178 const elfcpp::Sym<size, big_endian>& sym)
180 this->init_base(name, version, object, sym);
181 this->value_ = sym.get_st_value();
182 this->symsize_ = sym.get_st_size();
185 // Initialize the fields in Sized_symbol for a symbol defined in an
190 Sized_symbol<size>::init(const char* name, Output_data* od,
191 Value_type value, Size_type symsize,
192 elfcpp::STT type, elfcpp::STB binding,
193 elfcpp::STV visibility, unsigned char nonvis,
194 bool offset_is_from_end)
196 this->init_base(name, od, type, binding, visibility, nonvis,
198 this->value_ = value;
199 this->symsize_ = symsize;
202 // Initialize the fields in Sized_symbol for a symbol defined in an
207 Sized_symbol<size>::init(const char* name, Output_segment* os,
208 Value_type value, Size_type symsize,
209 elfcpp::STT type, elfcpp::STB binding,
210 elfcpp::STV visibility, unsigned char nonvis,
211 Segment_offset_base offset_base)
213 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
214 this->value_ = value;
215 this->symsize_ = symsize;
218 // Initialize the fields in Sized_symbol for a symbol defined as a
223 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
224 elfcpp::STT type, elfcpp::STB binding,
225 elfcpp::STV visibility, unsigned char nonvis)
227 this->init_base(name, type, binding, visibility, nonvis);
228 this->value_ = value;
229 this->symsize_ = symsize;
232 // Allocate a common symbol.
236 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
238 this->allocate_base_common(od);
239 this->value_ = value;
242 // Return true if this symbol should be added to the dynamic symbol
246 Symbol::should_add_dynsym_entry() const
248 // If the symbol is used by a dynamic relocation, we need to add it.
249 if (this->needs_dynsym_entry())
252 // If exporting all symbols or building a shared library,
253 // and the symbol is defined in a regular object and is
254 // externally visible, we need to add it.
255 if ((parameters->export_dynamic() || parameters->output_is_shared())
256 && !this->is_from_dynobj()
257 && this->is_externally_visible())
263 // Return true if the final value of this symbol is known at link
267 Symbol::final_value_is_known() const
269 // If we are not generating an executable, then no final values are
270 // known, since they will change at runtime.
271 if (!parameters->output_is_executable())
274 // If the symbol is not from an object file, then it is defined, and
276 if (this->source_ != FROM_OBJECT)
279 // If the symbol is from a dynamic object, then the final value is
281 if (this->object()->is_dynamic())
284 // If the symbol is not undefined (it is defined or common), then
285 // the final value is known.
286 if (!this->is_undefined())
289 // If the symbol is undefined, then whether the final value is known
290 // depends on whether we are doing a static link. If we are doing a
291 // dynamic link, then the final value could be filled in at runtime.
292 // This could reasonably be the case for a weak undefined symbol.
293 return parameters->doing_static_link();
296 // Class Symbol_table.
298 Symbol_table::Symbol_table(unsigned int count,
299 const Version_script_info& version_script)
300 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
301 forwarders_(), commons_(), warnings_(), version_script_(version_script)
303 namepool_.reserve(count);
306 Symbol_table::~Symbol_table()
310 // The hash function. The key values are Stringpool keys.
313 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
315 return key.first ^ key.second;
318 // The symbol table key equality function. This is called with
322 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
323 const Symbol_table_key& k2) const
325 return k1.first == k2.first && k1.second == k2.second;
328 // Make TO a symbol which forwards to FROM.
331 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
333 gold_assert(from != to);
334 gold_assert(!from->is_forwarder() && !to->is_forwarder());
335 this->forwarders_[from] = to;
336 from->set_forwarder();
339 // Resolve the forwards from FROM, returning the real symbol.
342 Symbol_table::resolve_forwards(const Symbol* from) const
344 gold_assert(from->is_forwarder());
345 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
346 this->forwarders_.find(from);
347 gold_assert(p != this->forwarders_.end());
351 // Look up a symbol by name.
354 Symbol_table::lookup(const char* name, const char* version) const
356 Stringpool::Key name_key;
357 name = this->namepool_.find(name, &name_key);
361 Stringpool::Key version_key = 0;
364 version = this->namepool_.find(version, &version_key);
369 Symbol_table_key key(name_key, version_key);
370 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
371 if (p == this->table_.end())
376 // Resolve a Symbol with another Symbol. This is only used in the
377 // unusual case where there are references to both an unversioned
378 // symbol and a symbol with a version, and we then discover that that
379 // version is the default version. Because this is unusual, we do
380 // this the slow way, by converting back to an ELF symbol.
382 template<int size, bool big_endian>
384 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
385 const char* version ACCEPT_SIZE_ENDIAN)
387 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
388 elfcpp::Sym_write<size, big_endian> esym(buf);
389 // We don't bother to set the st_name field.
390 esym.put_st_value(from->value());
391 esym.put_st_size(from->symsize());
392 esym.put_st_info(from->binding(), from->type());
393 esym.put_st_other(from->visibility(), from->nonvis());
394 esym.put_st_shndx(from->shndx());
395 this->resolve(to, esym.sym(), esym.sym(), from->object(), version);
402 // Add one symbol from OBJECT to the symbol table. NAME is symbol
403 // name and VERSION is the version; both are canonicalized. DEF is
404 // whether this is the default version.
406 // If DEF is true, then this is the definition of a default version of
407 // a symbol. That means that any lookup of NAME/NULL and any lookup
408 // of NAME/VERSION should always return the same symbol. This is
409 // obvious for references, but in particular we want to do this for
410 // definitions: overriding NAME/NULL should also override
411 // NAME/VERSION. If we don't do that, it would be very hard to
412 // override functions in a shared library which uses versioning.
414 // We implement this by simply making both entries in the hash table
415 // point to the same Symbol structure. That is easy enough if this is
416 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
417 // that we have seen both already, in which case they will both have
418 // independent entries in the symbol table. We can't simply change
419 // the symbol table entry, because we have pointers to the entries
420 // attached to the object files. So we mark the entry attached to the
421 // object file as a forwarder, and record it in the forwarders_ map.
422 // Note that entries in the hash table will never be marked as
425 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
426 // symbol exactly as it existed in the input file. SYM is usually
427 // that as well, but can be modified, for instance if we determine
428 // it's in a to-be-discarded section.
430 template<int size, bool big_endian>
432 Symbol_table::add_from_object(Object* object,
434 Stringpool::Key name_key,
436 Stringpool::Key version_key,
438 const elfcpp::Sym<size, big_endian>& sym,
439 const elfcpp::Sym<size, big_endian>& orig_sym)
441 Symbol* const snull = NULL;
442 std::pair<typename Symbol_table_type::iterator, bool> ins =
443 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
446 std::pair<typename Symbol_table_type::iterator, bool> insdef =
447 std::make_pair(this->table_.end(), false);
450 const Stringpool::Key vnull_key = 0;
451 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
456 // ins.first: an iterator, which is a pointer to a pair.
457 // ins.first->first: the key (a pair of name and version).
458 // ins.first->second: the value (Symbol*).
459 // ins.second: true if new entry was inserted, false if not.
461 Sized_symbol<size>* ret;
466 // We already have an entry for NAME/VERSION.
467 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
469 gold_assert(ret != NULL);
471 was_undefined = ret->is_undefined();
472 was_common = ret->is_common();
474 this->resolve(ret, sym, orig_sym, object, version);
480 // This is the first time we have seen NAME/NULL. Make
481 // NAME/NULL point to NAME/VERSION.
482 insdef.first->second = ret;
484 else if (insdef.first->second != ret
485 && insdef.first->second->is_undefined())
487 // This is the unfortunate case where we already have
488 // entries for both NAME/VERSION and NAME/NULL. Note
489 // that we don't want to combine them if the existing
490 // symbol is going to override the new one. FIXME: We
491 // currently just test is_undefined, but this may not do
492 // the right thing if the existing symbol is from a
493 // shared library and the new one is from a regular
496 const Sized_symbol<size>* sym2;
497 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
500 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
501 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
502 this->make_forwarder(insdef.first->second, ret);
503 insdef.first->second = ret;
509 // This is the first time we have seen NAME/VERSION.
510 gold_assert(ins.first->second == NULL);
512 was_undefined = false;
515 if (def && !insdef.second)
517 // We already have an entry for NAME/NULL. If we override
518 // it, then change it to NAME/VERSION.
519 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
522 this->resolve(ret, sym, orig_sym, object, version);
523 ins.first->second = ret;
527 Sized_target<size, big_endian>* target =
528 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
529 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
530 if (!target->has_make_symbol())
531 ret = new Sized_symbol<size>();
534 ret = target->make_symbol();
537 // This means that we don't want a symbol table
540 this->table_.erase(ins.first);
543 this->table_.erase(insdef.first);
544 // Inserting insdef invalidated ins.
545 this->table_.erase(std::make_pair(name_key,
552 ret->init(name, version, object, sym);
554 ins.first->second = ret;
557 // This is the first time we have seen NAME/NULL. Point
558 // it at the new entry for NAME/VERSION.
559 gold_assert(insdef.second);
560 insdef.first->second = ret;
565 // Record every time we see a new undefined symbol, to speed up
567 if (!was_undefined && ret->is_undefined())
568 ++this->saw_undefined_;
570 // Keep track of common symbols, to speed up common symbol
572 if (!was_common && ret->is_common())
573 this->commons_.push_back(ret);
575 ret->set_is_default(def);
579 // Add all the symbols in a relocatable object to the hash table.
581 template<int size, bool big_endian>
583 Symbol_table::add_from_relobj(
584 Sized_relobj<size, big_endian>* relobj,
585 const unsigned char* syms,
587 const char* sym_names,
588 size_t sym_name_size,
589 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
591 gold_assert(size == relobj->target()->get_size());
592 gold_assert(size == parameters->get_size());
594 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
596 const unsigned char* p = syms;
597 for (size_t i = 0; i < count; ++i, p += sym_size)
599 elfcpp::Sym<size, big_endian> sym(p);
600 elfcpp::Sym<size, big_endian>* psym = &sym;
602 unsigned int st_name = psym->get_st_name();
603 if (st_name >= sym_name_size)
605 relobj->error(_("bad global symbol name offset %u at %zu"),
610 const char* name = sym_names + st_name;
612 // A symbol defined in a section which we are not including must
613 // be treated as an undefined symbol.
614 unsigned char symbuf[sym_size];
615 elfcpp::Sym<size, big_endian> sym2(symbuf);
616 unsigned int st_shndx = psym->get_st_shndx();
617 if (st_shndx != elfcpp::SHN_UNDEF
618 && st_shndx < elfcpp::SHN_LORESERVE
619 && !relobj->is_section_included(st_shndx))
621 memcpy(symbuf, p, sym_size);
622 elfcpp::Sym_write<size, big_endian> sw(symbuf);
623 sw.put_st_shndx(elfcpp::SHN_UNDEF);
627 // In an object file, an '@' in the name separates the symbol
628 // name from the version name. If there are two '@' characters,
629 // this is the default version.
630 const char* ver = strchr(name, '@');
636 // The symbol name is of the form foo@VERSION or foo@@VERSION
637 namelen = ver - name;
645 else if (!version_script_.empty())
647 // The symbol name did not have a version, but
648 // the version script may assign a version anyway.
649 namelen = strlen(name);
651 const std::string& version =
652 version_script_.get_symbol_version(name);
653 if (!version.empty())
654 ver = version.c_str();
657 Sized_symbol<size>* res;
660 Stringpool::Key name_key;
661 name = this->namepool_.add(name, true, &name_key);
662 res = this->add_from_object(relobj, name, name_key, NULL, 0,
667 Stringpool::Key name_key;
668 name = this->namepool_.add_with_length(name, namelen, true,
670 Stringpool::Key ver_key;
671 ver = this->namepool_.add(ver, true, &ver_key);
673 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
677 (*sympointers)[i] = res;
681 // Add all the symbols in a dynamic object to the hash table.
683 template<int size, bool big_endian>
685 Symbol_table::add_from_dynobj(
686 Sized_dynobj<size, big_endian>* dynobj,
687 const unsigned char* syms,
689 const char* sym_names,
690 size_t sym_name_size,
691 const unsigned char* versym,
693 const std::vector<const char*>* version_map)
695 gold_assert(size == dynobj->target()->get_size());
696 gold_assert(size == parameters->get_size());
698 if (versym != NULL && versym_size / 2 < count)
700 dynobj->error(_("too few symbol versions"));
704 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
706 // We keep a list of all STT_OBJECT symbols, so that we can resolve
707 // weak aliases. This is necessary because if the dynamic object
708 // provides the same variable under two names, one of which is a
709 // weak definition, and the regular object refers to the weak
710 // definition, we have to put both the weak definition and the
711 // strong definition into the dynamic symbol table. Given a weak
712 // definition, the only way that we can find the corresponding
713 // strong definition, if any, is to search the symbol table.
714 std::vector<Sized_symbol<size>*> object_symbols;
716 const unsigned char* p = syms;
717 const unsigned char* vs = versym;
718 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
720 elfcpp::Sym<size, big_endian> sym(p);
722 // Ignore symbols with local binding.
723 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
726 unsigned int st_name = sym.get_st_name();
727 if (st_name >= sym_name_size)
729 dynobj->error(_("bad symbol name offset %u at %zu"),
734 const char* name = sym_names + st_name;
736 Sized_symbol<size>* res;
740 Stringpool::Key name_key;
741 name = this->namepool_.add(name, true, &name_key);
742 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
747 // Read the version information.
749 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
751 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
752 v &= elfcpp::VERSYM_VERSION;
754 // The Sun documentation says that V can be VER_NDX_LOCAL,
755 // or VER_NDX_GLOBAL, or a version index. The meaning of
756 // VER_NDX_LOCAL is defined as "Symbol has local scope."
757 // The old GNU linker will happily generate VER_NDX_LOCAL
758 // for an undefined symbol. I don't know what the Sun
759 // linker will generate.
761 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
762 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
764 // This symbol should not be visible outside the object.
768 // At this point we are definitely going to add this symbol.
769 Stringpool::Key name_key;
770 name = this->namepool_.add(name, true, &name_key);
772 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
773 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
775 // This symbol does not have a version.
776 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
781 if (v >= version_map->size())
783 dynobj->error(_("versym for symbol %zu out of range: %u"),
788 const char* version = (*version_map)[v];
791 dynobj->error(_("versym for symbol %zu has no name: %u"),
796 Stringpool::Key version_key;
797 version = this->namepool_.add(version, true, &version_key);
799 // If this is an absolute symbol, and the version name
800 // and symbol name are the same, then this is the
801 // version definition symbol. These symbols exist to
802 // support using -u to pull in particular versions. We
803 // do not want to record a version for them.
804 if (sym.get_st_shndx() == elfcpp::SHN_ABS
805 && name_key == version_key)
806 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
810 const bool def = (!hidden
811 && (sym.get_st_shndx()
812 != elfcpp::SHN_UNDEF));
813 res = this->add_from_object(dynobj, name, name_key, version,
814 version_key, def, sym, sym);
819 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
820 && sym.get_st_type() == elfcpp::STT_OBJECT)
821 object_symbols.push_back(res);
824 this->record_weak_aliases(&object_symbols);
827 // This is used to sort weak aliases. We sort them first by section
828 // index, then by offset, then by weak ahead of strong.
831 class Weak_alias_sorter
834 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
839 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
840 const Sized_symbol<size>* s2) const
842 if (s1->shndx() != s2->shndx())
843 return s1->shndx() < s2->shndx();
844 if (s1->value() != s2->value())
845 return s1->value() < s2->value();
846 if (s1->binding() != s2->binding())
848 if (s1->binding() == elfcpp::STB_WEAK)
850 if (s2->binding() == elfcpp::STB_WEAK)
853 return std::string(s1->name()) < std::string(s2->name());
856 // SYMBOLS is a list of object symbols from a dynamic object. Look
857 // for any weak aliases, and record them so that if we add the weak
858 // alias to the dynamic symbol table, we also add the corresponding
863 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
865 // Sort the vector by section index, then by offset, then by weak
867 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
869 // Walk through the vector. For each weak definition, record
871 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
876 if ((*p)->binding() != elfcpp::STB_WEAK)
879 // Build a circular list of weak aliases. Each symbol points to
880 // the next one in the circular list.
882 Sized_symbol<size>* from_sym = *p;
883 typename std::vector<Sized_symbol<size>*>::const_iterator q;
884 for (q = p + 1; q != symbols->end(); ++q)
886 if ((*q)->shndx() != from_sym->shndx()
887 || (*q)->value() != from_sym->value())
890 this->weak_aliases_[from_sym] = *q;
891 from_sym->set_has_alias();
897 this->weak_aliases_[from_sym] = *p;
898 from_sym->set_has_alias();
905 // Create and return a specially defined symbol. If ONLY_IF_REF is
906 // true, then only create the symbol if there is a reference to it.
907 // If this does not return NULL, it sets *POLDSYM to the existing
908 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
910 template<int size, bool big_endian>
912 Symbol_table::define_special_symbol(const Target* target, const char** pname,
913 const char** pversion, bool only_if_ref,
914 Sized_symbol<size>** poldsym
918 Sized_symbol<size>* sym;
919 bool add_to_table = false;
920 typename Symbol_table_type::iterator add_loc = this->table_.end();
924 oldsym = this->lookup(*pname, *pversion);
925 if (oldsym == NULL || !oldsym->is_undefined())
928 *pname = oldsym->name();
929 *pversion = oldsym->version();
933 // Canonicalize NAME and VERSION.
934 Stringpool::Key name_key;
935 *pname = this->namepool_.add(*pname, true, &name_key);
937 Stringpool::Key version_key = 0;
938 if (*pversion != NULL)
939 *pversion = this->namepool_.add(*pversion, true, &version_key);
941 Symbol* const snull = NULL;
942 std::pair<typename Symbol_table_type::iterator, bool> ins =
943 this->table_.insert(std::make_pair(std::make_pair(name_key,
949 // We already have a symbol table entry for NAME/VERSION.
950 oldsym = ins.first->second;
951 gold_assert(oldsym != NULL);
955 // We haven't seen this symbol before.
956 gold_assert(ins.first->second == NULL);
963 if (!target->has_make_symbol())
964 sym = new Sized_symbol<size>();
967 gold_assert(target->get_size() == size);
968 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
969 typedef Sized_target<size, big_endian> My_target;
970 const My_target* sized_target =
971 static_cast<const My_target*>(target);
972 sym = sized_target->make_symbol();
978 add_loc->second = sym;
980 gold_assert(oldsym != NULL);
982 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
988 // Define a symbol based on an Output_data.
991 Symbol_table::define_in_output_data(const Target* target, const char* name,
992 const char* version, Output_data* od,
993 uint64_t value, uint64_t symsize,
994 elfcpp::STT type, elfcpp::STB binding,
995 elfcpp::STV visibility,
996 unsigned char nonvis,
997 bool offset_is_from_end,
1000 if (parameters->get_size() == 32)
1002 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1003 return this->do_define_in_output_data<32>(target, name, version, od,
1004 value, symsize, type, binding,
1012 else if (parameters->get_size() == 64)
1014 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1015 return this->do_define_in_output_data<64>(target, name, version, od,
1016 value, symsize, type, binding,
1028 // Define a symbol in an Output_data, sized version.
1032 Symbol_table::do_define_in_output_data(
1033 const Target* target,
1035 const char* version,
1037 typename elfcpp::Elf_types<size>::Elf_Addr value,
1038 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1040 elfcpp::STB binding,
1041 elfcpp::STV visibility,
1042 unsigned char nonvis,
1043 bool offset_is_from_end,
1046 Sized_symbol<size>* sym;
1047 Sized_symbol<size>* oldsym;
1049 if (parameters->is_big_endian())
1051 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1052 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1053 target, &name, &version, only_if_ref, &oldsym
1054 SELECT_SIZE_ENDIAN(size, true));
1061 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1062 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1063 target, &name, &version, only_if_ref, &oldsym
1064 SELECT_SIZE_ENDIAN(size, false));
1073 gold_assert(version == NULL || oldsym != NULL);
1074 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1075 offset_is_from_end);
1080 if (Symbol_table::should_override_with_special(oldsym))
1081 this->override_with_special(oldsym, sym);
1086 // Define a symbol based on an Output_segment.
1089 Symbol_table::define_in_output_segment(const Target* target, const char* name,
1090 const char* version, Output_segment* os,
1091 uint64_t value, uint64_t symsize,
1092 elfcpp::STT type, elfcpp::STB binding,
1093 elfcpp::STV visibility,
1094 unsigned char nonvis,
1095 Symbol::Segment_offset_base offset_base,
1098 if (parameters->get_size() == 32)
1100 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1101 return this->do_define_in_output_segment<32>(target, name, version, os,
1102 value, symsize, type,
1103 binding, visibility, nonvis,
1104 offset_base, only_if_ref);
1109 else if (parameters->get_size() == 64)
1111 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1112 return this->do_define_in_output_segment<64>(target, name, version, os,
1113 value, symsize, type,
1114 binding, visibility, nonvis,
1115 offset_base, only_if_ref);
1124 // Define a symbol in an Output_segment, sized version.
1128 Symbol_table::do_define_in_output_segment(
1129 const Target* target,
1131 const char* version,
1133 typename elfcpp::Elf_types<size>::Elf_Addr value,
1134 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1136 elfcpp::STB binding,
1137 elfcpp::STV visibility,
1138 unsigned char nonvis,
1139 Symbol::Segment_offset_base offset_base,
1142 Sized_symbol<size>* sym;
1143 Sized_symbol<size>* oldsym;
1145 if (parameters->is_big_endian())
1147 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1148 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1149 target, &name, &version, only_if_ref, &oldsym
1150 SELECT_SIZE_ENDIAN(size, true));
1157 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1158 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1159 target, &name, &version, only_if_ref, &oldsym
1160 SELECT_SIZE_ENDIAN(size, false));
1169 gold_assert(version == NULL || oldsym != NULL);
1170 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1176 if (Symbol_table::should_override_with_special(oldsym))
1177 this->override_with_special(oldsym, sym);
1182 // Define a special symbol with a constant value. It is a multiple
1183 // definition error if this symbol is already defined.
1186 Symbol_table::define_as_constant(const Target* target, const char* name,
1187 const char* version, uint64_t value,
1188 uint64_t symsize, elfcpp::STT type,
1189 elfcpp::STB binding, elfcpp::STV visibility,
1190 unsigned char nonvis, bool only_if_ref)
1192 if (parameters->get_size() == 32)
1194 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1195 return this->do_define_as_constant<32>(target, name, version, value,
1196 symsize, type, binding,
1197 visibility, nonvis, only_if_ref);
1202 else if (parameters->get_size() == 64)
1204 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1205 return this->do_define_as_constant<64>(target, name, version, value,
1206 symsize, type, binding,
1207 visibility, nonvis, only_if_ref);
1216 // Define a symbol as a constant, sized version.
1220 Symbol_table::do_define_as_constant(
1221 const Target* target,
1223 const char* version,
1224 typename elfcpp::Elf_types<size>::Elf_Addr value,
1225 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1227 elfcpp::STB binding,
1228 elfcpp::STV visibility,
1229 unsigned char nonvis,
1232 Sized_symbol<size>* sym;
1233 Sized_symbol<size>* oldsym;
1235 if (parameters->is_big_endian())
1237 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1238 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1239 target, &name, &version, only_if_ref, &oldsym
1240 SELECT_SIZE_ENDIAN(size, true));
1247 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1248 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1249 target, &name, &version, only_if_ref, &oldsym
1250 SELECT_SIZE_ENDIAN(size, false));
1259 gold_assert(version == NULL || version == name || oldsym != NULL);
1260 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1265 if (Symbol_table::should_override_with_special(oldsym))
1266 this->override_with_special(oldsym, sym);
1271 // Define a set of symbols in output sections.
1274 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1275 int count, const Define_symbol_in_section* p)
1277 for (int i = 0; i < count; ++i, ++p)
1279 Output_section* os = layout->find_output_section(p->output_section);
1281 this->define_in_output_data(target, p->name, NULL, os, p->value,
1282 p->size, p->type, p->binding,
1283 p->visibility, p->nonvis,
1284 p->offset_is_from_end, p->only_if_ref);
1286 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1287 p->binding, p->visibility, p->nonvis,
1292 // Define a set of symbols in output segments.
1295 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1296 int count, const Define_symbol_in_segment* p)
1298 for (int i = 0; i < count; ++i, ++p)
1300 Output_segment* os = layout->find_output_segment(p->segment_type,
1301 p->segment_flags_set,
1302 p->segment_flags_clear);
1304 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1305 p->size, p->type, p->binding,
1306 p->visibility, p->nonvis,
1307 p->offset_base, p->only_if_ref);
1309 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1310 p->binding, p->visibility, p->nonvis,
1315 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1316 // symbol should be defined--typically a .dyn.bss section. VALUE is
1317 // the offset within POSD.
1321 Symbol_table::define_with_copy_reloc(
1322 const Target* target,
1323 Sized_symbol<size>* csym,
1325 typename elfcpp::Elf_types<size>::Elf_Addr value)
1327 gold_assert(csym->is_from_dynobj());
1328 gold_assert(!csym->is_copied_from_dynobj());
1329 Object* object = csym->object();
1330 gold_assert(object->is_dynamic());
1331 Dynobj* dynobj = static_cast<Dynobj*>(object);
1333 // Our copied variable has to override any variable in a shared
1335 elfcpp::STB binding = csym->binding();
1336 if (binding == elfcpp::STB_WEAK)
1337 binding = elfcpp::STB_GLOBAL;
1339 this->define_in_output_data(target, csym->name(), csym->version(),
1340 posd, value, csym->symsize(),
1341 csym->type(), binding,
1342 csym->visibility(), csym->nonvis(),
1345 csym->set_is_copied_from_dynobj();
1346 csym->set_needs_dynsym_entry();
1348 this->copied_symbol_dynobjs_[csym] = dynobj;
1350 // We have now defined all aliases, but we have not entered them all
1351 // in the copied_symbol_dynobjs_ map.
1352 if (csym->has_alias())
1357 sym = this->weak_aliases_[sym];
1360 gold_assert(sym->output_data() == posd);
1362 sym->set_is_copied_from_dynobj();
1363 this->copied_symbol_dynobjs_[sym] = dynobj;
1368 // SYM is defined using a COPY reloc. Return the dynamic object where
1369 // the original definition was found.
1372 Symbol_table::get_copy_source(const Symbol* sym) const
1374 gold_assert(sym->is_copied_from_dynobj());
1375 Copied_symbol_dynobjs::const_iterator p =
1376 this->copied_symbol_dynobjs_.find(sym);
1377 gold_assert(p != this->copied_symbol_dynobjs_.end());
1381 // Set the dynamic symbol indexes. INDEX is the index of the first
1382 // global dynamic symbol. Pointers to the symbols are stored into the
1383 // vector SYMS. The names are added to DYNPOOL. This returns an
1384 // updated dynamic symbol index.
1387 Symbol_table::set_dynsym_indexes(const Target* target,
1389 std::vector<Symbol*>* syms,
1390 Stringpool* dynpool,
1393 for (Symbol_table_type::iterator p = this->table_.begin();
1394 p != this->table_.end();
1397 Symbol* sym = p->second;
1399 // Note that SYM may already have a dynamic symbol index, since
1400 // some symbols appear more than once in the symbol table, with
1401 // and without a version.
1403 if (!sym->should_add_dynsym_entry())
1404 sym->set_dynsym_index(-1U);
1405 else if (!sym->has_dynsym_index())
1407 sym->set_dynsym_index(index);
1409 syms->push_back(sym);
1410 dynpool->add(sym->name(), false, NULL);
1412 // Record any version information.
1413 if (sym->version() != NULL)
1414 versions->record_version(this, dynpool, sym);
1418 // Finish up the versions. In some cases this may add new dynamic
1420 index = versions->finalize(target, this, index, syms);
1425 // Set the final values for all the symbols. The index of the first
1426 // global symbol in the output file is INDEX. Record the file offset
1427 // OFF. Add their names to POOL. Return the new file offset.
1430 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1431 size_t dyn_global_index, size_t dyncount,
1436 gold_assert(index != 0);
1437 this->first_global_index_ = index;
1439 this->dynamic_offset_ = dynoff;
1440 this->first_dynamic_global_index_ = dyn_global_index;
1441 this->dynamic_count_ = dyncount;
1443 if (parameters->get_size() == 32)
1445 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1446 ret = this->sized_finalize<32>(index, off, pool);
1451 else if (parameters->get_size() == 64)
1453 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1454 ret = this->sized_finalize<64>(index, off, pool);
1462 // Now that we have the final symbol table, we can reliably note
1463 // which symbols should get warnings.
1464 this->warnings_.note_warnings(this);
1469 // Set the final value for all the symbols. This is called after
1470 // Layout::finalize, so all the output sections have their final
1475 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1477 off = align_address(off, size >> 3);
1478 this->offset_ = off;
1480 size_t orig_index = index;
1482 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1483 for (Symbol_table_type::iterator p = this->table_.begin();
1484 p != this->table_.end();
1487 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1489 // FIXME: Here we need to decide which symbols should go into
1490 // the output file, based on --strip.
1492 // The default version of a symbol may appear twice in the
1493 // symbol table. We only need to finalize it once.
1494 if (sym->has_symtab_index())
1499 gold_assert(!sym->has_symtab_index());
1500 sym->set_symtab_index(-1U);
1501 gold_assert(sym->dynsym_index() == -1U);
1505 typename Sized_symbol<size>::Value_type value;
1507 switch (sym->source())
1509 case Symbol::FROM_OBJECT:
1511 unsigned int shndx = sym->shndx();
1513 // FIXME: We need some target specific support here.
1514 if (shndx >= elfcpp::SHN_LORESERVE
1515 && shndx != elfcpp::SHN_ABS)
1517 gold_error(_("%s: unsupported symbol section 0x%x"),
1518 sym->demangled_name().c_str(), shndx);
1519 shndx = elfcpp::SHN_UNDEF;
1522 Object* symobj = sym->object();
1523 if (symobj->is_dynamic())
1526 shndx = elfcpp::SHN_UNDEF;
1528 else if (shndx == elfcpp::SHN_UNDEF)
1530 else if (shndx == elfcpp::SHN_ABS)
1531 value = sym->value();
1534 Relobj* relobj = static_cast<Relobj*>(symobj);
1535 section_offset_type secoff;
1536 Output_section* os = relobj->output_section(shndx, &secoff);
1540 sym->set_symtab_index(-1U);
1541 gold_assert(sym->dynsym_index() == -1U);
1545 if (sym->type() == elfcpp::STT_TLS)
1546 value = sym->value() + os->tls_offset() + secoff;
1548 value = sym->value() + os->address() + secoff;
1553 case Symbol::IN_OUTPUT_DATA:
1555 Output_data* od = sym->output_data();
1556 value = sym->value() + od->address();
1557 if (sym->offset_is_from_end())
1558 value += od->data_size();
1562 case Symbol::IN_OUTPUT_SEGMENT:
1564 Output_segment* os = sym->output_segment();
1565 value = sym->value() + os->vaddr();
1566 switch (sym->offset_base())
1568 case Symbol::SEGMENT_START:
1570 case Symbol::SEGMENT_END:
1571 value += os->memsz();
1573 case Symbol::SEGMENT_BSS:
1574 value += os->filesz();
1582 case Symbol::CONSTANT:
1583 value = sym->value();
1590 sym->set_value(value);
1592 if (parameters->strip_all())
1593 sym->set_symtab_index(-1U);
1596 sym->set_symtab_index(index);
1597 pool->add(sym->name(), false, NULL);
1603 this->output_count_ = index - orig_index;
1608 // Write out the global symbols.
1611 Symbol_table::write_globals(const Input_objects* input_objects,
1612 const Stringpool* sympool,
1613 const Stringpool* dynpool, Output_file* of) const
1615 if (parameters->get_size() == 32)
1617 if (parameters->is_big_endian())
1619 #ifdef HAVE_TARGET_32_BIG
1620 this->sized_write_globals<32, true>(input_objects, sympool,
1628 #ifdef HAVE_TARGET_32_LITTLE
1629 this->sized_write_globals<32, false>(input_objects, sympool,
1636 else if (parameters->get_size() == 64)
1638 if (parameters->is_big_endian())
1640 #ifdef HAVE_TARGET_64_BIG
1641 this->sized_write_globals<64, true>(input_objects, sympool,
1649 #ifdef HAVE_TARGET_64_LITTLE
1650 this->sized_write_globals<64, false>(input_objects, sympool,
1661 // Write out the global symbols.
1663 template<int size, bool big_endian>
1665 Symbol_table::sized_write_globals(const Input_objects* input_objects,
1666 const Stringpool* sympool,
1667 const Stringpool* dynpool,
1668 Output_file* of) const
1670 const Target* const target = input_objects->target();
1672 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1673 unsigned int index = this->first_global_index_;
1674 const off_t oview_size = this->output_count_ * sym_size;
1675 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1677 unsigned int dynamic_count = this->dynamic_count_;
1678 off_t dynamic_size = dynamic_count * sym_size;
1679 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1680 unsigned char* dynamic_view;
1681 if (this->dynamic_offset_ == 0)
1682 dynamic_view = NULL;
1684 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1686 unsigned char* ps = psyms;
1687 for (Symbol_table_type::const_iterator p = this->table_.begin();
1688 p != this->table_.end();
1691 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1693 // Possibly warn about unresolved symbols in shared libraries.
1694 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
1696 unsigned int sym_index = sym->symtab_index();
1697 unsigned int dynsym_index;
1698 if (dynamic_view == NULL)
1701 dynsym_index = sym->dynsym_index();
1703 if (sym_index == -1U && dynsym_index == -1U)
1705 // This symbol is not included in the output file.
1709 if (sym_index == index)
1711 else if (sym_index != -1U)
1713 // We have already seen this symbol, because it has a
1715 gold_assert(sym_index < index);
1716 if (dynsym_index == -1U)
1722 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1723 switch (sym->source())
1725 case Symbol::FROM_OBJECT:
1727 unsigned int in_shndx = sym->shndx();
1729 // FIXME: We need some target specific support here.
1730 if (in_shndx >= elfcpp::SHN_LORESERVE
1731 && in_shndx != elfcpp::SHN_ABS)
1733 gold_error(_("%s: unsupported symbol section 0x%x"),
1734 sym->demangled_name().c_str(), in_shndx);
1739 Object* symobj = sym->object();
1740 if (symobj->is_dynamic())
1742 if (sym->needs_dynsym_value())
1743 value = target->dynsym_value(sym);
1744 shndx = elfcpp::SHN_UNDEF;
1746 else if (in_shndx == elfcpp::SHN_UNDEF
1747 || in_shndx == elfcpp::SHN_ABS)
1751 Relobj* relobj = static_cast<Relobj*>(symobj);
1752 section_offset_type secoff;
1753 Output_section* os = relobj->output_section(in_shndx,
1755 gold_assert(os != NULL);
1756 shndx = os->out_shndx();
1762 case Symbol::IN_OUTPUT_DATA:
1763 shndx = sym->output_data()->out_shndx();
1766 case Symbol::IN_OUTPUT_SEGMENT:
1767 shndx = elfcpp::SHN_ABS;
1770 case Symbol::CONSTANT:
1771 shndx = elfcpp::SHN_ABS;
1778 if (sym_index != -1U)
1780 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1781 sym, sym->value(), shndx, sympool, ps
1782 SELECT_SIZE_ENDIAN(size, big_endian));
1786 if (dynsym_index != -1U)
1788 dynsym_index -= first_dynamic_global_index;
1789 gold_assert(dynsym_index < dynamic_count);
1790 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1791 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1792 sym, value, shndx, dynpool, pd
1793 SELECT_SIZE_ENDIAN(size, big_endian));
1797 gold_assert(ps - psyms == oview_size);
1799 of->write_output_view(this->offset_, oview_size, psyms);
1800 if (dynamic_view != NULL)
1801 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1804 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1805 // strtab holding the name.
1807 template<int size, bool big_endian>
1809 Symbol_table::sized_write_symbol(
1810 Sized_symbol<size>* sym,
1811 typename elfcpp::Elf_types<size>::Elf_Addr value,
1813 const Stringpool* pool,
1815 ACCEPT_SIZE_ENDIAN) const
1817 elfcpp::Sym_write<size, big_endian> osym(p);
1818 osym.put_st_name(pool->get_offset(sym->name()));
1819 osym.put_st_value(value);
1820 osym.put_st_size(sym->symsize());
1821 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1822 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1823 osym.put_st_shndx(shndx);
1826 // Check for unresolved symbols in shared libraries. This is
1827 // controlled by the --allow-shlib-undefined option.
1829 // We only warn about libraries for which we have seen all the
1830 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1831 // which were not seen in this link. If we didn't see a DT_NEEDED
1832 // entry, we aren't going to be able to reliably report whether the
1833 // symbol is undefined.
1835 // We also don't warn about libraries found in the system library
1836 // directory (the directory were we find libc.so); we assume that
1837 // those libraries are OK. This heuristic avoids problems in
1838 // GNU/Linux, in which -ldl can have undefined references satisfied by
1842 Symbol_table::warn_about_undefined_dynobj_symbol(
1843 const Input_objects* input_objects,
1846 if (sym->source() == Symbol::FROM_OBJECT
1847 && sym->object()->is_dynamic()
1848 && sym->shndx() == elfcpp::SHN_UNDEF
1849 && sym->binding() != elfcpp::STB_WEAK
1850 && !parameters->allow_shlib_undefined()
1851 && !input_objects->target()->is_defined_by_abi(sym)
1852 && !input_objects->found_in_system_library_directory(sym->object()))
1854 // A very ugly cast.
1855 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
1856 if (!dynobj->has_unknown_needed_entries())
1857 gold_error(_("%s: undefined reference to '%s'"),
1858 sym->object()->name().c_str(),
1859 sym->demangled_name().c_str());
1863 // Write out a section symbol. Return the update offset.
1866 Symbol_table::write_section_symbol(const Output_section *os,
1870 if (parameters->get_size() == 32)
1872 if (parameters->is_big_endian())
1874 #ifdef HAVE_TARGET_32_BIG
1875 this->sized_write_section_symbol<32, true>(os, of, offset);
1882 #ifdef HAVE_TARGET_32_LITTLE
1883 this->sized_write_section_symbol<32, false>(os, of, offset);
1889 else if (parameters->get_size() == 64)
1891 if (parameters->is_big_endian())
1893 #ifdef HAVE_TARGET_64_BIG
1894 this->sized_write_section_symbol<64, true>(os, of, offset);
1901 #ifdef HAVE_TARGET_64_LITTLE
1902 this->sized_write_section_symbol<64, false>(os, of, offset);
1912 // Write out a section symbol, specialized for size and endianness.
1914 template<int size, bool big_endian>
1916 Symbol_table::sized_write_section_symbol(const Output_section* os,
1920 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1922 unsigned char* pov = of->get_output_view(offset, sym_size);
1924 elfcpp::Sym_write<size, big_endian> osym(pov);
1925 osym.put_st_name(0);
1926 osym.put_st_value(os->address());
1927 osym.put_st_size(0);
1928 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1929 elfcpp::STT_SECTION));
1930 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1931 osym.put_st_shndx(os->out_shndx());
1933 of->write_output_view(offset, sym_size, pov);
1936 // Print statistical information to stderr. This is used for --stats.
1939 Symbol_table::print_stats() const
1941 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
1942 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
1943 program_name, this->table_.size(), this->table_.bucket_count());
1945 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
1946 program_name, this->table_.size());
1948 this->namepool_.print_stats("symbol table stringpool");
1951 // We check for ODR violations by looking for symbols with the same
1952 // name for which the debugging information reports that they were
1953 // defined in different source locations. When comparing the source
1954 // location, we consider instances with the same base filename and
1955 // line number to be the same. This is because different object
1956 // files/shared libraries can include the same header file using
1957 // different paths, and we don't want to report an ODR violation in
1960 // This struct is used to compare line information, as returned by
1961 // Dwarf_line_info::one_addr2line. It implements a < comparison
1962 // operator used with std::set.
1964 struct Odr_violation_compare
1967 operator()(const std::string& s1, const std::string& s2) const
1969 std::string::size_type pos1 = s1.rfind('/');
1970 std::string::size_type pos2 = s2.rfind('/');
1971 if (pos1 == std::string::npos
1972 || pos2 == std::string::npos)
1974 return s1.compare(pos1, std::string::npos,
1975 s2, pos2, std::string::npos) < 0;
1979 // Check candidate_odr_violations_ to find symbols with the same name
1980 // but apparently different definitions (different source-file/line-no).
1983 Symbol_table::detect_odr_violations(const Task* task,
1984 const char* output_file_name) const
1986 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
1987 it != candidate_odr_violations_.end();
1990 const char* symbol_name = it->first;
1991 // We use a sorted set so the output is deterministic.
1992 std::set<std::string, Odr_violation_compare> line_nums;
1994 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
1995 locs = it->second.begin();
1996 locs != it->second.end();
1999 // We need to lock the object in order to read it. This
2000 // means that we have to run in a singleton Task. If we
2001 // want to run this in a general Task for better
2002 // performance, we will need one Task for object, plus
2003 // appropriate locking to ensure that we don't conflict with
2004 // other uses of the object.
2005 Task_lock_obj<Object> tl(task, locs->object);
2006 std::string lineno = Dwarf_line_info::one_addr2line(
2007 locs->object, locs->shndx, locs->offset);
2008 if (!lineno.empty())
2009 line_nums.insert(lineno);
2012 if (line_nums.size() > 1)
2014 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2015 "places (possible ODR violation):"),
2016 output_file_name, demangle(symbol_name).c_str());
2017 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2018 it2 != line_nums.end();
2020 fprintf(stderr, " %s\n", it2->c_str());
2025 // Warnings functions.
2027 // Add a new warning.
2030 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2031 const std::string& warning)
2033 name = symtab->canonicalize_name(name);
2034 this->warnings_[name].set(obj, warning);
2037 // Look through the warnings and mark the symbols for which we should
2038 // warn. This is called during Layout::finalize when we know the
2039 // sources for all the symbols.
2042 Warnings::note_warnings(Symbol_table* symtab)
2044 for (Warning_table::iterator p = this->warnings_.begin();
2045 p != this->warnings_.end();
2048 Symbol* sym = symtab->lookup(p->first, NULL);
2050 && sym->source() == Symbol::FROM_OBJECT
2051 && sym->object() == p->second.object)
2052 sym->set_has_warning();
2056 // Issue a warning. This is called when we see a relocation against a
2057 // symbol for which has a warning.
2059 template<int size, bool big_endian>
2061 Warnings::issue_warning(const Symbol* sym,
2062 const Relocate_info<size, big_endian>* relinfo,
2063 size_t relnum, off_t reloffset) const
2065 gold_assert(sym->has_warning());
2066 Warning_table::const_iterator p = this->warnings_.find(sym->name());
2067 gold_assert(p != this->warnings_.end());
2068 gold_warning_at_location(relinfo, relnum, reloffset,
2069 "%s", p->second.text.c_str());
2072 // Instantiate the templates we need. We could use the configure
2073 // script to restrict this to only the ones needed for implemented
2076 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2079 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2082 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2085 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2088 #ifdef HAVE_TARGET_32_LITTLE
2091 Symbol_table::add_from_relobj<32, false>(
2092 Sized_relobj<32, false>* relobj,
2093 const unsigned char* syms,
2095 const char* sym_names,
2096 size_t sym_name_size,
2097 Sized_relobj<32, true>::Symbols* sympointers);
2100 #ifdef HAVE_TARGET_32_BIG
2103 Symbol_table::add_from_relobj<32, true>(
2104 Sized_relobj<32, true>* relobj,
2105 const unsigned char* syms,
2107 const char* sym_names,
2108 size_t sym_name_size,
2109 Sized_relobj<32, false>::Symbols* sympointers);
2112 #ifdef HAVE_TARGET_64_LITTLE
2115 Symbol_table::add_from_relobj<64, false>(
2116 Sized_relobj<64, false>* relobj,
2117 const unsigned char* syms,
2119 const char* sym_names,
2120 size_t sym_name_size,
2121 Sized_relobj<64, true>::Symbols* sympointers);
2124 #ifdef HAVE_TARGET_64_BIG
2127 Symbol_table::add_from_relobj<64, true>(
2128 Sized_relobj<64, true>* relobj,
2129 const unsigned char* syms,
2131 const char* sym_names,
2132 size_t sym_name_size,
2133 Sized_relobj<64, false>::Symbols* sympointers);
2136 #ifdef HAVE_TARGET_32_LITTLE
2139 Symbol_table::add_from_dynobj<32, false>(
2140 Sized_dynobj<32, false>* dynobj,
2141 const unsigned char* syms,
2143 const char* sym_names,
2144 size_t sym_name_size,
2145 const unsigned char* versym,
2147 const std::vector<const char*>* version_map);
2150 #ifdef HAVE_TARGET_32_BIG
2153 Symbol_table::add_from_dynobj<32, true>(
2154 Sized_dynobj<32, true>* dynobj,
2155 const unsigned char* syms,
2157 const char* sym_names,
2158 size_t sym_name_size,
2159 const unsigned char* versym,
2161 const std::vector<const char*>* version_map);
2164 #ifdef HAVE_TARGET_64_LITTLE
2167 Symbol_table::add_from_dynobj<64, false>(
2168 Sized_dynobj<64, false>* dynobj,
2169 const unsigned char* syms,
2171 const char* sym_names,
2172 size_t sym_name_size,
2173 const unsigned char* versym,
2175 const std::vector<const char*>* version_map);
2178 #ifdef HAVE_TARGET_64_BIG
2181 Symbol_table::add_from_dynobj<64, true>(
2182 Sized_dynobj<64, true>* dynobj,
2183 const unsigned char* syms,
2185 const char* sym_names,
2186 size_t sym_name_size,
2187 const unsigned char* versym,
2189 const std::vector<const char*>* version_map);
2192 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2195 Symbol_table::define_with_copy_reloc<32>(
2196 const Target* target,
2197 Sized_symbol<32>* sym,
2199 elfcpp::Elf_types<32>::Elf_Addr value);
2202 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2205 Symbol_table::define_with_copy_reloc<64>(
2206 const Target* target,
2207 Sized_symbol<64>* sym,
2209 elfcpp::Elf_types<64>::Elf_Addr value);
2212 #ifdef HAVE_TARGET_32_LITTLE
2215 Warnings::issue_warning<32, false>(const Symbol* sym,
2216 const Relocate_info<32, false>* relinfo,
2217 size_t relnum, off_t reloffset) const;
2220 #ifdef HAVE_TARGET_32_BIG
2223 Warnings::issue_warning<32, true>(const Symbol* sym,
2224 const Relocate_info<32, true>* relinfo,
2225 size_t relnum, off_t reloffset) const;
2228 #ifdef HAVE_TARGET_64_LITTLE
2231 Warnings::issue_warning<64, false>(const Symbol* sym,
2232 const Relocate_info<64, false>* relinfo,
2233 size_t relnum, off_t reloffset) const;
2236 #ifdef HAVE_TARGET_64_BIG
2239 Warnings::issue_warning<64, true>(const Symbol* sym,
2240 const Relocate_info<64, true>* relinfo,
2241 size_t relnum, off_t reloffset) const;
2244 } // End namespace gold.