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