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