1 // symtab.cc -- the gold symbol table
20 // Initialize fields in Symbol. This initializes everything except u_
24 Symbol::init_fields(const char* name
, const char* version
,
25 elfcpp::STT type
, elfcpp::STB binding
,
26 elfcpp::STV visibility
, unsigned char nonvis
)
29 this->version_
= version
;
30 this->got_offset_
= 0;
32 this->binding_
= binding
;
33 this->visibility_
= visibility
;
34 this->nonvis_
= nonvis
;
35 this->is_target_special_
= false;
36 this->is_def_
= false;
37 this->is_forwarder_
= false;
38 this->in_dyn_
= false;
39 this->has_got_offset_
= false;
40 this->has_warning_
= false;
43 // Initialize the fields in the base class Symbol for SYM in OBJECT.
45 template<int size
, bool big_endian
>
47 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
48 const elfcpp::Sym
<size
, big_endian
>& sym
)
50 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
51 sym
.get_st_visibility(), sym
.get_st_nonvis());
52 this->u_
.from_object
.object
= object
;
53 // FIXME: Handle SHN_XINDEX.
54 this->u_
.from_object
.shnum
= sym
.get_st_shndx();
55 this->source_
= FROM_OBJECT
;
56 this->in_dyn_
= object
->is_dynamic();
59 // Initialize the fields in the base class Symbol for a symbol defined
63 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
64 elfcpp::STB binding
, elfcpp::STV visibility
,
65 unsigned char nonvis
, bool offset_is_from_end
)
67 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
68 this->u_
.in_output_data
.output_data
= od
;
69 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
70 this->source_
= IN_OUTPUT_DATA
;
73 // Initialize the fields in the base class Symbol for a symbol defined
74 // in an Output_segment.
77 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
78 elfcpp::STB binding
, elfcpp::STV visibility
,
79 unsigned char nonvis
, Segment_offset_base offset_base
)
81 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
82 this->u_
.in_output_segment
.output_segment
= os
;
83 this->u_
.in_output_segment
.offset_base
= offset_base
;
84 this->source_
= IN_OUTPUT_SEGMENT
;
87 // Initialize the fields in the base class Symbol for a symbol defined
91 Symbol::init_base(const char* name
, elfcpp::STT type
,
92 elfcpp::STB binding
, elfcpp::STV visibility
,
95 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
96 this->source_
= CONSTANT
;
99 // Initialize the fields in Sized_symbol for SYM in OBJECT.
102 template<bool big_endian
>
104 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
105 const elfcpp::Sym
<size
, big_endian
>& sym
)
107 this->init_base(name
, version
, object
, sym
);
108 this->value_
= sym
.get_st_value();
109 this->symsize_
= sym
.get_st_size();
112 // Initialize the fields in Sized_symbol for a symbol defined in an
117 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
118 Value_type value
, Size_type symsize
,
119 elfcpp::STT type
, elfcpp::STB binding
,
120 elfcpp::STV visibility
, unsigned char nonvis
,
121 bool offset_is_from_end
)
123 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
125 this->value_
= value
;
126 this->symsize_
= symsize
;
129 // Initialize the fields in Sized_symbol for a symbol defined in an
134 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
135 Value_type value
, Size_type symsize
,
136 elfcpp::STT type
, elfcpp::STB binding
,
137 elfcpp::STV visibility
, unsigned char nonvis
,
138 Segment_offset_base offset_base
)
140 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
141 this->value_
= value
;
142 this->symsize_
= symsize
;
145 // Initialize the fields in Sized_symbol for a symbol defined as a
150 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
151 elfcpp::STT type
, elfcpp::STB binding
,
152 elfcpp::STV visibility
, unsigned char nonvis
)
154 this->init_base(name
, type
, binding
, visibility
, nonvis
);
155 this->value_
= value
;
156 this->symsize_
= symsize
;
159 // Class Symbol_table.
161 Symbol_table::Symbol_table()
162 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
163 forwarders_(), commons_(), warnings_()
167 Symbol_table::~Symbol_table()
171 // The hash function. The key is always canonicalized, so we use a
172 // simple combination of the pointers.
175 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
177 return key
.first
^ key
.second
;
180 // The symbol table key equality function. This is only called with
181 // canonicalized name and version strings, so we can use pointer
185 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
186 const Symbol_table_key
& k2
) const
188 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
191 // Make TO a symbol which forwards to FROM.
194 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
196 assert(!from
->is_forwarder() && !to
->is_forwarder());
197 this->forwarders_
[from
] = to
;
198 from
->set_forwarder();
201 // Resolve the forwards from FROM, returning the real symbol.
204 Symbol_table::resolve_forwards(Symbol
* from
) const
206 assert(from
->is_forwarder());
207 Unordered_map
<Symbol
*, Symbol
*>::const_iterator p
=
208 this->forwarders_
.find(from
);
209 assert(p
!= this->forwarders_
.end());
213 // Look up a symbol by name.
216 Symbol_table::lookup(const char* name
, const char* version
) const
218 Stringpool::Key name_key
;
219 name
= this->namepool_
.find(name
, &name_key
);
223 Stringpool::Key version_key
= 0;
226 version
= this->namepool_
.find(version
, &version_key
);
231 Symbol_table_key
key(name_key
, version_key
);
232 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
233 if (p
== this->table_
.end())
238 // Resolve a Symbol with another Symbol. This is only used in the
239 // unusual case where there are references to both an unversioned
240 // symbol and a symbol with a version, and we then discover that that
241 // version is the default version. Because this is unusual, we do
242 // this the slow way, by converting back to an ELF symbol.
244 template<int size
, bool big_endian
>
246 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
249 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
250 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
251 // We don't bother to set the st_name field.
252 esym
.put_st_value(from
->value());
253 esym
.put_st_size(from
->symsize());
254 esym
.put_st_info(from
->binding(), from
->type());
255 esym
.put_st_other(from
->visibility(), from
->nonvis());
256 esym
.put_st_shndx(from
->shnum());
257 Symbol_table::resolve(to
, esym
.sym(), from
->object());
260 // Add one symbol from OBJECT to the symbol table. NAME is symbol
261 // name and VERSION is the version; both are canonicalized. DEF is
262 // whether this is the default version.
264 // If DEF is true, then this is the definition of a default version of
265 // a symbol. That means that any lookup of NAME/NULL and any lookup
266 // of NAME/VERSION should always return the same symbol. This is
267 // obvious for references, but in particular we want to do this for
268 // definitions: overriding NAME/NULL should also override
269 // NAME/VERSION. If we don't do that, it would be very hard to
270 // override functions in a shared library which uses versioning.
272 // We implement this by simply making both entries in the hash table
273 // point to the same Symbol structure. That is easy enough if this is
274 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
275 // that we have seen both already, in which case they will both have
276 // independent entries in the symbol table. We can't simply change
277 // the symbol table entry, because we have pointers to the entries
278 // attached to the object files. So we mark the entry attached to the
279 // object file as a forwarder, and record it in the forwarders_ map.
280 // Note that entries in the hash table will never be marked as
283 template<int size
, bool big_endian
>
285 Symbol_table::add_from_object(Object
* object
,
287 Stringpool::Key name_key
,
289 Stringpool::Key version_key
,
291 const elfcpp::Sym
<size
, big_endian
>& sym
)
293 Symbol
* const snull
= NULL
;
294 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
295 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
298 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
299 std::make_pair(this->table_
.end(), false);
302 const Stringpool::Key vnull_key
= 0;
303 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
308 // ins.first: an iterator, which is a pointer to a pair.
309 // ins.first->first: the key (a pair of name and version).
310 // ins.first->second: the value (Symbol*).
311 // ins.second: true if new entry was inserted, false if not.
313 Sized_symbol
<size
>* ret
;
318 // We already have an entry for NAME/VERSION.
319 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
323 was_undefined
= ret
->is_undefined();
324 was_common
= ret
->is_common();
326 Symbol_table::resolve(ret
, sym
, object
);
332 // This is the first time we have seen NAME/NULL. Make
333 // NAME/NULL point to NAME/VERSION.
334 insdef
.first
->second
= ret
;
338 // This is the unfortunate case where we already have
339 // entries for both NAME/VERSION and NAME/NULL.
340 const Sized_symbol
<size
>* sym2
;
341 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
344 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
345 ret
, sym2
SELECT_SIZE_ENDIAN(size
, big_endian
));
346 this->make_forwarder(insdef
.first
->second
, ret
);
347 insdef
.first
->second
= ret
;
353 // This is the first time we have seen NAME/VERSION.
354 assert(ins
.first
->second
== NULL
);
356 was_undefined
= false;
359 if (def
&& !insdef
.second
)
361 // We already have an entry for NAME/NULL. Make
362 // NAME/VERSION point to it.
363 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
366 Symbol_table::resolve(ret
, sym
, object
);
367 ins
.first
->second
= ret
;
371 Sized_target
<size
, big_endian
>* target
=
372 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
373 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
374 if (!target
->has_make_symbol())
375 ret
= new Sized_symbol
<size
>();
378 ret
= target
->make_symbol();
381 // This means that we don't want a symbol table
384 this->table_
.erase(ins
.first
);
387 this->table_
.erase(insdef
.first
);
388 // Inserting insdef invalidated ins.
389 this->table_
.erase(std::make_pair(name_key
,
396 ret
->init(name
, version
, object
, sym
);
398 ins
.first
->second
= ret
;
401 // This is the first time we have seen NAME/NULL. Point
402 // it at the new entry for NAME/VERSION.
403 assert(insdef
.second
);
404 insdef
.first
->second
= ret
;
409 // Record every time we see a new undefined symbol, to speed up
411 if (!was_undefined
&& ret
->is_undefined())
412 ++this->saw_undefined_
;
414 // Keep track of common symbols, to speed up common symbol
416 if (!was_common
&& ret
->is_common())
417 this->commons_
.push_back(ret
);
422 // Add all the symbols in a relocatable object to the hash table.
424 template<int size
, bool big_endian
>
426 Symbol_table::add_from_object(
428 const unsigned char* syms
,
430 const char* sym_names
,
431 size_t sym_name_size
,
432 Symbol
** sympointers
)
434 // We take the size from the first object we see.
435 if (this->get_size() == 0)
436 this->set_size(size
);
438 if (size
!= this->get_size() || size
!= object
->target()->get_size())
440 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
441 program_name
, object
->name().c_str());
445 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
447 const unsigned char* p
= syms
;
448 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
450 elfcpp::Sym
<size
, big_endian
> sym(p
);
451 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
453 unsigned int st_name
= psym
->get_st_name();
454 if (st_name
>= sym_name_size
)
457 _("%s: %s: bad global symbol name offset %u at %lu\n"),
458 program_name
, object
->name().c_str(), st_name
,
459 static_cast<unsigned long>(i
));
463 // A symbol defined in a section which we are not including must
464 // be treated as an undefined symbol.
465 unsigned char symbuf
[sym_size
];
466 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
467 unsigned int st_shndx
= psym
->get_st_shndx();
468 if (st_shndx
!= elfcpp::SHN_UNDEF
469 && st_shndx
< elfcpp::SHN_LORESERVE
470 && !object
->is_section_included(st_shndx
))
472 memcpy(symbuf
, p
, sym_size
);
473 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
474 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
478 const char* name
= sym_names
+ st_name
;
480 // In an object file, an '@' in the name separates the symbol
481 // name from the version name. If there are two '@' characters,
482 // this is the default version.
483 const char* ver
= strchr(name
, '@');
488 Stringpool::Key name_key
;
489 name
= this->namepool_
.add(name
, &name_key
);
490 res
= this->add_from_object(object
, name
, name_key
, NULL
, 0,
495 Stringpool::Key name_key
;
496 name
= this->namepool_
.add(name
, ver
- name
, &name_key
);
506 Stringpool::Key ver_key
;
507 ver
= this->namepool_
.add(ver
, &ver_key
);
509 res
= this->add_from_object(object
, name
, name_key
, ver
, ver_key
,
513 *sympointers
++ = res
;
517 // Create and return a specially defined symbol. If ONLY_IF_REF is
518 // true, then only create the symbol if there is a reference to it.
520 template<int size
, bool big_endian
>
522 Symbol_table::define_special_symbol(Target
* target
, const char* name
,
526 assert(this->size_
== size
);
529 Sized_symbol
<size
>* sym
;
533 oldsym
= this->lookup(name
, NULL
);
534 if (oldsym
== NULL
|| !oldsym
->is_undefined())
538 // Canonicalize NAME.
539 name
= oldsym
->name();
543 // Canonicalize NAME.
544 Stringpool::Key name_key
;
545 name
= this->namepool_
.add(name
, &name_key
);
547 Symbol
* const snull
= NULL
;
548 const Stringpool::Key ver_key
= 0;
549 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
550 this->table_
.insert(std::make_pair(std::make_pair(name_key
, ver_key
),
555 // We already have a symbol table entry for NAME.
556 oldsym
= ins
.first
->second
;
557 assert(oldsym
!= NULL
);
562 // We haven't seen this symbol before.
563 assert(ins
.first
->second
== NULL
);
565 if (!target
->has_make_symbol())
566 sym
= new Sized_symbol
<size
>();
569 assert(target
->get_size() == size
);
570 assert(target
->is_big_endian() ? big_endian
: !big_endian
);
571 typedef Sized_target
<size
, big_endian
> My_target
;
572 My_target
* sized_target
= static_cast<My_target
*>(target
);
573 sym
= sized_target
->make_symbol();
578 ins
.first
->second
= sym
;
587 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
589 assert(sym
->source() == Symbol::FROM_OBJECT
);
590 const int old_shnum
= sym
->shnum();
591 if (old_shnum
!= elfcpp::SHN_UNDEF
592 && old_shnum
!= elfcpp::SHN_COMMON
593 && !sym
->object()->is_dynamic())
595 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
597 // FIXME: Report old location. Record that we have seen an
602 // Our new definition is going to override the old reference.
608 // Define a symbol based on an Output_data.
611 Symbol_table::define_in_output_data(Target
* target
, const char* name
,
613 uint64_t value
, uint64_t symsize
,
614 elfcpp::STT type
, elfcpp::STB binding
,
615 elfcpp::STV visibility
,
616 unsigned char nonvis
,
617 bool offset_is_from_end
,
620 assert(target
->get_size() == this->size_
);
621 if (this->size_
== 32)
622 this->do_define_in_output_data
<32>(target
, name
, od
, value
, symsize
,
623 type
, binding
, visibility
, nonvis
,
624 offset_is_from_end
, only_if_ref
);
625 else if (this->size_
== 64)
626 this->do_define_in_output_data
<64>(target
, name
, od
, value
, symsize
,
627 type
, binding
, visibility
, nonvis
,
628 offset_is_from_end
, only_if_ref
);
633 // Define a symbol in an Output_data, sized version.
637 Symbol_table::do_define_in_output_data(
641 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
642 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
645 elfcpp::STV visibility
,
646 unsigned char nonvis
,
647 bool offset_is_from_end
,
650 Sized_symbol
<size
>* sym
;
652 if (target
->is_big_endian())
653 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
654 target
, name
, only_if_ref
655 SELECT_SIZE_ENDIAN(size
, true));
657 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
658 target
, name
, only_if_ref
659 SELECT_SIZE_ENDIAN(size
, false));
664 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
668 // Define a symbol based on an Output_segment.
671 Symbol_table::define_in_output_segment(Target
* target
, const char* name
,
673 uint64_t value
, uint64_t symsize
,
674 elfcpp::STT type
, elfcpp::STB binding
,
675 elfcpp::STV visibility
,
676 unsigned char nonvis
,
677 Symbol::Segment_offset_base offset_base
,
680 assert(target
->get_size() == this->size_
);
681 if (this->size_
== 32)
682 this->do_define_in_output_segment
<32>(target
, name
, os
, value
, symsize
,
683 type
, binding
, visibility
, nonvis
,
684 offset_base
, only_if_ref
);
685 else if (this->size_
== 64)
686 this->do_define_in_output_segment
<64>(target
, name
, os
, value
, symsize
,
687 type
, binding
, visibility
, nonvis
,
688 offset_base
, only_if_ref
);
693 // Define a symbol in an Output_segment, sized version.
697 Symbol_table::do_define_in_output_segment(
701 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
702 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
705 elfcpp::STV visibility
,
706 unsigned char nonvis
,
707 Symbol::Segment_offset_base offset_base
,
710 Sized_symbol
<size
>* sym
;
712 if (target
->is_big_endian())
713 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
714 target
, name
, only_if_ref
715 SELECT_SIZE_ENDIAN(size
, true));
717 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
718 target
, name
, only_if_ref
719 SELECT_SIZE_ENDIAN(size
, false));
724 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
728 // Define a special symbol with a constant value. It is a multiple
729 // definition error if this symbol is already defined.
732 Symbol_table::define_as_constant(Target
* target
, const char* name
,
733 uint64_t value
, uint64_t symsize
,
734 elfcpp::STT type
, elfcpp::STB binding
,
735 elfcpp::STV visibility
, unsigned char nonvis
,
738 assert(target
->get_size() == this->size_
);
739 if (this->size_
== 32)
740 this->do_define_as_constant
<32>(target
, name
, value
, symsize
,
741 type
, binding
, visibility
, nonvis
,
743 else if (this->size_
== 64)
744 this->do_define_as_constant
<64>(target
, name
, value
, symsize
,
745 type
, binding
, visibility
, nonvis
,
751 // Define a symbol as a constant, sized version.
755 Symbol_table::do_define_as_constant(
758 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
759 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
762 elfcpp::STV visibility
,
763 unsigned char nonvis
,
766 Sized_symbol
<size
>* sym
;
768 if (target
->is_big_endian())
769 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
770 target
, name
, only_if_ref
771 SELECT_SIZE_ENDIAN(size
, true));
773 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
774 target
, name
, only_if_ref
775 SELECT_SIZE_ENDIAN(size
, false));
780 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
783 // Define a set of symbols in output sections.
786 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
787 const Define_symbol_in_section
* p
)
789 for (int i
= 0; i
< count
; ++i
, ++p
)
791 Output_section
* os
= layout
->find_output_section(p
->output_section
);
793 this->define_in_output_data(target
, p
->name
, os
, p
->value
, p
->size
,
794 p
->type
, p
->binding
, p
->visibility
,
795 p
->nonvis
, p
->offset_is_from_end
,
798 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
799 p
->binding
, p
->visibility
, p
->nonvis
,
804 // Define a set of symbols in output segments.
807 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
808 const Define_symbol_in_segment
* p
)
810 for (int i
= 0; i
< count
; ++i
, ++p
)
812 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
813 p
->segment_flags_set
,
814 p
->segment_flags_clear
);
816 this->define_in_output_segment(target
, p
->name
, os
, p
->value
, p
->size
,
817 p
->type
, p
->binding
, p
->visibility
,
818 p
->nonvis
, p
->offset_base
,
821 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
822 p
->binding
, p
->visibility
, p
->nonvis
,
827 // Set the final values for all the symbols. Record the file offset
828 // OFF. Add their names to POOL. Return the new file offset.
831 Symbol_table::finalize(off_t off
, Stringpool
* pool
)
835 if (this->size_
== 32)
836 ret
= this->sized_finalize
<32>(off
, pool
);
837 else if (this->size_
== 64)
838 ret
= this->sized_finalize
<64>(off
, pool
);
842 // Now that we have the final symbol table, we can reliably note
843 // which symbols should get warnings.
844 this->warnings_
.note_warnings(this);
849 // Set the final value for all the symbols. This is called after
850 // Layout::finalize, so all the output sections have their final
855 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
)
857 off
= align_address(off
, size
>> 3);
860 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
861 Symbol_table_type::iterator p
= this->table_
.begin();
863 while (p
!= this->table_
.end())
865 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
867 // FIXME: Here we need to decide which symbols should go into
870 typename Sized_symbol
<size
>::Value_type value
;
872 switch (sym
->source())
874 case Symbol::FROM_OBJECT
:
876 unsigned int shnum
= sym
->shnum();
878 // FIXME: We need some target specific support here.
879 if (shnum
>= elfcpp::SHN_LORESERVE
880 && shnum
!= elfcpp::SHN_ABS
)
882 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
883 program_name
, sym
->name(), shnum
);
887 Object
* symobj
= sym
->object();
888 if (symobj
->is_dynamic())
891 shnum
= elfcpp::SHN_UNDEF
;
893 else if (shnum
== elfcpp::SHN_UNDEF
)
895 else if (shnum
== elfcpp::SHN_ABS
)
896 value
= sym
->value();
899 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
901 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
905 // We should be able to erase this symbol from the
906 // symbol table, but at least with gcc 4.0.2
907 // std::unordered_map::erase doesn't appear to return
909 // p = this->table_.erase(p);
914 value
= sym
->value() + os
->address() + secoff
;
919 case Symbol::IN_OUTPUT_DATA
:
921 Output_data
* od
= sym
->output_data();
922 value
= sym
->value() + od
->address();
923 if (sym
->offset_is_from_end())
924 value
+= od
->data_size();
928 case Symbol::IN_OUTPUT_SEGMENT
:
930 Output_segment
* os
= sym
->output_segment();
931 value
= sym
->value() + os
->vaddr();
932 switch (sym
->offset_base())
934 case Symbol::SEGMENT_START
:
936 case Symbol::SEGMENT_END
:
937 value
+= os
->memsz();
939 case Symbol::SEGMENT_BSS
:
940 value
+= os
->filesz();
948 case Symbol::CONSTANT
:
949 value
= sym
->value();
956 sym
->set_value(value
);
957 pool
->add(sym
->name(), NULL
);
963 this->output_count_
= count
;
968 // Write out the global symbols.
971 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
972 Output_file
* of
) const
974 if (this->size_
== 32)
976 if (target
->is_big_endian())
977 this->sized_write_globals
<32, true>(target
, sympool
, of
);
979 this->sized_write_globals
<32, false>(target
, sympool
, of
);
981 else if (this->size_
== 64)
983 if (target
->is_big_endian())
984 this->sized_write_globals
<64, true>(target
, sympool
, of
);
986 this->sized_write_globals
<64, false>(target
, sympool
, of
);
992 // Write out the global symbols.
994 template<int size
, bool big_endian
>
996 Symbol_table::sized_write_globals(const Target
*,
997 const Stringpool
* sympool
,
998 Output_file
* of
) const
1000 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1001 unsigned char* psyms
= of
->get_output_view(this->offset_
,
1002 this->output_count_
* sym_size
);
1003 unsigned char* ps
= psyms
;
1004 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1005 p
!= this->table_
.end();
1008 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1011 switch (sym
->source())
1013 case Symbol::FROM_OBJECT
:
1015 unsigned int shnum
= sym
->shnum();
1017 // FIXME: We need some target specific support here.
1018 if (shnum
>= elfcpp::SHN_LORESERVE
1019 && shnum
!= elfcpp::SHN_ABS
)
1021 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1022 program_name
, sym
->name(), sym
->shnum());
1026 Object
* symobj
= sym
->object();
1027 if (symobj
->is_dynamic())
1030 shndx
= elfcpp::SHN_UNDEF
;
1032 else if (shnum
== elfcpp::SHN_UNDEF
|| shnum
== elfcpp::SHN_ABS
)
1036 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1038 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
1042 shndx
= os
->out_shndx();
1047 case Symbol::IN_OUTPUT_DATA
:
1048 shndx
= sym
->output_data()->out_shndx();
1051 case Symbol::IN_OUTPUT_SEGMENT
:
1052 shndx
= elfcpp::SHN_ABS
;
1055 case Symbol::CONSTANT
:
1056 shndx
= elfcpp::SHN_ABS
;
1063 elfcpp::Sym_write
<size
, big_endian
> osym(ps
);
1064 osym
.put_st_name(sympool
->get_offset(sym
->name()));
1065 osym
.put_st_value(sym
->value());
1066 osym
.put_st_size(sym
->symsize());
1067 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1068 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(),
1070 osym
.put_st_shndx(shndx
);
1075 of
->write_output_view(this->offset_
, this->output_count_
* sym_size
, psyms
);
1078 // Warnings functions.
1080 // Add a new warning.
1083 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1086 name
= symtab
->canonicalize_name(name
);
1087 this->warnings_
[name
].set(obj
, shndx
);
1090 // Look through the warnings and mark the symbols for which we should
1091 // warn. This is called during Layout::finalize when we know the
1092 // sources for all the symbols.
1095 Warnings::note_warnings(Symbol_table
* symtab
)
1097 for (Warning_table::iterator p
= this->warnings_
.begin();
1098 p
!= this->warnings_
.end();
1101 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1103 && sym
->source() == Symbol::FROM_OBJECT
1104 && sym
->object() == p
->second
.object
)
1106 sym
->set_has_warning();
1108 // Read the section contents to get the warning text. It
1109 // would be nicer if we only did this if we have to actually
1110 // issue a warning. Unfortunately, warnings are issued as
1111 // we relocate sections. That means that we can not lock
1112 // the object then, as we might try to issue the same
1113 // warning multiple times simultaneously.
1114 const unsigned char* c
;
1116 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
);
1117 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1122 // Issue a warning. This is called when we see a relocation against a
1123 // symbol for which has a warning.
1126 Warnings::issue_warning(Symbol
* sym
, const std::string
& location
) const
1128 assert(sym
->has_warning());
1129 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1130 assert(p
!= this->warnings_
.end());
1131 fprintf(stderr
, _("%s: %s: warning: %s\n"), program_name
, location
.c_str(),
1132 p
->second
.text
.c_str());
1135 // Instantiate the templates we need. We could use the configure
1136 // script to restrict this to only the ones needed for implemented
1141 Symbol_table::add_from_object
<32, true>(
1143 const unsigned char* syms
,
1145 const char* sym_names
,
1146 size_t sym_name_size
,
1147 Symbol
** sympointers
);
1151 Symbol_table::add_from_object
<32, false>(
1153 const unsigned char* syms
,
1155 const char* sym_names
,
1156 size_t sym_name_size
,
1157 Symbol
** sympointers
);
1161 Symbol_table::add_from_object
<64, true>(
1163 const unsigned char* syms
,
1165 const char* sym_names
,
1166 size_t sym_name_size
,
1167 Symbol
** sympointers
);
1171 Symbol_table::add_from_object
<64, false>(
1173 const unsigned char* syms
,
1175 const char* sym_names
,
1176 size_t sym_name_size
,
1177 Symbol
** sympointers
);
1179 } // End namespace gold.