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