PR 11136
[binutils/dougsmingw.git] / gold / symtab.cc
blobc2a811f19c0be84cb5b45b56ee6e176551a1dd9d
1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008, 2009, 2010 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.
23 #include "gold.h"
25 #include <cstring>
26 #include <stdint.h>
27 #include <algorithm>
28 #include <set>
29 #include <string>
30 #include <utility>
31 #include "demangle.h"
33 #include "gc.h"
34 #include "object.h"
35 #include "dwarf_reader.h"
36 #include "dynobj.h"
37 #include "output.h"
38 #include "target.h"
39 #include "workqueue.h"
40 #include "symtab.h"
41 #include "demangle.h" // needed for --dynamic-list-cpp-new
42 #include "plugin.h"
44 namespace gold
47 // Class Symbol.
49 // Initialize fields in Symbol. This initializes everything except u_
50 // and source_.
52 void
53 Symbol::init_fields(const char* name, const char* version,
54 elfcpp::STT type, elfcpp::STB binding,
55 elfcpp::STV visibility, unsigned char nonvis)
57 this->name_ = name;
58 this->version_ = version;
59 this->symtab_index_ = 0;
60 this->dynsym_index_ = 0;
61 this->got_offsets_.init();
62 this->plt_offset_ = -1U;
63 this->type_ = type;
64 this->binding_ = binding;
65 this->visibility_ = visibility;
66 this->nonvis_ = nonvis;
67 this->is_def_ = false;
68 this->is_forwarder_ = false;
69 this->has_alias_ = false;
70 this->needs_dynsym_entry_ = false;
71 this->in_reg_ = false;
72 this->in_dyn_ = false;
73 this->has_warning_ = false;
74 this->is_copied_from_dynobj_ = false;
75 this->is_forced_local_ = false;
76 this->is_ordinary_shndx_ = false;
77 this->in_real_elf_ = false;
78 this->is_defined_in_discarded_section_ = false;
81 // Return the demangled version of the symbol's name, but only
82 // if the --demangle flag was set.
84 static std::string
85 demangle(const char* name)
87 if (!parameters->options().do_demangle())
88 return name;
90 // cplus_demangle allocates memory for the result it returns,
91 // and returns NULL if the name is already demangled.
92 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
93 if (demangled_name == NULL)
94 return name;
96 std::string retval(demangled_name);
97 free(demangled_name);
98 return retval;
101 std::string
102 Symbol::demangled_name() const
104 return demangle(this->name());
107 // Initialize the fields in the base class Symbol for SYM in OBJECT.
109 template<int size, bool big_endian>
110 void
111 Symbol::init_base_object(const char* name, const char* version, Object* object,
112 const elfcpp::Sym<size, big_endian>& sym,
113 unsigned int st_shndx, bool is_ordinary)
115 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
116 sym.get_st_visibility(), sym.get_st_nonvis());
117 this->u_.from_object.object = object;
118 this->u_.from_object.shndx = st_shndx;
119 this->is_ordinary_shndx_ = is_ordinary;
120 this->source_ = FROM_OBJECT;
121 this->in_reg_ = !object->is_dynamic();
122 this->in_dyn_ = object->is_dynamic();
123 this->in_real_elf_ = object->pluginobj() == NULL;
126 // Initialize the fields in the base class Symbol for a symbol defined
127 // in an Output_data.
129 void
130 Symbol::init_base_output_data(const char* name, const char* version,
131 Output_data* od, elfcpp::STT type,
132 elfcpp::STB binding, elfcpp::STV visibility,
133 unsigned char nonvis, bool offset_is_from_end)
135 this->init_fields(name, version, type, binding, visibility, nonvis);
136 this->u_.in_output_data.output_data = od;
137 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
138 this->source_ = IN_OUTPUT_DATA;
139 this->in_reg_ = true;
140 this->in_real_elf_ = true;
143 // Initialize the fields in the base class Symbol for a symbol defined
144 // in an Output_segment.
146 void
147 Symbol::init_base_output_segment(const char* name, const char* version,
148 Output_segment* os, elfcpp::STT type,
149 elfcpp::STB binding, elfcpp::STV visibility,
150 unsigned char nonvis,
151 Segment_offset_base offset_base)
153 this->init_fields(name, version, type, binding, visibility, nonvis);
154 this->u_.in_output_segment.output_segment = os;
155 this->u_.in_output_segment.offset_base = offset_base;
156 this->source_ = IN_OUTPUT_SEGMENT;
157 this->in_reg_ = true;
158 this->in_real_elf_ = true;
161 // Initialize the fields in the base class Symbol for a symbol defined
162 // as a constant.
164 void
165 Symbol::init_base_constant(const char* name, const char* version,
166 elfcpp::STT type, elfcpp::STB binding,
167 elfcpp::STV visibility, unsigned char nonvis)
169 this->init_fields(name, version, type, binding, visibility, nonvis);
170 this->source_ = IS_CONSTANT;
171 this->in_reg_ = true;
172 this->in_real_elf_ = true;
175 // Initialize the fields in the base class Symbol for an undefined
176 // symbol.
178 void
179 Symbol::init_base_undefined(const char* name, const char* version,
180 elfcpp::STT type, elfcpp::STB binding,
181 elfcpp::STV visibility, unsigned char nonvis)
183 this->init_fields(name, version, type, binding, visibility, nonvis);
184 this->dynsym_index_ = -1U;
185 this->source_ = IS_UNDEFINED;
186 this->in_reg_ = true;
187 this->in_real_elf_ = true;
190 // Allocate a common symbol in the base.
192 void
193 Symbol::allocate_base_common(Output_data* od)
195 gold_assert(this->is_common());
196 this->source_ = IN_OUTPUT_DATA;
197 this->u_.in_output_data.output_data = od;
198 this->u_.in_output_data.offset_is_from_end = false;
201 // Initialize the fields in Sized_symbol for SYM in OBJECT.
203 template<int size>
204 template<bool big_endian>
205 void
206 Sized_symbol<size>::init_object(const char* name, const char* version,
207 Object* object,
208 const elfcpp::Sym<size, big_endian>& sym,
209 unsigned int st_shndx, bool is_ordinary)
211 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
212 this->value_ = sym.get_st_value();
213 this->symsize_ = sym.get_st_size();
216 // Initialize the fields in Sized_symbol for a symbol defined in an
217 // Output_data.
219 template<int size>
220 void
221 Sized_symbol<size>::init_output_data(const char* name, const char* version,
222 Output_data* od, Value_type value,
223 Size_type symsize, elfcpp::STT type,
224 elfcpp::STB binding,
225 elfcpp::STV visibility,
226 unsigned char nonvis,
227 bool offset_is_from_end)
229 this->init_base_output_data(name, version, od, type, binding, visibility,
230 nonvis, offset_is_from_end);
231 this->value_ = value;
232 this->symsize_ = symsize;
235 // Initialize the fields in Sized_symbol for a symbol defined in an
236 // Output_segment.
238 template<int size>
239 void
240 Sized_symbol<size>::init_output_segment(const char* name, const char* version,
241 Output_segment* os, Value_type value,
242 Size_type symsize, elfcpp::STT type,
243 elfcpp::STB binding,
244 elfcpp::STV visibility,
245 unsigned char nonvis,
246 Segment_offset_base offset_base)
248 this->init_base_output_segment(name, version, os, type, binding, visibility,
249 nonvis, offset_base);
250 this->value_ = value;
251 this->symsize_ = symsize;
254 // Initialize the fields in Sized_symbol for a symbol defined as a
255 // constant.
257 template<int size>
258 void
259 Sized_symbol<size>::init_constant(const char* name, const char* version,
260 Value_type value, Size_type symsize,
261 elfcpp::STT type, elfcpp::STB binding,
262 elfcpp::STV visibility, unsigned char nonvis)
264 this->init_base_constant(name, version, type, binding, visibility, nonvis);
265 this->value_ = value;
266 this->symsize_ = symsize;
269 // Initialize the fields in Sized_symbol for an undefined symbol.
271 template<int size>
272 void
273 Sized_symbol<size>::init_undefined(const char* name, const char* version,
274 elfcpp::STT type, elfcpp::STB binding,
275 elfcpp::STV visibility, unsigned char nonvis)
277 this->init_base_undefined(name, version, type, binding, visibility, nonvis);
278 this->value_ = 0;
279 this->symsize_ = 0;
282 // Return true if SHNDX represents a common symbol.
284 bool
285 Symbol::is_common_shndx(unsigned int shndx)
287 return (shndx == elfcpp::SHN_COMMON
288 || shndx == parameters->target().small_common_shndx()
289 || shndx == parameters->target().large_common_shndx());
292 // Allocate a common symbol.
294 template<int size>
295 void
296 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
298 this->allocate_base_common(od);
299 this->value_ = value;
302 // The ""'s around str ensure str is a string literal, so sizeof works.
303 #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
305 // Return true if this symbol should be added to the dynamic symbol
306 // table.
308 inline bool
309 Symbol::should_add_dynsym_entry() const
311 // If the symbol is used by a dynamic relocation, we need to add it.
312 if (this->needs_dynsym_entry())
313 return true;
315 // If this symbol's section is not added, the symbol need not be added.
316 // The section may have been GCed. Note that export_dynamic is being
317 // overridden here. This should not be done for shared objects.
318 if (parameters->options().gc_sections()
319 && !parameters->options().shared()
320 && this->source() == Symbol::FROM_OBJECT
321 && !this->object()->is_dynamic())
323 Relobj* relobj = static_cast<Relobj*>(this->object());
324 bool is_ordinary;
325 unsigned int shndx = this->shndx(&is_ordinary);
326 if (is_ordinary && shndx != elfcpp::SHN_UNDEF
327 && !relobj->is_section_included(shndx))
328 return false;
331 // If the symbol was forced local in a version script, do not add it.
332 if (this->is_forced_local())
333 return false;
335 // If the symbol was forced dynamic in a --dynamic-list file, add it.
336 if (parameters->options().in_dynamic_list(this->name()))
337 return true;
339 // If dynamic-list-data was specified, add any STT_OBJECT.
340 if (parameters->options().dynamic_list_data()
341 && !this->is_from_dynobj()
342 && this->type() == elfcpp::STT_OBJECT)
343 return true;
345 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
346 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
347 if ((parameters->options().dynamic_list_cpp_new()
348 || parameters->options().dynamic_list_cpp_typeinfo())
349 && !this->is_from_dynobj())
351 // TODO(csilvers): We could probably figure out if we're an operator
352 // new/delete or typeinfo without the need to demangle.
353 char* demangled_name = cplus_demangle(this->name(),
354 DMGL_ANSI | DMGL_PARAMS);
355 if (demangled_name == NULL)
357 // Not a C++ symbol, so it can't satisfy these flags
359 else if (parameters->options().dynamic_list_cpp_new()
360 && (strprefix(demangled_name, "operator new")
361 || strprefix(demangled_name, "operator delete")))
363 free(demangled_name);
364 return true;
366 else if (parameters->options().dynamic_list_cpp_typeinfo()
367 && (strprefix(demangled_name, "typeinfo name for")
368 || strprefix(demangled_name, "typeinfo for")))
370 free(demangled_name);
371 return true;
373 else
374 free(demangled_name);
377 // If exporting all symbols or building a shared library,
378 // and the symbol is defined in a regular object and is
379 // externally visible, we need to add it.
380 if ((parameters->options().export_dynamic() || parameters->options().shared())
381 && !this->is_from_dynobj()
382 && this->is_externally_visible())
383 return true;
385 return false;
388 // Return true if the final value of this symbol is known at link
389 // time.
391 bool
392 Symbol::final_value_is_known() const
394 // If we are not generating an executable, then no final values are
395 // known, since they will change at runtime.
396 if (parameters->options().output_is_position_independent()
397 || parameters->options().relocatable())
398 return false;
400 // If the symbol is not from an object file, and is not undefined,
401 // then it is defined, and known.
402 if (this->source_ != FROM_OBJECT)
404 if (this->source_ != IS_UNDEFINED)
405 return true;
407 else
409 // If the symbol is from a dynamic object, then the final value
410 // is not known.
411 if (this->object()->is_dynamic())
412 return false;
414 // If the symbol is not undefined (it is defined or common),
415 // then the final value is known.
416 if (!this->is_undefined())
417 return true;
420 // If the symbol is undefined, then whether the final value is known
421 // depends on whether we are doing a static link. If we are doing a
422 // dynamic link, then the final value could be filled in at runtime.
423 // This could reasonably be the case for a weak undefined symbol.
424 return parameters->doing_static_link();
427 // Return the output section where this symbol is defined.
429 Output_section*
430 Symbol::output_section() const
432 switch (this->source_)
434 case FROM_OBJECT:
436 unsigned int shndx = this->u_.from_object.shndx;
437 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
439 gold_assert(!this->u_.from_object.object->is_dynamic());
440 gold_assert(this->u_.from_object.object->pluginobj() == NULL);
441 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
442 return relobj->output_section(shndx);
444 return NULL;
447 case IN_OUTPUT_DATA:
448 return this->u_.in_output_data.output_data->output_section();
450 case IN_OUTPUT_SEGMENT:
451 case IS_CONSTANT:
452 case IS_UNDEFINED:
453 return NULL;
455 default:
456 gold_unreachable();
460 // Set the symbol's output section. This is used for symbols defined
461 // in scripts. This should only be called after the symbol table has
462 // been finalized.
464 void
465 Symbol::set_output_section(Output_section* os)
467 switch (this->source_)
469 case FROM_OBJECT:
470 case IN_OUTPUT_DATA:
471 gold_assert(this->output_section() == os);
472 break;
473 case IS_CONSTANT:
474 this->source_ = IN_OUTPUT_DATA;
475 this->u_.in_output_data.output_data = os;
476 this->u_.in_output_data.offset_is_from_end = false;
477 break;
478 case IN_OUTPUT_SEGMENT:
479 case IS_UNDEFINED:
480 default:
481 gold_unreachable();
485 // Class Symbol_table.
487 Symbol_table::Symbol_table(unsigned int count,
488 const Version_script_info& version_script)
489 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
490 forwarders_(), commons_(), tls_commons_(), small_commons_(),
491 large_commons_(), forced_locals_(), warnings_(),
492 version_script_(version_script), gc_(NULL), icf_(NULL)
494 namepool_.reserve(count);
497 Symbol_table::~Symbol_table()
501 // The hash function. The key values are Stringpool keys.
503 inline size_t
504 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
506 return key.first ^ key.second;
509 // The symbol table key equality function. This is called with
510 // Stringpool keys.
512 inline bool
513 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
514 const Symbol_table_key& k2) const
516 return k1.first == k2.first && k1.second == k2.second;
519 bool
520 Symbol_table::is_section_folded(Object* obj, unsigned int shndx) const
522 return (parameters->options().icf_enabled()
523 && this->icf_->is_section_folded(obj, shndx));
526 // For symbols that have been listed with -u option, add them to the
527 // work list to avoid gc'ing them.
529 void
530 Symbol_table::gc_mark_undef_symbols()
532 for (options::String_set::const_iterator p =
533 parameters->options().undefined_begin();
534 p != parameters->options().undefined_end();
535 ++p)
537 const char* name = p->c_str();
538 Symbol* sym = this->lookup(name);
539 gold_assert (sym != NULL);
540 if (sym->source() == Symbol::FROM_OBJECT
541 && !sym->object()->is_dynamic())
543 Relobj* obj = static_cast<Relobj*>(sym->object());
544 bool is_ordinary;
545 unsigned int shndx = sym->shndx(&is_ordinary);
546 if (is_ordinary)
548 gold_assert(this->gc_ != NULL);
549 this->gc_->worklist().push(Section_id(obj, shndx));
555 void
556 Symbol_table::gc_mark_symbol_for_shlib(Symbol* sym)
558 if (!sym->is_from_dynobj()
559 && sym->is_externally_visible())
561 //Add the object and section to the work list.
562 Relobj* obj = static_cast<Relobj*>(sym->object());
563 bool is_ordinary;
564 unsigned int shndx = sym->shndx(&is_ordinary);
565 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
567 gold_assert(this->gc_!= NULL);
568 this->gc_->worklist().push(Section_id(obj, shndx));
573 // When doing garbage collection, keep symbols that have been seen in
574 // dynamic objects.
575 inline void
576 Symbol_table::gc_mark_dyn_syms(Symbol* sym)
578 if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
579 && !sym->object()->is_dynamic())
581 Relobj *obj = static_cast<Relobj*>(sym->object());
582 bool is_ordinary;
583 unsigned int shndx = sym->shndx(&is_ordinary);
584 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
586 gold_assert(this->gc_ != NULL);
587 this->gc_->worklist().push(Section_id(obj, shndx));
592 // Make TO a symbol which forwards to FROM.
594 void
595 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
597 gold_assert(from != to);
598 gold_assert(!from->is_forwarder() && !to->is_forwarder());
599 this->forwarders_[from] = to;
600 from->set_forwarder();
603 // Resolve the forwards from FROM, returning the real symbol.
605 Symbol*
606 Symbol_table::resolve_forwards(const Symbol* from) const
608 gold_assert(from->is_forwarder());
609 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
610 this->forwarders_.find(from);
611 gold_assert(p != this->forwarders_.end());
612 return p->second;
615 // Look up a symbol by name.
617 Symbol*
618 Symbol_table::lookup(const char* name, const char* version) const
620 Stringpool::Key name_key;
621 name = this->namepool_.find(name, &name_key);
622 if (name == NULL)
623 return NULL;
625 Stringpool::Key version_key = 0;
626 if (version != NULL)
628 version = this->namepool_.find(version, &version_key);
629 if (version == NULL)
630 return NULL;
633 Symbol_table_key key(name_key, version_key);
634 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
635 if (p == this->table_.end())
636 return NULL;
637 return p->second;
640 // Resolve a Symbol with another Symbol. This is only used in the
641 // unusual case where there are references to both an unversioned
642 // symbol and a symbol with a version, and we then discover that that
643 // version is the default version. Because this is unusual, we do
644 // this the slow way, by converting back to an ELF symbol.
646 template<int size, bool big_endian>
647 void
648 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
650 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
651 elfcpp::Sym_write<size, big_endian> esym(buf);
652 // We don't bother to set the st_name or the st_shndx field.
653 esym.put_st_value(from->value());
654 esym.put_st_size(from->symsize());
655 esym.put_st_info(from->binding(), from->type());
656 esym.put_st_other(from->visibility(), from->nonvis());
657 bool is_ordinary;
658 unsigned int shndx = from->shndx(&is_ordinary);
659 this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
660 from->version());
661 if (from->in_reg())
662 to->set_in_reg();
663 if (from->in_dyn())
664 to->set_in_dyn();
665 if (parameters->options().gc_sections())
666 this->gc_mark_dyn_syms(to);
669 // Record that a symbol is forced to be local by a version script or
670 // by visibility.
672 void
673 Symbol_table::force_local(Symbol* sym)
675 if (!sym->is_defined() && !sym->is_common())
676 return;
677 if (sym->is_forced_local())
679 // We already got this one.
680 return;
682 sym->set_is_forced_local();
683 this->forced_locals_.push_back(sym);
686 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
687 // is only called for undefined symbols, when at least one --wrap
688 // option was used.
690 const char*
691 Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key)
693 // For some targets, we need to ignore a specific character when
694 // wrapping, and add it back later.
695 char prefix = '\0';
696 if (name[0] == parameters->target().wrap_char())
698 prefix = name[0];
699 ++name;
702 if (parameters->options().is_wrap(name))
704 // Turn NAME into __wrap_NAME.
705 std::string s;
706 if (prefix != '\0')
707 s += prefix;
708 s += "__wrap_";
709 s += name;
711 // This will give us both the old and new name in NAMEPOOL_, but
712 // that is OK. Only the versions we need will wind up in the
713 // real string table in the output file.
714 return this->namepool_.add(s.c_str(), true, name_key);
717 const char* const real_prefix = "__real_";
718 const size_t real_prefix_length = strlen(real_prefix);
719 if (strncmp(name, real_prefix, real_prefix_length) == 0
720 && parameters->options().is_wrap(name + real_prefix_length))
722 // Turn __real_NAME into NAME.
723 std::string s;
724 if (prefix != '\0')
725 s += prefix;
726 s += name + real_prefix_length;
727 return this->namepool_.add(s.c_str(), true, name_key);
730 return name;
733 // This is called when we see a symbol NAME/VERSION, and the symbol
734 // already exists in the symbol table, and VERSION is marked as being
735 // the default version. SYM is the NAME/VERSION symbol we just added.
736 // DEFAULT_IS_NEW is true if this is the first time we have seen the
737 // symbol NAME/NULL. PDEF points to the entry for NAME/NULL.
739 template<int size, bool big_endian>
740 void
741 Symbol_table::define_default_version(Sized_symbol<size>* sym,
742 bool default_is_new,
743 Symbol_table_type::iterator pdef)
745 if (default_is_new)
747 // This is the first time we have seen NAME/NULL. Make
748 // NAME/NULL point to NAME/VERSION, and mark SYM as the default
749 // version.
750 pdef->second = sym;
751 sym->set_is_default();
753 else if (pdef->second == sym)
755 // NAME/NULL already points to NAME/VERSION. Don't mark the
756 // symbol as the default if it is not already the default.
758 else
760 // This is the unfortunate case where we already have entries
761 // for both NAME/VERSION and NAME/NULL. We now see a symbol
762 // NAME/VERSION where VERSION is the default version. We have
763 // already resolved this new symbol with the existing
764 // NAME/VERSION symbol.
766 // It's possible that NAME/NULL and NAME/VERSION are both
767 // defined in regular objects. This can only happen if one
768 // object file defines foo and another defines foo@@ver. This
769 // is somewhat obscure, but we call it a multiple definition
770 // error.
772 // It's possible that NAME/NULL actually has a version, in which
773 // case it won't be the same as VERSION. This happens with
774 // ver_test_7.so in the testsuite for the symbol t2_2. We see
775 // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We
776 // then see an unadorned t2_2 in an object file and give it
777 // version VER1 from the version script. This looks like a
778 // default definition for VER1, so it looks like we should merge
779 // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's
780 // not obvious that this is an error, either. So we just punt.
782 // If one of the symbols has non-default visibility, and the
783 // other is defined in a shared object, then they are different
784 // symbols.
786 // Otherwise, we just resolve the symbols as though they were
787 // the same.
789 if (pdef->second->version() != NULL)
790 gold_assert(pdef->second->version() != sym->version());
791 else if (sym->visibility() != elfcpp::STV_DEFAULT
792 && pdef->second->is_from_dynobj())
794 else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
795 && sym->is_from_dynobj())
797 else
799 const Sized_symbol<size>* symdef;
800 symdef = this->get_sized_symbol<size>(pdef->second);
801 Symbol_table::resolve<size, big_endian>(sym, symdef);
802 this->make_forwarder(pdef->second, sym);
803 pdef->second = sym;
804 sym->set_is_default();
809 // Add one symbol from OBJECT to the symbol table. NAME is symbol
810 // name and VERSION is the version; both are canonicalized. DEF is
811 // whether this is the default version. ST_SHNDX is the symbol's
812 // section index; IS_ORDINARY is whether this is a normal section
813 // rather than a special code.
815 // If IS_DEFAULT_VERSION is true, then this is the definition of a
816 // default version of a symbol. That means that any lookup of
817 // NAME/NULL and any lookup of NAME/VERSION should always return the
818 // same symbol. This is obvious for references, but in particular we
819 // want to do this for definitions: overriding NAME/NULL should also
820 // override NAME/VERSION. If we don't do that, it would be very hard
821 // to override functions in a shared library which uses versioning.
823 // We implement this by simply making both entries in the hash table
824 // point to the same Symbol structure. That is easy enough if this is
825 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
826 // that we have seen both already, in which case they will both have
827 // independent entries in the symbol table. We can't simply change
828 // the symbol table entry, because we have pointers to the entries
829 // attached to the object files. So we mark the entry attached to the
830 // object file as a forwarder, and record it in the forwarders_ map.
831 // Note that entries in the hash table will never be marked as
832 // forwarders.
834 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
835 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
836 // for a special section code. ST_SHNDX may be modified if the symbol
837 // is defined in a section being discarded.
839 template<int size, bool big_endian>
840 Sized_symbol<size>*
841 Symbol_table::add_from_object(Object* object,
842 const char *name,
843 Stringpool::Key name_key,
844 const char *version,
845 Stringpool::Key version_key,
846 bool is_default_version,
847 const elfcpp::Sym<size, big_endian>& sym,
848 unsigned int st_shndx,
849 bool is_ordinary,
850 unsigned int orig_st_shndx)
852 // Print a message if this symbol is being traced.
853 if (parameters->options().is_trace_symbol(name))
855 if (orig_st_shndx == elfcpp::SHN_UNDEF)
856 gold_info(_("%s: reference to %s"), object->name().c_str(), name);
857 else
858 gold_info(_("%s: definition of %s"), object->name().c_str(), name);
861 // For an undefined symbol, we may need to adjust the name using
862 // --wrap.
863 if (orig_st_shndx == elfcpp::SHN_UNDEF
864 && parameters->options().any_wrap())
866 const char* wrap_name = this->wrap_symbol(name, &name_key);
867 if (wrap_name != name)
869 // If we see a reference to malloc with version GLIBC_2.0,
870 // and we turn it into a reference to __wrap_malloc, then we
871 // discard the version number. Otherwise the user would be
872 // required to specify the correct version for
873 // __wrap_malloc.
874 version = NULL;
875 version_key = 0;
876 name = wrap_name;
880 Symbol* const snull = NULL;
881 std::pair<typename Symbol_table_type::iterator, bool> ins =
882 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
883 snull));
885 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
886 std::make_pair(this->table_.end(), false);
887 if (is_default_version)
889 const Stringpool::Key vnull_key = 0;
890 insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
891 vnull_key),
892 snull));
895 // ins.first: an iterator, which is a pointer to a pair.
896 // ins.first->first: the key (a pair of name and version).
897 // ins.first->second: the value (Symbol*).
898 // ins.second: true if new entry was inserted, false if not.
900 Sized_symbol<size>* ret;
901 bool was_undefined;
902 bool was_common;
903 if (!ins.second)
905 // We already have an entry for NAME/VERSION.
906 ret = this->get_sized_symbol<size>(ins.first->second);
907 gold_assert(ret != NULL);
909 was_undefined = ret->is_undefined();
910 was_common = ret->is_common();
912 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
913 version);
914 if (parameters->options().gc_sections())
915 this->gc_mark_dyn_syms(ret);
917 if (is_default_version)
918 this->define_default_version<size, big_endian>(ret, insdefault.second,
919 insdefault.first);
921 else
923 // This is the first time we have seen NAME/VERSION.
924 gold_assert(ins.first->second == NULL);
926 if (is_default_version && !insdefault.second)
928 // We already have an entry for NAME/NULL. If we override
929 // it, then change it to NAME/VERSION.
930 ret = this->get_sized_symbol<size>(insdefault.first->second);
932 was_undefined = ret->is_undefined();
933 was_common = ret->is_common();
935 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
936 version);
937 if (parameters->options().gc_sections())
938 this->gc_mark_dyn_syms(ret);
939 ins.first->second = ret;
941 else
943 was_undefined = false;
944 was_common = false;
946 Sized_target<size, big_endian>* target =
947 parameters->sized_target<size, big_endian>();
948 if (!target->has_make_symbol())
949 ret = new Sized_symbol<size>();
950 else
952 ret = target->make_symbol();
953 if (ret == NULL)
955 // This means that we don't want a symbol table
956 // entry after all.
957 if (!is_default_version)
958 this->table_.erase(ins.first);
959 else
961 this->table_.erase(insdefault.first);
962 // Inserting INSDEFAULT invalidated INS.
963 this->table_.erase(std::make_pair(name_key,
964 version_key));
966 return NULL;
970 ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
972 ins.first->second = ret;
973 if (is_default_version)
975 // This is the first time we have seen NAME/NULL. Point
976 // it at the new entry for NAME/VERSION.
977 gold_assert(insdefault.second);
978 insdefault.first->second = ret;
982 if (is_default_version)
983 ret->set_is_default();
986 // Record every time we see a new undefined symbol, to speed up
987 // archive groups.
988 if (!was_undefined && ret->is_undefined())
989 ++this->saw_undefined_;
991 // Keep track of common symbols, to speed up common symbol
992 // allocation.
993 if (!was_common && ret->is_common())
995 if (ret->type() == elfcpp::STT_TLS)
996 this->tls_commons_.push_back(ret);
997 else if (!is_ordinary
998 && st_shndx == parameters->target().small_common_shndx())
999 this->small_commons_.push_back(ret);
1000 else if (!is_ordinary
1001 && st_shndx == parameters->target().large_common_shndx())
1002 this->large_commons_.push_back(ret);
1003 else
1004 this->commons_.push_back(ret);
1007 // If we're not doing a relocatable link, then any symbol with
1008 // hidden or internal visibility is local.
1009 if ((ret->visibility() == elfcpp::STV_HIDDEN
1010 || ret->visibility() == elfcpp::STV_INTERNAL)
1011 && (ret->binding() == elfcpp::STB_GLOBAL
1012 || ret->binding() == elfcpp::STB_GNU_UNIQUE
1013 || ret->binding() == elfcpp::STB_WEAK)
1014 && !parameters->options().relocatable())
1015 this->force_local(ret);
1017 return ret;
1020 // Add all the symbols in a relocatable object to the hash table.
1022 template<int size, bool big_endian>
1023 void
1024 Symbol_table::add_from_relobj(
1025 Sized_relobj<size, big_endian>* relobj,
1026 const unsigned char* syms,
1027 size_t count,
1028 size_t symndx_offset,
1029 const char* sym_names,
1030 size_t sym_name_size,
1031 typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1032 size_t *defined)
1034 *defined = 0;
1036 gold_assert(size == parameters->target().get_size());
1038 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1040 const bool just_symbols = relobj->just_symbols();
1042 const unsigned char* p = syms;
1043 for (size_t i = 0; i < count; ++i, p += sym_size)
1045 (*sympointers)[i] = NULL;
1047 elfcpp::Sym<size, big_endian> sym(p);
1049 unsigned int st_name = sym.get_st_name();
1050 if (st_name >= sym_name_size)
1052 relobj->error(_("bad global symbol name offset %u at %zu"),
1053 st_name, i);
1054 continue;
1057 const char* name = sym_names + st_name;
1059 bool is_ordinary;
1060 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1061 sym.get_st_shndx(),
1062 &is_ordinary);
1063 unsigned int orig_st_shndx = st_shndx;
1064 if (!is_ordinary)
1065 orig_st_shndx = elfcpp::SHN_UNDEF;
1067 if (st_shndx != elfcpp::SHN_UNDEF)
1068 ++*defined;
1070 // A symbol defined in a section which we are not including must
1071 // be treated as an undefined symbol.
1072 bool is_defined_in_discarded_section = false;
1073 if (st_shndx != elfcpp::SHN_UNDEF
1074 && is_ordinary
1075 && !relobj->is_section_included(st_shndx))
1077 st_shndx = elfcpp::SHN_UNDEF;
1078 is_defined_in_discarded_section = true;
1081 // In an object file, an '@' in the name separates the symbol
1082 // name from the version name. If there are two '@' characters,
1083 // this is the default version.
1084 const char* ver = strchr(name, '@');
1085 Stringpool::Key ver_key = 0;
1086 int namelen = 0;
1087 // IS_DEFAULT_VERSION: is the version default?
1088 // IS_FORCED_LOCAL: is the symbol forced local?
1089 bool is_default_version = false;
1090 bool is_forced_local = false;
1092 if (ver != NULL)
1094 // The symbol name is of the form foo@VERSION or foo@@VERSION
1095 namelen = ver - name;
1096 ++ver;
1097 if (*ver == '@')
1099 is_default_version = true;
1100 ++ver;
1102 ver = this->namepool_.add(ver, true, &ver_key);
1104 // We don't want to assign a version to an undefined symbol,
1105 // even if it is listed in the version script. FIXME: What
1106 // about a common symbol?
1107 else
1109 namelen = strlen(name);
1110 if (!this->version_script_.empty()
1111 && st_shndx != elfcpp::SHN_UNDEF)
1113 // The symbol name did not have a version, but the
1114 // version script may assign a version anyway.
1115 std::string version;
1116 bool is_global;
1117 if (this->version_script_.get_symbol_version(name, &version,
1118 &is_global))
1120 if (!is_global)
1121 is_forced_local = true;
1122 else if (!version.empty())
1124 ver = this->namepool_.add_with_length(version.c_str(),
1125 version.length(),
1126 true,
1127 &ver_key);
1128 is_default_version = true;
1134 elfcpp::Sym<size, big_endian>* psym = &sym;
1135 unsigned char symbuf[sym_size];
1136 elfcpp::Sym<size, big_endian> sym2(symbuf);
1137 if (just_symbols)
1139 memcpy(symbuf, p, sym_size);
1140 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1141 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
1143 // Symbol values in object files are section relative.
1144 // This is normally what we want, but since here we are
1145 // converting the symbol to absolute we need to add the
1146 // section address. The section address in an object
1147 // file is normally zero, but people can use a linker
1148 // script to change it.
1149 sw.put_st_value(sym.get_st_value()
1150 + relobj->section_address(orig_st_shndx));
1152 st_shndx = elfcpp::SHN_ABS;
1153 is_ordinary = false;
1154 psym = &sym2;
1157 // Fix up visibility if object has no-export set.
1158 if (relobj->no_export()
1159 && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
1161 // We may have copied symbol already above.
1162 if (psym != &sym2)
1164 memcpy(symbuf, p, sym_size);
1165 psym = &sym2;
1168 elfcpp::STV visibility = sym2.get_st_visibility();
1169 if (visibility == elfcpp::STV_DEFAULT
1170 || visibility == elfcpp::STV_PROTECTED)
1172 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1173 unsigned char nonvis = sym2.get_st_nonvis();
1174 sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1178 Stringpool::Key name_key;
1179 name = this->namepool_.add_with_length(name, namelen, true,
1180 &name_key);
1182 Sized_symbol<size>* res;
1183 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1184 is_default_version, *psym, st_shndx,
1185 is_ordinary, orig_st_shndx);
1187 // If building a shared library using garbage collection, do not
1188 // treat externally visible symbols as garbage.
1189 if (parameters->options().gc_sections()
1190 && parameters->options().shared())
1191 this->gc_mark_symbol_for_shlib(res);
1193 if (is_forced_local)
1194 this->force_local(res);
1196 if (is_defined_in_discarded_section)
1197 res->set_is_defined_in_discarded_section();
1199 (*sympointers)[i] = res;
1203 // Add a symbol from a plugin-claimed file.
1205 template<int size, bool big_endian>
1206 Symbol*
1207 Symbol_table::add_from_pluginobj(
1208 Sized_pluginobj<size, big_endian>* obj,
1209 const char* name,
1210 const char* ver,
1211 elfcpp::Sym<size, big_endian>* sym)
1213 unsigned int st_shndx = sym->get_st_shndx();
1214 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1216 Stringpool::Key ver_key = 0;
1217 bool is_default_version = false;
1218 bool is_forced_local = false;
1220 if (ver != NULL)
1222 ver = this->namepool_.add(ver, true, &ver_key);
1224 // We don't want to assign a version to an undefined symbol,
1225 // even if it is listed in the version script. FIXME: What
1226 // about a common symbol?
1227 else
1229 if (!this->version_script_.empty()
1230 && st_shndx != elfcpp::SHN_UNDEF)
1232 // The symbol name did not have a version, but the
1233 // version script may assign a version anyway.
1234 std::string version;
1235 bool is_global;
1236 if (this->version_script_.get_symbol_version(name, &version,
1237 &is_global))
1239 if (!is_global)
1240 is_forced_local = true;
1241 else if (!version.empty())
1243 ver = this->namepool_.add_with_length(version.c_str(),
1244 version.length(),
1245 true,
1246 &ver_key);
1247 is_default_version = true;
1253 Stringpool::Key name_key;
1254 name = this->namepool_.add(name, true, &name_key);
1256 Sized_symbol<size>* res;
1257 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1258 is_default_version, *sym, st_shndx,
1259 is_ordinary, st_shndx);
1261 if (is_forced_local)
1262 this->force_local(res);
1264 return res;
1267 // Add all the symbols in a dynamic object to the hash table.
1269 template<int size, bool big_endian>
1270 void
1271 Symbol_table::add_from_dynobj(
1272 Sized_dynobj<size, big_endian>* dynobj,
1273 const unsigned char* syms,
1274 size_t count,
1275 const char* sym_names,
1276 size_t sym_name_size,
1277 const unsigned char* versym,
1278 size_t versym_size,
1279 const std::vector<const char*>* version_map,
1280 typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1281 size_t* defined)
1283 *defined = 0;
1285 gold_assert(size == parameters->target().get_size());
1287 if (dynobj->just_symbols())
1289 gold_error(_("--just-symbols does not make sense with a shared object"));
1290 return;
1293 if (versym != NULL && versym_size / 2 < count)
1295 dynobj->error(_("too few symbol versions"));
1296 return;
1299 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1301 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1302 // weak aliases. This is necessary because if the dynamic object
1303 // provides the same variable under two names, one of which is a
1304 // weak definition, and the regular object refers to the weak
1305 // definition, we have to put both the weak definition and the
1306 // strong definition into the dynamic symbol table. Given a weak
1307 // definition, the only way that we can find the corresponding
1308 // strong definition, if any, is to search the symbol table.
1309 std::vector<Sized_symbol<size>*> object_symbols;
1311 const unsigned char* p = syms;
1312 const unsigned char* vs = versym;
1313 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1315 elfcpp::Sym<size, big_endian> sym(p);
1317 if (sympointers != NULL)
1318 (*sympointers)[i] = NULL;
1320 // Ignore symbols with local binding or that have
1321 // internal or hidden visibility.
1322 if (sym.get_st_bind() == elfcpp::STB_LOCAL
1323 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1324 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
1325 continue;
1327 // A protected symbol in a shared library must be treated as a
1328 // normal symbol when viewed from outside the shared library.
1329 // Implement this by overriding the visibility here.
1330 elfcpp::Sym<size, big_endian>* psym = &sym;
1331 unsigned char symbuf[sym_size];
1332 elfcpp::Sym<size, big_endian> sym2(symbuf);
1333 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1335 memcpy(symbuf, p, sym_size);
1336 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1337 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1338 psym = &sym2;
1341 unsigned int st_name = psym->get_st_name();
1342 if (st_name >= sym_name_size)
1344 dynobj->error(_("bad symbol name offset %u at %zu"),
1345 st_name, i);
1346 continue;
1349 const char* name = sym_names + st_name;
1351 bool is_ordinary;
1352 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
1353 &is_ordinary);
1355 if (st_shndx != elfcpp::SHN_UNDEF)
1356 ++*defined;
1358 Sized_symbol<size>* res;
1360 if (versym == NULL)
1362 Stringpool::Key name_key;
1363 name = this->namepool_.add(name, true, &name_key);
1364 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1365 false, *psym, st_shndx, is_ordinary,
1366 st_shndx);
1368 else
1370 // Read the version information.
1372 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
1374 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1375 v &= elfcpp::VERSYM_VERSION;
1377 // The Sun documentation says that V can be VER_NDX_LOCAL,
1378 // or VER_NDX_GLOBAL, or a version index. The meaning of
1379 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1380 // The old GNU linker will happily generate VER_NDX_LOCAL
1381 // for an undefined symbol. I don't know what the Sun
1382 // linker will generate.
1384 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1385 && st_shndx != elfcpp::SHN_UNDEF)
1387 // This symbol should not be visible outside the object.
1388 continue;
1391 // At this point we are definitely going to add this symbol.
1392 Stringpool::Key name_key;
1393 name = this->namepool_.add(name, true, &name_key);
1395 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1396 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1398 // This symbol does not have a version.
1399 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1400 false, *psym, st_shndx, is_ordinary,
1401 st_shndx);
1403 else
1405 if (v >= version_map->size())
1407 dynobj->error(_("versym for symbol %zu out of range: %u"),
1408 i, v);
1409 continue;
1412 const char* version = (*version_map)[v];
1413 if (version == NULL)
1415 dynobj->error(_("versym for symbol %zu has no name: %u"),
1416 i, v);
1417 continue;
1420 Stringpool::Key version_key;
1421 version = this->namepool_.add(version, true, &version_key);
1423 // If this is an absolute symbol, and the version name
1424 // and symbol name are the same, then this is the
1425 // version definition symbol. These symbols exist to
1426 // support using -u to pull in particular versions. We
1427 // do not want to record a version for them.
1428 if (st_shndx == elfcpp::SHN_ABS
1429 && !is_ordinary
1430 && name_key == version_key)
1431 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1432 false, *psym, st_shndx, is_ordinary,
1433 st_shndx);
1434 else
1436 const bool is_default_version =
1437 !hidden && st_shndx != elfcpp::SHN_UNDEF;
1438 res = this->add_from_object(dynobj, name, name_key, version,
1439 version_key, is_default_version,
1440 *psym, st_shndx,
1441 is_ordinary, st_shndx);
1446 // Note that it is possible that RES was overridden by an
1447 // earlier object, in which case it can't be aliased here.
1448 if (st_shndx != elfcpp::SHN_UNDEF
1449 && is_ordinary
1450 && psym->get_st_type() == elfcpp::STT_OBJECT
1451 && res->source() == Symbol::FROM_OBJECT
1452 && res->object() == dynobj)
1453 object_symbols.push_back(res);
1455 if (sympointers != NULL)
1456 (*sympointers)[i] = res;
1459 this->record_weak_aliases(&object_symbols);
1462 // This is used to sort weak aliases. We sort them first by section
1463 // index, then by offset, then by weak ahead of strong.
1465 template<int size>
1466 class Weak_alias_sorter
1468 public:
1469 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1472 template<int size>
1473 bool
1474 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1475 const Sized_symbol<size>* s2) const
1477 bool is_ordinary;
1478 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1479 gold_assert(is_ordinary);
1480 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1481 gold_assert(is_ordinary);
1482 if (s1_shndx != s2_shndx)
1483 return s1_shndx < s2_shndx;
1485 if (s1->value() != s2->value())
1486 return s1->value() < s2->value();
1487 if (s1->binding() != s2->binding())
1489 if (s1->binding() == elfcpp::STB_WEAK)
1490 return true;
1491 if (s2->binding() == elfcpp::STB_WEAK)
1492 return false;
1494 return std::string(s1->name()) < std::string(s2->name());
1497 // SYMBOLS is a list of object symbols from a dynamic object. Look
1498 // for any weak aliases, and record them so that if we add the weak
1499 // alias to the dynamic symbol table, we also add the corresponding
1500 // strong symbol.
1502 template<int size>
1503 void
1504 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1506 // Sort the vector by section index, then by offset, then by weak
1507 // ahead of strong.
1508 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1510 // Walk through the vector. For each weak definition, record
1511 // aliases.
1512 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1513 symbols->begin();
1514 p != symbols->end();
1515 ++p)
1517 if ((*p)->binding() != elfcpp::STB_WEAK)
1518 continue;
1520 // Build a circular list of weak aliases. Each symbol points to
1521 // the next one in the circular list.
1523 Sized_symbol<size>* from_sym = *p;
1524 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1525 for (q = p + 1; q != symbols->end(); ++q)
1527 bool dummy;
1528 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1529 || (*q)->value() != from_sym->value())
1530 break;
1532 this->weak_aliases_[from_sym] = *q;
1533 from_sym->set_has_alias();
1534 from_sym = *q;
1537 if (from_sym != *p)
1539 this->weak_aliases_[from_sym] = *p;
1540 from_sym->set_has_alias();
1543 p = q - 1;
1547 // Create and return a specially defined symbol. If ONLY_IF_REF is
1548 // true, then only create the symbol if there is a reference to it.
1549 // If this does not return NULL, it sets *POLDSYM to the existing
1550 // symbol if there is one. This sets *RESOLVE_OLDSYM if we should
1551 // resolve the newly created symbol to the old one. This
1552 // canonicalizes *PNAME and *PVERSION.
1554 template<int size, bool big_endian>
1555 Sized_symbol<size>*
1556 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1557 bool only_if_ref,
1558 Sized_symbol<size>** poldsym,
1559 bool *resolve_oldsym)
1561 *resolve_oldsym = false;
1563 // If the caller didn't give us a version, see if we get one from
1564 // the version script.
1565 std::string v;
1566 bool is_default_version = false;
1567 if (*pversion == NULL)
1569 bool is_global;
1570 if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
1572 if (is_global && !v.empty())
1574 *pversion = v.c_str();
1575 // If we get the version from a version script, then we
1576 // are also the default version.
1577 is_default_version = true;
1582 Symbol* oldsym;
1583 Sized_symbol<size>* sym;
1585 bool add_to_table = false;
1586 typename Symbol_table_type::iterator add_loc = this->table_.end();
1587 bool add_def_to_table = false;
1588 typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1590 if (only_if_ref)
1592 oldsym = this->lookup(*pname, *pversion);
1593 if (oldsym == NULL && is_default_version)
1594 oldsym = this->lookup(*pname, NULL);
1595 if (oldsym == NULL || !oldsym->is_undefined())
1596 return NULL;
1598 *pname = oldsym->name();
1599 if (!is_default_version)
1600 *pversion = oldsym->version();
1602 else
1604 // Canonicalize NAME and VERSION.
1605 Stringpool::Key name_key;
1606 *pname = this->namepool_.add(*pname, true, &name_key);
1608 Stringpool::Key version_key = 0;
1609 if (*pversion != NULL)
1610 *pversion = this->namepool_.add(*pversion, true, &version_key);
1612 Symbol* const snull = NULL;
1613 std::pair<typename Symbol_table_type::iterator, bool> ins =
1614 this->table_.insert(std::make_pair(std::make_pair(name_key,
1615 version_key),
1616 snull));
1618 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
1619 std::make_pair(this->table_.end(), false);
1620 if (is_default_version)
1622 const Stringpool::Key vnull = 0;
1623 insdefault =
1624 this->table_.insert(std::make_pair(std::make_pair(name_key,
1625 vnull),
1626 snull));
1629 if (!ins.second)
1631 // We already have a symbol table entry for NAME/VERSION.
1632 oldsym = ins.first->second;
1633 gold_assert(oldsym != NULL);
1635 if (is_default_version)
1637 Sized_symbol<size>* soldsym =
1638 this->get_sized_symbol<size>(oldsym);
1639 this->define_default_version<size, big_endian>(soldsym,
1640 insdefault.second,
1641 insdefault.first);
1644 else
1646 // We haven't seen this symbol before.
1647 gold_assert(ins.first->second == NULL);
1649 add_to_table = true;
1650 add_loc = ins.first;
1652 if (is_default_version && !insdefault.second)
1654 // We are adding NAME/VERSION, and it is the default
1655 // version. We already have an entry for NAME/NULL.
1656 oldsym = insdefault.first->second;
1657 *resolve_oldsym = true;
1659 else
1661 oldsym = NULL;
1663 if (is_default_version)
1665 add_def_to_table = true;
1666 add_def_loc = insdefault.first;
1672 const Target& target = parameters->target();
1673 if (!target.has_make_symbol())
1674 sym = new Sized_symbol<size>();
1675 else
1677 Sized_target<size, big_endian>* sized_target =
1678 parameters->sized_target<size, big_endian>();
1679 sym = sized_target->make_symbol();
1680 if (sym == NULL)
1681 return NULL;
1684 if (add_to_table)
1685 add_loc->second = sym;
1686 else
1687 gold_assert(oldsym != NULL);
1689 if (add_def_to_table)
1690 add_def_loc->second = sym;
1692 *poldsym = this->get_sized_symbol<size>(oldsym);
1694 return sym;
1697 // Define a symbol based on an Output_data.
1699 Symbol*
1700 Symbol_table::define_in_output_data(const char* name,
1701 const char* version,
1702 Defined defined,
1703 Output_data* od,
1704 uint64_t value,
1705 uint64_t symsize,
1706 elfcpp::STT type,
1707 elfcpp::STB binding,
1708 elfcpp::STV visibility,
1709 unsigned char nonvis,
1710 bool offset_is_from_end,
1711 bool only_if_ref)
1713 if (parameters->target().get_size() == 32)
1715 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1716 return this->do_define_in_output_data<32>(name, version, defined, od,
1717 value, symsize, type, binding,
1718 visibility, nonvis,
1719 offset_is_from_end,
1720 only_if_ref);
1721 #else
1722 gold_unreachable();
1723 #endif
1725 else if (parameters->target().get_size() == 64)
1727 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1728 return this->do_define_in_output_data<64>(name, version, defined, od,
1729 value, symsize, type, binding,
1730 visibility, nonvis,
1731 offset_is_from_end,
1732 only_if_ref);
1733 #else
1734 gold_unreachable();
1735 #endif
1737 else
1738 gold_unreachable();
1741 // Define a symbol in an Output_data, sized version.
1743 template<int size>
1744 Sized_symbol<size>*
1745 Symbol_table::do_define_in_output_data(
1746 const char* name,
1747 const char* version,
1748 Defined defined,
1749 Output_data* od,
1750 typename elfcpp::Elf_types<size>::Elf_Addr value,
1751 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1752 elfcpp::STT type,
1753 elfcpp::STB binding,
1754 elfcpp::STV visibility,
1755 unsigned char nonvis,
1756 bool offset_is_from_end,
1757 bool only_if_ref)
1759 Sized_symbol<size>* sym;
1760 Sized_symbol<size>* oldsym;
1761 bool resolve_oldsym;
1763 if (parameters->target().is_big_endian())
1765 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1766 sym = this->define_special_symbol<size, true>(&name, &version,
1767 only_if_ref, &oldsym,
1768 &resolve_oldsym);
1769 #else
1770 gold_unreachable();
1771 #endif
1773 else
1775 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1776 sym = this->define_special_symbol<size, false>(&name, &version,
1777 only_if_ref, &oldsym,
1778 &resolve_oldsym);
1779 #else
1780 gold_unreachable();
1781 #endif
1784 if (sym == NULL)
1785 return NULL;
1787 sym->init_output_data(name, version, od, value, symsize, type, binding,
1788 visibility, nonvis, offset_is_from_end);
1790 if (oldsym == NULL)
1792 if (binding == elfcpp::STB_LOCAL
1793 || this->version_script_.symbol_is_local(name))
1794 this->force_local(sym);
1795 else if (version != NULL)
1796 sym->set_is_default();
1797 return sym;
1800 if (Symbol_table::should_override_with_special(oldsym, defined))
1801 this->override_with_special(oldsym, sym);
1803 if (resolve_oldsym)
1804 return sym;
1805 else
1807 delete sym;
1808 return oldsym;
1812 // Define a symbol based on an Output_segment.
1814 Symbol*
1815 Symbol_table::define_in_output_segment(const char* name,
1816 const char* version,
1817 Defined defined,
1818 Output_segment* os,
1819 uint64_t value,
1820 uint64_t symsize,
1821 elfcpp::STT type,
1822 elfcpp::STB binding,
1823 elfcpp::STV visibility,
1824 unsigned char nonvis,
1825 Symbol::Segment_offset_base offset_base,
1826 bool only_if_ref)
1828 if (parameters->target().get_size() == 32)
1830 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1831 return this->do_define_in_output_segment<32>(name, version, defined, os,
1832 value, symsize, type,
1833 binding, visibility, nonvis,
1834 offset_base, only_if_ref);
1835 #else
1836 gold_unreachable();
1837 #endif
1839 else if (parameters->target().get_size() == 64)
1841 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1842 return this->do_define_in_output_segment<64>(name, version, defined, os,
1843 value, symsize, type,
1844 binding, visibility, nonvis,
1845 offset_base, only_if_ref);
1846 #else
1847 gold_unreachable();
1848 #endif
1850 else
1851 gold_unreachable();
1854 // Define a symbol in an Output_segment, sized version.
1856 template<int size>
1857 Sized_symbol<size>*
1858 Symbol_table::do_define_in_output_segment(
1859 const char* name,
1860 const char* version,
1861 Defined defined,
1862 Output_segment* os,
1863 typename elfcpp::Elf_types<size>::Elf_Addr value,
1864 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1865 elfcpp::STT type,
1866 elfcpp::STB binding,
1867 elfcpp::STV visibility,
1868 unsigned char nonvis,
1869 Symbol::Segment_offset_base offset_base,
1870 bool only_if_ref)
1872 Sized_symbol<size>* sym;
1873 Sized_symbol<size>* oldsym;
1874 bool resolve_oldsym;
1876 if (parameters->target().is_big_endian())
1878 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1879 sym = this->define_special_symbol<size, true>(&name, &version,
1880 only_if_ref, &oldsym,
1881 &resolve_oldsym);
1882 #else
1883 gold_unreachable();
1884 #endif
1886 else
1888 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1889 sym = this->define_special_symbol<size, false>(&name, &version,
1890 only_if_ref, &oldsym,
1891 &resolve_oldsym);
1892 #else
1893 gold_unreachable();
1894 #endif
1897 if (sym == NULL)
1898 return NULL;
1900 sym->init_output_segment(name, version, os, value, symsize, type, binding,
1901 visibility, nonvis, offset_base);
1903 if (oldsym == NULL)
1905 if (binding == elfcpp::STB_LOCAL
1906 || this->version_script_.symbol_is_local(name))
1907 this->force_local(sym);
1908 else if (version != NULL)
1909 sym->set_is_default();
1910 return sym;
1913 if (Symbol_table::should_override_with_special(oldsym, defined))
1914 this->override_with_special(oldsym, sym);
1916 if (resolve_oldsym)
1917 return sym;
1918 else
1920 delete sym;
1921 return oldsym;
1925 // Define a special symbol with a constant value. It is a multiple
1926 // definition error if this symbol is already defined.
1928 Symbol*
1929 Symbol_table::define_as_constant(const char* name,
1930 const char* version,
1931 Defined defined,
1932 uint64_t value,
1933 uint64_t symsize,
1934 elfcpp::STT type,
1935 elfcpp::STB binding,
1936 elfcpp::STV visibility,
1937 unsigned char nonvis,
1938 bool only_if_ref,
1939 bool force_override)
1941 if (parameters->target().get_size() == 32)
1943 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1944 return this->do_define_as_constant<32>(name, version, defined, value,
1945 symsize, type, binding,
1946 visibility, nonvis, only_if_ref,
1947 force_override);
1948 #else
1949 gold_unreachable();
1950 #endif
1952 else if (parameters->target().get_size() == 64)
1954 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1955 return this->do_define_as_constant<64>(name, version, defined, value,
1956 symsize, type, binding,
1957 visibility, nonvis, only_if_ref,
1958 force_override);
1959 #else
1960 gold_unreachable();
1961 #endif
1963 else
1964 gold_unreachable();
1967 // Define a symbol as a constant, sized version.
1969 template<int size>
1970 Sized_symbol<size>*
1971 Symbol_table::do_define_as_constant(
1972 const char* name,
1973 const char* version,
1974 Defined defined,
1975 typename elfcpp::Elf_types<size>::Elf_Addr value,
1976 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1977 elfcpp::STT type,
1978 elfcpp::STB binding,
1979 elfcpp::STV visibility,
1980 unsigned char nonvis,
1981 bool only_if_ref,
1982 bool force_override)
1984 Sized_symbol<size>* sym;
1985 Sized_symbol<size>* oldsym;
1986 bool resolve_oldsym;
1988 if (parameters->target().is_big_endian())
1990 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1991 sym = this->define_special_symbol<size, true>(&name, &version,
1992 only_if_ref, &oldsym,
1993 &resolve_oldsym);
1994 #else
1995 gold_unreachable();
1996 #endif
1998 else
2000 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2001 sym = this->define_special_symbol<size, false>(&name, &version,
2002 only_if_ref, &oldsym,
2003 &resolve_oldsym);
2004 #else
2005 gold_unreachable();
2006 #endif
2009 if (sym == NULL)
2010 return NULL;
2012 sym->init_constant(name, version, value, symsize, type, binding, visibility,
2013 nonvis);
2015 if (oldsym == NULL)
2017 // Version symbols are absolute symbols with name == version.
2018 // We don't want to force them to be local.
2019 if ((version == NULL
2020 || name != version
2021 || value != 0)
2022 && (binding == elfcpp::STB_LOCAL
2023 || this->version_script_.symbol_is_local(name)))
2024 this->force_local(sym);
2025 else if (version != NULL
2026 && (name != version || value != 0))
2027 sym->set_is_default();
2028 return sym;
2031 if (force_override
2032 || Symbol_table::should_override_with_special(oldsym, defined))
2033 this->override_with_special(oldsym, sym);
2035 if (resolve_oldsym)
2036 return sym;
2037 else
2039 delete sym;
2040 return oldsym;
2044 // Define a set of symbols in output sections.
2046 void
2047 Symbol_table::define_symbols(const Layout* layout, int count,
2048 const Define_symbol_in_section* p,
2049 bool only_if_ref)
2051 for (int i = 0; i < count; ++i, ++p)
2053 Output_section* os = layout->find_output_section(p->output_section);
2054 if (os != NULL)
2055 this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
2056 p->size, p->type, p->binding,
2057 p->visibility, p->nonvis,
2058 p->offset_is_from_end,
2059 only_if_ref || p->only_if_ref);
2060 else
2061 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2062 p->type, p->binding, p->visibility, p->nonvis,
2063 only_if_ref || p->only_if_ref,
2064 false);
2068 // Define a set of symbols in output segments.
2070 void
2071 Symbol_table::define_symbols(const Layout* layout, int count,
2072 const Define_symbol_in_segment* p,
2073 bool only_if_ref)
2075 for (int i = 0; i < count; ++i, ++p)
2077 Output_segment* os = layout->find_output_segment(p->segment_type,
2078 p->segment_flags_set,
2079 p->segment_flags_clear);
2080 if (os != NULL)
2081 this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
2082 p->size, p->type, p->binding,
2083 p->visibility, p->nonvis,
2084 p->offset_base,
2085 only_if_ref || p->only_if_ref);
2086 else
2087 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2088 p->type, p->binding, p->visibility, p->nonvis,
2089 only_if_ref || p->only_if_ref,
2090 false);
2094 // Define CSYM using a COPY reloc. POSD is the Output_data where the
2095 // symbol should be defined--typically a .dyn.bss section. VALUE is
2096 // the offset within POSD.
2098 template<int size>
2099 void
2100 Symbol_table::define_with_copy_reloc(
2101 Sized_symbol<size>* csym,
2102 Output_data* posd,
2103 typename elfcpp::Elf_types<size>::Elf_Addr value)
2105 gold_assert(csym->is_from_dynobj());
2106 gold_assert(!csym->is_copied_from_dynobj());
2107 Object* object = csym->object();
2108 gold_assert(object->is_dynamic());
2109 Dynobj* dynobj = static_cast<Dynobj*>(object);
2111 // Our copied variable has to override any variable in a shared
2112 // library.
2113 elfcpp::STB binding = csym->binding();
2114 if (binding == elfcpp::STB_WEAK)
2115 binding = elfcpp::STB_GLOBAL;
2117 this->define_in_output_data(csym->name(), csym->version(), COPY,
2118 posd, value, csym->symsize(),
2119 csym->type(), binding,
2120 csym->visibility(), csym->nonvis(),
2121 false, false);
2123 csym->set_is_copied_from_dynobj();
2124 csym->set_needs_dynsym_entry();
2126 this->copied_symbol_dynobjs_[csym] = dynobj;
2128 // We have now defined all aliases, but we have not entered them all
2129 // in the copied_symbol_dynobjs_ map.
2130 if (csym->has_alias())
2132 Symbol* sym = csym;
2133 while (true)
2135 sym = this->weak_aliases_[sym];
2136 if (sym == csym)
2137 break;
2138 gold_assert(sym->output_data() == posd);
2140 sym->set_is_copied_from_dynobj();
2141 this->copied_symbol_dynobjs_[sym] = dynobj;
2146 // SYM is defined using a COPY reloc. Return the dynamic object where
2147 // the original definition was found.
2149 Dynobj*
2150 Symbol_table::get_copy_source(const Symbol* sym) const
2152 gold_assert(sym->is_copied_from_dynobj());
2153 Copied_symbol_dynobjs::const_iterator p =
2154 this->copied_symbol_dynobjs_.find(sym);
2155 gold_assert(p != this->copied_symbol_dynobjs_.end());
2156 return p->second;
2159 // Add any undefined symbols named on the command line.
2161 void
2162 Symbol_table::add_undefined_symbols_from_command_line()
2164 if (parameters->options().any_undefined())
2166 if (parameters->target().get_size() == 32)
2168 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2169 this->do_add_undefined_symbols_from_command_line<32>();
2170 #else
2171 gold_unreachable();
2172 #endif
2174 else if (parameters->target().get_size() == 64)
2176 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2177 this->do_add_undefined_symbols_from_command_line<64>();
2178 #else
2179 gold_unreachable();
2180 #endif
2182 else
2183 gold_unreachable();
2187 template<int size>
2188 void
2189 Symbol_table::do_add_undefined_symbols_from_command_line()
2191 for (options::String_set::const_iterator p =
2192 parameters->options().undefined_begin();
2193 p != parameters->options().undefined_end();
2194 ++p)
2196 const char* name = p->c_str();
2198 if (this->lookup(name) != NULL)
2199 continue;
2201 const char* version = NULL;
2203 Sized_symbol<size>* sym;
2204 Sized_symbol<size>* oldsym;
2205 bool resolve_oldsym;
2206 if (parameters->target().is_big_endian())
2208 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2209 sym = this->define_special_symbol<size, true>(&name, &version,
2210 false, &oldsym,
2211 &resolve_oldsym);
2212 #else
2213 gold_unreachable();
2214 #endif
2216 else
2218 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2219 sym = this->define_special_symbol<size, false>(&name, &version,
2220 false, &oldsym,
2221 &resolve_oldsym);
2222 #else
2223 gold_unreachable();
2224 #endif
2227 gold_assert(oldsym == NULL);
2229 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2230 elfcpp::STV_DEFAULT, 0);
2231 ++this->saw_undefined_;
2235 // Set the dynamic symbol indexes. INDEX is the index of the first
2236 // global dynamic symbol. Pointers to the symbols are stored into the
2237 // vector SYMS. The names are added to DYNPOOL. This returns an
2238 // updated dynamic symbol index.
2240 unsigned int
2241 Symbol_table::set_dynsym_indexes(unsigned int index,
2242 std::vector<Symbol*>* syms,
2243 Stringpool* dynpool,
2244 Versions* versions)
2246 for (Symbol_table_type::iterator p = this->table_.begin();
2247 p != this->table_.end();
2248 ++p)
2250 Symbol* sym = p->second;
2252 // Note that SYM may already have a dynamic symbol index, since
2253 // some symbols appear more than once in the symbol table, with
2254 // and without a version.
2256 if (!sym->should_add_dynsym_entry())
2257 sym->set_dynsym_index(-1U);
2258 else if (!sym->has_dynsym_index())
2260 sym->set_dynsym_index(index);
2261 ++index;
2262 syms->push_back(sym);
2263 dynpool->add(sym->name(), false, NULL);
2265 // Record any version information.
2266 if (sym->version() != NULL)
2267 versions->record_version(this, dynpool, sym);
2269 // If the symbol is defined in a dynamic object and is
2270 // referenced in a regular object, then mark the dynamic
2271 // object as needed. This is used to implement --as-needed.
2272 if (sym->is_from_dynobj() && sym->in_reg())
2273 sym->object()->set_is_needed();
2277 // Finish up the versions. In some cases this may add new dynamic
2278 // symbols.
2279 index = versions->finalize(this, index, syms);
2281 return index;
2284 // Set the final values for all the symbols. The index of the first
2285 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2286 // file offset OFF. Add their names to POOL. Return the new file
2287 // offset. Update *PLOCAL_SYMCOUNT if necessary.
2289 off_t
2290 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2291 size_t dyncount, Stringpool* pool,
2292 unsigned int *plocal_symcount)
2294 off_t ret;
2296 gold_assert(*plocal_symcount != 0);
2297 this->first_global_index_ = *plocal_symcount;
2299 this->dynamic_offset_ = dynoff;
2300 this->first_dynamic_global_index_ = dyn_global_index;
2301 this->dynamic_count_ = dyncount;
2303 if (parameters->target().get_size() == 32)
2305 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2306 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
2307 #else
2308 gold_unreachable();
2309 #endif
2311 else if (parameters->target().get_size() == 64)
2313 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2314 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
2315 #else
2316 gold_unreachable();
2317 #endif
2319 else
2320 gold_unreachable();
2322 // Now that we have the final symbol table, we can reliably note
2323 // which symbols should get warnings.
2324 this->warnings_.note_warnings(this);
2326 return ret;
2329 // SYM is going into the symbol table at *PINDEX. Add the name to
2330 // POOL, update *PINDEX and *POFF.
2332 template<int size>
2333 void
2334 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2335 unsigned int* pindex, off_t* poff)
2337 sym->set_symtab_index(*pindex);
2338 pool->add(sym->name(), false, NULL);
2339 ++*pindex;
2340 *poff += elfcpp::Elf_sizes<size>::sym_size;
2343 // Set the final value for all the symbols. This is called after
2344 // Layout::finalize, so all the output sections have their final
2345 // address.
2347 template<int size>
2348 off_t
2349 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2350 unsigned int* plocal_symcount)
2352 off = align_address(off, size >> 3);
2353 this->offset_ = off;
2355 unsigned int index = *plocal_symcount;
2356 const unsigned int orig_index = index;
2358 // First do all the symbols which have been forced to be local, as
2359 // they must appear before all global symbols.
2360 for (Forced_locals::iterator p = this->forced_locals_.begin();
2361 p != this->forced_locals_.end();
2362 ++p)
2364 Symbol* sym = *p;
2365 gold_assert(sym->is_forced_local());
2366 if (this->sized_finalize_symbol<size>(sym))
2368 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2369 ++*plocal_symcount;
2373 // Now do all the remaining symbols.
2374 for (Symbol_table_type::iterator p = this->table_.begin();
2375 p != this->table_.end();
2376 ++p)
2378 Symbol* sym = p->second;
2379 if (this->sized_finalize_symbol<size>(sym))
2380 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2383 this->output_count_ = index - orig_index;
2385 return off;
2388 // Compute the final value of SYM and store status in location PSTATUS.
2389 // During relaxation, this may be called multiple times for a symbol to
2390 // compute its would-be final value in each relaxation pass.
2392 template<int size>
2393 typename Sized_symbol<size>::Value_type
2394 Symbol_table::compute_final_value(
2395 const Sized_symbol<size>* sym,
2396 Compute_final_value_status* pstatus) const
2398 typedef typename Sized_symbol<size>::Value_type Value_type;
2399 Value_type value;
2401 switch (sym->source())
2403 case Symbol::FROM_OBJECT:
2405 bool is_ordinary;
2406 unsigned int shndx = sym->shndx(&is_ordinary);
2408 if (!is_ordinary
2409 && shndx != elfcpp::SHN_ABS
2410 && !Symbol::is_common_shndx(shndx))
2412 *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2413 return 0;
2416 Object* symobj = sym->object();
2417 if (symobj->is_dynamic())
2419 value = 0;
2420 shndx = elfcpp::SHN_UNDEF;
2422 else if (symobj->pluginobj() != NULL)
2424 value = 0;
2425 shndx = elfcpp::SHN_UNDEF;
2427 else if (shndx == elfcpp::SHN_UNDEF)
2428 value = 0;
2429 else if (!is_ordinary
2430 && (shndx == elfcpp::SHN_ABS
2431 || Symbol::is_common_shndx(shndx)))
2432 value = sym->value();
2433 else
2435 Relobj* relobj = static_cast<Relobj*>(symobj);
2436 Output_section* os = relobj->output_section(shndx);
2438 if (this->is_section_folded(relobj, shndx))
2440 gold_assert(os == NULL);
2441 // Get the os of the section it is folded onto.
2442 Section_id folded = this->icf_->get_folded_section(relobj,
2443 shndx);
2444 gold_assert(folded.first != NULL);
2445 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
2446 unsigned folded_shndx = folded.second;
2448 os = folded_obj->output_section(folded_shndx);
2449 gold_assert(os != NULL);
2451 // Replace (relobj, shndx) with canonical ICF input section.
2452 shndx = folded_shndx;
2453 relobj = folded_obj;
2456 uint64_t secoff64 = relobj->output_section_offset(shndx);
2457 if (os == NULL)
2459 bool static_or_reloc = (parameters->doing_static_link() ||
2460 parameters->options().relocatable());
2461 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2463 *pstatus = CFVS_NO_OUTPUT_SECTION;
2464 return 0;
2467 if (secoff64 == -1ULL)
2469 // The section needs special handling (e.g., a merge section).
2471 value = os->output_address(relobj, shndx, sym->value());
2473 else
2475 Value_type secoff =
2476 convert_types<Value_type, uint64_t>(secoff64);
2477 if (sym->type() == elfcpp::STT_TLS)
2478 value = sym->value() + os->tls_offset() + secoff;
2479 else
2480 value = sym->value() + os->address() + secoff;
2484 break;
2486 case Symbol::IN_OUTPUT_DATA:
2488 Output_data* od = sym->output_data();
2489 value = sym->value();
2490 if (sym->type() != elfcpp::STT_TLS)
2491 value += od->address();
2492 else
2494 Output_section* os = od->output_section();
2495 gold_assert(os != NULL);
2496 value += os->tls_offset() + (od->address() - os->address());
2498 if (sym->offset_is_from_end())
2499 value += od->data_size();
2501 break;
2503 case Symbol::IN_OUTPUT_SEGMENT:
2505 Output_segment* os = sym->output_segment();
2506 value = sym->value();
2507 if (sym->type() != elfcpp::STT_TLS)
2508 value += os->vaddr();
2509 switch (sym->offset_base())
2511 case Symbol::SEGMENT_START:
2512 break;
2513 case Symbol::SEGMENT_END:
2514 value += os->memsz();
2515 break;
2516 case Symbol::SEGMENT_BSS:
2517 value += os->filesz();
2518 break;
2519 default:
2520 gold_unreachable();
2523 break;
2525 case Symbol::IS_CONSTANT:
2526 value = sym->value();
2527 break;
2529 case Symbol::IS_UNDEFINED:
2530 value = 0;
2531 break;
2533 default:
2534 gold_unreachable();
2537 *pstatus = CFVS_OK;
2538 return value;
2541 // Finalize the symbol SYM. This returns true if the symbol should be
2542 // added to the symbol table, false otherwise.
2544 template<int size>
2545 bool
2546 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2548 typedef typename Sized_symbol<size>::Value_type Value_type;
2550 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2552 // The default version of a symbol may appear twice in the symbol
2553 // table. We only need to finalize it once.
2554 if (sym->has_symtab_index())
2555 return false;
2557 if (!sym->in_reg())
2559 gold_assert(!sym->has_symtab_index());
2560 sym->set_symtab_index(-1U);
2561 gold_assert(sym->dynsym_index() == -1U);
2562 return false;
2565 // Compute final symbol value.
2566 Compute_final_value_status status;
2567 Value_type value = this->compute_final_value(sym, &status);
2569 switch (status)
2571 case CFVS_OK:
2572 break;
2573 case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2575 bool is_ordinary;
2576 unsigned int shndx = sym->shndx(&is_ordinary);
2577 gold_error(_("%s: unsupported symbol section 0x%x"),
2578 sym->demangled_name().c_str(), shndx);
2580 break;
2581 case CFVS_NO_OUTPUT_SECTION:
2582 sym->set_symtab_index(-1U);
2583 return false;
2584 default:
2585 gold_unreachable();
2588 sym->set_value(value);
2590 if (parameters->options().strip_all()
2591 || !parameters->options().should_retain_symbol(sym->name()))
2593 sym->set_symtab_index(-1U);
2594 return false;
2597 return true;
2600 // Write out the global symbols.
2602 void
2603 Symbol_table::write_globals(const Stringpool* sympool,
2604 const Stringpool* dynpool,
2605 Output_symtab_xindex* symtab_xindex,
2606 Output_symtab_xindex* dynsym_xindex,
2607 Output_file* of) const
2609 switch (parameters->size_and_endianness())
2611 #ifdef HAVE_TARGET_32_LITTLE
2612 case Parameters::TARGET_32_LITTLE:
2613 this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
2614 dynsym_xindex, of);
2615 break;
2616 #endif
2617 #ifdef HAVE_TARGET_32_BIG
2618 case Parameters::TARGET_32_BIG:
2619 this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
2620 dynsym_xindex, of);
2621 break;
2622 #endif
2623 #ifdef HAVE_TARGET_64_LITTLE
2624 case Parameters::TARGET_64_LITTLE:
2625 this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
2626 dynsym_xindex, of);
2627 break;
2628 #endif
2629 #ifdef HAVE_TARGET_64_BIG
2630 case Parameters::TARGET_64_BIG:
2631 this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
2632 dynsym_xindex, of);
2633 break;
2634 #endif
2635 default:
2636 gold_unreachable();
2640 // Write out the global symbols.
2642 template<int size, bool big_endian>
2643 void
2644 Symbol_table::sized_write_globals(const Stringpool* sympool,
2645 const Stringpool* dynpool,
2646 Output_symtab_xindex* symtab_xindex,
2647 Output_symtab_xindex* dynsym_xindex,
2648 Output_file* of) const
2650 const Target& target = parameters->target();
2652 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2654 const unsigned int output_count = this->output_count_;
2655 const section_size_type oview_size = output_count * sym_size;
2656 const unsigned int first_global_index = this->first_global_index_;
2657 unsigned char* psyms;
2658 if (this->offset_ == 0 || output_count == 0)
2659 psyms = NULL;
2660 else
2661 psyms = of->get_output_view(this->offset_, oview_size);
2663 const unsigned int dynamic_count = this->dynamic_count_;
2664 const section_size_type dynamic_size = dynamic_count * sym_size;
2665 const unsigned int first_dynamic_global_index =
2666 this->first_dynamic_global_index_;
2667 unsigned char* dynamic_view;
2668 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2669 dynamic_view = NULL;
2670 else
2671 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2673 for (Symbol_table_type::const_iterator p = this->table_.begin();
2674 p != this->table_.end();
2675 ++p)
2677 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2679 // Possibly warn about unresolved symbols in shared libraries.
2680 this->warn_about_undefined_dynobj_symbol(sym);
2682 unsigned int sym_index = sym->symtab_index();
2683 unsigned int dynsym_index;
2684 if (dynamic_view == NULL)
2685 dynsym_index = -1U;
2686 else
2687 dynsym_index = sym->dynsym_index();
2689 if (sym_index == -1U && dynsym_index == -1U)
2691 // This symbol is not included in the output file.
2692 continue;
2695 unsigned int shndx;
2696 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2697 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
2698 switch (sym->source())
2700 case Symbol::FROM_OBJECT:
2702 bool is_ordinary;
2703 unsigned int in_shndx = sym->shndx(&is_ordinary);
2705 if (!is_ordinary
2706 && in_shndx != elfcpp::SHN_ABS
2707 && !Symbol::is_common_shndx(in_shndx))
2709 gold_error(_("%s: unsupported symbol section 0x%x"),
2710 sym->demangled_name().c_str(), in_shndx);
2711 shndx = in_shndx;
2713 else
2715 Object* symobj = sym->object();
2716 if (symobj->is_dynamic())
2718 if (sym->needs_dynsym_value())
2719 dynsym_value = target.dynsym_value(sym);
2720 shndx = elfcpp::SHN_UNDEF;
2722 else if (symobj->pluginobj() != NULL)
2723 shndx = elfcpp::SHN_UNDEF;
2724 else if (in_shndx == elfcpp::SHN_UNDEF
2725 || (!is_ordinary
2726 && (in_shndx == elfcpp::SHN_ABS
2727 || Symbol::is_common_shndx(in_shndx))))
2728 shndx = in_shndx;
2729 else
2731 Relobj* relobj = static_cast<Relobj*>(symobj);
2732 Output_section* os = relobj->output_section(in_shndx);
2733 if (this->is_section_folded(relobj, in_shndx))
2735 // This global symbol must be written out even though
2736 // it is folded.
2737 // Get the os of the section it is folded onto.
2738 Section_id folded =
2739 this->icf_->get_folded_section(relobj, in_shndx);
2740 gold_assert(folded.first !=NULL);
2741 Relobj* folded_obj =
2742 reinterpret_cast<Relobj*>(folded.first);
2743 os = folded_obj->output_section(folded.second);
2744 gold_assert(os != NULL);
2746 gold_assert(os != NULL);
2747 shndx = os->out_shndx();
2749 if (shndx >= elfcpp::SHN_LORESERVE)
2751 if (sym_index != -1U)
2752 symtab_xindex->add(sym_index, shndx);
2753 if (dynsym_index != -1U)
2754 dynsym_xindex->add(dynsym_index, shndx);
2755 shndx = elfcpp::SHN_XINDEX;
2758 // In object files symbol values are section
2759 // relative.
2760 if (parameters->options().relocatable())
2761 sym_value -= os->address();
2765 break;
2767 case Symbol::IN_OUTPUT_DATA:
2768 shndx = sym->output_data()->out_shndx();
2769 if (shndx >= elfcpp::SHN_LORESERVE)
2771 if (sym_index != -1U)
2772 symtab_xindex->add(sym_index, shndx);
2773 if (dynsym_index != -1U)
2774 dynsym_xindex->add(dynsym_index, shndx);
2775 shndx = elfcpp::SHN_XINDEX;
2777 break;
2779 case Symbol::IN_OUTPUT_SEGMENT:
2780 shndx = elfcpp::SHN_ABS;
2781 break;
2783 case Symbol::IS_CONSTANT:
2784 shndx = elfcpp::SHN_ABS;
2785 break;
2787 case Symbol::IS_UNDEFINED:
2788 shndx = elfcpp::SHN_UNDEF;
2789 break;
2791 default:
2792 gold_unreachable();
2795 if (sym_index != -1U)
2797 sym_index -= first_global_index;
2798 gold_assert(sym_index < output_count);
2799 unsigned char* ps = psyms + (sym_index * sym_size);
2800 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2801 sympool, ps);
2804 if (dynsym_index != -1U)
2806 dynsym_index -= first_dynamic_global_index;
2807 gold_assert(dynsym_index < dynamic_count);
2808 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2809 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2810 dynpool, pd);
2814 of->write_output_view(this->offset_, oview_size, psyms);
2815 if (dynamic_view != NULL)
2816 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2819 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2820 // strtab holding the name.
2822 template<int size, bool big_endian>
2823 void
2824 Symbol_table::sized_write_symbol(
2825 Sized_symbol<size>* sym,
2826 typename elfcpp::Elf_types<size>::Elf_Addr value,
2827 unsigned int shndx,
2828 const Stringpool* pool,
2829 unsigned char* p) const
2831 elfcpp::Sym_write<size, big_endian> osym(p);
2832 osym.put_st_name(pool->get_offset(sym->name()));
2833 osym.put_st_value(value);
2834 // Use a symbol size of zero for undefined symbols from shared libraries.
2835 if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
2836 osym.put_st_size(0);
2837 else
2838 osym.put_st_size(sym->symsize());
2839 elfcpp::STT type = sym->type();
2840 // Turn IFUNC symbols from shared libraries into normal FUNC symbols.
2841 if (type == elfcpp::STT_GNU_IFUNC
2842 && sym->is_from_dynobj())
2843 type = elfcpp::STT_FUNC;
2844 // A version script may have overridden the default binding.
2845 if (sym->is_forced_local())
2846 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
2847 else
2848 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), type));
2849 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2850 osym.put_st_shndx(shndx);
2853 // Check for unresolved symbols in shared libraries. This is
2854 // controlled by the --allow-shlib-undefined option.
2856 // We only warn about libraries for which we have seen all the
2857 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2858 // which were not seen in this link. If we didn't see a DT_NEEDED
2859 // entry, we aren't going to be able to reliably report whether the
2860 // symbol is undefined.
2862 // We also don't warn about libraries found in a system library
2863 // directory (e.g., /lib or /usr/lib); we assume that those libraries
2864 // are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
2865 // can have undefined references satisfied by ld-linux.so.
2867 inline void
2868 Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
2870 bool dummy;
2871 if (sym->source() == Symbol::FROM_OBJECT
2872 && sym->object()->is_dynamic()
2873 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
2874 && sym->binding() != elfcpp::STB_WEAK
2875 && !parameters->options().allow_shlib_undefined()
2876 && !parameters->target().is_defined_by_abi(sym)
2877 && !sym->object()->is_in_system_directory())
2879 // A very ugly cast.
2880 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2881 if (!dynobj->has_unknown_needed_entries())
2882 gold_undefined_symbol(sym);
2886 // Write out a section symbol. Return the update offset.
2888 void
2889 Symbol_table::write_section_symbol(const Output_section *os,
2890 Output_symtab_xindex* symtab_xindex,
2891 Output_file* of,
2892 off_t offset) const
2894 switch (parameters->size_and_endianness())
2896 #ifdef HAVE_TARGET_32_LITTLE
2897 case Parameters::TARGET_32_LITTLE:
2898 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2899 offset);
2900 break;
2901 #endif
2902 #ifdef HAVE_TARGET_32_BIG
2903 case Parameters::TARGET_32_BIG:
2904 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2905 offset);
2906 break;
2907 #endif
2908 #ifdef HAVE_TARGET_64_LITTLE
2909 case Parameters::TARGET_64_LITTLE:
2910 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2911 offset);
2912 break;
2913 #endif
2914 #ifdef HAVE_TARGET_64_BIG
2915 case Parameters::TARGET_64_BIG:
2916 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2917 offset);
2918 break;
2919 #endif
2920 default:
2921 gold_unreachable();
2925 // Write out a section symbol, specialized for size and endianness.
2927 template<int size, bool big_endian>
2928 void
2929 Symbol_table::sized_write_section_symbol(const Output_section* os,
2930 Output_symtab_xindex* symtab_xindex,
2931 Output_file* of,
2932 off_t offset) const
2934 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2936 unsigned char* pov = of->get_output_view(offset, sym_size);
2938 elfcpp::Sym_write<size, big_endian> osym(pov);
2939 osym.put_st_name(0);
2940 if (parameters->options().relocatable())
2941 osym.put_st_value(0);
2942 else
2943 osym.put_st_value(os->address());
2944 osym.put_st_size(0);
2945 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2946 elfcpp::STT_SECTION));
2947 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2949 unsigned int shndx = os->out_shndx();
2950 if (shndx >= elfcpp::SHN_LORESERVE)
2952 symtab_xindex->add(os->symtab_index(), shndx);
2953 shndx = elfcpp::SHN_XINDEX;
2955 osym.put_st_shndx(shndx);
2957 of->write_output_view(offset, sym_size, pov);
2960 // Print statistical information to stderr. This is used for --stats.
2962 void
2963 Symbol_table::print_stats() const
2965 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2966 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2967 program_name, this->table_.size(), this->table_.bucket_count());
2968 #else
2969 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2970 program_name, this->table_.size());
2971 #endif
2972 this->namepool_.print_stats("symbol table stringpool");
2975 // We check for ODR violations by looking for symbols with the same
2976 // name for which the debugging information reports that they were
2977 // defined in different source locations. When comparing the source
2978 // location, we consider instances with the same base filename and
2979 // line number to be the same. This is because different object
2980 // files/shared libraries can include the same header file using
2981 // different paths, and we don't want to report an ODR violation in
2982 // that case.
2984 // This struct is used to compare line information, as returned by
2985 // Dwarf_line_info::one_addr2line. It implements a < comparison
2986 // operator used with std::set.
2988 struct Odr_violation_compare
2990 bool
2991 operator()(const std::string& s1, const std::string& s2) const
2993 std::string::size_type pos1 = s1.rfind('/');
2994 std::string::size_type pos2 = s2.rfind('/');
2995 if (pos1 == std::string::npos
2996 || pos2 == std::string::npos)
2997 return s1 < s2;
2998 return s1.compare(pos1, std::string::npos,
2999 s2, pos2, std::string::npos) < 0;
3003 // Check candidate_odr_violations_ to find symbols with the same name
3004 // but apparently different definitions (different source-file/line-no).
3006 void
3007 Symbol_table::detect_odr_violations(const Task* task,
3008 const char* output_file_name) const
3010 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3011 it != candidate_odr_violations_.end();
3012 ++it)
3014 const char* symbol_name = it->first;
3015 // We use a sorted set so the output is deterministic.
3016 std::set<std::string, Odr_violation_compare> line_nums;
3018 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3019 locs = it->second.begin();
3020 locs != it->second.end();
3021 ++locs)
3023 // We need to lock the object in order to read it. This
3024 // means that we have to run in a singleton Task. If we
3025 // want to run this in a general Task for better
3026 // performance, we will need one Task for object, plus
3027 // appropriate locking to ensure that we don't conflict with
3028 // other uses of the object. Also note, one_addr2line is not
3029 // currently thread-safe.
3030 Task_lock_obj<Object> tl(task, locs->object);
3031 // 16 is the size of the object-cache that one_addr2line should use.
3032 std::string lineno = Dwarf_line_info::one_addr2line(
3033 locs->object, locs->shndx, locs->offset, 16);
3034 if (!lineno.empty())
3035 line_nums.insert(lineno);
3038 if (line_nums.size() > 1)
3040 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
3041 "places (possible ODR violation):"),
3042 output_file_name, demangle(symbol_name).c_str());
3043 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
3044 it2 != line_nums.end();
3045 ++it2)
3046 fprintf(stderr, " %s\n", it2->c_str());
3049 // We only call one_addr2line() in this function, so we can clear its cache.
3050 Dwarf_line_info::clear_addr2line_cache();
3053 // Warnings functions.
3055 // Add a new warning.
3057 void
3058 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
3059 const std::string& warning)
3061 name = symtab->canonicalize_name(name);
3062 this->warnings_[name].set(obj, warning);
3065 // Look through the warnings and mark the symbols for which we should
3066 // warn. This is called during Layout::finalize when we know the
3067 // sources for all the symbols.
3069 void
3070 Warnings::note_warnings(Symbol_table* symtab)
3072 for (Warning_table::iterator p = this->warnings_.begin();
3073 p != this->warnings_.end();
3074 ++p)
3076 Symbol* sym = symtab->lookup(p->first, NULL);
3077 if (sym != NULL
3078 && sym->source() == Symbol::FROM_OBJECT
3079 && sym->object() == p->second.object)
3080 sym->set_has_warning();
3084 // Issue a warning. This is called when we see a relocation against a
3085 // symbol for which has a warning.
3087 template<int size, bool big_endian>
3088 void
3089 Warnings::issue_warning(const Symbol* sym,
3090 const Relocate_info<size, big_endian>* relinfo,
3091 size_t relnum, off_t reloffset) const
3093 gold_assert(sym->has_warning());
3094 Warning_table::const_iterator p = this->warnings_.find(sym->name());
3095 gold_assert(p != this->warnings_.end());
3096 gold_warning_at_location(relinfo, relnum, reloffset,
3097 "%s", p->second.text.c_str());
3100 // Instantiate the templates we need. We could use the configure
3101 // script to restrict this to only the ones needed for implemented
3102 // targets.
3104 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3105 template
3106 void
3107 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3108 #endif
3110 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3111 template
3112 void
3113 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3114 #endif
3116 #ifdef HAVE_TARGET_32_LITTLE
3117 template
3118 void
3119 Symbol_table::add_from_relobj<32, false>(
3120 Sized_relobj<32, false>* relobj,
3121 const unsigned char* syms,
3122 size_t count,
3123 size_t symndx_offset,
3124 const char* sym_names,
3125 size_t sym_name_size,
3126 Sized_relobj<32, false>::Symbols* sympointers,
3127 size_t* defined);
3128 #endif
3130 #ifdef HAVE_TARGET_32_BIG
3131 template
3132 void
3133 Symbol_table::add_from_relobj<32, true>(
3134 Sized_relobj<32, true>* relobj,
3135 const unsigned char* syms,
3136 size_t count,
3137 size_t symndx_offset,
3138 const char* sym_names,
3139 size_t sym_name_size,
3140 Sized_relobj<32, true>::Symbols* sympointers,
3141 size_t* defined);
3142 #endif
3144 #ifdef HAVE_TARGET_64_LITTLE
3145 template
3146 void
3147 Symbol_table::add_from_relobj<64, false>(
3148 Sized_relobj<64, false>* relobj,
3149 const unsigned char* syms,
3150 size_t count,
3151 size_t symndx_offset,
3152 const char* sym_names,
3153 size_t sym_name_size,
3154 Sized_relobj<64, false>::Symbols* sympointers,
3155 size_t* defined);
3156 #endif
3158 #ifdef HAVE_TARGET_64_BIG
3159 template
3160 void
3161 Symbol_table::add_from_relobj<64, true>(
3162 Sized_relobj<64, true>* relobj,
3163 const unsigned char* syms,
3164 size_t count,
3165 size_t symndx_offset,
3166 const char* sym_names,
3167 size_t sym_name_size,
3168 Sized_relobj<64, true>::Symbols* sympointers,
3169 size_t* defined);
3170 #endif
3172 #ifdef HAVE_TARGET_32_LITTLE
3173 template
3174 Symbol*
3175 Symbol_table::add_from_pluginobj<32, false>(
3176 Sized_pluginobj<32, false>* obj,
3177 const char* name,
3178 const char* ver,
3179 elfcpp::Sym<32, false>* sym);
3180 #endif
3182 #ifdef HAVE_TARGET_32_BIG
3183 template
3184 Symbol*
3185 Symbol_table::add_from_pluginobj<32, true>(
3186 Sized_pluginobj<32, true>* obj,
3187 const char* name,
3188 const char* ver,
3189 elfcpp::Sym<32, true>* sym);
3190 #endif
3192 #ifdef HAVE_TARGET_64_LITTLE
3193 template
3194 Symbol*
3195 Symbol_table::add_from_pluginobj<64, false>(
3196 Sized_pluginobj<64, false>* obj,
3197 const char* name,
3198 const char* ver,
3199 elfcpp::Sym<64, false>* sym);
3200 #endif
3202 #ifdef HAVE_TARGET_64_BIG
3203 template
3204 Symbol*
3205 Symbol_table::add_from_pluginobj<64, true>(
3206 Sized_pluginobj<64, true>* obj,
3207 const char* name,
3208 const char* ver,
3209 elfcpp::Sym<64, true>* sym);
3210 #endif
3212 #ifdef HAVE_TARGET_32_LITTLE
3213 template
3214 void
3215 Symbol_table::add_from_dynobj<32, false>(
3216 Sized_dynobj<32, false>* dynobj,
3217 const unsigned char* syms,
3218 size_t count,
3219 const char* sym_names,
3220 size_t sym_name_size,
3221 const unsigned char* versym,
3222 size_t versym_size,
3223 const std::vector<const char*>* version_map,
3224 Sized_relobj<32, false>::Symbols* sympointers,
3225 size_t* defined);
3226 #endif
3228 #ifdef HAVE_TARGET_32_BIG
3229 template
3230 void
3231 Symbol_table::add_from_dynobj<32, true>(
3232 Sized_dynobj<32, true>* dynobj,
3233 const unsigned char* syms,
3234 size_t count,
3235 const char* sym_names,
3236 size_t sym_name_size,
3237 const unsigned char* versym,
3238 size_t versym_size,
3239 const std::vector<const char*>* version_map,
3240 Sized_relobj<32, true>::Symbols* sympointers,
3241 size_t* defined);
3242 #endif
3244 #ifdef HAVE_TARGET_64_LITTLE
3245 template
3246 void
3247 Symbol_table::add_from_dynobj<64, false>(
3248 Sized_dynobj<64, false>* dynobj,
3249 const unsigned char* syms,
3250 size_t count,
3251 const char* sym_names,
3252 size_t sym_name_size,
3253 const unsigned char* versym,
3254 size_t versym_size,
3255 const std::vector<const char*>* version_map,
3256 Sized_relobj<64, false>::Symbols* sympointers,
3257 size_t* defined);
3258 #endif
3260 #ifdef HAVE_TARGET_64_BIG
3261 template
3262 void
3263 Symbol_table::add_from_dynobj<64, true>(
3264 Sized_dynobj<64, true>* dynobj,
3265 const unsigned char* syms,
3266 size_t count,
3267 const char* sym_names,
3268 size_t sym_name_size,
3269 const unsigned char* versym,
3270 size_t versym_size,
3271 const std::vector<const char*>* version_map,
3272 Sized_relobj<64, true>::Symbols* sympointers,
3273 size_t* defined);
3274 #endif
3276 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3277 template
3278 void
3279 Symbol_table::define_with_copy_reloc<32>(
3280 Sized_symbol<32>* sym,
3281 Output_data* posd,
3282 elfcpp::Elf_types<32>::Elf_Addr value);
3283 #endif
3285 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3286 template
3287 void
3288 Symbol_table::define_with_copy_reloc<64>(
3289 Sized_symbol<64>* sym,
3290 Output_data* posd,
3291 elfcpp::Elf_types<64>::Elf_Addr value);
3292 #endif
3294 #ifdef HAVE_TARGET_32_LITTLE
3295 template
3296 void
3297 Warnings::issue_warning<32, false>(const Symbol* sym,
3298 const Relocate_info<32, false>* relinfo,
3299 size_t relnum, off_t reloffset) const;
3300 #endif
3302 #ifdef HAVE_TARGET_32_BIG
3303 template
3304 void
3305 Warnings::issue_warning<32, true>(const Symbol* sym,
3306 const Relocate_info<32, true>* relinfo,
3307 size_t relnum, off_t reloffset) const;
3308 #endif
3310 #ifdef HAVE_TARGET_64_LITTLE
3311 template
3312 void
3313 Warnings::issue_warning<64, false>(const Symbol* sym,
3314 const Relocate_info<64, false>* relinfo,
3315 size_t relnum, off_t reloffset) const;
3316 #endif
3318 #ifdef HAVE_TARGET_64_BIG
3319 template
3320 void
3321 Warnings::issue_warning<64, true>(const Symbol* sym,
3322 const Relocate_info<64, true>* relinfo,
3323 size_t relnum, off_t reloffset) const;
3324 #endif
3326 } // End namespace gold.