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