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"
47 // Initialize fields in Symbol. This initializes everything except u_
51 Symbol::init_fields(const char* name
, const char* version
,
52 elfcpp::STT type
, elfcpp::STB binding
,
53 elfcpp::STV visibility
, unsigned char nonvis
)
56 this->version_
= version
;
57 this->symtab_index_
= 0;
58 this->dynsym_index_
= 0;
59 this->got_offsets_
.init();
60 this->plt_offset_
= 0;
62 this->binding_
= binding
;
63 this->visibility_
= visibility
;
64 this->nonvis_
= nonvis
;
65 this->is_target_special_
= false;
66 this->is_def_
= false;
67 this->is_forwarder_
= false;
68 this->has_alias_
= false;
69 this->needs_dynsym_entry_
= false;
70 this->in_reg_
= false;
71 this->in_dyn_
= false;
72 this->has_plt_offset_
= false;
73 this->has_warning_
= false;
74 this->is_copied_from_dynobj_
= false;
75 this->is_forced_local_
= false;
76 this->is_ordinary_shndx_
= false;
77 this->in_real_elf_
= false;
80 // Return the demangled version of the symbol's name, but only
81 // if the --demangle flag was set.
84 demangle(const char* name
)
86 if (!parameters
->options().do_demangle())
89 // cplus_demangle allocates memory for the result it returns,
90 // and returns NULL if the name is already demangled.
91 char* demangled_name
= cplus_demangle(name
, DMGL_ANSI
| DMGL_PARAMS
);
92 if (demangled_name
== NULL
)
95 std::string
retval(demangled_name
);
101 Symbol::demangled_name() const
103 return demangle(this->name());
106 // Initialize the fields in the base class Symbol for SYM in OBJECT.
108 template<int size
, bool big_endian
>
110 Symbol::init_base_object(const char* name
, const char* version
, Object
* object
,
111 const elfcpp::Sym
<size
, big_endian
>& sym
,
112 unsigned int st_shndx
, bool is_ordinary
)
114 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
115 sym
.get_st_visibility(), sym
.get_st_nonvis());
116 this->u_
.from_object
.object
= object
;
117 this->u_
.from_object
.shndx
= st_shndx
;
118 this->is_ordinary_shndx_
= is_ordinary
;
119 this->source_
= FROM_OBJECT
;
120 this->in_reg_
= !object
->is_dynamic();
121 this->in_dyn_
= object
->is_dynamic();
122 this->in_real_elf_
= object
->pluginobj() == NULL
;
125 // Initialize the fields in the base class Symbol for a symbol defined
126 // in an Output_data.
129 Symbol::init_base_output_data(const char* name
, const char* version
,
130 Output_data
* od
, elfcpp::STT type
,
131 elfcpp::STB binding
, elfcpp::STV visibility
,
132 unsigned char nonvis
, bool offset_is_from_end
)
134 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
135 this->u_
.in_output_data
.output_data
= od
;
136 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
137 this->source_
= IN_OUTPUT_DATA
;
138 this->in_reg_
= true;
139 this->in_real_elf_
= true;
142 // Initialize the fields in the base class Symbol for a symbol defined
143 // in an Output_segment.
146 Symbol::init_base_output_segment(const char* name
, const char* version
,
147 Output_segment
* os
, elfcpp::STT type
,
148 elfcpp::STB binding
, elfcpp::STV visibility
,
149 unsigned char nonvis
,
150 Segment_offset_base offset_base
)
152 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
153 this->u_
.in_output_segment
.output_segment
= os
;
154 this->u_
.in_output_segment
.offset_base
= offset_base
;
155 this->source_
= IN_OUTPUT_SEGMENT
;
156 this->in_reg_
= true;
157 this->in_real_elf_
= true;
160 // Initialize the fields in the base class Symbol for a symbol defined
164 Symbol::init_base_constant(const char* name
, const char* version
,
165 elfcpp::STT type
, elfcpp::STB binding
,
166 elfcpp::STV visibility
, unsigned char nonvis
)
168 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
169 this->source_
= IS_CONSTANT
;
170 this->in_reg_
= true;
171 this->in_real_elf_
= true;
174 // Initialize the fields in the base class Symbol for an undefined
178 Symbol::init_base_undefined(const char* name
, const char* version
,
179 elfcpp::STT type
, elfcpp::STB binding
,
180 elfcpp::STV visibility
, unsigned char nonvis
)
182 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
183 this->dynsym_index_
= -1U;
184 this->source_
= IS_UNDEFINED
;
185 this->in_reg_
= true;
186 this->in_real_elf_
= true;
189 // Allocate a common symbol in the base.
192 Symbol::allocate_base_common(Output_data
* od
)
194 gold_assert(this->is_common());
195 this->source_
= IN_OUTPUT_DATA
;
196 this->u_
.in_output_data
.output_data
= od
;
197 this->u_
.in_output_data
.offset_is_from_end
= false;
200 // Initialize the fields in Sized_symbol for SYM in OBJECT.
203 template<bool big_endian
>
205 Sized_symbol
<size
>::init_object(const char* name
, const char* version
,
207 const elfcpp::Sym
<size
, big_endian
>& sym
,
208 unsigned int st_shndx
, bool is_ordinary
)
210 this->init_base_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
211 this->value_
= sym
.get_st_value();
212 this->symsize_
= sym
.get_st_size();
215 // Initialize the fields in Sized_symbol for a symbol defined in an
220 Sized_symbol
<size
>::init_output_data(const char* name
, const char* version
,
221 Output_data
* od
, Value_type value
,
222 Size_type symsize
, elfcpp::STT type
,
224 elfcpp::STV visibility
,
225 unsigned char nonvis
,
226 bool offset_is_from_end
)
228 this->init_base_output_data(name
, version
, od
, type
, binding
, visibility
,
229 nonvis
, offset_is_from_end
);
230 this->value_
= value
;
231 this->symsize_
= symsize
;
234 // Initialize the fields in Sized_symbol for a symbol defined in an
239 Sized_symbol
<size
>::init_output_segment(const char* name
, const char* version
,
240 Output_segment
* os
, Value_type value
,
241 Size_type symsize
, elfcpp::STT type
,
243 elfcpp::STV visibility
,
244 unsigned char nonvis
,
245 Segment_offset_base offset_base
)
247 this->init_base_output_segment(name
, version
, os
, type
, binding
, visibility
,
248 nonvis
, offset_base
);
249 this->value_
= value
;
250 this->symsize_
= symsize
;
253 // Initialize the fields in Sized_symbol for a symbol defined as a
258 Sized_symbol
<size
>::init_constant(const char* name
, const char* version
,
259 Value_type value
, Size_type symsize
,
260 elfcpp::STT type
, elfcpp::STB binding
,
261 elfcpp::STV visibility
, unsigned char nonvis
)
263 this->init_base_constant(name
, version
, type
, binding
, visibility
, nonvis
);
264 this->value_
= value
;
265 this->symsize_
= symsize
;
268 // Initialize the fields in Sized_symbol for an undefined symbol.
272 Sized_symbol
<size
>::init_undefined(const char* name
, const char* version
,
273 elfcpp::STT type
, elfcpp::STB binding
,
274 elfcpp::STV visibility
, unsigned char nonvis
)
276 this->init_base_undefined(name
, version
, type
, binding
, visibility
, nonvis
);
281 // Allocate a common symbol.
285 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
287 this->allocate_base_common(od
);
288 this->value_
= value
;
291 // Return true if this symbol should be added to the dynamic symbol
295 Symbol::should_add_dynsym_entry() const
297 // If the symbol is used by a dynamic relocation, we need to add it.
298 if (this->needs_dynsym_entry())
301 // If the symbol was forced local in a version script, do not add it.
302 if (this->is_forced_local())
305 // If exporting all symbols or building a shared library,
306 // and the symbol is defined in a regular object and is
307 // externally visible, we need to add it.
308 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
309 && !this->is_from_dynobj()
310 && this->is_externally_visible())
316 // Return true if the final value of this symbol is known at link
320 Symbol::final_value_is_known() const
322 // If we are not generating an executable, then no final values are
323 // known, since they will change at runtime.
324 if (parameters
->options().shared() || parameters
->options().relocatable())
327 // If the symbol is not from an object file, and is not undefined,
328 // then it is defined, and known.
329 if (this->source_
!= FROM_OBJECT
)
331 if (this->source_
!= IS_UNDEFINED
)
336 // If the symbol is from a dynamic object, then the final value
338 if (this->object()->is_dynamic())
341 // If the symbol is not undefined (it is defined or common),
342 // then the final value is known.
343 if (!this->is_undefined())
347 // If the symbol is undefined, then whether the final value is known
348 // depends on whether we are doing a static link. If we are doing a
349 // dynamic link, then the final value could be filled in at runtime.
350 // This could reasonably be the case for a weak undefined symbol.
351 return parameters
->doing_static_link();
354 // Return the output section where this symbol is defined.
357 Symbol::output_section() const
359 switch (this->source_
)
363 unsigned int shndx
= this->u_
.from_object
.shndx
;
364 if (shndx
!= elfcpp::SHN_UNDEF
&& this->is_ordinary_shndx_
)
366 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
367 gold_assert(this->u_
.from_object
.object
->pluginobj() == NULL
);
368 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
369 return relobj
->output_section(shndx
);
375 return this->u_
.in_output_data
.output_data
->output_section();
377 case IN_OUTPUT_SEGMENT
:
387 // Set the symbol's output section. This is used for symbols defined
388 // in scripts. This should only be called after the symbol table has
392 Symbol::set_output_section(Output_section
* os
)
394 switch (this->source_
)
398 gold_assert(this->output_section() == os
);
401 this->source_
= IN_OUTPUT_DATA
;
402 this->u_
.in_output_data
.output_data
= os
;
403 this->u_
.in_output_data
.offset_is_from_end
= false;
405 case IN_OUTPUT_SEGMENT
:
412 // Class Symbol_table.
414 Symbol_table::Symbol_table(unsigned int count
,
415 const Version_script_info
& version_script
)
416 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
417 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
418 version_script_(version_script
)
420 namepool_
.reserve(count
);
423 Symbol_table::~Symbol_table()
427 // The hash function. The key values are Stringpool keys.
430 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
432 return key
.first
^ key
.second
;
435 // The symbol table key equality function. This is called with
439 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
440 const Symbol_table_key
& k2
) const
442 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
445 // Make TO a symbol which forwards to FROM.
448 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
450 gold_assert(from
!= to
);
451 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
452 this->forwarders_
[from
] = to
;
453 from
->set_forwarder();
456 // Resolve the forwards from FROM, returning the real symbol.
459 Symbol_table::resolve_forwards(const Symbol
* from
) const
461 gold_assert(from
->is_forwarder());
462 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
463 this->forwarders_
.find(from
);
464 gold_assert(p
!= this->forwarders_
.end());
468 // Look up a symbol by name.
471 Symbol_table::lookup(const char* name
, const char* version
) const
473 Stringpool::Key name_key
;
474 name
= this->namepool_
.find(name
, &name_key
);
478 Stringpool::Key version_key
= 0;
481 version
= this->namepool_
.find(version
, &version_key
);
486 Symbol_table_key
key(name_key
, version_key
);
487 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
488 if (p
== this->table_
.end())
493 // Resolve a Symbol with another Symbol. This is only used in the
494 // unusual case where there are references to both an unversioned
495 // symbol and a symbol with a version, and we then discover that that
496 // version is the default version. Because this is unusual, we do
497 // this the slow way, by converting back to an ELF symbol.
499 template<int size
, bool big_endian
>
501 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
)
503 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
504 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
505 // We don't bother to set the st_name or the st_shndx field.
506 esym
.put_st_value(from
->value());
507 esym
.put_st_size(from
->symsize());
508 esym
.put_st_info(from
->binding(), from
->type());
509 esym
.put_st_other(from
->visibility(), from
->nonvis());
511 unsigned int shndx
= from
->shndx(&is_ordinary
);
512 this->resolve(to
, esym
.sym(), shndx
, is_ordinary
, shndx
, from
->object(),
520 // Record that a symbol is forced to be local by a version script.
523 Symbol_table::force_local(Symbol
* sym
)
525 if (!sym
->is_defined() && !sym
->is_common())
527 if (sym
->is_forced_local())
529 // We already got this one.
532 sym
->set_is_forced_local();
533 this->forced_locals_
.push_back(sym
);
536 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
537 // is only called for undefined symbols, when at least one --wrap
541 Symbol_table::wrap_symbol(Object
* object
, const char* name
,
542 Stringpool::Key
* name_key
)
544 // For some targets, we need to ignore a specific character when
545 // wrapping, and add it back later.
547 if (name
[0] == object
->target()->wrap_char())
553 if (parameters
->options().is_wrap(name
))
555 // Turn NAME into __wrap_NAME.
562 // This will give us both the old and new name in NAMEPOOL_, but
563 // that is OK. Only the versions we need will wind up in the
564 // real string table in the output file.
565 return this->namepool_
.add(s
.c_str(), true, name_key
);
568 const char* const real_prefix
= "__real_";
569 const size_t real_prefix_length
= strlen(real_prefix
);
570 if (strncmp(name
, real_prefix
, real_prefix_length
) == 0
571 && parameters
->options().is_wrap(name
+ real_prefix_length
))
573 // Turn __real_NAME into NAME.
577 s
+= name
+ real_prefix_length
;
578 return this->namepool_
.add(s
.c_str(), true, name_key
);
584 // Add one symbol from OBJECT to the symbol table. NAME is symbol
585 // name and VERSION is the version; both are canonicalized. DEF is
586 // whether this is the default version. ST_SHNDX is the symbol's
587 // section index; IS_ORDINARY is whether this is a normal section
588 // rather than a special code.
590 // If DEF is true, then this is the definition of a default version of
591 // a symbol. That means that any lookup of NAME/NULL and any lookup
592 // of NAME/VERSION should always return the same symbol. This is
593 // obvious for references, but in particular we want to do this for
594 // definitions: overriding NAME/NULL should also override
595 // NAME/VERSION. If we don't do that, it would be very hard to
596 // override functions in a shared library which uses versioning.
598 // We implement this by simply making both entries in the hash table
599 // point to the same Symbol structure. That is easy enough if this is
600 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
601 // that we have seen both already, in which case they will both have
602 // independent entries in the symbol table. We can't simply change
603 // the symbol table entry, because we have pointers to the entries
604 // attached to the object files. So we mark the entry attached to the
605 // object file as a forwarder, and record it in the forwarders_ map.
606 // Note that entries in the hash table will never be marked as
609 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
610 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
611 // for a special section code. ST_SHNDX may be modified if the symbol
612 // is defined in a section being discarded.
614 template<int size
, bool big_endian
>
616 Symbol_table::add_from_object(Object
* object
,
618 Stringpool::Key name_key
,
620 Stringpool::Key version_key
,
622 const elfcpp::Sym
<size
, big_endian
>& sym
,
623 unsigned int st_shndx
,
625 unsigned int orig_st_shndx
)
627 // Print a message if this symbol is being traced.
628 if (parameters
->options().is_trace_symbol(name
))
630 if (orig_st_shndx
== elfcpp::SHN_UNDEF
)
631 gold_info(_("%s: reference to %s"), object
->name().c_str(), name
);
633 gold_info(_("%s: definition of %s"), object
->name().c_str(), name
);
636 // For an undefined symbol, we may need to adjust the name using
638 if (orig_st_shndx
== elfcpp::SHN_UNDEF
639 && parameters
->options().any_wrap())
641 const char* wrap_name
= this->wrap_symbol(object
, name
, &name_key
);
642 if (wrap_name
!= name
)
644 // If we see a reference to malloc with version GLIBC_2.0,
645 // and we turn it into a reference to __wrap_malloc, then we
646 // discard the version number. Otherwise the user would be
647 // required to specify the correct version for
655 Symbol
* const snull
= NULL
;
656 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
657 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
660 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
661 std::make_pair(this->table_
.end(), false);
664 const Stringpool::Key vnull_key
= 0;
665 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
670 // ins.first: an iterator, which is a pointer to a pair.
671 // ins.first->first: the key (a pair of name and version).
672 // ins.first->second: the value (Symbol*).
673 // ins.second: true if new entry was inserted, false if not.
675 Sized_symbol
<size
>* ret
;
680 // We already have an entry for NAME/VERSION.
681 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
682 gold_assert(ret
!= NULL
);
684 was_undefined
= ret
->is_undefined();
685 was_common
= ret
->is_common();
687 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
694 // This is the first time we have seen NAME/NULL. Make
695 // NAME/NULL point to NAME/VERSION.
696 insdef
.first
->second
= ret
;
698 else if (insdef
.first
->second
!= ret
)
700 // This is the unfortunate case where we already have
701 // entries for both NAME/VERSION and NAME/NULL. We now
702 // see a symbol NAME/VERSION where VERSION is the
703 // default version. We have already resolved this new
704 // symbol with the existing NAME/VERSION symbol.
706 // It's possible that NAME/NULL and NAME/VERSION are
707 // both defined in regular objects. This can only
708 // happen if one object file defines foo and another
709 // defines foo@@ver. This is somewhat obscure, but we
710 // call it a multiple definition error.
712 // It's possible that NAME/NULL actually has a version,
713 // in which case it won't be the same as VERSION. This
714 // happens with ver_test_7.so in the testsuite for the
715 // symbol t2_2. We see t2_2@@VER2, so we define both
716 // t2_2/VER2 and t2_2/NULL. We then see an unadorned
717 // t2_2 in an object file and give it version VER1 from
718 // the version script. This looks like a default
719 // definition for VER1, so it looks like we should merge
720 // t2_2/NULL with t2_2/VER1. That doesn't make sense,
721 // but it's not obvious that this is an error, either.
724 // If one of the symbols has non-default visibility, and
725 // the other is defined in a shared object, then they
726 // are different symbols.
728 // Otherwise, we just resolve the symbols as though they
731 if (insdef
.first
->second
->version() != NULL
)
733 gold_assert(insdef
.first
->second
->version() != version
);
736 else if (ret
->visibility() != elfcpp::STV_DEFAULT
737 && insdef
.first
->second
->is_from_dynobj())
739 else if (insdef
.first
->second
->visibility() != elfcpp::STV_DEFAULT
740 && ret
->is_from_dynobj())
744 const Sized_symbol
<size
>* sym2
;
745 sym2
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
746 Symbol_table::resolve
<size
, big_endian
>(ret
, sym2
);
747 this->make_forwarder(insdef
.first
->second
, ret
);
748 insdef
.first
->second
= ret
;
757 // This is the first time we have seen NAME/VERSION.
758 gold_assert(ins
.first
->second
== NULL
);
760 if (def
&& !insdef
.second
)
762 // We already have an entry for NAME/NULL. If we override
763 // it, then change it to NAME/VERSION.
764 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
766 was_undefined
= ret
->is_undefined();
767 was_common
= ret
->is_common();
769 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
771 ins
.first
->second
= ret
;
775 was_undefined
= false;
778 Sized_target
<size
, big_endian
>* target
=
779 object
->sized_target
<size
, big_endian
>();
780 if (!target
->has_make_symbol())
781 ret
= new Sized_symbol
<size
>();
784 ret
= target
->make_symbol();
787 // This means that we don't want a symbol table
790 this->table_
.erase(ins
.first
);
793 this->table_
.erase(insdef
.first
);
794 // Inserting insdef invalidated ins.
795 this->table_
.erase(std::make_pair(name_key
,
802 ret
->init_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
804 ins
.first
->second
= ret
;
807 // This is the first time we have seen NAME/NULL. Point
808 // it at the new entry for NAME/VERSION.
809 gold_assert(insdef
.second
);
810 insdef
.first
->second
= ret
;
815 // Record every time we see a new undefined symbol, to speed up
817 if (!was_undefined
&& ret
->is_undefined())
818 ++this->saw_undefined_
;
820 // Keep track of common symbols, to speed up common symbol
822 if (!was_common
&& ret
->is_common())
824 if (ret
->type() != elfcpp::STT_TLS
)
825 this->commons_
.push_back(ret
);
827 this->tls_commons_
.push_back(ret
);
831 ret
->set_is_default();
835 // Add all the symbols in a relocatable object to the hash table.
837 template<int size
, bool big_endian
>
839 Symbol_table::add_from_relobj(
840 Sized_relobj
<size
, big_endian
>* relobj
,
841 const unsigned char* syms
,
843 size_t symndx_offset
,
844 const char* sym_names
,
845 size_t sym_name_size
,
846 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
851 gold_assert(size
== relobj
->target()->get_size());
852 gold_assert(size
== parameters
->target().get_size());
854 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
856 const bool just_symbols
= relobj
->just_symbols();
858 const unsigned char* p
= syms
;
859 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
861 (*sympointers
)[i
] = NULL
;
863 elfcpp::Sym
<size
, big_endian
> sym(p
);
865 unsigned int st_name
= sym
.get_st_name();
866 if (st_name
>= sym_name_size
)
868 relobj
->error(_("bad global symbol name offset %u at %zu"),
873 const char* name
= sym_names
+ st_name
;
876 unsigned int st_shndx
= relobj
->adjust_sym_shndx(i
+ symndx_offset
,
879 unsigned int orig_st_shndx
= st_shndx
;
881 orig_st_shndx
= elfcpp::SHN_UNDEF
;
883 if (st_shndx
!= elfcpp::SHN_UNDEF
)
886 // A symbol defined in a section which we are not including must
887 // be treated as an undefined symbol.
888 if (st_shndx
!= elfcpp::SHN_UNDEF
890 && !relobj
->is_section_included(st_shndx
))
891 st_shndx
= elfcpp::SHN_UNDEF
;
893 // In an object file, an '@' in the name separates the symbol
894 // name from the version name. If there are two '@' characters,
895 // this is the default version.
896 const char* ver
= strchr(name
, '@');
897 Stringpool::Key ver_key
= 0;
899 // DEF: is the version default? LOCAL: is the symbol forced local?
905 // The symbol name is of the form foo@VERSION or foo@@VERSION
906 namelen
= ver
- name
;
913 ver
= this->namepool_
.add(ver
, true, &ver_key
);
915 // We don't want to assign a version to an undefined symbol,
916 // even if it is listed in the version script. FIXME: What
917 // about a common symbol?
920 namelen
= strlen(name
);
921 if (!this->version_script_
.empty()
922 && st_shndx
!= elfcpp::SHN_UNDEF
)
924 // The symbol name did not have a version, but the
925 // version script may assign a version anyway.
927 if (this->version_script_
.get_symbol_version(name
, &version
))
929 // The version can be empty if the version script is
930 // only used to force some symbols to be local.
931 if (!version
.empty())
933 ver
= this->namepool_
.add_with_length(version
.c_str(),
940 else if (this->version_script_
.symbol_is_local(name
))
945 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
946 unsigned char symbuf
[sym_size
];
947 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
950 memcpy(symbuf
, p
, sym_size
);
951 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
952 if (orig_st_shndx
!= elfcpp::SHN_UNDEF
&& is_ordinary
)
954 // Symbol values in object files are section relative.
955 // This is normally what we want, but since here we are
956 // converting the symbol to absolute we need to add the
957 // section address. The section address in an object
958 // file is normally zero, but people can use a linker
959 // script to change it.
960 sw
.put_st_value(sym
.get_st_value()
961 + relobj
->section_address(orig_st_shndx
));
963 st_shndx
= elfcpp::SHN_ABS
;
968 Stringpool::Key name_key
;
969 name
= this->namepool_
.add_with_length(name
, namelen
, true,
972 Sized_symbol
<size
>* res
;
973 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
974 def
, *psym
, st_shndx
, is_ordinary
,
978 this->force_local(res
);
980 (*sympointers
)[i
] = res
;
984 // Add a symbol from a plugin-claimed file.
986 template<int size
, bool big_endian
>
988 Symbol_table::add_from_pluginobj(
989 Sized_pluginobj
<size
, big_endian
>* obj
,
992 elfcpp::Sym
<size
, big_endian
>* sym
)
994 unsigned int st_shndx
= sym
->get_st_shndx();
996 Stringpool::Key ver_key
= 0;
1002 ver
= this->namepool_
.add(ver
, true, &ver_key
);
1004 // We don't want to assign a version to an undefined symbol,
1005 // even if it is listed in the version script. FIXME: What
1006 // about a common symbol?
1009 if (!this->version_script_
.empty()
1010 && st_shndx
!= elfcpp::SHN_UNDEF
)
1012 // The symbol name did not have a version, but the
1013 // version script may assign a version anyway.
1014 std::string version
;
1015 if (this->version_script_
.get_symbol_version(name
, &version
))
1017 // The version can be empty if the version script is
1018 // only used to force some symbols to be local.
1019 if (!version
.empty())
1021 ver
= this->namepool_
.add_with_length(version
.c_str(),
1028 else if (this->version_script_
.symbol_is_local(name
))
1033 Stringpool::Key name_key
;
1034 name
= this->namepool_
.add(name
, true, &name_key
);
1036 Sized_symbol
<size
>* res
;
1037 res
= this->add_from_object(obj
, name
, name_key
, ver
, ver_key
,
1038 def
, *sym
, st_shndx
, true, st_shndx
);
1041 this->force_local(res
);
1046 // Add all the symbols in a dynamic object to the hash table.
1048 template<int size
, bool big_endian
>
1050 Symbol_table::add_from_dynobj(
1051 Sized_dynobj
<size
, big_endian
>* dynobj
,
1052 const unsigned char* syms
,
1054 const char* sym_names
,
1055 size_t sym_name_size
,
1056 const unsigned char* versym
,
1058 const std::vector
<const char*>* version_map
,
1059 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
1064 gold_assert(size
== dynobj
->target()->get_size());
1065 gold_assert(size
== parameters
->target().get_size());
1067 if (dynobj
->just_symbols())
1069 gold_error(_("--just-symbols does not make sense with a shared object"));
1073 if (versym
!= NULL
&& versym_size
/ 2 < count
)
1075 dynobj
->error(_("too few symbol versions"));
1079 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1081 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1082 // weak aliases. This is necessary because if the dynamic object
1083 // provides the same variable under two names, one of which is a
1084 // weak definition, and the regular object refers to the weak
1085 // definition, we have to put both the weak definition and the
1086 // strong definition into the dynamic symbol table. Given a weak
1087 // definition, the only way that we can find the corresponding
1088 // strong definition, if any, is to search the symbol table.
1089 std::vector
<Sized_symbol
<size
>*> object_symbols
;
1091 const unsigned char* p
= syms
;
1092 const unsigned char* vs
= versym
;
1093 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
1095 elfcpp::Sym
<size
, big_endian
> sym(p
);
1097 if (sympointers
!= NULL
)
1098 (*sympointers
)[i
] = NULL
;
1100 // Ignore symbols with local binding or that have
1101 // internal or hidden visibility.
1102 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
1103 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
1104 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
1107 // A protected symbol in a shared library must be treated as a
1108 // normal symbol when viewed from outside the shared library.
1109 // Implement this by overriding the visibility here.
1110 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1111 unsigned char symbuf
[sym_size
];
1112 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1113 if (sym
.get_st_visibility() == elfcpp::STV_PROTECTED
)
1115 memcpy(symbuf
, p
, sym_size
);
1116 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1117 sw
.put_st_other(elfcpp::STV_DEFAULT
, sym
.get_st_nonvis());
1121 unsigned int st_name
= psym
->get_st_name();
1122 if (st_name
>= sym_name_size
)
1124 dynobj
->error(_("bad symbol name offset %u at %zu"),
1129 const char* name
= sym_names
+ st_name
;
1132 unsigned int st_shndx
= dynobj
->adjust_sym_shndx(i
, psym
->get_st_shndx(),
1135 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1138 Sized_symbol
<size
>* res
;
1142 Stringpool::Key name_key
;
1143 name
= this->namepool_
.add(name
, true, &name_key
);
1144 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1145 false, *psym
, st_shndx
, is_ordinary
,
1150 // Read the version information.
1152 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
1154 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
1155 v
&= elfcpp::VERSYM_VERSION
;
1157 // The Sun documentation says that V can be VER_NDX_LOCAL,
1158 // or VER_NDX_GLOBAL, or a version index. The meaning of
1159 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1160 // The old GNU linker will happily generate VER_NDX_LOCAL
1161 // for an undefined symbol. I don't know what the Sun
1162 // linker will generate.
1164 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1165 && st_shndx
!= elfcpp::SHN_UNDEF
)
1167 // This symbol should not be visible outside the object.
1171 // At this point we are definitely going to add this symbol.
1172 Stringpool::Key name_key
;
1173 name
= this->namepool_
.add(name
, true, &name_key
);
1175 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1176 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
1178 // This symbol does not have a version.
1179 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1180 false, *psym
, st_shndx
, is_ordinary
,
1185 if (v
>= version_map
->size())
1187 dynobj
->error(_("versym for symbol %zu out of range: %u"),
1192 const char* version
= (*version_map
)[v
];
1193 if (version
== NULL
)
1195 dynobj
->error(_("versym for symbol %zu has no name: %u"),
1200 Stringpool::Key version_key
;
1201 version
= this->namepool_
.add(version
, true, &version_key
);
1203 // If this is an absolute symbol, and the version name
1204 // and symbol name are the same, then this is the
1205 // version definition symbol. These symbols exist to
1206 // support using -u to pull in particular versions. We
1207 // do not want to record a version for them.
1208 if (st_shndx
== elfcpp::SHN_ABS
1210 && name_key
== version_key
)
1211 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1212 false, *psym
, st_shndx
, is_ordinary
,
1216 const bool def
= (!hidden
1217 && st_shndx
!= elfcpp::SHN_UNDEF
);
1218 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
1219 version_key
, def
, *psym
, st_shndx
,
1220 is_ordinary
, st_shndx
);
1225 // Note that it is possible that RES was overridden by an
1226 // earlier object, in which case it can't be aliased here.
1227 if (st_shndx
!= elfcpp::SHN_UNDEF
1229 && psym
->get_st_type() == elfcpp::STT_OBJECT
1230 && res
->source() == Symbol::FROM_OBJECT
1231 && res
->object() == dynobj
)
1232 object_symbols
.push_back(res
);
1234 if (sympointers
!= NULL
)
1235 (*sympointers
)[i
] = res
;
1238 this->record_weak_aliases(&object_symbols
);
1241 // This is used to sort weak aliases. We sort them first by section
1242 // index, then by offset, then by weak ahead of strong.
1245 class Weak_alias_sorter
1248 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
1253 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
1254 const Sized_symbol
<size
>* s2
) const
1257 unsigned int s1_shndx
= s1
->shndx(&is_ordinary
);
1258 gold_assert(is_ordinary
);
1259 unsigned int s2_shndx
= s2
->shndx(&is_ordinary
);
1260 gold_assert(is_ordinary
);
1261 if (s1_shndx
!= s2_shndx
)
1262 return s1_shndx
< s2_shndx
;
1264 if (s1
->value() != s2
->value())
1265 return s1
->value() < s2
->value();
1266 if (s1
->binding() != s2
->binding())
1268 if (s1
->binding() == elfcpp::STB_WEAK
)
1270 if (s2
->binding() == elfcpp::STB_WEAK
)
1273 return std::string(s1
->name()) < std::string(s2
->name());
1276 // SYMBOLS is a list of object symbols from a dynamic object. Look
1277 // for any weak aliases, and record them so that if we add the weak
1278 // alias to the dynamic symbol table, we also add the corresponding
1283 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
1285 // Sort the vector by section index, then by offset, then by weak
1287 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
1289 // Walk through the vector. For each weak definition, record
1291 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
1293 p
!= symbols
->end();
1296 if ((*p
)->binding() != elfcpp::STB_WEAK
)
1299 // Build a circular list of weak aliases. Each symbol points to
1300 // the next one in the circular list.
1302 Sized_symbol
<size
>* from_sym
= *p
;
1303 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
1304 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
1307 if ((*q
)->shndx(&dummy
) != from_sym
->shndx(&dummy
)
1308 || (*q
)->value() != from_sym
->value())
1311 this->weak_aliases_
[from_sym
] = *q
;
1312 from_sym
->set_has_alias();
1318 this->weak_aliases_
[from_sym
] = *p
;
1319 from_sym
->set_has_alias();
1326 // Create and return a specially defined symbol. If ONLY_IF_REF is
1327 // true, then only create the symbol if there is a reference to it.
1328 // If this does not return NULL, it sets *POLDSYM to the existing
1329 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1331 template<int size
, bool big_endian
>
1333 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1335 Sized_symbol
<size
>** poldsym
)
1338 Sized_symbol
<size
>* sym
;
1339 bool add_to_table
= false;
1340 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1342 // If the caller didn't give us a version, see if we get one from
1343 // the version script.
1345 if (*pversion
== NULL
)
1347 if (this->version_script_
.get_symbol_version(*pname
, &v
))
1350 *pversion
= v
.c_str();
1356 oldsym
= this->lookup(*pname
, *pversion
);
1357 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1360 *pname
= oldsym
->name();
1361 *pversion
= oldsym
->version();
1365 // Canonicalize NAME and VERSION.
1366 Stringpool::Key name_key
;
1367 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1369 Stringpool::Key version_key
= 0;
1370 if (*pversion
!= NULL
)
1371 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1373 Symbol
* const snull
= NULL
;
1374 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1375 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1381 // We already have a symbol table entry for NAME/VERSION.
1382 oldsym
= ins
.first
->second
;
1383 gold_assert(oldsym
!= NULL
);
1387 // We haven't seen this symbol before.
1388 gold_assert(ins
.first
->second
== NULL
);
1389 add_to_table
= true;
1390 add_loc
= ins
.first
;
1395 const Target
& target
= parameters
->target();
1396 if (!target
.has_make_symbol())
1397 sym
= new Sized_symbol
<size
>();
1400 gold_assert(target
.get_size() == size
);
1401 gold_assert(target
.is_big_endian() ? big_endian
: !big_endian
);
1402 typedef Sized_target
<size
, big_endian
> My_target
;
1403 const My_target
* sized_target
=
1404 static_cast<const My_target
*>(&target
);
1405 sym
= sized_target
->make_symbol();
1411 add_loc
->second
= sym
;
1413 gold_assert(oldsym
!= NULL
);
1415 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1420 // Define a symbol based on an Output_data.
1423 Symbol_table::define_in_output_data(const char* name
,
1424 const char* version
,
1429 elfcpp::STB binding
,
1430 elfcpp::STV visibility
,
1431 unsigned char nonvis
,
1432 bool offset_is_from_end
,
1435 if (parameters
->target().get_size() == 32)
1437 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1438 return this->do_define_in_output_data
<32>(name
, version
, od
,
1439 value
, symsize
, type
, binding
,
1447 else if (parameters
->target().get_size() == 64)
1449 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1450 return this->do_define_in_output_data
<64>(name
, version
, od
,
1451 value
, symsize
, type
, binding
,
1463 // Define a symbol in an Output_data, sized version.
1467 Symbol_table::do_define_in_output_data(
1469 const char* version
,
1471 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1472 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1474 elfcpp::STB binding
,
1475 elfcpp::STV visibility
,
1476 unsigned char nonvis
,
1477 bool offset_is_from_end
,
1480 Sized_symbol
<size
>* sym
;
1481 Sized_symbol
<size
>* oldsym
;
1483 if (parameters
->target().is_big_endian())
1485 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1486 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1487 only_if_ref
, &oldsym
);
1494 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1495 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1496 only_if_ref
, &oldsym
);
1505 sym
->init_output_data(name
, version
, od
, value
, symsize
, type
, binding
,
1506 visibility
, nonvis
, offset_is_from_end
);
1510 if (binding
== elfcpp::STB_LOCAL
1511 || this->version_script_
.symbol_is_local(name
))
1512 this->force_local(sym
);
1513 else if (version
!= NULL
)
1514 sym
->set_is_default();
1518 if (Symbol_table::should_override_with_special(oldsym
))
1519 this->override_with_special(oldsym
, sym
);
1524 // Define a symbol based on an Output_segment.
1527 Symbol_table::define_in_output_segment(const char* name
,
1528 const char* version
, Output_segment
* os
,
1532 elfcpp::STB binding
,
1533 elfcpp::STV visibility
,
1534 unsigned char nonvis
,
1535 Symbol::Segment_offset_base offset_base
,
1538 if (parameters
->target().get_size() == 32)
1540 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1541 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1542 value
, symsize
, type
,
1543 binding
, visibility
, nonvis
,
1544 offset_base
, only_if_ref
);
1549 else if (parameters
->target().get_size() == 64)
1551 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1552 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1553 value
, symsize
, type
,
1554 binding
, visibility
, nonvis
,
1555 offset_base
, only_if_ref
);
1564 // Define a symbol in an Output_segment, sized version.
1568 Symbol_table::do_define_in_output_segment(
1570 const char* version
,
1572 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1573 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1575 elfcpp::STB binding
,
1576 elfcpp::STV visibility
,
1577 unsigned char nonvis
,
1578 Symbol::Segment_offset_base offset_base
,
1581 Sized_symbol
<size
>* sym
;
1582 Sized_symbol
<size
>* oldsym
;
1584 if (parameters
->target().is_big_endian())
1586 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1587 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1588 only_if_ref
, &oldsym
);
1595 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1596 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1597 only_if_ref
, &oldsym
);
1606 sym
->init_output_segment(name
, version
, os
, value
, symsize
, type
, binding
,
1607 visibility
, nonvis
, offset_base
);
1611 if (binding
== elfcpp::STB_LOCAL
1612 || this->version_script_
.symbol_is_local(name
))
1613 this->force_local(sym
);
1614 else if (version
!= NULL
)
1615 sym
->set_is_default();
1619 if (Symbol_table::should_override_with_special(oldsym
))
1620 this->override_with_special(oldsym
, sym
);
1625 // Define a special symbol with a constant value. It is a multiple
1626 // definition error if this symbol is already defined.
1629 Symbol_table::define_as_constant(const char* name
,
1630 const char* version
,
1634 elfcpp::STB binding
,
1635 elfcpp::STV visibility
,
1636 unsigned char nonvis
,
1638 bool force_override
)
1640 if (parameters
->target().get_size() == 32)
1642 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1643 return this->do_define_as_constant
<32>(name
, version
, value
,
1644 symsize
, type
, binding
,
1645 visibility
, nonvis
, only_if_ref
,
1651 else if (parameters
->target().get_size() == 64)
1653 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1654 return this->do_define_as_constant
<64>(name
, version
, value
,
1655 symsize
, type
, binding
,
1656 visibility
, nonvis
, only_if_ref
,
1666 // Define a symbol as a constant, sized version.
1670 Symbol_table::do_define_as_constant(
1672 const char* version
,
1673 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1674 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1676 elfcpp::STB binding
,
1677 elfcpp::STV visibility
,
1678 unsigned char nonvis
,
1680 bool force_override
)
1682 Sized_symbol
<size
>* sym
;
1683 Sized_symbol
<size
>* oldsym
;
1685 if (parameters
->target().is_big_endian())
1687 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1688 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1689 only_if_ref
, &oldsym
);
1696 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1697 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1698 only_if_ref
, &oldsym
);
1707 sym
->init_constant(name
, version
, value
, symsize
, type
, binding
, visibility
,
1712 // Version symbols are absolute symbols with name == version.
1713 // We don't want to force them to be local.
1714 if ((version
== NULL
1717 && (binding
== elfcpp::STB_LOCAL
1718 || this->version_script_
.symbol_is_local(name
)))
1719 this->force_local(sym
);
1720 else if (version
!= NULL
1721 && (name
!= version
|| value
!= 0))
1722 sym
->set_is_default();
1726 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
1727 this->override_with_special(oldsym
, sym
);
1732 // Define a set of symbols in output sections.
1735 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1736 const Define_symbol_in_section
* p
,
1739 for (int i
= 0; i
< count
; ++i
, ++p
)
1741 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1743 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
1744 p
->size
, p
->type
, p
->binding
,
1745 p
->visibility
, p
->nonvis
,
1746 p
->offset_is_from_end
,
1747 only_if_ref
|| p
->only_if_ref
);
1749 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1750 p
->binding
, p
->visibility
, p
->nonvis
,
1751 only_if_ref
|| p
->only_if_ref
,
1756 // Define a set of symbols in output segments.
1759 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1760 const Define_symbol_in_segment
* p
,
1763 for (int i
= 0; i
< count
; ++i
, ++p
)
1765 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1766 p
->segment_flags_set
,
1767 p
->segment_flags_clear
);
1769 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
1770 p
->size
, p
->type
, p
->binding
,
1771 p
->visibility
, p
->nonvis
,
1773 only_if_ref
|| p
->only_if_ref
);
1775 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1776 p
->binding
, p
->visibility
, p
->nonvis
,
1777 only_if_ref
|| p
->only_if_ref
,
1782 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1783 // symbol should be defined--typically a .dyn.bss section. VALUE is
1784 // the offset within POSD.
1788 Symbol_table::define_with_copy_reloc(
1789 Sized_symbol
<size
>* csym
,
1791 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
1793 gold_assert(csym
->is_from_dynobj());
1794 gold_assert(!csym
->is_copied_from_dynobj());
1795 Object
* object
= csym
->object();
1796 gold_assert(object
->is_dynamic());
1797 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1799 // Our copied variable has to override any variable in a shared
1801 elfcpp::STB binding
= csym
->binding();
1802 if (binding
== elfcpp::STB_WEAK
)
1803 binding
= elfcpp::STB_GLOBAL
;
1805 this->define_in_output_data(csym
->name(), csym
->version(),
1806 posd
, value
, csym
->symsize(),
1807 csym
->type(), binding
,
1808 csym
->visibility(), csym
->nonvis(),
1811 csym
->set_is_copied_from_dynobj();
1812 csym
->set_needs_dynsym_entry();
1814 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1816 // We have now defined all aliases, but we have not entered them all
1817 // in the copied_symbol_dynobjs_ map.
1818 if (csym
->has_alias())
1823 sym
= this->weak_aliases_
[sym
];
1826 gold_assert(sym
->output_data() == posd
);
1828 sym
->set_is_copied_from_dynobj();
1829 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1834 // SYM is defined using a COPY reloc. Return the dynamic object where
1835 // the original definition was found.
1838 Symbol_table::get_copy_source(const Symbol
* sym
) const
1840 gold_assert(sym
->is_copied_from_dynobj());
1841 Copied_symbol_dynobjs::const_iterator p
=
1842 this->copied_symbol_dynobjs_
.find(sym
);
1843 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1847 // Add any undefined symbols named on the command line.
1850 Symbol_table::add_undefined_symbols_from_command_line()
1852 if (parameters
->options().any_undefined())
1854 if (parameters
->target().get_size() == 32)
1856 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1857 this->do_add_undefined_symbols_from_command_line
<32>();
1862 else if (parameters
->target().get_size() == 64)
1864 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1865 this->do_add_undefined_symbols_from_command_line
<64>();
1877 Symbol_table::do_add_undefined_symbols_from_command_line()
1879 for (options::String_set::const_iterator p
=
1880 parameters
->options().undefined_begin();
1881 p
!= parameters
->options().undefined_end();
1884 const char* name
= p
->c_str();
1886 if (this->lookup(name
) != NULL
)
1889 const char* version
= NULL
;
1891 Sized_symbol
<size
>* sym
;
1892 Sized_symbol
<size
>* oldsym
;
1893 if (parameters
->target().is_big_endian())
1895 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1896 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1904 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1905 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1912 gold_assert(oldsym
== NULL
);
1914 sym
->init_undefined(name
, version
, elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1915 elfcpp::STV_DEFAULT
, 0);
1916 ++this->saw_undefined_
;
1920 // Set the dynamic symbol indexes. INDEX is the index of the first
1921 // global dynamic symbol. Pointers to the symbols are stored into the
1922 // vector SYMS. The names are added to DYNPOOL. This returns an
1923 // updated dynamic symbol index.
1926 Symbol_table::set_dynsym_indexes(unsigned int index
,
1927 std::vector
<Symbol
*>* syms
,
1928 Stringpool
* dynpool
,
1931 for (Symbol_table_type::iterator p
= this->table_
.begin();
1932 p
!= this->table_
.end();
1935 Symbol
* sym
= p
->second
;
1937 // Note that SYM may already have a dynamic symbol index, since
1938 // some symbols appear more than once in the symbol table, with
1939 // and without a version.
1941 if (!sym
->should_add_dynsym_entry())
1942 sym
->set_dynsym_index(-1U);
1943 else if (!sym
->has_dynsym_index())
1945 sym
->set_dynsym_index(index
);
1947 syms
->push_back(sym
);
1948 dynpool
->add(sym
->name(), false, NULL
);
1950 // Record any version information.
1951 if (sym
->version() != NULL
)
1952 versions
->record_version(this, dynpool
, sym
);
1956 // Finish up the versions. In some cases this may add new dynamic
1958 index
= versions
->finalize(this, index
, syms
);
1963 // Set the final values for all the symbols. The index of the first
1964 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1965 // file offset OFF. Add their names to POOL. Return the new file
1966 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1969 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
1970 size_t dyncount
, Stringpool
* pool
,
1971 unsigned int *plocal_symcount
)
1975 gold_assert(*plocal_symcount
!= 0);
1976 this->first_global_index_
= *plocal_symcount
;
1978 this->dynamic_offset_
= dynoff
;
1979 this->first_dynamic_global_index_
= dyn_global_index
;
1980 this->dynamic_count_
= dyncount
;
1982 if (parameters
->target().get_size() == 32)
1984 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1985 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
1990 else if (parameters
->target().get_size() == 64)
1992 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1993 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
2001 // Now that we have the final symbol table, we can reliably note
2002 // which symbols should get warnings.
2003 this->warnings_
.note_warnings(this);
2008 // SYM is going into the symbol table at *PINDEX. Add the name to
2009 // POOL, update *PINDEX and *POFF.
2013 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
2014 unsigned int* pindex
, off_t
* poff
)
2016 sym
->set_symtab_index(*pindex
);
2017 pool
->add(sym
->name(), false, NULL
);
2019 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
2022 // Set the final value for all the symbols. This is called after
2023 // Layout::finalize, so all the output sections have their final
2028 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
2029 unsigned int* plocal_symcount
)
2031 off
= align_address(off
, size
>> 3);
2032 this->offset_
= off
;
2034 unsigned int index
= *plocal_symcount
;
2035 const unsigned int orig_index
= index
;
2037 // First do all the symbols which have been forced to be local, as
2038 // they must appear before all global symbols.
2039 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
2040 p
!= this->forced_locals_
.end();
2044 gold_assert(sym
->is_forced_local());
2045 if (this->sized_finalize_symbol
<size
>(sym
))
2047 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2052 // Now do all the remaining symbols.
2053 for (Symbol_table_type::iterator p
= this->table_
.begin();
2054 p
!= this->table_
.end();
2057 Symbol
* sym
= p
->second
;
2058 if (this->sized_finalize_symbol
<size
>(sym
))
2059 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2062 this->output_count_
= index
- orig_index
;
2067 // Finalize the symbol SYM. This returns true if the symbol should be
2068 // added to the symbol table, false otherwise.
2072 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
2074 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2076 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
2078 // The default version of a symbol may appear twice in the symbol
2079 // table. We only need to finalize it once.
2080 if (sym
->has_symtab_index())
2085 gold_assert(!sym
->has_symtab_index());
2086 sym
->set_symtab_index(-1U);
2087 gold_assert(sym
->dynsym_index() == -1U);
2093 switch (sym
->source())
2095 case Symbol::FROM_OBJECT
:
2098 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2100 // FIXME: We need some target specific support here.
2102 && shndx
!= elfcpp::SHN_ABS
2103 && shndx
!= elfcpp::SHN_COMMON
)
2105 gold_error(_("%s: unsupported symbol section 0x%x"),
2106 sym
->demangled_name().c_str(), shndx
);
2107 shndx
= elfcpp::SHN_UNDEF
;
2110 Object
* symobj
= sym
->object();
2111 if (symobj
->is_dynamic())
2114 shndx
= elfcpp::SHN_UNDEF
;
2116 else if (symobj
->pluginobj() != NULL
)
2119 shndx
= elfcpp::SHN_UNDEF
;
2121 else if (shndx
== elfcpp::SHN_UNDEF
)
2123 else if (!is_ordinary
2124 && (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
))
2125 value
= sym
->value();
2128 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2129 Output_section
* os
= relobj
->output_section(shndx
);
2133 sym
->set_symtab_index(-1U);
2134 gold_assert(sym
->dynsym_index() == -1U);
2138 uint64_t secoff64
= relobj
->output_section_offset(shndx
);
2139 if (secoff64
== -1ULL)
2141 // The section needs special handling (e.g., a merge section).
2142 value
= os
->output_address(relobj
, shndx
, sym
->value());
2147 convert_types
<Value_type
, uint64_t>(secoff64
);
2148 if (sym
->type() == elfcpp::STT_TLS
)
2149 value
= sym
->value() + os
->tls_offset() + secoff
;
2151 value
= sym
->value() + os
->address() + secoff
;
2157 case Symbol::IN_OUTPUT_DATA
:
2159 Output_data
* od
= sym
->output_data();
2160 value
= sym
->value();
2161 if (sym
->type() != elfcpp::STT_TLS
)
2162 value
+= od
->address();
2165 Output_section
* os
= od
->output_section();
2166 gold_assert(os
!= NULL
);
2167 value
+= os
->tls_offset() + (od
->address() - os
->address());
2169 if (sym
->offset_is_from_end())
2170 value
+= od
->data_size();
2174 case Symbol::IN_OUTPUT_SEGMENT
:
2176 Output_segment
* os
= sym
->output_segment();
2177 value
= sym
->value();
2178 if (sym
->type() != elfcpp::STT_TLS
)
2179 value
+= os
->vaddr();
2180 switch (sym
->offset_base())
2182 case Symbol::SEGMENT_START
:
2184 case Symbol::SEGMENT_END
:
2185 value
+= os
->memsz();
2187 case Symbol::SEGMENT_BSS
:
2188 value
+= os
->filesz();
2196 case Symbol::IS_CONSTANT
:
2197 value
= sym
->value();
2200 case Symbol::IS_UNDEFINED
:
2208 sym
->set_value(value
);
2210 if (parameters
->options().strip_all())
2212 sym
->set_symtab_index(-1U);
2219 // Write out the global symbols.
2222 Symbol_table::write_globals(const Input_objects
* input_objects
,
2223 const Stringpool
* sympool
,
2224 const Stringpool
* dynpool
,
2225 Output_symtab_xindex
* symtab_xindex
,
2226 Output_symtab_xindex
* dynsym_xindex
,
2227 Output_file
* of
) const
2229 switch (parameters
->size_and_endianness())
2231 #ifdef HAVE_TARGET_32_LITTLE
2232 case Parameters::TARGET_32_LITTLE
:
2233 this->sized_write_globals
<32, false>(input_objects
, sympool
,
2234 dynpool
, symtab_xindex
,
2238 #ifdef HAVE_TARGET_32_BIG
2239 case Parameters::TARGET_32_BIG
:
2240 this->sized_write_globals
<32, true>(input_objects
, sympool
,
2241 dynpool
, symtab_xindex
,
2245 #ifdef HAVE_TARGET_64_LITTLE
2246 case Parameters::TARGET_64_LITTLE
:
2247 this->sized_write_globals
<64, false>(input_objects
, sympool
,
2248 dynpool
, symtab_xindex
,
2252 #ifdef HAVE_TARGET_64_BIG
2253 case Parameters::TARGET_64_BIG
:
2254 this->sized_write_globals
<64, true>(input_objects
, sympool
,
2255 dynpool
, symtab_xindex
,
2264 // Write out the global symbols.
2266 template<int size
, bool big_endian
>
2268 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
2269 const Stringpool
* sympool
,
2270 const Stringpool
* dynpool
,
2271 Output_symtab_xindex
* symtab_xindex
,
2272 Output_symtab_xindex
* dynsym_xindex
,
2273 Output_file
* of
) const
2275 const Target
& target
= parameters
->target();
2277 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2279 const unsigned int output_count
= this->output_count_
;
2280 const section_size_type oview_size
= output_count
* sym_size
;
2281 const unsigned int first_global_index
= this->first_global_index_
;
2282 unsigned char* psyms
;
2283 if (this->offset_
== 0 || output_count
== 0)
2286 psyms
= of
->get_output_view(this->offset_
, oview_size
);
2288 const unsigned int dynamic_count
= this->dynamic_count_
;
2289 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
2290 const unsigned int first_dynamic_global_index
=
2291 this->first_dynamic_global_index_
;
2292 unsigned char* dynamic_view
;
2293 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
2294 dynamic_view
= NULL
;
2296 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
2298 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
2299 p
!= this->table_
.end();
2302 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
2304 // Possibly warn about unresolved symbols in shared libraries.
2305 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
2307 unsigned int sym_index
= sym
->symtab_index();
2308 unsigned int dynsym_index
;
2309 if (dynamic_view
== NULL
)
2312 dynsym_index
= sym
->dynsym_index();
2314 if (sym_index
== -1U && dynsym_index
== -1U)
2316 // This symbol is not included in the output file.
2321 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
2322 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
2323 switch (sym
->source())
2325 case Symbol::FROM_OBJECT
:
2328 unsigned int in_shndx
= sym
->shndx(&is_ordinary
);
2330 // FIXME: We need some target specific support here.
2332 && in_shndx
!= elfcpp::SHN_ABS
2333 && in_shndx
!= elfcpp::SHN_COMMON
)
2335 gold_error(_("%s: unsupported symbol section 0x%x"),
2336 sym
->demangled_name().c_str(), in_shndx
);
2341 Object
* symobj
= sym
->object();
2342 if (symobj
->is_dynamic())
2344 if (sym
->needs_dynsym_value())
2345 dynsym_value
= target
.dynsym_value(sym
);
2346 shndx
= elfcpp::SHN_UNDEF
;
2348 else if (symobj
->pluginobj() != NULL
)
2349 shndx
= elfcpp::SHN_UNDEF
;
2350 else if (in_shndx
== elfcpp::SHN_UNDEF
2352 && (in_shndx
== elfcpp::SHN_ABS
2353 || in_shndx
== elfcpp::SHN_COMMON
)))
2357 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2358 Output_section
* os
= relobj
->output_section(in_shndx
);
2359 gold_assert(os
!= NULL
);
2360 shndx
= os
->out_shndx();
2362 if (shndx
>= elfcpp::SHN_LORESERVE
)
2364 if (sym_index
!= -1U)
2365 symtab_xindex
->add(sym_index
, shndx
);
2366 if (dynsym_index
!= -1U)
2367 dynsym_xindex
->add(dynsym_index
, shndx
);
2368 shndx
= elfcpp::SHN_XINDEX
;
2371 // In object files symbol values are section
2373 if (parameters
->options().relocatable())
2374 sym_value
-= os
->address();
2380 case Symbol::IN_OUTPUT_DATA
:
2381 shndx
= sym
->output_data()->out_shndx();
2382 if (shndx
>= elfcpp::SHN_LORESERVE
)
2384 if (sym_index
!= -1U)
2385 symtab_xindex
->add(sym_index
, shndx
);
2386 if (dynsym_index
!= -1U)
2387 dynsym_xindex
->add(dynsym_index
, shndx
);
2388 shndx
= elfcpp::SHN_XINDEX
;
2392 case Symbol::IN_OUTPUT_SEGMENT
:
2393 shndx
= elfcpp::SHN_ABS
;
2396 case Symbol::IS_CONSTANT
:
2397 shndx
= elfcpp::SHN_ABS
;
2400 case Symbol::IS_UNDEFINED
:
2401 shndx
= elfcpp::SHN_UNDEF
;
2408 if (sym_index
!= -1U)
2410 sym_index
-= first_global_index
;
2411 gold_assert(sym_index
< output_count
);
2412 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
2413 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
2417 if (dynsym_index
!= -1U)
2419 dynsym_index
-= first_dynamic_global_index
;
2420 gold_assert(dynsym_index
< dynamic_count
);
2421 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
2422 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
2427 of
->write_output_view(this->offset_
, oview_size
, psyms
);
2428 if (dynamic_view
!= NULL
)
2429 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
2432 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2433 // strtab holding the name.
2435 template<int size
, bool big_endian
>
2437 Symbol_table::sized_write_symbol(
2438 Sized_symbol
<size
>* sym
,
2439 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
2441 const Stringpool
* pool
,
2442 unsigned char* p
) const
2444 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
2445 osym
.put_st_name(pool
->get_offset(sym
->name()));
2446 osym
.put_st_value(value
);
2447 // Use a symbol size of zero for undefined symbols from shared libraries.
2448 if (shndx
== elfcpp::SHN_UNDEF
&& sym
->is_from_dynobj())
2449 osym
.put_st_size(0);
2451 osym
.put_st_size(sym
->symsize());
2452 // A version script may have overridden the default binding.
2453 if (sym
->is_forced_local())
2454 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, sym
->type()));
2456 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
2457 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
2458 osym
.put_st_shndx(shndx
);
2461 // Check for unresolved symbols in shared libraries. This is
2462 // controlled by the --allow-shlib-undefined option.
2464 // We only warn about libraries for which we have seen all the
2465 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2466 // which were not seen in this link. If we didn't see a DT_NEEDED
2467 // entry, we aren't going to be able to reliably report whether the
2468 // symbol is undefined.
2470 // We also don't warn about libraries found in the system library
2471 // directory (the directory were we find libc.so); we assume that
2472 // those libraries are OK. This heuristic avoids problems in
2473 // GNU/Linux, in which -ldl can have undefined references satisfied by
2477 Symbol_table::warn_about_undefined_dynobj_symbol(
2478 const Input_objects
* input_objects
,
2482 if (sym
->source() == Symbol::FROM_OBJECT
2483 && sym
->object()->is_dynamic()
2484 && sym
->shndx(&dummy
) == elfcpp::SHN_UNDEF
2485 && sym
->binding() != elfcpp::STB_WEAK
2486 && !parameters
->options().allow_shlib_undefined()
2487 && !parameters
->target().is_defined_by_abi(sym
)
2488 && !input_objects
->found_in_system_library_directory(sym
->object()))
2490 // A very ugly cast.
2491 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2492 if (!dynobj
->has_unknown_needed_entries())
2495 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2496 sym
->object()->name().c_str(),
2497 sym
->demangled_name().c_str(),
2500 gold_error(_("%s: undefined reference to '%s'"),
2501 sym
->object()->name().c_str(),
2502 sym
->demangled_name().c_str());
2507 // Write out a section symbol. Return the update offset.
2510 Symbol_table::write_section_symbol(const Output_section
*os
,
2511 Output_symtab_xindex
* symtab_xindex
,
2515 switch (parameters
->size_and_endianness())
2517 #ifdef HAVE_TARGET_32_LITTLE
2518 case Parameters::TARGET_32_LITTLE
:
2519 this->sized_write_section_symbol
<32, false>(os
, symtab_xindex
, of
,
2523 #ifdef HAVE_TARGET_32_BIG
2524 case Parameters::TARGET_32_BIG
:
2525 this->sized_write_section_symbol
<32, true>(os
, symtab_xindex
, of
,
2529 #ifdef HAVE_TARGET_64_LITTLE
2530 case Parameters::TARGET_64_LITTLE
:
2531 this->sized_write_section_symbol
<64, false>(os
, symtab_xindex
, of
,
2535 #ifdef HAVE_TARGET_64_BIG
2536 case Parameters::TARGET_64_BIG
:
2537 this->sized_write_section_symbol
<64, true>(os
, symtab_xindex
, of
,
2546 // Write out a section symbol, specialized for size and endianness.
2548 template<int size
, bool big_endian
>
2550 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2551 Output_symtab_xindex
* symtab_xindex
,
2555 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2557 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2559 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2560 osym
.put_st_name(0);
2561 osym
.put_st_value(os
->address());
2562 osym
.put_st_size(0);
2563 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2564 elfcpp::STT_SECTION
));
2565 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2567 unsigned int shndx
= os
->out_shndx();
2568 if (shndx
>= elfcpp::SHN_LORESERVE
)
2570 symtab_xindex
->add(os
->symtab_index(), shndx
);
2571 shndx
= elfcpp::SHN_XINDEX
;
2573 osym
.put_st_shndx(shndx
);
2575 of
->write_output_view(offset
, sym_size
, pov
);
2578 // Print statistical information to stderr. This is used for --stats.
2581 Symbol_table::print_stats() const
2583 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2584 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2585 program_name
, this->table_
.size(), this->table_
.bucket_count());
2587 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2588 program_name
, this->table_
.size());
2590 this->namepool_
.print_stats("symbol table stringpool");
2593 // We check for ODR violations by looking for symbols with the same
2594 // name for which the debugging information reports that they were
2595 // defined in different source locations. When comparing the source
2596 // location, we consider instances with the same base filename and
2597 // line number to be the same. This is because different object
2598 // files/shared libraries can include the same header file using
2599 // different paths, and we don't want to report an ODR violation in
2602 // This struct is used to compare line information, as returned by
2603 // Dwarf_line_info::one_addr2line. It implements a < comparison
2604 // operator used with std::set.
2606 struct Odr_violation_compare
2609 operator()(const std::string
& s1
, const std::string
& s2
) const
2611 std::string::size_type pos1
= s1
.rfind('/');
2612 std::string::size_type pos2
= s2
.rfind('/');
2613 if (pos1
== std::string::npos
2614 || pos2
== std::string::npos
)
2616 return s1
.compare(pos1
, std::string::npos
,
2617 s2
, pos2
, std::string::npos
) < 0;
2621 // Check candidate_odr_violations_ to find symbols with the same name
2622 // but apparently different definitions (different source-file/line-no).
2625 Symbol_table::detect_odr_violations(const Task
* task
,
2626 const char* output_file_name
) const
2628 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2629 it
!= candidate_odr_violations_
.end();
2632 const char* symbol_name
= it
->first
;
2633 // We use a sorted set so the output is deterministic.
2634 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2636 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2637 locs
= it
->second
.begin();
2638 locs
!= it
->second
.end();
2641 // We need to lock the object in order to read it. This
2642 // means that we have to run in a singleton Task. If we
2643 // want to run this in a general Task for better
2644 // performance, we will need one Task for object, plus
2645 // appropriate locking to ensure that we don't conflict with
2646 // other uses of the object. Also note, one_addr2line is not
2647 // currently thread-safe.
2648 Task_lock_obj
<Object
> tl(task
, locs
->object
);
2649 // 16 is the size of the object-cache that one_addr2line should use.
2650 std::string lineno
= Dwarf_line_info::one_addr2line(
2651 locs
->object
, locs
->shndx
, locs
->offset
, 16);
2652 if (!lineno
.empty())
2653 line_nums
.insert(lineno
);
2656 if (line_nums
.size() > 1)
2658 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2659 "places (possible ODR violation):"),
2660 output_file_name
, demangle(symbol_name
).c_str());
2661 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
2662 it2
!= line_nums
.end();
2664 fprintf(stderr
, " %s\n", it2
->c_str());
2667 // We only call one_addr2line() in this function, so we can clear its cache.
2668 Dwarf_line_info::clear_addr2line_cache();
2671 // Warnings functions.
2673 // Add a new warning.
2676 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
2677 const std::string
& warning
)
2679 name
= symtab
->canonicalize_name(name
);
2680 this->warnings_
[name
].set(obj
, warning
);
2683 // Look through the warnings and mark the symbols for which we should
2684 // warn. This is called during Layout::finalize when we know the
2685 // sources for all the symbols.
2688 Warnings::note_warnings(Symbol_table
* symtab
)
2690 for (Warning_table::iterator p
= this->warnings_
.begin();
2691 p
!= this->warnings_
.end();
2694 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2696 && sym
->source() == Symbol::FROM_OBJECT
2697 && sym
->object() == p
->second
.object
)
2698 sym
->set_has_warning();
2702 // Issue a warning. This is called when we see a relocation against a
2703 // symbol for which has a warning.
2705 template<int size
, bool big_endian
>
2707 Warnings::issue_warning(const Symbol
* sym
,
2708 const Relocate_info
<size
, big_endian
>* relinfo
,
2709 size_t relnum
, off_t reloffset
) const
2711 gold_assert(sym
->has_warning());
2712 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2713 gold_assert(p
!= this->warnings_
.end());
2714 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2715 "%s", p
->second
.text
.c_str());
2718 // Instantiate the templates we need. We could use the configure
2719 // script to restrict this to only the ones needed for implemented
2722 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2725 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2728 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2731 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2734 #ifdef HAVE_TARGET_32_LITTLE
2737 Symbol_table::add_from_relobj
<32, false>(
2738 Sized_relobj
<32, false>* relobj
,
2739 const unsigned char* syms
,
2741 size_t symndx_offset
,
2742 const char* sym_names
,
2743 size_t sym_name_size
,
2744 Sized_relobj
<32, false>::Symbols
* sympointers
,
2748 #ifdef HAVE_TARGET_32_BIG
2751 Symbol_table::add_from_relobj
<32, true>(
2752 Sized_relobj
<32, true>* relobj
,
2753 const unsigned char* syms
,
2755 size_t symndx_offset
,
2756 const char* sym_names
,
2757 size_t sym_name_size
,
2758 Sized_relobj
<32, true>::Symbols
* sympointers
,
2762 #ifdef HAVE_TARGET_64_LITTLE
2765 Symbol_table::add_from_relobj
<64, false>(
2766 Sized_relobj
<64, false>* relobj
,
2767 const unsigned char* syms
,
2769 size_t symndx_offset
,
2770 const char* sym_names
,
2771 size_t sym_name_size
,
2772 Sized_relobj
<64, false>::Symbols
* sympointers
,
2776 #ifdef HAVE_TARGET_64_BIG
2779 Symbol_table::add_from_relobj
<64, true>(
2780 Sized_relobj
<64, true>* relobj
,
2781 const unsigned char* syms
,
2783 size_t symndx_offset
,
2784 const char* sym_names
,
2785 size_t sym_name_size
,
2786 Sized_relobj
<64, true>::Symbols
* sympointers
,
2790 #ifdef HAVE_TARGET_32_LITTLE
2793 Symbol_table::add_from_pluginobj
<32, false>(
2794 Sized_pluginobj
<32, false>* obj
,
2797 elfcpp::Sym
<32, false>* sym
);
2800 #ifdef HAVE_TARGET_32_BIG
2803 Symbol_table::add_from_pluginobj
<32, true>(
2804 Sized_pluginobj
<32, true>* obj
,
2807 elfcpp::Sym
<32, true>* sym
);
2810 #ifdef HAVE_TARGET_64_LITTLE
2813 Symbol_table::add_from_pluginobj
<64, false>(
2814 Sized_pluginobj
<64, false>* obj
,
2817 elfcpp::Sym
<64, false>* sym
);
2820 #ifdef HAVE_TARGET_64_BIG
2823 Symbol_table::add_from_pluginobj
<64, true>(
2824 Sized_pluginobj
<64, true>* obj
,
2827 elfcpp::Sym
<64, true>* sym
);
2830 #ifdef HAVE_TARGET_32_LITTLE
2833 Symbol_table::add_from_dynobj
<32, false>(
2834 Sized_dynobj
<32, false>* dynobj
,
2835 const unsigned char* syms
,
2837 const char* sym_names
,
2838 size_t sym_name_size
,
2839 const unsigned char* versym
,
2841 const std::vector
<const char*>* version_map
,
2842 Sized_relobj
<32, false>::Symbols
* sympointers
,
2846 #ifdef HAVE_TARGET_32_BIG
2849 Symbol_table::add_from_dynobj
<32, true>(
2850 Sized_dynobj
<32, true>* dynobj
,
2851 const unsigned char* syms
,
2853 const char* sym_names
,
2854 size_t sym_name_size
,
2855 const unsigned char* versym
,
2857 const std::vector
<const char*>* version_map
,
2858 Sized_relobj
<32, true>::Symbols
* sympointers
,
2862 #ifdef HAVE_TARGET_64_LITTLE
2865 Symbol_table::add_from_dynobj
<64, false>(
2866 Sized_dynobj
<64, false>* dynobj
,
2867 const unsigned char* syms
,
2869 const char* sym_names
,
2870 size_t sym_name_size
,
2871 const unsigned char* versym
,
2873 const std::vector
<const char*>* version_map
,
2874 Sized_relobj
<64, false>::Symbols
* sympointers
,
2878 #ifdef HAVE_TARGET_64_BIG
2881 Symbol_table::add_from_dynobj
<64, true>(
2882 Sized_dynobj
<64, true>* dynobj
,
2883 const unsigned char* syms
,
2885 const char* sym_names
,
2886 size_t sym_name_size
,
2887 const unsigned char* versym
,
2889 const std::vector
<const char*>* version_map
,
2890 Sized_relobj
<64, true>::Symbols
* sympointers
,
2894 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2897 Symbol_table::define_with_copy_reloc
<32>(
2898 Sized_symbol
<32>* sym
,
2900 elfcpp::Elf_types
<32>::Elf_Addr value
);
2903 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2906 Symbol_table::define_with_copy_reloc
<64>(
2907 Sized_symbol
<64>* sym
,
2909 elfcpp::Elf_types
<64>::Elf_Addr value
);
2912 #ifdef HAVE_TARGET_32_LITTLE
2915 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2916 const Relocate_info
<32, false>* relinfo
,
2917 size_t relnum
, off_t reloffset
) const;
2920 #ifdef HAVE_TARGET_32_BIG
2923 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2924 const Relocate_info
<32, true>* relinfo
,
2925 size_t relnum
, off_t reloffset
) const;
2928 #ifdef HAVE_TARGET_64_LITTLE
2931 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2932 const Relocate_info
<64, false>* relinfo
,
2933 size_t relnum
, off_t reloffset
) const;
2936 #ifdef HAVE_TARGET_64_BIG
2939 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2940 const Relocate_info
<64, true>* relinfo
,
2941 size_t relnum
, off_t reloffset
) const;
2944 } // End namespace gold.