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.
34 #include "dwarf_reader.h"
38 #include "workqueue.h"
46 // Initialize fields in Symbol. This initializes everything except u_
50 Symbol::init_fields(const char* name, const char* version,
51 elfcpp::STT type, elfcpp::STB binding,
52 elfcpp::STV visibility, unsigned char nonvis)
55 this->version_ = version;
56 this->symtab_index_ = 0;
57 this->dynsym_index_ = 0;
58 this->got_offsets_.init();
59 this->plt_offset_ = 0;
61 this->binding_ = binding;
62 this->visibility_ = visibility;
63 this->nonvis_ = nonvis;
64 this->is_target_special_ = false;
65 this->is_def_ = false;
66 this->is_forwarder_ = false;
67 this->has_alias_ = false;
68 this->needs_dynsym_entry_ = false;
69 this->in_reg_ = false;
70 this->in_dyn_ = false;
71 this->has_plt_offset_ = false;
72 this->has_warning_ = false;
73 this->is_copied_from_dynobj_ = false;
74 this->is_forced_local_ = false;
75 this->is_ordinary_shndx_ = false;
78 // Return the demangled version of the symbol's name, but only
79 // if the --demangle flag was set.
82 demangle(const char* name)
84 if (!parameters->options().do_demangle())
87 // cplus_demangle allocates memory for the result it returns,
88 // and returns NULL if the name is already demangled.
89 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
90 if (demangled_name == NULL)
93 std::string retval(demangled_name);
99 Symbol::demangled_name() const
101 return demangle(this->name());
104 // Initialize the fields in the base class Symbol for SYM in OBJECT.
106 template<int size, bool big_endian>
108 Symbol::init_base(const char* name, const char* version, Object* object,
109 const elfcpp::Sym<size, big_endian>& sym,
110 unsigned int st_shndx, bool is_ordinary)
112 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
113 sym.get_st_visibility(), sym.get_st_nonvis());
114 this->u_.from_object.object = object;
115 this->u_.from_object.shndx = st_shndx;
116 this->is_ordinary_shndx_ = is_ordinary;
117 this->source_ = FROM_OBJECT;
118 this->in_reg_ = !object->is_dynamic();
119 this->in_dyn_ = object->is_dynamic();
122 // Initialize the fields in the base class Symbol for a symbol defined
123 // in an Output_data.
126 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
127 elfcpp::STB binding, elfcpp::STV visibility,
128 unsigned char nonvis, bool offset_is_from_end)
130 this->init_fields(name, NULL, type, binding, visibility, nonvis);
131 this->u_.in_output_data.output_data = od;
132 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
133 this->source_ = IN_OUTPUT_DATA;
134 this->in_reg_ = true;
137 // Initialize the fields in the base class Symbol for a symbol defined
138 // in an Output_segment.
141 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
142 elfcpp::STB binding, elfcpp::STV visibility,
143 unsigned char nonvis, Segment_offset_base offset_base)
145 this->init_fields(name, NULL, type, binding, visibility, nonvis);
146 this->u_.in_output_segment.output_segment = os;
147 this->u_.in_output_segment.offset_base = offset_base;
148 this->source_ = IN_OUTPUT_SEGMENT;
149 this->in_reg_ = true;
152 // Initialize the fields in the base class Symbol for a symbol defined
156 Symbol::init_base(const char* name, elfcpp::STT type,
157 elfcpp::STB binding, elfcpp::STV visibility,
158 unsigned char nonvis)
160 this->init_fields(name, NULL, type, binding, visibility, nonvis);
161 this->source_ = CONSTANT;
162 this->in_reg_ = true;
165 // Allocate a common symbol in the base.
168 Symbol::allocate_base_common(Output_data* od)
170 gold_assert(this->is_common());
171 this->source_ = IN_OUTPUT_DATA;
172 this->u_.in_output_data.output_data = od;
173 this->u_.in_output_data.offset_is_from_end = false;
176 // Initialize the fields in Sized_symbol for SYM in OBJECT.
179 template<bool big_endian>
181 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
182 const elfcpp::Sym<size, big_endian>& sym,
183 unsigned int st_shndx, bool is_ordinary)
185 this->init_base(name, version, object, sym, st_shndx, is_ordinary);
186 this->value_ = sym.get_st_value();
187 this->symsize_ = sym.get_st_size();
190 // Initialize the fields in Sized_symbol for a symbol defined in an
195 Sized_symbol<size>::init(const char* name, Output_data* od,
196 Value_type value, Size_type symsize,
197 elfcpp::STT type, elfcpp::STB binding,
198 elfcpp::STV visibility, unsigned char nonvis,
199 bool offset_is_from_end)
201 this->init_base(name, od, type, binding, visibility, nonvis,
203 this->value_ = value;
204 this->symsize_ = symsize;
207 // Initialize the fields in Sized_symbol for a symbol defined in an
212 Sized_symbol<size>::init(const char* name, Output_segment* os,
213 Value_type value, Size_type symsize,
214 elfcpp::STT type, elfcpp::STB binding,
215 elfcpp::STV visibility, unsigned char nonvis,
216 Segment_offset_base offset_base)
218 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
219 this->value_ = value;
220 this->symsize_ = symsize;
223 // Initialize the fields in Sized_symbol for a symbol defined as a
228 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
229 elfcpp::STT type, elfcpp::STB binding,
230 elfcpp::STV visibility, unsigned char nonvis)
232 this->init_base(name, type, binding, visibility, nonvis);
233 this->value_ = value;
234 this->symsize_ = symsize;
237 // Allocate a common symbol.
241 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
243 this->allocate_base_common(od);
244 this->value_ = value;
247 // Return true if this symbol should be added to the dynamic symbol
251 Symbol::should_add_dynsym_entry() const
253 // If the symbol is used by a dynamic relocation, we need to add it.
254 if (this->needs_dynsym_entry())
257 // If the symbol was forced local in a version script, do not add it.
258 if (this->is_forced_local())
261 // If exporting all symbols or building a shared library,
262 // and the symbol is defined in a regular object and is
263 // externally visible, we need to add it.
264 if ((parameters->options().export_dynamic() || parameters->options().shared())
265 && !this->is_from_dynobj()
266 && this->is_externally_visible())
272 // Return true if the final value of this symbol is known at link
276 Symbol::final_value_is_known() const
278 // If we are not generating an executable, then no final values are
279 // known, since they will change at runtime.
280 if (parameters->options().shared() || parameters->options().relocatable())
283 // If the symbol is not from an object file, then it is defined, and
285 if (this->source_ != FROM_OBJECT)
288 // If the symbol is from a dynamic object, then the final value is
290 if (this->object()->is_dynamic())
293 // If the symbol is not undefined (it is defined or common), then
294 // the final value is known.
295 if (!this->is_undefined())
298 // If the symbol is undefined, then whether the final value is known
299 // depends on whether we are doing a static link. If we are doing a
300 // dynamic link, then the final value could be filled in at runtime.
301 // This could reasonably be the case for a weak undefined symbol.
302 return parameters->doing_static_link();
305 // Return the output section where this symbol is defined.
308 Symbol::output_section() const
310 switch (this->source_)
314 unsigned int shndx = this->u_.from_object.shndx;
315 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
317 gold_assert(!this->u_.from_object.object->is_dynamic());
318 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
319 section_offset_type dummy;
320 return relobj->output_section(shndx, &dummy);
326 return this->u_.in_output_data.output_data->output_section();
328 case IN_OUTPUT_SEGMENT:
337 // Set the symbol's output section. This is used for symbols defined
338 // in scripts. This should only be called after the symbol table has
342 Symbol::set_output_section(Output_section* os)
344 switch (this->source_)
348 gold_assert(this->output_section() == os);
351 this->source_ = IN_OUTPUT_DATA;
352 this->u_.in_output_data.output_data = os;
353 this->u_.in_output_data.offset_is_from_end = false;
355 case IN_OUTPUT_SEGMENT:
361 // Class Symbol_table.
363 Symbol_table::Symbol_table(unsigned int count,
364 const Version_script_info& version_script)
365 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
366 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
367 version_script_(version_script)
369 namepool_.reserve(count);
372 Symbol_table::~Symbol_table()
376 // The hash function. The key values are Stringpool keys.
379 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
381 return key.first ^ key.second;
384 // The symbol table key equality function. This is called with
388 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
389 const Symbol_table_key& k2) const
391 return k1.first == k2.first && k1.second == k2.second;
394 // Make TO a symbol which forwards to FROM.
397 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
399 gold_assert(from != to);
400 gold_assert(!from->is_forwarder() && !to->is_forwarder());
401 this->forwarders_[from] = to;
402 from->set_forwarder();
405 // Resolve the forwards from FROM, returning the real symbol.
408 Symbol_table::resolve_forwards(const Symbol* from) const
410 gold_assert(from->is_forwarder());
411 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
412 this->forwarders_.find(from);
413 gold_assert(p != this->forwarders_.end());
417 // Look up a symbol by name.
420 Symbol_table::lookup(const char* name, const char* version) const
422 Stringpool::Key name_key;
423 name = this->namepool_.find(name, &name_key);
427 Stringpool::Key version_key = 0;
430 version = this->namepool_.find(version, &version_key);
435 Symbol_table_key key(name_key, version_key);
436 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
437 if (p == this->table_.end())
442 // Resolve a Symbol with another Symbol. This is only used in the
443 // unusual case where there are references to both an unversioned
444 // symbol and a symbol with a version, and we then discover that that
445 // version is the default version. Because this is unusual, we do
446 // this the slow way, by converting back to an ELF symbol.
448 template<int size, bool big_endian>
450 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
453 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
454 elfcpp::Sym_write<size, big_endian> esym(buf);
455 // We don't bother to set the st_name or the st_shndx field.
456 esym.put_st_value(from->value());
457 esym.put_st_size(from->symsize());
458 esym.put_st_info(from->binding(), from->type());
459 esym.put_st_other(from->visibility(), from->nonvis());
461 unsigned int shndx = from->shndx(&is_ordinary);
462 this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
470 // Record that a symbol is forced to be local by a version script.
473 Symbol_table::force_local(Symbol* sym)
475 if (!sym->is_defined() && !sym->is_common())
477 if (sym->is_forced_local())
479 // We already got this one.
482 sym->set_is_forced_local();
483 this->forced_locals_.push_back(sym);
486 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
487 // is only called for undefined symbols, when at least one --wrap
491 Symbol_table::wrap_symbol(Object* object, const char* name,
492 Stringpool::Key* name_key)
494 // For some targets, we need to ignore a specific character when
495 // wrapping, and add it back later.
497 if (name[0] == object->target()->wrap_char())
503 if (parameters->options().is_wrap(name))
505 // Turn NAME into __wrap_NAME.
512 // This will give us both the old and new name in NAMEPOOL_, but
513 // that is OK. Only the versions we need will wind up in the
514 // real string table in the output file.
515 return this->namepool_.add(s.c_str(), true, name_key);
518 const char* const real_prefix = "__real_";
519 const size_t real_prefix_length = strlen(real_prefix);
520 if (strncmp(name, real_prefix, real_prefix_length) == 0
521 && parameters->options().is_wrap(name + real_prefix_length))
523 // Turn __real_NAME into NAME.
527 s += name + real_prefix_length;
528 return this->namepool_.add(s.c_str(), true, name_key);
534 // Add one symbol from OBJECT to the symbol table. NAME is symbol
535 // name and VERSION is the version; both are canonicalized. DEF is
536 // whether this is the default version. ST_SHNDX is the symbol's
537 // section index; IS_ORDINARY is whether this is a normal section
538 // rather than a special code.
540 // If DEF is true, then this is the definition of a default version of
541 // a symbol. That means that any lookup of NAME/NULL and any lookup
542 // of NAME/VERSION should always return the same symbol. This is
543 // obvious for references, but in particular we want to do this for
544 // definitions: overriding NAME/NULL should also override
545 // NAME/VERSION. If we don't do that, it would be very hard to
546 // override functions in a shared library which uses versioning.
548 // We implement this by simply making both entries in the hash table
549 // point to the same Symbol structure. That is easy enough if this is
550 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
551 // that we have seen both already, in which case they will both have
552 // independent entries in the symbol table. We can't simply change
553 // the symbol table entry, because we have pointers to the entries
554 // attached to the object files. So we mark the entry attached to the
555 // object file as a forwarder, and record it in the forwarders_ map.
556 // Note that entries in the hash table will never be marked as
559 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
560 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
561 // for a special section code. ST_SHNDX may be modified if the symbol
562 // is defined in a section being discarded.
564 template<int size, bool big_endian>
566 Symbol_table::add_from_object(Object* object,
568 Stringpool::Key name_key,
570 Stringpool::Key version_key,
572 const elfcpp::Sym<size, big_endian>& sym,
573 unsigned int st_shndx,
575 unsigned int orig_st_shndx)
577 // Print a message if this symbol is being traced.
578 if (parameters->options().is_trace_symbol(name))
580 if (orig_st_shndx == elfcpp::SHN_UNDEF)
581 gold_info(_("%s: reference to %s"), object->name().c_str(), name);
583 gold_info(_("%s: definition of %s"), object->name().c_str(), name);
586 // For an undefined symbol, we may need to adjust the name using
588 if (orig_st_shndx == elfcpp::SHN_UNDEF
589 && parameters->options().any_wrap())
591 const char* wrap_name = this->wrap_symbol(object, name, &name_key);
592 if (wrap_name != name)
594 // If we see a reference to malloc with version GLIBC_2.0,
595 // and we turn it into a reference to __wrap_malloc, then we
596 // discard the version number. Otherwise the user would be
597 // required to specify the correct version for
605 Symbol* const snull = NULL;
606 std::pair<typename Symbol_table_type::iterator, bool> ins =
607 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
610 std::pair<typename Symbol_table_type::iterator, bool> insdef =
611 std::make_pair(this->table_.end(), false);
614 const Stringpool::Key vnull_key = 0;
615 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
620 // ins.first: an iterator, which is a pointer to a pair.
621 // ins.first->first: the key (a pair of name and version).
622 // ins.first->second: the value (Symbol*).
623 // ins.second: true if new entry was inserted, false if not.
625 Sized_symbol<size>* ret;
630 // We already have an entry for NAME/VERSION.
631 ret = this->get_sized_symbol<size>(ins.first->second);
632 gold_assert(ret != NULL);
634 was_undefined = ret->is_undefined();
635 was_common = ret->is_common();
637 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
644 // This is the first time we have seen NAME/NULL. Make
645 // NAME/NULL point to NAME/VERSION.
646 insdef.first->second = ret;
648 else if (insdef.first->second != ret
649 && insdef.first->second->is_undefined())
651 // This is the unfortunate case where we already have
652 // entries for both NAME/VERSION and NAME/NULL. Note
653 // that we don't want to combine them if the existing
654 // symbol is going to override the new one. FIXME: We
655 // currently just test is_undefined, but this may not do
656 // the right thing if the existing symbol is from a
657 // shared library and the new one is from a regular
660 const Sized_symbol<size>* sym2;
661 sym2 = this->get_sized_symbol<size>(insdef.first->second);
662 Symbol_table::resolve<size, big_endian>(ret, sym2, version);
663 this->make_forwarder(insdef.first->second, ret);
664 insdef.first->second = ret;
672 // This is the first time we have seen NAME/VERSION.
673 gold_assert(ins.first->second == NULL);
675 if (def && !insdef.second)
677 // We already have an entry for NAME/NULL. If we override
678 // it, then change it to NAME/VERSION.
679 ret = this->get_sized_symbol<size>(insdef.first->second);
681 was_undefined = ret->is_undefined();
682 was_common = ret->is_common();
684 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
686 ins.first->second = ret;
690 was_undefined = false;
693 Sized_target<size, big_endian>* target =
694 object->sized_target<size, big_endian>();
695 if (!target->has_make_symbol())
696 ret = new Sized_symbol<size>();
699 ret = target->make_symbol();
702 // This means that we don't want a symbol table
705 this->table_.erase(ins.first);
708 this->table_.erase(insdef.first);
709 // Inserting insdef invalidated ins.
710 this->table_.erase(std::make_pair(name_key,
717 ret->init(name, version, object, sym, st_shndx, is_ordinary);
719 ins.first->second = ret;
722 // This is the first time we have seen NAME/NULL. Point
723 // it at the new entry for NAME/VERSION.
724 gold_assert(insdef.second);
725 insdef.first->second = ret;
730 // Record every time we see a new undefined symbol, to speed up
732 if (!was_undefined && ret->is_undefined())
733 ++this->saw_undefined_;
735 // Keep track of common symbols, to speed up common symbol
737 if (!was_common && ret->is_common())
739 if (ret->type() != elfcpp::STT_TLS)
740 this->commons_.push_back(ret);
742 this->tls_commons_.push_back(ret);
746 ret->set_is_default();
750 // Add all the symbols in a relocatable object to the hash table.
752 template<int size, bool big_endian>
754 Symbol_table::add_from_relobj(
755 Sized_relobj<size, big_endian>* relobj,
756 const unsigned char* syms,
758 size_t symndx_offset,
759 const char* sym_names,
760 size_t sym_name_size,
761 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
763 gold_assert(size == relobj->target()->get_size());
764 gold_assert(size == parameters->target().get_size());
766 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
768 const bool just_symbols = relobj->just_symbols();
770 const unsigned char* p = syms;
771 for (size_t i = 0; i < count; ++i, p += sym_size)
773 elfcpp::Sym<size, big_endian> sym(p);
775 unsigned int st_name = sym.get_st_name();
776 if (st_name >= sym_name_size)
778 relobj->error(_("bad global symbol name offset %u at %zu"),
783 const char* name = sym_names + st_name;
786 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
789 unsigned int orig_st_shndx = st_shndx;
791 orig_st_shndx = elfcpp::SHN_UNDEF;
793 // A symbol defined in a section which we are not including must
794 // be treated as an undefined symbol.
795 if (st_shndx != elfcpp::SHN_UNDEF
797 && !relobj->is_section_included(st_shndx))
798 st_shndx = elfcpp::SHN_UNDEF;
800 // In an object file, an '@' in the name separates the symbol
801 // name from the version name. If there are two '@' characters,
802 // this is the default version.
803 const char* ver = strchr(name, '@');
805 // DEF: is the version default? LOCAL: is the symbol forced local?
811 // The symbol name is of the form foo@VERSION or foo@@VERSION
812 namelen = ver - name;
820 // We don't want to assign a version to an undefined symbol,
821 // even if it is listed in the version script. FIXME: What
822 // about a common symbol?
823 else if (!version_script_.empty()
824 && st_shndx != elfcpp::SHN_UNDEF)
826 // The symbol name did not have a version, but
827 // the version script may assign a version anyway.
828 namelen = strlen(name);
830 // Check the global: entries from the version script.
831 const std::string& version =
832 version_script_.get_symbol_version(name);
833 if (!version.empty())
834 ver = version.c_str();
835 // Check the local: entries from the version script
836 if (version_script_.symbol_is_local(name))
840 elfcpp::Sym<size, big_endian>* psym = &sym;
841 unsigned char symbuf[sym_size];
842 elfcpp::Sym<size, big_endian> sym2(symbuf);
845 memcpy(symbuf, p, sym_size);
846 elfcpp::Sym_write<size, big_endian> sw(symbuf);
847 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
849 // Symbol values in object files are section relative.
850 // This is normally what we want, but since here we are
851 // converting the symbol to absolute we need to add the
852 // section address. The section address in an object
853 // file is normally zero, but people can use a linker
854 // script to change it.
855 sw.put_st_value(sym.get_st_value()
856 + relobj->section_address(orig_st_shndx));
858 st_shndx = elfcpp::SHN_ABS;
863 Sized_symbol<size>* res;
866 Stringpool::Key name_key;
867 name = this->namepool_.add(name, true, &name_key);
868 res = this->add_from_object(relobj, name, name_key, NULL, 0,
869 false, *psym, st_shndx, is_ordinary,
872 this->force_local(res);
876 Stringpool::Key name_key;
877 name = this->namepool_.add_with_length(name, namelen, true,
879 Stringpool::Key ver_key;
880 ver = this->namepool_.add(ver, true, &ver_key);
882 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
883 def, *psym, st_shndx, is_ordinary,
887 (*sympointers)[i] = res;
891 // Add all the symbols in a dynamic object to the hash table.
893 template<int size, bool big_endian>
895 Symbol_table::add_from_dynobj(
896 Sized_dynobj<size, big_endian>* dynobj,
897 const unsigned char* syms,
899 const char* sym_names,
900 size_t sym_name_size,
901 const unsigned char* versym,
903 const std::vector<const char*>* version_map)
905 gold_assert(size == dynobj->target()->get_size());
906 gold_assert(size == parameters->target().get_size());
908 if (dynobj->just_symbols())
910 gold_error(_("--just-symbols does not make sense with a shared object"));
914 if (versym != NULL && versym_size / 2 < count)
916 dynobj->error(_("too few symbol versions"));
920 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
922 // We keep a list of all STT_OBJECT symbols, so that we can resolve
923 // weak aliases. This is necessary because if the dynamic object
924 // provides the same variable under two names, one of which is a
925 // weak definition, and the regular object refers to the weak
926 // definition, we have to put both the weak definition and the
927 // strong definition into the dynamic symbol table. Given a weak
928 // definition, the only way that we can find the corresponding
929 // strong definition, if any, is to search the symbol table.
930 std::vector<Sized_symbol<size>*> object_symbols;
932 const unsigned char* p = syms;
933 const unsigned char* vs = versym;
934 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
936 elfcpp::Sym<size, big_endian> sym(p);
938 // Ignore symbols with local binding or that have
939 // internal or hidden visibility.
940 if (sym.get_st_bind() == elfcpp::STB_LOCAL
941 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
942 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
945 // A protected symbol in a shared library must be treated as a
946 // normal symbol when viewed from outside the shared library.
947 // Implement this by overriding the visibility here.
948 elfcpp::Sym<size, big_endian>* psym = &sym;
949 unsigned char symbuf[sym_size];
950 elfcpp::Sym<size, big_endian> sym2(symbuf);
951 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
953 memcpy(symbuf, p, sym_size);
954 elfcpp::Sym_write<size, big_endian> sw(symbuf);
955 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
959 unsigned int st_name = psym->get_st_name();
960 if (st_name >= sym_name_size)
962 dynobj->error(_("bad symbol name offset %u at %zu"),
967 const char* name = sym_names + st_name;
970 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
973 Sized_symbol<size>* res;
977 Stringpool::Key name_key;
978 name = this->namepool_.add(name, true, &name_key);
979 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
980 false, *psym, st_shndx, is_ordinary,
985 // Read the version information.
987 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
989 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
990 v &= elfcpp::VERSYM_VERSION;
992 // The Sun documentation says that V can be VER_NDX_LOCAL,
993 // or VER_NDX_GLOBAL, or a version index. The meaning of
994 // VER_NDX_LOCAL is defined as "Symbol has local scope."
995 // The old GNU linker will happily generate VER_NDX_LOCAL
996 // for an undefined symbol. I don't know what the Sun
997 // linker will generate.
999 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1000 && st_shndx != elfcpp::SHN_UNDEF)
1002 // This symbol should not be visible outside the object.
1006 // At this point we are definitely going to add this symbol.
1007 Stringpool::Key name_key;
1008 name = this->namepool_.add(name, true, &name_key);
1010 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1011 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1013 // This symbol does not have a version.
1014 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1015 false, *psym, st_shndx, is_ordinary,
1020 if (v >= version_map->size())
1022 dynobj->error(_("versym for symbol %zu out of range: %u"),
1027 const char* version = (*version_map)[v];
1028 if (version == NULL)
1030 dynobj->error(_("versym for symbol %zu has no name: %u"),
1035 Stringpool::Key version_key;
1036 version = this->namepool_.add(version, true, &version_key);
1038 // If this is an absolute symbol, and the version name
1039 // and symbol name are the same, then this is the
1040 // version definition symbol. These symbols exist to
1041 // support using -u to pull in particular versions. We
1042 // do not want to record a version for them.
1043 if (st_shndx == elfcpp::SHN_ABS
1045 && name_key == version_key)
1046 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1047 false, *psym, st_shndx, is_ordinary,
1051 const bool def = (!hidden
1052 && st_shndx != elfcpp::SHN_UNDEF);
1053 res = this->add_from_object(dynobj, name, name_key, version,
1054 version_key, def, *psym, st_shndx,
1055 is_ordinary, st_shndx);
1060 // Note that it is possible that RES was overridden by an
1061 // earlier object, in which case it can't be aliased here.
1062 if (st_shndx != elfcpp::SHN_UNDEF
1064 && psym->get_st_type() == elfcpp::STT_OBJECT
1065 && res->source() == Symbol::FROM_OBJECT
1066 && res->object() == dynobj)
1067 object_symbols.push_back(res);
1070 this->record_weak_aliases(&object_symbols);
1073 // This is used to sort weak aliases. We sort them first by section
1074 // index, then by offset, then by weak ahead of strong.
1077 class Weak_alias_sorter
1080 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1085 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1086 const Sized_symbol<size>* s2) const
1089 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1090 gold_assert(is_ordinary);
1091 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1092 gold_assert(is_ordinary);
1093 if (s1_shndx != s2_shndx)
1094 return s1_shndx < s2_shndx;
1096 if (s1->value() != s2->value())
1097 return s1->value() < s2->value();
1098 if (s1->binding() != s2->binding())
1100 if (s1->binding() == elfcpp::STB_WEAK)
1102 if (s2->binding() == elfcpp::STB_WEAK)
1105 return std::string(s1->name()) < std::string(s2->name());
1108 // SYMBOLS is a list of object symbols from a dynamic object. Look
1109 // for any weak aliases, and record them so that if we add the weak
1110 // alias to the dynamic symbol table, we also add the corresponding
1115 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1117 // Sort the vector by section index, then by offset, then by weak
1119 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1121 // Walk through the vector. For each weak definition, record
1123 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1125 p != symbols->end();
1128 if ((*p)->binding() != elfcpp::STB_WEAK)
1131 // Build a circular list of weak aliases. Each symbol points to
1132 // the next one in the circular list.
1134 Sized_symbol<size>* from_sym = *p;
1135 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1136 for (q = p + 1; q != symbols->end(); ++q)
1139 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1140 || (*q)->value() != from_sym->value())
1143 this->weak_aliases_[from_sym] = *q;
1144 from_sym->set_has_alias();
1150 this->weak_aliases_[from_sym] = *p;
1151 from_sym->set_has_alias();
1158 // Create and return a specially defined symbol. If ONLY_IF_REF is
1159 // true, then only create the symbol if there is a reference to it.
1160 // If this does not return NULL, it sets *POLDSYM to the existing
1161 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1163 template<int size, bool big_endian>
1165 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1167 Sized_symbol<size>** poldsym)
1170 Sized_symbol<size>* sym;
1171 bool add_to_table = false;
1172 typename Symbol_table_type::iterator add_loc = this->table_.end();
1174 // If the caller didn't give us a version, see if we get one from
1175 // the version script.
1176 if (*pversion == NULL)
1178 const std::string& v(this->version_script_.get_symbol_version(*pname));
1180 *pversion = v.c_str();
1185 oldsym = this->lookup(*pname, *pversion);
1186 if (oldsym == NULL || !oldsym->is_undefined())
1189 *pname = oldsym->name();
1190 *pversion = oldsym->version();
1194 // Canonicalize NAME and VERSION.
1195 Stringpool::Key name_key;
1196 *pname = this->namepool_.add(*pname, true, &name_key);
1198 Stringpool::Key version_key = 0;
1199 if (*pversion != NULL)
1200 *pversion = this->namepool_.add(*pversion, true, &version_key);
1202 Symbol* const snull = NULL;
1203 std::pair<typename Symbol_table_type::iterator, bool> ins =
1204 this->table_.insert(std::make_pair(std::make_pair(name_key,
1210 // We already have a symbol table entry for NAME/VERSION.
1211 oldsym = ins.first->second;
1212 gold_assert(oldsym != NULL);
1216 // We haven't seen this symbol before.
1217 gold_assert(ins.first->second == NULL);
1218 add_to_table = true;
1219 add_loc = ins.first;
1224 const Target& target = parameters->target();
1225 if (!target.has_make_symbol())
1226 sym = new Sized_symbol<size>();
1229 gold_assert(target.get_size() == size);
1230 gold_assert(target.is_big_endian() ? big_endian : !big_endian);
1231 typedef Sized_target<size, big_endian> My_target;
1232 const My_target* sized_target =
1233 static_cast<const My_target*>(&target);
1234 sym = sized_target->make_symbol();
1240 add_loc->second = sym;
1242 gold_assert(oldsym != NULL);
1244 *poldsym = this->get_sized_symbol<size>(oldsym);
1249 // Define a symbol based on an Output_data.
1252 Symbol_table::define_in_output_data(const char* name,
1253 const char* version,
1258 elfcpp::STB binding,
1259 elfcpp::STV visibility,
1260 unsigned char nonvis,
1261 bool offset_is_from_end,
1264 if (parameters->target().get_size() == 32)
1266 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1267 return this->do_define_in_output_data<32>(name, version, od,
1268 value, symsize, type, binding,
1276 else if (parameters->target().get_size() == 64)
1278 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1279 return this->do_define_in_output_data<64>(name, version, od,
1280 value, symsize, type, binding,
1292 // Define a symbol in an Output_data, sized version.
1296 Symbol_table::do_define_in_output_data(
1298 const char* version,
1300 typename elfcpp::Elf_types<size>::Elf_Addr value,
1301 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1303 elfcpp::STB binding,
1304 elfcpp::STV visibility,
1305 unsigned char nonvis,
1306 bool offset_is_from_end,
1309 Sized_symbol<size>* sym;
1310 Sized_symbol<size>* oldsym;
1312 if (parameters->target().is_big_endian())
1314 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1315 sym = this->define_special_symbol<size, true>(&name, &version,
1316 only_if_ref, &oldsym);
1323 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1324 sym = this->define_special_symbol<size, false>(&name, &version,
1325 only_if_ref, &oldsym);
1334 gold_assert(version == NULL || oldsym != NULL);
1335 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1336 offset_is_from_end);
1340 if (binding == elfcpp::STB_LOCAL
1341 || this->version_script_.symbol_is_local(name))
1342 this->force_local(sym);
1346 if (Symbol_table::should_override_with_special(oldsym))
1347 this->override_with_special(oldsym, sym);
1352 // Define a symbol based on an Output_segment.
1355 Symbol_table::define_in_output_segment(const char* name,
1356 const char* version, Output_segment* os,
1360 elfcpp::STB binding,
1361 elfcpp::STV visibility,
1362 unsigned char nonvis,
1363 Symbol::Segment_offset_base offset_base,
1366 if (parameters->target().get_size() == 32)
1368 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1369 return this->do_define_in_output_segment<32>(name, version, os,
1370 value, symsize, type,
1371 binding, visibility, nonvis,
1372 offset_base, only_if_ref);
1377 else if (parameters->target().get_size() == 64)
1379 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1380 return this->do_define_in_output_segment<64>(name, version, os,
1381 value, symsize, type,
1382 binding, visibility, nonvis,
1383 offset_base, only_if_ref);
1392 // Define a symbol in an Output_segment, sized version.
1396 Symbol_table::do_define_in_output_segment(
1398 const char* version,
1400 typename elfcpp::Elf_types<size>::Elf_Addr value,
1401 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1403 elfcpp::STB binding,
1404 elfcpp::STV visibility,
1405 unsigned char nonvis,
1406 Symbol::Segment_offset_base offset_base,
1409 Sized_symbol<size>* sym;
1410 Sized_symbol<size>* oldsym;
1412 if (parameters->target().is_big_endian())
1414 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1415 sym = this->define_special_symbol<size, true>(&name, &version,
1416 only_if_ref, &oldsym);
1423 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1424 sym = this->define_special_symbol<size, false>(&name, &version,
1425 only_if_ref, &oldsym);
1434 gold_assert(version == NULL || oldsym != NULL);
1435 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1440 if (binding == elfcpp::STB_LOCAL
1441 || this->version_script_.symbol_is_local(name))
1442 this->force_local(sym);
1446 if (Symbol_table::should_override_with_special(oldsym))
1447 this->override_with_special(oldsym, sym);
1452 // Define a special symbol with a constant value. It is a multiple
1453 // definition error if this symbol is already defined.
1456 Symbol_table::define_as_constant(const char* name,
1457 const char* version,
1461 elfcpp::STB binding,
1462 elfcpp::STV visibility,
1463 unsigned char nonvis,
1465 bool force_override)
1467 if (parameters->target().get_size() == 32)
1469 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1470 return this->do_define_as_constant<32>(name, version, value,
1471 symsize, type, binding,
1472 visibility, nonvis, only_if_ref,
1478 else if (parameters->target().get_size() == 64)
1480 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1481 return this->do_define_as_constant<64>(name, version, value,
1482 symsize, type, binding,
1483 visibility, nonvis, only_if_ref,
1493 // Define a symbol as a constant, sized version.
1497 Symbol_table::do_define_as_constant(
1499 const char* version,
1500 typename elfcpp::Elf_types<size>::Elf_Addr value,
1501 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1503 elfcpp::STB binding,
1504 elfcpp::STV visibility,
1505 unsigned char nonvis,
1507 bool force_override)
1509 Sized_symbol<size>* sym;
1510 Sized_symbol<size>* oldsym;
1512 if (parameters->target().is_big_endian())
1514 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1515 sym = this->define_special_symbol<size, true>(&name, &version,
1516 only_if_ref, &oldsym);
1523 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1524 sym = this->define_special_symbol<size, false>(&name, &version,
1525 only_if_ref, &oldsym);
1534 gold_assert(version == NULL || version == name || oldsym != NULL);
1535 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1539 // Version symbols are absolute symbols with name == version.
1540 // We don't want to force them to be local.
1541 if ((version == NULL
1544 && (binding == elfcpp::STB_LOCAL
1545 || this->version_script_.symbol_is_local(name)))
1546 this->force_local(sym);
1550 if (force_override || Symbol_table::should_override_with_special(oldsym))
1551 this->override_with_special(oldsym, sym);
1556 // Define a set of symbols in output sections.
1559 Symbol_table::define_symbols(const Layout* layout, int count,
1560 const Define_symbol_in_section* p,
1563 for (int i = 0; i < count; ++i, ++p)
1565 Output_section* os = layout->find_output_section(p->output_section);
1567 this->define_in_output_data(p->name, NULL, os, p->value,
1568 p->size, p->type, p->binding,
1569 p->visibility, p->nonvis,
1570 p->offset_is_from_end,
1571 only_if_ref || p->only_if_ref);
1573 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1574 p->binding, p->visibility, p->nonvis,
1575 only_if_ref || p->only_if_ref,
1580 // Define a set of symbols in output segments.
1583 Symbol_table::define_symbols(const Layout* layout, int count,
1584 const Define_symbol_in_segment* p,
1587 for (int i = 0; i < count; ++i, ++p)
1589 Output_segment* os = layout->find_output_segment(p->segment_type,
1590 p->segment_flags_set,
1591 p->segment_flags_clear);
1593 this->define_in_output_segment(p->name, NULL, os, p->value,
1594 p->size, p->type, p->binding,
1595 p->visibility, p->nonvis,
1597 only_if_ref || p->only_if_ref);
1599 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1600 p->binding, p->visibility, p->nonvis,
1601 only_if_ref || p->only_if_ref,
1606 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1607 // symbol should be defined--typically a .dyn.bss section. VALUE is
1608 // the offset within POSD.
1612 Symbol_table::define_with_copy_reloc(
1613 Sized_symbol<size>* csym,
1615 typename elfcpp::Elf_types<size>::Elf_Addr value)
1617 gold_assert(csym->is_from_dynobj());
1618 gold_assert(!csym->is_copied_from_dynobj());
1619 Object* object = csym->object();
1620 gold_assert(object->is_dynamic());
1621 Dynobj* dynobj = static_cast<Dynobj*>(object);
1623 // Our copied variable has to override any variable in a shared
1625 elfcpp::STB binding = csym->binding();
1626 if (binding == elfcpp::STB_WEAK)
1627 binding = elfcpp::STB_GLOBAL;
1629 this->define_in_output_data(csym->name(), csym->version(),
1630 posd, value, csym->symsize(),
1631 csym->type(), binding,
1632 csym->visibility(), csym->nonvis(),
1635 csym->set_is_copied_from_dynobj();
1636 csym->set_needs_dynsym_entry();
1638 this->copied_symbol_dynobjs_[csym] = dynobj;
1640 // We have now defined all aliases, but we have not entered them all
1641 // in the copied_symbol_dynobjs_ map.
1642 if (csym->has_alias())
1647 sym = this->weak_aliases_[sym];
1650 gold_assert(sym->output_data() == posd);
1652 sym->set_is_copied_from_dynobj();
1653 this->copied_symbol_dynobjs_[sym] = dynobj;
1658 // SYM is defined using a COPY reloc. Return the dynamic object where
1659 // the original definition was found.
1662 Symbol_table::get_copy_source(const Symbol* sym) const
1664 gold_assert(sym->is_copied_from_dynobj());
1665 Copied_symbol_dynobjs::const_iterator p =
1666 this->copied_symbol_dynobjs_.find(sym);
1667 gold_assert(p != this->copied_symbol_dynobjs_.end());
1671 // Set the dynamic symbol indexes. INDEX is the index of the first
1672 // global dynamic symbol. Pointers to the symbols are stored into the
1673 // vector SYMS. The names are added to DYNPOOL. This returns an
1674 // updated dynamic symbol index.
1677 Symbol_table::set_dynsym_indexes(unsigned int index,
1678 std::vector<Symbol*>* syms,
1679 Stringpool* dynpool,
1682 for (Symbol_table_type::iterator p = this->table_.begin();
1683 p != this->table_.end();
1686 Symbol* sym = p->second;
1688 // Note that SYM may already have a dynamic symbol index, since
1689 // some symbols appear more than once in the symbol table, with
1690 // and without a version.
1692 if (!sym->should_add_dynsym_entry())
1693 sym->set_dynsym_index(-1U);
1694 else if (!sym->has_dynsym_index())
1696 sym->set_dynsym_index(index);
1698 syms->push_back(sym);
1699 dynpool->add(sym->name(), false, NULL);
1701 // Record any version information.
1702 if (sym->version() != NULL)
1703 versions->record_version(this, dynpool, sym);
1707 // Finish up the versions. In some cases this may add new dynamic
1709 index = versions->finalize(this, index, syms);
1714 // Set the final values for all the symbols. The index of the first
1715 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1716 // file offset OFF. Add their names to POOL. Return the new file
1717 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1720 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1721 size_t dyncount, Stringpool* pool,
1722 unsigned int *plocal_symcount)
1726 gold_assert(*plocal_symcount != 0);
1727 this->first_global_index_ = *plocal_symcount;
1729 this->dynamic_offset_ = dynoff;
1730 this->first_dynamic_global_index_ = dyn_global_index;
1731 this->dynamic_count_ = dyncount;
1733 if (parameters->target().get_size() == 32)
1735 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1736 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
1741 else if (parameters->target().get_size() == 64)
1743 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1744 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
1752 // Now that we have the final symbol table, we can reliably note
1753 // which symbols should get warnings.
1754 this->warnings_.note_warnings(this);
1759 // SYM is going into the symbol table at *PINDEX. Add the name to
1760 // POOL, update *PINDEX and *POFF.
1764 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1765 unsigned int* pindex, off_t* poff)
1767 sym->set_symtab_index(*pindex);
1768 pool->add(sym->name(), false, NULL);
1770 *poff += elfcpp::Elf_sizes<size>::sym_size;
1773 // Set the final value for all the symbols. This is called after
1774 // Layout::finalize, so all the output sections have their final
1779 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1780 unsigned int* plocal_symcount)
1782 off = align_address(off, size >> 3);
1783 this->offset_ = off;
1785 unsigned int index = *plocal_symcount;
1786 const unsigned int orig_index = index;
1788 // First do all the symbols which have been forced to be local, as
1789 // they must appear before all global symbols.
1790 for (Forced_locals::iterator p = this->forced_locals_.begin();
1791 p != this->forced_locals_.end();
1795 gold_assert(sym->is_forced_local());
1796 if (this->sized_finalize_symbol<size>(sym))
1798 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1803 // Now do all the remaining symbols.
1804 for (Symbol_table_type::iterator p = this->table_.begin();
1805 p != this->table_.end();
1808 Symbol* sym = p->second;
1809 if (this->sized_finalize_symbol<size>(sym))
1810 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1813 this->output_count_ = index - orig_index;
1818 // Finalize the symbol SYM. This returns true if the symbol should be
1819 // added to the symbol table, false otherwise.
1823 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1825 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
1827 // The default version of a symbol may appear twice in the symbol
1828 // table. We only need to finalize it once.
1829 if (sym->has_symtab_index())
1834 gold_assert(!sym->has_symtab_index());
1835 sym->set_symtab_index(-1U);
1836 gold_assert(sym->dynsym_index() == -1U);
1840 typename Sized_symbol<size>::Value_type value;
1842 switch (sym->source())
1844 case Symbol::FROM_OBJECT:
1847 unsigned int shndx = sym->shndx(&is_ordinary);
1849 // FIXME: We need some target specific support here.
1851 && shndx != elfcpp::SHN_ABS
1852 && shndx != elfcpp::SHN_COMMON)
1854 gold_error(_("%s: unsupported symbol section 0x%x"),
1855 sym->demangled_name().c_str(), shndx);
1856 shndx = elfcpp::SHN_UNDEF;
1859 Object* symobj = sym->object();
1860 if (symobj->is_dynamic())
1863 shndx = elfcpp::SHN_UNDEF;
1865 else if (shndx == elfcpp::SHN_UNDEF)
1867 else if (!is_ordinary
1868 && (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON))
1869 value = sym->value();
1872 Relobj* relobj = static_cast<Relobj*>(symobj);
1873 section_offset_type secoff;
1874 Output_section* os = relobj->output_section(shndx, &secoff);
1878 sym->set_symtab_index(-1U);
1879 gold_assert(sym->dynsym_index() == -1U);
1883 if (sym->type() == elfcpp::STT_TLS)
1884 value = sym->value() + os->tls_offset() + secoff;
1886 value = sym->value() + os->address() + secoff;
1891 case Symbol::IN_OUTPUT_DATA:
1893 Output_data* od = sym->output_data();
1894 value = sym->value();
1895 if (sym->type() != elfcpp::STT_TLS)
1896 value += od->address();
1899 Output_section* os = od->output_section();
1900 gold_assert(os != NULL);
1901 value += os->tls_offset() + (od->address() - os->address());
1903 if (sym->offset_is_from_end())
1904 value += od->data_size();
1908 case Symbol::IN_OUTPUT_SEGMENT:
1910 Output_segment* os = sym->output_segment();
1911 value = sym->value();
1912 if (sym->type() != elfcpp::STT_TLS)
1913 value += os->vaddr();
1914 switch (sym->offset_base())
1916 case Symbol::SEGMENT_START:
1918 case Symbol::SEGMENT_END:
1919 value += os->memsz();
1921 case Symbol::SEGMENT_BSS:
1922 value += os->filesz();
1930 case Symbol::CONSTANT:
1931 value = sym->value();
1938 sym->set_value(value);
1940 if (parameters->options().strip_all())
1942 sym->set_symtab_index(-1U);
1949 // Write out the global symbols.
1952 Symbol_table::write_globals(const Input_objects* input_objects,
1953 const Stringpool* sympool,
1954 const Stringpool* dynpool,
1955 Output_symtab_xindex* symtab_xindex,
1956 Output_symtab_xindex* dynsym_xindex,
1957 Output_file* of) const
1959 switch (parameters->size_and_endianness())
1961 #ifdef HAVE_TARGET_32_LITTLE
1962 case Parameters::TARGET_32_LITTLE:
1963 this->sized_write_globals<32, false>(input_objects, sympool,
1964 dynpool, symtab_xindex,
1968 #ifdef HAVE_TARGET_32_BIG
1969 case Parameters::TARGET_32_BIG:
1970 this->sized_write_globals<32, true>(input_objects, sympool,
1971 dynpool, symtab_xindex,
1975 #ifdef HAVE_TARGET_64_LITTLE
1976 case Parameters::TARGET_64_LITTLE:
1977 this->sized_write_globals<64, false>(input_objects, sympool,
1978 dynpool, symtab_xindex,
1982 #ifdef HAVE_TARGET_64_BIG
1983 case Parameters::TARGET_64_BIG:
1984 this->sized_write_globals<64, true>(input_objects, sympool,
1985 dynpool, symtab_xindex,
1994 // Write out the global symbols.
1996 template<int size, bool big_endian>
1998 Symbol_table::sized_write_globals(const Input_objects* input_objects,
1999 const Stringpool* sympool,
2000 const Stringpool* dynpool,
2001 Output_symtab_xindex* symtab_xindex,
2002 Output_symtab_xindex* dynsym_xindex,
2003 Output_file* of) const
2005 const Target& target = parameters->target();
2007 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2009 const unsigned int output_count = this->output_count_;
2010 const section_size_type oview_size = output_count * sym_size;
2011 const unsigned int first_global_index = this->first_global_index_;
2012 unsigned char* psyms;
2013 if (this->offset_ == 0 || output_count == 0)
2016 psyms = of->get_output_view(this->offset_, oview_size);
2018 const unsigned int dynamic_count = this->dynamic_count_;
2019 const section_size_type dynamic_size = dynamic_count * sym_size;
2020 const unsigned int first_dynamic_global_index =
2021 this->first_dynamic_global_index_;
2022 unsigned char* dynamic_view;
2023 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2024 dynamic_view = NULL;
2026 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2028 for (Symbol_table_type::const_iterator p = this->table_.begin();
2029 p != this->table_.end();
2032 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2034 // Possibly warn about unresolved symbols in shared libraries.
2035 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
2037 unsigned int sym_index = sym->symtab_index();
2038 unsigned int dynsym_index;
2039 if (dynamic_view == NULL)
2042 dynsym_index = sym->dynsym_index();
2044 if (sym_index == -1U && dynsym_index == -1U)
2046 // This symbol is not included in the output file.
2051 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2052 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
2053 switch (sym->source())
2055 case Symbol::FROM_OBJECT:
2058 unsigned int in_shndx = sym->shndx(&is_ordinary);
2060 // FIXME: We need some target specific support here.
2062 && in_shndx != elfcpp::SHN_ABS
2063 && in_shndx != elfcpp::SHN_COMMON)
2065 gold_error(_("%s: unsupported symbol section 0x%x"),
2066 sym->demangled_name().c_str(), in_shndx);
2071 Object* symobj = sym->object();
2072 if (symobj->is_dynamic())
2074 if (sym->needs_dynsym_value())
2075 dynsym_value = target.dynsym_value(sym);
2076 shndx = elfcpp::SHN_UNDEF;
2078 else if (in_shndx == elfcpp::SHN_UNDEF
2080 && (in_shndx == elfcpp::SHN_ABS
2081 || in_shndx == elfcpp::SHN_COMMON)))
2085 Relobj* relobj = static_cast<Relobj*>(symobj);
2086 section_offset_type secoff;
2087 Output_section* os = relobj->output_section(in_shndx,
2089 gold_assert(os != NULL);
2090 shndx = os->out_shndx();
2092 if (shndx >= elfcpp::SHN_LORESERVE)
2094 if (sym_index != -1U)
2095 symtab_xindex->add(sym_index, shndx);
2096 if (dynsym_index != -1U)
2097 dynsym_xindex->add(dynsym_index, shndx);
2098 shndx = elfcpp::SHN_XINDEX;
2101 // In object files symbol values are section
2103 if (parameters->options().relocatable())
2104 sym_value -= os->address();
2110 case Symbol::IN_OUTPUT_DATA:
2111 shndx = sym->output_data()->out_shndx();
2112 if (shndx >= elfcpp::SHN_LORESERVE)
2114 if (sym_index != -1U)
2115 symtab_xindex->add(sym_index, shndx);
2116 if (dynsym_index != -1U)
2117 dynsym_xindex->add(dynsym_index, shndx);
2118 shndx = elfcpp::SHN_XINDEX;
2122 case Symbol::IN_OUTPUT_SEGMENT:
2123 shndx = elfcpp::SHN_ABS;
2126 case Symbol::CONSTANT:
2127 shndx = elfcpp::SHN_ABS;
2134 if (sym_index != -1U)
2136 sym_index -= first_global_index;
2137 gold_assert(sym_index < output_count);
2138 unsigned char* ps = psyms + (sym_index * sym_size);
2139 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2143 if (dynsym_index != -1U)
2145 dynsym_index -= first_dynamic_global_index;
2146 gold_assert(dynsym_index < dynamic_count);
2147 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2148 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2153 of->write_output_view(this->offset_, oview_size, psyms);
2154 if (dynamic_view != NULL)
2155 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2158 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2159 // strtab holding the name.
2161 template<int size, bool big_endian>
2163 Symbol_table::sized_write_symbol(
2164 Sized_symbol<size>* sym,
2165 typename elfcpp::Elf_types<size>::Elf_Addr value,
2167 const Stringpool* pool,
2168 unsigned char* p) const
2170 elfcpp::Sym_write<size, big_endian> osym(p);
2171 osym.put_st_name(pool->get_offset(sym->name()));
2172 osym.put_st_value(value);
2173 osym.put_st_size(sym->symsize());
2174 // A version script may have overridden the default binding.
2175 if (sym->is_forced_local())
2176 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
2178 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
2179 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2180 osym.put_st_shndx(shndx);
2183 // Check for unresolved symbols in shared libraries. This is
2184 // controlled by the --allow-shlib-undefined option.
2186 // We only warn about libraries for which we have seen all the
2187 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2188 // which were not seen in this link. If we didn't see a DT_NEEDED
2189 // entry, we aren't going to be able to reliably report whether the
2190 // symbol is undefined.
2192 // We also don't warn about libraries found in the system library
2193 // directory (the directory were we find libc.so); we assume that
2194 // those libraries are OK. This heuristic avoids problems in
2195 // GNU/Linux, in which -ldl can have undefined references satisfied by
2199 Symbol_table::warn_about_undefined_dynobj_symbol(
2200 const Input_objects* input_objects,
2204 if (sym->source() == Symbol::FROM_OBJECT
2205 && sym->object()->is_dynamic()
2206 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
2207 && sym->binding() != elfcpp::STB_WEAK
2208 && !parameters->options().allow_shlib_undefined()
2209 && !parameters->target().is_defined_by_abi(sym)
2210 && !input_objects->found_in_system_library_directory(sym->object()))
2212 // A very ugly cast.
2213 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2214 if (!dynobj->has_unknown_needed_entries())
2215 gold_error(_("%s: undefined reference to '%s'"),
2216 sym->object()->name().c_str(),
2217 sym->demangled_name().c_str());
2221 // Write out a section symbol. Return the update offset.
2224 Symbol_table::write_section_symbol(const Output_section *os,
2225 Output_symtab_xindex* symtab_xindex,
2229 switch (parameters->size_and_endianness())
2231 #ifdef HAVE_TARGET_32_LITTLE
2232 case Parameters::TARGET_32_LITTLE:
2233 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2237 #ifdef HAVE_TARGET_32_BIG
2238 case Parameters::TARGET_32_BIG:
2239 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2243 #ifdef HAVE_TARGET_64_LITTLE
2244 case Parameters::TARGET_64_LITTLE:
2245 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2249 #ifdef HAVE_TARGET_64_BIG
2250 case Parameters::TARGET_64_BIG:
2251 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2260 // Write out a section symbol, specialized for size and endianness.
2262 template<int size, bool big_endian>
2264 Symbol_table::sized_write_section_symbol(const Output_section* os,
2265 Output_symtab_xindex* symtab_xindex,
2269 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2271 unsigned char* pov = of->get_output_view(offset, sym_size);
2273 elfcpp::Sym_write<size, big_endian> osym(pov);
2274 osym.put_st_name(0);
2275 osym.put_st_value(os->address());
2276 osym.put_st_size(0);
2277 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2278 elfcpp::STT_SECTION));
2279 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2281 unsigned int shndx = os->out_shndx();
2282 if (shndx >= elfcpp::SHN_LORESERVE)
2284 symtab_xindex->add(os->symtab_index(), shndx);
2285 shndx = elfcpp::SHN_XINDEX;
2287 osym.put_st_shndx(shndx);
2289 of->write_output_view(offset, sym_size, pov);
2292 // Print statistical information to stderr. This is used for --stats.
2295 Symbol_table::print_stats() const
2297 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2298 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2299 program_name, this->table_.size(), this->table_.bucket_count());
2301 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2302 program_name, this->table_.size());
2304 this->namepool_.print_stats("symbol table stringpool");
2307 // We check for ODR violations by looking for symbols with the same
2308 // name for which the debugging information reports that they were
2309 // defined in different source locations. When comparing the source
2310 // location, we consider instances with the same base filename and
2311 // line number to be the same. This is because different object
2312 // files/shared libraries can include the same header file using
2313 // different paths, and we don't want to report an ODR violation in
2316 // This struct is used to compare line information, as returned by
2317 // Dwarf_line_info::one_addr2line. It implements a < comparison
2318 // operator used with std::set.
2320 struct Odr_violation_compare
2323 operator()(const std::string& s1, const std::string& s2) const
2325 std::string::size_type pos1 = s1.rfind('/');
2326 std::string::size_type pos2 = s2.rfind('/');
2327 if (pos1 == std::string::npos
2328 || pos2 == std::string::npos)
2330 return s1.compare(pos1, std::string::npos,
2331 s2, pos2, std::string::npos) < 0;
2335 // Check candidate_odr_violations_ to find symbols with the same name
2336 // but apparently different definitions (different source-file/line-no).
2339 Symbol_table::detect_odr_violations(const Task* task,
2340 const char* output_file_name) const
2342 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2343 it != candidate_odr_violations_.end();
2346 const char* symbol_name = it->first;
2347 // We use a sorted set so the output is deterministic.
2348 std::set<std::string, Odr_violation_compare> line_nums;
2350 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2351 locs = it->second.begin();
2352 locs != it->second.end();
2355 // We need to lock the object in order to read it. This
2356 // means that we have to run in a singleton Task. If we
2357 // want to run this in a general Task for better
2358 // performance, we will need one Task for object, plus
2359 // appropriate locking to ensure that we don't conflict with
2360 // other uses of the object. Also note, one_addr2line is not
2361 // currently thread-safe.
2362 Task_lock_obj<Object> tl(task, locs->object);
2363 // 16 is the size of the object-cache that one_addr2line should use.
2364 std::string lineno = Dwarf_line_info::one_addr2line(
2365 locs->object, locs->shndx, locs->offset, 16);
2366 if (!lineno.empty())
2367 line_nums.insert(lineno);
2370 if (line_nums.size() > 1)
2372 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2373 "places (possible ODR violation):"),
2374 output_file_name, demangle(symbol_name).c_str());
2375 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2376 it2 != line_nums.end();
2378 fprintf(stderr, " %s\n", it2->c_str());
2381 // We only call one_addr2line() in this function, so we can clear its cache.
2382 Dwarf_line_info::clear_addr2line_cache();
2385 // Warnings functions.
2387 // Add a new warning.
2390 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2391 const std::string& warning)
2393 name = symtab->canonicalize_name(name);
2394 this->warnings_[name].set(obj, warning);
2397 // Look through the warnings and mark the symbols for which we should
2398 // warn. This is called during Layout::finalize when we know the
2399 // sources for all the symbols.
2402 Warnings::note_warnings(Symbol_table* symtab)
2404 for (Warning_table::iterator p = this->warnings_.begin();
2405 p != this->warnings_.end();
2408 Symbol* sym = symtab->lookup(p->first, NULL);
2410 && sym->source() == Symbol::FROM_OBJECT
2411 && sym->object() == p->second.object)
2412 sym->set_has_warning();
2416 // Issue a warning. This is called when we see a relocation against a
2417 // symbol for which has a warning.
2419 template<int size, bool big_endian>
2421 Warnings::issue_warning(const Symbol* sym,
2422 const Relocate_info<size, big_endian>* relinfo,
2423 size_t relnum, off_t reloffset) const
2425 gold_assert(sym->has_warning());
2426 Warning_table::const_iterator p = this->warnings_.find(sym->name());
2427 gold_assert(p != this->warnings_.end());
2428 gold_warning_at_location(relinfo, relnum, reloffset,
2429 "%s", p->second.text.c_str());
2432 // Instantiate the templates we need. We could use the configure
2433 // script to restrict this to only the ones needed for implemented
2436 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2439 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2442 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2445 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2448 #ifdef HAVE_TARGET_32_LITTLE
2451 Symbol_table::add_from_relobj<32, false>(
2452 Sized_relobj<32, false>* relobj,
2453 const unsigned char* syms,
2455 size_t symndx_offset,
2456 const char* sym_names,
2457 size_t sym_name_size,
2458 Sized_relobj<32, true>::Symbols* sympointers);
2461 #ifdef HAVE_TARGET_32_BIG
2464 Symbol_table::add_from_relobj<32, true>(
2465 Sized_relobj<32, true>* relobj,
2466 const unsigned char* syms,
2468 size_t symndx_offset,
2469 const char* sym_names,
2470 size_t sym_name_size,
2471 Sized_relobj<32, false>::Symbols* sympointers);
2474 #ifdef HAVE_TARGET_64_LITTLE
2477 Symbol_table::add_from_relobj<64, false>(
2478 Sized_relobj<64, false>* relobj,
2479 const unsigned char* syms,
2481 size_t symndx_offset,
2482 const char* sym_names,
2483 size_t sym_name_size,
2484 Sized_relobj<64, true>::Symbols* sympointers);
2487 #ifdef HAVE_TARGET_64_BIG
2490 Symbol_table::add_from_relobj<64, true>(
2491 Sized_relobj<64, true>* relobj,
2492 const unsigned char* syms,
2494 size_t symndx_offset,
2495 const char* sym_names,
2496 size_t sym_name_size,
2497 Sized_relobj<64, false>::Symbols* sympointers);
2500 #ifdef HAVE_TARGET_32_LITTLE
2503 Symbol_table::add_from_dynobj<32, false>(
2504 Sized_dynobj<32, false>* dynobj,
2505 const unsigned char* syms,
2507 const char* sym_names,
2508 size_t sym_name_size,
2509 const unsigned char* versym,
2511 const std::vector<const char*>* version_map);
2514 #ifdef HAVE_TARGET_32_BIG
2517 Symbol_table::add_from_dynobj<32, true>(
2518 Sized_dynobj<32, true>* dynobj,
2519 const unsigned char* syms,
2521 const char* sym_names,
2522 size_t sym_name_size,
2523 const unsigned char* versym,
2525 const std::vector<const char*>* version_map);
2528 #ifdef HAVE_TARGET_64_LITTLE
2531 Symbol_table::add_from_dynobj<64, false>(
2532 Sized_dynobj<64, false>* dynobj,
2533 const unsigned char* syms,
2535 const char* sym_names,
2536 size_t sym_name_size,
2537 const unsigned char* versym,
2539 const std::vector<const char*>* version_map);
2542 #ifdef HAVE_TARGET_64_BIG
2545 Symbol_table::add_from_dynobj<64, true>(
2546 Sized_dynobj<64, true>* dynobj,
2547 const unsigned char* syms,
2549 const char* sym_names,
2550 size_t sym_name_size,
2551 const unsigned char* versym,
2553 const std::vector<const char*>* version_map);
2556 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2559 Symbol_table::define_with_copy_reloc<32>(
2560 Sized_symbol<32>* sym,
2562 elfcpp::Elf_types<32>::Elf_Addr value);
2565 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2568 Symbol_table::define_with_copy_reloc<64>(
2569 Sized_symbol<64>* sym,
2571 elfcpp::Elf_types<64>::Elf_Addr value);
2574 #ifdef HAVE_TARGET_32_LITTLE
2577 Warnings::issue_warning<32, false>(const Symbol* sym,
2578 const Relocate_info<32, false>* relinfo,
2579 size_t relnum, off_t reloffset) const;
2582 #ifdef HAVE_TARGET_32_BIG
2585 Warnings::issue_warning<32, true>(const Symbol* sym,
2586 const Relocate_info<32, true>* relinfo,
2587 size_t relnum, off_t reloffset) const;
2590 #ifdef HAVE_TARGET_64_LITTLE
2593 Warnings::issue_warning<64, false>(const Symbol* sym,
2594 const Relocate_info<64, false>* relinfo,
2595 size_t relnum, off_t reloffset) const;
2598 #ifdef HAVE_TARGET_64_BIG
2601 Warnings::issue_warning<64, true>(const Symbol* sym,
2602 const Relocate_info<64, true>* relinfo,
2603 size_t relnum, off_t reloffset) const;
2606 } // End namespace gold.