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;
73 this->is_forced_local_ = false;
76 // Return the demangled version of the symbol's name, but only
77 // if the --demangle flag was set.
80 demangle(const char* name)
82 if (!parameters->demangle())
85 // cplus_demangle allocates memory for the result it returns,
86 // and returns NULL if the name is already demangled.
87 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
88 if (demangled_name == NULL)
91 std::string retval(demangled_name);
97 Symbol::demangled_name() const
99 return demangle(this->name());
102 // Initialize the fields in the base class Symbol for SYM in OBJECT.
104 template<int size, bool big_endian>
106 Symbol::init_base(const char* name, const char* version, Object* object,
107 const elfcpp::Sym<size, big_endian>& sym)
109 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
110 sym.get_st_visibility(), sym.get_st_nonvis());
111 this->u_.from_object.object = object;
112 // FIXME: Handle SHN_XINDEX.
113 this->u_.from_object.shndx = sym.get_st_shndx();
114 this->source_ = FROM_OBJECT;
115 this->in_reg_ = !object->is_dynamic();
116 this->in_dyn_ = object->is_dynamic();
119 // Initialize the fields in the base class Symbol for a symbol defined
120 // in an Output_data.
123 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
124 elfcpp::STB binding, elfcpp::STV visibility,
125 unsigned char nonvis, bool offset_is_from_end)
127 this->init_fields(name, NULL, type, binding, visibility, nonvis);
128 this->u_.in_output_data.output_data = od;
129 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
130 this->source_ = IN_OUTPUT_DATA;
131 this->in_reg_ = true;
134 // Initialize the fields in the base class Symbol for a symbol defined
135 // in an Output_segment.
138 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
139 elfcpp::STB binding, elfcpp::STV visibility,
140 unsigned char nonvis, Segment_offset_base offset_base)
142 this->init_fields(name, NULL, type, binding, visibility, nonvis);
143 this->u_.in_output_segment.output_segment = os;
144 this->u_.in_output_segment.offset_base = offset_base;
145 this->source_ = IN_OUTPUT_SEGMENT;
146 this->in_reg_ = true;
149 // Initialize the fields in the base class Symbol for a symbol defined
153 Symbol::init_base(const char* name, elfcpp::STT type,
154 elfcpp::STB binding, elfcpp::STV visibility,
155 unsigned char nonvis)
157 this->init_fields(name, NULL, type, binding, visibility, nonvis);
158 this->source_ = CONSTANT;
159 this->in_reg_ = true;
162 // Allocate a common symbol in the base.
165 Symbol::allocate_base_common(Output_data* od)
167 gold_assert(this->is_common());
168 this->source_ = IN_OUTPUT_DATA;
169 this->u_.in_output_data.output_data = od;
170 this->u_.in_output_data.offset_is_from_end = false;
173 // Initialize the fields in Sized_symbol for SYM in OBJECT.
176 template<bool big_endian>
178 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
179 const elfcpp::Sym<size, big_endian>& sym)
181 this->init_base(name, version, object, sym);
182 this->value_ = sym.get_st_value();
183 this->symsize_ = sym.get_st_size();
186 // Initialize the fields in Sized_symbol for a symbol defined in an
191 Sized_symbol<size>::init(const char* name, Output_data* od,
192 Value_type value, Size_type symsize,
193 elfcpp::STT type, elfcpp::STB binding,
194 elfcpp::STV visibility, unsigned char nonvis,
195 bool offset_is_from_end)
197 this->init_base(name, od, type, binding, visibility, nonvis,
199 this->value_ = value;
200 this->symsize_ = symsize;
203 // Initialize the fields in Sized_symbol for a symbol defined in an
208 Sized_symbol<size>::init(const char* name, Output_segment* os,
209 Value_type value, Size_type symsize,
210 elfcpp::STT type, elfcpp::STB binding,
211 elfcpp::STV visibility, unsigned char nonvis,
212 Segment_offset_base offset_base)
214 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
215 this->value_ = value;
216 this->symsize_ = symsize;
219 // Initialize the fields in Sized_symbol for a symbol defined as a
224 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
225 elfcpp::STT type, elfcpp::STB binding,
226 elfcpp::STV visibility, unsigned char nonvis)
228 this->init_base(name, type, binding, visibility, nonvis);
229 this->value_ = value;
230 this->symsize_ = symsize;
233 // Allocate a common symbol.
237 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
239 this->allocate_base_common(od);
240 this->value_ = value;
243 // Return true if this symbol should be added to the dynamic symbol
247 Symbol::should_add_dynsym_entry() const
249 // If the symbol is used by a dynamic relocation, we need to add it.
250 if (this->needs_dynsym_entry())
253 // If the symbol was forced local in a version script, do not add it.
254 if (this->is_forced_local())
257 // If exporting all symbols or building a shared library,
258 // and the symbol is defined in a regular object and is
259 // externally visible, we need to add it.
260 if ((parameters->export_dynamic() || parameters->output_is_shared())
261 && !this->is_from_dynobj()
262 && this->is_externally_visible())
268 // Return true if the final value of this symbol is known at link
272 Symbol::final_value_is_known() const
274 // If we are not generating an executable, then no final values are
275 // known, since they will change at runtime.
276 if (!parameters->output_is_executable())
279 // If the symbol is not from an object file, then it is defined, and
281 if (this->source_ != FROM_OBJECT)
284 // If the symbol is from a dynamic object, then the final value is
286 if (this->object()->is_dynamic())
289 // If the symbol is not undefined (it is defined or common), then
290 // the final value is known.
291 if (!this->is_undefined())
294 // If the symbol is undefined, then whether the final value is known
295 // depends on whether we are doing a static link. If we are doing a
296 // dynamic link, then the final value could be filled in at runtime.
297 // This could reasonably be the case for a weak undefined symbol.
298 return parameters->doing_static_link();
301 // Return whether the symbol has an absolute value.
304 Symbol::value_is_absolute() const
306 switch (this->source_)
309 return this->u_.from_object.shndx == elfcpp::SHN_ABS;
311 case IN_OUTPUT_SEGMENT:
320 // Class Symbol_table.
322 Symbol_table::Symbol_table(unsigned int count,
323 const Version_script_info& version_script)
324 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
325 forwarders_(), commons_(), forced_locals_(), warnings_(),
326 version_script_(version_script)
328 namepool_.reserve(count);
331 Symbol_table::~Symbol_table()
335 // The hash function. The key values are Stringpool keys.
338 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
340 return key.first ^ key.second;
343 // The symbol table key equality function. This is called with
347 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
348 const Symbol_table_key& k2) const
350 return k1.first == k2.first && k1.second == k2.second;
353 // Make TO a symbol which forwards to FROM.
356 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
358 gold_assert(from != to);
359 gold_assert(!from->is_forwarder() && !to->is_forwarder());
360 this->forwarders_[from] = to;
361 from->set_forwarder();
364 // Resolve the forwards from FROM, returning the real symbol.
367 Symbol_table::resolve_forwards(const Symbol* from) const
369 gold_assert(from->is_forwarder());
370 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
371 this->forwarders_.find(from);
372 gold_assert(p != this->forwarders_.end());
376 // Look up a symbol by name.
379 Symbol_table::lookup(const char* name, const char* version) const
381 Stringpool::Key name_key;
382 name = this->namepool_.find(name, &name_key);
386 Stringpool::Key version_key = 0;
389 version = this->namepool_.find(version, &version_key);
394 Symbol_table_key key(name_key, version_key);
395 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
396 if (p == this->table_.end())
401 // Resolve a Symbol with another Symbol. This is only used in the
402 // unusual case where there are references to both an unversioned
403 // symbol and a symbol with a version, and we then discover that that
404 // version is the default version. Because this is unusual, we do
405 // this the slow way, by converting back to an ELF symbol.
407 template<int size, bool big_endian>
409 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
410 const char* version ACCEPT_SIZE_ENDIAN)
412 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
413 elfcpp::Sym_write<size, big_endian> esym(buf);
414 // We don't bother to set the st_name field.
415 esym.put_st_value(from->value());
416 esym.put_st_size(from->symsize());
417 esym.put_st_info(from->binding(), from->type());
418 esym.put_st_other(from->visibility(), from->nonvis());
419 esym.put_st_shndx(from->shndx());
420 this->resolve(to, esym.sym(), esym.sym(), from->object(), version);
427 // Record that a symbol is forced to be local by a version script.
430 Symbol_table::force_local(Symbol* sym)
432 if (!sym->is_defined() && !sym->is_common())
434 if (sym->is_forced_local())
436 // We already got this one.
439 sym->set_is_forced_local();
440 this->forced_locals_.push_back(sym);
443 // Add one symbol from OBJECT to the symbol table. NAME is symbol
444 // name and VERSION is the version; both are canonicalized. DEF is
445 // whether this is the default version.
447 // If DEF is true, then this is the definition of a default version of
448 // a symbol. That means that any lookup of NAME/NULL and any lookup
449 // of NAME/VERSION should always return the same symbol. This is
450 // obvious for references, but in particular we want to do this for
451 // definitions: overriding NAME/NULL should also override
452 // NAME/VERSION. If we don't do that, it would be very hard to
453 // override functions in a shared library which uses versioning.
455 // We implement this by simply making both entries in the hash table
456 // point to the same Symbol structure. That is easy enough if this is
457 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
458 // that we have seen both already, in which case they will both have
459 // independent entries in the symbol table. We can't simply change
460 // the symbol table entry, because we have pointers to the entries
461 // attached to the object files. So we mark the entry attached to the
462 // object file as a forwarder, and record it in the forwarders_ map.
463 // Note that entries in the hash table will never be marked as
466 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
467 // symbol exactly as it existed in the input file. SYM is usually
468 // that as well, but can be modified, for instance if we determine
469 // it's in a to-be-discarded section.
471 template<int size, bool big_endian>
473 Symbol_table::add_from_object(Object* object,
475 Stringpool::Key name_key,
477 Stringpool::Key version_key,
479 const elfcpp::Sym<size, big_endian>& sym,
480 const elfcpp::Sym<size, big_endian>& orig_sym)
482 Symbol* const snull = NULL;
483 std::pair<typename Symbol_table_type::iterator, bool> ins =
484 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
487 std::pair<typename Symbol_table_type::iterator, bool> insdef =
488 std::make_pair(this->table_.end(), false);
491 const Stringpool::Key vnull_key = 0;
492 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
497 // ins.first: an iterator, which is a pointer to a pair.
498 // ins.first->first: the key (a pair of name and version).
499 // ins.first->second: the value (Symbol*).
500 // ins.second: true if new entry was inserted, false if not.
502 Sized_symbol<size>* ret;
507 // We already have an entry for NAME/VERSION.
508 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
510 gold_assert(ret != NULL);
512 was_undefined = ret->is_undefined();
513 was_common = ret->is_common();
515 this->resolve(ret, sym, orig_sym, object, version);
521 // This is the first time we have seen NAME/NULL. Make
522 // NAME/NULL point to NAME/VERSION.
523 insdef.first->second = ret;
525 else if (insdef.first->second != ret
526 && insdef.first->second->is_undefined())
528 // This is the unfortunate case where we already have
529 // entries for both NAME/VERSION and NAME/NULL. Note
530 // that we don't want to combine them if the existing
531 // symbol is going to override the new one. FIXME: We
532 // currently just test is_undefined, but this may not do
533 // the right thing if the existing symbol is from a
534 // shared library and the new one is from a regular
537 const Sized_symbol<size>* sym2;
538 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
541 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
542 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
543 this->make_forwarder(insdef.first->second, ret);
544 insdef.first->second = ret;
550 // This is the first time we have seen NAME/VERSION.
551 gold_assert(ins.first->second == NULL);
553 was_undefined = false;
556 if (def && !insdef.second)
558 // We already have an entry for NAME/NULL. If we override
559 // it, then change it to NAME/VERSION.
560 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
563 this->resolve(ret, sym, orig_sym, object, version);
564 ins.first->second = ret;
568 Sized_target<size, big_endian>* target =
569 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
570 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
571 if (!target->has_make_symbol())
572 ret = new Sized_symbol<size>();
575 ret = target->make_symbol();
578 // This means that we don't want a symbol table
581 this->table_.erase(ins.first);
584 this->table_.erase(insdef.first);
585 // Inserting insdef invalidated ins.
586 this->table_.erase(std::make_pair(name_key,
593 ret->init(name, version, object, sym);
595 ins.first->second = ret;
598 // This is the first time we have seen NAME/NULL. Point
599 // it at the new entry for NAME/VERSION.
600 gold_assert(insdef.second);
601 insdef.first->second = ret;
606 // Record every time we see a new undefined symbol, to speed up
608 if (!was_undefined && ret->is_undefined())
609 ++this->saw_undefined_;
611 // Keep track of common symbols, to speed up common symbol
613 if (!was_common && ret->is_common())
614 this->commons_.push_back(ret);
616 ret->set_is_default(def);
620 // Add all the symbols in a relocatable object to the hash table.
622 template<int size, bool big_endian>
624 Symbol_table::add_from_relobj(
625 Sized_relobj<size, big_endian>* relobj,
626 const unsigned char* syms,
628 const char* sym_names,
629 size_t sym_name_size,
630 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
632 gold_assert(size == relobj->target()->get_size());
633 gold_assert(size == parameters->get_size());
635 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
637 const unsigned char* p = syms;
638 for (size_t i = 0; i < count; ++i, p += sym_size)
640 elfcpp::Sym<size, big_endian> sym(p);
641 elfcpp::Sym<size, big_endian>* psym = &sym;
643 unsigned int st_name = psym->get_st_name();
644 if (st_name >= sym_name_size)
646 relobj->error(_("bad global symbol name offset %u at %zu"),
651 const char* name = sym_names + st_name;
653 // A symbol defined in a section which we are not including must
654 // be treated as an undefined symbol.
655 unsigned char symbuf[sym_size];
656 elfcpp::Sym<size, big_endian> sym2(symbuf);
657 unsigned int st_shndx = psym->get_st_shndx();
658 if (st_shndx != elfcpp::SHN_UNDEF
659 && st_shndx < elfcpp::SHN_LORESERVE
660 && !relobj->is_section_included(st_shndx))
662 memcpy(symbuf, p, sym_size);
663 elfcpp::Sym_write<size, big_endian> sw(symbuf);
664 sw.put_st_shndx(elfcpp::SHN_UNDEF);
668 // In an object file, an '@' in the name separates the symbol
669 // name from the version name. If there are two '@' characters,
670 // this is the default version.
671 const char* ver = strchr(name, '@');
673 // DEF: is the version default? LOCAL: is the symbol forced local?
679 // The symbol name is of the form foo@VERSION or foo@@VERSION
680 namelen = ver - name;
688 else if (!version_script_.empty())
690 // The symbol name did not have a version, but
691 // the version script may assign a version anyway.
692 namelen = strlen(name);
694 // Check the global: entries from the version script.
695 const std::string& version =
696 version_script_.get_symbol_version(name);
697 if (!version.empty())
698 ver = version.c_str();
699 // Check the local: entries from the version script
700 if (version_script_.symbol_is_local(name))
704 Sized_symbol<size>* res;
707 Stringpool::Key name_key;
708 name = this->namepool_.add(name, true, &name_key);
709 res = this->add_from_object(relobj, name, name_key, NULL, 0,
712 this->force_local(res);
716 Stringpool::Key name_key;
717 name = this->namepool_.add_with_length(name, namelen, true,
719 Stringpool::Key ver_key;
720 ver = this->namepool_.add(ver, true, &ver_key);
722 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
726 (*sympointers)[i] = res;
730 // Add all the symbols in a dynamic object to the hash table.
732 template<int size, bool big_endian>
734 Symbol_table::add_from_dynobj(
735 Sized_dynobj<size, big_endian>* dynobj,
736 const unsigned char* syms,
738 const char* sym_names,
739 size_t sym_name_size,
740 const unsigned char* versym,
742 const std::vector<const char*>* version_map)
744 gold_assert(size == dynobj->target()->get_size());
745 gold_assert(size == parameters->get_size());
747 if (versym != NULL && versym_size / 2 < count)
749 dynobj->error(_("too few symbol versions"));
753 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
755 // We keep a list of all STT_OBJECT symbols, so that we can resolve
756 // weak aliases. This is necessary because if the dynamic object
757 // provides the same variable under two names, one of which is a
758 // weak definition, and the regular object refers to the weak
759 // definition, we have to put both the weak definition and the
760 // strong definition into the dynamic symbol table. Given a weak
761 // definition, the only way that we can find the corresponding
762 // strong definition, if any, is to search the symbol table.
763 std::vector<Sized_symbol<size>*> object_symbols;
765 const unsigned char* p = syms;
766 const unsigned char* vs = versym;
767 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
769 elfcpp::Sym<size, big_endian> sym(p);
771 // Ignore symbols with local binding or that have
772 // internal or hidden visibility.
773 if (sym.get_st_bind() == elfcpp::STB_LOCAL
774 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
775 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
778 unsigned int st_name = sym.get_st_name();
779 if (st_name >= sym_name_size)
781 dynobj->error(_("bad symbol name offset %u at %zu"),
786 const char* name = sym_names + st_name;
788 Sized_symbol<size>* res;
792 Stringpool::Key name_key;
793 name = this->namepool_.add(name, true, &name_key);
794 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
799 // Read the version information.
801 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
803 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
804 v &= elfcpp::VERSYM_VERSION;
806 // The Sun documentation says that V can be VER_NDX_LOCAL,
807 // or VER_NDX_GLOBAL, or a version index. The meaning of
808 // VER_NDX_LOCAL is defined as "Symbol has local scope."
809 // The old GNU linker will happily generate VER_NDX_LOCAL
810 // for an undefined symbol. I don't know what the Sun
811 // linker will generate.
813 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
814 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
816 // This symbol should not be visible outside the object.
820 // At this point we are definitely going to add this symbol.
821 Stringpool::Key name_key;
822 name = this->namepool_.add(name, true, &name_key);
824 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
825 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
827 // This symbol does not have a version.
828 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
833 if (v >= version_map->size())
835 dynobj->error(_("versym for symbol %zu out of range: %u"),
840 const char* version = (*version_map)[v];
843 dynobj->error(_("versym for symbol %zu has no name: %u"),
848 Stringpool::Key version_key;
849 version = this->namepool_.add(version, true, &version_key);
851 // If this is an absolute symbol, and the version name
852 // and symbol name are the same, then this is the
853 // version definition symbol. These symbols exist to
854 // support using -u to pull in particular versions. We
855 // do not want to record a version for them.
856 if (sym.get_st_shndx() == elfcpp::SHN_ABS
857 && name_key == version_key)
858 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
862 const bool def = (!hidden
863 && (sym.get_st_shndx()
864 != elfcpp::SHN_UNDEF));
865 res = this->add_from_object(dynobj, name, name_key, version,
866 version_key, def, sym, sym);
871 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
872 && sym.get_st_type() == elfcpp::STT_OBJECT)
873 object_symbols.push_back(res);
876 this->record_weak_aliases(&object_symbols);
879 // This is used to sort weak aliases. We sort them first by section
880 // index, then by offset, then by weak ahead of strong.
883 class Weak_alias_sorter
886 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
891 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
892 const Sized_symbol<size>* s2) const
894 if (s1->shndx() != s2->shndx())
895 return s1->shndx() < s2->shndx();
896 if (s1->value() != s2->value())
897 return s1->value() < s2->value();
898 if (s1->binding() != s2->binding())
900 if (s1->binding() == elfcpp::STB_WEAK)
902 if (s2->binding() == elfcpp::STB_WEAK)
905 return std::string(s1->name()) < std::string(s2->name());
908 // SYMBOLS is a list of object symbols from a dynamic object. Look
909 // for any weak aliases, and record them so that if we add the weak
910 // alias to the dynamic symbol table, we also add the corresponding
915 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
917 // Sort the vector by section index, then by offset, then by weak
919 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
921 // Walk through the vector. For each weak definition, record
923 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
928 if ((*p)->binding() != elfcpp::STB_WEAK)
931 // Build a circular list of weak aliases. Each symbol points to
932 // the next one in the circular list.
934 Sized_symbol<size>* from_sym = *p;
935 typename std::vector<Sized_symbol<size>*>::const_iterator q;
936 for (q = p + 1; q != symbols->end(); ++q)
938 if ((*q)->shndx() != from_sym->shndx()
939 || (*q)->value() != from_sym->value())
942 this->weak_aliases_[from_sym] = *q;
943 from_sym->set_has_alias();
949 this->weak_aliases_[from_sym] = *p;
950 from_sym->set_has_alias();
957 // Create and return a specially defined symbol. If ONLY_IF_REF is
958 // true, then only create the symbol if there is a reference to it.
959 // If this does not return NULL, it sets *POLDSYM to the existing
960 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
962 template<int size, bool big_endian>
964 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
966 Sized_symbol<size>** poldsym
970 Sized_symbol<size>* sym;
971 bool add_to_table = false;
972 typename Symbol_table_type::iterator add_loc = this->table_.end();
974 // If the caller didn't give us a version, see if we get one from
975 // the version script.
976 if (*pversion == NULL)
978 const std::string& v(this->version_script_.get_symbol_version(*pname));
980 *pversion = v.c_str();
985 oldsym = this->lookup(*pname, *pversion);
986 if (oldsym == NULL || !oldsym->is_undefined())
989 *pname = oldsym->name();
990 *pversion = oldsym->version();
994 // Canonicalize NAME and VERSION.
995 Stringpool::Key name_key;
996 *pname = this->namepool_.add(*pname, true, &name_key);
998 Stringpool::Key version_key = 0;
999 if (*pversion != NULL)
1000 *pversion = this->namepool_.add(*pversion, true, &version_key);
1002 Symbol* const snull = NULL;
1003 std::pair<typename Symbol_table_type::iterator, bool> ins =
1004 this->table_.insert(std::make_pair(std::make_pair(name_key,
1010 // We already have a symbol table entry for NAME/VERSION.
1011 oldsym = ins.first->second;
1012 gold_assert(oldsym != NULL);
1016 // We haven't seen this symbol before.
1017 gold_assert(ins.first->second == NULL);
1018 add_to_table = true;
1019 add_loc = ins.first;
1024 const Target* target = parameters->target();
1025 if (!target->has_make_symbol())
1026 sym = new Sized_symbol<size>();
1029 gold_assert(target->get_size() == size);
1030 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
1031 typedef Sized_target<size, big_endian> My_target;
1032 const My_target* sized_target =
1033 static_cast<const My_target*>(target);
1034 sym = sized_target->make_symbol();
1040 add_loc->second = sym;
1042 gold_assert(oldsym != NULL);
1044 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
1050 // Define a symbol based on an Output_data.
1053 Symbol_table::define_in_output_data(const char* name,
1054 const char* version,
1059 elfcpp::STB binding,
1060 elfcpp::STV visibility,
1061 unsigned char nonvis,
1062 bool offset_is_from_end,
1065 if (parameters->get_size() == 32)
1067 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1068 return this->do_define_in_output_data<32>(name, version, od,
1069 value, symsize, type, binding,
1077 else if (parameters->get_size() == 64)
1079 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1080 return this->do_define_in_output_data<64>(name, version, od,
1081 value, symsize, type, binding,
1093 // Define a symbol in an Output_data, sized version.
1097 Symbol_table::do_define_in_output_data(
1099 const char* version,
1101 typename elfcpp::Elf_types<size>::Elf_Addr value,
1102 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1104 elfcpp::STB binding,
1105 elfcpp::STV visibility,
1106 unsigned char nonvis,
1107 bool offset_is_from_end,
1110 Sized_symbol<size>* sym;
1111 Sized_symbol<size>* oldsym;
1113 if (parameters->is_big_endian())
1115 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1116 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1117 &name, &version, only_if_ref, &oldsym
1118 SELECT_SIZE_ENDIAN(size, true));
1125 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1126 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1127 &name, &version, only_if_ref, &oldsym
1128 SELECT_SIZE_ENDIAN(size, false));
1137 gold_assert(version == NULL || oldsym != NULL);
1138 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1139 offset_is_from_end);
1143 if (binding == elfcpp::STB_LOCAL
1144 || this->version_script_.symbol_is_local(name))
1145 this->force_local(sym);
1149 if (Symbol_table::should_override_with_special(oldsym))
1150 this->override_with_special(oldsym, sym);
1155 // Define a symbol based on an Output_segment.
1158 Symbol_table::define_in_output_segment(const char* name,
1159 const char* version, Output_segment* os,
1163 elfcpp::STB binding,
1164 elfcpp::STV visibility,
1165 unsigned char nonvis,
1166 Symbol::Segment_offset_base offset_base,
1169 if (parameters->get_size() == 32)
1171 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1172 return this->do_define_in_output_segment<32>(name, version, os,
1173 value, symsize, type,
1174 binding, visibility, nonvis,
1175 offset_base, only_if_ref);
1180 else if (parameters->get_size() == 64)
1182 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1183 return this->do_define_in_output_segment<64>(name, version, os,
1184 value, symsize, type,
1185 binding, visibility, nonvis,
1186 offset_base, only_if_ref);
1195 // Define a symbol in an Output_segment, sized version.
1199 Symbol_table::do_define_in_output_segment(
1201 const char* version,
1203 typename elfcpp::Elf_types<size>::Elf_Addr value,
1204 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1206 elfcpp::STB binding,
1207 elfcpp::STV visibility,
1208 unsigned char nonvis,
1209 Symbol::Segment_offset_base offset_base,
1212 Sized_symbol<size>* sym;
1213 Sized_symbol<size>* oldsym;
1215 if (parameters->is_big_endian())
1217 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1218 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1219 &name, &version, only_if_ref, &oldsym
1220 SELECT_SIZE_ENDIAN(size, true));
1227 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1228 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1229 &name, &version, only_if_ref, &oldsym
1230 SELECT_SIZE_ENDIAN(size, false));
1239 gold_assert(version == NULL || oldsym != NULL);
1240 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1245 if (binding == elfcpp::STB_LOCAL
1246 || this->version_script_.symbol_is_local(name))
1247 this->force_local(sym);
1251 if (Symbol_table::should_override_with_special(oldsym))
1252 this->override_with_special(oldsym, sym);
1257 // Define a special symbol with a constant value. It is a multiple
1258 // definition error if this symbol is already defined.
1261 Symbol_table::define_as_constant(const char* name,
1262 const char* version,
1266 elfcpp::STB binding,
1267 elfcpp::STV visibility,
1268 unsigned char nonvis,
1271 if (parameters->get_size() == 32)
1273 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1274 return this->do_define_as_constant<32>(name, version, value,
1275 symsize, type, binding,
1276 visibility, nonvis, only_if_ref);
1281 else if (parameters->get_size() == 64)
1283 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1284 return this->do_define_as_constant<64>(name, version, value,
1285 symsize, type, binding,
1286 visibility, nonvis, only_if_ref);
1295 // Define a symbol as a constant, sized version.
1299 Symbol_table::do_define_as_constant(
1301 const char* version,
1302 typename elfcpp::Elf_types<size>::Elf_Addr value,
1303 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1305 elfcpp::STB binding,
1306 elfcpp::STV visibility,
1307 unsigned char nonvis,
1310 Sized_symbol<size>* sym;
1311 Sized_symbol<size>* oldsym;
1313 if (parameters->is_big_endian())
1315 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1316 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1317 &name, &version, only_if_ref, &oldsym
1318 SELECT_SIZE_ENDIAN(size, true));
1325 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1326 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1327 &name, &version, only_if_ref, &oldsym
1328 SELECT_SIZE_ENDIAN(size, false));
1337 gold_assert(version == NULL || version == name || oldsym != NULL);
1338 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1342 if (binding == elfcpp::STB_LOCAL
1343 || this->version_script_.symbol_is_local(name))
1344 this->force_local(sym);
1348 if (Symbol_table::should_override_with_special(oldsym))
1349 this->override_with_special(oldsym, sym);
1354 // Define a set of symbols in output sections.
1357 Symbol_table::define_symbols(const Layout* layout, int count,
1358 const Define_symbol_in_section* p,
1361 for (int i = 0; i < count; ++i, ++p)
1363 Output_section* os = layout->find_output_section(p->output_section);
1365 this->define_in_output_data(p->name, NULL, os, p->value,
1366 p->size, p->type, p->binding,
1367 p->visibility, p->nonvis,
1368 p->offset_is_from_end,
1369 only_if_ref || p->only_if_ref);
1371 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1372 p->binding, p->visibility, p->nonvis,
1373 only_if_ref || p->only_if_ref);
1377 // Define a set of symbols in output segments.
1380 Symbol_table::define_symbols(const Layout* layout, int count,
1381 const Define_symbol_in_segment* p,
1384 for (int i = 0; i < count; ++i, ++p)
1386 Output_segment* os = layout->find_output_segment(p->segment_type,
1387 p->segment_flags_set,
1388 p->segment_flags_clear);
1390 this->define_in_output_segment(p->name, NULL, os, p->value,
1391 p->size, p->type, p->binding,
1392 p->visibility, p->nonvis,
1394 only_if_ref || p->only_if_ref);
1396 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1397 p->binding, p->visibility, p->nonvis,
1398 only_if_ref || p->only_if_ref);
1402 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1403 // symbol should be defined--typically a .dyn.bss section. VALUE is
1404 // the offset within POSD.
1408 Symbol_table::define_with_copy_reloc(
1409 Sized_symbol<size>* csym,
1411 typename elfcpp::Elf_types<size>::Elf_Addr value)
1413 gold_assert(csym->is_from_dynobj());
1414 gold_assert(!csym->is_copied_from_dynobj());
1415 Object* object = csym->object();
1416 gold_assert(object->is_dynamic());
1417 Dynobj* dynobj = static_cast<Dynobj*>(object);
1419 // Our copied variable has to override any variable in a shared
1421 elfcpp::STB binding = csym->binding();
1422 if (binding == elfcpp::STB_WEAK)
1423 binding = elfcpp::STB_GLOBAL;
1425 this->define_in_output_data(csym->name(), csym->version(),
1426 posd, value, csym->symsize(),
1427 csym->type(), binding,
1428 csym->visibility(), csym->nonvis(),
1431 csym->set_is_copied_from_dynobj();
1432 csym->set_needs_dynsym_entry();
1434 this->copied_symbol_dynobjs_[csym] = dynobj;
1436 // We have now defined all aliases, but we have not entered them all
1437 // in the copied_symbol_dynobjs_ map.
1438 if (csym->has_alias())
1443 sym = this->weak_aliases_[sym];
1446 gold_assert(sym->output_data() == posd);
1448 sym->set_is_copied_from_dynobj();
1449 this->copied_symbol_dynobjs_[sym] = dynobj;
1454 // SYM is defined using a COPY reloc. Return the dynamic object where
1455 // the original definition was found.
1458 Symbol_table::get_copy_source(const Symbol* sym) const
1460 gold_assert(sym->is_copied_from_dynobj());
1461 Copied_symbol_dynobjs::const_iterator p =
1462 this->copied_symbol_dynobjs_.find(sym);
1463 gold_assert(p != this->copied_symbol_dynobjs_.end());
1467 // Set the dynamic symbol indexes. INDEX is the index of the first
1468 // global dynamic symbol. Pointers to the symbols are stored into the
1469 // vector SYMS. The names are added to DYNPOOL. This returns an
1470 // updated dynamic symbol index.
1473 Symbol_table::set_dynsym_indexes(unsigned int index,
1474 std::vector<Symbol*>* syms,
1475 Stringpool* dynpool,
1478 for (Symbol_table_type::iterator p = this->table_.begin();
1479 p != this->table_.end();
1482 Symbol* sym = p->second;
1484 // Note that SYM may already have a dynamic symbol index, since
1485 // some symbols appear more than once in the symbol table, with
1486 // and without a version.
1488 if (!sym->should_add_dynsym_entry())
1489 sym->set_dynsym_index(-1U);
1490 else if (!sym->has_dynsym_index())
1492 sym->set_dynsym_index(index);
1494 syms->push_back(sym);
1495 dynpool->add(sym->name(), false, NULL);
1497 // Record any version information.
1498 if (sym->version() != NULL)
1499 versions->record_version(this, dynpool, sym);
1503 // Finish up the versions. In some cases this may add new dynamic
1505 index = versions->finalize(this, index, syms);
1510 // Set the final values for all the symbols. The index of the first
1511 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1512 // file offset OFF. Add their names to POOL. Return the new file
1513 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1516 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1517 size_t dyncount, Stringpool* pool,
1518 unsigned int *plocal_symcount)
1522 gold_assert(*plocal_symcount != 0);
1523 this->first_global_index_ = *plocal_symcount;
1525 this->dynamic_offset_ = dynoff;
1526 this->first_dynamic_global_index_ = dyn_global_index;
1527 this->dynamic_count_ = dyncount;
1529 if (parameters->get_size() == 32)
1531 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1532 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
1537 else if (parameters->get_size() == 64)
1539 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1540 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
1548 // Now that we have the final symbol table, we can reliably note
1549 // which symbols should get warnings.
1550 this->warnings_.note_warnings(this);
1555 // SYM is going into the symbol table at *PINDEX. Add the name to
1556 // POOL, update *PINDEX and *POFF.
1560 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1561 unsigned int* pindex, off_t* poff)
1563 sym->set_symtab_index(*pindex);
1564 pool->add(sym->name(), false, NULL);
1566 *poff += elfcpp::Elf_sizes<size>::sym_size;
1569 // Set the final value for all the symbols. This is called after
1570 // Layout::finalize, so all the output sections have their final
1575 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1576 unsigned int* plocal_symcount)
1578 off = align_address(off, size >> 3);
1579 this->offset_ = off;
1581 unsigned int index = *plocal_symcount;
1582 const unsigned int orig_index = index;
1584 // First do all the symbols which have been forced to be local, as
1585 // they must appear before all global symbols.
1586 for (Forced_locals::iterator p = this->forced_locals_.begin();
1587 p != this->forced_locals_.end();
1591 gold_assert(sym->is_forced_local());
1592 if (this->sized_finalize_symbol<size>(sym))
1594 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1599 // Now do all the remaining symbols.
1600 for (Symbol_table_type::iterator p = this->table_.begin();
1601 p != this->table_.end();
1604 Symbol* sym = p->second;
1605 if (this->sized_finalize_symbol<size>(sym))
1606 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1609 this->output_count_ = index - orig_index;
1614 // Finalize the symbol SYM. This returns true if the symbol should be
1615 // added to the symbol table, false otherwise.
1619 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1621 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
1623 // The default version of a symbol may appear twice in the symbol
1624 // table. We only need to finalize it once.
1625 if (sym->has_symtab_index())
1630 gold_assert(!sym->has_symtab_index());
1631 sym->set_symtab_index(-1U);
1632 gold_assert(sym->dynsym_index() == -1U);
1636 typename Sized_symbol<size>::Value_type value;
1638 switch (sym->source())
1640 case Symbol::FROM_OBJECT:
1642 unsigned int shndx = sym->shndx();
1644 // FIXME: We need some target specific support here.
1645 if (shndx >= elfcpp::SHN_LORESERVE
1646 && shndx != elfcpp::SHN_ABS)
1648 gold_error(_("%s: unsupported symbol section 0x%x"),
1649 sym->demangled_name().c_str(), shndx);
1650 shndx = elfcpp::SHN_UNDEF;
1653 Object* symobj = sym->object();
1654 if (symobj->is_dynamic())
1657 shndx = elfcpp::SHN_UNDEF;
1659 else if (shndx == elfcpp::SHN_UNDEF)
1661 else if (shndx == elfcpp::SHN_ABS)
1662 value = sym->value();
1665 Relobj* relobj = static_cast<Relobj*>(symobj);
1666 section_offset_type secoff;
1667 Output_section* os = relobj->output_section(shndx, &secoff);
1671 sym->set_symtab_index(-1U);
1672 gold_assert(sym->dynsym_index() == -1U);
1676 if (sym->type() == elfcpp::STT_TLS)
1677 value = sym->value() + os->tls_offset() + secoff;
1679 value = sym->value() + os->address() + secoff;
1684 case Symbol::IN_OUTPUT_DATA:
1686 Output_data* od = sym->output_data();
1687 value = sym->value() + od->address();
1688 if (sym->offset_is_from_end())
1689 value += od->data_size();
1693 case Symbol::IN_OUTPUT_SEGMENT:
1695 Output_segment* os = sym->output_segment();
1696 value = sym->value() + os->vaddr();
1697 switch (sym->offset_base())
1699 case Symbol::SEGMENT_START:
1701 case Symbol::SEGMENT_END:
1702 value += os->memsz();
1704 case Symbol::SEGMENT_BSS:
1705 value += os->filesz();
1713 case Symbol::CONSTANT:
1714 value = sym->value();
1721 sym->set_value(value);
1723 if (parameters->strip_all())
1725 sym->set_symtab_index(-1U);
1732 // Write out the global symbols.
1735 Symbol_table::write_globals(const Input_objects* input_objects,
1736 const Stringpool* sympool,
1737 const Stringpool* dynpool, Output_file* of) const
1739 if (parameters->get_size() == 32)
1741 if (parameters->is_big_endian())
1743 #ifdef HAVE_TARGET_32_BIG
1744 this->sized_write_globals<32, true>(input_objects, sympool,
1752 #ifdef HAVE_TARGET_32_LITTLE
1753 this->sized_write_globals<32, false>(input_objects, sympool,
1760 else if (parameters->get_size() == 64)
1762 if (parameters->is_big_endian())
1764 #ifdef HAVE_TARGET_64_BIG
1765 this->sized_write_globals<64, true>(input_objects, sympool,
1773 #ifdef HAVE_TARGET_64_LITTLE
1774 this->sized_write_globals<64, false>(input_objects, sympool,
1785 // Write out the global symbols.
1787 template<int size, bool big_endian>
1789 Symbol_table::sized_write_globals(const Input_objects* input_objects,
1790 const Stringpool* sympool,
1791 const Stringpool* dynpool,
1792 Output_file* of) const
1794 const Target* const target = parameters->target();
1796 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1798 const unsigned int output_count = this->output_count_;
1799 const section_size_type oview_size = output_count * sym_size;
1800 const unsigned int first_global_index = this->first_global_index_;
1801 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1803 const unsigned int dynamic_count = this->dynamic_count_;
1804 const section_size_type dynamic_size = dynamic_count * sym_size;
1805 const unsigned int first_dynamic_global_index =
1806 this->first_dynamic_global_index_;
1807 unsigned char* dynamic_view;
1808 if (this->dynamic_offset_ == 0)
1809 dynamic_view = NULL;
1811 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1813 for (Symbol_table_type::const_iterator p = this->table_.begin();
1814 p != this->table_.end();
1817 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1819 // Possibly warn about unresolved symbols in shared libraries.
1820 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
1822 unsigned int sym_index = sym->symtab_index();
1823 unsigned int dynsym_index;
1824 if (dynamic_view == NULL)
1827 dynsym_index = sym->dynsym_index();
1829 if (sym_index == -1U && dynsym_index == -1U)
1831 // This symbol is not included in the output file.
1836 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1837 switch (sym->source())
1839 case Symbol::FROM_OBJECT:
1841 unsigned int in_shndx = sym->shndx();
1843 // FIXME: We need some target specific support here.
1844 if (in_shndx >= elfcpp::SHN_LORESERVE
1845 && in_shndx != elfcpp::SHN_ABS)
1847 gold_error(_("%s: unsupported symbol section 0x%x"),
1848 sym->demangled_name().c_str(), in_shndx);
1853 Object* symobj = sym->object();
1854 if (symobj->is_dynamic())
1856 if (sym->needs_dynsym_value())
1857 value = target->dynsym_value(sym);
1858 shndx = elfcpp::SHN_UNDEF;
1860 else if (in_shndx == elfcpp::SHN_UNDEF
1861 || in_shndx == elfcpp::SHN_ABS)
1865 Relobj* relobj = static_cast<Relobj*>(symobj);
1866 section_offset_type secoff;
1867 Output_section* os = relobj->output_section(in_shndx,
1869 gold_assert(os != NULL);
1870 shndx = os->out_shndx();
1876 case Symbol::IN_OUTPUT_DATA:
1877 shndx = sym->output_data()->out_shndx();
1880 case Symbol::IN_OUTPUT_SEGMENT:
1881 shndx = elfcpp::SHN_ABS;
1884 case Symbol::CONSTANT:
1885 shndx = elfcpp::SHN_ABS;
1892 if (sym_index != -1U)
1894 sym_index -= first_global_index;
1895 gold_assert(sym_index < output_count);
1896 unsigned char* ps = psyms + (sym_index * sym_size);
1897 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1898 sym, sym->value(), shndx, sympool, ps
1899 SELECT_SIZE_ENDIAN(size, big_endian));
1902 if (dynsym_index != -1U)
1904 dynsym_index -= first_dynamic_global_index;
1905 gold_assert(dynsym_index < dynamic_count);
1906 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1907 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1908 sym, value, shndx, dynpool, pd
1909 SELECT_SIZE_ENDIAN(size, big_endian));
1913 of->write_output_view(this->offset_, oview_size, psyms);
1914 if (dynamic_view != NULL)
1915 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1918 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1919 // strtab holding the name.
1921 template<int size, bool big_endian>
1923 Symbol_table::sized_write_symbol(
1924 Sized_symbol<size>* sym,
1925 typename elfcpp::Elf_types<size>::Elf_Addr value,
1927 const Stringpool* pool,
1929 ACCEPT_SIZE_ENDIAN) const
1931 elfcpp::Sym_write<size, big_endian> osym(p);
1932 osym.put_st_name(pool->get_offset(sym->name()));
1933 osym.put_st_value(value);
1934 osym.put_st_size(sym->symsize());
1935 // A version script may have overridden the default binding.
1936 if (sym->is_forced_local())
1937 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
1939 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1940 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1941 osym.put_st_shndx(shndx);
1944 // Check for unresolved symbols in shared libraries. This is
1945 // controlled by the --allow-shlib-undefined option.
1947 // We only warn about libraries for which we have seen all the
1948 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1949 // which were not seen in this link. If we didn't see a DT_NEEDED
1950 // entry, we aren't going to be able to reliably report whether the
1951 // symbol is undefined.
1953 // We also don't warn about libraries found in the system library
1954 // directory (the directory were we find libc.so); we assume that
1955 // those libraries are OK. This heuristic avoids problems in
1956 // GNU/Linux, in which -ldl can have undefined references satisfied by
1960 Symbol_table::warn_about_undefined_dynobj_symbol(
1961 const Input_objects* input_objects,
1964 if (sym->source() == Symbol::FROM_OBJECT
1965 && sym->object()->is_dynamic()
1966 && sym->shndx() == elfcpp::SHN_UNDEF
1967 && sym->binding() != elfcpp::STB_WEAK
1968 && !parameters->allow_shlib_undefined()
1969 && !parameters->target()->is_defined_by_abi(sym)
1970 && !input_objects->found_in_system_library_directory(sym->object()))
1972 // A very ugly cast.
1973 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
1974 if (!dynobj->has_unknown_needed_entries())
1975 gold_error(_("%s: undefined reference to '%s'"),
1976 sym->object()->name().c_str(),
1977 sym->demangled_name().c_str());
1981 // Write out a section symbol. Return the update offset.
1984 Symbol_table::write_section_symbol(const Output_section *os,
1988 if (parameters->get_size() == 32)
1990 if (parameters->is_big_endian())
1992 #ifdef HAVE_TARGET_32_BIG
1993 this->sized_write_section_symbol<32, true>(os, of, offset);
2000 #ifdef HAVE_TARGET_32_LITTLE
2001 this->sized_write_section_symbol<32, false>(os, of, offset);
2007 else if (parameters->get_size() == 64)
2009 if (parameters->is_big_endian())
2011 #ifdef HAVE_TARGET_64_BIG
2012 this->sized_write_section_symbol<64, true>(os, of, offset);
2019 #ifdef HAVE_TARGET_64_LITTLE
2020 this->sized_write_section_symbol<64, false>(os, of, offset);
2030 // Write out a section symbol, specialized for size and endianness.
2032 template<int size, bool big_endian>
2034 Symbol_table::sized_write_section_symbol(const Output_section* os,
2038 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2040 unsigned char* pov = of->get_output_view(offset, sym_size);
2042 elfcpp::Sym_write<size, big_endian> osym(pov);
2043 osym.put_st_name(0);
2044 osym.put_st_value(os->address());
2045 osym.put_st_size(0);
2046 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2047 elfcpp::STT_SECTION));
2048 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2049 osym.put_st_shndx(os->out_shndx());
2051 of->write_output_view(offset, sym_size, pov);
2054 // Print statistical information to stderr. This is used for --stats.
2057 Symbol_table::print_stats() const
2059 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2060 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2061 program_name, this->table_.size(), this->table_.bucket_count());
2063 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2064 program_name, this->table_.size());
2066 this->namepool_.print_stats("symbol table stringpool");
2069 // We check for ODR violations by looking for symbols with the same
2070 // name for which the debugging information reports that they were
2071 // defined in different source locations. When comparing the source
2072 // location, we consider instances with the same base filename and
2073 // line number to be the same. This is because different object
2074 // files/shared libraries can include the same header file using
2075 // different paths, and we don't want to report an ODR violation in
2078 // This struct is used to compare line information, as returned by
2079 // Dwarf_line_info::one_addr2line. It implements a < comparison
2080 // operator used with std::set.
2082 struct Odr_violation_compare
2085 operator()(const std::string& s1, const std::string& s2) const
2087 std::string::size_type pos1 = s1.rfind('/');
2088 std::string::size_type pos2 = s2.rfind('/');
2089 if (pos1 == std::string::npos
2090 || pos2 == std::string::npos)
2092 return s1.compare(pos1, std::string::npos,
2093 s2, pos2, std::string::npos) < 0;
2097 // Check candidate_odr_violations_ to find symbols with the same name
2098 // but apparently different definitions (different source-file/line-no).
2101 Symbol_table::detect_odr_violations(const Task* task,
2102 const char* output_file_name) const
2104 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2105 it != candidate_odr_violations_.end();
2108 const char* symbol_name = it->first;
2109 // We use a sorted set so the output is deterministic.
2110 std::set<std::string, Odr_violation_compare> line_nums;
2112 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2113 locs = it->second.begin();
2114 locs != it->second.end();
2117 // We need to lock the object in order to read it. This
2118 // means that we have to run in a singleton Task. If we
2119 // want to run this in a general Task for better
2120 // performance, we will need one Task for object, plus
2121 // appropriate locking to ensure that we don't conflict with
2122 // other uses of the object.
2123 Task_lock_obj<Object> tl(task, locs->object);
2124 std::string lineno = Dwarf_line_info::one_addr2line(
2125 locs->object, locs->shndx, locs->offset);
2126 if (!lineno.empty())
2127 line_nums.insert(lineno);
2130 if (line_nums.size() > 1)
2132 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2133 "places (possible ODR violation):"),
2134 output_file_name, demangle(symbol_name).c_str());
2135 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2136 it2 != line_nums.end();
2138 fprintf(stderr, " %s\n", it2->c_str());
2143 // Warnings functions.
2145 // Add a new warning.
2148 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2149 const std::string& warning)
2151 name = symtab->canonicalize_name(name);
2152 this->warnings_[name].set(obj, warning);
2155 // Look through the warnings and mark the symbols for which we should
2156 // warn. This is called during Layout::finalize when we know the
2157 // sources for all the symbols.
2160 Warnings::note_warnings(Symbol_table* symtab)
2162 for (Warning_table::iterator p = this->warnings_.begin();
2163 p != this->warnings_.end();
2166 Symbol* sym = symtab->lookup(p->first, NULL);
2168 && sym->source() == Symbol::FROM_OBJECT
2169 && sym->object() == p->second.object)
2170 sym->set_has_warning();
2174 // Issue a warning. This is called when we see a relocation against a
2175 // symbol for which has a warning.
2177 template<int size, bool big_endian>
2179 Warnings::issue_warning(const Symbol* sym,
2180 const Relocate_info<size, big_endian>* relinfo,
2181 size_t relnum, off_t reloffset) const
2183 gold_assert(sym->has_warning());
2184 Warning_table::const_iterator p = this->warnings_.find(sym->name());
2185 gold_assert(p != this->warnings_.end());
2186 gold_warning_at_location(relinfo, relnum, reloffset,
2187 "%s", p->second.text.c_str());
2190 // Instantiate the templates we need. We could use the configure
2191 // script to restrict this to only the ones needed for implemented
2194 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2197 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2200 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2203 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2206 #ifdef HAVE_TARGET_32_LITTLE
2209 Symbol_table::add_from_relobj<32, false>(
2210 Sized_relobj<32, false>* relobj,
2211 const unsigned char* syms,
2213 const char* sym_names,
2214 size_t sym_name_size,
2215 Sized_relobj<32, true>::Symbols* sympointers);
2218 #ifdef HAVE_TARGET_32_BIG
2221 Symbol_table::add_from_relobj<32, true>(
2222 Sized_relobj<32, true>* relobj,
2223 const unsigned char* syms,
2225 const char* sym_names,
2226 size_t sym_name_size,
2227 Sized_relobj<32, false>::Symbols* sympointers);
2230 #ifdef HAVE_TARGET_64_LITTLE
2233 Symbol_table::add_from_relobj<64, false>(
2234 Sized_relobj<64, false>* relobj,
2235 const unsigned char* syms,
2237 const char* sym_names,
2238 size_t sym_name_size,
2239 Sized_relobj<64, true>::Symbols* sympointers);
2242 #ifdef HAVE_TARGET_64_BIG
2245 Symbol_table::add_from_relobj<64, true>(
2246 Sized_relobj<64, true>* relobj,
2247 const unsigned char* syms,
2249 const char* sym_names,
2250 size_t sym_name_size,
2251 Sized_relobj<64, false>::Symbols* sympointers);
2254 #ifdef HAVE_TARGET_32_LITTLE
2257 Symbol_table::add_from_dynobj<32, false>(
2258 Sized_dynobj<32, false>* dynobj,
2259 const unsigned char* syms,
2261 const char* sym_names,
2262 size_t sym_name_size,
2263 const unsigned char* versym,
2265 const std::vector<const char*>* version_map);
2268 #ifdef HAVE_TARGET_32_BIG
2271 Symbol_table::add_from_dynobj<32, true>(
2272 Sized_dynobj<32, true>* dynobj,
2273 const unsigned char* syms,
2275 const char* sym_names,
2276 size_t sym_name_size,
2277 const unsigned char* versym,
2279 const std::vector<const char*>* version_map);
2282 #ifdef HAVE_TARGET_64_LITTLE
2285 Symbol_table::add_from_dynobj<64, false>(
2286 Sized_dynobj<64, false>* dynobj,
2287 const unsigned char* syms,
2289 const char* sym_names,
2290 size_t sym_name_size,
2291 const unsigned char* versym,
2293 const std::vector<const char*>* version_map);
2296 #ifdef HAVE_TARGET_64_BIG
2299 Symbol_table::add_from_dynobj<64, true>(
2300 Sized_dynobj<64, true>* dynobj,
2301 const unsigned char* syms,
2303 const char* sym_names,
2304 size_t sym_name_size,
2305 const unsigned char* versym,
2307 const std::vector<const char*>* version_map);
2310 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2313 Symbol_table::define_with_copy_reloc<32>(
2314 Sized_symbol<32>* sym,
2316 elfcpp::Elf_types<32>::Elf_Addr value);
2319 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2322 Symbol_table::define_with_copy_reloc<64>(
2323 Sized_symbol<64>* sym,
2325 elfcpp::Elf_types<64>::Elf_Addr value);
2328 #ifdef HAVE_TARGET_32_LITTLE
2331 Warnings::issue_warning<32, false>(const Symbol* sym,
2332 const Relocate_info<32, false>* relinfo,
2333 size_t relnum, off_t reloffset) const;
2336 #ifdef HAVE_TARGET_32_BIG
2339 Warnings::issue_warning<32, true>(const Symbol* sym,
2340 const Relocate_info<32, true>* relinfo,
2341 size_t relnum, off_t reloffset) const;
2344 #ifdef HAVE_TARGET_64_LITTLE
2347 Warnings::issue_warning<64, false>(const Symbol* sym,
2348 const Relocate_info<64, false>* relinfo,
2349 size_t relnum, off_t reloffset) const;
2352 #ifdef HAVE_TARGET_64_BIG
2355 Warnings::issue_warning<64, true>(const Symbol* sym,
2356 const Relocate_info<64, true>* relinfo,
2357 size_t relnum, off_t reloffset) const;
2360 } // End namespace gold.