1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
34 #include "dwarf_reader.h"
38 #include "workqueue.h"
46 // Initialize fields in Symbol. This initializes everything except u_
50 Symbol::init_fields(const char* name
, const char* version
,
51 elfcpp::STT type
, elfcpp::STB binding
,
52 elfcpp::STV visibility
, unsigned char nonvis
)
55 this->version_
= version
;
56 this->symtab_index_
= 0;
57 this->dynsym_index_
= 0;
58 this->got_offsets_
.init();
59 this->plt_offset_
= 0;
61 this->binding_
= binding
;
62 this->visibility_
= visibility
;
63 this->nonvis_
= nonvis
;
64 this->is_target_special_
= false;
65 this->is_def_
= false;
66 this->is_forwarder_
= false;
67 this->has_alias_
= false;
68 this->needs_dynsym_entry_
= false;
69 this->in_reg_
= false;
70 this->in_dyn_
= false;
71 this->has_plt_offset_
= false;
72 this->has_warning_
= false;
73 this->is_copied_from_dynobj_
= false;
74 this->is_forced_local_
= false;
75 this->is_ordinary_shndx_
= false;
78 // Return the demangled version of the symbol's name, but only
79 // if the --demangle flag was set.
82 demangle(const char* name
)
84 if (!parameters
->options().do_demangle())
87 // cplus_demangle allocates memory for the result it returns,
88 // and returns NULL if the name is already demangled.
89 char* demangled_name
= cplus_demangle(name
, DMGL_ANSI
| DMGL_PARAMS
);
90 if (demangled_name
== NULL
)
93 std::string
retval(demangled_name
);
99 Symbol::demangled_name() const
101 return demangle(this->name());
104 // Initialize the fields in the base class Symbol for SYM in OBJECT.
106 template<int size
, bool big_endian
>
108 Symbol::init_base_object(const char* name
, const char* version
, Object
* object
,
109 const elfcpp::Sym
<size
, big_endian
>& sym
,
110 unsigned int st_shndx
, bool is_ordinary
)
112 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
113 sym
.get_st_visibility(), sym
.get_st_nonvis());
114 this->u_
.from_object
.object
= object
;
115 this->u_
.from_object
.shndx
= st_shndx
;
116 this->is_ordinary_shndx_
= is_ordinary
;
117 this->source_
= FROM_OBJECT
;
118 this->in_reg_
= !object
->is_dynamic();
119 this->in_dyn_
= object
->is_dynamic();
122 // Initialize the fields in the base class Symbol for a symbol defined
123 // in an Output_data.
126 Symbol::init_base_output_data(const char* name
, const char* version
,
127 Output_data
* od
, elfcpp::STT type
,
128 elfcpp::STB binding
, elfcpp::STV visibility
,
129 unsigned char nonvis
, bool offset_is_from_end
)
131 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
132 this->u_
.in_output_data
.output_data
= od
;
133 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
134 this->source_
= IN_OUTPUT_DATA
;
135 this->in_reg_
= true;
138 // Initialize the fields in the base class Symbol for a symbol defined
139 // in an Output_segment.
142 Symbol::init_base_output_segment(const char* name
, const char* version
,
143 Output_segment
* os
, elfcpp::STT type
,
144 elfcpp::STB binding
, elfcpp::STV visibility
,
145 unsigned char nonvis
,
146 Segment_offset_base offset_base
)
148 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
149 this->u_
.in_output_segment
.output_segment
= os
;
150 this->u_
.in_output_segment
.offset_base
= offset_base
;
151 this->source_
= IN_OUTPUT_SEGMENT
;
152 this->in_reg_
= true;
155 // Initialize the fields in the base class Symbol for a symbol defined
159 Symbol::init_base_constant(const char* name
, const char* version
,
160 elfcpp::STT type
, elfcpp::STB binding
,
161 elfcpp::STV visibility
, unsigned char nonvis
)
163 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
164 this->source_
= IS_CONSTANT
;
165 this->in_reg_
= true;
168 // Initialize the fields in the base class Symbol for an undefined
172 Symbol::init_base_undefined(const char* name
, const char* version
,
173 elfcpp::STT type
, elfcpp::STB binding
,
174 elfcpp::STV visibility
, unsigned char nonvis
)
176 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
177 this->source_
= IS_UNDEFINED
;
178 this->in_reg_
= true;
181 // Allocate a common symbol in the base.
184 Symbol::allocate_base_common(Output_data
* od
)
186 gold_assert(this->is_common());
187 this->source_
= IN_OUTPUT_DATA
;
188 this->u_
.in_output_data
.output_data
= od
;
189 this->u_
.in_output_data
.offset_is_from_end
= false;
192 // Initialize the fields in Sized_symbol for SYM in OBJECT.
195 template<bool big_endian
>
197 Sized_symbol
<size
>::init_object(const char* name
, const char* version
,
199 const elfcpp::Sym
<size
, big_endian
>& sym
,
200 unsigned int st_shndx
, bool is_ordinary
)
202 this->init_base_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
203 this->value_
= sym
.get_st_value();
204 this->symsize_
= sym
.get_st_size();
207 // Initialize the fields in Sized_symbol for a symbol defined in an
212 Sized_symbol
<size
>::init_output_data(const char* name
, const char* version
,
213 Output_data
* od
, Value_type value
,
214 Size_type symsize
, elfcpp::STT type
,
216 elfcpp::STV visibility
,
217 unsigned char nonvis
,
218 bool offset_is_from_end
)
220 this->init_base_output_data(name
, version
, od
, type
, binding
, visibility
,
221 nonvis
, offset_is_from_end
);
222 this->value_
= value
;
223 this->symsize_
= symsize
;
226 // Initialize the fields in Sized_symbol for a symbol defined in an
231 Sized_symbol
<size
>::init_output_segment(const char* name
, const char* version
,
232 Output_segment
* os
, Value_type value
,
233 Size_type symsize
, elfcpp::STT type
,
235 elfcpp::STV visibility
,
236 unsigned char nonvis
,
237 Segment_offset_base offset_base
)
239 this->init_base_output_segment(name
, version
, os
, type
, binding
, visibility
,
240 nonvis
, offset_base
);
241 this->value_
= value
;
242 this->symsize_
= symsize
;
245 // Initialize the fields in Sized_symbol for a symbol defined as a
250 Sized_symbol
<size
>::init_constant(const char* name
, const char* version
,
251 Value_type value
, Size_type symsize
,
252 elfcpp::STT type
, elfcpp::STB binding
,
253 elfcpp::STV visibility
, unsigned char nonvis
)
255 this->init_base_constant(name
, version
, type
, binding
, visibility
, nonvis
);
256 this->value_
= value
;
257 this->symsize_
= symsize
;
260 // Initialize the fields in Sized_symbol for an undefined symbol.
264 Sized_symbol
<size
>::init_undefined(const char* name
, const char* version
,
265 elfcpp::STT type
, elfcpp::STB binding
,
266 elfcpp::STV visibility
, unsigned char nonvis
)
268 this->init_base_undefined(name
, version
, type
, binding
, visibility
, nonvis
);
273 // Allocate a common symbol.
277 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
279 this->allocate_base_common(od
);
280 this->value_
= value
;
283 // Return true if this symbol should be added to the dynamic symbol
287 Symbol::should_add_dynsym_entry() const
289 // If the symbol is used by a dynamic relocation, we need to add it.
290 if (this->needs_dynsym_entry())
293 // If the symbol was forced local in a version script, do not add it.
294 if (this->is_forced_local())
297 // If exporting all symbols or building a shared library,
298 // and the symbol is defined in a regular object and is
299 // externally visible, we need to add it.
300 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
301 && !this->is_from_dynobj()
302 && this->is_externally_visible())
308 // Return true if the final value of this symbol is known at link
312 Symbol::final_value_is_known() const
314 // If we are not generating an executable, then no final values are
315 // known, since they will change at runtime.
316 if (parameters
->options().shared() || parameters
->options().relocatable())
319 // If the symbol is not from an object file, and is not undefined,
320 // then it is defined, and known.
321 if (this->source_
!= FROM_OBJECT
)
323 if (this->source_
!= IS_UNDEFINED
)
328 // If the symbol is from a dynamic object, then the final value
330 if (this->object()->is_dynamic())
333 // If the symbol is not undefined (it is defined or common),
334 // then the final value is known.
335 if (!this->is_undefined())
339 // If the symbol is undefined, then whether the final value is known
340 // depends on whether we are doing a static link. If we are doing a
341 // dynamic link, then the final value could be filled in at runtime.
342 // This could reasonably be the case for a weak undefined symbol.
343 return parameters
->doing_static_link();
346 // Return the output section where this symbol is defined.
349 Symbol::output_section() const
351 switch (this->source_
)
355 unsigned int shndx
= this->u_
.from_object
.shndx
;
356 if (shndx
!= elfcpp::SHN_UNDEF
&& this->is_ordinary_shndx_
)
358 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
359 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
360 return relobj
->output_section(shndx
);
366 return this->u_
.in_output_data
.output_data
->output_section();
368 case IN_OUTPUT_SEGMENT
:
378 // Set the symbol's output section. This is used for symbols defined
379 // in scripts. This should only be called after the symbol table has
383 Symbol::set_output_section(Output_section
* os
)
385 switch (this->source_
)
389 gold_assert(this->output_section() == os
);
392 this->source_
= IN_OUTPUT_DATA
;
393 this->u_
.in_output_data
.output_data
= os
;
394 this->u_
.in_output_data
.offset_is_from_end
= false;
396 case IN_OUTPUT_SEGMENT
:
403 // Class Symbol_table.
405 Symbol_table::Symbol_table(unsigned int count
,
406 const Version_script_info
& version_script
)
407 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
408 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
409 version_script_(version_script
)
411 namepool_
.reserve(count
);
414 Symbol_table::~Symbol_table()
418 // The hash function. The key values are Stringpool keys.
421 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
423 return key
.first
^ key
.second
;
426 // The symbol table key equality function. This is called with
430 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
431 const Symbol_table_key
& k2
) const
433 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
436 // Make TO a symbol which forwards to FROM.
439 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
441 gold_assert(from
!= to
);
442 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
443 this->forwarders_
[from
] = to
;
444 from
->set_forwarder();
447 // Resolve the forwards from FROM, returning the real symbol.
450 Symbol_table::resolve_forwards(const Symbol
* from
) const
452 gold_assert(from
->is_forwarder());
453 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
454 this->forwarders_
.find(from
);
455 gold_assert(p
!= this->forwarders_
.end());
459 // Look up a symbol by name.
462 Symbol_table::lookup(const char* name
, const char* version
) const
464 Stringpool::Key name_key
;
465 name
= this->namepool_
.find(name
, &name_key
);
469 Stringpool::Key version_key
= 0;
472 version
= this->namepool_
.find(version
, &version_key
);
477 Symbol_table_key
key(name_key
, version_key
);
478 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
479 if (p
== this->table_
.end())
484 // Resolve a Symbol with another Symbol. This is only used in the
485 // unusual case where there are references to both an unversioned
486 // symbol and a symbol with a version, and we then discover that that
487 // version is the default version. Because this is unusual, we do
488 // this the slow way, by converting back to an ELF symbol.
490 template<int size
, bool big_endian
>
492 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
)
494 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
495 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
496 // We don't bother to set the st_name or the st_shndx field.
497 esym
.put_st_value(from
->value());
498 esym
.put_st_size(from
->symsize());
499 esym
.put_st_info(from
->binding(), from
->type());
500 esym
.put_st_other(from
->visibility(), from
->nonvis());
502 unsigned int shndx
= from
->shndx(&is_ordinary
);
503 this->resolve(to
, esym
.sym(), shndx
, is_ordinary
, shndx
, from
->object(),
511 // Record that a symbol is forced to be local by a version script.
514 Symbol_table::force_local(Symbol
* sym
)
516 if (!sym
->is_defined() && !sym
->is_common())
518 if (sym
->is_forced_local())
520 // We already got this one.
523 sym
->set_is_forced_local();
524 this->forced_locals_
.push_back(sym
);
527 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
528 // is only called for undefined symbols, when at least one --wrap
532 Symbol_table::wrap_symbol(Object
* object
, const char* name
,
533 Stringpool::Key
* name_key
)
535 // For some targets, we need to ignore a specific character when
536 // wrapping, and add it back later.
538 if (name
[0] == object
->target()->wrap_char())
544 if (parameters
->options().is_wrap(name
))
546 // Turn NAME into __wrap_NAME.
553 // This will give us both the old and new name in NAMEPOOL_, but
554 // that is OK. Only the versions we need will wind up in the
555 // real string table in the output file.
556 return this->namepool_
.add(s
.c_str(), true, name_key
);
559 const char* const real_prefix
= "__real_";
560 const size_t real_prefix_length
= strlen(real_prefix
);
561 if (strncmp(name
, real_prefix
, real_prefix_length
) == 0
562 && parameters
->options().is_wrap(name
+ real_prefix_length
))
564 // Turn __real_NAME into NAME.
568 s
+= name
+ real_prefix_length
;
569 return this->namepool_
.add(s
.c_str(), true, name_key
);
575 // Add one symbol from OBJECT to the symbol table. NAME is symbol
576 // name and VERSION is the version; both are canonicalized. DEF is
577 // whether this is the default version. ST_SHNDX is the symbol's
578 // section index; IS_ORDINARY is whether this is a normal section
579 // rather than a special code.
581 // If DEF is true, then this is the definition of a default version of
582 // a symbol. That means that any lookup of NAME/NULL and any lookup
583 // of NAME/VERSION should always return the same symbol. This is
584 // obvious for references, but in particular we want to do this for
585 // definitions: overriding NAME/NULL should also override
586 // NAME/VERSION. If we don't do that, it would be very hard to
587 // override functions in a shared library which uses versioning.
589 // We implement this by simply making both entries in the hash table
590 // point to the same Symbol structure. That is easy enough if this is
591 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
592 // that we have seen both already, in which case they will both have
593 // independent entries in the symbol table. We can't simply change
594 // the symbol table entry, because we have pointers to the entries
595 // attached to the object files. So we mark the entry attached to the
596 // object file as a forwarder, and record it in the forwarders_ map.
597 // Note that entries in the hash table will never be marked as
600 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
601 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
602 // for a special section code. ST_SHNDX may be modified if the symbol
603 // is defined in a section being discarded.
605 template<int size
, bool big_endian
>
607 Symbol_table::add_from_object(Object
* object
,
609 Stringpool::Key name_key
,
611 Stringpool::Key version_key
,
613 const elfcpp::Sym
<size
, big_endian
>& sym
,
614 unsigned int st_shndx
,
616 unsigned int orig_st_shndx
)
618 // Print a message if this symbol is being traced.
619 if (parameters
->options().is_trace_symbol(name
))
621 if (orig_st_shndx
== elfcpp::SHN_UNDEF
)
622 gold_info(_("%s: reference to %s"), object
->name().c_str(), name
);
624 gold_info(_("%s: definition of %s"), object
->name().c_str(), name
);
627 // For an undefined symbol, we may need to adjust the name using
629 if (orig_st_shndx
== elfcpp::SHN_UNDEF
630 && parameters
->options().any_wrap())
632 const char* wrap_name
= this->wrap_symbol(object
, name
, &name_key
);
633 if (wrap_name
!= name
)
635 // If we see a reference to malloc with version GLIBC_2.0,
636 // and we turn it into a reference to __wrap_malloc, then we
637 // discard the version number. Otherwise the user would be
638 // required to specify the correct version for
646 Symbol
* const snull
= NULL
;
647 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
648 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
651 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
652 std::make_pair(this->table_
.end(), false);
655 const Stringpool::Key vnull_key
= 0;
656 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
661 // ins.first: an iterator, which is a pointer to a pair.
662 // ins.first->first: the key (a pair of name and version).
663 // ins.first->second: the value (Symbol*).
664 // ins.second: true if new entry was inserted, false if not.
666 Sized_symbol
<size
>* ret
;
671 // We already have an entry for NAME/VERSION.
672 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
673 gold_assert(ret
!= NULL
);
675 was_undefined
= ret
->is_undefined();
676 was_common
= ret
->is_common();
678 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
685 // This is the first time we have seen NAME/NULL. Make
686 // NAME/NULL point to NAME/VERSION.
687 insdef
.first
->second
= ret
;
689 else if (insdef
.first
->second
!= ret
)
691 // This is the unfortunate case where we already have
692 // entries for both NAME/VERSION and NAME/NULL. We now
693 // see a symbol NAME/VERSION where VERSION is the
694 // default version. We have already resolved this new
695 // symbol with the existing NAME/VERSION symbol.
697 // It's possible that NAME/NULL and NAME/VERSION are
698 // both defined in regular objects. This can only
699 // happen if one object file defines foo and another
700 // defines foo@@ver. This is somewhat obscure, but we
701 // call it a multiple definition error.
703 // It's possible that NAME/NULL actually has a version,
704 // in which case it won't be the same as VERSION. This
705 // happens with ver_test_7.so in the testsuite for the
706 // symbol t2_2. We see t2_2@@VER2, so we define both
707 // t2_2/VER2 and t2_2/NULL. We then see an unadorned
708 // t2_2 in an object file and give it version VER1 from
709 // the version script. This looks like a default
710 // definition for VER1, so it looks like we should merge
711 // t2_2/NULL with t2_2/VER1. That doesn't make sense,
712 // but it's not obvious that this is an error, either.
715 // If one of the symbols has non-default visibility, and
716 // the other is defined in a shared object, then they
717 // are different symbols.
719 // Otherwise, we just resolve the symbols as though they
722 if (insdef
.first
->second
->version() != NULL
)
724 gold_assert(insdef
.first
->second
->version() != version
);
727 else if (ret
->visibility() != elfcpp::STV_DEFAULT
728 && insdef
.first
->second
->is_from_dynobj())
730 else if (insdef
.first
->second
->visibility() != elfcpp::STV_DEFAULT
731 && ret
->is_from_dynobj())
735 const Sized_symbol
<size
>* sym2
;
736 sym2
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
737 Symbol_table::resolve
<size
, big_endian
>(ret
, sym2
);
738 this->make_forwarder(insdef
.first
->second
, ret
);
739 insdef
.first
->second
= ret
;
748 // This is the first time we have seen NAME/VERSION.
749 gold_assert(ins
.first
->second
== NULL
);
751 if (def
&& !insdef
.second
)
753 // We already have an entry for NAME/NULL. If we override
754 // it, then change it to NAME/VERSION.
755 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
757 was_undefined
= ret
->is_undefined();
758 was_common
= ret
->is_common();
760 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
762 ins
.first
->second
= ret
;
766 was_undefined
= false;
769 Sized_target
<size
, big_endian
>* target
=
770 object
->sized_target
<size
, big_endian
>();
771 if (!target
->has_make_symbol())
772 ret
= new Sized_symbol
<size
>();
775 ret
= target
->make_symbol();
778 // This means that we don't want a symbol table
781 this->table_
.erase(ins
.first
);
784 this->table_
.erase(insdef
.first
);
785 // Inserting insdef invalidated ins.
786 this->table_
.erase(std::make_pair(name_key
,
793 ret
->init_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
795 ins
.first
->second
= ret
;
798 // This is the first time we have seen NAME/NULL. Point
799 // it at the new entry for NAME/VERSION.
800 gold_assert(insdef
.second
);
801 insdef
.first
->second
= ret
;
806 // Record every time we see a new undefined symbol, to speed up
808 if (!was_undefined
&& ret
->is_undefined())
809 ++this->saw_undefined_
;
811 // Keep track of common symbols, to speed up common symbol
813 if (!was_common
&& ret
->is_common())
815 if (ret
->type() != elfcpp::STT_TLS
)
816 this->commons_
.push_back(ret
);
818 this->tls_commons_
.push_back(ret
);
822 ret
->set_is_default();
826 // Add all the symbols in a relocatable object to the hash table.
828 template<int size
, bool big_endian
>
830 Symbol_table::add_from_relobj(
831 Sized_relobj
<size
, big_endian
>* relobj
,
832 const unsigned char* syms
,
834 size_t symndx_offset
,
835 const char* sym_names
,
836 size_t sym_name_size
,
837 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
842 gold_assert(size
== relobj
->target()->get_size());
843 gold_assert(size
== parameters
->target().get_size());
845 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
847 const bool just_symbols
= relobj
->just_symbols();
849 const unsigned char* p
= syms
;
850 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
852 (*sympointers
)[i
] = NULL
;
854 elfcpp::Sym
<size
, big_endian
> sym(p
);
856 unsigned int st_name
= sym
.get_st_name();
857 if (st_name
>= sym_name_size
)
859 relobj
->error(_("bad global symbol name offset %u at %zu"),
864 const char* name
= sym_names
+ st_name
;
867 unsigned int st_shndx
= relobj
->adjust_sym_shndx(i
+ symndx_offset
,
870 unsigned int orig_st_shndx
= st_shndx
;
872 orig_st_shndx
= elfcpp::SHN_UNDEF
;
874 if (st_shndx
!= elfcpp::SHN_UNDEF
)
877 // A symbol defined in a section which we are not including must
878 // be treated as an undefined symbol.
879 if (st_shndx
!= elfcpp::SHN_UNDEF
881 && !relobj
->is_section_included(st_shndx
))
882 st_shndx
= elfcpp::SHN_UNDEF
;
884 // In an object file, an '@' in the name separates the symbol
885 // name from the version name. If there are two '@' characters,
886 // this is the default version.
887 const char* ver
= strchr(name
, '@');
889 // DEF: is the version default? LOCAL: is the symbol forced local?
895 // The symbol name is of the form foo@VERSION or foo@@VERSION
896 namelen
= ver
- name
;
904 // We don't want to assign a version to an undefined symbol,
905 // even if it is listed in the version script. FIXME: What
906 // about a common symbol?
907 else if (!version_script_
.empty()
908 && st_shndx
!= elfcpp::SHN_UNDEF
)
910 // The symbol name did not have a version, but
911 // the version script may assign a version anyway.
912 namelen
= strlen(name
);
914 // Check the global: entries from the version script.
915 const std::string
& version
=
916 version_script_
.get_symbol_version(name
);
917 if (!version
.empty())
918 ver
= version
.c_str();
919 // Check the local: entries from the version script
920 if (version_script_
.symbol_is_local(name
))
924 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
925 unsigned char symbuf
[sym_size
];
926 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
929 memcpy(symbuf
, p
, sym_size
);
930 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
931 if (orig_st_shndx
!= elfcpp::SHN_UNDEF
&& is_ordinary
)
933 // Symbol values in object files are section relative.
934 // This is normally what we want, but since here we are
935 // converting the symbol to absolute we need to add the
936 // section address. The section address in an object
937 // file is normally zero, but people can use a linker
938 // script to change it.
939 sw
.put_st_value(sym
.get_st_value()
940 + relobj
->section_address(orig_st_shndx
));
942 st_shndx
= elfcpp::SHN_ABS
;
947 Sized_symbol
<size
>* res
;
950 Stringpool::Key name_key
;
951 name
= this->namepool_
.add(name
, true, &name_key
);
952 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
953 false, *psym
, st_shndx
, is_ordinary
,
956 this->force_local(res
);
960 Stringpool::Key name_key
;
961 name
= this->namepool_
.add_with_length(name
, namelen
, true,
963 Stringpool::Key ver_key
;
964 ver
= this->namepool_
.add(ver
, true, &ver_key
);
966 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
967 def
, *psym
, st_shndx
, is_ordinary
,
971 (*sympointers
)[i
] = res
;
975 // Add all the symbols in a dynamic object to the hash table.
977 template<int size
, bool big_endian
>
979 Symbol_table::add_from_dynobj(
980 Sized_dynobj
<size
, big_endian
>* dynobj
,
981 const unsigned char* syms
,
983 const char* sym_names
,
984 size_t sym_name_size
,
985 const unsigned char* versym
,
987 const std::vector
<const char*>* version_map
,
988 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
993 gold_assert(size
== dynobj
->target()->get_size());
994 gold_assert(size
== parameters
->target().get_size());
996 if (dynobj
->just_symbols())
998 gold_error(_("--just-symbols does not make sense with a shared object"));
1002 if (versym
!= NULL
&& versym_size
/ 2 < count
)
1004 dynobj
->error(_("too few symbol versions"));
1008 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1010 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1011 // weak aliases. This is necessary because if the dynamic object
1012 // provides the same variable under two names, one of which is a
1013 // weak definition, and the regular object refers to the weak
1014 // definition, we have to put both the weak definition and the
1015 // strong definition into the dynamic symbol table. Given a weak
1016 // definition, the only way that we can find the corresponding
1017 // strong definition, if any, is to search the symbol table.
1018 std::vector
<Sized_symbol
<size
>*> object_symbols
;
1020 const unsigned char* p
= syms
;
1021 const unsigned char* vs
= versym
;
1022 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
1024 elfcpp::Sym
<size
, big_endian
> sym(p
);
1026 if (sympointers
!= NULL
)
1027 (*sympointers
)[i
] = NULL
;
1029 // Ignore symbols with local binding or that have
1030 // internal or hidden visibility.
1031 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
1032 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
1033 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
1036 // A protected symbol in a shared library must be treated as a
1037 // normal symbol when viewed from outside the shared library.
1038 // Implement this by overriding the visibility here.
1039 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1040 unsigned char symbuf
[sym_size
];
1041 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1042 if (sym
.get_st_visibility() == elfcpp::STV_PROTECTED
)
1044 memcpy(symbuf
, p
, sym_size
);
1045 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1046 sw
.put_st_other(elfcpp::STV_DEFAULT
, sym
.get_st_nonvis());
1050 unsigned int st_name
= psym
->get_st_name();
1051 if (st_name
>= sym_name_size
)
1053 dynobj
->error(_("bad symbol name offset %u at %zu"),
1058 const char* name
= sym_names
+ st_name
;
1061 unsigned int st_shndx
= dynobj
->adjust_sym_shndx(i
, psym
->get_st_shndx(),
1064 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1067 Sized_symbol
<size
>* res
;
1071 Stringpool::Key name_key
;
1072 name
= this->namepool_
.add(name
, true, &name_key
);
1073 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1074 false, *psym
, st_shndx
, is_ordinary
,
1079 // Read the version information.
1081 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
1083 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
1084 v
&= elfcpp::VERSYM_VERSION
;
1086 // The Sun documentation says that V can be VER_NDX_LOCAL,
1087 // or VER_NDX_GLOBAL, or a version index. The meaning of
1088 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1089 // The old GNU linker will happily generate VER_NDX_LOCAL
1090 // for an undefined symbol. I don't know what the Sun
1091 // linker will generate.
1093 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1094 && st_shndx
!= elfcpp::SHN_UNDEF
)
1096 // This symbol should not be visible outside the object.
1100 // At this point we are definitely going to add this symbol.
1101 Stringpool::Key name_key
;
1102 name
= this->namepool_
.add(name
, true, &name_key
);
1104 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1105 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
1107 // This symbol does not have a version.
1108 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1109 false, *psym
, st_shndx
, is_ordinary
,
1114 if (v
>= version_map
->size())
1116 dynobj
->error(_("versym for symbol %zu out of range: %u"),
1121 const char* version
= (*version_map
)[v
];
1122 if (version
== NULL
)
1124 dynobj
->error(_("versym for symbol %zu has no name: %u"),
1129 Stringpool::Key version_key
;
1130 version
= this->namepool_
.add(version
, true, &version_key
);
1132 // If this is an absolute symbol, and the version name
1133 // and symbol name are the same, then this is the
1134 // version definition symbol. These symbols exist to
1135 // support using -u to pull in particular versions. We
1136 // do not want to record a version for them.
1137 if (st_shndx
== elfcpp::SHN_ABS
1139 && name_key
== version_key
)
1140 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1141 false, *psym
, st_shndx
, is_ordinary
,
1145 const bool def
= (!hidden
1146 && st_shndx
!= elfcpp::SHN_UNDEF
);
1147 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
1148 version_key
, def
, *psym
, st_shndx
,
1149 is_ordinary
, st_shndx
);
1154 // Note that it is possible that RES was overridden by an
1155 // earlier object, in which case it can't be aliased here.
1156 if (st_shndx
!= elfcpp::SHN_UNDEF
1158 && psym
->get_st_type() == elfcpp::STT_OBJECT
1159 && res
->source() == Symbol::FROM_OBJECT
1160 && res
->object() == dynobj
)
1161 object_symbols
.push_back(res
);
1163 if (sympointers
!= NULL
)
1164 (*sympointers
)[i
] = res
;
1167 this->record_weak_aliases(&object_symbols
);
1170 // This is used to sort weak aliases. We sort them first by section
1171 // index, then by offset, then by weak ahead of strong.
1174 class Weak_alias_sorter
1177 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
1182 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
1183 const Sized_symbol
<size
>* s2
) const
1186 unsigned int s1_shndx
= s1
->shndx(&is_ordinary
);
1187 gold_assert(is_ordinary
);
1188 unsigned int s2_shndx
= s2
->shndx(&is_ordinary
);
1189 gold_assert(is_ordinary
);
1190 if (s1_shndx
!= s2_shndx
)
1191 return s1_shndx
< s2_shndx
;
1193 if (s1
->value() != s2
->value())
1194 return s1
->value() < s2
->value();
1195 if (s1
->binding() != s2
->binding())
1197 if (s1
->binding() == elfcpp::STB_WEAK
)
1199 if (s2
->binding() == elfcpp::STB_WEAK
)
1202 return std::string(s1
->name()) < std::string(s2
->name());
1205 // SYMBOLS is a list of object symbols from a dynamic object. Look
1206 // for any weak aliases, and record them so that if we add the weak
1207 // alias to the dynamic symbol table, we also add the corresponding
1212 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
1214 // Sort the vector by section index, then by offset, then by weak
1216 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
1218 // Walk through the vector. For each weak definition, record
1220 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
1222 p
!= symbols
->end();
1225 if ((*p
)->binding() != elfcpp::STB_WEAK
)
1228 // Build a circular list of weak aliases. Each symbol points to
1229 // the next one in the circular list.
1231 Sized_symbol
<size
>* from_sym
= *p
;
1232 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
1233 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
1236 if ((*q
)->shndx(&dummy
) != from_sym
->shndx(&dummy
)
1237 || (*q
)->value() != from_sym
->value())
1240 this->weak_aliases_
[from_sym
] = *q
;
1241 from_sym
->set_has_alias();
1247 this->weak_aliases_
[from_sym
] = *p
;
1248 from_sym
->set_has_alias();
1255 // Create and return a specially defined symbol. If ONLY_IF_REF is
1256 // true, then only create the symbol if there is a reference to it.
1257 // If this does not return NULL, it sets *POLDSYM to the existing
1258 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1260 template<int size
, bool big_endian
>
1262 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1264 Sized_symbol
<size
>** poldsym
)
1267 Sized_symbol
<size
>* sym
;
1268 bool add_to_table
= false;
1269 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1271 // If the caller didn't give us a version, see if we get one from
1272 // the version script.
1273 if (*pversion
== NULL
)
1275 const std::string
& v(this->version_script_
.get_symbol_version(*pname
));
1277 *pversion
= v
.c_str();
1282 oldsym
= this->lookup(*pname
, *pversion
);
1283 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1286 *pname
= oldsym
->name();
1287 *pversion
= oldsym
->version();
1291 // Canonicalize NAME and VERSION.
1292 Stringpool::Key name_key
;
1293 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1295 Stringpool::Key version_key
= 0;
1296 if (*pversion
!= NULL
)
1297 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1299 Symbol
* const snull
= NULL
;
1300 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1301 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1307 // We already have a symbol table entry for NAME/VERSION.
1308 oldsym
= ins
.first
->second
;
1309 gold_assert(oldsym
!= NULL
);
1313 // We haven't seen this symbol before.
1314 gold_assert(ins
.first
->second
== NULL
);
1315 add_to_table
= true;
1316 add_loc
= ins
.first
;
1321 const Target
& target
= parameters
->target();
1322 if (!target
.has_make_symbol())
1323 sym
= new Sized_symbol
<size
>();
1326 gold_assert(target
.get_size() == size
);
1327 gold_assert(target
.is_big_endian() ? big_endian
: !big_endian
);
1328 typedef Sized_target
<size
, big_endian
> My_target
;
1329 const My_target
* sized_target
=
1330 static_cast<const My_target
*>(&target
);
1331 sym
= sized_target
->make_symbol();
1337 add_loc
->second
= sym
;
1339 gold_assert(oldsym
!= NULL
);
1341 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1346 // Define a symbol based on an Output_data.
1349 Symbol_table::define_in_output_data(const char* name
,
1350 const char* version
,
1355 elfcpp::STB binding
,
1356 elfcpp::STV visibility
,
1357 unsigned char nonvis
,
1358 bool offset_is_from_end
,
1361 if (parameters
->target().get_size() == 32)
1363 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1364 return this->do_define_in_output_data
<32>(name
, version
, od
,
1365 value
, symsize
, type
, binding
,
1373 else if (parameters
->target().get_size() == 64)
1375 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1376 return this->do_define_in_output_data
<64>(name
, version
, od
,
1377 value
, symsize
, type
, binding
,
1389 // Define a symbol in an Output_data, sized version.
1393 Symbol_table::do_define_in_output_data(
1395 const char* version
,
1397 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1398 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1400 elfcpp::STB binding
,
1401 elfcpp::STV visibility
,
1402 unsigned char nonvis
,
1403 bool offset_is_from_end
,
1406 Sized_symbol
<size
>* sym
;
1407 Sized_symbol
<size
>* oldsym
;
1409 if (parameters
->target().is_big_endian())
1411 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1412 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1413 only_if_ref
, &oldsym
);
1420 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1421 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1422 only_if_ref
, &oldsym
);
1431 sym
->init_output_data(name
, version
, od
, value
, symsize
, type
, binding
,
1432 visibility
, nonvis
, offset_is_from_end
);
1436 if (binding
== elfcpp::STB_LOCAL
1437 || this->version_script_
.symbol_is_local(name
))
1438 this->force_local(sym
);
1439 else if (version
!= NULL
)
1440 sym
->set_is_default();
1444 if (Symbol_table::should_override_with_special(oldsym
))
1445 this->override_with_special(oldsym
, sym
);
1450 // Define a symbol based on an Output_segment.
1453 Symbol_table::define_in_output_segment(const char* name
,
1454 const char* version
, Output_segment
* os
,
1458 elfcpp::STB binding
,
1459 elfcpp::STV visibility
,
1460 unsigned char nonvis
,
1461 Symbol::Segment_offset_base offset_base
,
1464 if (parameters
->target().get_size() == 32)
1466 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1467 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1468 value
, symsize
, type
,
1469 binding
, visibility
, nonvis
,
1470 offset_base
, only_if_ref
);
1475 else if (parameters
->target().get_size() == 64)
1477 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1478 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1479 value
, symsize
, type
,
1480 binding
, visibility
, nonvis
,
1481 offset_base
, only_if_ref
);
1490 // Define a symbol in an Output_segment, sized version.
1494 Symbol_table::do_define_in_output_segment(
1496 const char* version
,
1498 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1499 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1501 elfcpp::STB binding
,
1502 elfcpp::STV visibility
,
1503 unsigned char nonvis
,
1504 Symbol::Segment_offset_base offset_base
,
1507 Sized_symbol
<size
>* sym
;
1508 Sized_symbol
<size
>* oldsym
;
1510 if (parameters
->target().is_big_endian())
1512 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1513 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1514 only_if_ref
, &oldsym
);
1521 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1522 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1523 only_if_ref
, &oldsym
);
1532 sym
->init_output_segment(name
, version
, os
, value
, symsize
, type
, binding
,
1533 visibility
, nonvis
, offset_base
);
1537 if (binding
== elfcpp::STB_LOCAL
1538 || this->version_script_
.symbol_is_local(name
))
1539 this->force_local(sym
);
1540 else if (version
!= NULL
)
1541 sym
->set_is_default();
1545 if (Symbol_table::should_override_with_special(oldsym
))
1546 this->override_with_special(oldsym
, sym
);
1551 // Define a special symbol with a constant value. It is a multiple
1552 // definition error if this symbol is already defined.
1555 Symbol_table::define_as_constant(const char* name
,
1556 const char* version
,
1560 elfcpp::STB binding
,
1561 elfcpp::STV visibility
,
1562 unsigned char nonvis
,
1564 bool force_override
)
1566 if (parameters
->target().get_size() == 32)
1568 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1569 return this->do_define_as_constant
<32>(name
, version
, value
,
1570 symsize
, type
, binding
,
1571 visibility
, nonvis
, only_if_ref
,
1577 else if (parameters
->target().get_size() == 64)
1579 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1580 return this->do_define_as_constant
<64>(name
, version
, value
,
1581 symsize
, type
, binding
,
1582 visibility
, nonvis
, only_if_ref
,
1592 // Define a symbol as a constant, sized version.
1596 Symbol_table::do_define_as_constant(
1598 const char* version
,
1599 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1600 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1602 elfcpp::STB binding
,
1603 elfcpp::STV visibility
,
1604 unsigned char nonvis
,
1606 bool force_override
)
1608 Sized_symbol
<size
>* sym
;
1609 Sized_symbol
<size
>* oldsym
;
1611 if (parameters
->target().is_big_endian())
1613 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1614 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1615 only_if_ref
, &oldsym
);
1622 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1623 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1624 only_if_ref
, &oldsym
);
1633 sym
->init_constant(name
, version
, value
, symsize
, type
, binding
, visibility
,
1638 // Version symbols are absolute symbols with name == version.
1639 // We don't want to force them to be local.
1640 if ((version
== NULL
1643 && (binding
== elfcpp::STB_LOCAL
1644 || this->version_script_
.symbol_is_local(name
)))
1645 this->force_local(sym
);
1646 else if (version
!= NULL
1647 && (name
!= version
|| value
!= 0))
1648 sym
->set_is_default();
1652 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
1653 this->override_with_special(oldsym
, sym
);
1658 // Define a set of symbols in output sections.
1661 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1662 const Define_symbol_in_section
* p
,
1665 for (int i
= 0; i
< count
; ++i
, ++p
)
1667 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1669 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
1670 p
->size
, p
->type
, p
->binding
,
1671 p
->visibility
, p
->nonvis
,
1672 p
->offset_is_from_end
,
1673 only_if_ref
|| p
->only_if_ref
);
1675 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1676 p
->binding
, p
->visibility
, p
->nonvis
,
1677 only_if_ref
|| p
->only_if_ref
,
1682 // Define a set of symbols in output segments.
1685 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1686 const Define_symbol_in_segment
* p
,
1689 for (int i
= 0; i
< count
; ++i
, ++p
)
1691 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1692 p
->segment_flags_set
,
1693 p
->segment_flags_clear
);
1695 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
1696 p
->size
, p
->type
, p
->binding
,
1697 p
->visibility
, p
->nonvis
,
1699 only_if_ref
|| p
->only_if_ref
);
1701 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1702 p
->binding
, p
->visibility
, p
->nonvis
,
1703 only_if_ref
|| p
->only_if_ref
,
1708 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1709 // symbol should be defined--typically a .dyn.bss section. VALUE is
1710 // the offset within POSD.
1714 Symbol_table::define_with_copy_reloc(
1715 Sized_symbol
<size
>* csym
,
1717 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
1719 gold_assert(csym
->is_from_dynobj());
1720 gold_assert(!csym
->is_copied_from_dynobj());
1721 Object
* object
= csym
->object();
1722 gold_assert(object
->is_dynamic());
1723 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1725 // Our copied variable has to override any variable in a shared
1727 elfcpp::STB binding
= csym
->binding();
1728 if (binding
== elfcpp::STB_WEAK
)
1729 binding
= elfcpp::STB_GLOBAL
;
1731 this->define_in_output_data(csym
->name(), csym
->version(),
1732 posd
, value
, csym
->symsize(),
1733 csym
->type(), binding
,
1734 csym
->visibility(), csym
->nonvis(),
1737 csym
->set_is_copied_from_dynobj();
1738 csym
->set_needs_dynsym_entry();
1740 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1742 // We have now defined all aliases, but we have not entered them all
1743 // in the copied_symbol_dynobjs_ map.
1744 if (csym
->has_alias())
1749 sym
= this->weak_aliases_
[sym
];
1752 gold_assert(sym
->output_data() == posd
);
1754 sym
->set_is_copied_from_dynobj();
1755 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1760 // SYM is defined using a COPY reloc. Return the dynamic object where
1761 // the original definition was found.
1764 Symbol_table::get_copy_source(const Symbol
* sym
) const
1766 gold_assert(sym
->is_copied_from_dynobj());
1767 Copied_symbol_dynobjs::const_iterator p
=
1768 this->copied_symbol_dynobjs_
.find(sym
);
1769 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1773 // Add any undefined symbols named on the command line.
1776 Symbol_table::add_undefined_symbols_from_command_line()
1778 if (parameters
->options().any_undefined())
1780 if (parameters
->target().get_size() == 32)
1782 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1783 this->do_add_undefined_symbols_from_command_line
<32>();
1788 else if (parameters
->target().get_size() == 64)
1790 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1791 this->do_add_undefined_symbols_from_command_line
<64>();
1803 Symbol_table::do_add_undefined_symbols_from_command_line()
1805 for (options::String_set::const_iterator p
=
1806 parameters
->options().undefined_begin();
1807 p
!= parameters
->options().undefined_end();
1810 const char* name
= p
->c_str();
1812 if (this->lookup(name
) != NULL
)
1815 const char* version
= NULL
;
1817 Sized_symbol
<size
>* sym
;
1818 Sized_symbol
<size
>* oldsym
;
1819 if (parameters
->target().is_big_endian())
1821 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1822 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1830 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1831 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1838 gold_assert(oldsym
== NULL
);
1840 sym
->init_undefined(name
, version
, elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1841 elfcpp::STV_DEFAULT
, 0);
1842 ++this->saw_undefined_
;
1846 // Set the dynamic symbol indexes. INDEX is the index of the first
1847 // global dynamic symbol. Pointers to the symbols are stored into the
1848 // vector SYMS. The names are added to DYNPOOL. This returns an
1849 // updated dynamic symbol index.
1852 Symbol_table::set_dynsym_indexes(unsigned int index
,
1853 std::vector
<Symbol
*>* syms
,
1854 Stringpool
* dynpool
,
1857 for (Symbol_table_type::iterator p
= this->table_
.begin();
1858 p
!= this->table_
.end();
1861 Symbol
* sym
= p
->second
;
1863 // Note that SYM may already have a dynamic symbol index, since
1864 // some symbols appear more than once in the symbol table, with
1865 // and without a version.
1867 if (!sym
->should_add_dynsym_entry())
1868 sym
->set_dynsym_index(-1U);
1869 else if (!sym
->has_dynsym_index())
1871 sym
->set_dynsym_index(index
);
1873 syms
->push_back(sym
);
1874 dynpool
->add(sym
->name(), false, NULL
);
1876 // Record any version information.
1877 if (sym
->version() != NULL
)
1878 versions
->record_version(this, dynpool
, sym
);
1882 // Finish up the versions. In some cases this may add new dynamic
1884 index
= versions
->finalize(this, index
, syms
);
1889 // Set the final values for all the symbols. The index of the first
1890 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1891 // file offset OFF. Add their names to POOL. Return the new file
1892 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1895 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
1896 size_t dyncount
, Stringpool
* pool
,
1897 unsigned int *plocal_symcount
)
1901 gold_assert(*plocal_symcount
!= 0);
1902 this->first_global_index_
= *plocal_symcount
;
1904 this->dynamic_offset_
= dynoff
;
1905 this->first_dynamic_global_index_
= dyn_global_index
;
1906 this->dynamic_count_
= dyncount
;
1908 if (parameters
->target().get_size() == 32)
1910 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1911 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
1916 else if (parameters
->target().get_size() == 64)
1918 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1919 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
1927 // Now that we have the final symbol table, we can reliably note
1928 // which symbols should get warnings.
1929 this->warnings_
.note_warnings(this);
1934 // SYM is going into the symbol table at *PINDEX. Add the name to
1935 // POOL, update *PINDEX and *POFF.
1939 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
1940 unsigned int* pindex
, off_t
* poff
)
1942 sym
->set_symtab_index(*pindex
);
1943 pool
->add(sym
->name(), false, NULL
);
1945 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
1948 // Set the final value for all the symbols. This is called after
1949 // Layout::finalize, so all the output sections have their final
1954 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
1955 unsigned int* plocal_symcount
)
1957 off
= align_address(off
, size
>> 3);
1958 this->offset_
= off
;
1960 unsigned int index
= *plocal_symcount
;
1961 const unsigned int orig_index
= index
;
1963 // First do all the symbols which have been forced to be local, as
1964 // they must appear before all global symbols.
1965 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
1966 p
!= this->forced_locals_
.end();
1970 gold_assert(sym
->is_forced_local());
1971 if (this->sized_finalize_symbol
<size
>(sym
))
1973 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
1978 // Now do all the remaining symbols.
1979 for (Symbol_table_type::iterator p
= this->table_
.begin();
1980 p
!= this->table_
.end();
1983 Symbol
* sym
= p
->second
;
1984 if (this->sized_finalize_symbol
<size
>(sym
))
1985 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
1988 this->output_count_
= index
- orig_index
;
1993 // Finalize the symbol SYM. This returns true if the symbol should be
1994 // added to the symbol table, false otherwise.
1998 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
2000 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2002 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
2004 // The default version of a symbol may appear twice in the symbol
2005 // table. We only need to finalize it once.
2006 if (sym
->has_symtab_index())
2011 gold_assert(!sym
->has_symtab_index());
2012 sym
->set_symtab_index(-1U);
2013 gold_assert(sym
->dynsym_index() == -1U);
2019 switch (sym
->source())
2021 case Symbol::FROM_OBJECT
:
2024 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2026 // FIXME: We need some target specific support here.
2028 && shndx
!= elfcpp::SHN_ABS
2029 && shndx
!= elfcpp::SHN_COMMON
)
2031 gold_error(_("%s: unsupported symbol section 0x%x"),
2032 sym
->demangled_name().c_str(), shndx
);
2033 shndx
= elfcpp::SHN_UNDEF
;
2036 Object
* symobj
= sym
->object();
2037 if (symobj
->is_dynamic())
2040 shndx
= elfcpp::SHN_UNDEF
;
2042 else if (shndx
== elfcpp::SHN_UNDEF
)
2044 else if (!is_ordinary
2045 && (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
))
2046 value
= sym
->value();
2049 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2050 Output_section
* os
= relobj
->output_section(shndx
);
2054 sym
->set_symtab_index(-1U);
2055 gold_assert(sym
->dynsym_index() == -1U);
2059 uint64_t secoff64
= relobj
->output_section_offset(shndx
);
2060 Value_type secoff
= convert_types
<Value_type
, uint64_t>(secoff64
);
2061 if (sym
->type() == elfcpp::STT_TLS
)
2062 value
= sym
->value() + os
->tls_offset() + secoff
;
2064 value
= sym
->value() + os
->address() + secoff
;
2069 case Symbol::IN_OUTPUT_DATA
:
2071 Output_data
* od
= sym
->output_data();
2072 value
= sym
->value();
2073 if (sym
->type() != elfcpp::STT_TLS
)
2074 value
+= od
->address();
2077 Output_section
* os
= od
->output_section();
2078 gold_assert(os
!= NULL
);
2079 value
+= os
->tls_offset() + (od
->address() - os
->address());
2081 if (sym
->offset_is_from_end())
2082 value
+= od
->data_size();
2086 case Symbol::IN_OUTPUT_SEGMENT
:
2088 Output_segment
* os
= sym
->output_segment();
2089 value
= sym
->value();
2090 if (sym
->type() != elfcpp::STT_TLS
)
2091 value
+= os
->vaddr();
2092 switch (sym
->offset_base())
2094 case Symbol::SEGMENT_START
:
2096 case Symbol::SEGMENT_END
:
2097 value
+= os
->memsz();
2099 case Symbol::SEGMENT_BSS
:
2100 value
+= os
->filesz();
2108 case Symbol::IS_CONSTANT
:
2109 value
= sym
->value();
2112 case Symbol::IS_UNDEFINED
:
2120 sym
->set_value(value
);
2122 if (parameters
->options().strip_all())
2124 sym
->set_symtab_index(-1U);
2131 // Write out the global symbols.
2134 Symbol_table::write_globals(const Input_objects
* input_objects
,
2135 const Stringpool
* sympool
,
2136 const Stringpool
* dynpool
,
2137 Output_symtab_xindex
* symtab_xindex
,
2138 Output_symtab_xindex
* dynsym_xindex
,
2139 Output_file
* of
) const
2141 switch (parameters
->size_and_endianness())
2143 #ifdef HAVE_TARGET_32_LITTLE
2144 case Parameters::TARGET_32_LITTLE
:
2145 this->sized_write_globals
<32, false>(input_objects
, sympool
,
2146 dynpool
, symtab_xindex
,
2150 #ifdef HAVE_TARGET_32_BIG
2151 case Parameters::TARGET_32_BIG
:
2152 this->sized_write_globals
<32, true>(input_objects
, sympool
,
2153 dynpool
, symtab_xindex
,
2157 #ifdef HAVE_TARGET_64_LITTLE
2158 case Parameters::TARGET_64_LITTLE
:
2159 this->sized_write_globals
<64, false>(input_objects
, sympool
,
2160 dynpool
, symtab_xindex
,
2164 #ifdef HAVE_TARGET_64_BIG
2165 case Parameters::TARGET_64_BIG
:
2166 this->sized_write_globals
<64, true>(input_objects
, sympool
,
2167 dynpool
, symtab_xindex
,
2176 // Write out the global symbols.
2178 template<int size
, bool big_endian
>
2180 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
2181 const Stringpool
* sympool
,
2182 const Stringpool
* dynpool
,
2183 Output_symtab_xindex
* symtab_xindex
,
2184 Output_symtab_xindex
* dynsym_xindex
,
2185 Output_file
* of
) const
2187 const Target
& target
= parameters
->target();
2189 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2191 const unsigned int output_count
= this->output_count_
;
2192 const section_size_type oview_size
= output_count
* sym_size
;
2193 const unsigned int first_global_index
= this->first_global_index_
;
2194 unsigned char* psyms
;
2195 if (this->offset_
== 0 || output_count
== 0)
2198 psyms
= of
->get_output_view(this->offset_
, oview_size
);
2200 const unsigned int dynamic_count
= this->dynamic_count_
;
2201 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
2202 const unsigned int first_dynamic_global_index
=
2203 this->first_dynamic_global_index_
;
2204 unsigned char* dynamic_view
;
2205 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
2206 dynamic_view
= NULL
;
2208 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
2210 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
2211 p
!= this->table_
.end();
2214 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
2216 // Possibly warn about unresolved symbols in shared libraries.
2217 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
2219 unsigned int sym_index
= sym
->symtab_index();
2220 unsigned int dynsym_index
;
2221 if (dynamic_view
== NULL
)
2224 dynsym_index
= sym
->dynsym_index();
2226 if (sym_index
== -1U && dynsym_index
== -1U)
2228 // This symbol is not included in the output file.
2233 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
2234 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
2235 switch (sym
->source())
2237 case Symbol::FROM_OBJECT
:
2240 unsigned int in_shndx
= sym
->shndx(&is_ordinary
);
2242 // FIXME: We need some target specific support here.
2244 && in_shndx
!= elfcpp::SHN_ABS
2245 && in_shndx
!= elfcpp::SHN_COMMON
)
2247 gold_error(_("%s: unsupported symbol section 0x%x"),
2248 sym
->demangled_name().c_str(), in_shndx
);
2253 Object
* symobj
= sym
->object();
2254 if (symobj
->is_dynamic())
2256 if (sym
->needs_dynsym_value())
2257 dynsym_value
= target
.dynsym_value(sym
);
2258 shndx
= elfcpp::SHN_UNDEF
;
2260 else if (in_shndx
== elfcpp::SHN_UNDEF
2262 && (in_shndx
== elfcpp::SHN_ABS
2263 || in_shndx
== elfcpp::SHN_COMMON
)))
2267 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2268 Output_section
* os
= relobj
->output_section(in_shndx
);
2269 gold_assert(os
!= NULL
);
2270 shndx
= os
->out_shndx();
2272 if (shndx
>= elfcpp::SHN_LORESERVE
)
2274 if (sym_index
!= -1U)
2275 symtab_xindex
->add(sym_index
, shndx
);
2276 if (dynsym_index
!= -1U)
2277 dynsym_xindex
->add(dynsym_index
, shndx
);
2278 shndx
= elfcpp::SHN_XINDEX
;
2281 // In object files symbol values are section
2283 if (parameters
->options().relocatable())
2284 sym_value
-= os
->address();
2290 case Symbol::IN_OUTPUT_DATA
:
2291 shndx
= sym
->output_data()->out_shndx();
2292 if (shndx
>= elfcpp::SHN_LORESERVE
)
2294 if (sym_index
!= -1U)
2295 symtab_xindex
->add(sym_index
, shndx
);
2296 if (dynsym_index
!= -1U)
2297 dynsym_xindex
->add(dynsym_index
, shndx
);
2298 shndx
= elfcpp::SHN_XINDEX
;
2302 case Symbol::IN_OUTPUT_SEGMENT
:
2303 shndx
= elfcpp::SHN_ABS
;
2306 case Symbol::IS_CONSTANT
:
2307 shndx
= elfcpp::SHN_ABS
;
2310 case Symbol::IS_UNDEFINED
:
2311 shndx
= elfcpp::SHN_UNDEF
;
2318 if (sym_index
!= -1U)
2320 sym_index
-= first_global_index
;
2321 gold_assert(sym_index
< output_count
);
2322 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
2323 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
2327 if (dynsym_index
!= -1U)
2329 dynsym_index
-= first_dynamic_global_index
;
2330 gold_assert(dynsym_index
< dynamic_count
);
2331 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
2332 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
2337 of
->write_output_view(this->offset_
, oview_size
, psyms
);
2338 if (dynamic_view
!= NULL
)
2339 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
2342 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2343 // strtab holding the name.
2345 template<int size
, bool big_endian
>
2347 Symbol_table::sized_write_symbol(
2348 Sized_symbol
<size
>* sym
,
2349 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
2351 const Stringpool
* pool
,
2352 unsigned char* p
) const
2354 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
2355 osym
.put_st_name(pool
->get_offset(sym
->name()));
2356 osym
.put_st_value(value
);
2357 // Use a symbol size of zero for undefined symbols from shared libraries.
2358 if (shndx
== elfcpp::SHN_UNDEF
&& sym
->is_from_dynobj())
2359 osym
.put_st_size(0);
2361 osym
.put_st_size(sym
->symsize());
2362 // A version script may have overridden the default binding.
2363 if (sym
->is_forced_local())
2364 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, sym
->type()));
2366 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
2367 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
2368 osym
.put_st_shndx(shndx
);
2371 // Check for unresolved symbols in shared libraries. This is
2372 // controlled by the --allow-shlib-undefined option.
2374 // We only warn about libraries for which we have seen all the
2375 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2376 // which were not seen in this link. If we didn't see a DT_NEEDED
2377 // entry, we aren't going to be able to reliably report whether the
2378 // symbol is undefined.
2380 // We also don't warn about libraries found in the system library
2381 // directory (the directory were we find libc.so); we assume that
2382 // those libraries are OK. This heuristic avoids problems in
2383 // GNU/Linux, in which -ldl can have undefined references satisfied by
2387 Symbol_table::warn_about_undefined_dynobj_symbol(
2388 const Input_objects
* input_objects
,
2392 if (sym
->source() == Symbol::FROM_OBJECT
2393 && sym
->object()->is_dynamic()
2394 && sym
->shndx(&dummy
) == elfcpp::SHN_UNDEF
2395 && sym
->binding() != elfcpp::STB_WEAK
2396 && !parameters
->options().allow_shlib_undefined()
2397 && !parameters
->target().is_defined_by_abi(sym
)
2398 && !input_objects
->found_in_system_library_directory(sym
->object()))
2400 // A very ugly cast.
2401 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2402 if (!dynobj
->has_unknown_needed_entries())
2405 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2406 sym
->object()->name().c_str(),
2407 sym
->demangled_name().c_str(),
2410 gold_error(_("%s: undefined reference to '%s'"),
2411 sym
->object()->name().c_str(),
2412 sym
->demangled_name().c_str());
2417 // Write out a section symbol. Return the update offset.
2420 Symbol_table::write_section_symbol(const Output_section
*os
,
2421 Output_symtab_xindex
* symtab_xindex
,
2425 switch (parameters
->size_and_endianness())
2427 #ifdef HAVE_TARGET_32_LITTLE
2428 case Parameters::TARGET_32_LITTLE
:
2429 this->sized_write_section_symbol
<32, false>(os
, symtab_xindex
, of
,
2433 #ifdef HAVE_TARGET_32_BIG
2434 case Parameters::TARGET_32_BIG
:
2435 this->sized_write_section_symbol
<32, true>(os
, symtab_xindex
, of
,
2439 #ifdef HAVE_TARGET_64_LITTLE
2440 case Parameters::TARGET_64_LITTLE
:
2441 this->sized_write_section_symbol
<64, false>(os
, symtab_xindex
, of
,
2445 #ifdef HAVE_TARGET_64_BIG
2446 case Parameters::TARGET_64_BIG
:
2447 this->sized_write_section_symbol
<64, true>(os
, symtab_xindex
, of
,
2456 // Write out a section symbol, specialized for size and endianness.
2458 template<int size
, bool big_endian
>
2460 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2461 Output_symtab_xindex
* symtab_xindex
,
2465 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2467 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2469 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2470 osym
.put_st_name(0);
2471 osym
.put_st_value(os
->address());
2472 osym
.put_st_size(0);
2473 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2474 elfcpp::STT_SECTION
));
2475 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2477 unsigned int shndx
= os
->out_shndx();
2478 if (shndx
>= elfcpp::SHN_LORESERVE
)
2480 symtab_xindex
->add(os
->symtab_index(), shndx
);
2481 shndx
= elfcpp::SHN_XINDEX
;
2483 osym
.put_st_shndx(shndx
);
2485 of
->write_output_view(offset
, sym_size
, pov
);
2488 // Print statistical information to stderr. This is used for --stats.
2491 Symbol_table::print_stats() const
2493 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2494 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2495 program_name
, this->table_
.size(), this->table_
.bucket_count());
2497 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2498 program_name
, this->table_
.size());
2500 this->namepool_
.print_stats("symbol table stringpool");
2503 // We check for ODR violations by looking for symbols with the same
2504 // name for which the debugging information reports that they were
2505 // defined in different source locations. When comparing the source
2506 // location, we consider instances with the same base filename and
2507 // line number to be the same. This is because different object
2508 // files/shared libraries can include the same header file using
2509 // different paths, and we don't want to report an ODR violation in
2512 // This struct is used to compare line information, as returned by
2513 // Dwarf_line_info::one_addr2line. It implements a < comparison
2514 // operator used with std::set.
2516 struct Odr_violation_compare
2519 operator()(const std::string
& s1
, const std::string
& s2
) const
2521 std::string::size_type pos1
= s1
.rfind('/');
2522 std::string::size_type pos2
= s2
.rfind('/');
2523 if (pos1
== std::string::npos
2524 || pos2
== std::string::npos
)
2526 return s1
.compare(pos1
, std::string::npos
,
2527 s2
, pos2
, std::string::npos
) < 0;
2531 // Check candidate_odr_violations_ to find symbols with the same name
2532 // but apparently different definitions (different source-file/line-no).
2535 Symbol_table::detect_odr_violations(const Task
* task
,
2536 const char* output_file_name
) const
2538 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2539 it
!= candidate_odr_violations_
.end();
2542 const char* symbol_name
= it
->first
;
2543 // We use a sorted set so the output is deterministic.
2544 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2546 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2547 locs
= it
->second
.begin();
2548 locs
!= it
->second
.end();
2551 // We need to lock the object in order to read it. This
2552 // means that we have to run in a singleton Task. If we
2553 // want to run this in a general Task for better
2554 // performance, we will need one Task for object, plus
2555 // appropriate locking to ensure that we don't conflict with
2556 // other uses of the object. Also note, one_addr2line is not
2557 // currently thread-safe.
2558 Task_lock_obj
<Object
> tl(task
, locs
->object
);
2559 // 16 is the size of the object-cache that one_addr2line should use.
2560 std::string lineno
= Dwarf_line_info::one_addr2line(
2561 locs
->object
, locs
->shndx
, locs
->offset
, 16);
2562 if (!lineno
.empty())
2563 line_nums
.insert(lineno
);
2566 if (line_nums
.size() > 1)
2568 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2569 "places (possible ODR violation):"),
2570 output_file_name
, demangle(symbol_name
).c_str());
2571 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
2572 it2
!= line_nums
.end();
2574 fprintf(stderr
, " %s\n", it2
->c_str());
2577 // We only call one_addr2line() in this function, so we can clear its cache.
2578 Dwarf_line_info::clear_addr2line_cache();
2581 // Warnings functions.
2583 // Add a new warning.
2586 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
2587 const std::string
& warning
)
2589 name
= symtab
->canonicalize_name(name
);
2590 this->warnings_
[name
].set(obj
, warning
);
2593 // Look through the warnings and mark the symbols for which we should
2594 // warn. This is called during Layout::finalize when we know the
2595 // sources for all the symbols.
2598 Warnings::note_warnings(Symbol_table
* symtab
)
2600 for (Warning_table::iterator p
= this->warnings_
.begin();
2601 p
!= this->warnings_
.end();
2604 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2606 && sym
->source() == Symbol::FROM_OBJECT
2607 && sym
->object() == p
->second
.object
)
2608 sym
->set_has_warning();
2612 // Issue a warning. This is called when we see a relocation against a
2613 // symbol for which has a warning.
2615 template<int size
, bool big_endian
>
2617 Warnings::issue_warning(const Symbol
* sym
,
2618 const Relocate_info
<size
, big_endian
>* relinfo
,
2619 size_t relnum
, off_t reloffset
) const
2621 gold_assert(sym
->has_warning());
2622 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2623 gold_assert(p
!= this->warnings_
.end());
2624 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2625 "%s", p
->second
.text
.c_str());
2628 // Instantiate the templates we need. We could use the configure
2629 // script to restrict this to only the ones needed for implemented
2632 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2635 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2638 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2641 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2644 #ifdef HAVE_TARGET_32_LITTLE
2647 Symbol_table::add_from_relobj
<32, false>(
2648 Sized_relobj
<32, false>* relobj
,
2649 const unsigned char* syms
,
2651 size_t symndx_offset
,
2652 const char* sym_names
,
2653 size_t sym_name_size
,
2654 Sized_relobj
<32, true>::Symbols
* sympointers
,
2658 #ifdef HAVE_TARGET_32_BIG
2661 Symbol_table::add_from_relobj
<32, true>(
2662 Sized_relobj
<32, true>* relobj
,
2663 const unsigned char* syms
,
2665 size_t symndx_offset
,
2666 const char* sym_names
,
2667 size_t sym_name_size
,
2668 Sized_relobj
<32, false>::Symbols
* sympointers
,
2672 #ifdef HAVE_TARGET_64_LITTLE
2675 Symbol_table::add_from_relobj
<64, false>(
2676 Sized_relobj
<64, false>* relobj
,
2677 const unsigned char* syms
,
2679 size_t symndx_offset
,
2680 const char* sym_names
,
2681 size_t sym_name_size
,
2682 Sized_relobj
<64, true>::Symbols
* sympointers
,
2686 #ifdef HAVE_TARGET_64_BIG
2689 Symbol_table::add_from_relobj
<64, true>(
2690 Sized_relobj
<64, true>* relobj
,
2691 const unsigned char* syms
,
2693 size_t symndx_offset
,
2694 const char* sym_names
,
2695 size_t sym_name_size
,
2696 Sized_relobj
<64, false>::Symbols
* sympointers
,
2700 #ifdef HAVE_TARGET_32_LITTLE
2703 Symbol_table::add_from_dynobj
<32, false>(
2704 Sized_dynobj
<32, false>* dynobj
,
2705 const unsigned char* syms
,
2707 const char* sym_names
,
2708 size_t sym_name_size
,
2709 const unsigned char* versym
,
2711 const std::vector
<const char*>* version_map
,
2712 Sized_relobj
<32, false>::Symbols
* sympointers
,
2716 #ifdef HAVE_TARGET_32_BIG
2719 Symbol_table::add_from_dynobj
<32, true>(
2720 Sized_dynobj
<32, true>* dynobj
,
2721 const unsigned char* syms
,
2723 const char* sym_names
,
2724 size_t sym_name_size
,
2725 const unsigned char* versym
,
2727 const std::vector
<const char*>* version_map
,
2728 Sized_relobj
<32, true>::Symbols
* sympointers
,
2732 #ifdef HAVE_TARGET_64_LITTLE
2735 Symbol_table::add_from_dynobj
<64, false>(
2736 Sized_dynobj
<64, false>* dynobj
,
2737 const unsigned char* syms
,
2739 const char* sym_names
,
2740 size_t sym_name_size
,
2741 const unsigned char* versym
,
2743 const std::vector
<const char*>* version_map
,
2744 Sized_relobj
<64, false>::Symbols
* sympointers
,
2748 #ifdef HAVE_TARGET_64_BIG
2751 Symbol_table::add_from_dynobj
<64, true>(
2752 Sized_dynobj
<64, true>* dynobj
,
2753 const unsigned char* syms
,
2755 const char* sym_names
,
2756 size_t sym_name_size
,
2757 const unsigned char* versym
,
2759 const std::vector
<const char*>* version_map
,
2760 Sized_relobj
<64, true>::Symbols
* sympointers
,
2764 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2767 Symbol_table::define_with_copy_reloc
<32>(
2768 Sized_symbol
<32>* sym
,
2770 elfcpp::Elf_types
<32>::Elf_Addr value
);
2773 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2776 Symbol_table::define_with_copy_reloc
<64>(
2777 Sized_symbol
<64>* sym
,
2779 elfcpp::Elf_types
<64>::Elf_Addr value
);
2782 #ifdef HAVE_TARGET_32_LITTLE
2785 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2786 const Relocate_info
<32, false>* relinfo
,
2787 size_t relnum
, off_t reloffset
) const;
2790 #ifdef HAVE_TARGET_32_BIG
2793 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2794 const Relocate_info
<32, true>* relinfo
,
2795 size_t relnum
, off_t reloffset
) const;
2798 #ifdef HAVE_TARGET_64_LITTLE
2801 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2802 const Relocate_info
<64, false>* relinfo
,
2803 size_t relnum
, off_t reloffset
) const;
2806 #ifdef HAVE_TARGET_64_BIG
2809 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2810 const Relocate_info
<64, true>* relinfo
,
2811 size_t relnum
, off_t reloffset
) const;
2814 } // End namespace gold.