1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "dwarf_reader.h"
36 #include "workqueue.h"
44 // Initialize fields in Symbol. This initializes everything except u_
48 Symbol::init_fields(const char* name
, const char* version
,
49 elfcpp::STT type
, elfcpp::STB binding
,
50 elfcpp::STV visibility
, unsigned char nonvis
)
53 this->version_
= version
;
54 this->symtab_index_
= 0;
55 this->dynsym_index_
= 0;
56 this->got_offset_
= 0;
57 this->plt_offset_
= 0;
59 this->binding_
= binding
;
60 this->visibility_
= visibility
;
61 this->nonvis_
= nonvis
;
62 this->is_target_special_
= false;
63 this->is_def_
= false;
64 this->is_forwarder_
= false;
65 this->has_alias_
= false;
66 this->needs_dynsym_entry_
= false;
67 this->in_reg_
= false;
68 this->in_dyn_
= false;
69 this->has_got_offset_
= false;
70 this->has_plt_offset_
= false;
71 this->has_warning_
= false;
72 this->is_copied_from_dynobj_
= false;
73 this->is_forced_local_
= false;
76 // Return the demangled version of the symbol's name, but only
77 // if the --demangle flag was set.
80 demangle(const char* name
)
82 if (!parameters
->options().demangle())
85 // cplus_demangle allocates memory for the result it returns,
86 // and returns NULL if the name is already demangled.
87 char* demangled_name
= cplus_demangle(name
, DMGL_ANSI
| DMGL_PARAMS
);
88 if (demangled_name
== NULL
)
91 std::string
retval(demangled_name
);
97 Symbol::demangled_name() const
99 return demangle(this->name());
102 // Initialize the fields in the base class Symbol for SYM in OBJECT.
104 template<int size
, bool big_endian
>
106 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
107 const elfcpp::Sym
<size
, big_endian
>& sym
)
109 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
110 sym
.get_st_visibility(), sym
.get_st_nonvis());
111 this->u_
.from_object
.object
= object
;
112 // FIXME: Handle SHN_XINDEX.
113 this->u_
.from_object
.shndx
= sym
.get_st_shndx();
114 this->source_
= FROM_OBJECT
;
115 this->in_reg_
= !object
->is_dynamic();
116 this->in_dyn_
= object
->is_dynamic();
119 // Initialize the fields in the base class Symbol for a symbol defined
120 // in an Output_data.
123 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
124 elfcpp::STB binding
, elfcpp::STV visibility
,
125 unsigned char nonvis
, bool offset_is_from_end
)
127 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
128 this->u_
.in_output_data
.output_data
= od
;
129 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
130 this->source_
= IN_OUTPUT_DATA
;
131 this->in_reg_
= true;
134 // Initialize the fields in the base class Symbol for a symbol defined
135 // in an Output_segment.
138 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
139 elfcpp::STB binding
, elfcpp::STV visibility
,
140 unsigned char nonvis
, Segment_offset_base offset_base
)
142 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
143 this->u_
.in_output_segment
.output_segment
= os
;
144 this->u_
.in_output_segment
.offset_base
= offset_base
;
145 this->source_
= IN_OUTPUT_SEGMENT
;
146 this->in_reg_
= true;
149 // Initialize the fields in the base class Symbol for a symbol defined
153 Symbol::init_base(const char* name
, elfcpp::STT type
,
154 elfcpp::STB binding
, elfcpp::STV visibility
,
155 unsigned char nonvis
)
157 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
158 this->source_
= CONSTANT
;
159 this->in_reg_
= true;
162 // Allocate a common symbol in the base.
165 Symbol::allocate_base_common(Output_data
* od
)
167 gold_assert(this->is_common());
168 this->source_
= IN_OUTPUT_DATA
;
169 this->u_
.in_output_data
.output_data
= od
;
170 this->u_
.in_output_data
.offset_is_from_end
= false;
173 // Initialize the fields in Sized_symbol for SYM in OBJECT.
176 template<bool big_endian
>
178 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
179 const elfcpp::Sym
<size
, big_endian
>& sym
)
181 this->init_base(name
, version
, object
, sym
);
182 this->value_
= sym
.get_st_value();
183 this->symsize_
= sym
.get_st_size();
186 // Initialize the fields in Sized_symbol for a symbol defined in an
191 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
192 Value_type value
, Size_type symsize
,
193 elfcpp::STT type
, elfcpp::STB binding
,
194 elfcpp::STV visibility
, unsigned char nonvis
,
195 bool offset_is_from_end
)
197 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
199 this->value_
= value
;
200 this->symsize_
= symsize
;
203 // Initialize the fields in Sized_symbol for a symbol defined in an
208 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
209 Value_type value
, Size_type symsize
,
210 elfcpp::STT type
, elfcpp::STB binding
,
211 elfcpp::STV visibility
, unsigned char nonvis
,
212 Segment_offset_base offset_base
)
214 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
215 this->value_
= value
;
216 this->symsize_
= symsize
;
219 // Initialize the fields in Sized_symbol for a symbol defined as a
224 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
225 elfcpp::STT type
, elfcpp::STB binding
,
226 elfcpp::STV visibility
, unsigned char nonvis
)
228 this->init_base(name
, type
, binding
, visibility
, nonvis
);
229 this->value_
= value
;
230 this->symsize_
= symsize
;
233 // Allocate a common symbol.
237 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
239 this->allocate_base_common(od
);
240 this->value_
= value
;
243 // Return true if this symbol should be added to the dynamic symbol
247 Symbol::should_add_dynsym_entry() const
249 // If the symbol is used by a dynamic relocation, we need to add it.
250 if (this->needs_dynsym_entry())
253 // If the symbol was forced local in a version script, do not add it.
254 if (this->is_forced_local())
257 // If exporting all symbols or building a shared library,
258 // and the symbol is defined in a regular object and is
259 // externally visible, we need to add it.
260 if ((parameters
->options().export_dynamic() || parameters
->options().shared())
261 && !this->is_from_dynobj()
262 && this->is_externally_visible())
268 // Return true if the final value of this symbol is known at link
272 Symbol::final_value_is_known() const
274 // If we are not generating an executable, then no final values are
275 // known, since they will change at runtime.
276 if (parameters
->options().shared() || parameters
->options().relocatable())
279 // If the symbol is not from an object file, then it is defined, and
281 if (this->source_
!= FROM_OBJECT
)
284 // If the symbol is from a dynamic object, then the final value is
286 if (this->object()->is_dynamic())
289 // If the symbol is not undefined (it is defined or common), then
290 // the final value is known.
291 if (!this->is_undefined())
294 // If the symbol is undefined, then whether the final value is known
295 // depends on whether we are doing a static link. If we are doing a
296 // dynamic link, then the final value could be filled in at runtime.
297 // This could reasonably be the case for a weak undefined symbol.
298 return parameters
->doing_static_link();
301 // Return the output section where this symbol is defined.
304 Symbol::output_section() const
306 switch (this->source_
)
310 unsigned int shndx
= this->u_
.from_object
.shndx
;
311 if (shndx
!= elfcpp::SHN_UNDEF
&& shndx
< elfcpp::SHN_LORESERVE
)
313 gold_assert(!this->u_
.from_object
.object
->is_dynamic());
314 Relobj
* relobj
= static_cast<Relobj
*>(this->u_
.from_object
.object
);
315 section_offset_type dummy
;
316 return relobj
->output_section(shndx
, &dummy
);
322 return this->u_
.in_output_data
.output_data
->output_section();
324 case IN_OUTPUT_SEGMENT
:
333 // Set the symbol's output section. This is used for symbols defined
334 // in scripts. This should only be called after the symbol table has
338 Symbol::set_output_section(Output_section
* os
)
340 switch (this->source_
)
344 gold_assert(this->output_section() == os
);
347 this->source_
= IN_OUTPUT_DATA
;
348 this->u_
.in_output_data
.output_data
= os
;
349 this->u_
.in_output_data
.offset_is_from_end
= false;
351 case IN_OUTPUT_SEGMENT
:
357 // Class Symbol_table.
359 Symbol_table::Symbol_table(unsigned int count
,
360 const Version_script_info
& version_script
)
361 : saw_undefined_(0), offset_(0), table_(count
), namepool_(),
362 forwarders_(), commons_(), forced_locals_(), warnings_(),
363 version_script_(version_script
)
365 namepool_
.reserve(count
);
368 Symbol_table::~Symbol_table()
372 // The hash function. The key values are Stringpool keys.
375 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
377 return key
.first
^ key
.second
;
380 // The symbol table key equality function. This is called with
384 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
385 const Symbol_table_key
& k2
) const
387 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
390 // Make TO a symbol which forwards to FROM.
393 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
395 gold_assert(from
!= to
);
396 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
397 this->forwarders_
[from
] = to
;
398 from
->set_forwarder();
401 // Resolve the forwards from FROM, returning the real symbol.
404 Symbol_table::resolve_forwards(const Symbol
* from
) const
406 gold_assert(from
->is_forwarder());
407 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
408 this->forwarders_
.find(from
);
409 gold_assert(p
!= this->forwarders_
.end());
413 // Look up a symbol by name.
416 Symbol_table::lookup(const char* name
, const char* version
) const
418 Stringpool::Key name_key
;
419 name
= this->namepool_
.find(name
, &name_key
);
423 Stringpool::Key version_key
= 0;
426 version
= this->namepool_
.find(version
, &version_key
);
431 Symbol_table_key
key(name_key
, version_key
);
432 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
433 if (p
== this->table_
.end())
438 // Resolve a Symbol with another Symbol. This is only used in the
439 // unusual case where there are references to both an unversioned
440 // symbol and a symbol with a version, and we then discover that that
441 // version is the default version. Because this is unusual, we do
442 // this the slow way, by converting back to an ELF symbol.
444 template<int size
, bool big_endian
>
446 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
449 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
450 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
451 // We don't bother to set the st_name field.
452 esym
.put_st_value(from
->value());
453 esym
.put_st_size(from
->symsize());
454 esym
.put_st_info(from
->binding(), from
->type());
455 esym
.put_st_other(from
->visibility(), from
->nonvis());
456 esym
.put_st_shndx(from
->shndx());
457 this->resolve(to
, esym
.sym(), esym
.sym(), from
->object(), version
);
464 // Record that a symbol is forced to be local by a version script.
467 Symbol_table::force_local(Symbol
* sym
)
469 if (!sym
->is_defined() && !sym
->is_common())
471 if (sym
->is_forced_local())
473 // We already got this one.
476 sym
->set_is_forced_local();
477 this->forced_locals_
.push_back(sym
);
480 // Add one symbol from OBJECT to the symbol table. NAME is symbol
481 // name and VERSION is the version; both are canonicalized. DEF is
482 // whether this is the default version.
484 // If DEF is true, then this is the definition of a default version of
485 // a symbol. That means that any lookup of NAME/NULL and any lookup
486 // of NAME/VERSION should always return the same symbol. This is
487 // obvious for references, but in particular we want to do this for
488 // definitions: overriding NAME/NULL should also override
489 // NAME/VERSION. If we don't do that, it would be very hard to
490 // override functions in a shared library which uses versioning.
492 // We implement this by simply making both entries in the hash table
493 // point to the same Symbol structure. That is easy enough if this is
494 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
495 // that we have seen both already, in which case they will both have
496 // independent entries in the symbol table. We can't simply change
497 // the symbol table entry, because we have pointers to the entries
498 // attached to the object files. So we mark the entry attached to the
499 // object file as a forwarder, and record it in the forwarders_ map.
500 // Note that entries in the hash table will never be marked as
503 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
504 // symbol exactly as it existed in the input file. SYM is usually
505 // that as well, but can be modified, for instance if we determine
506 // it's in a to-be-discarded section.
508 template<int size
, bool big_endian
>
510 Symbol_table::add_from_object(Object
* object
,
512 Stringpool::Key name_key
,
514 Stringpool::Key version_key
,
516 const elfcpp::Sym
<size
, big_endian
>& sym
,
517 const elfcpp::Sym
<size
, big_endian
>& orig_sym
)
519 Symbol
* const snull
= NULL
;
520 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
521 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
524 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
525 std::make_pair(this->table_
.end(), false);
528 const Stringpool::Key vnull_key
= 0;
529 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
534 // ins.first: an iterator, which is a pointer to a pair.
535 // ins.first->first: the key (a pair of name and version).
536 // ins.first->second: the value (Symbol*).
537 // ins.second: true if new entry was inserted, false if not.
539 Sized_symbol
<size
>* ret
;
544 // We already have an entry for NAME/VERSION.
545 ret
= this->get_sized_symbol
<size
>(ins
.first
->second
);
546 gold_assert(ret
!= NULL
);
548 was_undefined
= ret
->is_undefined();
549 was_common
= ret
->is_common();
551 this->resolve(ret
, sym
, orig_sym
, object
, version
);
557 // This is the first time we have seen NAME/NULL. Make
558 // NAME/NULL point to NAME/VERSION.
559 insdef
.first
->second
= ret
;
561 else if (insdef
.first
->second
!= ret
562 && insdef
.first
->second
->is_undefined())
564 // This is the unfortunate case where we already have
565 // entries for both NAME/VERSION and NAME/NULL. Note
566 // that we don't want to combine them if the existing
567 // symbol is going to override the new one. FIXME: We
568 // currently just test is_undefined, but this may not do
569 // the right thing if the existing symbol is from a
570 // shared library and the new one is from a regular
573 const Sized_symbol
<size
>* sym2
;
574 sym2
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
575 Symbol_table::resolve
<size
, big_endian
>(ret
, sym2
, version
);
576 this->make_forwarder(insdef
.first
->second
, ret
);
577 insdef
.first
->second
= ret
;
583 // This is the first time we have seen NAME/VERSION.
584 gold_assert(ins
.first
->second
== NULL
);
586 was_undefined
= false;
589 if (def
&& !insdef
.second
)
591 // We already have an entry for NAME/NULL. If we override
592 // it, then change it to NAME/VERSION.
593 ret
= this->get_sized_symbol
<size
>(insdef
.first
->second
);
594 this->resolve(ret
, sym
, orig_sym
, object
, version
);
595 ins
.first
->second
= ret
;
599 Sized_target
<size
, big_endian
>* target
=
600 object
->sized_target
<size
, big_endian
>();
601 if (!target
->has_make_symbol())
602 ret
= new Sized_symbol
<size
>();
605 ret
= target
->make_symbol();
608 // This means that we don't want a symbol table
611 this->table_
.erase(ins
.first
);
614 this->table_
.erase(insdef
.first
);
615 // Inserting insdef invalidated ins.
616 this->table_
.erase(std::make_pair(name_key
,
623 ret
->init(name
, version
, object
, sym
);
625 ins
.first
->second
= ret
;
628 // This is the first time we have seen NAME/NULL. Point
629 // it at the new entry for NAME/VERSION.
630 gold_assert(insdef
.second
);
631 insdef
.first
->second
= ret
;
636 // Record every time we see a new undefined symbol, to speed up
638 if (!was_undefined
&& ret
->is_undefined())
639 ++this->saw_undefined_
;
641 // Keep track of common symbols, to speed up common symbol
643 if (!was_common
&& ret
->is_common())
644 this->commons_
.push_back(ret
);
646 ret
->set_is_default(def
);
650 // Add all the symbols in a relocatable object to the hash table.
652 template<int size
, bool big_endian
>
654 Symbol_table::add_from_relobj(
655 Sized_relobj
<size
, big_endian
>* relobj
,
656 const unsigned char* syms
,
658 const char* sym_names
,
659 size_t sym_name_size
,
660 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
)
662 gold_assert(size
== relobj
->target()->get_size());
663 gold_assert(size
== parameters
->target().get_size());
665 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
667 const bool just_symbols
= relobj
->just_symbols();
669 const unsigned char* p
= syms
;
670 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
672 elfcpp::Sym
<size
, big_endian
> sym(p
);
673 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
675 unsigned int st_name
= psym
->get_st_name();
676 if (st_name
>= sym_name_size
)
678 relobj
->error(_("bad global symbol name offset %u at %zu"),
683 const char* name
= sym_names
+ st_name
;
685 // A symbol defined in a section which we are not including must
686 // be treated as an undefined symbol.
687 unsigned char symbuf
[sym_size
];
688 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
689 unsigned int st_shndx
= psym
->get_st_shndx();
690 if (st_shndx
!= elfcpp::SHN_UNDEF
691 && st_shndx
< elfcpp::SHN_LORESERVE
692 && !relobj
->is_section_included(st_shndx
))
694 memcpy(symbuf
, p
, sym_size
);
695 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
696 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
700 // In an object file, an '@' in the name separates the symbol
701 // name from the version name. If there are two '@' characters,
702 // this is the default version.
703 const char* ver
= strchr(name
, '@');
705 // DEF: is the version default? LOCAL: is the symbol forced local?
711 // The symbol name is of the form foo@VERSION or foo@@VERSION
712 namelen
= ver
- name
;
720 else if (!version_script_
.empty())
722 // The symbol name did not have a version, but
723 // the version script may assign a version anyway.
724 namelen
= strlen(name
);
726 // Check the global: entries from the version script.
727 const std::string
& version
=
728 version_script_
.get_symbol_version(name
);
729 if (!version
.empty())
730 ver
= version
.c_str();
731 // Check the local: entries from the version script
732 if (version_script_
.symbol_is_local(name
))
739 memcpy(symbuf
, p
, sym_size
);
740 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
741 sw
.put_st_shndx(elfcpp::SHN_ABS
);
742 if (st_shndx
!= elfcpp::SHN_UNDEF
743 && st_shndx
< elfcpp::SHN_LORESERVE
)
745 // Symbol values in object files are section relative.
746 // This is normally what we want, but since here we are
747 // converting the symbol to absolute we need to add the
748 // section address. The section address in an object
749 // file is normally zero, but people can use a linker
750 // script to change it.
751 sw
.put_st_value(sym2
.get_st_value()
752 + relobj
->section_address(st_shndx
));
757 Sized_symbol
<size
>* res
;
760 Stringpool::Key name_key
;
761 name
= this->namepool_
.add(name
, true, &name_key
);
762 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
765 this->force_local(res
);
769 Stringpool::Key name_key
;
770 name
= this->namepool_
.add_with_length(name
, namelen
, true,
772 Stringpool::Key ver_key
;
773 ver
= this->namepool_
.add(ver
, true, &ver_key
);
775 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
779 (*sympointers
)[i
] = res
;
783 // Add all the symbols in a dynamic object to the hash table.
785 template<int size
, bool big_endian
>
787 Symbol_table::add_from_dynobj(
788 Sized_dynobj
<size
, big_endian
>* dynobj
,
789 const unsigned char* syms
,
791 const char* sym_names
,
792 size_t sym_name_size
,
793 const unsigned char* versym
,
795 const std::vector
<const char*>* version_map
)
797 gold_assert(size
== dynobj
->target()->get_size());
798 gold_assert(size
== parameters
->target().get_size());
800 if (dynobj
->just_symbols())
802 gold_error(_("--just-symbols does not make sense with a shared object"));
806 if (versym
!= NULL
&& versym_size
/ 2 < count
)
808 dynobj
->error(_("too few symbol versions"));
812 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
814 // We keep a list of all STT_OBJECT symbols, so that we can resolve
815 // weak aliases. This is necessary because if the dynamic object
816 // provides the same variable under two names, one of which is a
817 // weak definition, and the regular object refers to the weak
818 // definition, we have to put both the weak definition and the
819 // strong definition into the dynamic symbol table. Given a weak
820 // definition, the only way that we can find the corresponding
821 // strong definition, if any, is to search the symbol table.
822 std::vector
<Sized_symbol
<size
>*> object_symbols
;
824 const unsigned char* p
= syms
;
825 const unsigned char* vs
= versym
;
826 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
828 elfcpp::Sym
<size
, big_endian
> sym(p
);
830 // Ignore symbols with local binding or that have
831 // internal or hidden visibility.
832 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
833 || sym
.get_st_visibility() == elfcpp::STV_INTERNAL
834 || sym
.get_st_visibility() == elfcpp::STV_HIDDEN
)
837 unsigned int st_name
= sym
.get_st_name();
838 if (st_name
>= sym_name_size
)
840 dynobj
->error(_("bad symbol name offset %u at %zu"),
845 const char* name
= sym_names
+ st_name
;
847 Sized_symbol
<size
>* res
;
851 Stringpool::Key name_key
;
852 name
= this->namepool_
.add(name
, true, &name_key
);
853 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
858 // Read the version information.
860 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
862 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
863 v
&= elfcpp::VERSYM_VERSION
;
865 // The Sun documentation says that V can be VER_NDX_LOCAL,
866 // or VER_NDX_GLOBAL, or a version index. The meaning of
867 // VER_NDX_LOCAL is defined as "Symbol has local scope."
868 // The old GNU linker will happily generate VER_NDX_LOCAL
869 // for an undefined symbol. I don't know what the Sun
870 // linker will generate.
872 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
873 && sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
875 // This symbol should not be visible outside the object.
879 // At this point we are definitely going to add this symbol.
880 Stringpool::Key name_key
;
881 name
= this->namepool_
.add(name
, true, &name_key
);
883 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
884 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
886 // This symbol does not have a version.
887 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
892 if (v
>= version_map
->size())
894 dynobj
->error(_("versym for symbol %zu out of range: %u"),
899 const char* version
= (*version_map
)[v
];
902 dynobj
->error(_("versym for symbol %zu has no name: %u"),
907 Stringpool::Key version_key
;
908 version
= this->namepool_
.add(version
, true, &version_key
);
910 // If this is an absolute symbol, and the version name
911 // and symbol name are the same, then this is the
912 // version definition symbol. These symbols exist to
913 // support using -u to pull in particular versions. We
914 // do not want to record a version for them.
915 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
916 && name_key
== version_key
)
917 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
921 const bool def
= (!hidden
922 && (sym
.get_st_shndx()
923 != elfcpp::SHN_UNDEF
));
924 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
925 version_key
, def
, sym
, sym
);
930 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
931 && sym
.get_st_type() == elfcpp::STT_OBJECT
)
932 object_symbols
.push_back(res
);
935 this->record_weak_aliases(&object_symbols
);
938 // This is used to sort weak aliases. We sort them first by section
939 // index, then by offset, then by weak ahead of strong.
942 class Weak_alias_sorter
945 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
950 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
951 const Sized_symbol
<size
>* s2
) const
953 if (s1
->shndx() != s2
->shndx())
954 return s1
->shndx() < s2
->shndx();
955 if (s1
->value() != s2
->value())
956 return s1
->value() < s2
->value();
957 if (s1
->binding() != s2
->binding())
959 if (s1
->binding() == elfcpp::STB_WEAK
)
961 if (s2
->binding() == elfcpp::STB_WEAK
)
964 return std::string(s1
->name()) < std::string(s2
->name());
967 // SYMBOLS is a list of object symbols from a dynamic object. Look
968 // for any weak aliases, and record them so that if we add the weak
969 // alias to the dynamic symbol table, we also add the corresponding
974 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
976 // Sort the vector by section index, then by offset, then by weak
978 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
980 // Walk through the vector. For each weak definition, record
982 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
987 if ((*p
)->binding() != elfcpp::STB_WEAK
)
990 // Build a circular list of weak aliases. Each symbol points to
991 // the next one in the circular list.
993 Sized_symbol
<size
>* from_sym
= *p
;
994 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
995 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
997 if ((*q
)->shndx() != from_sym
->shndx()
998 || (*q
)->value() != from_sym
->value())
1001 this->weak_aliases_
[from_sym
] = *q
;
1002 from_sym
->set_has_alias();
1008 this->weak_aliases_
[from_sym
] = *p
;
1009 from_sym
->set_has_alias();
1016 // Create and return a specially defined symbol. If ONLY_IF_REF is
1017 // true, then only create the symbol if there is a reference to it.
1018 // If this does not return NULL, it sets *POLDSYM to the existing
1019 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
1021 template<int size
, bool big_endian
>
1023 Symbol_table::define_special_symbol(const char** pname
, const char** pversion
,
1025 Sized_symbol
<size
>** poldsym
)
1028 Sized_symbol
<size
>* sym
;
1029 bool add_to_table
= false;
1030 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
1032 // If the caller didn't give us a version, see if we get one from
1033 // the version script.
1034 if (*pversion
== NULL
)
1036 const std::string
& v(this->version_script_
.get_symbol_version(*pname
));
1038 *pversion
= v
.c_str();
1043 oldsym
= this->lookup(*pname
, *pversion
);
1044 if (oldsym
== NULL
|| !oldsym
->is_undefined())
1047 *pname
= oldsym
->name();
1048 *pversion
= oldsym
->version();
1052 // Canonicalize NAME and VERSION.
1053 Stringpool::Key name_key
;
1054 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
1056 Stringpool::Key version_key
= 0;
1057 if (*pversion
!= NULL
)
1058 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
1060 Symbol
* const snull
= NULL
;
1061 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
1062 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
1068 // We already have a symbol table entry for NAME/VERSION.
1069 oldsym
= ins
.first
->second
;
1070 gold_assert(oldsym
!= NULL
);
1074 // We haven't seen this symbol before.
1075 gold_assert(ins
.first
->second
== NULL
);
1076 add_to_table
= true;
1077 add_loc
= ins
.first
;
1082 const Target
& target
= parameters
->target();
1083 if (!target
.has_make_symbol())
1084 sym
= new Sized_symbol
<size
>();
1087 gold_assert(target
.get_size() == size
);
1088 gold_assert(target
.is_big_endian() ? big_endian
: !big_endian
);
1089 typedef Sized_target
<size
, big_endian
> My_target
;
1090 const My_target
* sized_target
=
1091 static_cast<const My_target
*>(&target
);
1092 sym
= sized_target
->make_symbol();
1098 add_loc
->second
= sym
;
1100 gold_assert(oldsym
!= NULL
);
1102 *poldsym
= this->get_sized_symbol
<size
>(oldsym
);
1107 // Define a symbol based on an Output_data.
1110 Symbol_table::define_in_output_data(const char* name
,
1111 const char* version
,
1116 elfcpp::STB binding
,
1117 elfcpp::STV visibility
,
1118 unsigned char nonvis
,
1119 bool offset_is_from_end
,
1122 if (parameters
->target().get_size() == 32)
1124 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1125 return this->do_define_in_output_data
<32>(name
, version
, od
,
1126 value
, symsize
, type
, binding
,
1134 else if (parameters
->target().get_size() == 64)
1136 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1137 return this->do_define_in_output_data
<64>(name
, version
, od
,
1138 value
, symsize
, type
, binding
,
1150 // Define a symbol in an Output_data, sized version.
1154 Symbol_table::do_define_in_output_data(
1156 const char* version
,
1158 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1159 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1161 elfcpp::STB binding
,
1162 elfcpp::STV visibility
,
1163 unsigned char nonvis
,
1164 bool offset_is_from_end
,
1167 Sized_symbol
<size
>* sym
;
1168 Sized_symbol
<size
>* oldsym
;
1170 if (parameters
->target().is_big_endian())
1172 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1173 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1174 only_if_ref
, &oldsym
);
1181 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1182 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1183 only_if_ref
, &oldsym
);
1192 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1193 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1194 offset_is_from_end
);
1198 if (binding
== elfcpp::STB_LOCAL
1199 || this->version_script_
.symbol_is_local(name
))
1200 this->force_local(sym
);
1204 if (Symbol_table::should_override_with_special(oldsym
))
1205 this->override_with_special(oldsym
, sym
);
1210 // Define a symbol based on an Output_segment.
1213 Symbol_table::define_in_output_segment(const char* name
,
1214 const char* version
, Output_segment
* os
,
1218 elfcpp::STB binding
,
1219 elfcpp::STV visibility
,
1220 unsigned char nonvis
,
1221 Symbol::Segment_offset_base offset_base
,
1224 if (parameters
->target().get_size() == 32)
1226 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1227 return this->do_define_in_output_segment
<32>(name
, version
, os
,
1228 value
, symsize
, type
,
1229 binding
, visibility
, nonvis
,
1230 offset_base
, only_if_ref
);
1235 else if (parameters
->target().get_size() == 64)
1237 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1238 return this->do_define_in_output_segment
<64>(name
, version
, os
,
1239 value
, symsize
, type
,
1240 binding
, visibility
, nonvis
,
1241 offset_base
, only_if_ref
);
1250 // Define a symbol in an Output_segment, sized version.
1254 Symbol_table::do_define_in_output_segment(
1256 const char* version
,
1258 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1259 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1261 elfcpp::STB binding
,
1262 elfcpp::STV visibility
,
1263 unsigned char nonvis
,
1264 Symbol::Segment_offset_base offset_base
,
1267 Sized_symbol
<size
>* sym
;
1268 Sized_symbol
<size
>* oldsym
;
1270 if (parameters
->target().is_big_endian())
1272 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1273 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1274 only_if_ref
, &oldsym
);
1281 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1282 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1283 only_if_ref
, &oldsym
);
1292 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1293 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1298 if (binding
== elfcpp::STB_LOCAL
1299 || this->version_script_
.symbol_is_local(name
))
1300 this->force_local(sym
);
1304 if (Symbol_table::should_override_with_special(oldsym
))
1305 this->override_with_special(oldsym
, sym
);
1310 // Define a special symbol with a constant value. It is a multiple
1311 // definition error if this symbol is already defined.
1314 Symbol_table::define_as_constant(const char* name
,
1315 const char* version
,
1319 elfcpp::STB binding
,
1320 elfcpp::STV visibility
,
1321 unsigned char nonvis
,
1323 bool force_override
)
1325 if (parameters
->target().get_size() == 32)
1327 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1328 return this->do_define_as_constant
<32>(name
, version
, value
,
1329 symsize
, type
, binding
,
1330 visibility
, nonvis
, only_if_ref
,
1336 else if (parameters
->target().get_size() == 64)
1338 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1339 return this->do_define_as_constant
<64>(name
, version
, value
,
1340 symsize
, type
, binding
,
1341 visibility
, nonvis
, only_if_ref
,
1351 // Define a symbol as a constant, sized version.
1355 Symbol_table::do_define_as_constant(
1357 const char* version
,
1358 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1359 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1361 elfcpp::STB binding
,
1362 elfcpp::STV visibility
,
1363 unsigned char nonvis
,
1365 bool force_override
)
1367 Sized_symbol
<size
>* sym
;
1368 Sized_symbol
<size
>* oldsym
;
1370 if (parameters
->target().is_big_endian())
1372 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1373 sym
= this->define_special_symbol
<size
, true>(&name
, &version
,
1374 only_if_ref
, &oldsym
);
1381 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1382 sym
= this->define_special_symbol
<size
, false>(&name
, &version
,
1383 only_if_ref
, &oldsym
);
1392 gold_assert(version
== NULL
|| version
== name
|| oldsym
!= NULL
);
1393 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
1397 if (binding
== elfcpp::STB_LOCAL
1398 || this->version_script_
.symbol_is_local(name
))
1399 this->force_local(sym
);
1403 if (force_override
|| Symbol_table::should_override_with_special(oldsym
))
1404 this->override_with_special(oldsym
, sym
);
1409 // Define a set of symbols in output sections.
1412 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1413 const Define_symbol_in_section
* p
,
1416 for (int i
= 0; i
< count
; ++i
, ++p
)
1418 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1420 this->define_in_output_data(p
->name
, NULL
, os
, p
->value
,
1421 p
->size
, p
->type
, p
->binding
,
1422 p
->visibility
, p
->nonvis
,
1423 p
->offset_is_from_end
,
1424 only_if_ref
|| p
->only_if_ref
);
1426 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1427 p
->binding
, p
->visibility
, p
->nonvis
,
1428 only_if_ref
|| p
->only_if_ref
,
1433 // Define a set of symbols in output segments.
1436 Symbol_table::define_symbols(const Layout
* layout
, int count
,
1437 const Define_symbol_in_segment
* p
,
1440 for (int i
= 0; i
< count
; ++i
, ++p
)
1442 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1443 p
->segment_flags_set
,
1444 p
->segment_flags_clear
);
1446 this->define_in_output_segment(p
->name
, NULL
, os
, p
->value
,
1447 p
->size
, p
->type
, p
->binding
,
1448 p
->visibility
, p
->nonvis
,
1450 only_if_ref
|| p
->only_if_ref
);
1452 this->define_as_constant(p
->name
, NULL
, 0, p
->size
, p
->type
,
1453 p
->binding
, p
->visibility
, p
->nonvis
,
1454 only_if_ref
|| p
->only_if_ref
,
1459 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1460 // symbol should be defined--typically a .dyn.bss section. VALUE is
1461 // the offset within POSD.
1465 Symbol_table::define_with_copy_reloc(
1466 Sized_symbol
<size
>* csym
,
1468 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
)
1470 gold_assert(csym
->is_from_dynobj());
1471 gold_assert(!csym
->is_copied_from_dynobj());
1472 Object
* object
= csym
->object();
1473 gold_assert(object
->is_dynamic());
1474 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1476 // Our copied variable has to override any variable in a shared
1478 elfcpp::STB binding
= csym
->binding();
1479 if (binding
== elfcpp::STB_WEAK
)
1480 binding
= elfcpp::STB_GLOBAL
;
1482 this->define_in_output_data(csym
->name(), csym
->version(),
1483 posd
, value
, csym
->symsize(),
1484 csym
->type(), binding
,
1485 csym
->visibility(), csym
->nonvis(),
1488 csym
->set_is_copied_from_dynobj();
1489 csym
->set_needs_dynsym_entry();
1491 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1493 // We have now defined all aliases, but we have not entered them all
1494 // in the copied_symbol_dynobjs_ map.
1495 if (csym
->has_alias())
1500 sym
= this->weak_aliases_
[sym
];
1503 gold_assert(sym
->output_data() == posd
);
1505 sym
->set_is_copied_from_dynobj();
1506 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1511 // SYM is defined using a COPY reloc. Return the dynamic object where
1512 // the original definition was found.
1515 Symbol_table::get_copy_source(const Symbol
* sym
) const
1517 gold_assert(sym
->is_copied_from_dynobj());
1518 Copied_symbol_dynobjs::const_iterator p
=
1519 this->copied_symbol_dynobjs_
.find(sym
);
1520 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1524 // Set the dynamic symbol indexes. INDEX is the index of the first
1525 // global dynamic symbol. Pointers to the symbols are stored into the
1526 // vector SYMS. The names are added to DYNPOOL. This returns an
1527 // updated dynamic symbol index.
1530 Symbol_table::set_dynsym_indexes(unsigned int index
,
1531 std::vector
<Symbol
*>* syms
,
1532 Stringpool
* dynpool
,
1535 for (Symbol_table_type::iterator p
= this->table_
.begin();
1536 p
!= this->table_
.end();
1539 Symbol
* sym
= p
->second
;
1541 // Note that SYM may already have a dynamic symbol index, since
1542 // some symbols appear more than once in the symbol table, with
1543 // and without a version.
1545 if (!sym
->should_add_dynsym_entry())
1546 sym
->set_dynsym_index(-1U);
1547 else if (!sym
->has_dynsym_index())
1549 sym
->set_dynsym_index(index
);
1551 syms
->push_back(sym
);
1552 dynpool
->add(sym
->name(), false, NULL
);
1554 // Record any version information.
1555 if (sym
->version() != NULL
)
1556 versions
->record_version(this, dynpool
, sym
);
1560 // Finish up the versions. In some cases this may add new dynamic
1562 index
= versions
->finalize(this, index
, syms
);
1567 // Set the final values for all the symbols. The index of the first
1568 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1569 // file offset OFF. Add their names to POOL. Return the new file
1570 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1573 Symbol_table::finalize(off_t off
, off_t dynoff
, size_t dyn_global_index
,
1574 size_t dyncount
, Stringpool
* pool
,
1575 unsigned int *plocal_symcount
)
1579 gold_assert(*plocal_symcount
!= 0);
1580 this->first_global_index_
= *plocal_symcount
;
1582 this->dynamic_offset_
= dynoff
;
1583 this->first_dynamic_global_index_
= dyn_global_index
;
1584 this->dynamic_count_
= dyncount
;
1586 if (parameters
->target().get_size() == 32)
1588 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1589 ret
= this->sized_finalize
<32>(off
, pool
, plocal_symcount
);
1594 else if (parameters
->target().get_size() == 64)
1596 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1597 ret
= this->sized_finalize
<64>(off
, pool
, plocal_symcount
);
1605 // Now that we have the final symbol table, we can reliably note
1606 // which symbols should get warnings.
1607 this->warnings_
.note_warnings(this);
1612 // SYM is going into the symbol table at *PINDEX. Add the name to
1613 // POOL, update *PINDEX and *POFF.
1617 Symbol_table::add_to_final_symtab(Symbol
* sym
, Stringpool
* pool
,
1618 unsigned int* pindex
, off_t
* poff
)
1620 sym
->set_symtab_index(*pindex
);
1621 pool
->add(sym
->name(), false, NULL
);
1623 *poff
+= elfcpp::Elf_sizes
<size
>::sym_size
;
1626 // Set the final value for all the symbols. This is called after
1627 // Layout::finalize, so all the output sections have their final
1632 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
,
1633 unsigned int* plocal_symcount
)
1635 off
= align_address(off
, size
>> 3);
1636 this->offset_
= off
;
1638 unsigned int index
= *plocal_symcount
;
1639 const unsigned int orig_index
= index
;
1641 // First do all the symbols which have been forced to be local, as
1642 // they must appear before all global symbols.
1643 for (Forced_locals::iterator p
= this->forced_locals_
.begin();
1644 p
!= this->forced_locals_
.end();
1648 gold_assert(sym
->is_forced_local());
1649 if (this->sized_finalize_symbol
<size
>(sym
))
1651 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
1656 // Now do all the remaining symbols.
1657 for (Symbol_table_type::iterator p
= this->table_
.begin();
1658 p
!= this->table_
.end();
1661 Symbol
* sym
= p
->second
;
1662 if (this->sized_finalize_symbol
<size
>(sym
))
1663 this->add_to_final_symtab
<size
>(sym
, pool
, &index
, &off
);
1666 this->output_count_
= index
- orig_index
;
1671 // Finalize the symbol SYM. This returns true if the symbol should be
1672 // added to the symbol table, false otherwise.
1676 Symbol_table::sized_finalize_symbol(Symbol
* unsized_sym
)
1678 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(unsized_sym
);
1680 // The default version of a symbol may appear twice in the symbol
1681 // table. We only need to finalize it once.
1682 if (sym
->has_symtab_index())
1687 gold_assert(!sym
->has_symtab_index());
1688 sym
->set_symtab_index(-1U);
1689 gold_assert(sym
->dynsym_index() == -1U);
1693 typename Sized_symbol
<size
>::Value_type value
;
1695 switch (sym
->source())
1697 case Symbol::FROM_OBJECT
:
1699 unsigned int shndx
= sym
->shndx();
1701 // FIXME: We need some target specific support here.
1702 if (shndx
>= elfcpp::SHN_LORESERVE
1703 && shndx
!= elfcpp::SHN_ABS
1704 && shndx
!= elfcpp::SHN_COMMON
)
1706 gold_error(_("%s: unsupported symbol section 0x%x"),
1707 sym
->demangled_name().c_str(), shndx
);
1708 shndx
= elfcpp::SHN_UNDEF
;
1711 Object
* symobj
= sym
->object();
1712 if (symobj
->is_dynamic())
1715 shndx
= elfcpp::SHN_UNDEF
;
1717 else if (shndx
== elfcpp::SHN_UNDEF
)
1719 else if (shndx
== elfcpp::SHN_ABS
|| shndx
== elfcpp::SHN_COMMON
)
1720 value
= sym
->value();
1723 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1724 section_offset_type secoff
;
1725 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1729 sym
->set_symtab_index(-1U);
1730 gold_assert(sym
->dynsym_index() == -1U);
1734 if (sym
->type() == elfcpp::STT_TLS
)
1735 value
= sym
->value() + os
->tls_offset() + secoff
;
1737 value
= sym
->value() + os
->address() + secoff
;
1742 case Symbol::IN_OUTPUT_DATA
:
1744 Output_data
* od
= sym
->output_data();
1745 value
= sym
->value() + od
->address();
1746 if (sym
->offset_is_from_end())
1747 value
+= od
->data_size();
1751 case Symbol::IN_OUTPUT_SEGMENT
:
1753 Output_segment
* os
= sym
->output_segment();
1754 value
= sym
->value() + os
->vaddr();
1755 switch (sym
->offset_base())
1757 case Symbol::SEGMENT_START
:
1759 case Symbol::SEGMENT_END
:
1760 value
+= os
->memsz();
1762 case Symbol::SEGMENT_BSS
:
1763 value
+= os
->filesz();
1771 case Symbol::CONSTANT
:
1772 value
= sym
->value();
1779 sym
->set_value(value
);
1781 if (parameters
->options().strip_all())
1783 sym
->set_symtab_index(-1U);
1790 // Write out the global symbols.
1793 Symbol_table::write_globals(const Input_objects
* input_objects
,
1794 const Stringpool
* sympool
,
1795 const Stringpool
* dynpool
, Output_file
* of
) const
1797 switch (parameters
->size_and_endianness())
1799 #ifdef HAVE_TARGET_32_LITTLE
1800 case Parameters::TARGET_32_LITTLE
:
1801 this->sized_write_globals
<32, false>(input_objects
, sympool
,
1805 #ifdef HAVE_TARGET_32_BIG
1806 case Parameters::TARGET_32_BIG
:
1807 this->sized_write_globals
<32, true>(input_objects
, sympool
,
1811 #ifdef HAVE_TARGET_64_LITTLE
1812 case Parameters::TARGET_64_LITTLE
:
1813 this->sized_write_globals
<64, false>(input_objects
, sympool
,
1817 #ifdef HAVE_TARGET_64_BIG
1818 case Parameters::TARGET_64_BIG
:
1819 this->sized_write_globals
<64, true>(input_objects
, sympool
,
1828 // Write out the global symbols.
1830 template<int size
, bool big_endian
>
1832 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
1833 const Stringpool
* sympool
,
1834 const Stringpool
* dynpool
,
1835 Output_file
* of
) const
1837 const Target
& target
= parameters
->target();
1839 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1841 const unsigned int output_count
= this->output_count_
;
1842 const section_size_type oview_size
= output_count
* sym_size
;
1843 const unsigned int first_global_index
= this->first_global_index_
;
1844 unsigned char* psyms
;
1845 if (this->offset_
== 0 || output_count
== 0)
1848 psyms
= of
->get_output_view(this->offset_
, oview_size
);
1850 const unsigned int dynamic_count
= this->dynamic_count_
;
1851 const section_size_type dynamic_size
= dynamic_count
* sym_size
;
1852 const unsigned int first_dynamic_global_index
=
1853 this->first_dynamic_global_index_
;
1854 unsigned char* dynamic_view
;
1855 if (this->dynamic_offset_
== 0 || dynamic_count
== 0)
1856 dynamic_view
= NULL
;
1858 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
1860 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1861 p
!= this->table_
.end();
1864 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1866 // Possibly warn about unresolved symbols in shared libraries.
1867 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
1869 unsigned int sym_index
= sym
->symtab_index();
1870 unsigned int dynsym_index
;
1871 if (dynamic_view
== NULL
)
1874 dynsym_index
= sym
->dynsym_index();
1876 if (sym_index
== -1U && dynsym_index
== -1U)
1878 // This symbol is not included in the output file.
1883 typename
elfcpp::Elf_types
<size
>::Elf_Addr sym_value
= sym
->value();
1884 typename
elfcpp::Elf_types
<size
>::Elf_Addr dynsym_value
= sym_value
;
1885 switch (sym
->source())
1887 case Symbol::FROM_OBJECT
:
1889 unsigned int in_shndx
= sym
->shndx();
1891 // FIXME: We need some target specific support here.
1892 if (in_shndx
>= elfcpp::SHN_LORESERVE
1893 && in_shndx
!= elfcpp::SHN_ABS
1894 && in_shndx
!= elfcpp::SHN_COMMON
)
1896 gold_error(_("%s: unsupported symbol section 0x%x"),
1897 sym
->demangled_name().c_str(), in_shndx
);
1902 Object
* symobj
= sym
->object();
1903 if (symobj
->is_dynamic())
1905 if (sym
->needs_dynsym_value())
1906 dynsym_value
= target
.dynsym_value(sym
);
1907 shndx
= elfcpp::SHN_UNDEF
;
1909 else if (in_shndx
== elfcpp::SHN_UNDEF
1910 || in_shndx
== elfcpp::SHN_ABS
1911 || in_shndx
== elfcpp::SHN_COMMON
)
1915 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1916 section_offset_type secoff
;
1917 Output_section
* os
= relobj
->output_section(in_shndx
,
1919 gold_assert(os
!= NULL
);
1920 shndx
= os
->out_shndx();
1922 // In object files symbol values are section
1924 if (parameters
->options().relocatable())
1925 sym_value
-= os
->address();
1931 case Symbol::IN_OUTPUT_DATA
:
1932 shndx
= sym
->output_data()->out_shndx();
1935 case Symbol::IN_OUTPUT_SEGMENT
:
1936 shndx
= elfcpp::SHN_ABS
;
1939 case Symbol::CONSTANT
:
1940 shndx
= elfcpp::SHN_ABS
;
1947 if (sym_index
!= -1U)
1949 sym_index
-= first_global_index
;
1950 gold_assert(sym_index
< output_count
);
1951 unsigned char* ps
= psyms
+ (sym_index
* sym_size
);
1952 this->sized_write_symbol
<size
, big_endian
>(sym
, sym_value
, shndx
,
1956 if (dynsym_index
!= -1U)
1958 dynsym_index
-= first_dynamic_global_index
;
1959 gold_assert(dynsym_index
< dynamic_count
);
1960 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
1961 this->sized_write_symbol
<size
, big_endian
>(sym
, dynsym_value
, shndx
,
1966 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1967 if (dynamic_view
!= NULL
)
1968 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
1971 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1972 // strtab holding the name.
1974 template<int size
, bool big_endian
>
1976 Symbol_table::sized_write_symbol(
1977 Sized_symbol
<size
>* sym
,
1978 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1980 const Stringpool
* pool
,
1981 unsigned char* p
) const
1983 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
1984 osym
.put_st_name(pool
->get_offset(sym
->name()));
1985 osym
.put_st_value(value
);
1986 osym
.put_st_size(sym
->symsize());
1987 // A version script may have overridden the default binding.
1988 if (sym
->is_forced_local())
1989 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
, sym
->type()));
1991 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1992 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
1993 osym
.put_st_shndx(shndx
);
1996 // Check for unresolved symbols in shared libraries. This is
1997 // controlled by the --allow-shlib-undefined option.
1999 // We only warn about libraries for which we have seen all the
2000 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2001 // which were not seen in this link. If we didn't see a DT_NEEDED
2002 // entry, we aren't going to be able to reliably report whether the
2003 // symbol is undefined.
2005 // We also don't warn about libraries found in the system library
2006 // directory (the directory were we find libc.so); we assume that
2007 // those libraries are OK. This heuristic avoids problems in
2008 // GNU/Linux, in which -ldl can have undefined references satisfied by
2012 Symbol_table::warn_about_undefined_dynobj_symbol(
2013 const Input_objects
* input_objects
,
2016 if (sym
->source() == Symbol::FROM_OBJECT
2017 && sym
->object()->is_dynamic()
2018 && sym
->shndx() == elfcpp::SHN_UNDEF
2019 && sym
->binding() != elfcpp::STB_WEAK
2020 && !parameters
->options().allow_shlib_undefined()
2021 && !parameters
->target().is_defined_by_abi(sym
)
2022 && !input_objects
->found_in_system_library_directory(sym
->object()))
2024 // A very ugly cast.
2025 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
2026 if (!dynobj
->has_unknown_needed_entries())
2027 gold_error(_("%s: undefined reference to '%s'"),
2028 sym
->object()->name().c_str(),
2029 sym
->demangled_name().c_str());
2033 // Write out a section symbol. Return the update offset.
2036 Symbol_table::write_section_symbol(const Output_section
*os
,
2040 switch (parameters
->size_and_endianness())
2042 #ifdef HAVE_TARGET_32_LITTLE
2043 case Parameters::TARGET_32_LITTLE
:
2044 this->sized_write_section_symbol
<32, false>(os
, of
, offset
);
2047 #ifdef HAVE_TARGET_32_BIG
2048 case Parameters::TARGET_32_BIG
:
2049 this->sized_write_section_symbol
<32, true>(os
, of
, offset
);
2052 #ifdef HAVE_TARGET_64_LITTLE
2053 case Parameters::TARGET_64_LITTLE
:
2054 this->sized_write_section_symbol
<64, false>(os
, of
, offset
);
2057 #ifdef HAVE_TARGET_64_BIG
2058 case Parameters::TARGET_64_BIG
:
2059 this->sized_write_section_symbol
<64, true>(os
, of
, offset
);
2067 // Write out a section symbol, specialized for size and endianness.
2069 template<int size
, bool big_endian
>
2071 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
2075 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
2077 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
2079 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
2080 osym
.put_st_name(0);
2081 osym
.put_st_value(os
->address());
2082 osym
.put_st_size(0);
2083 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
2084 elfcpp::STT_SECTION
));
2085 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
2086 osym
.put_st_shndx(os
->out_shndx());
2088 of
->write_output_view(offset
, sym_size
, pov
);
2091 // Print statistical information to stderr. This is used for --stats.
2094 Symbol_table::print_stats() const
2096 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2097 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2098 program_name
, this->table_
.size(), this->table_
.bucket_count());
2100 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
2101 program_name
, this->table_
.size());
2103 this->namepool_
.print_stats("symbol table stringpool");
2106 // We check for ODR violations by looking for symbols with the same
2107 // name for which the debugging information reports that they were
2108 // defined in different source locations. When comparing the source
2109 // location, we consider instances with the same base filename and
2110 // line number to be the same. This is because different object
2111 // files/shared libraries can include the same header file using
2112 // different paths, and we don't want to report an ODR violation in
2115 // This struct is used to compare line information, as returned by
2116 // Dwarf_line_info::one_addr2line. It implements a < comparison
2117 // operator used with std::set.
2119 struct Odr_violation_compare
2122 operator()(const std::string
& s1
, const std::string
& s2
) const
2124 std::string::size_type pos1
= s1
.rfind('/');
2125 std::string::size_type pos2
= s2
.rfind('/');
2126 if (pos1
== std::string::npos
2127 || pos2
== std::string::npos
)
2129 return s1
.compare(pos1
, std::string::npos
,
2130 s2
, pos2
, std::string::npos
) < 0;
2134 // Check candidate_odr_violations_ to find symbols with the same name
2135 // but apparently different definitions (different source-file/line-no).
2138 Symbol_table::detect_odr_violations(const Task
* task
,
2139 const char* output_file_name
) const
2141 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
2142 it
!= candidate_odr_violations_
.end();
2145 const char* symbol_name
= it
->first
;
2146 // We use a sorted set so the output is deterministic.
2147 std::set
<std::string
, Odr_violation_compare
> line_nums
;
2149 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
2150 locs
= it
->second
.begin();
2151 locs
!= it
->second
.end();
2154 // We need to lock the object in order to read it. This
2155 // means that we have to run in a singleton Task. If we
2156 // want to run this in a general Task for better
2157 // performance, we will need one Task for object, plus
2158 // appropriate locking to ensure that we don't conflict with
2159 // other uses of the object.
2160 Task_lock_obj
<Object
> tl(task
, locs
->object
);
2161 std::string lineno
= Dwarf_line_info::one_addr2line(
2162 locs
->object
, locs
->shndx
, locs
->offset
);
2163 if (!lineno
.empty())
2164 line_nums
.insert(lineno
);
2167 if (line_nums
.size() > 1)
2169 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2170 "places (possible ODR violation):"),
2171 output_file_name
, demangle(symbol_name
).c_str());
2172 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
2173 it2
!= line_nums
.end();
2175 fprintf(stderr
, " %s\n", it2
->c_str());
2180 // Warnings functions.
2182 // Add a new warning.
2185 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
2186 const std::string
& warning
)
2188 name
= symtab
->canonicalize_name(name
);
2189 this->warnings_
[name
].set(obj
, warning
);
2192 // Look through the warnings and mark the symbols for which we should
2193 // warn. This is called during Layout::finalize when we know the
2194 // sources for all the symbols.
2197 Warnings::note_warnings(Symbol_table
* symtab
)
2199 for (Warning_table::iterator p
= this->warnings_
.begin();
2200 p
!= this->warnings_
.end();
2203 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2205 && sym
->source() == Symbol::FROM_OBJECT
2206 && sym
->object() == p
->second
.object
)
2207 sym
->set_has_warning();
2211 // Issue a warning. This is called when we see a relocation against a
2212 // symbol for which has a warning.
2214 template<int size
, bool big_endian
>
2216 Warnings::issue_warning(const Symbol
* sym
,
2217 const Relocate_info
<size
, big_endian
>* relinfo
,
2218 size_t relnum
, off_t reloffset
) const
2220 gold_assert(sym
->has_warning());
2221 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2222 gold_assert(p
!= this->warnings_
.end());
2223 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2224 "%s", p
->second
.text
.c_str());
2227 // Instantiate the templates we need. We could use the configure
2228 // script to restrict this to only the ones needed for implemented
2231 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2234 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2237 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2240 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2243 #ifdef HAVE_TARGET_32_LITTLE
2246 Symbol_table::add_from_relobj
<32, false>(
2247 Sized_relobj
<32, false>* relobj
,
2248 const unsigned char* syms
,
2250 const char* sym_names
,
2251 size_t sym_name_size
,
2252 Sized_relobj
<32, true>::Symbols
* sympointers
);
2255 #ifdef HAVE_TARGET_32_BIG
2258 Symbol_table::add_from_relobj
<32, true>(
2259 Sized_relobj
<32, true>* relobj
,
2260 const unsigned char* syms
,
2262 const char* sym_names
,
2263 size_t sym_name_size
,
2264 Sized_relobj
<32, false>::Symbols
* sympointers
);
2267 #ifdef HAVE_TARGET_64_LITTLE
2270 Symbol_table::add_from_relobj
<64, false>(
2271 Sized_relobj
<64, false>* relobj
,
2272 const unsigned char* syms
,
2274 const char* sym_names
,
2275 size_t sym_name_size
,
2276 Sized_relobj
<64, true>::Symbols
* sympointers
);
2279 #ifdef HAVE_TARGET_64_BIG
2282 Symbol_table::add_from_relobj
<64, true>(
2283 Sized_relobj
<64, true>* relobj
,
2284 const unsigned char* syms
,
2286 const char* sym_names
,
2287 size_t sym_name_size
,
2288 Sized_relobj
<64, false>::Symbols
* sympointers
);
2291 #ifdef HAVE_TARGET_32_LITTLE
2294 Symbol_table::add_from_dynobj
<32, false>(
2295 Sized_dynobj
<32, false>* dynobj
,
2296 const unsigned char* syms
,
2298 const char* sym_names
,
2299 size_t sym_name_size
,
2300 const unsigned char* versym
,
2302 const std::vector
<const char*>* version_map
);
2305 #ifdef HAVE_TARGET_32_BIG
2308 Symbol_table::add_from_dynobj
<32, true>(
2309 Sized_dynobj
<32, true>* dynobj
,
2310 const unsigned char* syms
,
2312 const char* sym_names
,
2313 size_t sym_name_size
,
2314 const unsigned char* versym
,
2316 const std::vector
<const char*>* version_map
);
2319 #ifdef HAVE_TARGET_64_LITTLE
2322 Symbol_table::add_from_dynobj
<64, false>(
2323 Sized_dynobj
<64, false>* dynobj
,
2324 const unsigned char* syms
,
2326 const char* sym_names
,
2327 size_t sym_name_size
,
2328 const unsigned char* versym
,
2330 const std::vector
<const char*>* version_map
);
2333 #ifdef HAVE_TARGET_64_BIG
2336 Symbol_table::add_from_dynobj
<64, true>(
2337 Sized_dynobj
<64, true>* dynobj
,
2338 const unsigned char* syms
,
2340 const char* sym_names
,
2341 size_t sym_name_size
,
2342 const unsigned char* versym
,
2344 const std::vector
<const char*>* version_map
);
2347 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2350 Symbol_table::define_with_copy_reloc
<32>(
2351 Sized_symbol
<32>* sym
,
2353 elfcpp::Elf_types
<32>::Elf_Addr value
);
2356 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2359 Symbol_table::define_with_copy_reloc
<64>(
2360 Sized_symbol
<64>* sym
,
2362 elfcpp::Elf_types
<64>::Elf_Addr value
);
2365 #ifdef HAVE_TARGET_32_LITTLE
2368 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2369 const Relocate_info
<32, false>* relinfo
,
2370 size_t relnum
, off_t reloffset
) const;
2373 #ifdef HAVE_TARGET_32_BIG
2376 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2377 const Relocate_info
<32, true>* relinfo
,
2378 size_t relnum
, off_t reloffset
) const;
2381 #ifdef HAVE_TARGET_64_LITTLE
2384 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2385 const Relocate_info
<64, false>* relinfo
,
2386 size_t relnum
, off_t reloffset
) const;
2389 #ifdef HAVE_TARGET_64_BIG
2392 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2393 const Relocate_info
<64, true>* relinfo
,
2394 size_t relnum
, off_t reloffset
) const;
2397 } // End namespace gold.