1 // symtab.cc -- the gold symbol table
13 #include "workqueue.h"
21 // Initialize fields in Symbol. This initializes everything except u_
25 Symbol::init_fields(const char* name
, const char* version
,
26 elfcpp::STT type
, elfcpp::STB binding
,
27 elfcpp::STV visibility
, unsigned char nonvis
)
30 this->version_
= version
;
31 this->symtab_index_
= 0;
32 this->dynsym_index_
= 0;
33 this->got_offset_
= 0;
34 this->plt_offset_
= 0;
36 this->binding_
= binding
;
37 this->visibility_
= visibility
;
38 this->nonvis_
= nonvis
;
39 this->is_target_special_
= false;
40 this->is_def_
= false;
41 this->is_forwarder_
= false;
42 this->needs_dynsym_entry_
= false;
43 this->in_reg_
= false;
44 this->in_dyn_
= false;
45 this->has_got_offset_
= false;
46 this->has_plt_offset_
= false;
47 this->has_warning_
= false;
50 // Initialize the fields in the base class Symbol for SYM in OBJECT.
52 template<int size
, bool big_endian
>
54 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
55 const elfcpp::Sym
<size
, big_endian
>& sym
)
57 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
58 sym
.get_st_visibility(), sym
.get_st_nonvis());
59 this->u_
.from_object
.object
= object
;
60 // FIXME: Handle SHN_XINDEX.
61 this->u_
.from_object
.shndx
= sym
.get_st_shndx();
62 this->source_
= FROM_OBJECT
;
63 this->in_reg_
= !object
->is_dynamic();
64 this->in_dyn_
= object
->is_dynamic();
67 // Initialize the fields in the base class Symbol for a symbol defined
71 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
72 elfcpp::STB binding
, elfcpp::STV visibility
,
73 unsigned char nonvis
, bool offset_is_from_end
)
75 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
76 this->u_
.in_output_data
.output_data
= od
;
77 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
78 this->source_
= IN_OUTPUT_DATA
;
82 // Initialize the fields in the base class Symbol for a symbol defined
83 // in an Output_segment.
86 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
87 elfcpp::STB binding
, elfcpp::STV visibility
,
88 unsigned char nonvis
, Segment_offset_base offset_base
)
90 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
91 this->u_
.in_output_segment
.output_segment
= os
;
92 this->u_
.in_output_segment
.offset_base
= offset_base
;
93 this->source_
= IN_OUTPUT_SEGMENT
;
97 // Initialize the fields in the base class Symbol for a symbol defined
101 Symbol::init_base(const char* name
, elfcpp::STT type
,
102 elfcpp::STB binding
, elfcpp::STV visibility
,
103 unsigned char nonvis
)
105 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
106 this->source_
= CONSTANT
;
107 this->in_reg_
= true;
110 // Initialize the fields in Sized_symbol for SYM in OBJECT.
113 template<bool big_endian
>
115 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
116 const elfcpp::Sym
<size
, big_endian
>& sym
)
118 this->init_base(name
, version
, object
, sym
);
119 this->value_
= sym
.get_st_value();
120 this->symsize_
= sym
.get_st_size();
123 // Initialize the fields in Sized_symbol for a symbol defined in an
128 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
129 Value_type value
, Size_type symsize
,
130 elfcpp::STT type
, elfcpp::STB binding
,
131 elfcpp::STV visibility
, unsigned char nonvis
,
132 bool offset_is_from_end
)
134 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
136 this->value_
= value
;
137 this->symsize_
= symsize
;
140 // Initialize the fields in Sized_symbol for a symbol defined in an
145 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
146 Value_type value
, Size_type symsize
,
147 elfcpp::STT type
, elfcpp::STB binding
,
148 elfcpp::STV visibility
, unsigned char nonvis
,
149 Segment_offset_base offset_base
)
151 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
152 this->value_
= value
;
153 this->symsize_
= symsize
;
156 // Initialize the fields in Sized_symbol for a symbol defined as a
161 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
162 elfcpp::STT type
, elfcpp::STB binding
,
163 elfcpp::STV visibility
, unsigned char nonvis
)
165 this->init_base(name
, type
, binding
, visibility
, nonvis
);
166 this->value_
= value
;
167 this->symsize_
= symsize
;
170 // Class Symbol_table.
172 Symbol_table::Symbol_table()
173 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
174 forwarders_(), commons_(), warnings_()
178 Symbol_table::~Symbol_table()
182 // The hash function. The key is always canonicalized, so we use a
183 // simple combination of the pointers.
186 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
188 return key
.first
^ key
.second
;
191 // The symbol table key equality function. This is only called with
192 // canonicalized name and version strings, so we can use pointer
196 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
197 const Symbol_table_key
& k2
) const
199 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
202 // Make TO a symbol which forwards to FROM.
205 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
207 gold_assert(from
!= to
);
208 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
209 this->forwarders_
[from
] = to
;
210 from
->set_forwarder();
213 // Resolve the forwards from FROM, returning the real symbol.
216 Symbol_table::resolve_forwards(const Symbol
* from
) const
218 gold_assert(from
->is_forwarder());
219 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
220 this->forwarders_
.find(from
);
221 gold_assert(p
!= this->forwarders_
.end());
225 // Look up a symbol by name.
228 Symbol_table::lookup(const char* name
, const char* version
) const
230 Stringpool::Key name_key
;
231 name
= this->namepool_
.find(name
, &name_key
);
235 Stringpool::Key version_key
= 0;
238 version
= this->namepool_
.find(version
, &version_key
);
243 Symbol_table_key
key(name_key
, version_key
);
244 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
245 if (p
== this->table_
.end())
250 // Resolve a Symbol with another Symbol. This is only used in the
251 // unusual case where there are references to both an unversioned
252 // symbol and a symbol with a version, and we then discover that that
253 // version is the default version. Because this is unusual, we do
254 // this the slow way, by converting back to an ELF symbol.
256 template<int size
, bool big_endian
>
258 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
259 const char* version ACCEPT_SIZE_ENDIAN
)
261 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
262 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
263 // We don't bother to set the st_name field.
264 esym
.put_st_value(from
->value());
265 esym
.put_st_size(from
->symsize());
266 esym
.put_st_info(from
->binding(), from
->type());
267 esym
.put_st_other(from
->visibility(), from
->nonvis());
268 esym
.put_st_shndx(from
->shndx());
269 Symbol_table::resolve(to
, esym
.sym(), from
->object(), version
);
276 // Add one symbol from OBJECT to the symbol table. NAME is symbol
277 // name and VERSION is the version; both are canonicalized. DEF is
278 // whether this is the default version.
280 // If DEF is true, then this is the definition of a default version of
281 // a symbol. That means that any lookup of NAME/NULL and any lookup
282 // of NAME/VERSION should always return the same symbol. This is
283 // obvious for references, but in particular we want to do this for
284 // definitions: overriding NAME/NULL should also override
285 // NAME/VERSION. If we don't do that, it would be very hard to
286 // override functions in a shared library which uses versioning.
288 // We implement this by simply making both entries in the hash table
289 // point to the same Symbol structure. That is easy enough if this is
290 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
291 // that we have seen both already, in which case they will both have
292 // independent entries in the symbol table. We can't simply change
293 // the symbol table entry, because we have pointers to the entries
294 // attached to the object files. So we mark the entry attached to the
295 // object file as a forwarder, and record it in the forwarders_ map.
296 // Note that entries in the hash table will never be marked as
299 template<int size
, bool big_endian
>
301 Symbol_table::add_from_object(Object
* object
,
303 Stringpool::Key name_key
,
305 Stringpool::Key version_key
,
307 const elfcpp::Sym
<size
, big_endian
>& sym
)
309 Symbol
* const snull
= NULL
;
310 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
311 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
314 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
315 std::make_pair(this->table_
.end(), false);
318 const Stringpool::Key vnull_key
= 0;
319 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
324 // ins.first: an iterator, which is a pointer to a pair.
325 // ins.first->first: the key (a pair of name and version).
326 // ins.first->second: the value (Symbol*).
327 // ins.second: true if new entry was inserted, false if not.
329 Sized_symbol
<size
>* ret
;
334 // We already have an entry for NAME/VERSION.
335 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
337 gold_assert(ret
!= NULL
);
339 was_undefined
= ret
->is_undefined();
340 was_common
= ret
->is_common();
342 Symbol_table::resolve(ret
, sym
, object
, version
);
348 // This is the first time we have seen NAME/NULL. Make
349 // NAME/NULL point to NAME/VERSION.
350 insdef
.first
->second
= ret
;
352 else if (insdef
.first
->second
!= ret
)
354 // This is the unfortunate case where we already have
355 // entries for both NAME/VERSION and NAME/NULL.
356 const Sized_symbol
<size
>* sym2
;
357 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
360 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
361 ret
, sym2
, version
SELECT_SIZE_ENDIAN(size
, big_endian
));
362 this->make_forwarder(insdef
.first
->second
, ret
);
363 insdef
.first
->second
= ret
;
369 // This is the first time we have seen NAME/VERSION.
370 gold_assert(ins
.first
->second
== NULL
);
372 was_undefined
= false;
375 if (def
&& !insdef
.second
)
377 // We already have an entry for NAME/NULL. If we override
378 // it, then change it to NAME/VERSION.
379 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
382 Symbol_table::resolve(ret
, sym
, object
, version
);
383 ins
.first
->second
= ret
;
387 Sized_target
<size
, big_endian
>* target
=
388 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
389 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
390 if (!target
->has_make_symbol())
391 ret
= new Sized_symbol
<size
>();
394 ret
= target
->make_symbol();
397 // This means that we don't want a symbol table
400 this->table_
.erase(ins
.first
);
403 this->table_
.erase(insdef
.first
);
404 // Inserting insdef invalidated ins.
405 this->table_
.erase(std::make_pair(name_key
,
412 ret
->init(name
, version
, object
, sym
);
414 ins
.first
->second
= ret
;
417 // This is the first time we have seen NAME/NULL. Point
418 // it at the new entry for NAME/VERSION.
419 gold_assert(insdef
.second
);
420 insdef
.first
->second
= ret
;
425 // Record every time we see a new undefined symbol, to speed up
427 if (!was_undefined
&& ret
->is_undefined())
428 ++this->saw_undefined_
;
430 // Keep track of common symbols, to speed up common symbol
432 if (!was_common
&& ret
->is_common())
433 this->commons_
.push_back(ret
);
438 // Add all the symbols in a relocatable object to the hash table.
440 template<int size
, bool big_endian
>
442 Symbol_table::add_from_relobj(
443 Sized_relobj
<size
, big_endian
>* relobj
,
444 const unsigned char* syms
,
446 const char* sym_names
,
447 size_t sym_name_size
,
448 Symbol
** sympointers
)
450 // We take the size from the first object we see.
451 if (this->get_size() == 0)
452 this->set_size(size
);
454 if (size
!= this->get_size() || size
!= relobj
->target()->get_size())
456 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
457 program_name
, relobj
->name().c_str());
461 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
463 const unsigned char* p
= syms
;
464 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
466 elfcpp::Sym
<size
, big_endian
> sym(p
);
467 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
469 unsigned int st_name
= psym
->get_st_name();
470 if (st_name
>= sym_name_size
)
473 _("%s: %s: bad global symbol name offset %u at %lu\n"),
474 program_name
, relobj
->name().c_str(), st_name
,
475 static_cast<unsigned long>(i
));
479 const char* name
= sym_names
+ st_name
;
481 // A symbol defined in a section which we are not including must
482 // be treated as an undefined symbol.
483 unsigned char symbuf
[sym_size
];
484 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
485 unsigned int st_shndx
= psym
->get_st_shndx();
486 if (st_shndx
!= elfcpp::SHN_UNDEF
487 && st_shndx
< elfcpp::SHN_LORESERVE
488 && !relobj
->is_section_included(st_shndx
))
490 memcpy(symbuf
, p
, sym_size
);
491 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
492 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
496 // In an object file, an '@' in the name separates the symbol
497 // name from the version name. If there are two '@' characters,
498 // this is the default version.
499 const char* ver
= strchr(name
, '@');
504 Stringpool::Key name_key
;
505 name
= this->namepool_
.add(name
, &name_key
);
506 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
511 Stringpool::Key name_key
;
512 name
= this->namepool_
.add(name
, ver
- name
, &name_key
);
522 Stringpool::Key ver_key
;
523 ver
= this->namepool_
.add(ver
, &ver_key
);
525 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
529 *sympointers
++ = res
;
533 // Add all the symbols in a dynamic object to the hash table.
535 template<int size
, bool big_endian
>
537 Symbol_table::add_from_dynobj(
538 Sized_dynobj
<size
, big_endian
>* dynobj
,
539 const unsigned char* syms
,
541 const char* sym_names
,
542 size_t sym_name_size
,
543 const unsigned char* versym
,
545 const std::vector
<const char*>* version_map
)
547 // We take the size from the first object we see.
548 if (this->get_size() == 0)
549 this->set_size(size
);
551 if (size
!= this->get_size() || size
!= dynobj
->target()->get_size())
553 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
554 program_name
, dynobj
->name().c_str());
558 if (versym
!= NULL
&& versym_size
/ 2 < count
)
560 fprintf(stderr
, _("%s: %s: too few symbol versions\n"),
561 program_name
, dynobj
->name().c_str());
565 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
567 const unsigned char* p
= syms
;
568 const unsigned char* vs
= versym
;
569 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
571 elfcpp::Sym
<size
, big_endian
> sym(p
);
573 // Ignore symbols with local binding.
574 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
577 unsigned int st_name
= sym
.get_st_name();
578 if (st_name
>= sym_name_size
)
580 fprintf(stderr
, _("%s: %s: bad symbol name offset %u at %lu\n"),
581 program_name
, dynobj
->name().c_str(), st_name
,
582 static_cast<unsigned long>(i
));
586 const char* name
= sym_names
+ st_name
;
590 Stringpool::Key name_key
;
591 name
= this->namepool_
.add(name
, &name_key
);
592 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
597 // Read the version information.
599 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
601 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
602 v
&= elfcpp::VERSYM_VERSION
;
604 // The Sun documentation says that V can be VER_NDX_LOCAL, or
605 // VER_NDX_GLOBAL, or a version index. The meaning of
606 // VER_NDX_LOCAL is defined as "Symbol has local scope." The
607 // old GNU linker will happily generate VER_NDX_LOCAL for an
608 // undefined symbol. I don't know what the Sun linker will
611 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
612 && sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
614 // This symbol should not be visible outside the object.
618 // At this point we are definitely going to add this symbol.
619 Stringpool::Key name_key
;
620 name
= this->namepool_
.add(name
, &name_key
);
622 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
623 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
625 // This symbol does not have a version.
626 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0, false, sym
);
630 if (v
>= version_map
->size())
633 _("%s: %s: versym for symbol %zu out of range: %u\n"),
634 program_name
, dynobj
->name().c_str(), i
, v
);
638 const char* version
= (*version_map
)[v
];
641 fprintf(stderr
, _("%s: %s: versym for symbol %zu has no name: %u\n"),
642 program_name
, dynobj
->name().c_str(), i
, v
);
646 Stringpool::Key version_key
;
647 version
= this->namepool_
.add(version
, &version_key
);
649 // If this is an absolute symbol, and the version name and
650 // symbol name are the same, then this is the version definition
651 // symbol. These symbols exist to support using -u to pull in
652 // particular versions. We do not want to record a version for
654 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
&& name_key
== version_key
)
656 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0, false, sym
);
660 const bool def
= !hidden
&& sym
.get_st_shndx() != elfcpp::SHN_UNDEF
;
662 this->add_from_object(dynobj
, name
, name_key
, version
, version_key
,
667 // Create and return a specially defined symbol. If ONLY_IF_REF is
668 // true, then only create the symbol if there is a reference to it.
670 template<int size
, bool big_endian
>
672 Symbol_table::define_special_symbol(const Target
* target
, const char* name
,
673 const char* version
, bool only_if_ref
676 gold_assert(this->size_
== size
);
679 Sized_symbol
<size
>* sym
;
683 oldsym
= this->lookup(name
, version
);
684 if (oldsym
== NULL
|| !oldsym
->is_undefined())
688 // Canonicalize NAME and VERSION.
689 name
= oldsym
->name();
690 version
= oldsym
->version();
694 // Canonicalize NAME and VERSION.
695 Stringpool::Key name_key
;
696 name
= this->namepool_
.add(name
, &name_key
);
698 Stringpool::Key version_key
= 0;
700 version
= this->namepool_
.add(version
, &version_key
);
702 Symbol
* const snull
= NULL
;
703 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
704 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
710 // We already have a symbol table entry for NAME/VERSION.
711 oldsym
= ins
.first
->second
;
712 gold_assert(oldsym
!= NULL
);
717 // We haven't seen this symbol before.
718 gold_assert(ins
.first
->second
== NULL
);
720 if (!target
->has_make_symbol())
721 sym
= new Sized_symbol
<size
>();
724 gold_assert(target
->get_size() == size
);
725 gold_assert(target
->is_big_endian() ? big_endian
: !big_endian
);
726 typedef Sized_target
<size
, big_endian
> My_target
;
727 const My_target
* sized_target
=
728 static_cast<const My_target
*>(target
);
729 sym
= sized_target
->make_symbol();
734 ins
.first
->second
= sym
;
741 gold_assert(sym
== NULL
);
743 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
745 gold_assert(sym
->source() == Symbol::FROM_OBJECT
);
746 const int old_shndx
= sym
->shndx();
747 if (old_shndx
!= elfcpp::SHN_UNDEF
748 && old_shndx
!= elfcpp::SHN_COMMON
749 && !sym
->object()->is_dynamic())
751 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
753 // FIXME: Report old location. Record that we have seen an
758 // Our new definition is going to override the old reference.
764 // Define a symbol based on an Output_data.
767 Symbol_table::define_in_output_data(const Target
* target
, const char* name
,
768 const char* version
, Output_data
* od
,
769 uint64_t value
, uint64_t symsize
,
770 elfcpp::STT type
, elfcpp::STB binding
,
771 elfcpp::STV visibility
,
772 unsigned char nonvis
,
773 bool offset_is_from_end
,
776 gold_assert(target
->get_size() == this->size_
);
777 if (this->size_
== 32)
778 return this->do_define_in_output_data
<32>(target
, name
, version
, od
, value
,
779 symsize
, type
, binding
,
781 offset_is_from_end
, only_if_ref
);
782 else if (this->size_
== 64)
783 return this->do_define_in_output_data
<64>(target
, name
, version
, od
, value
,
784 symsize
, type
, binding
,
786 offset_is_from_end
, only_if_ref
);
791 // Define a symbol in an Output_data, sized version.
795 Symbol_table::do_define_in_output_data(
796 const Target
* target
,
800 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
801 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
804 elfcpp::STV visibility
,
805 unsigned char nonvis
,
806 bool offset_is_from_end
,
809 Sized_symbol
<size
>* sym
;
811 if (target
->is_big_endian())
813 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
814 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
815 target
, name
, version
, only_if_ref
816 SELECT_SIZE_ENDIAN(size
, true));
823 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
824 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
825 target
, name
, version
, only_if_ref
826 SELECT_SIZE_ENDIAN(size
, false));
835 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
841 // Define a symbol based on an Output_segment.
844 Symbol_table::define_in_output_segment(const Target
* target
, const char* name
,
845 const char* version
, Output_segment
* os
,
846 uint64_t value
, uint64_t symsize
,
847 elfcpp::STT type
, elfcpp::STB binding
,
848 elfcpp::STV visibility
,
849 unsigned char nonvis
,
850 Symbol::Segment_offset_base offset_base
,
853 gold_assert(target
->get_size() == this->size_
);
854 if (this->size_
== 32)
855 return this->do_define_in_output_segment
<32>(target
, name
, version
, os
,
856 value
, symsize
, type
, binding
,
858 offset_base
, only_if_ref
);
859 else if (this->size_
== 64)
860 return this->do_define_in_output_segment
<64>(target
, name
, version
, os
,
861 value
, symsize
, type
, binding
,
863 offset_base
, only_if_ref
);
868 // Define a symbol in an Output_segment, sized version.
872 Symbol_table::do_define_in_output_segment(
873 const Target
* target
,
877 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
878 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
881 elfcpp::STV visibility
,
882 unsigned char nonvis
,
883 Symbol::Segment_offset_base offset_base
,
886 Sized_symbol
<size
>* sym
;
888 if (target
->is_big_endian())
889 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
890 target
, name
, version
, only_if_ref
891 SELECT_SIZE_ENDIAN(size
, true));
893 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
894 target
, name
, version
, only_if_ref
895 SELECT_SIZE_ENDIAN(size
, false));
900 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
906 // Define a special symbol with a constant value. It is a multiple
907 // definition error if this symbol is already defined.
910 Symbol_table::define_as_constant(const Target
* target
, const char* name
,
911 const char* version
, uint64_t value
,
912 uint64_t symsize
, elfcpp::STT type
,
913 elfcpp::STB binding
, elfcpp::STV visibility
,
914 unsigned char nonvis
, bool only_if_ref
)
916 gold_assert(target
->get_size() == this->size_
);
917 if (this->size_
== 32)
918 return this->do_define_as_constant
<32>(target
, name
, version
, value
,
919 symsize
, type
, binding
, visibility
,
920 nonvis
, only_if_ref
);
921 else if (this->size_
== 64)
922 return this->do_define_as_constant
<64>(target
, name
, version
, value
,
923 symsize
, type
, binding
, visibility
,
924 nonvis
, only_if_ref
);
929 // Define a symbol as a constant, sized version.
933 Symbol_table::do_define_as_constant(
934 const Target
* target
,
937 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
938 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
941 elfcpp::STV visibility
,
942 unsigned char nonvis
,
945 Sized_symbol
<size
>* sym
;
947 if (target
->is_big_endian())
948 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
949 target
, name
, version
, only_if_ref
950 SELECT_SIZE_ENDIAN(size
, true));
952 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
953 target
, name
, version
, only_if_ref
954 SELECT_SIZE_ENDIAN(size
, false));
959 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
964 // Define a set of symbols in output sections.
967 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
968 int count
, const Define_symbol_in_section
* p
)
970 for (int i
= 0; i
< count
; ++i
, ++p
)
972 Output_section
* os
= layout
->find_output_section(p
->output_section
);
974 this->define_in_output_data(target
, p
->name
, NULL
, os
, p
->value
,
975 p
->size
, p
->type
, p
->binding
,
976 p
->visibility
, p
->nonvis
,
977 p
->offset_is_from_end
, p
->only_if_ref
);
979 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
980 p
->binding
, p
->visibility
, p
->nonvis
,
985 // Define a set of symbols in output segments.
988 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
989 int count
, const Define_symbol_in_segment
* p
)
991 for (int i
= 0; i
< count
; ++i
, ++p
)
993 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
994 p
->segment_flags_set
,
995 p
->segment_flags_clear
);
997 this->define_in_output_segment(target
, p
->name
, NULL
, os
, p
->value
,
998 p
->size
, p
->type
, p
->binding
,
999 p
->visibility
, p
->nonvis
,
1000 p
->offset_base
, p
->only_if_ref
);
1002 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1003 p
->binding
, p
->visibility
, p
->nonvis
,
1008 // Set the dynamic symbol indexes. INDEX is the index of the first
1009 // global dynamic symbol. Pointers to the symbols are stored into the
1010 // vector SYMS. The names are added to DYNPOOL. This returns an
1011 // updated dynamic symbol index.
1014 Symbol_table::set_dynsym_indexes(const General_options
* options
,
1015 const Target
* target
,
1017 std::vector
<Symbol
*>* syms
,
1018 Stringpool
* dynpool
,
1021 for (Symbol_table_type::iterator p
= this->table_
.begin();
1022 p
!= this->table_
.end();
1025 Symbol
* sym
= p
->second
;
1027 // Note that SYM may already have a dynamic symbol index, since
1028 // some symbols appear more than once in the symbol table, with
1029 // and without a version.
1031 if (!sym
->needs_dynsym_entry()
1032 && (!options
->export_dynamic()
1034 || !sym
->is_externally_visible()))
1035 sym
->set_dynsym_index(-1U);
1036 else if (!sym
->has_dynsym_index())
1038 sym
->set_dynsym_index(index
);
1040 syms
->push_back(sym
);
1041 dynpool
->add(sym
->name(), NULL
);
1043 // Record any version information.
1044 if (sym
->version() != NULL
)
1045 versions
->record_version(options
, dynpool
, sym
);
1049 // Finish up the versions. In some cases this may add new dynamic
1051 index
= versions
->finalize(target
, this, index
, syms
);
1056 // Set the final values for all the symbols. The index of the first
1057 // global symbol in the output file is INDEX. Record the file offset
1058 // OFF. Add their names to POOL. Return the new file offset.
1061 Symbol_table::finalize(unsigned int index
, off_t off
, off_t dynoff
,
1062 size_t dyn_global_index
, size_t dyncount
,
1067 gold_assert(index
!= 0);
1068 this->first_global_index_
= index
;
1070 this->dynamic_offset_
= dynoff
;
1071 this->first_dynamic_global_index_
= dyn_global_index
;
1072 this->dynamic_count_
= dyncount
;
1074 if (this->size_
== 32)
1075 ret
= this->sized_finalize
<32>(index
, off
, pool
);
1076 else if (this->size_
== 64)
1077 ret
= this->sized_finalize
<64>(index
, off
, pool
);
1081 // Now that we have the final symbol table, we can reliably note
1082 // which symbols should get warnings.
1083 this->warnings_
.note_warnings(this);
1088 // Set the final value for all the symbols. This is called after
1089 // Layout::finalize, so all the output sections have their final
1094 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
1096 off
= align_address(off
, size
>> 3);
1097 this->offset_
= off
;
1099 size_t orig_index
= index
;
1101 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1102 for (Symbol_table_type::iterator p
= this->table_
.begin();
1103 p
!= this->table_
.end();
1106 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1108 // FIXME: Here we need to decide which symbols should go into
1109 // the output file, based on --strip.
1111 // The default version of a symbol may appear twice in the
1112 // symbol table. We only need to finalize it once.
1113 if (sym
->has_symtab_index())
1118 gold_assert(!sym
->has_symtab_index());
1119 sym
->set_symtab_index(-1U);
1120 gold_assert(sym
->dynsym_index() == -1U);
1124 typename Sized_symbol
<size
>::Value_type value
;
1126 switch (sym
->source())
1128 case Symbol::FROM_OBJECT
:
1130 unsigned int shndx
= sym
->shndx();
1132 // FIXME: We need some target specific support here.
1133 if (shndx
>= elfcpp::SHN_LORESERVE
1134 && shndx
!= elfcpp::SHN_ABS
)
1136 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1137 program_name
, sym
->name(), shndx
);
1141 Object
* symobj
= sym
->object();
1142 if (symobj
->is_dynamic())
1145 shndx
= elfcpp::SHN_UNDEF
;
1147 else if (shndx
== elfcpp::SHN_UNDEF
)
1149 else if (shndx
== elfcpp::SHN_ABS
)
1150 value
= sym
->value();
1153 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1155 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1159 sym
->set_symtab_index(-1U);
1160 gold_assert(sym
->dynsym_index() == -1U);
1164 value
= sym
->value() + os
->address() + secoff
;
1169 case Symbol::IN_OUTPUT_DATA
:
1171 Output_data
* od
= sym
->output_data();
1172 value
= sym
->value() + od
->address();
1173 if (sym
->offset_is_from_end())
1174 value
+= od
->data_size();
1178 case Symbol::IN_OUTPUT_SEGMENT
:
1180 Output_segment
* os
= sym
->output_segment();
1181 value
= sym
->value() + os
->vaddr();
1182 switch (sym
->offset_base())
1184 case Symbol::SEGMENT_START
:
1186 case Symbol::SEGMENT_END
:
1187 value
+= os
->memsz();
1189 case Symbol::SEGMENT_BSS
:
1190 value
+= os
->filesz();
1198 case Symbol::CONSTANT
:
1199 value
= sym
->value();
1206 sym
->set_value(value
);
1207 sym
->set_symtab_index(index
);
1208 pool
->add(sym
->name(), NULL
);
1213 this->output_count_
= index
- orig_index
;
1218 // Write out the global symbols.
1221 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
1222 const Stringpool
* dynpool
, Output_file
* of
) const
1224 if (this->size_
== 32)
1226 if (target
->is_big_endian())
1227 this->sized_write_globals
<32, true>(target
, sympool
, dynpool
, of
);
1229 this->sized_write_globals
<32, false>(target
, sympool
, dynpool
, of
);
1231 else if (this->size_
== 64)
1233 if (target
->is_big_endian())
1234 this->sized_write_globals
<64, true>(target
, sympool
, dynpool
, of
);
1236 this->sized_write_globals
<64, false>(target
, sympool
, dynpool
, of
);
1242 // Write out the global symbols.
1244 template<int size
, bool big_endian
>
1246 Symbol_table::sized_write_globals(const Target
*,
1247 const Stringpool
* sympool
,
1248 const Stringpool
* dynpool
,
1249 Output_file
* of
) const
1251 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1252 unsigned int index
= this->first_global_index_
;
1253 const off_t oview_size
= this->output_count_
* sym_size
;
1254 unsigned char* const psyms
= of
->get_output_view(this->offset_
, oview_size
);
1256 unsigned int dynamic_count
= this->dynamic_count_
;
1257 off_t dynamic_size
= dynamic_count
* sym_size
;
1258 unsigned int first_dynamic_global_index
= this->first_dynamic_global_index_
;
1259 unsigned char* dynamic_view
;
1260 if (this->dynamic_offset_
== 0)
1261 dynamic_view
= NULL
;
1263 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
1265 unsigned char* ps
= psyms
;
1266 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1267 p
!= this->table_
.end();
1270 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1272 unsigned int sym_index
= sym
->symtab_index();
1273 unsigned int dynsym_index
;
1274 if (dynamic_view
== NULL
)
1277 dynsym_index
= sym
->dynsym_index();
1279 if (sym_index
== -1U && dynsym_index
== -1U)
1281 // This symbol is not included in the output file.
1285 if (sym_index
== index
)
1287 else if (sym_index
!= -1U)
1289 // We have already seen this symbol, because it has a
1291 gold_assert(sym_index
< index
);
1292 if (dynsym_index
== -1U)
1298 switch (sym
->source())
1300 case Symbol::FROM_OBJECT
:
1302 unsigned int in_shndx
= sym
->shndx();
1304 // FIXME: We need some target specific support here.
1305 if (in_shndx
>= elfcpp::SHN_LORESERVE
1306 && in_shndx
!= elfcpp::SHN_ABS
)
1308 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1309 program_name
, sym
->name(), in_shndx
);
1313 Object
* symobj
= sym
->object();
1314 if (symobj
->is_dynamic())
1317 shndx
= elfcpp::SHN_UNDEF
;
1319 else if (in_shndx
== elfcpp::SHN_UNDEF
1320 || in_shndx
== elfcpp::SHN_ABS
)
1324 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1326 Output_section
* os
= relobj
->output_section(in_shndx
, &secoff
);
1327 gold_assert(os
!= NULL
);
1328 shndx
= os
->out_shndx();
1333 case Symbol::IN_OUTPUT_DATA
:
1334 shndx
= sym
->output_data()->out_shndx();
1337 case Symbol::IN_OUTPUT_SEGMENT
:
1338 shndx
= elfcpp::SHN_ABS
;
1341 case Symbol::CONSTANT
:
1342 shndx
= elfcpp::SHN_ABS
;
1349 if (sym_index
!= -1U)
1351 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1352 sym
, shndx
, sympool
, ps
1353 SELECT_SIZE_ENDIAN(size
, big_endian
));
1357 if (dynsym_index
!= -1U)
1359 dynsym_index
-= first_dynamic_global_index
;
1360 gold_assert(dynsym_index
< dynamic_count
);
1361 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
1362 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1363 sym
, shndx
, dynpool
, pd
1364 SELECT_SIZE_ENDIAN(size
, big_endian
));
1368 gold_assert(ps
- psyms
== oview_size
);
1370 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1371 if (dynamic_view
!= NULL
)
1372 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
1375 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1376 // strtab holding the name.
1378 template<int size
, bool big_endian
>
1380 Symbol_table::sized_write_symbol(Sized_symbol
<size
>* sym
,
1382 const Stringpool
* pool
,
1384 ACCEPT_SIZE_ENDIAN
) const
1386 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
1387 osym
.put_st_name(pool
->get_offset(sym
->name()));
1388 osym
.put_st_value(sym
->value());
1389 osym
.put_st_size(sym
->symsize());
1390 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1391 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
1392 osym
.put_st_shndx(shndx
);
1395 // Write out a section symbol. Return the update offset.
1398 Symbol_table::write_section_symbol(const Target
* target
,
1399 const Output_section
*os
,
1403 if (this->size_
== 32)
1405 if (target
->is_big_endian())
1406 this->sized_write_section_symbol
<32, true>(os
, of
, offset
);
1408 this->sized_write_section_symbol
<32, false>(os
, of
, offset
);
1410 else if (this->size_
== 64)
1412 if (target
->is_big_endian())
1413 this->sized_write_section_symbol
<64, true>(os
, of
, offset
);
1415 this->sized_write_section_symbol
<64, false>(os
, of
, offset
);
1421 // Write out a section symbol, specialized for size and endianness.
1423 template<int size
, bool big_endian
>
1425 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
1429 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1431 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
1433 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
1434 osym
.put_st_name(0);
1435 osym
.put_st_value(os
->address());
1436 osym
.put_st_size(0);
1437 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
1438 elfcpp::STT_SECTION
));
1439 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
1440 osym
.put_st_shndx(os
->out_shndx());
1442 of
->write_output_view(offset
, sym_size
, pov
);
1445 // Warnings functions.
1447 // Add a new warning.
1450 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1453 name
= symtab
->canonicalize_name(name
);
1454 this->warnings_
[name
].set(obj
, shndx
);
1457 // Look through the warnings and mark the symbols for which we should
1458 // warn. This is called during Layout::finalize when we know the
1459 // sources for all the symbols.
1462 Warnings::note_warnings(Symbol_table
* symtab
)
1464 for (Warning_table::iterator p
= this->warnings_
.begin();
1465 p
!= this->warnings_
.end();
1468 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1470 && sym
->source() == Symbol::FROM_OBJECT
1471 && sym
->object() == p
->second
.object
)
1473 sym
->set_has_warning();
1475 // Read the section contents to get the warning text. It
1476 // would be nicer if we only did this if we have to actually
1477 // issue a warning. Unfortunately, warnings are issued as
1478 // we relocate sections. That means that we can not lock
1479 // the object then, as we might try to issue the same
1480 // warning multiple times simultaneously.
1482 Task_locker_obj
<Object
> tl(*p
->second
.object
);
1483 const unsigned char* c
;
1485 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
);
1486 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1492 // Issue a warning. This is called when we see a relocation against a
1493 // symbol for which has a warning.
1496 Warnings::issue_warning(const Symbol
* sym
, const std::string
& location
) const
1498 gold_assert(sym
->has_warning());
1499 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1500 gold_assert(p
!= this->warnings_
.end());
1501 fprintf(stderr
, _("%s: %s: warning: %s\n"), program_name
, location
.c_str(),
1502 p
->second
.text
.c_str());
1505 // Instantiate the templates we need. We could use the configure
1506 // script to restrict this to only the ones needed for implemented
1509 #ifdef HAVE_TARGET_32_LITTLE
1512 Symbol_table::add_from_relobj
<32, false>(
1513 Sized_relobj
<32, false>* relobj
,
1514 const unsigned char* syms
,
1516 const char* sym_names
,
1517 size_t sym_name_size
,
1518 Symbol
** sympointers
);
1521 #ifdef HAVE_TARGET_32_BIG
1524 Symbol_table::add_from_relobj
<32, true>(
1525 Sized_relobj
<32, true>* relobj
,
1526 const unsigned char* syms
,
1528 const char* sym_names
,
1529 size_t sym_name_size
,
1530 Symbol
** sympointers
);
1533 #ifdef HAVE_TARGET_64_LITTLE
1536 Symbol_table::add_from_relobj
<64, false>(
1537 Sized_relobj
<64, false>* relobj
,
1538 const unsigned char* syms
,
1540 const char* sym_names
,
1541 size_t sym_name_size
,
1542 Symbol
** sympointers
);
1545 #ifdef HAVE_TARGET_64_BIG
1548 Symbol_table::add_from_relobj
<64, true>(
1549 Sized_relobj
<64, true>* relobj
,
1550 const unsigned char* syms
,
1552 const char* sym_names
,
1553 size_t sym_name_size
,
1554 Symbol
** sympointers
);
1557 #ifdef HAVE_TARGET_32_LITTLE
1560 Symbol_table::add_from_dynobj
<32, false>(
1561 Sized_dynobj
<32, false>* dynobj
,
1562 const unsigned char* syms
,
1564 const char* sym_names
,
1565 size_t sym_name_size
,
1566 const unsigned char* versym
,
1568 const std::vector
<const char*>* version_map
);
1571 #ifdef HAVE_TARGET_32_BIG
1574 Symbol_table::add_from_dynobj
<32, true>(
1575 Sized_dynobj
<32, true>* dynobj
,
1576 const unsigned char* syms
,
1578 const char* sym_names
,
1579 size_t sym_name_size
,
1580 const unsigned char* versym
,
1582 const std::vector
<const char*>* version_map
);
1585 #ifdef HAVE_TARGET_64_LITTLE
1588 Symbol_table::add_from_dynobj
<64, false>(
1589 Sized_dynobj
<64, false>* dynobj
,
1590 const unsigned char* syms
,
1592 const char* sym_names
,
1593 size_t sym_name_size
,
1594 const unsigned char* versym
,
1596 const std::vector
<const char*>* version_map
);
1599 #ifdef HAVE_TARGET_64_BIG
1602 Symbol_table::add_from_dynobj
<64, true>(
1603 Sized_dynobj
<64, true>* dynobj
,
1604 const unsigned char* syms
,
1606 const char* sym_names
,
1607 size_t sym_name_size
,
1608 const unsigned char* versym
,
1610 const std::vector
<const char*>* version_map
);
1613 } // End namespace gold.