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 // Allocate a common symbol.
287 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
289 this->allocate_base_common(od
);
290 this->value_
= value
;
293 // The ""'s around str ensure str is a string literal, so sizeof works.
294 #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
296 // Return true if this symbol should be added to the dynamic symbol
300 Symbol::should_add_dynsym_entry() const
302 // If the symbol is used by a dynamic relocation, we need to add it.
303 if (this->needs_dynsym_entry())
306 // If this symbol's section is not added, the symbol need not be added.
307 // The section may have been GCed. Note that export_dynamic is being
308 // overridden here. This should not be done for shared objects.
309 if (parameters
->options().gc_sections()
310 && !parameters
->options().shared()
311 && this->source() == Symbol::FROM_OBJECT
312 && !this->object()->is_dynamic())
314 Relobj
* relobj
= static_cast<Relobj
*>(this->object());
316 unsigned int shndx
= this->shndx(&is_ordinary
);
317 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
318 && !relobj
->is_section_included(shndx
))
322 // If the symbol was forced local in a version script, do not add it.
323 if (this->is_forced_local())
326 // If the symbol was forced dynamic in a --dynamic-list file, add it.
327 if (parameters
->options().in_dynamic_list(this->name()))
330 // If dynamic-list-data was specified, add any STT_OBJECT.
331 if (parameters
->options().dynamic_list_data()
332 && !this->is_from_dynobj()
333 && this->type() == elfcpp::STT_OBJECT
)
336 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
337 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
338 if ((parameters
->options().dynamic_list_cpp_new()
339 || parameters
->options().dynamic_list_cpp_typeinfo())
340 && !this->is_from_dynobj())
342 // TODO(csilvers): We could probably figure out if we're an operator
343 // new/delete or typeinfo without the need to demangle.
344 char* demangled_name
= cplus_demangle(this->name(),
345 DMGL_ANSI
| DMGL_PARAMS
);
346 if (demangled_name
== NULL
)
348 // Not a C++ symbol, so it can't satisfy these flags
350 else if (parameters
->options().dynamic_list_cpp_new()
351 && (strprefix(demangled_name
, "operator new")
352 || strprefix(demangled_name
, "operator delete")))
354 free(demangled_name
);
357 else if (parameters
->options().dynamic_list_cpp_typeinfo()
358 && (strprefix(demangled_name
, "typeinfo name for")
359 || strprefix(demangled_name
, "typeinfo for")))
361 free(demangled_name
);
365 free(demangled_name
);
368 // If exporting all symbols or building a shared library,
369 // and the symbol is defined in a regular object and is
370 // externally visible, we need to add it.
371 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
372 && !this->is_from_dynobj()
373 && this->is_externally_visible())
379 // Return true if the final value of this symbol is known at link
383 Symbol::final_value_is_known() const
385 // If we are not generating an executable, then no final values are
386 // known, since they will change at runtime.
387 if (parameters
->options().shared() || parameters
->options().relocatable())
390 // If the symbol is not from an object file, and is not undefined,
391 // then it is defined, and known.
392 if (this->source_
!= FROM_OBJECT
)
394 if (this->source_
!= IS_UNDEFINED
)
399 // If the symbol is from a dynamic object, then the final value
401 if (this->object()->is_dynamic())
404 // If the symbol is not undefined (it is defined or common),
405 // then the final value is known.
406 if (!this->is_undefined())
410 // If the symbol is undefined, then whether the final value is known
411 // depends on whether we are doing a static link. If we are doing a
412 // dynamic link, then the final value could be filled in at runtime.
413 // This could reasonably be the case for a weak undefined symbol.
414 return parameters
->doing_static_link();
417 // Return the output section where this symbol is defined.
420 Symbol::output_section() const
422 switch (this->source_
)
426 unsigned int shndx
= this->u_
.from_object
.shndx
;
427 if (shndx
!= elfcpp::SHN_UNDEF
&& this->is_ordinary_shndx_
)
429 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
430 gold_assert(this->u_
.from_object
.object
->pluginobj() == NULL
);
431 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
432 return relobj
->output_section(shndx
);
438 return this->u_
.in_output_data
.output_data
->output_section();
440 case IN_OUTPUT_SEGMENT
:
450 // Set the symbol's output section. This is used for symbols defined
451 // in scripts. This should only be called after the symbol table has
455 Symbol::set_output_section(Output_section
* os
)
457 switch (this->source_
)
461 gold_assert(this->output_section() == os
);
464 this->source_
= IN_OUTPUT_DATA
;
465 this->u_
.in_output_data
.output_data
= os
;
466 this->u_
.in_output_data
.offset_is_from_end
= false;
468 case IN_OUTPUT_SEGMENT
:
475 // Class Symbol_table.
477 Symbol_table::Symbol_table(unsigned int count
,
478 const Version_script_info
& version_script
)
479 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
480 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
481 version_script_(version_script
), gc_(NULL
)
483 namepool_
.reserve(count
);
486 Symbol_table::~Symbol_table()
490 // The hash function. The key values are Stringpool keys.
493 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
495 return key
.first
^ key
.second
;
498 // The symbol table key equality function. This is called with
502 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
503 const Symbol_table_key
& k2
) const
505 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
508 // For symbols that have been listed with -u option, add them to the
509 // work list to avoid gc'ing them.
512 Symbol_table::gc_mark_undef_symbols()
514 for (options::String_set::const_iterator p
=
515 parameters
->options().undefined_begin();
516 p
!= parameters
->options().undefined_end();
519 const char* name
= p
->c_str();
520 Symbol
* sym
= this->lookup(name
);
521 gold_assert (sym
!= NULL
);
522 if (sym
->source() == Symbol::FROM_OBJECT
523 && !sym
->object()->is_dynamic())
525 Relobj
* obj
= static_cast<Relobj
*>(sym
->object());
527 unsigned int shndx
= sym
->shndx(&is_ordinary
);
530 gold_assert(this->gc_
!= NULL
);
531 this->gc_
->worklist().push(Section_id(obj
, shndx
));
538 Symbol_table::gc_mark_symbol_for_shlib(Symbol
* sym
)
540 if (!sym
->is_from_dynobj()
541 && sym
->is_externally_visible())
543 //Add the object and section to the work list.
544 Relobj
* obj
= static_cast<Relobj
*>(sym
->object());
546 unsigned int shndx
= sym
->shndx(&is_ordinary
);
547 if (is_ordinary
&& shndx
!= elfcpp::SHN_UNDEF
)
549 gold_assert(this->gc_
!= NULL
);
550 this->gc_
->worklist().push(Section_id(obj
, shndx
));
555 // When doing garbage collection, keep symbols that have been seen in
558 Symbol_table::gc_mark_dyn_syms(Symbol
* sym
)
560 if (sym
->in_dyn() && sym
->source() == Symbol::FROM_OBJECT
561 && !sym
->object()->is_dynamic())
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 // Make TO a symbol which forwards to FROM.
577 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
579 gold_assert(from
!= to
);
580 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
581 this->forwarders_
[from
] = to
;
582 from
->set_forwarder();
585 // Resolve the forwards from FROM, returning the real symbol.
588 Symbol_table::resolve_forwards(const Symbol
* from
) const
590 gold_assert(from
->is_forwarder());
591 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
592 this->forwarders_
.find(from
);
593 gold_assert(p
!= this->forwarders_
.end());
597 // Look up a symbol by name.
600 Symbol_table::lookup(const char* name
, const char* version
) const
602 Stringpool::Key name_key
;
603 name
= this->namepool_
.find(name
, &name_key
);
607 Stringpool::Key version_key
= 0;
610 version
= this->namepool_
.find(version
, &version_key
);
615 Symbol_table_key
key(name_key
, version_key
);
616 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
617 if (p
== this->table_
.end())
622 // Resolve a Symbol with another Symbol. This is only used in the
623 // unusual case where there are references to both an unversioned
624 // symbol and a symbol with a version, and we then discover that that
625 // version is the default version. Because this is unusual, we do
626 // this the slow way, by converting back to an ELF symbol.
628 template<int size
, bool big_endian
>
630 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
)
632 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
633 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
634 // We don't bother to set the st_name or the st_shndx field.
635 esym
.put_st_value(from
->value());
636 esym
.put_st_size(from
->symsize());
637 esym
.put_st_info(from
->binding(), from
->type());
638 esym
.put_st_other(from
->visibility(), from
->nonvis());
640 unsigned int shndx
= from
->shndx(&is_ordinary
);
641 this->resolve(to
, esym
.sym(), shndx
, is_ordinary
, shndx
, from
->object(),
647 if (parameters
->options().gc_sections())
648 this->gc_mark_dyn_syms(to
);
651 // Record that a symbol is forced to be local by a version script.
654 Symbol_table::force_local(Symbol
* sym
)
656 if (!sym
->is_defined() && !sym
->is_common())
658 if (sym
->is_forced_local())
660 // We already got this one.
663 sym
->set_is_forced_local();
664 this->forced_locals_
.push_back(sym
);
667 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
668 // is only called for undefined symbols, when at least one --wrap
672 Symbol_table::wrap_symbol(Object
* object
, const char* name
,
673 Stringpool::Key
* name_key
)
675 // For some targets, we need to ignore a specific character when
676 // wrapping, and add it back later.
678 if (name
[0] == object
->target()->wrap_char())
684 if (parameters
->options().is_wrap(name
))
686 // Turn NAME into __wrap_NAME.
693 // This will give us both the old and new name in NAMEPOOL_, but
694 // that is OK. Only the versions we need will wind up in the
695 // real string table in the output file.
696 return this->namepool_
.add(s
.c_str(), true, name_key
);
699 const char* const real_prefix
= "__real_";
700 const size_t real_prefix_length
= strlen(real_prefix
);
701 if (strncmp(name
, real_prefix
, real_prefix_length
) == 0
702 && parameters
->options().is_wrap(name
+ real_prefix_length
))
704 // Turn __real_NAME into NAME.
708 s
+= name
+ real_prefix_length
;
709 return this->namepool_
.add(s
.c_str(), true, name_key
);
715 // Add one symbol from OBJECT to the symbol table. NAME is symbol
716 // name and VERSION is the version; both are canonicalized. DEF is
717 // whether this is the default version. ST_SHNDX is the symbol's
718 // section index; IS_ORDINARY is whether this is a normal section
719 // rather than a special code.
721 // If DEF is true, then this is the definition of a default version of
722 // a symbol. That means that any lookup of NAME/NULL and any lookup
723 // of NAME/VERSION should always return the same symbol. This is
724 // obvious for references, but in particular we want to do this for
725 // definitions: overriding NAME/NULL should also override
726 // NAME/VERSION. If we don't do that, it would be very hard to
727 // override functions in a shared library which uses versioning.
729 // We implement this by simply making both entries in the hash table
730 // point to the same Symbol structure. That is easy enough if this is
731 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
732 // that we have seen both already, in which case they will both have
733 // independent entries in the symbol table. We can't simply change
734 // the symbol table entry, because we have pointers to the entries
735 // attached to the object files. So we mark the entry attached to the
736 // object file as a forwarder, and record it in the forwarders_ map.
737 // Note that entries in the hash table will never be marked as
740 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
741 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
742 // for a special section code. ST_SHNDX may be modified if the symbol
743 // is defined in a section being discarded.
745 template<int size
, bool big_endian
>
747 Symbol_table::add_from_object(Object
* object
,
749 Stringpool::Key name_key
,
751 Stringpool::Key version_key
,
753 const elfcpp::Sym
<size
, big_endian
>& sym
,
754 unsigned int st_shndx
,
756 unsigned int orig_st_shndx
)
758 // Print a message if this symbol is being traced.
759 if (parameters
->options().is_trace_symbol(name
))
761 if (orig_st_shndx
== elfcpp::SHN_UNDEF
)
762 gold_info(_("%s: reference to %s"), object
->name().c_str(), name
);
764 gold_info(_("%s: definition of %s"), object
->name().c_str(), name
);
767 // For an undefined symbol, we may need to adjust the name using
769 if (orig_st_shndx
== elfcpp::SHN_UNDEF
770 && parameters
->options().any_wrap())
772 const char* wrap_name
= this->wrap_symbol(object
, name
, &name_key
);
773 if (wrap_name
!= name
)
775 // If we see a reference to malloc with version GLIBC_2.0,
776 // and we turn it into a reference to __wrap_malloc, then we
777 // discard the version number. Otherwise the user would be
778 // required to specify the correct version for
786 Symbol
* const snull
= NULL
;
787 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
788 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
791 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
792 std::make_pair(this->table_
.end(), false);
795 const Stringpool::Key vnull_key
= 0;
796 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
801 // ins.first: an iterator, which is a pointer to a pair.
802 // ins.first->first: the key (a pair of name and version).
803 // ins.first->second: the value (Symbol*).
804 // ins.second: true if new entry was inserted, false if not.
806 Sized_symbol
<size
>* ret
;
811 // We already have an entry for NAME/VERSION.
812 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
813 gold_assert(ret
!= NULL
);
815 was_undefined
= ret
->is_undefined();
816 was_common
= ret
->is_common();
818 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
820 if (parameters
->options().gc_sections())
821 this->gc_mark_dyn_syms(ret
);
827 // This is the first time we have seen NAME/NULL. Make
828 // NAME/NULL point to NAME/VERSION.
829 insdef
.first
->second
= ret
;
831 else if (insdef
.first
->second
!= ret
)
833 // This is the unfortunate case where we already have
834 // entries for both NAME/VERSION and NAME/NULL. We now
835 // see a symbol NAME/VERSION where VERSION is the
836 // default version. We have already resolved this new
837 // symbol with the existing NAME/VERSION symbol.
839 // It's possible that NAME/NULL and NAME/VERSION are
840 // both defined in regular objects. This can only
841 // happen if one object file defines foo and another
842 // defines foo@@ver. This is somewhat obscure, but we
843 // call it a multiple definition error.
845 // It's possible that NAME/NULL actually has a version,
846 // in which case it won't be the same as VERSION. This
847 // happens with ver_test_7.so in the testsuite for the
848 // symbol t2_2. We see t2_2@@VER2, so we define both
849 // t2_2/VER2 and t2_2/NULL. We then see an unadorned
850 // t2_2 in an object file and give it version VER1 from
851 // the version script. This looks like a default
852 // definition for VER1, so it looks like we should merge
853 // t2_2/NULL with t2_2/VER1. That doesn't make sense,
854 // but it's not obvious that this is an error, either.
857 // If one of the symbols has non-default visibility, and
858 // the other is defined in a shared object, then they
859 // are different symbols.
861 // Otherwise, we just resolve the symbols as though they
864 if (insdef
.first
->second
->version() != NULL
)
866 gold_assert(insdef
.first
->second
->version() != version
);
869 else if (ret
->visibility() != elfcpp::STV_DEFAULT
870 && insdef
.first
->second
->is_from_dynobj())
872 else if (insdef
.first
->second
->visibility() != elfcpp::STV_DEFAULT
873 && ret
->is_from_dynobj())
877 const Sized_symbol
<size
>* sym2
;
878 sym2
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
879 Symbol_table::resolve
<size
, big_endian
>(ret
, sym2
);
880 this->make_forwarder(insdef
.first
->second
, ret
);
881 insdef
.first
->second
= ret
;
890 // This is the first time we have seen NAME/VERSION.
891 gold_assert(ins
.first
->second
== NULL
);
893 if (def
&& !insdef
.second
)
895 // We already have an entry for NAME/NULL. If we override
896 // it, then change it to NAME/VERSION.
897 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
899 was_undefined
= ret
->is_undefined();
900 was_common
= ret
->is_common();
902 this->resolve(ret
, sym
, st_shndx
, is_ordinary
, orig_st_shndx
, object
,
904 if (parameters
->options().gc_sections())
905 this->gc_mark_dyn_syms(ret
);
906 ins
.first
->second
= ret
;
910 was_undefined
= false;
913 Sized_target
<size
, big_endian
>* target
=
914 object
->sized_target
<size
, big_endian
>();
915 if (!target
->has_make_symbol())
916 ret
= new Sized_symbol
<size
>();
919 ret
= target
->make_symbol();
922 // This means that we don't want a symbol table
925 this->table_
.erase(ins
.first
);
928 this->table_
.erase(insdef
.first
);
929 // Inserting insdef invalidated ins.
930 this->table_
.erase(std::make_pair(name_key
,
937 ret
->init_object(name
, version
, object
, sym
, st_shndx
, is_ordinary
);
939 ins
.first
->second
= ret
;
942 // This is the first time we have seen NAME/NULL. Point
943 // it at the new entry for NAME/VERSION.
944 gold_assert(insdef
.second
);
945 insdef
.first
->second
= ret
;
950 // Record every time we see a new undefined symbol, to speed up
952 if (!was_undefined
&& ret
->is_undefined())
953 ++this->saw_undefined_
;
955 // Keep track of common symbols, to speed up common symbol
957 if (!was_common
&& ret
->is_common())
959 if (ret
->type() != elfcpp::STT_TLS
)
960 this->commons_
.push_back(ret
);
962 this->tls_commons_
.push_back(ret
);
966 ret
->set_is_default();
970 // Add all the symbols in a relocatable object to the hash table.
972 template<int size
, bool big_endian
>
974 Symbol_table::add_from_relobj(
975 Sized_relobj
<size
, big_endian
>* relobj
,
976 const unsigned char* syms
,
978 size_t symndx_offset
,
979 const char* sym_names
,
980 size_t sym_name_size
,
981 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
986 gold_assert(size
== relobj
->target()->get_size());
987 gold_assert(size
== parameters
->target().get_size());
989 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
991 const bool just_symbols
= relobj
->just_symbols();
993 const unsigned char* p
= syms
;
994 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
996 (*sympointers
)[i
] = NULL
;
998 elfcpp::Sym
<size
, big_endian
> sym(p
);
1000 unsigned int st_name
= sym
.get_st_name();
1001 if (st_name
>= sym_name_size
)
1003 relobj
->error(_("bad global symbol name offset %u at %zu"),
1008 const char* name
= sym_names
+ st_name
;
1011 unsigned int st_shndx
= relobj
->adjust_sym_shndx(i
+ symndx_offset
,
1014 unsigned int orig_st_shndx
= st_shndx
;
1016 orig_st_shndx
= elfcpp::SHN_UNDEF
;
1018 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1021 // A symbol defined in a section which we are not including must
1022 // be treated as an undefined symbol.
1023 if (st_shndx
!= elfcpp::SHN_UNDEF
1025 && !relobj
->is_section_included(st_shndx
))
1026 st_shndx
= elfcpp::SHN_UNDEF
;
1028 // In an object file, an '@' in the name separates the symbol
1029 // name from the version name. If there are two '@' characters,
1030 // this is the default version.
1031 const char* ver
= strchr(name
, '@');
1032 Stringpool::Key ver_key
= 0;
1034 // DEF: is the version default? LOCAL: is the symbol forced local?
1040 // The symbol name is of the form foo@VERSION or foo@@VERSION
1041 namelen
= ver
- name
;
1048 ver
= this->namepool_
.add(ver
, true, &ver_key
);
1050 // We don't want to assign a version to an undefined symbol,
1051 // even if it is listed in the version script. FIXME: What
1052 // about a common symbol?
1055 namelen
= strlen(name
);
1056 if (!this->version_script_
.empty()
1057 && st_shndx
!= elfcpp::SHN_UNDEF
)
1059 // The symbol name did not have a version, but the
1060 // version script may assign a version anyway.
1061 std::string version
;
1062 if (this->version_script_
.get_symbol_version(name
, &version
))
1064 // The version can be empty if the version script is
1065 // only used to force some symbols to be local.
1066 if (!version
.empty())
1068 ver
= this->namepool_
.add_with_length(version
.c_str(),
1075 else if (this->version_script_
.symbol_is_local(name
))
1080 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1081 unsigned char symbuf
[sym_size
];
1082 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1085 memcpy(symbuf
, p
, sym_size
);
1086 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1087 if (orig_st_shndx
!= elfcpp::SHN_UNDEF
&& is_ordinary
)
1089 // Symbol values in object files are section relative.
1090 // This is normally what we want, but since here we are
1091 // converting the symbol to absolute we need to add the
1092 // section address. The section address in an object
1093 // file is normally zero, but people can use a linker
1094 // script to change it.
1095 sw
.put_st_value(sym
.get_st_value()
1096 + relobj
->section_address(orig_st_shndx
));
1098 st_shndx
= elfcpp::SHN_ABS
;
1099 is_ordinary
= false;
1103 Stringpool::Key name_key
;
1104 name
= this->namepool_
.add_with_length(name
, namelen
, true,
1107 Sized_symbol
<size
>* res
;
1108 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
1109 def
, *psym
, st_shndx
, is_ordinary
,
1112 // If building a shared library using garbage collection, do not
1113 // treat externally visible symbols as garbage.
1114 if (parameters
->options().gc_sections()
1115 && parameters
->options().shared())
1116 this->gc_mark_symbol_for_shlib(res
);
1119 this->force_local(res
);
1121 (*sympointers
)[i
] = res
;
1125 // Add a symbol from a plugin-claimed file.
1127 template<int size
, bool big_endian
>
1129 Symbol_table::add_from_pluginobj(
1130 Sized_pluginobj
<size
, big_endian
>* obj
,
1133 elfcpp::Sym
<size
, big_endian
>* sym
)
1135 unsigned int st_shndx
= sym
->get_st_shndx();
1137 Stringpool::Key ver_key
= 0;
1143 ver
= this->namepool_
.add(ver
, true, &ver_key
);
1145 // We don't want to assign a version to an undefined symbol,
1146 // even if it is listed in the version script. FIXME: What
1147 // about a common symbol?
1150 if (!this->version_script_
.empty()
1151 && st_shndx
!= elfcpp::SHN_UNDEF
)
1153 // The symbol name did not have a version, but the
1154 // version script may assign a version anyway.
1155 std::string version
;
1156 if (this->version_script_
.get_symbol_version(name
, &version
))
1158 // The version can be empty if the version script is
1159 // only used to force some symbols to be local.
1160 if (!version
.empty())
1162 ver
= this->namepool_
.add_with_length(version
.c_str(),
1169 else if (this->version_script_
.symbol_is_local(name
))
1174 Stringpool::Key name_key
;
1175 name
= this->namepool_
.add(name
, true, &name_key
);
1177 Sized_symbol
<size
>* res
;
1178 res
= this->add_from_object(obj
, name
, name_key
, ver
, ver_key
,
1179 def
, *sym
, st_shndx
, true, st_shndx
);
1182 this->force_local(res
);
1187 // Add all the symbols in a dynamic object to the hash table.
1189 template<int size
, bool big_endian
>
1191 Symbol_table::add_from_dynobj(
1192 Sized_dynobj
<size
, big_endian
>* dynobj
,
1193 const unsigned char* syms
,
1195 const char* sym_names
,
1196 size_t sym_name_size
,
1197 const unsigned char* versym
,
1199 const std::vector
<const char*>* version_map
,
1200 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
,
1205 gold_assert(size
== dynobj
->target()->get_size());
1206 gold_assert(size
== parameters
->target().get_size());
1208 if (dynobj
->just_symbols())
1210 gold_error(_("--just-symbols does not make sense with a shared object"));
1214 if (versym
!= NULL
&& versym_size
/ 2 < count
)
1216 dynobj
->error(_("too few symbol versions"));
1220 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1222 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1223 // weak aliases. This is necessary because if the dynamic object
1224 // provides the same variable under two names, one of which is a
1225 // weak definition, and the regular object refers to the weak
1226 // definition, we have to put both the weak definition and the
1227 // strong definition into the dynamic symbol table. Given a weak
1228 // definition, the only way that we can find the corresponding
1229 // strong definition, if any, is to search the symbol table.
1230 std::vector
<Sized_symbol
<size
>*> object_symbols
;
1232 const unsigned char* p
= syms
;
1233 const unsigned char* vs
= versym
;
1234 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
1236 elfcpp::Sym
<size
, big_endian
> sym(p
);
1238 if (sympointers
!= NULL
)
1239 (*sympointers
)[i
] = NULL
;
1241 // Ignore symbols with local binding or that have
1242 // internal or hidden visibility.
1243 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
1244 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
1245 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
1248 // A protected symbol in a shared library must be treated as a
1249 // normal symbol when viewed from outside the shared library.
1250 // Implement this by overriding the visibility here.
1251 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
1252 unsigned char symbuf
[sym_size
];
1253 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
1254 if (sym
.get_st_visibility() == elfcpp::STV_PROTECTED
)
1256 memcpy(symbuf
, p
, sym_size
);
1257 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
1258 sw
.put_st_other(elfcpp::STV_DEFAULT
, sym
.get_st_nonvis());
1262 unsigned int st_name
= psym
->get_st_name();
1263 if (st_name
>= sym_name_size
)
1265 dynobj
->error(_("bad symbol name offset %u at %zu"),
1270 const char* name
= sym_names
+ st_name
;
1273 unsigned int st_shndx
= dynobj
->adjust_sym_shndx(i
, psym
->get_st_shndx(),
1276 if (st_shndx
!= elfcpp::SHN_UNDEF
)
1279 Sized_symbol
<size
>* res
;
1283 Stringpool::Key name_key
;
1284 name
= this->namepool_
.add(name
, true, &name_key
);
1285 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1286 false, *psym
, st_shndx
, is_ordinary
,
1291 // Read the version information.
1293 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
1295 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
1296 v
&= elfcpp::VERSYM_VERSION
;
1298 // The Sun documentation says that V can be VER_NDX_LOCAL,
1299 // or VER_NDX_GLOBAL, or a version index. The meaning of
1300 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1301 // The old GNU linker will happily generate VER_NDX_LOCAL
1302 // for an undefined symbol. I don't know what the Sun
1303 // linker will generate.
1305 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1306 && st_shndx
!= elfcpp::SHN_UNDEF
)
1308 // This symbol should not be visible outside the object.
1312 // At this point we are definitely going to add this symbol.
1313 Stringpool::Key name_key
;
1314 name
= this->namepool_
.add(name
, true, &name_key
);
1316 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
1317 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
1319 // This symbol does not have a version.
1320 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1321 false, *psym
, st_shndx
, is_ordinary
,
1326 if (v
>= version_map
->size())
1328 dynobj
->error(_("versym for symbol %zu out of range: %u"),
1333 const char* version
= (*version_map
)[v
];
1334 if (version
== NULL
)
1336 dynobj
->error(_("versym for symbol %zu has no name: %u"),
1341 Stringpool::Key version_key
;
1342 version
= this->namepool_
.add(version
, true, &version_key
);
1344 // If this is an absolute symbol, and the version name
1345 // and symbol name are the same, then this is the
1346 // version definition symbol. These symbols exist to
1347 // support using -u to pull in particular versions. We
1348 // do not want to record a version for them.
1349 if (st_shndx
== elfcpp::SHN_ABS
1351 && name_key
== version_key
)
1352 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
1353 false, *psym
, st_shndx
, is_ordinary
,
1357 const bool def
= (!hidden
1358 && st_shndx
!= elfcpp::SHN_UNDEF
);
1359 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
1360 version_key
, def
, *psym
, st_shndx
,
1361 is_ordinary
, st_shndx
);
1366 // Note that it is possible that RES was overridden by an
1367 // earlier object, in which case it can't be aliased here.
1368 if (st_shndx
!= elfcpp::SHN_UNDEF
1370 && psym
->get_st_type() == elfcpp::STT_OBJECT
1371 && res
->source() == Symbol::FROM_OBJECT
1372 && res
->object() == dynobj
)
1373 object_symbols
.push_back(res
);
1375 if (sympointers
!= NULL
)
1376 (*sympointers
)[i
] = res
;
1379 this->record_weak_aliases(&object_symbols
);
1382 // This is used to sort weak aliases. We sort them first by section
1383 // index, then by offset, then by weak ahead of strong.
1386 class Weak_alias_sorter
1389 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
1394 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
1395 const Sized_symbol
<size
>* s2
) const
1398 unsigned int s1_shndx
= s1
->shndx(&is_ordinary
);
1399 gold_assert(is_ordinary
);
1400 unsigned int s2_shndx
= s2
->shndx(&is_ordinary
);
1401 gold_assert(is_ordinary
);
1402 if (s1_shndx
!= s2_shndx
)
1403 return s1_shndx
< s2_shndx
;
1405 if (s1
->value() != s2
->value())
1406 return s1
->value() < s2
->value();
1407 if (s1
->binding() != s2
->binding())
1409 if (s1
->binding() == elfcpp::STB_WEAK
)
1411 if (s2
->binding() == elfcpp::STB_WEAK
)
1414 return std::string(s1
->name()) < std::string(s2
->name());
1417 // SYMBOLS is a list of object symbols from a dynamic object. Look
1418 // for any weak aliases, and record them so that if we add the weak
1419 // alias to the dynamic symbol table, we also add the corresponding
1424 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
1426 // Sort the vector by section index, then by offset, then by weak
1428 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
1430 // Walk through the vector. For each weak definition, record
1432 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
1434 p
!= symbols
->end();
1437 if ((*p
)->binding() != elfcpp::STB_WEAK
)
1440 // Build a circular list of weak aliases. Each symbol points to
1441 // the next one in the circular list.
1443 Sized_symbol
<size
>* from_sym
= *p
;
1444 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
1445 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
1448 if ((*q
)->shndx(&dummy
) != from_sym
->shndx(&dummy
)
1449 || (*q
)->value() != from_sym
->value())
1452 this->weak_aliases_
[from_sym
] = *q
;
1453 from_sym
->set_has_alias();
1459 this->weak_aliases_
[from_sym
] = *p
;
1460 from_sym
->set_has_alias();
1467 // Create and return a specially defined symbol. If ONLY_IF_REF is
1468 // true, then only create the symbol if there is a reference to it.
1469 // If this does not return NULL, it sets *POLDSYM to the existing
1470 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1472 template<int size
, bool big_endian
>
1474 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1476 Sized_symbol
<size
>** poldsym
)
1479 Sized_symbol
<size
>* sym
;
1480 bool add_to_table
= false;
1481 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1483 // If the caller didn't give us a version, see if we get one from
1484 // the version script.
1486 if (*pversion
== NULL
)
1488 if (this->version_script_
.get_symbol_version(*pname
, &v
))
1491 *pversion
= v
.c_str();
1497 oldsym
= this->lookup(*pname
, *pversion
);
1498 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1501 *pname
= oldsym
->name();
1502 *pversion
= oldsym
->version();
1506 // Canonicalize NAME and VERSION.
1507 Stringpool::Key name_key
;
1508 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1510 Stringpool::Key version_key
= 0;
1511 if (*pversion
!= NULL
)
1512 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1514 Symbol
* const snull
= NULL
;
1515 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1516 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1522 // We already have a symbol table entry for NAME/VERSION.
1523 oldsym
= ins
.first
->second
;
1524 gold_assert(oldsym
!= NULL
);
1528 // We haven't seen this symbol before.
1529 gold_assert(ins
.first
->second
== NULL
);
1530 add_to_table
= true;
1531 add_loc
= ins
.first
;
1536 const Target
& target
= parameters
->target();
1537 if (!target
.has_make_symbol())
1538 sym
= new Sized_symbol
<size
>();
1541 gold_assert(target
.get_size() == size
);
1542 gold_assert(target
.is_big_endian() ? big_endian
: !big_endian
);
1543 typedef Sized_target
<size
, big_endian
> My_target
;
1544 const My_target
* sized_target
=
1545 static_cast<const My_target
*>(&target
);
1546 sym
= sized_target
->make_symbol();
1552 add_loc
->second
= sym
;
1554 gold_assert(oldsym
!= NULL
);
1556 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1561 // Define a symbol based on an Output_data.
1564 Symbol_table::define_in_output_data(const char* name
,
1565 const char* version
,
1570 elfcpp::STB binding
,
1571 elfcpp::STV visibility
,
1572 unsigned char nonvis
,
1573 bool offset_is_from_end
,
1576 if (parameters
->target().get_size() == 32)
1578 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1579 return this->do_define_in_output_data
<32>(name
, version
, od
,
1580 value
, symsize
, type
, binding
,
1588 else if (parameters
->target().get_size() == 64)
1590 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1591 return this->do_define_in_output_data
<64>(name
, version
, od
,
1592 value
, symsize
, type
, binding
,
1604 // Define a symbol in an Output_data, sized version.
1608 Symbol_table::do_define_in_output_data(
1610 const char* version
,
1612 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1613 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1615 elfcpp::STB binding
,
1616 elfcpp::STV visibility
,
1617 unsigned char nonvis
,
1618 bool offset_is_from_end
,
1621 Sized_symbol
<size
>* sym
;
1622 Sized_symbol
<size
>* oldsym
;
1624 if (parameters
->target().is_big_endian())
1626 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1627 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1628 only_if_ref
, &oldsym
);
1635 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1636 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1637 only_if_ref
, &oldsym
);
1646 sym
->init_output_data(name
, version
, od
, value
, symsize
, type
, binding
,
1647 visibility
, nonvis
, offset_is_from_end
);
1651 if (binding
== elfcpp::STB_LOCAL
1652 || this->version_script_
.symbol_is_local(name
))
1653 this->force_local(sym
);
1654 else if (version
!= NULL
)
1655 sym
->set_is_default();
1659 if (Symbol_table::should_override_with_special(oldsym
))
1660 this->override_with_special(oldsym
, sym
);
1665 // Define a symbol based on an Output_segment.
1668 Symbol_table::define_in_output_segment(const char* name
,
1669 const char* version
, Output_segment
* os
,
1673 elfcpp::STB binding
,
1674 elfcpp::STV visibility
,
1675 unsigned char nonvis
,
1676 Symbol::Segment_offset_base offset_base
,
1679 if (parameters
->target().get_size() == 32)
1681 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1682 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1683 value
, symsize
, type
,
1684 binding
, visibility
, nonvis
,
1685 offset_base
, only_if_ref
);
1690 else if (parameters
->target().get_size() == 64)
1692 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1693 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1694 value
, symsize
, type
,
1695 binding
, visibility
, nonvis
,
1696 offset_base
, only_if_ref
);
1705 // Define a symbol in an Output_segment, sized version.
1709 Symbol_table::do_define_in_output_segment(
1711 const char* version
,
1713 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1714 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1716 elfcpp::STB binding
,
1717 elfcpp::STV visibility
,
1718 unsigned char nonvis
,
1719 Symbol::Segment_offset_base offset_base
,
1722 Sized_symbol
<size
>* sym
;
1723 Sized_symbol
<size
>* oldsym
;
1725 if (parameters
->target().is_big_endian())
1727 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1728 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1729 only_if_ref
, &oldsym
);
1736 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1737 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1738 only_if_ref
, &oldsym
);
1747 sym
->init_output_segment(name
, version
, os
, value
, symsize
, type
, binding
,
1748 visibility
, nonvis
, offset_base
);
1752 if (binding
== elfcpp::STB_LOCAL
1753 || this->version_script_
.symbol_is_local(name
))
1754 this->force_local(sym
);
1755 else if (version
!= NULL
)
1756 sym
->set_is_default();
1760 if (Symbol_table::should_override_with_special(oldsym
))
1761 this->override_with_special(oldsym
, sym
);
1766 // Define a special symbol with a constant value. It is a multiple
1767 // definition error if this symbol is already defined.
1770 Symbol_table::define_as_constant(const char* name
,
1771 const char* version
,
1775 elfcpp::STB binding
,
1776 elfcpp::STV visibility
,
1777 unsigned char nonvis
,
1779 bool force_override
)
1781 if (parameters
->target().get_size() == 32)
1783 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1784 return this->do_define_as_constant
<32>(name
, version
, value
,
1785 symsize
, type
, binding
,
1786 visibility
, nonvis
, only_if_ref
,
1792 else if (parameters
->target().get_size() == 64)
1794 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1795 return this->do_define_as_constant
<64>(name
, version
, value
,
1796 symsize
, type
, binding
,
1797 visibility
, nonvis
, only_if_ref
,
1807 // Define a symbol as a constant, sized version.
1811 Symbol_table::do_define_as_constant(
1813 const char* version
,
1814 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1815 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1817 elfcpp::STB binding
,
1818 elfcpp::STV visibility
,
1819 unsigned char nonvis
,
1821 bool force_override
)
1823 Sized_symbol
<size
>* sym
;
1824 Sized_symbol
<size
>* oldsym
;
1826 if (parameters
->target().is_big_endian())
1828 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1829 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1830 only_if_ref
, &oldsym
);
1837 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1838 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1839 only_if_ref
, &oldsym
);
1848 sym
->init_constant(name
, version
, value
, symsize
, type
, binding
, visibility
,
1853 // Version symbols are absolute symbols with name == version.
1854 // We don't want to force them to be local.
1855 if ((version
== NULL
1858 && (binding
== elfcpp::STB_LOCAL
1859 || this->version_script_
.symbol_is_local(name
)))
1860 this->force_local(sym
);
1861 else if (version
!= NULL
1862 && (name
!= version
|| value
!= 0))
1863 sym
->set_is_default();
1867 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
1868 this->override_with_special(oldsym
, sym
);
1873 // Define a set of symbols in output sections.
1876 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1877 const Define_symbol_in_section
* p
,
1880 for (int i
= 0; i
< count
; ++i
, ++p
)
1882 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1884 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
1885 p
->size
, p
->type
, p
->binding
,
1886 p
->visibility
, p
->nonvis
,
1887 p
->offset_is_from_end
,
1888 only_if_ref
|| p
->only_if_ref
);
1890 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1891 p
->binding
, p
->visibility
, p
->nonvis
,
1892 only_if_ref
|| p
->only_if_ref
,
1897 // Define a set of symbols in output segments.
1900 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1901 const Define_symbol_in_segment
* p
,
1904 for (int i
= 0; i
< count
; ++i
, ++p
)
1906 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1907 p
->segment_flags_set
,
1908 p
->segment_flags_clear
);
1910 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
1911 p
->size
, p
->type
, p
->binding
,
1912 p
->visibility
, p
->nonvis
,
1914 only_if_ref
|| p
->only_if_ref
);
1916 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1917 p
->binding
, p
->visibility
, p
->nonvis
,
1918 only_if_ref
|| p
->only_if_ref
,
1923 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1924 // symbol should be defined--typically a .dyn.bss section. VALUE is
1925 // the offset within POSD.
1929 Symbol_table::define_with_copy_reloc(
1930 Sized_symbol
<size
>* csym
,
1932 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
1934 gold_assert(csym
->is_from_dynobj());
1935 gold_assert(!csym
->is_copied_from_dynobj());
1936 Object
* object
= csym
->object();
1937 gold_assert(object
->is_dynamic());
1938 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1940 // Our copied variable has to override any variable in a shared
1942 elfcpp::STB binding
= csym
->binding();
1943 if (binding
== elfcpp::STB_WEAK
)
1944 binding
= elfcpp::STB_GLOBAL
;
1946 this->define_in_output_data(csym
->name(), csym
->version(),
1947 posd
, value
, csym
->symsize(),
1948 csym
->type(), binding
,
1949 csym
->visibility(), csym
->nonvis(),
1952 csym
->set_is_copied_from_dynobj();
1953 csym
->set_needs_dynsym_entry();
1955 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1957 // We have now defined all aliases, but we have not entered them all
1958 // in the copied_symbol_dynobjs_ map.
1959 if (csym
->has_alias())
1964 sym
= this->weak_aliases_
[sym
];
1967 gold_assert(sym
->output_data() == posd
);
1969 sym
->set_is_copied_from_dynobj();
1970 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1975 // SYM is defined using a COPY reloc. Return the dynamic object where
1976 // the original definition was found.
1979 Symbol_table::get_copy_source(const Symbol
* sym
) const
1981 gold_assert(sym
->is_copied_from_dynobj());
1982 Copied_symbol_dynobjs::const_iterator p
=
1983 this->copied_symbol_dynobjs_
.find(sym
);
1984 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1988 // Add any undefined symbols named on the command line.
1991 Symbol_table::add_undefined_symbols_from_command_line()
1993 if (parameters
->options().any_undefined())
1995 if (parameters
->target().get_size() == 32)
1997 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1998 this->do_add_undefined_symbols_from_command_line
<32>();
2003 else if (parameters
->target().get_size() == 64)
2005 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2006 this->do_add_undefined_symbols_from_command_line
<64>();
2018 Symbol_table::do_add_undefined_symbols_from_command_line()
2020 for (options::String_set::const_iterator p
=
2021 parameters
->options().undefined_begin();
2022 p
!= parameters
->options().undefined_end();
2025 const char* name
= p
->c_str();
2027 if (this->lookup(name
) != NULL
)
2030 const char* version
= NULL
;
2032 Sized_symbol
<size
>* sym
;
2033 Sized_symbol
<size
>* oldsym
;
2034 if (parameters
->target().is_big_endian())
2036 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2037 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
2045 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2046 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
2053 gold_assert(oldsym
== NULL
);
2055 sym
->init_undefined(name
, version
, elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
2056 elfcpp::STV_DEFAULT
, 0);
2057 ++this->saw_undefined_
;
2061 // Set the dynamic symbol indexes. INDEX is the index of the first
2062 // global dynamic symbol. Pointers to the symbols are stored into the
2063 // vector SYMS. The names are added to DYNPOOL. This returns an
2064 // updated dynamic symbol index.
2067 Symbol_table::set_dynsym_indexes(unsigned int index
,
2068 std::vector
<Symbol
*>* syms
,
2069 Stringpool
* dynpool
,
2072 for (Symbol_table_type::iterator p
= this->table_
.begin();
2073 p
!= this->table_
.end();
2076 Symbol
* sym
= p
->second
;
2078 // Note that SYM may already have a dynamic symbol index, since
2079 // some symbols appear more than once in the symbol table, with
2080 // and without a version.
2082 if (!sym
->should_add_dynsym_entry())
2083 sym
->set_dynsym_index(-1U);
2084 else if (!sym
->has_dynsym_index())
2086 sym
->set_dynsym_index(index
);
2088 syms
->push_back(sym
);
2089 dynpool
->add(sym
->name(), false, NULL
);
2091 // Record any version information.
2092 if (sym
->version() != NULL
)
2093 versions
->record_version(this, dynpool
, sym
);
2097 // Finish up the versions. In some cases this may add new dynamic
2099 index
= versions
->finalize(this, index
, syms
);
2104 // Set the final values for all the symbols. The index of the first
2105 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2106 // file offset OFF. Add their names to POOL. Return the new file
2107 // offset. Update *PLOCAL_SYMCOUNT if necessary.
2110 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
2111 size_t dyncount
, Stringpool
* pool
,
2112 unsigned int *plocal_symcount
)
2116 gold_assert(*plocal_symcount
!= 0);
2117 this->first_global_index_
= *plocal_symcount
;
2119 this->dynamic_offset_
= dynoff
;
2120 this->first_dynamic_global_index_
= dyn_global_index
;
2121 this->dynamic_count_
= dyncount
;
2123 if (parameters
->target().get_size() == 32)
2125 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2126 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
2131 else if (parameters
->target().get_size() == 64)
2133 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2134 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
2142 // Now that we have the final symbol table, we can reliably note
2143 // which symbols should get warnings.
2144 this->warnings_
.note_warnings(this);
2149 // SYM is going into the symbol table at *PINDEX. Add the name to
2150 // POOL, update *PINDEX and *POFF.
2154 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
2155 unsigned int* pindex
, off_t
* poff
)
2157 sym
->set_symtab_index(*pindex
);
2158 pool
->add(sym
->name(), false, NULL
);
2160 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
2163 // Set the final value for all the symbols. This is called after
2164 // Layout::finalize, so all the output sections have their final
2169 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
2170 unsigned int* plocal_symcount
)
2172 off
= align_address(off
, size
>> 3);
2173 this->offset_
= off
;
2175 unsigned int index
= *plocal_symcount
;
2176 const unsigned int orig_index
= index
;
2178 // First do all the symbols which have been forced to be local, as
2179 // they must appear before all global symbols.
2180 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
2181 p
!= this->forced_locals_
.end();
2185 gold_assert(sym
->is_forced_local());
2186 if (this->sized_finalize_symbol
<size
>(sym
))
2188 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2193 // Now do all the remaining symbols.
2194 for (Symbol_table_type::iterator p
= this->table_
.begin();
2195 p
!= this->table_
.end();
2198 Symbol
* sym
= p
->second
;
2199 if (this->sized_finalize_symbol
<size
>(sym
))
2200 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
2203 this->output_count_
= index
- orig_index
;
2208 // Finalize the symbol SYM. This returns true if the symbol should be
2209 // added to the symbol table, false otherwise.
2213 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
2215 typedef typename Sized_symbol
<size
>::Value_type Value_type
;
2217 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
2219 // The default version of a symbol may appear twice in the symbol
2220 // table. We only need to finalize it once.
2221 if (sym
->has_symtab_index())
2226 gold_assert(!sym
->has_symtab_index());
2227 sym
->set_symtab_index(-1U);
2228 gold_assert(sym
->dynsym_index() == -1U);
2234 switch (sym
->source())
2236 case Symbol::FROM_OBJECT
:
2239 unsigned int shndx
= sym
->shndx(&is_ordinary
);
2241 // FIXME: We need some target specific support here.
2243 && shndx
!= elfcpp::SHN_ABS
2244 && shndx
!= elfcpp::SHN_COMMON
)
2246 gold_error(_("%s: unsupported symbol section 0x%x"),
2247 sym
->demangled_name().c_str(), shndx
);
2248 shndx
= elfcpp::SHN_UNDEF
;
2251 Object
* symobj
= sym
->object();
2252 if (symobj
->is_dynamic())
2255 shndx
= elfcpp::SHN_UNDEF
;
2257 else if (symobj
->pluginobj() != NULL
)
2260 shndx
= elfcpp::SHN_UNDEF
;
2262 else if (shndx
== elfcpp::SHN_UNDEF
)
2264 else if (!is_ordinary
2265 && (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
))
2266 value
= sym
->value();
2269 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2270 Output_section
* os
= relobj
->output_section(shndx
);
2274 sym
->set_symtab_index(-1U);
2275 bool static_or_reloc
= (parameters
->doing_static_link() ||
2276 parameters
->options().relocatable());
2277 gold_assert(static_or_reloc
|| sym
->dynsym_index() == -1U);
2282 uint64_t secoff64
= relobj
->output_section_offset(shndx
);
2283 if (secoff64
== -1ULL)
2285 // The section needs special handling (e.g., a merge section).
2286 value
= os
->output_address(relobj
, shndx
, sym
->value());
2291 convert_types
<Value_type
, uint64_t>(secoff64
);
2292 if (sym
->type() == elfcpp::STT_TLS
)
2293 value
= sym
->value() + os
->tls_offset() + secoff
;
2295 value
= sym
->value() + os
->address() + secoff
;
2301 case Symbol::IN_OUTPUT_DATA
:
2303 Output_data
* od
= sym
->output_data();
2304 value
= sym
->value();
2305 if (sym
->type() != elfcpp::STT_TLS
)
2306 value
+= od
->address();
2309 Output_section
* os
= od
->output_section();
2310 gold_assert(os
!= NULL
);
2311 value
+= os
->tls_offset() + (od
->address() - os
->address());
2313 if (sym
->offset_is_from_end())
2314 value
+= od
->data_size();
2318 case Symbol::IN_OUTPUT_SEGMENT
:
2320 Output_segment
* os
= sym
->output_segment();
2321 value
= sym
->value();
2322 if (sym
->type() != elfcpp::STT_TLS
)
2323 value
+= os
->vaddr();
2324 switch (sym
->offset_base())
2326 case Symbol::SEGMENT_START
:
2328 case Symbol::SEGMENT_END
:
2329 value
+= os
->memsz();
2331 case Symbol::SEGMENT_BSS
:
2332 value
+= os
->filesz();
2340 case Symbol::IS_CONSTANT
:
2341 value
= sym
->value();
2344 case Symbol::IS_UNDEFINED
:
2352 sym
->set_value(value
);
2354 if (parameters
->options().strip_all())
2356 sym
->set_symtab_index(-1U);
2363 // Write out the global symbols.
2366 Symbol_table::write_globals(const Input_objects
* input_objects
,
2367 const Stringpool
* sympool
,
2368 const Stringpool
* dynpool
,
2369 Output_symtab_xindex
* symtab_xindex
,
2370 Output_symtab_xindex
* dynsym_xindex
,
2371 Output_file
* of
) const
2373 switch (parameters
->size_and_endianness())
2375 #ifdef HAVE_TARGET_32_LITTLE
2376 case Parameters::TARGET_32_LITTLE
:
2377 this->sized_write_globals
<32, false>(input_objects
, sympool
,
2378 dynpool
, symtab_xindex
,
2382 #ifdef HAVE_TARGET_32_BIG
2383 case Parameters::TARGET_32_BIG
:
2384 this->sized_write_globals
<32, true>(input_objects
, sympool
,
2385 dynpool
, symtab_xindex
,
2389 #ifdef HAVE_TARGET_64_LITTLE
2390 case Parameters::TARGET_64_LITTLE
:
2391 this->sized_write_globals
<64, false>(input_objects
, sympool
,
2392 dynpool
, symtab_xindex
,
2396 #ifdef HAVE_TARGET_64_BIG
2397 case Parameters::TARGET_64_BIG
:
2398 this->sized_write_globals
<64, true>(input_objects
, sympool
,
2399 dynpool
, symtab_xindex
,
2408 // Write out the global symbols.
2410 template<int size
, bool big_endian
>
2412 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
2413 const Stringpool
* sympool
,
2414 const Stringpool
* dynpool
,
2415 Output_symtab_xindex
* symtab_xindex
,
2416 Output_symtab_xindex
* dynsym_xindex
,
2417 Output_file
* of
) const
2419 const Target
& target
= parameters
->target();
2421 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2423 const unsigned int output_count
= this->output_count_
;
2424 const section_size_type oview_size
= output_count
* sym_size
;
2425 const unsigned int first_global_index
= this->first_global_index_
;
2426 unsigned char* psyms
;
2427 if (this->offset_
== 0 || output_count
== 0)
2430 psyms
= of
->get_output_view(this->offset_
, oview_size
);
2432 const unsigned int dynamic_count
= this->dynamic_count_
;
2433 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
2434 const unsigned int first_dynamic_global_index
=
2435 this->first_dynamic_global_index_
;
2436 unsigned char* dynamic_view
;
2437 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
2438 dynamic_view
= NULL
;
2440 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
2442 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
2443 p
!= this->table_
.end();
2446 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
2448 // Possibly warn about unresolved symbols in shared libraries.
2449 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
2451 unsigned int sym_index
= sym
->symtab_index();
2452 unsigned int dynsym_index
;
2453 if (dynamic_view
== NULL
)
2456 dynsym_index
= sym
->dynsym_index();
2458 if (sym_index
== -1U && dynsym_index
== -1U)
2460 // This symbol is not included in the output file.
2465 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
2466 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
2467 switch (sym
->source())
2469 case Symbol::FROM_OBJECT
:
2472 unsigned int in_shndx
= sym
->shndx(&is_ordinary
);
2474 // FIXME: We need some target specific support here.
2476 && in_shndx
!= elfcpp::SHN_ABS
2477 && in_shndx
!= elfcpp::SHN_COMMON
)
2479 gold_error(_("%s: unsupported symbol section 0x%x"),
2480 sym
->demangled_name().c_str(), in_shndx
);
2485 Object
* symobj
= sym
->object();
2486 if (symobj
->is_dynamic())
2488 if (sym
->needs_dynsym_value())
2489 dynsym_value
= target
.dynsym_value(sym
);
2490 shndx
= elfcpp::SHN_UNDEF
;
2492 else if (symobj
->pluginobj() != NULL
)
2493 shndx
= elfcpp::SHN_UNDEF
;
2494 else if (in_shndx
== elfcpp::SHN_UNDEF
2496 && (in_shndx
== elfcpp::SHN_ABS
2497 || in_shndx
== elfcpp::SHN_COMMON
)))
2501 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
2502 Output_section
* os
= relobj
->output_section(in_shndx
);
2503 gold_assert(os
!= NULL
);
2504 shndx
= os
->out_shndx();
2506 if (shndx
>= elfcpp::SHN_LORESERVE
)
2508 if (sym_index
!= -1U)
2509 symtab_xindex
->add(sym_index
, shndx
);
2510 if (dynsym_index
!= -1U)
2511 dynsym_xindex
->add(dynsym_index
, shndx
);
2512 shndx
= elfcpp::SHN_XINDEX
;
2515 // In object files symbol values are section
2517 if (parameters
->options().relocatable())
2518 sym_value
-= os
->address();
2524 case Symbol::IN_OUTPUT_DATA
:
2525 shndx
= sym
->output_data()->out_shndx();
2526 if (shndx
>= elfcpp::SHN_LORESERVE
)
2528 if (sym_index
!= -1U)
2529 symtab_xindex
->add(sym_index
, shndx
);
2530 if (dynsym_index
!= -1U)
2531 dynsym_xindex
->add(dynsym_index
, shndx
);
2532 shndx
= elfcpp::SHN_XINDEX
;
2536 case Symbol::IN_OUTPUT_SEGMENT
:
2537 shndx
= elfcpp::SHN_ABS
;
2540 case Symbol::IS_CONSTANT
:
2541 shndx
= elfcpp::SHN_ABS
;
2544 case Symbol::IS_UNDEFINED
:
2545 shndx
= elfcpp::SHN_UNDEF
;
2552 if (sym_index
!= -1U)
2554 sym_index
-= first_global_index
;
2555 gold_assert(sym_index
< output_count
);
2556 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
2557 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
2561 if (dynsym_index
!= -1U)
2563 dynsym_index
-= first_dynamic_global_index
;
2564 gold_assert(dynsym_index
< dynamic_count
);
2565 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
2566 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
2571 of
->write_output_view(this->offset_
, oview_size
, psyms
);
2572 if (dynamic_view
!= NULL
)
2573 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
2576 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2577 // strtab holding the name.
2579 template<int size
, bool big_endian
>
2581 Symbol_table::sized_write_symbol(
2582 Sized_symbol
<size
>* sym
,
2583 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
2585 const Stringpool
* pool
,
2586 unsigned char* p
) const
2588 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
2589 osym
.put_st_name(pool
->get_offset(sym
->name()));
2590 osym
.put_st_value(value
);
2591 // Use a symbol size of zero for undefined symbols from shared libraries.
2592 if (shndx
== elfcpp::SHN_UNDEF
&& sym
->is_from_dynobj())
2593 osym
.put_st_size(0);
2595 osym
.put_st_size(sym
->symsize());
2596 // A version script may have overridden the default binding.
2597 if (sym
->is_forced_local())
2598 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, sym
->type()));
2600 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
2601 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
2602 osym
.put_st_shndx(shndx
);
2605 // Check for unresolved symbols in shared libraries. This is
2606 // controlled by the --allow-shlib-undefined option.
2608 // We only warn about libraries for which we have seen all the
2609 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2610 // which were not seen in this link. If we didn't see a DT_NEEDED
2611 // entry, we aren't going to be able to reliably report whether the
2612 // symbol is undefined.
2614 // We also don't warn about libraries found in the system library
2615 // directory (the directory were we find libc.so); we assume that
2616 // those libraries are OK. This heuristic avoids problems in
2617 // GNU/Linux, in which -ldl can have undefined references satisfied by
2621 Symbol_table::warn_about_undefined_dynobj_symbol(
2622 const Input_objects
* input_objects
,
2626 if (sym
->source() == Symbol::FROM_OBJECT
2627 && sym
->object()->is_dynamic()
2628 && sym
->shndx(&dummy
) == elfcpp::SHN_UNDEF
2629 && sym
->binding() != elfcpp::STB_WEAK
2630 && !parameters
->options().allow_shlib_undefined()
2631 && !parameters
->target().is_defined_by_abi(sym
)
2632 && !input_objects
->found_in_system_library_directory(sym
->object()))
2634 // A very ugly cast.
2635 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2636 if (!dynobj
->has_unknown_needed_entries())
2639 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2640 sym
->object()->name().c_str(),
2641 sym
->demangled_name().c_str(),
2644 gold_error(_("%s: undefined reference to '%s'"),
2645 sym
->object()->name().c_str(),
2646 sym
->demangled_name().c_str());
2651 // Write out a section symbol. Return the update offset.
2654 Symbol_table::write_section_symbol(const Output_section
*os
,
2655 Output_symtab_xindex
* symtab_xindex
,
2659 switch (parameters
->size_and_endianness())
2661 #ifdef HAVE_TARGET_32_LITTLE
2662 case Parameters::TARGET_32_LITTLE
:
2663 this->sized_write_section_symbol
<32, false>(os
, symtab_xindex
, of
,
2667 #ifdef HAVE_TARGET_32_BIG
2668 case Parameters::TARGET_32_BIG
:
2669 this->sized_write_section_symbol
<32, true>(os
, symtab_xindex
, of
,
2673 #ifdef HAVE_TARGET_64_LITTLE
2674 case Parameters::TARGET_64_LITTLE
:
2675 this->sized_write_section_symbol
<64, false>(os
, symtab_xindex
, of
,
2679 #ifdef HAVE_TARGET_64_BIG
2680 case Parameters::TARGET_64_BIG
:
2681 this->sized_write_section_symbol
<64, true>(os
, symtab_xindex
, of
,
2690 // Write out a section symbol, specialized for size and endianness.
2692 template<int size
, bool big_endian
>
2694 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2695 Output_symtab_xindex
* symtab_xindex
,
2699 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2701 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2703 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2704 osym
.put_st_name(0);
2705 osym
.put_st_value(os
->address());
2706 osym
.put_st_size(0);
2707 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2708 elfcpp::STT_SECTION
));
2709 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2711 unsigned int shndx
= os
->out_shndx();
2712 if (shndx
>= elfcpp::SHN_LORESERVE
)
2714 symtab_xindex
->add(os
->symtab_index(), shndx
);
2715 shndx
= elfcpp::SHN_XINDEX
;
2717 osym
.put_st_shndx(shndx
);
2719 of
->write_output_view(offset
, sym_size
, pov
);
2722 // Print statistical information to stderr. This is used for --stats.
2725 Symbol_table::print_stats() const
2727 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2728 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2729 program_name
, this->table_
.size(), this->table_
.bucket_count());
2731 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2732 program_name
, this->table_
.size());
2734 this->namepool_
.print_stats("symbol table stringpool");
2737 // We check for ODR violations by looking for symbols with the same
2738 // name for which the debugging information reports that they were
2739 // defined in different source locations. When comparing the source
2740 // location, we consider instances with the same base filename and
2741 // line number to be the same. This is because different object
2742 // files/shared libraries can include the same header file using
2743 // different paths, and we don't want to report an ODR violation in
2746 // This struct is used to compare line information, as returned by
2747 // Dwarf_line_info::one_addr2line. It implements a < comparison
2748 // operator used with std::set.
2750 struct Odr_violation_compare
2753 operator()(const std::string
& s1
, const std::string
& s2
) const
2755 std::string::size_type pos1
= s1
.rfind('/');
2756 std::string::size_type pos2
= s2
.rfind('/');
2757 if (pos1
== std::string::npos
2758 || pos2
== std::string::npos
)
2760 return s1
.compare(pos1
, std::string::npos
,
2761 s2
, pos2
, std::string::npos
) < 0;
2765 // Check candidate_odr_violations_ to find symbols with the same name
2766 // but apparently different definitions (different source-file/line-no).
2769 Symbol_table::detect_odr_violations(const Task
* task
,
2770 const char* output_file_name
) const
2772 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2773 it
!= candidate_odr_violations_
.end();
2776 const char* symbol_name
= it
->first
;
2777 // We use a sorted set so the output is deterministic.
2778 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2780 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2781 locs
= it
->second
.begin();
2782 locs
!= it
->second
.end();
2785 // We need to lock the object in order to read it. This
2786 // means that we have to run in a singleton Task. If we
2787 // want to run this in a general Task for better
2788 // performance, we will need one Task for object, plus
2789 // appropriate locking to ensure that we don't conflict with
2790 // other uses of the object. Also note, one_addr2line is not
2791 // currently thread-safe.
2792 Task_lock_obj
<Object
> tl(task
, locs
->object
);
2793 // 16 is the size of the object-cache that one_addr2line should use.
2794 std::string lineno
= Dwarf_line_info::one_addr2line(
2795 locs
->object
, locs
->shndx
, locs
->offset
, 16);
2796 if (!lineno
.empty())
2797 line_nums
.insert(lineno
);
2800 if (line_nums
.size() > 1)
2802 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2803 "places (possible ODR violation):"),
2804 output_file_name
, demangle(symbol_name
).c_str());
2805 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
2806 it2
!= line_nums
.end();
2808 fprintf(stderr
, " %s\n", it2
->c_str());
2811 // We only call one_addr2line() in this function, so we can clear its cache.
2812 Dwarf_line_info::clear_addr2line_cache();
2815 // Warnings functions.
2817 // Add a new warning.
2820 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
2821 const std::string
& warning
)
2823 name
= symtab
->canonicalize_name(name
);
2824 this->warnings_
[name
].set(obj
, warning
);
2827 // Look through the warnings and mark the symbols for which we should
2828 // warn. This is called during Layout::finalize when we know the
2829 // sources for all the symbols.
2832 Warnings::note_warnings(Symbol_table
* symtab
)
2834 for (Warning_table::iterator p
= this->warnings_
.begin();
2835 p
!= this->warnings_
.end();
2838 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2840 && sym
->source() == Symbol::FROM_OBJECT
2841 && sym
->object() == p
->second
.object
)
2842 sym
->set_has_warning();
2846 // Issue a warning. This is called when we see a relocation against a
2847 // symbol for which has a warning.
2849 template<int size
, bool big_endian
>
2851 Warnings::issue_warning(const Symbol
* sym
,
2852 const Relocate_info
<size
, big_endian
>* relinfo
,
2853 size_t relnum
, off_t reloffset
) const
2855 gold_assert(sym
->has_warning());
2856 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2857 gold_assert(p
!= this->warnings_
.end());
2858 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2859 "%s", p
->second
.text
.c_str());
2862 // Instantiate the templates we need. We could use the configure
2863 // script to restrict this to only the ones needed for implemented
2866 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2869 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2872 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2875 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2878 #ifdef HAVE_TARGET_32_LITTLE
2881 Symbol_table::add_from_relobj
<32, false>(
2882 Sized_relobj
<32, false>* relobj
,
2883 const unsigned char* syms
,
2885 size_t symndx_offset
,
2886 const char* sym_names
,
2887 size_t sym_name_size
,
2888 Sized_relobj
<32, false>::Symbols
* sympointers
,
2892 #ifdef HAVE_TARGET_32_BIG
2895 Symbol_table::add_from_relobj
<32, true>(
2896 Sized_relobj
<32, true>* relobj
,
2897 const unsigned char* syms
,
2899 size_t symndx_offset
,
2900 const char* sym_names
,
2901 size_t sym_name_size
,
2902 Sized_relobj
<32, true>::Symbols
* sympointers
,
2906 #ifdef HAVE_TARGET_64_LITTLE
2909 Symbol_table::add_from_relobj
<64, false>(
2910 Sized_relobj
<64, false>* relobj
,
2911 const unsigned char* syms
,
2913 size_t symndx_offset
,
2914 const char* sym_names
,
2915 size_t sym_name_size
,
2916 Sized_relobj
<64, false>::Symbols
* sympointers
,
2920 #ifdef HAVE_TARGET_64_BIG
2923 Symbol_table::add_from_relobj
<64, true>(
2924 Sized_relobj
<64, true>* relobj
,
2925 const unsigned char* syms
,
2927 size_t symndx_offset
,
2928 const char* sym_names
,
2929 size_t sym_name_size
,
2930 Sized_relobj
<64, true>::Symbols
* sympointers
,
2934 #ifdef HAVE_TARGET_32_LITTLE
2937 Symbol_table::add_from_pluginobj
<32, false>(
2938 Sized_pluginobj
<32, false>* obj
,
2941 elfcpp::Sym
<32, false>* sym
);
2944 #ifdef HAVE_TARGET_32_BIG
2947 Symbol_table::add_from_pluginobj
<32, true>(
2948 Sized_pluginobj
<32, true>* obj
,
2951 elfcpp::Sym
<32, true>* sym
);
2954 #ifdef HAVE_TARGET_64_LITTLE
2957 Symbol_table::add_from_pluginobj
<64, false>(
2958 Sized_pluginobj
<64, false>* obj
,
2961 elfcpp::Sym
<64, false>* sym
);
2964 #ifdef HAVE_TARGET_64_BIG
2967 Symbol_table::add_from_pluginobj
<64, true>(
2968 Sized_pluginobj
<64, true>* obj
,
2971 elfcpp::Sym
<64, true>* sym
);
2974 #ifdef HAVE_TARGET_32_LITTLE
2977 Symbol_table::add_from_dynobj
<32, false>(
2978 Sized_dynobj
<32, false>* dynobj
,
2979 const unsigned char* syms
,
2981 const char* sym_names
,
2982 size_t sym_name_size
,
2983 const unsigned char* versym
,
2985 const std::vector
<const char*>* version_map
,
2986 Sized_relobj
<32, false>::Symbols
* sympointers
,
2990 #ifdef HAVE_TARGET_32_BIG
2993 Symbol_table::add_from_dynobj
<32, true>(
2994 Sized_dynobj
<32, true>* dynobj
,
2995 const unsigned char* syms
,
2997 const char* sym_names
,
2998 size_t sym_name_size
,
2999 const unsigned char* versym
,
3001 const std::vector
<const char*>* version_map
,
3002 Sized_relobj
<32, true>::Symbols
* sympointers
,
3006 #ifdef HAVE_TARGET_64_LITTLE
3009 Symbol_table::add_from_dynobj
<64, false>(
3010 Sized_dynobj
<64, false>* dynobj
,
3011 const unsigned char* syms
,
3013 const char* sym_names
,
3014 size_t sym_name_size
,
3015 const unsigned char* versym
,
3017 const std::vector
<const char*>* version_map
,
3018 Sized_relobj
<64, false>::Symbols
* sympointers
,
3022 #ifdef HAVE_TARGET_64_BIG
3025 Symbol_table::add_from_dynobj
<64, true>(
3026 Sized_dynobj
<64, true>* dynobj
,
3027 const unsigned char* syms
,
3029 const char* sym_names
,
3030 size_t sym_name_size
,
3031 const unsigned char* versym
,
3033 const std::vector
<const char*>* version_map
,
3034 Sized_relobj
<64, true>::Symbols
* sympointers
,
3038 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3041 Symbol_table::define_with_copy_reloc
<32>(
3042 Sized_symbol
<32>* sym
,
3044 elfcpp::Elf_types
<32>::Elf_Addr value
);
3047 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3050 Symbol_table::define_with_copy_reloc
<64>(
3051 Sized_symbol
<64>* sym
,
3053 elfcpp::Elf_types
<64>::Elf_Addr value
);
3056 #ifdef HAVE_TARGET_32_LITTLE
3059 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
3060 const Relocate_info
<32, false>* relinfo
,
3061 size_t relnum
, off_t reloffset
) const;
3064 #ifdef HAVE_TARGET_32_BIG
3067 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
3068 const Relocate_info
<32, true>* relinfo
,
3069 size_t relnum
, off_t reloffset
) const;
3072 #ifdef HAVE_TARGET_64_LITTLE
3075 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
3076 const Relocate_info
<64, false>* relinfo
,
3077 size_t relnum
, off_t reloffset
) const;
3080 #ifdef HAVE_TARGET_64_BIG
3083 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
3084 const Relocate_info
<64, true>* relinfo
,
3085 size_t relnum
, off_t reloffset
) const;
3088 } // End namespace gold.