1 // symtab.cc -- the gold symbol table
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
32 #include "dwarf_reader.h"
36 #include "workqueue.h"
44 // Initialize fields in Symbol. This initializes everything except u_
48 Symbol::init_fields(const char* name
, const char* version
,
49 elfcpp::STT type
, elfcpp::STB binding
,
50 elfcpp::STV visibility
, unsigned char nonvis
)
53 this->version_
= version
;
54 this->symtab_index_
= 0;
55 this->dynsym_index_
= 0;
56 this->got_offset_
= 0;
57 this->plt_offset_
= 0;
59 this->binding_
= binding
;
60 this->visibility_
= visibility
;
61 this->nonvis_
= nonvis
;
62 this->is_target_special_
= false;
63 this->is_def_
= false;
64 this->is_forwarder_
= false;
65 this->has_alias_
= false;
66 this->needs_dynsym_entry_
= false;
67 this->in_reg_
= false;
68 this->in_dyn_
= false;
69 this->has_got_offset_
= false;
70 this->has_plt_offset_
= false;
71 this->has_warning_
= false;
72 this->is_copied_from_dynobj_
= false;
73 this->needs_value_in_got_
= false;
76 // Return the demangled version of the symbol's name, but only
77 // if the --demangle flag was set.
80 demangle(const char* name
)
82 // cplus_demangle allocates memory for the result it returns,
83 // and returns NULL if the name is already demangled.
84 char* demangled_name
= cplus_demangle(name
, DMGL_ANSI
| DMGL_PARAMS
);
85 if (demangled_name
== NULL
)
88 std::string
retval(demangled_name
);
94 Symbol::demangled_name() const
96 if (parameters
->demangle())
97 return demangle(name());
102 // Initialize the fields in the base class Symbol for SYM in OBJECT.
104 template<int size
, bool big_endian
>
106 Symbol::init_base(const char* name
, const char* version
, Object
* object
,
107 const elfcpp::Sym
<size
, big_endian
>& sym
)
109 this->init_fields(name
, version
, sym
.get_st_type(), sym
.get_st_bind(),
110 sym
.get_st_visibility(), sym
.get_st_nonvis());
111 this->u_
.from_object
.object
= object
;
112 // FIXME: Handle SHN_XINDEX.
113 this->u_
.from_object
.shndx
= sym
.get_st_shndx();
114 this->source_
= FROM_OBJECT
;
115 this->in_reg_
= !object
->is_dynamic();
116 this->in_dyn_
= object
->is_dynamic();
119 // Initialize the fields in the base class Symbol for a symbol defined
120 // in an Output_data.
123 Symbol::init_base(const char* name
, Output_data
* od
, elfcpp::STT type
,
124 elfcpp::STB binding
, elfcpp::STV visibility
,
125 unsigned char nonvis
, bool offset_is_from_end
)
127 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
128 this->u_
.in_output_data
.output_data
= od
;
129 this->u_
.in_output_data
.offset_is_from_end
= offset_is_from_end
;
130 this->source_
= IN_OUTPUT_DATA
;
131 this->in_reg_
= true;
134 // Initialize the fields in the base class Symbol for a symbol defined
135 // in an Output_segment.
138 Symbol::init_base(const char* name
, Output_segment
* os
, elfcpp::STT type
,
139 elfcpp::STB binding
, elfcpp::STV visibility
,
140 unsigned char nonvis
, Segment_offset_base offset_base
)
142 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
143 this->u_
.in_output_segment
.output_segment
= os
;
144 this->u_
.in_output_segment
.offset_base
= offset_base
;
145 this->source_
= IN_OUTPUT_SEGMENT
;
146 this->in_reg_
= true;
149 // Initialize the fields in the base class Symbol for a symbol defined
153 Symbol::init_base(const char* name
, elfcpp::STT type
,
154 elfcpp::STB binding
, elfcpp::STV visibility
,
155 unsigned char nonvis
)
157 this->init_fields(name
, NULL
, type
, binding
, visibility
, nonvis
);
158 this->source_
= CONSTANT
;
159 this->in_reg_
= true;
162 // Initialize the fields in Sized_symbol for SYM in OBJECT.
165 template<bool big_endian
>
167 Sized_symbol
<size
>::init(const char* name
, const char* version
, Object
* object
,
168 const elfcpp::Sym
<size
, big_endian
>& sym
)
170 this->init_base(name
, version
, object
, sym
);
171 this->value_
= sym
.get_st_value();
172 this->symsize_
= sym
.get_st_size();
175 // Initialize the fields in Sized_symbol for a symbol defined in an
180 Sized_symbol
<size
>::init(const char* name
, Output_data
* od
,
181 Value_type value
, Size_type symsize
,
182 elfcpp::STT type
, elfcpp::STB binding
,
183 elfcpp::STV visibility
, unsigned char nonvis
,
184 bool offset_is_from_end
)
186 this->init_base(name
, od
, type
, binding
, visibility
, nonvis
,
188 this->value_
= value
;
189 this->symsize_
= symsize
;
192 // Initialize the fields in Sized_symbol for a symbol defined in an
197 Sized_symbol
<size
>::init(const char* name
, Output_segment
* os
,
198 Value_type value
, Size_type symsize
,
199 elfcpp::STT type
, elfcpp::STB binding
,
200 elfcpp::STV visibility
, unsigned char nonvis
,
201 Segment_offset_base offset_base
)
203 this->init_base(name
, os
, type
, binding
, visibility
, nonvis
, offset_base
);
204 this->value_
= value
;
205 this->symsize_
= symsize
;
208 // Initialize the fields in Sized_symbol for a symbol defined as a
213 Sized_symbol
<size
>::init(const char* name
, Value_type value
, Size_type symsize
,
214 elfcpp::STT type
, elfcpp::STB binding
,
215 elfcpp::STV visibility
, unsigned char nonvis
)
217 this->init_base(name
, type
, binding
, visibility
, nonvis
);
218 this->value_
= value
;
219 this->symsize_
= symsize
;
222 // Return true if this symbol should be added to the dynamic symbol
226 Symbol::should_add_dynsym_entry() const
228 // If the symbol is used by a dynamic relocation, we need to add it.
229 if (this->needs_dynsym_entry())
232 // If exporting all symbols or building a shared library,
233 // and the symbol is defined in a regular object and is
234 // externally visible, we need to add it.
235 if ((parameters
->export_dynamic() || parameters
->output_is_shared())
236 && !this->is_from_dynobj()
237 && this->is_externally_visible())
243 // Return true if the final value of this symbol is known at link
247 Symbol::final_value_is_known() const
249 // If we are not generating an executable, then no final values are
250 // known, since they will change at runtime.
251 if (!parameters
->output_is_executable())
254 // If the symbol is not from an object file, then it is defined, and
256 if (this->source_
!= FROM_OBJECT
)
259 // If the symbol is from a dynamic object, then the final value is
261 if (this->object()->is_dynamic())
264 // If the symbol is not undefined (it is defined or common), then
265 // the final value is known.
266 if (!this->is_undefined())
269 // If the symbol is undefined, then whether the final value is known
270 // depends on whether we are doing a static link. If we are doing a
271 // dynamic link, then the final value could be filled in at runtime.
272 // This could reasonably be the case for a weak undefined symbol.
273 return parameters
->doing_static_link();
276 // Class Symbol_table.
278 Symbol_table::Symbol_table()
279 : saw_undefined_(0), offset_(0), table_(), namepool_(),
280 forwarders_(), commons_(), warnings_()
284 Symbol_table::~Symbol_table()
288 // The hash function. The key is always canonicalized, so we use a
289 // simple combination of the pointers.
292 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key
& key
) const
294 return key
.first
^ key
.second
;
297 // The symbol table key equality function. This is only called with
298 // canonicalized name and version strings, so we can use pointer
302 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key
& k1
,
303 const Symbol_table_key
& k2
) const
305 return k1
.first
== k2
.first
&& k1
.second
== k2
.second
;
308 // Make TO a symbol which forwards to FROM.
311 Symbol_table::make_forwarder(Symbol
* from
, Symbol
* to
)
313 gold_assert(from
!= to
);
314 gold_assert(!from
->is_forwarder() && !to
->is_forwarder());
315 this->forwarders_
[from
] = to
;
316 from
->set_forwarder();
319 // Resolve the forwards from FROM, returning the real symbol.
322 Symbol_table::resolve_forwards(const Symbol
* from
) const
324 gold_assert(from
->is_forwarder());
325 Unordered_map
<const Symbol
*, Symbol
*>::const_iterator p
=
326 this->forwarders_
.find(from
);
327 gold_assert(p
!= this->forwarders_
.end());
331 // Look up a symbol by name.
334 Symbol_table::lookup(const char* name
, const char* version
) const
336 Stringpool::Key name_key
;
337 name
= this->namepool_
.find(name
, &name_key
);
341 Stringpool::Key version_key
= 0;
344 version
= this->namepool_
.find(version
, &version_key
);
349 Symbol_table_key
key(name_key
, version_key
);
350 Symbol_table::Symbol_table_type::const_iterator p
= this->table_
.find(key
);
351 if (p
== this->table_
.end())
356 // Resolve a Symbol with another Symbol. This is only used in the
357 // unusual case where there are references to both an unversioned
358 // symbol and a symbol with a version, and we then discover that that
359 // version is the default version. Because this is unusual, we do
360 // this the slow way, by converting back to an ELF symbol.
362 template<int size
, bool big_endian
>
364 Symbol_table::resolve(Sized_symbol
<size
>* to
, const Sized_symbol
<size
>* from
,
365 const char* version ACCEPT_SIZE_ENDIAN
)
367 unsigned char buf
[elfcpp::Elf_sizes
<size
>::sym_size
];
368 elfcpp::Sym_write
<size
, big_endian
> esym(buf
);
369 // We don't bother to set the st_name field.
370 esym
.put_st_value(from
->value());
371 esym
.put_st_size(from
->symsize());
372 esym
.put_st_info(from
->binding(), from
->type());
373 esym
.put_st_other(from
->visibility(), from
->nonvis());
374 esym
.put_st_shndx(from
->shndx());
375 this->resolve(to
, esym
.sym(), esym
.sym(), from
->object(), version
);
382 // Add one symbol from OBJECT to the symbol table. NAME is symbol
383 // name and VERSION is the version; both are canonicalized. DEF is
384 // whether this is the default version.
386 // If DEF is true, then this is the definition of a default version of
387 // a symbol. That means that any lookup of NAME/NULL and any lookup
388 // of NAME/VERSION should always return the same symbol. This is
389 // obvious for references, but in particular we want to do this for
390 // definitions: overriding NAME/NULL should also override
391 // NAME/VERSION. If we don't do that, it would be very hard to
392 // override functions in a shared library which uses versioning.
394 // We implement this by simply making both entries in the hash table
395 // point to the same Symbol structure. That is easy enough if this is
396 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
397 // that we have seen both already, in which case they will both have
398 // independent entries in the symbol table. We can't simply change
399 // the symbol table entry, because we have pointers to the entries
400 // attached to the object files. So we mark the entry attached to the
401 // object file as a forwarder, and record it in the forwarders_ map.
402 // Note that entries in the hash table will never be marked as
405 // SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
406 // symbol exactly as it existed in the input file. SYM is usually
407 // that as well, but can be modified, for instance if we determine
408 // it's in a to-be-discarded section.
410 template<int size
, bool big_endian
>
412 Symbol_table::add_from_object(Object
* object
,
414 Stringpool::Key name_key
,
416 Stringpool::Key version_key
,
418 const elfcpp::Sym
<size
, big_endian
>& sym
,
419 const elfcpp::Sym
<size
, big_endian
>& orig_sym
)
421 Symbol
* const snull
= NULL
;
422 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
423 this->table_
.insert(std::make_pair(std::make_pair(name_key
, version_key
),
426 std::pair
<typename
Symbol_table_type::iterator
, bool> insdef
=
427 std::make_pair(this->table_
.end(), false);
430 const Stringpool::Key vnull_key
= 0;
431 insdef
= this->table_
.insert(std::make_pair(std::make_pair(name_key
,
436 // ins.first: an iterator, which is a pointer to a pair.
437 // ins.first->first: the key (a pair of name and version).
438 // ins.first->second: the value (Symbol*).
439 // ins.second: true if new entry was inserted, false if not.
441 Sized_symbol
<size
>* ret
;
446 // We already have an entry for NAME/VERSION.
447 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (ins
.first
->second
449 gold_assert(ret
!= NULL
);
451 was_undefined
= ret
->is_undefined();
452 was_common
= ret
->is_common();
454 this->resolve(ret
, sym
, orig_sym
, object
, version
);
460 // This is the first time we have seen NAME/NULL. Make
461 // NAME/NULL point to NAME/VERSION.
462 insdef
.first
->second
= ret
;
464 else if (insdef
.first
->second
!= ret
)
466 // This is the unfortunate case where we already have
467 // entries for both NAME/VERSION and NAME/NULL.
468 const Sized_symbol
<size
>* sym2
;
469 sym2
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
472 Symbol_table::resolve
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
473 ret
, sym2
, version
SELECT_SIZE_ENDIAN(size
, big_endian
));
474 this->make_forwarder(insdef
.first
->second
, ret
);
475 insdef
.first
->second
= ret
;
481 // This is the first time we have seen NAME/VERSION.
482 gold_assert(ins
.first
->second
== NULL
);
484 was_undefined
= false;
487 if (def
&& !insdef
.second
)
489 // We already have an entry for NAME/NULL. If we override
490 // it, then change it to NAME/VERSION.
491 ret
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (
494 this->resolve(ret
, sym
, orig_sym
, object
, version
);
495 ins
.first
->second
= ret
;
499 Sized_target
<size
, big_endian
>* target
=
500 object
->sized_target
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
501 SELECT_SIZE_ENDIAN_ONLY(size
, big_endian
));
502 if (!target
->has_make_symbol())
503 ret
= new Sized_symbol
<size
>();
506 ret
= target
->make_symbol();
509 // This means that we don't want a symbol table
512 this->table_
.erase(ins
.first
);
515 this->table_
.erase(insdef
.first
);
516 // Inserting insdef invalidated ins.
517 this->table_
.erase(std::make_pair(name_key
,
524 ret
->init(name
, version
, object
, sym
);
526 ins
.first
->second
= ret
;
529 // This is the first time we have seen NAME/NULL. Point
530 // it at the new entry for NAME/VERSION.
531 gold_assert(insdef
.second
);
532 insdef
.first
->second
= ret
;
537 // Record every time we see a new undefined symbol, to speed up
539 if (!was_undefined
&& ret
->is_undefined())
540 ++this->saw_undefined_
;
542 // Keep track of common symbols, to speed up common symbol
544 if (!was_common
&& ret
->is_common())
545 this->commons_
.push_back(ret
);
550 // Add all the symbols in a relocatable object to the hash table.
552 template<int size
, bool big_endian
>
554 Symbol_table::add_from_relobj(
555 Sized_relobj
<size
, big_endian
>* relobj
,
556 const unsigned char* syms
,
558 const char* sym_names
,
559 size_t sym_name_size
,
560 typename Sized_relobj
<size
, big_endian
>::Symbols
* sympointers
)
562 gold_assert(size
== relobj
->target()->get_size());
563 gold_assert(size
== parameters
->get_size());
565 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
567 const unsigned char* p
= syms
;
568 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
)
570 elfcpp::Sym
<size
, big_endian
> sym(p
);
571 elfcpp::Sym
<size
, big_endian
>* psym
= &sym
;
573 unsigned int st_name
= psym
->get_st_name();
574 if (st_name
>= sym_name_size
)
576 relobj
->error(_("bad global symbol name offset %u at %zu"),
581 const char* name
= sym_names
+ st_name
;
583 // A symbol defined in a section which we are not including must
584 // be treated as an undefined symbol.
585 unsigned char symbuf
[sym_size
];
586 elfcpp::Sym
<size
, big_endian
> sym2(symbuf
);
587 unsigned int st_shndx
= psym
->get_st_shndx();
588 if (st_shndx
!= elfcpp::SHN_UNDEF
589 && st_shndx
< elfcpp::SHN_LORESERVE
590 && !relobj
->is_section_included(st_shndx
))
592 memcpy(symbuf
, p
, sym_size
);
593 elfcpp::Sym_write
<size
, big_endian
> sw(symbuf
);
594 sw
.put_st_shndx(elfcpp::SHN_UNDEF
);
598 // In an object file, an '@' in the name separates the symbol
599 // name from the version name. If there are two '@' characters,
600 // this is the default version.
601 const char* ver
= strchr(name
, '@');
603 Sized_symbol
<size
>* res
;
606 Stringpool::Key name_key
;
607 name
= this->namepool_
.add(name
, true, &name_key
);
608 res
= this->add_from_object(relobj
, name
, name_key
, NULL
, 0,
613 Stringpool::Key name_key
;
614 name
= this->namepool_
.add_prefix(name
, ver
- name
, &name_key
);
624 Stringpool::Key ver_key
;
625 ver
= this->namepool_
.add(ver
, true, &ver_key
);
627 res
= this->add_from_object(relobj
, name
, name_key
, ver
, ver_key
,
631 (*sympointers
)[i
] = res
;
635 // Add all the symbols in a dynamic object to the hash table.
637 template<int size
, bool big_endian
>
639 Symbol_table::add_from_dynobj(
640 Sized_dynobj
<size
, big_endian
>* dynobj
,
641 const unsigned char* syms
,
643 const char* sym_names
,
644 size_t sym_name_size
,
645 const unsigned char* versym
,
647 const std::vector
<const char*>* version_map
)
649 gold_assert(size
== dynobj
->target()->get_size());
650 gold_assert(size
== parameters
->get_size());
652 if (versym
!= NULL
&& versym_size
/ 2 < count
)
654 dynobj
->error(_("too few symbol versions"));
658 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
660 // We keep a list of all STT_OBJECT symbols, so that we can resolve
661 // weak aliases. This is necessary because if the dynamic object
662 // provides the same variable under two names, one of which is a
663 // weak definition, and the regular object refers to the weak
664 // definition, we have to put both the weak definition and the
665 // strong definition into the dynamic symbol table. Given a weak
666 // definition, the only way that we can find the corresponding
667 // strong definition, if any, is to search the symbol table.
668 std::vector
<Sized_symbol
<size
>*> object_symbols
;
670 const unsigned char* p
= syms
;
671 const unsigned char* vs
= versym
;
672 for (size_t i
= 0; i
< count
; ++i
, p
+= sym_size
, vs
+= 2)
674 elfcpp::Sym
<size
, big_endian
> sym(p
);
676 // Ignore symbols with local binding.
677 if (sym
.get_st_bind() == elfcpp::STB_LOCAL
)
680 unsigned int st_name
= sym
.get_st_name();
681 if (st_name
>= sym_name_size
)
683 dynobj
->error(_("bad symbol name offset %u at %zu"),
688 const char* name
= sym_names
+ st_name
;
690 Sized_symbol
<size
>* res
;
694 Stringpool::Key name_key
;
695 name
= this->namepool_
.add(name
, true, &name_key
);
696 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
701 // Read the version information.
703 unsigned int v
= elfcpp::Swap
<16, big_endian
>::readval(vs
);
705 bool hidden
= (v
& elfcpp::VERSYM_HIDDEN
) != 0;
706 v
&= elfcpp::VERSYM_VERSION
;
708 // The Sun documentation says that V can be VER_NDX_LOCAL,
709 // or VER_NDX_GLOBAL, or a version index. The meaning of
710 // VER_NDX_LOCAL is defined as "Symbol has local scope."
711 // The old GNU linker will happily generate VER_NDX_LOCAL
712 // for an undefined symbol. I don't know what the Sun
713 // linker will generate.
715 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
716 && sym
.get_st_shndx() != elfcpp::SHN_UNDEF
)
718 // This symbol should not be visible outside the object.
722 // At this point we are definitely going to add this symbol.
723 Stringpool::Key name_key
;
724 name
= this->namepool_
.add(name
, true, &name_key
);
726 if (v
== static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL
)
727 || v
== static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL
))
729 // This symbol does not have a version.
730 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
735 if (v
>= version_map
->size())
737 dynobj
->error(_("versym for symbol %zu out of range: %u"),
742 const char* version
= (*version_map
)[v
];
745 dynobj
->error(_("versym for symbol %zu has no name: %u"),
750 Stringpool::Key version_key
;
751 version
= this->namepool_
.add(version
, true, &version_key
);
753 // If this is an absolute symbol, and the version name
754 // and symbol name are the same, then this is the
755 // version definition symbol. These symbols exist to
756 // support using -u to pull in particular versions. We
757 // do not want to record a version for them.
758 if (sym
.get_st_shndx() == elfcpp::SHN_ABS
759 && name_key
== version_key
)
760 res
= this->add_from_object(dynobj
, name
, name_key
, NULL
, 0,
764 const bool def
= (!hidden
765 && (sym
.get_st_shndx()
766 != elfcpp::SHN_UNDEF
));
767 res
= this->add_from_object(dynobj
, name
, name_key
, version
,
768 version_key
, def
, sym
, sym
);
773 if (sym
.get_st_shndx() != elfcpp::SHN_UNDEF
774 && sym
.get_st_type() == elfcpp::STT_OBJECT
)
775 object_symbols
.push_back(res
);
778 this->record_weak_aliases(&object_symbols
);
781 // This is used to sort weak aliases. We sort them first by section
782 // index, then by offset, then by weak ahead of strong.
785 class Weak_alias_sorter
788 bool operator()(const Sized_symbol
<size
>*, const Sized_symbol
<size
>*) const;
793 Weak_alias_sorter
<size
>::operator()(const Sized_symbol
<size
>* s1
,
794 const Sized_symbol
<size
>* s2
) const
796 if (s1
->shndx() != s2
->shndx())
797 return s1
->shndx() < s2
->shndx();
798 if (s1
->value() != s2
->value())
799 return s1
->value() < s2
->value();
800 if (s1
->binding() != s2
->binding())
802 if (s1
->binding() == elfcpp::STB_WEAK
)
804 if (s2
->binding() == elfcpp::STB_WEAK
)
807 return std::string(s1
->name()) < std::string(s2
->name());
810 // SYMBOLS is a list of object symbols from a dynamic object. Look
811 // for any weak aliases, and record them so that if we add the weak
812 // alias to the dynamic symbol table, we also add the corresponding
817 Symbol_table::record_weak_aliases(std::vector
<Sized_symbol
<size
>*>* symbols
)
819 // Sort the vector by section index, then by offset, then by weak
821 std::sort(symbols
->begin(), symbols
->end(), Weak_alias_sorter
<size
>());
823 // Walk through the vector. For each weak definition, record
825 for (typename
std::vector
<Sized_symbol
<size
>*>::const_iterator p
=
830 if ((*p
)->binding() != elfcpp::STB_WEAK
)
833 // Build a circular list of weak aliases. Each symbol points to
834 // the next one in the circular list.
836 Sized_symbol
<size
>* from_sym
= *p
;
837 typename
std::vector
<Sized_symbol
<size
>*>::const_iterator q
;
838 for (q
= p
+ 1; q
!= symbols
->end(); ++q
)
840 if ((*q
)->shndx() != from_sym
->shndx()
841 || (*q
)->value() != from_sym
->value())
844 this->weak_aliases_
[from_sym
] = *q
;
845 from_sym
->set_has_alias();
851 this->weak_aliases_
[from_sym
] = *p
;
852 from_sym
->set_has_alias();
859 // Create and return a specially defined symbol. If ONLY_IF_REF is
860 // true, then only create the symbol if there is a reference to it.
861 // If this does not return NULL, it sets *POLDSYM to the existing
862 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
864 template<int size
, bool big_endian
>
866 Symbol_table::define_special_symbol(const Target
* target
, const char** pname
,
867 const char** pversion
, bool only_if_ref
,
868 Sized_symbol
<size
>** poldsym
872 Sized_symbol
<size
>* sym
;
873 bool add_to_table
= false;
874 typename
Symbol_table_type::iterator add_loc
= this->table_
.end();
878 oldsym
= this->lookup(*pname
, *pversion
);
879 if (oldsym
== NULL
|| !oldsym
->is_undefined())
882 *pname
= oldsym
->name();
883 *pversion
= oldsym
->version();
887 // Canonicalize NAME and VERSION.
888 Stringpool::Key name_key
;
889 *pname
= this->namepool_
.add(*pname
, true, &name_key
);
891 Stringpool::Key version_key
= 0;
892 if (*pversion
!= NULL
)
893 *pversion
= this->namepool_
.add(*pversion
, true, &version_key
);
895 Symbol
* const snull
= NULL
;
896 std::pair
<typename
Symbol_table_type::iterator
, bool> ins
=
897 this->table_
.insert(std::make_pair(std::make_pair(name_key
,
903 // We already have a symbol table entry for NAME/VERSION.
904 oldsym
= ins
.first
->second
;
905 gold_assert(oldsym
!= NULL
);
909 // We haven't seen this symbol before.
910 gold_assert(ins
.first
->second
== NULL
);
917 if (!target
->has_make_symbol())
918 sym
= new Sized_symbol
<size
>();
921 gold_assert(target
->get_size() == size
);
922 gold_assert(target
->is_big_endian() ? big_endian
: !big_endian
);
923 typedef Sized_target
<size
, big_endian
> My_target
;
924 const My_target
* sized_target
=
925 static_cast<const My_target
*>(target
);
926 sym
= sized_target
->make_symbol();
932 add_loc
->second
= sym
;
934 gold_assert(oldsym
!= NULL
);
936 *poldsym
= this->get_sized_symbol
SELECT_SIZE_NAME(size
) (oldsym
942 // Define a symbol based on an Output_data.
945 Symbol_table::define_in_output_data(const Target
* target
, const char* name
,
946 const char* version
, Output_data
* od
,
947 uint64_t value
, uint64_t symsize
,
948 elfcpp::STT type
, elfcpp::STB binding
,
949 elfcpp::STV visibility
,
950 unsigned char nonvis
,
951 bool offset_is_from_end
,
954 if (parameters
->get_size() == 32)
956 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
957 return this->do_define_in_output_data
<32>(target
, name
, version
, od
,
958 value
, symsize
, type
, binding
,
966 else if (parameters
->get_size() == 64)
968 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
969 return this->do_define_in_output_data
<64>(target
, name
, version
, od
,
970 value
, symsize
, type
, binding
,
982 // Define a symbol in an Output_data, sized version.
986 Symbol_table::do_define_in_output_data(
987 const Target
* target
,
991 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
992 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
995 elfcpp::STV visibility
,
996 unsigned char nonvis
,
997 bool offset_is_from_end
,
1000 Sized_symbol
<size
>* sym
;
1001 Sized_symbol
<size
>* oldsym
;
1003 if (parameters
->is_big_endian())
1005 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1006 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1007 target
, &name
, &version
, only_if_ref
, &oldsym
1008 SELECT_SIZE_ENDIAN(size
, true));
1015 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1016 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1017 target
, &name
, &version
, only_if_ref
, &oldsym
1018 SELECT_SIZE_ENDIAN(size
, false));
1027 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1028 sym
->init(name
, od
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1029 offset_is_from_end
);
1032 && Symbol_table::should_override_with_special(oldsym
))
1033 this->override_with_special(oldsym
, sym
);
1038 // Define a symbol based on an Output_segment.
1041 Symbol_table::define_in_output_segment(const Target
* target
, const char* name
,
1042 const char* version
, Output_segment
* os
,
1043 uint64_t value
, uint64_t symsize
,
1044 elfcpp::STT type
, elfcpp::STB binding
,
1045 elfcpp::STV visibility
,
1046 unsigned char nonvis
,
1047 Symbol::Segment_offset_base offset_base
,
1050 if (parameters
->get_size() == 32)
1052 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1053 return this->do_define_in_output_segment
<32>(target
, name
, version
, os
,
1054 value
, symsize
, type
,
1055 binding
, visibility
, nonvis
,
1056 offset_base
, only_if_ref
);
1061 else if (parameters
->get_size() == 64)
1063 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1064 return this->do_define_in_output_segment
<64>(target
, name
, version
, os
,
1065 value
, symsize
, type
,
1066 binding
, visibility
, nonvis
,
1067 offset_base
, only_if_ref
);
1076 // Define a symbol in an Output_segment, sized version.
1080 Symbol_table::do_define_in_output_segment(
1081 const Target
* target
,
1083 const char* version
,
1085 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1086 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1088 elfcpp::STB binding
,
1089 elfcpp::STV visibility
,
1090 unsigned char nonvis
,
1091 Symbol::Segment_offset_base offset_base
,
1094 Sized_symbol
<size
>* sym
;
1095 Sized_symbol
<size
>* oldsym
;
1097 if (parameters
->is_big_endian())
1099 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1100 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1101 target
, &name
, &version
, only_if_ref
, &oldsym
1102 SELECT_SIZE_ENDIAN(size
, true));
1109 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1110 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1111 target
, &name
, &version
, only_if_ref
, &oldsym
1112 SELECT_SIZE_ENDIAN(size
, false));
1121 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1122 sym
->init(name
, os
, value
, symsize
, type
, binding
, visibility
, nonvis
,
1126 && Symbol_table::should_override_with_special(oldsym
))
1127 this->override_with_special(oldsym
, sym
);
1132 // Define a special symbol with a constant value. It is a multiple
1133 // definition error if this symbol is already defined.
1136 Symbol_table::define_as_constant(const Target
* target
, const char* name
,
1137 const char* version
, uint64_t value
,
1138 uint64_t symsize
, elfcpp::STT type
,
1139 elfcpp::STB binding
, elfcpp::STV visibility
,
1140 unsigned char nonvis
, bool only_if_ref
)
1142 if (parameters
->get_size() == 32)
1144 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1145 return this->do_define_as_constant
<32>(target
, name
, version
, value
,
1146 symsize
, type
, binding
,
1147 visibility
, nonvis
, only_if_ref
);
1152 else if (parameters
->get_size() == 64)
1154 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1155 return this->do_define_as_constant
<64>(target
, name
, version
, value
,
1156 symsize
, type
, binding
,
1157 visibility
, nonvis
, only_if_ref
);
1166 // Define a symbol as a constant, sized version.
1170 Symbol_table::do_define_as_constant(
1171 const Target
* target
,
1173 const char* version
,
1174 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1175 typename
elfcpp::Elf_types
<size
>::Elf_WXword symsize
,
1177 elfcpp::STB binding
,
1178 elfcpp::STV visibility
,
1179 unsigned char nonvis
,
1182 Sized_symbol
<size
>* sym
;
1183 Sized_symbol
<size
>* oldsym
;
1185 if (parameters
->is_big_endian())
1187 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1188 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, true) (
1189 target
, &name
, &version
, only_if_ref
, &oldsym
1190 SELECT_SIZE_ENDIAN(size
, true));
1197 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1198 sym
= this->define_special_symbol
SELECT_SIZE_ENDIAN_NAME(size
, false) (
1199 target
, &name
, &version
, only_if_ref
, &oldsym
1200 SELECT_SIZE_ENDIAN(size
, false));
1209 gold_assert(version
== NULL
|| oldsym
!= NULL
);
1210 sym
->init(name
, value
, symsize
, type
, binding
, visibility
, nonvis
);
1213 && Symbol_table::should_override_with_special(oldsym
))
1214 this->override_with_special(oldsym
, sym
);
1219 // Define a set of symbols in output sections.
1222 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1223 int count
, const Define_symbol_in_section
* p
)
1225 for (int i
= 0; i
< count
; ++i
, ++p
)
1227 Output_section
* os
= layout
->find_output_section(p
->output_section
);
1229 this->define_in_output_data(target
, p
->name
, NULL
, os
, p
->value
,
1230 p
->size
, p
->type
, p
->binding
,
1231 p
->visibility
, p
->nonvis
,
1232 p
->offset_is_from_end
, p
->only_if_ref
);
1234 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1235 p
->binding
, p
->visibility
, p
->nonvis
,
1240 // Define a set of symbols in output segments.
1243 Symbol_table::define_symbols(const Layout
* layout
, const Target
* target
,
1244 int count
, const Define_symbol_in_segment
* p
)
1246 for (int i
= 0; i
< count
; ++i
, ++p
)
1248 Output_segment
* os
= layout
->find_output_segment(p
->segment_type
,
1249 p
->segment_flags_set
,
1250 p
->segment_flags_clear
);
1252 this->define_in_output_segment(target
, p
->name
, NULL
, os
, p
->value
,
1253 p
->size
, p
->type
, p
->binding
,
1254 p
->visibility
, p
->nonvis
,
1255 p
->offset_base
, p
->only_if_ref
);
1257 this->define_as_constant(target
, p
->name
, NULL
, 0, p
->size
, p
->type
,
1258 p
->binding
, p
->visibility
, p
->nonvis
,
1263 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1264 // symbol should be defined--typically a .dyn.bss section. VALUE is
1265 // the offset within POSD.
1269 Symbol_table::define_with_copy_reloc(const Target
* target
,
1270 Sized_symbol
<size
>* csym
,
1271 Output_data
* posd
, uint64_t value
)
1273 gold_assert(csym
->is_from_dynobj());
1274 gold_assert(!csym
->is_copied_from_dynobj());
1275 Object
* object
= csym
->object();
1276 gold_assert(object
->is_dynamic());
1277 Dynobj
* dynobj
= static_cast<Dynobj
*>(object
);
1279 // Our copied variable has to override any variable in a shared
1281 elfcpp::STB binding
= csym
->binding();
1282 if (binding
== elfcpp::STB_WEAK
)
1283 binding
= elfcpp::STB_GLOBAL
;
1285 this->define_in_output_data(target
, csym
->name(), csym
->version(),
1286 posd
, value
, csym
->symsize(),
1287 csym
->type(), binding
,
1288 csym
->visibility(), csym
->nonvis(),
1291 csym
->set_is_copied_from_dynobj();
1292 csym
->set_needs_dynsym_entry();
1294 this->copied_symbol_dynobjs_
[csym
] = dynobj
;
1296 // We have now defined all aliases, but we have not entered them all
1297 // in the copied_symbol_dynobjs_ map.
1298 if (csym
->has_alias())
1303 sym
= this->weak_aliases_
[sym
];
1306 gold_assert(sym
->output_data() == posd
);
1308 sym
->set_is_copied_from_dynobj();
1309 this->copied_symbol_dynobjs_
[sym
] = dynobj
;
1314 // SYM is defined using a COPY reloc. Return the dynamic object where
1315 // the original definition was found.
1318 Symbol_table::get_copy_source(const Symbol
* sym
) const
1320 gold_assert(sym
->is_copied_from_dynobj());
1321 Copied_symbol_dynobjs::const_iterator p
=
1322 this->copied_symbol_dynobjs_
.find(sym
);
1323 gold_assert(p
!= this->copied_symbol_dynobjs_
.end());
1327 // Set the dynamic symbol indexes. INDEX is the index of the first
1328 // global dynamic symbol. Pointers to the symbols are stored into the
1329 // vector SYMS. The names are added to DYNPOOL. This returns an
1330 // updated dynamic symbol index.
1333 Symbol_table::set_dynsym_indexes(const Target
* target
,
1335 std::vector
<Symbol
*>* syms
,
1336 Stringpool
* dynpool
,
1339 for (Symbol_table_type::iterator p
= this->table_
.begin();
1340 p
!= this->table_
.end();
1343 Symbol
* sym
= p
->second
;
1345 // Note that SYM may already have a dynamic symbol index, since
1346 // some symbols appear more than once in the symbol table, with
1347 // and without a version.
1349 if (!sym
->should_add_dynsym_entry())
1350 sym
->set_dynsym_index(-1U);
1351 else if (!sym
->has_dynsym_index())
1353 sym
->set_dynsym_index(index
);
1355 syms
->push_back(sym
);
1356 dynpool
->add(sym
->name(), false, NULL
);
1358 // Record any version information.
1359 if (sym
->version() != NULL
)
1360 versions
->record_version(this, dynpool
, sym
);
1364 // Finish up the versions. In some cases this may add new dynamic
1366 index
= versions
->finalize(target
, this, index
, syms
);
1371 // Set the final values for all the symbols. The index of the first
1372 // global symbol in the output file is INDEX. Record the file offset
1373 // OFF. Add their names to POOL. Return the new file offset.
1376 Symbol_table::finalize(unsigned int index
, off_t off
, off_t dynoff
,
1377 size_t dyn_global_index
, size_t dyncount
,
1382 gold_assert(index
!= 0);
1383 this->first_global_index_
= index
;
1385 this->dynamic_offset_
= dynoff
;
1386 this->first_dynamic_global_index_
= dyn_global_index
;
1387 this->dynamic_count_
= dyncount
;
1389 if (parameters
->get_size() == 32)
1391 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1392 ret
= this->sized_finalize
<32>(index
, off
, pool
);
1397 else if (parameters
->get_size() == 64)
1399 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1400 ret
= this->sized_finalize
<64>(index
, off
, pool
);
1408 // Now that we have the final symbol table, we can reliably note
1409 // which symbols should get warnings.
1410 this->warnings_
.note_warnings(this);
1415 // Set the final value for all the symbols. This is called after
1416 // Layout::finalize, so all the output sections have their final
1421 Symbol_table::sized_finalize(unsigned index
, off_t off
, Stringpool
* pool
)
1423 off
= align_address(off
, size
>> 3);
1424 this->offset_
= off
;
1426 size_t orig_index
= index
;
1428 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1429 for (Symbol_table_type::iterator p
= this->table_
.begin();
1430 p
!= this->table_
.end();
1433 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1435 // FIXME: Here we need to decide which symbols should go into
1436 // the output file, based on --strip.
1438 // The default version of a symbol may appear twice in the
1439 // symbol table. We only need to finalize it once.
1440 if (sym
->has_symtab_index())
1445 gold_assert(!sym
->has_symtab_index());
1446 sym
->set_symtab_index(-1U);
1447 gold_assert(sym
->dynsym_index() == -1U);
1451 typename Sized_symbol
<size
>::Value_type value
;
1453 switch (sym
->source())
1455 case Symbol::FROM_OBJECT
:
1457 unsigned int shndx
= sym
->shndx();
1459 // FIXME: We need some target specific support here.
1460 if (shndx
>= elfcpp::SHN_LORESERVE
1461 && shndx
!= elfcpp::SHN_ABS
)
1463 gold_error(_("%s: unsupported symbol section 0x%x"),
1464 sym
->demangled_name().c_str(), shndx
);
1465 shndx
= elfcpp::SHN_UNDEF
;
1468 Object
* symobj
= sym
->object();
1469 if (symobj
->is_dynamic())
1472 shndx
= elfcpp::SHN_UNDEF
;
1474 else if (shndx
== elfcpp::SHN_UNDEF
)
1476 else if (shndx
== elfcpp::SHN_ABS
)
1477 value
= sym
->value();
1480 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1482 Output_section
* os
= relobj
->output_section(shndx
, &secoff
);
1486 sym
->set_symtab_index(-1U);
1487 gold_assert(sym
->dynsym_index() == -1U);
1491 value
= sym
->value() + os
->address() + secoff
;
1496 case Symbol::IN_OUTPUT_DATA
:
1498 Output_data
* od
= sym
->output_data();
1499 value
= sym
->value() + od
->address();
1500 if (sym
->offset_is_from_end())
1501 value
+= od
->data_size();
1505 case Symbol::IN_OUTPUT_SEGMENT
:
1507 Output_segment
* os
= sym
->output_segment();
1508 value
= sym
->value() + os
->vaddr();
1509 switch (sym
->offset_base())
1511 case Symbol::SEGMENT_START
:
1513 case Symbol::SEGMENT_END
:
1514 value
+= os
->memsz();
1516 case Symbol::SEGMENT_BSS
:
1517 value
+= os
->filesz();
1525 case Symbol::CONSTANT
:
1526 value
= sym
->value();
1533 sym
->set_value(value
);
1535 if (parameters
->strip_all())
1536 sym
->set_symtab_index(-1U);
1539 sym
->set_symtab_index(index
);
1540 pool
->add(sym
->name(), false, NULL
);
1546 this->output_count_
= index
- orig_index
;
1551 // Write out the global symbols.
1554 Symbol_table::write_globals(const Input_objects
* input_objects
,
1555 const Stringpool
* sympool
,
1556 const Stringpool
* dynpool
, Output_file
* of
) const
1558 if (parameters
->get_size() == 32)
1560 if (parameters
->is_big_endian())
1562 #ifdef HAVE_TARGET_32_BIG
1563 this->sized_write_globals
<32, true>(input_objects
, sympool
,
1571 #ifdef HAVE_TARGET_32_LITTLE
1572 this->sized_write_globals
<32, false>(input_objects
, sympool
,
1579 else if (parameters
->get_size() == 64)
1581 if (parameters
->is_big_endian())
1583 #ifdef HAVE_TARGET_64_BIG
1584 this->sized_write_globals
<64, true>(input_objects
, sympool
,
1592 #ifdef HAVE_TARGET_64_LITTLE
1593 this->sized_write_globals
<64, false>(input_objects
, sympool
,
1604 // Write out the global symbols.
1606 template<int size
, bool big_endian
>
1608 Symbol_table::sized_write_globals(const Input_objects
* input_objects
,
1609 const Stringpool
* sympool
,
1610 const Stringpool
* dynpool
,
1611 Output_file
* of
) const
1613 const Target
* const target
= input_objects
->target();
1615 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1616 unsigned int index
= this->first_global_index_
;
1617 const off_t oview_size
= this->output_count_
* sym_size
;
1618 unsigned char* const psyms
= of
->get_output_view(this->offset_
, oview_size
);
1620 unsigned int dynamic_count
= this->dynamic_count_
;
1621 off_t dynamic_size
= dynamic_count
* sym_size
;
1622 unsigned int first_dynamic_global_index
= this->first_dynamic_global_index_
;
1623 unsigned char* dynamic_view
;
1624 if (this->dynamic_offset_
== 0)
1625 dynamic_view
= NULL
;
1627 dynamic_view
= of
->get_output_view(this->dynamic_offset_
, dynamic_size
);
1629 unsigned char* ps
= psyms
;
1630 for (Symbol_table_type::const_iterator p
= this->table_
.begin();
1631 p
!= this->table_
.end();
1634 Sized_symbol
<size
>* sym
= static_cast<Sized_symbol
<size
>*>(p
->second
);
1636 // Possibly warn about unresolved symbols in shared libraries.
1637 this->warn_about_undefined_dynobj_symbol(input_objects
, sym
);
1639 unsigned int sym_index
= sym
->symtab_index();
1640 unsigned int dynsym_index
;
1641 if (dynamic_view
== NULL
)
1644 dynsym_index
= sym
->dynsym_index();
1646 if (sym_index
== -1U && dynsym_index
== -1U)
1648 // This symbol is not included in the output file.
1652 if (sym_index
== index
)
1654 else if (sym_index
!= -1U)
1656 // We have already seen this symbol, because it has a
1658 gold_assert(sym_index
< index
);
1659 if (dynsym_index
== -1U)
1665 typename
elfcpp::Elf_types
<32>::Elf_Addr value
= sym
->value();
1666 switch (sym
->source())
1668 case Symbol::FROM_OBJECT
:
1670 unsigned int in_shndx
= sym
->shndx();
1672 // FIXME: We need some target specific support here.
1673 if (in_shndx
>= elfcpp::SHN_LORESERVE
1674 && in_shndx
!= elfcpp::SHN_ABS
)
1676 gold_error(_("%s: unsupported symbol section 0x%x"),
1677 sym
->demangled_name().c_str(), in_shndx
);
1682 Object
* symobj
= sym
->object();
1683 if (symobj
->is_dynamic())
1685 if (sym
->needs_dynsym_value())
1686 value
= target
->dynsym_value(sym
);
1687 shndx
= elfcpp::SHN_UNDEF
;
1689 else if (in_shndx
== elfcpp::SHN_UNDEF
1690 || in_shndx
== elfcpp::SHN_ABS
)
1694 Relobj
* relobj
= static_cast<Relobj
*>(symobj
);
1696 Output_section
* os
= relobj
->output_section(in_shndx
,
1698 gold_assert(os
!= NULL
);
1699 shndx
= os
->out_shndx();
1705 case Symbol::IN_OUTPUT_DATA
:
1706 shndx
= sym
->output_data()->out_shndx();
1709 case Symbol::IN_OUTPUT_SEGMENT
:
1710 shndx
= elfcpp::SHN_ABS
;
1713 case Symbol::CONSTANT
:
1714 shndx
= elfcpp::SHN_ABS
;
1721 if (sym_index
!= -1U)
1723 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1724 sym
, sym
->value(), shndx
, sympool
, ps
1725 SELECT_SIZE_ENDIAN(size
, big_endian
));
1729 if (dynsym_index
!= -1U)
1731 dynsym_index
-= first_dynamic_global_index
;
1732 gold_assert(dynsym_index
< dynamic_count
);
1733 unsigned char* pd
= dynamic_view
+ (dynsym_index
* sym_size
);
1734 this->sized_write_symbol
SELECT_SIZE_ENDIAN_NAME(size
, big_endian
) (
1735 sym
, value
, shndx
, dynpool
, pd
1736 SELECT_SIZE_ENDIAN(size
, big_endian
));
1740 gold_assert(ps
- psyms
== oview_size
);
1742 of
->write_output_view(this->offset_
, oview_size
, psyms
);
1743 if (dynamic_view
!= NULL
)
1744 of
->write_output_view(this->dynamic_offset_
, dynamic_size
, dynamic_view
);
1747 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1748 // strtab holding the name.
1750 template<int size
, bool big_endian
>
1752 Symbol_table::sized_write_symbol(
1753 Sized_symbol
<size
>* sym
,
1754 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1756 const Stringpool
* pool
,
1758 ACCEPT_SIZE_ENDIAN
) const
1760 elfcpp::Sym_write
<size
, big_endian
> osym(p
);
1761 osym
.put_st_name(pool
->get_offset(sym
->name()));
1762 osym
.put_st_value(value
);
1763 osym
.put_st_size(sym
->symsize());
1764 osym
.put_st_info(elfcpp::elf_st_info(sym
->binding(), sym
->type()));
1765 osym
.put_st_other(elfcpp::elf_st_other(sym
->visibility(), sym
->nonvis()));
1766 osym
.put_st_shndx(shndx
);
1769 // Check for unresolved symbols in shared libraries. This is
1770 // controlled by the --allow-shlib-undefined option.
1772 // We only warn about libraries for which we have seen all the
1773 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1774 // which were not seen in this link. If we didn't see a DT_NEEDED
1775 // entry, we aren't going to be able to reliably report whether the
1776 // symbol is undefined.
1778 // We also don't warn about libraries found in the system library
1779 // directory (the directory were we find libc.so); we assume that
1780 // those libraries are OK. This heuristic avoids problems in
1781 // GNU/Linux, in which -ldl can have undefined references satisfied by
1785 Symbol_table::warn_about_undefined_dynobj_symbol(
1786 const Input_objects
* input_objects
,
1789 if (sym
->source() == Symbol::FROM_OBJECT
1790 && sym
->object()->is_dynamic()
1791 && sym
->shndx() == elfcpp::SHN_UNDEF
1792 && sym
->binding() != elfcpp::STB_WEAK
1793 && !parameters
->allow_shlib_undefined()
1794 && !input_objects
->target()->is_defined_by_abi(sym
)
1795 && !input_objects
->found_in_system_library_directory(sym
->object()))
1797 // A very ugly cast.
1798 Dynobj
* dynobj
= static_cast<Dynobj
*>(sym
->object());
1799 if (!dynobj
->has_unknown_needed_entries())
1800 gold_error(_("%s: undefined reference to '%s'"),
1801 sym
->object()->name().c_str(),
1802 sym
->demangled_name().c_str());
1806 // Write out a section symbol. Return the update offset.
1809 Symbol_table::write_section_symbol(const Output_section
*os
,
1813 if (parameters
->get_size() == 32)
1815 if (parameters
->is_big_endian())
1817 #ifdef HAVE_TARGET_32_BIG
1818 this->sized_write_section_symbol
<32, true>(os
, of
, offset
);
1825 #ifdef HAVE_TARGET_32_LITTLE
1826 this->sized_write_section_symbol
<32, false>(os
, of
, offset
);
1832 else if (parameters
->get_size() == 64)
1834 if (parameters
->is_big_endian())
1836 #ifdef HAVE_TARGET_64_BIG
1837 this->sized_write_section_symbol
<64, true>(os
, of
, offset
);
1844 #ifdef HAVE_TARGET_64_LITTLE
1845 this->sized_write_section_symbol
<64, false>(os
, of
, offset
);
1855 // Write out a section symbol, specialized for size and endianness.
1857 template<int size
, bool big_endian
>
1859 Symbol_table::sized_write_section_symbol(const Output_section
* os
,
1863 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1865 unsigned char* pov
= of
->get_output_view(offset
, sym_size
);
1867 elfcpp::Sym_write
<size
, big_endian
> osym(pov
);
1868 osym
.put_st_name(0);
1869 osym
.put_st_value(os
->address());
1870 osym
.put_st_size(0);
1871 osym
.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL
,
1872 elfcpp::STT_SECTION
));
1873 osym
.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT
, 0));
1874 osym
.put_st_shndx(os
->out_shndx());
1876 of
->write_output_view(offset
, sym_size
, pov
);
1879 // Check candidate_odr_violations_ to find symbols with the same name
1880 // but apparently different definitions (different source-file/line-no).
1883 Symbol_table::detect_odr_violations(const char* output_file_name
) const
1885 for (Odr_map::const_iterator it
= candidate_odr_violations_
.begin();
1886 it
!= candidate_odr_violations_
.end();
1889 const char* symbol_name
= it
->first
;
1890 // We use a sorted set so the output is deterministic.
1891 std::set
<std::string
> line_nums
;
1893 Unordered_set
<Symbol_location
, Symbol_location_hash
>::const_iterator
1895 for (locs
= it
->second
.begin(); locs
!= it
->second
.end(); ++locs
)
1897 // We need to lock the object in order to read it. This
1898 // means that we can not run inside a Task. If we want to
1899 // run this in a Task for better performance, we will need
1900 // one Task for object, plus appropriate locking to ensure
1901 // that we don't conflict with other uses of the object.
1902 locs
->object
->lock();
1903 std::string lineno
= Dwarf_line_info::one_addr2line(
1904 locs
->object
, locs
->shndx
, locs
->offset
);
1905 locs
->object
->unlock();
1906 if (!lineno
.empty())
1907 line_nums
.insert(lineno
);
1910 if (line_nums
.size() > 1)
1912 gold_warning(_("while linking %s: symbol %s defined in multiple "
1913 "places (possible ODR violation):"),
1914 output_file_name
, demangle(symbol_name
).c_str());
1915 for (std::set
<std::string
>::const_iterator it2
= line_nums
.begin();
1916 it2
!= line_nums
.end();
1918 fprintf(stderr
, " %s\n", it2
->c_str());
1923 // Warnings functions.
1925 // Add a new warning.
1928 Warnings::add_warning(Symbol_table
* symtab
, const char* name
, Object
* obj
,
1931 name
= symtab
->canonicalize_name(name
);
1932 this->warnings_
[name
].set(obj
, shndx
);
1935 // Look through the warnings and mark the symbols for which we should
1936 // warn. This is called during Layout::finalize when we know the
1937 // sources for all the symbols.
1940 Warnings::note_warnings(Symbol_table
* symtab
)
1942 for (Warning_table::iterator p
= this->warnings_
.begin();
1943 p
!= this->warnings_
.end();
1946 Symbol
* sym
= symtab
->lookup(p
->first
, NULL
);
1948 && sym
->source() == Symbol::FROM_OBJECT
1949 && sym
->object() == p
->second
.object
)
1951 sym
->set_has_warning();
1953 // Read the section contents to get the warning text. It
1954 // would be nicer if we only did this if we have to actually
1955 // issue a warning. Unfortunately, warnings are issued as
1956 // we relocate sections. That means that we can not lock
1957 // the object then, as we might try to issue the same
1958 // warning multiple times simultaneously.
1960 Task_locker_obj
<Object
> tl(*p
->second
.object
);
1961 const unsigned char* c
;
1963 c
= p
->second
.object
->section_contents(p
->second
.shndx
, &len
,
1965 p
->second
.set_text(reinterpret_cast<const char*>(c
), len
);
1971 // Issue a warning. This is called when we see a relocation against a
1972 // symbol for which has a warning.
1974 template<int size
, bool big_endian
>
1976 Warnings::issue_warning(const Symbol
* sym
,
1977 const Relocate_info
<size
, big_endian
>* relinfo
,
1978 size_t relnum
, off_t reloffset
) const
1980 gold_assert(sym
->has_warning());
1981 Warning_table::const_iterator p
= this->warnings_
.find(sym
->name());
1982 gold_assert(p
!= this->warnings_
.end());
1983 gold_warning_at_location(relinfo
, relnum
, reloffset
,
1984 "%s", p
->second
.text
.c_str());
1987 // Instantiate the templates we need. We could use the configure
1988 // script to restrict this to only the ones needed for implemented
1991 #ifdef HAVE_TARGET_32_LITTLE
1994 Symbol_table::add_from_relobj
<32, false>(
1995 Sized_relobj
<32, false>* relobj
,
1996 const unsigned char* syms
,
1998 const char* sym_names
,
1999 size_t sym_name_size
,
2000 Sized_relobj
<32, true>::Symbols
* sympointers
);
2003 #ifdef HAVE_TARGET_32_BIG
2006 Symbol_table::add_from_relobj
<32, true>(
2007 Sized_relobj
<32, true>* relobj
,
2008 const unsigned char* syms
,
2010 const char* sym_names
,
2011 size_t sym_name_size
,
2012 Sized_relobj
<32, false>::Symbols
* sympointers
);
2015 #ifdef HAVE_TARGET_64_LITTLE
2018 Symbol_table::add_from_relobj
<64, false>(
2019 Sized_relobj
<64, false>* relobj
,
2020 const unsigned char* syms
,
2022 const char* sym_names
,
2023 size_t sym_name_size
,
2024 Sized_relobj
<64, true>::Symbols
* sympointers
);
2027 #ifdef HAVE_TARGET_64_BIG
2030 Symbol_table::add_from_relobj
<64, true>(
2031 Sized_relobj
<64, true>* relobj
,
2032 const unsigned char* syms
,
2034 const char* sym_names
,
2035 size_t sym_name_size
,
2036 Sized_relobj
<64, false>::Symbols
* sympointers
);
2039 #ifdef HAVE_TARGET_32_LITTLE
2042 Symbol_table::add_from_dynobj
<32, false>(
2043 Sized_dynobj
<32, false>* dynobj
,
2044 const unsigned char* syms
,
2046 const char* sym_names
,
2047 size_t sym_name_size
,
2048 const unsigned char* versym
,
2050 const std::vector
<const char*>* version_map
);
2053 #ifdef HAVE_TARGET_32_BIG
2056 Symbol_table::add_from_dynobj
<32, true>(
2057 Sized_dynobj
<32, true>* dynobj
,
2058 const unsigned char* syms
,
2060 const char* sym_names
,
2061 size_t sym_name_size
,
2062 const unsigned char* versym
,
2064 const std::vector
<const char*>* version_map
);
2067 #ifdef HAVE_TARGET_64_LITTLE
2070 Symbol_table::add_from_dynobj
<64, false>(
2071 Sized_dynobj
<64, false>* dynobj
,
2072 const unsigned char* syms
,
2074 const char* sym_names
,
2075 size_t sym_name_size
,
2076 const unsigned char* versym
,
2078 const std::vector
<const char*>* version_map
);
2081 #ifdef HAVE_TARGET_64_BIG
2084 Symbol_table::add_from_dynobj
<64, true>(
2085 Sized_dynobj
<64, true>* dynobj
,
2086 const unsigned char* syms
,
2088 const char* sym_names
,
2089 size_t sym_name_size
,
2090 const unsigned char* versym
,
2092 const std::vector
<const char*>* version_map
);
2095 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2098 Symbol_table::define_with_copy_reloc
<32>(const Target
* target
,
2099 Sized_symbol
<32>* sym
,
2100 Output_data
* posd
, uint64_t value
);
2103 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2106 Symbol_table::define_with_copy_reloc
<64>(const Target
* target
,
2107 Sized_symbol
<64>* sym
,
2108 Output_data
* posd
, uint64_t value
);
2111 #ifdef HAVE_TARGET_32_LITTLE
2114 Warnings::issue_warning
<32, false>(const Symbol
* sym
,
2115 const Relocate_info
<32, false>* relinfo
,
2116 size_t relnum
, off_t reloffset
) const;
2119 #ifdef HAVE_TARGET_32_BIG
2122 Warnings::issue_warning
<32, true>(const Symbol
* sym
,
2123 const Relocate_info
<32, true>* relinfo
,
2124 size_t relnum
, off_t reloffset
) const;
2127 #ifdef HAVE_TARGET_64_LITTLE
2130 Warnings::issue_warning
<64, false>(const Symbol
* sym
,
2131 const Relocate_info
<64, false>* relinfo
,
2132 size_t relnum
, off_t reloffset
) const;
2135 #ifdef HAVE_TARGET_64_BIG
2138 Warnings::issue_warning
<64, true>(const Symbol
* sym
,
2139 const Relocate_info
<64, true>* relinfo
,
2140 size_t relnum
, off_t reloffset
) const;
2143 } // End namespace gold.