1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
34 #include "dwarf_reader.h"
38 #include "workqueue.h"
40 #include "demangle.h" // needed for --dynamic-list-cpp-new
48 // Initialize fields in Symbol. This initializes everything except u_
52 Symbol::init_fields(const char* name
, const char* version
,
53 elfcpp::STT type
, elfcpp::STB binding
,
54 elfcpp::STV visibility
, unsigned char nonvis
)
57 this->version_
= version
;
58 this->symtab_index_
= 0;
59 this->dynsym_index_
= 0;
60 this->got_offsets_
.init();
61 this->plt_offset_
= 0;
63 this->binding_
= binding
;
64 this->visibility_
= visibility
;
65 this->nonvis_
= nonvis
;
66 this->is_target_special_
= false;
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_plt_offset_
= false;
74 this->has_warning_
= false;
75 this->is_copied_from_dynobj_
= false;
76 this->is_forced_local_
= false;
77 this->is_ordinary_shndx_
= false;
78 this->in_real_elf_
= false;
81 // Return the demangled version of the symbol's name, but only
82 // if the --demangle flag was set.
85 demangle(const char* name
)
87 if (!parameters
->options().do_demangle())
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
)
96 std::string
retval(demangled_name
);
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
>
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.
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.
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
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
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.
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.
204 template<bool big_endian
>
206 Sized_symbol
<size
>::init_object(const char* name
, const char* version
,
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
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
,
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
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
,
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
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.
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
);
282 // Allocate a common symbol.
286 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
288 this->allocate_base_common(od
);
289 this->value_
= value
;
292 // The ""'s around str ensure str is a string literal, so sizeof works.
293 #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
295 // Return true if this symbol should be added to the dynamic symbol
299 Symbol::should_add_dynsym_entry() const
301 // If the symbol is used by a dynamic relocation, we need to add it.
302 if (this->needs_dynsym_entry())
305 // If the symbol was forced local in a version script, do not add it.
306 if (this->is_forced_local())
309 // If the symbol was forced dynamic in a --dynamic-list file, add it.
310 if (parameters
->options().in_dynamic_list(this->name()))
313 // If dynamic-list-data was specified, add any STT_OBJECT.
314 if (parameters
->options().dynamic_list_data()
315 && !this->is_from_dynobj()
316 && this->type() == elfcpp::STT_OBJECT
)
319 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
320 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
321 if ((parameters
->options().dynamic_list_cpp_new()
322 || parameters
->options().dynamic_list_cpp_typeinfo())
323 && !this->is_from_dynobj())
325 // TODO(csilvers): We could probably figure out if we're an operator
326 // new/delete or typeinfo without the need to demangle.
327 char* demangled_name
= cplus_demangle(this->name(),
328 DMGL_ANSI
| DMGL_PARAMS
);
329 if (demangled_name
== NULL
)
331 // Not a C++ symbol, so it can't satisfy these flags
333 else if (parameters
->options().dynamic_list_cpp_new()
334 && (strprefix(demangled_name
, "operator new")
335 || strprefix(demangled_name
, "operator delete")))
337 free(demangled_name
);
340 else if (parameters
->options().dynamic_list_cpp_typeinfo()
341 && (strprefix(demangled_name
, "typeinfo name for")
342 || strprefix(demangled_name
, "typeinfo for")))
344 free(demangled_name
);
348 free(demangled_name
);
351 // If exporting all symbols or building a shared library,
352 // and the symbol is defined in a regular object and is
353 // externally visible, we need to add it.
354 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
355 && !this->is_from_dynobj()
356 && this->is_externally_visible())
362 // Return true if the final value of this symbol is known at link
366 Symbol::final_value_is_known() const
368 // If we are not generating an executable, then no final values are
369 // known, since they will change at runtime.
370 if (parameters
->options().shared() || parameters
->options().relocatable())
373 // If the symbol is not from an object file, and is not undefined,
374 // then it is defined, and known.
375 if (this->source_
!= FROM_OBJECT
)
377 if (this->source_
!= IS_UNDEFINED
)
382 // If the symbol is from a dynamic object, then the final value
384 if (this->object()->is_dynamic())
387 // If the symbol is not undefined (it is defined or common),
388 // then the final value is known.
389 if (!this->is_undefined())
393 // If the symbol is undefined, then whether the final value is known
394 // depends on whether we are doing a static link. If we are doing a
395 // dynamic link, then the final value could be filled in at runtime.
396 // This could reasonably be the case for a weak undefined symbol.
397 return parameters
->doing_static_link();
400 // Return the output section where this symbol is defined.
403 Symbol::output_section() const
405 switch (this->source_
)
409 unsigned int shndx
= this->u_
.from_object
.shndx
;
410 if (shndx
!= elfcpp::SHN_UNDEF
&& this->is_ordinary_shndx_
)
412 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
413 gold_assert(this->u_
.from_object
.object
->pluginobj() == NULL
);
414 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
415 return relobj
->output_section(shndx
);
421 return this->u_
.in_output_data
.output_data
->output_section();
423 case IN_OUTPUT_SEGMENT
:
433 // Set the symbol's output section. This is used for symbols defined
434 // in scripts. This should only be called after the symbol table has
438 Symbol::set_output_section(Output_section
* os
)
440 switch (this->source_
)
444 gold_assert(this->output_section() == os
);
447 this->source_
= IN_OUTPUT_DATA
;
448 this->u_
.in_output_data
.output_data
= os
;
449 this->u_
.in_output_data
.offset_is_from_end
= false;
451 case IN_OUTPUT_SEGMENT
:
458 // Class Symbol_table.
460 Symbol_table::Symbol_table(unsigned int count
,
461 const Version_script_info
& version_script
)
462 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
463 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
464 version_script_(version_script
)
466 namepool_
.reserve(count
);
469 Symbol_table::~Symbol_table()
473 // The hash function. The key values are Stringpool keys.
476 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
478 return key
.first
^ key
.second
;
481 // The symbol table key equality function. This is called with
485 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
486 const Symbol_table_key
& k2
) const
488 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
491 // Make TO a symbol which forwards to FROM.
494 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
496 gold_assert(from
!= to
);
497 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
498 this->forwarders_
[from
] = to
;
499 from
->set_forwarder();
502 // Resolve the forwards from FROM, returning the real symbol.
505 Symbol_table::resolve_forwards(const Symbol
* from
) const
507 gold_assert(from
->is_forwarder());
508 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
509 this->forwarders_
.find(from
);
510 gold_assert(p
!= this->forwarders_
.end());
514 // Look up a symbol by name.
517 Symbol_table::lookup(const char* name
, const char* version
) const
519 Stringpool::Key name_key
;
520 name
= this->namepool_
.find(name
, &name_key
);
524 Stringpool::Key version_key
= 0;
527 version
= this->namepool_
.find(version
, &version_key
);
532 Symbol_table_key
key(name_key
, version_key
);
533 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
534 if (p
== this->table_
.end())
539 // Resolve a Symbol with another Symbol. This is only used in the
540 // unusual case where there are references to both an unversioned
541 // symbol and a symbol with a version, and we then discover that that
542 // version is the default version. Because this is unusual, we do
543 // this the slow way, by converting back to an ELF symbol.
545 template<int size
, bool big_endian
>
547 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
)
549 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
550 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
551 // We don't bother to set the st_name or the st_shndx field.
552 esym
.put_st_value(from
->value());
553 esym
.put_st_size(from
->symsize());
554 esym
.put_st_info(from
->binding(), from
->type());
555 esym
.put_st_other(from
->visibility(), from
->nonvis());
557 unsigned int shndx
= from
->shndx(&is_ordinary
);
558 this->resolve(to
, esym
.sym(), shndx
, is_ordinary
, shndx
, from
->object(),
566 // Record that a symbol is forced to be local by a version script.
569 Symbol_table::force_local(Symbol
* sym
)
571 if (!sym
->is_defined() && !sym
->is_common())
573 if (sym
->is_forced_local())
575 // We already got this one.
578 sym
->set_is_forced_local();
579 this->forced_locals_
.push_back(sym
);
582 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
583 // is only called for undefined symbols, when at least one --wrap
587 Symbol_table::wrap_symbol(Object
* object
, const char* name
,
588 Stringpool::Key
* name_key
)
590 // For some targets, we need to ignore a specific character when
591 // wrapping, and add it back later.
593 if (name
[0] == object
->target()->wrap_char())
599 if (parameters
->options().is_wrap(name
))
601 // Turn NAME into __wrap_NAME.
608 // This will give us both the old and new name in NAMEPOOL_, but
609 // that is OK. Only the versions we need will wind up in the
610 // real string table in the output file.
611 return this->namepool_
.add(s
.c_str(), true, name_key
);
614 const char* const real_prefix
= "__real_";
615 const size_t real_prefix_length
= strlen(real_prefix
);
616 if (strncmp(name
, real_prefix
, real_prefix_length
) == 0
617 && parameters
->options().is_wrap(name
+ real_prefix_length
))
619 // Turn __real_NAME into NAME.
623 s
+= name
+ real_prefix_length
;
624 return this->namepool_
.add(s
.c_str(), true, name_key
);
630 // Add one symbol from OBJECT to the symbol table. NAME is symbol
631 // name and VERSION is the version; both are canonicalized. DEF is
632 // whether this is the default version. ST_SHNDX is the symbol's
633 // section index; IS_ORDINARY is whether this is a normal section
634 // rather than a special code.
636 // If DEF is true, then this is the definition of a default version of
637 // a symbol. That means that any lookup of NAME/NULL and any lookup
638 // of NAME/VERSION should always return the same symbol. This is
639 // obvious for references, but in particular we want to do this for
640 // definitions: overriding NAME/NULL should also override
641 // NAME/VERSION. If we don't do that, it would be very hard to
642 // override functions in a shared library which uses versioning.
644 // We implement this by simply making both entries in the hash table
645 // point to the same Symbol structure. That is easy enough if this is
646 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
647 // that we have seen both already, in which case they will both have
648 // independent entries in the symbol table. We can't simply change
649 // the symbol table entry, because we have pointers to the entries
650 // attached to the object files. So we mark the entry attached to the
651 // object file as a forwarder, and record it in the forwarders_ map.
652 // Note that entries in the hash table will never be marked as
655 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
656 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
657 // for a special section code. ST_SHNDX may be modified if the symbol
658 // is defined in a section being discarded.
660 template<int size
, bool big_endian
>
662 Symbol_table::add_from_object(Object
* object
,
664 Stringpool::Key name_key
,
666 Stringpool::Key version_key
,
668 const elfcpp::Sym
<size
, big_endian
>& sym
,
669 unsigned int st_shndx
,
671 unsigned int orig_st_shndx
)
673 // Print a message if this symbol is being traced.
674 if (parameters
->options().is_trace_symbol(name
))
676 if (orig_st_shndx
== elfcpp::SHN_UNDEF
)
677 gold_info(_("%s: reference to %s"), object
->name().c_str(), name
);
679 gold_info(_("%s: definition of %s"), object
->name().c_str(), name
);
682 // For an undefined symbol, we may need to adjust the name using
684 if (orig_st_shndx
== elfcpp::SHN_UNDEF
685 && parameters
->options().any_wrap())
687 const char* wrap_name
= this->wrap_symbol(object
, name
, &name_key
);
688 if (wrap_name
!= name
)
690 // If we see a reference to malloc with version GLIBC_2.0,
691 // and we turn it into a reference to __wrap_malloc, then we
692 // discard the version number. Otherwise the user would be
693 // required to specify the correct version for
701 Symbol
* const snull
= NULL
;
702 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
703 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
706 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
707 std::make_pair(this->table_
.end(), false);
710 const Stringpool::Key vnull_key
= 0;
711 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
716 // ins.first: an iterator, which is a pointer to a pair.
717 // ins.first->first: the key (a pair of name and version).
718 // ins.first->second: the value (Symbol*).
719 // ins.second: true if new entry was inserted, false if not.
721 Sized_symbol
<size
>* ret
;
726 // We already have an entry for NAME/VERSION.
727 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
728 gold_assert(ret
!= NULL
);
730 was_undefined
= ret
->is_undefined();
731 was_common
= ret
->is_common();
733 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
740 // This is the first time we have seen NAME/NULL. Make
741 // NAME/NULL point to NAME/VERSION.
742 insdef
.first
->second
= ret
;
744 else if (insdef
.first
->second
!= ret
)
746 // This is the unfortunate case where we already have
747 // entries for both NAME/VERSION and NAME/NULL. We now
748 // see a symbol NAME/VERSION where VERSION is the
749 // default version. We have already resolved this new
750 // symbol with the existing NAME/VERSION symbol.
752 // It's possible that NAME/NULL and NAME/VERSION are
753 // both defined in regular objects. This can only
754 // happen if one object file defines foo and another
755 // defines foo@@ver. This is somewhat obscure, but we
756 // call it a multiple definition error.
758 // It's possible that NAME/NULL actually has a version,
759 // in which case it won't be the same as VERSION. This
760 // happens with ver_test_7.so in the testsuite for the
761 // symbol t2_2. We see t2_2@@VER2, so we define both
762 // t2_2/VER2 and t2_2/NULL. We then see an unadorned
763 // t2_2 in an object file and give it version VER1 from
764 // the version script. This looks like a default
765 // definition for VER1, so it looks like we should merge
766 // t2_2/NULL with t2_2/VER1. That doesn't make sense,
767 // but it's not obvious that this is an error, either.
770 // If one of the symbols has non-default visibility, and
771 // the other is defined in a shared object, then they
772 // are different symbols.
774 // Otherwise, we just resolve the symbols as though they
777 if (insdef
.first
->second
->version() != NULL
)
779 gold_assert(insdef
.first
->second
->version() != version
);
782 else if (ret
->visibility() != elfcpp::STV_DEFAULT
783 && insdef
.first
->second
->is_from_dynobj())
785 else if (insdef
.first
->second
->visibility() != elfcpp::STV_DEFAULT
786 && ret
->is_from_dynobj())
790 const Sized_symbol
<size
>* sym2
;
791 sym2
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
792 Symbol_table::resolve
<size
, big_endian
>(ret
, sym2
);
793 this->make_forwarder(insdef
.first
->second
, ret
);
794 insdef
.first
->second
= ret
;
803 // This is the first time we have seen NAME/VERSION.
804 gold_assert(ins
.first
->second
== NULL
);
806 if (def
&& !insdef
.second
)
808 // We already have an entry for NAME/NULL. If we override
809 // it, then change it to NAME/VERSION.
810 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
812 was_undefined
= ret
->is_undefined();
813 was_common
= ret
->is_common();
815 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
817 ins
.first
->second
= ret
;
821 was_undefined
= false;
824 Sized_target
<size
, big_endian
>* target
=
825 object
->sized_target
<size
, big_endian
>();
826 if (!target
->has_make_symbol())
827 ret
= new Sized_symbol
<size
>();
830 ret
= target
->make_symbol();
833 // This means that we don't want a symbol table
836 this->table_
.erase(ins
.first
);
839 this->table_
.erase(insdef
.first
);
840 // Inserting insdef invalidated ins.
841 this->table_
.erase(std::make_pair(name_key
,
848 ret
->init_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
850 ins
.first
->second
= ret
;
853 // This is the first time we have seen NAME/NULL. Point
854 // it at the new entry for NAME/VERSION.
855 gold_assert(insdef
.second
);
856 insdef
.first
->second
= ret
;
861 // Record every time we see a new undefined symbol, to speed up
863 if (!was_undefined
&& ret
->is_undefined())
864 ++this->saw_undefined_
;
866 // Keep track of common symbols, to speed up common symbol
868 if (!was_common
&& ret
->is_common())
870 if (ret
->type() != elfcpp::STT_TLS
)
871 this->commons_
.push_back(ret
);
873 this->tls_commons_
.push_back(ret
);
877 ret
->set_is_default();
881 // Add all the symbols in a relocatable object to the hash table.
883 template<int size
, bool big_endian
>
885 Symbol_table::add_from_relobj(
886 Sized_relobj
<size
, big_endian
>* relobj
,
887 const unsigned char* syms
,
889 size_t symndx_offset
,
890 const char* sym_names
,
891 size_t sym_name_size
,
892 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
897 gold_assert(size
== relobj
->target()->get_size());
898 gold_assert(size
== parameters
->target().get_size());
900 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
902 const bool just_symbols
= relobj
->just_symbols();
904 const unsigned char* p
= syms
;
905 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
907 (*sympointers
)[i
] = NULL
;
909 elfcpp::Sym
<size
, big_endian
> sym(p
);
911 unsigned int st_name
= sym
.get_st_name();
912 if (st_name
>= sym_name_size
)
914 relobj
->error(_("bad global symbol name offset %u at %zu"),
919 const char* name
= sym_names
+ st_name
;
922 unsigned int st_shndx
= relobj
->adjust_sym_shndx(i
+ symndx_offset
,
925 unsigned int orig_st_shndx
= st_shndx
;
927 orig_st_shndx
= elfcpp::SHN_UNDEF
;
929 if (st_shndx
!= elfcpp::SHN_UNDEF
)
932 // A symbol defined in a section which we are not including must
933 // be treated as an undefined symbol.
934 if (st_shndx
!= elfcpp::SHN_UNDEF
936 && !relobj
->is_section_included(st_shndx
))
937 st_shndx
= elfcpp::SHN_UNDEF
;
939 // In an object file, an '@' in the name separates the symbol
940 // name from the version name. If there are two '@' characters,
941 // this is the default version.
942 const char* ver
= strchr(name
, '@');
943 Stringpool::Key ver_key
= 0;
945 // DEF: is the version default? LOCAL: is the symbol forced local?
951 // The symbol name is of the form foo@VERSION or foo@@VERSION
952 namelen
= ver
- name
;
959 ver
= this->namepool_
.add(ver
, true, &ver_key
);
961 // We don't want to assign a version to an undefined symbol,
962 // even if it is listed in the version script. FIXME: What
963 // about a common symbol?
966 namelen
= strlen(name
);
967 if (!this->version_script_
.empty()
968 && st_shndx
!= elfcpp::SHN_UNDEF
)
970 // The symbol name did not have a version, but the
971 // version script may assign a version anyway.
973 if (this->version_script_
.get_symbol_version(name
, &version
))
975 // The version can be empty if the version script is
976 // only used to force some symbols to be local.
977 if (!version
.empty())
979 ver
= this->namepool_
.add_with_length(version
.c_str(),
986 else if (this->version_script_
.symbol_is_local(name
))
991 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
992 unsigned char symbuf
[sym_size
];
993 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
996 memcpy(symbuf
, p
, sym_size
);
997 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
998 if (orig_st_shndx
!= elfcpp::SHN_UNDEF
&& is_ordinary
)
1000 // Symbol values in object files are section relative.
1001 // This is normally what we want, but since here we are
1002 // converting the symbol to absolute we need to add the
1003 // section address. The section address in an object
1004 // file is normally zero, but people can use a linker
1005 // script to change it.
1006 sw
.put_st_value(sym
.get_st_value()
1007 + relobj
->section_address(orig_st_shndx
));
1009 st_shndx
= elfcpp::SHN_ABS
;
1010 is_ordinary
= false;
1014 Stringpool::Key name_key
;
1015 name
= this->namepool_
.add_with_length(name
, namelen
, true,
1018 Sized_symbol
<size
>* res
;
1019 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
1020 def
, *psym
, st_shndx
, is_ordinary
,
1024 this->force_local(res
);
1026 (*sympointers
)[i
] = res
;
1030 // Add a symbol from a plugin-claimed file.
1032 template<int size
, bool big_endian
>
1034 Symbol_table::add_from_pluginobj(
1035 Sized_pluginobj
<size
, big_endian
>* obj
,
1038 elfcpp::Sym
<size
, big_endian
>* sym
)
1040 unsigned int st_shndx
= sym
->get_st_shndx();
1042 Stringpool::Key ver_key
= 0;
1048 ver
= this->namepool_
.add(ver
, true, &ver_key
);
1050 // We don't want to assign a version to an undefined symbol,
1051 // even if it is listed in the version script. FIXME: What
1052 // about a common symbol?
1055 if (!this->version_script_
.empty()
1056 && st_shndx
!= elfcpp::SHN_UNDEF
)
1058 // The symbol name did not have a version, but the
1059 // version script may assign a version anyway.
1060 std::string version
;
1061 if (this->version_script_
.get_symbol_version(name
, &version
))
1063 // The version can be empty if the version script is
1064 // only used to force some symbols to be local.
1065 if (!version
.empty())
1067 ver
= this->namepool_
.add_with_length(version
.c_str(),
1074 else if (this->version_script_
.symbol_is_local(name
))
1079 Stringpool::Key name_key
;
1080 name
= this->namepool_
.add(name
, true, &name_key
);
1082 Sized_symbol
<size
>* res
;
1083 res
= this->add_from_object(obj
, name
, name_key
, ver
, ver_key
,
1084 def
, *sym
, st_shndx
, true, st_shndx
);
1087 this->force_local(res
);
1092 // Add all the symbols in a dynamic object to the hash table.
1094 template<int size
, bool big_endian
>
1096 Symbol_table::add_from_dynobj(
1097 Sized_dynobj
<size
, big_endian
>* dynobj
,
1098 const unsigned char* syms
,
1100 const char* sym_names
,
1101 size_t sym_name_size
,
1102 const unsigned char* versym
,
1104 const std::vector
<const char*>* version_map
,
1105 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
1110 gold_assert(size
== dynobj
->target()->get_size());
1111 gold_assert(size
== parameters
->target().get_size());
1113 if (dynobj
->just_symbols())
1115 gold_error(_("--just-symbols does not make sense with a shared object"));
1119 if (versym
!= NULL
&& versym_size
/ 2 < count
)
1121 dynobj
->error(_("too few symbol versions"));
1125 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1127 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1128 // weak aliases. This is necessary because if the dynamic object
1129 // provides the same variable under two names, one of which is a
1130 // weak definition, and the regular object refers to the weak
1131 // definition, we have to put both the weak definition and the
1132 // strong definition into the dynamic symbol table. Given a weak
1133 // definition, the only way that we can find the corresponding
1134 // strong definition, if any, is to search the symbol table.
1135 std::vector
<Sized_symbol
<size
>*> object_symbols
;
1137 const unsigned char* p
= syms
;
1138 const unsigned char* vs
= versym
;
1139 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
1141 elfcpp::Sym
<size
, big_endian
> sym(p
);
1143 if (sympointers
!= NULL
)
1144 (*sympointers
)[i
] = NULL
;
1146 // Ignore symbols with local binding or that have
1147 // internal or hidden visibility.
1148 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
1149 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
1150 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
1153 // A protected symbol in a shared library must be treated as a
1154 // normal symbol when viewed from outside the shared library.
1155 // Implement this by overriding the visibility here.
1156 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1157 unsigned char symbuf
[sym_size
];
1158 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1159 if (sym
.get_st_visibility() == elfcpp::STV_PROTECTED
)
1161 memcpy(symbuf
, p
, sym_size
);
1162 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1163 sw
.put_st_other(elfcpp::STV_DEFAULT
, sym
.get_st_nonvis());
1167 unsigned int st_name
= psym
->get_st_name();
1168 if (st_name
>= sym_name_size
)
1170 dynobj
->error(_("bad symbol name offset %u at %zu"),
1175 const char* name
= sym_names
+ st_name
;
1178 unsigned int st_shndx
= dynobj
->adjust_sym_shndx(i
, psym
->get_st_shndx(),
1181 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1184 Sized_symbol
<size
>* res
;
1188 Stringpool::Key name_key
;
1189 name
= this->namepool_
.add(name
, true, &name_key
);
1190 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1191 false, *psym
, st_shndx
, is_ordinary
,
1196 // Read the version information.
1198 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
1200 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
1201 v
&= elfcpp::VERSYM_VERSION
;
1203 // The Sun documentation says that V can be VER_NDX_LOCAL,
1204 // or VER_NDX_GLOBAL, or a version index. The meaning of
1205 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1206 // The old GNU linker will happily generate VER_NDX_LOCAL
1207 // for an undefined symbol. I don't know what the Sun
1208 // linker will generate.
1210 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1211 && st_shndx
!= elfcpp::SHN_UNDEF
)
1213 // This symbol should not be visible outside the object.
1217 // At this point we are definitely going to add this symbol.
1218 Stringpool::Key name_key
;
1219 name
= this->namepool_
.add(name
, true, &name_key
);
1221 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1222 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
1224 // This symbol does not have a version.
1225 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1226 false, *psym
, st_shndx
, is_ordinary
,
1231 if (v
>= version_map
->size())
1233 dynobj
->error(_("versym for symbol %zu out of range: %u"),
1238 const char* version
= (*version_map
)[v
];
1239 if (version
== NULL
)
1241 dynobj
->error(_("versym for symbol %zu has no name: %u"),
1246 Stringpool::Key version_key
;
1247 version
= this->namepool_
.add(version
, true, &version_key
);
1249 // If this is an absolute symbol, and the version name
1250 // and symbol name are the same, then this is the
1251 // version definition symbol. These symbols exist to
1252 // support using -u to pull in particular versions. We
1253 // do not want to record a version for them.
1254 if (st_shndx
== elfcpp::SHN_ABS
1256 && name_key
== version_key
)
1257 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1258 false, *psym
, st_shndx
, is_ordinary
,
1262 const bool def
= (!hidden
1263 && st_shndx
!= elfcpp::SHN_UNDEF
);
1264 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
1265 version_key
, def
, *psym
, st_shndx
,
1266 is_ordinary
, st_shndx
);
1271 // Note that it is possible that RES was overridden by an
1272 // earlier object, in which case it can't be aliased here.
1273 if (st_shndx
!= elfcpp::SHN_UNDEF
1275 && psym
->get_st_type() == elfcpp::STT_OBJECT
1276 && res
->source() == Symbol::FROM_OBJECT
1277 && res
->object() == dynobj
)
1278 object_symbols
.push_back(res
);
1280 if (sympointers
!= NULL
)
1281 (*sympointers
)[i
] = res
;
1284 this->record_weak_aliases(&object_symbols
);
1287 // This is used to sort weak aliases. We sort them first by section
1288 // index, then by offset, then by weak ahead of strong.
1291 class Weak_alias_sorter
1294 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
1299 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
1300 const Sized_symbol
<size
>* s2
) const
1303 unsigned int s1_shndx
= s1
->shndx(&is_ordinary
);
1304 gold_assert(is_ordinary
);
1305 unsigned int s2_shndx
= s2
->shndx(&is_ordinary
);
1306 gold_assert(is_ordinary
);
1307 if (s1_shndx
!= s2_shndx
)
1308 return s1_shndx
< s2_shndx
;
1310 if (s1
->value() != s2
->value())
1311 return s1
->value() < s2
->value();
1312 if (s1
->binding() != s2
->binding())
1314 if (s1
->binding() == elfcpp::STB_WEAK
)
1316 if (s2
->binding() == elfcpp::STB_WEAK
)
1319 return std::string(s1
->name()) < std::string(s2
->name());
1322 // SYMBOLS is a list of object symbols from a dynamic object. Look
1323 // for any weak aliases, and record them so that if we add the weak
1324 // alias to the dynamic symbol table, we also add the corresponding
1329 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
1331 // Sort the vector by section index, then by offset, then by weak
1333 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
1335 // Walk through the vector. For each weak definition, record
1337 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
1339 p
!= symbols
->end();
1342 if ((*p
)->binding() != elfcpp::STB_WEAK
)
1345 // Build a circular list of weak aliases. Each symbol points to
1346 // the next one in the circular list.
1348 Sized_symbol
<size
>* from_sym
= *p
;
1349 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
1350 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
1353 if ((*q
)->shndx(&dummy
) != from_sym
->shndx(&dummy
)
1354 || (*q
)->value() != from_sym
->value())
1357 this->weak_aliases_
[from_sym
] = *q
;
1358 from_sym
->set_has_alias();
1364 this->weak_aliases_
[from_sym
] = *p
;
1365 from_sym
->set_has_alias();
1372 // Create and return a specially defined symbol. If ONLY_IF_REF is
1373 // true, then only create the symbol if there is a reference to it.
1374 // If this does not return NULL, it sets *POLDSYM to the existing
1375 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1377 template<int size
, bool big_endian
>
1379 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1381 Sized_symbol
<size
>** poldsym
)
1384 Sized_symbol
<size
>* sym
;
1385 bool add_to_table
= false;
1386 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1388 // If the caller didn't give us a version, see if we get one from
1389 // the version script.
1391 if (*pversion
== NULL
)
1393 if (this->version_script_
.get_symbol_version(*pname
, &v
))
1396 *pversion
= v
.c_str();
1402 oldsym
= this->lookup(*pname
, *pversion
);
1403 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1406 *pname
= oldsym
->name();
1407 *pversion
= oldsym
->version();
1411 // Canonicalize NAME and VERSION.
1412 Stringpool::Key name_key
;
1413 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1415 Stringpool::Key version_key
= 0;
1416 if (*pversion
!= NULL
)
1417 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1419 Symbol
* const snull
= NULL
;
1420 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1421 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1427 // We already have a symbol table entry for NAME/VERSION.
1428 oldsym
= ins
.first
->second
;
1429 gold_assert(oldsym
!= NULL
);
1433 // We haven't seen this symbol before.
1434 gold_assert(ins
.first
->second
== NULL
);
1435 add_to_table
= true;
1436 add_loc
= ins
.first
;
1441 const Target
& target
= parameters
->target();
1442 if (!target
.has_make_symbol())
1443 sym
= new Sized_symbol
<size
>();
1446 gold_assert(target
.get_size() == size
);
1447 gold_assert(target
.is_big_endian() ? big_endian
: !big_endian
);
1448 typedef Sized_target
<size
, big_endian
> My_target
;
1449 const My_target
* sized_target
=
1450 static_cast<const My_target
*>(&target
);
1451 sym
= sized_target
->make_symbol();
1457 add_loc
->second
= sym
;
1459 gold_assert(oldsym
!= NULL
);
1461 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1466 // Define a symbol based on an Output_data.
1469 Symbol_table::define_in_output_data(const char* name
,
1470 const char* version
,
1475 elfcpp::STB binding
,
1476 elfcpp::STV visibility
,
1477 unsigned char nonvis
,
1478 bool offset_is_from_end
,
1481 if (parameters
->target().get_size() == 32)
1483 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1484 return this->do_define_in_output_data
<32>(name
, version
, od
,
1485 value
, symsize
, type
, binding
,
1493 else if (parameters
->target().get_size() == 64)
1495 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1496 return this->do_define_in_output_data
<64>(name
, version
, od
,
1497 value
, symsize
, type
, binding
,
1509 // Define a symbol in an Output_data, sized version.
1513 Symbol_table::do_define_in_output_data(
1515 const char* version
,
1517 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1518 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1520 elfcpp::STB binding
,
1521 elfcpp::STV visibility
,
1522 unsigned char nonvis
,
1523 bool offset_is_from_end
,
1526 Sized_symbol
<size
>* sym
;
1527 Sized_symbol
<size
>* oldsym
;
1529 if (parameters
->target().is_big_endian())
1531 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1532 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1533 only_if_ref
, &oldsym
);
1540 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1541 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1542 only_if_ref
, &oldsym
);
1551 sym
->init_output_data(name
, version
, od
, value
, symsize
, type
, binding
,
1552 visibility
, nonvis
, offset_is_from_end
);
1556 if (binding
== elfcpp::STB_LOCAL
1557 || this->version_script_
.symbol_is_local(name
))
1558 this->force_local(sym
);
1559 else if (version
!= NULL
)
1560 sym
->set_is_default();
1564 if (Symbol_table::should_override_with_special(oldsym
))
1565 this->override_with_special(oldsym
, sym
);
1570 // Define a symbol based on an Output_segment.
1573 Symbol_table::define_in_output_segment(const char* name
,
1574 const char* version
, Output_segment
* os
,
1578 elfcpp::STB binding
,
1579 elfcpp::STV visibility
,
1580 unsigned char nonvis
,
1581 Symbol::Segment_offset_base offset_base
,
1584 if (parameters
->target().get_size() == 32)
1586 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1587 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1588 value
, symsize
, type
,
1589 binding
, visibility
, nonvis
,
1590 offset_base
, only_if_ref
);
1595 else if (parameters
->target().get_size() == 64)
1597 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1598 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1599 value
, symsize
, type
,
1600 binding
, visibility
, nonvis
,
1601 offset_base
, only_if_ref
);
1610 // Define a symbol in an Output_segment, sized version.
1614 Symbol_table::do_define_in_output_segment(
1616 const char* version
,
1618 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1619 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1621 elfcpp::STB binding
,
1622 elfcpp::STV visibility
,
1623 unsigned char nonvis
,
1624 Symbol::Segment_offset_base offset_base
,
1627 Sized_symbol
<size
>* sym
;
1628 Sized_symbol
<size
>* oldsym
;
1630 if (parameters
->target().is_big_endian())
1632 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1633 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1634 only_if_ref
, &oldsym
);
1641 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1642 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1643 only_if_ref
, &oldsym
);
1652 sym
->init_output_segment(name
, version
, os
, value
, symsize
, type
, binding
,
1653 visibility
, nonvis
, offset_base
);
1657 if (binding
== elfcpp::STB_LOCAL
1658 || this->version_script_
.symbol_is_local(name
))
1659 this->force_local(sym
);
1660 else if (version
!= NULL
)
1661 sym
->set_is_default();
1665 if (Symbol_table::should_override_with_special(oldsym
))
1666 this->override_with_special(oldsym
, sym
);
1671 // Define a special symbol with a constant value. It is a multiple
1672 // definition error if this symbol is already defined.
1675 Symbol_table::define_as_constant(const char* name
,
1676 const char* version
,
1680 elfcpp::STB binding
,
1681 elfcpp::STV visibility
,
1682 unsigned char nonvis
,
1684 bool force_override
)
1686 if (parameters
->target().get_size() == 32)
1688 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1689 return this->do_define_as_constant
<32>(name
, version
, value
,
1690 symsize
, type
, binding
,
1691 visibility
, nonvis
, only_if_ref
,
1697 else if (parameters
->target().get_size() == 64)
1699 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1700 return this->do_define_as_constant
<64>(name
, version
, value
,
1701 symsize
, type
, binding
,
1702 visibility
, nonvis
, only_if_ref
,
1712 // Define a symbol as a constant, sized version.
1716 Symbol_table::do_define_as_constant(
1718 const char* version
,
1719 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1720 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1722 elfcpp::STB binding
,
1723 elfcpp::STV visibility
,
1724 unsigned char nonvis
,
1726 bool force_override
)
1728 Sized_symbol
<size
>* sym
;
1729 Sized_symbol
<size
>* oldsym
;
1731 if (parameters
->target().is_big_endian())
1733 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1734 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1735 only_if_ref
, &oldsym
);
1742 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1743 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1744 only_if_ref
, &oldsym
);
1753 sym
->init_constant(name
, version
, value
, symsize
, type
, binding
, visibility
,
1758 // Version symbols are absolute symbols with name == version.
1759 // We don't want to force them to be local.
1760 if ((version
== NULL
1763 && (binding
== elfcpp::STB_LOCAL
1764 || this->version_script_
.symbol_is_local(name
)))
1765 this->force_local(sym
);
1766 else if (version
!= NULL
1767 && (name
!= version
|| value
!= 0))
1768 sym
->set_is_default();
1772 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
1773 this->override_with_special(oldsym
, sym
);
1778 // Define a set of symbols in output sections.
1781 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1782 const Define_symbol_in_section
* p
,
1785 for (int i
= 0; i
< count
; ++i
, ++p
)
1787 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1789 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
1790 p
->size
, p
->type
, p
->binding
,
1791 p
->visibility
, p
->nonvis
,
1792 p
->offset_is_from_end
,
1793 only_if_ref
|| p
->only_if_ref
);
1795 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1796 p
->binding
, p
->visibility
, p
->nonvis
,
1797 only_if_ref
|| p
->only_if_ref
,
1802 // Define a set of symbols in output segments.
1805 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1806 const Define_symbol_in_segment
* p
,
1809 for (int i
= 0; i
< count
; ++i
, ++p
)
1811 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1812 p
->segment_flags_set
,
1813 p
->segment_flags_clear
);
1815 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
1816 p
->size
, p
->type
, p
->binding
,
1817 p
->visibility
, p
->nonvis
,
1819 only_if_ref
|| p
->only_if_ref
);
1821 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1822 p
->binding
, p
->visibility
, p
->nonvis
,
1823 only_if_ref
|| p
->only_if_ref
,
1828 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1829 // symbol should be defined--typically a .dyn.bss section. VALUE is
1830 // the offset within POSD.
1834 Symbol_table::define_with_copy_reloc(
1835 Sized_symbol
<size
>* csym
,
1837 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
1839 gold_assert(csym
->is_from_dynobj());
1840 gold_assert(!csym
->is_copied_from_dynobj());
1841 Object
* object
= csym
->object();
1842 gold_assert(object
->is_dynamic());
1843 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1845 // Our copied variable has to override any variable in a shared
1847 elfcpp::STB binding
= csym
->binding();
1848 if (binding
== elfcpp::STB_WEAK
)
1849 binding
= elfcpp::STB_GLOBAL
;
1851 this->define_in_output_data(csym
->name(), csym
->version(),
1852 posd
, value
, csym
->symsize(),
1853 csym
->type(), binding
,
1854 csym
->visibility(), csym
->nonvis(),
1857 csym
->set_is_copied_from_dynobj();
1858 csym
->set_needs_dynsym_entry();
1860 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1862 // We have now defined all aliases, but we have not entered them all
1863 // in the copied_symbol_dynobjs_ map.
1864 if (csym
->has_alias())
1869 sym
= this->weak_aliases_
[sym
];
1872 gold_assert(sym
->output_data() == posd
);
1874 sym
->set_is_copied_from_dynobj();
1875 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1880 // SYM is defined using a COPY reloc. Return the dynamic object where
1881 // the original definition was found.
1884 Symbol_table::get_copy_source(const Symbol
* sym
) const
1886 gold_assert(sym
->is_copied_from_dynobj());
1887 Copied_symbol_dynobjs::const_iterator p
=
1888 this->copied_symbol_dynobjs_
.find(sym
);
1889 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1893 // Add any undefined symbols named on the command line.
1896 Symbol_table::add_undefined_symbols_from_command_line()
1898 if (parameters
->options().any_undefined())
1900 if (parameters
->target().get_size() == 32)
1902 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1903 this->do_add_undefined_symbols_from_command_line
<32>();
1908 else if (parameters
->target().get_size() == 64)
1910 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1911 this->do_add_undefined_symbols_from_command_line
<64>();
1923 Symbol_table::do_add_undefined_symbols_from_command_line()
1925 for (options::String_set::const_iterator p
=
1926 parameters
->options().undefined_begin();
1927 p
!= parameters
->options().undefined_end();
1930 const char* name
= p
->c_str();
1932 if (this->lookup(name
) != NULL
)
1935 const char* version
= NULL
;
1937 Sized_symbol
<size
>* sym
;
1938 Sized_symbol
<size
>* oldsym
;
1939 if (parameters
->target().is_big_endian())
1941 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1942 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1950 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1951 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1958 gold_assert(oldsym
== NULL
);
1960 sym
->init_undefined(name
, version
, elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1961 elfcpp::STV_DEFAULT
, 0);
1962 ++this->saw_undefined_
;
1966 // Set the dynamic symbol indexes. INDEX is the index of the first
1967 // global dynamic symbol. Pointers to the symbols are stored into the
1968 // vector SYMS. The names are added to DYNPOOL. This returns an
1969 // updated dynamic symbol index.
1972 Symbol_table::set_dynsym_indexes(unsigned int index
,
1973 std::vector
<Symbol
*>* syms
,
1974 Stringpool
* dynpool
,
1977 for (Symbol_table_type::iterator p
= this->table_
.begin();
1978 p
!= this->table_
.end();
1981 Symbol
* sym
= p
->second
;
1983 // Note that SYM may already have a dynamic symbol index, since
1984 // some symbols appear more than once in the symbol table, with
1985 // and without a version.
1987 if (!sym
->should_add_dynsym_entry())
1988 sym
->set_dynsym_index(-1U);
1989 else if (!sym
->has_dynsym_index())
1991 sym
->set_dynsym_index(index
);
1993 syms
->push_back(sym
);
1994 dynpool
->add(sym
->name(), false, NULL
);
1996 // Record any version information.
1997 if (sym
->version() != NULL
)
1998 versions
->record_version(this, dynpool
, sym
);
2002 // Finish up the versions. In some cases this may add new dynamic
2004 index
= versions
->finalize(this, index
, syms
);
2009 // Set the final values for all the symbols. The index of the first
2010 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2011 // file offset OFF. Add their names to POOL. Return the new file
2012 // offset. Update *PLOCAL_SYMCOUNT if necessary.
2015 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
2016 size_t dyncount
, Stringpool
* pool
,
2017 unsigned int *plocal_symcount
)
2021 gold_assert(*plocal_symcount
!= 0);
2022 this->first_global_index_
= *plocal_symcount
;
2024 this->dynamic_offset_
= dynoff
;
2025 this->first_dynamic_global_index_
= dyn_global_index
;
2026 this->dynamic_count_
= dyncount
;
2028 if (parameters
->target().get_size() == 32)
2030 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2031 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
2036 else if (parameters
->target().get_size() == 64)
2038 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2039 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
2047 // Now that we have the final symbol table, we can reliably note
2048 // which symbols should get warnings.
2049 this->warnings_
.note_warnings(this);
2054 // SYM is going into the symbol table at *PINDEX. Add the name to
2055 // POOL, update *PINDEX and *POFF.
2059 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
2060 unsigned int* pindex
, off_t
* poff
)
2062 sym
->set_symtab_index(*pindex
);
2063 pool
->add(sym
->name(), false, NULL
);
2065 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
2068 // Set the final value for all the symbols. This is called after
2069 // Layout::finalize, so all the output sections have their final
2074 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
2075 unsigned int* plocal_symcount
)
2077 off
= align_address(off
, size
>> 3);
2078 this->offset_
= off
;
2080 unsigned int index
= *plocal_symcount
;
2081 const unsigned int orig_index
= index
;
2083 // First do all the symbols which have been forced to be local, as
2084 // they must appear before all global symbols.
2085 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
2086 p
!= this->forced_locals_
.end();
2090 gold_assert(sym
->is_forced_local());
2091 if (this->sized_finalize_symbol
<size
>(sym
))
2093 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2098 // Now do all the remaining symbols.
2099 for (Symbol_table_type::iterator p
= this->table_
.begin();
2100 p
!= this->table_
.end();
2103 Symbol
* sym
= p
->second
;
2104 if (this->sized_finalize_symbol
<size
>(sym
))
2105 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2108 this->output_count_
= index
- orig_index
;
2113 // Finalize the symbol SYM. This returns true if the symbol should be
2114 // added to the symbol table, false otherwise.
2118 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
2120 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2122 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
2124 // The default version of a symbol may appear twice in the symbol
2125 // table. We only need to finalize it once.
2126 if (sym
->has_symtab_index())
2131 gold_assert(!sym
->has_symtab_index());
2132 sym
->set_symtab_index(-1U);
2133 gold_assert(sym
->dynsym_index() == -1U);
2139 switch (sym
->source())
2141 case Symbol::FROM_OBJECT
:
2144 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2146 // FIXME: We need some target specific support here.
2148 && shndx
!= elfcpp::SHN_ABS
2149 && shndx
!= elfcpp::SHN_COMMON
)
2151 gold_error(_("%s: unsupported symbol section 0x%x"),
2152 sym
->demangled_name().c_str(), shndx
);
2153 shndx
= elfcpp::SHN_UNDEF
;
2156 Object
* symobj
= sym
->object();
2157 if (symobj
->is_dynamic())
2160 shndx
= elfcpp::SHN_UNDEF
;
2162 else if (symobj
->pluginobj() != NULL
)
2165 shndx
= elfcpp::SHN_UNDEF
;
2167 else if (shndx
== elfcpp::SHN_UNDEF
)
2169 else if (!is_ordinary
2170 && (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
))
2171 value
= sym
->value();
2174 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2175 Output_section
* os
= relobj
->output_section(shndx
);
2179 sym
->set_symtab_index(-1U);
2180 gold_assert(sym
->dynsym_index() == -1U);
2184 uint64_t secoff64
= relobj
->output_section_offset(shndx
);
2185 if (secoff64
== -1ULL)
2187 // The section needs special handling (e.g., a merge section).
2188 value
= os
->output_address(relobj
, shndx
, sym
->value());
2193 convert_types
<Value_type
, uint64_t>(secoff64
);
2194 if (sym
->type() == elfcpp::STT_TLS
)
2195 value
= sym
->value() + os
->tls_offset() + secoff
;
2197 value
= sym
->value() + os
->address() + secoff
;
2203 case Symbol::IN_OUTPUT_DATA
:
2205 Output_data
* od
= sym
->output_data();
2206 value
= sym
->value();
2207 if (sym
->type() != elfcpp::STT_TLS
)
2208 value
+= od
->address();
2211 Output_section
* os
= od
->output_section();
2212 gold_assert(os
!= NULL
);
2213 value
+= os
->tls_offset() + (od
->address() - os
->address());
2215 if (sym
->offset_is_from_end())
2216 value
+= od
->data_size();
2220 case Symbol::IN_OUTPUT_SEGMENT
:
2222 Output_segment
* os
= sym
->output_segment();
2223 value
= sym
->value();
2224 if (sym
->type() != elfcpp::STT_TLS
)
2225 value
+= os
->vaddr();
2226 switch (sym
->offset_base())
2228 case Symbol::SEGMENT_START
:
2230 case Symbol::SEGMENT_END
:
2231 value
+= os
->memsz();
2233 case Symbol::SEGMENT_BSS
:
2234 value
+= os
->filesz();
2242 case Symbol::IS_CONSTANT
:
2243 value
= sym
->value();
2246 case Symbol::IS_UNDEFINED
:
2254 sym
->set_value(value
);
2256 if (parameters
->options().strip_all())
2258 sym
->set_symtab_index(-1U);
2265 // Write out the global symbols.
2268 Symbol_table::write_globals(const Input_objects
* input_objects
,
2269 const Stringpool
* sympool
,
2270 const Stringpool
* dynpool
,
2271 Output_symtab_xindex
* symtab_xindex
,
2272 Output_symtab_xindex
* dynsym_xindex
,
2273 Output_file
* of
) const
2275 switch (parameters
->size_and_endianness())
2277 #ifdef HAVE_TARGET_32_LITTLE
2278 case Parameters::TARGET_32_LITTLE
:
2279 this->sized_write_globals
<32, false>(input_objects
, sympool
,
2280 dynpool
, symtab_xindex
,
2284 #ifdef HAVE_TARGET_32_BIG
2285 case Parameters::TARGET_32_BIG
:
2286 this->sized_write_globals
<32, true>(input_objects
, sympool
,
2287 dynpool
, symtab_xindex
,
2291 #ifdef HAVE_TARGET_64_LITTLE
2292 case Parameters::TARGET_64_LITTLE
:
2293 this->sized_write_globals
<64, false>(input_objects
, sympool
,
2294 dynpool
, symtab_xindex
,
2298 #ifdef HAVE_TARGET_64_BIG
2299 case Parameters::TARGET_64_BIG
:
2300 this->sized_write_globals
<64, true>(input_objects
, sympool
,
2301 dynpool
, symtab_xindex
,
2310 // Write out the global symbols.
2312 template<int size
, bool big_endian
>
2314 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
2315 const Stringpool
* sympool
,
2316 const Stringpool
* dynpool
,
2317 Output_symtab_xindex
* symtab_xindex
,
2318 Output_symtab_xindex
* dynsym_xindex
,
2319 Output_file
* of
) const
2321 const Target
& target
= parameters
->target();
2323 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2325 const unsigned int output_count
= this->output_count_
;
2326 const section_size_type oview_size
= output_count
* sym_size
;
2327 const unsigned int first_global_index
= this->first_global_index_
;
2328 unsigned char* psyms
;
2329 if (this->offset_
== 0 || output_count
== 0)
2332 psyms
= of
->get_output_view(this->offset_
, oview_size
);
2334 const unsigned int dynamic_count
= this->dynamic_count_
;
2335 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
2336 const unsigned int first_dynamic_global_index
=
2337 this->first_dynamic_global_index_
;
2338 unsigned char* dynamic_view
;
2339 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
2340 dynamic_view
= NULL
;
2342 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
2344 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
2345 p
!= this->table_
.end();
2348 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
2350 // Possibly warn about unresolved symbols in shared libraries.
2351 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
2353 unsigned int sym_index
= sym
->symtab_index();
2354 unsigned int dynsym_index
;
2355 if (dynamic_view
== NULL
)
2358 dynsym_index
= sym
->dynsym_index();
2360 if (sym_index
== -1U && dynsym_index
== -1U)
2362 // This symbol is not included in the output file.
2367 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
2368 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
2369 switch (sym
->source())
2371 case Symbol::FROM_OBJECT
:
2374 unsigned int in_shndx
= sym
->shndx(&is_ordinary
);
2376 // FIXME: We need some target specific support here.
2378 && in_shndx
!= elfcpp::SHN_ABS
2379 && in_shndx
!= elfcpp::SHN_COMMON
)
2381 gold_error(_("%s: unsupported symbol section 0x%x"),
2382 sym
->demangled_name().c_str(), in_shndx
);
2387 Object
* symobj
= sym
->object();
2388 if (symobj
->is_dynamic())
2390 if (sym
->needs_dynsym_value())
2391 dynsym_value
= target
.dynsym_value(sym
);
2392 shndx
= elfcpp::SHN_UNDEF
;
2394 else if (symobj
->pluginobj() != NULL
)
2395 shndx
= elfcpp::SHN_UNDEF
;
2396 else if (in_shndx
== elfcpp::SHN_UNDEF
2398 && (in_shndx
== elfcpp::SHN_ABS
2399 || in_shndx
== elfcpp::SHN_COMMON
)))
2403 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2404 Output_section
* os
= relobj
->output_section(in_shndx
);
2405 gold_assert(os
!= NULL
);
2406 shndx
= os
->out_shndx();
2408 if (shndx
>= elfcpp::SHN_LORESERVE
)
2410 if (sym_index
!= -1U)
2411 symtab_xindex
->add(sym_index
, shndx
);
2412 if (dynsym_index
!= -1U)
2413 dynsym_xindex
->add(dynsym_index
, shndx
);
2414 shndx
= elfcpp::SHN_XINDEX
;
2417 // In object files symbol values are section
2419 if (parameters
->options().relocatable())
2420 sym_value
-= os
->address();
2426 case Symbol::IN_OUTPUT_DATA
:
2427 shndx
= sym
->output_data()->out_shndx();
2428 if (shndx
>= elfcpp::SHN_LORESERVE
)
2430 if (sym_index
!= -1U)
2431 symtab_xindex
->add(sym_index
, shndx
);
2432 if (dynsym_index
!= -1U)
2433 dynsym_xindex
->add(dynsym_index
, shndx
);
2434 shndx
= elfcpp::SHN_XINDEX
;
2438 case Symbol::IN_OUTPUT_SEGMENT
:
2439 shndx
= elfcpp::SHN_ABS
;
2442 case Symbol::IS_CONSTANT
:
2443 shndx
= elfcpp::SHN_ABS
;
2446 case Symbol::IS_UNDEFINED
:
2447 shndx
= elfcpp::SHN_UNDEF
;
2454 if (sym_index
!= -1U)
2456 sym_index
-= first_global_index
;
2457 gold_assert(sym_index
< output_count
);
2458 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
2459 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
2463 if (dynsym_index
!= -1U)
2465 dynsym_index
-= first_dynamic_global_index
;
2466 gold_assert(dynsym_index
< dynamic_count
);
2467 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
2468 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
2473 of
->write_output_view(this->offset_
, oview_size
, psyms
);
2474 if (dynamic_view
!= NULL
)
2475 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
2478 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2479 // strtab holding the name.
2481 template<int size
, bool big_endian
>
2483 Symbol_table::sized_write_symbol(
2484 Sized_symbol
<size
>* sym
,
2485 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
2487 const Stringpool
* pool
,
2488 unsigned char* p
) const
2490 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
2491 osym
.put_st_name(pool
->get_offset(sym
->name()));
2492 osym
.put_st_value(value
);
2493 // Use a symbol size of zero for undefined symbols from shared libraries.
2494 if (shndx
== elfcpp::SHN_UNDEF
&& sym
->is_from_dynobj())
2495 osym
.put_st_size(0);
2497 osym
.put_st_size(sym
->symsize());
2498 // A version script may have overridden the default binding.
2499 if (sym
->is_forced_local())
2500 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, sym
->type()));
2502 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
2503 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
2504 osym
.put_st_shndx(shndx
);
2507 // Check for unresolved symbols in shared libraries. This is
2508 // controlled by the --allow-shlib-undefined option.
2510 // We only warn about libraries for which we have seen all the
2511 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2512 // which were not seen in this link. If we didn't see a DT_NEEDED
2513 // entry, we aren't going to be able to reliably report whether the
2514 // symbol is undefined.
2516 // We also don't warn about libraries found in the system library
2517 // directory (the directory were we find libc.so); we assume that
2518 // those libraries are OK. This heuristic avoids problems in
2519 // GNU/Linux, in which -ldl can have undefined references satisfied by
2523 Symbol_table::warn_about_undefined_dynobj_symbol(
2524 const Input_objects
* input_objects
,
2528 if (sym
->source() == Symbol::FROM_OBJECT
2529 && sym
->object()->is_dynamic()
2530 && sym
->shndx(&dummy
) == elfcpp::SHN_UNDEF
2531 && sym
->binding() != elfcpp::STB_WEAK
2532 && !parameters
->options().allow_shlib_undefined()
2533 && !parameters
->target().is_defined_by_abi(sym
)
2534 && !input_objects
->found_in_system_library_directory(sym
->object()))
2536 // A very ugly cast.
2537 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2538 if (!dynobj
->has_unknown_needed_entries())
2541 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2542 sym
->object()->name().c_str(),
2543 sym
->demangled_name().c_str(),
2546 gold_error(_("%s: undefined reference to '%s'"),
2547 sym
->object()->name().c_str(),
2548 sym
->demangled_name().c_str());
2553 // Write out a section symbol. Return the update offset.
2556 Symbol_table::write_section_symbol(const Output_section
*os
,
2557 Output_symtab_xindex
* symtab_xindex
,
2561 switch (parameters
->size_and_endianness())
2563 #ifdef HAVE_TARGET_32_LITTLE
2564 case Parameters::TARGET_32_LITTLE
:
2565 this->sized_write_section_symbol
<32, false>(os
, symtab_xindex
, of
,
2569 #ifdef HAVE_TARGET_32_BIG
2570 case Parameters::TARGET_32_BIG
:
2571 this->sized_write_section_symbol
<32, true>(os
, symtab_xindex
, of
,
2575 #ifdef HAVE_TARGET_64_LITTLE
2576 case Parameters::TARGET_64_LITTLE
:
2577 this->sized_write_section_symbol
<64, false>(os
, symtab_xindex
, of
,
2581 #ifdef HAVE_TARGET_64_BIG
2582 case Parameters::TARGET_64_BIG
:
2583 this->sized_write_section_symbol
<64, true>(os
, symtab_xindex
, of
,
2592 // Write out a section symbol, specialized for size and endianness.
2594 template<int size
, bool big_endian
>
2596 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2597 Output_symtab_xindex
* symtab_xindex
,
2601 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2603 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2605 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2606 osym
.put_st_name(0);
2607 osym
.put_st_value(os
->address());
2608 osym
.put_st_size(0);
2609 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2610 elfcpp::STT_SECTION
));
2611 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2613 unsigned int shndx
= os
->out_shndx();
2614 if (shndx
>= elfcpp::SHN_LORESERVE
)
2616 symtab_xindex
->add(os
->symtab_index(), shndx
);
2617 shndx
= elfcpp::SHN_XINDEX
;
2619 osym
.put_st_shndx(shndx
);
2621 of
->write_output_view(offset
, sym_size
, pov
);
2624 // Print statistical information to stderr. This is used for --stats.
2627 Symbol_table::print_stats() const
2629 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2630 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2631 program_name
, this->table_
.size(), this->table_
.bucket_count());
2633 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2634 program_name
, this->table_
.size());
2636 this->namepool_
.print_stats("symbol table stringpool");
2639 // We check for ODR violations by looking for symbols with the same
2640 // name for which the debugging information reports that they were
2641 // defined in different source locations. When comparing the source
2642 // location, we consider instances with the same base filename and
2643 // line number to be the same. This is because different object
2644 // files/shared libraries can include the same header file using
2645 // different paths, and we don't want to report an ODR violation in
2648 // This struct is used to compare line information, as returned by
2649 // Dwarf_line_info::one_addr2line. It implements a < comparison
2650 // operator used with std::set.
2652 struct Odr_violation_compare
2655 operator()(const std::string
& s1
, const std::string
& s2
) const
2657 std::string::size_type pos1
= s1
.rfind('/');
2658 std::string::size_type pos2
= s2
.rfind('/');
2659 if (pos1
== std::string::npos
2660 || pos2
== std::string::npos
)
2662 return s1
.compare(pos1
, std::string::npos
,
2663 s2
, pos2
, std::string::npos
) < 0;
2667 // Check candidate_odr_violations_ to find symbols with the same name
2668 // but apparently different definitions (different source-file/line-no).
2671 Symbol_table::detect_odr_violations(const Task
* task
,
2672 const char* output_file_name
) const
2674 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2675 it
!= candidate_odr_violations_
.end();
2678 const char* symbol_name
= it
->first
;
2679 // We use a sorted set so the output is deterministic.
2680 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2682 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2683 locs
= it
->second
.begin();
2684 locs
!= it
->second
.end();
2687 // We need to lock the object in order to read it. This
2688 // means that we have to run in a singleton Task. If we
2689 // want to run this in a general Task for better
2690 // performance, we will need one Task for object, plus
2691 // appropriate locking to ensure that we don't conflict with
2692 // other uses of the object. Also note, one_addr2line is not
2693 // currently thread-safe.
2694 Task_lock_obj
<Object
> tl(task
, locs
->object
);
2695 // 16 is the size of the object-cache that one_addr2line should use.
2696 std::string lineno
= Dwarf_line_info::one_addr2line(
2697 locs
->object
, locs
->shndx
, locs
->offset
, 16);
2698 if (!lineno
.empty())
2699 line_nums
.insert(lineno
);
2702 if (line_nums
.size() > 1)
2704 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2705 "places (possible ODR violation):"),
2706 output_file_name
, demangle(symbol_name
).c_str());
2707 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
2708 it2
!= line_nums
.end();
2710 fprintf(stderr
, " %s\n", it2
->c_str());
2713 // We only call one_addr2line() in this function, so we can clear its cache.
2714 Dwarf_line_info::clear_addr2line_cache();
2717 // Warnings functions.
2719 // Add a new warning.
2722 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
2723 const std::string
& warning
)
2725 name
= symtab
->canonicalize_name(name
);
2726 this->warnings_
[name
].set(obj
, warning
);
2729 // Look through the warnings and mark the symbols for which we should
2730 // warn. This is called during Layout::finalize when we know the
2731 // sources for all the symbols.
2734 Warnings::note_warnings(Symbol_table
* symtab
)
2736 for (Warning_table::iterator p
= this->warnings_
.begin();
2737 p
!= this->warnings_
.end();
2740 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2742 && sym
->source() == Symbol::FROM_OBJECT
2743 && sym
->object() == p
->second
.object
)
2744 sym
->set_has_warning();
2748 // Issue a warning. This is called when we see a relocation against a
2749 // symbol for which has a warning.
2751 template<int size
, bool big_endian
>
2753 Warnings::issue_warning(const Symbol
* sym
,
2754 const Relocate_info
<size
, big_endian
>* relinfo
,
2755 size_t relnum
, off_t reloffset
) const
2757 gold_assert(sym
->has_warning());
2758 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2759 gold_assert(p
!= this->warnings_
.end());
2760 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2761 "%s", p
->second
.text
.c_str());
2764 // Instantiate the templates we need. We could use the configure
2765 // script to restrict this to only the ones needed for implemented
2768 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2771 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2774 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2777 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2780 #ifdef HAVE_TARGET_32_LITTLE
2783 Symbol_table::add_from_relobj
<32, false>(
2784 Sized_relobj
<32, false>* relobj
,
2785 const unsigned char* syms
,
2787 size_t symndx_offset
,
2788 const char* sym_names
,
2789 size_t sym_name_size
,
2790 Sized_relobj
<32, false>::Symbols
* sympointers
,
2794 #ifdef HAVE_TARGET_32_BIG
2797 Symbol_table::add_from_relobj
<32, true>(
2798 Sized_relobj
<32, true>* relobj
,
2799 const unsigned char* syms
,
2801 size_t symndx_offset
,
2802 const char* sym_names
,
2803 size_t sym_name_size
,
2804 Sized_relobj
<32, true>::Symbols
* sympointers
,
2808 #ifdef HAVE_TARGET_64_LITTLE
2811 Symbol_table::add_from_relobj
<64, false>(
2812 Sized_relobj
<64, false>* relobj
,
2813 const unsigned char* syms
,
2815 size_t symndx_offset
,
2816 const char* sym_names
,
2817 size_t sym_name_size
,
2818 Sized_relobj
<64, false>::Symbols
* sympointers
,
2822 #ifdef HAVE_TARGET_64_BIG
2825 Symbol_table::add_from_relobj
<64, true>(
2826 Sized_relobj
<64, true>* relobj
,
2827 const unsigned char* syms
,
2829 size_t symndx_offset
,
2830 const char* sym_names
,
2831 size_t sym_name_size
,
2832 Sized_relobj
<64, true>::Symbols
* sympointers
,
2836 #ifdef HAVE_TARGET_32_LITTLE
2839 Symbol_table::add_from_pluginobj
<32, false>(
2840 Sized_pluginobj
<32, false>* obj
,
2843 elfcpp::Sym
<32, false>* sym
);
2846 #ifdef HAVE_TARGET_32_BIG
2849 Symbol_table::add_from_pluginobj
<32, true>(
2850 Sized_pluginobj
<32, true>* obj
,
2853 elfcpp::Sym
<32, true>* sym
);
2856 #ifdef HAVE_TARGET_64_LITTLE
2859 Symbol_table::add_from_pluginobj
<64, false>(
2860 Sized_pluginobj
<64, false>* obj
,
2863 elfcpp::Sym
<64, false>* sym
);
2866 #ifdef HAVE_TARGET_64_BIG
2869 Symbol_table::add_from_pluginobj
<64, true>(
2870 Sized_pluginobj
<64, true>* obj
,
2873 elfcpp::Sym
<64, true>* sym
);
2876 #ifdef HAVE_TARGET_32_LITTLE
2879 Symbol_table::add_from_dynobj
<32, false>(
2880 Sized_dynobj
<32, false>* dynobj
,
2881 const unsigned char* syms
,
2883 const char* sym_names
,
2884 size_t sym_name_size
,
2885 const unsigned char* versym
,
2887 const std::vector
<const char*>* version_map
,
2888 Sized_relobj
<32, false>::Symbols
* sympointers
,
2892 #ifdef HAVE_TARGET_32_BIG
2895 Symbol_table::add_from_dynobj
<32, true>(
2896 Sized_dynobj
<32, true>* dynobj
,
2897 const unsigned char* syms
,
2899 const char* sym_names
,
2900 size_t sym_name_size
,
2901 const unsigned char* versym
,
2903 const std::vector
<const char*>* version_map
,
2904 Sized_relobj
<32, true>::Symbols
* sympointers
,
2908 #ifdef HAVE_TARGET_64_LITTLE
2911 Symbol_table::add_from_dynobj
<64, false>(
2912 Sized_dynobj
<64, false>* dynobj
,
2913 const unsigned char* syms
,
2915 const char* sym_names
,
2916 size_t sym_name_size
,
2917 const unsigned char* versym
,
2919 const std::vector
<const char*>* version_map
,
2920 Sized_relobj
<64, false>::Symbols
* sympointers
,
2924 #ifdef HAVE_TARGET_64_BIG
2927 Symbol_table::add_from_dynobj
<64, true>(
2928 Sized_dynobj
<64, true>* dynobj
,
2929 const unsigned char* syms
,
2931 const char* sym_names
,
2932 size_t sym_name_size
,
2933 const unsigned char* versym
,
2935 const std::vector
<const char*>* version_map
,
2936 Sized_relobj
<64, true>::Symbols
* sympointers
,
2940 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2943 Symbol_table::define_with_copy_reloc
<32>(
2944 Sized_symbol
<32>* sym
,
2946 elfcpp::Elf_types
<32>::Elf_Addr value
);
2949 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2952 Symbol_table::define_with_copy_reloc
<64>(
2953 Sized_symbol
<64>* sym
,
2955 elfcpp::Elf_types
<64>::Elf_Addr value
);
2958 #ifdef HAVE_TARGET_32_LITTLE
2961 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2962 const Relocate_info
<32, false>* relinfo
,
2963 size_t relnum
, off_t reloffset
) const;
2966 #ifdef HAVE_TARGET_32_BIG
2969 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2970 const Relocate_info
<32, true>* relinfo
,
2971 size_t relnum
, off_t reloffset
) const;
2974 #ifdef HAVE_TARGET_64_LITTLE
2977 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2978 const Relocate_info
<64, false>* relinfo
,
2979 size_t relnum
, off_t reloffset
) const;
2982 #ifdef HAVE_TARGET_64_BIG
2985 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2986 const Relocate_info
<64, true>* relinfo
,
2987 size_t relnum
, off_t reloffset
) const;
2990 } // End namespace gold.