daily update
[binutils.git] / gold / symtab.cc
blob332c59f2f946b9bb82af7c0309ba6c64de24ab30
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->needs_dynsym_entry_ = false;
63 this->in_reg_ = false;
64 this->in_dyn_ = false;
65 this->has_got_offset_ = false;
66 this->has_plt_offset_ = false;
67 this->has_warning_ = false;
70 // Initialize the fields in the base class Symbol for SYM in OBJECT.
72 template<int size, bool big_endian>
73 void
74 Symbol::init_base(const char* name, const char* version, Object* object,
75 const elfcpp::Sym<size, big_endian>& sym)
77 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
78 sym.get_st_visibility(), sym.get_st_nonvis());
79 this->u_.from_object.object = object;
80 // FIXME: Handle SHN_XINDEX.
81 this->u_.from_object.shndx = sym.get_st_shndx();
82 this->source_ = FROM_OBJECT;
83 this->in_reg_ = !object->is_dynamic();
84 this->in_dyn_ = object->is_dynamic();
87 // Initialize the fields in the base class Symbol for a symbol defined
88 // in an Output_data.
90 void
91 Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
92 elfcpp::STB binding, elfcpp::STV visibility,
93 unsigned char nonvis, bool offset_is_from_end)
95 this->init_fields(name, NULL, type, binding, visibility, nonvis);
96 this->u_.in_output_data.output_data = od;
97 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
98 this->source_ = IN_OUTPUT_DATA;
99 this->in_reg_ = true;
102 // Initialize the fields in the base class Symbol for a symbol defined
103 // in an Output_segment.
105 void
106 Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
107 elfcpp::STB binding, elfcpp::STV visibility,
108 unsigned char nonvis, Segment_offset_base offset_base)
110 this->init_fields(name, NULL, type, binding, visibility, nonvis);
111 this->u_.in_output_segment.output_segment = os;
112 this->u_.in_output_segment.offset_base = offset_base;
113 this->source_ = IN_OUTPUT_SEGMENT;
114 this->in_reg_ = true;
117 // Initialize the fields in the base class Symbol for a symbol defined
118 // as a constant.
120 void
121 Symbol::init_base(const char* name, elfcpp::STT type,
122 elfcpp::STB binding, elfcpp::STV visibility,
123 unsigned char nonvis)
125 this->init_fields(name, NULL, type, binding, visibility, nonvis);
126 this->source_ = CONSTANT;
127 this->in_reg_ = true;
130 // Initialize the fields in Sized_symbol for SYM in OBJECT.
132 template<int size>
133 template<bool big_endian>
134 void
135 Sized_symbol<size>::init(const char* name, const char* version, Object* object,
136 const elfcpp::Sym<size, big_endian>& sym)
138 this->init_base(name, version, object, sym);
139 this->value_ = sym.get_st_value();
140 this->symsize_ = sym.get_st_size();
143 // Initialize the fields in Sized_symbol for a symbol defined in an
144 // Output_data.
146 template<int size>
147 void
148 Sized_symbol<size>::init(const char* name, Output_data* od,
149 Value_type value, Size_type symsize,
150 elfcpp::STT type, elfcpp::STB binding,
151 elfcpp::STV visibility, unsigned char nonvis,
152 bool offset_is_from_end)
154 this->init_base(name, od, type, binding, visibility, nonvis,
155 offset_is_from_end);
156 this->value_ = value;
157 this->symsize_ = symsize;
160 // Initialize the fields in Sized_symbol for a symbol defined in an
161 // Output_segment.
163 template<int size>
164 void
165 Sized_symbol<size>::init(const char* name, Output_segment* os,
166 Value_type value, Size_type symsize,
167 elfcpp::STT type, elfcpp::STB binding,
168 elfcpp::STV visibility, unsigned char nonvis,
169 Segment_offset_base offset_base)
171 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
172 this->value_ = value;
173 this->symsize_ = symsize;
176 // Initialize the fields in Sized_symbol for a symbol defined as a
177 // constant.
179 template<int size>
180 void
181 Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
182 elfcpp::STT type, elfcpp::STB binding,
183 elfcpp::STV visibility, unsigned char nonvis)
185 this->init_base(name, type, binding, visibility, nonvis);
186 this->value_ = value;
187 this->symsize_ = symsize;
190 // Class Symbol_table.
192 Symbol_table::Symbol_table()
193 : saw_undefined_(0), offset_(0), table_(), namepool_(),
194 forwarders_(), commons_(), warnings_()
198 Symbol_table::~Symbol_table()
202 // The hash function. The key is always canonicalized, so we use a
203 // simple combination of the pointers.
205 size_t
206 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
208 return key.first ^ key.second;
211 // The symbol table key equality function. This is only called with
212 // canonicalized name and version strings, so we can use pointer
213 // comparison.
215 bool
216 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
217 const Symbol_table_key& k2) const
219 return k1.first == k2.first && k1.second == k2.second;
222 // Make TO a symbol which forwards to FROM.
224 void
225 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
227 gold_assert(from != to);
228 gold_assert(!from->is_forwarder() && !to->is_forwarder());
229 this->forwarders_[from] = to;
230 from->set_forwarder();
233 // Resolve the forwards from FROM, returning the real symbol.
235 Symbol*
236 Symbol_table::resolve_forwards(const Symbol* from) const
238 gold_assert(from->is_forwarder());
239 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
240 this->forwarders_.find(from);
241 gold_assert(p != this->forwarders_.end());
242 return p->second;
245 // Look up a symbol by name.
247 Symbol*
248 Symbol_table::lookup(const char* name, const char* version) const
250 Stringpool::Key name_key;
251 name = this->namepool_.find(name, &name_key);
252 if (name == NULL)
253 return NULL;
255 Stringpool::Key version_key = 0;
256 if (version != NULL)
258 version = this->namepool_.find(version, &version_key);
259 if (version == NULL)
260 return NULL;
263 Symbol_table_key key(name_key, version_key);
264 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
265 if (p == this->table_.end())
266 return NULL;
267 return p->second;
270 // Resolve a Symbol with another Symbol. This is only used in the
271 // unusual case where there are references to both an unversioned
272 // symbol and a symbol with a version, and we then discover that that
273 // version is the default version. Because this is unusual, we do
274 // this the slow way, by converting back to an ELF symbol.
276 template<int size, bool big_endian>
277 void
278 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
279 const char* version ACCEPT_SIZE_ENDIAN)
281 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
282 elfcpp::Sym_write<size, big_endian> esym(buf);
283 // We don't bother to set the st_name field.
284 esym.put_st_value(from->value());
285 esym.put_st_size(from->symsize());
286 esym.put_st_info(from->binding(), from->type());
287 esym.put_st_other(from->visibility(), from->nonvis());
288 esym.put_st_shndx(from->shndx());
289 Symbol_table::resolve(to, esym.sym(), from->object(), version);
290 if (from->in_reg())
291 to->set_in_reg();
292 if (from->in_dyn())
293 to->set_in_dyn();
296 // Add one symbol from OBJECT to the symbol table. NAME is symbol
297 // name and VERSION is the version; both are canonicalized. DEF is
298 // whether this is the default version.
300 // If DEF is true, then this is the definition of a default version of
301 // a symbol. That means that any lookup of NAME/NULL and any lookup
302 // of NAME/VERSION should always return the same symbol. This is
303 // obvious for references, but in particular we want to do this for
304 // definitions: overriding NAME/NULL should also override
305 // NAME/VERSION. If we don't do that, it would be very hard to
306 // override functions in a shared library which uses versioning.
308 // We implement this by simply making both entries in the hash table
309 // point to the same Symbol structure. That is easy enough if this is
310 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
311 // that we have seen both already, in which case they will both have
312 // independent entries in the symbol table. We can't simply change
313 // the symbol table entry, because we have pointers to the entries
314 // attached to the object files. So we mark the entry attached to the
315 // object file as a forwarder, and record it in the forwarders_ map.
316 // Note that entries in the hash table will never be marked as
317 // forwarders.
319 template<int size, bool big_endian>
320 Symbol*
321 Symbol_table::add_from_object(Object* object,
322 const char *name,
323 Stringpool::Key name_key,
324 const char *version,
325 Stringpool::Key version_key,
326 bool def,
327 const elfcpp::Sym<size, big_endian>& sym)
329 Symbol* const snull = NULL;
330 std::pair<typename Symbol_table_type::iterator, bool> ins =
331 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
332 snull));
334 std::pair<typename Symbol_table_type::iterator, bool> insdef =
335 std::make_pair(this->table_.end(), false);
336 if (def)
338 const Stringpool::Key vnull_key = 0;
339 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
340 vnull_key),
341 snull));
344 // ins.first: an iterator, which is a pointer to a pair.
345 // ins.first->first: the key (a pair of name and version).
346 // ins.first->second: the value (Symbol*).
347 // ins.second: true if new entry was inserted, false if not.
349 Sized_symbol<size>* ret;
350 bool was_undefined;
351 bool was_common;
352 if (!ins.second)
354 // We already have an entry for NAME/VERSION.
355 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
356 SELECT_SIZE(size));
357 gold_assert(ret != NULL);
359 was_undefined = ret->is_undefined();
360 was_common = ret->is_common();
362 Symbol_table::resolve(ret, sym, object, version);
364 if (def)
366 if (insdef.second)
368 // This is the first time we have seen NAME/NULL. Make
369 // NAME/NULL point to NAME/VERSION.
370 insdef.first->second = ret;
372 else if (insdef.first->second != ret)
374 // This is the unfortunate case where we already have
375 // entries for both NAME/VERSION and NAME/NULL.
376 const Sized_symbol<size>* sym2;
377 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
378 insdef.first->second
379 SELECT_SIZE(size));
380 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
381 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
382 this->make_forwarder(insdef.first->second, ret);
383 insdef.first->second = ret;
387 else
389 // This is the first time we have seen NAME/VERSION.
390 gold_assert(ins.first->second == NULL);
392 was_undefined = false;
393 was_common = false;
395 if (def && !insdef.second)
397 // We already have an entry for NAME/NULL. If we override
398 // it, then change it to NAME/VERSION.
399 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
400 insdef.first->second
401 SELECT_SIZE(size));
402 Symbol_table::resolve(ret, sym, object, version);
403 ins.first->second = ret;
405 else
407 Sized_target<size, big_endian>* target =
408 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
409 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
410 if (!target->has_make_symbol())
411 ret = new Sized_symbol<size>();
412 else
414 ret = target->make_symbol();
415 if (ret == NULL)
417 // This means that we don't want a symbol table
418 // entry after all.
419 if (!def)
420 this->table_.erase(ins.first);
421 else
423 this->table_.erase(insdef.first);
424 // Inserting insdef invalidated ins.
425 this->table_.erase(std::make_pair(name_key,
426 version_key));
428 return NULL;
432 ret->init(name, version, object, sym);
434 ins.first->second = ret;
435 if (def)
437 // This is the first time we have seen NAME/NULL. Point
438 // it at the new entry for NAME/VERSION.
439 gold_assert(insdef.second);
440 insdef.first->second = ret;
445 // Record every time we see a new undefined symbol, to speed up
446 // archive groups.
447 if (!was_undefined && ret->is_undefined())
448 ++this->saw_undefined_;
450 // Keep track of common symbols, to speed up common symbol
451 // allocation.
452 if (!was_common && ret->is_common())
453 this->commons_.push_back(ret);
455 return ret;
458 // Add all the symbols in a relocatable object to the hash table.
460 template<int size, bool big_endian>
461 void
462 Symbol_table::add_from_relobj(
463 Sized_relobj<size, big_endian>* relobj,
464 const unsigned char* syms,
465 size_t count,
466 const char* sym_names,
467 size_t sym_name_size,
468 Symbol** sympointers)
470 gold_assert(size == relobj->target()->get_size());
471 gold_assert(size == parameters->get_size());
473 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
475 const unsigned char* p = syms;
476 for (size_t i = 0; i < count; ++i, p += sym_size)
478 elfcpp::Sym<size, big_endian> sym(p);
479 elfcpp::Sym<size, big_endian>* psym = &sym;
481 unsigned int st_name = psym->get_st_name();
482 if (st_name >= sym_name_size)
484 fprintf(stderr,
485 _("%s: %s: bad global symbol name offset %u at %lu\n"),
486 program_name, relobj->name().c_str(), st_name,
487 static_cast<unsigned long>(i));
488 gold_exit(false);
491 const char* name = sym_names + st_name;
493 // A symbol defined in a section which we are not including must
494 // be treated as an undefined symbol.
495 unsigned char symbuf[sym_size];
496 elfcpp::Sym<size, big_endian> sym2(symbuf);
497 unsigned int st_shndx = psym->get_st_shndx();
498 if (st_shndx != elfcpp::SHN_UNDEF
499 && st_shndx < elfcpp::SHN_LORESERVE
500 && !relobj->is_section_included(st_shndx))
502 memcpy(symbuf, p, sym_size);
503 elfcpp::Sym_write<size, big_endian> sw(symbuf);
504 sw.put_st_shndx(elfcpp::SHN_UNDEF);
505 psym = &sym2;
508 // In an object file, an '@' in the name separates the symbol
509 // name from the version name. If there are two '@' characters,
510 // this is the default version.
511 const char* ver = strchr(name, '@');
513 Symbol* res;
514 if (ver == NULL)
516 Stringpool::Key name_key;
517 name = this->namepool_.add(name, &name_key);
518 res = this->add_from_object(relobj, name, name_key, NULL, 0,
519 false, *psym);
521 else
523 Stringpool::Key name_key;
524 name = this->namepool_.add(name, ver - name, &name_key);
526 bool def = false;
527 ++ver;
528 if (*ver == '@')
530 def = true;
531 ++ver;
534 Stringpool::Key ver_key;
535 ver = this->namepool_.add(ver, &ver_key);
537 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
538 def, *psym);
541 *sympointers++ = res;
545 // Add all the symbols in a dynamic object to the hash table.
547 template<int size, bool big_endian>
548 void
549 Symbol_table::add_from_dynobj(
550 Sized_dynobj<size, big_endian>* dynobj,
551 const unsigned char* syms,
552 size_t count,
553 const char* sym_names,
554 size_t sym_name_size,
555 const unsigned char* versym,
556 size_t versym_size,
557 const std::vector<const char*>* version_map)
559 gold_assert(size == dynobj->target()->get_size());
560 gold_assert(size == parameters->get_size());
562 if (versym != NULL && versym_size / 2 < count)
564 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
565 program_name, dynobj->name().c_str());
566 gold_exit(false);
569 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
571 const unsigned char* p = syms;
572 const unsigned char* vs = versym;
573 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
575 elfcpp::Sym<size, big_endian> sym(p);
577 // Ignore symbols with local binding.
578 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
579 continue;
581 unsigned int st_name = sym.get_st_name();
582 if (st_name >= sym_name_size)
584 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
585 program_name, dynobj->name().c_str(), st_name,
586 static_cast<unsigned long>(i));
587 gold_exit(false);
590 const char* name = sym_names + st_name;
592 if (versym == NULL)
594 Stringpool::Key name_key;
595 name = this->namepool_.add(name, &name_key);
596 this->add_from_object(dynobj, name, name_key, NULL, 0,
597 false, sym);
598 continue;
601 // Read the version information.
603 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
605 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
606 v &= elfcpp::VERSYM_VERSION;
608 // The Sun documentation says that V can be VER_NDX_LOCAL, or
609 // VER_NDX_GLOBAL, or a version index. The meaning of
610 // VER_NDX_LOCAL is defined as "Symbol has local scope." The
611 // old GNU linker will happily generate VER_NDX_LOCAL for an
612 // undefined symbol. I don't know what the Sun linker will
613 // generate.
615 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
616 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
618 // This symbol should not be visible outside the object.
619 continue;
622 // At this point we are definitely going to add this symbol.
623 Stringpool::Key name_key;
624 name = this->namepool_.add(name, &name_key);
626 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
627 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
629 // This symbol does not have a version.
630 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
631 continue;
634 if (v >= version_map->size())
636 fprintf(stderr,
637 _("%s: %s: versym for symbol %zu out of range: %u\n"),
638 program_name, dynobj->name().c_str(), i, v);
639 gold_exit(false);
642 const char* version = (*version_map)[v];
643 if (version == NULL)
645 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
646 program_name, dynobj->name().c_str(), i, v);
647 gold_exit(false);
650 Stringpool::Key version_key;
651 version = this->namepool_.add(version, &version_key);
653 // If this is an absolute symbol, and the version name and
654 // symbol name are the same, then this is the version definition
655 // symbol. These symbols exist to support using -u to pull in
656 // particular versions. We do not want to record a version for
657 // them.
658 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
660 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
661 continue;
664 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
666 this->add_from_object(dynobj, name, name_key, version, version_key,
667 def, sym);
671 // Create and return a specially defined symbol. If ONLY_IF_REF is
672 // true, then only create the symbol if there is a reference to it.
673 // If this does not return NULL, it sets *POLDSYM to the existing
674 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
676 template<int size, bool big_endian>
677 Sized_symbol<size>*
678 Symbol_table::define_special_symbol(const Target* target, const char** pname,
679 const char** pversion, bool only_if_ref,
680 Sized_symbol<size>** poldsym
681 ACCEPT_SIZE_ENDIAN)
683 Symbol* oldsym;
684 Sized_symbol<size>* sym;
685 bool add_to_table = false;
686 typename Symbol_table_type::iterator add_loc = this->table_.end();
688 if (only_if_ref)
690 oldsym = this->lookup(*pname, *pversion);
691 if (oldsym == NULL || !oldsym->is_undefined())
692 return NULL;
694 *pname = oldsym->name();
695 *pversion = oldsym->version();
697 else
699 // Canonicalize NAME and VERSION.
700 Stringpool::Key name_key;
701 *pname = this->namepool_.add(*pname, &name_key);
703 Stringpool::Key version_key = 0;
704 if (*pversion != NULL)
705 *pversion = this->namepool_.add(*pversion, &version_key);
707 Symbol* const snull = NULL;
708 std::pair<typename Symbol_table_type::iterator, bool> ins =
709 this->table_.insert(std::make_pair(std::make_pair(name_key,
710 version_key),
711 snull));
713 if (!ins.second)
715 // We already have a symbol table entry for NAME/VERSION.
716 oldsym = ins.first->second;
717 gold_assert(oldsym != NULL);
719 else
721 // We haven't seen this symbol before.
722 gold_assert(ins.first->second == NULL);
723 add_to_table = true;
724 add_loc = ins.first;
725 oldsym = NULL;
729 if (!target->has_make_symbol())
730 sym = new Sized_symbol<size>();
731 else
733 gold_assert(target->get_size() == size);
734 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
735 typedef Sized_target<size, big_endian> My_target;
736 const My_target* sized_target =
737 static_cast<const My_target*>(target);
738 sym = sized_target->make_symbol();
739 if (sym == NULL)
740 return NULL;
743 if (add_to_table)
744 add_loc->second = sym;
745 else
746 gold_assert(oldsym != NULL);
748 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
749 SELECT_SIZE(size));
751 return sym;
754 // Define a symbol based on an Output_data.
756 Symbol*
757 Symbol_table::define_in_output_data(const Target* target, const char* name,
758 const char* version, Output_data* od,
759 uint64_t value, uint64_t symsize,
760 elfcpp::STT type, elfcpp::STB binding,
761 elfcpp::STV visibility,
762 unsigned char nonvis,
763 bool offset_is_from_end,
764 bool only_if_ref)
766 if (parameters->get_size() == 32)
768 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
769 return this->do_define_in_output_data<32>(target, name, version, od,
770 value, symsize, type, binding,
771 visibility, nonvis,
772 offset_is_from_end,
773 only_if_ref);
774 #else
775 gold_unreachable();
776 #endif
778 else if (parameters->get_size() == 64)
780 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
781 return this->do_define_in_output_data<64>(target, name, version, od,
782 value, symsize, type, binding,
783 visibility, nonvis,
784 offset_is_from_end,
785 only_if_ref);
786 #else
787 gold_unreachable();
788 #endif
790 else
791 gold_unreachable();
794 // Define a symbol in an Output_data, sized version.
796 template<int size>
797 Sized_symbol<size>*
798 Symbol_table::do_define_in_output_data(
799 const Target* target,
800 const char* name,
801 const char* version,
802 Output_data* od,
803 typename elfcpp::Elf_types<size>::Elf_Addr value,
804 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
805 elfcpp::STT type,
806 elfcpp::STB binding,
807 elfcpp::STV visibility,
808 unsigned char nonvis,
809 bool offset_is_from_end,
810 bool only_if_ref)
812 Sized_symbol<size>* sym;
813 Sized_symbol<size>* oldsym;
815 if (parameters->is_big_endian())
817 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
818 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
819 target, &name, &version, only_if_ref, &oldsym
820 SELECT_SIZE_ENDIAN(size, true));
821 #else
822 gold_unreachable();
823 #endif
825 else
827 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
828 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
829 target, &name, &version, only_if_ref, &oldsym
830 SELECT_SIZE_ENDIAN(size, false));
831 #else
832 gold_unreachable();
833 #endif
836 if (sym == NULL)
837 return NULL;
839 gold_assert(version == NULL || oldsym != NULL);
840 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
841 offset_is_from_end);
843 if (oldsym != NULL
844 && Symbol_table::should_override_with_special(oldsym))
845 oldsym->override_with_special(sym);
847 return sym;
850 // Define a symbol based on an Output_segment.
852 Symbol*
853 Symbol_table::define_in_output_segment(const Target* target, const char* name,
854 const char* version, Output_segment* os,
855 uint64_t value, uint64_t symsize,
856 elfcpp::STT type, elfcpp::STB binding,
857 elfcpp::STV visibility,
858 unsigned char nonvis,
859 Symbol::Segment_offset_base offset_base,
860 bool only_if_ref)
862 if (parameters->get_size() == 32)
864 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
865 return this->do_define_in_output_segment<32>(target, name, version, os,
866 value, symsize, type,
867 binding, visibility, nonvis,
868 offset_base, only_if_ref);
869 #else
870 gold_unreachable();
871 #endif
873 else if (parameters->get_size() == 64)
875 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
876 return this->do_define_in_output_segment<64>(target, name, version, os,
877 value, symsize, type,
878 binding, visibility, nonvis,
879 offset_base, only_if_ref);
880 #else
881 gold_unreachable();
882 #endif
884 else
885 gold_unreachable();
888 // Define a symbol in an Output_segment, sized version.
890 template<int size>
891 Sized_symbol<size>*
892 Symbol_table::do_define_in_output_segment(
893 const Target* target,
894 const char* name,
895 const char* version,
896 Output_segment* os,
897 typename elfcpp::Elf_types<size>::Elf_Addr value,
898 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
899 elfcpp::STT type,
900 elfcpp::STB binding,
901 elfcpp::STV visibility,
902 unsigned char nonvis,
903 Symbol::Segment_offset_base offset_base,
904 bool only_if_ref)
906 Sized_symbol<size>* sym;
907 Sized_symbol<size>* oldsym;
909 if (parameters->is_big_endian())
911 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
912 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
913 target, &name, &version, only_if_ref, &oldsym
914 SELECT_SIZE_ENDIAN(size, true));
915 #else
916 gold_unreachable();
917 #endif
919 else
921 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
922 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
923 target, &name, &version, only_if_ref, &oldsym
924 SELECT_SIZE_ENDIAN(size, false));
925 #else
926 gold_unreachable();
927 #endif
930 if (sym == NULL)
931 return NULL;
933 gold_assert(version == NULL || oldsym != NULL);
934 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
935 offset_base);
937 if (oldsym != NULL
938 && Symbol_table::should_override_with_special(oldsym))
939 oldsym->override_with_special(sym);
941 return sym;
944 // Define a special symbol with a constant value. It is a multiple
945 // definition error if this symbol is already defined.
947 Symbol*
948 Symbol_table::define_as_constant(const Target* target, const char* name,
949 const char* version, uint64_t value,
950 uint64_t symsize, elfcpp::STT type,
951 elfcpp::STB binding, elfcpp::STV visibility,
952 unsigned char nonvis, bool only_if_ref)
954 if (parameters->get_size() == 32)
956 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
957 return this->do_define_as_constant<32>(target, name, version, value,
958 symsize, type, binding,
959 visibility, nonvis, only_if_ref);
960 #else
961 gold_unreachable();
962 #endif
964 else if (parameters->get_size() == 64)
966 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
967 return this->do_define_as_constant<64>(target, name, version, value,
968 symsize, type, binding,
969 visibility, nonvis, only_if_ref);
970 #else
971 gold_unreachable();
972 #endif
974 else
975 gold_unreachable();
978 // Define a symbol as a constant, sized version.
980 template<int size>
981 Sized_symbol<size>*
982 Symbol_table::do_define_as_constant(
983 const Target* target,
984 const char* name,
985 const char* version,
986 typename elfcpp::Elf_types<size>::Elf_Addr value,
987 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
988 elfcpp::STT type,
989 elfcpp::STB binding,
990 elfcpp::STV visibility,
991 unsigned char nonvis,
992 bool only_if_ref)
994 Sized_symbol<size>* sym;
995 Sized_symbol<size>* oldsym;
997 if (parameters->is_big_endian())
999 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1000 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1001 target, &name, &version, only_if_ref, &oldsym
1002 SELECT_SIZE_ENDIAN(size, true));
1003 #else
1004 gold_unreachable();
1005 #endif
1007 else
1009 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1010 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1011 target, &name, &version, only_if_ref, &oldsym
1012 SELECT_SIZE_ENDIAN(size, false));
1013 #else
1014 gold_unreachable();
1015 #endif
1018 if (sym == NULL)
1019 return NULL;
1021 gold_assert(version == NULL || oldsym != NULL);
1022 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1024 if (oldsym != NULL
1025 && Symbol_table::should_override_with_special(oldsym))
1026 oldsym->override_with_special(sym);
1028 return sym;
1031 // Define a set of symbols in output sections.
1033 void
1034 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1035 int count, const Define_symbol_in_section* p)
1037 for (int i = 0; i < count; ++i, ++p)
1039 Output_section* os = layout->find_output_section(p->output_section);
1040 if (os != NULL)
1041 this->define_in_output_data(target, p->name, NULL, os, p->value,
1042 p->size, p->type, p->binding,
1043 p->visibility, p->nonvis,
1044 p->offset_is_from_end, p->only_if_ref);
1045 else
1046 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1047 p->binding, p->visibility, p->nonvis,
1048 p->only_if_ref);
1052 // Define a set of symbols in output segments.
1054 void
1055 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1056 int count, const Define_symbol_in_segment* p)
1058 for (int i = 0; i < count; ++i, ++p)
1060 Output_segment* os = layout->find_output_segment(p->segment_type,
1061 p->segment_flags_set,
1062 p->segment_flags_clear);
1063 if (os != NULL)
1064 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1065 p->size, p->type, p->binding,
1066 p->visibility, p->nonvis,
1067 p->offset_base, p->only_if_ref);
1068 else
1069 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1070 p->binding, p->visibility, p->nonvis,
1071 p->only_if_ref);
1075 // Set the dynamic symbol indexes. INDEX is the index of the first
1076 // global dynamic symbol. Pointers to the symbols are stored into the
1077 // vector SYMS. The names are added to DYNPOOL. This returns an
1078 // updated dynamic symbol index.
1080 unsigned int
1081 Symbol_table::set_dynsym_indexes(const General_options* options,
1082 const Target* target,
1083 unsigned int index,
1084 std::vector<Symbol*>* syms,
1085 Stringpool* dynpool,
1086 Versions* versions)
1088 for (Symbol_table_type::iterator p = this->table_.begin();
1089 p != this->table_.end();
1090 ++p)
1092 Symbol* sym = p->second;
1094 // Note that SYM may already have a dynamic symbol index, since
1095 // some symbols appear more than once in the symbol table, with
1096 // and without a version.
1098 if (!sym->needs_dynsym_entry()
1099 && (!options->export_dynamic()
1100 || !sym->in_reg()
1101 || !sym->is_externally_visible()))
1102 sym->set_dynsym_index(-1U);
1103 else if (!sym->has_dynsym_index())
1105 sym->set_dynsym_index(index);
1106 ++index;
1107 syms->push_back(sym);
1108 dynpool->add(sym->name(), NULL);
1110 // Record any version information.
1111 if (sym->version() != NULL)
1112 versions->record_version(options, dynpool, sym);
1116 // Finish up the versions. In some cases this may add new dynamic
1117 // symbols.
1118 index = versions->finalize(target, this, index, syms);
1120 return index;
1123 // Set the final values for all the symbols. The index of the first
1124 // global symbol in the output file is INDEX. Record the file offset
1125 // OFF. Add their names to POOL. Return the new file offset.
1127 off_t
1128 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1129 size_t dyn_global_index, size_t dyncount,
1130 Stringpool* pool)
1132 off_t ret;
1134 gold_assert(index != 0);
1135 this->first_global_index_ = index;
1137 this->dynamic_offset_ = dynoff;
1138 this->first_dynamic_global_index_ = dyn_global_index;
1139 this->dynamic_count_ = dyncount;
1141 if (parameters->get_size() == 32)
1143 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1144 ret = this->sized_finalize<32>(index, off, pool);
1145 #else
1146 gold_unreachable();
1147 #endif
1149 else if (parameters->get_size() == 64)
1151 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1152 ret = this->sized_finalize<64>(index, off, pool);
1153 #else
1154 gold_unreachable();
1155 #endif
1157 else
1158 gold_unreachable();
1160 // Now that we have the final symbol table, we can reliably note
1161 // which symbols should get warnings.
1162 this->warnings_.note_warnings(this);
1164 return ret;
1167 // Set the final value for all the symbols. This is called after
1168 // Layout::finalize, so all the output sections have their final
1169 // address.
1171 template<int size>
1172 off_t
1173 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1175 off = align_address(off, size >> 3);
1176 this->offset_ = off;
1178 size_t orig_index = index;
1180 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1181 for (Symbol_table_type::iterator p = this->table_.begin();
1182 p != this->table_.end();
1183 ++p)
1185 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1187 // FIXME: Here we need to decide which symbols should go into
1188 // the output file, based on --strip.
1190 // The default version of a symbol may appear twice in the
1191 // symbol table. We only need to finalize it once.
1192 if (sym->has_symtab_index())
1193 continue;
1195 if (!sym->in_reg())
1197 gold_assert(!sym->has_symtab_index());
1198 sym->set_symtab_index(-1U);
1199 gold_assert(sym->dynsym_index() == -1U);
1200 continue;
1203 typename Sized_symbol<size>::Value_type value;
1205 switch (sym->source())
1207 case Symbol::FROM_OBJECT:
1209 unsigned int shndx = sym->shndx();
1211 // FIXME: We need some target specific support here.
1212 if (shndx >= elfcpp::SHN_LORESERVE
1213 && shndx != elfcpp::SHN_ABS)
1215 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1216 program_name, sym->name(), shndx);
1217 gold_exit(false);
1220 Object* symobj = sym->object();
1221 if (symobj->is_dynamic())
1223 value = 0;
1224 shndx = elfcpp::SHN_UNDEF;
1226 else if (shndx == elfcpp::SHN_UNDEF)
1227 value = 0;
1228 else if (shndx == elfcpp::SHN_ABS)
1229 value = sym->value();
1230 else
1232 Relobj* relobj = static_cast<Relobj*>(symobj);
1233 off_t secoff;
1234 Output_section* os = relobj->output_section(shndx, &secoff);
1236 if (os == NULL)
1238 sym->set_symtab_index(-1U);
1239 gold_assert(sym->dynsym_index() == -1U);
1240 continue;
1243 value = sym->value() + os->address() + secoff;
1246 break;
1248 case Symbol::IN_OUTPUT_DATA:
1250 Output_data* od = sym->output_data();
1251 value = sym->value() + od->address();
1252 if (sym->offset_is_from_end())
1253 value += od->data_size();
1255 break;
1257 case Symbol::IN_OUTPUT_SEGMENT:
1259 Output_segment* os = sym->output_segment();
1260 value = sym->value() + os->vaddr();
1261 switch (sym->offset_base())
1263 case Symbol::SEGMENT_START:
1264 break;
1265 case Symbol::SEGMENT_END:
1266 value += os->memsz();
1267 break;
1268 case Symbol::SEGMENT_BSS:
1269 value += os->filesz();
1270 break;
1271 default:
1272 gold_unreachable();
1275 break;
1277 case Symbol::CONSTANT:
1278 value = sym->value();
1279 break;
1281 default:
1282 gold_unreachable();
1285 sym->set_value(value);
1286 sym->set_symtab_index(index);
1287 pool->add(sym->name(), NULL);
1288 ++index;
1289 off += sym_size;
1292 this->output_count_ = index - orig_index;
1294 return off;
1297 // Write out the global symbols.
1299 void
1300 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1301 const Stringpool* dynpool, Output_file* of) const
1303 if (parameters->get_size() == 32)
1305 if (parameters->is_big_endian())
1307 #ifdef HAVE_TARGET_32_BIG
1308 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1309 #else
1310 gold_unreachable();
1311 #endif
1313 else
1315 #ifdef HAVE_TARGET_32_LITTLE
1316 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1317 #else
1318 gold_unreachable();
1319 #endif
1322 else if (parameters->get_size() == 64)
1324 if (parameters->is_big_endian())
1326 #ifdef HAVE_TARGET_64_BIG
1327 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1328 #else
1329 gold_unreachable();
1330 #endif
1332 else
1334 #ifdef HAVE_TARGET_64_LITTLE
1335 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1336 #else
1337 gold_unreachable();
1338 #endif
1341 else
1342 gold_unreachable();
1345 // Write out the global symbols.
1347 template<int size, bool big_endian>
1348 void
1349 Symbol_table::sized_write_globals(const Target* target,
1350 const Stringpool* sympool,
1351 const Stringpool* dynpool,
1352 Output_file* of) const
1354 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1355 unsigned int index = this->first_global_index_;
1356 const off_t oview_size = this->output_count_ * sym_size;
1357 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1359 unsigned int dynamic_count = this->dynamic_count_;
1360 off_t dynamic_size = dynamic_count * sym_size;
1361 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1362 unsigned char* dynamic_view;
1363 if (this->dynamic_offset_ == 0)
1364 dynamic_view = NULL;
1365 else
1366 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1368 unsigned char* ps = psyms;
1369 for (Symbol_table_type::const_iterator p = this->table_.begin();
1370 p != this->table_.end();
1371 ++p)
1373 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1375 unsigned int sym_index = sym->symtab_index();
1376 unsigned int dynsym_index;
1377 if (dynamic_view == NULL)
1378 dynsym_index = -1U;
1379 else
1380 dynsym_index = sym->dynsym_index();
1382 if (sym_index == -1U && dynsym_index == -1U)
1384 // This symbol is not included in the output file.
1385 continue;
1388 if (sym_index == index)
1389 ++index;
1390 else if (sym_index != -1U)
1392 // We have already seen this symbol, because it has a
1393 // default version.
1394 gold_assert(sym_index < index);
1395 if (dynsym_index == -1U)
1396 continue;
1397 sym_index = -1U;
1400 unsigned int shndx;
1401 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1402 switch (sym->source())
1404 case Symbol::FROM_OBJECT:
1406 unsigned int in_shndx = sym->shndx();
1408 // FIXME: We need some target specific support here.
1409 if (in_shndx >= elfcpp::SHN_LORESERVE
1410 && in_shndx != elfcpp::SHN_ABS)
1412 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1413 program_name, sym->name(), in_shndx);
1414 gold_exit(false);
1417 Object* symobj = sym->object();
1418 if (symobj->is_dynamic())
1420 if (sym->needs_dynsym_value())
1421 value = target->dynsym_value(sym);
1422 shndx = elfcpp::SHN_UNDEF;
1424 else if (in_shndx == elfcpp::SHN_UNDEF
1425 || in_shndx == elfcpp::SHN_ABS)
1426 shndx = in_shndx;
1427 else
1429 Relobj* relobj = static_cast<Relobj*>(symobj);
1430 off_t secoff;
1431 Output_section* os = relobj->output_section(in_shndx, &secoff);
1432 gold_assert(os != NULL);
1433 shndx = os->out_shndx();
1436 break;
1438 case Symbol::IN_OUTPUT_DATA:
1439 shndx = sym->output_data()->out_shndx();
1440 break;
1442 case Symbol::IN_OUTPUT_SEGMENT:
1443 shndx = elfcpp::SHN_ABS;
1444 break;
1446 case Symbol::CONSTANT:
1447 shndx = elfcpp::SHN_ABS;
1448 break;
1450 default:
1451 gold_unreachable();
1454 if (sym_index != -1U)
1456 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1457 sym, sym->value(), shndx, sympool, ps
1458 SELECT_SIZE_ENDIAN(size, big_endian));
1459 ps += sym_size;
1462 if (dynsym_index != -1U)
1464 dynsym_index -= first_dynamic_global_index;
1465 gold_assert(dynsym_index < dynamic_count);
1466 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1467 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1468 sym, value, shndx, dynpool, pd
1469 SELECT_SIZE_ENDIAN(size, big_endian));
1473 gold_assert(ps - psyms == oview_size);
1475 of->write_output_view(this->offset_, oview_size, psyms);
1476 if (dynamic_view != NULL)
1477 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1480 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1481 // strtab holding the name.
1483 template<int size, bool big_endian>
1484 void
1485 Symbol_table::sized_write_symbol(
1486 Sized_symbol<size>* sym,
1487 typename elfcpp::Elf_types<size>::Elf_Addr value,
1488 unsigned int shndx,
1489 const Stringpool* pool,
1490 unsigned char* p
1491 ACCEPT_SIZE_ENDIAN) const
1493 elfcpp::Sym_write<size, big_endian> osym(p);
1494 osym.put_st_name(pool->get_offset(sym->name()));
1495 osym.put_st_value(value);
1496 osym.put_st_size(sym->symsize());
1497 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1498 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1499 osym.put_st_shndx(shndx);
1502 // Write out a section symbol. Return the update offset.
1504 void
1505 Symbol_table::write_section_symbol(const Output_section *os,
1506 Output_file* of,
1507 off_t offset) const
1509 if (parameters->get_size() == 32)
1511 if (parameters->is_big_endian())
1513 #ifdef HAVE_TARGET_32_BIG
1514 this->sized_write_section_symbol<32, true>(os, of, offset);
1515 #else
1516 gold_unreachable();
1517 #endif
1519 else
1521 #ifdef HAVE_TARGET_32_LITTLE
1522 this->sized_write_section_symbol<32, false>(os, of, offset);
1523 #else
1524 gold_unreachable();
1525 #endif
1528 else if (parameters->get_size() == 64)
1530 if (parameters->is_big_endian())
1532 #ifdef HAVE_TARGET_64_BIG
1533 this->sized_write_section_symbol<64, true>(os, of, offset);
1534 #else
1535 gold_unreachable();
1536 #endif
1538 else
1540 #ifdef HAVE_TARGET_64_LITTLE
1541 this->sized_write_section_symbol<64, false>(os, of, offset);
1542 #else
1543 gold_unreachable();
1544 #endif
1547 else
1548 gold_unreachable();
1551 // Write out a section symbol, specialized for size and endianness.
1553 template<int size, bool big_endian>
1554 void
1555 Symbol_table::sized_write_section_symbol(const Output_section* os,
1556 Output_file* of,
1557 off_t offset) const
1559 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1561 unsigned char* pov = of->get_output_view(offset, sym_size);
1563 elfcpp::Sym_write<size, big_endian> osym(pov);
1564 osym.put_st_name(0);
1565 osym.put_st_value(os->address());
1566 osym.put_st_size(0);
1567 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1568 elfcpp::STT_SECTION));
1569 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1570 osym.put_st_shndx(os->out_shndx());
1572 of->write_output_view(offset, sym_size, pov);
1575 // Warnings functions.
1577 // Add a new warning.
1579 void
1580 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1581 unsigned int shndx)
1583 name = symtab->canonicalize_name(name);
1584 this->warnings_[name].set(obj, shndx);
1587 // Look through the warnings and mark the symbols for which we should
1588 // warn. This is called during Layout::finalize when we know the
1589 // sources for all the symbols.
1591 void
1592 Warnings::note_warnings(Symbol_table* symtab)
1594 for (Warning_table::iterator p = this->warnings_.begin();
1595 p != this->warnings_.end();
1596 ++p)
1598 Symbol* sym = symtab->lookup(p->first, NULL);
1599 if (sym != NULL
1600 && sym->source() == Symbol::FROM_OBJECT
1601 && sym->object() == p->second.object)
1603 sym->set_has_warning();
1605 // Read the section contents to get the warning text. It
1606 // would be nicer if we only did this if we have to actually
1607 // issue a warning. Unfortunately, warnings are issued as
1608 // we relocate sections. That means that we can not lock
1609 // the object then, as we might try to issue the same
1610 // warning multiple times simultaneously.
1612 Task_locker_obj<Object> tl(*p->second.object);
1613 const unsigned char* c;
1614 off_t len;
1615 c = p->second.object->section_contents(p->second.shndx, &len,
1616 false);
1617 p->second.set_text(reinterpret_cast<const char*>(c), len);
1623 // Issue a warning. This is called when we see a relocation against a
1624 // symbol for which has a warning.
1626 void
1627 Warnings::issue_warning(const Symbol* sym, const std::string& location) const
1629 gold_assert(sym->has_warning());
1630 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1631 gold_assert(p != this->warnings_.end());
1632 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1633 p->second.text.c_str());
1636 // Instantiate the templates we need. We could use the configure
1637 // script to restrict this to only the ones needed for implemented
1638 // targets.
1640 #ifdef HAVE_TARGET_32_LITTLE
1641 template
1642 void
1643 Symbol_table::add_from_relobj<32, false>(
1644 Sized_relobj<32, false>* relobj,
1645 const unsigned char* syms,
1646 size_t count,
1647 const char* sym_names,
1648 size_t sym_name_size,
1649 Symbol** sympointers);
1650 #endif
1652 #ifdef HAVE_TARGET_32_BIG
1653 template
1654 void
1655 Symbol_table::add_from_relobj<32, true>(
1656 Sized_relobj<32, true>* relobj,
1657 const unsigned char* syms,
1658 size_t count,
1659 const char* sym_names,
1660 size_t sym_name_size,
1661 Symbol** sympointers);
1662 #endif
1664 #ifdef HAVE_TARGET_64_LITTLE
1665 template
1666 void
1667 Symbol_table::add_from_relobj<64, false>(
1668 Sized_relobj<64, false>* relobj,
1669 const unsigned char* syms,
1670 size_t count,
1671 const char* sym_names,
1672 size_t sym_name_size,
1673 Symbol** sympointers);
1674 #endif
1676 #ifdef HAVE_TARGET_64_BIG
1677 template
1678 void
1679 Symbol_table::add_from_relobj<64, true>(
1680 Sized_relobj<64, true>* relobj,
1681 const unsigned char* syms,
1682 size_t count,
1683 const char* sym_names,
1684 size_t sym_name_size,
1685 Symbol** sympointers);
1686 #endif
1688 #ifdef HAVE_TARGET_32_LITTLE
1689 template
1690 void
1691 Symbol_table::add_from_dynobj<32, false>(
1692 Sized_dynobj<32, false>* dynobj,
1693 const unsigned char* syms,
1694 size_t count,
1695 const char* sym_names,
1696 size_t sym_name_size,
1697 const unsigned char* versym,
1698 size_t versym_size,
1699 const std::vector<const char*>* version_map);
1700 #endif
1702 #ifdef HAVE_TARGET_32_BIG
1703 template
1704 void
1705 Symbol_table::add_from_dynobj<32, true>(
1706 Sized_dynobj<32, true>* dynobj,
1707 const unsigned char* syms,
1708 size_t count,
1709 const char* sym_names,
1710 size_t sym_name_size,
1711 const unsigned char* versym,
1712 size_t versym_size,
1713 const std::vector<const char*>* version_map);
1714 #endif
1716 #ifdef HAVE_TARGET_64_LITTLE
1717 template
1718 void
1719 Symbol_table::add_from_dynobj<64, false>(
1720 Sized_dynobj<64, false>* dynobj,
1721 const unsigned char* syms,
1722 size_t count,
1723 const char* sym_names,
1724 size_t sym_name_size,
1725 const unsigned char* versym,
1726 size_t versym_size,
1727 const std::vector<const char*>* version_map);
1728 #endif
1730 #ifdef HAVE_TARGET_64_BIG
1731 template
1732 void
1733 Symbol_table::add_from_dynobj<64, true>(
1734 Sized_dynobj<64, true>* dynobj,
1735 const unsigned char* syms,
1736 size_t count,
1737 const char* sym_names,
1738 size_t sym_name_size,
1739 const unsigned char* versym,
1740 size_t versym_size,
1741 const std::vector<const char*>* version_map);
1742 #endif
1744 } // End namespace gold.