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;
35 this->binding_
= binding
;
36 this->visibility_
= visibility
;
37 this->nonvis_
= nonvis
;
38 this->is_target_special_
= false;
39 this->is_def_
= false;
40 this->is_forwarder_
= false;
41 this->needs_dynsym_entry_
= false;
42 this->in_dyn_
= false;
43 this->has_got_offset_
= false;
44 this->has_warning_
= false;
47 // Initialize the fields in the base class Symbol for SYM in OBJECT.
49 template<int size
, bool big_endian
>
51 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
52 const elfcpp::Sym
<size
, big_endian
>& sym
)
54 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
55 sym
.get_st_visibility(), sym
.get_st_nonvis());
56 this->u_
.from_object
.object
= object
;
57 // FIXME: Handle SHN_XINDEX.
58 this->u_
.from_object
.shndx
= sym
.get_st_shndx();
59 this->source_
= FROM_OBJECT
;
60 this->in_dyn_
= object
->is_dynamic();
63 // Initialize the fields in the base class Symbol for a symbol defined
67 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
68 elfcpp::STB binding
, elfcpp::STV visibility
,
69 unsigned char nonvis
, bool offset_is_from_end
)
71 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
72 this->u_
.in_output_data
.output_data
= od
;
73 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
74 this->source_
= IN_OUTPUT_DATA
;
77 // Initialize the fields in the base class Symbol for a symbol defined
78 // in an Output_segment.
81 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
82 elfcpp::STB binding
, elfcpp::STV visibility
,
83 unsigned char nonvis
, Segment_offset_base offset_base
)
85 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
86 this->u_
.in_output_segment
.output_segment
= os
;
87 this->u_
.in_output_segment
.offset_base
= offset_base
;
88 this->source_
= IN_OUTPUT_SEGMENT
;
91 // Initialize the fields in the base class Symbol for a symbol defined
95 Symbol::init_base(const char* name
, elfcpp::STT type
,
96 elfcpp::STB binding
, elfcpp::STV visibility
,
99 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
100 this->source_
= CONSTANT
;
103 // Initialize the fields in Sized_symbol for SYM in OBJECT.
106 template<bool big_endian
>
108 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
109 const elfcpp::Sym
<size
, big_endian
>& sym
)
111 this->init_base(name
, version
, object
, sym
);
112 this->value_
= sym
.get_st_value();
113 this->symsize_
= sym
.get_st_size();
116 // Initialize the fields in Sized_symbol for a symbol defined in an
121 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
122 Value_type value
, Size_type symsize
,
123 elfcpp::STT type
, elfcpp::STB binding
,
124 elfcpp::STV visibility
, unsigned char nonvis
,
125 bool offset_is_from_end
)
127 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
129 this->value_
= value
;
130 this->symsize_
= symsize
;
133 // Initialize the fields in Sized_symbol for a symbol defined in an
138 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
139 Value_type value
, Size_type symsize
,
140 elfcpp::STT type
, elfcpp::STB binding
,
141 elfcpp::STV visibility
, unsigned char nonvis
,
142 Segment_offset_base offset_base
)
144 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
145 this->value_
= value
;
146 this->symsize_
= symsize
;
149 // Initialize the fields in Sized_symbol for a symbol defined as a
154 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
155 elfcpp::STT type
, elfcpp::STB binding
,
156 elfcpp::STV visibility
, unsigned char nonvis
)
158 this->init_base(name
, type
, binding
, visibility
, nonvis
);
159 this->value_
= value
;
160 this->symsize_
= symsize
;
163 // Class Symbol_table.
165 Symbol_table::Symbol_table()
166 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
167 forwarders_(), commons_(), warnings_()
171 Symbol_table::~Symbol_table()
175 // The hash function. The key is always canonicalized, so we use a
176 // simple combination of the pointers.
179 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
181 return key
.first
^ key
.second
;
184 // The symbol table key equality function. This is only called with
185 // canonicalized name and version strings, so we can use pointer
189 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
190 const Symbol_table_key
& k2
) const
192 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
195 // Make TO a symbol which forwards to FROM.
198 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
200 gold_assert(from
!= to
);
201 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
202 this->forwarders_
[from
] = to
;
203 from
->set_forwarder();
206 // Resolve the forwards from FROM, returning the real symbol.
209 Symbol_table::resolve_forwards(const Symbol
* from
) const
211 gold_assert(from
->is_forwarder());
212 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
213 this->forwarders_
.find(from
);
214 gold_assert(p
!= this->forwarders_
.end());
218 // Look up a symbol by name.
221 Symbol_table::lookup(const char* name
, const char* version
) const
223 Stringpool::Key name_key
;
224 name
= this->namepool_
.find(name
, &name_key
);
228 Stringpool::Key version_key
= 0;
231 version
= this->namepool_
.find(version
, &version_key
);
236 Symbol_table_key
key(name_key
, version_key
);
237 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
238 if (p
== this->table_
.end())
243 // Resolve a Symbol with another Symbol. This is only used in the
244 // unusual case where there are references to both an unversioned
245 // symbol and a symbol with a version, and we then discover that that
246 // version is the default version. Because this is unusual, we do
247 // this the slow way, by converting back to an ELF symbol.
249 template<int size
, bool big_endian
>
251 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
254 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
255 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
256 // We don't bother to set the st_name field.
257 esym
.put_st_value(from
->value());
258 esym
.put_st_size(from
->symsize());
259 esym
.put_st_info(from
->binding(), from
->type());
260 esym
.put_st_other(from
->visibility(), from
->nonvis());
261 esym
.put_st_shndx(from
->shndx());
262 Symbol_table::resolve(to
, esym
.sym(), from
->object());
265 // Add one symbol from OBJECT to the symbol table. NAME is symbol
266 // name and VERSION is the version; both are canonicalized. DEF is
267 // whether this is the default version.
269 // If DEF is true, then this is the definition of a default version of
270 // a symbol. That means that any lookup of NAME/NULL and any lookup
271 // of NAME/VERSION should always return the same symbol. This is
272 // obvious for references, but in particular we want to do this for
273 // definitions: overriding NAME/NULL should also override
274 // NAME/VERSION. If we don't do that, it would be very hard to
275 // override functions in a shared library which uses versioning.
277 // We implement this by simply making both entries in the hash table
278 // point to the same Symbol structure. That is easy enough if this is
279 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
280 // that we have seen both already, in which case they will both have
281 // independent entries in the symbol table. We can't simply change
282 // the symbol table entry, because we have pointers to the entries
283 // attached to the object files. So we mark the entry attached to the
284 // object file as a forwarder, and record it in the forwarders_ map.
285 // Note that entries in the hash table will never be marked as
288 template<int size
, bool big_endian
>
290 Symbol_table::add_from_object(Object
* object
,
292 Stringpool::Key name_key
,
294 Stringpool::Key version_key
,
296 const elfcpp::Sym
<size
, big_endian
>& sym
)
298 Symbol
* const snull
= NULL
;
299 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
300 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
303 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
304 std::make_pair(this->table_
.end(), false);
307 const Stringpool::Key vnull_key
= 0;
308 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
313 // ins.first: an iterator, which is a pointer to a pair.
314 // ins.first->first: the key (a pair of name and version).
315 // ins.first->second: the value (Symbol*).
316 // ins.second: true if new entry was inserted, false if not.
318 Sized_symbol
<size
>* ret
;
323 // We already have an entry for NAME/VERSION.
324 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
326 gold_assert(ret
!= NULL
);
328 was_undefined
= ret
->is_undefined();
329 was_common
= ret
->is_common();
331 Symbol_table::resolve(ret
, sym
, object
);
337 // This is the first time we have seen NAME/NULL. Make
338 // NAME/NULL point to NAME/VERSION.
339 insdef
.first
->second
= ret
;
341 else if (insdef
.first
->second
!= ret
)
343 // This is the unfortunate case where we already have
344 // entries for both NAME/VERSION and NAME/NULL.
345 const Sized_symbol
<size
>* sym2
;
346 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
349 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
350 ret
, sym2
SELECT_SIZE_ENDIAN(size
, big_endian
));
351 this->make_forwarder(insdef
.first
->second
, ret
);
352 insdef
.first
->second
= ret
;
358 // This is the first time we have seen NAME/VERSION.
359 gold_assert(ins
.first
->second
== NULL
);
361 was_undefined
= false;
364 if (def
&& !insdef
.second
)
366 // We already have an entry for NAME/NULL. Make
367 // NAME/VERSION point to it.
368 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
371 Symbol_table::resolve(ret
, sym
, object
);
372 ins
.first
->second
= ret
;
376 Sized_target
<size
, big_endian
>* target
=
377 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
378 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
379 if (!target
->has_make_symbol())
380 ret
= new Sized_symbol
<size
>();
383 ret
= target
->make_symbol();
386 // This means that we don't want a symbol table
389 this->table_
.erase(ins
.first
);
392 this->table_
.erase(insdef
.first
);
393 // Inserting insdef invalidated ins.
394 this->table_
.erase(std::make_pair(name_key
,
401 ret
->init(name
, version
, object
, sym
);
403 ins
.first
->second
= ret
;
406 // This is the first time we have seen NAME/NULL. Point
407 // it at the new entry for NAME/VERSION.
408 gold_assert(insdef
.second
);
409 insdef
.first
->second
= ret
;
414 // Record every time we see a new undefined symbol, to speed up
416 if (!was_undefined
&& ret
->is_undefined())
417 ++this->saw_undefined_
;
419 // Keep track of common symbols, to speed up common symbol
421 if (!was_common
&& ret
->is_common())
422 this->commons_
.push_back(ret
);
427 // Add all the symbols in a relocatable object to the hash table.
429 template<int size
, bool big_endian
>
431 Symbol_table::add_from_relobj(
432 Sized_relobj
<size
, big_endian
>* relobj
,
433 const unsigned char* syms
,
435 const char* sym_names
,
436 size_t sym_name_size
,
437 Symbol
** sympointers
)
439 // We take the size from the first object we see.
440 if (this->get_size() == 0)
441 this->set_size(size
);
443 if (size
!= this->get_size() || size
!= relobj
->target()->get_size())
445 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
446 program_name
, relobj
->name().c_str());
450 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
452 const unsigned char* p
= syms
;
453 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
455 elfcpp::Sym
<size
, big_endian
> sym(p
);
456 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
458 unsigned int st_name
= psym
->get_st_name();
459 if (st_name
>= sym_name_size
)
462 _("%s: %s: bad global symbol name offset %u at %lu\n"),
463 program_name
, relobj
->name().c_str(), st_name
,
464 static_cast<unsigned long>(i
));
468 const char* name
= sym_names
+ st_name
;
470 // A symbol defined in a section which we are not including must
471 // be treated as an undefined symbol.
472 unsigned char symbuf
[sym_size
];
473 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
474 unsigned int st_shndx
= psym
->get_st_shndx();
475 if (st_shndx
!= elfcpp::SHN_UNDEF
476 && st_shndx
< elfcpp::SHN_LORESERVE
477 && !relobj
->is_section_included(st_shndx
))
479 memcpy(symbuf
, p
, sym_size
);
480 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
481 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
485 // In an object file, an '@' in the name separates the symbol
486 // name from the version name. If there are two '@' characters,
487 // this is the default version.
488 const char* ver
= strchr(name
, '@');
493 Stringpool::Key name_key
;
494 name
= this->namepool_
.add(name
, &name_key
);
495 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
500 Stringpool::Key name_key
;
501 name
= this->namepool_
.add(name
, ver
- name
, &name_key
);
511 Stringpool::Key ver_key
;
512 ver
= this->namepool_
.add(ver
, &ver_key
);
514 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
518 *sympointers
++ = res
;
522 // Add all the symbols in a dynamic object to the hash table.
524 template<int size
, bool big_endian
>
526 Symbol_table::add_from_dynobj(
527 Sized_dynobj
<size
, big_endian
>* dynobj
,
528 const unsigned char* syms
,
530 const char* sym_names
,
531 size_t sym_name_size
,
532 const unsigned char* versym
,
534 const std::vector
<const char*>* version_map
)
536 // We take the size from the first object we see.
537 if (this->get_size() == 0)
538 this->set_size(size
);
540 if (size
!= this->get_size() || size
!= dynobj
->target()->get_size())
542 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
543 program_name
, dynobj
->name().c_str());
547 if (versym
!= NULL
&& versym_size
/ 2 < count
)
549 fprintf(stderr
, _("%s: %s: too few symbol versions\n"),
550 program_name
, dynobj
->name().c_str());
554 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
556 const unsigned char* p
= syms
;
557 const unsigned char* vs
= versym
;
558 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
560 elfcpp::Sym
<size
, big_endian
> sym(p
);
562 // Ignore symbols with local binding.
563 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
566 unsigned int st_name
= sym
.get_st_name();
567 if (st_name
>= sym_name_size
)
569 fprintf(stderr
, _("%s: %s: bad symbol name offset %u at %lu\n"),
570 program_name
, dynobj
->name().c_str(), st_name
,
571 static_cast<unsigned long>(i
));
575 const char* name
= sym_names
+ st_name
;
579 Stringpool::Key name_key
;
580 name
= this->namepool_
.add(name
, &name_key
);
581 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
586 // Read the version information.
588 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
590 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
591 v
&= elfcpp::VERSYM_VERSION
;
593 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
))
595 // This symbol should not be visible outside the object.
599 // At this point we are definitely going to add this symbol.
600 Stringpool::Key name_key
;
601 name
= this->namepool_
.add(name
, &name_key
);
603 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
605 // This symbol does not have a version.
606 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0, false, sym
);
610 if (v
>= version_map
->size())
613 _("%s: %s: versym for symbol %zu out of range: %u\n"),
614 program_name
, dynobj
->name().c_str(), i
, v
);
618 const char* version
= (*version_map
)[v
];
621 fprintf(stderr
, _("%s: %s: versym for symbol %zu has no name: %u\n"),
622 program_name
, dynobj
->name().c_str(), i
, v
);
626 Stringpool::Key version_key
;
627 version
= this->namepool_
.add(version
, &version_key
);
629 // If this is an absolute symbol, and the version name and
630 // symbol name are the same, then this is the version definition
631 // symbol. These symbols exist to support using -u to pull in
632 // particular versions. We do not want to record a version for
634 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
&& name_key
== version_key
)
636 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0, false, sym
);
640 const bool def
= !hidden
&& sym
.get_st_shndx() != elfcpp::SHN_UNDEF
;
642 this->add_from_object(dynobj
, name
, name_key
, version
, version_key
,
647 // Create and return a specially defined symbol. If ONLY_IF_REF is
648 // true, then only create the symbol if there is a reference to it.
650 template<int size
, bool big_endian
>
652 Symbol_table::define_special_symbol(Target
* target
, const char* name
,
656 gold_assert(this->size_
== size
);
659 Sized_symbol
<size
>* sym
;
663 oldsym
= this->lookup(name
, NULL
);
664 if (oldsym
== NULL
|| !oldsym
->is_undefined())
668 // Canonicalize NAME.
669 name
= oldsym
->name();
673 // Canonicalize NAME.
674 Stringpool::Key name_key
;
675 name
= this->namepool_
.add(name
, &name_key
);
677 Symbol
* const snull
= NULL
;
678 const Stringpool::Key ver_key
= 0;
679 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
680 this->table_
.insert(std::make_pair(std::make_pair(name_key
, ver_key
),
685 // We already have a symbol table entry for NAME.
686 oldsym
= ins
.first
->second
;
687 gold_assert(oldsym
!= NULL
);
692 // We haven't seen this symbol before.
693 gold_assert(ins
.first
->second
== NULL
);
695 if (!target
->has_make_symbol())
696 sym
= new Sized_symbol
<size
>();
699 gold_assert(target
->get_size() == size
);
700 gold_assert(target
->is_big_endian() ? big_endian
: !big_endian
);
701 typedef Sized_target
<size
, big_endian
> My_target
;
702 My_target
* sized_target
= static_cast<My_target
*>(target
);
703 sym
= sized_target
->make_symbol();
708 ins
.first
->second
= sym
;
715 gold_assert(sym
== NULL
);
717 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
719 gold_assert(sym
->source() == Symbol::FROM_OBJECT
);
720 const int old_shndx
= sym
->shndx();
721 if (old_shndx
!= elfcpp::SHN_UNDEF
722 && old_shndx
!= elfcpp::SHN_COMMON
723 && !sym
->object()->is_dynamic())
725 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
727 // FIXME: Report old location. Record that we have seen an
732 // Our new definition is going to override the old reference.
738 // Define a symbol based on an Output_data.
741 Symbol_table::define_in_output_data(Target
* target
, const char* name
,
743 uint64_t value
, uint64_t symsize
,
744 elfcpp::STT type
, elfcpp::STB binding
,
745 elfcpp::STV visibility
,
746 unsigned char nonvis
,
747 bool offset_is_from_end
,
750 gold_assert(target
->get_size() == this->size_
);
751 if (this->size_
== 32)
752 this->do_define_in_output_data
<32>(target
, name
, od
, value
, symsize
,
753 type
, binding
, visibility
, nonvis
,
754 offset_is_from_end
, only_if_ref
);
755 else if (this->size_
== 64)
756 this->do_define_in_output_data
<64>(target
, name
, od
, value
, symsize
,
757 type
, binding
, visibility
, nonvis
,
758 offset_is_from_end
, only_if_ref
);
763 // Define a symbol in an Output_data, sized version.
767 Symbol_table::do_define_in_output_data(
771 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
772 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
775 elfcpp::STV visibility
,
776 unsigned char nonvis
,
777 bool offset_is_from_end
,
780 Sized_symbol
<size
>* sym
;
782 if (target
->is_big_endian())
783 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
784 target
, name
, only_if_ref
785 SELECT_SIZE_ENDIAN(size
, true));
787 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
788 target
, name
, only_if_ref
789 SELECT_SIZE_ENDIAN(size
, false));
794 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
798 // Define a symbol based on an Output_segment.
801 Symbol_table::define_in_output_segment(Target
* target
, const char* name
,
803 uint64_t value
, uint64_t symsize
,
804 elfcpp::STT type
, elfcpp::STB binding
,
805 elfcpp::STV visibility
,
806 unsigned char nonvis
,
807 Symbol::Segment_offset_base offset_base
,
810 gold_assert(target
->get_size() == this->size_
);
811 if (this->size_
== 32)
812 this->do_define_in_output_segment
<32>(target
, name
, os
, value
, symsize
,
813 type
, binding
, visibility
, nonvis
,
814 offset_base
, only_if_ref
);
815 else if (this->size_
== 64)
816 this->do_define_in_output_segment
<64>(target
, name
, os
, value
, symsize
,
817 type
, binding
, visibility
, nonvis
,
818 offset_base
, only_if_ref
);
823 // Define a symbol in an Output_segment, sized version.
827 Symbol_table::do_define_in_output_segment(
831 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
832 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
835 elfcpp::STV visibility
,
836 unsigned char nonvis
,
837 Symbol::Segment_offset_base offset_base
,
840 Sized_symbol
<size
>* sym
;
842 if (target
->is_big_endian())
843 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
844 target
, name
, only_if_ref
845 SELECT_SIZE_ENDIAN(size
, true));
847 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
848 target
, name
, only_if_ref
849 SELECT_SIZE_ENDIAN(size
, false));
854 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
858 // Define a special symbol with a constant value. It is a multiple
859 // definition error if this symbol is already defined.
862 Symbol_table::define_as_constant(Target
* target
, const char* name
,
863 uint64_t value
, uint64_t symsize
,
864 elfcpp::STT type
, elfcpp::STB binding
,
865 elfcpp::STV visibility
, unsigned char nonvis
,
868 gold_assert(target
->get_size() == this->size_
);
869 if (this->size_
== 32)
870 this->do_define_as_constant
<32>(target
, name
, value
, symsize
,
871 type
, binding
, visibility
, nonvis
,
873 else if (this->size_
== 64)
874 this->do_define_as_constant
<64>(target
, name
, value
, symsize
,
875 type
, binding
, visibility
, nonvis
,
881 // Define a symbol as a constant, sized version.
885 Symbol_table::do_define_as_constant(
888 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
889 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
892 elfcpp::STV visibility
,
893 unsigned char nonvis
,
896 Sized_symbol
<size
>* sym
;
898 if (target
->is_big_endian())
899 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
900 target
, name
, only_if_ref
901 SELECT_SIZE_ENDIAN(size
, true));
903 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
904 target
, name
, only_if_ref
905 SELECT_SIZE_ENDIAN(size
, false));
910 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
913 // Define a set of symbols in output sections.
916 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
917 const Define_symbol_in_section
* p
)
919 for (int i
= 0; i
< count
; ++i
, ++p
)
921 Output_section
* os
= layout
->find_output_section(p
->output_section
);
923 this->define_in_output_data(target
, p
->name
, os
, p
->value
, p
->size
,
924 p
->type
, p
->binding
, p
->visibility
,
925 p
->nonvis
, p
->offset_is_from_end
,
928 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
929 p
->binding
, p
->visibility
, p
->nonvis
,
934 // Define a set of symbols in output segments.
937 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
938 const Define_symbol_in_segment
* p
)
940 for (int i
= 0; i
< count
; ++i
, ++p
)
942 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
943 p
->segment_flags_set
,
944 p
->segment_flags_clear
);
946 this->define_in_output_segment(target
, p
->name
, os
, p
->value
, p
->size
,
947 p
->type
, p
->binding
, p
->visibility
,
948 p
->nonvis
, p
->offset_base
,
951 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
952 p
->binding
, p
->visibility
, p
->nonvis
,
957 // Set the dynamic symbol indexes. INDEX is the index of the first
958 // global dynamic symbol. Pointers to the symbols are stored into the
959 // vector SYMS. The names are added to DYNPOOL. This returns an
960 // updated dynamic symbol index.
963 Symbol_table::set_dynsym_indexes(unsigned int index
,
964 std::vector
<Symbol
*>* syms
,
967 for (Symbol_table_type::iterator p
= this->table_
.begin();
968 p
!= this->table_
.end();
971 Symbol
* sym
= p
->second
;
973 // Note that SYM may already have a dynamic symbol index, since
974 // some symbols appear more than once in the symbol table, with
975 // and without a version.
977 if (!sym
->needs_dynsym_entry())
978 sym
->set_dynsym_index(-1U);
979 else if (!sym
->has_dynsym_index())
981 sym
->set_dynsym_index(index
);
983 syms
->push_back(sym
);
984 dynpool
->add(sym
->name(), NULL
);
991 // Set the final values for all the symbols. The index of the first
992 // global symbol in the output file is INDEX. Record the file offset
993 // OFF. Add their names to POOL. Return the new file offset.
996 Symbol_table::finalize(unsigned int index
, off_t off
, off_t dynoff
,
997 size_t dyn_global_index
, size_t dyncount
,
1002 gold_assert(index
!= 0);
1003 this->first_global_index_
= index
;
1005 this->dynamic_offset_
= dynoff
;
1006 this->first_dynamic_global_index_
= dyn_global_index
;
1007 this->dynamic_count_
= dyncount
;
1009 if (this->size_
== 32)
1010 ret
= this->sized_finalize
<32>(index
, off
, pool
);
1011 else if (this->size_
== 64)
1012 ret
= this->sized_finalize
<64>(index
, off
, pool
);
1016 // Now that we have the final symbol table, we can reliably note
1017 // which symbols should get warnings.
1018 this->warnings_
.note_warnings(this);
1023 // Set the final value for all the symbols. This is called after
1024 // Layout::finalize, so all the output sections have their final
1029 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
1031 off
= align_address(off
, size
>> 3);
1032 this->offset_
= off
;
1034 size_t orig_index
= index
;
1036 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1037 for (Symbol_table_type::iterator p
= this->table_
.begin();
1038 p
!= this->table_
.end();
1041 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1043 // FIXME: Here we need to decide which symbols should go into
1044 // the output file, based on --strip.
1046 // The default version of a symbol may appear twice in the
1047 // symbol table. We only need to finalize it once.
1048 if (sym
->has_symtab_index())
1051 typename Sized_symbol
<size
>::Value_type value
;
1053 switch (sym
->source())
1055 case Symbol::FROM_OBJECT
:
1057 unsigned int shndx
= sym
->shndx();
1059 // FIXME: We need some target specific support here.
1060 if (shndx
>= elfcpp::SHN_LORESERVE
1061 && shndx
!= elfcpp::SHN_ABS
)
1063 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1064 program_name
, sym
->name(), shndx
);
1068 Object
* symobj
= sym
->object();
1069 if (symobj
->is_dynamic())
1072 shndx
= elfcpp::SHN_UNDEF
;
1074 else if (shndx
== elfcpp::SHN_UNDEF
)
1076 else if (shndx
== elfcpp::SHN_ABS
)
1077 value
= sym
->value();
1080 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1082 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1086 sym
->set_symtab_index(-1U);
1087 gold_assert(sym
->dynsym_index() == -1U);
1091 value
= sym
->value() + os
->address() + secoff
;
1096 case Symbol::IN_OUTPUT_DATA
:
1098 Output_data
* od
= sym
->output_data();
1099 value
= sym
->value() + od
->address();
1100 if (sym
->offset_is_from_end())
1101 value
+= od
->data_size();
1105 case Symbol::IN_OUTPUT_SEGMENT
:
1107 Output_segment
* os
= sym
->output_segment();
1108 value
= sym
->value() + os
->vaddr();
1109 switch (sym
->offset_base())
1111 case Symbol::SEGMENT_START
:
1113 case Symbol::SEGMENT_END
:
1114 value
+= os
->memsz();
1116 case Symbol::SEGMENT_BSS
:
1117 value
+= os
->filesz();
1125 case Symbol::CONSTANT
:
1126 value
= sym
->value();
1133 sym
->set_value(value
);
1134 sym
->set_symtab_index(index
);
1135 pool
->add(sym
->name(), NULL
);
1140 this->output_count_
= index
- orig_index
;
1145 // Write out the global symbols.
1148 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
1149 const Stringpool
* dynpool
, Output_file
* of
) const
1151 if (this->size_
== 32)
1153 if (target
->is_big_endian())
1154 this->sized_write_globals
<32, true>(target
, sympool
, dynpool
, of
);
1156 this->sized_write_globals
<32, false>(target
, sympool
, dynpool
, of
);
1158 else if (this->size_
== 64)
1160 if (target
->is_big_endian())
1161 this->sized_write_globals
<64, true>(target
, sympool
, dynpool
, of
);
1163 this->sized_write_globals
<64, false>(target
, sympool
, dynpool
, of
);
1169 // Write out the global symbols.
1171 template<int size
, bool big_endian
>
1173 Symbol_table::sized_write_globals(const Target
*,
1174 const Stringpool
* sympool
,
1175 const Stringpool
* dynpool
,
1176 Output_file
* of
) const
1178 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1179 unsigned int index
= this->first_global_index_
;
1180 const off_t oview_size
= this->output_count_
* sym_size
;
1181 unsigned char* const psyms
= of
->get_output_view(this->offset_
, oview_size
);
1183 unsigned int dynamic_count
= this->dynamic_count_
;
1184 off_t dynamic_size
= dynamic_count
* sym_size
;
1185 unsigned int first_dynamic_global_index
= this->first_dynamic_global_index_
;
1186 unsigned char* dynamic_view
;
1187 if (this->dynamic_offset_
== 0)
1188 dynamic_view
= NULL
;
1190 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
1192 unsigned char* ps
= psyms
;
1193 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1194 p
!= this->table_
.end();
1197 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1199 unsigned int sym_index
= sym
->symtab_index();
1200 unsigned int dynsym_index
;
1201 if (dynamic_view
== NULL
)
1204 dynsym_index
= sym
->dynsym_index();
1206 if (sym_index
== -1U && dynsym_index
== -1U)
1208 // This symbol is not included in the output file.
1212 if (sym_index
== index
)
1214 else if (sym_index
!= -1U)
1216 // We have already seen this symbol, because it has a
1218 gold_assert(sym_index
< index
);
1219 if (dynsym_index
== -1U)
1225 switch (sym
->source())
1227 case Symbol::FROM_OBJECT
:
1229 unsigned int in_shndx
= sym
->shndx();
1231 // FIXME: We need some target specific support here.
1232 if (in_shndx
>= elfcpp::SHN_LORESERVE
1233 && in_shndx
!= elfcpp::SHN_ABS
)
1235 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1236 program_name
, sym
->name(), in_shndx
);
1240 Object
* symobj
= sym
->object();
1241 if (symobj
->is_dynamic())
1244 shndx
= elfcpp::SHN_UNDEF
;
1246 else if (in_shndx
== elfcpp::SHN_UNDEF
1247 || in_shndx
== elfcpp::SHN_ABS
)
1251 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1253 Output_section
* os
= relobj
->output_section(in_shndx
, &secoff
);
1254 gold_assert(os
!= NULL
);
1255 shndx
= os
->out_shndx();
1260 case Symbol::IN_OUTPUT_DATA
:
1261 shndx
= sym
->output_data()->out_shndx();
1264 case Symbol::IN_OUTPUT_SEGMENT
:
1265 shndx
= elfcpp::SHN_ABS
;
1268 case Symbol::CONSTANT
:
1269 shndx
= elfcpp::SHN_ABS
;
1276 if (sym_index
!= -1U)
1278 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1279 sym
, shndx
, sympool
, ps
1280 SELECT_SIZE_ENDIAN(size
, big_endian
));
1284 if (dynsym_index
!= -1U)
1286 dynsym_index
-= first_dynamic_global_index
;
1287 gold_assert(dynsym_index
< dynamic_count
);
1288 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
1289 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1290 sym
, shndx
, dynpool
, pd
1291 SELECT_SIZE_ENDIAN(size
, big_endian
));
1295 gold_assert(ps
- psyms
== oview_size
);
1297 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1298 if (dynamic_view
!= NULL
)
1299 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
1302 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1303 // strtab holding the name.
1305 template<int size
, bool big_endian
>
1307 Symbol_table::sized_write_symbol(Sized_symbol
<size
>* sym
,
1309 const Stringpool
* pool
,
1311 ACCEPT_SIZE_ENDIAN
) const
1313 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
1314 osym
.put_st_name(pool
->get_offset(sym
->name()));
1315 osym
.put_st_value(sym
->value());
1316 osym
.put_st_size(sym
->symsize());
1317 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1318 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
1319 osym
.put_st_shndx(shndx
);
1322 // Write out a section symbol. Return the update offset.
1325 Symbol_table::write_section_symbol(const Target
* target
,
1326 const Output_section
*os
,
1330 if (this->size_
== 32)
1332 if (target
->is_big_endian())
1333 this->sized_write_section_symbol
<32, true>(os
, of
, offset
);
1335 this->sized_write_section_symbol
<32, false>(os
, of
, offset
);
1337 else if (this->size_
== 64)
1339 if (target
->is_big_endian())
1340 this->sized_write_section_symbol
<64, true>(os
, of
, offset
);
1342 this->sized_write_section_symbol
<64, false>(os
, of
, offset
);
1348 // Write out a section symbol, specialized for size and endianness.
1350 template<int size
, bool big_endian
>
1352 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
1356 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1358 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
1360 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
1361 osym
.put_st_name(0);
1362 osym
.put_st_value(os
->address());
1363 osym
.put_st_size(0);
1364 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
1365 elfcpp::STT_SECTION
));
1366 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
1367 osym
.put_st_shndx(os
->out_shndx());
1369 of
->write_output_view(offset
, sym_size
, pov
);
1372 // Warnings functions.
1374 // Add a new warning.
1377 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1380 name
= symtab
->canonicalize_name(name
);
1381 this->warnings_
[name
].set(obj
, shndx
);
1384 // Look through the warnings and mark the symbols for which we should
1385 // warn. This is called during Layout::finalize when we know the
1386 // sources for all the symbols.
1389 Warnings::note_warnings(Symbol_table
* symtab
)
1391 for (Warning_table::iterator p
= this->warnings_
.begin();
1392 p
!= this->warnings_
.end();
1395 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1397 && sym
->source() == Symbol::FROM_OBJECT
1398 && sym
->object() == p
->second
.object
)
1400 sym
->set_has_warning();
1402 // Read the section contents to get the warning text. It
1403 // would be nicer if we only did this if we have to actually
1404 // issue a warning. Unfortunately, warnings are issued as
1405 // we relocate sections. That means that we can not lock
1406 // the object then, as we might try to issue the same
1407 // warning multiple times simultaneously.
1409 Task_locker_obj
<Object
> tl(*p
->second
.object
);
1410 const unsigned char* c
;
1412 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
);
1413 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1419 // Issue a warning. This is called when we see a relocation against a
1420 // symbol for which has a warning.
1423 Warnings::issue_warning(const Symbol
* sym
, const std::string
& location
) const
1425 gold_assert(sym
->has_warning());
1426 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1427 gold_assert(p
!= this->warnings_
.end());
1428 fprintf(stderr
, _("%s: %s: warning: %s\n"), program_name
, location
.c_str(),
1429 p
->second
.text
.c_str());
1432 // Instantiate the templates we need. We could use the configure
1433 // script to restrict this to only the ones needed for implemented
1438 Symbol_table::add_from_relobj
<32, true>(
1439 Sized_relobj
<32, true>* relobj
,
1440 const unsigned char* syms
,
1442 const char* sym_names
,
1443 size_t sym_name_size
,
1444 Symbol
** sympointers
);
1448 Symbol_table::add_from_relobj
<32, false>(
1449 Sized_relobj
<32, false>* relobj
,
1450 const unsigned char* syms
,
1452 const char* sym_names
,
1453 size_t sym_name_size
,
1454 Symbol
** sympointers
);
1458 Symbol_table::add_from_relobj
<64, true>(
1459 Sized_relobj
<64, true>* relobj
,
1460 const unsigned char* syms
,
1462 const char* sym_names
,
1463 size_t sym_name_size
,
1464 Symbol
** sympointers
);
1468 Symbol_table::add_from_relobj
<64, false>(
1469 Sized_relobj
<64, false>* relobj
,
1470 const unsigned char* syms
,
1472 const char* sym_names
,
1473 size_t sym_name_size
,
1474 Symbol
** sympointers
);
1478 Symbol_table::add_from_dynobj
<32, true>(
1479 Sized_dynobj
<32, true>* dynobj
,
1480 const unsigned char* syms
,
1482 const char* sym_names
,
1483 size_t sym_name_size
,
1484 const unsigned char* versym
,
1486 const std::vector
<const char*>* version_map
);
1490 Symbol_table::add_from_dynobj
<32, false>(
1491 Sized_dynobj
<32, false>* dynobj
,
1492 const unsigned char* syms
,
1494 const char* sym_names
,
1495 size_t sym_name_size
,
1496 const unsigned char* versym
,
1498 const std::vector
<const char*>* version_map
);
1502 Symbol_table::add_from_dynobj
<64, true>(
1503 Sized_dynobj
<64, true>* dynobj
,
1504 const unsigned char* syms
,
1506 const char* sym_names
,
1507 size_t sym_name_size
,
1508 const unsigned char* versym
,
1510 const std::vector
<const char*>* version_map
);
1514 Symbol_table::add_from_dynobj
<64, false>(
1515 Sized_dynobj
<64, false>* dynobj
,
1516 const unsigned char* syms
,
1518 const char* sym_names
,
1519 size_t sym_name_size
,
1520 const unsigned char* versym
,
1522 const std::vector
<const char*>* version_map
);
1524 } // End namespace gold.