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 (reinterpret_cast<size_t>(key
.first
)
178 ^ reinterpret_cast<size_t>(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 name
= this->namepool_
.find(name
);
224 version
= this->namepool_
.find(version
);
229 Symbol_table_key
key(name
, version
);
230 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
231 if (p
== this->table_
.end())
236 // Resolve a Symbol with another Symbol. This is only used in the
237 // unusual case where there are references to both an unversioned
238 // symbol and a symbol with a version, and we then discover that that
239 // version is the default version. Because this is unusual, we do
240 // this the slow way, by converting back to an ELF symbol.
242 template<int size
, bool big_endian
>
244 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
247 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
248 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
249 // We don't bother to set the st_name field.
250 esym
.put_st_value(from
->value());
251 esym
.put_st_size(from
->symsize());
252 esym
.put_st_info(from
->binding(), from
->type());
253 esym
.put_st_other(from
->visibility(), from
->nonvis());
254 esym
.put_st_shndx(from
->shnum());
255 Symbol_table::resolve(to
, esym
.sym(), from
->object());
258 // Add one symbol from OBJECT to the symbol table. NAME is symbol
259 // name and VERSION is the version; both are canonicalized. DEF is
260 // whether this is the default version.
262 // If DEF is true, then this is the definition of a default version of
263 // a symbol. That means that any lookup of NAME/NULL and any lookup
264 // of NAME/VERSION should always return the same symbol. This is
265 // obvious for references, but in particular we want to do this for
266 // definitions: overriding NAME/NULL should also override
267 // NAME/VERSION. If we don't do that, it would be very hard to
268 // override functions in a shared library which uses versioning.
270 // We implement this by simply making both entries in the hash table
271 // point to the same Symbol structure. That is easy enough if this is
272 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
273 // that we have seen both already, in which case they will both have
274 // independent entries in the symbol table. We can't simply change
275 // the symbol table entry, because we have pointers to the entries
276 // attached to the object files. So we mark the entry attached to the
277 // object file as a forwarder, and record it in the forwarders_ map.
278 // Note that entries in the hash table will never be marked as
281 template<int size
, bool big_endian
>
283 Symbol_table::add_from_object(Object
* object
,
285 const char *version
, bool def
,
286 const elfcpp::Sym
<size
, big_endian
>& sym
)
288 Symbol
* const snull
= NULL
;
289 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
290 this->table_
.insert(std::make_pair(std::make_pair(name
, version
), snull
));
292 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
293 std::make_pair(this->table_
.end(), false);
296 const char* const vnull
= NULL
;
297 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name
, vnull
),
301 // ins.first: an iterator, which is a pointer to a pair.
302 // ins.first->first: the key (a pair of name and version).
303 // ins.first->second: the value (Symbol*).
304 // ins.second: true if new entry was inserted, false if not.
306 Sized_symbol
<size
>* ret
;
311 // We already have an entry for NAME/VERSION.
312 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
316 was_undefined
= ret
->is_undefined();
317 was_common
= ret
->is_common();
319 Symbol_table::resolve(ret
, sym
, object
);
325 // This is the first time we have seen NAME/NULL. Make
326 // NAME/NULL point to NAME/VERSION.
327 insdef
.first
->second
= ret
;
331 // This is the unfortunate case where we already have
332 // entries for both NAME/VERSION and NAME/NULL.
333 const Sized_symbol
<size
>* sym2
;
334 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
337 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
338 ret
, sym2
SELECT_SIZE_ENDIAN(size
, big_endian
));
339 this->make_forwarder(insdef
.first
->second
, ret
);
340 insdef
.first
->second
= ret
;
346 // This is the first time we have seen NAME/VERSION.
347 assert(ins
.first
->second
== NULL
);
349 was_undefined
= false;
352 if (def
&& !insdef
.second
)
354 // We already have an entry for NAME/NULL. Make
355 // NAME/VERSION point to it.
356 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
359 Symbol_table::resolve(ret
, sym
, object
);
360 ins
.first
->second
= ret
;
364 Sized_target
<size
, big_endian
>* target
=
365 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
366 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
367 if (!target
->has_make_symbol())
368 ret
= new Sized_symbol
<size
>();
371 ret
= target
->make_symbol();
374 // This means that we don't want a symbol table
377 this->table_
.erase(ins
.first
);
380 this->table_
.erase(insdef
.first
);
381 // Inserting insdef invalidated ins.
382 this->table_
.erase(std::make_pair(name
, version
));
388 ret
->init(name
, version
, object
, sym
);
390 ins
.first
->second
= ret
;
393 // This is the first time we have seen NAME/NULL. Point
394 // it at the new entry for NAME/VERSION.
395 assert(insdef
.second
);
396 insdef
.first
->second
= ret
;
401 // Record every time we see a new undefined symbol, to speed up
403 if (!was_undefined
&& ret
->is_undefined())
404 ++this->saw_undefined_
;
406 // Keep track of common symbols, to speed up common symbol
408 if (!was_common
&& ret
->is_common())
409 this->commons_
.push_back(ret
);
414 // Add all the symbols in a relocatable object to the hash table.
416 template<int size
, bool big_endian
>
418 Symbol_table::add_from_object(
420 const unsigned char* syms
,
422 const char* sym_names
,
423 size_t sym_name_size
,
424 Symbol
** sympointers
)
426 // We take the size from the first object we see.
427 if (this->get_size() == 0)
428 this->set_size(size
);
430 if (size
!= this->get_size() || size
!= object
->target()->get_size())
432 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
433 program_name
, object
->name().c_str());
437 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
439 const unsigned char* p
= syms
;
440 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
442 elfcpp::Sym
<size
, big_endian
> sym(p
);
443 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
445 unsigned int st_name
= psym
->get_st_name();
446 if (st_name
>= sym_name_size
)
449 _("%s: %s: bad global symbol name offset %u at %lu\n"),
450 program_name
, object
->name().c_str(), st_name
,
451 static_cast<unsigned long>(i
));
455 // A symbol defined in a section which we are not including must
456 // be treated as an undefined symbol.
457 unsigned char symbuf
[sym_size
];
458 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
459 unsigned int st_shndx
= psym
->get_st_shndx();
460 if (st_shndx
!= elfcpp::SHN_UNDEF
461 && st_shndx
< elfcpp::SHN_LORESERVE
462 && !object
->is_section_included(st_shndx
))
464 memcpy(symbuf
, p
, sym_size
);
465 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
466 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
470 const char* name
= sym_names
+ st_name
;
472 // In an object file, an '@' in the name separates the symbol
473 // name from the version name. If there are two '@' characters,
474 // this is the default version.
475 const char* ver
= strchr(name
, '@');
480 name
= this->namepool_
.add(name
);
481 res
= this->add_from_object(object
, name
, NULL
, false, *psym
);
485 name
= this->namepool_
.add(name
, ver
- name
);
493 ver
= this->namepool_
.add(ver
);
494 res
= this->add_from_object(object
, name
, ver
, def
, *psym
);
497 *sympointers
++ = res
;
501 // Create and return a specially defined symbol. If ONLY_IF_REF is
502 // true, then only create the symbol if there is a reference to it.
504 template<int size
, bool big_endian
>
506 Symbol_table::define_special_symbol(Target
* target
, const char* name
,
510 assert(this->size_
== size
);
513 Sized_symbol
<size
>* sym
;
517 oldsym
= this->lookup(name
, NULL
);
518 if (oldsym
== NULL
|| !oldsym
->is_undefined())
522 // Canonicalize NAME.
523 name
= oldsym
->name();
527 // Canonicalize NAME.
528 name
= this->namepool_
.add(name
);
530 Symbol
* const snull
= NULL
;
531 const char* const vnull
= NULL
;
532 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
533 this->table_
.insert(std::make_pair(std::make_pair(name
, vnull
),
538 // We already have a symbol table entry for NAME.
539 oldsym
= ins
.first
->second
;
540 assert(oldsym
!= NULL
);
545 // We haven't seen this symbol before.
546 assert(ins
.first
->second
== NULL
);
548 if (!target
->has_make_symbol())
549 sym
= new Sized_symbol
<size
>();
552 assert(target
->get_size() == size
);
553 assert(target
->is_big_endian() ? big_endian
: !big_endian
);
554 typedef Sized_target
<size
, big_endian
> My_target
;
555 My_target
* sized_target
= static_cast<My_target
*>(target
);
556 sym
= sized_target
->make_symbol();
561 ins
.first
->second
= sym
;
570 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
572 assert(sym
->source() == Symbol::FROM_OBJECT
);
573 const int old_shnum
= sym
->shnum();
574 if (old_shnum
!= elfcpp::SHN_UNDEF
575 && old_shnum
!= elfcpp::SHN_COMMON
576 && !sym
->object()->is_dynamic())
578 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
580 // FIXME: Report old location. Record that we have seen an
585 // Our new definition is going to override the old reference.
591 // Define a symbol based on an Output_data.
594 Symbol_table::define_in_output_data(Target
* target
, const char* name
,
596 uint64_t value
, uint64_t symsize
,
597 elfcpp::STT type
, elfcpp::STB binding
,
598 elfcpp::STV visibility
,
599 unsigned char nonvis
,
600 bool offset_is_from_end
,
603 assert(target
->get_size() == this->size_
);
604 if (this->size_
== 32)
605 this->do_define_in_output_data
<32>(target
, name
, od
, value
, symsize
,
606 type
, binding
, visibility
, nonvis
,
607 offset_is_from_end
, only_if_ref
);
608 else if (this->size_
== 64)
609 this->do_define_in_output_data
<64>(target
, name
, od
, value
, symsize
,
610 type
, binding
, visibility
, nonvis
,
611 offset_is_from_end
, only_if_ref
);
616 // Define a symbol in an Output_data, sized version.
620 Symbol_table::do_define_in_output_data(
624 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
625 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
628 elfcpp::STV visibility
,
629 unsigned char nonvis
,
630 bool offset_is_from_end
,
633 Sized_symbol
<size
>* sym
;
635 if (target
->is_big_endian())
636 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
637 target
, name
, only_if_ref
638 SELECT_SIZE_ENDIAN(size
, true));
640 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
641 target
, name
, only_if_ref
642 SELECT_SIZE_ENDIAN(size
, false));
647 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
651 // Define a symbol based on an Output_segment.
654 Symbol_table::define_in_output_segment(Target
* target
, const char* name
,
656 uint64_t value
, uint64_t symsize
,
657 elfcpp::STT type
, elfcpp::STB binding
,
658 elfcpp::STV visibility
,
659 unsigned char nonvis
,
660 Symbol::Segment_offset_base offset_base
,
663 assert(target
->get_size() == this->size_
);
664 if (this->size_
== 32)
665 this->do_define_in_output_segment
<32>(target
, name
, os
, value
, symsize
,
666 type
, binding
, visibility
, nonvis
,
667 offset_base
, only_if_ref
);
668 else if (this->size_
== 64)
669 this->do_define_in_output_segment
<64>(target
, name
, os
, value
, symsize
,
670 type
, binding
, visibility
, nonvis
,
671 offset_base
, only_if_ref
);
676 // Define a symbol in an Output_segment, sized version.
680 Symbol_table::do_define_in_output_segment(
684 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
685 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
688 elfcpp::STV visibility
,
689 unsigned char nonvis
,
690 Symbol::Segment_offset_base offset_base
,
693 Sized_symbol
<size
>* sym
;
695 if (target
->is_big_endian())
696 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
697 target
, name
, only_if_ref
698 SELECT_SIZE_ENDIAN(size
, true));
700 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
701 target
, name
, only_if_ref
702 SELECT_SIZE_ENDIAN(size
, false));
707 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
711 // Define a special symbol with a constant value. It is a multiple
712 // definition error if this symbol is already defined.
715 Symbol_table::define_as_constant(Target
* target
, const char* name
,
716 uint64_t value
, uint64_t symsize
,
717 elfcpp::STT type
, elfcpp::STB binding
,
718 elfcpp::STV visibility
, unsigned char nonvis
,
721 assert(target
->get_size() == this->size_
);
722 if (this->size_
== 32)
723 this->do_define_as_constant
<32>(target
, name
, value
, symsize
,
724 type
, binding
, visibility
, nonvis
,
726 else if (this->size_
== 64)
727 this->do_define_as_constant
<64>(target
, name
, value
, symsize
,
728 type
, binding
, visibility
, nonvis
,
734 // Define a symbol as a constant, sized version.
738 Symbol_table::do_define_as_constant(
741 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
742 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
745 elfcpp::STV visibility
,
746 unsigned char nonvis
,
749 Sized_symbol
<size
>* sym
;
751 if (target
->is_big_endian())
752 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
753 target
, name
, only_if_ref
754 SELECT_SIZE_ENDIAN(size
, true));
756 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
757 target
, name
, only_if_ref
758 SELECT_SIZE_ENDIAN(size
, false));
763 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
766 // Define a set of symbols in output sections.
769 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
770 const Define_symbol_in_section
* p
)
772 for (int i
= 0; i
< count
; ++i
, ++p
)
774 Output_section
* os
= layout
->find_output_section(p
->output_section
);
776 this->define_in_output_data(target
, p
->name
, os
, p
->value
, p
->size
,
777 p
->type
, p
->binding
, p
->visibility
,
778 p
->nonvis
, p
->offset_is_from_end
,
781 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
782 p
->binding
, p
->visibility
, p
->nonvis
,
787 // Define a set of symbols in output segments.
790 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
791 const Define_symbol_in_segment
* p
)
793 for (int i
= 0; i
< count
; ++i
, ++p
)
795 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
796 p
->segment_flags_set
,
797 p
->segment_flags_clear
);
799 this->define_in_output_segment(target
, p
->name
, os
, p
->value
, p
->size
,
800 p
->type
, p
->binding
, p
->visibility
,
801 p
->nonvis
, p
->offset_base
,
804 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
805 p
->binding
, p
->visibility
, p
->nonvis
,
810 // Set the final values for all the symbols. Record the file offset
811 // OFF. Add their names to POOL. Return the new file offset.
814 Symbol_table::finalize(off_t off
, Stringpool
* pool
)
818 if (this->size_
== 32)
819 ret
= this->sized_finalize
<32>(off
, pool
);
820 else if (this->size_
== 64)
821 ret
= this->sized_finalize
<64>(off
, pool
);
825 // Now that we have the final symbol table, we can reliably note
826 // which symbols should get warnings.
827 this->warnings_
.note_warnings(this);
832 // Set the final value for all the symbols. This is called after
833 // Layout::finalize, so all the output sections have their final
838 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
)
840 off
= align_address(off
, size
>> 3);
843 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
844 Symbol_table_type::iterator p
= this->table_
.begin();
846 while (p
!= this->table_
.end())
848 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
850 // FIXME: Here we need to decide which symbols should go into
853 typename Sized_symbol
<size
>::Value_type value
;
855 switch (sym
->source())
857 case Symbol::FROM_OBJECT
:
859 unsigned int shnum
= sym
->shnum();
861 // FIXME: We need some target specific support here.
862 if (shnum
>= elfcpp::SHN_LORESERVE
863 && shnum
!= elfcpp::SHN_ABS
)
865 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
866 program_name
, sym
->name(), shnum
);
870 Object
* symobj
= sym
->object();
871 if (symobj
->is_dynamic())
874 shnum
= elfcpp::SHN_UNDEF
;
876 else if (shnum
== elfcpp::SHN_UNDEF
)
878 else if (shnum
== elfcpp::SHN_ABS
)
879 value
= sym
->value();
882 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
884 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
888 // We should be able to erase this symbol from the
889 // symbol table, but at least with gcc 4.0.2
890 // std::unordered_map::erase doesn't appear to return
892 // p = this->table_.erase(p);
897 value
= sym
->value() + os
->address() + secoff
;
902 case Symbol::IN_OUTPUT_DATA
:
904 Output_data
* od
= sym
->output_data();
905 value
= sym
->value() + od
->address();
906 if (sym
->offset_is_from_end())
907 value
+= od
->data_size();
911 case Symbol::IN_OUTPUT_SEGMENT
:
913 Output_segment
* os
= sym
->output_segment();
914 value
= sym
->value() + os
->vaddr();
915 switch (sym
->offset_base())
917 case Symbol::SEGMENT_START
:
919 case Symbol::SEGMENT_END
:
920 value
+= os
->memsz();
922 case Symbol::SEGMENT_BSS
:
923 value
+= os
->filesz();
931 case Symbol::CONSTANT
:
932 value
= sym
->value();
939 sym
->set_value(value
);
940 pool
->add(sym
->name());
946 this->output_count_
= count
;
951 // Write out the global symbols.
954 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
955 Output_file
* of
) const
957 if (this->size_
== 32)
959 if (target
->is_big_endian())
960 this->sized_write_globals
<32, true>(target
, sympool
, of
);
962 this->sized_write_globals
<32, false>(target
, sympool
, of
);
964 else if (this->size_
== 64)
966 if (target
->is_big_endian())
967 this->sized_write_globals
<64, true>(target
, sympool
, of
);
969 this->sized_write_globals
<64, false>(target
, sympool
, of
);
975 // Write out the global symbols.
977 template<int size
, bool big_endian
>
979 Symbol_table::sized_write_globals(const Target
*,
980 const Stringpool
* sympool
,
981 Output_file
* of
) const
983 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
984 unsigned char* psyms
= of
->get_output_view(this->offset_
,
985 this->output_count_
* sym_size
);
986 unsigned char* ps
= psyms
;
987 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
988 p
!= this->table_
.end();
991 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
994 switch (sym
->source())
996 case Symbol::FROM_OBJECT
:
998 unsigned int shnum
= sym
->shnum();
1000 // FIXME: We need some target specific support here.
1001 if (shnum
>= elfcpp::SHN_LORESERVE
1002 && shnum
!= elfcpp::SHN_ABS
)
1004 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
1005 program_name
, sym
->name(), sym
->shnum());
1009 Object
* symobj
= sym
->object();
1010 if (symobj
->is_dynamic())
1013 shndx
= elfcpp::SHN_UNDEF
;
1015 else if (shnum
== elfcpp::SHN_UNDEF
|| shnum
== elfcpp::SHN_ABS
)
1019 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1021 Output_section
* os
= relobj
->output_section(shnum
, &secoff
);
1025 shndx
= os
->out_shndx();
1030 case Symbol::IN_OUTPUT_DATA
:
1031 shndx
= sym
->output_data()->out_shndx();
1034 case Symbol::IN_OUTPUT_SEGMENT
:
1035 shndx
= elfcpp::SHN_ABS
;
1038 case Symbol::CONSTANT
:
1039 shndx
= elfcpp::SHN_ABS
;
1046 elfcpp::Sym_write
<size
, big_endian
> osym(ps
);
1047 osym
.put_st_name(sympool
->get_offset(sym
->name()));
1048 osym
.put_st_value(sym
->value());
1049 osym
.put_st_size(sym
->symsize());
1050 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1051 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(),
1053 osym
.put_st_shndx(shndx
);
1058 of
->write_output_view(this->offset_
, this->output_count_
* sym_size
, psyms
);
1061 // Warnings functions.
1063 // Add a new warning.
1066 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1069 name
= symtab
->canonicalize_name(name
);
1070 this->warnings_
[name
].set(obj
, shndx
);
1073 // Look through the warnings and mark the symbols for which we should
1074 // warn. This is called during Layout::finalize when we know the
1075 // sources for all the symbols.
1078 Warnings::note_warnings(Symbol_table
* symtab
)
1080 for (Warning_table::iterator p
= this->warnings_
.begin();
1081 p
!= this->warnings_
.end();
1084 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1086 && sym
->source() == Symbol::FROM_OBJECT
1087 && sym
->object() == p
->second
.object
)
1089 sym
->set_has_warning();
1091 // Read the section contents to get the warning text. It
1092 // would be nicer if we only did this if we have to actually
1093 // issue a warning. Unfortunately, warnings are issued as
1094 // we relocate sections. That means that we can not lock
1095 // the object then, as we might try to issue the same
1096 // warning multiple times simultaneously.
1097 const unsigned char* c
;
1099 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
);
1100 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1105 // Issue a warning. This is called when we see a relocation against a
1106 // symbol for which has a warning.
1109 Warnings::issue_warning(Symbol
* sym
, const std::string
& location
) const
1111 assert(sym
->has_warning());
1112 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1113 assert(p
!= this->warnings_
.end());
1114 fprintf(stderr
, _("%s: %s: warning: %s\n"), program_name
, location
.c_str(),
1115 p
->second
.text
.c_str());
1118 // Instantiate the templates we need. We could use the configure
1119 // script to restrict this to only the ones needed for implemented
1124 Symbol_table::add_from_object
<32, true>(
1126 const unsigned char* syms
,
1128 const char* sym_names
,
1129 size_t sym_name_size
,
1130 Symbol
** sympointers
);
1134 Symbol_table::add_from_object
<32, false>(
1136 const unsigned char* syms
,
1138 const char* sym_names
,
1139 size_t sym_name_size
,
1140 Symbol
** sympointers
);
1144 Symbol_table::add_from_object
<64, true>(
1146 const unsigned char* syms
,
1148 const char* sym_names
,
1149 size_t sym_name_size
,
1150 Symbol
** sympointers
);
1154 Symbol_table::add_from_object
<64, false>(
1156 const unsigned char* syms
,
1158 const char* sym_names
,
1159 size_t sym_name_size
,
1160 Symbol
** sympointers
);
1162 } // End namespace gold.