]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
daily update
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
3#include "gold.h"
4
14bfc3f5
ILT
5#include <stdint.h>
6#include <string>
7#include <utility>
8
9#include "object.h"
dbe717ef 10#include "dynobj.h"
75f65a3e 11#include "output.h"
61ba1cf9 12#include "target.h"
645f8123 13#include "workqueue.h"
14bfc3f5
ILT
14#include "symtab.h"
15
16namespace gold
17{
18
19// Class Symbol.
20
ead1e424
ILT
21// Initialize fields in Symbol. This initializes everything except u_
22// and source_.
14bfc3f5 23
14bfc3f5 24void
ead1e424
ILT
25Symbol::init_fields(const char* name, const char* version,
26 elfcpp::STT type, elfcpp::STB binding,
27 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
28{
29 this->name_ = name;
30 this->version_ = version;
c06b7b0b
ILT
31 this->symtab_index_ = 0;
32 this->dynsym_index_ = 0;
ead1e424 33 this->got_offset_ = 0;
f4151f89 34 this->plt_offset_ = 0;
ead1e424
ILT
35 this->type_ = type;
36 this->binding_ = binding;
37 this->visibility_ = visibility;
38 this->nonvis_ = nonvis;
39 this->is_target_special_ = false;
1564db8d
ILT
40 this->is_def_ = false;
41 this->is_forwarder_ = false;
c06b7b0b 42 this->needs_dynsym_entry_ = false;
008db82e 43 this->in_reg_ = false;
ead1e424
ILT
44 this->in_dyn_ = false;
45 this->has_got_offset_ = false;
f4151f89 46 this->has_plt_offset_ = false;
f6ce93d6 47 this->has_warning_ = false;
ead1e424
ILT
48}
49
50// Initialize the fields in the base class Symbol for SYM in OBJECT.
51
52template<int size, bool big_endian>
53void
54Symbol::init_base(const char* name, const char* version, Object* object,
55 const elfcpp::Sym<size, big_endian>& sym)
56{
57 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
58 sym.get_st_visibility(), sym.get_st_nonvis());
59 this->u_.from_object.object = object;
60 // FIXME: Handle SHN_XINDEX.
16649710 61 this->u_.from_object.shndx = sym.get_st_shndx();
ead1e424 62 this->source_ = FROM_OBJECT;
008db82e 63 this->in_reg_ = !object->is_dynamic();
1564db8d 64 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
65}
66
ead1e424
ILT
67// Initialize the fields in the base class Symbol for a symbol defined
68// in an Output_data.
69
70void
71Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
72 elfcpp::STB binding, elfcpp::STV visibility,
73 unsigned char nonvis, bool offset_is_from_end)
74{
75 this->init_fields(name, NULL, type, binding, visibility, nonvis);
76 this->u_.in_output_data.output_data = od;
77 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
78 this->source_ = IN_OUTPUT_DATA;
008db82e 79 this->in_reg_ = true;
ead1e424
ILT
80}
81
82// Initialize the fields in the base class Symbol for a symbol defined
83// in an Output_segment.
84
85void
86Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
87 elfcpp::STB binding, elfcpp::STV visibility,
88 unsigned char nonvis, Segment_offset_base offset_base)
89{
90 this->init_fields(name, NULL, type, binding, visibility, nonvis);
91 this->u_.in_output_segment.output_segment = os;
92 this->u_.in_output_segment.offset_base = offset_base;
93 this->source_ = IN_OUTPUT_SEGMENT;
008db82e 94 this->in_reg_ = true;
ead1e424
ILT
95}
96
97// Initialize the fields in the base class Symbol for a symbol defined
98// as a constant.
99
100void
101Symbol::init_base(const char* name, elfcpp::STT type,
102 elfcpp::STB binding, elfcpp::STV visibility,
103 unsigned char nonvis)
104{
105 this->init_fields(name, NULL, type, binding, visibility, nonvis);
106 this->source_ = CONSTANT;
008db82e 107 this->in_reg_ = true;
ead1e424
ILT
108}
109
110// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
111
112template<int size>
113template<bool big_endian>
114void
115Sized_symbol<size>::init(const char* name, const char* version, Object* object,
116 const elfcpp::Sym<size, big_endian>& sym)
117{
118 this->init_base(name, version, object, sym);
119 this->value_ = sym.get_st_value();
ead1e424
ILT
120 this->symsize_ = sym.get_st_size();
121}
122
123// Initialize the fields in Sized_symbol for a symbol defined in an
124// Output_data.
125
126template<int size>
127void
128Sized_symbol<size>::init(const char* name, Output_data* od,
129 Value_type value, Size_type symsize,
130 elfcpp::STT type, elfcpp::STB binding,
131 elfcpp::STV visibility, unsigned char nonvis,
132 bool offset_is_from_end)
133{
134 this->init_base(name, od, type, binding, visibility, nonvis,
135 offset_is_from_end);
136 this->value_ = value;
137 this->symsize_ = symsize;
138}
139
140// Initialize the fields in Sized_symbol for a symbol defined in an
141// Output_segment.
142
143template<int size>
144void
145Sized_symbol<size>::init(const char* name, Output_segment* os,
146 Value_type value, Size_type symsize,
147 elfcpp::STT type, elfcpp::STB binding,
148 elfcpp::STV visibility, unsigned char nonvis,
149 Segment_offset_base offset_base)
150{
151 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
152 this->value_ = value;
153 this->symsize_ = symsize;
154}
155
156// Initialize the fields in Sized_symbol for a symbol defined as a
157// constant.
158
159template<int size>
160void
161Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
162 elfcpp::STT type, elfcpp::STB binding,
163 elfcpp::STV visibility, unsigned char nonvis)
164{
165 this->init_base(name, type, binding, visibility, nonvis);
166 this->value_ = value;
167 this->symsize_ = symsize;
14bfc3f5
ILT
168}
169
170// Class Symbol_table.
171
172Symbol_table::Symbol_table()
ead1e424 173 : size_(0), saw_undefined_(0), offset_(0), table_(), namepool_(),
f6ce93d6 174 forwarders_(), commons_(), warnings_()
14bfc3f5
ILT
175{
176}
177
178Symbol_table::~Symbol_table()
179{
180}
181
182// The hash function. The key is always canonicalized, so we use a
183// simple combination of the pointers.
184
185size_t
186Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
187{
f0641a0b 188 return key.first ^ key.second;
14bfc3f5
ILT
189}
190
191// The symbol table key equality function. This is only called with
192// canonicalized name and version strings, so we can use pointer
193// comparison.
194
195bool
196Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
197 const Symbol_table_key& k2) const
198{
199 return k1.first == k2.first && k1.second == k2.second;
200}
201
202// Make TO a symbol which forwards to FROM.
203
204void
205Symbol_table::make_forwarder(Symbol* from, Symbol* to)
206{
a3ad94ed
ILT
207 gold_assert(from != to);
208 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
209 this->forwarders_[from] = to;
210 from->set_forwarder();
211}
212
61ba1cf9
ILT
213// Resolve the forwards from FROM, returning the real symbol.
214
14bfc3f5 215Symbol*
c06b7b0b 216Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 217{
a3ad94ed 218 gold_assert(from->is_forwarder());
c06b7b0b 219 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 220 this->forwarders_.find(from);
a3ad94ed 221 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
222 return p->second;
223}
224
61ba1cf9
ILT
225// Look up a symbol by name.
226
227Symbol*
228Symbol_table::lookup(const char* name, const char* version) const
229{
f0641a0b
ILT
230 Stringpool::Key name_key;
231 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
232 if (name == NULL)
233 return NULL;
f0641a0b
ILT
234
235 Stringpool::Key version_key = 0;
61ba1cf9
ILT
236 if (version != NULL)
237 {
f0641a0b 238 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
239 if (version == NULL)
240 return NULL;
241 }
242
f0641a0b 243 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
244 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
245 if (p == this->table_.end())
246 return NULL;
247 return p->second;
248}
249
14bfc3f5
ILT
250// Resolve a Symbol with another Symbol. This is only used in the
251// unusual case where there are references to both an unversioned
252// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
253// version is the default version. Because this is unusual, we do
254// this the slow way, by converting back to an ELF symbol.
14bfc3f5 255
1564db8d 256template<int size, bool big_endian>
14bfc3f5 257void
14b31740
ILT
258Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
259 const char* version ACCEPT_SIZE_ENDIAN)
14bfc3f5 260{
1564db8d
ILT
261 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
262 elfcpp::Sym_write<size, big_endian> esym(buf);
263 // We don't bother to set the st_name field.
264 esym.put_st_value(from->value());
265 esym.put_st_size(from->symsize());
266 esym.put_st_info(from->binding(), from->type());
ead1e424 267 esym.put_st_other(from->visibility(), from->nonvis());
16649710 268 esym.put_st_shndx(from->shndx());
14b31740 269 Symbol_table::resolve(to, esym.sym(), from->object(), version);
14bfc3f5
ILT
270}
271
272// Add one symbol from OBJECT to the symbol table. NAME is symbol
273// name and VERSION is the version; both are canonicalized. DEF is
274// whether this is the default version.
275
276// If DEF is true, then this is the definition of a default version of
277// a symbol. That means that any lookup of NAME/NULL and any lookup
278// of NAME/VERSION should always return the same symbol. This is
279// obvious for references, but in particular we want to do this for
280// definitions: overriding NAME/NULL should also override
281// NAME/VERSION. If we don't do that, it would be very hard to
282// override functions in a shared library which uses versioning.
283
284// We implement this by simply making both entries in the hash table
285// point to the same Symbol structure. That is easy enough if this is
286// the first time we see NAME/NULL or NAME/VERSION, but it is possible
287// that we have seen both already, in which case they will both have
288// independent entries in the symbol table. We can't simply change
289// the symbol table entry, because we have pointers to the entries
290// attached to the object files. So we mark the entry attached to the
291// object file as a forwarder, and record it in the forwarders_ map.
292// Note that entries in the hash table will never be marked as
293// forwarders.
294
295template<int size, bool big_endian>
296Symbol*
f6ce93d6 297Symbol_table::add_from_object(Object* object,
14bfc3f5 298 const char *name,
f0641a0b
ILT
299 Stringpool::Key name_key,
300 const char *version,
301 Stringpool::Key version_key,
302 bool def,
14bfc3f5
ILT
303 const elfcpp::Sym<size, big_endian>& sym)
304{
305 Symbol* const snull = NULL;
306 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
307 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
308 snull));
14bfc3f5
ILT
309
310 std::pair<typename Symbol_table_type::iterator, bool> insdef =
311 std::make_pair(this->table_.end(), false);
312 if (def)
313 {
f0641a0b
ILT
314 const Stringpool::Key vnull_key = 0;
315 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
316 vnull_key),
14bfc3f5
ILT
317 snull));
318 }
319
320 // ins.first: an iterator, which is a pointer to a pair.
321 // ins.first->first: the key (a pair of name and version).
322 // ins.first->second: the value (Symbol*).
323 // ins.second: true if new entry was inserted, false if not.
324
1564db8d 325 Sized_symbol<size>* ret;
ead1e424
ILT
326 bool was_undefined;
327 bool was_common;
14bfc3f5
ILT
328 if (!ins.second)
329 {
330 // We already have an entry for NAME/VERSION.
593f47df
ILT
331 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
332 SELECT_SIZE(size));
a3ad94ed 333 gold_assert(ret != NULL);
ead1e424
ILT
334
335 was_undefined = ret->is_undefined();
336 was_common = ret->is_common();
337
14b31740 338 Symbol_table::resolve(ret, sym, object, version);
14bfc3f5
ILT
339
340 if (def)
341 {
342 if (insdef.second)
343 {
344 // This is the first time we have seen NAME/NULL. Make
345 // NAME/NULL point to NAME/VERSION.
346 insdef.first->second = ret;
347 }
dbe717ef 348 else if (insdef.first->second != ret)
14bfc3f5
ILT
349 {
350 // This is the unfortunate case where we already have
351 // entries for both NAME/VERSION and NAME/NULL.
274e99f9 352 const Sized_symbol<size>* sym2;
593f47df 353 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d
ILT
354 insdef.first->second
355 SELECT_SIZE(size));
593f47df 356 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
14b31740 357 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
14bfc3f5
ILT
358 this->make_forwarder(insdef.first->second, ret);
359 insdef.first->second = ret;
360 }
361 }
362 }
363 else
364 {
365 // This is the first time we have seen NAME/VERSION.
a3ad94ed 366 gold_assert(ins.first->second == NULL);
ead1e424
ILT
367
368 was_undefined = false;
369 was_common = false;
370
14bfc3f5
ILT
371 if (def && !insdef.second)
372 {
14b31740
ILT
373 // We already have an entry for NAME/NULL. If we override
374 // it, then change it to NAME/VERSION.
593f47df
ILT
375 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
376 insdef.first->second
377 SELECT_SIZE(size));
14b31740 378 Symbol_table::resolve(ret, sym, object, version);
14bfc3f5
ILT
379 ins.first->second = ret;
380 }
381 else
382 {
f6ce93d6
ILT
383 Sized_target<size, big_endian>* target =
384 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
385 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
1564db8d
ILT
386 if (!target->has_make_symbol())
387 ret = new Sized_symbol<size>();
388 else
14bfc3f5 389 {
1564db8d
ILT
390 ret = target->make_symbol();
391 if (ret == NULL)
14bfc3f5
ILT
392 {
393 // This means that we don't want a symbol table
394 // entry after all.
395 if (!def)
396 this->table_.erase(ins.first);
397 else
398 {
399 this->table_.erase(insdef.first);
400 // Inserting insdef invalidated ins.
f0641a0b
ILT
401 this->table_.erase(std::make_pair(name_key,
402 version_key));
14bfc3f5
ILT
403 }
404 return NULL;
405 }
406 }
14bfc3f5 407
1564db8d
ILT
408 ret->init(name, version, object, sym);
409
14bfc3f5
ILT
410 ins.first->second = ret;
411 if (def)
412 {
413 // This is the first time we have seen NAME/NULL. Point
414 // it at the new entry for NAME/VERSION.
a3ad94ed 415 gold_assert(insdef.second);
14bfc3f5
ILT
416 insdef.first->second = ret;
417 }
418 }
419 }
420
ead1e424
ILT
421 // Record every time we see a new undefined symbol, to speed up
422 // archive groups.
423 if (!was_undefined && ret->is_undefined())
424 ++this->saw_undefined_;
425
426 // Keep track of common symbols, to speed up common symbol
427 // allocation.
428 if (!was_common && ret->is_common())
429 this->commons_.push_back(ret);
430
14bfc3f5
ILT
431 return ret;
432}
433
f6ce93d6 434// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
435
436template<int size, bool big_endian>
437void
dbe717ef
ILT
438Symbol_table::add_from_relobj(
439 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 440 const unsigned char* syms,
14bfc3f5
ILT
441 size_t count,
442 const char* sym_names,
443 size_t sym_name_size,
444 Symbol** sympointers)
445{
446 // We take the size from the first object we see.
447 if (this->get_size() == 0)
448 this->set_size(size);
449
dbe717ef 450 if (size != this->get_size() || size != relobj->target()->get_size())
14bfc3f5
ILT
451 {
452 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
dbe717ef 453 program_name, relobj->name().c_str());
14bfc3f5
ILT
454 gold_exit(false);
455 }
456
a783673b
ILT
457 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
458
f6ce93d6 459 const unsigned char* p = syms;
a783673b 460 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
461 {
462 elfcpp::Sym<size, big_endian> sym(p);
a783673b 463 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 464
a783673b 465 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
466 if (st_name >= sym_name_size)
467 {
54dc6425
ILT
468 fprintf(stderr,
469 _("%s: %s: bad global symbol name offset %u at %lu\n"),
dbe717ef 470 program_name, relobj->name().c_str(), st_name,
14bfc3f5
ILT
471 static_cast<unsigned long>(i));
472 gold_exit(false);
473 }
474
dbe717ef
ILT
475 const char* name = sym_names + st_name;
476
a783673b
ILT
477 // A symbol defined in a section which we are not including must
478 // be treated as an undefined symbol.
479 unsigned char symbuf[sym_size];
480 elfcpp::Sym<size, big_endian> sym2(symbuf);
481 unsigned int st_shndx = psym->get_st_shndx();
482 if (st_shndx != elfcpp::SHN_UNDEF
483 && st_shndx < elfcpp::SHN_LORESERVE
dbe717ef 484 && !relobj->is_section_included(st_shndx))
a783673b
ILT
485 {
486 memcpy(symbuf, p, sym_size);
487 elfcpp::Sym_write<size, big_endian> sw(symbuf);
488 sw.put_st_shndx(elfcpp::SHN_UNDEF);
489 psym = &sym2;
490 }
491
14bfc3f5
ILT
492 // In an object file, an '@' in the name separates the symbol
493 // name from the version name. If there are two '@' characters,
494 // this is the default version.
495 const char* ver = strchr(name, '@');
496
497 Symbol* res;
498 if (ver == NULL)
499 {
f0641a0b
ILT
500 Stringpool::Key name_key;
501 name = this->namepool_.add(name, &name_key);
dbe717ef 502 res = this->add_from_object(relobj, name, name_key, NULL, 0,
f0641a0b 503 false, *psym);
14bfc3f5
ILT
504 }
505 else
506 {
f0641a0b
ILT
507 Stringpool::Key name_key;
508 name = this->namepool_.add(name, ver - name, &name_key);
509
14bfc3f5
ILT
510 bool def = false;
511 ++ver;
512 if (*ver == '@')
513 {
514 def = true;
515 ++ver;
516 }
f0641a0b
ILT
517
518 Stringpool::Key ver_key;
519 ver = this->namepool_.add(ver, &ver_key);
520
dbe717ef 521 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
f0641a0b 522 def, *psym);
14bfc3f5
ILT
523 }
524
525 *sympointers++ = res;
14bfc3f5
ILT
526 }
527}
528
dbe717ef
ILT
529// Add all the symbols in a dynamic object to the hash table.
530
531template<int size, bool big_endian>
532void
533Symbol_table::add_from_dynobj(
534 Sized_dynobj<size, big_endian>* dynobj,
535 const unsigned char* syms,
536 size_t count,
537 const char* sym_names,
538 size_t sym_name_size,
539 const unsigned char* versym,
540 size_t versym_size,
541 const std::vector<const char*>* version_map)
542{
543 // We take the size from the first object we see.
544 if (this->get_size() == 0)
545 this->set_size(size);
546
547 if (size != this->get_size() || size != dynobj->target()->get_size())
548 {
549 fprintf(stderr, _("%s: %s: mixing 32-bit and 64-bit ELF objects\n"),
550 program_name, dynobj->name().c_str());
551 gold_exit(false);
552 }
553
554 if (versym != NULL && versym_size / 2 < count)
555 {
556 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
557 program_name, dynobj->name().c_str());
558 gold_exit(false);
559 }
560
561 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
562
563 const unsigned char* p = syms;
564 const unsigned char* vs = versym;
565 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
566 {
567 elfcpp::Sym<size, big_endian> sym(p);
568
569 // Ignore symbols with local binding.
570 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
571 continue;
572
573 unsigned int st_name = sym.get_st_name();
574 if (st_name >= sym_name_size)
575 {
576 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
577 program_name, dynobj->name().c_str(), st_name,
578 static_cast<unsigned long>(i));
579 gold_exit(false);
580 }
581
582 const char* name = sym_names + st_name;
583
584 if (versym == NULL)
585 {
586 Stringpool::Key name_key;
587 name = this->namepool_.add(name, &name_key);
588 this->add_from_object(dynobj, name, name_key, NULL, 0,
589 false, sym);
590 continue;
591 }
592
593 // Read the version information.
594
595 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
596
597 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
598 v &= elfcpp::VERSYM_VERSION;
599
600 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL))
601 {
602 // This symbol should not be visible outside the object.
603 continue;
604 }
605
606 // At this point we are definitely going to add this symbol.
607 Stringpool::Key name_key;
608 name = this->namepool_.add(name, &name_key);
609
610 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
611 {
612 // This symbol does not have a version.
613 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
614 continue;
615 }
616
617 if (v >= version_map->size())
618 {
619 fprintf(stderr,
620 _("%s: %s: versym for symbol %zu out of range: %u\n"),
621 program_name, dynobj->name().c_str(), i, v);
622 gold_exit(false);
623 }
624
625 const char* version = (*version_map)[v];
626 if (version == NULL)
627 {
628 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
629 program_name, dynobj->name().c_str(), i, v);
630 gold_exit(false);
631 }
632
633 Stringpool::Key version_key;
634 version = this->namepool_.add(version, &version_key);
635
636 // If this is an absolute symbol, and the version name and
637 // symbol name are the same, then this is the version definition
638 // symbol. These symbols exist to support using -u to pull in
639 // particular versions. We do not want to record a version for
640 // them.
641 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
642 {
643 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
644 continue;
645 }
646
647 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
648
649 this->add_from_object(dynobj, name, name_key, version, version_key,
650 def, sym);
651 }
652}
653
ead1e424
ILT
654// Create and return a specially defined symbol. If ONLY_IF_REF is
655// true, then only create the symbol if there is a reference to it.
656
657template<int size, bool big_endian>
658Sized_symbol<size>*
14b31740
ILT
659Symbol_table::define_special_symbol(const Target* target, const char* name,
660 const char* version, bool only_if_ref
593f47df 661 ACCEPT_SIZE_ENDIAN)
ead1e424 662{
a3ad94ed 663 gold_assert(this->size_ == size);
ead1e424
ILT
664
665 Symbol* oldsym;
666 Sized_symbol<size>* sym;
667
668 if (only_if_ref)
669 {
14b31740 670 oldsym = this->lookup(name, version);
f6ce93d6 671 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424
ILT
672 return NULL;
673 sym = NULL;
674
14b31740 675 // Canonicalize NAME and VERSION.
ead1e424 676 name = oldsym->name();
14b31740 677 version = oldsym->version();
ead1e424
ILT
678 }
679 else
680 {
14b31740 681 // Canonicalize NAME and VERSION.
f0641a0b
ILT
682 Stringpool::Key name_key;
683 name = this->namepool_.add(name, &name_key);
ead1e424 684
14b31740
ILT
685 Stringpool::Key version_key = 0;
686 if (version != NULL)
687 version = this->namepool_.add(version, &version_key);
688
ead1e424 689 Symbol* const snull = NULL;
ead1e424 690 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
691 this->table_.insert(std::make_pair(std::make_pair(name_key,
692 version_key),
ead1e424
ILT
693 snull));
694
695 if (!ins.second)
696 {
14b31740 697 // We already have a symbol table entry for NAME/VERSION.
ead1e424 698 oldsym = ins.first->second;
a3ad94ed 699 gold_assert(oldsym != NULL);
ead1e424
ILT
700 sym = NULL;
701 }
702 else
703 {
704 // We haven't seen this symbol before.
a3ad94ed 705 gold_assert(ins.first->second == NULL);
ead1e424
ILT
706
707 if (!target->has_make_symbol())
708 sym = new Sized_symbol<size>();
709 else
710 {
a3ad94ed
ILT
711 gold_assert(target->get_size() == size);
712 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
ead1e424 713 typedef Sized_target<size, big_endian> My_target;
14b31740
ILT
714 const My_target* sized_target =
715 static_cast<const My_target*>(target);
ead1e424
ILT
716 sym = sized_target->make_symbol();
717 if (sym == NULL)
718 return NULL;
719 }
720
721 ins.first->second = sym;
722 oldsym = NULL;
723 }
724 }
725
726 if (oldsym != NULL)
727 {
a3ad94ed 728 gold_assert(sym == NULL);
ead1e424 729
593f47df
ILT
730 sym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
731 SELECT_SIZE(size));
a3ad94ed 732 gold_assert(sym->source() == Symbol::FROM_OBJECT);
16649710
ILT
733 const int old_shndx = sym->shndx();
734 if (old_shndx != elfcpp::SHN_UNDEF
735 && old_shndx != elfcpp::SHN_COMMON
ead1e424
ILT
736 && !sym->object()->is_dynamic())
737 {
738 fprintf(stderr, "%s: linker defined: multiple definition of %s\n",
739 program_name, name);
740 // FIXME: Report old location. Record that we have seen an
741 // error.
742 return NULL;
743 }
744
745 // Our new definition is going to override the old reference.
746 }
747
748 return sym;
749}
750
751// Define a symbol based on an Output_data.
752
14b31740
ILT
753Symbol*
754Symbol_table::define_in_output_data(const Target* target, const char* name,
755 const char* version, Output_data* od,
ead1e424
ILT
756 uint64_t value, uint64_t symsize,
757 elfcpp::STT type, elfcpp::STB binding,
758 elfcpp::STV visibility,
759 unsigned char nonvis,
760 bool offset_is_from_end,
761 bool only_if_ref)
762{
a3ad94ed 763 gold_assert(target->get_size() == this->size_);
ead1e424 764 if (this->size_ == 32)
14b31740
ILT
765 return this->do_define_in_output_data<32>(target, name, version, od, value,
766 symsize, type, binding,
767 visibility, nonvis,
768 offset_is_from_end, only_if_ref);
ead1e424 769 else if (this->size_ == 64)
14b31740
ILT
770 return this->do_define_in_output_data<64>(target, name, version, od, value,
771 symsize, type, binding,
772 visibility, nonvis,
773 offset_is_from_end, only_if_ref);
ead1e424 774 else
a3ad94ed 775 gold_unreachable();
ead1e424
ILT
776}
777
778// Define a symbol in an Output_data, sized version.
779
780template<int size>
14b31740 781Sized_symbol<size>*
ead1e424 782Symbol_table::do_define_in_output_data(
14b31740 783 const Target* target,
ead1e424 784 const char* name,
14b31740 785 const char* version,
ead1e424
ILT
786 Output_data* od,
787 typename elfcpp::Elf_types<size>::Elf_Addr value,
788 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
789 elfcpp::STT type,
790 elfcpp::STB binding,
791 elfcpp::STV visibility,
792 unsigned char nonvis,
793 bool offset_is_from_end,
794 bool only_if_ref)
795{
796 Sized_symbol<size>* sym;
797
798 if (target->is_big_endian())
593f47df 799 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
14b31740 800 target, name, version, only_if_ref
593f47df 801 SELECT_SIZE_ENDIAN(size, true));
ead1e424 802 else
593f47df 803 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
14b31740 804 target, name, version, only_if_ref
593f47df 805 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
806
807 if (sym == NULL)
14b31740 808 return NULL;
ead1e424
ILT
809
810 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
811 offset_is_from_end);
14b31740
ILT
812
813 return sym;
ead1e424
ILT
814}
815
816// Define a symbol based on an Output_segment.
817
14b31740
ILT
818Symbol*
819Symbol_table::define_in_output_segment(const Target* target, const char* name,
820 const char* version, Output_segment* os,
ead1e424
ILT
821 uint64_t value, uint64_t symsize,
822 elfcpp::STT type, elfcpp::STB binding,
823 elfcpp::STV visibility,
824 unsigned char nonvis,
825 Symbol::Segment_offset_base offset_base,
826 bool only_if_ref)
827{
a3ad94ed 828 gold_assert(target->get_size() == this->size_);
ead1e424 829 if (this->size_ == 32)
14b31740
ILT
830 return this->do_define_in_output_segment<32>(target, name, version, os,
831 value, symsize, type, binding,
832 visibility, nonvis,
833 offset_base, only_if_ref);
ead1e424 834 else if (this->size_ == 64)
14b31740
ILT
835 return this->do_define_in_output_segment<64>(target, name, version, os,
836 value, symsize, type, binding,
837 visibility, nonvis,
838 offset_base, only_if_ref);
ead1e424 839 else
a3ad94ed 840 gold_unreachable();
ead1e424
ILT
841}
842
843// Define a symbol in an Output_segment, sized version.
844
845template<int size>
14b31740 846Sized_symbol<size>*
ead1e424 847Symbol_table::do_define_in_output_segment(
14b31740 848 const Target* target,
ead1e424 849 const char* name,
14b31740 850 const char* version,
ead1e424
ILT
851 Output_segment* os,
852 typename elfcpp::Elf_types<size>::Elf_Addr value,
853 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
854 elfcpp::STT type,
855 elfcpp::STB binding,
856 elfcpp::STV visibility,
857 unsigned char nonvis,
858 Symbol::Segment_offset_base offset_base,
859 bool only_if_ref)
860{
861 Sized_symbol<size>* sym;
862
863 if (target->is_big_endian())
593f47df 864 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
14b31740 865 target, name, version, only_if_ref
593f47df 866 SELECT_SIZE_ENDIAN(size, true));
ead1e424 867 else
593f47df 868 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
14b31740 869 target, name, version, only_if_ref
593f47df 870 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
871
872 if (sym == NULL)
14b31740 873 return NULL;
ead1e424
ILT
874
875 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
876 offset_base);
14b31740
ILT
877
878 return sym;
ead1e424
ILT
879}
880
881// Define a special symbol with a constant value. It is a multiple
882// definition error if this symbol is already defined.
883
14b31740
ILT
884Symbol*
885Symbol_table::define_as_constant(const Target* target, const char* name,
886 const char* version, uint64_t value,
887 uint64_t symsize, elfcpp::STT type,
888 elfcpp::STB binding, elfcpp::STV visibility,
889 unsigned char nonvis, bool only_if_ref)
ead1e424 890{
a3ad94ed 891 gold_assert(target->get_size() == this->size_);
ead1e424 892 if (this->size_ == 32)
14b31740
ILT
893 return this->do_define_as_constant<32>(target, name, version, value,
894 symsize, type, binding, visibility,
895 nonvis, only_if_ref);
ead1e424 896 else if (this->size_ == 64)
14b31740
ILT
897 return this->do_define_as_constant<64>(target, name, version, value,
898 symsize, type, binding, visibility,
899 nonvis, only_if_ref);
ead1e424 900 else
a3ad94ed 901 gold_unreachable();
ead1e424
ILT
902}
903
904// Define a symbol as a constant, sized version.
905
906template<int size>
14b31740 907Sized_symbol<size>*
ead1e424 908Symbol_table::do_define_as_constant(
14b31740 909 const Target* target,
ead1e424 910 const char* name,
14b31740 911 const char* version,
ead1e424
ILT
912 typename elfcpp::Elf_types<size>::Elf_Addr value,
913 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
914 elfcpp::STT type,
915 elfcpp::STB binding,
916 elfcpp::STV visibility,
917 unsigned char nonvis,
918 bool only_if_ref)
919{
920 Sized_symbol<size>* sym;
921
922 if (target->is_big_endian())
593f47df 923 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
14b31740 924 target, name, version, only_if_ref
593f47df 925 SELECT_SIZE_ENDIAN(size, true));
ead1e424 926 else
593f47df 927 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
14b31740 928 target, name, version, only_if_ref
593f47df 929 SELECT_SIZE_ENDIAN(size, false));
ead1e424
ILT
930
931 if (sym == NULL)
14b31740 932 return NULL;
ead1e424
ILT
933
934 sym->init(name, value, symsize, type, binding, visibility, nonvis);
14b31740
ILT
935
936 return sym;
ead1e424
ILT
937}
938
939// Define a set of symbols in output sections.
940
941void
14b31740
ILT
942Symbol_table::define_symbols(const Layout* layout, const Target* target,
943 int count, const Define_symbol_in_section* p)
ead1e424
ILT
944{
945 for (int i = 0; i < count; ++i, ++p)
946 {
947 Output_section* os = layout->find_output_section(p->output_section);
948 if (os != NULL)
14b31740
ILT
949 this->define_in_output_data(target, p->name, NULL, os, p->value,
950 p->size, p->type, p->binding,
951 p->visibility, p->nonvis,
952 p->offset_is_from_end, p->only_if_ref);
ead1e424 953 else
14b31740 954 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
955 p->binding, p->visibility, p->nonvis,
956 p->only_if_ref);
957 }
958}
959
960// Define a set of symbols in output segments.
961
962void
14b31740
ILT
963Symbol_table::define_symbols(const Layout* layout, const Target* target,
964 int count, const Define_symbol_in_segment* p)
ead1e424
ILT
965{
966 for (int i = 0; i < count; ++i, ++p)
967 {
968 Output_segment* os = layout->find_output_segment(p->segment_type,
969 p->segment_flags_set,
970 p->segment_flags_clear);
971 if (os != NULL)
14b31740
ILT
972 this->define_in_output_segment(target, p->name, NULL, os, p->value,
973 p->size, p->type, p->binding,
974 p->visibility, p->nonvis,
975 p->offset_base, p->only_if_ref);
ead1e424 976 else
14b31740 977 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
978 p->binding, p->visibility, p->nonvis,
979 p->only_if_ref);
980 }
981}
982
a3ad94ed
ILT
983// Set the dynamic symbol indexes. INDEX is the index of the first
984// global dynamic symbol. Pointers to the symbols are stored into the
985// vector SYMS. The names are added to DYNPOOL. This returns an
986// updated dynamic symbol index.
987
988unsigned int
14b31740
ILT
989Symbol_table::set_dynsym_indexes(const General_options* options,
990 const Target* target,
991 unsigned int index,
a3ad94ed 992 std::vector<Symbol*>* syms,
14b31740
ILT
993 Stringpool* dynpool,
994 Versions* versions)
a3ad94ed
ILT
995{
996 for (Symbol_table_type::iterator p = this->table_.begin();
997 p != this->table_.end();
998 ++p)
999 {
1000 Symbol* sym = p->second;
16649710
ILT
1001
1002 // Note that SYM may already have a dynamic symbol index, since
1003 // some symbols appear more than once in the symbol table, with
1004 // and without a version.
1005
a6badf5a
ILT
1006 if (!sym->needs_dynsym_entry()
1007 && (!options->export_dynamic()
1008 || !sym->in_reg()
1009 || !sym->is_externally_visible()))
16649710
ILT
1010 sym->set_dynsym_index(-1U);
1011 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1012 {
1013 sym->set_dynsym_index(index);
1014 ++index;
1015 syms->push_back(sym);
1016 dynpool->add(sym->name(), NULL);
14b31740
ILT
1017
1018 // Record any version information.
1019 if (sym->version() != NULL)
1020 versions->record_version(options, dynpool, sym);
a3ad94ed
ILT
1021 }
1022 }
1023
14b31740
ILT
1024 // Finish up the versions. In some cases this may add new dynamic
1025 // symbols.
1026 index = versions->finalize(target, this, index, syms);
1027
a3ad94ed
ILT
1028 return index;
1029}
1030
c06b7b0b
ILT
1031// Set the final values for all the symbols. The index of the first
1032// global symbol in the output file is INDEX. Record the file offset
75f65a3e 1033// OFF. Add their names to POOL. Return the new file offset.
54dc6425 1034
75f65a3e 1035off_t
16649710
ILT
1036Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1037 size_t dyn_global_index, size_t dyncount,
1038 Stringpool* pool)
54dc6425 1039{
f6ce93d6
ILT
1040 off_t ret;
1041
a3ad94ed 1042 gold_assert(index != 0);
c06b7b0b
ILT
1043 this->first_global_index_ = index;
1044
16649710
ILT
1045 this->dynamic_offset_ = dynoff;
1046 this->first_dynamic_global_index_ = dyn_global_index;
1047 this->dynamic_count_ = dyncount;
1048
75f65a3e 1049 if (this->size_ == 32)
c06b7b0b 1050 ret = this->sized_finalize<32>(index, off, pool);
61ba1cf9 1051 else if (this->size_ == 64)
c06b7b0b 1052 ret = this->sized_finalize<64>(index, off, pool);
61ba1cf9 1053 else
a3ad94ed 1054 gold_unreachable();
f6ce93d6
ILT
1055
1056 // Now that we have the final symbol table, we can reliably note
1057 // which symbols should get warnings.
1058 this->warnings_.note_warnings(this);
1059
1060 return ret;
75f65a3e
ILT
1061}
1062
ead1e424
ILT
1063// Set the final value for all the symbols. This is called after
1064// Layout::finalize, so all the output sections have their final
1065// address.
75f65a3e
ILT
1066
1067template<int size>
1068off_t
c06b7b0b 1069Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
75f65a3e 1070{
ead1e424 1071 off = align_address(off, size >> 3);
75f65a3e
ILT
1072 this->offset_ = off;
1073
c06b7b0b
ILT
1074 size_t orig_index = index;
1075
75f65a3e 1076 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1077 for (Symbol_table_type::iterator p = this->table_.begin();
1078 p != this->table_.end();
1079 ++p)
54dc6425 1080 {
75f65a3e 1081 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 1082
75f65a3e 1083 // FIXME: Here we need to decide which symbols should go into
a3ad94ed
ILT
1084 // the output file, based on --strip.
1085
1086 // The default version of a symbol may appear twice in the
1087 // symbol table. We only need to finalize it once.
1088 if (sym->has_symtab_index())
1089 continue;
75f65a3e 1090
008db82e
ILT
1091 if (!sym->in_reg())
1092 {
1093 gold_assert(!sym->has_symtab_index());
1094 sym->set_symtab_index(-1U);
1095 gold_assert(sym->dynsym_index() == -1U);
1096 continue;
1097 }
1098
ead1e424 1099 typename Sized_symbol<size>::Value_type value;
75f65a3e 1100
ead1e424 1101 switch (sym->source())
75f65a3e 1102 {
ead1e424
ILT
1103 case Symbol::FROM_OBJECT:
1104 {
16649710 1105 unsigned int shndx = sym->shndx();
ead1e424
ILT
1106
1107 // FIXME: We need some target specific support here.
16649710
ILT
1108 if (shndx >= elfcpp::SHN_LORESERVE
1109 && shndx != elfcpp::SHN_ABS)
ead1e424
ILT
1110 {
1111 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
16649710 1112 program_name, sym->name(), shndx);
ead1e424
ILT
1113 gold_exit(false);
1114 }
1115
f6ce93d6
ILT
1116 Object* symobj = sym->object();
1117 if (symobj->is_dynamic())
1118 {
1119 value = 0;
16649710 1120 shndx = elfcpp::SHN_UNDEF;
f6ce93d6 1121 }
16649710 1122 else if (shndx == elfcpp::SHN_UNDEF)
ead1e424 1123 value = 0;
16649710 1124 else if (shndx == elfcpp::SHN_ABS)
ead1e424
ILT
1125 value = sym->value();
1126 else
1127 {
f6ce93d6 1128 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1129 off_t secoff;
16649710 1130 Output_section* os = relobj->output_section(shndx, &secoff);
ead1e424
ILT
1131
1132 if (os == NULL)
1133 {
c06b7b0b 1134 sym->set_symtab_index(-1U);
16649710 1135 gold_assert(sym->dynsym_index() == -1U);
ead1e424
ILT
1136 continue;
1137 }
1138
1139 value = sym->value() + os->address() + secoff;
1140 }
1141 }
1142 break;
1143
1144 case Symbol::IN_OUTPUT_DATA:
1145 {
1146 Output_data* od = sym->output_data();
1147 value = sym->value() + od->address();
1148 if (sym->offset_is_from_end())
1149 value += od->data_size();
1150 }
1151 break;
1152
1153 case Symbol::IN_OUTPUT_SEGMENT:
1154 {
1155 Output_segment* os = sym->output_segment();
1156 value = sym->value() + os->vaddr();
1157 switch (sym->offset_base())
1158 {
1159 case Symbol::SEGMENT_START:
1160 break;
1161 case Symbol::SEGMENT_END:
1162 value += os->memsz();
1163 break;
1164 case Symbol::SEGMENT_BSS:
1165 value += os->filesz();
1166 break;
1167 default:
a3ad94ed 1168 gold_unreachable();
ead1e424
ILT
1169 }
1170 }
1171 break;
1172
1173 case Symbol::CONSTANT:
1174 value = sym->value();
1175 break;
1176
1177 default:
a3ad94ed 1178 gold_unreachable();
54dc6425 1179 }
ead1e424
ILT
1180
1181 sym->set_value(value);
c06b7b0b 1182 sym->set_symtab_index(index);
f0641a0b 1183 pool->add(sym->name(), NULL);
c06b7b0b 1184 ++index;
ead1e424 1185 off += sym_size;
54dc6425 1186 }
75f65a3e 1187
c06b7b0b 1188 this->output_count_ = index - orig_index;
61ba1cf9 1189
75f65a3e 1190 return off;
54dc6425
ILT
1191}
1192
61ba1cf9
ILT
1193// Write out the global symbols.
1194
1195void
1196Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
16649710 1197 const Stringpool* dynpool, Output_file* of) const
61ba1cf9
ILT
1198{
1199 if (this->size_ == 32)
1200 {
1201 if (target->is_big_endian())
16649710 1202 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
61ba1cf9 1203 else
16649710 1204 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
61ba1cf9
ILT
1205 }
1206 else if (this->size_ == 64)
1207 {
1208 if (target->is_big_endian())
16649710 1209 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
61ba1cf9 1210 else
16649710 1211 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
61ba1cf9
ILT
1212 }
1213 else
a3ad94ed 1214 gold_unreachable();
61ba1cf9
ILT
1215}
1216
1217// Write out the global symbols.
1218
1219template<int size, bool big_endian>
1220void
1221Symbol_table::sized_write_globals(const Target*,
1222 const Stringpool* sympool,
16649710 1223 const Stringpool* dynpool,
61ba1cf9
ILT
1224 Output_file* of) const
1225{
1226 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1227 unsigned int index = this->first_global_index_;
1228 const off_t oview_size = this->output_count_ * sym_size;
16649710
ILT
1229 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1230
1231 unsigned int dynamic_count = this->dynamic_count_;
1232 off_t dynamic_size = dynamic_count * sym_size;
1233 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1234 unsigned char* dynamic_view;
1235 if (this->dynamic_offset_ == 0)
1236 dynamic_view = NULL;
1237 else
1238 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 1239
61ba1cf9
ILT
1240 unsigned char* ps = psyms;
1241 for (Symbol_table_type::const_iterator p = this->table_.begin();
1242 p != this->table_.end();
1243 ++p)
1244 {
1245 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1246
a3ad94ed 1247 unsigned int sym_index = sym->symtab_index();
16649710
ILT
1248 unsigned int dynsym_index;
1249 if (dynamic_view == NULL)
1250 dynsym_index = -1U;
1251 else
1252 dynsym_index = sym->dynsym_index();
1253
1254 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
1255 {
1256 // This symbol is not included in the output file.
1257 continue;
1258 }
16649710
ILT
1259
1260 if (sym_index == index)
1261 ++index;
1262 else if (sym_index != -1U)
a3ad94ed
ILT
1263 {
1264 // We have already seen this symbol, because it has a
1265 // default version.
1266 gold_assert(sym_index < index);
16649710
ILT
1267 if (dynsym_index == -1U)
1268 continue;
1269 sym_index = -1U;
a3ad94ed 1270 }
c06b7b0b 1271
ead1e424
ILT
1272 unsigned int shndx;
1273 switch (sym->source())
1274 {
1275 case Symbol::FROM_OBJECT:
1276 {
16649710 1277 unsigned int in_shndx = sym->shndx();
ead1e424
ILT
1278
1279 // FIXME: We need some target specific support here.
16649710
ILT
1280 if (in_shndx >= elfcpp::SHN_LORESERVE
1281 && in_shndx != elfcpp::SHN_ABS)
ead1e424
ILT
1282 {
1283 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
16649710 1284 program_name, sym->name(), in_shndx);
ead1e424
ILT
1285 gold_exit(false);
1286 }
1287
f6ce93d6
ILT
1288 Object* symobj = sym->object();
1289 if (symobj->is_dynamic())
1290 {
1291 // FIXME.
1292 shndx = elfcpp::SHN_UNDEF;
1293 }
16649710
ILT
1294 else if (in_shndx == elfcpp::SHN_UNDEF
1295 || in_shndx == elfcpp::SHN_ABS)
1296 shndx = in_shndx;
ead1e424
ILT
1297 else
1298 {
f6ce93d6 1299 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1300 off_t secoff;
16649710 1301 Output_section* os = relobj->output_section(in_shndx, &secoff);
a3ad94ed 1302 gold_assert(os != NULL);
ead1e424
ILT
1303 shndx = os->out_shndx();
1304 }
1305 }
1306 break;
1307
1308 case Symbol::IN_OUTPUT_DATA:
1309 shndx = sym->output_data()->out_shndx();
1310 break;
1311
1312 case Symbol::IN_OUTPUT_SEGMENT:
1313 shndx = elfcpp::SHN_ABS;
1314 break;
1315
1316 case Symbol::CONSTANT:
1317 shndx = elfcpp::SHN_ABS;
1318 break;
1319
1320 default:
a3ad94ed 1321 gold_unreachable();
ead1e424 1322 }
61ba1cf9 1323
16649710
ILT
1324 if (sym_index != -1U)
1325 {
6a469986
ILT
1326 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1327 sym, shndx, sympool, ps
1328 SELECT_SIZE_ENDIAN(size, big_endian));
16649710
ILT
1329 ps += sym_size;
1330 }
61ba1cf9 1331
16649710
ILT
1332 if (dynsym_index != -1U)
1333 {
1334 dynsym_index -= first_dynamic_global_index;
1335 gold_assert(dynsym_index < dynamic_count);
1336 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
6a469986
ILT
1337 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1338 sym, shndx, dynpool, pd
1339 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1340 }
61ba1cf9
ILT
1341 }
1342
a3ad94ed 1343 gold_assert(ps - psyms == oview_size);
c06b7b0b
ILT
1344
1345 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
1346 if (dynamic_view != NULL)
1347 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1348}
1349
1350// Write out the symbol SYM, in section SHNDX, to P. POOL is the
1351// strtab holding the name.
1352
1353template<int size, bool big_endian>
1354void
1355Symbol_table::sized_write_symbol(Sized_symbol<size>* sym,
1356 unsigned int shndx,
1357 const Stringpool* pool,
6a469986
ILT
1358 unsigned char* p
1359 ACCEPT_SIZE_ENDIAN) const
16649710
ILT
1360{
1361 elfcpp::Sym_write<size, big_endian> osym(p);
1362 osym.put_st_name(pool->get_offset(sym->name()));
1363 osym.put_st_value(sym->value());
1364 osym.put_st_size(sym->symsize());
1365 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1366 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1367 osym.put_st_shndx(shndx);
61ba1cf9
ILT
1368}
1369
a3ad94ed
ILT
1370// Write out a section symbol. Return the update offset.
1371
1372void
1373Symbol_table::write_section_symbol(const Target* target,
1374 const Output_section *os,
1375 Output_file* of,
1376 off_t offset) const
1377{
1378 if (this->size_ == 32)
1379 {
1380 if (target->is_big_endian())
1381 this->sized_write_section_symbol<32, true>(os, of, offset);
1382 else
1383 this->sized_write_section_symbol<32, false>(os, of, offset);
1384 }
1385 else if (this->size_ == 64)
1386 {
1387 if (target->is_big_endian())
1388 this->sized_write_section_symbol<64, true>(os, of, offset);
1389 else
1390 this->sized_write_section_symbol<64, false>(os, of, offset);
1391 }
1392 else
1393 gold_unreachable();
1394}
1395
1396// Write out a section symbol, specialized for size and endianness.
1397
1398template<int size, bool big_endian>
1399void
1400Symbol_table::sized_write_section_symbol(const Output_section* os,
1401 Output_file* of,
1402 off_t offset) const
1403{
1404 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1405
1406 unsigned char* pov = of->get_output_view(offset, sym_size);
1407
1408 elfcpp::Sym_write<size, big_endian> osym(pov);
1409 osym.put_st_name(0);
1410 osym.put_st_value(os->address());
1411 osym.put_st_size(0);
1412 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1413 elfcpp::STT_SECTION));
1414 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1415 osym.put_st_shndx(os->out_shndx());
1416
1417 of->write_output_view(offset, sym_size, pov);
1418}
1419
f6ce93d6
ILT
1420// Warnings functions.
1421
1422// Add a new warning.
1423
1424void
1425Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1426 unsigned int shndx)
1427{
1428 name = symtab->canonicalize_name(name);
1429 this->warnings_[name].set(obj, shndx);
1430}
1431
1432// Look through the warnings and mark the symbols for which we should
1433// warn. This is called during Layout::finalize when we know the
1434// sources for all the symbols.
1435
1436void
1437Warnings::note_warnings(Symbol_table* symtab)
1438{
1439 for (Warning_table::iterator p = this->warnings_.begin();
1440 p != this->warnings_.end();
1441 ++p)
1442 {
1443 Symbol* sym = symtab->lookup(p->first, NULL);
1444 if (sym != NULL
1445 && sym->source() == Symbol::FROM_OBJECT
1446 && sym->object() == p->second.object)
1447 {
1448 sym->set_has_warning();
1449
1450 // Read the section contents to get the warning text. It
1451 // would be nicer if we only did this if we have to actually
1452 // issue a warning. Unfortunately, warnings are issued as
1453 // we relocate sections. That means that we can not lock
1454 // the object then, as we might try to issue the same
1455 // warning multiple times simultaneously.
645f8123
ILT
1456 {
1457 Task_locker_obj<Object> tl(*p->second.object);
1458 const unsigned char* c;
1459 off_t len;
1460 c = p->second.object->section_contents(p->second.shndx, &len);
1461 p->second.set_text(reinterpret_cast<const char*>(c), len);
1462 }
f6ce93d6
ILT
1463 }
1464 }
1465}
1466
1467// Issue a warning. This is called when we see a relocation against a
1468// symbol for which has a warning.
1469
1470void
c06b7b0b 1471Warnings::issue_warning(const Symbol* sym, const std::string& location) const
f6ce93d6 1472{
a3ad94ed 1473 gold_assert(sym->has_warning());
f6ce93d6 1474 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 1475 gold_assert(p != this->warnings_.end());
f6ce93d6
ILT
1476 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1477 p->second.text.c_str());
1478}
1479
14bfc3f5
ILT
1480// Instantiate the templates we need. We could use the configure
1481// script to restrict this to only the ones needed for implemented
1482// targets.
1483
1484template
1485void
dbe717ef
ILT
1486Symbol_table::add_from_relobj<32, true>(
1487 Sized_relobj<32, true>* relobj,
f6ce93d6 1488 const unsigned char* syms,
14bfc3f5
ILT
1489 size_t count,
1490 const char* sym_names,
1491 size_t sym_name_size,
1492 Symbol** sympointers);
1493
1494template
1495void
dbe717ef
ILT
1496Symbol_table::add_from_relobj<32, false>(
1497 Sized_relobj<32, false>* relobj,
f6ce93d6 1498 const unsigned char* syms,
14bfc3f5
ILT
1499 size_t count,
1500 const char* sym_names,
1501 size_t sym_name_size,
1502 Symbol** sympointers);
1503
1504template
1505void
dbe717ef
ILT
1506Symbol_table::add_from_relobj<64, true>(
1507 Sized_relobj<64, true>* relobj,
f6ce93d6 1508 const unsigned char* syms,
14bfc3f5
ILT
1509 size_t count,
1510 const char* sym_names,
1511 size_t sym_name_size,
1512 Symbol** sympointers);
1513
1514template
1515void
dbe717ef
ILT
1516Symbol_table::add_from_relobj<64, false>(
1517 Sized_relobj<64, false>* relobj,
f6ce93d6 1518 const unsigned char* syms,
14bfc3f5
ILT
1519 size_t count,
1520 const char* sym_names,
1521 size_t sym_name_size,
1522 Symbol** sympointers);
1523
dbe717ef
ILT
1524template
1525void
1526Symbol_table::add_from_dynobj<32, true>(
1527 Sized_dynobj<32, true>* dynobj,
1528 const unsigned char* syms,
1529 size_t count,
1530 const char* sym_names,
1531 size_t sym_name_size,
1532 const unsigned char* versym,
1533 size_t versym_size,
1534 const std::vector<const char*>* version_map);
1535
1536template
1537void
1538Symbol_table::add_from_dynobj<32, false>(
1539 Sized_dynobj<32, false>* dynobj,
1540 const unsigned char* syms,
1541 size_t count,
1542 const char* sym_names,
1543 size_t sym_name_size,
1544 const unsigned char* versym,
1545 size_t versym_size,
1546 const std::vector<const char*>* version_map);
1547
1548template
1549void
1550Symbol_table::add_from_dynobj<64, true>(
1551 Sized_dynobj<64, true>* dynobj,
1552 const unsigned char* syms,
1553 size_t count,
1554 const char* sym_names,
1555 size_t sym_name_size,
1556 const unsigned char* versym,
1557 size_t versym_size,
1558 const std::vector<const char*>* version_map);
1559
1560template
1561void
1562Symbol_table::add_from_dynobj<64, false>(
1563 Sized_dynobj<64, false>* dynobj,
1564 const unsigned char* syms,
1565 size_t count,
1566 const char* sym_names,
1567 size_t sym_name_size,
1568 const unsigned char* versym,
1569 size_t versym_size,
1570 const std::vector<const char*>* version_map);
1571
14bfc3f5 1572} // End namespace gold.