1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007 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;
75 // Return the demangled version of the symbol's name, but only
76 // if the --demangle flag was set.
79 demangle(const char* name
)
81 if (!parameters
->demangle())
84 // cplus_demangle allocates memory for the result it returns,
85 // and returns NULL if the name is already demangled.
86 char* demangled_name
= cplus_demangle(name
, DMGL_ANSI
| DMGL_PARAMS
);
87 if (demangled_name
== NULL
)
90 std::string
retval(demangled_name
);
96 Symbol::demangled_name() const
98 return demangle(this->name());
101 // Initialize the fields in the base class Symbol for SYM in OBJECT.
103 template<int size
, bool big_endian
>
105 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
106 const elfcpp::Sym
<size
, big_endian
>& sym
)
108 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
109 sym
.get_st_visibility(), sym
.get_st_nonvis());
110 this->u_
.from_object
.object
= object
;
111 // FIXME: Handle SHN_XINDEX.
112 this->u_
.from_object
.shndx
= sym
.get_st_shndx();
113 this->source_
= FROM_OBJECT
;
114 this->in_reg_
= !object
->is_dynamic();
115 this->in_dyn_
= object
->is_dynamic();
118 // Initialize the fields in the base class Symbol for a symbol defined
119 // in an Output_data.
122 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
123 elfcpp::STB binding
, elfcpp::STV visibility
,
124 unsigned char nonvis
, bool offset_is_from_end
)
126 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
127 this->u_
.in_output_data
.output_data
= od
;
128 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
129 this->source_
= IN_OUTPUT_DATA
;
130 this->in_reg_
= true;
133 // Initialize the fields in the base class Symbol for a symbol defined
134 // in an Output_segment.
137 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
138 elfcpp::STB binding
, elfcpp::STV visibility
,
139 unsigned char nonvis
, Segment_offset_base offset_base
)
141 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
142 this->u_
.in_output_segment
.output_segment
= os
;
143 this->u_
.in_output_segment
.offset_base
= offset_base
;
144 this->source_
= IN_OUTPUT_SEGMENT
;
145 this->in_reg_
= true;
148 // Initialize the fields in the base class Symbol for a symbol defined
152 Symbol::init_base(const char* name
, elfcpp::STT type
,
153 elfcpp::STB binding
, elfcpp::STV visibility
,
154 unsigned char nonvis
)
156 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
157 this->source_
= CONSTANT
;
158 this->in_reg_
= true;
161 // Allocate a common symbol in the base.
164 Symbol::allocate_base_common(Output_data
* od
)
166 gold_assert(this->is_common());
167 this->source_
= IN_OUTPUT_DATA
;
168 this->u_
.in_output_data
.output_data
= od
;
169 this->u_
.in_output_data
.offset_is_from_end
= false;
172 // Initialize the fields in Sized_symbol for SYM in OBJECT.
175 template<bool big_endian
>
177 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
178 const elfcpp::Sym
<size
, big_endian
>& sym
)
180 this->init_base(name
, version
, object
, sym
);
181 this->value_
= sym
.get_st_value();
182 this->symsize_
= sym
.get_st_size();
185 // Initialize the fields in Sized_symbol for a symbol defined in an
190 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
191 Value_type value
, Size_type symsize
,
192 elfcpp::STT type
, elfcpp::STB binding
,
193 elfcpp::STV visibility
, unsigned char nonvis
,
194 bool offset_is_from_end
)
196 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
198 this->value_
= value
;
199 this->symsize_
= symsize
;
202 // Initialize the fields in Sized_symbol for a symbol defined in an
207 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
208 Value_type value
, Size_type symsize
,
209 elfcpp::STT type
, elfcpp::STB binding
,
210 elfcpp::STV visibility
, unsigned char nonvis
,
211 Segment_offset_base offset_base
)
213 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
214 this->value_
= value
;
215 this->symsize_
= symsize
;
218 // Initialize the fields in Sized_symbol for a symbol defined as a
223 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
224 elfcpp::STT type
, elfcpp::STB binding
,
225 elfcpp::STV visibility
, unsigned char nonvis
)
227 this->init_base(name
, type
, binding
, visibility
, nonvis
);
228 this->value_
= value
;
229 this->symsize_
= symsize
;
232 // Allocate a common symbol.
236 Sized_symbol
<size
>::allocate_common(Output_data
* od
, Value_type value
)
238 this->allocate_base_common(od
);
239 this->value_
= value
;
242 // Return true if this symbol should be added to the dynamic symbol
246 Symbol::should_add_dynsym_entry() const
248 // If the symbol is used by a dynamic relocation, we need to add it.
249 if (this->needs_dynsym_entry())
252 // If exporting all symbols or building a shared library,
253 // and the symbol is defined in a regular object and is
254 // externally visible, we need to add it.
255 if ((parameters
->export_dynamic() || parameters
->output_is_shared())
256 && !this->is_from_dynobj()
257 && this->is_externally_visible())
263 // Return true if the final value of this symbol is known at link
267 Symbol::final_value_is_known() const
269 // If we are not generating an executable, then no final values are
270 // known, since they will change at runtime.
271 if (!parameters
->output_is_executable())
274 // If the symbol is not from an object file, then it is defined, and
276 if (this->source_
!= FROM_OBJECT
)
279 // If the symbol is from a dynamic object, then the final value is
281 if (this->object()->is_dynamic())
284 // If the symbol is not undefined (it is defined or common), then
285 // the final value is known.
286 if (!this->is_undefined())
289 // If the symbol is undefined, then whether the final value is known
290 // depends on whether we are doing a static link. If we are doing a
291 // dynamic link, then the final value could be filled in at runtime.
292 // This could reasonably be the case for a weak undefined symbol.
293 return parameters
->doing_static_link();
296 // Class Symbol_table.
298 Symbol_table::Symbol_table()
299 : saw_undefined_(0), offset_(0), table_(), namepool_(),
300 forwarders_(), commons_(), warnings_()
304 Symbol_table::~Symbol_table()
308 // The hash function. The key values are Stringpool keys.
311 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
313 return key
.first
^ key
.second
;
316 // The symbol table key equality function. This is called with
320 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
321 const Symbol_table_key
& k2
) const
323 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
326 // Make TO a symbol which forwards to FROM.
329 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
331 gold_assert(from
!= to
);
332 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
333 this->forwarders_
[from
] = to
;
334 from
->set_forwarder();
337 // Resolve the forwards from FROM, returning the real symbol.
340 Symbol_table::resolve_forwards(const Symbol
* from
) const
342 gold_assert(from
->is_forwarder());
343 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
344 this->forwarders_
.find(from
);
345 gold_assert(p
!= this->forwarders_
.end());
349 // Look up a symbol by name.
352 Symbol_table::lookup(const char* name
, const char* version
) const
354 Stringpool::Key name_key
;
355 name
= this->namepool_
.find(name
, &name_key
);
359 Stringpool::Key version_key
= 0;
362 version
= this->namepool_
.find(version
, &version_key
);
367 Symbol_table_key
key(name_key
, version_key
);
368 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
369 if (p
== this->table_
.end())
374 // Resolve a Symbol with another Symbol. This is only used in the
375 // unusual case where there are references to both an unversioned
376 // symbol and a symbol with a version, and we then discover that that
377 // version is the default version. Because this is unusual, we do
378 // this the slow way, by converting back to an ELF symbol.
380 template<int size
, bool big_endian
>
382 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
383 const char* version ACCEPT_SIZE_ENDIAN
)
385 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
386 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
387 // We don't bother to set the st_name field.
388 esym
.put_st_value(from
->value());
389 esym
.put_st_size(from
->symsize());
390 esym
.put_st_info(from
->binding(), from
->type());
391 esym
.put_st_other(from
->visibility(), from
->nonvis());
392 esym
.put_st_shndx(from
->shndx());
393 this->resolve(to
, esym
.sym(), esym
.sym(), from
->object(), version
);
400 // Add one symbol from OBJECT to the symbol table. NAME is symbol
401 // name and VERSION is the version; both are canonicalized. DEF is
402 // whether this is the default version.
404 // If DEF is true, then this is the definition of a default version of
405 // a symbol. That means that any lookup of NAME/NULL and any lookup
406 // of NAME/VERSION should always return the same symbol. This is
407 // obvious for references, but in particular we want to do this for
408 // definitions: overriding NAME/NULL should also override
409 // NAME/VERSION. If we don't do that, it would be very hard to
410 // override functions in a shared library which uses versioning.
412 // We implement this by simply making both entries in the hash table
413 // point to the same Symbol structure. That is easy enough if this is
414 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
415 // that we have seen both already, in which case they will both have
416 // independent entries in the symbol table. We can't simply change
417 // the symbol table entry, because we have pointers to the entries
418 // attached to the object files. So we mark the entry attached to the
419 // object file as a forwarder, and record it in the forwarders_ map.
420 // Note that entries in the hash table will never be marked as
423 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
424 // symbol exactly as it existed in the input file. SYM is usually
425 // that as well, but can be modified, for instance if we determine
426 // it's in a to-be-discarded section.
428 template<int size
, bool big_endian
>
430 Symbol_table::add_from_object(Object
* object
,
432 Stringpool::Key name_key
,
434 Stringpool::Key version_key
,
436 const elfcpp::Sym
<size
, big_endian
>& sym
,
437 const elfcpp::Sym
<size
, big_endian
>& orig_sym
)
439 Symbol
* const snull
= NULL
;
440 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
441 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
444 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
445 std::make_pair(this->table_
.end(), false);
448 const Stringpool::Key vnull_key
= 0;
449 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
454 // ins.first: an iterator, which is a pointer to a pair.
455 // ins.first->first: the key (a pair of name and version).
456 // ins.first->second: the value (Symbol*).
457 // ins.second: true if new entry was inserted, false if not.
459 Sized_symbol
<size
>* ret
;
464 // We already have an entry for NAME/VERSION.
465 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
467 gold_assert(ret
!= NULL
);
469 was_undefined
= ret
->is_undefined();
470 was_common
= ret
->is_common();
472 this->resolve(ret
, sym
, orig_sym
, object
, version
);
478 // This is the first time we have seen NAME/NULL. Make
479 // NAME/NULL point to NAME/VERSION.
480 insdef
.first
->second
= ret
;
482 else if (insdef
.first
->second
!= ret
)
484 // This is the unfortunate case where we already have
485 // entries for both NAME/VERSION and NAME/NULL.
486 const Sized_symbol
<size
>* sym2
;
487 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
490 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
491 ret
, sym2
, version
SELECT_SIZE_ENDIAN(size
, big_endian
));
492 this->make_forwarder(insdef
.first
->second
, ret
);
493 insdef
.first
->second
= ret
;
499 // This is the first time we have seen NAME/VERSION.
500 gold_assert(ins
.first
->second
== NULL
);
502 was_undefined
= false;
505 if (def
&& !insdef
.second
)
507 // We already have an entry for NAME/NULL. If we override
508 // it, then change it to NAME/VERSION.
509 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
512 this->resolve(ret
, sym
, orig_sym
, object
, version
);
513 ins
.first
->second
= ret
;
517 Sized_target
<size
, big_endian
>* target
=
518 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
519 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
520 if (!target
->has_make_symbol())
521 ret
= new Sized_symbol
<size
>();
524 ret
= target
->make_symbol();
527 // This means that we don't want a symbol table
530 this->table_
.erase(ins
.first
);
533 this->table_
.erase(insdef
.first
);
534 // Inserting insdef invalidated ins.
535 this->table_
.erase(std::make_pair(name_key
,
542 ret
->init(name
, version
, object
, sym
);
544 ins
.first
->second
= ret
;
547 // This is the first time we have seen NAME/NULL. Point
548 // it at the new entry for NAME/VERSION.
549 gold_assert(insdef
.second
);
550 insdef
.first
->second
= ret
;
555 // Record every time we see a new undefined symbol, to speed up
557 if (!was_undefined
&& ret
->is_undefined())
558 ++this->saw_undefined_
;
560 // Keep track of common symbols, to speed up common symbol
562 if (!was_common
&& ret
->is_common())
563 this->commons_
.push_back(ret
);
568 // Add all the symbols in a relocatable object to the hash table.
570 template<int size
, bool big_endian
>
572 Symbol_table::add_from_relobj(
573 Sized_relobj
<size
, big_endian
>* relobj
,
574 const unsigned char* syms
,
576 const char* sym_names
,
577 size_t sym_name_size
,
578 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
)
580 gold_assert(size
== relobj
->target()->get_size());
581 gold_assert(size
== parameters
->get_size());
583 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
585 const unsigned char* p
= syms
;
586 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
588 elfcpp::Sym
<size
, big_endian
> sym(p
);
589 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
591 unsigned int st_name
= psym
->get_st_name();
592 if (st_name
>= sym_name_size
)
594 relobj
->error(_("bad global symbol name offset %u at %zu"),
599 const char* name
= sym_names
+ st_name
;
601 // A symbol defined in a section which we are not including must
602 // be treated as an undefined symbol.
603 unsigned char symbuf
[sym_size
];
604 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
605 unsigned int st_shndx
= psym
->get_st_shndx();
606 if (st_shndx
!= elfcpp::SHN_UNDEF
607 && st_shndx
< elfcpp::SHN_LORESERVE
608 && !relobj
->is_section_included(st_shndx
))
610 memcpy(symbuf
, p
, sym_size
);
611 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
612 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
616 // In an object file, an '@' in the name separates the symbol
617 // name from the version name. If there are two '@' characters,
618 // this is the default version.
619 const char* ver
= strchr(name
, '@');
621 Sized_symbol
<size
>* res
;
624 Stringpool::Key name_key
;
625 name
= this->namepool_
.add(name
, true, &name_key
);
626 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
631 Stringpool::Key name_key
;
632 name
= this->namepool_
.add_prefix(name
, ver
- name
, &name_key
);
642 Stringpool::Key ver_key
;
643 ver
= this->namepool_
.add(ver
, true, &ver_key
);
645 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
649 (*sympointers
)[i
] = res
;
653 // Add all the symbols in a dynamic object to the hash table.
655 template<int size
, bool big_endian
>
657 Symbol_table::add_from_dynobj(
658 Sized_dynobj
<size
, big_endian
>* dynobj
,
659 const unsigned char* syms
,
661 const char* sym_names
,
662 size_t sym_name_size
,
663 const unsigned char* versym
,
665 const std::vector
<const char*>* version_map
)
667 gold_assert(size
== dynobj
->target()->get_size());
668 gold_assert(size
== parameters
->get_size());
670 if (versym
!= NULL
&& versym_size
/ 2 < count
)
672 dynobj
->error(_("too few symbol versions"));
676 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
678 // We keep a list of all STT_OBJECT symbols, so that we can resolve
679 // weak aliases. This is necessary because if the dynamic object
680 // provides the same variable under two names, one of which is a
681 // weak definition, and the regular object refers to the weak
682 // definition, we have to put both the weak definition and the
683 // strong definition into the dynamic symbol table. Given a weak
684 // definition, the only way that we can find the corresponding
685 // strong definition, if any, is to search the symbol table.
686 std::vector
<Sized_symbol
<size
>*> object_symbols
;
688 const unsigned char* p
= syms
;
689 const unsigned char* vs
= versym
;
690 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
692 elfcpp::Sym
<size
, big_endian
> sym(p
);
694 // Ignore symbols with local binding.
695 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
698 unsigned int st_name
= sym
.get_st_name();
699 if (st_name
>= sym_name_size
)
701 dynobj
->error(_("bad symbol name offset %u at %zu"),
706 const char* name
= sym_names
+ st_name
;
708 Sized_symbol
<size
>* res
;
712 Stringpool::Key name_key
;
713 name
= this->namepool_
.add(name
, true, &name_key
);
714 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
719 // Read the version information.
721 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
723 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
724 v
&= elfcpp::VERSYM_VERSION
;
726 // The Sun documentation says that V can be VER_NDX_LOCAL,
727 // or VER_NDX_GLOBAL, or a version index. The meaning of
728 // VER_NDX_LOCAL is defined as "Symbol has local scope."
729 // The old GNU linker will happily generate VER_NDX_LOCAL
730 // for an undefined symbol. I don't know what the Sun
731 // linker will generate.
733 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
734 && sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
736 // This symbol should not be visible outside the object.
740 // At this point we are definitely going to add this symbol.
741 Stringpool::Key name_key
;
742 name
= this->namepool_
.add(name
, true, &name_key
);
744 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
745 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
747 // This symbol does not have a version.
748 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
753 if (v
>= version_map
->size())
755 dynobj
->error(_("versym for symbol %zu out of range: %u"),
760 const char* version
= (*version_map
)[v
];
763 dynobj
->error(_("versym for symbol %zu has no name: %u"),
768 Stringpool::Key version_key
;
769 version
= this->namepool_
.add(version
, true, &version_key
);
771 // If this is an absolute symbol, and the version name
772 // and symbol name are the same, then this is the
773 // version definition symbol. These symbols exist to
774 // support using -u to pull in particular versions. We
775 // do not want to record a version for them.
776 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
777 && name_key
== version_key
)
778 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
782 const bool def
= (!hidden
783 && (sym
.get_st_shndx()
784 != elfcpp::SHN_UNDEF
));
785 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
786 version_key
, def
, sym
, sym
);
791 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
792 && sym
.get_st_type() == elfcpp::STT_OBJECT
)
793 object_symbols
.push_back(res
);
796 this->record_weak_aliases(&object_symbols
);
799 // This is used to sort weak aliases. We sort them first by section
800 // index, then by offset, then by weak ahead of strong.
803 class Weak_alias_sorter
806 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
811 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
812 const Sized_symbol
<size
>* s2
) const
814 if (s1
->shndx() != s2
->shndx())
815 return s1
->shndx() < s2
->shndx();
816 if (s1
->value() != s2
->value())
817 return s1
->value() < s2
->value();
818 if (s1
->binding() != s2
->binding())
820 if (s1
->binding() == elfcpp::STB_WEAK
)
822 if (s2
->binding() == elfcpp::STB_WEAK
)
825 return std::string(s1
->name()) < std::string(s2
->name());
828 // SYMBOLS is a list of object symbols from a dynamic object. Look
829 // for any weak aliases, and record them so that if we add the weak
830 // alias to the dynamic symbol table, we also add the corresponding
835 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
837 // Sort the vector by section index, then by offset, then by weak
839 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
841 // Walk through the vector. For each weak definition, record
843 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
848 if ((*p
)->binding() != elfcpp::STB_WEAK
)
851 // Build a circular list of weak aliases. Each symbol points to
852 // the next one in the circular list.
854 Sized_symbol
<size
>* from_sym
= *p
;
855 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
856 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
858 if ((*q
)->shndx() != from_sym
->shndx()
859 || (*q
)->value() != from_sym
->value())
862 this->weak_aliases_
[from_sym
] = *q
;
863 from_sym
->set_has_alias();
869 this->weak_aliases_
[from_sym
] = *p
;
870 from_sym
->set_has_alias();
877 // Create and return a specially defined symbol. If ONLY_IF_REF is
878 // true, then only create the symbol if there is a reference to it.
879 // If this does not return NULL, it sets *POLDSYM to the existing
880 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
882 template<int size
, bool big_endian
>
884 Symbol_table::define_special_symbol(const Target
* target
, const char** pname
,
885 const char** pversion
, bool only_if_ref
,
886 Sized_symbol
<size
>** poldsym
890 Sized_symbol
<size
>* sym
;
891 bool add_to_table
= false;
892 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
896 oldsym
= this->lookup(*pname
, *pversion
);
897 if (oldsym
== NULL
|| !oldsym
->is_undefined())
900 *pname
= oldsym
->name();
901 *pversion
= oldsym
->version();
905 // Canonicalize NAME and VERSION.
906 Stringpool::Key name_key
;
907 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
909 Stringpool::Key version_key
= 0;
910 if (*pversion
!= NULL
)
911 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
913 Symbol
* const snull
= NULL
;
914 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
915 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
921 // We already have a symbol table entry for NAME/VERSION.
922 oldsym
= ins
.first
->second
;
923 gold_assert(oldsym
!= NULL
);
927 // We haven't seen this symbol before.
928 gold_assert(ins
.first
->second
== NULL
);
935 if (!target
->has_make_symbol())
936 sym
= new Sized_symbol
<size
>();
939 gold_assert(target
->get_size() == size
);
940 gold_assert(target
->is_big_endian() ? big_endian
: !big_endian
);
941 typedef Sized_target
<size
, big_endian
> My_target
;
942 const My_target
* sized_target
=
943 static_cast<const My_target
*>(target
);
944 sym
= sized_target
->make_symbol();
950 add_loc
->second
= sym
;
952 gold_assert(oldsym
!= NULL
);
954 *poldsym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
960 // Define a symbol based on an Output_data.
963 Symbol_table::define_in_output_data(const Target
* target
, const char* name
,
964 const char* version
, Output_data
* od
,
965 uint64_t value
, uint64_t symsize
,
966 elfcpp::STT type
, elfcpp::STB binding
,
967 elfcpp::STV visibility
,
968 unsigned char nonvis
,
969 bool offset_is_from_end
,
972 if (parameters
->get_size() == 32)
974 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
975 return this->do_define_in_output_data
<32>(target
, name
, version
, od
,
976 value
, symsize
, type
, binding
,
984 else if (parameters
->get_size() == 64)
986 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
987 return this->do_define_in_output_data
<64>(target
, name
, version
, od
,
988 value
, symsize
, type
, binding
,
1000 // Define a symbol in an Output_data, sized version.
1004 Symbol_table::do_define_in_output_data(
1005 const Target
* target
,
1007 const char* version
,
1009 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1010 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1012 elfcpp::STB binding
,
1013 elfcpp::STV visibility
,
1014 unsigned char nonvis
,
1015 bool offset_is_from_end
,
1018 Sized_symbol
<size
>* sym
;
1019 Sized_symbol
<size
>* oldsym
;
1021 if (parameters
->is_big_endian())
1023 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1024 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1025 target
, &name
, &version
, only_if_ref
, &oldsym
1026 SELECT_SIZE_ENDIAN(size
, true));
1033 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1034 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1035 target
, &name
, &version
, only_if_ref
, &oldsym
1036 SELECT_SIZE_ENDIAN(size
, false));
1045 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1046 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1047 offset_is_from_end
);
1050 && Symbol_table::should_override_with_special(oldsym
))
1051 this->override_with_special(oldsym
, sym
);
1056 // Define a symbol based on an Output_segment.
1059 Symbol_table::define_in_output_segment(const Target
* target
, const char* name
,
1060 const char* version
, Output_segment
* os
,
1061 uint64_t value
, uint64_t symsize
,
1062 elfcpp::STT type
, elfcpp::STB binding
,
1063 elfcpp::STV visibility
,
1064 unsigned char nonvis
,
1065 Symbol::Segment_offset_base offset_base
,
1068 if (parameters
->get_size() == 32)
1070 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1071 return this->do_define_in_output_segment
<32>(target
, name
, version
, os
,
1072 value
, symsize
, type
,
1073 binding
, visibility
, nonvis
,
1074 offset_base
, only_if_ref
);
1079 else if (parameters
->get_size() == 64)
1081 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1082 return this->do_define_in_output_segment
<64>(target
, name
, version
, os
,
1083 value
, symsize
, type
,
1084 binding
, visibility
, nonvis
,
1085 offset_base
, only_if_ref
);
1094 // Define a symbol in an Output_segment, sized version.
1098 Symbol_table::do_define_in_output_segment(
1099 const Target
* target
,
1101 const char* version
,
1103 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1104 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1106 elfcpp::STB binding
,
1107 elfcpp::STV visibility
,
1108 unsigned char nonvis
,
1109 Symbol::Segment_offset_base offset_base
,
1112 Sized_symbol
<size
>* sym
;
1113 Sized_symbol
<size
>* oldsym
;
1115 if (parameters
->is_big_endian())
1117 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1118 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1119 target
, &name
, &version
, only_if_ref
, &oldsym
1120 SELECT_SIZE_ENDIAN(size
, true));
1127 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1128 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1129 target
, &name
, &version
, only_if_ref
, &oldsym
1130 SELECT_SIZE_ENDIAN(size
, false));
1139 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1140 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1144 && Symbol_table::should_override_with_special(oldsym
))
1145 this->override_with_special(oldsym
, sym
);
1150 // Define a special symbol with a constant value. It is a multiple
1151 // definition error if this symbol is already defined.
1154 Symbol_table::define_as_constant(const Target
* target
, const char* name
,
1155 const char* version
, uint64_t value
,
1156 uint64_t symsize
, elfcpp::STT type
,
1157 elfcpp::STB binding
, elfcpp::STV visibility
,
1158 unsigned char nonvis
, bool only_if_ref
)
1160 if (parameters
->get_size() == 32)
1162 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1163 return this->do_define_as_constant
<32>(target
, name
, version
, value
,
1164 symsize
, type
, binding
,
1165 visibility
, nonvis
, only_if_ref
);
1170 else if (parameters
->get_size() == 64)
1172 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1173 return this->do_define_as_constant
<64>(target
, name
, version
, value
,
1174 symsize
, type
, binding
,
1175 visibility
, nonvis
, only_if_ref
);
1184 // Define a symbol as a constant, sized version.
1188 Symbol_table::do_define_as_constant(
1189 const Target
* target
,
1191 const char* version
,
1192 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1193 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1195 elfcpp::STB binding
,
1196 elfcpp::STV visibility
,
1197 unsigned char nonvis
,
1200 Sized_symbol
<size
>* sym
;
1201 Sized_symbol
<size
>* oldsym
;
1203 if (parameters
->is_big_endian())
1205 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1206 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1207 target
, &name
, &version
, only_if_ref
, &oldsym
1208 SELECT_SIZE_ENDIAN(size
, true));
1215 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1216 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1217 target
, &name
, &version
, only_if_ref
, &oldsym
1218 SELECT_SIZE_ENDIAN(size
, false));
1227 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1228 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
1231 && Symbol_table::should_override_with_special(oldsym
))
1232 this->override_with_special(oldsym
, sym
);
1237 // Define a set of symbols in output sections.
1240 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1241 int count
, const Define_symbol_in_section
* p
)
1243 for (int i
= 0; i
< count
; ++i
, ++p
)
1245 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1247 this->define_in_output_data(target
, p
->name
, NULL
, os
, p
->value
,
1248 p
->size
, p
->type
, p
->binding
,
1249 p
->visibility
, p
->nonvis
,
1250 p
->offset_is_from_end
, p
->only_if_ref
);
1252 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1253 p
->binding
, p
->visibility
, p
->nonvis
,
1258 // Define a set of symbols in output segments.
1261 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1262 int count
, const Define_symbol_in_segment
* p
)
1264 for (int i
= 0; i
< count
; ++i
, ++p
)
1266 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1267 p
->segment_flags_set
,
1268 p
->segment_flags_clear
);
1270 this->define_in_output_segment(target
, p
->name
, NULL
, os
, p
->value
,
1271 p
->size
, p
->type
, p
->binding
,
1272 p
->visibility
, p
->nonvis
,
1273 p
->offset_base
, p
->only_if_ref
);
1275 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1276 p
->binding
, p
->visibility
, p
->nonvis
,
1281 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1282 // symbol should be defined--typically a .dyn.bss section. VALUE is
1283 // the offset within POSD.
1287 Symbol_table::define_with_copy_reloc(const Target
* target
,
1288 Sized_symbol
<size
>* csym
,
1289 Output_data
* posd
, uint64_t value
)
1291 gold_assert(csym
->is_from_dynobj());
1292 gold_assert(!csym
->is_copied_from_dynobj());
1293 Object
* object
= csym
->object();
1294 gold_assert(object
->is_dynamic());
1295 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1297 // Our copied variable has to override any variable in a shared
1299 elfcpp::STB binding
= csym
->binding();
1300 if (binding
== elfcpp::STB_WEAK
)
1301 binding
= elfcpp::STB_GLOBAL
;
1303 this->define_in_output_data(target
, csym
->name(), csym
->version(),
1304 posd
, value
, csym
->symsize(),
1305 csym
->type(), binding
,
1306 csym
->visibility(), csym
->nonvis(),
1309 csym
->set_is_copied_from_dynobj();
1310 csym
->set_needs_dynsym_entry();
1312 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1314 // We have now defined all aliases, but we have not entered them all
1315 // in the copied_symbol_dynobjs_ map.
1316 if (csym
->has_alias())
1321 sym
= this->weak_aliases_
[sym
];
1324 gold_assert(sym
->output_data() == posd
);
1326 sym
->set_is_copied_from_dynobj();
1327 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1332 // SYM is defined using a COPY reloc. Return the dynamic object where
1333 // the original definition was found.
1336 Symbol_table::get_copy_source(const Symbol
* sym
) const
1338 gold_assert(sym
->is_copied_from_dynobj());
1339 Copied_symbol_dynobjs::const_iterator p
=
1340 this->copied_symbol_dynobjs_
.find(sym
);
1341 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1345 // Set the dynamic symbol indexes. INDEX is the index of the first
1346 // global dynamic symbol. Pointers to the symbols are stored into the
1347 // vector SYMS. The names are added to DYNPOOL. This returns an
1348 // updated dynamic symbol index.
1351 Symbol_table::set_dynsym_indexes(const Target
* target
,
1353 std::vector
<Symbol
*>* syms
,
1354 Stringpool
* dynpool
,
1357 for (Symbol_table_type::iterator p
= this->table_
.begin();
1358 p
!= this->table_
.end();
1361 Symbol
* sym
= p
->second
;
1363 // Note that SYM may already have a dynamic symbol index, since
1364 // some symbols appear more than once in the symbol table, with
1365 // and without a version.
1367 if (!sym
->should_add_dynsym_entry())
1368 sym
->set_dynsym_index(-1U);
1369 else if (!sym
->has_dynsym_index())
1371 sym
->set_dynsym_index(index
);
1373 syms
->push_back(sym
);
1374 dynpool
->add(sym
->name(), false, NULL
);
1376 // Record any version information.
1377 if (sym
->version() != NULL
)
1378 versions
->record_version(this, dynpool
, sym
);
1382 // Finish up the versions. In some cases this may add new dynamic
1384 index
= versions
->finalize(target
, this, index
, syms
);
1389 // Set the final values for all the symbols. The index of the first
1390 // global symbol in the output file is INDEX. Record the file offset
1391 // OFF. Add their names to POOL. Return the new file offset.
1394 Symbol_table::finalize(unsigned int index
, off_t off
, off_t dynoff
,
1395 size_t dyn_global_index
, size_t dyncount
,
1400 gold_assert(index
!= 0);
1401 this->first_global_index_
= index
;
1403 this->dynamic_offset_
= dynoff
;
1404 this->first_dynamic_global_index_
= dyn_global_index
;
1405 this->dynamic_count_
= dyncount
;
1407 if (parameters
->get_size() == 32)
1409 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1410 ret
= this->sized_finalize
<32>(index
, off
, pool
);
1415 else if (parameters
->get_size() == 64)
1417 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1418 ret
= this->sized_finalize
<64>(index
, off
, pool
);
1426 // Now that we have the final symbol table, we can reliably note
1427 // which symbols should get warnings.
1428 this->warnings_
.note_warnings(this);
1433 // Set the final value for all the symbols. This is called after
1434 // Layout::finalize, so all the output sections have their final
1439 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
1441 off
= align_address(off
, size
>> 3);
1442 this->offset_
= off
;
1444 size_t orig_index
= index
;
1446 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1447 for (Symbol_table_type::iterator p
= this->table_
.begin();
1448 p
!= this->table_
.end();
1451 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1453 // FIXME: Here we need to decide which symbols should go into
1454 // the output file, based on --strip.
1456 // The default version of a symbol may appear twice in the
1457 // symbol table. We only need to finalize it once.
1458 if (sym
->has_symtab_index())
1463 gold_assert(!sym
->has_symtab_index());
1464 sym
->set_symtab_index(-1U);
1465 gold_assert(sym
->dynsym_index() == -1U);
1469 typename Sized_symbol
<size
>::Value_type value
;
1471 switch (sym
->source())
1473 case Symbol::FROM_OBJECT
:
1475 unsigned int shndx
= sym
->shndx();
1477 // FIXME: We need some target specific support here.
1478 if (shndx
>= elfcpp::SHN_LORESERVE
1479 && shndx
!= elfcpp::SHN_ABS
)
1481 gold_error(_("%s: unsupported symbol section 0x%x"),
1482 sym
->demangled_name().c_str(), shndx
);
1483 shndx
= elfcpp::SHN_UNDEF
;
1486 Object
* symobj
= sym
->object();
1487 if (symobj
->is_dynamic())
1490 shndx
= elfcpp::SHN_UNDEF
;
1492 else if (shndx
== elfcpp::SHN_UNDEF
)
1494 else if (shndx
== elfcpp::SHN_ABS
)
1495 value
= sym
->value();
1498 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1500 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1504 sym
->set_symtab_index(-1U);
1505 gold_assert(sym
->dynsym_index() == -1U);
1509 if (sym
->type() == elfcpp::STT_TLS
)
1510 value
= sym
->value() + os
->tls_offset() + secoff
;
1512 value
= sym
->value() + os
->address() + secoff
;
1517 case Symbol::IN_OUTPUT_DATA
:
1519 Output_data
* od
= sym
->output_data();
1520 value
= sym
->value() + od
->address();
1521 if (sym
->offset_is_from_end())
1522 value
+= od
->data_size();
1526 case Symbol::IN_OUTPUT_SEGMENT
:
1528 Output_segment
* os
= sym
->output_segment();
1529 value
= sym
->value() + os
->vaddr();
1530 switch (sym
->offset_base())
1532 case Symbol::SEGMENT_START
:
1534 case Symbol::SEGMENT_END
:
1535 value
+= os
->memsz();
1537 case Symbol::SEGMENT_BSS
:
1538 value
+= os
->filesz();
1546 case Symbol::CONSTANT
:
1547 value
= sym
->value();
1554 sym
->set_value(value
);
1556 if (parameters
->strip_all())
1557 sym
->set_symtab_index(-1U);
1560 sym
->set_symtab_index(index
);
1561 pool
->add(sym
->name(), false, NULL
);
1567 this->output_count_
= index
- orig_index
;
1572 // Write out the global symbols.
1575 Symbol_table::write_globals(const Input_objects
* input_objects
,
1576 const Stringpool
* sympool
,
1577 const Stringpool
* dynpool
, Output_file
* of
) const
1579 if (parameters
->get_size() == 32)
1581 if (parameters
->is_big_endian())
1583 #ifdef HAVE_TARGET_32_BIG
1584 this->sized_write_globals
<32, true>(input_objects
, sympool
,
1592 #ifdef HAVE_TARGET_32_LITTLE
1593 this->sized_write_globals
<32, false>(input_objects
, sympool
,
1600 else if (parameters
->get_size() == 64)
1602 if (parameters
->is_big_endian())
1604 #ifdef HAVE_TARGET_64_BIG
1605 this->sized_write_globals
<64, true>(input_objects
, sympool
,
1613 #ifdef HAVE_TARGET_64_LITTLE
1614 this->sized_write_globals
<64, false>(input_objects
, sympool
,
1625 // Write out the global symbols.
1627 template<int size
, bool big_endian
>
1629 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
1630 const Stringpool
* sympool
,
1631 const Stringpool
* dynpool
,
1632 Output_file
* of
) const
1634 const Target
* const target
= input_objects
->target();
1636 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1637 unsigned int index
= this->first_global_index_
;
1638 const off_t oview_size
= this->output_count_
* sym_size
;
1639 unsigned char* const psyms
= of
->get_output_view(this->offset_
, oview_size
);
1641 unsigned int dynamic_count
= this->dynamic_count_
;
1642 off_t dynamic_size
= dynamic_count
* sym_size
;
1643 unsigned int first_dynamic_global_index
= this->first_dynamic_global_index_
;
1644 unsigned char* dynamic_view
;
1645 if (this->dynamic_offset_
== 0)
1646 dynamic_view
= NULL
;
1648 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
1650 unsigned char* ps
= psyms
;
1651 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1652 p
!= this->table_
.end();
1655 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1657 // Possibly warn about unresolved symbols in shared libraries.
1658 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
1660 unsigned int sym_index
= sym
->symtab_index();
1661 unsigned int dynsym_index
;
1662 if (dynamic_view
== NULL
)
1665 dynsym_index
= sym
->dynsym_index();
1667 if (sym_index
== -1U && dynsym_index
== -1U)
1669 // This symbol is not included in the output file.
1673 if (sym_index
== index
)
1675 else if (sym_index
!= -1U)
1677 // We have already seen this symbol, because it has a
1679 gold_assert(sym_index
< index
);
1680 if (dynsym_index
== -1U)
1686 typename
elfcpp::Elf_types
<32>::Elf_Addr value
= sym
->value();
1687 switch (sym
->source())
1689 case Symbol::FROM_OBJECT
:
1691 unsigned int in_shndx
= sym
->shndx();
1693 // FIXME: We need some target specific support here.
1694 if (in_shndx
>= elfcpp::SHN_LORESERVE
1695 && in_shndx
!= elfcpp::SHN_ABS
)
1697 gold_error(_("%s: unsupported symbol section 0x%x"),
1698 sym
->demangled_name().c_str(), in_shndx
);
1703 Object
* symobj
= sym
->object();
1704 if (symobj
->is_dynamic())
1706 if (sym
->needs_dynsym_value())
1707 value
= target
->dynsym_value(sym
);
1708 shndx
= elfcpp::SHN_UNDEF
;
1710 else if (in_shndx
== elfcpp::SHN_UNDEF
1711 || in_shndx
== elfcpp::SHN_ABS
)
1715 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1717 Output_section
* os
= relobj
->output_section(in_shndx
,
1719 gold_assert(os
!= NULL
);
1720 shndx
= os
->out_shndx();
1726 case Symbol::IN_OUTPUT_DATA
:
1727 shndx
= sym
->output_data()->out_shndx();
1730 case Symbol::IN_OUTPUT_SEGMENT
:
1731 shndx
= elfcpp::SHN_ABS
;
1734 case Symbol::CONSTANT
:
1735 shndx
= elfcpp::SHN_ABS
;
1742 if (sym_index
!= -1U)
1744 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1745 sym
, sym
->value(), shndx
, sympool
, ps
1746 SELECT_SIZE_ENDIAN(size
, big_endian
));
1750 if (dynsym_index
!= -1U)
1752 dynsym_index
-= first_dynamic_global_index
;
1753 gold_assert(dynsym_index
< dynamic_count
);
1754 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
1755 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1756 sym
, value
, shndx
, dynpool
, pd
1757 SELECT_SIZE_ENDIAN(size
, big_endian
));
1761 gold_assert(ps
- psyms
== oview_size
);
1763 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1764 if (dynamic_view
!= NULL
)
1765 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
1768 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1769 // strtab holding the name.
1771 template<int size
, bool big_endian
>
1773 Symbol_table::sized_write_symbol(
1774 Sized_symbol
<size
>* sym
,
1775 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1777 const Stringpool
* pool
,
1779 ACCEPT_SIZE_ENDIAN
) const
1781 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
1782 osym
.put_st_name(pool
->get_offset(sym
->name()));
1783 osym
.put_st_value(value
);
1784 osym
.put_st_size(sym
->symsize());
1785 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1786 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
1787 osym
.put_st_shndx(shndx
);
1790 // Check for unresolved symbols in shared libraries. This is
1791 // controlled by the --allow-shlib-undefined option.
1793 // We only warn about libraries for which we have seen all the
1794 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1795 // which were not seen in this link. If we didn't see a DT_NEEDED
1796 // entry, we aren't going to be able to reliably report whether the
1797 // symbol is undefined.
1799 // We also don't warn about libraries found in the system library
1800 // directory (the directory were we find libc.so); we assume that
1801 // those libraries are OK. This heuristic avoids problems in
1802 // GNU/Linux, in which -ldl can have undefined references satisfied by
1806 Symbol_table::warn_about_undefined_dynobj_symbol(
1807 const Input_objects
* input_objects
,
1810 if (sym
->source() == Symbol::FROM_OBJECT
1811 && sym
->object()->is_dynamic()
1812 && sym
->shndx() == elfcpp::SHN_UNDEF
1813 && sym
->binding() != elfcpp::STB_WEAK
1814 && !parameters
->allow_shlib_undefined()
1815 && !input_objects
->target()->is_defined_by_abi(sym
)
1816 && !input_objects
->found_in_system_library_directory(sym
->object()))
1818 // A very ugly cast.
1819 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
1820 if (!dynobj
->has_unknown_needed_entries())
1821 gold_error(_("%s: undefined reference to '%s'"),
1822 sym
->object()->name().c_str(),
1823 sym
->demangled_name().c_str());
1827 // Write out a section symbol. Return the update offset.
1830 Symbol_table::write_section_symbol(const Output_section
*os
,
1834 if (parameters
->get_size() == 32)
1836 if (parameters
->is_big_endian())
1838 #ifdef HAVE_TARGET_32_BIG
1839 this->sized_write_section_symbol
<32, true>(os
, of
, offset
);
1846 #ifdef HAVE_TARGET_32_LITTLE
1847 this->sized_write_section_symbol
<32, false>(os
, of
, offset
);
1853 else if (parameters
->get_size() == 64)
1855 if (parameters
->is_big_endian())
1857 #ifdef HAVE_TARGET_64_BIG
1858 this->sized_write_section_symbol
<64, true>(os
, of
, offset
);
1865 #ifdef HAVE_TARGET_64_LITTLE
1866 this->sized_write_section_symbol
<64, false>(os
, of
, offset
);
1876 // Write out a section symbol, specialized for size and endianness.
1878 template<int size
, bool big_endian
>
1880 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
1884 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1886 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
1888 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
1889 osym
.put_st_name(0);
1890 osym
.put_st_value(os
->address());
1891 osym
.put_st_size(0);
1892 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
1893 elfcpp::STT_SECTION
));
1894 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
1895 osym
.put_st_shndx(os
->out_shndx());
1897 of
->write_output_view(offset
, sym_size
, pov
);
1900 // Print statistical information to stderr. This is used for --stats.
1903 Symbol_table::print_stats() const
1905 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
1906 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
1907 program_name
, this->table_
.size(), this->table_
.bucket_count());
1909 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
1910 program_name
, this->table_
.size());
1912 this->namepool_
.print_stats("symbol table stringpool");
1915 // We check for ODR violations by looking for symbols with the same
1916 // name for which the debugging information reports that they were
1917 // defined in different source locations. When comparing the source
1918 // location, we consider instances with the same base filename and
1919 // line number to be the same. This is because different object
1920 // files/shared libraries can include the same header file using
1921 // different paths, and we don't want to report an ODR violation in
1924 // This struct is used to compare line information, as returned by
1925 // Dwarf_line_info::one_addr2line. It implements a < comparison
1926 // operator used with std::set.
1928 struct Odr_violation_compare
1931 operator()(const std::string
& s1
, const std::string
& s2
) const
1933 std::string::size_type pos1
= s1
.rfind('/');
1934 std::string::size_type pos2
= s2
.rfind('/');
1935 if (pos1
== std::string::npos
1936 || pos2
== std::string::npos
)
1938 return s1
.compare(pos1
, std::string::npos
,
1939 s2
, pos2
, std::string::npos
) < 0;
1943 // Check candidate_odr_violations_ to find symbols with the same name
1944 // but apparently different definitions (different source-file/line-no).
1947 Symbol_table::detect_odr_violations(const char* output_file_name
) const
1949 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
1950 it
!= candidate_odr_violations_
.end();
1953 const char* symbol_name
= it
->first
;
1954 // We use a sorted set so the output is deterministic.
1955 std::set
<std::string
, Odr_violation_compare
> line_nums
;
1957 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
1958 locs
= it
->second
.begin();
1959 locs
!= it
->second
.end();
1962 // We need to lock the object in order to read it. This
1963 // means that we can not run inside a Task. If we want to
1964 // run this in a Task for better performance, we will need
1965 // one Task for object, plus appropriate locking to ensure
1966 // that we don't conflict with other uses of the object.
1967 locs
->object
->lock();
1968 std::string lineno
= Dwarf_line_info::one_addr2line(
1969 locs
->object
, locs
->shndx
, locs
->offset
);
1970 locs
->object
->unlock();
1971 if (!lineno
.empty())
1972 line_nums
.insert(lineno
);
1975 if (line_nums
.size() > 1)
1977 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
1978 "places (possible ODR violation):"),
1979 output_file_name
, demangle(symbol_name
).c_str());
1980 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
1981 it2
!= line_nums
.end();
1983 fprintf(stderr
, " %s\n", it2
->c_str());
1988 // Warnings functions.
1990 // Add a new warning.
1993 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1996 name
= symtab
->canonicalize_name(name
);
1997 this->warnings_
[name
].set(obj
, shndx
);
2000 // Look through the warnings and mark the symbols for which we should
2001 // warn. This is called during Layout::finalize when we know the
2002 // sources for all the symbols.
2005 Warnings::note_warnings(Symbol_table
* symtab
)
2007 for (Warning_table::iterator p
= this->warnings_
.begin();
2008 p
!= this->warnings_
.end();
2011 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2013 && sym
->source() == Symbol::FROM_OBJECT
2014 && sym
->object() == p
->second
.object
)
2016 sym
->set_has_warning();
2018 // Read the section contents to get the warning text. It
2019 // would be nicer if we only did this if we have to actually
2020 // issue a warning. Unfortunately, warnings are issued as
2021 // we relocate sections. That means that we can not lock
2022 // the object then, as we might try to issue the same
2023 // warning multiple times simultaneously.
2025 Task_locker_obj
<Object
> tl(*p
->second
.object
);
2026 const unsigned char* c
;
2028 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
,
2030 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
2036 // Issue a warning. This is called when we see a relocation against a
2037 // symbol for which has a warning.
2039 template<int size
, bool big_endian
>
2041 Warnings::issue_warning(const Symbol
* sym
,
2042 const Relocate_info
<size
, big_endian
>* relinfo
,
2043 size_t relnum
, off_t reloffset
) const
2045 gold_assert(sym
->has_warning());
2046 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2047 gold_assert(p
!= this->warnings_
.end());
2048 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2049 "%s", p
->second
.text
.c_str());
2052 // Instantiate the templates we need. We could use the configure
2053 // script to restrict this to only the ones needed for implemented
2056 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2059 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2062 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2065 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2068 #ifdef HAVE_TARGET_32_LITTLE
2071 Symbol_table::add_from_relobj
<32, false>(
2072 Sized_relobj
<32, false>* relobj
,
2073 const unsigned char* syms
,
2075 const char* sym_names
,
2076 size_t sym_name_size
,
2077 Sized_relobj
<32, true>::Symbols
* sympointers
);
2080 #ifdef HAVE_TARGET_32_BIG
2083 Symbol_table::add_from_relobj
<32, true>(
2084 Sized_relobj
<32, true>* relobj
,
2085 const unsigned char* syms
,
2087 const char* sym_names
,
2088 size_t sym_name_size
,
2089 Sized_relobj
<32, false>::Symbols
* sympointers
);
2092 #ifdef HAVE_TARGET_64_LITTLE
2095 Symbol_table::add_from_relobj
<64, false>(
2096 Sized_relobj
<64, false>* relobj
,
2097 const unsigned char* syms
,
2099 const char* sym_names
,
2100 size_t sym_name_size
,
2101 Sized_relobj
<64, true>::Symbols
* sympointers
);
2104 #ifdef HAVE_TARGET_64_BIG
2107 Symbol_table::add_from_relobj
<64, true>(
2108 Sized_relobj
<64, true>* relobj
,
2109 const unsigned char* syms
,
2111 const char* sym_names
,
2112 size_t sym_name_size
,
2113 Sized_relobj
<64, false>::Symbols
* sympointers
);
2116 #ifdef HAVE_TARGET_32_LITTLE
2119 Symbol_table::add_from_dynobj
<32, false>(
2120 Sized_dynobj
<32, false>* dynobj
,
2121 const unsigned char* syms
,
2123 const char* sym_names
,
2124 size_t sym_name_size
,
2125 const unsigned char* versym
,
2127 const std::vector
<const char*>* version_map
);
2130 #ifdef HAVE_TARGET_32_BIG
2133 Symbol_table::add_from_dynobj
<32, true>(
2134 Sized_dynobj
<32, true>* dynobj
,
2135 const unsigned char* syms
,
2137 const char* sym_names
,
2138 size_t sym_name_size
,
2139 const unsigned char* versym
,
2141 const std::vector
<const char*>* version_map
);
2144 #ifdef HAVE_TARGET_64_LITTLE
2147 Symbol_table::add_from_dynobj
<64, false>(
2148 Sized_dynobj
<64, false>* dynobj
,
2149 const unsigned char* syms
,
2151 const char* sym_names
,
2152 size_t sym_name_size
,
2153 const unsigned char* versym
,
2155 const std::vector
<const char*>* version_map
);
2158 #ifdef HAVE_TARGET_64_BIG
2161 Symbol_table::add_from_dynobj
<64, true>(
2162 Sized_dynobj
<64, true>* dynobj
,
2163 const unsigned char* syms
,
2165 const char* sym_names
,
2166 size_t sym_name_size
,
2167 const unsigned char* versym
,
2169 const std::vector
<const char*>* version_map
);
2172 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2175 Symbol_table::define_with_copy_reloc
<32>(const Target
* target
,
2176 Sized_symbol
<32>* sym
,
2177 Output_data
* posd
, uint64_t value
);
2180 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2183 Symbol_table::define_with_copy_reloc
<64>(const Target
* target
,
2184 Sized_symbol
<64>* sym
,
2185 Output_data
* posd
, uint64_t value
);
2188 #ifdef HAVE_TARGET_32_LITTLE
2191 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2192 const Relocate_info
<32, false>* relinfo
,
2193 size_t relnum
, off_t reloffset
) const;
2196 #ifdef HAVE_TARGET_32_BIG
2199 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2200 const Relocate_info
<32, true>* relinfo
,
2201 size_t relnum
, off_t reloffset
) const;
2204 #ifdef HAVE_TARGET_64_LITTLE
2207 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2208 const Relocate_info
<64, false>* relinfo
,
2209 size_t relnum
, off_t reloffset
) const;
2212 #ifdef HAVE_TARGET_64_BIG
2215 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2216 const Relocate_info
<64, true>* relinfo
,
2217 size_t relnum
, off_t reloffset
) const;
2220 } // End namespace gold.