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