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