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->got_offset_
= 0;
33 this->binding_
= binding
;
34 this->visibility_
= visibility
;
35 this->nonvis_
= nonvis
;
36 this->is_target_special_
= false;
37 this->is_def_
= false;
38 this->is_forwarder_
= false;
39 this->in_dyn_
= false;
40 this->has_got_offset_
= false;
41 this->has_warning_
= false;
44 // Initialize the fields in the base class Symbol for SYM in OBJECT.
46 template<int size
, bool big_endian
>
48 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
49 const elfcpp::Sym
<size
, big_endian
>& sym
)
51 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
52 sym
.get_st_visibility(), sym
.get_st_nonvis());
53 this->u_
.from_object
.object
= object
;
54 // FIXME: Handle SHN_XINDEX.
55 this->u_
.from_object
.shnum
= sym
.get_st_shndx();
56 this->source_
= FROM_OBJECT
;
57 this->in_dyn_
= object
->is_dynamic();
60 // Initialize the fields in the base class Symbol for a symbol defined
64 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
65 elfcpp::STB binding
, elfcpp::STV visibility
,
66 unsigned char nonvis
, bool offset_is_from_end
)
68 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
69 this->u_
.in_output_data
.output_data
= od
;
70 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
71 this->source_
= IN_OUTPUT_DATA
;
74 // Initialize the fields in the base class Symbol for a symbol defined
75 // in an Output_segment.
78 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
79 elfcpp::STB binding
, elfcpp::STV visibility
,
80 unsigned char nonvis
, Segment_offset_base offset_base
)
82 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
83 this->u_
.in_output_segment
.output_segment
= os
;
84 this->u_
.in_output_segment
.offset_base
= offset_base
;
85 this->source_
= IN_OUTPUT_SEGMENT
;
88 // Initialize the fields in the base class Symbol for a symbol defined
92 Symbol::init_base(const char* name
, elfcpp::STT type
,
93 elfcpp::STB binding
, elfcpp::STV visibility
,
96 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
97 this->source_
= CONSTANT
;
100 // Initialize the fields in Sized_symbol for SYM in OBJECT.
103 template<bool big_endian
>
105 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
106 const elfcpp::Sym
<size
, big_endian
>& sym
)
108 this->init_base(name
, version
, object
, sym
);
109 this->value_
= sym
.get_st_value();
110 this->symsize_
= sym
.get_st_size();
113 // Initialize the fields in Sized_symbol for a symbol defined in an
118 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
119 Value_type value
, Size_type symsize
,
120 elfcpp::STT type
, elfcpp::STB binding
,
121 elfcpp::STV visibility
, unsigned char nonvis
,
122 bool offset_is_from_end
)
124 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
126 this->value_
= value
;
127 this->symsize_
= symsize
;
130 // Initialize the fields in Sized_symbol for a symbol defined in an
135 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
136 Value_type value
, Size_type symsize
,
137 elfcpp::STT type
, elfcpp::STB binding
,
138 elfcpp::STV visibility
, unsigned char nonvis
,
139 Segment_offset_base offset_base
)
141 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
142 this->value_
= value
;
143 this->symsize_
= symsize
;
146 // Initialize the fields in Sized_symbol for a symbol defined as a
151 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
152 elfcpp::STT type
, elfcpp::STB binding
,
153 elfcpp::STV visibility
, unsigned char nonvis
)
155 this->init_base(name
, type
, binding
, visibility
, nonvis
);
156 this->value_
= value
;
157 this->symsize_
= symsize
;
160 // Class Symbol_table.
162 Symbol_table::Symbol_table()
163 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
164 forwarders_(), commons_(), warnings_()
168 Symbol_table::~Symbol_table()
172 // The hash function. The key is always canonicalized, so we use a
173 // simple combination of the pointers.
176 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
178 return key
.first
^ key
.second
;
181 // The symbol table key equality function. This is only called with
182 // canonicalized name and version strings, so we can use pointer
186 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
187 const Symbol_table_key
& k2
) const
189 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
192 // Make TO a symbol which forwards to FROM.
195 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
197 assert(!from
->is_forwarder() && !to
->is_forwarder());
198 this->forwarders_
[from
] = to
;
199 from
->set_forwarder();
202 // Resolve the forwards from FROM, returning the real symbol.
205 Symbol_table::resolve_forwards(Symbol
* from
) const
207 assert(from
->is_forwarder());
208 Unordered_map
<Symbol
*, Symbol
*>::const_iterator p
=
209 this->forwarders_
.find(from
);
210 assert(p
!= this->forwarders_
.end());
214 // Look up a symbol by name.
217 Symbol_table::lookup(const char* name
, const char* version
) const
219 Stringpool::Key name_key
;
220 name
= this->namepool_
.find(name
, &name_key
);
224 Stringpool::Key version_key
= 0;
227 version
= this->namepool_
.find(version
, &version_key
);
232 Symbol_table_key
key(name_key
, version_key
);
233 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
234 if (p
== this->table_
.end())
239 // Resolve a Symbol with another Symbol. This is only used in the
240 // unusual case where there are references to both an unversioned
241 // symbol and a symbol with a version, and we then discover that that
242 // version is the default version. Because this is unusual, we do
243 // this the slow way, by converting back to an ELF symbol.
245 template<int size
, bool big_endian
>
247 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
250 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
251 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
252 // We don't bother to set the st_name field.
253 esym
.put_st_value(from
->value());
254 esym
.put_st_size(from
->symsize());
255 esym
.put_st_info(from
->binding(), from
->type());
256 esym
.put_st_other(from
->visibility(), from
->nonvis());
257 esym
.put_st_shndx(from
->shnum());
258 Symbol_table::resolve(to
, esym
.sym(), from
->object());
261 // Add one symbol from OBJECT to the symbol table. NAME is symbol
262 // name and VERSION is the version; both are canonicalized. DEF is
263 // whether this is the default version.
265 // If DEF is true, then this is the definition of a default version of
266 // a symbol. That means that any lookup of NAME/NULL and any lookup
267 // of NAME/VERSION should always return the same symbol. This is
268 // obvious for references, but in particular we want to do this for
269 // definitions: overriding NAME/NULL should also override
270 // NAME/VERSION. If we don't do that, it would be very hard to
271 // override functions in a shared library which uses versioning.
273 // We implement this by simply making both entries in the hash table
274 // point to the same Symbol structure. That is easy enough if this is
275 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
276 // that we have seen both already, in which case they will both have
277 // independent entries in the symbol table. We can't simply change
278 // the symbol table entry, because we have pointers to the entries
279 // attached to the object files. So we mark the entry attached to the
280 // object file as a forwarder, and record it in the forwarders_ map.
281 // Note that entries in the hash table will never be marked as
284 template<int size
, bool big_endian
>
286 Symbol_table::add_from_object(Object
* object
,
288 Stringpool::Key name_key
,
290 Stringpool::Key version_key
,
292 const elfcpp::Sym
<size
, big_endian
>& sym
)
294 Symbol
* const snull
= NULL
;
295 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
296 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
299 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
300 std::make_pair(this->table_
.end(), false);
303 const Stringpool::Key vnull_key
= 0;
304 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
309 // ins.first: an iterator, which is a pointer to a pair.
310 // ins.first->first: the key (a pair of name and version).
311 // ins.first->second: the value (Symbol*).
312 // ins.second: true if new entry was inserted, false if not.
314 Sized_symbol
<size
>* ret
;
319 // We already have an entry for NAME/VERSION.
320 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
324 was_undefined
= ret
->is_undefined();
325 was_common
= ret
->is_common();
327 Symbol_table::resolve(ret
, sym
, object
);
333 // This is the first time we have seen NAME/NULL. Make
334 // NAME/NULL point to NAME/VERSION.
335 insdef
.first
->second
= ret
;
339 // This is the unfortunate case where we already have
340 // entries for both NAME/VERSION and NAME/NULL.
341 const Sized_symbol
<size
>* sym2
;
342 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
345 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
346 ret
, sym2
SELECT_SIZE_ENDIAN(size
, big_endian
));
347 this->make_forwarder(insdef
.first
->second
, ret
);
348 insdef
.first
->second
= ret
;
354 // This is the first time we have seen NAME/VERSION.
355 assert(ins
.first
->second
== NULL
);
357 was_undefined
= false;
360 if (def
&& !insdef
.second
)
362 // We already have an entry for NAME/NULL. Make
363 // NAME/VERSION point to it.
364 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
367 Symbol_table::resolve(ret
, sym
, object
);
368 ins
.first
->second
= ret
;
372 Sized_target
<size
, big_endian
>* target
=
373 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
374 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
375 if (!target
->has_make_symbol())
376 ret
= new Sized_symbol
<size
>();
379 ret
= target
->make_symbol();
382 // This means that we don't want a symbol table
385 this->table_
.erase(ins
.first
);
388 this->table_
.erase(insdef
.first
);
389 // Inserting insdef invalidated ins.
390 this->table_
.erase(std::make_pair(name_key
,
397 ret
->init(name
, version
, object
, sym
);
399 ins
.first
->second
= ret
;
402 // This is the first time we have seen NAME/NULL. Point
403 // it at the new entry for NAME/VERSION.
404 assert(insdef
.second
);
405 insdef
.first
->second
= ret
;
410 // Record every time we see a new undefined symbol, to speed up
412 if (!was_undefined
&& ret
->is_undefined())
413 ++this->saw_undefined_
;
415 // Keep track of common symbols, to speed up common symbol
417 if (!was_common
&& ret
->is_common())
418 this->commons_
.push_back(ret
);
423 // Add all the symbols in a relocatable object to the hash table.
425 template<int size
, bool big_endian
>
427 Symbol_table::add_from_object(
429 const unsigned char* syms
,
431 const char* sym_names
,
432 size_t sym_name_size
,
433 Symbol
** sympointers
)
435 // We take the size from the first object we see.
436 if (this->get_size() == 0)
437 this->set_size(size
);
439 if (size
!= this->get_size() || size
!= object
->target()->get_size())
441 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
442 program_name
, object
->name().c_str());
446 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
448 const unsigned char* p
= syms
;
449 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
451 elfcpp::Sym
<size
, big_endian
> sym(p
);
452 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
454 unsigned int st_name
= psym
->get_st_name();
455 if (st_name
>= sym_name_size
)
458 _("%s: %s: bad global symbol name offset %u at %lu\n"),
459 program_name
, object
->name().c_str(), st_name
,
460 static_cast<unsigned long>(i
));
464 // A symbol defined in a section which we are not including must
465 // be treated as an undefined symbol.
466 unsigned char symbuf
[sym_size
];
467 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
468 unsigned int st_shndx
= psym
->get_st_shndx();
469 if (st_shndx
!= elfcpp::SHN_UNDEF
470 && st_shndx
< elfcpp::SHN_LORESERVE
471 && !object
->is_section_included(st_shndx
))
473 memcpy(symbuf
, p
, sym_size
);
474 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
475 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
479 const char* name
= sym_names
+ st_name
;
481 // In an object file, an '@' in the name separates the symbol
482 // name from the version name. If there are two '@' characters,
483 // this is the default version.
484 const char* ver
= strchr(name
, '@');
489 Stringpool::Key name_key
;
490 name
= this->namepool_
.add(name
, &name_key
);
491 res
= this->add_from_object(object
, name
, name_key
, NULL
, 0,
496 Stringpool::Key name_key
;
497 name
= this->namepool_
.add(name
, ver
- name
, &name_key
);
507 Stringpool::Key ver_key
;
508 ver
= this->namepool_
.add(ver
, &ver_key
);
510 res
= this->add_from_object(object
, name
, name_key
, ver
, ver_key
,
514 *sympointers
++ = res
;
518 // Create and return a specially defined symbol. If ONLY_IF_REF is
519 // true, then only create the symbol if there is a reference to it.
521 template<int size
, bool big_endian
>
523 Symbol_table::define_special_symbol(Target
* target
, const char* name
,
527 assert(this->size_
== size
);
530 Sized_symbol
<size
>* sym
;
534 oldsym
= this->lookup(name
, NULL
);
535 if (oldsym
== NULL
|| !oldsym
->is_undefined())
539 // Canonicalize NAME.
540 name
= oldsym
->name();
544 // Canonicalize NAME.
545 Stringpool::Key name_key
;
546 name
= this->namepool_
.add(name
, &name_key
);
548 Symbol
* const snull
= NULL
;
549 const Stringpool::Key ver_key
= 0;
550 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
551 this->table_
.insert(std::make_pair(std::make_pair(name_key
, ver_key
),
556 // We already have a symbol table entry for NAME.
557 oldsym
= ins
.first
->second
;
558 assert(oldsym
!= NULL
);
563 // We haven't seen this symbol before.
564 assert(ins
.first
->second
== NULL
);
566 if (!target
->has_make_symbol())
567 sym
= new Sized_symbol
<size
>();
570 assert(target
->get_size() == size
);
571 assert(target
->is_big_endian() ? big_endian
: !big_endian
);
572 typedef Sized_target
<size
, big_endian
> My_target
;
573 My_target
* sized_target
= static_cast<My_target
*>(target
);
574 sym
= sized_target
->make_symbol();
579 ins
.first
->second
= sym
;
588 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
590 assert(sym
->source() == Symbol::FROM_OBJECT
);
591 const int old_shnum
= sym
->shnum();
592 if (old_shnum
!= elfcpp::SHN_UNDEF
593 && old_shnum
!= elfcpp::SHN_COMMON
594 && !sym
->object()->is_dynamic())
596 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
598 // FIXME: Report old location. Record that we have seen an
603 // Our new definition is going to override the old reference.
609 // Define a symbol based on an Output_data.
612 Symbol_table::define_in_output_data(Target
* target
, const char* name
,
614 uint64_t value
, uint64_t symsize
,
615 elfcpp::STT type
, elfcpp::STB binding
,
616 elfcpp::STV visibility
,
617 unsigned char nonvis
,
618 bool offset_is_from_end
,
621 assert(target
->get_size() == this->size_
);
622 if (this->size_
== 32)
623 this->do_define_in_output_data
<32>(target
, name
, od
, value
, symsize
,
624 type
, binding
, visibility
, nonvis
,
625 offset_is_from_end
, only_if_ref
);
626 else if (this->size_
== 64)
627 this->do_define_in_output_data
<64>(target
, name
, od
, value
, symsize
,
628 type
, binding
, visibility
, nonvis
,
629 offset_is_from_end
, only_if_ref
);
634 // Define a symbol in an Output_data, sized version.
638 Symbol_table::do_define_in_output_data(
642 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
643 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
646 elfcpp::STV visibility
,
647 unsigned char nonvis
,
648 bool offset_is_from_end
,
651 Sized_symbol
<size
>* sym
;
653 if (target
->is_big_endian())
654 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
655 target
, name
, only_if_ref
656 SELECT_SIZE_ENDIAN(size
, true));
658 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
659 target
, name
, only_if_ref
660 SELECT_SIZE_ENDIAN(size
, false));
665 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
669 // Define a symbol based on an Output_segment.
672 Symbol_table::define_in_output_segment(Target
* target
, const char* name
,
674 uint64_t value
, uint64_t symsize
,
675 elfcpp::STT type
, elfcpp::STB binding
,
676 elfcpp::STV visibility
,
677 unsigned char nonvis
,
678 Symbol::Segment_offset_base offset_base
,
681 assert(target
->get_size() == this->size_
);
682 if (this->size_
== 32)
683 this->do_define_in_output_segment
<32>(target
, name
, os
, value
, symsize
,
684 type
, binding
, visibility
, nonvis
,
685 offset_base
, only_if_ref
);
686 else if (this->size_
== 64)
687 this->do_define_in_output_segment
<64>(target
, name
, os
, value
, symsize
,
688 type
, binding
, visibility
, nonvis
,
689 offset_base
, only_if_ref
);
694 // Define a symbol in an Output_segment, sized version.
698 Symbol_table::do_define_in_output_segment(
702 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
703 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
706 elfcpp::STV visibility
,
707 unsigned char nonvis
,
708 Symbol::Segment_offset_base offset_base
,
711 Sized_symbol
<size
>* sym
;
713 if (target
->is_big_endian())
714 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
715 target
, name
, only_if_ref
716 SELECT_SIZE_ENDIAN(size
, true));
718 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
719 target
, name
, only_if_ref
720 SELECT_SIZE_ENDIAN(size
, false));
725 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
729 // Define a special symbol with a constant value. It is a multiple
730 // definition error if this symbol is already defined.
733 Symbol_table::define_as_constant(Target
* target
, const char* name
,
734 uint64_t value
, uint64_t symsize
,
735 elfcpp::STT type
, elfcpp::STB binding
,
736 elfcpp::STV visibility
, unsigned char nonvis
,
739 assert(target
->get_size() == this->size_
);
740 if (this->size_
== 32)
741 this->do_define_as_constant
<32>(target
, name
, value
, symsize
,
742 type
, binding
, visibility
, nonvis
,
744 else if (this->size_
== 64)
745 this->do_define_as_constant
<64>(target
, name
, value
, symsize
,
746 type
, binding
, visibility
, nonvis
,
752 // Define a symbol as a constant, sized version.
756 Symbol_table::do_define_as_constant(
759 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
760 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
763 elfcpp::STV visibility
,
764 unsigned char nonvis
,
767 Sized_symbol
<size
>* sym
;
769 if (target
->is_big_endian())
770 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
771 target
, name
, only_if_ref
772 SELECT_SIZE_ENDIAN(size
, true));
774 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
775 target
, name
, only_if_ref
776 SELECT_SIZE_ENDIAN(size
, false));
781 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
784 // Define a set of symbols in output sections.
787 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
788 const Define_symbol_in_section
* p
)
790 for (int i
= 0; i
< count
; ++i
, ++p
)
792 Output_section
* os
= layout
->find_output_section(p
->output_section
);
794 this->define_in_output_data(target
, p
->name
, os
, p
->value
, p
->size
,
795 p
->type
, p
->binding
, p
->visibility
,
796 p
->nonvis
, p
->offset_is_from_end
,
799 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
800 p
->binding
, p
->visibility
, p
->nonvis
,
805 // Define a set of symbols in output segments.
808 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
809 const Define_symbol_in_segment
* p
)
811 for (int i
= 0; i
< count
; ++i
, ++p
)
813 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
814 p
->segment_flags_set
,
815 p
->segment_flags_clear
);
817 this->define_in_output_segment(target
, p
->name
, os
, p
->value
, p
->size
,
818 p
->type
, p
->binding
, p
->visibility
,
819 p
->nonvis
, p
->offset_base
,
822 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
823 p
->binding
, p
->visibility
, p
->nonvis
,
828 // Set the final values for all the symbols. Record the file offset
829 // OFF. Add their names to POOL. Return the new file offset.
832 Symbol_table::finalize(off_t off
, Stringpool
* pool
)
836 if (this->size_
== 32)
837 ret
= this->sized_finalize
<32>(off
, pool
);
838 else if (this->size_
== 64)
839 ret
= this->sized_finalize
<64>(off
, pool
);
843 // Now that we have the final symbol table, we can reliably note
844 // which symbols should get warnings.
845 this->warnings_
.note_warnings(this);
850 // Set the final value for all the symbols. This is called after
851 // Layout::finalize, so all the output sections have their final
856 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
)
858 off
= align_address(off
, size
>> 3);
861 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
862 Symbol_table_type::iterator p
= this->table_
.begin();
864 while (p
!= this->table_
.end())
866 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
868 // FIXME: Here we need to decide which symbols should go into
871 typename Sized_symbol
<size
>::Value_type value
;
873 switch (sym
->source())
875 case Symbol::FROM_OBJECT
:
877 unsigned int shnum
= sym
->shnum();
879 // FIXME: We need some target specific support here.
880 if (shnum
>= elfcpp::SHN_LORESERVE
881 && shnum
!= elfcpp::SHN_ABS
)
883 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
884 program_name
, sym
->name(), shnum
);
888 Object
* symobj
= sym
->object();
889 if (symobj
->is_dynamic())
892 shnum
= elfcpp::SHN_UNDEF
;
894 else if (shnum
== elfcpp::SHN_UNDEF
)
896 else if (shnum
== elfcpp::SHN_ABS
)
897 value
= sym
->value();
900 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
902 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
906 // We should be able to erase this symbol from the
907 // symbol table, but at least with gcc 4.0.2
908 // std::unordered_map::erase doesn't appear to return
910 // p = this->table_.erase(p);
915 value
= sym
->value() + os
->address() + secoff
;
920 case Symbol::IN_OUTPUT_DATA
:
922 Output_data
* od
= sym
->output_data();
923 value
= sym
->value() + od
->address();
924 if (sym
->offset_is_from_end())
925 value
+= od
->data_size();
929 case Symbol::IN_OUTPUT_SEGMENT
:
931 Output_segment
* os
= sym
->output_segment();
932 value
= sym
->value() + os
->vaddr();
933 switch (sym
->offset_base())
935 case Symbol::SEGMENT_START
:
937 case Symbol::SEGMENT_END
:
938 value
+= os
->memsz();
940 case Symbol::SEGMENT_BSS
:
941 value
+= os
->filesz();
949 case Symbol::CONSTANT
:
950 value
= sym
->value();
957 sym
->set_value(value
);
958 pool
->add(sym
->name(), NULL
);
964 this->output_count_
= count
;
969 // Write out the global symbols.
972 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
973 Output_file
* of
) const
975 if (this->size_
== 32)
977 if (target
->is_big_endian())
978 this->sized_write_globals
<32, true>(target
, sympool
, of
);
980 this->sized_write_globals
<32, false>(target
, sympool
, of
);
982 else if (this->size_
== 64)
984 if (target
->is_big_endian())
985 this->sized_write_globals
<64, true>(target
, sympool
, of
);
987 this->sized_write_globals
<64, false>(target
, sympool
, of
);
993 // Write out the global symbols.
995 template<int size
, bool big_endian
>
997 Symbol_table::sized_write_globals(const Target
*,
998 const Stringpool
* sympool
,
999 Output_file
* of
) const
1001 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1002 unsigned char* psyms
= of
->get_output_view(this->offset_
,
1003 this->output_count_
* sym_size
);
1004 unsigned char* ps
= psyms
;
1005 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1006 p
!= this->table_
.end();
1009 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1012 switch (sym
->source())
1014 case Symbol::FROM_OBJECT
:
1016 unsigned int shnum
= sym
->shnum();
1018 // FIXME: We need some target specific support here.
1019 if (shnum
>= elfcpp::SHN_LORESERVE
1020 && shnum
!= elfcpp::SHN_ABS
)
1022 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1023 program_name
, sym
->name(), sym
->shnum());
1027 Object
* symobj
= sym
->object();
1028 if (symobj
->is_dynamic())
1031 shndx
= elfcpp::SHN_UNDEF
;
1033 else if (shnum
== elfcpp::SHN_UNDEF
|| shnum
== elfcpp::SHN_ABS
)
1037 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1039 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
1043 shndx
= os
->out_shndx();
1048 case Symbol::IN_OUTPUT_DATA
:
1049 shndx
= sym
->output_data()->out_shndx();
1052 case Symbol::IN_OUTPUT_SEGMENT
:
1053 shndx
= elfcpp::SHN_ABS
;
1056 case Symbol::CONSTANT
:
1057 shndx
= elfcpp::SHN_ABS
;
1064 elfcpp::Sym_write
<size
, big_endian
> osym(ps
);
1065 osym
.put_st_name(sympool
->get_offset(sym
->name()));
1066 osym
.put_st_value(sym
->value());
1067 osym
.put_st_size(sym
->symsize());
1068 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1069 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(),
1071 osym
.put_st_shndx(shndx
);
1076 of
->write_output_view(this->offset_
, this->output_count_
* sym_size
, psyms
);
1079 // Warnings functions.
1081 // Add a new warning.
1084 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1087 name
= symtab
->canonicalize_name(name
);
1088 this->warnings_
[name
].set(obj
, shndx
);
1091 // Look through the warnings and mark the symbols for which we should
1092 // warn. This is called during Layout::finalize when we know the
1093 // sources for all the symbols.
1096 Warnings::note_warnings(Symbol_table
* symtab
)
1098 for (Warning_table::iterator p
= this->warnings_
.begin();
1099 p
!= this->warnings_
.end();
1102 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1104 && sym
->source() == Symbol::FROM_OBJECT
1105 && sym
->object() == p
->second
.object
)
1107 sym
->set_has_warning();
1109 // Read the section contents to get the warning text. It
1110 // would be nicer if we only did this if we have to actually
1111 // issue a warning. Unfortunately, warnings are issued as
1112 // we relocate sections. That means that we can not lock
1113 // the object then, as we might try to issue the same
1114 // warning multiple times simultaneously.
1116 Task_locker_obj
<Object
> tl(*p
->second
.object
);
1117 const unsigned char* c
;
1119 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
);
1120 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1126 // Issue a warning. This is called when we see a relocation against a
1127 // symbol for which has a warning.
1130 Warnings::issue_warning(Symbol
* sym
, const std::string
& location
) const
1132 assert(sym
->has_warning());
1133 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1134 assert(p
!= this->warnings_
.end());
1135 fprintf(stderr
, _("%s: %s: warning: %s\n"), program_name
, location
.c_str(),
1136 p
->second
.text
.c_str());
1139 // Instantiate the templates we need. We could use the configure
1140 // script to restrict this to only the ones needed for implemented
1145 Symbol_table::add_from_object
<32, true>(
1147 const unsigned char* syms
,
1149 const char* sym_names
,
1150 size_t sym_name_size
,
1151 Symbol
** sympointers
);
1155 Symbol_table::add_from_object
<32, false>(
1157 const unsigned char* syms
,
1159 const char* sym_names
,
1160 size_t sym_name_size
,
1161 Symbol
** sympointers
);
1165 Symbol_table::add_from_object
<64, true>(
1167 const unsigned char* syms
,
1169 const char* sym_names
,
1170 size_t sym_name_size
,
1171 Symbol
** sympointers
);
1175 Symbol_table::add_from_object
<64, false>(
1177 const unsigned char* syms
,
1179 const char* sym_names
,
1180 size_t sym_name_size
,
1181 Symbol
** sympointers
);
1183 } // End namespace gold.