1 // symtab.cc -- the gold symbol table
14 #include "workqueue.h"
22 // Initialize fields in Symbol. This initializes everything except u_
26 Symbol::init_fields(const char* name
, const char* version
,
27 elfcpp::STT type
, elfcpp::STB binding
,
28 elfcpp::STV visibility
, unsigned char nonvis
)
31 this->version_
= version
;
32 this->symtab_index_
= 0;
33 this->dynsym_index_
= 0;
34 this->got_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_dyn_
= false;
44 this->has_got_offset_
= false;
45 this->has_warning_
= false;
48 // Initialize the fields in the base class Symbol for SYM in OBJECT.
50 template<int size
, bool big_endian
>
52 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
53 const elfcpp::Sym
<size
, big_endian
>& sym
)
55 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
56 sym
.get_st_visibility(), sym
.get_st_nonvis());
57 this->u_
.from_object
.object
= object
;
58 // FIXME: Handle SHN_XINDEX.
59 this->u_
.from_object
.shnum
= sym
.get_st_shndx();
60 this->source_
= FROM_OBJECT
;
61 this->in_dyn_
= object
->is_dynamic();
64 // Initialize the fields in the base class Symbol for a symbol defined
68 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
69 elfcpp::STB binding
, elfcpp::STV visibility
,
70 unsigned char nonvis
, bool offset_is_from_end
)
72 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
73 this->u_
.in_output_data
.output_data
= od
;
74 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
75 this->source_
= IN_OUTPUT_DATA
;
78 // Initialize the fields in the base class Symbol for a symbol defined
79 // in an Output_segment.
82 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
83 elfcpp::STB binding
, elfcpp::STV visibility
,
84 unsigned char nonvis
, Segment_offset_base offset_base
)
86 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
87 this->u_
.in_output_segment
.output_segment
= os
;
88 this->u_
.in_output_segment
.offset_base
= offset_base
;
89 this->source_
= IN_OUTPUT_SEGMENT
;
92 // Initialize the fields in the base class Symbol for a symbol defined
96 Symbol::init_base(const char* name
, elfcpp::STT type
,
97 elfcpp::STB binding
, elfcpp::STV visibility
,
100 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
101 this->source_
= CONSTANT
;
104 // Initialize the fields in Sized_symbol for SYM in OBJECT.
107 template<bool big_endian
>
109 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
110 const elfcpp::Sym
<size
, big_endian
>& sym
)
112 this->init_base(name
, version
, object
, sym
);
113 this->value_
= sym
.get_st_value();
114 this->symsize_
= sym
.get_st_size();
117 // Initialize the fields in Sized_symbol for a symbol defined in an
122 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
123 Value_type value
, Size_type symsize
,
124 elfcpp::STT type
, elfcpp::STB binding
,
125 elfcpp::STV visibility
, unsigned char nonvis
,
126 bool offset_is_from_end
)
128 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
130 this->value_
= value
;
131 this->symsize_
= symsize
;
134 // Initialize the fields in Sized_symbol for a symbol defined in an
139 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
140 Value_type value
, Size_type symsize
,
141 elfcpp::STT type
, elfcpp::STB binding
,
142 elfcpp::STV visibility
, unsigned char nonvis
,
143 Segment_offset_base offset_base
)
145 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
146 this->value_
= value
;
147 this->symsize_
= symsize
;
150 // Initialize the fields in Sized_symbol for a symbol defined as a
155 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
156 elfcpp::STT type
, elfcpp::STB binding
,
157 elfcpp::STV visibility
, unsigned char nonvis
)
159 this->init_base(name
, type
, binding
, visibility
, nonvis
);
160 this->value_
= value
;
161 this->symsize_
= symsize
;
164 // Class Symbol_table.
166 Symbol_table::Symbol_table()
167 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
168 forwarders_(), commons_(), warnings_()
172 Symbol_table::~Symbol_table()
176 // The hash function. The key is always canonicalized, so we use a
177 // simple combination of the pointers.
180 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
182 return key
.first
^ key
.second
;
185 // The symbol table key equality function. This is only called with
186 // canonicalized name and version strings, so we can use pointer
190 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
191 const Symbol_table_key
& k2
) const
193 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
196 // Make TO a symbol which forwards to FROM.
199 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
202 assert(!from
->is_forwarder() && !to
->is_forwarder());
203 this->forwarders_
[from
] = to
;
204 from
->set_forwarder();
207 // Resolve the forwards from FROM, returning the real symbol.
210 Symbol_table::resolve_forwards(const Symbol
* from
) const
212 assert(from
->is_forwarder());
213 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
214 this->forwarders_
.find(from
);
215 assert(p
!= this->forwarders_
.end());
219 // Look up a symbol by name.
222 Symbol_table::lookup(const char* name
, const char* version
) const
224 Stringpool::Key name_key
;
225 name
= this->namepool_
.find(name
, &name_key
);
229 Stringpool::Key version_key
= 0;
232 version
= this->namepool_
.find(version
, &version_key
);
237 Symbol_table_key
key(name_key
, version_key
);
238 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
239 if (p
== this->table_
.end())
244 // Resolve a Symbol with another Symbol. This is only used in the
245 // unusual case where there are references to both an unversioned
246 // symbol and a symbol with a version, and we then discover that that
247 // version is the default version. Because this is unusual, we do
248 // this the slow way, by converting back to an ELF symbol.
250 template<int size
, bool big_endian
>
252 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
255 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
256 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
257 // We don't bother to set the st_name field.
258 esym
.put_st_value(from
->value());
259 esym
.put_st_size(from
->symsize());
260 esym
.put_st_info(from
->binding(), from
->type());
261 esym
.put_st_other(from
->visibility(), from
->nonvis());
262 esym
.put_st_shndx(from
->shnum());
263 Symbol_table::resolve(to
, esym
.sym(), from
->object());
266 // Add one symbol from OBJECT to the symbol table. NAME is symbol
267 // name and VERSION is the version; both are canonicalized. DEF is
268 // whether this is the default version.
270 // If DEF is true, then this is the definition of a default version of
271 // a symbol. That means that any lookup of NAME/NULL and any lookup
272 // of NAME/VERSION should always return the same symbol. This is
273 // obvious for references, but in particular we want to do this for
274 // definitions: overriding NAME/NULL should also override
275 // NAME/VERSION. If we don't do that, it would be very hard to
276 // override functions in a shared library which uses versioning.
278 // We implement this by simply making both entries in the hash table
279 // point to the same Symbol structure. That is easy enough if this is
280 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
281 // that we have seen both already, in which case they will both have
282 // independent entries in the symbol table. We can't simply change
283 // the symbol table entry, because we have pointers to the entries
284 // attached to the object files. So we mark the entry attached to the
285 // object file as a forwarder, and record it in the forwarders_ map.
286 // Note that entries in the hash table will never be marked as
289 template<int size
, bool big_endian
>
291 Symbol_table::add_from_object(Object
* object
,
293 Stringpool::Key name_key
,
295 Stringpool::Key version_key
,
297 const elfcpp::Sym
<size
, big_endian
>& sym
)
299 Symbol
* const snull
= NULL
;
300 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
301 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
304 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
305 std::make_pair(this->table_
.end(), false);
308 const Stringpool::Key vnull_key
= 0;
309 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
314 // ins.first: an iterator, which is a pointer to a pair.
315 // ins.first->first: the key (a pair of name and version).
316 // ins.first->second: the value (Symbol*).
317 // ins.second: true if new entry was inserted, false if not.
319 Sized_symbol
<size
>* ret
;
324 // We already have an entry for NAME/VERSION.
325 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
329 was_undefined
= ret
->is_undefined();
330 was_common
= ret
->is_common();
332 Symbol_table::resolve(ret
, sym
, object
);
338 // This is the first time we have seen NAME/NULL. Make
339 // NAME/NULL point to NAME/VERSION.
340 insdef
.first
->second
= ret
;
342 else if (insdef
.first
->second
!= ret
)
344 // This is the unfortunate case where we already have
345 // entries for both NAME/VERSION and NAME/NULL.
346 const Sized_symbol
<size
>* sym2
;
347 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
350 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
351 ret
, sym2
SELECT_SIZE_ENDIAN(size
, big_endian
));
352 this->make_forwarder(insdef
.first
->second
, ret
);
353 insdef
.first
->second
= ret
;
359 // This is the first time we have seen NAME/VERSION.
360 assert(ins
.first
->second
== NULL
);
362 was_undefined
= false;
365 if (def
&& !insdef
.second
)
367 // We already have an entry for NAME/NULL. Make
368 // NAME/VERSION point to it.
369 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
372 Symbol_table::resolve(ret
, sym
, object
);
373 ins
.first
->second
= ret
;
377 Sized_target
<size
, big_endian
>* target
=
378 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
379 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
380 if (!target
->has_make_symbol())
381 ret
= new Sized_symbol
<size
>();
384 ret
= target
->make_symbol();
387 // This means that we don't want a symbol table
390 this->table_
.erase(ins
.first
);
393 this->table_
.erase(insdef
.first
);
394 // Inserting insdef invalidated ins.
395 this->table_
.erase(std::make_pair(name_key
,
402 ret
->init(name
, version
, object
, sym
);
404 ins
.first
->second
= ret
;
407 // This is the first time we have seen NAME/NULL. Point
408 // it at the new entry for NAME/VERSION.
409 assert(insdef
.second
);
410 insdef
.first
->second
= ret
;
415 // Record every time we see a new undefined symbol, to speed up
417 if (!was_undefined
&& ret
->is_undefined())
418 ++this->saw_undefined_
;
420 // Keep track of common symbols, to speed up common symbol
422 if (!was_common
&& ret
->is_common())
423 this->commons_
.push_back(ret
);
428 // Add all the symbols in a relocatable object to the hash table.
430 template<int size
, bool big_endian
>
432 Symbol_table::add_from_relobj(
433 Sized_relobj
<size
, big_endian
>* relobj
,
434 const unsigned char* syms
,
436 const char* sym_names
,
437 size_t sym_name_size
,
438 Symbol
** sympointers
)
440 // We take the size from the first object we see.
441 if (this->get_size() == 0)
442 this->set_size(size
);
444 if (size
!= this->get_size() || size
!= relobj
->target()->get_size())
446 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
447 program_name
, relobj
->name().c_str());
451 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
453 const unsigned char* p
= syms
;
454 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
456 elfcpp::Sym
<size
, big_endian
> sym(p
);
457 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
459 unsigned int st_name
= psym
->get_st_name();
460 if (st_name
>= sym_name_size
)
463 _("%s: %s: bad global symbol name offset %u at %lu\n"),
464 program_name
, relobj
->name().c_str(), st_name
,
465 static_cast<unsigned long>(i
));
469 const char* name
= sym_names
+ st_name
;
471 // A symbol defined in a section which we are not including must
472 // be treated as an undefined symbol.
473 unsigned char symbuf
[sym_size
];
474 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
475 unsigned int st_shndx
= psym
->get_st_shndx();
476 if (st_shndx
!= elfcpp::SHN_UNDEF
477 && st_shndx
< elfcpp::SHN_LORESERVE
478 && !relobj
->is_section_included(st_shndx
))
480 memcpy(symbuf
, p
, sym_size
);
481 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
482 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
486 // In an object file, an '@' in the name separates the symbol
487 // name from the version name. If there are two '@' characters,
488 // this is the default version.
489 const char* ver
= strchr(name
, '@');
494 Stringpool::Key name_key
;
495 name
= this->namepool_
.add(name
, &name_key
);
496 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
501 Stringpool::Key name_key
;
502 name
= this->namepool_
.add(name
, ver
- name
, &name_key
);
512 Stringpool::Key ver_key
;
513 ver
= this->namepool_
.add(ver
, &ver_key
);
515 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
519 *sympointers
++ = res
;
523 // Add all the symbols in a dynamic object to the hash table.
525 template<int size
, bool big_endian
>
527 Symbol_table::add_from_dynobj(
528 Sized_dynobj
<size
, big_endian
>* dynobj
,
529 const unsigned char* syms
,
531 const char* sym_names
,
532 size_t sym_name_size
,
533 const unsigned char* versym
,
535 const std::vector
<const char*>* version_map
)
537 // We take the size from the first object we see.
538 if (this->get_size() == 0)
539 this->set_size(size
);
541 if (size
!= this->get_size() || size
!= dynobj
->target()->get_size())
543 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
544 program_name
, dynobj
->name().c_str());
548 if (versym
!= NULL
&& versym_size
/ 2 < count
)
550 fprintf(stderr
, _("%s: %s: too few symbol versions\n"),
551 program_name
, dynobj
->name().c_str());
555 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
557 const unsigned char* p
= syms
;
558 const unsigned char* vs
= versym
;
559 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
561 elfcpp::Sym
<size
, big_endian
> sym(p
);
563 // Ignore symbols with local binding.
564 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
567 unsigned int st_name
= sym
.get_st_name();
568 if (st_name
>= sym_name_size
)
570 fprintf(stderr
, _("%s: %s: bad symbol name offset %u at %lu\n"),
571 program_name
, dynobj
->name().c_str(), st_name
,
572 static_cast<unsigned long>(i
));
576 const char* name
= sym_names
+ st_name
;
580 Stringpool::Key name_key
;
581 name
= this->namepool_
.add(name
, &name_key
);
582 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
587 // Read the version information.
589 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
591 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
592 v
&= elfcpp::VERSYM_VERSION
;
594 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
))
596 // This symbol should not be visible outside the object.
600 // At this point we are definitely going to add this symbol.
601 Stringpool::Key name_key
;
602 name
= this->namepool_
.add(name
, &name_key
);
604 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
606 // This symbol does not have a version.
607 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0, false, sym
);
611 if (v
>= version_map
->size())
614 _("%s: %s: versym for symbol %zu out of range: %u\n"),
615 program_name
, dynobj
->name().c_str(), i
, v
);
619 const char* version
= (*version_map
)[v
];
622 fprintf(stderr
, _("%s: %s: versym for symbol %zu has no name: %u\n"),
623 program_name
, dynobj
->name().c_str(), i
, v
);
627 Stringpool::Key version_key
;
628 version
= this->namepool_
.add(version
, &version_key
);
630 // If this is an absolute symbol, and the version name and
631 // symbol name are the same, then this is the version definition
632 // symbol. These symbols exist to support using -u to pull in
633 // particular versions. We do not want to record a version for
635 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
&& name_key
== version_key
)
637 this->add_from_object(dynobj
, name
, name_key
, NULL
, 0, false, sym
);
641 const bool def
= !hidden
&& sym
.get_st_shndx() != elfcpp::SHN_UNDEF
;
643 this->add_from_object(dynobj
, name
, name_key
, version
, version_key
,
648 // Create and return a specially defined symbol. If ONLY_IF_REF is
649 // true, then only create the symbol if there is a reference to it.
651 template<int size
, bool big_endian
>
653 Symbol_table::define_special_symbol(Target
* target
, const char* name
,
657 assert(this->size_
== size
);
660 Sized_symbol
<size
>* sym
;
664 oldsym
= this->lookup(name
, NULL
);
665 if (oldsym
== NULL
|| !oldsym
->is_undefined())
669 // Canonicalize NAME.
670 name
= oldsym
->name();
674 // Canonicalize NAME.
675 Stringpool::Key name_key
;
676 name
= this->namepool_
.add(name
, &name_key
);
678 Symbol
* const snull
= NULL
;
679 const Stringpool::Key ver_key
= 0;
680 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
681 this->table_
.insert(std::make_pair(std::make_pair(name_key
, ver_key
),
686 // We already have a symbol table entry for NAME.
687 oldsym
= ins
.first
->second
;
688 assert(oldsym
!= NULL
);
693 // We haven't seen this symbol before.
694 assert(ins
.first
->second
== NULL
);
696 if (!target
->has_make_symbol())
697 sym
= new Sized_symbol
<size
>();
700 assert(target
->get_size() == size
);
701 assert(target
->is_big_endian() ? big_endian
: !big_endian
);
702 typedef Sized_target
<size
, big_endian
> My_target
;
703 My_target
* sized_target
= static_cast<My_target
*>(target
);
704 sym
= sized_target
->make_symbol();
709 ins
.first
->second
= sym
;
718 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
720 assert(sym
->source() == Symbol::FROM_OBJECT
);
721 const int old_shnum
= sym
->shnum();
722 if (old_shnum
!= elfcpp::SHN_UNDEF
723 && old_shnum
!= elfcpp::SHN_COMMON
724 && !sym
->object()->is_dynamic())
726 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
728 // FIXME: Report old location. Record that we have seen an
733 // Our new definition is going to override the old reference.
739 // Define a symbol based on an Output_data.
742 Symbol_table::define_in_output_data(Target
* target
, const char* name
,
744 uint64_t value
, uint64_t symsize
,
745 elfcpp::STT type
, elfcpp::STB binding
,
746 elfcpp::STV visibility
,
747 unsigned char nonvis
,
748 bool offset_is_from_end
,
751 assert(target
->get_size() == this->size_
);
752 if (this->size_
== 32)
753 this->do_define_in_output_data
<32>(target
, name
, od
, value
, symsize
,
754 type
, binding
, visibility
, nonvis
,
755 offset_is_from_end
, only_if_ref
);
756 else if (this->size_
== 64)
757 this->do_define_in_output_data
<64>(target
, name
, od
, value
, symsize
,
758 type
, binding
, visibility
, nonvis
,
759 offset_is_from_end
, only_if_ref
);
764 // Define a symbol in an Output_data, sized version.
768 Symbol_table::do_define_in_output_data(
772 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
773 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
776 elfcpp::STV visibility
,
777 unsigned char nonvis
,
778 bool offset_is_from_end
,
781 Sized_symbol
<size
>* sym
;
783 if (target
->is_big_endian())
784 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
785 target
, name
, only_if_ref
786 SELECT_SIZE_ENDIAN(size
, true));
788 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
789 target
, name
, only_if_ref
790 SELECT_SIZE_ENDIAN(size
, false));
795 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
799 // Define a symbol based on an Output_segment.
802 Symbol_table::define_in_output_segment(Target
* target
, const char* name
,
804 uint64_t value
, uint64_t symsize
,
805 elfcpp::STT type
, elfcpp::STB binding
,
806 elfcpp::STV visibility
,
807 unsigned char nonvis
,
808 Symbol::Segment_offset_base offset_base
,
811 assert(target
->get_size() == this->size_
);
812 if (this->size_
== 32)
813 this->do_define_in_output_segment
<32>(target
, name
, os
, value
, symsize
,
814 type
, binding
, visibility
, nonvis
,
815 offset_base
, only_if_ref
);
816 else if (this->size_
== 64)
817 this->do_define_in_output_segment
<64>(target
, name
, os
, value
, symsize
,
818 type
, binding
, visibility
, nonvis
,
819 offset_base
, only_if_ref
);
824 // Define a symbol in an Output_segment, sized version.
828 Symbol_table::do_define_in_output_segment(
832 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
833 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
836 elfcpp::STV visibility
,
837 unsigned char nonvis
,
838 Symbol::Segment_offset_base offset_base
,
841 Sized_symbol
<size
>* sym
;
843 if (target
->is_big_endian())
844 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
845 target
, name
, only_if_ref
846 SELECT_SIZE_ENDIAN(size
, true));
848 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
849 target
, name
, only_if_ref
850 SELECT_SIZE_ENDIAN(size
, false));
855 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
859 // Define a special symbol with a constant value. It is a multiple
860 // definition error if this symbol is already defined.
863 Symbol_table::define_as_constant(Target
* target
, const char* name
,
864 uint64_t value
, uint64_t symsize
,
865 elfcpp::STT type
, elfcpp::STB binding
,
866 elfcpp::STV visibility
, unsigned char nonvis
,
869 assert(target
->get_size() == this->size_
);
870 if (this->size_
== 32)
871 this->do_define_as_constant
<32>(target
, name
, value
, symsize
,
872 type
, binding
, visibility
, nonvis
,
874 else if (this->size_
== 64)
875 this->do_define_as_constant
<64>(target
, name
, value
, symsize
,
876 type
, binding
, visibility
, nonvis
,
882 // Define a symbol as a constant, sized version.
886 Symbol_table::do_define_as_constant(
889 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
890 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
893 elfcpp::STV visibility
,
894 unsigned char nonvis
,
897 Sized_symbol
<size
>* sym
;
899 if (target
->is_big_endian())
900 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
901 target
, name
, only_if_ref
902 SELECT_SIZE_ENDIAN(size
, true));
904 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
905 target
, name
, only_if_ref
906 SELECT_SIZE_ENDIAN(size
, false));
911 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
914 // Define a set of symbols in output sections.
917 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
918 const Define_symbol_in_section
* p
)
920 for (int i
= 0; i
< count
; ++i
, ++p
)
922 Output_section
* os
= layout
->find_output_section(p
->output_section
);
924 this->define_in_output_data(target
, p
->name
, os
, p
->value
, p
->size
,
925 p
->type
, p
->binding
, p
->visibility
,
926 p
->nonvis
, p
->offset_is_from_end
,
929 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
930 p
->binding
, p
->visibility
, p
->nonvis
,
935 // Define a set of symbols in output segments.
938 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
939 const Define_symbol_in_segment
* p
)
941 for (int i
= 0; i
< count
; ++i
, ++p
)
943 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
944 p
->segment_flags_set
,
945 p
->segment_flags_clear
);
947 this->define_in_output_segment(target
, p
->name
, os
, p
->value
, p
->size
,
948 p
->type
, p
->binding
, p
->visibility
,
949 p
->nonvis
, p
->offset_base
,
952 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
953 p
->binding
, p
->visibility
, p
->nonvis
,
958 // Set the final values for all the symbols. The index of the first
959 // global symbol in the output file is INDEX. Record the file offset
960 // OFF. Add their names to POOL. Return the new file offset.
963 Symbol_table::finalize(unsigned int index
, off_t off
, Stringpool
* pool
)
968 this->first_global_index_
= index
;
970 if (this->size_
== 32)
971 ret
= this->sized_finalize
<32>(index
, off
, pool
);
972 else if (this->size_
== 64)
973 ret
= this->sized_finalize
<64>(index
, off
, pool
);
977 // Now that we have the final symbol table, we can reliably note
978 // which symbols should get warnings.
979 this->warnings_
.note_warnings(this);
984 // Set the final value for all the symbols. This is called after
985 // Layout::finalize, so all the output sections have their final
990 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
992 off
= align_address(off
, size
>> 3);
995 size_t orig_index
= index
;
997 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
998 for (Symbol_table_type::iterator p
= this->table_
.begin();
999 p
!= this->table_
.end();
1002 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1004 // FIXME: Here we need to decide which symbols should go into
1007 typename Sized_symbol
<size
>::Value_type value
;
1009 switch (sym
->source())
1011 case Symbol::FROM_OBJECT
:
1013 unsigned int shnum
= sym
->shnum();
1015 // FIXME: We need some target specific support here.
1016 if (shnum
>= elfcpp::SHN_LORESERVE
1017 && shnum
!= elfcpp::SHN_ABS
)
1019 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1020 program_name
, sym
->name(), shnum
);
1024 Object
* symobj
= sym
->object();
1025 if (symobj
->is_dynamic())
1028 shnum
= elfcpp::SHN_UNDEF
;
1030 else if (shnum
== elfcpp::SHN_UNDEF
)
1032 else if (shnum
== elfcpp::SHN_ABS
)
1033 value
= sym
->value();
1036 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1038 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
1042 sym
->set_symtab_index(-1U);
1046 value
= sym
->value() + os
->address() + secoff
;
1051 case Symbol::IN_OUTPUT_DATA
:
1053 Output_data
* od
= sym
->output_data();
1054 value
= sym
->value() + od
->address();
1055 if (sym
->offset_is_from_end())
1056 value
+= od
->data_size();
1060 case Symbol::IN_OUTPUT_SEGMENT
:
1062 Output_segment
* os
= sym
->output_segment();
1063 value
= sym
->value() + os
->vaddr();
1064 switch (sym
->offset_base())
1066 case Symbol::SEGMENT_START
:
1068 case Symbol::SEGMENT_END
:
1069 value
+= os
->memsz();
1071 case Symbol::SEGMENT_BSS
:
1072 value
+= os
->filesz();
1080 case Symbol::CONSTANT
:
1081 value
= sym
->value();
1088 sym
->set_value(value
);
1089 sym
->set_symtab_index(index
);
1090 pool
->add(sym
->name(), NULL
);
1095 this->output_count_
= index
- orig_index
;
1100 // Write out the global symbols.
1103 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
1104 Output_file
* of
) const
1106 if (this->size_
== 32)
1108 if (target
->is_big_endian())
1109 this->sized_write_globals
<32, true>(target
, sympool
, of
);
1111 this->sized_write_globals
<32, false>(target
, sympool
, of
);
1113 else if (this->size_
== 64)
1115 if (target
->is_big_endian())
1116 this->sized_write_globals
<64, true>(target
, sympool
, of
);
1118 this->sized_write_globals
<64, false>(target
, sympool
, of
);
1124 // Write out the global symbols.
1126 template<int size
, bool big_endian
>
1128 Symbol_table::sized_write_globals(const Target
*,
1129 const Stringpool
* sympool
,
1130 Output_file
* of
) const
1132 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1133 unsigned int index
= this->first_global_index_
;
1134 const off_t oview_size
= this->output_count_
* sym_size
;
1135 unsigned char* psyms
= of
->get_output_view(this->offset_
, oview_size
);
1137 unsigned char* ps
= psyms
;
1138 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1139 p
!= this->table_
.end();
1142 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1144 if (sym
->symtab_index() == -1U)
1148 switch (sym
->source())
1150 case Symbol::FROM_OBJECT
:
1152 unsigned int shnum
= sym
->shnum();
1154 // FIXME: We need some target specific support here.
1155 if (shnum
>= elfcpp::SHN_LORESERVE
1156 && shnum
!= elfcpp::SHN_ABS
)
1158 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1159 program_name
, sym
->name(), sym
->shnum());
1163 Object
* symobj
= sym
->object();
1164 if (symobj
->is_dynamic())
1167 shndx
= elfcpp::SHN_UNDEF
;
1169 else if (shnum
== elfcpp::SHN_UNDEF
|| shnum
== elfcpp::SHN_ABS
)
1173 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1175 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
1177 shndx
= os
->out_shndx();
1182 case Symbol::IN_OUTPUT_DATA
:
1183 shndx
= sym
->output_data()->out_shndx();
1186 case Symbol::IN_OUTPUT_SEGMENT
:
1187 shndx
= elfcpp::SHN_ABS
;
1190 case Symbol::CONSTANT
:
1191 shndx
= elfcpp::SHN_ABS
;
1198 assert(sym
->symtab_index() == index
);
1201 elfcpp::Sym_write
<size
, big_endian
> osym(ps
);
1202 osym
.put_st_name(sympool
->get_offset(sym
->name()));
1203 osym
.put_st_value(sym
->value());
1204 osym
.put_st_size(sym
->symsize());
1205 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1206 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(),
1208 osym
.put_st_shndx(shndx
);
1213 assert(ps
- psyms
== oview_size
);
1215 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1218 // Warnings functions.
1220 // Add a new warning.
1223 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1226 name
= symtab
->canonicalize_name(name
);
1227 this->warnings_
[name
].set(obj
, shndx
);
1230 // Look through the warnings and mark the symbols for which we should
1231 // warn. This is called during Layout::finalize when we know the
1232 // sources for all the symbols.
1235 Warnings::note_warnings(Symbol_table
* symtab
)
1237 for (Warning_table::iterator p
= this->warnings_
.begin();
1238 p
!= this->warnings_
.end();
1241 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1243 && sym
->source() == Symbol::FROM_OBJECT
1244 && sym
->object() == p
->second
.object
)
1246 sym
->set_has_warning();
1248 // Read the section contents to get the warning text. It
1249 // would be nicer if we only did this if we have to actually
1250 // issue a warning. Unfortunately, warnings are issued as
1251 // we relocate sections. That means that we can not lock
1252 // the object then, as we might try to issue the same
1253 // warning multiple times simultaneously.
1255 Task_locker_obj
<Object
> tl(*p
->second
.object
);
1256 const unsigned char* c
;
1258 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
);
1259 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1265 // Issue a warning. This is called when we see a relocation against a
1266 // symbol for which has a warning.
1269 Warnings::issue_warning(const Symbol
* sym
, const std::string
& location
) const
1271 assert(sym
->has_warning());
1272 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1273 assert(p
!= this->warnings_
.end());
1274 fprintf(stderr
, _("%s: %s: warning: %s\n"), program_name
, location
.c_str(),
1275 p
->second
.text
.c_str());
1278 // Instantiate the templates we need. We could use the configure
1279 // script to restrict this to only the ones needed for implemented
1284 Symbol_table::add_from_relobj
<32, true>(
1285 Sized_relobj
<32, true>* relobj
,
1286 const unsigned char* syms
,
1288 const char* sym_names
,
1289 size_t sym_name_size
,
1290 Symbol
** sympointers
);
1294 Symbol_table::add_from_relobj
<32, false>(
1295 Sized_relobj
<32, false>* relobj
,
1296 const unsigned char* syms
,
1298 const char* sym_names
,
1299 size_t sym_name_size
,
1300 Symbol
** sympointers
);
1304 Symbol_table::add_from_relobj
<64, true>(
1305 Sized_relobj
<64, true>* relobj
,
1306 const unsigned char* syms
,
1308 const char* sym_names
,
1309 size_t sym_name_size
,
1310 Symbol
** sympointers
);
1314 Symbol_table::add_from_relobj
<64, false>(
1315 Sized_relobj
<64, false>* relobj
,
1316 const unsigned char* syms
,
1318 const char* sym_names
,
1319 size_t sym_name_size
,
1320 Symbol
** sympointers
);
1324 Symbol_table::add_from_dynobj
<32, true>(
1325 Sized_dynobj
<32, true>* dynobj
,
1326 const unsigned char* syms
,
1328 const char* sym_names
,
1329 size_t sym_name_size
,
1330 const unsigned char* versym
,
1332 const std::vector
<const char*>* version_map
);
1336 Symbol_table::add_from_dynobj
<32, false>(
1337 Sized_dynobj
<32, false>* dynobj
,
1338 const unsigned char* syms
,
1340 const char* sym_names
,
1341 size_t sym_name_size
,
1342 const unsigned char* versym
,
1344 const std::vector
<const char*>* version_map
);
1348 Symbol_table::add_from_dynobj
<64, true>(
1349 Sized_dynobj
<64, true>* dynobj
,
1350 const unsigned char* syms
,
1352 const char* sym_names
,
1353 size_t sym_name_size
,
1354 const unsigned char* versym
,
1356 const std::vector
<const char*>* version_map
);
1360 Symbol_table::add_from_dynobj
<64, false>(
1361 Sized_dynobj
<64, false>* dynobj
,
1362 const unsigned char* syms
,
1364 const char* sym_names
,
1365 size_t sym_name_size
,
1366 const unsigned char* versym
,
1368 const std::vector
<const char*>* version_map
);
1370 } // End namespace gold.