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;
42 // Initialize the fields in the base class Symbol for SYM in OBJECT.
44 template<int size
, bool big_endian
>
46 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
47 const elfcpp::Sym
<size
, big_endian
>& sym
)
49 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
50 sym
.get_st_visibility(), sym
.get_st_nonvis());
51 this->u_
.from_object
.object
= object
;
52 // FIXME: Handle SHN_XINDEX.
53 this->u_
.from_object
.shnum
= sym
.get_st_shndx();
54 this->source_
= FROM_OBJECT
;
55 this->in_dyn_
= object
->is_dynamic();
58 // Initialize the fields in the base class Symbol for a symbol defined
62 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
63 elfcpp::STB binding
, elfcpp::STV visibility
,
64 unsigned char nonvis
, bool offset_is_from_end
)
66 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
67 this->u_
.in_output_data
.output_data
= od
;
68 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
69 this->source_
= IN_OUTPUT_DATA
;
72 // Initialize the fields in the base class Symbol for a symbol defined
73 // in an Output_segment.
76 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
77 elfcpp::STB binding
, elfcpp::STV visibility
,
78 unsigned char nonvis
, Segment_offset_base offset_base
)
80 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
81 this->u_
.in_output_segment
.output_segment
= os
;
82 this->u_
.in_output_segment
.offset_base
= offset_base
;
83 this->source_
= IN_OUTPUT_SEGMENT
;
86 // Initialize the fields in the base class Symbol for a symbol defined
90 Symbol::init_base(const char* name
, elfcpp::STT type
,
91 elfcpp::STB binding
, elfcpp::STV visibility
,
94 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
95 this->source_
= CONSTANT
;
98 // Initialize the fields in Sized_symbol for SYM in OBJECT.
101 template<bool big_endian
>
103 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
104 const elfcpp::Sym
<size
, big_endian
>& sym
)
106 this->init_base(name
, version
, object
, sym
);
107 this->value_
= sym
.get_st_value();
108 this->symsize_
= sym
.get_st_size();
111 // Initialize the fields in Sized_symbol for a symbol defined in an
116 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
117 Value_type value
, Size_type symsize
,
118 elfcpp::STT type
, elfcpp::STB binding
,
119 elfcpp::STV visibility
, unsigned char nonvis
,
120 bool offset_is_from_end
)
122 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
124 this->value_
= value
;
125 this->symsize_
= symsize
;
128 // Initialize the fields in Sized_symbol for a symbol defined in an
133 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
134 Value_type value
, Size_type symsize
,
135 elfcpp::STT type
, elfcpp::STB binding
,
136 elfcpp::STV visibility
, unsigned char nonvis
,
137 Segment_offset_base offset_base
)
139 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
140 this->value_
= value
;
141 this->symsize_
= symsize
;
144 // Initialize the fields in Sized_symbol for a symbol defined as a
149 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
150 elfcpp::STT type
, elfcpp::STB binding
,
151 elfcpp::STV visibility
, unsigned char nonvis
)
153 this->init_base(name
, type
, binding
, visibility
, nonvis
);
154 this->value_
= value
;
155 this->symsize_
= symsize
;
158 // Class Symbol_table.
160 Symbol_table::Symbol_table()
161 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
162 forwarders_(), commons_()
166 Symbol_table::~Symbol_table()
170 // The hash function. The key is always canonicalized, so we use a
171 // simple combination of the pointers.
174 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
176 return (reinterpret_cast<size_t>(key
.first
)
177 ^ reinterpret_cast<size_t>(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 name
= this->namepool_
.find(name
);
223 version
= this->namepool_
.find(version
);
228 Symbol_table_key
key(name
, version
);
229 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
230 if (p
== this->table_
.end())
235 // Resolve a Symbol with another Symbol. This is only used in the
236 // unusual case where there are references to both an unversioned
237 // symbol and a symbol with a version, and we then discover that that
238 // version is the default version. Because this is unusual, we do
239 // this the slow way, by converting back to an ELF symbol.
241 template<int size
, bool big_endian
>
243 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
246 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
247 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
248 // We don't bother to set the st_name field.
249 esym
.put_st_value(from
->value());
250 esym
.put_st_size(from
->symsize());
251 esym
.put_st_info(from
->binding(), from
->type());
252 esym
.put_st_other(from
->visibility(), from
->nonvis());
253 esym
.put_st_shndx(from
->shnum());
254 Symbol_table::resolve(to
, esym
.sym(), from
->object());
257 // Add one symbol from OBJECT to the symbol table. NAME is symbol
258 // name and VERSION is the version; both are canonicalized. DEF is
259 // whether this is the default version.
261 // If DEF is true, then this is the definition of a default version of
262 // a symbol. That means that any lookup of NAME/NULL and any lookup
263 // of NAME/VERSION should always return the same symbol. This is
264 // obvious for references, but in particular we want to do this for
265 // definitions: overriding NAME/NULL should also override
266 // NAME/VERSION. If we don't do that, it would be very hard to
267 // override functions in a shared library which uses versioning.
269 // We implement this by simply making both entries in the hash table
270 // point to the same Symbol structure. That is easy enough if this is
271 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
272 // that we have seen both already, in which case they will both have
273 // independent entries in the symbol table. We can't simply change
274 // the symbol table entry, because we have pointers to the entries
275 // attached to the object files. So we mark the entry attached to the
276 // object file as a forwarder, and record it in the forwarders_ map.
277 // Note that entries in the hash table will never be marked as
280 template<int size
, bool big_endian
>
282 Symbol_table::add_from_object(Sized_object
<size
, big_endian
>* object
,
284 const char *version
, bool def
,
285 const elfcpp::Sym
<size
, big_endian
>& sym
)
287 Symbol
* const snull
= NULL
;
288 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
289 this->table_
.insert(std::make_pair(std::make_pair(name
, version
), snull
));
291 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
292 std::make_pair(this->table_
.end(), false);
295 const char* const vnull
= NULL
;
296 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name
, vnull
),
300 // ins.first: an iterator, which is a pointer to a pair.
301 // ins.first->first: the key (a pair of name and version).
302 // ins.first->second: the value (Symbol*).
303 // ins.second: true if new entry was inserted, false if not.
305 Sized_symbol
<size
>* ret
;
310 // We already have an entry for NAME/VERSION.
311 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
315 was_undefined
= ret
->is_undefined();
316 was_common
= ret
->is_common();
318 Symbol_table::resolve(ret
, sym
, object
);
324 // This is the first time we have seen NAME/NULL. Make
325 // NAME/NULL point to NAME/VERSION.
326 insdef
.first
->second
= ret
;
330 // This is the unfortunate case where we already have
331 // entries for both NAME/VERSION and NAME/NULL.
332 const Sized_symbol
<size
>* sym2
;
333 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
336 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
337 ret
, sym2
SELECT_SIZE_ENDIAN(size
, big_endian
));
338 this->make_forwarder(insdef
.first
->second
, ret
);
339 insdef
.first
->second
= ret
;
345 // This is the first time we have seen NAME/VERSION.
346 assert(ins
.first
->second
== NULL
);
348 was_undefined
= false;
351 if (def
&& !insdef
.second
)
353 // We already have an entry for NAME/NULL. Make
354 // NAME/VERSION point to it.
355 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
358 Symbol_table::resolve(ret
, sym
, object
);
359 ins
.first
->second
= ret
;
363 Sized_target
<size
, big_endian
>* target
= object
->sized_target();
364 if (!target
->has_make_symbol())
365 ret
= new Sized_symbol
<size
>();
368 ret
= target
->make_symbol();
371 // This means that we don't want a symbol table
374 this->table_
.erase(ins
.first
);
377 this->table_
.erase(insdef
.first
);
378 // Inserting insdef invalidated ins.
379 this->table_
.erase(std::make_pair(name
, version
));
385 ret
->init(name
, version
, object
, sym
);
387 ins
.first
->second
= ret
;
390 // This is the first time we have seen NAME/NULL. Point
391 // it at the new entry for NAME/VERSION.
392 assert(insdef
.second
);
393 insdef
.first
->second
= ret
;
398 // Record every time we see a new undefined symbol, to speed up
400 if (!was_undefined
&& ret
->is_undefined())
401 ++this->saw_undefined_
;
403 // Keep track of common symbols, to speed up common symbol
405 if (!was_common
&& ret
->is_common())
406 this->commons_
.push_back(ret
);
411 // Add all the symbols in an object to the hash table.
413 template<int size
, bool big_endian
>
415 Symbol_table::add_from_object(
416 Sized_object
<size
, big_endian
>* object
,
417 const elfcpp::Sym
<size
, big_endian
>* syms
,
419 const char* sym_names
,
420 size_t sym_name_size
,
421 Symbol
** sympointers
)
423 // We take the size from the first object we see.
424 if (this->get_size() == 0)
425 this->set_size(size
);
427 if (size
!= this->get_size() || size
!= object
->target()->get_size())
429 fprintf(stderr
, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
430 program_name
, object
->name().c_str());
434 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
436 const unsigned char* p
= reinterpret_cast<const unsigned char*>(syms
);
437 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
439 elfcpp::Sym
<size
, big_endian
> sym(p
);
440 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
442 unsigned int st_name
= psym
->get_st_name();
443 if (st_name
>= sym_name_size
)
446 _("%s: %s: bad global symbol name offset %u at %lu\n"),
447 program_name
, object
->name().c_str(), st_name
,
448 static_cast<unsigned long>(i
));
452 // A symbol defined in a section which we are not including must
453 // be treated as an undefined symbol.
454 unsigned char symbuf
[sym_size
];
455 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
456 unsigned int st_shndx
= psym
->get_st_shndx();
457 if (st_shndx
!= elfcpp::SHN_UNDEF
458 && st_shndx
< elfcpp::SHN_LORESERVE
459 && !object
->is_section_included(st_shndx
))
461 memcpy(symbuf
, p
, sym_size
);
462 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
463 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
467 const char* name
= sym_names
+ st_name
;
469 // In an object file, an '@' in the name separates the symbol
470 // name from the version name. If there are two '@' characters,
471 // this is the default version.
472 const char* ver
= strchr(name
, '@');
477 name
= this->namepool_
.add(name
);
478 res
= this->add_from_object(object
, name
, NULL
, false, *psym
);
482 name
= this->namepool_
.add(name
, ver
- name
);
490 ver
= this->namepool_
.add(ver
);
491 res
= this->add_from_object(object
, name
, ver
, def
, *psym
);
494 *sympointers
++ = res
;
498 // Create and return a specially defined symbol. If ONLY_IF_REF is
499 // true, then only create the symbol if there is a reference to it.
501 template<int size
, bool big_endian
>
503 Symbol_table::define_special_symbol(Target
* target
, const char* name
,
507 assert(this->size_
== size
);
510 Sized_symbol
<size
>* sym
;
514 oldsym
= this->lookup(name
, NULL
);
519 // Canonicalize NAME.
520 name
= oldsym
->name();
524 // Canonicalize NAME.
525 name
= this->namepool_
.add(name
);
527 Symbol
* const snull
= NULL
;
528 const char* const vnull
= NULL
;
529 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
530 this->table_
.insert(std::make_pair(std::make_pair(name
, vnull
),
535 // We already have a symbol table entry for NAME.
536 oldsym
= ins
.first
->second
;
537 assert(oldsym
!= NULL
);
542 // We haven't seen this symbol before.
543 assert(ins
.first
->second
== NULL
);
545 if (!target
->has_make_symbol())
546 sym
= new Sized_symbol
<size
>();
549 assert(target
->get_size() == size
);
550 assert(target
->is_big_endian() ? big_endian
: !big_endian
);
551 typedef Sized_target
<size
, big_endian
> My_target
;
552 My_target
* sized_target
= static_cast<My_target
*>(target
);
553 sym
= sized_target
->make_symbol();
558 ins
.first
->second
= sym
;
567 sym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
569 assert(sym
->source() == Symbol::FROM_OBJECT
);
570 const int old_shnum
= sym
->shnum();
571 if (old_shnum
!= elfcpp::SHN_UNDEF
572 && old_shnum
!= elfcpp::SHN_COMMON
573 && !sym
->object()->is_dynamic())
575 fprintf(stderr
, "%s: linker defined: multiple definition of %s\n",
577 // FIXME: Report old location. Record that we have seen an
582 // Our new definition is going to override the old reference.
588 // Define a symbol based on an Output_data.
591 Symbol_table::define_in_output_data(Target
* target
, const char* name
,
593 uint64_t value
, uint64_t symsize
,
594 elfcpp::STT type
, elfcpp::STB binding
,
595 elfcpp::STV visibility
,
596 unsigned char nonvis
,
597 bool offset_is_from_end
,
600 assert(target
->get_size() == this->size_
);
601 if (this->size_
== 32)
602 this->do_define_in_output_data
<32>(target
, name
, od
, value
, symsize
,
603 type
, binding
, visibility
, nonvis
,
604 offset_is_from_end
, only_if_ref
);
605 else if (this->size_
== 64)
606 this->do_define_in_output_data
<64>(target
, name
, od
, value
, symsize
,
607 type
, binding
, visibility
, nonvis
,
608 offset_is_from_end
, only_if_ref
);
613 // Define a symbol in an Output_data, sized version.
617 Symbol_table::do_define_in_output_data(
621 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
622 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
625 elfcpp::STV visibility
,
626 unsigned char nonvis
,
627 bool offset_is_from_end
,
630 Sized_symbol
<size
>* sym
;
632 if (target
->is_big_endian())
633 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
634 target
, name
, only_if_ref
635 SELECT_SIZE_ENDIAN(size
, true));
637 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
638 target
, name
, only_if_ref
639 SELECT_SIZE_ENDIAN(size
, false));
644 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
648 // Define a symbol based on an Output_segment.
651 Symbol_table::define_in_output_segment(Target
* target
, const char* name
,
653 uint64_t value
, uint64_t symsize
,
654 elfcpp::STT type
, elfcpp::STB binding
,
655 elfcpp::STV visibility
,
656 unsigned char nonvis
,
657 Symbol::Segment_offset_base offset_base
,
660 assert(target
->get_size() == this->size_
);
661 if (this->size_
== 32)
662 this->do_define_in_output_segment
<32>(target
, name
, os
, value
, symsize
,
663 type
, binding
, visibility
, nonvis
,
664 offset_base
, only_if_ref
);
665 else if (this->size_
== 64)
666 this->do_define_in_output_segment
<64>(target
, name
, os
, value
, symsize
,
667 type
, binding
, visibility
, nonvis
,
668 offset_base
, only_if_ref
);
673 // Define a symbol in an Output_segment, sized version.
677 Symbol_table::do_define_in_output_segment(
681 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
682 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
685 elfcpp::STV visibility
,
686 unsigned char nonvis
,
687 Symbol::Segment_offset_base offset_base
,
690 Sized_symbol
<size
>* sym
;
692 if (target
->is_big_endian())
693 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
694 target
, name
, only_if_ref
695 SELECT_SIZE_ENDIAN(size
, true));
697 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
698 target
, name
, only_if_ref
699 SELECT_SIZE_ENDIAN(size
, false));
704 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
708 // Define a special symbol with a constant value. It is a multiple
709 // definition error if this symbol is already defined.
712 Symbol_table::define_as_constant(Target
* target
, const char* name
,
713 uint64_t value
, uint64_t symsize
,
714 elfcpp::STT type
, elfcpp::STB binding
,
715 elfcpp::STV visibility
, unsigned char nonvis
,
718 assert(target
->get_size() == this->size_
);
719 if (this->size_
== 32)
720 this->do_define_as_constant
<32>(target
, name
, value
, symsize
,
721 type
, binding
, visibility
, nonvis
,
723 else if (this->size_
== 64)
724 this->do_define_as_constant
<64>(target
, name
, value
, symsize
,
725 type
, binding
, visibility
, nonvis
,
731 // Define a symbol as a constant, sized version.
735 Symbol_table::do_define_as_constant(
738 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
739 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
742 elfcpp::STV visibility
,
743 unsigned char nonvis
,
746 Sized_symbol
<size
>* sym
;
748 if (target
->is_big_endian())
749 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
750 target
, name
, only_if_ref
751 SELECT_SIZE_ENDIAN(size
, true));
753 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
754 target
, name
, only_if_ref
755 SELECT_SIZE_ENDIAN(size
, false));
760 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
763 // Define a set of symbols in output sections.
766 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
767 const Define_symbol_in_section
* p
)
769 for (int i
= 0; i
< count
; ++i
, ++p
)
771 Output_section
* os
= layout
->find_output_section(p
->output_section
);
773 this->define_in_output_data(target
, p
->name
, os
, p
->value
, p
->size
,
774 p
->type
, p
->binding
, p
->visibility
,
775 p
->nonvis
, p
->offset_is_from_end
,
778 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
779 p
->binding
, p
->visibility
, p
->nonvis
,
784 // Define a set of symbols in output segments.
787 Symbol_table::define_symbols(const Layout
* layout
, Target
* target
, int count
,
788 const Define_symbol_in_segment
* p
)
790 for (int i
= 0; i
< count
; ++i
, ++p
)
792 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
793 p
->segment_flags_set
,
794 p
->segment_flags_clear
);
796 this->define_in_output_segment(target
, p
->name
, os
, p
->value
, p
->size
,
797 p
->type
, p
->binding
, p
->visibility
,
798 p
->nonvis
, p
->offset_base
,
801 this->define_as_constant(target
, p
->name
, 0, p
->size
, p
->type
,
802 p
->binding
, p
->visibility
, p
->nonvis
,
807 // Set the final values for all the symbols. Record the file offset
808 // OFF. Add their names to POOL. Return the new file offset.
811 Symbol_table::finalize(off_t off
, Stringpool
* pool
)
813 if (this->size_
== 32)
814 return this->sized_finalize
<32>(off
, pool
);
815 else if (this->size_
== 64)
816 return this->sized_finalize
<64>(off
, pool
);
821 // Set the final value for all the symbols. This is called after
822 // Layout::finalize, so all the output sections have their final
827 Symbol_table::sized_finalize(off_t off
, Stringpool
* pool
)
829 off
= align_address(off
, size
>> 3);
832 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
833 Symbol_table_type::iterator p
= this->table_
.begin();
835 while (p
!= this->table_
.end())
837 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
839 // FIXME: Here we need to decide which symbols should go into
842 typename Sized_symbol
<size
>::Value_type value
;
844 switch (sym
->source())
846 case Symbol::FROM_OBJECT
:
848 unsigned int shnum
= sym
->shnum();
850 // FIXME: We need some target specific support here.
851 if (shnum
>= elfcpp::SHN_LORESERVE
852 && shnum
!= elfcpp::SHN_ABS
)
854 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
855 program_name
, sym
->name(), shnum
);
859 if (shnum
== elfcpp::SHN_UNDEF
)
861 else if (shnum
== elfcpp::SHN_ABS
)
862 value
= sym
->value();
866 Output_section
* os
= sym
->object()->output_section(shnum
,
871 // We should be able to erase this symbol from the
872 // symbol table, but at least with gcc 4.0.2
873 // std::unordered_map::erase doesn't appear to return
875 // p = this->table_.erase(p);
880 value
= sym
->value() + os
->address() + secoff
;
885 case Symbol::IN_OUTPUT_DATA
:
887 Output_data
* od
= sym
->output_data();
888 value
= sym
->value() + od
->address();
889 if (sym
->offset_is_from_end())
890 value
+= od
->data_size();
894 case Symbol::IN_OUTPUT_SEGMENT
:
896 Output_segment
* os
= sym
->output_segment();
897 value
= sym
->value() + os
->vaddr();
898 switch (sym
->offset_base())
900 case Symbol::SEGMENT_START
:
902 case Symbol::SEGMENT_END
:
903 value
+= os
->memsz();
905 case Symbol::SEGMENT_BSS
:
906 value
+= os
->filesz();
914 case Symbol::CONSTANT
:
915 value
= sym
->value();
922 sym
->set_value(value
);
923 pool
->add(sym
->name());
929 this->output_count_
= count
;
934 // Write out the global symbols.
937 Symbol_table::write_globals(const Target
* target
, const Stringpool
* sympool
,
938 Output_file
* of
) const
940 if (this->size_
== 32)
942 if (target
->is_big_endian())
943 this->sized_write_globals
<32, true>(target
, sympool
, of
);
945 this->sized_write_globals
<32, false>(target
, sympool
, of
);
947 else if (this->size_
== 64)
949 if (target
->is_big_endian())
950 this->sized_write_globals
<64, true>(target
, sympool
, of
);
952 this->sized_write_globals
<64, false>(target
, sympool
, of
);
958 // Write out the global symbols.
960 template<int size
, bool big_endian
>
962 Symbol_table::sized_write_globals(const Target
*,
963 const Stringpool
* sympool
,
964 Output_file
* of
) const
966 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
967 unsigned char* psyms
= of
->get_output_view(this->offset_
,
968 this->output_count_
* sym_size
);
969 unsigned char* ps
= psyms
;
970 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
971 p
!= this->table_
.end();
974 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
976 // FIXME: This repeats sized_finalize().
979 switch (sym
->source())
981 case Symbol::FROM_OBJECT
:
983 unsigned int shnum
= sym
->shnum();
985 // FIXME: We need some target specific support here.
986 if (shnum
>= elfcpp::SHN_LORESERVE
987 && shnum
!= elfcpp::SHN_ABS
)
989 fprintf(stderr
, _("%s: %s: unsupported symbol section 0x%x\n"),
990 program_name
, sym
->name(), sym
->shnum());
994 if (shnum
== elfcpp::SHN_UNDEF
|| shnum
== elfcpp::SHN_ABS
)
999 Output_section
* os
= sym
->object()->output_section(shnum
,
1004 shndx
= os
->out_shndx();
1009 case Symbol::IN_OUTPUT_DATA
:
1010 shndx
= sym
->output_data()->out_shndx();
1013 case Symbol::IN_OUTPUT_SEGMENT
:
1014 shndx
= elfcpp::SHN_ABS
;
1017 case Symbol::CONSTANT
:
1018 shndx
= elfcpp::SHN_ABS
;
1025 elfcpp::Sym_write
<size
, big_endian
> osym(ps
);
1026 osym
.put_st_name(sympool
->get_offset(sym
->name()));
1027 osym
.put_st_value(sym
->value());
1028 osym
.put_st_size(sym
->symsize());
1029 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1030 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(),
1032 osym
.put_st_shndx(shndx
);
1037 of
->write_output_view(this->offset_
, this->output_count_
* sym_size
, psyms
);
1040 // Instantiate the templates we need. We could use the configure
1041 // script to restrict this to only the ones needed for implemented
1046 Symbol_table::add_from_object
<32, true>(
1047 Sized_object
<32, true>* object
,
1048 const elfcpp::Sym
<32, true>* syms
,
1050 const char* sym_names
,
1051 size_t sym_name_size
,
1052 Symbol
** sympointers
);
1056 Symbol_table::add_from_object
<32, false>(
1057 Sized_object
<32, false>* object
,
1058 const elfcpp::Sym
<32, false>* syms
,
1060 const char* sym_names
,
1061 size_t sym_name_size
,
1062 Symbol
** sympointers
);
1066 Symbol_table::add_from_object
<64, true>(
1067 Sized_object
<64, true>* object
,
1068 const elfcpp::Sym
<64, true>* syms
,
1070 const char* sym_names
,
1071 size_t sym_name_size
,
1072 Symbol
** sympointers
);
1076 Symbol_table::add_from_object
<64, false>(
1077 Sized_object
<64, false>* object
,
1078 const elfcpp::Sym
<64, false>* syms
,
1080 const char* sym_names
,
1081 size_t sym_name_size
,
1082 Symbol
** sympointers
);
1084 } // End namespace gold.