1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.
35 #include "dwarf_reader.h"
39 #include "workqueue.h"
43 #include "incremental.h"
50 // Initialize fields in Symbol. This initializes everything except u_
54 Symbol::init_fields(const char* name, const char* version,
55 elfcpp::STT type, elfcpp::STB binding,
56 elfcpp::STV visibility, unsigned char nonvis)
59 this->version_ = version;
60 this->symtab_index_ = 0;
61 this->dynsym_index_ = 0;
62 this->got_offsets_.init();
63 this->plt_offset_ = -1U;
65 this->binding_ = binding;
66 this->visibility_ = visibility;
67 this->nonvis_ = nonvis;
68 this->is_def_ = false;
69 this->is_forwarder_ = false;
70 this->has_alias_ = false;
71 this->needs_dynsym_entry_ = false;
72 this->in_reg_ = false;
73 this->in_dyn_ = false;
74 this->has_warning_ = false;
75 this->is_copied_from_dynobj_ = false;
76 this->is_forced_local_ = false;
77 this->is_ordinary_shndx_ = false;
78 this->in_real_elf_ = false;
79 this->is_defined_in_discarded_section_ = false;
80 this->undef_binding_set_ = false;
81 this->undef_binding_weak_ = false;
84 // Return the demangled version of the symbol's name, but only
85 // if the --demangle flag was set.
88 demangle(const char* name)
90 if (!parameters->options().do_demangle())
93 // cplus_demangle allocates memory for the result it returns,
94 // and returns NULL if the name is already demangled.
95 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
96 if (demangled_name == NULL)
99 std::string retval(demangled_name);
100 free(demangled_name);
105 Symbol::demangled_name() const
107 return demangle(this->name());
110 // Initialize the fields in the base class Symbol for SYM in OBJECT.
112 template<int size, bool big_endian>
114 Symbol::init_base_object(const char* name, const char* version, Object* object,
115 const elfcpp::Sym<size, big_endian>& sym,
116 unsigned int st_shndx, bool is_ordinary)
118 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
119 sym.get_st_visibility(), sym.get_st_nonvis());
120 this->u_.from_object.object = object;
121 this->u_.from_object.shndx = st_shndx;
122 this->is_ordinary_shndx_ = is_ordinary;
123 this->source_ = FROM_OBJECT;
124 this->in_reg_ = !object->is_dynamic();
125 this->in_dyn_ = object->is_dynamic();
126 this->in_real_elf_ = object->pluginobj() == NULL;
129 // Initialize the fields in the base class Symbol for a symbol defined
130 // in an Output_data.
133 Symbol::init_base_output_data(const char* name, const char* version,
134 Output_data* od, elfcpp::STT type,
135 elfcpp::STB binding, elfcpp::STV visibility,
136 unsigned char nonvis, bool offset_is_from_end)
138 this->init_fields(name, version, type, binding, visibility, nonvis);
139 this->u_.in_output_data.output_data = od;
140 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
141 this->source_ = IN_OUTPUT_DATA;
142 this->in_reg_ = true;
143 this->in_real_elf_ = true;
146 // Initialize the fields in the base class Symbol for a symbol defined
147 // in an Output_segment.
150 Symbol::init_base_output_segment(const char* name, const char* version,
151 Output_segment* os, elfcpp::STT type,
152 elfcpp::STB binding, elfcpp::STV visibility,
153 unsigned char nonvis,
154 Segment_offset_base offset_base)
156 this->init_fields(name, version, type, binding, visibility, nonvis);
157 this->u_.in_output_segment.output_segment = os;
158 this->u_.in_output_segment.offset_base = offset_base;
159 this->source_ = IN_OUTPUT_SEGMENT;
160 this->in_reg_ = true;
161 this->in_real_elf_ = true;
164 // Initialize the fields in the base class Symbol for a symbol defined
168 Symbol::init_base_constant(const char* name, const char* version,
169 elfcpp::STT type, elfcpp::STB binding,
170 elfcpp::STV visibility, unsigned char nonvis)
172 this->init_fields(name, version, type, binding, visibility, nonvis);
173 this->source_ = IS_CONSTANT;
174 this->in_reg_ = true;
175 this->in_real_elf_ = true;
178 // Initialize the fields in the base class Symbol for an undefined
182 Symbol::init_base_undefined(const char* name, const char* version,
183 elfcpp::STT type, elfcpp::STB binding,
184 elfcpp::STV visibility, unsigned char nonvis)
186 this->init_fields(name, version, type, binding, visibility, nonvis);
187 this->dynsym_index_ = -1U;
188 this->source_ = IS_UNDEFINED;
189 this->in_reg_ = true;
190 this->in_real_elf_ = true;
193 // Allocate a common symbol in the base.
196 Symbol::allocate_base_common(Output_data* od)
198 gold_assert(this->is_common());
199 this->source_ = IN_OUTPUT_DATA;
200 this->u_.in_output_data.output_data = od;
201 this->u_.in_output_data.offset_is_from_end = false;
204 // Initialize the fields in Sized_symbol for SYM in OBJECT.
207 template<bool big_endian>
209 Sized_symbol<size>::init_object(const char* name, const char* version,
211 const elfcpp::Sym<size, big_endian>& sym,
212 unsigned int st_shndx, bool is_ordinary)
214 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
215 this->value_ = sym.get_st_value();
216 this->symsize_ = sym.get_st_size();
219 // Initialize the fields in Sized_symbol for a symbol defined in an
224 Sized_symbol<size>::init_output_data(const char* name, const char* version,
225 Output_data* od, Value_type value,
226 Size_type symsize, elfcpp::STT type,
228 elfcpp::STV visibility,
229 unsigned char nonvis,
230 bool offset_is_from_end)
232 this->init_base_output_data(name, version, od, type, binding, visibility,
233 nonvis, offset_is_from_end);
234 this->value_ = value;
235 this->symsize_ = symsize;
238 // Initialize the fields in Sized_symbol for a symbol defined in an
243 Sized_symbol<size>::init_output_segment(const char* name, const char* version,
244 Output_segment* os, Value_type value,
245 Size_type symsize, elfcpp::STT type,
247 elfcpp::STV visibility,
248 unsigned char nonvis,
249 Segment_offset_base offset_base)
251 this->init_base_output_segment(name, version, os, type, binding, visibility,
252 nonvis, offset_base);
253 this->value_ = value;
254 this->symsize_ = symsize;
257 // Initialize the fields in Sized_symbol for a symbol defined as a
262 Sized_symbol<size>::init_constant(const char* name, const char* version,
263 Value_type value, Size_type symsize,
264 elfcpp::STT type, elfcpp::STB binding,
265 elfcpp::STV visibility, unsigned char nonvis)
267 this->init_base_constant(name, version, type, binding, visibility, nonvis);
268 this->value_ = value;
269 this->symsize_ = symsize;
272 // Initialize the fields in Sized_symbol for an undefined symbol.
276 Sized_symbol<size>::init_undefined(const char* name, const char* version,
277 elfcpp::STT type, elfcpp::STB binding,
278 elfcpp::STV visibility, unsigned char nonvis)
280 this->init_base_undefined(name, version, type, binding, visibility, nonvis);
285 // Return true if SHNDX represents a common symbol.
288 Symbol::is_common_shndx(unsigned int shndx)
290 return (shndx == elfcpp::SHN_COMMON
291 || shndx == parameters->target().small_common_shndx()
292 || shndx == parameters->target().large_common_shndx());
295 // Allocate a common symbol.
299 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
301 this->allocate_base_common(od);
302 this->value_ = value;
305 // The ""'s around str ensure str is a string literal, so sizeof works.
306 #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
308 // Return true if this symbol should be added to the dynamic symbol
312 Symbol::should_add_dynsym_entry(Symbol_table* symtab) const
314 // If the symbol is only present on plugin files, the plugin decided we
316 if (!this->in_real_elf())
319 // If the symbol is used by a dynamic relocation, we need to add it.
320 if (this->needs_dynsym_entry())
323 // If this symbol's section is not added, the symbol need not be added.
324 // The section may have been GCed. Note that export_dynamic is being
325 // overridden here. This should not be done for shared objects.
326 if (parameters->options().gc_sections()
327 && !parameters->options().shared()
328 && this->source() == Symbol::FROM_OBJECT
329 && !this->object()->is_dynamic())
331 Relobj* relobj = static_cast<Relobj*>(this->object());
333 unsigned int shndx = this->shndx(&is_ordinary);
334 if (is_ordinary && shndx != elfcpp::SHN_UNDEF
335 && !relobj->is_section_included(shndx)
336 && !symtab->is_section_folded(relobj, shndx))
340 // If the symbol was forced local in a version script, do not add it.
341 if (this->is_forced_local())
344 // If the symbol was forced dynamic in a --dynamic-list file, add it.
345 if (parameters->options().in_dynamic_list(this->name()))
348 // If dynamic-list-data was specified, add any STT_OBJECT.
349 if (parameters->options().dynamic_list_data()
350 && !this->is_from_dynobj()
351 && this->type() == elfcpp::STT_OBJECT)
354 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
355 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
356 if ((parameters->options().dynamic_list_cpp_new()
357 || parameters->options().dynamic_list_cpp_typeinfo())
358 && !this->is_from_dynobj())
360 // TODO(csilvers): We could probably figure out if we're an operator
361 // new/delete or typeinfo without the need to demangle.
362 char* demangled_name = cplus_demangle(this->name(),
363 DMGL_ANSI | DMGL_PARAMS);
364 if (demangled_name == NULL)
366 // Not a C++ symbol, so it can't satisfy these flags
368 else if (parameters->options().dynamic_list_cpp_new()
369 && (strprefix(demangled_name, "operator new")
370 || strprefix(demangled_name, "operator delete")))
372 free(demangled_name);
375 else if (parameters->options().dynamic_list_cpp_typeinfo()
376 && (strprefix(demangled_name, "typeinfo name for")
377 || strprefix(demangled_name, "typeinfo for")))
379 free(demangled_name);
383 free(demangled_name);
386 // If exporting all symbols or building a shared library,
387 // and the symbol is defined in a regular object and is
388 // externally visible, we need to add it.
389 if ((parameters->options().export_dynamic() || parameters->options().shared())
390 && !this->is_from_dynobj()
391 && this->is_externally_visible())
397 // Return true if the final value of this symbol is known at link
401 Symbol::final_value_is_known() const
403 // If we are not generating an executable, then no final values are
404 // known, since they will change at runtime.
405 if (parameters->options().output_is_position_independent()
406 || parameters->options().relocatable())
409 // If the symbol is not from an object file, and is not undefined,
410 // then it is defined, and known.
411 if (this->source_ != FROM_OBJECT)
413 if (this->source_ != IS_UNDEFINED)
418 // If the symbol is from a dynamic object, then the final value
420 if (this->object()->is_dynamic())
423 // If the symbol is not undefined (it is defined or common),
424 // then the final value is known.
425 if (!this->is_undefined())
429 // If the symbol is undefined, then whether the final value is known
430 // depends on whether we are doing a static link. If we are doing a
431 // dynamic link, then the final value could be filled in at runtime.
432 // This could reasonably be the case for a weak undefined symbol.
433 return parameters->doing_static_link();
436 // Return the output section where this symbol is defined.
439 Symbol::output_section() const
441 switch (this->source_)
445 unsigned int shndx = this->u_.from_object.shndx;
446 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
448 gold_assert(!this->u_.from_object.object->is_dynamic());
449 gold_assert(this->u_.from_object.object->pluginobj() == NULL);
450 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
451 return relobj->output_section(shndx);
457 return this->u_.in_output_data.output_data->output_section();
459 case IN_OUTPUT_SEGMENT:
469 // Set the symbol's output section. This is used for symbols defined
470 // in scripts. This should only be called after the symbol table has
474 Symbol::set_output_section(Output_section* os)
476 switch (this->source_)
480 gold_assert(this->output_section() == os);
483 this->source_ = IN_OUTPUT_DATA;
484 this->u_.in_output_data.output_data = os;
485 this->u_.in_output_data.offset_is_from_end = false;
487 case IN_OUTPUT_SEGMENT:
494 // Class Symbol_table.
496 Symbol_table::Symbol_table(unsigned int count,
497 const Version_script_info& version_script)
498 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
499 forwarders_(), commons_(), tls_commons_(), small_commons_(),
500 large_commons_(), forced_locals_(), warnings_(),
501 version_script_(version_script), gc_(NULL), icf_(NULL)
503 namepool_.reserve(count);
506 Symbol_table::~Symbol_table()
510 // The symbol table key equality function. This is called with
514 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
515 const Symbol_table_key& k2) const
517 return k1.first == k2.first && k1.second == k2.second;
521 Symbol_table::is_section_folded(Object* obj, unsigned int shndx) const
523 return (parameters->options().icf_enabled()
524 && this->icf_->is_section_folded(obj, shndx));
527 // For symbols that have been listed with -u option, add them to the
528 // work list to avoid gc'ing them.
531 Symbol_table::gc_mark_undef_symbols(Layout* layout)
533 for (options::String_set::const_iterator p =
534 parameters->options().undefined_begin();
535 p != parameters->options().undefined_end();
538 const char* name = p->c_str();
539 Symbol* sym = this->lookup(name);
540 gold_assert(sym != NULL);
541 if (sym->source() == Symbol::FROM_OBJECT
542 && !sym->object()->is_dynamic())
544 Relobj* obj = static_cast<Relobj*>(sym->object());
546 unsigned int shndx = sym->shndx(&is_ordinary);
549 gold_assert(this->gc_ != NULL);
550 this->gc_->worklist().push(Section_id(obj, shndx));
555 for (Script_options::referenced_const_iterator p =
556 layout->script_options()->referenced_begin();
557 p != layout->script_options()->referenced_end();
560 Symbol* sym = this->lookup(p->c_str());
561 gold_assert(sym != NULL);
562 if (sym->source() == Symbol::FROM_OBJECT
563 && !sym->object()->is_dynamic())
565 Relobj* obj = static_cast<Relobj*>(sym->object());
567 unsigned int shndx = sym->shndx(&is_ordinary);
570 gold_assert(this->gc_ != NULL);
571 this->gc_->worklist().push(Section_id(obj, shndx));
578 Symbol_table::gc_mark_symbol_for_shlib(Symbol* sym)
580 if (!sym->is_from_dynobj()
581 && sym->is_externally_visible())
583 //Add the object and section to the work list.
584 Relobj* obj = static_cast<Relobj*>(sym->object());
586 unsigned int shndx = sym->shndx(&is_ordinary);
587 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
589 gold_assert(this->gc_!= NULL);
590 this->gc_->worklist().push(Section_id(obj, shndx));
595 // When doing garbage collection, keep symbols that have been seen in
598 Symbol_table::gc_mark_dyn_syms(Symbol* sym)
600 if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
601 && !sym->object()->is_dynamic())
603 Relobj* obj = static_cast<Relobj*>(sym->object());
605 unsigned int shndx = sym->shndx(&is_ordinary);
606 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
608 gold_assert(this->gc_ != NULL);
609 this->gc_->worklist().push(Section_id(obj, shndx));
614 // Make TO a symbol which forwards to FROM.
617 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
619 gold_assert(from != to);
620 gold_assert(!from->is_forwarder() && !to->is_forwarder());
621 this->forwarders_[from] = to;
622 from->set_forwarder();
625 // Resolve the forwards from FROM, returning the real symbol.
628 Symbol_table::resolve_forwards(const Symbol* from) const
630 gold_assert(from->is_forwarder());
631 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
632 this->forwarders_.find(from);
633 gold_assert(p != this->forwarders_.end());
637 // Look up a symbol by name.
640 Symbol_table::lookup(const char* name, const char* version) const
642 Stringpool::Key name_key;
643 name = this->namepool_.find(name, &name_key);
647 Stringpool::Key version_key = 0;
650 version = this->namepool_.find(version, &version_key);
655 Symbol_table_key key(name_key, version_key);
656 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
657 if (p == this->table_.end())
662 // Resolve a Symbol with another Symbol. This is only used in the
663 // unusual case where there are references to both an unversioned
664 // symbol and a symbol with a version, and we then discover that that
665 // version is the default version. Because this is unusual, we do
666 // this the slow way, by converting back to an ELF symbol.
668 template<int size, bool big_endian>
670 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
672 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
673 elfcpp::Sym_write<size, big_endian> esym(buf);
674 // We don't bother to set the st_name or the st_shndx field.
675 esym.put_st_value(from->value());
676 esym.put_st_size(from->symsize());
677 esym.put_st_info(from->binding(), from->type());
678 esym.put_st_other(from->visibility(), from->nonvis());
680 unsigned int shndx = from->shndx(&is_ordinary);
681 this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
687 if (parameters->options().gc_sections())
688 this->gc_mark_dyn_syms(to);
691 // Record that a symbol is forced to be local by a version script or
695 Symbol_table::force_local(Symbol* sym)
697 if (!sym->is_defined() && !sym->is_common())
699 if (sym->is_forced_local())
701 // We already got this one.
704 sym->set_is_forced_local();
705 this->forced_locals_.push_back(sym);
708 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
709 // is only called for undefined symbols, when at least one --wrap
713 Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key)
715 // For some targets, we need to ignore a specific character when
716 // wrapping, and add it back later.
718 if (name[0] == parameters->target().wrap_char())
724 if (parameters->options().is_wrap(name))
726 // Turn NAME into __wrap_NAME.
733 // This will give us both the old and new name in NAMEPOOL_, but
734 // that is OK. Only the versions we need will wind up in the
735 // real string table in the output file.
736 return this->namepool_.add(s.c_str(), true, name_key);
739 const char* const real_prefix = "__real_";
740 const size_t real_prefix_length = strlen(real_prefix);
741 if (strncmp(name, real_prefix, real_prefix_length) == 0
742 && parameters->options().is_wrap(name + real_prefix_length))
744 // Turn __real_NAME into NAME.
748 s += name + real_prefix_length;
749 return this->namepool_.add(s.c_str(), true, name_key);
755 // This is called when we see a symbol NAME/VERSION, and the symbol
756 // already exists in the symbol table, and VERSION is marked as being
757 // the default version. SYM is the NAME/VERSION symbol we just added.
758 // DEFAULT_IS_NEW is true if this is the first time we have seen the
759 // symbol NAME/NULL. PDEF points to the entry for NAME/NULL.
761 template<int size, bool big_endian>
763 Symbol_table::define_default_version(Sized_symbol<size>* sym,
765 Symbol_table_type::iterator pdef)
769 // This is the first time we have seen NAME/NULL. Make
770 // NAME/NULL point to NAME/VERSION, and mark SYM as the default
773 sym->set_is_default();
775 else if (pdef->second == sym)
777 // NAME/NULL already points to NAME/VERSION. Don't mark the
778 // symbol as the default if it is not already the default.
782 // This is the unfortunate case where we already have entries
783 // for both NAME/VERSION and NAME/NULL. We now see a symbol
784 // NAME/VERSION where VERSION is the default version. We have
785 // already resolved this new symbol with the existing
786 // NAME/VERSION symbol.
788 // It's possible that NAME/NULL and NAME/VERSION are both
789 // defined in regular objects. This can only happen if one
790 // object file defines foo and another defines foo@@ver. This
791 // is somewhat obscure, but we call it a multiple definition
794 // It's possible that NAME/NULL actually has a version, in which
795 // case it won't be the same as VERSION. This happens with
796 // ver_test_7.so in the testsuite for the symbol t2_2. We see
797 // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We
798 // then see an unadorned t2_2 in an object file and give it
799 // version VER1 from the version script. This looks like a
800 // default definition for VER1, so it looks like we should merge
801 // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's
802 // not obvious that this is an error, either. So we just punt.
804 // If one of the symbols has non-default visibility, and the
805 // other is defined in a shared object, then they are different
808 // Otherwise, we just resolve the symbols as though they were
811 if (pdef->second->version() != NULL)
812 gold_assert(pdef->second->version() != sym->version());
813 else if (sym->visibility() != elfcpp::STV_DEFAULT
814 && pdef->second->is_from_dynobj())
816 else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
817 && sym->is_from_dynobj())
821 const Sized_symbol<size>* symdef;
822 symdef = this->get_sized_symbol<size>(pdef->second);
823 Symbol_table::resolve<size, big_endian>(sym, symdef);
824 this->make_forwarder(pdef->second, sym);
826 sym->set_is_default();
831 // Add one symbol from OBJECT to the symbol table. NAME is symbol
832 // name and VERSION is the version; both are canonicalized. DEF is
833 // whether this is the default version. ST_SHNDX is the symbol's
834 // section index; IS_ORDINARY is whether this is a normal section
835 // rather than a special code.
837 // If IS_DEFAULT_VERSION is true, then this is the definition of a
838 // default version of a symbol. That means that any lookup of
839 // NAME/NULL and any lookup of NAME/VERSION should always return the
840 // same symbol. This is obvious for references, but in particular we
841 // want to do this for definitions: overriding NAME/NULL should also
842 // override NAME/VERSION. If we don't do that, it would be very hard
843 // to override functions in a shared library which uses versioning.
845 // We implement this by simply making both entries in the hash table
846 // point to the same Symbol structure. That is easy enough if this is
847 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
848 // that we have seen both already, in which case they will both have
849 // independent entries in the symbol table. We can't simply change
850 // the symbol table entry, because we have pointers to the entries
851 // attached to the object files. So we mark the entry attached to the
852 // object file as a forwarder, and record it in the forwarders_ map.
853 // Note that entries in the hash table will never be marked as
856 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
857 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
858 // for a special section code. ST_SHNDX may be modified if the symbol
859 // is defined in a section being discarded.
861 template<int size, bool big_endian>
863 Symbol_table::add_from_object(Object* object,
865 Stringpool::Key name_key,
867 Stringpool::Key version_key,
868 bool is_default_version,
869 const elfcpp::Sym<size, big_endian>& sym,
870 unsigned int st_shndx,
872 unsigned int orig_st_shndx)
874 // Print a message if this symbol is being traced.
875 if (parameters->options().is_trace_symbol(name))
877 if (orig_st_shndx == elfcpp::SHN_UNDEF)
878 gold_info(_("%s: reference to %s"), object->name().c_str(), name);
880 gold_info(_("%s: definition of %s"), object->name().c_str(), name);
883 // For an undefined symbol, we may need to adjust the name using
885 if (orig_st_shndx == elfcpp::SHN_UNDEF
886 && parameters->options().any_wrap())
888 const char* wrap_name = this->wrap_symbol(name, &name_key);
889 if (wrap_name != name)
891 // If we see a reference to malloc with version GLIBC_2.0,
892 // and we turn it into a reference to __wrap_malloc, then we
893 // discard the version number. Otherwise the user would be
894 // required to specify the correct version for
902 Symbol* const snull = NULL;
903 std::pair<typename Symbol_table_type::iterator, bool> ins =
904 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
907 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
908 std::make_pair(this->table_.end(), false);
909 if (is_default_version)
911 const Stringpool::Key vnull_key = 0;
912 insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
917 // ins.first: an iterator, which is a pointer to a pair.
918 // ins.first->first: the key (a pair of name and version).
919 // ins.first->second: the value (Symbol*).
920 // ins.second: true if new entry was inserted, false if not.
922 Sized_symbol<size>* ret;
927 // We already have an entry for NAME/VERSION.
928 ret = this->get_sized_symbol<size>(ins.first->second);
929 gold_assert(ret != NULL);
931 was_undefined = ret->is_undefined();
932 was_common = ret->is_common();
934 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
936 if (parameters->options().gc_sections())
937 this->gc_mark_dyn_syms(ret);
939 if (is_default_version)
940 this->define_default_version<size, big_endian>(ret, insdefault.second,
945 // This is the first time we have seen NAME/VERSION.
946 gold_assert(ins.first->second == NULL);
948 if (is_default_version && !insdefault.second)
950 // We already have an entry for NAME/NULL. If we override
951 // it, then change it to NAME/VERSION.
952 ret = this->get_sized_symbol<size>(insdefault.first->second);
954 was_undefined = ret->is_undefined();
955 was_common = ret->is_common();
957 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
959 if (parameters->options().gc_sections())
960 this->gc_mark_dyn_syms(ret);
961 ins.first->second = ret;
965 was_undefined = false;
968 Sized_target<size, big_endian>* target =
969 parameters->sized_target<size, big_endian>();
970 if (!target->has_make_symbol())
971 ret = new Sized_symbol<size>();
974 ret = target->make_symbol();
977 // This means that we don't want a symbol table
979 if (!is_default_version)
980 this->table_.erase(ins.first);
983 this->table_.erase(insdefault.first);
984 // Inserting INSDEFAULT invalidated INS.
985 this->table_.erase(std::make_pair(name_key,
992 ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
994 ins.first->second = ret;
995 if (is_default_version)
997 // This is the first time we have seen NAME/NULL. Point
998 // it at the new entry for NAME/VERSION.
999 gold_assert(insdefault.second);
1000 insdefault.first->second = ret;
1004 if (is_default_version)
1005 ret->set_is_default();
1008 // Record every time we see a new undefined symbol, to speed up
1010 if (!was_undefined && ret->is_undefined())
1012 ++this->saw_undefined_;
1013 if (parameters->options().has_plugins())
1014 parameters->options().plugins()->new_undefined_symbol(ret);
1017 // Keep track of common symbols, to speed up common symbol
1019 if (!was_common && ret->is_common())
1021 if (ret->type() == elfcpp::STT_TLS)
1022 this->tls_commons_.push_back(ret);
1023 else if (!is_ordinary
1024 && st_shndx == parameters->target().small_common_shndx())
1025 this->small_commons_.push_back(ret);
1026 else if (!is_ordinary
1027 && st_shndx == parameters->target().large_common_shndx())
1028 this->large_commons_.push_back(ret);
1030 this->commons_.push_back(ret);
1033 // If we're not doing a relocatable link, then any symbol with
1034 // hidden or internal visibility is local.
1035 if ((ret->visibility() == elfcpp::STV_HIDDEN
1036 || ret->visibility() == elfcpp::STV_INTERNAL)
1037 && (ret->binding() == elfcpp::STB_GLOBAL
1038 || ret->binding() == elfcpp::STB_GNU_UNIQUE
1039 || ret->binding() == elfcpp::STB_WEAK)
1040 && !parameters->options().relocatable())
1041 this->force_local(ret);
1046 // Add all the symbols in a relocatable object to the hash table.
1048 template<int size, bool big_endian>
1050 Symbol_table::add_from_relobj(
1051 Sized_relobj<size, big_endian>* relobj,
1052 const unsigned char* syms,
1054 size_t symndx_offset,
1055 const char* sym_names,
1056 size_t sym_name_size,
1057 typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1062 gold_assert(size == parameters->target().get_size());
1064 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1066 const bool just_symbols = relobj->just_symbols();
1068 const unsigned char* p = syms;
1069 for (size_t i = 0; i < count; ++i, p += sym_size)
1071 (*sympointers)[i] = NULL;
1073 elfcpp::Sym<size, big_endian> sym(p);
1075 unsigned int st_name = sym.get_st_name();
1076 if (st_name >= sym_name_size)
1078 relobj->error(_("bad global symbol name offset %u at %zu"),
1083 const char* name = sym_names + st_name;
1086 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1089 unsigned int orig_st_shndx = st_shndx;
1091 orig_st_shndx = elfcpp::SHN_UNDEF;
1093 if (st_shndx != elfcpp::SHN_UNDEF)
1096 // A symbol defined in a section which we are not including must
1097 // be treated as an undefined symbol.
1098 bool is_defined_in_discarded_section = false;
1099 if (st_shndx != elfcpp::SHN_UNDEF
1101 && !relobj->is_section_included(st_shndx)
1102 && !this->is_section_folded(relobj, st_shndx))
1104 st_shndx = elfcpp::SHN_UNDEF;
1105 is_defined_in_discarded_section = true;
1108 // In an object file, an '@' in the name separates the symbol
1109 // name from the version name. If there are two '@' characters,
1110 // this is the default version.
1111 const char* ver = strchr(name, '@');
1112 Stringpool::Key ver_key = 0;
1114 // IS_DEFAULT_VERSION: is the version default?
1115 // IS_FORCED_LOCAL: is the symbol forced local?
1116 bool is_default_version = false;
1117 bool is_forced_local = false;
1121 // The symbol name is of the form foo@VERSION or foo@@VERSION
1122 namelen = ver - name;
1126 is_default_version = true;
1129 ver = this->namepool_.add(ver, true, &ver_key);
1131 // We don't want to assign a version to an undefined symbol,
1132 // even if it is listed in the version script. FIXME: What
1133 // about a common symbol?
1136 namelen = strlen(name);
1137 if (!this->version_script_.empty()
1138 && st_shndx != elfcpp::SHN_UNDEF)
1140 // The symbol name did not have a version, but the
1141 // version script may assign a version anyway.
1142 std::string version;
1144 if (this->version_script_.get_symbol_version(name, &version,
1148 is_forced_local = true;
1149 else if (!version.empty())
1151 ver = this->namepool_.add_with_length(version.c_str(),
1155 is_default_version = true;
1161 elfcpp::Sym<size, big_endian>* psym = &sym;
1162 unsigned char symbuf[sym_size];
1163 elfcpp::Sym<size, big_endian> sym2(symbuf);
1166 memcpy(symbuf, p, sym_size);
1167 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1168 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
1170 // Symbol values in object files are section relative.
1171 // This is normally what we want, but since here we are
1172 // converting the symbol to absolute we need to add the
1173 // section address. The section address in an object
1174 // file is normally zero, but people can use a linker
1175 // script to change it.
1176 sw.put_st_value(sym.get_st_value()
1177 + relobj->section_address(orig_st_shndx));
1179 st_shndx = elfcpp::SHN_ABS;
1180 is_ordinary = false;
1184 // Fix up visibility if object has no-export set.
1185 if (relobj->no_export()
1186 && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
1188 // We may have copied symbol already above.
1191 memcpy(symbuf, p, sym_size);
1195 elfcpp::STV visibility = sym2.get_st_visibility();
1196 if (visibility == elfcpp::STV_DEFAULT
1197 || visibility == elfcpp::STV_PROTECTED)
1199 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1200 unsigned char nonvis = sym2.get_st_nonvis();
1201 sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1205 Stringpool::Key name_key;
1206 name = this->namepool_.add_with_length(name, namelen, true,
1209 Sized_symbol<size>* res;
1210 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1211 is_default_version, *psym, st_shndx,
1212 is_ordinary, orig_st_shndx);
1214 // If building a shared library using garbage collection, do not
1215 // treat externally visible symbols as garbage.
1216 if (parameters->options().gc_sections()
1217 && parameters->options().shared())
1218 this->gc_mark_symbol_for_shlib(res);
1220 if (is_forced_local)
1221 this->force_local(res);
1223 if (is_defined_in_discarded_section)
1224 res->set_is_defined_in_discarded_section();
1226 (*sympointers)[i] = res;
1230 // Add a symbol from a plugin-claimed file.
1232 template<int size, bool big_endian>
1234 Symbol_table::add_from_pluginobj(
1235 Sized_pluginobj<size, big_endian>* obj,
1238 elfcpp::Sym<size, big_endian>* sym)
1240 unsigned int st_shndx = sym->get_st_shndx();
1241 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1243 Stringpool::Key ver_key = 0;
1244 bool is_default_version = false;
1245 bool is_forced_local = false;
1249 ver = this->namepool_.add(ver, true, &ver_key);
1251 // We don't want to assign a version to an undefined symbol,
1252 // even if it is listed in the version script. FIXME: What
1253 // about a common symbol?
1256 if (!this->version_script_.empty()
1257 && st_shndx != elfcpp::SHN_UNDEF)
1259 // The symbol name did not have a version, but the
1260 // version script may assign a version anyway.
1261 std::string version;
1263 if (this->version_script_.get_symbol_version(name, &version,
1267 is_forced_local = true;
1268 else if (!version.empty())
1270 ver = this->namepool_.add_with_length(version.c_str(),
1274 is_default_version = true;
1280 Stringpool::Key name_key;
1281 name = this->namepool_.add(name, true, &name_key);
1283 Sized_symbol<size>* res;
1284 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1285 is_default_version, *sym, st_shndx,
1286 is_ordinary, st_shndx);
1288 if (is_forced_local)
1289 this->force_local(res);
1294 // Add all the symbols in a dynamic object to the hash table.
1296 template<int size, bool big_endian>
1298 Symbol_table::add_from_dynobj(
1299 Sized_dynobj<size, big_endian>* dynobj,
1300 const unsigned char* syms,
1302 const char* sym_names,
1303 size_t sym_name_size,
1304 const unsigned char* versym,
1306 const std::vector<const char*>* version_map,
1307 typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1312 gold_assert(size == parameters->target().get_size());
1314 if (dynobj->just_symbols())
1316 gold_error(_("--just-symbols does not make sense with a shared object"));
1320 if (versym != NULL && versym_size / 2 < count)
1322 dynobj->error(_("too few symbol versions"));
1326 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1328 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1329 // weak aliases. This is necessary because if the dynamic object
1330 // provides the same variable under two names, one of which is a
1331 // weak definition, and the regular object refers to the weak
1332 // definition, we have to put both the weak definition and the
1333 // strong definition into the dynamic symbol table. Given a weak
1334 // definition, the only way that we can find the corresponding
1335 // strong definition, if any, is to search the symbol table.
1336 std::vector<Sized_symbol<size>*> object_symbols;
1338 const unsigned char* p = syms;
1339 const unsigned char* vs = versym;
1340 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1342 elfcpp::Sym<size, big_endian> sym(p);
1344 if (sympointers != NULL)
1345 (*sympointers)[i] = NULL;
1347 // Ignore symbols with local binding or that have
1348 // internal or hidden visibility.
1349 if (sym.get_st_bind() == elfcpp::STB_LOCAL
1350 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1351 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
1354 // A protected symbol in a shared library must be treated as a
1355 // normal symbol when viewed from outside the shared library.
1356 // Implement this by overriding the visibility here.
1357 elfcpp::Sym<size, big_endian>* psym = &sym;
1358 unsigned char symbuf[sym_size];
1359 elfcpp::Sym<size, big_endian> sym2(symbuf);
1360 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1362 memcpy(symbuf, p, sym_size);
1363 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1364 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1368 unsigned int st_name = psym->get_st_name();
1369 if (st_name >= sym_name_size)
1371 dynobj->error(_("bad symbol name offset %u at %zu"),
1376 const char* name = sym_names + st_name;
1379 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
1382 if (st_shndx != elfcpp::SHN_UNDEF)
1385 Sized_symbol<size>* res;
1389 Stringpool::Key name_key;
1390 name = this->namepool_.add(name, true, &name_key);
1391 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1392 false, *psym, st_shndx, is_ordinary,
1397 // Read the version information.
1399 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
1401 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1402 v &= elfcpp::VERSYM_VERSION;
1404 // The Sun documentation says that V can be VER_NDX_LOCAL,
1405 // or VER_NDX_GLOBAL, or a version index. The meaning of
1406 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1407 // The old GNU linker will happily generate VER_NDX_LOCAL
1408 // for an undefined symbol. I don't know what the Sun
1409 // linker will generate.
1411 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1412 && st_shndx != elfcpp::SHN_UNDEF)
1414 // This symbol should not be visible outside the object.
1418 // At this point we are definitely going to add this symbol.
1419 Stringpool::Key name_key;
1420 name = this->namepool_.add(name, true, &name_key);
1422 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1423 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1425 // This symbol does not have a version.
1426 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1427 false, *psym, st_shndx, is_ordinary,
1432 if (v >= version_map->size())
1434 dynobj->error(_("versym for symbol %zu out of range: %u"),
1439 const char* version = (*version_map)[v];
1440 if (version == NULL)
1442 dynobj->error(_("versym for symbol %zu has no name: %u"),
1447 Stringpool::Key version_key;
1448 version = this->namepool_.add(version, true, &version_key);
1450 // If this is an absolute symbol, and the version name
1451 // and symbol name are the same, then this is the
1452 // version definition symbol. These symbols exist to
1453 // support using -u to pull in particular versions. We
1454 // do not want to record a version for them.
1455 if (st_shndx == elfcpp::SHN_ABS
1457 && name_key == version_key)
1458 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1459 false, *psym, st_shndx, is_ordinary,
1463 const bool is_default_version =
1464 !hidden && st_shndx != elfcpp::SHN_UNDEF;
1465 res = this->add_from_object(dynobj, name, name_key, version,
1466 version_key, is_default_version,
1468 is_ordinary, st_shndx);
1473 // Note that it is possible that RES was overridden by an
1474 // earlier object, in which case it can't be aliased here.
1475 if (st_shndx != elfcpp::SHN_UNDEF
1477 && psym->get_st_type() == elfcpp::STT_OBJECT
1478 && res->source() == Symbol::FROM_OBJECT
1479 && res->object() == dynobj)
1480 object_symbols.push_back(res);
1482 if (sympointers != NULL)
1483 (*sympointers)[i] = res;
1486 this->record_weak_aliases(&object_symbols);
1489 // Add a symbol from a incremental object file.
1491 template<int size, bool big_endian>
1493 Symbol_table::add_from_incrobj(
1497 elfcpp::Sym<size, big_endian>* sym)
1499 unsigned int st_shndx = sym->get_st_shndx();
1500 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1502 Stringpool::Key ver_key = 0;
1503 bool is_default_version = false;
1504 bool is_forced_local = false;
1506 Stringpool::Key name_key;
1507 name = this->namepool_.add(name, true, &name_key);
1509 Sized_symbol<size>* res;
1510 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1511 is_default_version, *sym, st_shndx,
1512 is_ordinary, st_shndx);
1514 if (is_forced_local)
1515 this->force_local(res);
1520 // This is used to sort weak aliases. We sort them first by section
1521 // index, then by offset, then by weak ahead of strong.
1524 class Weak_alias_sorter
1527 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1532 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1533 const Sized_symbol<size>* s2) const
1536 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1537 gold_assert(is_ordinary);
1538 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1539 gold_assert(is_ordinary);
1540 if (s1_shndx != s2_shndx)
1541 return s1_shndx < s2_shndx;
1543 if (s1->value() != s2->value())
1544 return s1->value() < s2->value();
1545 if (s1->binding() != s2->binding())
1547 if (s1->binding() == elfcpp::STB_WEAK)
1549 if (s2->binding() == elfcpp::STB_WEAK)
1552 return std::string(s1->name()) < std::string(s2->name());
1555 // SYMBOLS is a list of object symbols from a dynamic object. Look
1556 // for any weak aliases, and record them so that if we add the weak
1557 // alias to the dynamic symbol table, we also add the corresponding
1562 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1564 // Sort the vector by section index, then by offset, then by weak
1566 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1568 // Walk through the vector. For each weak definition, record
1570 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1572 p != symbols->end();
1575 if ((*p)->binding() != elfcpp::STB_WEAK)
1578 // Build a circular list of weak aliases. Each symbol points to
1579 // the next one in the circular list.
1581 Sized_symbol<size>* from_sym = *p;
1582 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1583 for (q = p + 1; q != symbols->end(); ++q)
1586 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1587 || (*q)->value() != from_sym->value())
1590 this->weak_aliases_[from_sym] = *q;
1591 from_sym->set_has_alias();
1597 this->weak_aliases_[from_sym] = *p;
1598 from_sym->set_has_alias();
1605 // Create and return a specially defined symbol. If ONLY_IF_REF is
1606 // true, then only create the symbol if there is a reference to it.
1607 // If this does not return NULL, it sets *POLDSYM to the existing
1608 // symbol if there is one. This sets *RESOLVE_OLDSYM if we should
1609 // resolve the newly created symbol to the old one. This
1610 // canonicalizes *PNAME and *PVERSION.
1612 template<int size, bool big_endian>
1614 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1616 Sized_symbol<size>** poldsym,
1617 bool* resolve_oldsym)
1619 *resolve_oldsym = false;
1621 // If the caller didn't give us a version, see if we get one from
1622 // the version script.
1624 bool is_default_version = false;
1625 if (*pversion == NULL)
1628 if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
1630 if (is_global && !v.empty())
1632 *pversion = v.c_str();
1633 // If we get the version from a version script, then we
1634 // are also the default version.
1635 is_default_version = true;
1641 Sized_symbol<size>* sym;
1643 bool add_to_table = false;
1644 typename Symbol_table_type::iterator add_loc = this->table_.end();
1645 bool add_def_to_table = false;
1646 typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1650 oldsym = this->lookup(*pname, *pversion);
1651 if (oldsym == NULL && is_default_version)
1652 oldsym = this->lookup(*pname, NULL);
1653 if (oldsym == NULL || !oldsym->is_undefined())
1656 *pname = oldsym->name();
1657 if (!is_default_version)
1658 *pversion = oldsym->version();
1662 // Canonicalize NAME and VERSION.
1663 Stringpool::Key name_key;
1664 *pname = this->namepool_.add(*pname, true, &name_key);
1666 Stringpool::Key version_key = 0;
1667 if (*pversion != NULL)
1668 *pversion = this->namepool_.add(*pversion, true, &version_key);
1670 Symbol* const snull = NULL;
1671 std::pair<typename Symbol_table_type::iterator, bool> ins =
1672 this->table_.insert(std::make_pair(std::make_pair(name_key,
1676 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
1677 std::make_pair(this->table_.end(), false);
1678 if (is_default_version)
1680 const Stringpool::Key vnull = 0;
1682 this->table_.insert(std::make_pair(std::make_pair(name_key,
1689 // We already have a symbol table entry for NAME/VERSION.
1690 oldsym = ins.first->second;
1691 gold_assert(oldsym != NULL);
1693 if (is_default_version)
1695 Sized_symbol<size>* soldsym =
1696 this->get_sized_symbol<size>(oldsym);
1697 this->define_default_version<size, big_endian>(soldsym,
1704 // We haven't seen this symbol before.
1705 gold_assert(ins.first->second == NULL);
1707 add_to_table = true;
1708 add_loc = ins.first;
1710 if (is_default_version && !insdefault.second)
1712 // We are adding NAME/VERSION, and it is the default
1713 // version. We already have an entry for NAME/NULL.
1714 oldsym = insdefault.first->second;
1715 *resolve_oldsym = true;
1721 if (is_default_version)
1723 add_def_to_table = true;
1724 add_def_loc = insdefault.first;
1730 const Target& target = parameters->target();
1731 if (!target.has_make_symbol())
1732 sym = new Sized_symbol<size>();
1735 Sized_target<size, big_endian>* sized_target =
1736 parameters->sized_target<size, big_endian>();
1737 sym = sized_target->make_symbol();
1743 add_loc->second = sym;
1745 gold_assert(oldsym != NULL);
1747 if (add_def_to_table)
1748 add_def_loc->second = sym;
1750 *poldsym = this->get_sized_symbol<size>(oldsym);
1755 // Define a symbol based on an Output_data.
1758 Symbol_table::define_in_output_data(const char* name,
1759 const char* version,
1765 elfcpp::STB binding,
1766 elfcpp::STV visibility,
1767 unsigned char nonvis,
1768 bool offset_is_from_end,
1771 if (parameters->target().get_size() == 32)
1773 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1774 return this->do_define_in_output_data<32>(name, version, defined, od,
1775 value, symsize, type, binding,
1783 else if (parameters->target().get_size() == 64)
1785 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1786 return this->do_define_in_output_data<64>(name, version, defined, od,
1787 value, symsize, type, binding,
1799 // Define a symbol in an Output_data, sized version.
1803 Symbol_table::do_define_in_output_data(
1805 const char* version,
1808 typename elfcpp::Elf_types<size>::Elf_Addr value,
1809 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1811 elfcpp::STB binding,
1812 elfcpp::STV visibility,
1813 unsigned char nonvis,
1814 bool offset_is_from_end,
1817 Sized_symbol<size>* sym;
1818 Sized_symbol<size>* oldsym;
1819 bool resolve_oldsym;
1821 if (parameters->target().is_big_endian())
1823 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1824 sym = this->define_special_symbol<size, true>(&name, &version,
1825 only_if_ref, &oldsym,
1833 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1834 sym = this->define_special_symbol<size, false>(&name, &version,
1835 only_if_ref, &oldsym,
1845 sym->init_output_data(name, version, od, value, symsize, type, binding,
1846 visibility, nonvis, offset_is_from_end);
1850 if (binding == elfcpp::STB_LOCAL
1851 || this->version_script_.symbol_is_local(name))
1852 this->force_local(sym);
1853 else if (version != NULL)
1854 sym->set_is_default();
1858 if (Symbol_table::should_override_with_special(oldsym, defined))
1859 this->override_with_special(oldsym, sym);
1870 // Define a symbol based on an Output_segment.
1873 Symbol_table::define_in_output_segment(const char* name,
1874 const char* version,
1880 elfcpp::STB binding,
1881 elfcpp::STV visibility,
1882 unsigned char nonvis,
1883 Symbol::Segment_offset_base offset_base,
1886 if (parameters->target().get_size() == 32)
1888 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1889 return this->do_define_in_output_segment<32>(name, version, defined, os,
1890 value, symsize, type,
1891 binding, visibility, nonvis,
1892 offset_base, only_if_ref);
1897 else if (parameters->target().get_size() == 64)
1899 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1900 return this->do_define_in_output_segment<64>(name, version, defined, os,
1901 value, symsize, type,
1902 binding, visibility, nonvis,
1903 offset_base, only_if_ref);
1912 // Define a symbol in an Output_segment, sized version.
1916 Symbol_table::do_define_in_output_segment(
1918 const char* version,
1921 typename elfcpp::Elf_types<size>::Elf_Addr value,
1922 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1924 elfcpp::STB binding,
1925 elfcpp::STV visibility,
1926 unsigned char nonvis,
1927 Symbol::Segment_offset_base offset_base,
1930 Sized_symbol<size>* sym;
1931 Sized_symbol<size>* oldsym;
1932 bool resolve_oldsym;
1934 if (parameters->target().is_big_endian())
1936 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1937 sym = this->define_special_symbol<size, true>(&name, &version,
1938 only_if_ref, &oldsym,
1946 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1947 sym = this->define_special_symbol<size, false>(&name, &version,
1948 only_if_ref, &oldsym,
1958 sym->init_output_segment(name, version, os, value, symsize, type, binding,
1959 visibility, nonvis, offset_base);
1963 if (binding == elfcpp::STB_LOCAL
1964 || this->version_script_.symbol_is_local(name))
1965 this->force_local(sym);
1966 else if (version != NULL)
1967 sym->set_is_default();
1971 if (Symbol_table::should_override_with_special(oldsym, defined))
1972 this->override_with_special(oldsym, sym);
1983 // Define a special symbol with a constant value. It is a multiple
1984 // definition error if this symbol is already defined.
1987 Symbol_table::define_as_constant(const char* name,
1988 const char* version,
1993 elfcpp::STB binding,
1994 elfcpp::STV visibility,
1995 unsigned char nonvis,
1997 bool force_override)
1999 if (parameters->target().get_size() == 32)
2001 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2002 return this->do_define_as_constant<32>(name, version, defined, value,
2003 symsize, type, binding,
2004 visibility, nonvis, only_if_ref,
2010 else if (parameters->target().get_size() == 64)
2012 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2013 return this->do_define_as_constant<64>(name, version, defined, value,
2014 symsize, type, binding,
2015 visibility, nonvis, only_if_ref,
2025 // Define a symbol as a constant, sized version.
2029 Symbol_table::do_define_as_constant(
2031 const char* version,
2033 typename elfcpp::Elf_types<size>::Elf_Addr value,
2034 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
2036 elfcpp::STB binding,
2037 elfcpp::STV visibility,
2038 unsigned char nonvis,
2040 bool force_override)
2042 Sized_symbol<size>* sym;
2043 Sized_symbol<size>* oldsym;
2044 bool resolve_oldsym;
2046 if (parameters->target().is_big_endian())
2048 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2049 sym = this->define_special_symbol<size, true>(&name, &version,
2050 only_if_ref, &oldsym,
2058 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2059 sym = this->define_special_symbol<size, false>(&name, &version,
2060 only_if_ref, &oldsym,
2070 sym->init_constant(name, version, value, symsize, type, binding, visibility,
2075 // Version symbols are absolute symbols with name == version.
2076 // We don't want to force them to be local.
2077 if ((version == NULL
2080 && (binding == elfcpp::STB_LOCAL
2081 || this->version_script_.symbol_is_local(name)))
2082 this->force_local(sym);
2083 else if (version != NULL
2084 && (name != version || value != 0))
2085 sym->set_is_default();
2090 || Symbol_table::should_override_with_special(oldsym, defined))
2091 this->override_with_special(oldsym, sym);
2102 // Define a set of symbols in output sections.
2105 Symbol_table::define_symbols(const Layout* layout, int count,
2106 const Define_symbol_in_section* p,
2109 for (int i = 0; i < count; ++i, ++p)
2111 Output_section* os = layout->find_output_section(p->output_section);
2113 this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
2114 p->size, p->type, p->binding,
2115 p->visibility, p->nonvis,
2116 p->offset_is_from_end,
2117 only_if_ref || p->only_if_ref);
2119 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2120 p->type, p->binding, p->visibility, p->nonvis,
2121 only_if_ref || p->only_if_ref,
2126 // Define a set of symbols in output segments.
2129 Symbol_table::define_symbols(const Layout* layout, int count,
2130 const Define_symbol_in_segment* p,
2133 for (int i = 0; i < count; ++i, ++p)
2135 Output_segment* os = layout->find_output_segment(p->segment_type,
2136 p->segment_flags_set,
2137 p->segment_flags_clear);
2139 this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
2140 p->size, p->type, p->binding,
2141 p->visibility, p->nonvis,
2143 only_if_ref || p->only_if_ref);
2145 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2146 p->type, p->binding, p->visibility, p->nonvis,
2147 only_if_ref || p->only_if_ref,
2152 // Define CSYM using a COPY reloc. POSD is the Output_data where the
2153 // symbol should be defined--typically a .dyn.bss section. VALUE is
2154 // the offset within POSD.
2158 Symbol_table::define_with_copy_reloc(
2159 Sized_symbol<size>* csym,
2161 typename elfcpp::Elf_types<size>::Elf_Addr value)
2163 gold_assert(csym->is_from_dynobj());
2164 gold_assert(!csym->is_copied_from_dynobj());
2165 Object* object = csym->object();
2166 gold_assert(object->is_dynamic());
2167 Dynobj* dynobj = static_cast<Dynobj*>(object);
2169 // Our copied variable has to override any variable in a shared
2171 elfcpp::STB binding = csym->binding();
2172 if (binding == elfcpp::STB_WEAK)
2173 binding = elfcpp::STB_GLOBAL;
2175 this->define_in_output_data(csym->name(), csym->version(), COPY,
2176 posd, value, csym->symsize(),
2177 csym->type(), binding,
2178 csym->visibility(), csym->nonvis(),
2181 csym->set_is_copied_from_dynobj();
2182 csym->set_needs_dynsym_entry();
2184 this->copied_symbol_dynobjs_[csym] = dynobj;
2186 // We have now defined all aliases, but we have not entered them all
2187 // in the copied_symbol_dynobjs_ map.
2188 if (csym->has_alias())
2193 sym = this->weak_aliases_[sym];
2196 gold_assert(sym->output_data() == posd);
2198 sym->set_is_copied_from_dynobj();
2199 this->copied_symbol_dynobjs_[sym] = dynobj;
2204 // SYM is defined using a COPY reloc. Return the dynamic object where
2205 // the original definition was found.
2208 Symbol_table::get_copy_source(const Symbol* sym) const
2210 gold_assert(sym->is_copied_from_dynobj());
2211 Copied_symbol_dynobjs::const_iterator p =
2212 this->copied_symbol_dynobjs_.find(sym);
2213 gold_assert(p != this->copied_symbol_dynobjs_.end());
2217 // Add any undefined symbols named on the command line.
2220 Symbol_table::add_undefined_symbols_from_command_line(Layout* layout)
2222 if (parameters->options().any_undefined()
2223 || layout->script_options()->any_unreferenced())
2225 if (parameters->target().get_size() == 32)
2227 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2228 this->do_add_undefined_symbols_from_command_line<32>(layout);
2233 else if (parameters->target().get_size() == 64)
2235 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2236 this->do_add_undefined_symbols_from_command_line<64>(layout);
2248 Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout)
2250 for (options::String_set::const_iterator p =
2251 parameters->options().undefined_begin();
2252 p != parameters->options().undefined_end();
2254 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2256 for (Script_options::referenced_const_iterator p =
2257 layout->script_options()->referenced_begin();
2258 p != layout->script_options()->referenced_end();
2260 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2265 Symbol_table::add_undefined_symbol_from_command_line(const char* name)
2267 if (this->lookup(name) != NULL)
2270 const char* version = NULL;
2272 Sized_symbol<size>* sym;
2273 Sized_symbol<size>* oldsym;
2274 bool resolve_oldsym;
2275 if (parameters->target().is_big_endian())
2277 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2278 sym = this->define_special_symbol<size, true>(&name, &version,
2287 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2288 sym = this->define_special_symbol<size, false>(&name, &version,
2296 gold_assert(oldsym == NULL);
2298 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2299 elfcpp::STV_DEFAULT, 0);
2300 ++this->saw_undefined_;
2303 // Set the dynamic symbol indexes. INDEX is the index of the first
2304 // global dynamic symbol. Pointers to the symbols are stored into the
2305 // vector SYMS. The names are added to DYNPOOL. This returns an
2306 // updated dynamic symbol index.
2309 Symbol_table::set_dynsym_indexes(unsigned int index,
2310 std::vector<Symbol*>* syms,
2311 Stringpool* dynpool,
2314 for (Symbol_table_type::iterator p = this->table_.begin();
2315 p != this->table_.end();
2318 Symbol* sym = p->second;
2320 // Note that SYM may already have a dynamic symbol index, since
2321 // some symbols appear more than once in the symbol table, with
2322 // and without a version.
2324 if (!sym->should_add_dynsym_entry(this))
2325 sym->set_dynsym_index(-1U);
2326 else if (!sym->has_dynsym_index())
2328 sym->set_dynsym_index(index);
2330 syms->push_back(sym);
2331 dynpool->add(sym->name(), false, NULL);
2333 // Record any version information.
2334 if (sym->version() != NULL)
2335 versions->record_version(this, dynpool, sym);
2337 // If the symbol is defined in a dynamic object and is
2338 // referenced in a regular object, then mark the dynamic
2339 // object as needed. This is used to implement --as-needed.
2340 if (sym->is_from_dynobj() && sym->in_reg())
2341 sym->object()->set_is_needed();
2345 // Finish up the versions. In some cases this may add new dynamic
2347 index = versions->finalize(this, index, syms);
2352 // Set the final values for all the symbols. The index of the first
2353 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2354 // file offset OFF. Add their names to POOL. Return the new file
2355 // offset. Update *PLOCAL_SYMCOUNT if necessary.
2358 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2359 size_t dyncount, Stringpool* pool,
2360 unsigned int* plocal_symcount)
2364 gold_assert(*plocal_symcount != 0);
2365 this->first_global_index_ = *plocal_symcount;
2367 this->dynamic_offset_ = dynoff;
2368 this->first_dynamic_global_index_ = dyn_global_index;
2369 this->dynamic_count_ = dyncount;
2371 if (parameters->target().get_size() == 32)
2373 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2374 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
2379 else if (parameters->target().get_size() == 64)
2381 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2382 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
2390 // Now that we have the final symbol table, we can reliably note
2391 // which symbols should get warnings.
2392 this->warnings_.note_warnings(this);
2397 // SYM is going into the symbol table at *PINDEX. Add the name to
2398 // POOL, update *PINDEX and *POFF.
2402 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2403 unsigned int* pindex, off_t* poff)
2405 sym->set_symtab_index(*pindex);
2406 pool->add(sym->name(), false, NULL);
2408 *poff += elfcpp::Elf_sizes<size>::sym_size;
2411 // Set the final value for all the symbols. This is called after
2412 // Layout::finalize, so all the output sections have their final
2417 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2418 unsigned int* plocal_symcount)
2420 off = align_address(off, size >> 3);
2421 this->offset_ = off;
2423 unsigned int index = *plocal_symcount;
2424 const unsigned int orig_index = index;
2426 // First do all the symbols which have been forced to be local, as
2427 // they must appear before all global symbols.
2428 for (Forced_locals::iterator p = this->forced_locals_.begin();
2429 p != this->forced_locals_.end();
2433 gold_assert(sym->is_forced_local());
2434 if (this->sized_finalize_symbol<size>(sym))
2436 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2441 // Now do all the remaining symbols.
2442 for (Symbol_table_type::iterator p = this->table_.begin();
2443 p != this->table_.end();
2446 Symbol* sym = p->second;
2447 if (this->sized_finalize_symbol<size>(sym))
2448 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2451 this->output_count_ = index - orig_index;
2456 // Compute the final value of SYM and store status in location PSTATUS.
2457 // During relaxation, this may be called multiple times for a symbol to
2458 // compute its would-be final value in each relaxation pass.
2461 typename Sized_symbol<size>::Value_type
2462 Symbol_table::compute_final_value(
2463 const Sized_symbol<size>* sym,
2464 Compute_final_value_status* pstatus) const
2466 typedef typename Sized_symbol<size>::Value_type Value_type;
2469 switch (sym->source())
2471 case Symbol::FROM_OBJECT:
2474 unsigned int shndx = sym->shndx(&is_ordinary);
2477 && shndx != elfcpp::SHN_ABS
2478 && !Symbol::is_common_shndx(shndx))
2480 *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2484 Object* symobj = sym->object();
2485 if (symobj->is_dynamic())
2488 shndx = elfcpp::SHN_UNDEF;
2490 else if (symobj->pluginobj() != NULL)
2493 shndx = elfcpp::SHN_UNDEF;
2495 else if (shndx == elfcpp::SHN_UNDEF)
2497 else if (!is_ordinary
2498 && (shndx == elfcpp::SHN_ABS
2499 || Symbol::is_common_shndx(shndx)))
2500 value = sym->value();
2503 Relobj* relobj = static_cast<Relobj*>(symobj);
2504 Output_section* os = relobj->output_section(shndx);
2506 if (this->is_section_folded(relobj, shndx))
2508 gold_assert(os == NULL);
2509 // Get the os of the section it is folded onto.
2510 Section_id folded = this->icf_->get_folded_section(relobj,
2512 gold_assert(folded.first != NULL);
2513 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
2514 unsigned folded_shndx = folded.second;
2516 os = folded_obj->output_section(folded_shndx);
2517 gold_assert(os != NULL);
2519 // Replace (relobj, shndx) with canonical ICF input section.
2520 shndx = folded_shndx;
2521 relobj = folded_obj;
2524 uint64_t secoff64 = relobj->output_section_offset(shndx);
2527 bool static_or_reloc = (parameters->doing_static_link() ||
2528 parameters->options().relocatable());
2529 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2531 *pstatus = CFVS_NO_OUTPUT_SECTION;
2535 if (secoff64 == -1ULL)
2537 // The section needs special handling (e.g., a merge section).
2539 value = os->output_address(relobj, shndx, sym->value());
2544 convert_types<Value_type, uint64_t>(secoff64);
2545 if (sym->type() == elfcpp::STT_TLS)
2546 value = sym->value() + os->tls_offset() + secoff;
2548 value = sym->value() + os->address() + secoff;
2554 case Symbol::IN_OUTPUT_DATA:
2556 Output_data* od = sym->output_data();
2557 value = sym->value();
2558 if (sym->type() != elfcpp::STT_TLS)
2559 value += od->address();
2562 Output_section* os = od->output_section();
2563 gold_assert(os != NULL);
2564 value += os->tls_offset() + (od->address() - os->address());
2566 if (sym->offset_is_from_end())
2567 value += od->data_size();
2571 case Symbol::IN_OUTPUT_SEGMENT:
2573 Output_segment* os = sym->output_segment();
2574 value = sym->value();
2575 if (sym->type() != elfcpp::STT_TLS)
2576 value += os->vaddr();
2577 switch (sym->offset_base())
2579 case Symbol::SEGMENT_START:
2581 case Symbol::SEGMENT_END:
2582 value += os->memsz();
2584 case Symbol::SEGMENT_BSS:
2585 value += os->filesz();
2593 case Symbol::IS_CONSTANT:
2594 value = sym->value();
2597 case Symbol::IS_UNDEFINED:
2609 // Finalize the symbol SYM. This returns true if the symbol should be
2610 // added to the symbol table, false otherwise.
2614 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2616 typedef typename Sized_symbol<size>::Value_type Value_type;
2618 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2620 // The default version of a symbol may appear twice in the symbol
2621 // table. We only need to finalize it once.
2622 if (sym->has_symtab_index())
2627 gold_assert(!sym->has_symtab_index());
2628 sym->set_symtab_index(-1U);
2629 gold_assert(sym->dynsym_index() == -1U);
2633 // If the symbol is only present on plugin files, the plugin decided we
2635 if (!sym->in_real_elf())
2637 gold_assert(!sym->has_symtab_index());
2638 sym->set_symtab_index(-1U);
2642 // Compute final symbol value.
2643 Compute_final_value_status status;
2644 Value_type value = this->compute_final_value(sym, &status);
2650 case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2653 unsigned int shndx = sym->shndx(&is_ordinary);
2654 gold_error(_("%s: unsupported symbol section 0x%x"),
2655 sym->demangled_name().c_str(), shndx);
2658 case CFVS_NO_OUTPUT_SECTION:
2659 sym->set_symtab_index(-1U);
2665 sym->set_value(value);
2667 if (parameters->options().strip_all()
2668 || !parameters->options().should_retain_symbol(sym->name()))
2670 sym->set_symtab_index(-1U);
2677 // Write out the global symbols.
2680 Symbol_table::write_globals(const Stringpool* sympool,
2681 const Stringpool* dynpool,
2682 Output_symtab_xindex* symtab_xindex,
2683 Output_symtab_xindex* dynsym_xindex,
2684 Output_file* of) const
2686 switch (parameters->size_and_endianness())
2688 #ifdef HAVE_TARGET_32_LITTLE
2689 case Parameters::TARGET_32_LITTLE:
2690 this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
2694 #ifdef HAVE_TARGET_32_BIG
2695 case Parameters::TARGET_32_BIG:
2696 this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
2700 #ifdef HAVE_TARGET_64_LITTLE
2701 case Parameters::TARGET_64_LITTLE:
2702 this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
2706 #ifdef HAVE_TARGET_64_BIG
2707 case Parameters::TARGET_64_BIG:
2708 this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
2717 // Write out the global symbols.
2719 template<int size, bool big_endian>
2721 Symbol_table::sized_write_globals(const Stringpool* sympool,
2722 const Stringpool* dynpool,
2723 Output_symtab_xindex* symtab_xindex,
2724 Output_symtab_xindex* dynsym_xindex,
2725 Output_file* of) const
2727 const Target& target = parameters->target();
2729 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2731 const unsigned int output_count = this->output_count_;
2732 const section_size_type oview_size = output_count * sym_size;
2733 const unsigned int first_global_index = this->first_global_index_;
2734 unsigned char* psyms;
2735 if (this->offset_ == 0 || output_count == 0)
2738 psyms = of->get_output_view(this->offset_, oview_size);
2740 const unsigned int dynamic_count = this->dynamic_count_;
2741 const section_size_type dynamic_size = dynamic_count * sym_size;
2742 const unsigned int first_dynamic_global_index =
2743 this->first_dynamic_global_index_;
2744 unsigned char* dynamic_view;
2745 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2746 dynamic_view = NULL;
2748 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2750 for (Symbol_table_type::const_iterator p = this->table_.begin();
2751 p != this->table_.end();
2754 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2756 // Possibly warn about unresolved symbols in shared libraries.
2757 this->warn_about_undefined_dynobj_symbol(sym);
2759 unsigned int sym_index = sym->symtab_index();
2760 unsigned int dynsym_index;
2761 if (dynamic_view == NULL)
2764 dynsym_index = sym->dynsym_index();
2766 if (sym_index == -1U && dynsym_index == -1U)
2768 // This symbol is not included in the output file.
2773 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2774 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
2775 elfcpp::STB binding = sym->binding();
2776 switch (sym->source())
2778 case Symbol::FROM_OBJECT:
2781 unsigned int in_shndx = sym->shndx(&is_ordinary);
2784 && in_shndx != elfcpp::SHN_ABS
2785 && !Symbol::is_common_shndx(in_shndx))
2787 gold_error(_("%s: unsupported symbol section 0x%x"),
2788 sym->demangled_name().c_str(), in_shndx);
2793 Object* symobj = sym->object();
2794 if (symobj->is_dynamic())
2796 if (sym->needs_dynsym_value())
2797 dynsym_value = target.dynsym_value(sym);
2798 shndx = elfcpp::SHN_UNDEF;
2799 if (sym->is_undef_binding_weak())
2800 binding = elfcpp::STB_WEAK;
2802 binding = elfcpp::STB_GLOBAL;
2804 else if (symobj->pluginobj() != NULL)
2805 shndx = elfcpp::SHN_UNDEF;
2806 else if (in_shndx == elfcpp::SHN_UNDEF
2808 && (in_shndx == elfcpp::SHN_ABS
2809 || Symbol::is_common_shndx(in_shndx))))
2813 Relobj* relobj = static_cast<Relobj*>(symobj);
2814 Output_section* os = relobj->output_section(in_shndx);
2815 if (this->is_section_folded(relobj, in_shndx))
2817 // This global symbol must be written out even though
2819 // Get the os of the section it is folded onto.
2821 this->icf_->get_folded_section(relobj, in_shndx);
2822 gold_assert(folded.first !=NULL);
2823 Relobj* folded_obj =
2824 reinterpret_cast<Relobj*>(folded.first);
2825 os = folded_obj->output_section(folded.second);
2826 gold_assert(os != NULL);
2828 gold_assert(os != NULL);
2829 shndx = os->out_shndx();
2831 if (shndx >= elfcpp::SHN_LORESERVE)
2833 if (sym_index != -1U)
2834 symtab_xindex->add(sym_index, shndx);
2835 if (dynsym_index != -1U)
2836 dynsym_xindex->add(dynsym_index, shndx);
2837 shndx = elfcpp::SHN_XINDEX;
2840 // In object files symbol values are section
2842 if (parameters->options().relocatable())
2843 sym_value -= os->address();
2849 case Symbol::IN_OUTPUT_DATA:
2850 shndx = sym->output_data()->out_shndx();
2851 if (shndx >= elfcpp::SHN_LORESERVE)
2853 if (sym_index != -1U)
2854 symtab_xindex->add(sym_index, shndx);
2855 if (dynsym_index != -1U)
2856 dynsym_xindex->add(dynsym_index, shndx);
2857 shndx = elfcpp::SHN_XINDEX;
2861 case Symbol::IN_OUTPUT_SEGMENT:
2862 shndx = elfcpp::SHN_ABS;
2865 case Symbol::IS_CONSTANT:
2866 shndx = elfcpp::SHN_ABS;
2869 case Symbol::IS_UNDEFINED:
2870 shndx = elfcpp::SHN_UNDEF;
2877 if (sym_index != -1U)
2879 sym_index -= first_global_index;
2880 gold_assert(sym_index < output_count);
2881 unsigned char* ps = psyms + (sym_index * sym_size);
2882 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2883 binding, sympool, ps);
2886 if (dynsym_index != -1U)
2888 dynsym_index -= first_dynamic_global_index;
2889 gold_assert(dynsym_index < dynamic_count);
2890 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2891 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2892 binding, dynpool, pd);
2896 of->write_output_view(this->offset_, oview_size, psyms);
2897 if (dynamic_view != NULL)
2898 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2901 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2902 // strtab holding the name.
2904 template<int size, bool big_endian>
2906 Symbol_table::sized_write_symbol(
2907 Sized_symbol<size>* sym,
2908 typename elfcpp::Elf_types<size>::Elf_Addr value,
2910 elfcpp::STB binding,
2911 const Stringpool* pool,
2912 unsigned char* p) const
2914 elfcpp::Sym_write<size, big_endian> osym(p);
2915 osym.put_st_name(pool->get_offset(sym->name()));
2916 osym.put_st_value(value);
2917 // Use a symbol size of zero for undefined symbols from shared libraries.
2918 if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
2919 osym.put_st_size(0);
2921 osym.put_st_size(sym->symsize());
2922 elfcpp::STT type = sym->type();
2923 // Turn IFUNC symbols from shared libraries into normal FUNC symbols.
2924 if (type == elfcpp::STT_GNU_IFUNC
2925 && sym->is_from_dynobj())
2926 type = elfcpp::STT_FUNC;
2927 // A version script may have overridden the default binding.
2928 if (sym->is_forced_local())
2929 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
2931 osym.put_st_info(elfcpp::elf_st_info(binding, type));
2932 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2933 osym.put_st_shndx(shndx);
2936 // Check for unresolved symbols in shared libraries. This is
2937 // controlled by the --allow-shlib-undefined option.
2939 // We only warn about libraries for which we have seen all the
2940 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2941 // which were not seen in this link. If we didn't see a DT_NEEDED
2942 // entry, we aren't going to be able to reliably report whether the
2943 // symbol is undefined.
2945 // We also don't warn about libraries found in a system library
2946 // directory (e.g., /lib or /usr/lib); we assume that those libraries
2947 // are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
2948 // can have undefined references satisfied by ld-linux.so.
2951 Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
2954 if (sym->source() == Symbol::FROM_OBJECT
2955 && sym->object()->is_dynamic()
2956 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
2957 && sym->binding() != elfcpp::STB_WEAK
2958 && !parameters->options().allow_shlib_undefined()
2959 && !parameters->target().is_defined_by_abi(sym)
2960 && !sym->object()->is_in_system_directory())
2962 // A very ugly cast.
2963 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2964 if (!dynobj->has_unknown_needed_entries())
2965 gold_undefined_symbol(sym);
2969 // Write out a section symbol. Return the update offset.
2972 Symbol_table::write_section_symbol(const Output_section* os,
2973 Output_symtab_xindex* symtab_xindex,
2977 switch (parameters->size_and_endianness())
2979 #ifdef HAVE_TARGET_32_LITTLE
2980 case Parameters::TARGET_32_LITTLE:
2981 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2985 #ifdef HAVE_TARGET_32_BIG
2986 case Parameters::TARGET_32_BIG:
2987 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2991 #ifdef HAVE_TARGET_64_LITTLE
2992 case Parameters::TARGET_64_LITTLE:
2993 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2997 #ifdef HAVE_TARGET_64_BIG
2998 case Parameters::TARGET_64_BIG:
2999 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
3008 // Write out a section symbol, specialized for size and endianness.
3010 template<int size, bool big_endian>
3012 Symbol_table::sized_write_section_symbol(const Output_section* os,
3013 Output_symtab_xindex* symtab_xindex,
3017 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
3019 unsigned char* pov = of->get_output_view(offset, sym_size);
3021 elfcpp::Sym_write<size, big_endian> osym(pov);
3022 osym.put_st_name(0);
3023 if (parameters->options().relocatable())
3024 osym.put_st_value(0);
3026 osym.put_st_value(os->address());
3027 osym.put_st_size(0);
3028 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
3029 elfcpp::STT_SECTION));
3030 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
3032 unsigned int shndx = os->out_shndx();
3033 if (shndx >= elfcpp::SHN_LORESERVE)
3035 symtab_xindex->add(os->symtab_index(), shndx);
3036 shndx = elfcpp::SHN_XINDEX;
3038 osym.put_st_shndx(shndx);
3040 of->write_output_view(offset, sym_size, pov);
3043 // Print statistical information to stderr. This is used for --stats.
3046 Symbol_table::print_stats() const
3048 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
3049 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
3050 program_name, this->table_.size(), this->table_.bucket_count());
3052 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
3053 program_name, this->table_.size());
3055 this->namepool_.print_stats("symbol table stringpool");
3058 // We check for ODR violations by looking for symbols with the same
3059 // name for which the debugging information reports that they were
3060 // defined in disjoint source locations. When comparing the source
3061 // location, we consider instances with the same base filename to be
3062 // the same. This is because different object files/shared libraries
3063 // can include the same header file using different paths, and
3064 // different optimization settings can make the line number appear to
3065 // be a couple lines off, and we don't want to report an ODR violation
3068 // This struct is used to compare line information, as returned by
3069 // Dwarf_line_info::one_addr2line. It implements a < comparison
3070 // operator used with std::sort.
3072 struct Odr_violation_compare
3075 operator()(const std::string& s1, const std::string& s2) const
3077 // Inputs should be of the form "dirname/filename:linenum" where
3078 // "dirname/" is optional. We want to compare just the filename:linenum.
3080 // Find the last '/' in each string.
3081 std::string::size_type s1begin = s1.rfind('/');
3082 std::string::size_type s2begin = s2.rfind('/');
3083 // If there was no '/' in a string, start at the beginning.
3084 if (s1begin == std::string::npos)
3086 if (s2begin == std::string::npos)
3088 return s1.compare(s1begin, std::string::npos,
3089 s2, s2begin, std::string::npos) < 0;
3093 // Returns all of the lines attached to LOC, not just the one the
3094 // instruction actually came from.
3095 std::vector<std::string>
3096 Symbol_table::linenos_from_loc(const Task* task,
3097 const Symbol_location& loc)
3099 // We need to lock the object in order to read it. This
3100 // means that we have to run in a singleton Task. If we
3101 // want to run this in a general Task for better
3102 // performance, we will need one Task for object, plus
3103 // appropriate locking to ensure that we don't conflict with
3104 // other uses of the object. Also note, one_addr2line is not
3105 // currently thread-safe.
3106 Task_lock_obj<Object> tl(task, loc.object);
3108 std::vector<std::string> result;
3109 // 16 is the size of the object-cache that one_addr2line should use.
3110 std::string canonical_result = Dwarf_line_info::one_addr2line(
3111 loc.object, loc.shndx, loc.offset, 16, &result);
3112 if (!canonical_result.empty())
3113 result.push_back(canonical_result);
3117 // OutputIterator that records if it was ever assigned to. This
3118 // allows it to be used with std::set_intersection() to check for
3119 // intersection rather than computing the intersection.
3120 struct Check_intersection
3122 Check_intersection()
3126 bool had_intersection() const
3127 { return this->value_; }
3129 Check_intersection& operator++()
3132 Check_intersection& operator*()
3135 template<typename T>
3136 Check_intersection& operator=(const T&)
3138 this->value_ = true;
3146 // Check candidate_odr_violations_ to find symbols with the same name
3147 // but apparently different definitions (different source-file/line-no
3148 // for each line assigned to the first instruction).
3151 Symbol_table::detect_odr_violations(const Task* task,
3152 const char* output_file_name) const
3154 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3155 it != candidate_odr_violations_.end();
3158 const char* const symbol_name = it->first;
3160 std::string first_object_name;
3161 std::vector<std::string> first_object_linenos;
3163 Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3164 locs = it->second.begin();
3165 const Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3166 locs_end = it->second.end();
3167 for (; locs != locs_end && first_object_linenos.empty(); ++locs)
3169 // Save the line numbers from the first definition to
3170 // compare to the other definitions. Ideally, we'd compare
3171 // every definition to every other, but we don't want to
3172 // take O(N^2) time to do this. This shortcut may cause
3173 // false negatives that appear or disappear depending on the
3174 // link order, but it won't cause false positives.
3175 first_object_name = locs->object->name();
3176 first_object_linenos = this->linenos_from_loc(task, *locs);
3179 // Sort by Odr_violation_compare to make std::set_intersection work.
3180 std::sort(first_object_linenos.begin(), first_object_linenos.end(),
3181 Odr_violation_compare());
3183 for (; locs != locs_end; ++locs)
3185 std::vector<std::string> linenos =
3186 this->linenos_from_loc(task, *locs);
3187 // linenos will be empty if we couldn't parse the debug info.
3188 if (linenos.empty())
3190 // Sort by Odr_violation_compare to make std::set_intersection work.
3191 std::sort(linenos.begin(), linenos.end(), Odr_violation_compare());
3193 Check_intersection intersection_result =
3194 std::set_intersection(first_object_linenos.begin(),
3195 first_object_linenos.end(),
3198 Check_intersection(),
3199 Odr_violation_compare());
3200 if (!intersection_result.had_intersection())
3202 gold_warning(_("while linking %s: symbol '%s' defined in "
3203 "multiple places (possible ODR violation):"),
3204 output_file_name, demangle(symbol_name).c_str());
3205 // This only prints one location from each definition,
3206 // which may not be the location we expect to intersect
3207 // with another definition. We could print the whole
3208 // set of locations, but that seems too verbose.
3209 gold_assert(!first_object_linenos.empty());
3210 gold_assert(!linenos.empty());
3211 fprintf(stderr, _(" %s from %s\n"),
3212 first_object_linenos[0].c_str(),
3213 first_object_name.c_str());
3214 fprintf(stderr, _(" %s from %s\n"),
3216 locs->object->name().c_str());
3217 // Only print one broken pair, to avoid needing to
3218 // compare against a list of the disjoint definition
3219 // locations we've found so far. (If we kept comparing
3220 // against just the first one, we'd get a lot of
3221 // redundant complaints about the second definition
3227 // We only call one_addr2line() in this function, so we can clear its cache.
3228 Dwarf_line_info::clear_addr2line_cache();
3231 // Warnings functions.
3233 // Add a new warning.
3236 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
3237 const std::string& warning)
3239 name = symtab->canonicalize_name(name);
3240 this->warnings_[name].set(obj, warning);
3243 // Look through the warnings and mark the symbols for which we should
3244 // warn. This is called during Layout::finalize when we know the
3245 // sources for all the symbols.
3248 Warnings::note_warnings(Symbol_table* symtab)
3250 for (Warning_table::iterator p = this->warnings_.begin();
3251 p != this->warnings_.end();
3254 Symbol* sym = symtab->lookup(p->first, NULL);
3256 && sym->source() == Symbol::FROM_OBJECT
3257 && sym->object() == p->second.object)
3258 sym->set_has_warning();
3262 // Issue a warning. This is called when we see a relocation against a
3263 // symbol for which has a warning.
3265 template<int size, bool big_endian>
3267 Warnings::issue_warning(const Symbol* sym,
3268 const Relocate_info<size, big_endian>* relinfo,
3269 size_t relnum, off_t reloffset) const
3271 gold_assert(sym->has_warning());
3272 Warning_table::const_iterator p = this->warnings_.find(sym->name());
3273 gold_assert(p != this->warnings_.end());
3274 gold_warning_at_location(relinfo, relnum, reloffset,
3275 "%s", p->second.text.c_str());
3278 // Instantiate the templates we need. We could use the configure
3279 // script to restrict this to only the ones needed for implemented
3282 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3285 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3288 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3291 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3294 #ifdef HAVE_TARGET_32_LITTLE
3297 Symbol_table::add_from_relobj<32, false>(
3298 Sized_relobj<32, false>* relobj,
3299 const unsigned char* syms,
3301 size_t symndx_offset,
3302 const char* sym_names,
3303 size_t sym_name_size,
3304 Sized_relobj<32, false>::Symbols* sympointers,
3308 #ifdef HAVE_TARGET_32_BIG
3311 Symbol_table::add_from_relobj<32, true>(
3312 Sized_relobj<32, true>* relobj,
3313 const unsigned char* syms,
3315 size_t symndx_offset,
3316 const char* sym_names,
3317 size_t sym_name_size,
3318 Sized_relobj<32, true>::Symbols* sympointers,
3322 #ifdef HAVE_TARGET_64_LITTLE
3325 Symbol_table::add_from_relobj<64, false>(
3326 Sized_relobj<64, false>* relobj,
3327 const unsigned char* syms,
3329 size_t symndx_offset,
3330 const char* sym_names,
3331 size_t sym_name_size,
3332 Sized_relobj<64, false>::Symbols* sympointers,
3336 #ifdef HAVE_TARGET_64_BIG
3339 Symbol_table::add_from_relobj<64, true>(
3340 Sized_relobj<64, true>* relobj,
3341 const unsigned char* syms,
3343 size_t symndx_offset,
3344 const char* sym_names,
3345 size_t sym_name_size,
3346 Sized_relobj<64, true>::Symbols* sympointers,
3350 #ifdef HAVE_TARGET_32_LITTLE
3353 Symbol_table::add_from_pluginobj<32, false>(
3354 Sized_pluginobj<32, false>* obj,
3357 elfcpp::Sym<32, false>* sym);
3360 #ifdef HAVE_TARGET_32_BIG
3363 Symbol_table::add_from_pluginobj<32, true>(
3364 Sized_pluginobj<32, true>* obj,
3367 elfcpp::Sym<32, true>* sym);
3370 #ifdef HAVE_TARGET_64_LITTLE
3373 Symbol_table::add_from_pluginobj<64, false>(
3374 Sized_pluginobj<64, false>* obj,
3377 elfcpp::Sym<64, false>* sym);
3380 #ifdef HAVE_TARGET_64_BIG
3383 Symbol_table::add_from_pluginobj<64, true>(
3384 Sized_pluginobj<64, true>* obj,
3387 elfcpp::Sym<64, true>* sym);
3390 #ifdef HAVE_TARGET_32_LITTLE
3393 Symbol_table::add_from_dynobj<32, false>(
3394 Sized_dynobj<32, false>* dynobj,
3395 const unsigned char* syms,
3397 const char* sym_names,
3398 size_t sym_name_size,
3399 const unsigned char* versym,
3401 const std::vector<const char*>* version_map,
3402 Sized_relobj<32, false>::Symbols* sympointers,
3406 #ifdef HAVE_TARGET_32_BIG
3409 Symbol_table::add_from_dynobj<32, true>(
3410 Sized_dynobj<32, true>* dynobj,
3411 const unsigned char* syms,
3413 const char* sym_names,
3414 size_t sym_name_size,
3415 const unsigned char* versym,
3417 const std::vector<const char*>* version_map,
3418 Sized_relobj<32, true>::Symbols* sympointers,
3422 #ifdef HAVE_TARGET_64_LITTLE
3425 Symbol_table::add_from_dynobj<64, false>(
3426 Sized_dynobj<64, false>* dynobj,
3427 const unsigned char* syms,
3429 const char* sym_names,
3430 size_t sym_name_size,
3431 const unsigned char* versym,
3433 const std::vector<const char*>* version_map,
3434 Sized_relobj<64, false>::Symbols* sympointers,
3438 #ifdef HAVE_TARGET_64_BIG
3441 Symbol_table::add_from_dynobj<64, true>(
3442 Sized_dynobj<64, true>* dynobj,
3443 const unsigned char* syms,
3445 const char* sym_names,
3446 size_t sym_name_size,
3447 const unsigned char* versym,
3449 const std::vector<const char*>* version_map,
3450 Sized_relobj<64, true>::Symbols* sympointers,
3454 #ifdef HAVE_TARGET_32_LITTLE
3457 Symbol_table::add_from_incrobj(
3461 elfcpp::Sym<32, false>* sym);
3464 #ifdef HAVE_TARGET_32_BIG
3467 Symbol_table::add_from_incrobj(
3471 elfcpp::Sym<32, true>* sym);
3474 #ifdef HAVE_TARGET_64_LITTLE
3477 Symbol_table::add_from_incrobj(
3481 elfcpp::Sym<64, false>* sym);
3484 #ifdef HAVE_TARGET_64_BIG
3487 Symbol_table::add_from_incrobj(
3491 elfcpp::Sym<64, true>* sym);
3494 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3497 Symbol_table::define_with_copy_reloc<32>(
3498 Sized_symbol<32>* sym,
3500 elfcpp::Elf_types<32>::Elf_Addr value);
3503 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3506 Symbol_table::define_with_copy_reloc<64>(
3507 Sized_symbol<64>* sym,
3509 elfcpp::Elf_types<64>::Elf_Addr value);
3512 #ifdef HAVE_TARGET_32_LITTLE
3515 Warnings::issue_warning<32, false>(const Symbol* sym,
3516 const Relocate_info<32, false>* relinfo,
3517 size_t relnum, off_t reloffset) const;
3520 #ifdef HAVE_TARGET_32_BIG
3523 Warnings::issue_warning<32, true>(const Symbol* sym,
3524 const Relocate_info<32, true>* relinfo,
3525 size_t relnum, off_t reloffset) const;
3528 #ifdef HAVE_TARGET_64_LITTLE
3531 Warnings::issue_warning<64, false>(const Symbol* sym,
3532 const Relocate_info<64, false>* relinfo,
3533 size_t relnum, off_t reloffset) const;
3536 #ifdef HAVE_TARGET_64_BIG
3539 Warnings::issue_warning<64, true>(const Symbol* sym,
3540 const Relocate_info<64, true>* relinfo,
3541 size_t relnum, off_t reloffset) const;
3544 } // End namespace gold.