]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
gas/
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
6cb15b7f
ILT
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
14bfc3f5
ILT
23#include "gold.h"
24
14bfc3f5
ILT
25#include <stdint.h>
26#include <string>
27#include <utility>
28
29#include "object.h"
dbe717ef 30#include "dynobj.h"
75f65a3e 31#include "output.h"
61ba1cf9 32#include "target.h"
645f8123 33#include "workqueue.h"
14bfc3f5
ILT
34#include "symtab.h"
35
36namespace gold
37{
38
39// Class Symbol.
40
ead1e424
ILT
41// Initialize fields in Symbol. This initializes everything except u_
42// and source_.
14bfc3f5 43
14bfc3f5 44void
ead1e424
ILT
45Symbol::init_fields(const char* name, const char* version,
46 elfcpp::STT type, elfcpp::STB binding,
47 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
48{
49 this->name_ = name;
50 this->version_ = version;
c06b7b0b
ILT
51 this->symtab_index_ = 0;
52 this->dynsym_index_ = 0;
ead1e424 53 this->got_offset_ = 0;
f4151f89 54 this->plt_offset_ = 0;
ead1e424
ILT
55 this->type_ = type;
56 this->binding_ = binding;
57 this->visibility_ = visibility;
58 this->nonvis_ = nonvis;
59 this->is_target_special_ = false;
1564db8d
ILT
60 this->is_def_ = false;
61 this->is_forwarder_ = false;
c06b7b0b 62 this->needs_dynsym_entry_ = false;
008db82e 63 this->in_reg_ = false;
ead1e424
ILT
64 this->in_dyn_ = false;
65 this->has_got_offset_ = false;
f4151f89 66 this->has_plt_offset_ = false;
f6ce93d6 67 this->has_warning_ = false;
ead1e424
ILT
68}
69
70// Initialize the fields in the base class Symbol for SYM in OBJECT.
71
72template<int size, bool big_endian>
73void
74Symbol::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.
16649710 81 this->u_.from_object.shndx = sym.get_st_shndx();
ead1e424 82 this->source_ = FROM_OBJECT;
008db82e 83 this->in_reg_ = !object->is_dynamic();
1564db8d 84 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
85}
86
ead1e424
ILT
87// Initialize the fields in the base class Symbol for a symbol defined
88// in an Output_data.
89
90void
91Symbol::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;
008db82e 99 this->in_reg_ = true;
ead1e424
ILT
100}
101
102// Initialize the fields in the base class Symbol for a symbol defined
103// in an Output_segment.
104
105void
106Symbol::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;
008db82e 114 this->in_reg_ = true;
ead1e424
ILT
115}
116
117// Initialize the fields in the base class Symbol for a symbol defined
118// as a constant.
119
120void
121Symbol::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;
008db82e 127 this->in_reg_ = true;
ead1e424
ILT
128}
129
130// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
131
132template<int size>
133template<bool big_endian>
134void
135Sized_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();
ead1e424
ILT
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
146template<int size>
147void
148Sized_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
163template<int size>
164void
165Sized_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
179template<int size>
180void
181Sized_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;
14bfc3f5
ILT
188}
189
190// Class Symbol_table.
191
192Symbol_table::Symbol_table()
ead1e424 193 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
f6ce93d6 194 forwarders_(), commons_(), warnings_()
14bfc3f5
ILT
195{
196}
197
198Symbol_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
205size_t
206Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
207{
f0641a0b 208 return key.first ^ key.second;
14bfc3f5
ILT
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
215bool
216Symbol_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
224void
225Symbol_table::make_forwarder(Symbol* from, Symbol* to)
226{
a3ad94ed
ILT
227 gold_assert(from != to);
228 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
229 this->forwarders_[from] = to;
230 from->set_forwarder();
231}
232
61ba1cf9
ILT
233// Resolve the forwards from FROM, returning the real symbol.
234
14bfc3f5 235Symbol*
c06b7b0b 236Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 237{
a3ad94ed 238 gold_assert(from->is_forwarder());
c06b7b0b 239 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 240 this->forwarders_.find(from);
a3ad94ed 241 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
242 return p->second;
243}
244
61ba1cf9
ILT
245// Look up a symbol by name.
246
247Symbol*
248Symbol_table::lookup(const char* name, const char* version) const
249{
f0641a0b
ILT
250 Stringpool::Key name_key;
251 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
252 if (name == NULL)
253 return NULL;
f0641a0b
ILT
254
255 Stringpool::Key version_key = 0;
61ba1cf9
ILT
256 if (version != NULL)
257 {
f0641a0b 258 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
259 if (version == NULL)
260 return NULL;
261 }
262
f0641a0b 263 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
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
14bfc3f5
ILT
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
1564db8d
ILT
273// version is the default version. Because this is unusual, we do
274// this the slow way, by converting back to an ELF symbol.
14bfc3f5 275
1564db8d 276template<int size, bool big_endian>
14bfc3f5 277void
14b31740
ILT
278Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
279 const char* version ACCEPT_SIZE_ENDIAN)
14bfc3f5 280{
1564db8d
ILT
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());
ead1e424 287 esym.put_st_other(from->visibility(), from->nonvis());
16649710 288 esym.put_st_shndx(from->shndx());
14b31740 289 Symbol_table::resolve(to, esym.sym(), from->object(), version);
1ebd95fd
ILT
290 if (from->in_reg())
291 to->set_in_reg();
292 if (from->in_dyn())
293 to->set_in_dyn();
14bfc3f5
ILT
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
319template<int size, bool big_endian>
320Symbol*
f6ce93d6 321Symbol_table::add_from_object(Object* object,
14bfc3f5 322 const char *name,
f0641a0b
ILT
323 Stringpool::Key name_key,
324 const char *version,
325 Stringpool::Key version_key,
326 bool def,
14bfc3f5
ILT
327 const elfcpp::Sym<size, big_endian>& sym)
328{
329 Symbol* const snull = NULL;
330 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
331 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
332 snull));
14bfc3f5
ILT
333
334 std::pair<typename Symbol_table_type::iterator, bool> insdef =
335 std::make_pair(this->table_.end(), false);
336 if (def)
337 {
f0641a0b
ILT
338 const Stringpool::Key vnull_key = 0;
339 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
340 vnull_key),
14bfc3f5
ILT
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
1564db8d 349 Sized_symbol<size>* ret;
ead1e424
ILT
350 bool was_undefined;
351 bool was_common;
14bfc3f5
ILT
352 if (!ins.second)
353 {
354 // We already have an entry for NAME/VERSION.
593f47df
ILT
355 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
356 SELECT_SIZE(size));
a3ad94ed 357 gold_assert(ret != NULL);
ead1e424
ILT
358
359 was_undefined = ret->is_undefined();
360 was_common = ret->is_common();
361
14b31740 362 Symbol_table::resolve(ret, sym, object, version);
14bfc3f5
ILT
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 }
dbe717ef 372 else if (insdef.first->second != ret)
14bfc3f5
ILT
373 {
374 // This is the unfortunate case where we already have
375 // entries for both NAME/VERSION and NAME/NULL.
274e99f9 376 const Sized_symbol<size>* sym2;
593f47df 377 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d
ILT
378 insdef.first->second
379 SELECT_SIZE(size));
593f47df 380 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
14b31740 381 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
14bfc3f5
ILT
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.
a3ad94ed 390 gold_assert(ins.first->second == NULL);
ead1e424
ILT
391
392 was_undefined = false;
393 was_common = false;
394
14bfc3f5
ILT
395 if (def && !insdef.second)
396 {
14b31740
ILT
397 // We already have an entry for NAME/NULL. If we override
398 // it, then change it to NAME/VERSION.
593f47df
ILT
399 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
400 insdef.first->second
401 SELECT_SIZE(size));
14b31740 402 Symbol_table::resolve(ret, sym, object, version);
14bfc3f5
ILT
403 ins.first->second = ret;
404 }
405 else
406 {
f6ce93d6
ILT
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));
1564db8d
ILT
410 if (!target->has_make_symbol())
411 ret = new Sized_symbol<size>();
412 else
14bfc3f5 413 {
1564db8d
ILT
414 ret = target->make_symbol();
415 if (ret == NULL)
14bfc3f5
ILT
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.
f0641a0b
ILT
425 this->table_.erase(std::make_pair(name_key,
426 version_key));
14bfc3f5
ILT
427 }
428 return NULL;
429 }
430 }
14bfc3f5 431
1564db8d
ILT
432 ret->init(name, version, object, sym);
433
14bfc3f5
ILT
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.
a3ad94ed 439 gold_assert(insdef.second);
14bfc3f5
ILT
440 insdef.first->second = ret;
441 }
442 }
443 }
444
ead1e424
ILT
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
14bfc3f5
ILT
455 return ret;
456}
457
f6ce93d6 458// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
459
460template<int size, bool big_endian>
461void
dbe717ef
ILT
462Symbol_table::add_from_relobj(
463 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 464 const unsigned char* syms,
14bfc3f5
ILT
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
dbe717ef 474 if (size != this->get_size() || size != relobj->target()->get_size())
14bfc3f5
ILT
475 {
476 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
dbe717ef 477 program_name, relobj->name().c_str());
14bfc3f5
ILT
478 gold_exit(false);
479 }
480
a783673b
ILT
481 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
482
f6ce93d6 483 const unsigned char* p = syms;
a783673b 484 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
485 {
486 elfcpp::Sym<size, big_endian> sym(p);
a783673b 487 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 488
a783673b 489 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
490 if (st_name >= sym_name_size)
491 {
54dc6425
ILT
492 fprintf(stderr,
493 _("%s: %s: bad global symbol name offset %u at %lu\n"),
dbe717ef 494 program_name, relobj->name().c_str(), st_name,
14bfc3f5
ILT
495 static_cast<unsigned long>(i));
496 gold_exit(false);
497 }
498
dbe717ef
ILT
499 const char* name = sym_names + st_name;
500
a783673b
ILT
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
dbe717ef 508 && !relobj->is_section_included(st_shndx))
a783673b
ILT
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
14bfc3f5
ILT
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 {
f0641a0b
ILT
524 Stringpool::Key name_key;
525 name = this->namepool_.add(name, &name_key);
dbe717ef 526 res = this->add_from_object(relobj, name, name_key, NULL, 0,
f0641a0b 527 false, *psym);
14bfc3f5
ILT
528 }
529 else
530 {
f0641a0b
ILT
531 Stringpool::Key name_key;
532 name = this->namepool_.add(name, ver - name, &name_key);
533
14bfc3f5
ILT
534 bool def = false;
535 ++ver;
536 if (*ver == '@')
537 {
538 def = true;
539 ++ver;
540 }
f0641a0b
ILT
541
542 Stringpool::Key ver_key;
543 ver = this->namepool_.add(ver, &ver_key);
544
dbe717ef 545 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
f0641a0b 546 def, *psym);
14bfc3f5
ILT
547 }
548
549 *sympointers++ = res;
14bfc3f5
ILT
550 }
551}
552
dbe717ef
ILT
553// Add all the symbols in a dynamic object to the hash table.
554
555template<int size, bool big_endian>
556void
557Symbol_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
64707334
ILT
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)
dbe717ef
ILT
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
64707334
ILT
642 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
643 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
dbe717ef
ILT
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
ead1e424
ILT
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.
86f2e683 689// If this does not return NULL, it sets *POLDSYM to the existing
306d9ef0 690// symbol if there is one. This canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
691
692template<int size, bool big_endian>
693Sized_symbol<size>*
306d9ef0
ILT
694Symbol_table::define_special_symbol(const Target* target, const char** pname,
695 const char** pversion, bool only_if_ref,
86f2e683 696 Sized_symbol<size>** poldsym
593f47df 697 ACCEPT_SIZE_ENDIAN)
ead1e424 698{
a3ad94ed 699 gold_assert(this->size_ == size);
ead1e424
ILT
700
701 Symbol* oldsym;
702 Sized_symbol<size>* sym;
86f2e683
ILT
703 bool add_to_table = false;
704 typename Symbol_table_type::iterator add_loc = this->table_.end();
ead1e424
ILT
705
706 if (only_if_ref)
707 {
306d9ef0 708 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 709 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 710 return NULL;
306d9ef0
ILT
711
712 *pname = oldsym->name();
713 *pversion = oldsym->version();
ead1e424
ILT
714 }
715 else
716 {
14b31740 717 // Canonicalize NAME and VERSION.
f0641a0b 718 Stringpool::Key name_key;
306d9ef0 719 *pname = this->namepool_.add(*pname, &name_key);
ead1e424 720
14b31740 721 Stringpool::Key version_key = 0;
306d9ef0
ILT
722 if (*pversion != NULL)
723 *pversion = this->namepool_.add(*pversion, &version_key);
14b31740 724
ead1e424 725 Symbol* const snull = NULL;
ead1e424 726 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
727 this->table_.insert(std::make_pair(std::make_pair(name_key,
728 version_key),
ead1e424
ILT
729 snull));
730
731 if (!ins.second)
732 {
14b31740 733 // We already have a symbol table entry for NAME/VERSION.
ead1e424 734 oldsym = ins.first->second;
a3ad94ed 735 gold_assert(oldsym != NULL);
ead1e424
ILT
736 }
737 else
738 {
739 // We haven't seen this symbol before.
a3ad94ed 740 gold_assert(ins.first->second == NULL);
86f2e683
ILT
741 add_to_table = true;
742 add_loc = ins.first;
ead1e424
ILT
743 oldsym = NULL;
744 }
745 }
746
86f2e683
ILT
747 if (!target->has_make_symbol())
748 sym = new Sized_symbol<size>();
749 else
ead1e424 750 {
86f2e683
ILT
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 }
ead1e424 760
86f2e683
ILT
761 if (add_to_table)
762 add_loc->second = sym;
763 else
764 gold_assert(oldsym != NULL);
ead1e424 765
86f2e683
ILT
766 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
767 SELECT_SIZE(size));
ead1e424
ILT
768
769 return sym;
770}
771
772// Define a symbol based on an Output_data.
773
14b31740
ILT
774Symbol*
775Symbol_table::define_in_output_data(const Target* target, const char* name,
776 const char* version, Output_data* od,
ead1e424
ILT
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{
a3ad94ed 784 gold_assert(target->get_size() == this->size_);
ead1e424 785 if (this->size_ == 32)
86f2e683
ILT
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 }
ead1e424 797 else if (this->size_ == 64)
86f2e683
ILT
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 }
ead1e424 809 else
a3ad94ed 810 gold_unreachable();
ead1e424
ILT
811}
812
813// Define a symbol in an Output_data, sized version.
814
815template<int size>
14b31740 816Sized_symbol<size>*
ead1e424 817Symbol_table::do_define_in_output_data(
14b31740 818 const Target* target,
ead1e424 819 const char* name,
14b31740 820 const char* version,
ead1e424
ILT
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;
86f2e683 832 Sized_symbol<size>* oldsym;
ead1e424
ILT
833
834 if (target->is_big_endian())
193a53d9
ILT
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) (
306d9ef0 838 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
839 SELECT_SIZE_ENDIAN(size, true));
840#else
841 gold_unreachable();
842#endif
843 }
ead1e424 844 else
193a53d9
ILT
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) (
306d9ef0 848 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
849 SELECT_SIZE_ENDIAN(size, false));
850#else
851 gold_unreachable();
852#endif
853 }
ead1e424
ILT
854
855 if (sym == NULL)
14b31740 856 return NULL;
ead1e424 857
d4f5281b 858 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
859 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
860 offset_is_from_end);
14b31740 861
86f2e683
ILT
862 if (oldsym != NULL
863 && Symbol_table::should_override_with_special(oldsym))
864 oldsym->override_with_special(sym);
865
14b31740 866 return sym;
ead1e424
ILT
867}
868
869// Define a symbol based on an Output_segment.
870
14b31740
ILT
871Symbol*
872Symbol_table::define_in_output_segment(const Target* target, const char* name,
873 const char* version, Output_segment* os,
ead1e424
ILT
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{
a3ad94ed 881 gold_assert(target->get_size() == this->size_);
ead1e424 882 if (this->size_ == 32)
86f2e683
ILT
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 }
ead1e424 893 else if (this->size_ == 64)
86f2e683
ILT
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 }
ead1e424 904 else
a3ad94ed 905 gold_unreachable();
ead1e424
ILT
906}
907
908// Define a symbol in an Output_segment, sized version.
909
910template<int size>
14b31740 911Sized_symbol<size>*
ead1e424 912Symbol_table::do_define_in_output_segment(
14b31740 913 const Target* target,
ead1e424 914 const char* name,
14b31740 915 const char* version,
ead1e424
ILT
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;
86f2e683 927 Sized_symbol<size>* oldsym;
ead1e424
ILT
928
929 if (target->is_big_endian())
593f47df 930 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
306d9ef0 931 target, &name, &version, only_if_ref, &oldsym
593f47df 932 SELECT_SIZE_ENDIAN(size, true));
ead1e424 933 else
593f47df 934 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
306d9ef0 935 target, &name, &version, only_if_ref, &oldsym
593f47df 936 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
937
938 if (sym == NULL)
14b31740 939 return NULL;
ead1e424 940
d4f5281b 941 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
942 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
943 offset_base);
14b31740 944
86f2e683
ILT
945 if (oldsym != NULL
946 && Symbol_table::should_override_with_special(oldsym))
947 oldsym->override_with_special(sym);
948
14b31740 949 return sym;
ead1e424
ILT
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
14b31740
ILT
955Symbol*
956Symbol_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)
ead1e424 961{
a3ad94ed 962 gold_assert(target->get_size() == this->size_);
ead1e424 963 if (this->size_ == 32)
86f2e683
ILT
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 }
ead1e424 973 else if (this->size_ == 64)
86f2e683
ILT
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 }
ead1e424 983 else
a3ad94ed 984 gold_unreachable();
ead1e424
ILT
985}
986
987// Define a symbol as a constant, sized version.
988
989template<int size>
14b31740 990Sized_symbol<size>*
ead1e424 991Symbol_table::do_define_as_constant(
14b31740 992 const Target* target,
ead1e424 993 const char* name,
14b31740 994 const char* version,
ead1e424
ILT
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;
86f2e683 1004 Sized_symbol<size>* oldsym;
ead1e424
ILT
1005
1006 if (target->is_big_endian())
593f47df 1007 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
306d9ef0 1008 target, &name, &version, only_if_ref, &oldsym
593f47df 1009 SELECT_SIZE_ENDIAN(size, true));
ead1e424 1010 else
593f47df 1011 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
306d9ef0 1012 target, &name, &version, only_if_ref, &oldsym
593f47df 1013 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
1014
1015 if (sym == NULL)
14b31740 1016 return NULL;
ead1e424 1017
d4f5281b 1018 gold_assert(version == NULL || oldsym != NULL);
ead1e424 1019 sym->init(name, value, symsize, type, binding, visibility, nonvis);
14b31740 1020
86f2e683
ILT
1021 if (oldsym != NULL
1022 && Symbol_table::should_override_with_special(oldsym))
1023 oldsym->override_with_special(sym);
1024
14b31740 1025 return sym;
ead1e424
ILT
1026}
1027
1028// Define a set of symbols in output sections.
1029
1030void
14b31740
ILT
1031Symbol_table::define_symbols(const Layout* layout, const Target* target,
1032 int count, const Define_symbol_in_section* p)
ead1e424
ILT
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)
14b31740
ILT
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);
ead1e424 1042 else
14b31740 1043 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
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
1051void
14b31740
ILT
1052Symbol_table::define_symbols(const Layout* layout, const Target* target,
1053 int count, const Define_symbol_in_segment* p)
ead1e424
ILT
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)
14b31740
ILT
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);
ead1e424 1065 else
14b31740 1066 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1067 p->binding, p->visibility, p->nonvis,
1068 p->only_if_ref);
1069 }
1070}
1071
a3ad94ed
ILT
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
1077unsigned int
14b31740
ILT
1078Symbol_table::set_dynsym_indexes(const General_options* options,
1079 const Target* target,
1080 unsigned int index,
a3ad94ed 1081 std::vector<Symbol*>* syms,
14b31740
ILT
1082 Stringpool* dynpool,
1083 Versions* versions)
a3ad94ed
ILT
1084{
1085 for (Symbol_table_type::iterator p = this->table_.begin();
1086 p != this->table_.end();
1087 ++p)
1088 {
1089 Symbol* sym = p->second;
16649710
ILT
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
a6badf5a
ILT
1095 if (!sym->needs_dynsym_entry()
1096 && (!options->export_dynamic()
1097 || !sym->in_reg()
1098 || !sym->is_externally_visible()))
16649710
ILT
1099 sym->set_dynsym_index(-1U);
1100 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1101 {
1102 sym->set_dynsym_index(index);
1103 ++index;
1104 syms->push_back(sym);
1105 dynpool->add(sym->name(), NULL);
14b31740
ILT
1106
1107 // Record any version information.
1108 if (sym->version() != NULL)
1109 versions->record_version(options, dynpool, sym);
a3ad94ed
ILT
1110 }
1111 }
1112
14b31740
ILT
1113 // Finish up the versions. In some cases this may add new dynamic
1114 // symbols.
1115 index = versions->finalize(target, this, index, syms);
1116
a3ad94ed
ILT
1117 return index;
1118}
1119
c06b7b0b
ILT
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
75f65a3e 1122// OFF. Add their names to POOL. Return the new file offset.
54dc6425 1123
75f65a3e 1124off_t
16649710
ILT
1125Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1126 size_t dyn_global_index, size_t dyncount,
1127 Stringpool* pool)
54dc6425 1128{
f6ce93d6
ILT
1129 off_t ret;
1130
a3ad94ed 1131 gold_assert(index != 0);
c06b7b0b
ILT
1132 this->first_global_index_ = index;
1133
16649710
ILT
1134 this->dynamic_offset_ = dynoff;
1135 this->first_dynamic_global_index_ = dyn_global_index;
1136 this->dynamic_count_ = dyncount;
1137
75f65a3e 1138 if (this->size_ == 32)
c06b7b0b 1139 ret = this->sized_finalize<32>(index, off, pool);
61ba1cf9 1140 else if (this->size_ == 64)
c06b7b0b 1141 ret = this->sized_finalize<64>(index, off, pool);
61ba1cf9 1142 else
a3ad94ed 1143 gold_unreachable();
f6ce93d6
ILT
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;
75f65a3e
ILT
1150}
1151
ead1e424
ILT
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.
75f65a3e
ILT
1155
1156template<int size>
1157off_t
c06b7b0b 1158Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
75f65a3e 1159{
ead1e424 1160 off = align_address(off, size >> 3);
75f65a3e
ILT
1161 this->offset_ = off;
1162
c06b7b0b
ILT
1163 size_t orig_index = index;
1164
75f65a3e 1165 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1166 for (Symbol_table_type::iterator p = this->table_.begin();
1167 p != this->table_.end();
1168 ++p)
54dc6425 1169 {
75f65a3e 1170 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 1171
75f65a3e 1172 // FIXME: Here we need to decide which symbols should go into
a3ad94ed
ILT
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;
75f65a3e 1179
008db82e
ILT
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
ead1e424 1188 typename Sized_symbol<size>::Value_type value;
75f65a3e 1189
ead1e424 1190 switch (sym->source())
75f65a3e 1191 {
ead1e424
ILT
1192 case Symbol::FROM_OBJECT:
1193 {
16649710 1194 unsigned int shndx = sym->shndx();
ead1e424
ILT
1195
1196 // FIXME: We need some target specific support here.
16649710
ILT
1197 if (shndx >= elfcpp::SHN_LORESERVE
1198 && shndx != elfcpp::SHN_ABS)
ead1e424
ILT
1199 {
1200 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
16649710 1201 program_name, sym->name(), shndx);
ead1e424
ILT
1202 gold_exit(false);
1203 }
1204
f6ce93d6
ILT
1205 Object* symobj = sym->object();
1206 if (symobj->is_dynamic())
1207 {
1208 value = 0;
16649710 1209 shndx = elfcpp::SHN_UNDEF;
f6ce93d6 1210 }
16649710 1211 else if (shndx == elfcpp::SHN_UNDEF)
ead1e424 1212 value = 0;
16649710 1213 else if (shndx == elfcpp::SHN_ABS)
ead1e424
ILT
1214 value = sym->value();
1215 else
1216 {
f6ce93d6 1217 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1218 off_t secoff;
16649710 1219 Output_section* os = relobj->output_section(shndx, &secoff);
ead1e424
ILT
1220
1221 if (os == NULL)
1222 {
c06b7b0b 1223 sym->set_symtab_index(-1U);
16649710 1224 gold_assert(sym->dynsym_index() == -1U);
ead1e424
ILT
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:
a3ad94ed 1257 gold_unreachable();
ead1e424
ILT
1258 }
1259 }
1260 break;
1261
1262 case Symbol::CONSTANT:
1263 value = sym->value();
1264 break;
1265
1266 default:
a3ad94ed 1267 gold_unreachable();
54dc6425 1268 }
ead1e424
ILT
1269
1270 sym->set_value(value);
c06b7b0b 1271 sym->set_symtab_index(index);
f0641a0b 1272 pool->add(sym->name(), NULL);
c06b7b0b 1273 ++index;
ead1e424 1274 off += sym_size;
54dc6425 1275 }
75f65a3e 1276
c06b7b0b 1277 this->output_count_ = index - orig_index;
61ba1cf9 1278
75f65a3e 1279 return off;
54dc6425
ILT
1280}
1281
61ba1cf9
ILT
1282// Write out the global symbols.
1283
1284void
1285Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
16649710 1286 const Stringpool* dynpool, Output_file* of) const
61ba1cf9
ILT
1287{
1288 if (this->size_ == 32)
1289 {
1290 if (target->is_big_endian())
16649710 1291 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
61ba1cf9 1292 else
16649710 1293 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
61ba1cf9
ILT
1294 }
1295 else if (this->size_ == 64)
1296 {
1297 if (target->is_big_endian())
16649710 1298 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
61ba1cf9 1299 else
16649710 1300 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
61ba1cf9
ILT
1301 }
1302 else
a3ad94ed 1303 gold_unreachable();
61ba1cf9
ILT
1304}
1305
1306// Write out the global symbols.
1307
1308template<int size, bool big_endian>
1309void
ab5c9e90 1310Symbol_table::sized_write_globals(const Target* target,
61ba1cf9 1311 const Stringpool* sympool,
16649710 1312 const Stringpool* dynpool,
61ba1cf9
ILT
1313 Output_file* of) const
1314{
1315 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1316 unsigned int index = this->first_global_index_;
1317 const off_t oview_size = this->output_count_ * sym_size;
16649710
ILT
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);
c06b7b0b 1328
61ba1cf9
ILT
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
a3ad94ed 1336 unsigned int sym_index = sym->symtab_index();
16649710
ILT
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)
a3ad94ed
ILT
1344 {
1345 // This symbol is not included in the output file.
1346 continue;
1347 }
16649710
ILT
1348
1349 if (sym_index == index)
1350 ++index;
1351 else if (sym_index != -1U)
a3ad94ed
ILT
1352 {
1353 // We have already seen this symbol, because it has a
1354 // default version.
1355 gold_assert(sym_index < index);
16649710
ILT
1356 if (dynsym_index == -1U)
1357 continue;
1358 sym_index = -1U;
a3ad94ed 1359 }
c06b7b0b 1360
ead1e424 1361 unsigned int shndx;
ab5c9e90 1362 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
ead1e424
ILT
1363 switch (sym->source())
1364 {
1365 case Symbol::FROM_OBJECT:
1366 {
16649710 1367 unsigned int in_shndx = sym->shndx();
ead1e424
ILT
1368
1369 // FIXME: We need some target specific support here.
16649710
ILT
1370 if (in_shndx >= elfcpp::SHN_LORESERVE
1371 && in_shndx != elfcpp::SHN_ABS)
ead1e424
ILT
1372 {
1373 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
16649710 1374 program_name, sym->name(), in_shndx);
ead1e424
ILT
1375 gold_exit(false);
1376 }
1377
f6ce93d6
ILT
1378 Object* symobj = sym->object();
1379 if (symobj->is_dynamic())
1380 {
ab5c9e90
ILT
1381 if (sym->needs_dynsym_value())
1382 value = target->dynsym_value(sym);
f6ce93d6
ILT
1383 shndx = elfcpp::SHN_UNDEF;
1384 }
16649710
ILT
1385 else if (in_shndx == elfcpp::SHN_UNDEF
1386 || in_shndx == elfcpp::SHN_ABS)
1387 shndx = in_shndx;
ead1e424
ILT
1388 else
1389 {
f6ce93d6 1390 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1391 off_t secoff;
16649710 1392 Output_section* os = relobj->output_section(in_shndx, &secoff);
a3ad94ed 1393 gold_assert(os != NULL);
ead1e424
ILT
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:
a3ad94ed 1412 gold_unreachable();
ead1e424 1413 }
61ba1cf9 1414
16649710
ILT
1415 if (sym_index != -1U)
1416 {
6a469986 1417 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1418 sym, sym->value(), shndx, sympool, ps
6a469986 1419 SELECT_SIZE_ENDIAN(size, big_endian));
16649710
ILT
1420 ps += sym_size;
1421 }
61ba1cf9 1422
16649710
ILT
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);
6a469986 1428 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1429 sym, value, shndx, dynpool, pd
6a469986 1430 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1431 }
61ba1cf9
ILT
1432 }
1433
a3ad94ed 1434 gold_assert(ps - psyms == oview_size);
c06b7b0b
ILT
1435
1436 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
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
1444template<int size, bool big_endian>
1445void
ab5c9e90
ILT
1446Symbol_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
16649710
ILT
1453{
1454 elfcpp::Sym_write<size, big_endian> osym(p);
1455 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 1456 osym.put_st_value(value);
16649710
ILT
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);
61ba1cf9
ILT
1461}
1462
a3ad94ed
ILT
1463// Write out a section symbol. Return the update offset.
1464
1465void
1466Symbol_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
1491template<int size, bool big_endian>
1492void
1493Symbol_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
f6ce93d6
ILT
1513// Warnings functions.
1514
1515// Add a new warning.
1516
1517void
1518Warnings::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
1529void
1530Warnings::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.
645f8123
ILT
1549 {
1550 Task_locker_obj<Object> tl(*p->second.object);
1551 const unsigned char* c;
1552 off_t len;
9eb9fa57
ILT
1553 c = p->second.object->section_contents(p->second.shndx, &len,
1554 false);
645f8123
ILT
1555 p->second.set_text(reinterpret_cast<const char*>(c), len);
1556 }
f6ce93d6
ILT
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
1564void
c06b7b0b 1565Warnings::issue_warning(const Symbol* sym, const std::string& location) const
f6ce93d6 1566{
a3ad94ed 1567 gold_assert(sym->has_warning());
f6ce93d6 1568 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 1569 gold_assert(p != this->warnings_.end());
f6ce93d6
ILT
1570 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1571 p->second.text.c_str());
1572}
1573
14bfc3f5
ILT
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
193a53d9 1578#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
1579template
1580void
193a53d9
ILT
1581Symbol_table::add_from_relobj<32, false>(
1582 Sized_relobj<32, false>* relobj,
f6ce93d6 1583 const unsigned char* syms,
14bfc3f5
ILT
1584 size_t count,
1585 const char* sym_names,
1586 size_t sym_name_size,
1587 Symbol** sympointers);
193a53d9 1588#endif
14bfc3f5 1589
193a53d9 1590#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
1591template
1592void
193a53d9
ILT
1593Symbol_table::add_from_relobj<32, true>(
1594 Sized_relobj<32, true>* relobj,
f6ce93d6 1595 const unsigned char* syms,
14bfc3f5
ILT
1596 size_t count,
1597 const char* sym_names,
1598 size_t sym_name_size,
1599 Symbol** sympointers);
193a53d9 1600#endif
14bfc3f5 1601
193a53d9 1602#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
1603template
1604void
193a53d9
ILT
1605Symbol_table::add_from_relobj<64, false>(
1606 Sized_relobj<64, false>* relobj,
f6ce93d6 1607 const unsigned char* syms,
14bfc3f5
ILT
1608 size_t count,
1609 const char* sym_names,
1610 size_t sym_name_size,
1611 Symbol** sympointers);
193a53d9 1612#endif
14bfc3f5 1613
193a53d9 1614#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
1615template
1616void
193a53d9
ILT
1617Symbol_table::add_from_relobj<64, true>(
1618 Sized_relobj<64, true>* relobj,
f6ce93d6 1619 const unsigned char* syms,
14bfc3f5
ILT
1620 size_t count,
1621 const char* sym_names,
1622 size_t sym_name_size,
1623 Symbol** sympointers);
193a53d9 1624#endif
14bfc3f5 1625
193a53d9 1626#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
1627template
1628void
193a53d9
ILT
1629Symbol_table::add_from_dynobj<32, false>(
1630 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
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);
193a53d9 1638#endif
dbe717ef 1639
193a53d9 1640#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
1641template
1642void
193a53d9
ILT
1643Symbol_table::add_from_dynobj<32, true>(
1644 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
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);
193a53d9 1652#endif
dbe717ef 1653
193a53d9 1654#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
1655template
1656void
193a53d9
ILT
1657Symbol_table::add_from_dynobj<64, false>(
1658 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
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);
193a53d9 1666#endif
dbe717ef 1667
193a53d9 1668#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
1669template
1670void
193a53d9
ILT
1671Symbol_table::add_from_dynobj<64, true>(
1672 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
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);
193a53d9 1680#endif
dbe717ef 1681
14bfc3f5 1682} // End namespace gold.