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->dynsym_index_
= -1U;
178 this->source_
= IS_UNDEFINED
;
179 this->in_reg_
= true;
182 // Allocate a common symbol in the base.
185 Symbol::allocate_base_common(Output_data
* od
)
187 gold_assert(this->is_common());
188 this->source_
= IN_OUTPUT_DATA
;
189 this->u_
.in_output_data
.output_data
= od
;
190 this->u_
.in_output_data
.offset_is_from_end
= false;
193 // Initialize the fields in Sized_symbol for SYM in OBJECT.
196 template<bool big_endian
>
198 Sized_symbol
<size
>::init_object(const char* name
, const char* version
,
200 const elfcpp::Sym
<size
, big_endian
>& sym
,
201 unsigned int st_shndx
, bool is_ordinary
)
203 this->init_base_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
204 this->value_
= sym
.get_st_value();
205 this->symsize_
= sym
.get_st_size();
208 // Initialize the fields in Sized_symbol for a symbol defined in an
213 Sized_symbol
<size
>::init_output_data(const char* name
, const char* version
,
214 Output_data
* od
, Value_type value
,
215 Size_type symsize
, elfcpp::STT type
,
217 elfcpp::STV visibility
,
218 unsigned char nonvis
,
219 bool offset_is_from_end
)
221 this->init_base_output_data(name
, version
, od
, type
, binding
, visibility
,
222 nonvis
, offset_is_from_end
);
223 this->value_
= value
;
224 this->symsize_
= symsize
;
227 // Initialize the fields in Sized_symbol for a symbol defined in an
232 Sized_symbol
<size
>::init_output_segment(const char* name
, const char* version
,
233 Output_segment
* os
, Value_type value
,
234 Size_type symsize
, elfcpp::STT type
,
236 elfcpp::STV visibility
,
237 unsigned char nonvis
,
238 Segment_offset_base offset_base
)
240 this->init_base_output_segment(name
, version
, os
, type
, binding
, visibility
,
241 nonvis
, offset_base
);
242 this->value_
= value
;
243 this->symsize_
= symsize
;
246 // Initialize the fields in Sized_symbol for a symbol defined as a
251 Sized_symbol
<size
>::init_constant(const char* name
, const char* version
,
252 Value_type value
, Size_type symsize
,
253 elfcpp::STT type
, elfcpp::STB binding
,
254 elfcpp::STV visibility
, unsigned char nonvis
)
256 this->init_base_constant(name
, version
, type
, binding
, visibility
, nonvis
);
257 this->value_
= value
;
258 this->symsize_
= symsize
;
261 // Initialize the fields in Sized_symbol for an undefined symbol.
265 Sized_symbol
<size
>::init_undefined(const char* name
, const char* version
,
266 elfcpp::STT type
, elfcpp::STB binding
,
267 elfcpp::STV visibility
, unsigned char nonvis
)
269 this->init_base_undefined(name
, version
, type
, binding
, visibility
, nonvis
);
274 // Allocate a common symbol.
278 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
280 this->allocate_base_common(od
);
281 this->value_
= value
;
284 // Return true if this symbol should be added to the dynamic symbol
288 Symbol::should_add_dynsym_entry() const
290 // If the symbol is used by a dynamic relocation, we need to add it.
291 if (this->needs_dynsym_entry())
294 // If the symbol was forced local in a version script, do not add it.
295 if (this->is_forced_local())
298 // If exporting all symbols or building a shared library,
299 // and the symbol is defined in a regular object and is
300 // externally visible, we need to add it.
301 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
302 && !this->is_from_dynobj()
303 && this->is_externally_visible())
309 // Return true if the final value of this symbol is known at link
313 Symbol::final_value_is_known() const
315 // If we are not generating an executable, then no final values are
316 // known, since they will change at runtime.
317 if (parameters
->options().shared() || parameters
->options().relocatable())
320 // If the symbol is not from an object file, and is not undefined,
321 // then it is defined, and known.
322 if (this->source_
!= FROM_OBJECT
)
324 if (this->source_
!= IS_UNDEFINED
)
329 // If the symbol is from a dynamic object, then the final value
331 if (this->object()->is_dynamic())
334 // If the symbol is not undefined (it is defined or common),
335 // then the final value is known.
336 if (!this->is_undefined())
340 // If the symbol is undefined, then whether the final value is known
341 // depends on whether we are doing a static link. If we are doing a
342 // dynamic link, then the final value could be filled in at runtime.
343 // This could reasonably be the case for a weak undefined symbol.
344 return parameters
->doing_static_link();
347 // Return the output section where this symbol is defined.
350 Symbol::output_section() const
352 switch (this->source_
)
356 unsigned int shndx
= this->u_
.from_object
.shndx
;
357 if (shndx
!= elfcpp::SHN_UNDEF
&& this->is_ordinary_shndx_
)
359 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
360 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
361 return relobj
->output_section(shndx
);
367 return this->u_
.in_output_data
.output_data
->output_section();
369 case IN_OUTPUT_SEGMENT
:
379 // Set the symbol's output section. This is used for symbols defined
380 // in scripts. This should only be called after the symbol table has
384 Symbol::set_output_section(Output_section
* os
)
386 switch (this->source_
)
390 gold_assert(this->output_section() == os
);
393 this->source_
= IN_OUTPUT_DATA
;
394 this->u_
.in_output_data
.output_data
= os
;
395 this->u_
.in_output_data
.offset_is_from_end
= false;
397 case IN_OUTPUT_SEGMENT
:
404 // Class Symbol_table.
406 Symbol_table::Symbol_table(unsigned int count
,
407 const Version_script_info
& version_script
)
408 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
409 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
410 version_script_(version_script
)
412 namepool_
.reserve(count
);
415 Symbol_table::~Symbol_table()
419 // The hash function. The key values are Stringpool keys.
422 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
424 return key
.first
^ key
.second
;
427 // The symbol table key equality function. This is called with
431 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
432 const Symbol_table_key
& k2
) const
434 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
437 // Make TO a symbol which forwards to FROM.
440 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
442 gold_assert(from
!= to
);
443 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
444 this->forwarders_
[from
] = to
;
445 from
->set_forwarder();
448 // Resolve the forwards from FROM, returning the real symbol.
451 Symbol_table::resolve_forwards(const Symbol
* from
) const
453 gold_assert(from
->is_forwarder());
454 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
455 this->forwarders_
.find(from
);
456 gold_assert(p
!= this->forwarders_
.end());
460 // Look up a symbol by name.
463 Symbol_table::lookup(const char* name
, const char* version
) const
465 Stringpool::Key name_key
;
466 name
= this->namepool_
.find(name
, &name_key
);
470 Stringpool::Key version_key
= 0;
473 version
= this->namepool_
.find(version
, &version_key
);
478 Symbol_table_key
key(name_key
, version_key
);
479 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
480 if (p
== this->table_
.end())
485 // Resolve a Symbol with another Symbol. This is only used in the
486 // unusual case where there are references to both an unversioned
487 // symbol and a symbol with a version, and we then discover that that
488 // version is the default version. Because this is unusual, we do
489 // this the slow way, by converting back to an ELF symbol.
491 template<int size
, bool big_endian
>
493 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
)
495 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
496 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
497 // We don't bother to set the st_name or the st_shndx field.
498 esym
.put_st_value(from
->value());
499 esym
.put_st_size(from
->symsize());
500 esym
.put_st_info(from
->binding(), from
->type());
501 esym
.put_st_other(from
->visibility(), from
->nonvis());
503 unsigned int shndx
= from
->shndx(&is_ordinary
);
504 this->resolve(to
, esym
.sym(), shndx
, is_ordinary
, shndx
, from
->object(),
512 // Record that a symbol is forced to be local by a version script.
515 Symbol_table::force_local(Symbol
* sym
)
517 if (!sym
->is_defined() && !sym
->is_common())
519 if (sym
->is_forced_local())
521 // We already got this one.
524 sym
->set_is_forced_local();
525 this->forced_locals_
.push_back(sym
);
528 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
529 // is only called for undefined symbols, when at least one --wrap
533 Symbol_table::wrap_symbol(Object
* object
, const char* name
,
534 Stringpool::Key
* name_key
)
536 // For some targets, we need to ignore a specific character when
537 // wrapping, and add it back later.
539 if (name
[0] == object
->target()->wrap_char())
545 if (parameters
->options().is_wrap(name
))
547 // Turn NAME into __wrap_NAME.
554 // This will give us both the old and new name in NAMEPOOL_, but
555 // that is OK. Only the versions we need will wind up in the
556 // real string table in the output file.
557 return this->namepool_
.add(s
.c_str(), true, name_key
);
560 const char* const real_prefix
= "__real_";
561 const size_t real_prefix_length
= strlen(real_prefix
);
562 if (strncmp(name
, real_prefix
, real_prefix_length
) == 0
563 && parameters
->options().is_wrap(name
+ real_prefix_length
))
565 // Turn __real_NAME into NAME.
569 s
+= name
+ real_prefix_length
;
570 return this->namepool_
.add(s
.c_str(), true, name_key
);
576 // Add one symbol from OBJECT to the symbol table. NAME is symbol
577 // name and VERSION is the version; both are canonicalized. DEF is
578 // whether this is the default version. ST_SHNDX is the symbol's
579 // section index; IS_ORDINARY is whether this is a normal section
580 // rather than a special code.
582 // If DEF is true, then this is the definition of a default version of
583 // a symbol. That means that any lookup of NAME/NULL and any lookup
584 // of NAME/VERSION should always return the same symbol. This is
585 // obvious for references, but in particular we want to do this for
586 // definitions: overriding NAME/NULL should also override
587 // NAME/VERSION. If we don't do that, it would be very hard to
588 // override functions in a shared library which uses versioning.
590 // We implement this by simply making both entries in the hash table
591 // point to the same Symbol structure. That is easy enough if this is
592 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
593 // that we have seen both already, in which case they will both have
594 // independent entries in the symbol table. We can't simply change
595 // the symbol table entry, because we have pointers to the entries
596 // attached to the object files. So we mark the entry attached to the
597 // object file as a forwarder, and record it in the forwarders_ map.
598 // Note that entries in the hash table will never be marked as
601 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
602 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
603 // for a special section code. ST_SHNDX may be modified if the symbol
604 // is defined in a section being discarded.
606 template<int size
, bool big_endian
>
608 Symbol_table::add_from_object(Object
* object
,
610 Stringpool::Key name_key
,
612 Stringpool::Key version_key
,
614 const elfcpp::Sym
<size
, big_endian
>& sym
,
615 unsigned int st_shndx
,
617 unsigned int orig_st_shndx
)
619 // Print a message if this symbol is being traced.
620 if (parameters
->options().is_trace_symbol(name
))
622 if (orig_st_shndx
== elfcpp::SHN_UNDEF
)
623 gold_info(_("%s: reference to %s"), object
->name().c_str(), name
);
625 gold_info(_("%s: definition of %s"), object
->name().c_str(), name
);
628 // For an undefined symbol, we may need to adjust the name using
630 if (orig_st_shndx
== elfcpp::SHN_UNDEF
631 && parameters
->options().any_wrap())
633 const char* wrap_name
= this->wrap_symbol(object
, name
, &name_key
);
634 if (wrap_name
!= name
)
636 // If we see a reference to malloc with version GLIBC_2.0,
637 // and we turn it into a reference to __wrap_malloc, then we
638 // discard the version number. Otherwise the user would be
639 // required to specify the correct version for
647 Symbol
* const snull
= NULL
;
648 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
649 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
652 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
653 std::make_pair(this->table_
.end(), false);
656 const Stringpool::Key vnull_key
= 0;
657 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
662 // ins.first: an iterator, which is a pointer to a pair.
663 // ins.first->first: the key (a pair of name and version).
664 // ins.first->second: the value (Symbol*).
665 // ins.second: true if new entry was inserted, false if not.
667 Sized_symbol
<size
>* ret
;
672 // We already have an entry for NAME/VERSION.
673 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
674 gold_assert(ret
!= NULL
);
676 was_undefined
= ret
->is_undefined();
677 was_common
= ret
->is_common();
679 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
686 // This is the first time we have seen NAME/NULL. Make
687 // NAME/NULL point to NAME/VERSION.
688 insdef
.first
->second
= ret
;
690 else if (insdef
.first
->second
!= ret
)
692 // This is the unfortunate case where we already have
693 // entries for both NAME/VERSION and NAME/NULL. We now
694 // see a symbol NAME/VERSION where VERSION is the
695 // default version. We have already resolved this new
696 // symbol with the existing NAME/VERSION symbol.
698 // It's possible that NAME/NULL and NAME/VERSION are
699 // both defined in regular objects. This can only
700 // happen if one object file defines foo and another
701 // defines foo@@ver. This is somewhat obscure, but we
702 // call it a multiple definition error.
704 // It's possible that NAME/NULL actually has a version,
705 // in which case it won't be the same as VERSION. This
706 // happens with ver_test_7.so in the testsuite for the
707 // symbol t2_2. We see t2_2@@VER2, so we define both
708 // t2_2/VER2 and t2_2/NULL. We then see an unadorned
709 // t2_2 in an object file and give it version VER1 from
710 // the version script. This looks like a default
711 // definition for VER1, so it looks like we should merge
712 // t2_2/NULL with t2_2/VER1. That doesn't make sense,
713 // but it's not obvious that this is an error, either.
716 // If one of the symbols has non-default visibility, and
717 // the other is defined in a shared object, then they
718 // are different symbols.
720 // Otherwise, we just resolve the symbols as though they
723 if (insdef
.first
->second
->version() != NULL
)
725 gold_assert(insdef
.first
->second
->version() != version
);
728 else if (ret
->visibility() != elfcpp::STV_DEFAULT
729 && insdef
.first
->second
->is_from_dynobj())
731 else if (insdef
.first
->second
->visibility() != elfcpp::STV_DEFAULT
732 && ret
->is_from_dynobj())
736 const Sized_symbol
<size
>* sym2
;
737 sym2
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
738 Symbol_table::resolve
<size
, big_endian
>(ret
, sym2
);
739 this->make_forwarder(insdef
.first
->second
, ret
);
740 insdef
.first
->second
= ret
;
749 // This is the first time we have seen NAME/VERSION.
750 gold_assert(ins
.first
->second
== NULL
);
752 if (def
&& !insdef
.second
)
754 // We already have an entry for NAME/NULL. If we override
755 // it, then change it to NAME/VERSION.
756 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
758 was_undefined
= ret
->is_undefined();
759 was_common
= ret
->is_common();
761 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
763 ins
.first
->second
= ret
;
767 was_undefined
= false;
770 Sized_target
<size
, big_endian
>* target
=
771 object
->sized_target
<size
, big_endian
>();
772 if (!target
->has_make_symbol())
773 ret
= new Sized_symbol
<size
>();
776 ret
= target
->make_symbol();
779 // This means that we don't want a symbol table
782 this->table_
.erase(ins
.first
);
785 this->table_
.erase(insdef
.first
);
786 // Inserting insdef invalidated ins.
787 this->table_
.erase(std::make_pair(name_key
,
794 ret
->init_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
796 ins
.first
->second
= ret
;
799 // This is the first time we have seen NAME/NULL. Point
800 // it at the new entry for NAME/VERSION.
801 gold_assert(insdef
.second
);
802 insdef
.first
->second
= ret
;
807 // Record every time we see a new undefined symbol, to speed up
809 if (!was_undefined
&& ret
->is_undefined())
810 ++this->saw_undefined_
;
812 // Keep track of common symbols, to speed up common symbol
814 if (!was_common
&& ret
->is_common())
816 if (ret
->type() != elfcpp::STT_TLS
)
817 this->commons_
.push_back(ret
);
819 this->tls_commons_
.push_back(ret
);
823 ret
->set_is_default();
827 // Add all the symbols in a relocatable object to the hash table.
829 template<int size
, bool big_endian
>
831 Symbol_table::add_from_relobj(
832 Sized_relobj
<size
, big_endian
>* relobj
,
833 const unsigned char* syms
,
835 size_t symndx_offset
,
836 const char* sym_names
,
837 size_t sym_name_size
,
838 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
843 gold_assert(size
== relobj
->target()->get_size());
844 gold_assert(size
== parameters
->target().get_size());
846 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
848 const bool just_symbols
= relobj
->just_symbols();
850 const unsigned char* p
= syms
;
851 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
853 (*sympointers
)[i
] = NULL
;
855 elfcpp::Sym
<size
, big_endian
> sym(p
);
857 unsigned int st_name
= sym
.get_st_name();
858 if (st_name
>= sym_name_size
)
860 relobj
->error(_("bad global symbol name offset %u at %zu"),
865 const char* name
= sym_names
+ st_name
;
868 unsigned int st_shndx
= relobj
->adjust_sym_shndx(i
+ symndx_offset
,
871 unsigned int orig_st_shndx
= st_shndx
;
873 orig_st_shndx
= elfcpp::SHN_UNDEF
;
875 if (st_shndx
!= elfcpp::SHN_UNDEF
)
878 // A symbol defined in a section which we are not including must
879 // be treated as an undefined symbol.
880 if (st_shndx
!= elfcpp::SHN_UNDEF
882 && !relobj
->is_section_included(st_shndx
))
883 st_shndx
= elfcpp::SHN_UNDEF
;
885 // In an object file, an '@' in the name separates the symbol
886 // name from the version name. If there are two '@' characters,
887 // this is the default version.
888 const char* ver
= strchr(name
, '@');
889 Stringpool::Key ver_key
= 0;
891 // DEF: is the version default? LOCAL: is the symbol forced local?
897 // The symbol name is of the form foo@VERSION or foo@@VERSION
898 namelen
= ver
- name
;
905 ver
= this->namepool_
.add(ver
, true, &ver_key
);
907 // We don't want to assign a version to an undefined symbol,
908 // even if it is listed in the version script. FIXME: What
909 // about a common symbol?
912 namelen
= strlen(name
);
913 if (!this->version_script_
.empty()
914 && st_shndx
!= elfcpp::SHN_UNDEF
)
916 // The symbol name did not have a version, but the
917 // version script may assign a version anyway.
919 if (this->version_script_
.get_symbol_version(name
, &version
))
921 // The version can be empty if the version script is
922 // only used to force some symbols to be local.
923 if (!version
.empty())
925 ver
= this->namepool_
.add_with_length(version
.c_str(),
932 else if (this->version_script_
.symbol_is_local(name
))
937 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
938 unsigned char symbuf
[sym_size
];
939 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
942 memcpy(symbuf
, p
, sym_size
);
943 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
944 if (orig_st_shndx
!= elfcpp::SHN_UNDEF
&& is_ordinary
)
946 // Symbol values in object files are section relative.
947 // This is normally what we want, but since here we are
948 // converting the symbol to absolute we need to add the
949 // section address. The section address in an object
950 // file is normally zero, but people can use a linker
951 // script to change it.
952 sw
.put_st_value(sym
.get_st_value()
953 + relobj
->section_address(orig_st_shndx
));
955 st_shndx
= elfcpp::SHN_ABS
;
960 Stringpool::Key name_key
;
961 name
= this->namepool_
.add_with_length(name
, namelen
, true,
964 Sized_symbol
<size
>* res
;
965 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
966 def
, *psym
, st_shndx
, is_ordinary
,
970 this->force_local(res
);
972 (*sympointers
)[i
] = res
;
976 // Add all the symbols in a dynamic object to the hash table.
978 template<int size
, bool big_endian
>
980 Symbol_table::add_from_dynobj(
981 Sized_dynobj
<size
, big_endian
>* dynobj
,
982 const unsigned char* syms
,
984 const char* sym_names
,
985 size_t sym_name_size
,
986 const unsigned char* versym
,
988 const std::vector
<const char*>* version_map
,
989 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
994 gold_assert(size
== dynobj
->target()->get_size());
995 gold_assert(size
== parameters
->target().get_size());
997 if (dynobj
->just_symbols())
999 gold_error(_("--just-symbols does not make sense with a shared object"));
1003 if (versym
!= NULL
&& versym_size
/ 2 < count
)
1005 dynobj
->error(_("too few symbol versions"));
1009 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1011 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1012 // weak aliases. This is necessary because if the dynamic object
1013 // provides the same variable under two names, one of which is a
1014 // weak definition, and the regular object refers to the weak
1015 // definition, we have to put both the weak definition and the
1016 // strong definition into the dynamic symbol table. Given a weak
1017 // definition, the only way that we can find the corresponding
1018 // strong definition, if any, is to search the symbol table.
1019 std::vector
<Sized_symbol
<size
>*> object_symbols
;
1021 const unsigned char* p
= syms
;
1022 const unsigned char* vs
= versym
;
1023 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
1025 elfcpp::Sym
<size
, big_endian
> sym(p
);
1027 if (sympointers
!= NULL
)
1028 (*sympointers
)[i
] = NULL
;
1030 // Ignore symbols with local binding or that have
1031 // internal or hidden visibility.
1032 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
1033 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
1034 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
1037 // A protected symbol in a shared library must be treated as a
1038 // normal symbol when viewed from outside the shared library.
1039 // Implement this by overriding the visibility here.
1040 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1041 unsigned char symbuf
[sym_size
];
1042 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1043 if (sym
.get_st_visibility() == elfcpp::STV_PROTECTED
)
1045 memcpy(symbuf
, p
, sym_size
);
1046 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1047 sw
.put_st_other(elfcpp::STV_DEFAULT
, sym
.get_st_nonvis());
1051 unsigned int st_name
= psym
->get_st_name();
1052 if (st_name
>= sym_name_size
)
1054 dynobj
->error(_("bad symbol name offset %u at %zu"),
1059 const char* name
= sym_names
+ st_name
;
1062 unsigned int st_shndx
= dynobj
->adjust_sym_shndx(i
, psym
->get_st_shndx(),
1065 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1068 Sized_symbol
<size
>* res
;
1072 Stringpool::Key name_key
;
1073 name
= this->namepool_
.add(name
, true, &name_key
);
1074 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1075 false, *psym
, st_shndx
, is_ordinary
,
1080 // Read the version information.
1082 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
1084 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
1085 v
&= elfcpp::VERSYM_VERSION
;
1087 // The Sun documentation says that V can be VER_NDX_LOCAL,
1088 // or VER_NDX_GLOBAL, or a version index. The meaning of
1089 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1090 // The old GNU linker will happily generate VER_NDX_LOCAL
1091 // for an undefined symbol. I don't know what the Sun
1092 // linker will generate.
1094 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1095 && st_shndx
!= elfcpp::SHN_UNDEF
)
1097 // This symbol should not be visible outside the object.
1101 // At this point we are definitely going to add this symbol.
1102 Stringpool::Key name_key
;
1103 name
= this->namepool_
.add(name
, true, &name_key
);
1105 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1106 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
1108 // This symbol does not have a version.
1109 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1110 false, *psym
, st_shndx
, is_ordinary
,
1115 if (v
>= version_map
->size())
1117 dynobj
->error(_("versym for symbol %zu out of range: %u"),
1122 const char* version
= (*version_map
)[v
];
1123 if (version
== NULL
)
1125 dynobj
->error(_("versym for symbol %zu has no name: %u"),
1130 Stringpool::Key version_key
;
1131 version
= this->namepool_
.add(version
, true, &version_key
);
1133 // If this is an absolute symbol, and the version name
1134 // and symbol name are the same, then this is the
1135 // version definition symbol. These symbols exist to
1136 // support using -u to pull in particular versions. We
1137 // do not want to record a version for them.
1138 if (st_shndx
== elfcpp::SHN_ABS
1140 && name_key
== version_key
)
1141 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1142 false, *psym
, st_shndx
, is_ordinary
,
1146 const bool def
= (!hidden
1147 && st_shndx
!= elfcpp::SHN_UNDEF
);
1148 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
1149 version_key
, def
, *psym
, st_shndx
,
1150 is_ordinary
, st_shndx
);
1155 // Note that it is possible that RES was overridden by an
1156 // earlier object, in which case it can't be aliased here.
1157 if (st_shndx
!= elfcpp::SHN_UNDEF
1159 && psym
->get_st_type() == elfcpp::STT_OBJECT
1160 && res
->source() == Symbol::FROM_OBJECT
1161 && res
->object() == dynobj
)
1162 object_symbols
.push_back(res
);
1164 if (sympointers
!= NULL
)
1165 (*sympointers
)[i
] = res
;
1168 this->record_weak_aliases(&object_symbols
);
1171 // This is used to sort weak aliases. We sort them first by section
1172 // index, then by offset, then by weak ahead of strong.
1175 class Weak_alias_sorter
1178 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
1183 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
1184 const Sized_symbol
<size
>* s2
) const
1187 unsigned int s1_shndx
= s1
->shndx(&is_ordinary
);
1188 gold_assert(is_ordinary
);
1189 unsigned int s2_shndx
= s2
->shndx(&is_ordinary
);
1190 gold_assert(is_ordinary
);
1191 if (s1_shndx
!= s2_shndx
)
1192 return s1_shndx
< s2_shndx
;
1194 if (s1
->value() != s2
->value())
1195 return s1
->value() < s2
->value();
1196 if (s1
->binding() != s2
->binding())
1198 if (s1
->binding() == elfcpp::STB_WEAK
)
1200 if (s2
->binding() == elfcpp::STB_WEAK
)
1203 return std::string(s1
->name()) < std::string(s2
->name());
1206 // SYMBOLS is a list of object symbols from a dynamic object. Look
1207 // for any weak aliases, and record them so that if we add the weak
1208 // alias to the dynamic symbol table, we also add the corresponding
1213 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
1215 // Sort the vector by section index, then by offset, then by weak
1217 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
1219 // Walk through the vector. For each weak definition, record
1221 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
1223 p
!= symbols
->end();
1226 if ((*p
)->binding() != elfcpp::STB_WEAK
)
1229 // Build a circular list of weak aliases. Each symbol points to
1230 // the next one in the circular list.
1232 Sized_symbol
<size
>* from_sym
= *p
;
1233 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
1234 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
1237 if ((*q
)->shndx(&dummy
) != from_sym
->shndx(&dummy
)
1238 || (*q
)->value() != from_sym
->value())
1241 this->weak_aliases_
[from_sym
] = *q
;
1242 from_sym
->set_has_alias();
1248 this->weak_aliases_
[from_sym
] = *p
;
1249 from_sym
->set_has_alias();
1256 // Create and return a specially defined symbol. If ONLY_IF_REF is
1257 // true, then only create the symbol if there is a reference to it.
1258 // If this does not return NULL, it sets *POLDSYM to the existing
1259 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1261 template<int size
, bool big_endian
>
1263 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1265 Sized_symbol
<size
>** poldsym
)
1268 Sized_symbol
<size
>* sym
;
1269 bool add_to_table
= false;
1270 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1272 // If the caller didn't give us a version, see if we get one from
1273 // the version script.
1275 if (*pversion
== NULL
)
1277 if (this->version_script_
.get_symbol_version(*pname
, &v
))
1280 *pversion
= v
.c_str();
1286 oldsym
= this->lookup(*pname
, *pversion
);
1287 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1290 *pname
= oldsym
->name();
1291 *pversion
= oldsym
->version();
1295 // Canonicalize NAME and VERSION.
1296 Stringpool::Key name_key
;
1297 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1299 Stringpool::Key version_key
= 0;
1300 if (*pversion
!= NULL
)
1301 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1303 Symbol
* const snull
= NULL
;
1304 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1305 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1311 // We already have a symbol table entry for NAME/VERSION.
1312 oldsym
= ins
.first
->second
;
1313 gold_assert(oldsym
!= NULL
);
1317 // We haven't seen this symbol before.
1318 gold_assert(ins
.first
->second
== NULL
);
1319 add_to_table
= true;
1320 add_loc
= ins
.first
;
1325 const Target
& target
= parameters
->target();
1326 if (!target
.has_make_symbol())
1327 sym
= new Sized_symbol
<size
>();
1330 gold_assert(target
.get_size() == size
);
1331 gold_assert(target
.is_big_endian() ? big_endian
: !big_endian
);
1332 typedef Sized_target
<size
, big_endian
> My_target
;
1333 const My_target
* sized_target
=
1334 static_cast<const My_target
*>(&target
);
1335 sym
= sized_target
->make_symbol();
1341 add_loc
->second
= sym
;
1343 gold_assert(oldsym
!= NULL
);
1345 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1350 // Define a symbol based on an Output_data.
1353 Symbol_table::define_in_output_data(const char* name
,
1354 const char* version
,
1359 elfcpp::STB binding
,
1360 elfcpp::STV visibility
,
1361 unsigned char nonvis
,
1362 bool offset_is_from_end
,
1365 if (parameters
->target().get_size() == 32)
1367 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1368 return this->do_define_in_output_data
<32>(name
, version
, od
,
1369 value
, symsize
, type
, binding
,
1377 else if (parameters
->target().get_size() == 64)
1379 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1380 return this->do_define_in_output_data
<64>(name
, version
, od
,
1381 value
, symsize
, type
, binding
,
1393 // Define a symbol in an Output_data, sized version.
1397 Symbol_table::do_define_in_output_data(
1399 const char* version
,
1401 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1402 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1404 elfcpp::STB binding
,
1405 elfcpp::STV visibility
,
1406 unsigned char nonvis
,
1407 bool offset_is_from_end
,
1410 Sized_symbol
<size
>* sym
;
1411 Sized_symbol
<size
>* oldsym
;
1413 if (parameters
->target().is_big_endian())
1415 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1416 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1417 only_if_ref
, &oldsym
);
1424 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1425 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1426 only_if_ref
, &oldsym
);
1435 sym
->init_output_data(name
, version
, od
, value
, symsize
, type
, binding
,
1436 visibility
, nonvis
, offset_is_from_end
);
1440 if (binding
== elfcpp::STB_LOCAL
1441 || this->version_script_
.symbol_is_local(name
))
1442 this->force_local(sym
);
1443 else if (version
!= NULL
)
1444 sym
->set_is_default();
1448 if (Symbol_table::should_override_with_special(oldsym
))
1449 this->override_with_special(oldsym
, sym
);
1454 // Define a symbol based on an Output_segment.
1457 Symbol_table::define_in_output_segment(const char* name
,
1458 const char* version
, Output_segment
* os
,
1462 elfcpp::STB binding
,
1463 elfcpp::STV visibility
,
1464 unsigned char nonvis
,
1465 Symbol::Segment_offset_base offset_base
,
1468 if (parameters
->target().get_size() == 32)
1470 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1471 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1472 value
, symsize
, type
,
1473 binding
, visibility
, nonvis
,
1474 offset_base
, only_if_ref
);
1479 else if (parameters
->target().get_size() == 64)
1481 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1482 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1483 value
, symsize
, type
,
1484 binding
, visibility
, nonvis
,
1485 offset_base
, only_if_ref
);
1494 // Define a symbol in an Output_segment, sized version.
1498 Symbol_table::do_define_in_output_segment(
1500 const char* version
,
1502 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1503 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1505 elfcpp::STB binding
,
1506 elfcpp::STV visibility
,
1507 unsigned char nonvis
,
1508 Symbol::Segment_offset_base offset_base
,
1511 Sized_symbol
<size
>* sym
;
1512 Sized_symbol
<size
>* oldsym
;
1514 if (parameters
->target().is_big_endian())
1516 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1517 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1518 only_if_ref
, &oldsym
);
1525 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1526 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1527 only_if_ref
, &oldsym
);
1536 sym
->init_output_segment(name
, version
, os
, value
, symsize
, type
, binding
,
1537 visibility
, nonvis
, offset_base
);
1541 if (binding
== elfcpp::STB_LOCAL
1542 || this->version_script_
.symbol_is_local(name
))
1543 this->force_local(sym
);
1544 else if (version
!= NULL
)
1545 sym
->set_is_default();
1549 if (Symbol_table::should_override_with_special(oldsym
))
1550 this->override_with_special(oldsym
, sym
);
1555 // Define a special symbol with a constant value. It is a multiple
1556 // definition error if this symbol is already defined.
1559 Symbol_table::define_as_constant(const char* name
,
1560 const char* version
,
1564 elfcpp::STB binding
,
1565 elfcpp::STV visibility
,
1566 unsigned char nonvis
,
1568 bool force_override
)
1570 if (parameters
->target().get_size() == 32)
1572 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1573 return this->do_define_as_constant
<32>(name
, version
, value
,
1574 symsize
, type
, binding
,
1575 visibility
, nonvis
, only_if_ref
,
1581 else if (parameters
->target().get_size() == 64)
1583 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1584 return this->do_define_as_constant
<64>(name
, version
, value
,
1585 symsize
, type
, binding
,
1586 visibility
, nonvis
, only_if_ref
,
1596 // Define a symbol as a constant, sized version.
1600 Symbol_table::do_define_as_constant(
1602 const char* version
,
1603 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1604 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1606 elfcpp::STB binding
,
1607 elfcpp::STV visibility
,
1608 unsigned char nonvis
,
1610 bool force_override
)
1612 Sized_symbol
<size
>* sym
;
1613 Sized_symbol
<size
>* oldsym
;
1615 if (parameters
->target().is_big_endian())
1617 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1618 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1619 only_if_ref
, &oldsym
);
1626 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1627 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1628 only_if_ref
, &oldsym
);
1637 sym
->init_constant(name
, version
, value
, symsize
, type
, binding
, visibility
,
1642 // Version symbols are absolute symbols with name == version.
1643 // We don't want to force them to be local.
1644 if ((version
== NULL
1647 && (binding
== elfcpp::STB_LOCAL
1648 || this->version_script_
.symbol_is_local(name
)))
1649 this->force_local(sym
);
1650 else if (version
!= NULL
1651 && (name
!= version
|| value
!= 0))
1652 sym
->set_is_default();
1656 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
1657 this->override_with_special(oldsym
, sym
);
1662 // Define a set of symbols in output sections.
1665 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1666 const Define_symbol_in_section
* p
,
1669 for (int i
= 0; i
< count
; ++i
, ++p
)
1671 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1673 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
1674 p
->size
, p
->type
, p
->binding
,
1675 p
->visibility
, p
->nonvis
,
1676 p
->offset_is_from_end
,
1677 only_if_ref
|| p
->only_if_ref
);
1679 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1680 p
->binding
, p
->visibility
, p
->nonvis
,
1681 only_if_ref
|| p
->only_if_ref
,
1686 // Define a set of symbols in output segments.
1689 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1690 const Define_symbol_in_segment
* p
,
1693 for (int i
= 0; i
< count
; ++i
, ++p
)
1695 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1696 p
->segment_flags_set
,
1697 p
->segment_flags_clear
);
1699 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
1700 p
->size
, p
->type
, p
->binding
,
1701 p
->visibility
, p
->nonvis
,
1703 only_if_ref
|| p
->only_if_ref
);
1705 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1706 p
->binding
, p
->visibility
, p
->nonvis
,
1707 only_if_ref
|| p
->only_if_ref
,
1712 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1713 // symbol should be defined--typically a .dyn.bss section. VALUE is
1714 // the offset within POSD.
1718 Symbol_table::define_with_copy_reloc(
1719 Sized_symbol
<size
>* csym
,
1721 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
1723 gold_assert(csym
->is_from_dynobj());
1724 gold_assert(!csym
->is_copied_from_dynobj());
1725 Object
* object
= csym
->object();
1726 gold_assert(object
->is_dynamic());
1727 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1729 // Our copied variable has to override any variable in a shared
1731 elfcpp::STB binding
= csym
->binding();
1732 if (binding
== elfcpp::STB_WEAK
)
1733 binding
= elfcpp::STB_GLOBAL
;
1735 this->define_in_output_data(csym
->name(), csym
->version(),
1736 posd
, value
, csym
->symsize(),
1737 csym
->type(), binding
,
1738 csym
->visibility(), csym
->nonvis(),
1741 csym
->set_is_copied_from_dynobj();
1742 csym
->set_needs_dynsym_entry();
1744 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1746 // We have now defined all aliases, but we have not entered them all
1747 // in the copied_symbol_dynobjs_ map.
1748 if (csym
->has_alias())
1753 sym
= this->weak_aliases_
[sym
];
1756 gold_assert(sym
->output_data() == posd
);
1758 sym
->set_is_copied_from_dynobj();
1759 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1764 // SYM is defined using a COPY reloc. Return the dynamic object where
1765 // the original definition was found.
1768 Symbol_table::get_copy_source(const Symbol
* sym
) const
1770 gold_assert(sym
->is_copied_from_dynobj());
1771 Copied_symbol_dynobjs::const_iterator p
=
1772 this->copied_symbol_dynobjs_
.find(sym
);
1773 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1777 // Add any undefined symbols named on the command line.
1780 Symbol_table::add_undefined_symbols_from_command_line()
1782 if (parameters
->options().any_undefined())
1784 if (parameters
->target().get_size() == 32)
1786 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1787 this->do_add_undefined_symbols_from_command_line
<32>();
1792 else if (parameters
->target().get_size() == 64)
1794 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1795 this->do_add_undefined_symbols_from_command_line
<64>();
1807 Symbol_table::do_add_undefined_symbols_from_command_line()
1809 for (options::String_set::const_iterator p
=
1810 parameters
->options().undefined_begin();
1811 p
!= parameters
->options().undefined_end();
1814 const char* name
= p
->c_str();
1816 if (this->lookup(name
) != NULL
)
1819 const char* version
= NULL
;
1821 Sized_symbol
<size
>* sym
;
1822 Sized_symbol
<size
>* oldsym
;
1823 if (parameters
->target().is_big_endian())
1825 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1826 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1834 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1835 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1842 gold_assert(oldsym
== NULL
);
1844 sym
->init_undefined(name
, version
, elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1845 elfcpp::STV_DEFAULT
, 0);
1846 ++this->saw_undefined_
;
1850 // Set the dynamic symbol indexes. INDEX is the index of the first
1851 // global dynamic symbol. Pointers to the symbols are stored into the
1852 // vector SYMS. The names are added to DYNPOOL. This returns an
1853 // updated dynamic symbol index.
1856 Symbol_table::set_dynsym_indexes(unsigned int index
,
1857 std::vector
<Symbol
*>* syms
,
1858 Stringpool
* dynpool
,
1861 for (Symbol_table_type::iterator p
= this->table_
.begin();
1862 p
!= this->table_
.end();
1865 Symbol
* sym
= p
->second
;
1867 // Note that SYM may already have a dynamic symbol index, since
1868 // some symbols appear more than once in the symbol table, with
1869 // and without a version.
1871 if (!sym
->should_add_dynsym_entry())
1872 sym
->set_dynsym_index(-1U);
1873 else if (!sym
->has_dynsym_index())
1875 sym
->set_dynsym_index(index
);
1877 syms
->push_back(sym
);
1878 dynpool
->add(sym
->name(), false, NULL
);
1880 // Record any version information.
1881 if (sym
->version() != NULL
)
1882 versions
->record_version(this, dynpool
, sym
);
1886 // Finish up the versions. In some cases this may add new dynamic
1888 index
= versions
->finalize(this, index
, syms
);
1893 // Set the final values for all the symbols. The index of the first
1894 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1895 // file offset OFF. Add their names to POOL. Return the new file
1896 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1899 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
1900 size_t dyncount
, Stringpool
* pool
,
1901 unsigned int *plocal_symcount
)
1905 gold_assert(*plocal_symcount
!= 0);
1906 this->first_global_index_
= *plocal_symcount
;
1908 this->dynamic_offset_
= dynoff
;
1909 this->first_dynamic_global_index_
= dyn_global_index
;
1910 this->dynamic_count_
= dyncount
;
1912 if (parameters
->target().get_size() == 32)
1914 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1915 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
1920 else if (parameters
->target().get_size() == 64)
1922 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1923 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
1931 // Now that we have the final symbol table, we can reliably note
1932 // which symbols should get warnings.
1933 this->warnings_
.note_warnings(this);
1938 // SYM is going into the symbol table at *PINDEX. Add the name to
1939 // POOL, update *PINDEX and *POFF.
1943 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
1944 unsigned int* pindex
, off_t
* poff
)
1946 sym
->set_symtab_index(*pindex
);
1947 pool
->add(sym
->name(), false, NULL
);
1949 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
1952 // Set the final value for all the symbols. This is called after
1953 // Layout::finalize, so all the output sections have their final
1958 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
1959 unsigned int* plocal_symcount
)
1961 off
= align_address(off
, size
>> 3);
1962 this->offset_
= off
;
1964 unsigned int index
= *plocal_symcount
;
1965 const unsigned int orig_index
= index
;
1967 // First do all the symbols which have been forced to be local, as
1968 // they must appear before all global symbols.
1969 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
1970 p
!= this->forced_locals_
.end();
1974 gold_assert(sym
->is_forced_local());
1975 if (this->sized_finalize_symbol
<size
>(sym
))
1977 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
1982 // Now do all the remaining symbols.
1983 for (Symbol_table_type::iterator p
= this->table_
.begin();
1984 p
!= this->table_
.end();
1987 Symbol
* sym
= p
->second
;
1988 if (this->sized_finalize_symbol
<size
>(sym
))
1989 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
1992 this->output_count_
= index
- orig_index
;
1997 // Finalize the symbol SYM. This returns true if the symbol should be
1998 // added to the symbol table, false otherwise.
2002 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
2004 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2006 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
2008 // The default version of a symbol may appear twice in the symbol
2009 // table. We only need to finalize it once.
2010 if (sym
->has_symtab_index())
2015 gold_assert(!sym
->has_symtab_index());
2016 sym
->set_symtab_index(-1U);
2017 gold_assert(sym
->dynsym_index() == -1U);
2023 switch (sym
->source())
2025 case Symbol::FROM_OBJECT
:
2028 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2030 // FIXME: We need some target specific support here.
2032 && shndx
!= elfcpp::SHN_ABS
2033 && shndx
!= elfcpp::SHN_COMMON
)
2035 gold_error(_("%s: unsupported symbol section 0x%x"),
2036 sym
->demangled_name().c_str(), shndx
);
2037 shndx
= elfcpp::SHN_UNDEF
;
2040 Object
* symobj
= sym
->object();
2041 if (symobj
->is_dynamic())
2044 shndx
= elfcpp::SHN_UNDEF
;
2046 else if (shndx
== elfcpp::SHN_UNDEF
)
2048 else if (!is_ordinary
2049 && (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
))
2050 value
= sym
->value();
2053 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2054 Output_section
* os
= relobj
->output_section(shndx
);
2058 sym
->set_symtab_index(-1U);
2059 gold_assert(sym
->dynsym_index() == -1U);
2063 uint64_t secoff64
= relobj
->output_section_offset(shndx
);
2064 Value_type secoff
= convert_types
<Value_type
, uint64_t>(secoff64
);
2065 if (sym
->type() == elfcpp::STT_TLS
)
2066 value
= sym
->value() + os
->tls_offset() + secoff
;
2068 value
= sym
->value() + os
->address() + secoff
;
2073 case Symbol::IN_OUTPUT_DATA
:
2075 Output_data
* od
= sym
->output_data();
2076 value
= sym
->value();
2077 if (sym
->type() != elfcpp::STT_TLS
)
2078 value
+= od
->address();
2081 Output_section
* os
= od
->output_section();
2082 gold_assert(os
!= NULL
);
2083 value
+= os
->tls_offset() + (od
->address() - os
->address());
2085 if (sym
->offset_is_from_end())
2086 value
+= od
->data_size();
2090 case Symbol::IN_OUTPUT_SEGMENT
:
2092 Output_segment
* os
= sym
->output_segment();
2093 value
= sym
->value();
2094 if (sym
->type() != elfcpp::STT_TLS
)
2095 value
+= os
->vaddr();
2096 switch (sym
->offset_base())
2098 case Symbol::SEGMENT_START
:
2100 case Symbol::SEGMENT_END
:
2101 value
+= os
->memsz();
2103 case Symbol::SEGMENT_BSS
:
2104 value
+= os
->filesz();
2112 case Symbol::IS_CONSTANT
:
2113 value
= sym
->value();
2116 case Symbol::IS_UNDEFINED
:
2124 sym
->set_value(value
);
2126 if (parameters
->options().strip_all())
2128 sym
->set_symtab_index(-1U);
2135 // Write out the global symbols.
2138 Symbol_table::write_globals(const Input_objects
* input_objects
,
2139 const Stringpool
* sympool
,
2140 const Stringpool
* dynpool
,
2141 Output_symtab_xindex
* symtab_xindex
,
2142 Output_symtab_xindex
* dynsym_xindex
,
2143 Output_file
* of
) const
2145 switch (parameters
->size_and_endianness())
2147 #ifdef HAVE_TARGET_32_LITTLE
2148 case Parameters::TARGET_32_LITTLE
:
2149 this->sized_write_globals
<32, false>(input_objects
, sympool
,
2150 dynpool
, symtab_xindex
,
2154 #ifdef HAVE_TARGET_32_BIG
2155 case Parameters::TARGET_32_BIG
:
2156 this->sized_write_globals
<32, true>(input_objects
, sympool
,
2157 dynpool
, symtab_xindex
,
2161 #ifdef HAVE_TARGET_64_LITTLE
2162 case Parameters::TARGET_64_LITTLE
:
2163 this->sized_write_globals
<64, false>(input_objects
, sympool
,
2164 dynpool
, symtab_xindex
,
2168 #ifdef HAVE_TARGET_64_BIG
2169 case Parameters::TARGET_64_BIG
:
2170 this->sized_write_globals
<64, true>(input_objects
, sympool
,
2171 dynpool
, symtab_xindex
,
2180 // Write out the global symbols.
2182 template<int size
, bool big_endian
>
2184 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
2185 const Stringpool
* sympool
,
2186 const Stringpool
* dynpool
,
2187 Output_symtab_xindex
* symtab_xindex
,
2188 Output_symtab_xindex
* dynsym_xindex
,
2189 Output_file
* of
) const
2191 const Target
& target
= parameters
->target();
2193 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2195 const unsigned int output_count
= this->output_count_
;
2196 const section_size_type oview_size
= output_count
* sym_size
;
2197 const unsigned int first_global_index
= this->first_global_index_
;
2198 unsigned char* psyms
;
2199 if (this->offset_
== 0 || output_count
== 0)
2202 psyms
= of
->get_output_view(this->offset_
, oview_size
);
2204 const unsigned int dynamic_count
= this->dynamic_count_
;
2205 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
2206 const unsigned int first_dynamic_global_index
=
2207 this->first_dynamic_global_index_
;
2208 unsigned char* dynamic_view
;
2209 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
2210 dynamic_view
= NULL
;
2212 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
2214 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
2215 p
!= this->table_
.end();
2218 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
2220 // Possibly warn about unresolved symbols in shared libraries.
2221 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
2223 unsigned int sym_index
= sym
->symtab_index();
2224 unsigned int dynsym_index
;
2225 if (dynamic_view
== NULL
)
2228 dynsym_index
= sym
->dynsym_index();
2230 if (sym_index
== -1U && dynsym_index
== -1U)
2232 // This symbol is not included in the output file.
2237 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
2238 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
2239 switch (sym
->source())
2241 case Symbol::FROM_OBJECT
:
2244 unsigned int in_shndx
= sym
->shndx(&is_ordinary
);
2246 // FIXME: We need some target specific support here.
2248 && in_shndx
!= elfcpp::SHN_ABS
2249 && in_shndx
!= elfcpp::SHN_COMMON
)
2251 gold_error(_("%s: unsupported symbol section 0x%x"),
2252 sym
->demangled_name().c_str(), in_shndx
);
2257 Object
* symobj
= sym
->object();
2258 if (symobj
->is_dynamic())
2260 if (sym
->needs_dynsym_value())
2261 dynsym_value
= target
.dynsym_value(sym
);
2262 shndx
= elfcpp::SHN_UNDEF
;
2264 else if (in_shndx
== elfcpp::SHN_UNDEF
2266 && (in_shndx
== elfcpp::SHN_ABS
2267 || in_shndx
== elfcpp::SHN_COMMON
)))
2271 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2272 Output_section
* os
= relobj
->output_section(in_shndx
);
2273 gold_assert(os
!= NULL
);
2274 shndx
= os
->out_shndx();
2276 if (shndx
>= elfcpp::SHN_LORESERVE
)
2278 if (sym_index
!= -1U)
2279 symtab_xindex
->add(sym_index
, shndx
);
2280 if (dynsym_index
!= -1U)
2281 dynsym_xindex
->add(dynsym_index
, shndx
);
2282 shndx
= elfcpp::SHN_XINDEX
;
2285 // In object files symbol values are section
2287 if (parameters
->options().relocatable())
2288 sym_value
-= os
->address();
2294 case Symbol::IN_OUTPUT_DATA
:
2295 shndx
= sym
->output_data()->out_shndx();
2296 if (shndx
>= elfcpp::SHN_LORESERVE
)
2298 if (sym_index
!= -1U)
2299 symtab_xindex
->add(sym_index
, shndx
);
2300 if (dynsym_index
!= -1U)
2301 dynsym_xindex
->add(dynsym_index
, shndx
);
2302 shndx
= elfcpp::SHN_XINDEX
;
2306 case Symbol::IN_OUTPUT_SEGMENT
:
2307 shndx
= elfcpp::SHN_ABS
;
2310 case Symbol::IS_CONSTANT
:
2311 shndx
= elfcpp::SHN_ABS
;
2314 case Symbol::IS_UNDEFINED
:
2315 shndx
= elfcpp::SHN_UNDEF
;
2322 if (sym_index
!= -1U)
2324 sym_index
-= first_global_index
;
2325 gold_assert(sym_index
< output_count
);
2326 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
2327 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
2331 if (dynsym_index
!= -1U)
2333 dynsym_index
-= first_dynamic_global_index
;
2334 gold_assert(dynsym_index
< dynamic_count
);
2335 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
2336 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
2341 of
->write_output_view(this->offset_
, oview_size
, psyms
);
2342 if (dynamic_view
!= NULL
)
2343 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
2346 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2347 // strtab holding the name.
2349 template<int size
, bool big_endian
>
2351 Symbol_table::sized_write_symbol(
2352 Sized_symbol
<size
>* sym
,
2353 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
2355 const Stringpool
* pool
,
2356 unsigned char* p
) const
2358 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
2359 osym
.put_st_name(pool
->get_offset(sym
->name()));
2360 osym
.put_st_value(value
);
2361 // Use a symbol size of zero for undefined symbols from shared libraries.
2362 if (shndx
== elfcpp::SHN_UNDEF
&& sym
->is_from_dynobj())
2363 osym
.put_st_size(0);
2365 osym
.put_st_size(sym
->symsize());
2366 // A version script may have overridden the default binding.
2367 if (sym
->is_forced_local())
2368 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, sym
->type()));
2370 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
2371 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
2372 osym
.put_st_shndx(shndx
);
2375 // Check for unresolved symbols in shared libraries. This is
2376 // controlled by the --allow-shlib-undefined option.
2378 // We only warn about libraries for which we have seen all the
2379 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2380 // which were not seen in this link. If we didn't see a DT_NEEDED
2381 // entry, we aren't going to be able to reliably report whether the
2382 // symbol is undefined.
2384 // We also don't warn about libraries found in the system library
2385 // directory (the directory were we find libc.so); we assume that
2386 // those libraries are OK. This heuristic avoids problems in
2387 // GNU/Linux, in which -ldl can have undefined references satisfied by
2391 Symbol_table::warn_about_undefined_dynobj_symbol(
2392 const Input_objects
* input_objects
,
2396 if (sym
->source() == Symbol::FROM_OBJECT
2397 && sym
->object()->is_dynamic()
2398 && sym
->shndx(&dummy
) == elfcpp::SHN_UNDEF
2399 && sym
->binding() != elfcpp::STB_WEAK
2400 && !parameters
->options().allow_shlib_undefined()
2401 && !parameters
->target().is_defined_by_abi(sym
)
2402 && !input_objects
->found_in_system_library_directory(sym
->object()))
2404 // A very ugly cast.
2405 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2406 if (!dynobj
->has_unknown_needed_entries())
2409 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2410 sym
->object()->name().c_str(),
2411 sym
->demangled_name().c_str(),
2414 gold_error(_("%s: undefined reference to '%s'"),
2415 sym
->object()->name().c_str(),
2416 sym
->demangled_name().c_str());
2421 // Write out a section symbol. Return the update offset.
2424 Symbol_table::write_section_symbol(const Output_section
*os
,
2425 Output_symtab_xindex
* symtab_xindex
,
2429 switch (parameters
->size_and_endianness())
2431 #ifdef HAVE_TARGET_32_LITTLE
2432 case Parameters::TARGET_32_LITTLE
:
2433 this->sized_write_section_symbol
<32, false>(os
, symtab_xindex
, of
,
2437 #ifdef HAVE_TARGET_32_BIG
2438 case Parameters::TARGET_32_BIG
:
2439 this->sized_write_section_symbol
<32, true>(os
, symtab_xindex
, of
,
2443 #ifdef HAVE_TARGET_64_LITTLE
2444 case Parameters::TARGET_64_LITTLE
:
2445 this->sized_write_section_symbol
<64, false>(os
, symtab_xindex
, of
,
2449 #ifdef HAVE_TARGET_64_BIG
2450 case Parameters::TARGET_64_BIG
:
2451 this->sized_write_section_symbol
<64, true>(os
, symtab_xindex
, of
,
2460 // Write out a section symbol, specialized for size and endianness.
2462 template<int size
, bool big_endian
>
2464 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2465 Output_symtab_xindex
* symtab_xindex
,
2469 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2471 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2473 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2474 osym
.put_st_name(0);
2475 osym
.put_st_value(os
->address());
2476 osym
.put_st_size(0);
2477 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2478 elfcpp::STT_SECTION
));
2479 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2481 unsigned int shndx
= os
->out_shndx();
2482 if (shndx
>= elfcpp::SHN_LORESERVE
)
2484 symtab_xindex
->add(os
->symtab_index(), shndx
);
2485 shndx
= elfcpp::SHN_XINDEX
;
2487 osym
.put_st_shndx(shndx
);
2489 of
->write_output_view(offset
, sym_size
, pov
);
2492 // Print statistical information to stderr. This is used for --stats.
2495 Symbol_table::print_stats() const
2497 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2498 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2499 program_name
, this->table_
.size(), this->table_
.bucket_count());
2501 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2502 program_name
, this->table_
.size());
2504 this->namepool_
.print_stats("symbol table stringpool");
2507 // We check for ODR violations by looking for symbols with the same
2508 // name for which the debugging information reports that they were
2509 // defined in different source locations. When comparing the source
2510 // location, we consider instances with the same base filename and
2511 // line number to be the same. This is because different object
2512 // files/shared libraries can include the same header file using
2513 // different paths, and we don't want to report an ODR violation in
2516 // This struct is used to compare line information, as returned by
2517 // Dwarf_line_info::one_addr2line. It implements a < comparison
2518 // operator used with std::set.
2520 struct Odr_violation_compare
2523 operator()(const std::string
& s1
, const std::string
& s2
) const
2525 std::string::size_type pos1
= s1
.rfind('/');
2526 std::string::size_type pos2
= s2
.rfind('/');
2527 if (pos1
== std::string::npos
2528 || pos2
== std::string::npos
)
2530 return s1
.compare(pos1
, std::string::npos
,
2531 s2
, pos2
, std::string::npos
) < 0;
2535 // Check candidate_odr_violations_ to find symbols with the same name
2536 // but apparently different definitions (different source-file/line-no).
2539 Symbol_table::detect_odr_violations(const Task
* task
,
2540 const char* output_file_name
) const
2542 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2543 it
!= candidate_odr_violations_
.end();
2546 const char* symbol_name
= it
->first
;
2547 // We use a sorted set so the output is deterministic.
2548 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2550 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2551 locs
= it
->second
.begin();
2552 locs
!= it
->second
.end();
2555 // We need to lock the object in order to read it. This
2556 // means that we have to run in a singleton Task. If we
2557 // want to run this in a general Task for better
2558 // performance, we will need one Task for object, plus
2559 // appropriate locking to ensure that we don't conflict with
2560 // other uses of the object. Also note, one_addr2line is not
2561 // currently thread-safe.
2562 Task_lock_obj
<Object
> tl(task
, locs
->object
);
2563 // 16 is the size of the object-cache that one_addr2line should use.
2564 std::string lineno
= Dwarf_line_info::one_addr2line(
2565 locs
->object
, locs
->shndx
, locs
->offset
, 16);
2566 if (!lineno
.empty())
2567 line_nums
.insert(lineno
);
2570 if (line_nums
.size() > 1)
2572 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2573 "places (possible ODR violation):"),
2574 output_file_name
, demangle(symbol_name
).c_str());
2575 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
2576 it2
!= line_nums
.end();
2578 fprintf(stderr
, " %s\n", it2
->c_str());
2581 // We only call one_addr2line() in this function, so we can clear its cache.
2582 Dwarf_line_info::clear_addr2line_cache();
2585 // Warnings functions.
2587 // Add a new warning.
2590 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
2591 const std::string
& warning
)
2593 name
= symtab
->canonicalize_name(name
);
2594 this->warnings_
[name
].set(obj
, warning
);
2597 // Look through the warnings and mark the symbols for which we should
2598 // warn. This is called during Layout::finalize when we know the
2599 // sources for all the symbols.
2602 Warnings::note_warnings(Symbol_table
* symtab
)
2604 for (Warning_table::iterator p
= this->warnings_
.begin();
2605 p
!= this->warnings_
.end();
2608 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2610 && sym
->source() == Symbol::FROM_OBJECT
2611 && sym
->object() == p
->second
.object
)
2612 sym
->set_has_warning();
2616 // Issue a warning. This is called when we see a relocation against a
2617 // symbol for which has a warning.
2619 template<int size
, bool big_endian
>
2621 Warnings::issue_warning(const Symbol
* sym
,
2622 const Relocate_info
<size
, big_endian
>* relinfo
,
2623 size_t relnum
, off_t reloffset
) const
2625 gold_assert(sym
->has_warning());
2626 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2627 gold_assert(p
!= this->warnings_
.end());
2628 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2629 "%s", p
->second
.text
.c_str());
2632 // Instantiate the templates we need. We could use the configure
2633 // script to restrict this to only the ones needed for implemented
2636 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2639 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2642 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2645 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2648 #ifdef HAVE_TARGET_32_LITTLE
2651 Symbol_table::add_from_relobj
<32, false>(
2652 Sized_relobj
<32, false>* relobj
,
2653 const unsigned char* syms
,
2655 size_t symndx_offset
,
2656 const char* sym_names
,
2657 size_t sym_name_size
,
2658 Sized_relobj
<32, true>::Symbols
* sympointers
,
2662 #ifdef HAVE_TARGET_32_BIG
2665 Symbol_table::add_from_relobj
<32, true>(
2666 Sized_relobj
<32, true>* relobj
,
2667 const unsigned char* syms
,
2669 size_t symndx_offset
,
2670 const char* sym_names
,
2671 size_t sym_name_size
,
2672 Sized_relobj
<32, false>::Symbols
* sympointers
,
2676 #ifdef HAVE_TARGET_64_LITTLE
2679 Symbol_table::add_from_relobj
<64, false>(
2680 Sized_relobj
<64, false>* relobj
,
2681 const unsigned char* syms
,
2683 size_t symndx_offset
,
2684 const char* sym_names
,
2685 size_t sym_name_size
,
2686 Sized_relobj
<64, true>::Symbols
* sympointers
,
2690 #ifdef HAVE_TARGET_64_BIG
2693 Symbol_table::add_from_relobj
<64, true>(
2694 Sized_relobj
<64, true>* relobj
,
2695 const unsigned char* syms
,
2697 size_t symndx_offset
,
2698 const char* sym_names
,
2699 size_t sym_name_size
,
2700 Sized_relobj
<64, false>::Symbols
* sympointers
,
2704 #ifdef HAVE_TARGET_32_LITTLE
2707 Symbol_table::add_from_dynobj
<32, false>(
2708 Sized_dynobj
<32, false>* dynobj
,
2709 const unsigned char* syms
,
2711 const char* sym_names
,
2712 size_t sym_name_size
,
2713 const unsigned char* versym
,
2715 const std::vector
<const char*>* version_map
,
2716 Sized_relobj
<32, false>::Symbols
* sympointers
,
2720 #ifdef HAVE_TARGET_32_BIG
2723 Symbol_table::add_from_dynobj
<32, true>(
2724 Sized_dynobj
<32, true>* dynobj
,
2725 const unsigned char* syms
,
2727 const char* sym_names
,
2728 size_t sym_name_size
,
2729 const unsigned char* versym
,
2731 const std::vector
<const char*>* version_map
,
2732 Sized_relobj
<32, true>::Symbols
* sympointers
,
2736 #ifdef HAVE_TARGET_64_LITTLE
2739 Symbol_table::add_from_dynobj
<64, false>(
2740 Sized_dynobj
<64, false>* dynobj
,
2741 const unsigned char* syms
,
2743 const char* sym_names
,
2744 size_t sym_name_size
,
2745 const unsigned char* versym
,
2747 const std::vector
<const char*>* version_map
,
2748 Sized_relobj
<64, false>::Symbols
* sympointers
,
2752 #ifdef HAVE_TARGET_64_BIG
2755 Symbol_table::add_from_dynobj
<64, true>(
2756 Sized_dynobj
<64, true>* dynobj
,
2757 const unsigned char* syms
,
2759 const char* sym_names
,
2760 size_t sym_name_size
,
2761 const unsigned char* versym
,
2763 const std::vector
<const char*>* version_map
,
2764 Sized_relobj
<64, true>::Symbols
* sympointers
,
2768 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2771 Symbol_table::define_with_copy_reloc
<32>(
2772 Sized_symbol
<32>* sym
,
2774 elfcpp::Elf_types
<32>::Elf_Addr value
);
2777 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2780 Symbol_table::define_with_copy_reloc
<64>(
2781 Sized_symbol
<64>* sym
,
2783 elfcpp::Elf_types
<64>::Elf_Addr value
);
2786 #ifdef HAVE_TARGET_32_LITTLE
2789 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2790 const Relocate_info
<32, false>* relinfo
,
2791 size_t relnum
, off_t reloffset
) const;
2794 #ifdef HAVE_TARGET_32_BIG
2797 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2798 const Relocate_info
<32, true>* relinfo
,
2799 size_t relnum
, off_t reloffset
) const;
2802 #ifdef HAVE_TARGET_64_LITTLE
2805 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2806 const Relocate_info
<64, false>* relinfo
,
2807 size_t relnum
, off_t reloffset
) const;
2810 #ifdef HAVE_TARGET_64_BIG
2813 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2814 const Relocate_info
<64, true>* relinfo
,
2815 size_t relnum
, off_t reloffset
) const;
2818 } // End namespace gold.