1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
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;
77 // Return the demangled version of the symbol's name, but only
78 // if the --demangle flag was set.
81 demangle(const char* name)
83 if (!parameters->options().do_demangle())
86 // cplus_demangle allocates memory for the result it returns,
87 // and returns NULL if the name is already demangled.
88 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
89 if (demangled_name == NULL)
92 std::string retval(demangled_name);
98 Symbol::demangled_name() const
100 return demangle(this->name());
103 // Initialize the fields in the base class Symbol for SYM in OBJECT.
105 template<int size, bool big_endian>
107 Symbol::init_base(const char* name, const char* version, Object* object,
108 const elfcpp::Sym<size, big_endian>& sym)
110 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
111 sym.get_st_visibility(), sym.get_st_nonvis());
112 this->u_.from_object.object = object;
113 // FIXME: Handle SHN_XINDEX.
114 this->u_.from_object.shndx = sym.get_st_shndx();
115 this->source_ = FROM_OBJECT;
116 this->in_reg_ = !object->is_dynamic();
117 this->in_dyn_ = object->is_dynamic();
120 // Initialize the fields in the base class Symbol for a symbol defined
121 // in an Output_data.
124 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
125 elfcpp::STB binding, elfcpp::STV visibility,
126 unsigned char nonvis, bool offset_is_from_end)
128 this->init_fields(name, NULL, type, binding, visibility, nonvis);
129 this->u_.in_output_data.output_data = od;
130 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
131 this->source_ = IN_OUTPUT_DATA;
132 this->in_reg_ = true;
135 // Initialize the fields in the base class Symbol for a symbol defined
136 // in an Output_segment.
139 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
140 elfcpp::STB binding, elfcpp::STV visibility,
141 unsigned char nonvis, Segment_offset_base offset_base)
143 this->init_fields(name, NULL, type, binding, visibility, nonvis);
144 this->u_.in_output_segment.output_segment = os;
145 this->u_.in_output_segment.offset_base = offset_base;
146 this->source_ = IN_OUTPUT_SEGMENT;
147 this->in_reg_ = true;
150 // Initialize the fields in the base class Symbol for a symbol defined
154 Symbol::init_base(const char* name, elfcpp::STT type,
155 elfcpp::STB binding, elfcpp::STV visibility,
156 unsigned char nonvis)
158 this->init_fields(name, NULL, type, binding, visibility, nonvis);
159 this->source_ = CONSTANT;
160 this->in_reg_ = true;
163 // Allocate a common symbol in the base.
166 Symbol::allocate_base_common(Output_data* od)
168 gold_assert(this->is_common());
169 this->source_ = IN_OUTPUT_DATA;
170 this->u_.in_output_data.output_data = od;
171 this->u_.in_output_data.offset_is_from_end = false;
174 // Initialize the fields in Sized_symbol for SYM in OBJECT.
177 template<bool big_endian>
179 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
180 const elfcpp::Sym<size, big_endian>& sym)
182 this->init_base(name, version, object, sym);
183 this->value_ = sym.get_st_value();
184 this->symsize_ = sym.get_st_size();
187 // Initialize the fields in Sized_symbol for a symbol defined in an
192 Sized_symbol<size>::init(const char* name, Output_data* od,
193 Value_type value, Size_type symsize,
194 elfcpp::STT type, elfcpp::STB binding,
195 elfcpp::STV visibility, unsigned char nonvis,
196 bool offset_is_from_end)
198 this->init_base(name, od, type, binding, visibility, nonvis,
200 this->value_ = value;
201 this->symsize_ = symsize;
204 // Initialize the fields in Sized_symbol for a symbol defined in an
209 Sized_symbol<size>::init(const char* name, Output_segment* os,
210 Value_type value, Size_type symsize,
211 elfcpp::STT type, elfcpp::STB binding,
212 elfcpp::STV visibility, unsigned char nonvis,
213 Segment_offset_base offset_base)
215 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
216 this->value_ = value;
217 this->symsize_ = symsize;
220 // Initialize the fields in Sized_symbol for a symbol defined as a
225 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
226 elfcpp::STT type, elfcpp::STB binding,
227 elfcpp::STV visibility, unsigned char nonvis)
229 this->init_base(name, type, binding, visibility, nonvis);
230 this->value_ = value;
231 this->symsize_ = symsize;
234 // Allocate a common symbol.
238 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
240 this->allocate_base_common(od);
241 this->value_ = value;
244 // Return true if this symbol should be added to the dynamic symbol
248 Symbol::should_add_dynsym_entry() const
250 // If the symbol is used by a dynamic relocation, we need to add it.
251 if (this->needs_dynsym_entry())
254 // If the symbol was forced local in a version script, do not add it.
255 if (this->is_forced_local())
258 // If exporting all symbols or building a shared library,
259 // and the symbol is defined in a regular object and is
260 // externally visible, we need to add it.
261 if ((parameters->options().export_dynamic() || parameters->options().shared())
262 && !this->is_from_dynobj()
263 && this->is_externally_visible())
269 // Return true if the final value of this symbol is known at link
273 Symbol::final_value_is_known() const
275 // If we are not generating an executable, then no final values are
276 // known, since they will change at runtime.
277 if (parameters->options().shared() || parameters->options().relocatable())
280 // If the symbol is not from an object file, then it is defined, and
282 if (this->source_ != FROM_OBJECT)
285 // If the symbol is from a dynamic object, then the final value is
287 if (this->object()->is_dynamic())
290 // If the symbol is not undefined (it is defined or common), then
291 // the final value is known.
292 if (!this->is_undefined())
295 // If the symbol is undefined, then whether the final value is known
296 // depends on whether we are doing a static link. If we are doing a
297 // dynamic link, then the final value could be filled in at runtime.
298 // This could reasonably be the case for a weak undefined symbol.
299 return parameters->doing_static_link();
302 // Return the output section where this symbol is defined.
305 Symbol::output_section() const
307 switch (this->source_)
311 unsigned int shndx = this->u_.from_object.shndx;
312 if (shndx != elfcpp::SHN_UNDEF && shndx < elfcpp::SHN_LORESERVE)
314 gold_assert(!this->u_.from_object.object->is_dynamic());
315 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
316 section_offset_type dummy;
317 return relobj->output_section(shndx, &dummy);
323 return this->u_.in_output_data.output_data->output_section();
325 case IN_OUTPUT_SEGMENT:
334 // Set the symbol's output section. This is used for symbols defined
335 // in scripts. This should only be called after the symbol table has
339 Symbol::set_output_section(Output_section* os)
341 switch (this->source_)
345 gold_assert(this->output_section() == os);
348 this->source_ = IN_OUTPUT_DATA;
349 this->u_.in_output_data.output_data = os;
350 this->u_.in_output_data.offset_is_from_end = false;
352 case IN_OUTPUT_SEGMENT:
358 // Class Symbol_table.
360 Symbol_table::Symbol_table(unsigned int count,
361 const Version_script_info& version_script)
362 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
363 forwarders_(), commons_(), forced_locals_(), warnings_(),
364 version_script_(version_script)
366 namepool_.reserve(count);
369 Symbol_table::~Symbol_table()
373 // The hash function. The key values are Stringpool keys.
376 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
378 return key.first ^ key.second;
381 // The symbol table key equality function. This is called with
385 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
386 const Symbol_table_key& k2) const
388 return k1.first == k2.first && k1.second == k2.second;
391 // Make TO a symbol which forwards to FROM.
394 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
396 gold_assert(from != to);
397 gold_assert(!from->is_forwarder() && !to->is_forwarder());
398 this->forwarders_[from] = to;
399 from->set_forwarder();
402 // Resolve the forwards from FROM, returning the real symbol.
405 Symbol_table::resolve_forwards(const Symbol* from) const
407 gold_assert(from->is_forwarder());
408 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
409 this->forwarders_.find(from);
410 gold_assert(p != this->forwarders_.end());
414 // Look up a symbol by name.
417 Symbol_table::lookup(const char* name, const char* version) const
419 Stringpool::Key name_key;
420 name = this->namepool_.find(name, &name_key);
424 Stringpool::Key version_key = 0;
427 version = this->namepool_.find(version, &version_key);
432 Symbol_table_key key(name_key, version_key);
433 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
434 if (p == this->table_.end())
439 // Resolve a Symbol with another Symbol. This is only used in the
440 // unusual case where there are references to both an unversioned
441 // symbol and a symbol with a version, and we then discover that that
442 // version is the default version. Because this is unusual, we do
443 // this the slow way, by converting back to an ELF symbol.
445 template<int size, bool big_endian>
447 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
450 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
451 elfcpp::Sym_write<size, big_endian> esym(buf);
452 // We don't bother to set the st_name field.
453 esym.put_st_value(from->value());
454 esym.put_st_size(from->symsize());
455 esym.put_st_info(from->binding(), from->type());
456 esym.put_st_other(from->visibility(), from->nonvis());
457 esym.put_st_shndx(from->shndx());
458 this->resolve(to, esym.sym(), esym.sym(), from->object(), version);
465 // Record that a symbol is forced to be local by a version script.
468 Symbol_table::force_local(Symbol* sym)
470 if (!sym->is_defined() && !sym->is_common())
472 if (sym->is_forced_local())
474 // We already got this one.
477 sym->set_is_forced_local();
478 this->forced_locals_.push_back(sym);
481 // Add one symbol from OBJECT to the symbol table. NAME is symbol
482 // name and VERSION is the version; both are canonicalized. DEF is
483 // whether this is the default version.
485 // If DEF is true, then this is the definition of a default version of
486 // a symbol. That means that any lookup of NAME/NULL and any lookup
487 // of NAME/VERSION should always return the same symbol. This is
488 // obvious for references, but in particular we want to do this for
489 // definitions: overriding NAME/NULL should also override
490 // NAME/VERSION. If we don't do that, it would be very hard to
491 // override functions in a shared library which uses versioning.
493 // We implement this by simply making both entries in the hash table
494 // point to the same Symbol structure. That is easy enough if this is
495 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
496 // that we have seen both already, in which case they will both have
497 // independent entries in the symbol table. We can't simply change
498 // the symbol table entry, because we have pointers to the entries
499 // attached to the object files. So we mark the entry attached to the
500 // object file as a forwarder, and record it in the forwarders_ map.
501 // Note that entries in the hash table will never be marked as
504 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
505 // symbol exactly as it existed in the input file. SYM is usually
506 // that as well, but can be modified, for instance if we determine
507 // it's in a to-be-discarded section.
509 template<int size, bool big_endian>
511 Symbol_table::add_from_object(Object* object,
513 Stringpool::Key name_key,
515 Stringpool::Key version_key,
517 const elfcpp::Sym<size, big_endian>& sym,
518 const elfcpp::Sym<size, big_endian>& orig_sym)
520 Symbol* const snull = NULL;
521 std::pair<typename Symbol_table_type::iterator, bool> ins =
522 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
525 std::pair<typename Symbol_table_type::iterator, bool> insdef =
526 std::make_pair(this->table_.end(), false);
529 const Stringpool::Key vnull_key = 0;
530 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
535 // ins.first: an iterator, which is a pointer to a pair.
536 // ins.first->first: the key (a pair of name and version).
537 // ins.first->second: the value (Symbol*).
538 // ins.second: true if new entry was inserted, false if not.
540 Sized_symbol<size>* ret;
545 // We already have an entry for NAME/VERSION.
546 ret = this->get_sized_symbol<size>(ins.first->second);
547 gold_assert(ret != NULL);
549 was_undefined = ret->is_undefined();
550 was_common = ret->is_common();
552 this->resolve(ret, sym, orig_sym, object, version);
558 // This is the first time we have seen NAME/NULL. Make
559 // NAME/NULL point to NAME/VERSION.
560 insdef.first->second = ret;
562 else if (insdef.first->second != ret
563 && insdef.first->second->is_undefined())
565 // This is the unfortunate case where we already have
566 // entries for both NAME/VERSION and NAME/NULL. Note
567 // that we don't want to combine them if the existing
568 // symbol is going to override the new one. FIXME: We
569 // currently just test is_undefined, but this may not do
570 // the right thing if the existing symbol is from a
571 // shared library and the new one is from a regular
574 const Sized_symbol<size>* sym2;
575 sym2 = this->get_sized_symbol<size>(insdef.first->second);
576 Symbol_table::resolve<size, big_endian>(ret, sym2, version);
577 this->make_forwarder(insdef.first->second, ret);
578 insdef.first->second = ret;
586 // This is the first time we have seen NAME/VERSION.
587 gold_assert(ins.first->second == NULL);
589 if (def && !insdef.second)
591 // We already have an entry for NAME/NULL. If we override
592 // it, then change it to NAME/VERSION.
593 ret = this->get_sized_symbol<size>(insdef.first->second);
595 was_undefined = ret->is_undefined();
596 was_common = ret->is_common();
598 this->resolve(ret, sym, orig_sym, object, version);
599 ins.first->second = ret;
603 was_undefined = false;
606 Sized_target<size, big_endian>* target =
607 object->sized_target<size, big_endian>();
608 if (!target->has_make_symbol())
609 ret = new Sized_symbol<size>();
612 ret = target->make_symbol();
615 // This means that we don't want a symbol table
618 this->table_.erase(ins.first);
621 this->table_.erase(insdef.first);
622 // Inserting insdef invalidated ins.
623 this->table_.erase(std::make_pair(name_key,
630 ret->init(name, version, object, sym);
632 ins.first->second = ret;
635 // This is the first time we have seen NAME/NULL. Point
636 // it at the new entry for NAME/VERSION.
637 gold_assert(insdef.second);
638 insdef.first->second = ret;
643 // Record every time we see a new undefined symbol, to speed up
645 if (!was_undefined && ret->is_undefined())
646 ++this->saw_undefined_;
648 // Keep track of common symbols, to speed up common symbol
650 if (!was_common && ret->is_common())
651 this->commons_.push_back(ret);
654 ret->set_is_default();
658 // Add all the symbols in a relocatable object to the hash table.
660 template<int size, bool big_endian>
662 Symbol_table::add_from_relobj(
663 Sized_relobj<size, big_endian>* relobj,
664 const unsigned char* syms,
666 const char* sym_names,
667 size_t sym_name_size,
668 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
670 gold_assert(size == relobj->target()->get_size());
671 gold_assert(size == parameters->target().get_size());
673 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
675 const bool just_symbols = relobj->just_symbols();
677 const unsigned char* p = syms;
678 for (size_t i = 0; i < count; ++i, p += sym_size)
680 elfcpp::Sym<size, big_endian> sym(p);
681 elfcpp::Sym<size, big_endian>* psym = &sym;
683 unsigned int st_name = psym->get_st_name();
684 if (st_name >= sym_name_size)
686 relobj->error(_("bad global symbol name offset %u at %zu"),
691 const char* name = sym_names + st_name;
693 // A symbol defined in a section which we are not including must
694 // be treated as an undefined symbol.
695 unsigned char symbuf[sym_size];
696 elfcpp::Sym<size, big_endian> sym2(symbuf);
697 unsigned int st_shndx = psym->get_st_shndx();
698 if (st_shndx != elfcpp::SHN_UNDEF
699 && st_shndx < elfcpp::SHN_LORESERVE
700 && !relobj->is_section_included(st_shndx))
702 memcpy(symbuf, p, sym_size);
703 elfcpp::Sym_write<size, big_endian> sw(symbuf);
704 sw.put_st_shndx(elfcpp::SHN_UNDEF);
708 // In an object file, an '@' in the name separates the symbol
709 // name from the version name. If there are two '@' characters,
710 // this is the default version.
711 const char* ver = strchr(name, '@');
713 // DEF: is the version default? LOCAL: is the symbol forced local?
719 // The symbol name is of the form foo@VERSION or foo@@VERSION
720 namelen = ver - name;
728 // We don't want to assign a version to an undefined symbol,
729 // even if it is listed in the version script. FIXME: What
730 // about a common symbol?
731 else if (!version_script_.empty()
732 && psym->get_st_shndx() != elfcpp::SHN_UNDEF)
734 // The symbol name did not have a version, but
735 // the version script may assign a version anyway.
736 namelen = strlen(name);
738 // Check the global: entries from the version script.
739 const std::string& version =
740 version_script_.get_symbol_version(name);
741 if (!version.empty())
742 ver = version.c_str();
743 // Check the local: entries from the version script
744 if (version_script_.symbol_is_local(name))
751 memcpy(symbuf, p, sym_size);
752 elfcpp::Sym_write<size, big_endian> sw(symbuf);
753 sw.put_st_shndx(elfcpp::SHN_ABS);
754 if (st_shndx != elfcpp::SHN_UNDEF
755 && st_shndx < elfcpp::SHN_LORESERVE)
757 // Symbol values in object files are section relative.
758 // This is normally what we want, but since here we are
759 // converting the symbol to absolute we need to add the
760 // section address. The section address in an object
761 // file is normally zero, but people can use a linker
762 // script to change it.
763 sw.put_st_value(sym2.get_st_value()
764 + relobj->section_address(st_shndx));
769 Sized_symbol<size>* res;
772 Stringpool::Key name_key;
773 name = this->namepool_.add(name, true, &name_key);
774 res = this->add_from_object(relobj, name, name_key, NULL, 0,
777 this->force_local(res);
781 Stringpool::Key name_key;
782 name = this->namepool_.add_with_length(name, namelen, true,
784 Stringpool::Key ver_key;
785 ver = this->namepool_.add(ver, true, &ver_key);
787 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
791 (*sympointers)[i] = res;
795 // Add all the symbols in a dynamic object to the hash table.
797 template<int size, bool big_endian>
799 Symbol_table::add_from_dynobj(
800 Sized_dynobj<size, big_endian>* dynobj,
801 const unsigned char* syms,
803 const char* sym_names,
804 size_t sym_name_size,
805 const unsigned char* versym,
807 const std::vector<const char*>* version_map)
809 gold_assert(size == dynobj->target()->get_size());
810 gold_assert(size == parameters->target().get_size());
812 if (dynobj->just_symbols())
814 gold_error(_("--just-symbols does not make sense with a shared object"));
818 if (versym != NULL && versym_size / 2 < count)
820 dynobj->error(_("too few symbol versions"));
824 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
826 // We keep a list of all STT_OBJECT symbols, so that we can resolve
827 // weak aliases. This is necessary because if the dynamic object
828 // provides the same variable under two names, one of which is a
829 // weak definition, and the regular object refers to the weak
830 // definition, we have to put both the weak definition and the
831 // strong definition into the dynamic symbol table. Given a weak
832 // definition, the only way that we can find the corresponding
833 // strong definition, if any, is to search the symbol table.
834 std::vector<Sized_symbol<size>*> object_symbols;
836 const unsigned char* p = syms;
837 const unsigned char* vs = versym;
838 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
840 elfcpp::Sym<size, big_endian> sym(p);
842 // Ignore symbols with local binding or that have
843 // internal or hidden visibility.
844 if (sym.get_st_bind() == elfcpp::STB_LOCAL
845 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
846 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
849 unsigned int st_name = sym.get_st_name();
850 if (st_name >= sym_name_size)
852 dynobj->error(_("bad symbol name offset %u at %zu"),
857 const char* name = sym_names + st_name;
859 Sized_symbol<size>* res;
863 Stringpool::Key name_key;
864 name = this->namepool_.add(name, true, &name_key);
865 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
870 // Read the version information.
872 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
874 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
875 v &= elfcpp::VERSYM_VERSION;
877 // The Sun documentation says that V can be VER_NDX_LOCAL,
878 // or VER_NDX_GLOBAL, or a version index. The meaning of
879 // VER_NDX_LOCAL is defined as "Symbol has local scope."
880 // The old GNU linker will happily generate VER_NDX_LOCAL
881 // for an undefined symbol. I don't know what the Sun
882 // linker will generate.
884 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
885 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
887 // This symbol should not be visible outside the object.
891 // At this point we are definitely going to add this symbol.
892 Stringpool::Key name_key;
893 name = this->namepool_.add(name, true, &name_key);
895 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
896 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
898 // This symbol does not have a version.
899 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
904 if (v >= version_map->size())
906 dynobj->error(_("versym for symbol %zu out of range: %u"),
911 const char* version = (*version_map)[v];
914 dynobj->error(_("versym for symbol %zu has no name: %u"),
919 Stringpool::Key version_key;
920 version = this->namepool_.add(version, true, &version_key);
922 // If this is an absolute symbol, and the version name
923 // and symbol name are the same, then this is the
924 // version definition symbol. These symbols exist to
925 // support using -u to pull in particular versions. We
926 // do not want to record a version for them.
927 if (sym.get_st_shndx() == elfcpp::SHN_ABS
928 && name_key == version_key)
929 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
933 const bool def = (!hidden
934 && (sym.get_st_shndx()
935 != elfcpp::SHN_UNDEF));
936 res = this->add_from_object(dynobj, name, name_key, version,
937 version_key, def, sym, sym);
942 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
943 && sym.get_st_type() == elfcpp::STT_OBJECT)
944 object_symbols.push_back(res);
947 this->record_weak_aliases(&object_symbols);
950 // This is used to sort weak aliases. We sort them first by section
951 // index, then by offset, then by weak ahead of strong.
954 class Weak_alias_sorter
957 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
962 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
963 const Sized_symbol<size>* s2) const
965 if (s1->shndx() != s2->shndx())
966 return s1->shndx() < s2->shndx();
967 if (s1->value() != s2->value())
968 return s1->value() < s2->value();
969 if (s1->binding() != s2->binding())
971 if (s1->binding() == elfcpp::STB_WEAK)
973 if (s2->binding() == elfcpp::STB_WEAK)
976 return std::string(s1->name()) < std::string(s2->name());
979 // SYMBOLS is a list of object symbols from a dynamic object. Look
980 // for any weak aliases, and record them so that if we add the weak
981 // alias to the dynamic symbol table, we also add the corresponding
986 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
988 // Sort the vector by section index, then by offset, then by weak
990 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
992 // Walk through the vector. For each weak definition, record
994 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
999 if ((*p)->binding() != elfcpp::STB_WEAK)
1002 // Build a circular list of weak aliases. Each symbol points to
1003 // the next one in the circular list.
1005 Sized_symbol<size>* from_sym = *p;
1006 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1007 for (q = p + 1; q != symbols->end(); ++q)
1009 if ((*q)->shndx() != from_sym->shndx()
1010 || (*q)->value() != from_sym->value())
1013 this->weak_aliases_[from_sym] = *q;
1014 from_sym->set_has_alias();
1020 this->weak_aliases_[from_sym] = *p;
1021 from_sym->set_has_alias();
1028 // Create and return a specially defined symbol. If ONLY_IF_REF is
1029 // true, then only create the symbol if there is a reference to it.
1030 // If this does not return NULL, it sets *POLDSYM to the existing
1031 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1033 template<int size, bool big_endian>
1035 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1037 Sized_symbol<size>** poldsym)
1040 Sized_symbol<size>* sym;
1041 bool add_to_table = false;
1042 typename Symbol_table_type::iterator add_loc = this->table_.end();
1044 // If the caller didn't give us a version, see if we get one from
1045 // the version script.
1046 if (*pversion == NULL)
1048 const std::string& v(this->version_script_.get_symbol_version(*pname));
1050 *pversion = v.c_str();
1055 oldsym = this->lookup(*pname, *pversion);
1056 if (oldsym == NULL || !oldsym->is_undefined())
1059 *pname = oldsym->name();
1060 *pversion = oldsym->version();
1064 // Canonicalize NAME and VERSION.
1065 Stringpool::Key name_key;
1066 *pname = this->namepool_.add(*pname, true, &name_key);
1068 Stringpool::Key version_key = 0;
1069 if (*pversion != NULL)
1070 *pversion = this->namepool_.add(*pversion, true, &version_key);
1072 Symbol* const snull = NULL;
1073 std::pair<typename Symbol_table_type::iterator, bool> ins =
1074 this->table_.insert(std::make_pair(std::make_pair(name_key,
1080 // We already have a symbol table entry for NAME/VERSION.
1081 oldsym = ins.first->second;
1082 gold_assert(oldsym != NULL);
1086 // We haven't seen this symbol before.
1087 gold_assert(ins.first->second == NULL);
1088 add_to_table = true;
1089 add_loc = ins.first;
1094 const Target& target = parameters->target();
1095 if (!target.has_make_symbol())
1096 sym = new Sized_symbol<size>();
1099 gold_assert(target.get_size() == size);
1100 gold_assert(target.is_big_endian() ? big_endian : !big_endian);
1101 typedef Sized_target<size, big_endian> My_target;
1102 const My_target* sized_target =
1103 static_cast<const My_target*>(&target);
1104 sym = sized_target->make_symbol();
1110 add_loc->second = sym;
1112 gold_assert(oldsym != NULL);
1114 *poldsym = this->get_sized_symbol<size>(oldsym);
1119 // Define a symbol based on an Output_data.
1122 Symbol_table::define_in_output_data(const char* name,
1123 const char* version,
1128 elfcpp::STB binding,
1129 elfcpp::STV visibility,
1130 unsigned char nonvis,
1131 bool offset_is_from_end,
1134 if (parameters->target().get_size() == 32)
1136 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1137 return this->do_define_in_output_data<32>(name, version, od,
1138 value, symsize, type, binding,
1146 else if (parameters->target().get_size() == 64)
1148 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1149 return this->do_define_in_output_data<64>(name, version, od,
1150 value, symsize, type, binding,
1162 // Define a symbol in an Output_data, sized version.
1166 Symbol_table::do_define_in_output_data(
1168 const char* version,
1170 typename elfcpp::Elf_types<size>::Elf_Addr value,
1171 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1173 elfcpp::STB binding,
1174 elfcpp::STV visibility,
1175 unsigned char nonvis,
1176 bool offset_is_from_end,
1179 Sized_symbol<size>* sym;
1180 Sized_symbol<size>* oldsym;
1182 if (parameters->target().is_big_endian())
1184 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1185 sym = this->define_special_symbol<size, true>(&name, &version,
1186 only_if_ref, &oldsym);
1193 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1194 sym = this->define_special_symbol<size, false>(&name, &version,
1195 only_if_ref, &oldsym);
1204 gold_assert(version == NULL || oldsym != NULL);
1205 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1206 offset_is_from_end);
1210 if (binding == elfcpp::STB_LOCAL
1211 || this->version_script_.symbol_is_local(name))
1212 this->force_local(sym);
1216 if (Symbol_table::should_override_with_special(oldsym))
1217 this->override_with_special(oldsym, sym);
1222 // Define a symbol based on an Output_segment.
1225 Symbol_table::define_in_output_segment(const char* name,
1226 const char* version, Output_segment* os,
1230 elfcpp::STB binding,
1231 elfcpp::STV visibility,
1232 unsigned char nonvis,
1233 Symbol::Segment_offset_base offset_base,
1236 if (parameters->target().get_size() == 32)
1238 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1239 return this->do_define_in_output_segment<32>(name, version, os,
1240 value, symsize, type,
1241 binding, visibility, nonvis,
1242 offset_base, only_if_ref);
1247 else if (parameters->target().get_size() == 64)
1249 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1250 return this->do_define_in_output_segment<64>(name, version, os,
1251 value, symsize, type,
1252 binding, visibility, nonvis,
1253 offset_base, only_if_ref);
1262 // Define a symbol in an Output_segment, sized version.
1266 Symbol_table::do_define_in_output_segment(
1268 const char* version,
1270 typename elfcpp::Elf_types<size>::Elf_Addr value,
1271 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1273 elfcpp::STB binding,
1274 elfcpp::STV visibility,
1275 unsigned char nonvis,
1276 Symbol::Segment_offset_base offset_base,
1279 Sized_symbol<size>* sym;
1280 Sized_symbol<size>* oldsym;
1282 if (parameters->target().is_big_endian())
1284 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1285 sym = this->define_special_symbol<size, true>(&name, &version,
1286 only_if_ref, &oldsym);
1293 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1294 sym = this->define_special_symbol<size, false>(&name, &version,
1295 only_if_ref, &oldsym);
1304 gold_assert(version == NULL || oldsym != NULL);
1305 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1310 if (binding == elfcpp::STB_LOCAL
1311 || this->version_script_.symbol_is_local(name))
1312 this->force_local(sym);
1316 if (Symbol_table::should_override_with_special(oldsym))
1317 this->override_with_special(oldsym, sym);
1322 // Define a special symbol with a constant value. It is a multiple
1323 // definition error if this symbol is already defined.
1326 Symbol_table::define_as_constant(const char* name,
1327 const char* version,
1331 elfcpp::STB binding,
1332 elfcpp::STV visibility,
1333 unsigned char nonvis,
1335 bool force_override)
1337 if (parameters->target().get_size() == 32)
1339 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1340 return this->do_define_as_constant<32>(name, version, value,
1341 symsize, type, binding,
1342 visibility, nonvis, only_if_ref,
1348 else if (parameters->target().get_size() == 64)
1350 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1351 return this->do_define_as_constant<64>(name, version, value,
1352 symsize, type, binding,
1353 visibility, nonvis, only_if_ref,
1363 // Define a symbol as a constant, sized version.
1367 Symbol_table::do_define_as_constant(
1369 const char* version,
1370 typename elfcpp::Elf_types<size>::Elf_Addr value,
1371 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1373 elfcpp::STB binding,
1374 elfcpp::STV visibility,
1375 unsigned char nonvis,
1377 bool force_override)
1379 Sized_symbol<size>* sym;
1380 Sized_symbol<size>* oldsym;
1382 if (parameters->target().is_big_endian())
1384 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1385 sym = this->define_special_symbol<size, true>(&name, &version,
1386 only_if_ref, &oldsym);
1393 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1394 sym = this->define_special_symbol<size, false>(&name, &version,
1395 only_if_ref, &oldsym);
1404 gold_assert(version == NULL || version == name || oldsym != NULL);
1405 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1409 // Version symbols are absolute symbols with name == version.
1410 // We don't want to force them to be local.
1411 if ((version == NULL
1414 && (binding == elfcpp::STB_LOCAL
1415 || this->version_script_.symbol_is_local(name)))
1416 this->force_local(sym);
1420 if (force_override || Symbol_table::should_override_with_special(oldsym))
1421 this->override_with_special(oldsym, sym);
1426 // Define a set of symbols in output sections.
1429 Symbol_table::define_symbols(const Layout* layout, int count,
1430 const Define_symbol_in_section* p,
1433 for (int i = 0; i < count; ++i, ++p)
1435 Output_section* os = layout->find_output_section(p->output_section);
1437 this->define_in_output_data(p->name, NULL, os, p->value,
1438 p->size, p->type, p->binding,
1439 p->visibility, p->nonvis,
1440 p->offset_is_from_end,
1441 only_if_ref || p->only_if_ref);
1443 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1444 p->binding, p->visibility, p->nonvis,
1445 only_if_ref || p->only_if_ref,
1450 // Define a set of symbols in output segments.
1453 Symbol_table::define_symbols(const Layout* layout, int count,
1454 const Define_symbol_in_segment* p,
1457 for (int i = 0; i < count; ++i, ++p)
1459 Output_segment* os = layout->find_output_segment(p->segment_type,
1460 p->segment_flags_set,
1461 p->segment_flags_clear);
1463 this->define_in_output_segment(p->name, NULL, os, p->value,
1464 p->size, p->type, p->binding,
1465 p->visibility, p->nonvis,
1467 only_if_ref || p->only_if_ref);
1469 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1470 p->binding, p->visibility, p->nonvis,
1471 only_if_ref || p->only_if_ref,
1476 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1477 // symbol should be defined--typically a .dyn.bss section. VALUE is
1478 // the offset within POSD.
1482 Symbol_table::define_with_copy_reloc(
1483 Sized_symbol<size>* csym,
1485 typename elfcpp::Elf_types<size>::Elf_Addr value)
1487 gold_assert(csym->is_from_dynobj());
1488 gold_assert(!csym->is_copied_from_dynobj());
1489 Object* object = csym->object();
1490 gold_assert(object->is_dynamic());
1491 Dynobj* dynobj = static_cast<Dynobj*>(object);
1493 // Our copied variable has to override any variable in a shared
1495 elfcpp::STB binding = csym->binding();
1496 if (binding == elfcpp::STB_WEAK)
1497 binding = elfcpp::STB_GLOBAL;
1499 this->define_in_output_data(csym->name(), csym->version(),
1500 posd, value, csym->symsize(),
1501 csym->type(), binding,
1502 csym->visibility(), csym->nonvis(),
1505 csym->set_is_copied_from_dynobj();
1506 csym->set_needs_dynsym_entry();
1508 this->copied_symbol_dynobjs_[csym] = dynobj;
1510 // We have now defined all aliases, but we have not entered them all
1511 // in the copied_symbol_dynobjs_ map.
1512 if (csym->has_alias())
1517 sym = this->weak_aliases_[sym];
1520 gold_assert(sym->output_data() == posd);
1522 sym->set_is_copied_from_dynobj();
1523 this->copied_symbol_dynobjs_[sym] = dynobj;
1528 // SYM is defined using a COPY reloc. Return the dynamic object where
1529 // the original definition was found.
1532 Symbol_table::get_copy_source(const Symbol* sym) const
1534 gold_assert(sym->is_copied_from_dynobj());
1535 Copied_symbol_dynobjs::const_iterator p =
1536 this->copied_symbol_dynobjs_.find(sym);
1537 gold_assert(p != this->copied_symbol_dynobjs_.end());
1541 // Set the dynamic symbol indexes. INDEX is the index of the first
1542 // global dynamic symbol. Pointers to the symbols are stored into the
1543 // vector SYMS. The names are added to DYNPOOL. This returns an
1544 // updated dynamic symbol index.
1547 Symbol_table::set_dynsym_indexes(unsigned int index,
1548 std::vector<Symbol*>* syms,
1549 Stringpool* dynpool,
1552 for (Symbol_table_type::iterator p = this->table_.begin();
1553 p != this->table_.end();
1556 Symbol* sym = p->second;
1558 // Note that SYM may already have a dynamic symbol index, since
1559 // some symbols appear more than once in the symbol table, with
1560 // and without a version.
1562 if (!sym->should_add_dynsym_entry())
1563 sym->set_dynsym_index(-1U);
1564 else if (!sym->has_dynsym_index())
1566 sym->set_dynsym_index(index);
1568 syms->push_back(sym);
1569 dynpool->add(sym->name(), false, NULL);
1571 // Record any version information.
1572 if (sym->version() != NULL)
1573 versions->record_version(this, dynpool, sym);
1577 // Finish up the versions. In some cases this may add new dynamic
1579 index = versions->finalize(this, index, syms);
1584 // Set the final values for all the symbols. The index of the first
1585 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1586 // file offset OFF. Add their names to POOL. Return the new file
1587 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1590 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1591 size_t dyncount, Stringpool* pool,
1592 unsigned int *plocal_symcount)
1596 gold_assert(*plocal_symcount != 0);
1597 this->first_global_index_ = *plocal_symcount;
1599 this->dynamic_offset_ = dynoff;
1600 this->first_dynamic_global_index_ = dyn_global_index;
1601 this->dynamic_count_ = dyncount;
1603 if (parameters->target().get_size() == 32)
1605 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1606 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
1611 else if (parameters->target().get_size() == 64)
1613 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1614 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
1622 // Now that we have the final symbol table, we can reliably note
1623 // which symbols should get warnings.
1624 this->warnings_.note_warnings(this);
1629 // SYM is going into the symbol table at *PINDEX. Add the name to
1630 // POOL, update *PINDEX and *POFF.
1634 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1635 unsigned int* pindex, off_t* poff)
1637 sym->set_symtab_index(*pindex);
1638 pool->add(sym->name(), false, NULL);
1640 *poff += elfcpp::Elf_sizes<size>::sym_size;
1643 // Set the final value for all the symbols. This is called after
1644 // Layout::finalize, so all the output sections have their final
1649 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1650 unsigned int* plocal_symcount)
1652 off = align_address(off, size >> 3);
1653 this->offset_ = off;
1655 unsigned int index = *plocal_symcount;
1656 const unsigned int orig_index = index;
1658 // First do all the symbols which have been forced to be local, as
1659 // they must appear before all global symbols.
1660 for (Forced_locals::iterator p = this->forced_locals_.begin();
1661 p != this->forced_locals_.end();
1665 gold_assert(sym->is_forced_local());
1666 if (this->sized_finalize_symbol<size>(sym))
1668 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1673 // Now do all the remaining symbols.
1674 for (Symbol_table_type::iterator p = this->table_.begin();
1675 p != this->table_.end();
1678 Symbol* sym = p->second;
1679 if (this->sized_finalize_symbol<size>(sym))
1680 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1683 this->output_count_ = index - orig_index;
1688 // Finalize the symbol SYM. This returns true if the symbol should be
1689 // added to the symbol table, false otherwise.
1693 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1695 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
1697 // The default version of a symbol may appear twice in the symbol
1698 // table. We only need to finalize it once.
1699 if (sym->has_symtab_index())
1704 gold_assert(!sym->has_symtab_index());
1705 sym->set_symtab_index(-1U);
1706 gold_assert(sym->dynsym_index() == -1U);
1710 typename Sized_symbol<size>::Value_type value;
1712 switch (sym->source())
1714 case Symbol::FROM_OBJECT:
1716 unsigned int shndx = sym->shndx();
1718 // FIXME: We need some target specific support here.
1719 if (shndx >= elfcpp::SHN_LORESERVE
1720 && shndx != elfcpp::SHN_ABS
1721 && shndx != elfcpp::SHN_COMMON)
1723 gold_error(_("%s: unsupported symbol section 0x%x"),
1724 sym->demangled_name().c_str(), shndx);
1725 shndx = elfcpp::SHN_UNDEF;
1728 Object* symobj = sym->object();
1729 if (symobj->is_dynamic())
1732 shndx = elfcpp::SHN_UNDEF;
1734 else if (shndx == elfcpp::SHN_UNDEF)
1736 else if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1737 value = sym->value();
1740 Relobj* relobj = static_cast<Relobj*>(symobj);
1741 section_offset_type secoff;
1742 Output_section* os = relobj->output_section(shndx, &secoff);
1746 sym->set_symtab_index(-1U);
1747 gold_assert(sym->dynsym_index() == -1U);
1751 if (sym->type() == elfcpp::STT_TLS)
1752 value = sym->value() + os->tls_offset() + secoff;
1754 value = sym->value() + os->address() + secoff;
1759 case Symbol::IN_OUTPUT_DATA:
1761 Output_data* od = sym->output_data();
1762 value = sym->value() + od->address();
1763 if (sym->offset_is_from_end())
1764 value += od->data_size();
1768 case Symbol::IN_OUTPUT_SEGMENT:
1770 Output_segment* os = sym->output_segment();
1771 value = sym->value() + os->vaddr();
1772 switch (sym->offset_base())
1774 case Symbol::SEGMENT_START:
1776 case Symbol::SEGMENT_END:
1777 value += os->memsz();
1779 case Symbol::SEGMENT_BSS:
1780 value += os->filesz();
1788 case Symbol::CONSTANT:
1789 value = sym->value();
1796 sym->set_value(value);
1798 if (parameters->options().strip_all())
1800 sym->set_symtab_index(-1U);
1807 // Write out the global symbols.
1810 Symbol_table::write_globals(const Input_objects* input_objects,
1811 const Stringpool* sympool,
1812 const Stringpool* dynpool, Output_file* of) const
1814 switch (parameters->size_and_endianness())
1816 #ifdef HAVE_TARGET_32_LITTLE
1817 case Parameters::TARGET_32_LITTLE:
1818 this->sized_write_globals<32, false>(input_objects, sympool,
1822 #ifdef HAVE_TARGET_32_BIG
1823 case Parameters::TARGET_32_BIG:
1824 this->sized_write_globals<32, true>(input_objects, sympool,
1828 #ifdef HAVE_TARGET_64_LITTLE
1829 case Parameters::TARGET_64_LITTLE:
1830 this->sized_write_globals<64, false>(input_objects, sympool,
1834 #ifdef HAVE_TARGET_64_BIG
1835 case Parameters::TARGET_64_BIG:
1836 this->sized_write_globals<64, true>(input_objects, sympool,
1845 // Write out the global symbols.
1847 template<int size, bool big_endian>
1849 Symbol_table::sized_write_globals(const Input_objects* input_objects,
1850 const Stringpool* sympool,
1851 const Stringpool* dynpool,
1852 Output_file* of) const
1854 const Target& target = parameters->target();
1856 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1858 const unsigned int output_count = this->output_count_;
1859 const section_size_type oview_size = output_count * sym_size;
1860 const unsigned int first_global_index = this->first_global_index_;
1861 unsigned char* psyms;
1862 if (this->offset_ == 0 || output_count == 0)
1865 psyms = of->get_output_view(this->offset_, oview_size);
1867 const unsigned int dynamic_count = this->dynamic_count_;
1868 const section_size_type dynamic_size = dynamic_count * sym_size;
1869 const unsigned int first_dynamic_global_index =
1870 this->first_dynamic_global_index_;
1871 unsigned char* dynamic_view;
1872 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
1873 dynamic_view = NULL;
1875 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1877 for (Symbol_table_type::const_iterator p = this->table_.begin();
1878 p != this->table_.end();
1881 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1883 // Possibly warn about unresolved symbols in shared libraries.
1884 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
1886 unsigned int sym_index = sym->symtab_index();
1887 unsigned int dynsym_index;
1888 if (dynamic_view == NULL)
1891 dynsym_index = sym->dynsym_index();
1893 if (sym_index == -1U && dynsym_index == -1U)
1895 // This symbol is not included in the output file.
1900 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
1901 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
1902 switch (sym->source())
1904 case Symbol::FROM_OBJECT:
1906 unsigned int in_shndx = sym->shndx();
1908 // FIXME: We need some target specific support here.
1909 if (in_shndx >= elfcpp::SHN_LORESERVE
1910 && in_shndx != elfcpp::SHN_ABS
1911 && in_shndx != elfcpp::SHN_COMMON)
1913 gold_error(_("%s: unsupported symbol section 0x%x"),
1914 sym->demangled_name().c_str(), in_shndx);
1919 Object* symobj = sym->object();
1920 if (symobj->is_dynamic())
1922 if (sym->needs_dynsym_value())
1923 dynsym_value = target.dynsym_value(sym);
1924 shndx = elfcpp::SHN_UNDEF;
1926 else if (in_shndx == elfcpp::SHN_UNDEF
1927 || in_shndx == elfcpp::SHN_ABS
1928 || in_shndx == elfcpp::SHN_COMMON)
1932 Relobj* relobj = static_cast<Relobj*>(symobj);
1933 section_offset_type secoff;
1934 Output_section* os = relobj->output_section(in_shndx,
1936 gold_assert(os != NULL);
1937 shndx = os->out_shndx();
1939 // In object files symbol values are section
1941 if (parameters->options().relocatable())
1942 sym_value -= os->address();
1948 case Symbol::IN_OUTPUT_DATA:
1949 shndx = sym->output_data()->out_shndx();
1952 case Symbol::IN_OUTPUT_SEGMENT:
1953 shndx = elfcpp::SHN_ABS;
1956 case Symbol::CONSTANT:
1957 shndx = elfcpp::SHN_ABS;
1964 if (sym_index != -1U)
1966 sym_index -= first_global_index;
1967 gold_assert(sym_index < output_count);
1968 unsigned char* ps = psyms + (sym_index * sym_size);
1969 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
1973 if (dynsym_index != -1U)
1975 dynsym_index -= first_dynamic_global_index;
1976 gold_assert(dynsym_index < dynamic_count);
1977 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1978 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
1983 of->write_output_view(this->offset_, oview_size, psyms);
1984 if (dynamic_view != NULL)
1985 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1988 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1989 // strtab holding the name.
1991 template<int size, bool big_endian>
1993 Symbol_table::sized_write_symbol(
1994 Sized_symbol<size>* sym,
1995 typename elfcpp::Elf_types<size>::Elf_Addr value,
1997 const Stringpool* pool,
1998 unsigned char* p) const
2000 elfcpp::Sym_write<size, big_endian> osym(p);
2001 osym.put_st_name(pool->get_offset(sym->name()));
2002 osym.put_st_value(value);
2003 osym.put_st_size(sym->symsize());
2004 // A version script may have overridden the default binding.
2005 if (sym->is_forced_local())
2006 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
2008 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
2009 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2010 osym.put_st_shndx(shndx);
2013 // Check for unresolved symbols in shared libraries. This is
2014 // controlled by the --allow-shlib-undefined option.
2016 // We only warn about libraries for which we have seen all the
2017 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2018 // which were not seen in this link. If we didn't see a DT_NEEDED
2019 // entry, we aren't going to be able to reliably report whether the
2020 // symbol is undefined.
2022 // We also don't warn about libraries found in the system library
2023 // directory (the directory were we find libc.so); we assume that
2024 // those libraries are OK. This heuristic avoids problems in
2025 // GNU/Linux, in which -ldl can have undefined references satisfied by
2029 Symbol_table::warn_about_undefined_dynobj_symbol(
2030 const Input_objects* input_objects,
2033 if (sym->source() == Symbol::FROM_OBJECT
2034 && sym->object()->is_dynamic()
2035 && sym->shndx() == elfcpp::SHN_UNDEF
2036 && sym->binding() != elfcpp::STB_WEAK
2037 && !parameters->options().allow_shlib_undefined()
2038 && !parameters->target().is_defined_by_abi(sym)
2039 && !input_objects->found_in_system_library_directory(sym->object()))
2041 // A very ugly cast.
2042 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2043 if (!dynobj->has_unknown_needed_entries())
2044 gold_error(_("%s: undefined reference to '%s'"),
2045 sym->object()->name().c_str(),
2046 sym->demangled_name().c_str());
2050 // Write out a section symbol. Return the update offset.
2053 Symbol_table::write_section_symbol(const Output_section *os,
2057 switch (parameters->size_and_endianness())
2059 #ifdef HAVE_TARGET_32_LITTLE
2060 case Parameters::TARGET_32_LITTLE:
2061 this->sized_write_section_symbol<32, false>(os, of, offset);
2064 #ifdef HAVE_TARGET_32_BIG
2065 case Parameters::TARGET_32_BIG:
2066 this->sized_write_section_symbol<32, true>(os, of, offset);
2069 #ifdef HAVE_TARGET_64_LITTLE
2070 case Parameters::TARGET_64_LITTLE:
2071 this->sized_write_section_symbol<64, false>(os, of, offset);
2074 #ifdef HAVE_TARGET_64_BIG
2075 case Parameters::TARGET_64_BIG:
2076 this->sized_write_section_symbol<64, true>(os, of, offset);
2084 // Write out a section symbol, specialized for size and endianness.
2086 template<int size, bool big_endian>
2088 Symbol_table::sized_write_section_symbol(const Output_section* os,
2092 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2094 unsigned char* pov = of->get_output_view(offset, sym_size);
2096 elfcpp::Sym_write<size, big_endian> osym(pov);
2097 osym.put_st_name(0);
2098 osym.put_st_value(os->address());
2099 osym.put_st_size(0);
2100 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2101 elfcpp::STT_SECTION));
2102 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2103 osym.put_st_shndx(os->out_shndx());
2105 of->write_output_view(offset, sym_size, pov);
2108 // Print statistical information to stderr. This is used for --stats.
2111 Symbol_table::print_stats() const
2113 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2114 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2115 program_name, this->table_.size(), this->table_.bucket_count());
2117 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2118 program_name, this->table_.size());
2120 this->namepool_.print_stats("symbol table stringpool");
2123 // We check for ODR violations by looking for symbols with the same
2124 // name for which the debugging information reports that they were
2125 // defined in different source locations. When comparing the source
2126 // location, we consider instances with the same base filename and
2127 // line number to be the same. This is because different object
2128 // files/shared libraries can include the same header file using
2129 // different paths, and we don't want to report an ODR violation in
2132 // This struct is used to compare line information, as returned by
2133 // Dwarf_line_info::one_addr2line. It implements a < comparison
2134 // operator used with std::set.
2136 struct Odr_violation_compare
2139 operator()(const std::string& s1, const std::string& s2) const
2141 std::string::size_type pos1 = s1.rfind('/');
2142 std::string::size_type pos2 = s2.rfind('/');
2143 if (pos1 == std::string::npos
2144 || pos2 == std::string::npos)
2146 return s1.compare(pos1, std::string::npos,
2147 s2, pos2, std::string::npos) < 0;
2151 // Check candidate_odr_violations_ to find symbols with the same name
2152 // but apparently different definitions (different source-file/line-no).
2155 Symbol_table::detect_odr_violations(const Task* task,
2156 const char* output_file_name) const
2158 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2159 it != candidate_odr_violations_.end();
2162 const char* symbol_name = it->first;
2163 // We use a sorted set so the output is deterministic.
2164 std::set<std::string, Odr_violation_compare> line_nums;
2166 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2167 locs = it->second.begin();
2168 locs != it->second.end();
2171 // We need to lock the object in order to read it. This
2172 // means that we have to run in a singleton Task. If we
2173 // want to run this in a general Task for better
2174 // performance, we will need one Task for object, plus
2175 // appropriate locking to ensure that we don't conflict with
2176 // other uses of the object.
2177 Task_lock_obj<Object> tl(task, locs->object);
2178 std::string lineno = Dwarf_line_info::one_addr2line(
2179 locs->object, locs->shndx, locs->offset);
2180 if (!lineno.empty())
2181 line_nums.insert(lineno);
2184 if (line_nums.size() > 1)
2186 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2187 "places (possible ODR violation):"),
2188 output_file_name, demangle(symbol_name).c_str());
2189 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2190 it2 != line_nums.end();
2192 fprintf(stderr, " %s\n", it2->c_str());
2197 // Warnings functions.
2199 // Add a new warning.
2202 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2203 const std::string& warning)
2205 name = symtab->canonicalize_name(name);
2206 this->warnings_[name].set(obj, warning);
2209 // Look through the warnings and mark the symbols for which we should
2210 // warn. This is called during Layout::finalize when we know the
2211 // sources for all the symbols.
2214 Warnings::note_warnings(Symbol_table* symtab)
2216 for (Warning_table::iterator p = this->warnings_.begin();
2217 p != this->warnings_.end();
2220 Symbol* sym = symtab->lookup(p->first, NULL);
2222 && sym->source() == Symbol::FROM_OBJECT
2223 && sym->object() == p->second.object)
2224 sym->set_has_warning();
2228 // Issue a warning. This is called when we see a relocation against a
2229 // symbol for which has a warning.
2231 template<int size, bool big_endian>
2233 Warnings::issue_warning(const Symbol* sym,
2234 const Relocate_info<size, big_endian>* relinfo,
2235 size_t relnum, off_t reloffset) const
2237 gold_assert(sym->has_warning());
2238 Warning_table::const_iterator p = this->warnings_.find(sym->name());
2239 gold_assert(p != this->warnings_.end());
2240 gold_warning_at_location(relinfo, relnum, reloffset,
2241 "%s", p->second.text.c_str());
2244 // Instantiate the templates we need. We could use the configure
2245 // script to restrict this to only the ones needed for implemented
2248 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2251 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2254 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2257 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2260 #ifdef HAVE_TARGET_32_LITTLE
2263 Symbol_table::add_from_relobj<32, false>(
2264 Sized_relobj<32, false>* relobj,
2265 const unsigned char* syms,
2267 const char* sym_names,
2268 size_t sym_name_size,
2269 Sized_relobj<32, true>::Symbols* sympointers);
2272 #ifdef HAVE_TARGET_32_BIG
2275 Symbol_table::add_from_relobj<32, true>(
2276 Sized_relobj<32, true>* relobj,
2277 const unsigned char* syms,
2279 const char* sym_names,
2280 size_t sym_name_size,
2281 Sized_relobj<32, false>::Symbols* sympointers);
2284 #ifdef HAVE_TARGET_64_LITTLE
2287 Symbol_table::add_from_relobj<64, false>(
2288 Sized_relobj<64, false>* relobj,
2289 const unsigned char* syms,
2291 const char* sym_names,
2292 size_t sym_name_size,
2293 Sized_relobj<64, true>::Symbols* sympointers);
2296 #ifdef HAVE_TARGET_64_BIG
2299 Symbol_table::add_from_relobj<64, true>(
2300 Sized_relobj<64, true>* relobj,
2301 const unsigned char* syms,
2303 const char* sym_names,
2304 size_t sym_name_size,
2305 Sized_relobj<64, false>::Symbols* sympointers);
2308 #ifdef HAVE_TARGET_32_LITTLE
2311 Symbol_table::add_from_dynobj<32, false>(
2312 Sized_dynobj<32, false>* dynobj,
2313 const unsigned char* syms,
2315 const char* sym_names,
2316 size_t sym_name_size,
2317 const unsigned char* versym,
2319 const std::vector<const char*>* version_map);
2322 #ifdef HAVE_TARGET_32_BIG
2325 Symbol_table::add_from_dynobj<32, true>(
2326 Sized_dynobj<32, true>* dynobj,
2327 const unsigned char* syms,
2329 const char* sym_names,
2330 size_t sym_name_size,
2331 const unsigned char* versym,
2333 const std::vector<const char*>* version_map);
2336 #ifdef HAVE_TARGET_64_LITTLE
2339 Symbol_table::add_from_dynobj<64, false>(
2340 Sized_dynobj<64, false>* dynobj,
2341 const unsigned char* syms,
2343 const char* sym_names,
2344 size_t sym_name_size,
2345 const unsigned char* versym,
2347 const std::vector<const char*>* version_map);
2350 #ifdef HAVE_TARGET_64_BIG
2353 Symbol_table::add_from_dynobj<64, true>(
2354 Sized_dynobj<64, true>* dynobj,
2355 const unsigned char* syms,
2357 const char* sym_names,
2358 size_t sym_name_size,
2359 const unsigned char* versym,
2361 const std::vector<const char*>* version_map);
2364 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2367 Symbol_table::define_with_copy_reloc<32>(
2368 Sized_symbol<32>* sym,
2370 elfcpp::Elf_types<32>::Elf_Addr value);
2373 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2376 Symbol_table::define_with_copy_reloc<64>(
2377 Sized_symbol<64>* sym,
2379 elfcpp::Elf_types<64>::Elf_Addr value);
2382 #ifdef HAVE_TARGET_32_LITTLE
2385 Warnings::issue_warning<32, false>(const Symbol* sym,
2386 const Relocate_info<32, false>* relinfo,
2387 size_t relnum, off_t reloffset) const;
2390 #ifdef HAVE_TARGET_32_BIG
2393 Warnings::issue_warning<32, true>(const Symbol* sym,
2394 const Relocate_info<32, true>* relinfo,
2395 size_t relnum, off_t reloffset) const;
2398 #ifdef HAVE_TARGET_64_LITTLE
2401 Warnings::issue_warning<64, false>(const Symbol* sym,
2402 const Relocate_info<64, false>* relinfo,
2403 size_t relnum, off_t reloffset) const;
2406 #ifdef HAVE_TARGET_64_BIG
2409 Warnings::issue_warning<64, true>(const Symbol* sym,
2410 const Relocate_info<64, true>* relinfo,
2411 size_t relnum, off_t reloffset) const;
2414 } // End namespace gold.