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;
73 this->needs_value_in_got_
= 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
->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 exporting all symbols or building a shared library,
254 // and the symbol is defined in a regular object and is
255 // externally visible, we need to add it.
256 if ((parameters
->export_dynamic() || parameters
->output_is_shared())
257 && !this->is_from_dynobj()
258 && this->is_externally_visible())
264 // Return true if the final value of this symbol is known at link
268 Symbol::final_value_is_known() const
270 // If we are not generating an executable, then no final values are
271 // known, since they will change at runtime.
272 if (!parameters
->output_is_executable())
275 // If the symbol is not from an object file, then it is defined, and
277 if (this->source_
!= FROM_OBJECT
)
280 // If the symbol is from a dynamic object, then the final value is
282 if (this->object()->is_dynamic())
285 // If the symbol is not undefined (it is defined or common), then
286 // the final value is known.
287 if (!this->is_undefined())
290 // If the symbol is undefined, then whether the final value is known
291 // depends on whether we are doing a static link. If we are doing a
292 // dynamic link, then the final value could be filled in at runtime.
293 // This could reasonably be the case for a weak undefined symbol.
294 return parameters
->doing_static_link();
297 // Class Symbol_table.
299 Symbol_table::Symbol_table()
300 : saw_undefined_(0), offset_(0), table_(), namepool_(),
301 forwarders_(), commons_(), warnings_()
305 Symbol_table::~Symbol_table()
309 // The hash function. The key values are Stringpool keys.
312 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
314 return key
.first
^ key
.second
;
317 // The symbol table key equality function. This is called with
321 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
322 const Symbol_table_key
& k2
) const
324 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
327 // Make TO a symbol which forwards to FROM.
330 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
332 gold_assert(from
!= to
);
333 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
334 this->forwarders_
[from
] = to
;
335 from
->set_forwarder();
338 // Resolve the forwards from FROM, returning the real symbol.
341 Symbol_table::resolve_forwards(const Symbol
* from
) const
343 gold_assert(from
->is_forwarder());
344 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
345 this->forwarders_
.find(from
);
346 gold_assert(p
!= this->forwarders_
.end());
350 // Look up a symbol by name.
353 Symbol_table::lookup(const char* name
, const char* version
) const
355 Stringpool::Key name_key
;
356 name
= this->namepool_
.find(name
, &name_key
);
360 Stringpool::Key version_key
= 0;
363 version
= this->namepool_
.find(version
, &version_key
);
368 Symbol_table_key
key(name_key
, version_key
);
369 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
370 if (p
== this->table_
.end())
375 // Resolve a Symbol with another Symbol. This is only used in the
376 // unusual case where there are references to both an unversioned
377 // symbol and a symbol with a version, and we then discover that that
378 // version is the default version. Because this is unusual, we do
379 // this the slow way, by converting back to an ELF symbol.
381 template<int size
, bool big_endian
>
383 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
384 const char* version ACCEPT_SIZE_ENDIAN
)
386 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
387 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
388 // We don't bother to set the st_name field.
389 esym
.put_st_value(from
->value());
390 esym
.put_st_size(from
->symsize());
391 esym
.put_st_info(from
->binding(), from
->type());
392 esym
.put_st_other(from
->visibility(), from
->nonvis());
393 esym
.put_st_shndx(from
->shndx());
394 this->resolve(to
, esym
.sym(), esym
.sym(), from
->object(), version
);
401 // Add one symbol from OBJECT to the symbol table. NAME is symbol
402 // name and VERSION is the version; both are canonicalized. DEF is
403 // whether this is the default version.
405 // If DEF is true, then this is the definition of a default version of
406 // a symbol. That means that any lookup of NAME/NULL and any lookup
407 // of NAME/VERSION should always return the same symbol. This is
408 // obvious for references, but in particular we want to do this for
409 // definitions: overriding NAME/NULL should also override
410 // NAME/VERSION. If we don't do that, it would be very hard to
411 // override functions in a shared library which uses versioning.
413 // We implement this by simply making both entries in the hash table
414 // point to the same Symbol structure. That is easy enough if this is
415 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
416 // that we have seen both already, in which case they will both have
417 // independent entries in the symbol table. We can't simply change
418 // the symbol table entry, because we have pointers to the entries
419 // attached to the object files. So we mark the entry attached to the
420 // object file as a forwarder, and record it in the forwarders_ map.
421 // Note that entries in the hash table will never be marked as
424 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
425 // symbol exactly as it existed in the input file. SYM is usually
426 // that as well, but can be modified, for instance if we determine
427 // it's in a to-be-discarded section.
429 template<int size
, bool big_endian
>
431 Symbol_table::add_from_object(Object
* object
,
433 Stringpool::Key name_key
,
435 Stringpool::Key version_key
,
437 const elfcpp::Sym
<size
, big_endian
>& sym
,
438 const elfcpp::Sym
<size
, big_endian
>& orig_sym
)
440 Symbol
* const snull
= NULL
;
441 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
442 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
445 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
446 std::make_pair(this->table_
.end(), false);
449 const Stringpool::Key vnull_key
= 0;
450 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
455 // ins.first: an iterator, which is a pointer to a pair.
456 // ins.first->first: the key (a pair of name and version).
457 // ins.first->second: the value (Symbol*).
458 // ins.second: true if new entry was inserted, false if not.
460 Sized_symbol
<size
>* ret
;
465 // We already have an entry for NAME/VERSION.
466 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
468 gold_assert(ret
!= NULL
);
470 was_undefined
= ret
->is_undefined();
471 was_common
= ret
->is_common();
473 this->resolve(ret
, sym
, orig_sym
, object
, version
);
479 // This is the first time we have seen NAME/NULL. Make
480 // NAME/NULL point to NAME/VERSION.
481 insdef
.first
->second
= ret
;
483 else if (insdef
.first
->second
!= ret
)
485 // This is the unfortunate case where we already have
486 // entries for both NAME/VERSION and NAME/NULL.
487 const Sized_symbol
<size
>* sym2
;
488 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
491 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
492 ret
, sym2
, version
SELECT_SIZE_ENDIAN(size
, big_endian
));
493 this->make_forwarder(insdef
.first
->second
, ret
);
494 insdef
.first
->second
= ret
;
500 // This is the first time we have seen NAME/VERSION.
501 gold_assert(ins
.first
->second
== NULL
);
503 was_undefined
= false;
506 if (def
&& !insdef
.second
)
508 // We already have an entry for NAME/NULL. If we override
509 // it, then change it to NAME/VERSION.
510 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
513 this->resolve(ret
, sym
, orig_sym
, object
, version
);
514 ins
.first
->second
= ret
;
518 Sized_target
<size
, big_endian
>* target
=
519 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
520 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
521 if (!target
->has_make_symbol())
522 ret
= new Sized_symbol
<size
>();
525 ret
= target
->make_symbol();
528 // This means that we don't want a symbol table
531 this->table_
.erase(ins
.first
);
534 this->table_
.erase(insdef
.first
);
535 // Inserting insdef invalidated ins.
536 this->table_
.erase(std::make_pair(name_key
,
543 ret
->init(name
, version
, object
, sym
);
545 ins
.first
->second
= ret
;
548 // This is the first time we have seen NAME/NULL. Point
549 // it at the new entry for NAME/VERSION.
550 gold_assert(insdef
.second
);
551 insdef
.first
->second
= ret
;
556 // Record every time we see a new undefined symbol, to speed up
558 if (!was_undefined
&& ret
->is_undefined())
559 ++this->saw_undefined_
;
561 // Keep track of common symbols, to speed up common symbol
563 if (!was_common
&& ret
->is_common())
564 this->commons_
.push_back(ret
);
569 // Add all the symbols in a relocatable object to the hash table.
571 template<int size
, bool big_endian
>
573 Symbol_table::add_from_relobj(
574 Sized_relobj
<size
, big_endian
>* relobj
,
575 const unsigned char* syms
,
577 const char* sym_names
,
578 size_t sym_name_size
,
579 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
)
581 gold_assert(size
== relobj
->target()->get_size());
582 gold_assert(size
== parameters
->get_size());
584 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
586 const unsigned char* p
= syms
;
587 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
589 elfcpp::Sym
<size
, big_endian
> sym(p
);
590 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
592 unsigned int st_name
= psym
->get_st_name();
593 if (st_name
>= sym_name_size
)
595 relobj
->error(_("bad global symbol name offset %u at %zu"),
600 const char* name
= sym_names
+ st_name
;
602 // A symbol defined in a section which we are not including must
603 // be treated as an undefined symbol.
604 unsigned char symbuf
[sym_size
];
605 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
606 unsigned int st_shndx
= psym
->get_st_shndx();
607 if (st_shndx
!= elfcpp::SHN_UNDEF
608 && st_shndx
< elfcpp::SHN_LORESERVE
609 && !relobj
->is_section_included(st_shndx
))
611 memcpy(symbuf
, p
, sym_size
);
612 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
613 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
617 // In an object file, an '@' in the name separates the symbol
618 // name from the version name. If there are two '@' characters,
619 // this is the default version.
620 const char* ver
= strchr(name
, '@');
622 Sized_symbol
<size
>* res
;
625 Stringpool::Key name_key
;
626 name
= this->namepool_
.add(name
, true, &name_key
);
627 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
632 Stringpool::Key name_key
;
633 name
= this->namepool_
.add_prefix(name
, ver
- name
, &name_key
);
643 Stringpool::Key ver_key
;
644 ver
= this->namepool_
.add(ver
, true, &ver_key
);
646 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
650 (*sympointers
)[i
] = res
;
654 // Add all the symbols in a dynamic object to the hash table.
656 template<int size
, bool big_endian
>
658 Symbol_table::add_from_dynobj(
659 Sized_dynobj
<size
, big_endian
>* dynobj
,
660 const unsigned char* syms
,
662 const char* sym_names
,
663 size_t sym_name_size
,
664 const unsigned char* versym
,
666 const std::vector
<const char*>* version_map
)
668 gold_assert(size
== dynobj
->target()->get_size());
669 gold_assert(size
== parameters
->get_size());
671 if (versym
!= NULL
&& versym_size
/ 2 < count
)
673 dynobj
->error(_("too few symbol versions"));
677 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
679 // We keep a list of all STT_OBJECT symbols, so that we can resolve
680 // weak aliases. This is necessary because if the dynamic object
681 // provides the same variable under two names, one of which is a
682 // weak definition, and the regular object refers to the weak
683 // definition, we have to put both the weak definition and the
684 // strong definition into the dynamic symbol table. Given a weak
685 // definition, the only way that we can find the corresponding
686 // strong definition, if any, is to search the symbol table.
687 std::vector
<Sized_symbol
<size
>*> object_symbols
;
689 const unsigned char* p
= syms
;
690 const unsigned char* vs
= versym
;
691 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
693 elfcpp::Sym
<size
, big_endian
> sym(p
);
695 // Ignore symbols with local binding.
696 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
699 unsigned int st_name
= sym
.get_st_name();
700 if (st_name
>= sym_name_size
)
702 dynobj
->error(_("bad symbol name offset %u at %zu"),
707 const char* name
= sym_names
+ st_name
;
709 Sized_symbol
<size
>* res
;
713 Stringpool::Key name_key
;
714 name
= this->namepool_
.add(name
, true, &name_key
);
715 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
720 // Read the version information.
722 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
724 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
725 v
&= elfcpp::VERSYM_VERSION
;
727 // The Sun documentation says that V can be VER_NDX_LOCAL,
728 // or VER_NDX_GLOBAL, or a version index. The meaning of
729 // VER_NDX_LOCAL is defined as "Symbol has local scope."
730 // The old GNU linker will happily generate VER_NDX_LOCAL
731 // for an undefined symbol. I don't know what the Sun
732 // linker will generate.
734 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
735 && sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
737 // This symbol should not be visible outside the object.
741 // At this point we are definitely going to add this symbol.
742 Stringpool::Key name_key
;
743 name
= this->namepool_
.add(name
, true, &name_key
);
745 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
746 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
748 // This symbol does not have a version.
749 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
754 if (v
>= version_map
->size())
756 dynobj
->error(_("versym for symbol %zu out of range: %u"),
761 const char* version
= (*version_map
)[v
];
764 dynobj
->error(_("versym for symbol %zu has no name: %u"),
769 Stringpool::Key version_key
;
770 version
= this->namepool_
.add(version
, true, &version_key
);
772 // If this is an absolute symbol, and the version name
773 // and symbol name are the same, then this is the
774 // version definition symbol. These symbols exist to
775 // support using -u to pull in particular versions. We
776 // do not want to record a version for them.
777 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
778 && name_key
== version_key
)
779 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
783 const bool def
= (!hidden
784 && (sym
.get_st_shndx()
785 != elfcpp::SHN_UNDEF
));
786 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
787 version_key
, def
, sym
, sym
);
792 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
793 && sym
.get_st_type() == elfcpp::STT_OBJECT
)
794 object_symbols
.push_back(res
);
797 this->record_weak_aliases(&object_symbols
);
800 // This is used to sort weak aliases. We sort them first by section
801 // index, then by offset, then by weak ahead of strong.
804 class Weak_alias_sorter
807 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
812 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
813 const Sized_symbol
<size
>* s2
) const
815 if (s1
->shndx() != s2
->shndx())
816 return s1
->shndx() < s2
->shndx();
817 if (s1
->value() != s2
->value())
818 return s1
->value() < s2
->value();
819 if (s1
->binding() != s2
->binding())
821 if (s1
->binding() == elfcpp::STB_WEAK
)
823 if (s2
->binding() == elfcpp::STB_WEAK
)
826 return std::string(s1
->name()) < std::string(s2
->name());
829 // SYMBOLS is a list of object symbols from a dynamic object. Look
830 // for any weak aliases, and record them so that if we add the weak
831 // alias to the dynamic symbol table, we also add the corresponding
836 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
838 // Sort the vector by section index, then by offset, then by weak
840 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
842 // Walk through the vector. For each weak definition, record
844 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
849 if ((*p
)->binding() != elfcpp::STB_WEAK
)
852 // Build a circular list of weak aliases. Each symbol points to
853 // the next one in the circular list.
855 Sized_symbol
<size
>* from_sym
= *p
;
856 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
857 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
859 if ((*q
)->shndx() != from_sym
->shndx()
860 || (*q
)->value() != from_sym
->value())
863 this->weak_aliases_
[from_sym
] = *q
;
864 from_sym
->set_has_alias();
870 this->weak_aliases_
[from_sym
] = *p
;
871 from_sym
->set_has_alias();
878 // Create and return a specially defined symbol. If ONLY_IF_REF is
879 // true, then only create the symbol if there is a reference to it.
880 // If this does not return NULL, it sets *POLDSYM to the existing
881 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
883 template<int size
, bool big_endian
>
885 Symbol_table::define_special_symbol(const Target
* target
, const char** pname
,
886 const char** pversion
, bool only_if_ref
,
887 Sized_symbol
<size
>** poldsym
891 Sized_symbol
<size
>* sym
;
892 bool add_to_table
= false;
893 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
897 oldsym
= this->lookup(*pname
, *pversion
);
898 if (oldsym
== NULL
|| !oldsym
->is_undefined())
901 *pname
= oldsym
->name();
902 *pversion
= oldsym
->version();
906 // Canonicalize NAME and VERSION.
907 Stringpool::Key name_key
;
908 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
910 Stringpool::Key version_key
= 0;
911 if (*pversion
!= NULL
)
912 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
914 Symbol
* const snull
= NULL
;
915 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
916 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
922 // We already have a symbol table entry for NAME/VERSION.
923 oldsym
= ins
.first
->second
;
924 gold_assert(oldsym
!= NULL
);
928 // We haven't seen this symbol before.
929 gold_assert(ins
.first
->second
== NULL
);
936 if (!target
->has_make_symbol())
937 sym
= new Sized_symbol
<size
>();
940 gold_assert(target
->get_size() == size
);
941 gold_assert(target
->is_big_endian() ? big_endian
: !big_endian
);
942 typedef Sized_target
<size
, big_endian
> My_target
;
943 const My_target
* sized_target
=
944 static_cast<const My_target
*>(target
);
945 sym
= sized_target
->make_symbol();
951 add_loc
->second
= sym
;
953 gold_assert(oldsym
!= NULL
);
955 *poldsym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
961 // Define a symbol based on an Output_data.
964 Symbol_table::define_in_output_data(const Target
* target
, const char* name
,
965 const char* version
, Output_data
* od
,
966 uint64_t value
, uint64_t symsize
,
967 elfcpp::STT type
, elfcpp::STB binding
,
968 elfcpp::STV visibility
,
969 unsigned char nonvis
,
970 bool offset_is_from_end
,
973 if (parameters
->get_size() == 32)
975 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
976 return this->do_define_in_output_data
<32>(target
, name
, version
, od
,
977 value
, symsize
, type
, binding
,
985 else if (parameters
->get_size() == 64)
987 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
988 return this->do_define_in_output_data
<64>(target
, name
, version
, od
,
989 value
, symsize
, type
, binding
,
1001 // Define a symbol in an Output_data, sized version.
1005 Symbol_table::do_define_in_output_data(
1006 const Target
* target
,
1008 const char* version
,
1010 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1011 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1013 elfcpp::STB binding
,
1014 elfcpp::STV visibility
,
1015 unsigned char nonvis
,
1016 bool offset_is_from_end
,
1019 Sized_symbol
<size
>* sym
;
1020 Sized_symbol
<size
>* oldsym
;
1022 if (parameters
->is_big_endian())
1024 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1025 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1026 target
, &name
, &version
, only_if_ref
, &oldsym
1027 SELECT_SIZE_ENDIAN(size
, true));
1034 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1035 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1036 target
, &name
, &version
, only_if_ref
, &oldsym
1037 SELECT_SIZE_ENDIAN(size
, false));
1046 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1047 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1048 offset_is_from_end
);
1051 && Symbol_table::should_override_with_special(oldsym
))
1052 this->override_with_special(oldsym
, sym
);
1057 // Define a symbol based on an Output_segment.
1060 Symbol_table::define_in_output_segment(const Target
* target
, const char* name
,
1061 const char* version
, Output_segment
* os
,
1062 uint64_t value
, uint64_t symsize
,
1063 elfcpp::STT type
, elfcpp::STB binding
,
1064 elfcpp::STV visibility
,
1065 unsigned char nonvis
,
1066 Symbol::Segment_offset_base offset_base
,
1069 if (parameters
->get_size() == 32)
1071 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1072 return this->do_define_in_output_segment
<32>(target
, name
, version
, os
,
1073 value
, symsize
, type
,
1074 binding
, visibility
, nonvis
,
1075 offset_base
, only_if_ref
);
1080 else if (parameters
->get_size() == 64)
1082 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1083 return this->do_define_in_output_segment
<64>(target
, name
, version
, os
,
1084 value
, symsize
, type
,
1085 binding
, visibility
, nonvis
,
1086 offset_base
, only_if_ref
);
1095 // Define a symbol in an Output_segment, sized version.
1099 Symbol_table::do_define_in_output_segment(
1100 const Target
* target
,
1102 const char* version
,
1104 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1105 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1107 elfcpp::STB binding
,
1108 elfcpp::STV visibility
,
1109 unsigned char nonvis
,
1110 Symbol::Segment_offset_base offset_base
,
1113 Sized_symbol
<size
>* sym
;
1114 Sized_symbol
<size
>* oldsym
;
1116 if (parameters
->is_big_endian())
1118 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1119 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1120 target
, &name
, &version
, only_if_ref
, &oldsym
1121 SELECT_SIZE_ENDIAN(size
, true));
1128 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1129 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1130 target
, &name
, &version
, only_if_ref
, &oldsym
1131 SELECT_SIZE_ENDIAN(size
, false));
1140 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1141 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1145 && Symbol_table::should_override_with_special(oldsym
))
1146 this->override_with_special(oldsym
, sym
);
1151 // Define a special symbol with a constant value. It is a multiple
1152 // definition error if this symbol is already defined.
1155 Symbol_table::define_as_constant(const Target
* target
, const char* name
,
1156 const char* version
, uint64_t value
,
1157 uint64_t symsize
, elfcpp::STT type
,
1158 elfcpp::STB binding
, elfcpp::STV visibility
,
1159 unsigned char nonvis
, bool only_if_ref
)
1161 if (parameters
->get_size() == 32)
1163 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1164 return this->do_define_as_constant
<32>(target
, name
, version
, value
,
1165 symsize
, type
, binding
,
1166 visibility
, nonvis
, only_if_ref
);
1171 else if (parameters
->get_size() == 64)
1173 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1174 return this->do_define_as_constant
<64>(target
, name
, version
, value
,
1175 symsize
, type
, binding
,
1176 visibility
, nonvis
, only_if_ref
);
1185 // Define a symbol as a constant, sized version.
1189 Symbol_table::do_define_as_constant(
1190 const Target
* target
,
1192 const char* version
,
1193 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1194 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1196 elfcpp::STB binding
,
1197 elfcpp::STV visibility
,
1198 unsigned char nonvis
,
1201 Sized_symbol
<size
>* sym
;
1202 Sized_symbol
<size
>* oldsym
;
1204 if (parameters
->is_big_endian())
1206 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1207 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1208 target
, &name
, &version
, only_if_ref
, &oldsym
1209 SELECT_SIZE_ENDIAN(size
, true));
1216 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1217 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1218 target
, &name
, &version
, only_if_ref
, &oldsym
1219 SELECT_SIZE_ENDIAN(size
, false));
1228 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1229 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
1232 && Symbol_table::should_override_with_special(oldsym
))
1233 this->override_with_special(oldsym
, sym
);
1238 // Define a set of symbols in output sections.
1241 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1242 int count
, const Define_symbol_in_section
* p
)
1244 for (int i
= 0; i
< count
; ++i
, ++p
)
1246 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1248 this->define_in_output_data(target
, p
->name
, NULL
, os
, p
->value
,
1249 p
->size
, p
->type
, p
->binding
,
1250 p
->visibility
, p
->nonvis
,
1251 p
->offset_is_from_end
, p
->only_if_ref
);
1253 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1254 p
->binding
, p
->visibility
, p
->nonvis
,
1259 // Define a set of symbols in output segments.
1262 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1263 int count
, const Define_symbol_in_segment
* p
)
1265 for (int i
= 0; i
< count
; ++i
, ++p
)
1267 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1268 p
->segment_flags_set
,
1269 p
->segment_flags_clear
);
1271 this->define_in_output_segment(target
, p
->name
, NULL
, os
, p
->value
,
1272 p
->size
, p
->type
, p
->binding
,
1273 p
->visibility
, p
->nonvis
,
1274 p
->offset_base
, p
->only_if_ref
);
1276 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1277 p
->binding
, p
->visibility
, p
->nonvis
,
1282 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1283 // symbol should be defined--typically a .dyn.bss section. VALUE is
1284 // the offset within POSD.
1288 Symbol_table::define_with_copy_reloc(const Target
* target
,
1289 Sized_symbol
<size
>* csym
,
1290 Output_data
* posd
, uint64_t value
)
1292 gold_assert(csym
->is_from_dynobj());
1293 gold_assert(!csym
->is_copied_from_dynobj());
1294 Object
* object
= csym
->object();
1295 gold_assert(object
->is_dynamic());
1296 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1298 // Our copied variable has to override any variable in a shared
1300 elfcpp::STB binding
= csym
->binding();
1301 if (binding
== elfcpp::STB_WEAK
)
1302 binding
= elfcpp::STB_GLOBAL
;
1304 this->define_in_output_data(target
, csym
->name(), csym
->version(),
1305 posd
, value
, csym
->symsize(),
1306 csym
->type(), binding
,
1307 csym
->visibility(), csym
->nonvis(),
1310 csym
->set_is_copied_from_dynobj();
1311 csym
->set_needs_dynsym_entry();
1313 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1315 // We have now defined all aliases, but we have not entered them all
1316 // in the copied_symbol_dynobjs_ map.
1317 if (csym
->has_alias())
1322 sym
= this->weak_aliases_
[sym
];
1325 gold_assert(sym
->output_data() == posd
);
1327 sym
->set_is_copied_from_dynobj();
1328 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1333 // SYM is defined using a COPY reloc. Return the dynamic object where
1334 // the original definition was found.
1337 Symbol_table::get_copy_source(const Symbol
* sym
) const
1339 gold_assert(sym
->is_copied_from_dynobj());
1340 Copied_symbol_dynobjs::const_iterator p
=
1341 this->copied_symbol_dynobjs_
.find(sym
);
1342 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1346 // Set the dynamic symbol indexes. INDEX is the index of the first
1347 // global dynamic symbol. Pointers to the symbols are stored into the
1348 // vector SYMS. The names are added to DYNPOOL. This returns an
1349 // updated dynamic symbol index.
1352 Symbol_table::set_dynsym_indexes(const Target
* target
,
1354 std::vector
<Symbol
*>* syms
,
1355 Stringpool
* dynpool
,
1358 for (Symbol_table_type::iterator p
= this->table_
.begin();
1359 p
!= this->table_
.end();
1362 Symbol
* sym
= p
->second
;
1364 // Note that SYM may already have a dynamic symbol index, since
1365 // some symbols appear more than once in the symbol table, with
1366 // and without a version.
1368 if (!sym
->should_add_dynsym_entry())
1369 sym
->set_dynsym_index(-1U);
1370 else if (!sym
->has_dynsym_index())
1372 sym
->set_dynsym_index(index
);
1374 syms
->push_back(sym
);
1375 dynpool
->add(sym
->name(), false, NULL
);
1377 // Record any version information.
1378 if (sym
->version() != NULL
)
1379 versions
->record_version(this, dynpool
, sym
);
1383 // Finish up the versions. In some cases this may add new dynamic
1385 index
= versions
->finalize(target
, this, index
, syms
);
1390 // Set the final values for all the symbols. The index of the first
1391 // global symbol in the output file is INDEX. Record the file offset
1392 // OFF. Add their names to POOL. Return the new file offset.
1395 Symbol_table::finalize(unsigned int index
, off_t off
, off_t dynoff
,
1396 size_t dyn_global_index
, size_t dyncount
,
1401 gold_assert(index
!= 0);
1402 this->first_global_index_
= index
;
1404 this->dynamic_offset_
= dynoff
;
1405 this->first_dynamic_global_index_
= dyn_global_index
;
1406 this->dynamic_count_
= dyncount
;
1408 if (parameters
->get_size() == 32)
1410 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1411 ret
= this->sized_finalize
<32>(index
, off
, pool
);
1416 else if (parameters
->get_size() == 64)
1418 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1419 ret
= this->sized_finalize
<64>(index
, off
, pool
);
1427 // Now that we have the final symbol table, we can reliably note
1428 // which symbols should get warnings.
1429 this->warnings_
.note_warnings(this);
1434 // Set the final value for all the symbols. This is called after
1435 // Layout::finalize, so all the output sections have their final
1440 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
1442 off
= align_address(off
, size
>> 3);
1443 this->offset_
= off
;
1445 size_t orig_index
= index
;
1447 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1448 for (Symbol_table_type::iterator p
= this->table_
.begin();
1449 p
!= this->table_
.end();
1452 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1454 // FIXME: Here we need to decide which symbols should go into
1455 // the output file, based on --strip.
1457 // The default version of a symbol may appear twice in the
1458 // symbol table. We only need to finalize it once.
1459 if (sym
->has_symtab_index())
1464 gold_assert(!sym
->has_symtab_index());
1465 sym
->set_symtab_index(-1U);
1466 gold_assert(sym
->dynsym_index() == -1U);
1470 typename Sized_symbol
<size
>::Value_type value
;
1472 switch (sym
->source())
1474 case Symbol::FROM_OBJECT
:
1476 unsigned int shndx
= sym
->shndx();
1478 // FIXME: We need some target specific support here.
1479 if (shndx
>= elfcpp::SHN_LORESERVE
1480 && shndx
!= elfcpp::SHN_ABS
)
1482 gold_error(_("%s: unsupported symbol section 0x%x"),
1483 sym
->demangled_name().c_str(), shndx
);
1484 shndx
= elfcpp::SHN_UNDEF
;
1487 Object
* symobj
= sym
->object();
1488 if (symobj
->is_dynamic())
1491 shndx
= elfcpp::SHN_UNDEF
;
1493 else if (shndx
== elfcpp::SHN_UNDEF
)
1495 else if (shndx
== elfcpp::SHN_ABS
)
1496 value
= sym
->value();
1499 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1501 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1505 sym
->set_symtab_index(-1U);
1506 gold_assert(sym
->dynsym_index() == -1U);
1510 value
= sym
->value() + os
->address() + secoff
;
1515 case Symbol::IN_OUTPUT_DATA
:
1517 Output_data
* od
= sym
->output_data();
1518 value
= sym
->value() + od
->address();
1519 if (sym
->offset_is_from_end())
1520 value
+= od
->data_size();
1524 case Symbol::IN_OUTPUT_SEGMENT
:
1526 Output_segment
* os
= sym
->output_segment();
1527 value
= sym
->value() + os
->vaddr();
1528 switch (sym
->offset_base())
1530 case Symbol::SEGMENT_START
:
1532 case Symbol::SEGMENT_END
:
1533 value
+= os
->memsz();
1535 case Symbol::SEGMENT_BSS
:
1536 value
+= os
->filesz();
1544 case Symbol::CONSTANT
:
1545 value
= sym
->value();
1552 sym
->set_value(value
);
1554 if (parameters
->strip_all())
1555 sym
->set_symtab_index(-1U);
1558 sym
->set_symtab_index(index
);
1559 pool
->add(sym
->name(), false, NULL
);
1565 this->output_count_
= index
- orig_index
;
1570 // Write out the global symbols.
1573 Symbol_table::write_globals(const Input_objects
* input_objects
,
1574 const Stringpool
* sympool
,
1575 const Stringpool
* dynpool
, Output_file
* of
) const
1577 if (parameters
->get_size() == 32)
1579 if (parameters
->is_big_endian())
1581 #ifdef HAVE_TARGET_32_BIG
1582 this->sized_write_globals
<32, true>(input_objects
, sympool
,
1590 #ifdef HAVE_TARGET_32_LITTLE
1591 this->sized_write_globals
<32, false>(input_objects
, sympool
,
1598 else if (parameters
->get_size() == 64)
1600 if (parameters
->is_big_endian())
1602 #ifdef HAVE_TARGET_64_BIG
1603 this->sized_write_globals
<64, true>(input_objects
, sympool
,
1611 #ifdef HAVE_TARGET_64_LITTLE
1612 this->sized_write_globals
<64, false>(input_objects
, sympool
,
1623 // Write out the global symbols.
1625 template<int size
, bool big_endian
>
1627 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
1628 const Stringpool
* sympool
,
1629 const Stringpool
* dynpool
,
1630 Output_file
* of
) const
1632 const Target
* const target
= input_objects
->target();
1634 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1635 unsigned int index
= this->first_global_index_
;
1636 const off_t oview_size
= this->output_count_
* sym_size
;
1637 unsigned char* const psyms
= of
->get_output_view(this->offset_
, oview_size
);
1639 unsigned int dynamic_count
= this->dynamic_count_
;
1640 off_t dynamic_size
= dynamic_count
* sym_size
;
1641 unsigned int first_dynamic_global_index
= this->first_dynamic_global_index_
;
1642 unsigned char* dynamic_view
;
1643 if (this->dynamic_offset_
== 0)
1644 dynamic_view
= NULL
;
1646 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
1648 unsigned char* ps
= psyms
;
1649 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1650 p
!= this->table_
.end();
1653 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1655 // Possibly warn about unresolved symbols in shared libraries.
1656 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
1658 unsigned int sym_index
= sym
->symtab_index();
1659 unsigned int dynsym_index
;
1660 if (dynamic_view
== NULL
)
1663 dynsym_index
= sym
->dynsym_index();
1665 if (sym_index
== -1U && dynsym_index
== -1U)
1667 // This symbol is not included in the output file.
1671 if (sym_index
== index
)
1673 else if (sym_index
!= -1U)
1675 // We have already seen this symbol, because it has a
1677 gold_assert(sym_index
< index
);
1678 if (dynsym_index
== -1U)
1684 typename
elfcpp::Elf_types
<32>::Elf_Addr value
= sym
->value();
1685 switch (sym
->source())
1687 case Symbol::FROM_OBJECT
:
1689 unsigned int in_shndx
= sym
->shndx();
1691 // FIXME: We need some target specific support here.
1692 if (in_shndx
>= elfcpp::SHN_LORESERVE
1693 && in_shndx
!= elfcpp::SHN_ABS
)
1695 gold_error(_("%s: unsupported symbol section 0x%x"),
1696 sym
->demangled_name().c_str(), in_shndx
);
1701 Object
* symobj
= sym
->object();
1702 if (symobj
->is_dynamic())
1704 if (sym
->needs_dynsym_value())
1705 value
= target
->dynsym_value(sym
);
1706 shndx
= elfcpp::SHN_UNDEF
;
1708 else if (in_shndx
== elfcpp::SHN_UNDEF
1709 || in_shndx
== elfcpp::SHN_ABS
)
1713 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1715 Output_section
* os
= relobj
->output_section(in_shndx
,
1717 gold_assert(os
!= NULL
);
1718 shndx
= os
->out_shndx();
1724 case Symbol::IN_OUTPUT_DATA
:
1725 shndx
= sym
->output_data()->out_shndx();
1728 case Symbol::IN_OUTPUT_SEGMENT
:
1729 shndx
= elfcpp::SHN_ABS
;
1732 case Symbol::CONSTANT
:
1733 shndx
= elfcpp::SHN_ABS
;
1740 if (sym_index
!= -1U)
1742 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1743 sym
, sym
->value(), shndx
, sympool
, ps
1744 SELECT_SIZE_ENDIAN(size
, big_endian
));
1748 if (dynsym_index
!= -1U)
1750 dynsym_index
-= first_dynamic_global_index
;
1751 gold_assert(dynsym_index
< dynamic_count
);
1752 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
1753 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1754 sym
, value
, shndx
, dynpool
, pd
1755 SELECT_SIZE_ENDIAN(size
, big_endian
));
1759 gold_assert(ps
- psyms
== oview_size
);
1761 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1762 if (dynamic_view
!= NULL
)
1763 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
1766 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1767 // strtab holding the name.
1769 template<int size
, bool big_endian
>
1771 Symbol_table::sized_write_symbol(
1772 Sized_symbol
<size
>* sym
,
1773 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1775 const Stringpool
* pool
,
1777 ACCEPT_SIZE_ENDIAN
) const
1779 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
1780 osym
.put_st_name(pool
->get_offset(sym
->name()));
1781 osym
.put_st_value(value
);
1782 osym
.put_st_size(sym
->symsize());
1783 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1784 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
1785 osym
.put_st_shndx(shndx
);
1788 // Check for unresolved symbols in shared libraries. This is
1789 // controlled by the --allow-shlib-undefined option.
1791 // We only warn about libraries for which we have seen all the
1792 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1793 // which were not seen in this link. If we didn't see a DT_NEEDED
1794 // entry, we aren't going to be able to reliably report whether the
1795 // symbol is undefined.
1797 // We also don't warn about libraries found in the system library
1798 // directory (the directory were we find libc.so); we assume that
1799 // those libraries are OK. This heuristic avoids problems in
1800 // GNU/Linux, in which -ldl can have undefined references satisfied by
1804 Symbol_table::warn_about_undefined_dynobj_symbol(
1805 const Input_objects
* input_objects
,
1808 if (sym
->source() == Symbol::FROM_OBJECT
1809 && sym
->object()->is_dynamic()
1810 && sym
->shndx() == elfcpp::SHN_UNDEF
1811 && sym
->binding() != elfcpp::STB_WEAK
1812 && !parameters
->allow_shlib_undefined()
1813 && !input_objects
->target()->is_defined_by_abi(sym
)
1814 && !input_objects
->found_in_system_library_directory(sym
->object()))
1816 // A very ugly cast.
1817 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
1818 if (!dynobj
->has_unknown_needed_entries())
1819 gold_error(_("%s: undefined reference to '%s'"),
1820 sym
->object()->name().c_str(),
1821 sym
->demangled_name().c_str());
1825 // Write out a section symbol. Return the update offset.
1828 Symbol_table::write_section_symbol(const Output_section
*os
,
1832 if (parameters
->get_size() == 32)
1834 if (parameters
->is_big_endian())
1836 #ifdef HAVE_TARGET_32_BIG
1837 this->sized_write_section_symbol
<32, true>(os
, of
, offset
);
1844 #ifdef HAVE_TARGET_32_LITTLE
1845 this->sized_write_section_symbol
<32, false>(os
, of
, offset
);
1851 else if (parameters
->get_size() == 64)
1853 if (parameters
->is_big_endian())
1855 #ifdef HAVE_TARGET_64_BIG
1856 this->sized_write_section_symbol
<64, true>(os
, of
, offset
);
1863 #ifdef HAVE_TARGET_64_LITTLE
1864 this->sized_write_section_symbol
<64, false>(os
, of
, offset
);
1874 // Write out a section symbol, specialized for size and endianness.
1876 template<int size
, bool big_endian
>
1878 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
1882 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1884 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
1886 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
1887 osym
.put_st_name(0);
1888 osym
.put_st_value(os
->address());
1889 osym
.put_st_size(0);
1890 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
1891 elfcpp::STT_SECTION
));
1892 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
1893 osym
.put_st_shndx(os
->out_shndx());
1895 of
->write_output_view(offset
, sym_size
, pov
);
1898 // Print statistical information to stderr. This is used for --stats.
1901 Symbol_table::print_stats() const
1903 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
1904 fprintf(stderr
, _("%s: symbol table entries: %zu; buckets: %zu\n"),
1905 program_name
, this->table_
.size(), this->table_
.bucket_count());
1907 fprintf(stderr
, _("%s: symbol table entries: %zu\n"),
1908 program_name
, this->table_
.size());
1910 this->namepool_
.print_stats("symbol table stringpool");
1913 // We check for ODR violations by looking for symbols with the same
1914 // name for which the debugging information reports that they were
1915 // defined in different source locations. When comparing the source
1916 // location, we consider instances with the same base filename and
1917 // line number to be the same. This is because different object
1918 // files/shared libraries can include the same header file using
1919 // different paths, and we don't want to report an ODR violation in
1922 // This struct is used to compare line information, as returned by
1923 // Dwarf_line_info::one_addr2line. It imlements a < comparison
1924 // operator used with std::set.
1926 struct Odr_violation_compare
1929 operator()(const std::string
& s1
, const std::string
& s2
) const
1931 std::string::size_type pos1
= s1
.rfind('/');
1932 std::string::size_type pos2
= s2
.rfind('/');
1933 if (pos1
== std::string::npos
1934 || pos2
== std::string::npos
)
1936 return s1
.compare(pos1
, std::string::npos
,
1937 s2
, pos2
, std::string::npos
) < 0;
1941 // Check candidate_odr_violations_ to find symbols with the same name
1942 // but apparently different definitions (different source-file/line-no).
1945 Symbol_table::detect_odr_violations(const char* output_file_name
) const
1947 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
1948 it
!= candidate_odr_violations_
.end();
1951 const char* symbol_name
= it
->first
;
1952 // We use a sorted set so the output is deterministic.
1953 std::set
<std::string
, Odr_violation_compare
> line_nums
;
1955 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
1956 locs
= it
->second
.begin();
1957 locs
!= it
->second
.end();
1960 // We need to lock the object in order to read it. This
1961 // means that we can not run inside a Task. If we want to
1962 // run this in a Task for better performance, we will need
1963 // one Task for object, plus appropriate locking to ensure
1964 // that we don't conflict with other uses of the object.
1965 locs
->object
->lock();
1966 std::string lineno
= Dwarf_line_info::one_addr2line(
1967 locs
->object
, locs
->shndx
, locs
->offset
);
1968 locs
->object
->unlock();
1969 if (!lineno
.empty())
1970 line_nums
.insert(lineno
);
1973 if (line_nums
.size() > 1)
1975 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
1976 "places (possible ODR violation):"),
1977 output_file_name
, demangle(symbol_name
).c_str());
1978 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
1979 it2
!= line_nums
.end();
1981 fprintf(stderr
, " %s\n", it2
->c_str());
1986 // Warnings functions.
1988 // Add a new warning.
1991 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1994 name
= symtab
->canonicalize_name(name
);
1995 this->warnings_
[name
].set(obj
, shndx
);
1998 // Look through the warnings and mark the symbols for which we should
1999 // warn. This is called during Layout::finalize when we know the
2000 // sources for all the symbols.
2003 Warnings::note_warnings(Symbol_table
* symtab
)
2005 for (Warning_table::iterator p
= this->warnings_
.begin();
2006 p
!= this->warnings_
.end();
2009 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
2011 && sym
->source() == Symbol::FROM_OBJECT
2012 && sym
->object() == p
->second
.object
)
2014 sym
->set_has_warning();
2016 // Read the section contents to get the warning text. It
2017 // would be nicer if we only did this if we have to actually
2018 // issue a warning. Unfortunately, warnings are issued as
2019 // we relocate sections. That means that we can not lock
2020 // the object then, as we might try to issue the same
2021 // warning multiple times simultaneously.
2023 Task_locker_obj
<Object
> tl(*p
->second
.object
);
2024 const unsigned char* c
;
2026 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
,
2028 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
2034 // Issue a warning. This is called when we see a relocation against a
2035 // symbol for which has a warning.
2037 template<int size
, bool big_endian
>
2039 Warnings::issue_warning(const Symbol
* sym
,
2040 const Relocate_info
<size
, big_endian
>* relinfo
,
2041 size_t relnum
, off_t reloffset
) const
2043 gold_assert(sym
->has_warning());
2044 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2045 gold_assert(p
!= this->warnings_
.end());
2046 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2047 "%s", p
->second
.text
.c_str());
2050 // Instantiate the templates we need. We could use the configure
2051 // script to restrict this to only the ones needed for implemented
2054 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2057 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2060 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2063 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2066 #ifdef HAVE_TARGET_32_LITTLE
2069 Symbol_table::add_from_relobj
<32, false>(
2070 Sized_relobj
<32, false>* relobj
,
2071 const unsigned char* syms
,
2073 const char* sym_names
,
2074 size_t sym_name_size
,
2075 Sized_relobj
<32, true>::Symbols
* sympointers
);
2078 #ifdef HAVE_TARGET_32_BIG
2081 Symbol_table::add_from_relobj
<32, true>(
2082 Sized_relobj
<32, true>* relobj
,
2083 const unsigned char* syms
,
2085 const char* sym_names
,
2086 size_t sym_name_size
,
2087 Sized_relobj
<32, false>::Symbols
* sympointers
);
2090 #ifdef HAVE_TARGET_64_LITTLE
2093 Symbol_table::add_from_relobj
<64, false>(
2094 Sized_relobj
<64, false>* relobj
,
2095 const unsigned char* syms
,
2097 const char* sym_names
,
2098 size_t sym_name_size
,
2099 Sized_relobj
<64, true>::Symbols
* sympointers
);
2102 #ifdef HAVE_TARGET_64_BIG
2105 Symbol_table::add_from_relobj
<64, true>(
2106 Sized_relobj
<64, true>* relobj
,
2107 const unsigned char* syms
,
2109 const char* sym_names
,
2110 size_t sym_name_size
,
2111 Sized_relobj
<64, false>::Symbols
* sympointers
);
2114 #ifdef HAVE_TARGET_32_LITTLE
2117 Symbol_table::add_from_dynobj
<32, false>(
2118 Sized_dynobj
<32, false>* dynobj
,
2119 const unsigned char* syms
,
2121 const char* sym_names
,
2122 size_t sym_name_size
,
2123 const unsigned char* versym
,
2125 const std::vector
<const char*>* version_map
);
2128 #ifdef HAVE_TARGET_32_BIG
2131 Symbol_table::add_from_dynobj
<32, true>(
2132 Sized_dynobj
<32, true>* dynobj
,
2133 const unsigned char* syms
,
2135 const char* sym_names
,
2136 size_t sym_name_size
,
2137 const unsigned char* versym
,
2139 const std::vector
<const char*>* version_map
);
2142 #ifdef HAVE_TARGET_64_LITTLE
2145 Symbol_table::add_from_dynobj
<64, false>(
2146 Sized_dynobj
<64, false>* dynobj
,
2147 const unsigned char* syms
,
2149 const char* sym_names
,
2150 size_t sym_name_size
,
2151 const unsigned char* versym
,
2153 const std::vector
<const char*>* version_map
);
2156 #ifdef HAVE_TARGET_64_BIG
2159 Symbol_table::add_from_dynobj
<64, true>(
2160 Sized_dynobj
<64, true>* dynobj
,
2161 const unsigned char* syms
,
2163 const char* sym_names
,
2164 size_t sym_name_size
,
2165 const unsigned char* versym
,
2167 const std::vector
<const char*>* version_map
);
2170 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2173 Symbol_table::define_with_copy_reloc
<32>(const Target
* target
,
2174 Sized_symbol
<32>* sym
,
2175 Output_data
* posd
, uint64_t value
);
2178 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2181 Symbol_table::define_with_copy_reloc
<64>(const Target
* target
,
2182 Sized_symbol
<64>* sym
,
2183 Output_data
* posd
, uint64_t value
);
2186 #ifdef HAVE_TARGET_32_LITTLE
2189 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2190 const Relocate_info
<32, false>* relinfo
,
2191 size_t relnum
, off_t reloffset
) const;
2194 #ifdef HAVE_TARGET_32_BIG
2197 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2198 const Relocate_info
<32, true>* relinfo
,
2199 size_t relnum
, off_t reloffset
) const;
2202 #ifdef HAVE_TARGET_64_LITTLE
2205 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2206 const Relocate_info
<64, false>* relinfo
,
2207 size_t relnum
, off_t reloffset
) const;
2210 #ifdef HAVE_TARGET_64_BIG
2213 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2214 const Relocate_info
<64, true>* relinfo
,
2215 size_t relnum
, off_t reloffset
) const;
2218 } // End namespace gold.