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