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 bool just_symbols = relobj->just_symbols();
639 const unsigned char* p = syms;
640 for (size_t i = 0; i < count; ++i, p += sym_size)
642 elfcpp::Sym<size, big_endian> sym(p);
643 elfcpp::Sym<size, big_endian>* psym = &sym;
645 unsigned int st_name = psym->get_st_name();
646 if (st_name >= sym_name_size)
648 relobj->error(_("bad global symbol name offset %u at %zu"),
653 const char* name = sym_names + st_name;
655 // A symbol defined in a section which we are not including must
656 // be treated as an undefined symbol.
657 unsigned char symbuf[sym_size];
658 elfcpp::Sym<size, big_endian> sym2(symbuf);
659 unsigned int st_shndx = psym->get_st_shndx();
660 if (st_shndx != elfcpp::SHN_UNDEF
661 && st_shndx < elfcpp::SHN_LORESERVE
662 && !relobj->is_section_included(st_shndx))
664 memcpy(symbuf, p, sym_size);
665 elfcpp::Sym_write<size, big_endian> sw(symbuf);
666 sw.put_st_shndx(elfcpp::SHN_UNDEF);
670 // In an object file, an '@' in the name separates the symbol
671 // name from the version name. If there are two '@' characters,
672 // this is the default version.
673 const char* ver = strchr(name, '@');
675 // DEF: is the version default? LOCAL: is the symbol forced local?
681 // The symbol name is of the form foo@VERSION or foo@@VERSION
682 namelen = ver - name;
690 else if (!version_script_.empty())
692 // The symbol name did not have a version, but
693 // the version script may assign a version anyway.
694 namelen = strlen(name);
696 // Check the global: entries from the version script.
697 const std::string& version =
698 version_script_.get_symbol_version(name);
699 if (!version.empty())
700 ver = version.c_str();
701 // Check the local: entries from the version script
702 if (version_script_.symbol_is_local(name))
709 memcpy(symbuf, p, sym_size);
710 elfcpp::Sym_write<size, big_endian> sw(symbuf);
711 sw.put_st_shndx(elfcpp::SHN_ABS);
712 if (st_shndx != elfcpp::SHN_UNDEF
713 && st_shndx < elfcpp::SHN_LORESERVE)
715 // Symbol values in object files are section relative.
716 // This is normally what we want, but since here we are
717 // converting the symbol to absolute we need to add the
718 // section address. The section address in an object
719 // file is normally zero, but people can use a linker
720 // script to change it.
721 sw.put_st_value(sym2.get_st_value()
722 + relobj->section_address(st_shndx));
727 Sized_symbol<size>* res;
730 Stringpool::Key name_key;
731 name = this->namepool_.add(name, true, &name_key);
732 res = this->add_from_object(relobj, name, name_key, NULL, 0,
735 this->force_local(res);
739 Stringpool::Key name_key;
740 name = this->namepool_.add_with_length(name, namelen, true,
742 Stringpool::Key ver_key;
743 ver = this->namepool_.add(ver, true, &ver_key);
745 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
749 (*sympointers)[i] = res;
753 // Add all the symbols in a dynamic object to the hash table.
755 template<int size, bool big_endian>
757 Symbol_table::add_from_dynobj(
758 Sized_dynobj<size, big_endian>* dynobj,
759 const unsigned char* syms,
761 const char* sym_names,
762 size_t sym_name_size,
763 const unsigned char* versym,
765 const std::vector<const char*>* version_map)
767 gold_assert(size == dynobj->target()->get_size());
768 gold_assert(size == parameters->get_size());
770 if (dynobj->just_symbols())
772 gold_error(_("--just-symbols does not make sense with a shared object"));
776 if (versym != NULL && versym_size / 2 < count)
778 dynobj->error(_("too few symbol versions"));
782 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
784 // We keep a list of all STT_OBJECT symbols, so that we can resolve
785 // weak aliases. This is necessary because if the dynamic object
786 // provides the same variable under two names, one of which is a
787 // weak definition, and the regular object refers to the weak
788 // definition, we have to put both the weak definition and the
789 // strong definition into the dynamic symbol table. Given a weak
790 // definition, the only way that we can find the corresponding
791 // strong definition, if any, is to search the symbol table.
792 std::vector<Sized_symbol<size>*> object_symbols;
794 const unsigned char* p = syms;
795 const unsigned char* vs = versym;
796 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
798 elfcpp::Sym<size, big_endian> sym(p);
800 // Ignore symbols with local binding or that have
801 // internal or hidden visibility.
802 if (sym.get_st_bind() == elfcpp::STB_LOCAL
803 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
804 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
807 unsigned int st_name = sym.get_st_name();
808 if (st_name >= sym_name_size)
810 dynobj->error(_("bad symbol name offset %u at %zu"),
815 const char* name = sym_names + st_name;
817 Sized_symbol<size>* res;
821 Stringpool::Key name_key;
822 name = this->namepool_.add(name, true, &name_key);
823 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
828 // Read the version information.
830 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
832 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
833 v &= elfcpp::VERSYM_VERSION;
835 // The Sun documentation says that V can be VER_NDX_LOCAL,
836 // or VER_NDX_GLOBAL, or a version index. The meaning of
837 // VER_NDX_LOCAL is defined as "Symbol has local scope."
838 // The old GNU linker will happily generate VER_NDX_LOCAL
839 // for an undefined symbol. I don't know what the Sun
840 // linker will generate.
842 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
843 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
845 // This symbol should not be visible outside the object.
849 // At this point we are definitely going to add this symbol.
850 Stringpool::Key name_key;
851 name = this->namepool_.add(name, true, &name_key);
853 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
854 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
856 // This symbol does not have a version.
857 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
862 if (v >= version_map->size())
864 dynobj->error(_("versym for symbol %zu out of range: %u"),
869 const char* version = (*version_map)[v];
872 dynobj->error(_("versym for symbol %zu has no name: %u"),
877 Stringpool::Key version_key;
878 version = this->namepool_.add(version, true, &version_key);
880 // If this is an absolute symbol, and the version name
881 // and symbol name are the same, then this is the
882 // version definition symbol. These symbols exist to
883 // support using -u to pull in particular versions. We
884 // do not want to record a version for them.
885 if (sym.get_st_shndx() == elfcpp::SHN_ABS
886 && name_key == version_key)
887 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
891 const bool def = (!hidden
892 && (sym.get_st_shndx()
893 != elfcpp::SHN_UNDEF));
894 res = this->add_from_object(dynobj, name, name_key, version,
895 version_key, def, sym, sym);
900 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
901 && sym.get_st_type() == elfcpp::STT_OBJECT)
902 object_symbols.push_back(res);
905 this->record_weak_aliases(&object_symbols);
908 // This is used to sort weak aliases. We sort them first by section
909 // index, then by offset, then by weak ahead of strong.
912 class Weak_alias_sorter
915 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
920 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
921 const Sized_symbol<size>* s2) const
923 if (s1->shndx() != s2->shndx())
924 return s1->shndx() < s2->shndx();
925 if (s1->value() != s2->value())
926 return s1->value() < s2->value();
927 if (s1->binding() != s2->binding())
929 if (s1->binding() == elfcpp::STB_WEAK)
931 if (s2->binding() == elfcpp::STB_WEAK)
934 return std::string(s1->name()) < std::string(s2->name());
937 // SYMBOLS is a list of object symbols from a dynamic object. Look
938 // for any weak aliases, and record them so that if we add the weak
939 // alias to the dynamic symbol table, we also add the corresponding
944 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
946 // Sort the vector by section index, then by offset, then by weak
948 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
950 // Walk through the vector. For each weak definition, record
952 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
957 if ((*p)->binding() != elfcpp::STB_WEAK)
960 // Build a circular list of weak aliases. Each symbol points to
961 // the next one in the circular list.
963 Sized_symbol<size>* from_sym = *p;
964 typename std::vector<Sized_symbol<size>*>::const_iterator q;
965 for (q = p + 1; q != symbols->end(); ++q)
967 if ((*q)->shndx() != from_sym->shndx()
968 || (*q)->value() != from_sym->value())
971 this->weak_aliases_[from_sym] = *q;
972 from_sym->set_has_alias();
978 this->weak_aliases_[from_sym] = *p;
979 from_sym->set_has_alias();
986 // Create and return a specially defined symbol. If ONLY_IF_REF is
987 // true, then only create the symbol if there is a reference to it.
988 // If this does not return NULL, it sets *POLDSYM to the existing
989 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
991 template<int size, bool big_endian>
993 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
995 Sized_symbol<size>** poldsym
999 Sized_symbol<size>* sym;
1000 bool add_to_table = false;
1001 typename Symbol_table_type::iterator add_loc = this->table_.end();
1003 // If the caller didn't give us a version, see if we get one from
1004 // the version script.
1005 if (*pversion == NULL)
1007 const std::string& v(this->version_script_.get_symbol_version(*pname));
1009 *pversion = v.c_str();
1014 oldsym = this->lookup(*pname, *pversion);
1015 if (oldsym == NULL || !oldsym->is_undefined())
1018 *pname = oldsym->name();
1019 *pversion = oldsym->version();
1023 // Canonicalize NAME and VERSION.
1024 Stringpool::Key name_key;
1025 *pname = this->namepool_.add(*pname, true, &name_key);
1027 Stringpool::Key version_key = 0;
1028 if (*pversion != NULL)
1029 *pversion = this->namepool_.add(*pversion, true, &version_key);
1031 Symbol* const snull = NULL;
1032 std::pair<typename Symbol_table_type::iterator, bool> ins =
1033 this->table_.insert(std::make_pair(std::make_pair(name_key,
1039 // We already have a symbol table entry for NAME/VERSION.
1040 oldsym = ins.first->second;
1041 gold_assert(oldsym != NULL);
1045 // We haven't seen this symbol before.
1046 gold_assert(ins.first->second == NULL);
1047 add_to_table = true;
1048 add_loc = ins.first;
1053 const Target* target = parameters->target();
1054 if (!target->has_make_symbol())
1055 sym = new Sized_symbol<size>();
1058 gold_assert(target->get_size() == size);
1059 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
1060 typedef Sized_target<size, big_endian> My_target;
1061 const My_target* sized_target =
1062 static_cast<const My_target*>(target);
1063 sym = sized_target->make_symbol();
1069 add_loc->second = sym;
1071 gold_assert(oldsym != NULL);
1073 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
1079 // Define a symbol based on an Output_data.
1082 Symbol_table::define_in_output_data(const char* name,
1083 const char* version,
1088 elfcpp::STB binding,
1089 elfcpp::STV visibility,
1090 unsigned char nonvis,
1091 bool offset_is_from_end,
1094 if (parameters->get_size() == 32)
1096 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1097 return this->do_define_in_output_data<32>(name, version, od,
1098 value, symsize, type, binding,
1106 else if (parameters->get_size() == 64)
1108 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1109 return this->do_define_in_output_data<64>(name, version, od,
1110 value, symsize, type, binding,
1122 // Define a symbol in an Output_data, sized version.
1126 Symbol_table::do_define_in_output_data(
1128 const char* version,
1130 typename elfcpp::Elf_types<size>::Elf_Addr value,
1131 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1133 elfcpp::STB binding,
1134 elfcpp::STV visibility,
1135 unsigned char nonvis,
1136 bool offset_is_from_end,
1139 Sized_symbol<size>* sym;
1140 Sized_symbol<size>* oldsym;
1142 if (parameters->is_big_endian())
1144 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1145 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1146 &name, &version, only_if_ref, &oldsym
1147 SELECT_SIZE_ENDIAN(size, true));
1154 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1155 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1156 &name, &version, only_if_ref, &oldsym
1157 SELECT_SIZE_ENDIAN(size, false));
1166 gold_assert(version == NULL || oldsym != NULL);
1167 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1168 offset_is_from_end);
1172 if (binding == elfcpp::STB_LOCAL
1173 || this->version_script_.symbol_is_local(name))
1174 this->force_local(sym);
1178 if (Symbol_table::should_override_with_special(oldsym))
1179 this->override_with_special(oldsym, sym);
1184 // Define a symbol based on an Output_segment.
1187 Symbol_table::define_in_output_segment(const char* name,
1188 const char* version, Output_segment* os,
1192 elfcpp::STB binding,
1193 elfcpp::STV visibility,
1194 unsigned char nonvis,
1195 Symbol::Segment_offset_base offset_base,
1198 if (parameters->get_size() == 32)
1200 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1201 return this->do_define_in_output_segment<32>(name, version, os,
1202 value, symsize, type,
1203 binding, visibility, nonvis,
1204 offset_base, only_if_ref);
1209 else if (parameters->get_size() == 64)
1211 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1212 return this->do_define_in_output_segment<64>(name, version, os,
1213 value, symsize, type,
1214 binding, visibility, nonvis,
1215 offset_base, only_if_ref);
1224 // Define a symbol in an Output_segment, sized version.
1228 Symbol_table::do_define_in_output_segment(
1230 const char* version,
1232 typename elfcpp::Elf_types<size>::Elf_Addr value,
1233 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1235 elfcpp::STB binding,
1236 elfcpp::STV visibility,
1237 unsigned char nonvis,
1238 Symbol::Segment_offset_base offset_base,
1241 Sized_symbol<size>* sym;
1242 Sized_symbol<size>* oldsym;
1244 if (parameters->is_big_endian())
1246 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1247 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1248 &name, &version, only_if_ref, &oldsym
1249 SELECT_SIZE_ENDIAN(size, true));
1256 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1257 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1258 &name, &version, only_if_ref, &oldsym
1259 SELECT_SIZE_ENDIAN(size, false));
1268 gold_assert(version == NULL || oldsym != NULL);
1269 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1274 if (binding == elfcpp::STB_LOCAL
1275 || this->version_script_.symbol_is_local(name))
1276 this->force_local(sym);
1280 if (Symbol_table::should_override_with_special(oldsym))
1281 this->override_with_special(oldsym, sym);
1286 // Define a special symbol with a constant value. It is a multiple
1287 // definition error if this symbol is already defined.
1290 Symbol_table::define_as_constant(const char* name,
1291 const char* version,
1295 elfcpp::STB binding,
1296 elfcpp::STV visibility,
1297 unsigned char nonvis,
1300 if (parameters->get_size() == 32)
1302 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1303 return this->do_define_as_constant<32>(name, version, value,
1304 symsize, type, binding,
1305 visibility, nonvis, only_if_ref);
1310 else if (parameters->get_size() == 64)
1312 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1313 return this->do_define_as_constant<64>(name, version, value,
1314 symsize, type, binding,
1315 visibility, nonvis, only_if_ref);
1324 // Define a symbol as a constant, sized version.
1328 Symbol_table::do_define_as_constant(
1330 const char* version,
1331 typename elfcpp::Elf_types<size>::Elf_Addr value,
1332 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1334 elfcpp::STB binding,
1335 elfcpp::STV visibility,
1336 unsigned char nonvis,
1339 Sized_symbol<size>* sym;
1340 Sized_symbol<size>* oldsym;
1342 if (parameters->is_big_endian())
1344 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1345 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1346 &name, &version, only_if_ref, &oldsym
1347 SELECT_SIZE_ENDIAN(size, true));
1354 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1355 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1356 &name, &version, only_if_ref, &oldsym
1357 SELECT_SIZE_ENDIAN(size, false));
1366 gold_assert(version == NULL || version == name || oldsym != NULL);
1367 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1371 if (binding == elfcpp::STB_LOCAL
1372 || this->version_script_.symbol_is_local(name))
1373 this->force_local(sym);
1377 if (Symbol_table::should_override_with_special(oldsym))
1378 this->override_with_special(oldsym, sym);
1383 // Define a set of symbols in output sections.
1386 Symbol_table::define_symbols(const Layout* layout, int count,
1387 const Define_symbol_in_section* p,
1390 for (int i = 0; i < count; ++i, ++p)
1392 Output_section* os = layout->find_output_section(p->output_section);
1394 this->define_in_output_data(p->name, NULL, os, p->value,
1395 p->size, p->type, p->binding,
1396 p->visibility, p->nonvis,
1397 p->offset_is_from_end,
1398 only_if_ref || p->only_if_ref);
1400 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1401 p->binding, p->visibility, p->nonvis,
1402 only_if_ref || p->only_if_ref);
1406 // Define a set of symbols in output segments.
1409 Symbol_table::define_symbols(const Layout* layout, int count,
1410 const Define_symbol_in_segment* p,
1413 for (int i = 0; i < count; ++i, ++p)
1415 Output_segment* os = layout->find_output_segment(p->segment_type,
1416 p->segment_flags_set,
1417 p->segment_flags_clear);
1419 this->define_in_output_segment(p->name, NULL, os, p->value,
1420 p->size, p->type, p->binding,
1421 p->visibility, p->nonvis,
1423 only_if_ref || p->only_if_ref);
1425 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1426 p->binding, p->visibility, p->nonvis,
1427 only_if_ref || p->only_if_ref);
1431 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1432 // symbol should be defined--typically a .dyn.bss section. VALUE is
1433 // the offset within POSD.
1437 Symbol_table::define_with_copy_reloc(
1438 Sized_symbol<size>* csym,
1440 typename elfcpp::Elf_types<size>::Elf_Addr value)
1442 gold_assert(csym->is_from_dynobj());
1443 gold_assert(!csym->is_copied_from_dynobj());
1444 Object* object = csym->object();
1445 gold_assert(object->is_dynamic());
1446 Dynobj* dynobj = static_cast<Dynobj*>(object);
1448 // Our copied variable has to override any variable in a shared
1450 elfcpp::STB binding = csym->binding();
1451 if (binding == elfcpp::STB_WEAK)
1452 binding = elfcpp::STB_GLOBAL;
1454 this->define_in_output_data(csym->name(), csym->version(),
1455 posd, value, csym->symsize(),
1456 csym->type(), binding,
1457 csym->visibility(), csym->nonvis(),
1460 csym->set_is_copied_from_dynobj();
1461 csym->set_needs_dynsym_entry();
1463 this->copied_symbol_dynobjs_[csym] = dynobj;
1465 // We have now defined all aliases, but we have not entered them all
1466 // in the copied_symbol_dynobjs_ map.
1467 if (csym->has_alias())
1472 sym = this->weak_aliases_[sym];
1475 gold_assert(sym->output_data() == posd);
1477 sym->set_is_copied_from_dynobj();
1478 this->copied_symbol_dynobjs_[sym] = dynobj;
1483 // SYM is defined using a COPY reloc. Return the dynamic object where
1484 // the original definition was found.
1487 Symbol_table::get_copy_source(const Symbol* sym) const
1489 gold_assert(sym->is_copied_from_dynobj());
1490 Copied_symbol_dynobjs::const_iterator p =
1491 this->copied_symbol_dynobjs_.find(sym);
1492 gold_assert(p != this->copied_symbol_dynobjs_.end());
1496 // Set the dynamic symbol indexes. INDEX is the index of the first
1497 // global dynamic symbol. Pointers to the symbols are stored into the
1498 // vector SYMS. The names are added to DYNPOOL. This returns an
1499 // updated dynamic symbol index.
1502 Symbol_table::set_dynsym_indexes(unsigned int index,
1503 std::vector<Symbol*>* syms,
1504 Stringpool* dynpool,
1507 for (Symbol_table_type::iterator p = this->table_.begin();
1508 p != this->table_.end();
1511 Symbol* sym = p->second;
1513 // Note that SYM may already have a dynamic symbol index, since
1514 // some symbols appear more than once in the symbol table, with
1515 // and without a version.
1517 if (!sym->should_add_dynsym_entry())
1518 sym->set_dynsym_index(-1U);
1519 else if (!sym->has_dynsym_index())
1521 sym->set_dynsym_index(index);
1523 syms->push_back(sym);
1524 dynpool->add(sym->name(), false, NULL);
1526 // Record any version information.
1527 if (sym->version() != NULL)
1528 versions->record_version(this, dynpool, sym);
1532 // Finish up the versions. In some cases this may add new dynamic
1534 index = versions->finalize(this, index, syms);
1539 // Set the final values for all the symbols. The index of the first
1540 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1541 // file offset OFF. Add their names to POOL. Return the new file
1542 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1545 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1546 size_t dyncount, Stringpool* pool,
1547 unsigned int *plocal_symcount)
1551 gold_assert(*plocal_symcount != 0);
1552 this->first_global_index_ = *plocal_symcount;
1554 this->dynamic_offset_ = dynoff;
1555 this->first_dynamic_global_index_ = dyn_global_index;
1556 this->dynamic_count_ = dyncount;
1558 if (parameters->get_size() == 32)
1560 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1561 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
1566 else if (parameters->get_size() == 64)
1568 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1569 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
1577 // Now that we have the final symbol table, we can reliably note
1578 // which symbols should get warnings.
1579 this->warnings_.note_warnings(this);
1584 // SYM is going into the symbol table at *PINDEX. Add the name to
1585 // POOL, update *PINDEX and *POFF.
1589 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1590 unsigned int* pindex, off_t* poff)
1592 sym->set_symtab_index(*pindex);
1593 pool->add(sym->name(), false, NULL);
1595 *poff += elfcpp::Elf_sizes<size>::sym_size;
1598 // Set the final value for all the symbols. This is called after
1599 // Layout::finalize, so all the output sections have their final
1604 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1605 unsigned int* plocal_symcount)
1607 off = align_address(off, size >> 3);
1608 this->offset_ = off;
1610 unsigned int index = *plocal_symcount;
1611 const unsigned int orig_index = index;
1613 // First do all the symbols which have been forced to be local, as
1614 // they must appear before all global symbols.
1615 for (Forced_locals::iterator p = this->forced_locals_.begin();
1616 p != this->forced_locals_.end();
1620 gold_assert(sym->is_forced_local());
1621 if (this->sized_finalize_symbol<size>(sym))
1623 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1628 // Now do all the remaining symbols.
1629 for (Symbol_table_type::iterator p = this->table_.begin();
1630 p != this->table_.end();
1633 Symbol* sym = p->second;
1634 if (this->sized_finalize_symbol<size>(sym))
1635 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1638 this->output_count_ = index - orig_index;
1643 // Finalize the symbol SYM. This returns true if the symbol should be
1644 // added to the symbol table, false otherwise.
1648 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1650 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
1652 // The default version of a symbol may appear twice in the symbol
1653 // table. We only need to finalize it once.
1654 if (sym->has_symtab_index())
1659 gold_assert(!sym->has_symtab_index());
1660 sym->set_symtab_index(-1U);
1661 gold_assert(sym->dynsym_index() == -1U);
1665 typename Sized_symbol<size>::Value_type value;
1667 switch (sym->source())
1669 case Symbol::FROM_OBJECT:
1671 unsigned int shndx = sym->shndx();
1673 // FIXME: We need some target specific support here.
1674 if (shndx >= elfcpp::SHN_LORESERVE
1675 && shndx != elfcpp::SHN_ABS)
1677 gold_error(_("%s: unsupported symbol section 0x%x"),
1678 sym->demangled_name().c_str(), shndx);
1679 shndx = elfcpp::SHN_UNDEF;
1682 Object* symobj = sym->object();
1683 if (symobj->is_dynamic())
1686 shndx = elfcpp::SHN_UNDEF;
1688 else if (shndx == elfcpp::SHN_UNDEF)
1690 else if (shndx == elfcpp::SHN_ABS)
1691 value = sym->value();
1694 Relobj* relobj = static_cast<Relobj*>(symobj);
1695 section_offset_type secoff;
1696 Output_section* os = relobj->output_section(shndx, &secoff);
1700 sym->set_symtab_index(-1U);
1701 gold_assert(sym->dynsym_index() == -1U);
1705 if (sym->type() == elfcpp::STT_TLS)
1706 value = sym->value() + os->tls_offset() + secoff;
1708 value = sym->value() + os->address() + secoff;
1713 case Symbol::IN_OUTPUT_DATA:
1715 Output_data* od = sym->output_data();
1716 value = sym->value() + od->address();
1717 if (sym->offset_is_from_end())
1718 value += od->data_size();
1722 case Symbol::IN_OUTPUT_SEGMENT:
1724 Output_segment* os = sym->output_segment();
1725 value = sym->value() + os->vaddr();
1726 switch (sym->offset_base())
1728 case Symbol::SEGMENT_START:
1730 case Symbol::SEGMENT_END:
1731 value += os->memsz();
1733 case Symbol::SEGMENT_BSS:
1734 value += os->filesz();
1742 case Symbol::CONSTANT:
1743 value = sym->value();
1750 sym->set_value(value);
1752 if (parameters->strip_all())
1754 sym->set_symtab_index(-1U);
1761 // Write out the global symbols.
1764 Symbol_table::write_globals(const Input_objects* input_objects,
1765 const Stringpool* sympool,
1766 const Stringpool* dynpool, Output_file* of) const
1768 if (parameters->get_size() == 32)
1770 if (parameters->is_big_endian())
1772 #ifdef HAVE_TARGET_32_BIG
1773 this->sized_write_globals<32, true>(input_objects, sympool,
1781 #ifdef HAVE_TARGET_32_LITTLE
1782 this->sized_write_globals<32, false>(input_objects, sympool,
1789 else if (parameters->get_size() == 64)
1791 if (parameters->is_big_endian())
1793 #ifdef HAVE_TARGET_64_BIG
1794 this->sized_write_globals<64, true>(input_objects, sympool,
1802 #ifdef HAVE_TARGET_64_LITTLE
1803 this->sized_write_globals<64, false>(input_objects, sympool,
1814 // Write out the global symbols.
1816 template<int size, bool big_endian>
1818 Symbol_table::sized_write_globals(const Input_objects* input_objects,
1819 const Stringpool* sympool,
1820 const Stringpool* dynpool,
1821 Output_file* of) const
1823 const Target* const target = parameters->target();
1825 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1827 const unsigned int output_count = this->output_count_;
1828 const section_size_type oview_size = output_count * sym_size;
1829 const unsigned int first_global_index = this->first_global_index_;
1830 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1832 const unsigned int dynamic_count = this->dynamic_count_;
1833 const section_size_type dynamic_size = dynamic_count * sym_size;
1834 const unsigned int first_dynamic_global_index =
1835 this->first_dynamic_global_index_;
1836 unsigned char* dynamic_view;
1837 if (this->dynamic_offset_ == 0)
1838 dynamic_view = NULL;
1840 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1842 for (Symbol_table_type::const_iterator p = this->table_.begin();
1843 p != this->table_.end();
1846 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1848 // Possibly warn about unresolved symbols in shared libraries.
1849 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
1851 unsigned int sym_index = sym->symtab_index();
1852 unsigned int dynsym_index;
1853 if (dynamic_view == NULL)
1856 dynsym_index = sym->dynsym_index();
1858 if (sym_index == -1U && dynsym_index == -1U)
1860 // This symbol is not included in the output file.
1865 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
1866 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
1867 switch (sym->source())
1869 case Symbol::FROM_OBJECT:
1871 unsigned int in_shndx = sym->shndx();
1873 // FIXME: We need some target specific support here.
1874 if (in_shndx >= elfcpp::SHN_LORESERVE
1875 && in_shndx != elfcpp::SHN_ABS)
1877 gold_error(_("%s: unsupported symbol section 0x%x"),
1878 sym->demangled_name().c_str(), in_shndx);
1883 Object* symobj = sym->object();
1884 if (symobj->is_dynamic())
1886 if (sym->needs_dynsym_value())
1887 dynsym_value = target->dynsym_value(sym);
1888 shndx = elfcpp::SHN_UNDEF;
1890 else if (in_shndx == elfcpp::SHN_UNDEF
1891 || in_shndx == elfcpp::SHN_ABS)
1895 Relobj* relobj = static_cast<Relobj*>(symobj);
1896 section_offset_type secoff;
1897 Output_section* os = relobj->output_section(in_shndx,
1899 gold_assert(os != NULL);
1900 shndx = os->out_shndx();
1902 // In object files symbol values are section
1904 if (parameters->output_is_object())
1905 sym_value -= os->address();
1911 case Symbol::IN_OUTPUT_DATA:
1912 shndx = sym->output_data()->out_shndx();
1915 case Symbol::IN_OUTPUT_SEGMENT:
1916 shndx = elfcpp::SHN_ABS;
1919 case Symbol::CONSTANT:
1920 shndx = elfcpp::SHN_ABS;
1927 if (sym_index != -1U)
1929 sym_index -= first_global_index;
1930 gold_assert(sym_index < output_count);
1931 unsigned char* ps = psyms + (sym_index * sym_size);
1932 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1933 sym, sym_value, shndx, sympool, ps
1934 SELECT_SIZE_ENDIAN(size, big_endian));
1937 if (dynsym_index != -1U)
1939 dynsym_index -= first_dynamic_global_index;
1940 gold_assert(dynsym_index < dynamic_count);
1941 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1942 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1943 sym, dynsym_value, shndx, dynpool, pd
1944 SELECT_SIZE_ENDIAN(size, big_endian));
1948 of->write_output_view(this->offset_, oview_size, psyms);
1949 if (dynamic_view != NULL)
1950 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1953 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1954 // strtab holding the name.
1956 template<int size, bool big_endian>
1958 Symbol_table::sized_write_symbol(
1959 Sized_symbol<size>* sym,
1960 typename elfcpp::Elf_types<size>::Elf_Addr value,
1962 const Stringpool* pool,
1964 ACCEPT_SIZE_ENDIAN) const
1966 elfcpp::Sym_write<size, big_endian> osym(p);
1967 osym.put_st_name(pool->get_offset(sym->name()));
1968 osym.put_st_value(value);
1969 osym.put_st_size(sym->symsize());
1970 // A version script may have overridden the default binding.
1971 if (sym->is_forced_local())
1972 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
1974 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1975 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1976 osym.put_st_shndx(shndx);
1979 // Check for unresolved symbols in shared libraries. This is
1980 // controlled by the --allow-shlib-undefined option.
1982 // We only warn about libraries for which we have seen all the
1983 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1984 // which were not seen in this link. If we didn't see a DT_NEEDED
1985 // entry, we aren't going to be able to reliably report whether the
1986 // symbol is undefined.
1988 // We also don't warn about libraries found in the system library
1989 // directory (the directory were we find libc.so); we assume that
1990 // those libraries are OK. This heuristic avoids problems in
1991 // GNU/Linux, in which -ldl can have undefined references satisfied by
1995 Symbol_table::warn_about_undefined_dynobj_symbol(
1996 const Input_objects* input_objects,
1999 if (sym->source() == Symbol::FROM_OBJECT
2000 && sym->object()->is_dynamic()
2001 && sym->shndx() == elfcpp::SHN_UNDEF
2002 && sym->binding() != elfcpp::STB_WEAK
2003 && !parameters->allow_shlib_undefined()
2004 && !parameters->target()->is_defined_by_abi(sym)
2005 && !input_objects->found_in_system_library_directory(sym->object()))
2007 // A very ugly cast.
2008 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2009 if (!dynobj->has_unknown_needed_entries())
2010 gold_error(_("%s: undefined reference to '%s'"),
2011 sym->object()->name().c_str(),
2012 sym->demangled_name().c_str());
2016 // Write out a section symbol. Return the update offset.
2019 Symbol_table::write_section_symbol(const Output_section *os,
2023 if (parameters->get_size() == 32)
2025 if (parameters->is_big_endian())
2027 #ifdef HAVE_TARGET_32_BIG
2028 this->sized_write_section_symbol<32, true>(os, of, offset);
2035 #ifdef HAVE_TARGET_32_LITTLE
2036 this->sized_write_section_symbol<32, false>(os, of, offset);
2042 else if (parameters->get_size() == 64)
2044 if (parameters->is_big_endian())
2046 #ifdef HAVE_TARGET_64_BIG
2047 this->sized_write_section_symbol<64, true>(os, of, offset);
2054 #ifdef HAVE_TARGET_64_LITTLE
2055 this->sized_write_section_symbol<64, false>(os, of, offset);
2065 // Write out a section symbol, specialized for size and endianness.
2067 template<int size, bool big_endian>
2069 Symbol_table::sized_write_section_symbol(const Output_section* os,
2073 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2075 unsigned char* pov = of->get_output_view(offset, sym_size);
2077 elfcpp::Sym_write<size, big_endian> osym(pov);
2078 osym.put_st_name(0);
2079 osym.put_st_value(os->address());
2080 osym.put_st_size(0);
2081 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2082 elfcpp::STT_SECTION));
2083 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2084 osym.put_st_shndx(os->out_shndx());
2086 of->write_output_view(offset, sym_size, pov);
2089 // Print statistical information to stderr. This is used for --stats.
2092 Symbol_table::print_stats() const
2094 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2095 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2096 program_name, this->table_.size(), this->table_.bucket_count());
2098 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2099 program_name, this->table_.size());
2101 this->namepool_.print_stats("symbol table stringpool");
2104 // We check for ODR violations by looking for symbols with the same
2105 // name for which the debugging information reports that they were
2106 // defined in different source locations. When comparing the source
2107 // location, we consider instances with the same base filename and
2108 // line number to be the same. This is because different object
2109 // files/shared libraries can include the same header file using
2110 // different paths, and we don't want to report an ODR violation in
2113 // This struct is used to compare line information, as returned by
2114 // Dwarf_line_info::one_addr2line. It implements a < comparison
2115 // operator used with std::set.
2117 struct Odr_violation_compare
2120 operator()(const std::string& s1, const std::string& s2) const
2122 std::string::size_type pos1 = s1.rfind('/');
2123 std::string::size_type pos2 = s2.rfind('/');
2124 if (pos1 == std::string::npos
2125 || pos2 == std::string::npos)
2127 return s1.compare(pos1, std::string::npos,
2128 s2, pos2, std::string::npos) < 0;
2132 // Check candidate_odr_violations_ to find symbols with the same name
2133 // but apparently different definitions (different source-file/line-no).
2136 Symbol_table::detect_odr_violations(const Task* task,
2137 const char* output_file_name) const
2139 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2140 it != candidate_odr_violations_.end();
2143 const char* symbol_name = it->first;
2144 // We use a sorted set so the output is deterministic.
2145 std::set<std::string, Odr_violation_compare> line_nums;
2147 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2148 locs = it->second.begin();
2149 locs != it->second.end();
2152 // We need to lock the object in order to read it. This
2153 // means that we have to run in a singleton Task. If we
2154 // want to run this in a general Task for better
2155 // performance, we will need one Task for object, plus
2156 // appropriate locking to ensure that we don't conflict with
2157 // other uses of the object.
2158 Task_lock_obj<Object> tl(task, locs->object);
2159 std::string lineno = Dwarf_line_info::one_addr2line(
2160 locs->object, locs->shndx, locs->offset);
2161 if (!lineno.empty())
2162 line_nums.insert(lineno);
2165 if (line_nums.size() > 1)
2167 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2168 "places (possible ODR violation):"),
2169 output_file_name, demangle(symbol_name).c_str());
2170 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2171 it2 != line_nums.end();
2173 fprintf(stderr, " %s\n", it2->c_str());
2178 // Warnings functions.
2180 // Add a new warning.
2183 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2184 const std::string& warning)
2186 name = symtab->canonicalize_name(name);
2187 this->warnings_[name].set(obj, warning);
2190 // Look through the warnings and mark the symbols for which we should
2191 // warn. This is called during Layout::finalize when we know the
2192 // sources for all the symbols.
2195 Warnings::note_warnings(Symbol_table* symtab)
2197 for (Warning_table::iterator p = this->warnings_.begin();
2198 p != this->warnings_.end();
2201 Symbol* sym = symtab->lookup(p->first, NULL);
2203 && sym->source() == Symbol::FROM_OBJECT
2204 && sym->object() == p->second.object)
2205 sym->set_has_warning();
2209 // Issue a warning. This is called when we see a relocation against a
2210 // symbol for which has a warning.
2212 template<int size, bool big_endian>
2214 Warnings::issue_warning(const Symbol* sym,
2215 const Relocate_info<size, big_endian>* relinfo,
2216 size_t relnum, off_t reloffset) const
2218 gold_assert(sym->has_warning());
2219 Warning_table::const_iterator p = this->warnings_.find(sym->name());
2220 gold_assert(p != this->warnings_.end());
2221 gold_warning_at_location(relinfo, relnum, reloffset,
2222 "%s", p->second.text.c_str());
2225 // Instantiate the templates we need. We could use the configure
2226 // script to restrict this to only the ones needed for implemented
2229 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2232 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2235 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2238 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2241 #ifdef HAVE_TARGET_32_LITTLE
2244 Symbol_table::add_from_relobj<32, false>(
2245 Sized_relobj<32, false>* relobj,
2246 const unsigned char* syms,
2248 const char* sym_names,
2249 size_t sym_name_size,
2250 Sized_relobj<32, true>::Symbols* sympointers);
2253 #ifdef HAVE_TARGET_32_BIG
2256 Symbol_table::add_from_relobj<32, true>(
2257 Sized_relobj<32, true>* relobj,
2258 const unsigned char* syms,
2260 const char* sym_names,
2261 size_t sym_name_size,
2262 Sized_relobj<32, false>::Symbols* sympointers);
2265 #ifdef HAVE_TARGET_64_LITTLE
2268 Symbol_table::add_from_relobj<64, false>(
2269 Sized_relobj<64, false>* relobj,
2270 const unsigned char* syms,
2272 const char* sym_names,
2273 size_t sym_name_size,
2274 Sized_relobj<64, true>::Symbols* sympointers);
2277 #ifdef HAVE_TARGET_64_BIG
2280 Symbol_table::add_from_relobj<64, true>(
2281 Sized_relobj<64, true>* relobj,
2282 const unsigned char* syms,
2284 const char* sym_names,
2285 size_t sym_name_size,
2286 Sized_relobj<64, false>::Symbols* sympointers);
2289 #ifdef HAVE_TARGET_32_LITTLE
2292 Symbol_table::add_from_dynobj<32, false>(
2293 Sized_dynobj<32, false>* dynobj,
2294 const unsigned char* syms,
2296 const char* sym_names,
2297 size_t sym_name_size,
2298 const unsigned char* versym,
2300 const std::vector<const char*>* version_map);
2303 #ifdef HAVE_TARGET_32_BIG
2306 Symbol_table::add_from_dynobj<32, true>(
2307 Sized_dynobj<32, true>* dynobj,
2308 const unsigned char* syms,
2310 const char* sym_names,
2311 size_t sym_name_size,
2312 const unsigned char* versym,
2314 const std::vector<const char*>* version_map);
2317 #ifdef HAVE_TARGET_64_LITTLE
2320 Symbol_table::add_from_dynobj<64, false>(
2321 Sized_dynobj<64, false>* dynobj,
2322 const unsigned char* syms,
2324 const char* sym_names,
2325 size_t sym_name_size,
2326 const unsigned char* versym,
2328 const std::vector<const char*>* version_map);
2331 #ifdef HAVE_TARGET_64_BIG
2334 Symbol_table::add_from_dynobj<64, true>(
2335 Sized_dynobj<64, true>* dynobj,
2336 const unsigned char* syms,
2338 const char* sym_names,
2339 size_t sym_name_size,
2340 const unsigned char* versym,
2342 const std::vector<const char*>* version_map);
2345 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2348 Symbol_table::define_with_copy_reloc<32>(
2349 Sized_symbol<32>* sym,
2351 elfcpp::Elf_types<32>::Elf_Addr value);
2354 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2357 Symbol_table::define_with_copy_reloc<64>(
2358 Sized_symbol<64>* sym,
2360 elfcpp::Elf_types<64>::Elf_Addr value);
2363 #ifdef HAVE_TARGET_32_LITTLE
2366 Warnings::issue_warning<32, false>(const Symbol* sym,
2367 const Relocate_info<32, false>* relinfo,
2368 size_t relnum, off_t reloffset) const;
2371 #ifdef HAVE_TARGET_32_BIG
2374 Warnings::issue_warning<32, true>(const Symbol* sym,
2375 const Relocate_info<32, true>* relinfo,
2376 size_t relnum, off_t reloffset) const;
2379 #ifdef HAVE_TARGET_64_LITTLE
2382 Warnings::issue_warning<64, false>(const Symbol* sym,
2383 const Relocate_info<64, false>* relinfo,
2384 size_t relnum, off_t reloffset) const;
2387 #ifdef HAVE_TARGET_64_BIG
2390 Warnings::issue_warning<64, true>(const Symbol* sym,
2391 const Relocate_info<64, true>* relinfo,
2392 size_t relnum, off_t reloffset) const;
2395 } // End namespace gold.