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 is always canonicalized, so we use a
310 // simple combination of the pointers.
313 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
315 return key
.first
^ key
.second
;
318 // The symbol table key equality function. This is only called with
319 // canonicalized name and version strings, so we can use pointer
323 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
324 const Symbol_table_key
& k2
) const
326 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
329 // Make TO a symbol which forwards to FROM.
332 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
334 gold_assert(from
!= to
);
335 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
336 this->forwarders_
[from
] = to
;
337 from
->set_forwarder();
340 // Resolve the forwards from FROM, returning the real symbol.
343 Symbol_table::resolve_forwards(const Symbol
* from
) const
345 gold_assert(from
->is_forwarder());
346 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
347 this->forwarders_
.find(from
);
348 gold_assert(p
!= this->forwarders_
.end());
352 // Look up a symbol by name.
355 Symbol_table::lookup(const char* name
, const char* version
) const
357 Stringpool::Key name_key
;
358 name
= this->namepool_
.find(name
, &name_key
);
362 Stringpool::Key version_key
= 0;
365 version
= this->namepool_
.find(version
, &version_key
);
370 Symbol_table_key
key(name_key
, version_key
);
371 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
372 if (p
== this->table_
.end())
377 // Resolve a Symbol with another Symbol. This is only used in the
378 // unusual case where there are references to both an unversioned
379 // symbol and a symbol with a version, and we then discover that that
380 // version is the default version. Because this is unusual, we do
381 // this the slow way, by converting back to an ELF symbol.
383 template<int size
, bool big_endian
>
385 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
386 const char* version ACCEPT_SIZE_ENDIAN
)
388 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
389 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
390 // We don't bother to set the st_name field.
391 esym
.put_st_value(from
->value());
392 esym
.put_st_size(from
->symsize());
393 esym
.put_st_info(from
->binding(), from
->type());
394 esym
.put_st_other(from
->visibility(), from
->nonvis());
395 esym
.put_st_shndx(from
->shndx());
396 this->resolve(to
, esym
.sym(), esym
.sym(), from
->object(), version
);
403 // Add one symbol from OBJECT to the symbol table. NAME is symbol
404 // name and VERSION is the version; both are canonicalized. DEF is
405 // whether this is the default version.
407 // If DEF is true, then this is the definition of a default version of
408 // a symbol. That means that any lookup of NAME/NULL and any lookup
409 // of NAME/VERSION should always return the same symbol. This is
410 // obvious for references, but in particular we want to do this for
411 // definitions: overriding NAME/NULL should also override
412 // NAME/VERSION. If we don't do that, it would be very hard to
413 // override functions in a shared library which uses versioning.
415 // We implement this by simply making both entries in the hash table
416 // point to the same Symbol structure. That is easy enough if this is
417 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
418 // that we have seen both already, in which case they will both have
419 // independent entries in the symbol table. We can't simply change
420 // the symbol table entry, because we have pointers to the entries
421 // attached to the object files. So we mark the entry attached to the
422 // object file as a forwarder, and record it in the forwarders_ map.
423 // Note that entries in the hash table will never be marked as
426 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
427 // symbol exactly as it existed in the input file. SYM is usually
428 // that as well, but can be modified, for instance if we determine
429 // it's in a to-be-discarded section.
431 template<int size
, bool big_endian
>
433 Symbol_table::add_from_object(Object
* object
,
435 Stringpool::Key name_key
,
437 Stringpool::Key version_key
,
439 const elfcpp::Sym
<size
, big_endian
>& sym
,
440 const elfcpp::Sym
<size
, big_endian
>& orig_sym
)
442 Symbol
* const snull
= NULL
;
443 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
444 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
447 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
448 std::make_pair(this->table_
.end(), false);
451 const Stringpool::Key vnull_key
= 0;
452 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
457 // ins.first: an iterator, which is a pointer to a pair.
458 // ins.first->first: the key (a pair of name and version).
459 // ins.first->second: the value (Symbol*).
460 // ins.second: true if new entry was inserted, false if not.
462 Sized_symbol
<size
>* ret
;
467 // We already have an entry for NAME/VERSION.
468 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
470 gold_assert(ret
!= NULL
);
472 was_undefined
= ret
->is_undefined();
473 was_common
= ret
->is_common();
475 this->resolve(ret
, sym
, orig_sym
, object
, version
);
481 // This is the first time we have seen NAME/NULL. Make
482 // NAME/NULL point to NAME/VERSION.
483 insdef
.first
->second
= ret
;
485 else if (insdef
.first
->second
!= ret
)
487 // This is the unfortunate case where we already have
488 // entries for both NAME/VERSION and NAME/NULL.
489 const Sized_symbol
<size
>* sym2
;
490 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
493 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
494 ret
, sym2
, version
SELECT_SIZE_ENDIAN(size
, big_endian
));
495 this->make_forwarder(insdef
.first
->second
, ret
);
496 insdef
.first
->second
= ret
;
502 // This is the first time we have seen NAME/VERSION.
503 gold_assert(ins
.first
->second
== NULL
);
505 was_undefined
= false;
508 if (def
&& !insdef
.second
)
510 // We already have an entry for NAME/NULL. If we override
511 // it, then change it to NAME/VERSION.
512 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
515 this->resolve(ret
, sym
, orig_sym
, object
, version
);
516 ins
.first
->second
= ret
;
520 Sized_target
<size
, big_endian
>* target
=
521 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
522 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
523 if (!target
->has_make_symbol())
524 ret
= new Sized_symbol
<size
>();
527 ret
= target
->make_symbol();
530 // This means that we don't want a symbol table
533 this->table_
.erase(ins
.first
);
536 this->table_
.erase(insdef
.first
);
537 // Inserting insdef invalidated ins.
538 this->table_
.erase(std::make_pair(name_key
,
545 ret
->init(name
, version
, object
, sym
);
547 ins
.first
->second
= ret
;
550 // This is the first time we have seen NAME/NULL. Point
551 // it at the new entry for NAME/VERSION.
552 gold_assert(insdef
.second
);
553 insdef
.first
->second
= ret
;
558 // Record every time we see a new undefined symbol, to speed up
560 if (!was_undefined
&& ret
->is_undefined())
561 ++this->saw_undefined_
;
563 // Keep track of common symbols, to speed up common symbol
565 if (!was_common
&& ret
->is_common())
566 this->commons_
.push_back(ret
);
571 // Add all the symbols in a relocatable object to the hash table.
573 template<int size
, bool big_endian
>
575 Symbol_table::add_from_relobj(
576 Sized_relobj
<size
, big_endian
>* relobj
,
577 const unsigned char* syms
,
579 const char* sym_names
,
580 size_t sym_name_size
,
581 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
)
583 gold_assert(size
== relobj
->target()->get_size());
584 gold_assert(size
== parameters
->get_size());
586 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
588 const unsigned char* p
= syms
;
589 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
591 elfcpp::Sym
<size
, big_endian
> sym(p
);
592 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
594 unsigned int st_name
= psym
->get_st_name();
595 if (st_name
>= sym_name_size
)
597 relobj
->error(_("bad global symbol name offset %u at %zu"),
602 const char* name
= sym_names
+ st_name
;
604 // A symbol defined in a section which we are not including must
605 // be treated as an undefined symbol.
606 unsigned char symbuf
[sym_size
];
607 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
608 unsigned int st_shndx
= psym
->get_st_shndx();
609 if (st_shndx
!= elfcpp::SHN_UNDEF
610 && st_shndx
< elfcpp::SHN_LORESERVE
611 && !relobj
->is_section_included(st_shndx
))
613 memcpy(symbuf
, p
, sym_size
);
614 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
615 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
619 // In an object file, an '@' in the name separates the symbol
620 // name from the version name. If there are two '@' characters,
621 // this is the default version.
622 const char* ver
= strchr(name
, '@');
624 Sized_symbol
<size
>* res
;
627 Stringpool::Key name_key
;
628 name
= this->namepool_
.add(name
, true, &name_key
);
629 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
634 Stringpool::Key name_key
;
635 name
= this->namepool_
.add_prefix(name
, ver
- name
, &name_key
);
645 Stringpool::Key ver_key
;
646 ver
= this->namepool_
.add(ver
, true, &ver_key
);
648 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
652 (*sympointers
)[i
] = res
;
656 // Add all the symbols in a dynamic object to the hash table.
658 template<int size
, bool big_endian
>
660 Symbol_table::add_from_dynobj(
661 Sized_dynobj
<size
, big_endian
>* dynobj
,
662 const unsigned char* syms
,
664 const char* sym_names
,
665 size_t sym_name_size
,
666 const unsigned char* versym
,
668 const std::vector
<const char*>* version_map
)
670 gold_assert(size
== dynobj
->target()->get_size());
671 gold_assert(size
== parameters
->get_size());
673 if (versym
!= NULL
&& versym_size
/ 2 < count
)
675 dynobj
->error(_("too few symbol versions"));
679 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
681 // We keep a list of all STT_OBJECT symbols, so that we can resolve
682 // weak aliases. This is necessary because if the dynamic object
683 // provides the same variable under two names, one of which is a
684 // weak definition, and the regular object refers to the weak
685 // definition, we have to put both the weak definition and the
686 // strong definition into the dynamic symbol table. Given a weak
687 // definition, the only way that we can find the corresponding
688 // strong definition, if any, is to search the symbol table.
689 std::vector
<Sized_symbol
<size
>*> object_symbols
;
691 const unsigned char* p
= syms
;
692 const unsigned char* vs
= versym
;
693 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
695 elfcpp::Sym
<size
, big_endian
> sym(p
);
697 // Ignore symbols with local binding.
698 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
701 unsigned int st_name
= sym
.get_st_name();
702 if (st_name
>= sym_name_size
)
704 dynobj
->error(_("bad symbol name offset %u at %zu"),
709 const char* name
= sym_names
+ st_name
;
711 Sized_symbol
<size
>* res
;
715 Stringpool::Key name_key
;
716 name
= this->namepool_
.add(name
, true, &name_key
);
717 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
722 // Read the version information.
724 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
726 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
727 v
&= elfcpp::VERSYM_VERSION
;
729 // The Sun documentation says that V can be VER_NDX_LOCAL,
730 // or VER_NDX_GLOBAL, or a version index. The meaning of
731 // VER_NDX_LOCAL is defined as "Symbol has local scope."
732 // The old GNU linker will happily generate VER_NDX_LOCAL
733 // for an undefined symbol. I don't know what the Sun
734 // linker will generate.
736 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
737 && sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
739 // This symbol should not be visible outside the object.
743 // At this point we are definitely going to add this symbol.
744 Stringpool::Key name_key
;
745 name
= this->namepool_
.add(name
, true, &name_key
);
747 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
748 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
750 // This symbol does not have a version.
751 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
756 if (v
>= version_map
->size())
758 dynobj
->error(_("versym for symbol %zu out of range: %u"),
763 const char* version
= (*version_map
)[v
];
766 dynobj
->error(_("versym for symbol %zu has no name: %u"),
771 Stringpool::Key version_key
;
772 version
= this->namepool_
.add(version
, true, &version_key
);
774 // If this is an absolute symbol, and the version name
775 // and symbol name are the same, then this is the
776 // version definition symbol. These symbols exist to
777 // support using -u to pull in particular versions. We
778 // do not want to record a version for them.
779 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
780 && name_key
== version_key
)
781 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
785 const bool def
= (!hidden
786 && (sym
.get_st_shndx()
787 != elfcpp::SHN_UNDEF
));
788 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
789 version_key
, def
, sym
, sym
);
794 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
795 && sym
.get_st_type() == elfcpp::STT_OBJECT
)
796 object_symbols
.push_back(res
);
799 this->record_weak_aliases(&object_symbols
);
802 // This is used to sort weak aliases. We sort them first by section
803 // index, then by offset, then by weak ahead of strong.
806 class Weak_alias_sorter
809 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
814 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
815 const Sized_symbol
<size
>* s2
) const
817 if (s1
->shndx() != s2
->shndx())
818 return s1
->shndx() < s2
->shndx();
819 if (s1
->value() != s2
->value())
820 return s1
->value() < s2
->value();
821 if (s1
->binding() != s2
->binding())
823 if (s1
->binding() == elfcpp::STB_WEAK
)
825 if (s2
->binding() == elfcpp::STB_WEAK
)
828 return std::string(s1
->name()) < std::string(s2
->name());
831 // SYMBOLS is a list of object symbols from a dynamic object. Look
832 // for any weak aliases, and record them so that if we add the weak
833 // alias to the dynamic symbol table, we also add the corresponding
838 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
840 // Sort the vector by section index, then by offset, then by weak
842 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
844 // Walk through the vector. For each weak definition, record
846 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
851 if ((*p
)->binding() != elfcpp::STB_WEAK
)
854 // Build a circular list of weak aliases. Each symbol points to
855 // the next one in the circular list.
857 Sized_symbol
<size
>* from_sym
= *p
;
858 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
859 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
861 if ((*q
)->shndx() != from_sym
->shndx()
862 || (*q
)->value() != from_sym
->value())
865 this->weak_aliases_
[from_sym
] = *q
;
866 from_sym
->set_has_alias();
872 this->weak_aliases_
[from_sym
] = *p
;
873 from_sym
->set_has_alias();
880 // Create and return a specially defined symbol. If ONLY_IF_REF is
881 // true, then only create the symbol if there is a reference to it.
882 // If this does not return NULL, it sets *POLDSYM to the existing
883 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
885 template<int size
, bool big_endian
>
887 Symbol_table::define_special_symbol(const Target
* target
, const char** pname
,
888 const char** pversion
, bool only_if_ref
,
889 Sized_symbol
<size
>** poldsym
893 Sized_symbol
<size
>* sym
;
894 bool add_to_table
= false;
895 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
899 oldsym
= this->lookup(*pname
, *pversion
);
900 if (oldsym
== NULL
|| !oldsym
->is_undefined())
903 *pname
= oldsym
->name();
904 *pversion
= oldsym
->version();
908 // Canonicalize NAME and VERSION.
909 Stringpool::Key name_key
;
910 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
912 Stringpool::Key version_key
= 0;
913 if (*pversion
!= NULL
)
914 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
916 Symbol
* const snull
= NULL
;
917 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
918 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
924 // We already have a symbol table entry for NAME/VERSION.
925 oldsym
= ins
.first
->second
;
926 gold_assert(oldsym
!= NULL
);
930 // We haven't seen this symbol before.
931 gold_assert(ins
.first
->second
== NULL
);
938 if (!target
->has_make_symbol())
939 sym
= new Sized_symbol
<size
>();
942 gold_assert(target
->get_size() == size
);
943 gold_assert(target
->is_big_endian() ? big_endian
: !big_endian
);
944 typedef Sized_target
<size
, big_endian
> My_target
;
945 const My_target
* sized_target
=
946 static_cast<const My_target
*>(target
);
947 sym
= sized_target
->make_symbol();
953 add_loc
->second
= sym
;
955 gold_assert(oldsym
!= NULL
);
957 *poldsym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
963 // Define a symbol based on an Output_data.
966 Symbol_table::define_in_output_data(const Target
* target
, const char* name
,
967 const char* version
, Output_data
* od
,
968 uint64_t value
, uint64_t symsize
,
969 elfcpp::STT type
, elfcpp::STB binding
,
970 elfcpp::STV visibility
,
971 unsigned char nonvis
,
972 bool offset_is_from_end
,
975 if (parameters
->get_size() == 32)
977 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
978 return this->do_define_in_output_data
<32>(target
, name
, version
, od
,
979 value
, symsize
, type
, binding
,
987 else if (parameters
->get_size() == 64)
989 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
990 return this->do_define_in_output_data
<64>(target
, name
, version
, od
,
991 value
, symsize
, type
, binding
,
1003 // Define a symbol in an Output_data, sized version.
1007 Symbol_table::do_define_in_output_data(
1008 const Target
* target
,
1010 const char* version
,
1012 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1013 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1015 elfcpp::STB binding
,
1016 elfcpp::STV visibility
,
1017 unsigned char nonvis
,
1018 bool offset_is_from_end
,
1021 Sized_symbol
<size
>* sym
;
1022 Sized_symbol
<size
>* oldsym
;
1024 if (parameters
->is_big_endian())
1026 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1027 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1028 target
, &name
, &version
, only_if_ref
, &oldsym
1029 SELECT_SIZE_ENDIAN(size
, true));
1036 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1037 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1038 target
, &name
, &version
, only_if_ref
, &oldsym
1039 SELECT_SIZE_ENDIAN(size
, false));
1048 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1049 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1050 offset_is_from_end
);
1053 && Symbol_table::should_override_with_special(oldsym
))
1054 this->override_with_special(oldsym
, sym
);
1059 // Define a symbol based on an Output_segment.
1062 Symbol_table::define_in_output_segment(const Target
* target
, const char* name
,
1063 const char* version
, Output_segment
* os
,
1064 uint64_t value
, uint64_t symsize
,
1065 elfcpp::STT type
, elfcpp::STB binding
,
1066 elfcpp::STV visibility
,
1067 unsigned char nonvis
,
1068 Symbol::Segment_offset_base offset_base
,
1071 if (parameters
->get_size() == 32)
1073 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1074 return this->do_define_in_output_segment
<32>(target
, name
, version
, os
,
1075 value
, symsize
, type
,
1076 binding
, visibility
, nonvis
,
1077 offset_base
, only_if_ref
);
1082 else if (parameters
->get_size() == 64)
1084 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1085 return this->do_define_in_output_segment
<64>(target
, name
, version
, os
,
1086 value
, symsize
, type
,
1087 binding
, visibility
, nonvis
,
1088 offset_base
, only_if_ref
);
1097 // Define a symbol in an Output_segment, sized version.
1101 Symbol_table::do_define_in_output_segment(
1102 const Target
* target
,
1104 const char* version
,
1106 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1107 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1109 elfcpp::STB binding
,
1110 elfcpp::STV visibility
,
1111 unsigned char nonvis
,
1112 Symbol::Segment_offset_base offset_base
,
1115 Sized_symbol
<size
>* sym
;
1116 Sized_symbol
<size
>* oldsym
;
1118 if (parameters
->is_big_endian())
1120 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1121 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1122 target
, &name
, &version
, only_if_ref
, &oldsym
1123 SELECT_SIZE_ENDIAN(size
, true));
1130 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1131 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1132 target
, &name
, &version
, only_if_ref
, &oldsym
1133 SELECT_SIZE_ENDIAN(size
, false));
1142 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1143 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1147 && Symbol_table::should_override_with_special(oldsym
))
1148 this->override_with_special(oldsym
, sym
);
1153 // Define a special symbol with a constant value. It is a multiple
1154 // definition error if this symbol is already defined.
1157 Symbol_table::define_as_constant(const Target
* target
, const char* name
,
1158 const char* version
, uint64_t value
,
1159 uint64_t symsize
, elfcpp::STT type
,
1160 elfcpp::STB binding
, elfcpp::STV visibility
,
1161 unsigned char nonvis
, bool only_if_ref
)
1163 if (parameters
->get_size() == 32)
1165 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1166 return this->do_define_as_constant
<32>(target
, name
, version
, value
,
1167 symsize
, type
, binding
,
1168 visibility
, nonvis
, only_if_ref
);
1173 else if (parameters
->get_size() == 64)
1175 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1176 return this->do_define_as_constant
<64>(target
, name
, version
, value
,
1177 symsize
, type
, binding
,
1178 visibility
, nonvis
, only_if_ref
);
1187 // Define a symbol as a constant, sized version.
1191 Symbol_table::do_define_as_constant(
1192 const Target
* target
,
1194 const char* version
,
1195 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1196 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1198 elfcpp::STB binding
,
1199 elfcpp::STV visibility
,
1200 unsigned char nonvis
,
1203 Sized_symbol
<size
>* sym
;
1204 Sized_symbol
<size
>* oldsym
;
1206 if (parameters
->is_big_endian())
1208 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1209 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1210 target
, &name
, &version
, only_if_ref
, &oldsym
1211 SELECT_SIZE_ENDIAN(size
, true));
1218 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1219 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1220 target
, &name
, &version
, only_if_ref
, &oldsym
1221 SELECT_SIZE_ENDIAN(size
, false));
1230 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1231 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
1234 && Symbol_table::should_override_with_special(oldsym
))
1235 this->override_with_special(oldsym
, sym
);
1240 // Define a set of symbols in output sections.
1243 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1244 int count
, const Define_symbol_in_section
* p
)
1246 for (int i
= 0; i
< count
; ++i
, ++p
)
1248 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1250 this->define_in_output_data(target
, p
->name
, NULL
, os
, p
->value
,
1251 p
->size
, p
->type
, p
->binding
,
1252 p
->visibility
, p
->nonvis
,
1253 p
->offset_is_from_end
, p
->only_if_ref
);
1255 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1256 p
->binding
, p
->visibility
, p
->nonvis
,
1261 // Define a set of symbols in output segments.
1264 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1265 int count
, const Define_symbol_in_segment
* p
)
1267 for (int i
= 0; i
< count
; ++i
, ++p
)
1269 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1270 p
->segment_flags_set
,
1271 p
->segment_flags_clear
);
1273 this->define_in_output_segment(target
, p
->name
, NULL
, os
, p
->value
,
1274 p
->size
, p
->type
, p
->binding
,
1275 p
->visibility
, p
->nonvis
,
1276 p
->offset_base
, p
->only_if_ref
);
1278 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1279 p
->binding
, p
->visibility
, p
->nonvis
,
1284 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1285 // symbol should be defined--typically a .dyn.bss section. VALUE is
1286 // the offset within POSD.
1290 Symbol_table::define_with_copy_reloc(const Target
* target
,
1291 Sized_symbol
<size
>* csym
,
1292 Output_data
* posd
, uint64_t value
)
1294 gold_assert(csym
->is_from_dynobj());
1295 gold_assert(!csym
->is_copied_from_dynobj());
1296 Object
* object
= csym
->object();
1297 gold_assert(object
->is_dynamic());
1298 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1300 // Our copied variable has to override any variable in a shared
1302 elfcpp::STB binding
= csym
->binding();
1303 if (binding
== elfcpp::STB_WEAK
)
1304 binding
= elfcpp::STB_GLOBAL
;
1306 this->define_in_output_data(target
, csym
->name(), csym
->version(),
1307 posd
, value
, csym
->symsize(),
1308 csym
->type(), binding
,
1309 csym
->visibility(), csym
->nonvis(),
1312 csym
->set_is_copied_from_dynobj();
1313 csym
->set_needs_dynsym_entry();
1315 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1317 // We have now defined all aliases, but we have not entered them all
1318 // in the copied_symbol_dynobjs_ map.
1319 if (csym
->has_alias())
1324 sym
= this->weak_aliases_
[sym
];
1327 gold_assert(sym
->output_data() == posd
);
1329 sym
->set_is_copied_from_dynobj();
1330 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1335 // SYM is defined using a COPY reloc. Return the dynamic object where
1336 // the original definition was found.
1339 Symbol_table::get_copy_source(const Symbol
* sym
) const
1341 gold_assert(sym
->is_copied_from_dynobj());
1342 Copied_symbol_dynobjs::const_iterator p
=
1343 this->copied_symbol_dynobjs_
.find(sym
);
1344 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1348 // Set the dynamic symbol indexes. INDEX is the index of the first
1349 // global dynamic symbol. Pointers to the symbols are stored into the
1350 // vector SYMS. The names are added to DYNPOOL. This returns an
1351 // updated dynamic symbol index.
1354 Symbol_table::set_dynsym_indexes(const Target
* target
,
1356 std::vector
<Symbol
*>* syms
,
1357 Stringpool
* dynpool
,
1360 for (Symbol_table_type::iterator p
= this->table_
.begin();
1361 p
!= this->table_
.end();
1364 Symbol
* sym
= p
->second
;
1366 // Note that SYM may already have a dynamic symbol index, since
1367 // some symbols appear more than once in the symbol table, with
1368 // and without a version.
1370 if (!sym
->should_add_dynsym_entry())
1371 sym
->set_dynsym_index(-1U);
1372 else if (!sym
->has_dynsym_index())
1374 sym
->set_dynsym_index(index
);
1376 syms
->push_back(sym
);
1377 dynpool
->add(sym
->name(), false, NULL
);
1379 // Record any version information.
1380 if (sym
->version() != NULL
)
1381 versions
->record_version(this, dynpool
, sym
);
1385 // Finish up the versions. In some cases this may add new dynamic
1387 index
= versions
->finalize(target
, this, index
, syms
);
1392 // Set the final values for all the symbols. The index of the first
1393 // global symbol in the output file is INDEX. Record the file offset
1394 // OFF. Add their names to POOL. Return the new file offset.
1397 Symbol_table::finalize(unsigned int index
, off_t off
, off_t dynoff
,
1398 size_t dyn_global_index
, size_t dyncount
,
1403 gold_assert(index
!= 0);
1404 this->first_global_index_
= index
;
1406 this->dynamic_offset_
= dynoff
;
1407 this->first_dynamic_global_index_
= dyn_global_index
;
1408 this->dynamic_count_
= dyncount
;
1410 if (parameters
->get_size() == 32)
1412 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1413 ret
= this->sized_finalize
<32>(index
, off
, pool
);
1418 else if (parameters
->get_size() == 64)
1420 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1421 ret
= this->sized_finalize
<64>(index
, off
, pool
);
1429 // Now that we have the final symbol table, we can reliably note
1430 // which symbols should get warnings.
1431 this->warnings_
.note_warnings(this);
1436 // Set the final value for all the symbols. This is called after
1437 // Layout::finalize, so all the output sections have their final
1442 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
1444 off
= align_address(off
, size
>> 3);
1445 this->offset_
= off
;
1447 size_t orig_index
= index
;
1449 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1450 for (Symbol_table_type::iterator p
= this->table_
.begin();
1451 p
!= this->table_
.end();
1454 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1456 // FIXME: Here we need to decide which symbols should go into
1457 // the output file, based on --strip.
1459 // The default version of a symbol may appear twice in the
1460 // symbol table. We only need to finalize it once.
1461 if (sym
->has_symtab_index())
1466 gold_assert(!sym
->has_symtab_index());
1467 sym
->set_symtab_index(-1U);
1468 gold_assert(sym
->dynsym_index() == -1U);
1472 typename Sized_symbol
<size
>::Value_type value
;
1474 switch (sym
->source())
1476 case Symbol::FROM_OBJECT
:
1478 unsigned int shndx
= sym
->shndx();
1480 // FIXME: We need some target specific support here.
1481 if (shndx
>= elfcpp::SHN_LORESERVE
1482 && shndx
!= elfcpp::SHN_ABS
)
1484 gold_error(_("%s: unsupported symbol section 0x%x"),
1485 sym
->demangled_name().c_str(), shndx
);
1486 shndx
= elfcpp::SHN_UNDEF
;
1489 Object
* symobj
= sym
->object();
1490 if (symobj
->is_dynamic())
1493 shndx
= elfcpp::SHN_UNDEF
;
1495 else if (shndx
== elfcpp::SHN_UNDEF
)
1497 else if (shndx
== elfcpp::SHN_ABS
)
1498 value
= sym
->value();
1501 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1503 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1507 sym
->set_symtab_index(-1U);
1508 gold_assert(sym
->dynsym_index() == -1U);
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 // We check for ODR violations by looking for symbols with the same
1901 // name for which the debugging information reports that they were
1902 // defined in different source locations. When comparing the source
1903 // location, we consider instances with the same base filename and
1904 // line number to be the same. This is because different object
1905 // files/shared libraries can include the same header file using
1906 // different paths, and we don't want to report an ODR violation in
1909 // This struct is used to compare line information, as returned by
1910 // Dwarf_line_info::one_addr2line. It imlements a < comparison
1911 // operator used with std::set.
1913 struct Odr_violation_compare
1916 operator()(const std::string
& s1
, const std::string
& s2
) const
1918 std::string::size_type pos1
= s1
.rfind('/');
1919 std::string::size_type pos2
= s2
.rfind('/');
1920 if (pos1
== std::string::npos
1921 || pos2
== std::string::npos
)
1923 return s1
.compare(pos1
, std::string::npos
,
1924 s2
, pos2
, std::string::npos
) < 0;
1928 // Check candidate_odr_violations_ to find symbols with the same name
1929 // but apparently different definitions (different source-file/line-no).
1932 Symbol_table::detect_odr_violations(const char* output_file_name
) const
1934 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
1935 it
!= candidate_odr_violations_
.end();
1938 const char* symbol_name
= it
->first
;
1939 // We use a sorted set so the output is deterministic.
1940 std::set
<std::string
, Odr_violation_compare
> line_nums
;
1942 for (Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
1943 locs
= it
->second
.begin();
1944 locs
!= it
->second
.end();
1947 // We need to lock the object in order to read it. This
1948 // means that we can not run inside a Task. If we want to
1949 // run this in a Task for better performance, we will need
1950 // one Task for object, plus appropriate locking to ensure
1951 // that we don't conflict with other uses of the object.
1952 locs
->object
->lock();
1953 std::string lineno
= Dwarf_line_info::one_addr2line(
1954 locs
->object
, locs
->shndx
, locs
->offset
);
1955 locs
->object
->unlock();
1956 if (!lineno
.empty())
1957 line_nums
.insert(lineno
);
1960 if (line_nums
.size() > 1)
1962 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
1963 "places (possible ODR violation):"),
1964 output_file_name
, demangle(symbol_name
).c_str());
1965 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
1966 it2
!= line_nums
.end();
1968 fprintf(stderr
, " %s\n", it2
->c_str());
1973 // Warnings functions.
1975 // Add a new warning.
1978 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1981 name
= symtab
->canonicalize_name(name
);
1982 this->warnings_
[name
].set(obj
, shndx
);
1985 // Look through the warnings and mark the symbols for which we should
1986 // warn. This is called during Layout::finalize when we know the
1987 // sources for all the symbols.
1990 Warnings::note_warnings(Symbol_table
* symtab
)
1992 for (Warning_table::iterator p
= this->warnings_
.begin();
1993 p
!= this->warnings_
.end();
1996 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1998 && sym
->source() == Symbol::FROM_OBJECT
1999 && sym
->object() == p
->second
.object
)
2001 sym
->set_has_warning();
2003 // Read the section contents to get the warning text. It
2004 // would be nicer if we only did this if we have to actually
2005 // issue a warning. Unfortunately, warnings are issued as
2006 // we relocate sections. That means that we can not lock
2007 // the object then, as we might try to issue the same
2008 // warning multiple times simultaneously.
2010 Task_locker_obj
<Object
> tl(*p
->second
.object
);
2011 const unsigned char* c
;
2013 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
,
2015 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
2021 // Issue a warning. This is called when we see a relocation against a
2022 // symbol for which has a warning.
2024 template<int size
, bool big_endian
>
2026 Warnings::issue_warning(const Symbol
* sym
,
2027 const Relocate_info
<size
, big_endian
>* relinfo
,
2028 size_t relnum
, off_t reloffset
) const
2030 gold_assert(sym
->has_warning());
2031 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
2032 gold_assert(p
!= this->warnings_
.end());
2033 gold_warning_at_location(relinfo
, relnum
, reloffset
,
2034 "%s", p
->second
.text
.c_str());
2037 // Instantiate the templates we need. We could use the configure
2038 // script to restrict this to only the ones needed for implemented
2041 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2044 Sized_symbol
<32>::allocate_common(Output_data
*, Value_type
);
2047 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2050 Sized_symbol
<64>::allocate_common(Output_data
*, Value_type
);
2053 #ifdef HAVE_TARGET_32_LITTLE
2056 Symbol_table::add_from_relobj
<32, false>(
2057 Sized_relobj
<32, false>* relobj
,
2058 const unsigned char* syms
,
2060 const char* sym_names
,
2061 size_t sym_name_size
,
2062 Sized_relobj
<32, true>::Symbols
* sympointers
);
2065 #ifdef HAVE_TARGET_32_BIG
2068 Symbol_table::add_from_relobj
<32, true>(
2069 Sized_relobj
<32, true>* relobj
,
2070 const unsigned char* syms
,
2072 const char* sym_names
,
2073 size_t sym_name_size
,
2074 Sized_relobj
<32, false>::Symbols
* sympointers
);
2077 #ifdef HAVE_TARGET_64_LITTLE
2080 Symbol_table::add_from_relobj
<64, false>(
2081 Sized_relobj
<64, false>* relobj
,
2082 const unsigned char* syms
,
2084 const char* sym_names
,
2085 size_t sym_name_size
,
2086 Sized_relobj
<64, true>::Symbols
* sympointers
);
2089 #ifdef HAVE_TARGET_64_BIG
2092 Symbol_table::add_from_relobj
<64, true>(
2093 Sized_relobj
<64, true>* relobj
,
2094 const unsigned char* syms
,
2096 const char* sym_names
,
2097 size_t sym_name_size
,
2098 Sized_relobj
<64, false>::Symbols
* sympointers
);
2101 #ifdef HAVE_TARGET_32_LITTLE
2104 Symbol_table::add_from_dynobj
<32, false>(
2105 Sized_dynobj
<32, false>* dynobj
,
2106 const unsigned char* syms
,
2108 const char* sym_names
,
2109 size_t sym_name_size
,
2110 const unsigned char* versym
,
2112 const std::vector
<const char*>* version_map
);
2115 #ifdef HAVE_TARGET_32_BIG
2118 Symbol_table::add_from_dynobj
<32, true>(
2119 Sized_dynobj
<32, true>* dynobj
,
2120 const unsigned char* syms
,
2122 const char* sym_names
,
2123 size_t sym_name_size
,
2124 const unsigned char* versym
,
2126 const std::vector
<const char*>* version_map
);
2129 #ifdef HAVE_TARGET_64_LITTLE
2132 Symbol_table::add_from_dynobj
<64, false>(
2133 Sized_dynobj
<64, false>* dynobj
,
2134 const unsigned char* syms
,
2136 const char* sym_names
,
2137 size_t sym_name_size
,
2138 const unsigned char* versym
,
2140 const std::vector
<const char*>* version_map
);
2143 #ifdef HAVE_TARGET_64_BIG
2146 Symbol_table::add_from_dynobj
<64, true>(
2147 Sized_dynobj
<64, true>* dynobj
,
2148 const unsigned char* syms
,
2150 const char* sym_names
,
2151 size_t sym_name_size
,
2152 const unsigned char* versym
,
2154 const std::vector
<const char*>* version_map
);
2157 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2160 Symbol_table::define_with_copy_reloc
<32>(const Target
* target
,
2161 Sized_symbol
<32>* sym
,
2162 Output_data
* posd
, uint64_t value
);
2165 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2168 Symbol_table::define_with_copy_reloc
<64>(const Target
* target
,
2169 Sized_symbol
<64>* sym
,
2170 Output_data
* posd
, uint64_t value
);
2173 #ifdef HAVE_TARGET_32_LITTLE
2176 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2177 const Relocate_info
<32, false>* relinfo
,
2178 size_t relnum
, off_t reloffset
) const;
2181 #ifdef HAVE_TARGET_32_BIG
2184 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2185 const Relocate_info
<32, true>* relinfo
,
2186 size_t relnum
, off_t reloffset
) const;
2189 #ifdef HAVE_TARGET_64_LITTLE
2192 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2193 const Relocate_info
<64, false>* relinfo
,
2194 size_t relnum
, off_t reloffset
) const;
2197 #ifdef HAVE_TARGET_64_BIG
2200 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2201 const Relocate_info
<64, true>* relinfo
,
2202 size_t relnum
, off_t reloffset
) const;
2205 } // End namespace gold.