* tls.m4 (GCC_CHECK_TLS): Rename have_tls to gcc_cv_have_tls.
[binutils.git] / gold / symtab.cc
blob7e0af342a88c6388d671aec88b53f29f16e3b30c
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.
23 #include "gold.h"
25 #include <stdint.h>
26 #include <string>
27 #include <utility>
29 #include "object.h"
30 #include "dynobj.h"
31 #include "output.h"
32 #include "target.h"
33 #include "workqueue.h"
34 #include "symtab.h"
36 namespace gold
39 // Class Symbol.
41 // Initialize fields in Symbol. This initializes everything except u_
42 // and source_.
44 void
45 Symbol::init_fields(const char* name, const char* version,
46 elfcpp::STT type, elfcpp::STB binding,
47 elfcpp::STV visibility, unsigned char nonvis)
49 this->name_ = name;
50 this->version_ = version;
51 this->symtab_index_ = 0;
52 this->dynsym_index_ = 0;
53 this->got_offset_ = 0;
54 this->plt_offset_ = 0;
55 this->type_ = type;
56 this->binding_ = binding;
57 this->visibility_ = visibility;
58 this->nonvis_ = nonvis;
59 this->is_target_special_ = false;
60 this->is_def_ = false;
61 this->is_forwarder_ = false;
62 this->has_alias_ = false;
63 this->needs_dynsym_entry_ = false;
64 this->in_reg_ = false;
65 this->in_dyn_ = false;
66 this->has_got_offset_ = false;
67 this->has_plt_offset_ = false;
68 this->has_warning_ = false;
71 // Initialize the fields in the base class Symbol for SYM in OBJECT.
73 template<int size, bool big_endian>
74 void
75 Symbol::init_base(const char* name, const char* version, Object* object,
76 const elfcpp::Sym<size, big_endian>& sym)
78 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
79 sym.get_st_visibility(), sym.get_st_nonvis());
80 this->u_.from_object.object = object;
81 // FIXME: Handle SHN_XINDEX.
82 this->u_.from_object.shndx = sym.get_st_shndx();
83 this->source_ = FROM_OBJECT;
84 this->in_reg_ = !object->is_dynamic();
85 this->in_dyn_ = object->is_dynamic();
88 // Initialize the fields in the base class Symbol for a symbol defined
89 // in an Output_data.
91 void
92 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
93 elfcpp::STB binding, elfcpp::STV visibility,
94 unsigned char nonvis, bool offset_is_from_end)
96 this->init_fields(name, NULL, type, binding, visibility, nonvis);
97 this->u_.in_output_data.output_data = od;
98 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
99 this->source_ = IN_OUTPUT_DATA;
100 this->in_reg_ = true;
103 // Initialize the fields in the base class Symbol for a symbol defined
104 // in an Output_segment.
106 void
107 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
108 elfcpp::STB binding, elfcpp::STV visibility,
109 unsigned char nonvis, Segment_offset_base offset_base)
111 this->init_fields(name, NULL, type, binding, visibility, nonvis);
112 this->u_.in_output_segment.output_segment = os;
113 this->u_.in_output_segment.offset_base = offset_base;
114 this->source_ = IN_OUTPUT_SEGMENT;
115 this->in_reg_ = true;
118 // Initialize the fields in the base class Symbol for a symbol defined
119 // as a constant.
121 void
122 Symbol::init_base(const char* name, elfcpp::STT type,
123 elfcpp::STB binding, elfcpp::STV visibility,
124 unsigned char nonvis)
126 this->init_fields(name, NULL, type, binding, visibility, nonvis);
127 this->source_ = CONSTANT;
128 this->in_reg_ = true;
131 // Initialize the fields in Sized_symbol for SYM in OBJECT.
133 template<int size>
134 template<bool big_endian>
135 void
136 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
137 const elfcpp::Sym<size, big_endian>& sym)
139 this->init_base(name, version, object, sym);
140 this->value_ = sym.get_st_value();
141 this->symsize_ = sym.get_st_size();
144 // Initialize the fields in Sized_symbol for a symbol defined in an
145 // Output_data.
147 template<int size>
148 void
149 Sized_symbol<size>::init(const char* name, Output_data* od,
150 Value_type value, Size_type symsize,
151 elfcpp::STT type, elfcpp::STB binding,
152 elfcpp::STV visibility, unsigned char nonvis,
153 bool offset_is_from_end)
155 this->init_base(name, od, type, binding, visibility, nonvis,
156 offset_is_from_end);
157 this->value_ = value;
158 this->symsize_ = symsize;
161 // Initialize the fields in Sized_symbol for a symbol defined in an
162 // Output_segment.
164 template<int size>
165 void
166 Sized_symbol<size>::init(const char* name, Output_segment* os,
167 Value_type value, Size_type symsize,
168 elfcpp::STT type, elfcpp::STB binding,
169 elfcpp::STV visibility, unsigned char nonvis,
170 Segment_offset_base offset_base)
172 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
173 this->value_ = value;
174 this->symsize_ = symsize;
177 // Initialize the fields in Sized_symbol for a symbol defined as a
178 // constant.
180 template<int size>
181 void
182 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
183 elfcpp::STT type, elfcpp::STB binding,
184 elfcpp::STV visibility, unsigned char nonvis)
186 this->init_base(name, type, binding, visibility, nonvis);
187 this->value_ = value;
188 this->symsize_ = symsize;
191 // Return true if the final value of this symbol is known at link
192 // time.
194 bool
195 Symbol::final_value_is_known() const
197 // If we are not generating an executable, then no final values are
198 // known, since they will change at runtime.
199 if (!parameters->output_is_executable())
200 return false;
202 // If the symbol is not from an object file, then it is defined, and
203 // known.
204 if (this->source_ != FROM_OBJECT)
205 return true;
207 // If the symbol is from a dynamic object, then the final value is
208 // not known.
209 if (this->object()->is_dynamic())
210 return false;
212 // If the symbol is not undefined (it is defined or common), then
213 // the final value is known.
214 if (!this->is_undefined())
215 return true;
217 // If the symbol is undefined, then whether the final value is known
218 // depends on whether we are doing a static link. If we are doing a
219 // dynamic link, then the final value could be filled in at runtime.
220 // This could reasonably be the case for a weak undefined symbol.
221 return parameters->doing_static_link();
224 // Class Symbol_table.
226 Symbol_table::Symbol_table()
227 : saw_undefined_(0), offset_(0), table_(), namepool_(),
228 forwarders_(), commons_(), warnings_()
232 Symbol_table::~Symbol_table()
236 // The hash function. The key is always canonicalized, so we use a
237 // simple combination of the pointers.
239 size_t
240 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
242 return key.first ^ key.second;
245 // The symbol table key equality function. This is only called with
246 // canonicalized name and version strings, so we can use pointer
247 // comparison.
249 bool
250 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
251 const Symbol_table_key& k2) const
253 return k1.first == k2.first && k1.second == k2.second;
256 // Make TO a symbol which forwards to FROM.
258 void
259 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
261 gold_assert(from != to);
262 gold_assert(!from->is_forwarder() && !to->is_forwarder());
263 this->forwarders_[from] = to;
264 from->set_forwarder();
267 // Resolve the forwards from FROM, returning the real symbol.
269 Symbol*
270 Symbol_table::resolve_forwards(const Symbol* from) const
272 gold_assert(from->is_forwarder());
273 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
274 this->forwarders_.find(from);
275 gold_assert(p != this->forwarders_.end());
276 return p->second;
279 // Look up a symbol by name.
281 Symbol*
282 Symbol_table::lookup(const char* name, const char* version) const
284 Stringpool::Key name_key;
285 name = this->namepool_.find(name, &name_key);
286 if (name == NULL)
287 return NULL;
289 Stringpool::Key version_key = 0;
290 if (version != NULL)
292 version = this->namepool_.find(version, &version_key);
293 if (version == NULL)
294 return NULL;
297 Symbol_table_key key(name_key, version_key);
298 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
299 if (p == this->table_.end())
300 return NULL;
301 return p->second;
304 // Resolve a Symbol with another Symbol. This is only used in the
305 // unusual case where there are references to both an unversioned
306 // symbol and a symbol with a version, and we then discover that that
307 // version is the default version. Because this is unusual, we do
308 // this the slow way, by converting back to an ELF symbol.
310 template<int size, bool big_endian>
311 void
312 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
313 const char* version ACCEPT_SIZE_ENDIAN)
315 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
316 elfcpp::Sym_write<size, big_endian> esym(buf);
317 // We don't bother to set the st_name field.
318 esym.put_st_value(from->value());
319 esym.put_st_size(from->symsize());
320 esym.put_st_info(from->binding(), from->type());
321 esym.put_st_other(from->visibility(), from->nonvis());
322 esym.put_st_shndx(from->shndx());
323 this->resolve(to, esym.sym(), from->object(), version);
324 if (from->in_reg())
325 to->set_in_reg();
326 if (from->in_dyn())
327 to->set_in_dyn();
330 // Add one symbol from OBJECT to the symbol table. NAME is symbol
331 // name and VERSION is the version; both are canonicalized. DEF is
332 // whether this is the default version.
334 // If DEF is true, then this is the definition of a default version of
335 // a symbol. That means that any lookup of NAME/NULL and any lookup
336 // of NAME/VERSION should always return the same symbol. This is
337 // obvious for references, but in particular we want to do this for
338 // definitions: overriding NAME/NULL should also override
339 // NAME/VERSION. If we don't do that, it would be very hard to
340 // override functions in a shared library which uses versioning.
342 // We implement this by simply making both entries in the hash table
343 // point to the same Symbol structure. That is easy enough if this is
344 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
345 // that we have seen both already, in which case they will both have
346 // independent entries in the symbol table. We can't simply change
347 // the symbol table entry, because we have pointers to the entries
348 // attached to the object files. So we mark the entry attached to the
349 // object file as a forwarder, and record it in the forwarders_ map.
350 // Note that entries in the hash table will never be marked as
351 // forwarders.
353 template<int size, bool big_endian>
354 Sized_symbol<size>*
355 Symbol_table::add_from_object(Object* object,
356 const char *name,
357 Stringpool::Key name_key,
358 const char *version,
359 Stringpool::Key version_key,
360 bool def,
361 const elfcpp::Sym<size, big_endian>& sym)
363 Symbol* const snull = NULL;
364 std::pair<typename Symbol_table_type::iterator, bool> ins =
365 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
366 snull));
368 std::pair<typename Symbol_table_type::iterator, bool> insdef =
369 std::make_pair(this->table_.end(), false);
370 if (def)
372 const Stringpool::Key vnull_key = 0;
373 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
374 vnull_key),
375 snull));
378 // ins.first: an iterator, which is a pointer to a pair.
379 // ins.first->first: the key (a pair of name and version).
380 // ins.first->second: the value (Symbol*).
381 // ins.second: true if new entry was inserted, false if not.
383 Sized_symbol<size>* ret;
384 bool was_undefined;
385 bool was_common;
386 if (!ins.second)
388 // We already have an entry for NAME/VERSION.
389 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
390 SELECT_SIZE(size));
391 gold_assert(ret != NULL);
393 was_undefined = ret->is_undefined();
394 was_common = ret->is_common();
396 this->resolve(ret, sym, object, version);
398 if (def)
400 if (insdef.second)
402 // This is the first time we have seen NAME/NULL. Make
403 // NAME/NULL point to NAME/VERSION.
404 insdef.first->second = ret;
406 else if (insdef.first->second != ret)
408 // This is the unfortunate case where we already have
409 // entries for both NAME/VERSION and NAME/NULL.
410 const Sized_symbol<size>* sym2;
411 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
412 insdef.first->second
413 SELECT_SIZE(size));
414 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
415 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
416 this->make_forwarder(insdef.first->second, ret);
417 insdef.first->second = ret;
421 else
423 // This is the first time we have seen NAME/VERSION.
424 gold_assert(ins.first->second == NULL);
426 was_undefined = false;
427 was_common = false;
429 if (def && !insdef.second)
431 // We already have an entry for NAME/NULL. If we override
432 // it, then change it to NAME/VERSION.
433 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
434 insdef.first->second
435 SELECT_SIZE(size));
436 this->resolve(ret, sym, object, version);
437 ins.first->second = ret;
439 else
441 Sized_target<size, big_endian>* target =
442 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
443 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
444 if (!target->has_make_symbol())
445 ret = new Sized_symbol<size>();
446 else
448 ret = target->make_symbol();
449 if (ret == NULL)
451 // This means that we don't want a symbol table
452 // entry after all.
453 if (!def)
454 this->table_.erase(ins.first);
455 else
457 this->table_.erase(insdef.first);
458 // Inserting insdef invalidated ins.
459 this->table_.erase(std::make_pair(name_key,
460 version_key));
462 return NULL;
466 ret->init(name, version, object, sym);
468 ins.first->second = ret;
469 if (def)
471 // This is the first time we have seen NAME/NULL. Point
472 // it at the new entry for NAME/VERSION.
473 gold_assert(insdef.second);
474 insdef.first->second = ret;
479 // Record every time we see a new undefined symbol, to speed up
480 // archive groups.
481 if (!was_undefined && ret->is_undefined())
482 ++this->saw_undefined_;
484 // Keep track of common symbols, to speed up common symbol
485 // allocation.
486 if (!was_common && ret->is_common())
487 this->commons_.push_back(ret);
489 return ret;
492 // Add all the symbols in a relocatable object to the hash table.
494 template<int size, bool big_endian>
495 void
496 Symbol_table::add_from_relobj(
497 Sized_relobj<size, big_endian>* relobj,
498 const unsigned char* syms,
499 size_t count,
500 const char* sym_names,
501 size_t sym_name_size,
502 Symbol** sympointers)
504 gold_assert(size == relobj->target()->get_size());
505 gold_assert(size == parameters->get_size());
507 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
509 const unsigned char* p = syms;
510 for (size_t i = 0; i < count; ++i, p += sym_size)
512 elfcpp::Sym<size, big_endian> sym(p);
513 elfcpp::Sym<size, big_endian>* psym = &sym;
515 unsigned int st_name = psym->get_st_name();
516 if (st_name >= sym_name_size)
518 relobj->error(_("bad global symbol name offset %u at %zu"),
519 st_name, i);
520 continue;
523 const char* name = sym_names + st_name;
525 // A symbol defined in a section which we are not including must
526 // be treated as an undefined symbol.
527 unsigned char symbuf[sym_size];
528 elfcpp::Sym<size, big_endian> sym2(symbuf);
529 unsigned int st_shndx = psym->get_st_shndx();
530 if (st_shndx != elfcpp::SHN_UNDEF
531 && st_shndx < elfcpp::SHN_LORESERVE
532 && !relobj->is_section_included(st_shndx))
534 memcpy(symbuf, p, sym_size);
535 elfcpp::Sym_write<size, big_endian> sw(symbuf);
536 sw.put_st_shndx(elfcpp::SHN_UNDEF);
537 psym = &sym2;
540 // In an object file, an '@' in the name separates the symbol
541 // name from the version name. If there are two '@' characters,
542 // this is the default version.
543 const char* ver = strchr(name, '@');
545 Sized_symbol<size>* res;
546 if (ver == NULL)
548 Stringpool::Key name_key;
549 name = this->namepool_.add(name, true, &name_key);
550 res = this->add_from_object(relobj, name, name_key, NULL, 0,
551 false, *psym);
553 else
555 Stringpool::Key name_key;
556 name = this->namepool_.add_prefix(name, ver - name, &name_key);
558 bool def = false;
559 ++ver;
560 if (*ver == '@')
562 def = true;
563 ++ver;
566 Stringpool::Key ver_key;
567 ver = this->namepool_.add(ver, true, &ver_key);
569 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
570 def, *psym);
573 *sympointers++ = res;
577 // Add all the symbols in a dynamic object to the hash table.
579 template<int size, bool big_endian>
580 void
581 Symbol_table::add_from_dynobj(
582 Sized_dynobj<size, big_endian>* dynobj,
583 const unsigned char* syms,
584 size_t count,
585 const char* sym_names,
586 size_t sym_name_size,
587 const unsigned char* versym,
588 size_t versym_size,
589 const std::vector<const char*>* version_map)
591 gold_assert(size == dynobj->target()->get_size());
592 gold_assert(size == parameters->get_size());
594 if (versym != NULL && versym_size / 2 < count)
596 dynobj->error(_("too few symbol versions"));
597 return;
600 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
602 // We keep a list of all STT_OBJECT symbols, so that we can resolve
603 // weak aliases. This is necessary because if the dynamic object
604 // provides the same variable under two names, one of which is a
605 // weak definition, and the regular object refers to the weak
606 // definition, we have to put both the weak definition and the
607 // strong definition into the dynamic symbol table. Given a weak
608 // definition, the only way that we can find the corresponding
609 // strong definition, if any, is to search the symbol table.
610 std::vector<Sized_symbol<size>*> object_symbols;
612 const unsigned char* p = syms;
613 const unsigned char* vs = versym;
614 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
616 elfcpp::Sym<size, big_endian> sym(p);
618 // Ignore symbols with local binding.
619 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
620 continue;
622 unsigned int st_name = sym.get_st_name();
623 if (st_name >= sym_name_size)
625 dynobj->error(_("bad symbol name offset %u at %zu"),
626 st_name, i);
627 continue;
630 const char* name = sym_names + st_name;
632 Sized_symbol<size>* res;
634 if (versym == NULL)
636 Stringpool::Key name_key;
637 name = this->namepool_.add(name, true, &name_key);
638 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
639 false, sym);
641 else
643 // Read the version information.
645 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
647 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
648 v &= elfcpp::VERSYM_VERSION;
650 // The Sun documentation says that V can be VER_NDX_LOCAL,
651 // or VER_NDX_GLOBAL, or a version index. The meaning of
652 // VER_NDX_LOCAL is defined as "Symbol has local scope."
653 // The old GNU linker will happily generate VER_NDX_LOCAL
654 // for an undefined symbol. I don't know what the Sun
655 // linker will generate.
657 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
658 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
660 // This symbol should not be visible outside the object.
661 continue;
664 // At this point we are definitely going to add this symbol.
665 Stringpool::Key name_key;
666 name = this->namepool_.add(name, true, &name_key);
668 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
669 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
671 // This symbol does not have a version.
672 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
673 false, sym);
675 else
677 if (v >= version_map->size())
679 dynobj->error(_("versym for symbol %zu out of range: %u"),
680 i, v);
681 continue;
684 const char* version = (*version_map)[v];
685 if (version == NULL)
687 dynobj->error(_("versym for symbol %zu has no name: %u"),
688 i, v);
689 continue;
692 Stringpool::Key version_key;
693 version = this->namepool_.add(version, true, &version_key);
695 // If this is an absolute symbol, and the version name
696 // and symbol name are the same, then this is the
697 // version definition symbol. These symbols exist to
698 // support using -u to pull in particular versions. We
699 // do not want to record a version for them.
700 if (sym.get_st_shndx() == elfcpp::SHN_ABS
701 && name_key == version_key)
702 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
703 false, sym);
704 else
706 const bool def = (!hidden
707 && (sym.get_st_shndx()
708 != elfcpp::SHN_UNDEF));
709 res = this->add_from_object(dynobj, name, name_key, version,
710 version_key, def, sym);
715 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
716 && sym.get_st_type() == elfcpp::STT_OBJECT)
717 object_symbols.push_back(res);
720 this->record_weak_aliases(&object_symbols);
723 // This is used to sort weak aliases. We sort them first by section
724 // index, then by offset, then by weak ahead of strong.
726 template<int size>
727 class Weak_alias_sorter
729 public:
730 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
733 template<int size>
734 bool
735 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
736 const Sized_symbol<size>* s2) const
738 if (s1->shndx() != s2->shndx())
739 return s1->shndx() < s2->shndx();
740 if (s1->value() != s2->value())
741 return s1->value() < s2->value();
742 if (s1->binding() != s2->binding())
744 if (s1->binding() == elfcpp::STB_WEAK)
745 return true;
746 if (s2->binding() == elfcpp::STB_WEAK)
747 return false;
749 return std::string(s1->name()) < std::string(s2->name());
752 // SYMBOLS is a list of object symbols from a dynamic object. Look
753 // for any weak aliases, and record them so that if we add the weak
754 // alias to the dynamic symbol table, we also add the corresponding
755 // strong symbol.
757 template<int size>
758 void
759 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
761 // Sort the vector by section index, then by offset, then by weak
762 // ahead of strong.
763 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
765 // Walk through the vector. For each weak definition, record
766 // aliases.
767 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
768 symbols->begin();
769 p != symbols->end();
770 ++p)
772 if ((*p)->binding() != elfcpp::STB_WEAK)
773 continue;
775 // Build a circular list of weak aliases. Each symbol points to
776 // the next one in the circular list.
778 Sized_symbol<size>* from_sym = *p;
779 typename std::vector<Sized_symbol<size>*>::const_iterator q;
780 for (q = p + 1; q != symbols->end(); ++q)
782 if ((*q)->shndx() != from_sym->shndx()
783 || (*q)->value() != from_sym->value())
784 break;
786 this->weak_aliases_[from_sym] = *q;
787 from_sym->set_has_alias();
788 from_sym = *q;
791 if (from_sym != *p)
793 this->weak_aliases_[from_sym] = *p;
794 from_sym->set_has_alias();
797 p = q - 1;
801 // Create and return a specially defined symbol. If ONLY_IF_REF is
802 // true, then only create the symbol if there is a reference to it.
803 // If this does not return NULL, it sets *POLDSYM to the existing
804 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
806 template<int size, bool big_endian>
807 Sized_symbol<size>*
808 Symbol_table::define_special_symbol(const Target* target, const char** pname,
809 const char** pversion, bool only_if_ref,
810 Sized_symbol<size>** poldsym
811 ACCEPT_SIZE_ENDIAN)
813 Symbol* oldsym;
814 Sized_symbol<size>* sym;
815 bool add_to_table = false;
816 typename Symbol_table_type::iterator add_loc = this->table_.end();
818 if (only_if_ref)
820 oldsym = this->lookup(*pname, *pversion);
821 if (oldsym == NULL || !oldsym->is_undefined())
822 return NULL;
824 *pname = oldsym->name();
825 *pversion = oldsym->version();
827 else
829 // Canonicalize NAME and VERSION.
830 Stringpool::Key name_key;
831 *pname = this->namepool_.add(*pname, true, &name_key);
833 Stringpool::Key version_key = 0;
834 if (*pversion != NULL)
835 *pversion = this->namepool_.add(*pversion, true, &version_key);
837 Symbol* const snull = NULL;
838 std::pair<typename Symbol_table_type::iterator, bool> ins =
839 this->table_.insert(std::make_pair(std::make_pair(name_key,
840 version_key),
841 snull));
843 if (!ins.second)
845 // We already have a symbol table entry for NAME/VERSION.
846 oldsym = ins.first->second;
847 gold_assert(oldsym != NULL);
849 else
851 // We haven't seen this symbol before.
852 gold_assert(ins.first->second == NULL);
853 add_to_table = true;
854 add_loc = ins.first;
855 oldsym = NULL;
859 if (!target->has_make_symbol())
860 sym = new Sized_symbol<size>();
861 else
863 gold_assert(target->get_size() == size);
864 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
865 typedef Sized_target<size, big_endian> My_target;
866 const My_target* sized_target =
867 static_cast<const My_target*>(target);
868 sym = sized_target->make_symbol();
869 if (sym == NULL)
870 return NULL;
873 if (add_to_table)
874 add_loc->second = sym;
875 else
876 gold_assert(oldsym != NULL);
878 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
879 SELECT_SIZE(size));
881 return sym;
884 // Define a symbol based on an Output_data.
886 Symbol*
887 Symbol_table::define_in_output_data(const Target* target, const char* name,
888 const char* version, Output_data* od,
889 uint64_t value, uint64_t symsize,
890 elfcpp::STT type, elfcpp::STB binding,
891 elfcpp::STV visibility,
892 unsigned char nonvis,
893 bool offset_is_from_end,
894 bool only_if_ref)
896 if (parameters->get_size() == 32)
898 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
899 return this->do_define_in_output_data<32>(target, name, version, od,
900 value, symsize, type, binding,
901 visibility, nonvis,
902 offset_is_from_end,
903 only_if_ref);
904 #else
905 gold_unreachable();
906 #endif
908 else if (parameters->get_size() == 64)
910 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
911 return this->do_define_in_output_data<64>(target, name, version, od,
912 value, symsize, type, binding,
913 visibility, nonvis,
914 offset_is_from_end,
915 only_if_ref);
916 #else
917 gold_unreachable();
918 #endif
920 else
921 gold_unreachable();
924 // Define a symbol in an Output_data, sized version.
926 template<int size>
927 Sized_symbol<size>*
928 Symbol_table::do_define_in_output_data(
929 const Target* target,
930 const char* name,
931 const char* version,
932 Output_data* od,
933 typename elfcpp::Elf_types<size>::Elf_Addr value,
934 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
935 elfcpp::STT type,
936 elfcpp::STB binding,
937 elfcpp::STV visibility,
938 unsigned char nonvis,
939 bool offset_is_from_end,
940 bool only_if_ref)
942 Sized_symbol<size>* sym;
943 Sized_symbol<size>* oldsym;
945 if (parameters->is_big_endian())
947 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
948 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
949 target, &name, &version, only_if_ref, &oldsym
950 SELECT_SIZE_ENDIAN(size, true));
951 #else
952 gold_unreachable();
953 #endif
955 else
957 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
958 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
959 target, &name, &version, only_if_ref, &oldsym
960 SELECT_SIZE_ENDIAN(size, false));
961 #else
962 gold_unreachable();
963 #endif
966 if (sym == NULL)
967 return NULL;
969 gold_assert(version == NULL || oldsym != NULL);
970 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
971 offset_is_from_end);
973 if (oldsym != NULL
974 && Symbol_table::should_override_with_special(oldsym))
975 this->override_with_special(oldsym, sym);
977 return sym;
980 // Define a symbol based on an Output_segment.
982 Symbol*
983 Symbol_table::define_in_output_segment(const Target* target, const char* name,
984 const char* version, Output_segment* os,
985 uint64_t value, uint64_t symsize,
986 elfcpp::STT type, elfcpp::STB binding,
987 elfcpp::STV visibility,
988 unsigned char nonvis,
989 Symbol::Segment_offset_base offset_base,
990 bool only_if_ref)
992 if (parameters->get_size() == 32)
994 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
995 return this->do_define_in_output_segment<32>(target, name, version, os,
996 value, symsize, type,
997 binding, visibility, nonvis,
998 offset_base, only_if_ref);
999 #else
1000 gold_unreachable();
1001 #endif
1003 else if (parameters->get_size() == 64)
1005 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1006 return this->do_define_in_output_segment<64>(target, name, version, os,
1007 value, symsize, type,
1008 binding, visibility, nonvis,
1009 offset_base, only_if_ref);
1010 #else
1011 gold_unreachable();
1012 #endif
1014 else
1015 gold_unreachable();
1018 // Define a symbol in an Output_segment, sized version.
1020 template<int size>
1021 Sized_symbol<size>*
1022 Symbol_table::do_define_in_output_segment(
1023 const Target* target,
1024 const char* name,
1025 const char* version,
1026 Output_segment* os,
1027 typename elfcpp::Elf_types<size>::Elf_Addr value,
1028 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1029 elfcpp::STT type,
1030 elfcpp::STB binding,
1031 elfcpp::STV visibility,
1032 unsigned char nonvis,
1033 Symbol::Segment_offset_base offset_base,
1034 bool only_if_ref)
1036 Sized_symbol<size>* sym;
1037 Sized_symbol<size>* oldsym;
1039 if (parameters->is_big_endian())
1041 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1042 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1043 target, &name, &version, only_if_ref, &oldsym
1044 SELECT_SIZE_ENDIAN(size, true));
1045 #else
1046 gold_unreachable();
1047 #endif
1049 else
1051 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1052 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1053 target, &name, &version, only_if_ref, &oldsym
1054 SELECT_SIZE_ENDIAN(size, false));
1055 #else
1056 gold_unreachable();
1057 #endif
1060 if (sym == NULL)
1061 return NULL;
1063 gold_assert(version == NULL || oldsym != NULL);
1064 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1065 offset_base);
1067 if (oldsym != NULL
1068 && Symbol_table::should_override_with_special(oldsym))
1069 this->override_with_special(oldsym, sym);
1071 return sym;
1074 // Define a special symbol with a constant value. It is a multiple
1075 // definition error if this symbol is already defined.
1077 Symbol*
1078 Symbol_table::define_as_constant(const Target* target, const char* name,
1079 const char* version, uint64_t value,
1080 uint64_t symsize, elfcpp::STT type,
1081 elfcpp::STB binding, elfcpp::STV visibility,
1082 unsigned char nonvis, bool only_if_ref)
1084 if (parameters->get_size() == 32)
1086 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1087 return this->do_define_as_constant<32>(target, name, version, value,
1088 symsize, type, binding,
1089 visibility, nonvis, only_if_ref);
1090 #else
1091 gold_unreachable();
1092 #endif
1094 else if (parameters->get_size() == 64)
1096 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1097 return this->do_define_as_constant<64>(target, name, version, value,
1098 symsize, type, binding,
1099 visibility, nonvis, only_if_ref);
1100 #else
1101 gold_unreachable();
1102 #endif
1104 else
1105 gold_unreachable();
1108 // Define a symbol as a constant, sized version.
1110 template<int size>
1111 Sized_symbol<size>*
1112 Symbol_table::do_define_as_constant(
1113 const Target* target,
1114 const char* name,
1115 const char* version,
1116 typename elfcpp::Elf_types<size>::Elf_Addr value,
1117 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1118 elfcpp::STT type,
1119 elfcpp::STB binding,
1120 elfcpp::STV visibility,
1121 unsigned char nonvis,
1122 bool only_if_ref)
1124 Sized_symbol<size>* sym;
1125 Sized_symbol<size>* oldsym;
1127 if (parameters->is_big_endian())
1129 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1130 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1131 target, &name, &version, only_if_ref, &oldsym
1132 SELECT_SIZE_ENDIAN(size, true));
1133 #else
1134 gold_unreachable();
1135 #endif
1137 else
1139 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1140 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1141 target, &name, &version, only_if_ref, &oldsym
1142 SELECT_SIZE_ENDIAN(size, false));
1143 #else
1144 gold_unreachable();
1145 #endif
1148 if (sym == NULL)
1149 return NULL;
1151 gold_assert(version == NULL || oldsym != NULL);
1152 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1154 if (oldsym != NULL
1155 && Symbol_table::should_override_with_special(oldsym))
1156 this->override_with_special(oldsym, sym);
1158 return sym;
1161 // Define a set of symbols in output sections.
1163 void
1164 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1165 int count, const Define_symbol_in_section* p)
1167 for (int i = 0; i < count; ++i, ++p)
1169 Output_section* os = layout->find_output_section(p->output_section);
1170 if (os != NULL)
1171 this->define_in_output_data(target, p->name, NULL, os, p->value,
1172 p->size, p->type, p->binding,
1173 p->visibility, p->nonvis,
1174 p->offset_is_from_end, p->only_if_ref);
1175 else
1176 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1177 p->binding, p->visibility, p->nonvis,
1178 p->only_if_ref);
1182 // Define a set of symbols in output segments.
1184 void
1185 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1186 int count, const Define_symbol_in_segment* p)
1188 for (int i = 0; i < count; ++i, ++p)
1190 Output_segment* os = layout->find_output_segment(p->segment_type,
1191 p->segment_flags_set,
1192 p->segment_flags_clear);
1193 if (os != NULL)
1194 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1195 p->size, p->type, p->binding,
1196 p->visibility, p->nonvis,
1197 p->offset_base, p->only_if_ref);
1198 else
1199 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1200 p->binding, p->visibility, p->nonvis,
1201 p->only_if_ref);
1205 // Set the dynamic symbol indexes. INDEX is the index of the first
1206 // global dynamic symbol. Pointers to the symbols are stored into the
1207 // vector SYMS. The names are added to DYNPOOL. This returns an
1208 // updated dynamic symbol index.
1210 unsigned int
1211 Symbol_table::set_dynsym_indexes(const General_options* options,
1212 const Target* target,
1213 unsigned int index,
1214 std::vector<Symbol*>* syms,
1215 Stringpool* dynpool,
1216 Versions* versions)
1218 for (Symbol_table_type::iterator p = this->table_.begin();
1219 p != this->table_.end();
1220 ++p)
1222 Symbol* sym = p->second;
1224 // Note that SYM may already have a dynamic symbol index, since
1225 // some symbols appear more than once in the symbol table, with
1226 // and without a version.
1228 if (!sym->needs_dynsym_entry()
1229 && (!options->export_dynamic()
1230 || !sym->in_reg()
1231 || !sym->is_externally_visible()))
1232 sym->set_dynsym_index(-1U);
1233 else if (!sym->has_dynsym_index())
1235 sym->set_dynsym_index(index);
1236 ++index;
1237 syms->push_back(sym);
1238 dynpool->add(sym->name(), false, NULL);
1240 // Record any version information.
1241 if (sym->version() != NULL)
1242 versions->record_version(options, dynpool, sym);
1246 // Finish up the versions. In some cases this may add new dynamic
1247 // symbols.
1248 index = versions->finalize(target, this, index, syms);
1250 return index;
1253 // Set the final values for all the symbols. The index of the first
1254 // global symbol in the output file is INDEX. Record the file offset
1255 // OFF. Add their names to POOL. Return the new file offset.
1257 off_t
1258 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1259 size_t dyn_global_index, size_t dyncount,
1260 Stringpool* pool)
1262 off_t ret;
1264 gold_assert(index != 0);
1265 this->first_global_index_ = index;
1267 this->dynamic_offset_ = dynoff;
1268 this->first_dynamic_global_index_ = dyn_global_index;
1269 this->dynamic_count_ = dyncount;
1271 if (parameters->get_size() == 32)
1273 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1274 ret = this->sized_finalize<32>(index, off, pool);
1275 #else
1276 gold_unreachable();
1277 #endif
1279 else if (parameters->get_size() == 64)
1281 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1282 ret = this->sized_finalize<64>(index, off, pool);
1283 #else
1284 gold_unreachable();
1285 #endif
1287 else
1288 gold_unreachable();
1290 // Now that we have the final symbol table, we can reliably note
1291 // which symbols should get warnings.
1292 this->warnings_.note_warnings(this);
1294 return ret;
1297 // Set the final value for all the symbols. This is called after
1298 // Layout::finalize, so all the output sections have their final
1299 // address.
1301 template<int size>
1302 off_t
1303 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1305 off = align_address(off, size >> 3);
1306 this->offset_ = off;
1308 size_t orig_index = index;
1310 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1311 for (Symbol_table_type::iterator p = this->table_.begin();
1312 p != this->table_.end();
1313 ++p)
1315 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1317 // FIXME: Here we need to decide which symbols should go into
1318 // the output file, based on --strip.
1320 // The default version of a symbol may appear twice in the
1321 // symbol table. We only need to finalize it once.
1322 if (sym->has_symtab_index())
1323 continue;
1325 if (!sym->in_reg())
1327 gold_assert(!sym->has_symtab_index());
1328 sym->set_symtab_index(-1U);
1329 gold_assert(sym->dynsym_index() == -1U);
1330 continue;
1333 typename Sized_symbol<size>::Value_type value;
1335 switch (sym->source())
1337 case Symbol::FROM_OBJECT:
1339 unsigned int shndx = sym->shndx();
1341 // FIXME: We need some target specific support here.
1342 if (shndx >= elfcpp::SHN_LORESERVE
1343 && shndx != elfcpp::SHN_ABS)
1345 gold_error(_("%s: unsupported symbol section 0x%x"),
1346 sym->name(), shndx);
1347 shndx = elfcpp::SHN_UNDEF;
1350 Object* symobj = sym->object();
1351 if (symobj->is_dynamic())
1353 value = 0;
1354 shndx = elfcpp::SHN_UNDEF;
1356 else if (shndx == elfcpp::SHN_UNDEF)
1357 value = 0;
1358 else if (shndx == elfcpp::SHN_ABS)
1359 value = sym->value();
1360 else
1362 Relobj* relobj = static_cast<Relobj*>(symobj);
1363 off_t secoff;
1364 Output_section* os = relobj->output_section(shndx, &secoff);
1366 if (os == NULL)
1368 sym->set_symtab_index(-1U);
1369 gold_assert(sym->dynsym_index() == -1U);
1370 continue;
1373 value = sym->value() + os->address() + secoff;
1376 break;
1378 case Symbol::IN_OUTPUT_DATA:
1380 Output_data* od = sym->output_data();
1381 value = sym->value() + od->address();
1382 if (sym->offset_is_from_end())
1383 value += od->data_size();
1385 break;
1387 case Symbol::IN_OUTPUT_SEGMENT:
1389 Output_segment* os = sym->output_segment();
1390 value = sym->value() + os->vaddr();
1391 switch (sym->offset_base())
1393 case Symbol::SEGMENT_START:
1394 break;
1395 case Symbol::SEGMENT_END:
1396 value += os->memsz();
1397 break;
1398 case Symbol::SEGMENT_BSS:
1399 value += os->filesz();
1400 break;
1401 default:
1402 gold_unreachable();
1405 break;
1407 case Symbol::CONSTANT:
1408 value = sym->value();
1409 break;
1411 default:
1412 gold_unreachable();
1415 sym->set_value(value);
1417 if (parameters->strip_all())
1418 sym->set_symtab_index(-1U);
1419 else
1421 sym->set_symtab_index(index);
1422 pool->add(sym->name(), false, NULL);
1423 ++index;
1424 off += sym_size;
1428 this->output_count_ = index - orig_index;
1430 return off;
1433 // Write out the global symbols.
1435 void
1436 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1437 const Stringpool* dynpool, Output_file* of) const
1439 if (parameters->get_size() == 32)
1441 if (parameters->is_big_endian())
1443 #ifdef HAVE_TARGET_32_BIG
1444 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1445 #else
1446 gold_unreachable();
1447 #endif
1449 else
1451 #ifdef HAVE_TARGET_32_LITTLE
1452 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1453 #else
1454 gold_unreachable();
1455 #endif
1458 else if (parameters->get_size() == 64)
1460 if (parameters->is_big_endian())
1462 #ifdef HAVE_TARGET_64_BIG
1463 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1464 #else
1465 gold_unreachable();
1466 #endif
1468 else
1470 #ifdef HAVE_TARGET_64_LITTLE
1471 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1472 #else
1473 gold_unreachable();
1474 #endif
1477 else
1478 gold_unreachable();
1481 // Write out the global symbols.
1483 template<int size, bool big_endian>
1484 void
1485 Symbol_table::sized_write_globals(const Target* target,
1486 const Stringpool* sympool,
1487 const Stringpool* dynpool,
1488 Output_file* of) const
1490 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1491 unsigned int index = this->first_global_index_;
1492 const off_t oview_size = this->output_count_ * sym_size;
1493 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1495 unsigned int dynamic_count = this->dynamic_count_;
1496 off_t dynamic_size = dynamic_count * sym_size;
1497 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1498 unsigned char* dynamic_view;
1499 if (this->dynamic_offset_ == 0)
1500 dynamic_view = NULL;
1501 else
1502 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1504 unsigned char* ps = psyms;
1505 for (Symbol_table_type::const_iterator p = this->table_.begin();
1506 p != this->table_.end();
1507 ++p)
1509 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1511 unsigned int sym_index = sym->symtab_index();
1512 unsigned int dynsym_index;
1513 if (dynamic_view == NULL)
1514 dynsym_index = -1U;
1515 else
1516 dynsym_index = sym->dynsym_index();
1518 if (sym_index == -1U && dynsym_index == -1U)
1520 // This symbol is not included in the output file.
1521 continue;
1524 if (sym_index == index)
1525 ++index;
1526 else if (sym_index != -1U)
1528 // We have already seen this symbol, because it has a
1529 // default version.
1530 gold_assert(sym_index < index);
1531 if (dynsym_index == -1U)
1532 continue;
1533 sym_index = -1U;
1536 unsigned int shndx;
1537 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1538 switch (sym->source())
1540 case Symbol::FROM_OBJECT:
1542 unsigned int in_shndx = sym->shndx();
1544 // FIXME: We need some target specific support here.
1545 if (in_shndx >= elfcpp::SHN_LORESERVE
1546 && in_shndx != elfcpp::SHN_ABS)
1548 gold_error(_("%s: unsupported symbol section 0x%x"),
1549 sym->name(), in_shndx);
1550 shndx = in_shndx;
1552 else
1554 Object* symobj = sym->object();
1555 if (symobj->is_dynamic())
1557 if (sym->needs_dynsym_value())
1558 value = target->dynsym_value(sym);
1559 shndx = elfcpp::SHN_UNDEF;
1561 else if (in_shndx == elfcpp::SHN_UNDEF
1562 || in_shndx == elfcpp::SHN_ABS)
1563 shndx = in_shndx;
1564 else
1566 Relobj* relobj = static_cast<Relobj*>(symobj);
1567 off_t secoff;
1568 Output_section* os = relobj->output_section(in_shndx,
1569 &secoff);
1570 gold_assert(os != NULL);
1571 shndx = os->out_shndx();
1575 break;
1577 case Symbol::IN_OUTPUT_DATA:
1578 shndx = sym->output_data()->out_shndx();
1579 break;
1581 case Symbol::IN_OUTPUT_SEGMENT:
1582 shndx = elfcpp::SHN_ABS;
1583 break;
1585 case Symbol::CONSTANT:
1586 shndx = elfcpp::SHN_ABS;
1587 break;
1589 default:
1590 gold_unreachable();
1593 if (sym_index != -1U)
1595 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1596 sym, sym->value(), shndx, sympool, ps
1597 SELECT_SIZE_ENDIAN(size, big_endian));
1598 ps += sym_size;
1601 if (dynsym_index != -1U)
1603 dynsym_index -= first_dynamic_global_index;
1604 gold_assert(dynsym_index < dynamic_count);
1605 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1606 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1607 sym, value, shndx, dynpool, pd
1608 SELECT_SIZE_ENDIAN(size, big_endian));
1612 gold_assert(ps - psyms == oview_size);
1614 of->write_output_view(this->offset_, oview_size, psyms);
1615 if (dynamic_view != NULL)
1616 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1619 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1620 // strtab holding the name.
1622 template<int size, bool big_endian>
1623 void
1624 Symbol_table::sized_write_symbol(
1625 Sized_symbol<size>* sym,
1626 typename elfcpp::Elf_types<size>::Elf_Addr value,
1627 unsigned int shndx,
1628 const Stringpool* pool,
1629 unsigned char* p
1630 ACCEPT_SIZE_ENDIAN) const
1632 elfcpp::Sym_write<size, big_endian> osym(p);
1633 osym.put_st_name(pool->get_offset(sym->name()));
1634 osym.put_st_value(value);
1635 osym.put_st_size(sym->symsize());
1636 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1637 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1638 osym.put_st_shndx(shndx);
1641 // Write out a section symbol. Return the update offset.
1643 void
1644 Symbol_table::write_section_symbol(const Output_section *os,
1645 Output_file* of,
1646 off_t offset) const
1648 if (parameters->get_size() == 32)
1650 if (parameters->is_big_endian())
1652 #ifdef HAVE_TARGET_32_BIG
1653 this->sized_write_section_symbol<32, true>(os, of, offset);
1654 #else
1655 gold_unreachable();
1656 #endif
1658 else
1660 #ifdef HAVE_TARGET_32_LITTLE
1661 this->sized_write_section_symbol<32, false>(os, of, offset);
1662 #else
1663 gold_unreachable();
1664 #endif
1667 else if (parameters->get_size() == 64)
1669 if (parameters->is_big_endian())
1671 #ifdef HAVE_TARGET_64_BIG
1672 this->sized_write_section_symbol<64, true>(os, of, offset);
1673 #else
1674 gold_unreachable();
1675 #endif
1677 else
1679 #ifdef HAVE_TARGET_64_LITTLE
1680 this->sized_write_section_symbol<64, false>(os, of, offset);
1681 #else
1682 gold_unreachable();
1683 #endif
1686 else
1687 gold_unreachable();
1690 // Write out a section symbol, specialized for size and endianness.
1692 template<int size, bool big_endian>
1693 void
1694 Symbol_table::sized_write_section_symbol(const Output_section* os,
1695 Output_file* of,
1696 off_t offset) const
1698 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1700 unsigned char* pov = of->get_output_view(offset, sym_size);
1702 elfcpp::Sym_write<size, big_endian> osym(pov);
1703 osym.put_st_name(0);
1704 osym.put_st_value(os->address());
1705 osym.put_st_size(0);
1706 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1707 elfcpp::STT_SECTION));
1708 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1709 osym.put_st_shndx(os->out_shndx());
1711 of->write_output_view(offset, sym_size, pov);
1714 // Warnings functions.
1716 // Add a new warning.
1718 void
1719 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1720 unsigned int shndx)
1722 name = symtab->canonicalize_name(name);
1723 this->warnings_[name].set(obj, shndx);
1726 // Look through the warnings and mark the symbols for which we should
1727 // warn. This is called during Layout::finalize when we know the
1728 // sources for all the symbols.
1730 void
1731 Warnings::note_warnings(Symbol_table* symtab)
1733 for (Warning_table::iterator p = this->warnings_.begin();
1734 p != this->warnings_.end();
1735 ++p)
1737 Symbol* sym = symtab->lookup(p->first, NULL);
1738 if (sym != NULL
1739 && sym->source() == Symbol::FROM_OBJECT
1740 && sym->object() == p->second.object)
1742 sym->set_has_warning();
1744 // Read the section contents to get the warning text. It
1745 // would be nicer if we only did this if we have to actually
1746 // issue a warning. Unfortunately, warnings are issued as
1747 // we relocate sections. That means that we can not lock
1748 // the object then, as we might try to issue the same
1749 // warning multiple times simultaneously.
1751 Task_locker_obj<Object> tl(*p->second.object);
1752 const unsigned char* c;
1753 off_t len;
1754 c = p->second.object->section_contents(p->second.shndx, &len,
1755 false);
1756 p->second.set_text(reinterpret_cast<const char*>(c), len);
1762 // Issue a warning. This is called when we see a relocation against a
1763 // symbol for which has a warning.
1765 template<int size, bool big_endian>
1766 void
1767 Warnings::issue_warning(const Symbol* sym,
1768 const Relocate_info<size, big_endian>* relinfo,
1769 size_t relnum, off_t reloffset) const
1771 gold_assert(sym->has_warning());
1772 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1773 gold_assert(p != this->warnings_.end());
1774 gold_warning_at_location(relinfo, relnum, reloffset,
1775 "%s", p->second.text.c_str());
1778 // Instantiate the templates we need. We could use the configure
1779 // script to restrict this to only the ones needed for implemented
1780 // targets.
1782 #ifdef HAVE_TARGET_32_LITTLE
1783 template
1784 void
1785 Symbol_table::add_from_relobj<32, false>(
1786 Sized_relobj<32, false>* relobj,
1787 const unsigned char* syms,
1788 size_t count,
1789 const char* sym_names,
1790 size_t sym_name_size,
1791 Symbol** sympointers);
1792 #endif
1794 #ifdef HAVE_TARGET_32_BIG
1795 template
1796 void
1797 Symbol_table::add_from_relobj<32, true>(
1798 Sized_relobj<32, true>* relobj,
1799 const unsigned char* syms,
1800 size_t count,
1801 const char* sym_names,
1802 size_t sym_name_size,
1803 Symbol** sympointers);
1804 #endif
1806 #ifdef HAVE_TARGET_64_LITTLE
1807 template
1808 void
1809 Symbol_table::add_from_relobj<64, false>(
1810 Sized_relobj<64, false>* relobj,
1811 const unsigned char* syms,
1812 size_t count,
1813 const char* sym_names,
1814 size_t sym_name_size,
1815 Symbol** sympointers);
1816 #endif
1818 #ifdef HAVE_TARGET_64_BIG
1819 template
1820 void
1821 Symbol_table::add_from_relobj<64, true>(
1822 Sized_relobj<64, true>* relobj,
1823 const unsigned char* syms,
1824 size_t count,
1825 const char* sym_names,
1826 size_t sym_name_size,
1827 Symbol** sympointers);
1828 #endif
1830 #ifdef HAVE_TARGET_32_LITTLE
1831 template
1832 void
1833 Symbol_table::add_from_dynobj<32, false>(
1834 Sized_dynobj<32, false>* dynobj,
1835 const unsigned char* syms,
1836 size_t count,
1837 const char* sym_names,
1838 size_t sym_name_size,
1839 const unsigned char* versym,
1840 size_t versym_size,
1841 const std::vector<const char*>* version_map);
1842 #endif
1844 #ifdef HAVE_TARGET_32_BIG
1845 template
1846 void
1847 Symbol_table::add_from_dynobj<32, true>(
1848 Sized_dynobj<32, true>* dynobj,
1849 const unsigned char* syms,
1850 size_t count,
1851 const char* sym_names,
1852 size_t sym_name_size,
1853 const unsigned char* versym,
1854 size_t versym_size,
1855 const std::vector<const char*>* version_map);
1856 #endif
1858 #ifdef HAVE_TARGET_64_LITTLE
1859 template
1860 void
1861 Symbol_table::add_from_dynobj<64, false>(
1862 Sized_dynobj<64, false>* dynobj,
1863 const unsigned char* syms,
1864 size_t count,
1865 const char* sym_names,
1866 size_t sym_name_size,
1867 const unsigned char* versym,
1868 size_t versym_size,
1869 const std::vector<const char*>* version_map);
1870 #endif
1872 #ifdef HAVE_TARGET_64_BIG
1873 template
1874 void
1875 Symbol_table::add_from_dynobj<64, true>(
1876 Sized_dynobj<64, true>* dynobj,
1877 const unsigned char* syms,
1878 size_t count,
1879 const char* sym_names,
1880 size_t sym_name_size,
1881 const unsigned char* versym,
1882 size_t versym_size,
1883 const std::vector<const char*>* version_map);
1884 #endif
1886 #ifdef HAVE_TARGET_32_LITTLE
1887 template
1888 void
1889 Warnings::issue_warning<32, false>(const Symbol* sym,
1890 const Relocate_info<32, false>* relinfo,
1891 size_t relnum, off_t reloffset) const;
1892 #endif
1894 #ifdef HAVE_TARGET_32_BIG
1895 template
1896 void
1897 Warnings::issue_warning<32, true>(const Symbol* sym,
1898 const Relocate_info<32, true>* relinfo,
1899 size_t relnum, off_t reloffset) const;
1900 #endif
1902 #ifdef HAVE_TARGET_64_LITTLE
1903 template
1904 void
1905 Warnings::issue_warning<64, false>(const Symbol* sym,
1906 const Relocate_info<64, false>* relinfo,
1907 size_t relnum, off_t reloffset) const;
1908 #endif
1910 #ifdef HAVE_TARGET_64_BIG
1911 template
1912 void
1913 Warnings::issue_warning<64, true>(const Symbol* sym,
1914 const Relocate_info<64, true>* relinfo,
1915 size_t relnum, off_t reloffset) const;
1916 #endif
1919 } // End namespace gold.