]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
*** empty log message ***
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
14bfc3f5
ILT
23#include "gold.h"
24
14bfc3f5 25#include <stdint.h>
70e654ba 26#include <set>
14bfc3f5
ILT
27#include <string>
28#include <utility>
29
30#include "object.h"
70e654ba 31#include "dwarf_reader.h"
dbe717ef 32#include "dynobj.h"
75f65a3e 33#include "output.h"
61ba1cf9 34#include "target.h"
645f8123 35#include "workqueue.h"
14bfc3f5
ILT
36#include "symtab.h"
37
38namespace gold
39{
40
41// Class Symbol.
42
ead1e424
ILT
43// Initialize fields in Symbol. This initializes everything except u_
44// and source_.
14bfc3f5 45
14bfc3f5 46void
ead1e424
ILT
47Symbol::init_fields(const char* name, const char* version,
48 elfcpp::STT type, elfcpp::STB binding,
49 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
50{
51 this->name_ = name;
52 this->version_ = version;
c06b7b0b
ILT
53 this->symtab_index_ = 0;
54 this->dynsym_index_ = 0;
ead1e424 55 this->got_offset_ = 0;
f4151f89 56 this->plt_offset_ = 0;
ead1e424
ILT
57 this->type_ = type;
58 this->binding_ = binding;
59 this->visibility_ = visibility;
60 this->nonvis_ = nonvis;
61 this->is_target_special_ = false;
1564db8d
ILT
62 this->is_def_ = false;
63 this->is_forwarder_ = false;
aeddab66 64 this->has_alias_ = false;
c06b7b0b 65 this->needs_dynsym_entry_ = false;
008db82e 66 this->in_reg_ = false;
ead1e424
ILT
67 this->in_dyn_ = false;
68 this->has_got_offset_ = false;
f4151f89 69 this->has_plt_offset_ = false;
f6ce93d6 70 this->has_warning_ = false;
46fe1623 71 this->is_copied_from_dynobj_ = false;
d61c6bd4 72 this->needs_value_in_got_ = false;
ead1e424
ILT
73}
74
75// Initialize the fields in the base class Symbol for SYM in OBJECT.
76
77template<int size, bool big_endian>
78void
79Symbol::init_base(const char* name, const char* version, Object* object,
80 const elfcpp::Sym<size, big_endian>& sym)
81{
82 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
83 sym.get_st_visibility(), sym.get_st_nonvis());
84 this->u_.from_object.object = object;
85 // FIXME: Handle SHN_XINDEX.
16649710 86 this->u_.from_object.shndx = sym.get_st_shndx();
ead1e424 87 this->source_ = FROM_OBJECT;
008db82e 88 this->in_reg_ = !object->is_dynamic();
1564db8d 89 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
90}
91
ead1e424
ILT
92// Initialize the fields in the base class Symbol for a symbol defined
93// in an Output_data.
94
95void
96Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
97 elfcpp::STB binding, elfcpp::STV visibility,
98 unsigned char nonvis, bool offset_is_from_end)
99{
100 this->init_fields(name, NULL, type, binding, visibility, nonvis);
101 this->u_.in_output_data.output_data = od;
102 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
103 this->source_ = IN_OUTPUT_DATA;
008db82e 104 this->in_reg_ = true;
ead1e424
ILT
105}
106
107// Initialize the fields in the base class Symbol for a symbol defined
108// in an Output_segment.
109
110void
111Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
112 elfcpp::STB binding, elfcpp::STV visibility,
113 unsigned char nonvis, Segment_offset_base offset_base)
114{
115 this->init_fields(name, NULL, type, binding, visibility, nonvis);
116 this->u_.in_output_segment.output_segment = os;
117 this->u_.in_output_segment.offset_base = offset_base;
118 this->source_ = IN_OUTPUT_SEGMENT;
008db82e 119 this->in_reg_ = true;
ead1e424
ILT
120}
121
122// Initialize the fields in the base class Symbol for a symbol defined
123// as a constant.
124
125void
126Symbol::init_base(const char* name, elfcpp::STT type,
127 elfcpp::STB binding, elfcpp::STV visibility,
128 unsigned char nonvis)
129{
130 this->init_fields(name, NULL, type, binding, visibility, nonvis);
131 this->source_ = CONSTANT;
008db82e 132 this->in_reg_ = true;
ead1e424
ILT
133}
134
135// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
136
137template<int size>
138template<bool big_endian>
139void
140Sized_symbol<size>::init(const char* name, const char* version, Object* object,
141 const elfcpp::Sym<size, big_endian>& sym)
142{
143 this->init_base(name, version, object, sym);
144 this->value_ = sym.get_st_value();
ead1e424
ILT
145 this->symsize_ = sym.get_st_size();
146}
147
148// Initialize the fields in Sized_symbol for a symbol defined in an
149// Output_data.
150
151template<int size>
152void
153Sized_symbol<size>::init(const char* name, Output_data* od,
154 Value_type value, Size_type symsize,
155 elfcpp::STT type, elfcpp::STB binding,
156 elfcpp::STV visibility, unsigned char nonvis,
157 bool offset_is_from_end)
158{
159 this->init_base(name, od, type, binding, visibility, nonvis,
160 offset_is_from_end);
161 this->value_ = value;
162 this->symsize_ = symsize;
163}
164
165// Initialize the fields in Sized_symbol for a symbol defined in an
166// Output_segment.
167
168template<int size>
169void
170Sized_symbol<size>::init(const char* name, Output_segment* os,
171 Value_type value, Size_type symsize,
172 elfcpp::STT type, elfcpp::STB binding,
173 elfcpp::STV visibility, unsigned char nonvis,
174 Segment_offset_base offset_base)
175{
176 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
177 this->value_ = value;
178 this->symsize_ = symsize;
179}
180
181// Initialize the fields in Sized_symbol for a symbol defined as a
182// constant.
183
184template<int size>
185void
186Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
187 elfcpp::STT type, elfcpp::STB binding,
188 elfcpp::STV visibility, unsigned char nonvis)
189{
190 this->init_base(name, type, binding, visibility, nonvis);
191 this->value_ = value;
192 this->symsize_ = symsize;
14bfc3f5
ILT
193}
194
436ca963
ILT
195// Return true if this symbol should be added to the dynamic symbol
196// table.
197
198inline bool
199Symbol::should_add_dynsym_entry() const
200{
201 // If the symbol is used by a dynamic relocation, we need to add it.
202 if (this->needs_dynsym_entry())
203 return true;
204
205 // If exporting all symbols or building a shared library,
206 // and the symbol is defined in a regular object and is
207 // externally visible, we need to add it.
208 if ((parameters->export_dynamic() || parameters->output_is_shared())
209 && !this->is_from_dynobj()
210 && this->is_externally_visible())
211 return true;
212
213 return false;
214}
215
b3b74ddc
ILT
216// Return true if the final value of this symbol is known at link
217// time.
218
219bool
220Symbol::final_value_is_known() const
221{
222 // If we are not generating an executable, then no final values are
223 // known, since they will change at runtime.
224 if (!parameters->output_is_executable())
225 return false;
226
227 // If the symbol is not from an object file, then it is defined, and
228 // known.
229 if (this->source_ != FROM_OBJECT)
230 return true;
231
232 // If the symbol is from a dynamic object, then the final value is
233 // not known.
234 if (this->object()->is_dynamic())
235 return false;
236
237 // If the symbol is not undefined (it is defined or common), then
238 // the final value is known.
239 if (!this->is_undefined())
240 return true;
241
242 // If the symbol is undefined, then whether the final value is known
243 // depends on whether we are doing a static link. If we are doing a
244 // dynamic link, then the final value could be filled in at runtime.
245 // This could reasonably be the case for a weak undefined symbol.
246 return parameters->doing_static_link();
247}
248
14bfc3f5
ILT
249// Class Symbol_table.
250
251Symbol_table::Symbol_table()
9025d29d 252 : saw_undefined_(0), offset_(0), table_(), namepool_(),
f6ce93d6 253 forwarders_(), commons_(), warnings_()
14bfc3f5
ILT
254{
255}
256
257Symbol_table::~Symbol_table()
258{
259}
260
261// The hash function. The key is always canonicalized, so we use a
262// simple combination of the pointers.
263
264size_t
265Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
266{
f0641a0b 267 return key.first ^ key.second;
14bfc3f5
ILT
268}
269
270// The symbol table key equality function. This is only called with
271// canonicalized name and version strings, so we can use pointer
272// comparison.
273
274bool
275Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
276 const Symbol_table_key& k2) const
277{
278 return k1.first == k2.first && k1.second == k2.second;
279}
280
281// Make TO a symbol which forwards to FROM.
282
283void
284Symbol_table::make_forwarder(Symbol* from, Symbol* to)
285{
a3ad94ed
ILT
286 gold_assert(from != to);
287 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
288 this->forwarders_[from] = to;
289 from->set_forwarder();
290}
291
61ba1cf9
ILT
292// Resolve the forwards from FROM, returning the real symbol.
293
14bfc3f5 294Symbol*
c06b7b0b 295Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 296{
a3ad94ed 297 gold_assert(from->is_forwarder());
c06b7b0b 298 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 299 this->forwarders_.find(from);
a3ad94ed 300 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
301 return p->second;
302}
303
61ba1cf9
ILT
304// Look up a symbol by name.
305
306Symbol*
307Symbol_table::lookup(const char* name, const char* version) const
308{
f0641a0b
ILT
309 Stringpool::Key name_key;
310 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
311 if (name == NULL)
312 return NULL;
f0641a0b
ILT
313
314 Stringpool::Key version_key = 0;
61ba1cf9
ILT
315 if (version != NULL)
316 {
f0641a0b 317 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
318 if (version == NULL)
319 return NULL;
320 }
321
f0641a0b 322 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
323 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
324 if (p == this->table_.end())
325 return NULL;
326 return p->second;
327}
328
14bfc3f5
ILT
329// Resolve a Symbol with another Symbol. This is only used in the
330// unusual case where there are references to both an unversioned
331// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
332// version is the default version. Because this is unusual, we do
333// this the slow way, by converting back to an ELF symbol.
14bfc3f5 334
1564db8d 335template<int size, bool big_endian>
14bfc3f5 336void
14b31740
ILT
337Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
338 const char* version ACCEPT_SIZE_ENDIAN)
14bfc3f5 339{
1564db8d
ILT
340 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
341 elfcpp::Sym_write<size, big_endian> esym(buf);
342 // We don't bother to set the st_name field.
343 esym.put_st_value(from->value());
344 esym.put_st_size(from->symsize());
345 esym.put_st_info(from->binding(), from->type());
ead1e424 346 esym.put_st_other(from->visibility(), from->nonvis());
16649710 347 esym.put_st_shndx(from->shndx());
70e654ba 348 this->resolve(to, esym.sym(), esym.sym(), from->object(), version);
1ebd95fd
ILT
349 if (from->in_reg())
350 to->set_in_reg();
351 if (from->in_dyn())
352 to->set_in_dyn();
14bfc3f5
ILT
353}
354
355// Add one symbol from OBJECT to the symbol table. NAME is symbol
356// name and VERSION is the version; both are canonicalized. DEF is
357// whether this is the default version.
358
359// If DEF is true, then this is the definition of a default version of
360// a symbol. That means that any lookup of NAME/NULL and any lookup
361// of NAME/VERSION should always return the same symbol. This is
362// obvious for references, but in particular we want to do this for
363// definitions: overriding NAME/NULL should also override
364// NAME/VERSION. If we don't do that, it would be very hard to
365// override functions in a shared library which uses versioning.
366
367// We implement this by simply making both entries in the hash table
368// point to the same Symbol structure. That is easy enough if this is
369// the first time we see NAME/NULL or NAME/VERSION, but it is possible
370// that we have seen both already, in which case they will both have
371// independent entries in the symbol table. We can't simply change
372// the symbol table entry, because we have pointers to the entries
373// attached to the object files. So we mark the entry attached to the
374// object file as a forwarder, and record it in the forwarders_ map.
375// Note that entries in the hash table will never be marked as
376// forwarders.
70e654ba
ILT
377//
378// SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
379// symbol exactly as it existed in the input file. SYM is usually
380// that as well, but can be modified, for instance if we determine
381// it's in a to-be-discarded section.
14bfc3f5
ILT
382
383template<int size, bool big_endian>
aeddab66 384Sized_symbol<size>*
f6ce93d6 385Symbol_table::add_from_object(Object* object,
14bfc3f5 386 const char *name,
f0641a0b
ILT
387 Stringpool::Key name_key,
388 const char *version,
389 Stringpool::Key version_key,
390 bool def,
70e654ba
ILT
391 const elfcpp::Sym<size, big_endian>& sym,
392 const elfcpp::Sym<size, big_endian>& orig_sym)
14bfc3f5
ILT
393{
394 Symbol* const snull = NULL;
395 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
396 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
397 snull));
14bfc3f5
ILT
398
399 std::pair<typename Symbol_table_type::iterator, bool> insdef =
400 std::make_pair(this->table_.end(), false);
401 if (def)
402 {
f0641a0b
ILT
403 const Stringpool::Key vnull_key = 0;
404 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
405 vnull_key),
14bfc3f5
ILT
406 snull));
407 }
408
409 // ins.first: an iterator, which is a pointer to a pair.
410 // ins.first->first: the key (a pair of name and version).
411 // ins.first->second: the value (Symbol*).
412 // ins.second: true if new entry was inserted, false if not.
413
1564db8d 414 Sized_symbol<size>* ret;
ead1e424
ILT
415 bool was_undefined;
416 bool was_common;
14bfc3f5
ILT
417 if (!ins.second)
418 {
419 // We already have an entry for NAME/VERSION.
593f47df
ILT
420 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
421 SELECT_SIZE(size));
a3ad94ed 422 gold_assert(ret != NULL);
ead1e424
ILT
423
424 was_undefined = ret->is_undefined();
425 was_common = ret->is_common();
426
70e654ba 427 this->resolve(ret, sym, orig_sym, object, version);
14bfc3f5
ILT
428
429 if (def)
430 {
431 if (insdef.second)
432 {
433 // This is the first time we have seen NAME/NULL. Make
434 // NAME/NULL point to NAME/VERSION.
435 insdef.first->second = ret;
436 }
dbe717ef 437 else if (insdef.first->second != ret)
14bfc3f5
ILT
438 {
439 // This is the unfortunate case where we already have
440 // entries for both NAME/VERSION and NAME/NULL.
274e99f9 441 const Sized_symbol<size>* sym2;
593f47df 442 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d
ILT
443 insdef.first->second
444 SELECT_SIZE(size));
593f47df 445 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
14b31740 446 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
14bfc3f5
ILT
447 this->make_forwarder(insdef.first->second, ret);
448 insdef.first->second = ret;
449 }
450 }
451 }
452 else
453 {
454 // This is the first time we have seen NAME/VERSION.
a3ad94ed 455 gold_assert(ins.first->second == NULL);
ead1e424
ILT
456
457 was_undefined = false;
458 was_common = false;
459
14bfc3f5
ILT
460 if (def && !insdef.second)
461 {
14b31740
ILT
462 // We already have an entry for NAME/NULL. If we override
463 // it, then change it to NAME/VERSION.
593f47df
ILT
464 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
465 insdef.first->second
466 SELECT_SIZE(size));
70e654ba 467 this->resolve(ret, sym, orig_sym, object, version);
14bfc3f5
ILT
468 ins.first->second = ret;
469 }
470 else
471 {
f6ce93d6
ILT
472 Sized_target<size, big_endian>* target =
473 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
474 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
1564db8d
ILT
475 if (!target->has_make_symbol())
476 ret = new Sized_symbol<size>();
477 else
14bfc3f5 478 {
1564db8d
ILT
479 ret = target->make_symbol();
480 if (ret == NULL)
14bfc3f5
ILT
481 {
482 // This means that we don't want a symbol table
483 // entry after all.
484 if (!def)
485 this->table_.erase(ins.first);
486 else
487 {
488 this->table_.erase(insdef.first);
489 // Inserting insdef invalidated ins.
f0641a0b
ILT
490 this->table_.erase(std::make_pair(name_key,
491 version_key));
14bfc3f5
ILT
492 }
493 return NULL;
494 }
495 }
14bfc3f5 496
1564db8d
ILT
497 ret->init(name, version, object, sym);
498
14bfc3f5
ILT
499 ins.first->second = ret;
500 if (def)
501 {
502 // This is the first time we have seen NAME/NULL. Point
503 // it at the new entry for NAME/VERSION.
a3ad94ed 504 gold_assert(insdef.second);
14bfc3f5
ILT
505 insdef.first->second = ret;
506 }
507 }
508 }
509
ead1e424
ILT
510 // Record every time we see a new undefined symbol, to speed up
511 // archive groups.
512 if (!was_undefined && ret->is_undefined())
513 ++this->saw_undefined_;
514
515 // Keep track of common symbols, to speed up common symbol
516 // allocation.
517 if (!was_common && ret->is_common())
518 this->commons_.push_back(ret);
519
14bfc3f5
ILT
520 return ret;
521}
522
f6ce93d6 523// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
524
525template<int size, bool big_endian>
526void
dbe717ef
ILT
527Symbol_table::add_from_relobj(
528 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 529 const unsigned char* syms,
14bfc3f5
ILT
530 size_t count,
531 const char* sym_names,
532 size_t sym_name_size,
730cdc88 533 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
14bfc3f5 534{
9025d29d
ILT
535 gold_assert(size == relobj->target()->get_size());
536 gold_assert(size == parameters->get_size());
14bfc3f5 537
a783673b
ILT
538 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
539
f6ce93d6 540 const unsigned char* p = syms;
a783673b 541 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
542 {
543 elfcpp::Sym<size, big_endian> sym(p);
a783673b 544 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 545
a783673b 546 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
547 if (st_name >= sym_name_size)
548 {
75f2446e
ILT
549 relobj->error(_("bad global symbol name offset %u at %zu"),
550 st_name, i);
551 continue;
14bfc3f5
ILT
552 }
553
dbe717ef
ILT
554 const char* name = sym_names + st_name;
555
a783673b
ILT
556 // A symbol defined in a section which we are not including must
557 // be treated as an undefined symbol.
558 unsigned char symbuf[sym_size];
559 elfcpp::Sym<size, big_endian> sym2(symbuf);
560 unsigned int st_shndx = psym->get_st_shndx();
561 if (st_shndx != elfcpp::SHN_UNDEF
562 && st_shndx < elfcpp::SHN_LORESERVE
dbe717ef 563 && !relobj->is_section_included(st_shndx))
a783673b
ILT
564 {
565 memcpy(symbuf, p, sym_size);
566 elfcpp::Sym_write<size, big_endian> sw(symbuf);
567 sw.put_st_shndx(elfcpp::SHN_UNDEF);
568 psym = &sym2;
569 }
570
14bfc3f5
ILT
571 // In an object file, an '@' in the name separates the symbol
572 // name from the version name. If there are two '@' characters,
573 // this is the default version.
574 const char* ver = strchr(name, '@');
575
aeddab66 576 Sized_symbol<size>* res;
14bfc3f5
ILT
577 if (ver == NULL)
578 {
f0641a0b 579 Stringpool::Key name_key;
cfd73a4e 580 name = this->namepool_.add(name, true, &name_key);
dbe717ef 581 res = this->add_from_object(relobj, name, name_key, NULL, 0,
70e654ba 582 false, *psym, sym);
14bfc3f5
ILT
583 }
584 else
585 {
f0641a0b 586 Stringpool::Key name_key;
cfd73a4e 587 name = this->namepool_.add_prefix(name, ver - name, &name_key);
f0641a0b 588
14bfc3f5
ILT
589 bool def = false;
590 ++ver;
591 if (*ver == '@')
592 {
593 def = true;
594 ++ver;
595 }
f0641a0b
ILT
596
597 Stringpool::Key ver_key;
cfd73a4e 598 ver = this->namepool_.add(ver, true, &ver_key);
f0641a0b 599
dbe717ef 600 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
70e654ba 601 def, *psym, sym);
14bfc3f5
ILT
602 }
603
730cdc88 604 (*sympointers)[i] = res;
14bfc3f5
ILT
605 }
606}
607
dbe717ef
ILT
608// Add all the symbols in a dynamic object to the hash table.
609
610template<int size, bool big_endian>
611void
612Symbol_table::add_from_dynobj(
613 Sized_dynobj<size, big_endian>* dynobj,
614 const unsigned char* syms,
615 size_t count,
616 const char* sym_names,
617 size_t sym_name_size,
618 const unsigned char* versym,
619 size_t versym_size,
620 const std::vector<const char*>* version_map)
621{
9025d29d
ILT
622 gold_assert(size == dynobj->target()->get_size());
623 gold_assert(size == parameters->get_size());
dbe717ef
ILT
624
625 if (versym != NULL && versym_size / 2 < count)
626 {
75f2446e
ILT
627 dynobj->error(_("too few symbol versions"));
628 return;
dbe717ef
ILT
629 }
630
631 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
632
aeddab66
ILT
633 // We keep a list of all STT_OBJECT symbols, so that we can resolve
634 // weak aliases. This is necessary because if the dynamic object
635 // provides the same variable under two names, one of which is a
636 // weak definition, and the regular object refers to the weak
637 // definition, we have to put both the weak definition and the
638 // strong definition into the dynamic symbol table. Given a weak
639 // definition, the only way that we can find the corresponding
640 // strong definition, if any, is to search the symbol table.
641 std::vector<Sized_symbol<size>*> object_symbols;
642
dbe717ef
ILT
643 const unsigned char* p = syms;
644 const unsigned char* vs = versym;
645 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
646 {
647 elfcpp::Sym<size, big_endian> sym(p);
648
649 // Ignore symbols with local binding.
650 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
651 continue;
652
653 unsigned int st_name = sym.get_st_name();
654 if (st_name >= sym_name_size)
655 {
75f2446e
ILT
656 dynobj->error(_("bad symbol name offset %u at %zu"),
657 st_name, i);
658 continue;
dbe717ef
ILT
659 }
660
661 const char* name = sym_names + st_name;
662
aeddab66
ILT
663 Sized_symbol<size>* res;
664
dbe717ef
ILT
665 if (versym == NULL)
666 {
667 Stringpool::Key name_key;
cfd73a4e 668 name = this->namepool_.add(name, true, &name_key);
aeddab66 669 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 670 false, sym, sym);
dbe717ef 671 }
aeddab66
ILT
672 else
673 {
674 // Read the version information.
dbe717ef 675
aeddab66 676 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
dbe717ef 677
aeddab66
ILT
678 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
679 v &= elfcpp::VERSYM_VERSION;
dbe717ef 680
aeddab66
ILT
681 // The Sun documentation says that V can be VER_NDX_LOCAL,
682 // or VER_NDX_GLOBAL, or a version index. The meaning of
683 // VER_NDX_LOCAL is defined as "Symbol has local scope."
684 // The old GNU linker will happily generate VER_NDX_LOCAL
685 // for an undefined symbol. I don't know what the Sun
686 // linker will generate.
dbe717ef 687
aeddab66
ILT
688 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
689 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
690 {
691 // This symbol should not be visible outside the object.
692 continue;
693 }
64707334 694
aeddab66
ILT
695 // At this point we are definitely going to add this symbol.
696 Stringpool::Key name_key;
697 name = this->namepool_.add(name, true, &name_key);
dbe717ef 698
aeddab66
ILT
699 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
700 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
701 {
702 // This symbol does not have a version.
703 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 704 false, sym, sym);
aeddab66
ILT
705 }
706 else
707 {
708 if (v >= version_map->size())
709 {
710 dynobj->error(_("versym for symbol %zu out of range: %u"),
711 i, v);
712 continue;
713 }
dbe717ef 714
aeddab66
ILT
715 const char* version = (*version_map)[v];
716 if (version == NULL)
717 {
718 dynobj->error(_("versym for symbol %zu has no name: %u"),
719 i, v);
720 continue;
721 }
dbe717ef 722
aeddab66
ILT
723 Stringpool::Key version_key;
724 version = this->namepool_.add(version, true, &version_key);
725
726 // If this is an absolute symbol, and the version name
727 // and symbol name are the same, then this is the
728 // version definition symbol. These symbols exist to
729 // support using -u to pull in particular versions. We
730 // do not want to record a version for them.
731 if (sym.get_st_shndx() == elfcpp::SHN_ABS
732 && name_key == version_key)
733 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 734 false, sym, sym);
aeddab66
ILT
735 else
736 {
737 const bool def = (!hidden
738 && (sym.get_st_shndx()
739 != elfcpp::SHN_UNDEF));
740 res = this->add_from_object(dynobj, name, name_key, version,
70e654ba 741 version_key, def, sym, sym);
aeddab66
ILT
742 }
743 }
dbe717ef
ILT
744 }
745
aeddab66
ILT
746 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
747 && sym.get_st_type() == elfcpp::STT_OBJECT)
748 object_symbols.push_back(res);
749 }
750
751 this->record_weak_aliases(&object_symbols);
752}
753
754// This is used to sort weak aliases. We sort them first by section
755// index, then by offset, then by weak ahead of strong.
756
757template<int size>
758class Weak_alias_sorter
759{
760 public:
761 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
762};
763
764template<int size>
765bool
766Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
767 const Sized_symbol<size>* s2) const
768{
769 if (s1->shndx() != s2->shndx())
770 return s1->shndx() < s2->shndx();
771 if (s1->value() != s2->value())
772 return s1->value() < s2->value();
773 if (s1->binding() != s2->binding())
774 {
775 if (s1->binding() == elfcpp::STB_WEAK)
776 return true;
777 if (s2->binding() == elfcpp::STB_WEAK)
778 return false;
779 }
780 return std::string(s1->name()) < std::string(s2->name());
781}
dbe717ef 782
aeddab66
ILT
783// SYMBOLS is a list of object symbols from a dynamic object. Look
784// for any weak aliases, and record them so that if we add the weak
785// alias to the dynamic symbol table, we also add the corresponding
786// strong symbol.
dbe717ef 787
aeddab66
ILT
788template<int size>
789void
790Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
791{
792 // Sort the vector by section index, then by offset, then by weak
793 // ahead of strong.
794 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
795
796 // Walk through the vector. For each weak definition, record
797 // aliases.
798 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
799 symbols->begin();
800 p != symbols->end();
801 ++p)
802 {
803 if ((*p)->binding() != elfcpp::STB_WEAK)
804 continue;
805
806 // Build a circular list of weak aliases. Each symbol points to
807 // the next one in the circular list.
808
809 Sized_symbol<size>* from_sym = *p;
810 typename std::vector<Sized_symbol<size>*>::const_iterator q;
811 for (q = p + 1; q != symbols->end(); ++q)
dbe717ef 812 {
aeddab66
ILT
813 if ((*q)->shndx() != from_sym->shndx()
814 || (*q)->value() != from_sym->value())
815 break;
816
817 this->weak_aliases_[from_sym] = *q;
818 from_sym->set_has_alias();
819 from_sym = *q;
dbe717ef
ILT
820 }
821
aeddab66
ILT
822 if (from_sym != *p)
823 {
824 this->weak_aliases_[from_sym] = *p;
825 from_sym->set_has_alias();
826 }
dbe717ef 827
aeddab66 828 p = q - 1;
dbe717ef
ILT
829 }
830}
831
ead1e424
ILT
832// Create and return a specially defined symbol. If ONLY_IF_REF is
833// true, then only create the symbol if there is a reference to it.
86f2e683 834// If this does not return NULL, it sets *POLDSYM to the existing
306d9ef0 835// symbol if there is one. This canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
836
837template<int size, bool big_endian>
838Sized_symbol<size>*
306d9ef0
ILT
839Symbol_table::define_special_symbol(const Target* target, const char** pname,
840 const char** pversion, bool only_if_ref,
86f2e683 841 Sized_symbol<size>** poldsym
593f47df 842 ACCEPT_SIZE_ENDIAN)
ead1e424 843{
ead1e424
ILT
844 Symbol* oldsym;
845 Sized_symbol<size>* sym;
86f2e683
ILT
846 bool add_to_table = false;
847 typename Symbol_table_type::iterator add_loc = this->table_.end();
ead1e424
ILT
848
849 if (only_if_ref)
850 {
306d9ef0 851 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 852 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 853 return NULL;
306d9ef0
ILT
854
855 *pname = oldsym->name();
856 *pversion = oldsym->version();
ead1e424
ILT
857 }
858 else
859 {
14b31740 860 // Canonicalize NAME and VERSION.
f0641a0b 861 Stringpool::Key name_key;
cfd73a4e 862 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 863
14b31740 864 Stringpool::Key version_key = 0;
306d9ef0 865 if (*pversion != NULL)
cfd73a4e 866 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 867
ead1e424 868 Symbol* const snull = NULL;
ead1e424 869 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
870 this->table_.insert(std::make_pair(std::make_pair(name_key,
871 version_key),
ead1e424
ILT
872 snull));
873
874 if (!ins.second)
875 {
14b31740 876 // We already have a symbol table entry for NAME/VERSION.
ead1e424 877 oldsym = ins.first->second;
a3ad94ed 878 gold_assert(oldsym != NULL);
ead1e424
ILT
879 }
880 else
881 {
882 // We haven't seen this symbol before.
a3ad94ed 883 gold_assert(ins.first->second == NULL);
86f2e683
ILT
884 add_to_table = true;
885 add_loc = ins.first;
ead1e424
ILT
886 oldsym = NULL;
887 }
888 }
889
86f2e683
ILT
890 if (!target->has_make_symbol())
891 sym = new Sized_symbol<size>();
892 else
ead1e424 893 {
86f2e683
ILT
894 gold_assert(target->get_size() == size);
895 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
896 typedef Sized_target<size, big_endian> My_target;
897 const My_target* sized_target =
898 static_cast<const My_target*>(target);
899 sym = sized_target->make_symbol();
900 if (sym == NULL)
901 return NULL;
902 }
ead1e424 903
86f2e683
ILT
904 if (add_to_table)
905 add_loc->second = sym;
906 else
907 gold_assert(oldsym != NULL);
ead1e424 908
86f2e683
ILT
909 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
910 SELECT_SIZE(size));
ead1e424
ILT
911
912 return sym;
913}
914
915// Define a symbol based on an Output_data.
916
14b31740
ILT
917Symbol*
918Symbol_table::define_in_output_data(const Target* target, const char* name,
919 const char* version, Output_data* od,
ead1e424
ILT
920 uint64_t value, uint64_t symsize,
921 elfcpp::STT type, elfcpp::STB binding,
922 elfcpp::STV visibility,
923 unsigned char nonvis,
924 bool offset_is_from_end,
925 bool only_if_ref)
926{
9025d29d 927 if (parameters->get_size() == 32)
86f2e683
ILT
928 {
929#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
930 return this->do_define_in_output_data<32>(target, name, version, od,
931 value, symsize, type, binding,
932 visibility, nonvis,
933 offset_is_from_end,
934 only_if_ref);
935#else
936 gold_unreachable();
937#endif
938 }
9025d29d 939 else if (parameters->get_size() == 64)
86f2e683
ILT
940 {
941#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
942 return this->do_define_in_output_data<64>(target, name, version, od,
943 value, symsize, type, binding,
944 visibility, nonvis,
945 offset_is_from_end,
946 only_if_ref);
947#else
948 gold_unreachable();
949#endif
950 }
ead1e424 951 else
a3ad94ed 952 gold_unreachable();
ead1e424
ILT
953}
954
955// Define a symbol in an Output_data, sized version.
956
957template<int size>
14b31740 958Sized_symbol<size>*
ead1e424 959Symbol_table::do_define_in_output_data(
14b31740 960 const Target* target,
ead1e424 961 const char* name,
14b31740 962 const char* version,
ead1e424
ILT
963 Output_data* od,
964 typename elfcpp::Elf_types<size>::Elf_Addr value,
965 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
966 elfcpp::STT type,
967 elfcpp::STB binding,
968 elfcpp::STV visibility,
969 unsigned char nonvis,
970 bool offset_is_from_end,
971 bool only_if_ref)
972{
973 Sized_symbol<size>* sym;
86f2e683 974 Sized_symbol<size>* oldsym;
ead1e424 975
9025d29d 976 if (parameters->is_big_endian())
193a53d9
ILT
977 {
978#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
979 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
306d9ef0 980 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
981 SELECT_SIZE_ENDIAN(size, true));
982#else
983 gold_unreachable();
984#endif
985 }
ead1e424 986 else
193a53d9
ILT
987 {
988#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
989 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
306d9ef0 990 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
991 SELECT_SIZE_ENDIAN(size, false));
992#else
993 gold_unreachable();
994#endif
995 }
ead1e424
ILT
996
997 if (sym == NULL)
14b31740 998 return NULL;
ead1e424 999
d4f5281b 1000 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
1001 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1002 offset_is_from_end);
14b31740 1003
86f2e683
ILT
1004 if (oldsym != NULL
1005 && Symbol_table::should_override_with_special(oldsym))
aeddab66 1006 this->override_with_special(oldsym, sym);
86f2e683 1007
14b31740 1008 return sym;
ead1e424
ILT
1009}
1010
1011// Define a symbol based on an Output_segment.
1012
14b31740
ILT
1013Symbol*
1014Symbol_table::define_in_output_segment(const Target* target, const char* name,
1015 const char* version, Output_segment* os,
ead1e424
ILT
1016 uint64_t value, uint64_t symsize,
1017 elfcpp::STT type, elfcpp::STB binding,
1018 elfcpp::STV visibility,
1019 unsigned char nonvis,
1020 Symbol::Segment_offset_base offset_base,
1021 bool only_if_ref)
1022{
9025d29d 1023 if (parameters->get_size() == 32)
86f2e683
ILT
1024 {
1025#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1026 return this->do_define_in_output_segment<32>(target, name, version, os,
1027 value, symsize, type,
1028 binding, visibility, nonvis,
1029 offset_base, only_if_ref);
1030#else
1031 gold_unreachable();
1032#endif
1033 }
9025d29d 1034 else if (parameters->get_size() == 64)
86f2e683
ILT
1035 {
1036#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1037 return this->do_define_in_output_segment<64>(target, name, version, os,
1038 value, symsize, type,
1039 binding, visibility, nonvis,
1040 offset_base, only_if_ref);
1041#else
1042 gold_unreachable();
1043#endif
1044 }
ead1e424 1045 else
a3ad94ed 1046 gold_unreachable();
ead1e424
ILT
1047}
1048
1049// Define a symbol in an Output_segment, sized version.
1050
1051template<int size>
14b31740 1052Sized_symbol<size>*
ead1e424 1053Symbol_table::do_define_in_output_segment(
14b31740 1054 const Target* target,
ead1e424 1055 const char* name,
14b31740 1056 const char* version,
ead1e424
ILT
1057 Output_segment* os,
1058 typename elfcpp::Elf_types<size>::Elf_Addr value,
1059 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1060 elfcpp::STT type,
1061 elfcpp::STB binding,
1062 elfcpp::STV visibility,
1063 unsigned char nonvis,
1064 Symbol::Segment_offset_base offset_base,
1065 bool only_if_ref)
1066{
1067 Sized_symbol<size>* sym;
86f2e683 1068 Sized_symbol<size>* oldsym;
ead1e424 1069
9025d29d
ILT
1070 if (parameters->is_big_endian())
1071 {
1072#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1073 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1074 target, &name, &version, only_if_ref, &oldsym
1075 SELECT_SIZE_ENDIAN(size, true));
1076#else
1077 gold_unreachable();
1078#endif
1079 }
ead1e424 1080 else
9025d29d
ILT
1081 {
1082#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1083 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1084 target, &name, &version, only_if_ref, &oldsym
1085 SELECT_SIZE_ENDIAN(size, false));
1086#else
1087 gold_unreachable();
1088#endif
1089 }
ead1e424
ILT
1090
1091 if (sym == NULL)
14b31740 1092 return NULL;
ead1e424 1093
d4f5281b 1094 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
1095 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1096 offset_base);
14b31740 1097
86f2e683
ILT
1098 if (oldsym != NULL
1099 && Symbol_table::should_override_with_special(oldsym))
aeddab66 1100 this->override_with_special(oldsym, sym);
86f2e683 1101
14b31740 1102 return sym;
ead1e424
ILT
1103}
1104
1105// Define a special symbol with a constant value. It is a multiple
1106// definition error if this symbol is already defined.
1107
14b31740
ILT
1108Symbol*
1109Symbol_table::define_as_constant(const Target* target, const char* name,
1110 const char* version, uint64_t value,
1111 uint64_t symsize, elfcpp::STT type,
1112 elfcpp::STB binding, elfcpp::STV visibility,
1113 unsigned char nonvis, bool only_if_ref)
ead1e424 1114{
9025d29d 1115 if (parameters->get_size() == 32)
86f2e683
ILT
1116 {
1117#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1118 return this->do_define_as_constant<32>(target, name, version, value,
1119 symsize, type, binding,
1120 visibility, nonvis, only_if_ref);
1121#else
1122 gold_unreachable();
1123#endif
1124 }
9025d29d 1125 else if (parameters->get_size() == 64)
86f2e683
ILT
1126 {
1127#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1128 return this->do_define_as_constant<64>(target, name, version, value,
1129 symsize, type, binding,
1130 visibility, nonvis, only_if_ref);
1131#else
1132 gold_unreachable();
1133#endif
1134 }
ead1e424 1135 else
a3ad94ed 1136 gold_unreachable();
ead1e424
ILT
1137}
1138
1139// Define a symbol as a constant, sized version.
1140
1141template<int size>
14b31740 1142Sized_symbol<size>*
ead1e424 1143Symbol_table::do_define_as_constant(
14b31740 1144 const Target* target,
ead1e424 1145 const char* name,
14b31740 1146 const char* version,
ead1e424
ILT
1147 typename elfcpp::Elf_types<size>::Elf_Addr value,
1148 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1149 elfcpp::STT type,
1150 elfcpp::STB binding,
1151 elfcpp::STV visibility,
1152 unsigned char nonvis,
1153 bool only_if_ref)
1154{
1155 Sized_symbol<size>* sym;
86f2e683 1156 Sized_symbol<size>* oldsym;
ead1e424 1157
9025d29d
ILT
1158 if (parameters->is_big_endian())
1159 {
1160#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1161 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1162 target, &name, &version, only_if_ref, &oldsym
1163 SELECT_SIZE_ENDIAN(size, true));
1164#else
1165 gold_unreachable();
1166#endif
1167 }
ead1e424 1168 else
9025d29d
ILT
1169 {
1170#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1171 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1172 target, &name, &version, only_if_ref, &oldsym
1173 SELECT_SIZE_ENDIAN(size, false));
1174#else
1175 gold_unreachable();
1176#endif
1177 }
ead1e424
ILT
1178
1179 if (sym == NULL)
14b31740 1180 return NULL;
ead1e424 1181
d4f5281b 1182 gold_assert(version == NULL || oldsym != NULL);
ead1e424 1183 sym->init(name, value, symsize, type, binding, visibility, nonvis);
14b31740 1184
86f2e683
ILT
1185 if (oldsym != NULL
1186 && Symbol_table::should_override_with_special(oldsym))
aeddab66 1187 this->override_with_special(oldsym, sym);
86f2e683 1188
14b31740 1189 return sym;
ead1e424
ILT
1190}
1191
1192// Define a set of symbols in output sections.
1193
1194void
14b31740
ILT
1195Symbol_table::define_symbols(const Layout* layout, const Target* target,
1196 int count, const Define_symbol_in_section* p)
ead1e424
ILT
1197{
1198 for (int i = 0; i < count; ++i, ++p)
1199 {
1200 Output_section* os = layout->find_output_section(p->output_section);
1201 if (os != NULL)
14b31740
ILT
1202 this->define_in_output_data(target, p->name, NULL, os, p->value,
1203 p->size, p->type, p->binding,
1204 p->visibility, p->nonvis,
1205 p->offset_is_from_end, p->only_if_ref);
ead1e424 1206 else
14b31740 1207 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1208 p->binding, p->visibility, p->nonvis,
1209 p->only_if_ref);
1210 }
1211}
1212
1213// Define a set of symbols in output segments.
1214
1215void
14b31740
ILT
1216Symbol_table::define_symbols(const Layout* layout, const Target* target,
1217 int count, const Define_symbol_in_segment* p)
ead1e424
ILT
1218{
1219 for (int i = 0; i < count; ++i, ++p)
1220 {
1221 Output_segment* os = layout->find_output_segment(p->segment_type,
1222 p->segment_flags_set,
1223 p->segment_flags_clear);
1224 if (os != NULL)
14b31740
ILT
1225 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1226 p->size, p->type, p->binding,
1227 p->visibility, p->nonvis,
1228 p->offset_base, p->only_if_ref);
ead1e424 1229 else
14b31740 1230 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1231 p->binding, p->visibility, p->nonvis,
1232 p->only_if_ref);
1233 }
1234}
1235
46fe1623
ILT
1236// Define CSYM using a COPY reloc. POSD is the Output_data where the
1237// symbol should be defined--typically a .dyn.bss section. VALUE is
1238// the offset within POSD.
1239
1240template<int size>
1241void
1242Symbol_table::define_with_copy_reloc(const Target* target,
1243 Sized_symbol<size>* csym,
1244 Output_data* posd, uint64_t value)
1245{
1246 gold_assert(csym->is_from_dynobj());
1247 gold_assert(!csym->is_copied_from_dynobj());
1248 Object* object = csym->object();
1249 gold_assert(object->is_dynamic());
1250 Dynobj* dynobj = static_cast<Dynobj*>(object);
1251
1252 // Our copied variable has to override any variable in a shared
1253 // library.
1254 elfcpp::STB binding = csym->binding();
1255 if (binding == elfcpp::STB_WEAK)
1256 binding = elfcpp::STB_GLOBAL;
1257
1258 this->define_in_output_data(target, csym->name(), csym->version(),
1259 posd, value, csym->symsize(),
1260 csym->type(), binding,
1261 csym->visibility(), csym->nonvis(),
1262 false, false);
1263
1264 csym->set_is_copied_from_dynobj();
1265 csym->set_needs_dynsym_entry();
1266
1267 this->copied_symbol_dynobjs_[csym] = dynobj;
1268
1269 // We have now defined all aliases, but we have not entered them all
1270 // in the copied_symbol_dynobjs_ map.
1271 if (csym->has_alias())
1272 {
1273 Symbol* sym = csym;
1274 while (true)
1275 {
1276 sym = this->weak_aliases_[sym];
1277 if (sym == csym)
1278 break;
1279 gold_assert(sym->output_data() == posd);
1280
1281 sym->set_is_copied_from_dynobj();
1282 this->copied_symbol_dynobjs_[sym] = dynobj;
1283 }
1284 }
1285}
1286
1287// SYM is defined using a COPY reloc. Return the dynamic object where
1288// the original definition was found.
1289
1290Dynobj*
1291Symbol_table::get_copy_source(const Symbol* sym) const
1292{
1293 gold_assert(sym->is_copied_from_dynobj());
1294 Copied_symbol_dynobjs::const_iterator p =
1295 this->copied_symbol_dynobjs_.find(sym);
1296 gold_assert(p != this->copied_symbol_dynobjs_.end());
1297 return p->second;
1298}
1299
a3ad94ed
ILT
1300// Set the dynamic symbol indexes. INDEX is the index of the first
1301// global dynamic symbol. Pointers to the symbols are stored into the
1302// vector SYMS. The names are added to DYNPOOL. This returns an
1303// updated dynamic symbol index.
1304
1305unsigned int
35cdfc9a 1306Symbol_table::set_dynsym_indexes(const Target* target,
14b31740 1307 unsigned int index,
a3ad94ed 1308 std::vector<Symbol*>* syms,
14b31740
ILT
1309 Stringpool* dynpool,
1310 Versions* versions)
a3ad94ed
ILT
1311{
1312 for (Symbol_table_type::iterator p = this->table_.begin();
1313 p != this->table_.end();
1314 ++p)
1315 {
1316 Symbol* sym = p->second;
16649710
ILT
1317
1318 // Note that SYM may already have a dynamic symbol index, since
1319 // some symbols appear more than once in the symbol table, with
1320 // and without a version.
1321
436ca963 1322 if (!sym->should_add_dynsym_entry())
16649710
ILT
1323 sym->set_dynsym_index(-1U);
1324 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1325 {
1326 sym->set_dynsym_index(index);
1327 ++index;
1328 syms->push_back(sym);
cfd73a4e 1329 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
1330
1331 // Record any version information.
1332 if (sym->version() != NULL)
35cdfc9a 1333 versions->record_version(this, dynpool, sym);
a3ad94ed
ILT
1334 }
1335 }
1336
14b31740
ILT
1337 // Finish up the versions. In some cases this may add new dynamic
1338 // symbols.
1339 index = versions->finalize(target, this, index, syms);
1340
a3ad94ed
ILT
1341 return index;
1342}
1343
c06b7b0b
ILT
1344// Set the final values for all the symbols. The index of the first
1345// global symbol in the output file is INDEX. Record the file offset
75f65a3e 1346// OFF. Add their names to POOL. Return the new file offset.
54dc6425 1347
75f65a3e 1348off_t
16649710
ILT
1349Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1350 size_t dyn_global_index, size_t dyncount,
1351 Stringpool* pool)
54dc6425 1352{
f6ce93d6
ILT
1353 off_t ret;
1354
a3ad94ed 1355 gold_assert(index != 0);
c06b7b0b
ILT
1356 this->first_global_index_ = index;
1357
16649710
ILT
1358 this->dynamic_offset_ = dynoff;
1359 this->first_dynamic_global_index_ = dyn_global_index;
1360 this->dynamic_count_ = dyncount;
1361
9025d29d
ILT
1362 if (parameters->get_size() == 32)
1363 {
1364#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1365 ret = this->sized_finalize<32>(index, off, pool);
1366#else
1367 gold_unreachable();
1368#endif
1369 }
1370 else if (parameters->get_size() == 64)
1371 {
1372#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1373 ret = this->sized_finalize<64>(index, off, pool);
1374#else
1375 gold_unreachable();
1376#endif
1377 }
61ba1cf9 1378 else
a3ad94ed 1379 gold_unreachable();
f6ce93d6
ILT
1380
1381 // Now that we have the final symbol table, we can reliably note
1382 // which symbols should get warnings.
1383 this->warnings_.note_warnings(this);
1384
1385 return ret;
75f65a3e
ILT
1386}
1387
ead1e424
ILT
1388// Set the final value for all the symbols. This is called after
1389// Layout::finalize, so all the output sections have their final
1390// address.
75f65a3e
ILT
1391
1392template<int size>
1393off_t
c06b7b0b 1394Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
75f65a3e 1395{
ead1e424 1396 off = align_address(off, size >> 3);
75f65a3e
ILT
1397 this->offset_ = off;
1398
c06b7b0b
ILT
1399 size_t orig_index = index;
1400
75f65a3e 1401 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1402 for (Symbol_table_type::iterator p = this->table_.begin();
1403 p != this->table_.end();
1404 ++p)
54dc6425 1405 {
75f65a3e 1406 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 1407
75f65a3e 1408 // FIXME: Here we need to decide which symbols should go into
a3ad94ed
ILT
1409 // the output file, based on --strip.
1410
1411 // The default version of a symbol may appear twice in the
1412 // symbol table. We only need to finalize it once.
1413 if (sym->has_symtab_index())
1414 continue;
75f65a3e 1415
008db82e
ILT
1416 if (!sym->in_reg())
1417 {
1418 gold_assert(!sym->has_symtab_index());
1419 sym->set_symtab_index(-1U);
1420 gold_assert(sym->dynsym_index() == -1U);
1421 continue;
1422 }
1423
ead1e424 1424 typename Sized_symbol<size>::Value_type value;
75f65a3e 1425
ead1e424 1426 switch (sym->source())
75f65a3e 1427 {
ead1e424
ILT
1428 case Symbol::FROM_OBJECT:
1429 {
16649710 1430 unsigned int shndx = sym->shndx();
ead1e424
ILT
1431
1432 // FIXME: We need some target specific support here.
16649710
ILT
1433 if (shndx >= elfcpp::SHN_LORESERVE
1434 && shndx != elfcpp::SHN_ABS)
ead1e424 1435 {
75f2446e
ILT
1436 gold_error(_("%s: unsupported symbol section 0x%x"),
1437 sym->name(), shndx);
1438 shndx = elfcpp::SHN_UNDEF;
ead1e424
ILT
1439 }
1440
f6ce93d6
ILT
1441 Object* symobj = sym->object();
1442 if (symobj->is_dynamic())
1443 {
1444 value = 0;
16649710 1445 shndx = elfcpp::SHN_UNDEF;
f6ce93d6 1446 }
16649710 1447 else if (shndx == elfcpp::SHN_UNDEF)
ead1e424 1448 value = 0;
16649710 1449 else if (shndx == elfcpp::SHN_ABS)
ead1e424
ILT
1450 value = sym->value();
1451 else
1452 {
f6ce93d6 1453 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1454 off_t secoff;
16649710 1455 Output_section* os = relobj->output_section(shndx, &secoff);
ead1e424
ILT
1456
1457 if (os == NULL)
1458 {
c06b7b0b 1459 sym->set_symtab_index(-1U);
16649710 1460 gold_assert(sym->dynsym_index() == -1U);
ead1e424
ILT
1461 continue;
1462 }
1463
1464 value = sym->value() + os->address() + secoff;
1465 }
1466 }
1467 break;
1468
1469 case Symbol::IN_OUTPUT_DATA:
1470 {
1471 Output_data* od = sym->output_data();
1472 value = sym->value() + od->address();
1473 if (sym->offset_is_from_end())
1474 value += od->data_size();
1475 }
1476 break;
1477
1478 case Symbol::IN_OUTPUT_SEGMENT:
1479 {
1480 Output_segment* os = sym->output_segment();
1481 value = sym->value() + os->vaddr();
1482 switch (sym->offset_base())
1483 {
1484 case Symbol::SEGMENT_START:
1485 break;
1486 case Symbol::SEGMENT_END:
1487 value += os->memsz();
1488 break;
1489 case Symbol::SEGMENT_BSS:
1490 value += os->filesz();
1491 break;
1492 default:
a3ad94ed 1493 gold_unreachable();
ead1e424
ILT
1494 }
1495 }
1496 break;
1497
1498 case Symbol::CONSTANT:
1499 value = sym->value();
1500 break;
1501
1502 default:
a3ad94ed 1503 gold_unreachable();
54dc6425 1504 }
ead1e424
ILT
1505
1506 sym->set_value(value);
9e2dcb77
ILT
1507
1508 if (parameters->strip_all())
1509 sym->set_symtab_index(-1U);
1510 else
1511 {
1512 sym->set_symtab_index(index);
cfd73a4e 1513 pool->add(sym->name(), false, NULL);
9e2dcb77
ILT
1514 ++index;
1515 off += sym_size;
1516 }
54dc6425 1517 }
75f65a3e 1518
c06b7b0b 1519 this->output_count_ = index - orig_index;
61ba1cf9 1520
75f65a3e 1521 return off;
54dc6425
ILT
1522}
1523
61ba1cf9
ILT
1524// Write out the global symbols.
1525
1526void
9a2d6984
ILT
1527Symbol_table::write_globals(const Input_objects* input_objects,
1528 const Stringpool* sympool,
16649710 1529 const Stringpool* dynpool, Output_file* of) const
61ba1cf9 1530{
9025d29d 1531 if (parameters->get_size() == 32)
61ba1cf9 1532 {
9025d29d
ILT
1533 if (parameters->is_big_endian())
1534 {
1535#ifdef HAVE_TARGET_32_BIG
9a2d6984
ILT
1536 this->sized_write_globals<32, true>(input_objects, sympool,
1537 dynpool, of);
9025d29d
ILT
1538#else
1539 gold_unreachable();
1540#endif
1541 }
61ba1cf9 1542 else
9025d29d
ILT
1543 {
1544#ifdef HAVE_TARGET_32_LITTLE
9a2d6984
ILT
1545 this->sized_write_globals<32, false>(input_objects, sympool,
1546 dynpool, of);
9025d29d
ILT
1547#else
1548 gold_unreachable();
1549#endif
1550 }
61ba1cf9 1551 }
9025d29d 1552 else if (parameters->get_size() == 64)
61ba1cf9 1553 {
9025d29d
ILT
1554 if (parameters->is_big_endian())
1555 {
1556#ifdef HAVE_TARGET_64_BIG
9a2d6984
ILT
1557 this->sized_write_globals<64, true>(input_objects, sympool,
1558 dynpool, of);
9025d29d
ILT
1559#else
1560 gold_unreachable();
1561#endif
1562 }
61ba1cf9 1563 else
9025d29d
ILT
1564 {
1565#ifdef HAVE_TARGET_64_LITTLE
9a2d6984
ILT
1566 this->sized_write_globals<64, false>(input_objects, sympool,
1567 dynpool, of);
9025d29d
ILT
1568#else
1569 gold_unreachable();
1570#endif
1571 }
61ba1cf9
ILT
1572 }
1573 else
a3ad94ed 1574 gold_unreachable();
61ba1cf9
ILT
1575}
1576
1577// Write out the global symbols.
1578
1579template<int size, bool big_endian>
1580void
9a2d6984 1581Symbol_table::sized_write_globals(const Input_objects* input_objects,
61ba1cf9 1582 const Stringpool* sympool,
16649710 1583 const Stringpool* dynpool,
61ba1cf9
ILT
1584 Output_file* of) const
1585{
9a2d6984
ILT
1586 const Target* const target = input_objects->target();
1587
61ba1cf9 1588 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1589 unsigned int index = this->first_global_index_;
1590 const off_t oview_size = this->output_count_ * sym_size;
16649710
ILT
1591 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1592
1593 unsigned int dynamic_count = this->dynamic_count_;
1594 off_t dynamic_size = dynamic_count * sym_size;
1595 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1596 unsigned char* dynamic_view;
1597 if (this->dynamic_offset_ == 0)
1598 dynamic_view = NULL;
1599 else
1600 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 1601
61ba1cf9
ILT
1602 unsigned char* ps = psyms;
1603 for (Symbol_table_type::const_iterator p = this->table_.begin();
1604 p != this->table_.end();
1605 ++p)
1606 {
1607 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1608
9a2d6984
ILT
1609 // Possibly warn about unresolved symbols in shared libraries.
1610 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
e2827e5f 1611
a3ad94ed 1612 unsigned int sym_index = sym->symtab_index();
16649710
ILT
1613 unsigned int dynsym_index;
1614 if (dynamic_view == NULL)
1615 dynsym_index = -1U;
1616 else
1617 dynsym_index = sym->dynsym_index();
1618
1619 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
1620 {
1621 // This symbol is not included in the output file.
1622 continue;
1623 }
16649710
ILT
1624
1625 if (sym_index == index)
1626 ++index;
1627 else if (sym_index != -1U)
a3ad94ed
ILT
1628 {
1629 // We have already seen this symbol, because it has a
1630 // default version.
1631 gold_assert(sym_index < index);
16649710
ILT
1632 if (dynsym_index == -1U)
1633 continue;
1634 sym_index = -1U;
a3ad94ed 1635 }
c06b7b0b 1636
ead1e424 1637 unsigned int shndx;
ab5c9e90 1638 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
ead1e424
ILT
1639 switch (sym->source())
1640 {
1641 case Symbol::FROM_OBJECT:
1642 {
16649710 1643 unsigned int in_shndx = sym->shndx();
ead1e424
ILT
1644
1645 // FIXME: We need some target specific support here.
16649710
ILT
1646 if (in_shndx >= elfcpp::SHN_LORESERVE
1647 && in_shndx != elfcpp::SHN_ABS)
ead1e424 1648 {
75f2446e
ILT
1649 gold_error(_("%s: unsupported symbol section 0x%x"),
1650 sym->name(), in_shndx);
1651 shndx = in_shndx;
f6ce93d6 1652 }
ead1e424
ILT
1653 else
1654 {
75f2446e
ILT
1655 Object* symobj = sym->object();
1656 if (symobj->is_dynamic())
1657 {
1658 if (sym->needs_dynsym_value())
1659 value = target->dynsym_value(sym);
1660 shndx = elfcpp::SHN_UNDEF;
1661 }
1662 else if (in_shndx == elfcpp::SHN_UNDEF
1663 || in_shndx == elfcpp::SHN_ABS)
1664 shndx = in_shndx;
1665 else
1666 {
1667 Relobj* relobj = static_cast<Relobj*>(symobj);
1668 off_t secoff;
1669 Output_section* os = relobj->output_section(in_shndx,
1670 &secoff);
1671 gold_assert(os != NULL);
1672 shndx = os->out_shndx();
1673 }
ead1e424
ILT
1674 }
1675 }
1676 break;
1677
1678 case Symbol::IN_OUTPUT_DATA:
1679 shndx = sym->output_data()->out_shndx();
1680 break;
1681
1682 case Symbol::IN_OUTPUT_SEGMENT:
1683 shndx = elfcpp::SHN_ABS;
1684 break;
1685
1686 case Symbol::CONSTANT:
1687 shndx = elfcpp::SHN_ABS;
1688 break;
1689
1690 default:
a3ad94ed 1691 gold_unreachable();
ead1e424 1692 }
61ba1cf9 1693
16649710
ILT
1694 if (sym_index != -1U)
1695 {
6a469986 1696 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1697 sym, sym->value(), shndx, sympool, ps
6a469986 1698 SELECT_SIZE_ENDIAN(size, big_endian));
16649710
ILT
1699 ps += sym_size;
1700 }
61ba1cf9 1701
16649710
ILT
1702 if (dynsym_index != -1U)
1703 {
1704 dynsym_index -= first_dynamic_global_index;
1705 gold_assert(dynsym_index < dynamic_count);
1706 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
6a469986 1707 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1708 sym, value, shndx, dynpool, pd
6a469986 1709 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1710 }
61ba1cf9
ILT
1711 }
1712
a3ad94ed 1713 gold_assert(ps - psyms == oview_size);
c06b7b0b
ILT
1714
1715 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
1716 if (dynamic_view != NULL)
1717 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1718}
1719
1720// Write out the symbol SYM, in section SHNDX, to P. POOL is the
1721// strtab holding the name.
1722
1723template<int size, bool big_endian>
1724void
ab5c9e90
ILT
1725Symbol_table::sized_write_symbol(
1726 Sized_symbol<size>* sym,
1727 typename elfcpp::Elf_types<size>::Elf_Addr value,
1728 unsigned int shndx,
1729 const Stringpool* pool,
1730 unsigned char* p
1731 ACCEPT_SIZE_ENDIAN) const
16649710
ILT
1732{
1733 elfcpp::Sym_write<size, big_endian> osym(p);
1734 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 1735 osym.put_st_value(value);
16649710
ILT
1736 osym.put_st_size(sym->symsize());
1737 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1738 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1739 osym.put_st_shndx(shndx);
61ba1cf9
ILT
1740}
1741
9a2d6984
ILT
1742// Check for unresolved symbols in shared libraries. This is
1743// controlled by the --allow-shlib-undefined option.
1744
1745// We only warn about libraries for which we have seen all the
1746// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1747// which were not seen in this link. If we didn't see a DT_NEEDED
1748// entry, we aren't going to be able to reliably report whether the
1749// symbol is undefined.
1750
1751// We also don't warn about libraries found in the system library
1752// directory (the directory were we find libc.so); we assume that
1753// those libraries are OK. This heuristic avoids problems in
1754// GNU/Linux, in which -ldl can have undefined references satisfied by
1755// ld-linux.so.
1756
1757inline void
1758Symbol_table::warn_about_undefined_dynobj_symbol(
1759 const Input_objects* input_objects,
1760 Symbol* sym) const
1761{
1762 if (sym->source() == Symbol::FROM_OBJECT
1763 && sym->object()->is_dynamic()
1764 && sym->shndx() == elfcpp::SHN_UNDEF
1765 && sym->binding() != elfcpp::STB_WEAK
1766 && !parameters->allow_shlib_undefined()
1767 && !input_objects->target()->is_defined_by_abi(sym)
1768 && !input_objects->found_in_system_library_directory(sym->object()))
1769 {
1770 // A very ugly cast.
1771 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
1772 if (!dynobj->has_unknown_needed_entries())
1773 gold_error(_("%s: undefined reference to '%s'"),
1774 sym->object()->name().c_str(), sym->name());
1775 }
1776}
1777
a3ad94ed
ILT
1778// Write out a section symbol. Return the update offset.
1779
1780void
9025d29d 1781Symbol_table::write_section_symbol(const Output_section *os,
a3ad94ed
ILT
1782 Output_file* of,
1783 off_t offset) const
1784{
9025d29d 1785 if (parameters->get_size() == 32)
a3ad94ed 1786 {
9025d29d
ILT
1787 if (parameters->is_big_endian())
1788 {
1789#ifdef HAVE_TARGET_32_BIG
1790 this->sized_write_section_symbol<32, true>(os, of, offset);
1791#else
1792 gold_unreachable();
1793#endif
1794 }
a3ad94ed 1795 else
9025d29d
ILT
1796 {
1797#ifdef HAVE_TARGET_32_LITTLE
1798 this->sized_write_section_symbol<32, false>(os, of, offset);
1799#else
1800 gold_unreachable();
1801#endif
1802 }
a3ad94ed 1803 }
9025d29d 1804 else if (parameters->get_size() == 64)
a3ad94ed 1805 {
9025d29d
ILT
1806 if (parameters->is_big_endian())
1807 {
1808#ifdef HAVE_TARGET_64_BIG
1809 this->sized_write_section_symbol<64, true>(os, of, offset);
1810#else
1811 gold_unreachable();
1812#endif
1813 }
a3ad94ed 1814 else
9025d29d
ILT
1815 {
1816#ifdef HAVE_TARGET_64_LITTLE
1817 this->sized_write_section_symbol<64, false>(os, of, offset);
1818#else
1819 gold_unreachable();
1820#endif
1821 }
a3ad94ed
ILT
1822 }
1823 else
1824 gold_unreachable();
1825}
1826
1827// Write out a section symbol, specialized for size and endianness.
1828
1829template<int size, bool big_endian>
1830void
1831Symbol_table::sized_write_section_symbol(const Output_section* os,
1832 Output_file* of,
1833 off_t offset) const
1834{
1835 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1836
1837 unsigned char* pov = of->get_output_view(offset, sym_size);
1838
1839 elfcpp::Sym_write<size, big_endian> osym(pov);
1840 osym.put_st_name(0);
1841 osym.put_st_value(os->address());
1842 osym.put_st_size(0);
1843 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1844 elfcpp::STT_SECTION));
1845 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1846 osym.put_st_shndx(os->out_shndx());
1847
1848 of->write_output_view(offset, sym_size, pov);
1849}
1850
70e654ba
ILT
1851// Check candidate_odr_violations_ to find symbols with the same name
1852// but apparently different definitions (different source-file/line-no).
1853
1854void
78f15696 1855Symbol_table::detect_odr_violations(const char* output_file_name) const
70e654ba
ILT
1856{
1857 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
1858 it != candidate_odr_violations_.end();
1859 ++it)
1860 {
1861 const char* symbol_name = it->first;
1862 // We use a sorted set so the output is deterministic.
1863 std::set<std::string> line_nums;
1864
1865 Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
1866 locs;
1867 for (locs = it->second.begin(); locs != it->second.end(); ++locs)
1868 {
1869 // We need to lock the object in order to read it. This
1870 // means that we can not run inside a Task. If we want to
1871 // run this in a Task for better performance, we will need
1872 // one Task for object, plus appropriate locking to ensure
1873 // that we don't conflict with other uses of the object.
1874 locs->object->lock();
a55ce7fe
ILT
1875 std::string lineno = Dwarf_line_info::one_addr2line(
1876 locs->object, locs->shndx, locs->offset);
70e654ba 1877 locs->object->unlock();
70e654ba
ILT
1878 if (!lineno.empty())
1879 line_nums.insert(lineno);
1880 }
1881
1882 if (line_nums.size() > 1)
1883 {
78f15696
ILT
1884 gold_warning(_("while linking %s: symbol %s defined in multiple "
1885 "places (possible ODR violation):"),
1886 output_file_name, symbol_name);
70e654ba
ILT
1887 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
1888 it2 != line_nums.end();
1889 ++it2)
1890 fprintf(stderr, " %s\n", it2->c_str());
1891 }
1892 }
1893}
1894
f6ce93d6
ILT
1895// Warnings functions.
1896
1897// Add a new warning.
1898
1899void
1900Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1901 unsigned int shndx)
1902{
1903 name = symtab->canonicalize_name(name);
1904 this->warnings_[name].set(obj, shndx);
1905}
1906
1907// Look through the warnings and mark the symbols for which we should
1908// warn. This is called during Layout::finalize when we know the
1909// sources for all the symbols.
1910
1911void
1912Warnings::note_warnings(Symbol_table* symtab)
1913{
1914 for (Warning_table::iterator p = this->warnings_.begin();
1915 p != this->warnings_.end();
1916 ++p)
1917 {
1918 Symbol* sym = symtab->lookup(p->first, NULL);
1919 if (sym != NULL
1920 && sym->source() == Symbol::FROM_OBJECT
1921 && sym->object() == p->second.object)
1922 {
1923 sym->set_has_warning();
1924
1925 // Read the section contents to get the warning text. It
1926 // would be nicer if we only did this if we have to actually
1927 // issue a warning. Unfortunately, warnings are issued as
1928 // we relocate sections. That means that we can not lock
1929 // the object then, as we might try to issue the same
1930 // warning multiple times simultaneously.
645f8123
ILT
1931 {
1932 Task_locker_obj<Object> tl(*p->second.object);
1933 const unsigned char* c;
1934 off_t len;
9eb9fa57
ILT
1935 c = p->second.object->section_contents(p->second.shndx, &len,
1936 false);
645f8123
ILT
1937 p->second.set_text(reinterpret_cast<const char*>(c), len);
1938 }
f6ce93d6
ILT
1939 }
1940 }
1941}
1942
1943// Issue a warning. This is called when we see a relocation against a
1944// symbol for which has a warning.
1945
75f2446e 1946template<int size, bool big_endian>
f6ce93d6 1947void
75f2446e
ILT
1948Warnings::issue_warning(const Symbol* sym,
1949 const Relocate_info<size, big_endian>* relinfo,
1950 size_t relnum, off_t reloffset) const
f6ce93d6 1951{
a3ad94ed 1952 gold_assert(sym->has_warning());
f6ce93d6 1953 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 1954 gold_assert(p != this->warnings_.end());
75f2446e
ILT
1955 gold_warning_at_location(relinfo, relnum, reloffset,
1956 "%s", p->second.text.c_str());
f6ce93d6
ILT
1957}
1958
14bfc3f5
ILT
1959// Instantiate the templates we need. We could use the configure
1960// script to restrict this to only the ones needed for implemented
1961// targets.
1962
193a53d9 1963#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
1964template
1965void
193a53d9
ILT
1966Symbol_table::add_from_relobj<32, false>(
1967 Sized_relobj<32, false>* relobj,
f6ce93d6 1968 const unsigned char* syms,
14bfc3f5
ILT
1969 size_t count,
1970 const char* sym_names,
1971 size_t sym_name_size,
730cdc88 1972 Sized_relobj<32, true>::Symbols* sympointers);
193a53d9 1973#endif
14bfc3f5 1974
193a53d9 1975#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
1976template
1977void
193a53d9
ILT
1978Symbol_table::add_from_relobj<32, true>(
1979 Sized_relobj<32, true>* relobj,
f6ce93d6 1980 const unsigned char* syms,
14bfc3f5
ILT
1981 size_t count,
1982 const char* sym_names,
1983 size_t sym_name_size,
730cdc88 1984 Sized_relobj<32, false>::Symbols* sympointers);
193a53d9 1985#endif
14bfc3f5 1986
193a53d9 1987#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
1988template
1989void
193a53d9
ILT
1990Symbol_table::add_from_relobj<64, false>(
1991 Sized_relobj<64, false>* relobj,
f6ce93d6 1992 const unsigned char* syms,
14bfc3f5
ILT
1993 size_t count,
1994 const char* sym_names,
1995 size_t sym_name_size,
730cdc88 1996 Sized_relobj<64, true>::Symbols* sympointers);
193a53d9 1997#endif
14bfc3f5 1998
193a53d9 1999#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
2000template
2001void
193a53d9
ILT
2002Symbol_table::add_from_relobj<64, true>(
2003 Sized_relobj<64, true>* relobj,
f6ce93d6 2004 const unsigned char* syms,
14bfc3f5
ILT
2005 size_t count,
2006 const char* sym_names,
2007 size_t sym_name_size,
730cdc88 2008 Sized_relobj<64, false>::Symbols* sympointers);
193a53d9 2009#endif
14bfc3f5 2010
193a53d9 2011#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
2012template
2013void
193a53d9
ILT
2014Symbol_table::add_from_dynobj<32, false>(
2015 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
2016 const unsigned char* syms,
2017 size_t count,
2018 const char* sym_names,
2019 size_t sym_name_size,
2020 const unsigned char* versym,
2021 size_t versym_size,
2022 const std::vector<const char*>* version_map);
193a53d9 2023#endif
dbe717ef 2024
193a53d9 2025#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
2026template
2027void
193a53d9
ILT
2028Symbol_table::add_from_dynobj<32, true>(
2029 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
2030 const unsigned char* syms,
2031 size_t count,
2032 const char* sym_names,
2033 size_t sym_name_size,
2034 const unsigned char* versym,
2035 size_t versym_size,
2036 const std::vector<const char*>* version_map);
193a53d9 2037#endif
dbe717ef 2038
193a53d9 2039#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
2040template
2041void
193a53d9
ILT
2042Symbol_table::add_from_dynobj<64, false>(
2043 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
2044 const unsigned char* syms,
2045 size_t count,
2046 const char* sym_names,
2047 size_t sym_name_size,
2048 const unsigned char* versym,
2049 size_t versym_size,
2050 const std::vector<const char*>* version_map);
193a53d9 2051#endif
dbe717ef 2052
193a53d9 2053#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
2054template
2055void
193a53d9
ILT
2056Symbol_table::add_from_dynobj<64, true>(
2057 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
2058 const unsigned char* syms,
2059 size_t count,
2060 const char* sym_names,
2061 size_t sym_name_size,
2062 const unsigned char* versym,
2063 size_t versym_size,
2064 const std::vector<const char*>* version_map);
193a53d9 2065#endif
dbe717ef 2066
46fe1623
ILT
2067#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2068template
2069void
2070Symbol_table::define_with_copy_reloc<32>(const Target* target,
2071 Sized_symbol<32>* sym,
2072 Output_data* posd, uint64_t value);
2073#endif
2074
2075#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2076template
2077void
2078Symbol_table::define_with_copy_reloc<64>(const Target* target,
2079 Sized_symbol<64>* sym,
2080 Output_data* posd, uint64_t value);
2081#endif
2082
75f2446e
ILT
2083#ifdef HAVE_TARGET_32_LITTLE
2084template
2085void
2086Warnings::issue_warning<32, false>(const Symbol* sym,
2087 const Relocate_info<32, false>* relinfo,
2088 size_t relnum, off_t reloffset) const;
2089#endif
2090
2091#ifdef HAVE_TARGET_32_BIG
2092template
2093void
2094Warnings::issue_warning<32, true>(const Symbol* sym,
2095 const Relocate_info<32, true>* relinfo,
2096 size_t relnum, off_t reloffset) const;
2097#endif
2098
2099#ifdef HAVE_TARGET_64_LITTLE
2100template
2101void
2102Warnings::issue_warning<64, false>(const Symbol* sym,
2103 const Relocate_info<64, false>* relinfo,
2104 size_t relnum, off_t reloffset) const;
2105#endif
2106
2107#ifdef HAVE_TARGET_64_BIG
2108template
2109void
2110Warnings::issue_warning<64, true>(const Symbol* sym,
2111 const Relocate_info<64, true>* relinfo,
2112 size_t relnum, off_t reloffset) const;
2113#endif
2114
14bfc3f5 2115} // End namespace gold.