1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008, 2009 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.
35 #include "dwarf_reader.h"
39 #include "workqueue.h"
41 #include "demangle.h" // needed for --dynamic-list-cpp-new
49 // Initialize fields in Symbol. This initializes everything except u_
53 Symbol::init_fields(const char* name
, const char* version
,
54 elfcpp::STT type
, elfcpp::STB binding
,
55 elfcpp::STV visibility
, unsigned char nonvis
)
58 this->version_
= version
;
59 this->symtab_index_
= 0;
60 this->dynsym_index_
= 0;
61 this->got_offsets_
.init();
62 this->plt_offset_
= 0;
64 this->binding_
= binding
;
65 this->visibility_
= visibility
;
66 this->nonvis_
= nonvis
;
67 this->is_target_special_
= false;
68 this->is_def_
= false;
69 this->is_forwarder_
= false;
70 this->has_alias_
= false;
71 this->needs_dynsym_entry_
= false;
72 this->in_reg_
= false;
73 this->in_dyn_
= false;
74 this->has_plt_offset_
= false;
75 this->has_warning_
= false;
76 this->is_copied_from_dynobj_
= false;
77 this->is_forced_local_
= false;
78 this->is_ordinary_shndx_
= false;
79 this->in_real_elf_
= false;
82 // Return the demangled version of the symbol's name, but only
83 // if the --demangle flag was set.
86 demangle(const char* name
)
88 if (!parameters
->options().do_demangle())
91 // cplus_demangle allocates memory for the result it returns,
92 // and returns NULL if the name is already demangled.
93 char* demangled_name
= cplus_demangle(name
, DMGL_ANSI
| DMGL_PARAMS
);
94 if (demangled_name
== NULL
)
97 std::string
retval(demangled_name
);
103 Symbol::demangled_name() const
105 return demangle(this->name());
108 // Initialize the fields in the base class Symbol for SYM in OBJECT.
110 template<int size
, bool big_endian
>
112 Symbol::init_base_object(const char* name
, const char* version
, Object
* object
,
113 const elfcpp::Sym
<size
, big_endian
>& sym
,
114 unsigned int st_shndx
, bool is_ordinary
)
116 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
117 sym
.get_st_visibility(), sym
.get_st_nonvis());
118 this->u_
.from_object
.object
= object
;
119 this->u_
.from_object
.shndx
= st_shndx
;
120 this->is_ordinary_shndx_
= is_ordinary
;
121 this->source_
= FROM_OBJECT
;
122 this->in_reg_
= !object
->is_dynamic();
123 this->in_dyn_
= object
->is_dynamic();
124 this->in_real_elf_
= object
->pluginobj() == NULL
;
127 // Initialize the fields in the base class Symbol for a symbol defined
128 // in an Output_data.
131 Symbol::init_base_output_data(const char* name
, const char* version
,
132 Output_data
* od
, elfcpp::STT type
,
133 elfcpp::STB binding
, elfcpp::STV visibility
,
134 unsigned char nonvis
, bool offset_is_from_end
)
136 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
137 this->u_
.in_output_data
.output_data
= od
;
138 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
139 this->source_
= IN_OUTPUT_DATA
;
140 this->in_reg_
= true;
141 this->in_real_elf_
= true;
144 // Initialize the fields in the base class Symbol for a symbol defined
145 // in an Output_segment.
148 Symbol::init_base_output_segment(const char* name
, const char* version
,
149 Output_segment
* os
, elfcpp::STT type
,
150 elfcpp::STB binding
, elfcpp::STV visibility
,
151 unsigned char nonvis
,
152 Segment_offset_base offset_base
)
154 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
155 this->u_
.in_output_segment
.output_segment
= os
;
156 this->u_
.in_output_segment
.offset_base
= offset_base
;
157 this->source_
= IN_OUTPUT_SEGMENT
;
158 this->in_reg_
= true;
159 this->in_real_elf_
= true;
162 // Initialize the fields in the base class Symbol for a symbol defined
166 Symbol::init_base_constant(const char* name
, const char* version
,
167 elfcpp::STT type
, elfcpp::STB binding
,
168 elfcpp::STV visibility
, unsigned char nonvis
)
170 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
171 this->source_
= IS_CONSTANT
;
172 this->in_reg_
= true;
173 this->in_real_elf_
= true;
176 // Initialize the fields in the base class Symbol for an undefined
180 Symbol::init_base_undefined(const char* name
, const char* version
,
181 elfcpp::STT type
, elfcpp::STB binding
,
182 elfcpp::STV visibility
, unsigned char nonvis
)
184 this->init_fields(name
, version
, type
, binding
, visibility
, nonvis
);
185 this->dynsym_index_
= -1U;
186 this->source_
= IS_UNDEFINED
;
187 this->in_reg_
= true;
188 this->in_real_elf_
= true;
191 // Allocate a common symbol in the base.
194 Symbol::allocate_base_common(Output_data
* od
)
196 gold_assert(this->is_common());
197 this->source_
= IN_OUTPUT_DATA
;
198 this->u_
.in_output_data
.output_data
= od
;
199 this->u_
.in_output_data
.offset_is_from_end
= false;
202 // Initialize the fields in Sized_symbol for SYM in OBJECT.
205 template<bool big_endian
>
207 Sized_symbol
<size
>::init_object(const char* name
, const char* version
,
209 const elfcpp::Sym
<size
, big_endian
>& sym
,
210 unsigned int st_shndx
, bool is_ordinary
)
212 this->init_base_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
213 this->value_
= sym
.get_st_value();
214 this->symsize_
= sym
.get_st_size();
217 // Initialize the fields in Sized_symbol for a symbol defined in an
222 Sized_symbol
<size
>::init_output_data(const char* name
, const char* version
,
223 Output_data
* od
, Value_type value
,
224 Size_type symsize
, elfcpp::STT type
,
226 elfcpp::STV visibility
,
227 unsigned char nonvis
,
228 bool offset_is_from_end
)
230 this->init_base_output_data(name
, version
, od
, type
, binding
, visibility
,
231 nonvis
, offset_is_from_end
);
232 this->value_
= value
;
233 this->symsize_
= symsize
;
236 // Initialize the fields in Sized_symbol for a symbol defined in an
241 Sized_symbol
<size
>::init_output_segment(const char* name
, const char* version
,
242 Output_segment
* os
, Value_type value
,
243 Size_type symsize
, elfcpp::STT type
,
245 elfcpp::STV visibility
,
246 unsigned char nonvis
,
247 Segment_offset_base offset_base
)
249 this->init_base_output_segment(name
, version
, os
, type
, binding
, visibility
,
250 nonvis
, offset_base
);
251 this->value_
= value
;
252 this->symsize_
= symsize
;
255 // Initialize the fields in Sized_symbol for a symbol defined as a
260 Sized_symbol
<size
>::init_constant(const char* name
, const char* version
,
261 Value_type value
, Size_type symsize
,
262 elfcpp::STT type
, elfcpp::STB binding
,
263 elfcpp::STV visibility
, unsigned char nonvis
)
265 this->init_base_constant(name
, version
, type
, binding
, visibility
, nonvis
);
266 this->value_
= value
;
267 this->symsize_
= symsize
;
270 // Initialize the fields in Sized_symbol for an undefined symbol.
274 Sized_symbol
<size
>::init_undefined(const char* name
, const char* version
,
275 elfcpp::STT type
, elfcpp::STB binding
,
276 elfcpp::STV visibility
, unsigned char nonvis
)
278 this->init_base_undefined(name
, version
, type
, binding
, visibility
, nonvis
);
283 // Return true if SHNDX represents a common symbol.
286 Symbol::is_common_shndx(unsigned int shndx
)
288 return (shndx
== elfcpp::SHN_COMMON
289 || shndx
== parameters
->target().small_common_shndx()
290 || shndx
== parameters
->target().large_common_shndx());
293 // Allocate a common symbol.
297 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
299 this->allocate_base_common(od
);
300 this->value_
= value
;
303 // The ""'s around str ensure str is a string literal, so sizeof works.
304 #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
306 // Return true if this symbol should be added to the dynamic symbol
310 Symbol::should_add_dynsym_entry() const
312 // If the symbol is used by a dynamic relocation, we need to add it.
313 if (this->needs_dynsym_entry())
316 // If this symbol's section is not added, the symbol need not be added.
317 // The section may have been GCed. Note that export_dynamic is being
318 // overridden here. This should not be done for shared objects.
319 if (parameters
->options().gc_sections()
320 && !parameters
->options().shared()
321 && this->source() == Symbol::FROM_OBJECT
322 && !this->object()->is_dynamic())
324 Relobj
* relobj
= static_cast<Relobj
*>(this->object());
326 unsigned int shndx
= this->shndx(&is_ordinary
);
327 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
328 && !relobj
->is_section_included(shndx
))
332 // If the symbol was forced local in a version script, do not add it.
333 if (this->is_forced_local())
336 // If the symbol was forced dynamic in a --dynamic-list file, add it.
337 if (parameters
->options().in_dynamic_list(this->name()))
340 // If dynamic-list-data was specified, add any STT_OBJECT.
341 if (parameters
->options().dynamic_list_data()
342 && !this->is_from_dynobj()
343 && this->type() == elfcpp::STT_OBJECT
)
346 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
347 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
348 if ((parameters
->options().dynamic_list_cpp_new()
349 || parameters
->options().dynamic_list_cpp_typeinfo())
350 && !this->is_from_dynobj())
352 // TODO(csilvers): We could probably figure out if we're an operator
353 // new/delete or typeinfo without the need to demangle.
354 char* demangled_name
= cplus_demangle(this->name(),
355 DMGL_ANSI
| DMGL_PARAMS
);
356 if (demangled_name
== NULL
)
358 // Not a C++ symbol, so it can't satisfy these flags
360 else if (parameters
->options().dynamic_list_cpp_new()
361 && (strprefix(demangled_name
, "operator new")
362 || strprefix(demangled_name
, "operator delete")))
364 free(demangled_name
);
367 else if (parameters
->options().dynamic_list_cpp_typeinfo()
368 && (strprefix(demangled_name
, "typeinfo name for")
369 || strprefix(demangled_name
, "typeinfo for")))
371 free(demangled_name
);
375 free(demangled_name
);
378 // If exporting all symbols or building a shared library,
379 // and the symbol is defined in a regular object and is
380 // externally visible, we need to add it.
381 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
382 && !this->is_from_dynobj()
383 && this->is_externally_visible())
389 // Return true if the final value of this symbol is known at link
393 Symbol::final_value_is_known() const
395 // If we are not generating an executable, then no final values are
396 // known, since they will change at runtime.
397 if (parameters
->options().output_is_position_independent()
398 || parameters
->options().relocatable())
401 // If the symbol is not from an object file, and is not undefined,
402 // then it is defined, and known.
403 if (this->source_
!= FROM_OBJECT
)
405 if (this->source_
!= IS_UNDEFINED
)
410 // If the symbol is from a dynamic object, then the final value
412 if (this->object()->is_dynamic())
415 // If the symbol is not undefined (it is defined or common),
416 // then the final value is known.
417 if (!this->is_undefined())
421 // If the symbol is undefined, then whether the final value is known
422 // depends on whether we are doing a static link. If we are doing a
423 // dynamic link, then the final value could be filled in at runtime.
424 // This could reasonably be the case for a weak undefined symbol.
425 return parameters
->doing_static_link();
428 // Return the output section where this symbol is defined.
431 Symbol::output_section() const
433 switch (this->source_
)
437 unsigned int shndx
= this->u_
.from_object
.shndx
;
438 if (shndx
!= elfcpp::SHN_UNDEF
&& this->is_ordinary_shndx_
)
440 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
441 gold_assert(this->u_
.from_object
.object
->pluginobj() == NULL
);
442 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
443 return relobj
->output_section(shndx
);
449 return this->u_
.in_output_data
.output_data
->output_section();
451 case IN_OUTPUT_SEGMENT
:
461 // Set the symbol's output section. This is used for symbols defined
462 // in scripts. This should only be called after the symbol table has
466 Symbol::set_output_section(Output_section
* os
)
468 switch (this->source_
)
472 gold_assert(this->output_section() == os
);
475 this->source_
= IN_OUTPUT_DATA
;
476 this->u_
.in_output_data
.output_data
= os
;
477 this->u_
.in_output_data
.offset_is_from_end
= false;
479 case IN_OUTPUT_SEGMENT
:
486 // Class Symbol_table.
488 Symbol_table::Symbol_table(unsigned int count
,
489 const Version_script_info
& version_script
)
490 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
491 forwarders_(), commons_(), tls_commons_(), small_commons_(),
492 large_commons_(), forced_locals_(), warnings_(),
493 version_script_(version_script
), gc_(NULL
), icf_(NULL
)
495 namepool_
.reserve(count
);
498 Symbol_table::~Symbol_table()
502 // The hash function. The key values are Stringpool keys.
505 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
507 return key
.first
^ key
.second
;
510 // The symbol table key equality function. This is called with
514 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
515 const Symbol_table_key
& k2
) const
517 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
521 Symbol_table::is_section_folded(Object
* obj
, unsigned int shndx
) const
523 return (parameters
->options().icf_enabled()
524 && this->icf_
->is_section_folded(obj
, shndx
));
527 // For symbols that have been listed with -u option, add them to the
528 // work list to avoid gc'ing them.
531 Symbol_table::gc_mark_undef_symbols()
533 for (options::String_set::const_iterator p
=
534 parameters
->options().undefined_begin();
535 p
!= parameters
->options().undefined_end();
538 const char* name
= p
->c_str();
539 Symbol
* sym
= this->lookup(name
);
540 gold_assert (sym
!= NULL
);
541 if (sym
->source() == Symbol::FROM_OBJECT
542 && !sym
->object()->is_dynamic())
544 Relobj
* obj
= static_cast<Relobj
*>(sym
->object());
546 unsigned int shndx
= sym
->shndx(&is_ordinary
);
549 gold_assert(this->gc_
!= NULL
);
550 this->gc_
->worklist().push(Section_id(obj
, shndx
));
557 Symbol_table::gc_mark_symbol_for_shlib(Symbol
* sym
)
559 if (!sym
->is_from_dynobj()
560 && sym
->is_externally_visible())
562 //Add the object and section to the work list.
563 Relobj
* obj
= static_cast<Relobj
*>(sym
->object());
565 unsigned int shndx
= sym
->shndx(&is_ordinary
);
566 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
)
568 gold_assert(this->gc_
!= NULL
);
569 this->gc_
->worklist().push(Section_id(obj
, shndx
));
574 // When doing garbage collection, keep symbols that have been seen in
577 Symbol_table::gc_mark_dyn_syms(Symbol
* sym
)
579 if (sym
->in_dyn() && sym
->source() == Symbol::FROM_OBJECT
580 && !sym
->object()->is_dynamic())
582 Relobj
*obj
= static_cast<Relobj
*>(sym
->object());
584 unsigned int shndx
= sym
->shndx(&is_ordinary
);
585 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
)
587 gold_assert(this->gc_
!= NULL
);
588 this->gc_
->worklist().push(Section_id(obj
, shndx
));
593 // Make TO a symbol which forwards to FROM.
596 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
598 gold_assert(from
!= to
);
599 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
600 this->forwarders_
[from
] = to
;
601 from
->set_forwarder();
604 // Resolve the forwards from FROM, returning the real symbol.
607 Symbol_table::resolve_forwards(const Symbol
* from
) const
609 gold_assert(from
->is_forwarder());
610 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
611 this->forwarders_
.find(from
);
612 gold_assert(p
!= this->forwarders_
.end());
616 // Look up a symbol by name.
619 Symbol_table::lookup(const char* name
, const char* version
) const
621 Stringpool::Key name_key
;
622 name
= this->namepool_
.find(name
, &name_key
);
626 Stringpool::Key version_key
= 0;
629 version
= this->namepool_
.find(version
, &version_key
);
634 Symbol_table_key
key(name_key
, version_key
);
635 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
636 if (p
== this->table_
.end())
641 // Resolve a Symbol with another Symbol. This is only used in the
642 // unusual case where there are references to both an unversioned
643 // symbol and a symbol with a version, and we then discover that that
644 // version is the default version. Because this is unusual, we do
645 // this the slow way, by converting back to an ELF symbol.
647 template<int size
, bool big_endian
>
649 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
)
651 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
652 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
653 // We don't bother to set the st_name or the st_shndx field.
654 esym
.put_st_value(from
->value());
655 esym
.put_st_size(from
->symsize());
656 esym
.put_st_info(from
->binding(), from
->type());
657 esym
.put_st_other(from
->visibility(), from
->nonvis());
659 unsigned int shndx
= from
->shndx(&is_ordinary
);
660 this->resolve(to
, esym
.sym(), shndx
, is_ordinary
, shndx
, from
->object(),
666 if (parameters
->options().gc_sections())
667 this->gc_mark_dyn_syms(to
);
670 // Record that a symbol is forced to be local by a version script or
674 Symbol_table::force_local(Symbol
* sym
)
676 if (!sym
->is_defined() && !sym
->is_common())
678 if (sym
->is_forced_local())
680 // We already got this one.
683 sym
->set_is_forced_local();
684 this->forced_locals_
.push_back(sym
);
687 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
688 // is only called for undefined symbols, when at least one --wrap
692 Symbol_table::wrap_symbol(const char* name
, Stringpool::Key
* name_key
)
694 // For some targets, we need to ignore a specific character when
695 // wrapping, and add it back later.
697 if (name
[0] == parameters
->target().wrap_char())
703 if (parameters
->options().is_wrap(name
))
705 // Turn NAME into __wrap_NAME.
712 // This will give us both the old and new name in NAMEPOOL_, but
713 // that is OK. Only the versions we need will wind up in the
714 // real string table in the output file.
715 return this->namepool_
.add(s
.c_str(), true, name_key
);
718 const char* const real_prefix
= "__real_";
719 const size_t real_prefix_length
= strlen(real_prefix
);
720 if (strncmp(name
, real_prefix
, real_prefix_length
) == 0
721 && parameters
->options().is_wrap(name
+ real_prefix_length
))
723 // Turn __real_NAME into NAME.
727 s
+= name
+ real_prefix_length
;
728 return this->namepool_
.add(s
.c_str(), true, name_key
);
734 // This is called when we see a symbol NAME/VERSION, and the symbol
735 // already exists in the symbol table, and VERSION is marked as being
736 // the default version. SYM is the NAME/VERSION symbol we just added.
737 // DEFAULT_IS_NEW is true if this is the first time we have seen the
738 // symbol NAME/NULL. PDEF points to the entry for NAME/NULL.
740 template<int size
, bool big_endian
>
742 Symbol_table::define_default_version(Sized_symbol
<size
>* sym
,
744 Symbol_table_type::iterator pdef
)
748 // This is the first time we have seen NAME/NULL. Make
749 // NAME/NULL point to NAME/VERSION, and mark SYM as the default
752 sym
->set_is_default();
754 else if (pdef
->second
== sym
)
756 // NAME/NULL already points to NAME/VERSION. Don't mark the
757 // symbol as the default if it is not already the default.
761 // This is the unfortunate case where we already have entries
762 // for both NAME/VERSION and NAME/NULL. We now see a symbol
763 // NAME/VERSION where VERSION is the default version. We have
764 // already resolved this new symbol with the existing
765 // NAME/VERSION symbol.
767 // It's possible that NAME/NULL and NAME/VERSION are both
768 // defined in regular objects. This can only happen if one
769 // object file defines foo and another defines foo@@ver. This
770 // is somewhat obscure, but we call it a multiple definition
773 // It's possible that NAME/NULL actually has a version, in which
774 // case it won't be the same as VERSION. This happens with
775 // ver_test_7.so in the testsuite for the symbol t2_2. We see
776 // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We
777 // then see an unadorned t2_2 in an object file and give it
778 // version VER1 from the version script. This looks like a
779 // default definition for VER1, so it looks like we should merge
780 // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's
781 // not obvious that this is an error, either. So we just punt.
783 // If one of the symbols has non-default visibility, and the
784 // other is defined in a shared object, then they are different
787 // Otherwise, we just resolve the symbols as though they were
790 if (pdef
->second
->version() != NULL
)
791 gold_assert(pdef
->second
->version() != sym
->version());
792 else if (sym
->visibility() != elfcpp::STV_DEFAULT
793 && pdef
->second
->is_from_dynobj())
795 else if (pdef
->second
->visibility() != elfcpp::STV_DEFAULT
796 && sym
->is_from_dynobj())
800 const Sized_symbol
<size
>* symdef
;
801 symdef
= this->get_sized_symbol
<size
>(pdef
->second
);
802 Symbol_table::resolve
<size
, big_endian
>(sym
, symdef
);
803 this->make_forwarder(pdef
->second
, sym
);
805 sym
->set_is_default();
810 // Add one symbol from OBJECT to the symbol table. NAME is symbol
811 // name and VERSION is the version; both are canonicalized. DEF is
812 // whether this is the default version. ST_SHNDX is the symbol's
813 // section index; IS_ORDINARY is whether this is a normal section
814 // rather than a special code.
816 // If DEF is true, then this is the definition of a default version of
817 // a symbol. That means that any lookup of NAME/NULL and any lookup
818 // of NAME/VERSION should always return the same symbol. This is
819 // obvious for references, but in particular we want to do this for
820 // definitions: overriding NAME/NULL should also override
821 // NAME/VERSION. If we don't do that, it would be very hard to
822 // override functions in a shared library which uses versioning.
824 // We implement this by simply making both entries in the hash table
825 // point to the same Symbol structure. That is easy enough if this is
826 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
827 // that we have seen both already, in which case they will both have
828 // independent entries in the symbol table. We can't simply change
829 // the symbol table entry, because we have pointers to the entries
830 // attached to the object files. So we mark the entry attached to the
831 // object file as a forwarder, and record it in the forwarders_ map.
832 // Note that entries in the hash table will never be marked as
835 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
836 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
837 // for a special section code. ST_SHNDX may be modified if the symbol
838 // is defined in a section being discarded.
840 template<int size
, bool big_endian
>
842 Symbol_table::add_from_object(Object
* object
,
844 Stringpool::Key name_key
,
846 Stringpool::Key version_key
,
848 const elfcpp::Sym
<size
, big_endian
>& sym
,
849 unsigned int st_shndx
,
851 unsigned int orig_st_shndx
)
853 // Print a message if this symbol is being traced.
854 if (parameters
->options().is_trace_symbol(name
))
856 if (orig_st_shndx
== elfcpp::SHN_UNDEF
)
857 gold_info(_("%s: reference to %s"), object
->name().c_str(), name
);
859 gold_info(_("%s: definition of %s"), object
->name().c_str(), name
);
862 // For an undefined symbol, we may need to adjust the name using
864 if (orig_st_shndx
== elfcpp::SHN_UNDEF
865 && parameters
->options().any_wrap())
867 const char* wrap_name
= this->wrap_symbol(name
, &name_key
);
868 if (wrap_name
!= name
)
870 // If we see a reference to malloc with version GLIBC_2.0,
871 // and we turn it into a reference to __wrap_malloc, then we
872 // discard the version number. Otherwise the user would be
873 // required to specify the correct version for
881 Symbol
* const snull
= NULL
;
882 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
883 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
886 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
887 std::make_pair(this->table_
.end(), false);
890 const Stringpool::Key vnull_key
= 0;
891 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
896 // ins.first: an iterator, which is a pointer to a pair.
897 // ins.first->first: the key (a pair of name and version).
898 // ins.first->second: the value (Symbol*).
899 // ins.second: true if new entry was inserted, false if not.
901 Sized_symbol
<size
>* ret
;
906 // We already have an entry for NAME/VERSION.
907 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
908 gold_assert(ret
!= NULL
);
910 was_undefined
= ret
->is_undefined();
911 was_common
= ret
->is_common();
913 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
915 if (parameters
->options().gc_sections())
916 this->gc_mark_dyn_syms(ret
);
919 this->define_default_version
<size
, big_endian
>(ret
, insdef
.second
,
924 // This is the first time we have seen NAME/VERSION.
925 gold_assert(ins
.first
->second
== NULL
);
927 if (def
&& !insdef
.second
)
929 // We already have an entry for NAME/NULL. If we override
930 // it, then change it to NAME/VERSION.
931 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
933 was_undefined
= ret
->is_undefined();
934 was_common
= ret
->is_common();
936 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
938 if (parameters
->options().gc_sections())
939 this->gc_mark_dyn_syms(ret
);
940 ins
.first
->second
= ret
;
944 was_undefined
= false;
947 Sized_target
<size
, big_endian
>* target
=
948 parameters
->sized_target
<size
, big_endian
>();
949 if (!target
->has_make_symbol())
950 ret
= new Sized_symbol
<size
>();
953 ret
= target
->make_symbol();
956 // This means that we don't want a symbol table
959 this->table_
.erase(ins
.first
);
962 this->table_
.erase(insdef
.first
);
963 // Inserting insdef invalidated ins.
964 this->table_
.erase(std::make_pair(name_key
,
971 ret
->init_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
973 ins
.first
->second
= ret
;
976 // This is the first time we have seen NAME/NULL. Point
977 // it at the new entry for NAME/VERSION.
978 gold_assert(insdef
.second
);
979 insdef
.first
->second
= ret
;
984 ret
->set_is_default();
987 // Record every time we see a new undefined symbol, to speed up
989 if (!was_undefined
&& ret
->is_undefined())
990 ++this->saw_undefined_
;
992 // Keep track of common symbols, to speed up common symbol
994 if (!was_common
&& ret
->is_common())
996 if (ret
->type() == elfcpp::STT_TLS
)
997 this->tls_commons_
.push_back(ret
);
998 else if (!is_ordinary
999 && st_shndx
== parameters
->target().small_common_shndx())
1000 this->small_commons_
.push_back(ret
);
1001 else if (!is_ordinary
1002 && st_shndx
== parameters
->target().large_common_shndx())
1003 this->large_commons_
.push_back(ret
);
1005 this->commons_
.push_back(ret
);
1008 // If we're not doing a relocatable link, then any symbol with
1009 // hidden or internal visibility is local.
1010 if ((ret
->visibility() == elfcpp::STV_HIDDEN
1011 || ret
->visibility() == elfcpp::STV_INTERNAL
)
1012 && (ret
->binding() == elfcpp::STB_GLOBAL
1013 || ret
->binding() == elfcpp::STB_GNU_UNIQUE
1014 || ret
->binding() == elfcpp::STB_WEAK
)
1015 && !parameters
->options().relocatable())
1016 this->force_local(ret
);
1021 // Add all the symbols in a relocatable object to the hash table.
1023 template<int size
, bool big_endian
>
1025 Symbol_table::add_from_relobj(
1026 Sized_relobj
<size
, big_endian
>* relobj
,
1027 const unsigned char* syms
,
1029 size_t symndx_offset
,
1030 const char* sym_names
,
1031 size_t sym_name_size
,
1032 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
1037 gold_assert(size
== parameters
->target().get_size());
1039 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1041 const bool just_symbols
= relobj
->just_symbols();
1043 const unsigned char* p
= syms
;
1044 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
1046 (*sympointers
)[i
] = NULL
;
1048 elfcpp::Sym
<size
, big_endian
> sym(p
);
1050 unsigned int st_name
= sym
.get_st_name();
1051 if (st_name
>= sym_name_size
)
1053 relobj
->error(_("bad global symbol name offset %u at %zu"),
1058 const char* name
= sym_names
+ st_name
;
1061 unsigned int st_shndx
= relobj
->adjust_sym_shndx(i
+ symndx_offset
,
1064 unsigned int orig_st_shndx
= st_shndx
;
1066 orig_st_shndx
= elfcpp::SHN_UNDEF
;
1068 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1071 // A symbol defined in a section which we are not including must
1072 // be treated as an undefined symbol.
1073 if (st_shndx
!= elfcpp::SHN_UNDEF
1075 && !relobj
->is_section_included(st_shndx
))
1076 st_shndx
= elfcpp::SHN_UNDEF
;
1078 // In an object file, an '@' in the name separates the symbol
1079 // name from the version name. If there are two '@' characters,
1080 // this is the default version.
1081 const char* ver
= strchr(name
, '@');
1082 Stringpool::Key ver_key
= 0;
1084 // DEF: is the version default? LOCAL: is the symbol forced local?
1090 // The symbol name is of the form foo@VERSION or foo@@VERSION
1091 namelen
= ver
- name
;
1098 ver
= this->namepool_
.add(ver
, true, &ver_key
);
1100 // We don't want to assign a version to an undefined symbol,
1101 // even if it is listed in the version script. FIXME: What
1102 // about a common symbol?
1105 namelen
= strlen(name
);
1106 if (!this->version_script_
.empty()
1107 && st_shndx
!= elfcpp::SHN_UNDEF
)
1109 // The symbol name did not have a version, but the
1110 // version script may assign a version anyway.
1111 std::string version
;
1112 if (this->version_script_
.get_symbol_version(name
, &version
))
1114 // The version can be empty if the version script is
1115 // only used to force some symbols to be local.
1116 if (!version
.empty())
1118 ver
= this->namepool_
.add_with_length(version
.c_str(),
1125 else if (this->version_script_
.symbol_is_local(name
))
1130 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1131 unsigned char symbuf
[sym_size
];
1132 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1135 memcpy(symbuf
, p
, sym_size
);
1136 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1137 if (orig_st_shndx
!= elfcpp::SHN_UNDEF
&& is_ordinary
)
1139 // Symbol values in object files are section relative.
1140 // This is normally what we want, but since here we are
1141 // converting the symbol to absolute we need to add the
1142 // section address. The section address in an object
1143 // file is normally zero, but people can use a linker
1144 // script to change it.
1145 sw
.put_st_value(sym
.get_st_value()
1146 + relobj
->section_address(orig_st_shndx
));
1148 st_shndx
= elfcpp::SHN_ABS
;
1149 is_ordinary
= false;
1153 // Fix up visibility if object has no-export set.
1154 if (relobj
->no_export())
1156 // We may have copied symbol already above.
1159 memcpy(symbuf
, p
, sym_size
);
1163 elfcpp::STV visibility
= sym2
.get_st_visibility();
1164 if (visibility
== elfcpp::STV_DEFAULT
1165 || visibility
== elfcpp::STV_PROTECTED
)
1167 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1168 unsigned char nonvis
= sym2
.get_st_nonvis();
1169 sw
.put_st_other(elfcpp::STV_HIDDEN
, nonvis
);
1173 Stringpool::Key name_key
;
1174 name
= this->namepool_
.add_with_length(name
, namelen
, true,
1177 Sized_symbol
<size
>* res
;
1178 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
1179 def
, *psym
, st_shndx
, is_ordinary
,
1182 // If building a shared library using garbage collection, do not
1183 // treat externally visible symbols as garbage.
1184 if (parameters
->options().gc_sections()
1185 && parameters
->options().shared())
1186 this->gc_mark_symbol_for_shlib(res
);
1189 this->force_local(res
);
1191 (*sympointers
)[i
] = res
;
1195 // Add a symbol from a plugin-claimed file.
1197 template<int size
, bool big_endian
>
1199 Symbol_table::add_from_pluginobj(
1200 Sized_pluginobj
<size
, big_endian
>* obj
,
1203 elfcpp::Sym
<size
, big_endian
>* sym
)
1205 unsigned int st_shndx
= sym
->get_st_shndx();
1206 bool is_ordinary
= st_shndx
< elfcpp::SHN_LORESERVE
;
1208 Stringpool::Key ver_key
= 0;
1214 ver
= this->namepool_
.add(ver
, true, &ver_key
);
1216 // We don't want to assign a version to an undefined symbol,
1217 // even if it is listed in the version script. FIXME: What
1218 // about a common symbol?
1221 if (!this->version_script_
.empty()
1222 && st_shndx
!= elfcpp::SHN_UNDEF
)
1224 // The symbol name did not have a version, but the
1225 // version script may assign a version anyway.
1226 std::string version
;
1227 if (this->version_script_
.get_symbol_version(name
, &version
))
1229 // The version can be empty if the version script is
1230 // only used to force some symbols to be local.
1231 if (!version
.empty())
1233 ver
= this->namepool_
.add_with_length(version
.c_str(),
1240 else if (this->version_script_
.symbol_is_local(name
))
1245 Stringpool::Key name_key
;
1246 name
= this->namepool_
.add(name
, true, &name_key
);
1248 Sized_symbol
<size
>* res
;
1249 res
= this->add_from_object(obj
, name
, name_key
, ver
, ver_key
,
1250 def
, *sym
, st_shndx
, is_ordinary
, st_shndx
);
1253 this->force_local(res
);
1258 // Add all the symbols in a dynamic object to the hash table.
1260 template<int size
, bool big_endian
>
1262 Symbol_table::add_from_dynobj(
1263 Sized_dynobj
<size
, big_endian
>* dynobj
,
1264 const unsigned char* syms
,
1266 const char* sym_names
,
1267 size_t sym_name_size
,
1268 const unsigned char* versym
,
1270 const std::vector
<const char*>* version_map
,
1271 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
1276 gold_assert(size
== parameters
->target().get_size());
1278 if (dynobj
->just_symbols())
1280 gold_error(_("--just-symbols does not make sense with a shared object"));
1284 if (versym
!= NULL
&& versym_size
/ 2 < count
)
1286 dynobj
->error(_("too few symbol versions"));
1290 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1292 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1293 // weak aliases. This is necessary because if the dynamic object
1294 // provides the same variable under two names, one of which is a
1295 // weak definition, and the regular object refers to the weak
1296 // definition, we have to put both the weak definition and the
1297 // strong definition into the dynamic symbol table. Given a weak
1298 // definition, the only way that we can find the corresponding
1299 // strong definition, if any, is to search the symbol table.
1300 std::vector
<Sized_symbol
<size
>*> object_symbols
;
1302 const unsigned char* p
= syms
;
1303 const unsigned char* vs
= versym
;
1304 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
1306 elfcpp::Sym
<size
, big_endian
> sym(p
);
1308 if (sympointers
!= NULL
)
1309 (*sympointers
)[i
] = NULL
;
1311 // Ignore symbols with local binding or that have
1312 // internal or hidden visibility.
1313 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
1314 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
1315 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
1318 // A protected symbol in a shared library must be treated as a
1319 // normal symbol when viewed from outside the shared library.
1320 // Implement this by overriding the visibility here.
1321 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1322 unsigned char symbuf
[sym_size
];
1323 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1324 if (sym
.get_st_visibility() == elfcpp::STV_PROTECTED
)
1326 memcpy(symbuf
, p
, sym_size
);
1327 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1328 sw
.put_st_other(elfcpp::STV_DEFAULT
, sym
.get_st_nonvis());
1332 unsigned int st_name
= psym
->get_st_name();
1333 if (st_name
>= sym_name_size
)
1335 dynobj
->error(_("bad symbol name offset %u at %zu"),
1340 const char* name
= sym_names
+ st_name
;
1343 unsigned int st_shndx
= dynobj
->adjust_sym_shndx(i
, psym
->get_st_shndx(),
1346 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1349 Sized_symbol
<size
>* res
;
1353 Stringpool::Key name_key
;
1354 name
= this->namepool_
.add(name
, true, &name_key
);
1355 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1356 false, *psym
, st_shndx
, is_ordinary
,
1361 // Read the version information.
1363 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
1365 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
1366 v
&= elfcpp::VERSYM_VERSION
;
1368 // The Sun documentation says that V can be VER_NDX_LOCAL,
1369 // or VER_NDX_GLOBAL, or a version index. The meaning of
1370 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1371 // The old GNU linker will happily generate VER_NDX_LOCAL
1372 // for an undefined symbol. I don't know what the Sun
1373 // linker will generate.
1375 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1376 && st_shndx
!= elfcpp::SHN_UNDEF
)
1378 // This symbol should not be visible outside the object.
1382 // At this point we are definitely going to add this symbol.
1383 Stringpool::Key name_key
;
1384 name
= this->namepool_
.add(name
, true, &name_key
);
1386 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1387 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
1389 // This symbol does not have a version.
1390 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1391 false, *psym
, st_shndx
, is_ordinary
,
1396 if (v
>= version_map
->size())
1398 dynobj
->error(_("versym for symbol %zu out of range: %u"),
1403 const char* version
= (*version_map
)[v
];
1404 if (version
== NULL
)
1406 dynobj
->error(_("versym for symbol %zu has no name: %u"),
1411 Stringpool::Key version_key
;
1412 version
= this->namepool_
.add(version
, true, &version_key
);
1414 // If this is an absolute symbol, and the version name
1415 // and symbol name are the same, then this is the
1416 // version definition symbol. These symbols exist to
1417 // support using -u to pull in particular versions. We
1418 // do not want to record a version for them.
1419 if (st_shndx
== elfcpp::SHN_ABS
1421 && name_key
== version_key
)
1422 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1423 false, *psym
, st_shndx
, is_ordinary
,
1427 const bool def
= (!hidden
1428 && st_shndx
!= elfcpp::SHN_UNDEF
);
1429 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
1430 version_key
, def
, *psym
, st_shndx
,
1431 is_ordinary
, st_shndx
);
1436 // Note that it is possible that RES was overridden by an
1437 // earlier object, in which case it can't be aliased here.
1438 if (st_shndx
!= elfcpp::SHN_UNDEF
1440 && psym
->get_st_type() == elfcpp::STT_OBJECT
1441 && res
->source() == Symbol::FROM_OBJECT
1442 && res
->object() == dynobj
)
1443 object_symbols
.push_back(res
);
1445 if (sympointers
!= NULL
)
1446 (*sympointers
)[i
] = res
;
1449 this->record_weak_aliases(&object_symbols
);
1452 // This is used to sort weak aliases. We sort them first by section
1453 // index, then by offset, then by weak ahead of strong.
1456 class Weak_alias_sorter
1459 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
1464 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
1465 const Sized_symbol
<size
>* s2
) const
1468 unsigned int s1_shndx
= s1
->shndx(&is_ordinary
);
1469 gold_assert(is_ordinary
);
1470 unsigned int s2_shndx
= s2
->shndx(&is_ordinary
);
1471 gold_assert(is_ordinary
);
1472 if (s1_shndx
!= s2_shndx
)
1473 return s1_shndx
< s2_shndx
;
1475 if (s1
->value() != s2
->value())
1476 return s1
->value() < s2
->value();
1477 if (s1
->binding() != s2
->binding())
1479 if (s1
->binding() == elfcpp::STB_WEAK
)
1481 if (s2
->binding() == elfcpp::STB_WEAK
)
1484 return std::string(s1
->name()) < std::string(s2
->name());
1487 // SYMBOLS is a list of object symbols from a dynamic object. Look
1488 // for any weak aliases, and record them so that if we add the weak
1489 // alias to the dynamic symbol table, we also add the corresponding
1494 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
1496 // Sort the vector by section index, then by offset, then by weak
1498 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
1500 // Walk through the vector. For each weak definition, record
1502 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
1504 p
!= symbols
->end();
1507 if ((*p
)->binding() != elfcpp::STB_WEAK
)
1510 // Build a circular list of weak aliases. Each symbol points to
1511 // the next one in the circular list.
1513 Sized_symbol
<size
>* from_sym
= *p
;
1514 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
1515 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
1518 if ((*q
)->shndx(&dummy
) != from_sym
->shndx(&dummy
)
1519 || (*q
)->value() != from_sym
->value())
1522 this->weak_aliases_
[from_sym
] = *q
;
1523 from_sym
->set_has_alias();
1529 this->weak_aliases_
[from_sym
] = *p
;
1530 from_sym
->set_has_alias();
1537 // Create and return a specially defined symbol. If ONLY_IF_REF is
1538 // true, then only create the symbol if there is a reference to it.
1539 // If this does not return NULL, it sets *POLDSYM to the existing
1540 // symbol if there is one. This sets *RESOLVE_OLDSYM if we should
1541 // resolve the newly created symbol to the old one. This
1542 // canonicalizes *PNAME and *PVERSION.
1544 template<int size
, bool big_endian
>
1546 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1548 Sized_symbol
<size
>** poldsym
,
1549 bool *resolve_oldsym
)
1551 *resolve_oldsym
= false;
1553 // If the caller didn't give us a version, see if we get one from
1554 // the version script.
1556 bool is_default_version
= false;
1557 if (*pversion
== NULL
)
1559 if (this->version_script_
.get_symbol_version(*pname
, &v
))
1562 *pversion
= v
.c_str();
1564 // If we get the version from a version script, then we are
1565 // also the default version.
1566 is_default_version
= true;
1571 Sized_symbol
<size
>* sym
;
1573 bool add_to_table
= false;
1574 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1575 bool add_def_to_table
= false;
1576 typename
Symbol_table_type::iterator add_def_loc
= this->table_
.end();
1580 oldsym
= this->lookup(*pname
, *pversion
);
1581 if (oldsym
== NULL
&& is_default_version
)
1582 oldsym
= this->lookup(*pname
, NULL
);
1583 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1586 *pname
= oldsym
->name();
1587 if (!is_default_version
)
1588 *pversion
= oldsym
->version();
1592 // Canonicalize NAME and VERSION.
1593 Stringpool::Key name_key
;
1594 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1596 Stringpool::Key version_key
= 0;
1597 if (*pversion
!= NULL
)
1598 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1600 Symbol
* const snull
= NULL
;
1601 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1602 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1606 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
1607 std::make_pair(this->table_
.end(), false);
1608 if (is_default_version
)
1610 const Stringpool::Key vnull
= 0;
1611 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1618 // We already have a symbol table entry for NAME/VERSION.
1619 oldsym
= ins
.first
->second
;
1620 gold_assert(oldsym
!= NULL
);
1622 if (is_default_version
)
1624 Sized_symbol
<size
>* soldsym
=
1625 this->get_sized_symbol
<size
>(oldsym
);
1626 this->define_default_version
<size
, big_endian
>(soldsym
,
1633 // We haven't seen this symbol before.
1634 gold_assert(ins
.first
->second
== NULL
);
1636 add_to_table
= true;
1637 add_loc
= ins
.first
;
1639 if (is_default_version
&& !insdef
.second
)
1641 // We are adding NAME/VERSION, and it is the default
1642 // version. We already have an entry for NAME/NULL.
1643 oldsym
= insdef
.first
->second
;
1644 *resolve_oldsym
= true;
1650 if (is_default_version
)
1652 add_def_to_table
= true;
1653 add_def_loc
= insdef
.first
;
1659 const Target
& target
= parameters
->target();
1660 if (!target
.has_make_symbol())
1661 sym
= new Sized_symbol
<size
>();
1664 Sized_target
<size
, big_endian
>* sized_target
=
1665 parameters
->sized_target
<size
, big_endian
>();
1666 sym
= sized_target
->make_symbol();
1672 add_loc
->second
= sym
;
1674 gold_assert(oldsym
!= NULL
);
1676 if (add_def_to_table
)
1677 add_def_loc
->second
= sym
;
1679 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1684 // Define a symbol based on an Output_data.
1687 Symbol_table::define_in_output_data(const char* name
,
1688 const char* version
,
1693 elfcpp::STB binding
,
1694 elfcpp::STV visibility
,
1695 unsigned char nonvis
,
1696 bool offset_is_from_end
,
1699 if (parameters
->target().get_size() == 32)
1701 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1702 return this->do_define_in_output_data
<32>(name
, version
, od
,
1703 value
, symsize
, type
, binding
,
1711 else if (parameters
->target().get_size() == 64)
1713 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1714 return this->do_define_in_output_data
<64>(name
, version
, od
,
1715 value
, symsize
, type
, binding
,
1727 // Define a symbol in an Output_data, sized version.
1731 Symbol_table::do_define_in_output_data(
1733 const char* version
,
1735 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1736 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1738 elfcpp::STB binding
,
1739 elfcpp::STV visibility
,
1740 unsigned char nonvis
,
1741 bool offset_is_from_end
,
1744 Sized_symbol
<size
>* sym
;
1745 Sized_symbol
<size
>* oldsym
;
1746 bool resolve_oldsym
;
1748 if (parameters
->target().is_big_endian())
1750 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1751 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1752 only_if_ref
, &oldsym
,
1760 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1761 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1762 only_if_ref
, &oldsym
,
1772 sym
->init_output_data(name
, version
, od
, value
, symsize
, type
, binding
,
1773 visibility
, nonvis
, offset_is_from_end
);
1777 if (binding
== elfcpp::STB_LOCAL
1778 || this->version_script_
.symbol_is_local(name
))
1779 this->force_local(sym
);
1780 else if (version
!= NULL
)
1781 sym
->set_is_default();
1785 if (Symbol_table::should_override_with_special(oldsym
))
1786 this->override_with_special(oldsym
, sym
);
1797 // Define a symbol based on an Output_segment.
1800 Symbol_table::define_in_output_segment(const char* name
,
1801 const char* version
, Output_segment
* os
,
1805 elfcpp::STB binding
,
1806 elfcpp::STV visibility
,
1807 unsigned char nonvis
,
1808 Symbol::Segment_offset_base offset_base
,
1811 if (parameters
->target().get_size() == 32)
1813 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1814 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1815 value
, symsize
, type
,
1816 binding
, visibility
, nonvis
,
1817 offset_base
, only_if_ref
);
1822 else if (parameters
->target().get_size() == 64)
1824 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1825 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1826 value
, symsize
, type
,
1827 binding
, visibility
, nonvis
,
1828 offset_base
, only_if_ref
);
1837 // Define a symbol in an Output_segment, sized version.
1841 Symbol_table::do_define_in_output_segment(
1843 const char* version
,
1845 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1846 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1848 elfcpp::STB binding
,
1849 elfcpp::STV visibility
,
1850 unsigned char nonvis
,
1851 Symbol::Segment_offset_base offset_base
,
1854 Sized_symbol
<size
>* sym
;
1855 Sized_symbol
<size
>* oldsym
;
1856 bool resolve_oldsym
;
1858 if (parameters
->target().is_big_endian())
1860 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1861 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1862 only_if_ref
, &oldsym
,
1870 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1871 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1872 only_if_ref
, &oldsym
,
1882 sym
->init_output_segment(name
, version
, os
, value
, symsize
, type
, binding
,
1883 visibility
, nonvis
, offset_base
);
1887 if (binding
== elfcpp::STB_LOCAL
1888 || this->version_script_
.symbol_is_local(name
))
1889 this->force_local(sym
);
1890 else if (version
!= NULL
)
1891 sym
->set_is_default();
1895 if (Symbol_table::should_override_with_special(oldsym
))
1896 this->override_with_special(oldsym
, sym
);
1907 // Define a special symbol with a constant value. It is a multiple
1908 // definition error if this symbol is already defined.
1911 Symbol_table::define_as_constant(const char* name
,
1912 const char* version
,
1916 elfcpp::STB binding
,
1917 elfcpp::STV visibility
,
1918 unsigned char nonvis
,
1920 bool force_override
)
1922 if (parameters
->target().get_size() == 32)
1924 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1925 return this->do_define_as_constant
<32>(name
, version
, value
,
1926 symsize
, type
, binding
,
1927 visibility
, nonvis
, only_if_ref
,
1933 else if (parameters
->target().get_size() == 64)
1935 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1936 return this->do_define_as_constant
<64>(name
, version
, value
,
1937 symsize
, type
, binding
,
1938 visibility
, nonvis
, only_if_ref
,
1948 // Define a symbol as a constant, sized version.
1952 Symbol_table::do_define_as_constant(
1954 const char* version
,
1955 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1956 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1958 elfcpp::STB binding
,
1959 elfcpp::STV visibility
,
1960 unsigned char nonvis
,
1962 bool force_override
)
1964 Sized_symbol
<size
>* sym
;
1965 Sized_symbol
<size
>* oldsym
;
1966 bool resolve_oldsym
;
1968 if (parameters
->target().is_big_endian())
1970 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1971 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1972 only_if_ref
, &oldsym
,
1980 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1981 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1982 only_if_ref
, &oldsym
,
1992 sym
->init_constant(name
, version
, value
, symsize
, type
, binding
, visibility
,
1997 // Version symbols are absolute symbols with name == version.
1998 // We don't want to force them to be local.
1999 if ((version
== NULL
2002 && (binding
== elfcpp::STB_LOCAL
2003 || this->version_script_
.symbol_is_local(name
)))
2004 this->force_local(sym
);
2005 else if (version
!= NULL
2006 && (name
!= version
|| value
!= 0))
2007 sym
->set_is_default();
2011 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
2012 this->override_with_special(oldsym
, sym
);
2023 // Define a set of symbols in output sections.
2026 Symbol_table::define_symbols(const Layout
* layout
, int count
,
2027 const Define_symbol_in_section
* p
,
2030 for (int i
= 0; i
< count
; ++i
, ++p
)
2032 Output_section
* os
= layout
->find_output_section(p
->output_section
);
2034 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
2035 p
->size
, p
->type
, p
->binding
,
2036 p
->visibility
, p
->nonvis
,
2037 p
->offset_is_from_end
,
2038 only_if_ref
|| p
->only_if_ref
);
2040 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
2041 p
->binding
, p
->visibility
, p
->nonvis
,
2042 only_if_ref
|| p
->only_if_ref
,
2047 // Define a set of symbols in output segments.
2050 Symbol_table::define_symbols(const Layout
* layout
, int count
,
2051 const Define_symbol_in_segment
* p
,
2054 for (int i
= 0; i
< count
; ++i
, ++p
)
2056 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
2057 p
->segment_flags_set
,
2058 p
->segment_flags_clear
);
2060 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
2061 p
->size
, p
->type
, p
->binding
,
2062 p
->visibility
, p
->nonvis
,
2064 only_if_ref
|| p
->only_if_ref
);
2066 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
2067 p
->binding
, p
->visibility
, p
->nonvis
,
2068 only_if_ref
|| p
->only_if_ref
,
2073 // Define CSYM using a COPY reloc. POSD is the Output_data where the
2074 // symbol should be defined--typically a .dyn.bss section. VALUE is
2075 // the offset within POSD.
2079 Symbol_table::define_with_copy_reloc(
2080 Sized_symbol
<size
>* csym
,
2082 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
2084 gold_assert(csym
->is_from_dynobj());
2085 gold_assert(!csym
->is_copied_from_dynobj());
2086 Object
* object
= csym
->object();
2087 gold_assert(object
->is_dynamic());
2088 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
2090 // Our copied variable has to override any variable in a shared
2092 elfcpp::STB binding
= csym
->binding();
2093 if (binding
== elfcpp::STB_WEAK
)
2094 binding
= elfcpp::STB_GLOBAL
;
2096 this->define_in_output_data(csym
->name(), csym
->version(),
2097 posd
, value
, csym
->symsize(),
2098 csym
->type(), binding
,
2099 csym
->visibility(), csym
->nonvis(),
2102 csym
->set_is_copied_from_dynobj();
2103 csym
->set_needs_dynsym_entry();
2105 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
2107 // We have now defined all aliases, but we have not entered them all
2108 // in the copied_symbol_dynobjs_ map.
2109 if (csym
->has_alias())
2114 sym
= this->weak_aliases_
[sym
];
2117 gold_assert(sym
->output_data() == posd
);
2119 sym
->set_is_copied_from_dynobj();
2120 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
2125 // SYM is defined using a COPY reloc. Return the dynamic object where
2126 // the original definition was found.
2129 Symbol_table::get_copy_source(const Symbol
* sym
) const
2131 gold_assert(sym
->is_copied_from_dynobj());
2132 Copied_symbol_dynobjs::const_iterator p
=
2133 this->copied_symbol_dynobjs_
.find(sym
);
2134 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
2138 // Add any undefined symbols named on the command line.
2141 Symbol_table::add_undefined_symbols_from_command_line()
2143 if (parameters
->options().any_undefined())
2145 if (parameters
->target().get_size() == 32)
2147 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2148 this->do_add_undefined_symbols_from_command_line
<32>();
2153 else if (parameters
->target().get_size() == 64)
2155 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2156 this->do_add_undefined_symbols_from_command_line
<64>();
2168 Symbol_table::do_add_undefined_symbols_from_command_line()
2170 for (options::String_set::const_iterator p
=
2171 parameters
->options().undefined_begin();
2172 p
!= parameters
->options().undefined_end();
2175 const char* name
= p
->c_str();
2177 if (this->lookup(name
) != NULL
)
2180 const char* version
= NULL
;
2182 Sized_symbol
<size
>* sym
;
2183 Sized_symbol
<size
>* oldsym
;
2184 bool resolve_oldsym
;
2185 if (parameters
->target().is_big_endian())
2187 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2188 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
2197 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2198 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
2206 gold_assert(oldsym
== NULL
);
2208 sym
->init_undefined(name
, version
, elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
2209 elfcpp::STV_DEFAULT
, 0);
2210 ++this->saw_undefined_
;
2214 // Set the dynamic symbol indexes. INDEX is the index of the first
2215 // global dynamic symbol. Pointers to the symbols are stored into the
2216 // vector SYMS. The names are added to DYNPOOL. This returns an
2217 // updated dynamic symbol index.
2220 Symbol_table::set_dynsym_indexes(unsigned int index
,
2221 std::vector
<Symbol
*>* syms
,
2222 Stringpool
* dynpool
,
2225 for (Symbol_table_type::iterator p
= this->table_
.begin();
2226 p
!= this->table_
.end();
2229 Symbol
* sym
= p
->second
;
2231 // Note that SYM may already have a dynamic symbol index, since
2232 // some symbols appear more than once in the symbol table, with
2233 // and without a version.
2235 if (!sym
->should_add_dynsym_entry())
2236 sym
->set_dynsym_index(-1U);
2237 else if (!sym
->has_dynsym_index())
2239 sym
->set_dynsym_index(index
);
2241 syms
->push_back(sym
);
2242 dynpool
->add(sym
->name(), false, NULL
);
2244 // Record any version information.
2245 if (sym
->version() != NULL
)
2246 versions
->record_version(this, dynpool
, sym
);
2248 // If the symbol is defined in a dynamic object and is
2249 // referenced in a regular object, then mark the dynamic
2250 // object as needed. This is used to implement --as-needed.
2251 if (sym
->is_from_dynobj() && sym
->in_reg())
2252 sym
->object()->set_is_needed();
2256 // Finish up the versions. In some cases this may add new dynamic
2258 index
= versions
->finalize(this, index
, syms
);
2263 // Set the final values for all the symbols. The index of the first
2264 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2265 // file offset OFF. Add their names to POOL. Return the new file
2266 // offset. Update *PLOCAL_SYMCOUNT if necessary.
2269 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
2270 size_t dyncount
, Stringpool
* pool
,
2271 unsigned int *plocal_symcount
)
2275 gold_assert(*plocal_symcount
!= 0);
2276 this->first_global_index_
= *plocal_symcount
;
2278 this->dynamic_offset_
= dynoff
;
2279 this->first_dynamic_global_index_
= dyn_global_index
;
2280 this->dynamic_count_
= dyncount
;
2282 if (parameters
->target().get_size() == 32)
2284 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2285 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
2290 else if (parameters
->target().get_size() == 64)
2292 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2293 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
2301 // Now that we have the final symbol table, we can reliably note
2302 // which symbols should get warnings.
2303 this->warnings_
.note_warnings(this);
2308 // SYM is going into the symbol table at *PINDEX. Add the name to
2309 // POOL, update *PINDEX and *POFF.
2313 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
2314 unsigned int* pindex
, off_t
* poff
)
2316 sym
->set_symtab_index(*pindex
);
2317 pool
->add(sym
->name(), false, NULL
);
2319 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
2322 // Set the final value for all the symbols. This is called after
2323 // Layout::finalize, so all the output sections have their final
2328 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
2329 unsigned int* plocal_symcount
)
2331 off
= align_address(off
, size
>> 3);
2332 this->offset_
= off
;
2334 unsigned int index
= *plocal_symcount
;
2335 const unsigned int orig_index
= index
;
2337 // First do all the symbols which have been forced to be local, as
2338 // they must appear before all global symbols.
2339 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
2340 p
!= this->forced_locals_
.end();
2344 gold_assert(sym
->is_forced_local());
2345 if (this->sized_finalize_symbol
<size
>(sym
))
2347 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2352 // Now do all the remaining symbols.
2353 for (Symbol_table_type::iterator p
= this->table_
.begin();
2354 p
!= this->table_
.end();
2357 Symbol
* sym
= p
->second
;
2358 if (this->sized_finalize_symbol
<size
>(sym
))
2359 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2362 this->output_count_
= index
- orig_index
;
2367 // Compute the final value of SYM and store status in location PSTATUS.
2368 // During relaxation, this may be called multiple times for a symbol to
2369 // compute its would-be final value in each relaxation pass.
2372 typename Sized_symbol
<size
>::Value_type
2373 Symbol_table::compute_final_value(
2374 const Sized_symbol
<size
>* sym
,
2375 Compute_final_value_status
* pstatus
) const
2377 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2380 switch (sym
->source())
2382 case Symbol::FROM_OBJECT
:
2385 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2388 && shndx
!= elfcpp::SHN_ABS
2389 && !Symbol::is_common_shndx(shndx
))
2391 *pstatus
= CFVS_UNSUPPORTED_SYMBOL_SECTION
;
2395 Object
* symobj
= sym
->object();
2396 if (symobj
->is_dynamic())
2399 shndx
= elfcpp::SHN_UNDEF
;
2401 else if (symobj
->pluginobj() != NULL
)
2404 shndx
= elfcpp::SHN_UNDEF
;
2406 else if (shndx
== elfcpp::SHN_UNDEF
)
2408 else if (!is_ordinary
2409 && (shndx
== elfcpp::SHN_ABS
2410 || Symbol::is_common_shndx(shndx
)))
2411 value
= sym
->value();
2414 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2415 Output_section
* os
= relobj
->output_section(shndx
);
2417 if (this->is_section_folded(relobj
, shndx
))
2419 gold_assert(os
== NULL
);
2420 // Get the os of the section it is folded onto.
2421 Section_id folded
= this->icf_
->get_folded_section(relobj
,
2423 gold_assert(folded
.first
!= NULL
);
2424 Relobj
* folded_obj
= reinterpret_cast<Relobj
*>(folded
.first
);
2425 unsigned folded_shndx
= folded
.second
;
2427 os
= folded_obj
->output_section(folded_shndx
);
2428 gold_assert(os
!= NULL
);
2430 // Replace (relobj, shndx) with canonical ICF input section.
2431 shndx
= folded_shndx
;
2432 relobj
= folded_obj
;
2435 uint64_t secoff64
= relobj
->output_section_offset(shndx
);
2438 bool static_or_reloc
= (parameters
->doing_static_link() ||
2439 parameters
->options().relocatable());
2440 gold_assert(static_or_reloc
|| sym
->dynsym_index() == -1U);
2442 *pstatus
= CFVS_NO_OUTPUT_SECTION
;
2446 if (secoff64
== -1ULL)
2448 // The section needs special handling (e.g., a merge section).
2450 value
= os
->output_address(relobj
, shndx
, sym
->value());
2455 convert_types
<Value_type
, uint64_t>(secoff64
);
2456 if (sym
->type() == elfcpp::STT_TLS
)
2457 value
= sym
->value() + os
->tls_offset() + secoff
;
2459 value
= sym
->value() + os
->address() + secoff
;
2465 case Symbol::IN_OUTPUT_DATA
:
2467 Output_data
* od
= sym
->output_data();
2468 value
= sym
->value();
2469 if (sym
->type() != elfcpp::STT_TLS
)
2470 value
+= od
->address();
2473 Output_section
* os
= od
->output_section();
2474 gold_assert(os
!= NULL
);
2475 value
+= os
->tls_offset() + (od
->address() - os
->address());
2477 if (sym
->offset_is_from_end())
2478 value
+= od
->data_size();
2482 case Symbol::IN_OUTPUT_SEGMENT
:
2484 Output_segment
* os
= sym
->output_segment();
2485 value
= sym
->value();
2486 if (sym
->type() != elfcpp::STT_TLS
)
2487 value
+= os
->vaddr();
2488 switch (sym
->offset_base())
2490 case Symbol::SEGMENT_START
:
2492 case Symbol::SEGMENT_END
:
2493 value
+= os
->memsz();
2495 case Symbol::SEGMENT_BSS
:
2496 value
+= os
->filesz();
2504 case Symbol::IS_CONSTANT
:
2505 value
= sym
->value();
2508 case Symbol::IS_UNDEFINED
:
2520 // Finalize the symbol SYM. This returns true if the symbol should be
2521 // added to the symbol table, false otherwise.
2525 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
2527 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2529 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
2531 // The default version of a symbol may appear twice in the symbol
2532 // table. We only need to finalize it once.
2533 if (sym
->has_symtab_index())
2538 gold_assert(!sym
->has_symtab_index());
2539 sym
->set_symtab_index(-1U);
2540 gold_assert(sym
->dynsym_index() == -1U);
2544 // Compute final symbol value.
2545 Compute_final_value_status status
;
2546 Value_type value
= this->compute_final_value(sym
, &status
);
2552 case CFVS_UNSUPPORTED_SYMBOL_SECTION
:
2555 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2556 gold_error(_("%s: unsupported symbol section 0x%x"),
2557 sym
->demangled_name().c_str(), shndx
);
2560 case CFVS_NO_OUTPUT_SECTION
:
2561 sym
->set_symtab_index(-1U);
2567 sym
->set_value(value
);
2569 if (parameters
->options().strip_all()
2570 || !parameters
->options().should_retain_symbol(sym
->name()))
2572 sym
->set_symtab_index(-1U);
2579 // Write out the global symbols.
2582 Symbol_table::write_globals(const Stringpool
* sympool
,
2583 const Stringpool
* dynpool
,
2584 Output_symtab_xindex
* symtab_xindex
,
2585 Output_symtab_xindex
* dynsym_xindex
,
2586 Output_file
* of
) const
2588 switch (parameters
->size_and_endianness())
2590 #ifdef HAVE_TARGET_32_LITTLE
2591 case Parameters::TARGET_32_LITTLE
:
2592 this->sized_write_globals
<32, false>(sympool
, dynpool
, symtab_xindex
,
2596 #ifdef HAVE_TARGET_32_BIG
2597 case Parameters::TARGET_32_BIG
:
2598 this->sized_write_globals
<32, true>(sympool
, dynpool
, symtab_xindex
,
2602 #ifdef HAVE_TARGET_64_LITTLE
2603 case Parameters::TARGET_64_LITTLE
:
2604 this->sized_write_globals
<64, false>(sympool
, dynpool
, symtab_xindex
,
2608 #ifdef HAVE_TARGET_64_BIG
2609 case Parameters::TARGET_64_BIG
:
2610 this->sized_write_globals
<64, true>(sympool
, dynpool
, symtab_xindex
,
2619 // Write out the global symbols.
2621 template<int size
, bool big_endian
>
2623 Symbol_table::sized_write_globals(const Stringpool
* sympool
,
2624 const Stringpool
* dynpool
,
2625 Output_symtab_xindex
* symtab_xindex
,
2626 Output_symtab_xindex
* dynsym_xindex
,
2627 Output_file
* of
) const
2629 const Target
& target
= parameters
->target();
2631 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2633 const unsigned int output_count
= this->output_count_
;
2634 const section_size_type oview_size
= output_count
* sym_size
;
2635 const unsigned int first_global_index
= this->first_global_index_
;
2636 unsigned char* psyms
;
2637 if (this->offset_
== 0 || output_count
== 0)
2640 psyms
= of
->get_output_view(this->offset_
, oview_size
);
2642 const unsigned int dynamic_count
= this->dynamic_count_
;
2643 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
2644 const unsigned int first_dynamic_global_index
=
2645 this->first_dynamic_global_index_
;
2646 unsigned char* dynamic_view
;
2647 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
2648 dynamic_view
= NULL
;
2650 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
2652 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
2653 p
!= this->table_
.end();
2656 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
2658 // Possibly warn about unresolved symbols in shared libraries.
2659 this->warn_about_undefined_dynobj_symbol(sym
);
2661 unsigned int sym_index
= sym
->symtab_index();
2662 unsigned int dynsym_index
;
2663 if (dynamic_view
== NULL
)
2666 dynsym_index
= sym
->dynsym_index();
2668 if (sym_index
== -1U && dynsym_index
== -1U)
2670 // This symbol is not included in the output file.
2675 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
2676 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
2677 switch (sym
->source())
2679 case Symbol::FROM_OBJECT
:
2682 unsigned int in_shndx
= sym
->shndx(&is_ordinary
);
2685 && in_shndx
!= elfcpp::SHN_ABS
2686 && !Symbol::is_common_shndx(in_shndx
))
2688 gold_error(_("%s: unsupported symbol section 0x%x"),
2689 sym
->demangled_name().c_str(), in_shndx
);
2694 Object
* symobj
= sym
->object();
2695 if (symobj
->is_dynamic())
2697 if (sym
->needs_dynsym_value())
2698 dynsym_value
= target
.dynsym_value(sym
);
2699 shndx
= elfcpp::SHN_UNDEF
;
2701 else if (symobj
->pluginobj() != NULL
)
2702 shndx
= elfcpp::SHN_UNDEF
;
2703 else if (in_shndx
== elfcpp::SHN_UNDEF
2705 && (in_shndx
== elfcpp::SHN_ABS
2706 || Symbol::is_common_shndx(in_shndx
))))
2710 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2711 Output_section
* os
= relobj
->output_section(in_shndx
);
2712 if (this->is_section_folded(relobj
, in_shndx
))
2714 // This global symbol must be written out even though
2716 // Get the os of the section it is folded onto.
2718 this->icf_
->get_folded_section(relobj
, in_shndx
);
2719 gold_assert(folded
.first
!=NULL
);
2720 Relobj
* folded_obj
=
2721 reinterpret_cast<Relobj
*>(folded
.first
);
2722 os
= folded_obj
->output_section(folded
.second
);
2723 gold_assert(os
!= NULL
);
2725 gold_assert(os
!= NULL
);
2726 shndx
= os
->out_shndx();
2728 if (shndx
>= elfcpp::SHN_LORESERVE
)
2730 if (sym_index
!= -1U)
2731 symtab_xindex
->add(sym_index
, shndx
);
2732 if (dynsym_index
!= -1U)
2733 dynsym_xindex
->add(dynsym_index
, shndx
);
2734 shndx
= elfcpp::SHN_XINDEX
;
2737 // In object files symbol values are section
2739 if (parameters
->options().relocatable())
2740 sym_value
-= os
->address();
2746 case Symbol::IN_OUTPUT_DATA
:
2747 shndx
= sym
->output_data()->out_shndx();
2748 if (shndx
>= elfcpp::SHN_LORESERVE
)
2750 if (sym_index
!= -1U)
2751 symtab_xindex
->add(sym_index
, shndx
);
2752 if (dynsym_index
!= -1U)
2753 dynsym_xindex
->add(dynsym_index
, shndx
);
2754 shndx
= elfcpp::SHN_XINDEX
;
2758 case Symbol::IN_OUTPUT_SEGMENT
:
2759 shndx
= elfcpp::SHN_ABS
;
2762 case Symbol::IS_CONSTANT
:
2763 shndx
= elfcpp::SHN_ABS
;
2766 case Symbol::IS_UNDEFINED
:
2767 shndx
= elfcpp::SHN_UNDEF
;
2774 if (sym_index
!= -1U)
2776 sym_index
-= first_global_index
;
2777 gold_assert(sym_index
< output_count
);
2778 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
2779 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
2783 if (dynsym_index
!= -1U)
2785 dynsym_index
-= first_dynamic_global_index
;
2786 gold_assert(dynsym_index
< dynamic_count
);
2787 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
2788 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
2793 of
->write_output_view(this->offset_
, oview_size
, psyms
);
2794 if (dynamic_view
!= NULL
)
2795 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
2798 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2799 // strtab holding the name.
2801 template<int size
, bool big_endian
>
2803 Symbol_table::sized_write_symbol(
2804 Sized_symbol
<size
>* sym
,
2805 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
2807 const Stringpool
* pool
,
2808 unsigned char* p
) const
2810 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
2811 osym
.put_st_name(pool
->get_offset(sym
->name()));
2812 osym
.put_st_value(value
);
2813 // Use a symbol size of zero for undefined symbols from shared libraries.
2814 if (shndx
== elfcpp::SHN_UNDEF
&& sym
->is_from_dynobj())
2815 osym
.put_st_size(0);
2817 osym
.put_st_size(sym
->symsize());
2818 elfcpp::STT type
= sym
->type();
2819 // Turn IFUNC symbols from shared libraries into normal FUNC symbols.
2820 if (type
== elfcpp::STT_GNU_IFUNC
2821 && sym
->is_from_dynobj())
2822 type
= elfcpp::STT_FUNC
;
2823 // A version script may have overridden the default binding.
2824 if (sym
->is_forced_local())
2825 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, type
));
2827 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), type
));
2828 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
2829 osym
.put_st_shndx(shndx
);
2832 // Check for unresolved symbols in shared libraries. This is
2833 // controlled by the --allow-shlib-undefined option.
2835 // We only warn about libraries for which we have seen all the
2836 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2837 // which were not seen in this link. If we didn't see a DT_NEEDED
2838 // entry, we aren't going to be able to reliably report whether the
2839 // symbol is undefined.
2841 // We also don't warn about libraries found in a system library
2842 // directory (e.g., /lib or /usr/lib); we assume that those libraries
2843 // are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
2844 // can have undefined references satisfied by ld-linux.so.
2847 Symbol_table::warn_about_undefined_dynobj_symbol(Symbol
* sym
) const
2850 if (sym
->source() == Symbol::FROM_OBJECT
2851 && sym
->object()->is_dynamic()
2852 && sym
->shndx(&dummy
) == elfcpp::SHN_UNDEF
2853 && sym
->binding() != elfcpp::STB_WEAK
2854 && !parameters
->options().allow_shlib_undefined()
2855 && !parameters
->target().is_defined_by_abi(sym
)
2856 && !sym
->object()->is_in_system_directory())
2858 // A very ugly cast.
2859 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2860 if (!dynobj
->has_unknown_needed_entries())
2861 gold_undefined_symbol(sym
);
2865 // Write out a section symbol. Return the update offset.
2868 Symbol_table::write_section_symbol(const Output_section
*os
,
2869 Output_symtab_xindex
* symtab_xindex
,
2873 switch (parameters
->size_and_endianness())
2875 #ifdef HAVE_TARGET_32_LITTLE
2876 case Parameters::TARGET_32_LITTLE
:
2877 this->sized_write_section_symbol
<32, false>(os
, symtab_xindex
, of
,
2881 #ifdef HAVE_TARGET_32_BIG
2882 case Parameters::TARGET_32_BIG
:
2883 this->sized_write_section_symbol
<32, true>(os
, symtab_xindex
, of
,
2887 #ifdef HAVE_TARGET_64_LITTLE
2888 case Parameters::TARGET_64_LITTLE
:
2889 this->sized_write_section_symbol
<64, false>(os
, symtab_xindex
, of
,
2893 #ifdef HAVE_TARGET_64_BIG
2894 case Parameters::TARGET_64_BIG
:
2895 this->sized_write_section_symbol
<64, true>(os
, symtab_xindex
, of
,
2904 // Write out a section symbol, specialized for size and endianness.
2906 template<int size
, bool big_endian
>
2908 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2909 Output_symtab_xindex
* symtab_xindex
,
2913 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2915 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2917 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2918 osym
.put_st_name(0);
2919 if (parameters
->options().relocatable())
2920 osym
.put_st_value(0);
2922 osym
.put_st_value(os
->address());
2923 osym
.put_st_size(0);
2924 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2925 elfcpp::STT_SECTION
));
2926 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2928 unsigned int shndx
= os
->out_shndx();
2929 if (shndx
>= elfcpp::SHN_LORESERVE
)
2931 symtab_xindex
->add(os
->symtab_index(), shndx
);
2932 shndx
= elfcpp::SHN_XINDEX
;
2934 osym
.put_st_shndx(shndx
);
2936 of
->write_output_view(offset
, sym_size
, pov
);
2939 // Print statistical information to stderr. This is used for --stats.
2942 Symbol_table::print_stats() const
2944 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2945 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2946 program_name
, this->table_
.size(), this->table_
.bucket_count());
2948 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2949 program_name
, this->table_
.size());
2951 this->namepool_
.print_stats("symbol table stringpool");
2954 // We check for ODR violations by looking for symbols with the same
2955 // name for which the debugging information reports that they were
2956 // defined in different source locations. When comparing the source
2957 // location, we consider instances with the same base filename and
2958 // line number to be the same. This is because different object
2959 // files/shared libraries can include the same header file using
2960 // different paths, and we don't want to report an ODR violation in
2963 // This struct is used to compare line information, as returned by
2964 // Dwarf_line_info::one_addr2line. It implements a < comparison
2965 // operator used with std::set.
2967 struct Odr_violation_compare
2970 operator()(const std::string
& s1
, const std::string
& s2
) const
2972 std::string::size_type pos1
= s1
.rfind('/');
2973 std::string::size_type pos2
= s2
.rfind('/');
2974 if (pos1
== std::string::npos
2975 || pos2
== std::string::npos
)
2977 return s1
.compare(pos1
, std::string::npos
,
2978 s2
, pos2
, std::string::npos
) < 0;
2982 // Check candidate_odr_violations_ to find symbols with the same name
2983 // but apparently different definitions (different source-file/line-no).
2986 Symbol_table::detect_odr_violations(const Task
* task
,
2987 const char* output_file_name
) const
2989 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2990 it
!= candidate_odr_violations_
.end();
2993 const char* symbol_name
= it
->first
;
2994 // We use a sorted set so the output is deterministic.
2995 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2997 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2998 locs
= it
->second
.begin();
2999 locs
!= it
->second
.end();
3002 // We need to lock the object in order to read it. This
3003 // means that we have to run in a singleton Task. If we
3004 // want to run this in a general Task for better
3005 // performance, we will need one Task for object, plus
3006 // appropriate locking to ensure that we don't conflict with
3007 // other uses of the object. Also note, one_addr2line is not
3008 // currently thread-safe.
3009 Task_lock_obj
<Object
> tl(task
, locs
->object
);
3010 // 16 is the size of the object-cache that one_addr2line should use.
3011 std::string lineno
= Dwarf_line_info::one_addr2line(
3012 locs
->object
, locs
->shndx
, locs
->offset
, 16);
3013 if (!lineno
.empty())
3014 line_nums
.insert(lineno
);
3017 if (line_nums
.size() > 1)
3019 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
3020 "places (possible ODR violation):"),
3021 output_file_name
, demangle(symbol_name
).c_str());
3022 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
3023 it2
!= line_nums
.end();
3025 fprintf(stderr
, " %s\n", it2
->c_str());
3028 // We only call one_addr2line() in this function, so we can clear its cache.
3029 Dwarf_line_info::clear_addr2line_cache();
3032 // Warnings functions.
3034 // Add a new warning.
3037 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
3038 const std::string
& warning
)
3040 name
= symtab
->canonicalize_name(name
);
3041 this->warnings_
[name
].set(obj
, warning
);
3044 // Look through the warnings and mark the symbols for which we should
3045 // warn. This is called during Layout::finalize when we know the
3046 // sources for all the symbols.
3049 Warnings::note_warnings(Symbol_table
* symtab
)
3051 for (Warning_table::iterator p
= this->warnings_
.begin();
3052 p
!= this->warnings_
.end();
3055 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
3057 && sym
->source() == Symbol::FROM_OBJECT
3058 && sym
->object() == p
->second
.object
)
3059 sym
->set_has_warning();
3063 // Issue a warning. This is called when we see a relocation against a
3064 // symbol for which has a warning.
3066 template<int size
, bool big_endian
>
3068 Warnings::issue_warning(const Symbol
* sym
,
3069 const Relocate_info
<size
, big_endian
>* relinfo
,
3070 size_t relnum
, off_t reloffset
) const
3072 gold_assert(sym
->has_warning());
3073 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
3074 gold_assert(p
!= this->warnings_
.end());
3075 gold_warning_at_location(relinfo
, relnum
, reloffset
,
3076 "%s", p
->second
.text
.c_str());
3079 // Instantiate the templates we need. We could use the configure
3080 // script to restrict this to only the ones needed for implemented
3083 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3086 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
3089 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3092 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
3095 #ifdef HAVE_TARGET_32_LITTLE
3098 Symbol_table::add_from_relobj
<32, false>(
3099 Sized_relobj
<32, false>* relobj
,
3100 const unsigned char* syms
,
3102 size_t symndx_offset
,
3103 const char* sym_names
,
3104 size_t sym_name_size
,
3105 Sized_relobj
<32, false>::Symbols
* sympointers
,
3109 #ifdef HAVE_TARGET_32_BIG
3112 Symbol_table::add_from_relobj
<32, true>(
3113 Sized_relobj
<32, true>* relobj
,
3114 const unsigned char* syms
,
3116 size_t symndx_offset
,
3117 const char* sym_names
,
3118 size_t sym_name_size
,
3119 Sized_relobj
<32, true>::Symbols
* sympointers
,
3123 #ifdef HAVE_TARGET_64_LITTLE
3126 Symbol_table::add_from_relobj
<64, false>(
3127 Sized_relobj
<64, false>* relobj
,
3128 const unsigned char* syms
,
3130 size_t symndx_offset
,
3131 const char* sym_names
,
3132 size_t sym_name_size
,
3133 Sized_relobj
<64, false>::Symbols
* sympointers
,
3137 #ifdef HAVE_TARGET_64_BIG
3140 Symbol_table::add_from_relobj
<64, true>(
3141 Sized_relobj
<64, true>* relobj
,
3142 const unsigned char* syms
,
3144 size_t symndx_offset
,
3145 const char* sym_names
,
3146 size_t sym_name_size
,
3147 Sized_relobj
<64, true>::Symbols
* sympointers
,
3151 #ifdef HAVE_TARGET_32_LITTLE
3154 Symbol_table::add_from_pluginobj
<32, false>(
3155 Sized_pluginobj
<32, false>* obj
,
3158 elfcpp::Sym
<32, false>* sym
);
3161 #ifdef HAVE_TARGET_32_BIG
3164 Symbol_table::add_from_pluginobj
<32, true>(
3165 Sized_pluginobj
<32, true>* obj
,
3168 elfcpp::Sym
<32, true>* sym
);
3171 #ifdef HAVE_TARGET_64_LITTLE
3174 Symbol_table::add_from_pluginobj
<64, false>(
3175 Sized_pluginobj
<64, false>* obj
,
3178 elfcpp::Sym
<64, false>* sym
);
3181 #ifdef HAVE_TARGET_64_BIG
3184 Symbol_table::add_from_pluginobj
<64, true>(
3185 Sized_pluginobj
<64, true>* obj
,
3188 elfcpp::Sym
<64, true>* sym
);
3191 #ifdef HAVE_TARGET_32_LITTLE
3194 Symbol_table::add_from_dynobj
<32, false>(
3195 Sized_dynobj
<32, false>* dynobj
,
3196 const unsigned char* syms
,
3198 const char* sym_names
,
3199 size_t sym_name_size
,
3200 const unsigned char* versym
,
3202 const std::vector
<const char*>* version_map
,
3203 Sized_relobj
<32, false>::Symbols
* sympointers
,
3207 #ifdef HAVE_TARGET_32_BIG
3210 Symbol_table::add_from_dynobj
<32, true>(
3211 Sized_dynobj
<32, true>* dynobj
,
3212 const unsigned char* syms
,
3214 const char* sym_names
,
3215 size_t sym_name_size
,
3216 const unsigned char* versym
,
3218 const std::vector
<const char*>* version_map
,
3219 Sized_relobj
<32, true>::Symbols
* sympointers
,
3223 #ifdef HAVE_TARGET_64_LITTLE
3226 Symbol_table::add_from_dynobj
<64, false>(
3227 Sized_dynobj
<64, false>* dynobj
,
3228 const unsigned char* syms
,
3230 const char* sym_names
,
3231 size_t sym_name_size
,
3232 const unsigned char* versym
,
3234 const std::vector
<const char*>* version_map
,
3235 Sized_relobj
<64, false>::Symbols
* sympointers
,
3239 #ifdef HAVE_TARGET_64_BIG
3242 Symbol_table::add_from_dynobj
<64, true>(
3243 Sized_dynobj
<64, true>* dynobj
,
3244 const unsigned char* syms
,
3246 const char* sym_names
,
3247 size_t sym_name_size
,
3248 const unsigned char* versym
,
3250 const std::vector
<const char*>* version_map
,
3251 Sized_relobj
<64, true>::Symbols
* sympointers
,
3255 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3258 Symbol_table::define_with_copy_reloc
<32>(
3259 Sized_symbol
<32>* sym
,
3261 elfcpp::Elf_types
<32>::Elf_Addr value
);
3264 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3267 Symbol_table::define_with_copy_reloc
<64>(
3268 Sized_symbol
<64>* sym
,
3270 elfcpp::Elf_types
<64>::Elf_Addr value
);
3273 #ifdef HAVE_TARGET_32_LITTLE
3276 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
3277 const Relocate_info
<32, false>* relinfo
,
3278 size_t relnum
, off_t reloffset
) const;
3281 #ifdef HAVE_TARGET_32_BIG
3284 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
3285 const Relocate_info
<32, true>* relinfo
,
3286 size_t relnum
, off_t reloffset
) const;
3289 #ifdef HAVE_TARGET_64_LITTLE
3292 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
3293 const Relocate_info
<64, false>* relinfo
,
3294 size_t relnum
, off_t reloffset
) const;
3297 #ifdef HAVE_TARGET_64_BIG
3300 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
3301 const Relocate_info
<64, true>* relinfo
,
3302 size_t relnum
, off_t reloffset
) const;
3305 } // End namespace gold.