Add cache parameter to get_view. Discard uncached views on unlock.
[binutils-gdb.git] / gold / symtab.cc
1 // symtab.cc -- the gold symbol table
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <stdint.h>
26 #include <string>
27 #include <utility>
28
29 #include "object.h"
30 #include "dynobj.h"
31 #include "output.h"
32 #include "target.h"
33 #include "workqueue.h"
34 #include "symtab.h"
35
36 namespace gold
37 {
38
39 // Class Symbol.
40
41 // Initialize fields in Symbol. This initializes everything except u_
42 // and source_.
43
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)
48 {
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;
68 }
69
70 // Initialize the fields in the base class Symbol for SYM in OBJECT.
71
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)
76 {
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();
85 }
86
87 // Initialize the fields in the base class Symbol for a symbol defined
88 // in an Output_data.
89
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)
94 {
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;
100 }
101
102 // Initialize the fields in the base class Symbol for a symbol defined
103 // in an Output_segment.
104
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)
109 {
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;
115 }
116
117 // Initialize the fields in the base class Symbol for a symbol defined
118 // as a constant.
119
120 void
121 Symbol::init_base(const char* name, elfcpp::STT type,
122 elfcpp::STB binding, elfcpp::STV visibility,
123 unsigned char nonvis)
124 {
125 this->init_fields(name, NULL, type, binding, visibility, nonvis);
126 this->source_ = CONSTANT;
127 this->in_reg_ = true;
128 }
129
130 // Initialize the fields in Sized_symbol for SYM in OBJECT.
131
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)
137 {
138 this->init_base(name, version, object, sym);
139 this->value_ = sym.get_st_value();
140 this->symsize_ = sym.get_st_size();
141 }
142
143 // Initialize the fields in Sized_symbol for a symbol defined in an
144 // Output_data.
145
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)
153 {
154 this->init_base(name, od, type, binding, visibility, nonvis,
155 offset_is_from_end);
156 this->value_ = value;
157 this->symsize_ = symsize;
158 }
159
160 // Initialize the fields in Sized_symbol for a symbol defined in an
161 // Output_segment.
162
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)
170 {
171 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
172 this->value_ = value;
173 this->symsize_ = symsize;
174 }
175
176 // Initialize the fields in Sized_symbol for a symbol defined as a
177 // constant.
178
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)
184 {
185 this->init_base(name, type, binding, visibility, nonvis);
186 this->value_ = value;
187 this->symsize_ = symsize;
188 }
189
190 // Class Symbol_table.
191
192 Symbol_table::Symbol_table()
193 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
194 forwarders_(), commons_(), warnings_()
195 {
196 }
197
198 Symbol_table::~Symbol_table()
199 {
200 }
201
202 // The hash function. The key is always canonicalized, so we use a
203 // simple combination of the pointers.
204
205 size_t
206 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
207 {
208 return key.first ^ key.second;
209 }
210
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.
214
215 bool
216 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
217 const Symbol_table_key& k2) const
218 {
219 return k1.first == k2.first && k1.second == k2.second;
220 }
221
222 // Make TO a symbol which forwards to FROM.
223
224 void
225 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
226 {
227 gold_assert(from != to);
228 gold_assert(!from->is_forwarder() && !to->is_forwarder());
229 this->forwarders_[from] = to;
230 from->set_forwarder();
231 }
232
233 // Resolve the forwards from FROM, returning the real symbol.
234
235 Symbol*
236 Symbol_table::resolve_forwards(const Symbol* from) const
237 {
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;
243 }
244
245 // Look up a symbol by name.
246
247 Symbol*
248 Symbol_table::lookup(const char* name, const char* version) const
249 {
250 Stringpool::Key name_key;
251 name = this->namepool_.find(name, &name_key);
252 if (name == NULL)
253 return NULL;
254
255 Stringpool::Key version_key = 0;
256 if (version != NULL)
257 {
258 version = this->namepool_.find(version, &version_key);
259 if (version == NULL)
260 return NULL;
261 }
262
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;
268 }
269
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.
275
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)
280 {
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();
294 }
295
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.
299
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.
307
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.
318
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)
328 {
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));
333
334 std::pair<typename Symbol_table_type::iterator, bool> insdef =
335 std::make_pair(this->table_.end(), false);
336 if (def)
337 {
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));
342 }
343
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.
348
349 Sized_symbol<size>* ret;
350 bool was_undefined;
351 bool was_common;
352 if (!ins.second)
353 {
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);
358
359 was_undefined = ret->is_undefined();
360 was_common = ret->is_common();
361
362 Symbol_table::resolve(ret, sym, object, version);
363
364 if (def)
365 {
366 if (insdef.second)
367 {
368 // This is the first time we have seen NAME/NULL. Make
369 // NAME/NULL point to NAME/VERSION.
370 insdef.first->second = ret;
371 }
372 else if (insdef.first->second != ret)
373 {
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;
384 }
385 }
386 }
387 else
388 {
389 // This is the first time we have seen NAME/VERSION.
390 gold_assert(ins.first->second == NULL);
391
392 was_undefined = false;
393 was_common = false;
394
395 if (def && !insdef.second)
396 {
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;
404 }
405 else
406 {
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
413 {
414 ret = target->make_symbol();
415 if (ret == NULL)
416 {
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
422 {
423 this->table_.erase(insdef.first);
424 // Inserting insdef invalidated ins.
425 this->table_.erase(std::make_pair(name_key,
426 version_key));
427 }
428 return NULL;
429 }
430 }
431
432 ret->init(name, version, object, sym);
433
434 ins.first->second = ret;
435 if (def)
436 {
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;
441 }
442 }
443 }
444
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_;
449
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);
454
455 return ret;
456 }
457
458 // Add all the symbols in a relocatable object to the hash table.
459
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)
469 {
470 // We take the size from the first object we see.
471 if (this->get_size() == 0)
472 this->set_size(size);
473
474 if (size != this->get_size() || size != relobj->target()->get_size())
475 {
476 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
477 program_name, relobj->name().c_str());
478 gold_exit(false);
479 }
480
481 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
482
483 const unsigned char* p = syms;
484 for (size_t i = 0; i < count; ++i, p += sym_size)
485 {
486 elfcpp::Sym<size, big_endian> sym(p);
487 elfcpp::Sym<size, big_endian>* psym = &sym;
488
489 unsigned int st_name = psym->get_st_name();
490 if (st_name >= sym_name_size)
491 {
492 fprintf(stderr,
493 _("%s: %s: bad global symbol name offset %u at %lu\n"),
494 program_name, relobj->name().c_str(), st_name,
495 static_cast<unsigned long>(i));
496 gold_exit(false);
497 }
498
499 const char* name = sym_names + st_name;
500
501 // A symbol defined in a section which we are not including must
502 // be treated as an undefined symbol.
503 unsigned char symbuf[sym_size];
504 elfcpp::Sym<size, big_endian> sym2(symbuf);
505 unsigned int st_shndx = psym->get_st_shndx();
506 if (st_shndx != elfcpp::SHN_UNDEF
507 && st_shndx < elfcpp::SHN_LORESERVE
508 && !relobj->is_section_included(st_shndx))
509 {
510 memcpy(symbuf, p, sym_size);
511 elfcpp::Sym_write<size, big_endian> sw(symbuf);
512 sw.put_st_shndx(elfcpp::SHN_UNDEF);
513 psym = &sym2;
514 }
515
516 // In an object file, an '@' in the name separates the symbol
517 // name from the version name. If there are two '@' characters,
518 // this is the default version.
519 const char* ver = strchr(name, '@');
520
521 Symbol* res;
522 if (ver == NULL)
523 {
524 Stringpool::Key name_key;
525 name = this->namepool_.add(name, &name_key);
526 res = this->add_from_object(relobj, name, name_key, NULL, 0,
527 false, *psym);
528 }
529 else
530 {
531 Stringpool::Key name_key;
532 name = this->namepool_.add(name, ver - name, &name_key);
533
534 bool def = false;
535 ++ver;
536 if (*ver == '@')
537 {
538 def = true;
539 ++ver;
540 }
541
542 Stringpool::Key ver_key;
543 ver = this->namepool_.add(ver, &ver_key);
544
545 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
546 def, *psym);
547 }
548
549 *sympointers++ = res;
550 }
551 }
552
553 // Add all the symbols in a dynamic object to the hash table.
554
555 template<int size, bool big_endian>
556 void
557 Symbol_table::add_from_dynobj(
558 Sized_dynobj<size, big_endian>* dynobj,
559 const unsigned char* syms,
560 size_t count,
561 const char* sym_names,
562 size_t sym_name_size,
563 const unsigned char* versym,
564 size_t versym_size,
565 const std::vector<const char*>* version_map)
566 {
567 // We take the size from the first object we see.
568 if (this->get_size() == 0)
569 this->set_size(size);
570
571 if (size != this->get_size() || size != dynobj->target()->get_size())
572 {
573 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
574 program_name, dynobj->name().c_str());
575 gold_exit(false);
576 }
577
578 if (versym != NULL && versym_size / 2 < count)
579 {
580 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
581 program_name, dynobj->name().c_str());
582 gold_exit(false);
583 }
584
585 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
586
587 const unsigned char* p = syms;
588 const unsigned char* vs = versym;
589 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
590 {
591 elfcpp::Sym<size, big_endian> sym(p);
592
593 // Ignore symbols with local binding.
594 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
595 continue;
596
597 unsigned int st_name = sym.get_st_name();
598 if (st_name >= sym_name_size)
599 {
600 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
601 program_name, dynobj->name().c_str(), st_name,
602 static_cast<unsigned long>(i));
603 gold_exit(false);
604 }
605
606 const char* name = sym_names + st_name;
607
608 if (versym == NULL)
609 {
610 Stringpool::Key name_key;
611 name = this->namepool_.add(name, &name_key);
612 this->add_from_object(dynobj, name, name_key, NULL, 0,
613 false, sym);
614 continue;
615 }
616
617 // Read the version information.
618
619 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
620
621 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
622 v &= elfcpp::VERSYM_VERSION;
623
624 // The Sun documentation says that V can be VER_NDX_LOCAL, or
625 // VER_NDX_GLOBAL, or a version index. The meaning of
626 // VER_NDX_LOCAL is defined as "Symbol has local scope." The
627 // old GNU linker will happily generate VER_NDX_LOCAL for an
628 // undefined symbol. I don't know what the Sun linker will
629 // generate.
630
631 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
632 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
633 {
634 // This symbol should not be visible outside the object.
635 continue;
636 }
637
638 // At this point we are definitely going to add this symbol.
639 Stringpool::Key name_key;
640 name = this->namepool_.add(name, &name_key);
641
642 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
643 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
644 {
645 // This symbol does not have a version.
646 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
647 continue;
648 }
649
650 if (v >= version_map->size())
651 {
652 fprintf(stderr,
653 _("%s: %s: versym for symbol %zu out of range: %u\n"),
654 program_name, dynobj->name().c_str(), i, v);
655 gold_exit(false);
656 }
657
658 const char* version = (*version_map)[v];
659 if (version == NULL)
660 {
661 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
662 program_name, dynobj->name().c_str(), i, v);
663 gold_exit(false);
664 }
665
666 Stringpool::Key version_key;
667 version = this->namepool_.add(version, &version_key);
668
669 // If this is an absolute symbol, and the version name and
670 // symbol name are the same, then this is the version definition
671 // symbol. These symbols exist to support using -u to pull in
672 // particular versions. We do not want to record a version for
673 // them.
674 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
675 {
676 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
677 continue;
678 }
679
680 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
681
682 this->add_from_object(dynobj, name, name_key, version, version_key,
683 def, sym);
684 }
685 }
686
687 // Create and return a specially defined symbol. If ONLY_IF_REF is
688 // true, then only create the symbol if there is a reference to it.
689 // If this does not return NULL, it sets *POLDSYM to the existing
690 // symbol if there is one. This canonicalizes *PNAME and *PVERSION.
691
692 template<int size, bool big_endian>
693 Sized_symbol<size>*
694 Symbol_table::define_special_symbol(const Target* target, const char** pname,
695 const char** pversion, bool only_if_ref,
696 Sized_symbol<size>** poldsym
697 ACCEPT_SIZE_ENDIAN)
698 {
699 gold_assert(this->size_ == size);
700
701 Symbol* oldsym;
702 Sized_symbol<size>* sym;
703 bool add_to_table = false;
704 typename Symbol_table_type::iterator add_loc = this->table_.end();
705
706 if (only_if_ref)
707 {
708 oldsym = this->lookup(*pname, *pversion);
709 if (oldsym == NULL || !oldsym->is_undefined())
710 return NULL;
711
712 *pname = oldsym->name();
713 *pversion = oldsym->version();
714 }
715 else
716 {
717 // Canonicalize NAME and VERSION.
718 Stringpool::Key name_key;
719 *pname = this->namepool_.add(*pname, &name_key);
720
721 Stringpool::Key version_key = 0;
722 if (*pversion != NULL)
723 *pversion = this->namepool_.add(*pversion, &version_key);
724
725 Symbol* const snull = NULL;
726 std::pair<typename Symbol_table_type::iterator, bool> ins =
727 this->table_.insert(std::make_pair(std::make_pair(name_key,
728 version_key),
729 snull));
730
731 if (!ins.second)
732 {
733 // We already have a symbol table entry for NAME/VERSION.
734 oldsym = ins.first->second;
735 gold_assert(oldsym != NULL);
736 }
737 else
738 {
739 // We haven't seen this symbol before.
740 gold_assert(ins.first->second == NULL);
741 add_to_table = true;
742 add_loc = ins.first;
743 oldsym = NULL;
744 }
745 }
746
747 if (!target->has_make_symbol())
748 sym = new Sized_symbol<size>();
749 else
750 {
751 gold_assert(target->get_size() == size);
752 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
753 typedef Sized_target<size, big_endian> My_target;
754 const My_target* sized_target =
755 static_cast<const My_target*>(target);
756 sym = sized_target->make_symbol();
757 if (sym == NULL)
758 return NULL;
759 }
760
761 if (add_to_table)
762 add_loc->second = sym;
763 else
764 gold_assert(oldsym != NULL);
765
766 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
767 SELECT_SIZE(size));
768
769 return sym;
770 }
771
772 // Define a symbol based on an Output_data.
773
774 Symbol*
775 Symbol_table::define_in_output_data(const Target* target, const char* name,
776 const char* version, Output_data* od,
777 uint64_t value, uint64_t symsize,
778 elfcpp::STT type, elfcpp::STB binding,
779 elfcpp::STV visibility,
780 unsigned char nonvis,
781 bool offset_is_from_end,
782 bool only_if_ref)
783 {
784 gold_assert(target->get_size() == this->size_);
785 if (this->size_ == 32)
786 {
787 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
788 return this->do_define_in_output_data<32>(target, name, version, od,
789 value, symsize, type, binding,
790 visibility, nonvis,
791 offset_is_from_end,
792 only_if_ref);
793 #else
794 gold_unreachable();
795 #endif
796 }
797 else if (this->size_ == 64)
798 {
799 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
800 return this->do_define_in_output_data<64>(target, name, version, od,
801 value, symsize, type, binding,
802 visibility, nonvis,
803 offset_is_from_end,
804 only_if_ref);
805 #else
806 gold_unreachable();
807 #endif
808 }
809 else
810 gold_unreachable();
811 }
812
813 // Define a symbol in an Output_data, sized version.
814
815 template<int size>
816 Sized_symbol<size>*
817 Symbol_table::do_define_in_output_data(
818 const Target* target,
819 const char* name,
820 const char* version,
821 Output_data* od,
822 typename elfcpp::Elf_types<size>::Elf_Addr value,
823 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
824 elfcpp::STT type,
825 elfcpp::STB binding,
826 elfcpp::STV visibility,
827 unsigned char nonvis,
828 bool offset_is_from_end,
829 bool only_if_ref)
830 {
831 Sized_symbol<size>* sym;
832 Sized_symbol<size>* oldsym;
833
834 if (target->is_big_endian())
835 {
836 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
837 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
838 target, &name, &version, only_if_ref, &oldsym
839 SELECT_SIZE_ENDIAN(size, true));
840 #else
841 gold_unreachable();
842 #endif
843 }
844 else
845 {
846 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
847 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
848 target, &name, &version, only_if_ref, &oldsym
849 SELECT_SIZE_ENDIAN(size, false));
850 #else
851 gold_unreachable();
852 #endif
853 }
854
855 if (sym == NULL)
856 return NULL;
857
858 gold_assert(version == NULL || oldsym != NULL);
859 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
860 offset_is_from_end);
861
862 if (oldsym != NULL
863 && Symbol_table::should_override_with_special(oldsym))
864 oldsym->override_with_special(sym);
865
866 return sym;
867 }
868
869 // Define a symbol based on an Output_segment.
870
871 Symbol*
872 Symbol_table::define_in_output_segment(const Target* target, const char* name,
873 const char* version, Output_segment* os,
874 uint64_t value, uint64_t symsize,
875 elfcpp::STT type, elfcpp::STB binding,
876 elfcpp::STV visibility,
877 unsigned char nonvis,
878 Symbol::Segment_offset_base offset_base,
879 bool only_if_ref)
880 {
881 gold_assert(target->get_size() == this->size_);
882 if (this->size_ == 32)
883 {
884 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
885 return this->do_define_in_output_segment<32>(target, name, version, os,
886 value, symsize, type,
887 binding, visibility, nonvis,
888 offset_base, only_if_ref);
889 #else
890 gold_unreachable();
891 #endif
892 }
893 else if (this->size_ == 64)
894 {
895 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
896 return this->do_define_in_output_segment<64>(target, name, version, os,
897 value, symsize, type,
898 binding, visibility, nonvis,
899 offset_base, only_if_ref);
900 #else
901 gold_unreachable();
902 #endif
903 }
904 else
905 gold_unreachable();
906 }
907
908 // Define a symbol in an Output_segment, sized version.
909
910 template<int size>
911 Sized_symbol<size>*
912 Symbol_table::do_define_in_output_segment(
913 const Target* target,
914 const char* name,
915 const char* version,
916 Output_segment* os,
917 typename elfcpp::Elf_types<size>::Elf_Addr value,
918 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
919 elfcpp::STT type,
920 elfcpp::STB binding,
921 elfcpp::STV visibility,
922 unsigned char nonvis,
923 Symbol::Segment_offset_base offset_base,
924 bool only_if_ref)
925 {
926 Sized_symbol<size>* sym;
927 Sized_symbol<size>* oldsym;
928
929 if (target->is_big_endian())
930 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
931 target, &name, &version, only_if_ref, &oldsym
932 SELECT_SIZE_ENDIAN(size, true));
933 else
934 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
935 target, &name, &version, only_if_ref, &oldsym
936 SELECT_SIZE_ENDIAN(size, false));
937
938 if (sym == NULL)
939 return NULL;
940
941 gold_assert(version == NULL || oldsym != NULL);
942 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
943 offset_base);
944
945 if (oldsym != NULL
946 && Symbol_table::should_override_with_special(oldsym))
947 oldsym->override_with_special(sym);
948
949 return sym;
950 }
951
952 // Define a special symbol with a constant value. It is a multiple
953 // definition error if this symbol is already defined.
954
955 Symbol*
956 Symbol_table::define_as_constant(const Target* target, const char* name,
957 const char* version, uint64_t value,
958 uint64_t symsize, elfcpp::STT type,
959 elfcpp::STB binding, elfcpp::STV visibility,
960 unsigned char nonvis, bool only_if_ref)
961 {
962 gold_assert(target->get_size() == this->size_);
963 if (this->size_ == 32)
964 {
965 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
966 return this->do_define_as_constant<32>(target, name, version, value,
967 symsize, type, binding,
968 visibility, nonvis, only_if_ref);
969 #else
970 gold_unreachable();
971 #endif
972 }
973 else if (this->size_ == 64)
974 {
975 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
976 return this->do_define_as_constant<64>(target, name, version, value,
977 symsize, type, binding,
978 visibility, nonvis, only_if_ref);
979 #else
980 gold_unreachable();
981 #endif
982 }
983 else
984 gold_unreachable();
985 }
986
987 // Define a symbol as a constant, sized version.
988
989 template<int size>
990 Sized_symbol<size>*
991 Symbol_table::do_define_as_constant(
992 const Target* target,
993 const char* name,
994 const char* version,
995 typename elfcpp::Elf_types<size>::Elf_Addr value,
996 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
997 elfcpp::STT type,
998 elfcpp::STB binding,
999 elfcpp::STV visibility,
1000 unsigned char nonvis,
1001 bool only_if_ref)
1002 {
1003 Sized_symbol<size>* sym;
1004 Sized_symbol<size>* oldsym;
1005
1006 if (target->is_big_endian())
1007 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1008 target, &name, &version, only_if_ref, &oldsym
1009 SELECT_SIZE_ENDIAN(size, true));
1010 else
1011 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1012 target, &name, &version, only_if_ref, &oldsym
1013 SELECT_SIZE_ENDIAN(size, false));
1014
1015 if (sym == NULL)
1016 return NULL;
1017
1018 gold_assert(version == NULL || oldsym != NULL);
1019 sym->init(name, value, symsize, type, binding, visibility, nonvis);
1020
1021 if (oldsym != NULL
1022 && Symbol_table::should_override_with_special(oldsym))
1023 oldsym->override_with_special(sym);
1024
1025 return sym;
1026 }
1027
1028 // Define a set of symbols in output sections.
1029
1030 void
1031 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1032 int count, const Define_symbol_in_section* p)
1033 {
1034 for (int i = 0; i < count; ++i, ++p)
1035 {
1036 Output_section* os = layout->find_output_section(p->output_section);
1037 if (os != NULL)
1038 this->define_in_output_data(target, p->name, NULL, os, p->value,
1039 p->size, p->type, p->binding,
1040 p->visibility, p->nonvis,
1041 p->offset_is_from_end, p->only_if_ref);
1042 else
1043 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1044 p->binding, p->visibility, p->nonvis,
1045 p->only_if_ref);
1046 }
1047 }
1048
1049 // Define a set of symbols in output segments.
1050
1051 void
1052 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1053 int count, const Define_symbol_in_segment* p)
1054 {
1055 for (int i = 0; i < count; ++i, ++p)
1056 {
1057 Output_segment* os = layout->find_output_segment(p->segment_type,
1058 p->segment_flags_set,
1059 p->segment_flags_clear);
1060 if (os != NULL)
1061 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1062 p->size, p->type, p->binding,
1063 p->visibility, p->nonvis,
1064 p->offset_base, p->only_if_ref);
1065 else
1066 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1067 p->binding, p->visibility, p->nonvis,
1068 p->only_if_ref);
1069 }
1070 }
1071
1072 // Set the dynamic symbol indexes. INDEX is the index of the first
1073 // global dynamic symbol. Pointers to the symbols are stored into the
1074 // vector SYMS. The names are added to DYNPOOL. This returns an
1075 // updated dynamic symbol index.
1076
1077 unsigned int
1078 Symbol_table::set_dynsym_indexes(const General_options* options,
1079 const Target* target,
1080 unsigned int index,
1081 std::vector<Symbol*>* syms,
1082 Stringpool* dynpool,
1083 Versions* versions)
1084 {
1085 for (Symbol_table_type::iterator p = this->table_.begin();
1086 p != this->table_.end();
1087 ++p)
1088 {
1089 Symbol* sym = p->second;
1090
1091 // Note that SYM may already have a dynamic symbol index, since
1092 // some symbols appear more than once in the symbol table, with
1093 // and without a version.
1094
1095 if (!sym->needs_dynsym_entry()
1096 && (!options->export_dynamic()
1097 || !sym->in_reg()
1098 || !sym->is_externally_visible()))
1099 sym->set_dynsym_index(-1U);
1100 else if (!sym->has_dynsym_index())
1101 {
1102 sym->set_dynsym_index(index);
1103 ++index;
1104 syms->push_back(sym);
1105 dynpool->add(sym->name(), NULL);
1106
1107 // Record any version information.
1108 if (sym->version() != NULL)
1109 versions->record_version(options, dynpool, sym);
1110 }
1111 }
1112
1113 // Finish up the versions. In some cases this may add new dynamic
1114 // symbols.
1115 index = versions->finalize(target, this, index, syms);
1116
1117 return index;
1118 }
1119
1120 // Set the final values for all the symbols. The index of the first
1121 // global symbol in the output file is INDEX. Record the file offset
1122 // OFF. Add their names to POOL. Return the new file offset.
1123
1124 off_t
1125 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1126 size_t dyn_global_index, size_t dyncount,
1127 Stringpool* pool)
1128 {
1129 off_t ret;
1130
1131 gold_assert(index != 0);
1132 this->first_global_index_ = index;
1133
1134 this->dynamic_offset_ = dynoff;
1135 this->first_dynamic_global_index_ = dyn_global_index;
1136 this->dynamic_count_ = dyncount;
1137
1138 if (this->size_ == 32)
1139 ret = this->sized_finalize<32>(index, off, pool);
1140 else if (this->size_ == 64)
1141 ret = this->sized_finalize<64>(index, off, pool);
1142 else
1143 gold_unreachable();
1144
1145 // Now that we have the final symbol table, we can reliably note
1146 // which symbols should get warnings.
1147 this->warnings_.note_warnings(this);
1148
1149 return ret;
1150 }
1151
1152 // Set the final value for all the symbols. This is called after
1153 // Layout::finalize, so all the output sections have their final
1154 // address.
1155
1156 template<int size>
1157 off_t
1158 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1159 {
1160 off = align_address(off, size >> 3);
1161 this->offset_ = off;
1162
1163 size_t orig_index = index;
1164
1165 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1166 for (Symbol_table_type::iterator p = this->table_.begin();
1167 p != this->table_.end();
1168 ++p)
1169 {
1170 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1171
1172 // FIXME: Here we need to decide which symbols should go into
1173 // the output file, based on --strip.
1174
1175 // The default version of a symbol may appear twice in the
1176 // symbol table. We only need to finalize it once.
1177 if (sym->has_symtab_index())
1178 continue;
1179
1180 if (!sym->in_reg())
1181 {
1182 gold_assert(!sym->has_symtab_index());
1183 sym->set_symtab_index(-1U);
1184 gold_assert(sym->dynsym_index() == -1U);
1185 continue;
1186 }
1187
1188 typename Sized_symbol<size>::Value_type value;
1189
1190 switch (sym->source())
1191 {
1192 case Symbol::FROM_OBJECT:
1193 {
1194 unsigned int shndx = sym->shndx();
1195
1196 // FIXME: We need some target specific support here.
1197 if (shndx >= elfcpp::SHN_LORESERVE
1198 && shndx != elfcpp::SHN_ABS)
1199 {
1200 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1201 program_name, sym->name(), shndx);
1202 gold_exit(false);
1203 }
1204
1205 Object* symobj = sym->object();
1206 if (symobj->is_dynamic())
1207 {
1208 value = 0;
1209 shndx = elfcpp::SHN_UNDEF;
1210 }
1211 else if (shndx == elfcpp::SHN_UNDEF)
1212 value = 0;
1213 else if (shndx == elfcpp::SHN_ABS)
1214 value = sym->value();
1215 else
1216 {
1217 Relobj* relobj = static_cast<Relobj*>(symobj);
1218 off_t secoff;
1219 Output_section* os = relobj->output_section(shndx, &secoff);
1220
1221 if (os == NULL)
1222 {
1223 sym->set_symtab_index(-1U);
1224 gold_assert(sym->dynsym_index() == -1U);
1225 continue;
1226 }
1227
1228 value = sym->value() + os->address() + secoff;
1229 }
1230 }
1231 break;
1232
1233 case Symbol::IN_OUTPUT_DATA:
1234 {
1235 Output_data* od = sym->output_data();
1236 value = sym->value() + od->address();
1237 if (sym->offset_is_from_end())
1238 value += od->data_size();
1239 }
1240 break;
1241
1242 case Symbol::IN_OUTPUT_SEGMENT:
1243 {
1244 Output_segment* os = sym->output_segment();
1245 value = sym->value() + os->vaddr();
1246 switch (sym->offset_base())
1247 {
1248 case Symbol::SEGMENT_START:
1249 break;
1250 case Symbol::SEGMENT_END:
1251 value += os->memsz();
1252 break;
1253 case Symbol::SEGMENT_BSS:
1254 value += os->filesz();
1255 break;
1256 default:
1257 gold_unreachable();
1258 }
1259 }
1260 break;
1261
1262 case Symbol::CONSTANT:
1263 value = sym->value();
1264 break;
1265
1266 default:
1267 gold_unreachable();
1268 }
1269
1270 sym->set_value(value);
1271 sym->set_symtab_index(index);
1272 pool->add(sym->name(), NULL);
1273 ++index;
1274 off += sym_size;
1275 }
1276
1277 this->output_count_ = index - orig_index;
1278
1279 return off;
1280 }
1281
1282 // Write out the global symbols.
1283
1284 void
1285 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1286 const Stringpool* dynpool, Output_file* of) const
1287 {
1288 if (this->size_ == 32)
1289 {
1290 if (target->is_big_endian())
1291 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1292 else
1293 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1294 }
1295 else if (this->size_ == 64)
1296 {
1297 if (target->is_big_endian())
1298 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1299 else
1300 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1301 }
1302 else
1303 gold_unreachable();
1304 }
1305
1306 // Write out the global symbols.
1307
1308 template<int size, bool big_endian>
1309 void
1310 Symbol_table::sized_write_globals(const Target* target,
1311 const Stringpool* sympool,
1312 const Stringpool* dynpool,
1313 Output_file* of) const
1314 {
1315 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1316 unsigned int index = this->first_global_index_;
1317 const off_t oview_size = this->output_count_ * sym_size;
1318 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1319
1320 unsigned int dynamic_count = this->dynamic_count_;
1321 off_t dynamic_size = dynamic_count * sym_size;
1322 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1323 unsigned char* dynamic_view;
1324 if (this->dynamic_offset_ == 0)
1325 dynamic_view = NULL;
1326 else
1327 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1328
1329 unsigned char* ps = psyms;
1330 for (Symbol_table_type::const_iterator p = this->table_.begin();
1331 p != this->table_.end();
1332 ++p)
1333 {
1334 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1335
1336 unsigned int sym_index = sym->symtab_index();
1337 unsigned int dynsym_index;
1338 if (dynamic_view == NULL)
1339 dynsym_index = -1U;
1340 else
1341 dynsym_index = sym->dynsym_index();
1342
1343 if (sym_index == -1U && dynsym_index == -1U)
1344 {
1345 // This symbol is not included in the output file.
1346 continue;
1347 }
1348
1349 if (sym_index == index)
1350 ++index;
1351 else if (sym_index != -1U)
1352 {
1353 // We have already seen this symbol, because it has a
1354 // default version.
1355 gold_assert(sym_index < index);
1356 if (dynsym_index == -1U)
1357 continue;
1358 sym_index = -1U;
1359 }
1360
1361 unsigned int shndx;
1362 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1363 switch (sym->source())
1364 {
1365 case Symbol::FROM_OBJECT:
1366 {
1367 unsigned int in_shndx = sym->shndx();
1368
1369 // FIXME: We need some target specific support here.
1370 if (in_shndx >= elfcpp::SHN_LORESERVE
1371 && in_shndx != elfcpp::SHN_ABS)
1372 {
1373 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1374 program_name, sym->name(), in_shndx);
1375 gold_exit(false);
1376 }
1377
1378 Object* symobj = sym->object();
1379 if (symobj->is_dynamic())
1380 {
1381 if (sym->needs_dynsym_value())
1382 value = target->dynsym_value(sym);
1383 shndx = elfcpp::SHN_UNDEF;
1384 }
1385 else if (in_shndx == elfcpp::SHN_UNDEF
1386 || in_shndx == elfcpp::SHN_ABS)
1387 shndx = in_shndx;
1388 else
1389 {
1390 Relobj* relobj = static_cast<Relobj*>(symobj);
1391 off_t secoff;
1392 Output_section* os = relobj->output_section(in_shndx, &secoff);
1393 gold_assert(os != NULL);
1394 shndx = os->out_shndx();
1395 }
1396 }
1397 break;
1398
1399 case Symbol::IN_OUTPUT_DATA:
1400 shndx = sym->output_data()->out_shndx();
1401 break;
1402
1403 case Symbol::IN_OUTPUT_SEGMENT:
1404 shndx = elfcpp::SHN_ABS;
1405 break;
1406
1407 case Symbol::CONSTANT:
1408 shndx = elfcpp::SHN_ABS;
1409 break;
1410
1411 default:
1412 gold_unreachable();
1413 }
1414
1415 if (sym_index != -1U)
1416 {
1417 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1418 sym, sym->value(), shndx, sympool, ps
1419 SELECT_SIZE_ENDIAN(size, big_endian));
1420 ps += sym_size;
1421 }
1422
1423 if (dynsym_index != -1U)
1424 {
1425 dynsym_index -= first_dynamic_global_index;
1426 gold_assert(dynsym_index < dynamic_count);
1427 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1428 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1429 sym, value, shndx, dynpool, pd
1430 SELECT_SIZE_ENDIAN(size, big_endian));
1431 }
1432 }
1433
1434 gold_assert(ps - psyms == oview_size);
1435
1436 of->write_output_view(this->offset_, oview_size, psyms);
1437 if (dynamic_view != NULL)
1438 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1439 }
1440
1441 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
1442 // strtab holding the name.
1443
1444 template<int size, bool big_endian>
1445 void
1446 Symbol_table::sized_write_symbol(
1447 Sized_symbol<size>* sym,
1448 typename elfcpp::Elf_types<size>::Elf_Addr value,
1449 unsigned int shndx,
1450 const Stringpool* pool,
1451 unsigned char* p
1452 ACCEPT_SIZE_ENDIAN) const
1453 {
1454 elfcpp::Sym_write<size, big_endian> osym(p);
1455 osym.put_st_name(pool->get_offset(sym->name()));
1456 osym.put_st_value(value);
1457 osym.put_st_size(sym->symsize());
1458 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1459 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1460 osym.put_st_shndx(shndx);
1461 }
1462
1463 // Write out a section symbol. Return the update offset.
1464
1465 void
1466 Symbol_table::write_section_symbol(const Target* target,
1467 const Output_section *os,
1468 Output_file* of,
1469 off_t offset) const
1470 {
1471 if (this->size_ == 32)
1472 {
1473 if (target->is_big_endian())
1474 this->sized_write_section_symbol<32, true>(os, of, offset);
1475 else
1476 this->sized_write_section_symbol<32, false>(os, of, offset);
1477 }
1478 else if (this->size_ == 64)
1479 {
1480 if (target->is_big_endian())
1481 this->sized_write_section_symbol<64, true>(os, of, offset);
1482 else
1483 this->sized_write_section_symbol<64, false>(os, of, offset);
1484 }
1485 else
1486 gold_unreachable();
1487 }
1488
1489 // Write out a section symbol, specialized for size and endianness.
1490
1491 template<int size, bool big_endian>
1492 void
1493 Symbol_table::sized_write_section_symbol(const Output_section* os,
1494 Output_file* of,
1495 off_t offset) const
1496 {
1497 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1498
1499 unsigned char* pov = of->get_output_view(offset, sym_size);
1500
1501 elfcpp::Sym_write<size, big_endian> osym(pov);
1502 osym.put_st_name(0);
1503 osym.put_st_value(os->address());
1504 osym.put_st_size(0);
1505 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1506 elfcpp::STT_SECTION));
1507 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1508 osym.put_st_shndx(os->out_shndx());
1509
1510 of->write_output_view(offset, sym_size, pov);
1511 }
1512
1513 // Warnings functions.
1514
1515 // Add a new warning.
1516
1517 void
1518 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1519 unsigned int shndx)
1520 {
1521 name = symtab->canonicalize_name(name);
1522 this->warnings_[name].set(obj, shndx);
1523 }
1524
1525 // Look through the warnings and mark the symbols for which we should
1526 // warn. This is called during Layout::finalize when we know the
1527 // sources for all the symbols.
1528
1529 void
1530 Warnings::note_warnings(Symbol_table* symtab)
1531 {
1532 for (Warning_table::iterator p = this->warnings_.begin();
1533 p != this->warnings_.end();
1534 ++p)
1535 {
1536 Symbol* sym = symtab->lookup(p->first, NULL);
1537 if (sym != NULL
1538 && sym->source() == Symbol::FROM_OBJECT
1539 && sym->object() == p->second.object)
1540 {
1541 sym->set_has_warning();
1542
1543 // Read the section contents to get the warning text. It
1544 // would be nicer if we only did this if we have to actually
1545 // issue a warning. Unfortunately, warnings are issued as
1546 // we relocate sections. That means that we can not lock
1547 // the object then, as we might try to issue the same
1548 // warning multiple times simultaneously.
1549 {
1550 Task_locker_obj<Object> tl(*p->second.object);
1551 const unsigned char* c;
1552 off_t len;
1553 c = p->second.object->section_contents(p->second.shndx, &len,
1554 false);
1555 p->second.set_text(reinterpret_cast<const char*>(c), len);
1556 }
1557 }
1558 }
1559 }
1560
1561 // Issue a warning. This is called when we see a relocation against a
1562 // symbol for which has a warning.
1563
1564 void
1565 Warnings::issue_warning(const Symbol* sym, const std::string& location) const
1566 {
1567 gold_assert(sym->has_warning());
1568 Warning_table::const_iterator p = this->warnings_.find(sym->name());
1569 gold_assert(p != this->warnings_.end());
1570 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1571 p->second.text.c_str());
1572 }
1573
1574 // Instantiate the templates we need. We could use the configure
1575 // script to restrict this to only the ones needed for implemented
1576 // targets.
1577
1578 #ifdef HAVE_TARGET_32_LITTLE
1579 template
1580 void
1581 Symbol_table::add_from_relobj<32, false>(
1582 Sized_relobj<32, false>* relobj,
1583 const unsigned char* syms,
1584 size_t count,
1585 const char* sym_names,
1586 size_t sym_name_size,
1587 Symbol** sympointers);
1588 #endif
1589
1590 #ifdef HAVE_TARGET_32_BIG
1591 template
1592 void
1593 Symbol_table::add_from_relobj<32, true>(
1594 Sized_relobj<32, true>* relobj,
1595 const unsigned char* syms,
1596 size_t count,
1597 const char* sym_names,
1598 size_t sym_name_size,
1599 Symbol** sympointers);
1600 #endif
1601
1602 #ifdef HAVE_TARGET_64_LITTLE
1603 template
1604 void
1605 Symbol_table::add_from_relobj<64, false>(
1606 Sized_relobj<64, false>* relobj,
1607 const unsigned char* syms,
1608 size_t count,
1609 const char* sym_names,
1610 size_t sym_name_size,
1611 Symbol** sympointers);
1612 #endif
1613
1614 #ifdef HAVE_TARGET_64_BIG
1615 template
1616 void
1617 Symbol_table::add_from_relobj<64, true>(
1618 Sized_relobj<64, true>* relobj,
1619 const unsigned char* syms,
1620 size_t count,
1621 const char* sym_names,
1622 size_t sym_name_size,
1623 Symbol** sympointers);
1624 #endif
1625
1626 #ifdef HAVE_TARGET_32_LITTLE
1627 template
1628 void
1629 Symbol_table::add_from_dynobj<32, false>(
1630 Sized_dynobj<32, false>* dynobj,
1631 const unsigned char* syms,
1632 size_t count,
1633 const char* sym_names,
1634 size_t sym_name_size,
1635 const unsigned char* versym,
1636 size_t versym_size,
1637 const std::vector<const char*>* version_map);
1638 #endif
1639
1640 #ifdef HAVE_TARGET_32_BIG
1641 template
1642 void
1643 Symbol_table::add_from_dynobj<32, true>(
1644 Sized_dynobj<32, true>* dynobj,
1645 const unsigned char* syms,
1646 size_t count,
1647 const char* sym_names,
1648 size_t sym_name_size,
1649 const unsigned char* versym,
1650 size_t versym_size,
1651 const std::vector<const char*>* version_map);
1652 #endif
1653
1654 #ifdef HAVE_TARGET_64_LITTLE
1655 template
1656 void
1657 Symbol_table::add_from_dynobj<64, false>(
1658 Sized_dynobj<64, false>* dynobj,
1659 const unsigned char* syms,
1660 size_t count,
1661 const char* sym_names,
1662 size_t sym_name_size,
1663 const unsigned char* versym,
1664 size_t versym_size,
1665 const std::vector<const char*>* version_map);
1666 #endif
1667
1668 #ifdef HAVE_TARGET_64_BIG
1669 template
1670 void
1671 Symbol_table::add_from_dynobj<64, true>(
1672 Sized_dynobj<64, true>* dynobj,
1673 const unsigned char* syms,
1674 size_t count,
1675 const char* sym_names,
1676 size_t sym_name_size,
1677 const unsigned char* versym,
1678 size_t versym_size,
1679 const std::vector<const char*>* version_map);
1680 #endif
1681
1682 } // End namespace gold.