]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
bfd/
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
6d03d481 3// Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
14bfc3f5
ILT
23#include "gold.h"
24
04bf7072 25#include <cstring>
14bfc3f5 26#include <stdint.h>
04bf7072 27#include <algorithm>
70e654ba 28#include <set>
14bfc3f5
ILT
29#include <string>
30#include <utility>
a2b1aa12 31#include "demangle.h"
14bfc3f5 32
6d03d481 33#include "gc.h"
14bfc3f5 34#include "object.h"
70e654ba 35#include "dwarf_reader.h"
dbe717ef 36#include "dynobj.h"
75f65a3e 37#include "output.h"
61ba1cf9 38#include "target.h"
645f8123 39#include "workqueue.h"
14bfc3f5 40#include "symtab.h"
c82fbeee 41#include "demangle.h" // needed for --dynamic-list-cpp-new
89fc3421 42#include "plugin.h"
14bfc3f5
ILT
43
44namespace gold
45{
46
47// Class Symbol.
48
ead1e424
ILT
49// Initialize fields in Symbol. This initializes everything except u_
50// and source_.
14bfc3f5 51
14bfc3f5 52void
ead1e424
ILT
53Symbol::init_fields(const char* name, const char* version,
54 elfcpp::STT type, elfcpp::STB binding,
55 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
56{
57 this->name_ = name;
58 this->version_ = version;
c06b7b0b
ILT
59 this->symtab_index_ = 0;
60 this->dynsym_index_ = 0;
0a65a3a7 61 this->got_offsets_.init();
f4151f89 62 this->plt_offset_ = 0;
ead1e424
ILT
63 this->type_ = type;
64 this->binding_ = binding;
65 this->visibility_ = visibility;
66 this->nonvis_ = nonvis;
67 this->is_target_special_ = false;
1564db8d
ILT
68 this->is_def_ = false;
69 this->is_forwarder_ = false;
aeddab66 70 this->has_alias_ = false;
c06b7b0b 71 this->needs_dynsym_entry_ = false;
008db82e 72 this->in_reg_ = false;
ead1e424 73 this->in_dyn_ = false;
f4151f89 74 this->has_plt_offset_ = false;
f6ce93d6 75 this->has_warning_ = false;
46fe1623 76 this->is_copied_from_dynobj_ = false;
55a93433 77 this->is_forced_local_ = false;
d491d34e 78 this->is_ordinary_shndx_ = false;
89fc3421 79 this->in_real_elf_ = false;
ead1e424
ILT
80}
81
a2b1aa12
ILT
82// Return the demangled version of the symbol's name, but only
83// if the --demangle flag was set.
84
85static std::string
86demangle(const char* name)
87{
086a1841 88 if (!parameters->options().do_demangle())
ff541f30
ILT
89 return name;
90
a2b1aa12
ILT
91 // cplus_demangle allocates memory for the result it returns,
92 // and returns NULL if the name is already demangled.
93 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
94 if (demangled_name == NULL)
95 return name;
96
97 std::string retval(demangled_name);
98 free(demangled_name);
99 return retval;
100}
101
102std::string
103Symbol::demangled_name() const
104{
ff541f30 105 return demangle(this->name());
a2b1aa12
ILT
106}
107
ead1e424
ILT
108// Initialize the fields in the base class Symbol for SYM in OBJECT.
109
110template<int size, bool big_endian>
111void
f3e9c5c5
ILT
112Symbol::init_base_object(const char* name, const char* version, Object* object,
113 const elfcpp::Sym<size, big_endian>& sym,
114 unsigned int st_shndx, bool is_ordinary)
ead1e424
ILT
115{
116 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
117 sym.get_st_visibility(), sym.get_st_nonvis());
118 this->u_.from_object.object = object;
d491d34e
ILT
119 this->u_.from_object.shndx = st_shndx;
120 this->is_ordinary_shndx_ = is_ordinary;
ead1e424 121 this->source_ = FROM_OBJECT;
008db82e 122 this->in_reg_ = !object->is_dynamic();
1564db8d 123 this->in_dyn_ = object->is_dynamic();
89fc3421 124 this->in_real_elf_ = object->pluginobj() == NULL;
14bfc3f5
ILT
125}
126
ead1e424
ILT
127// Initialize the fields in the base class Symbol for a symbol defined
128// in an Output_data.
129
130void
75517b77
ILT
131Symbol::init_base_output_data(const char* name, const char* version,
132 Output_data* od, elfcpp::STT type,
133 elfcpp::STB binding, elfcpp::STV visibility,
134 unsigned char nonvis, bool offset_is_from_end)
ead1e424 135{
75517b77 136 this->init_fields(name, version, type, binding, visibility, nonvis);
ead1e424
ILT
137 this->u_.in_output_data.output_data = od;
138 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
139 this->source_ = IN_OUTPUT_DATA;
008db82e 140 this->in_reg_ = true;
89fc3421 141 this->in_real_elf_ = true;
ead1e424
ILT
142}
143
144// Initialize the fields in the base class Symbol for a symbol defined
145// in an Output_segment.
146
147void
75517b77
ILT
148Symbol::init_base_output_segment(const char* name, const char* version,
149 Output_segment* os, elfcpp::STT type,
150 elfcpp::STB binding, elfcpp::STV visibility,
151 unsigned char nonvis,
f3e9c5c5 152 Segment_offset_base offset_base)
ead1e424 153{
75517b77 154 this->init_fields(name, version, type, binding, visibility, nonvis);
ead1e424
ILT
155 this->u_.in_output_segment.output_segment = os;
156 this->u_.in_output_segment.offset_base = offset_base;
157 this->source_ = IN_OUTPUT_SEGMENT;
008db82e 158 this->in_reg_ = true;
89fc3421 159 this->in_real_elf_ = true;
ead1e424
ILT
160}
161
162// Initialize the fields in the base class Symbol for a symbol defined
163// as a constant.
164
165void
75517b77
ILT
166Symbol::init_base_constant(const char* name, const char* version,
167 elfcpp::STT type, elfcpp::STB binding,
168 elfcpp::STV visibility, unsigned char nonvis)
f3e9c5c5 169{
75517b77 170 this->init_fields(name, version, type, binding, visibility, nonvis);
f3e9c5c5
ILT
171 this->source_ = IS_CONSTANT;
172 this->in_reg_ = true;
89fc3421 173 this->in_real_elf_ = true;
f3e9c5c5
ILT
174}
175
176// Initialize the fields in the base class Symbol for an undefined
177// symbol.
178
179void
75517b77
ILT
180Symbol::init_base_undefined(const char* name, const char* version,
181 elfcpp::STT type, elfcpp::STB binding,
182 elfcpp::STV visibility, unsigned char nonvis)
ead1e424 183{
75517b77 184 this->init_fields(name, version, type, binding, visibility, nonvis);
d7ab2a47 185 this->dynsym_index_ = -1U;
f3e9c5c5 186 this->source_ = IS_UNDEFINED;
008db82e 187 this->in_reg_ = true;
89fc3421 188 this->in_real_elf_ = true;
ead1e424
ILT
189}
190
c7912668
ILT
191// Allocate a common symbol in the base.
192
193void
194Symbol::allocate_base_common(Output_data* od)
195{
196 gold_assert(this->is_common());
197 this->source_ = IN_OUTPUT_DATA;
198 this->u_.in_output_data.output_data = od;
199 this->u_.in_output_data.offset_is_from_end = false;
200}
201
ead1e424 202// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
203
204template<int size>
205template<bool big_endian>
206void
f3e9c5c5
ILT
207Sized_symbol<size>::init_object(const char* name, const char* version,
208 Object* object,
209 const elfcpp::Sym<size, big_endian>& sym,
210 unsigned int st_shndx, bool is_ordinary)
14bfc3f5 211{
f3e9c5c5 212 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
14bfc3f5 213 this->value_ = sym.get_st_value();
ead1e424
ILT
214 this->symsize_ = sym.get_st_size();
215}
216
217// Initialize the fields in Sized_symbol for a symbol defined in an
218// Output_data.
219
220template<int size>
221void
75517b77
ILT
222Sized_symbol<size>::init_output_data(const char* name, const char* version,
223 Output_data* od, Value_type value,
224 Size_type symsize, elfcpp::STT type,
225 elfcpp::STB binding,
f3e9c5c5
ILT
226 elfcpp::STV visibility,
227 unsigned char nonvis,
228 bool offset_is_from_end)
ead1e424 229{
75517b77
ILT
230 this->init_base_output_data(name, version, od, type, binding, visibility,
231 nonvis, offset_is_from_end);
ead1e424
ILT
232 this->value_ = value;
233 this->symsize_ = symsize;
234}
235
236// Initialize the fields in Sized_symbol for a symbol defined in an
237// Output_segment.
238
239template<int size>
240void
75517b77
ILT
241Sized_symbol<size>::init_output_segment(const char* name, const char* version,
242 Output_segment* os, Value_type value,
243 Size_type symsize, elfcpp::STT type,
244 elfcpp::STB binding,
f3e9c5c5
ILT
245 elfcpp::STV visibility,
246 unsigned char nonvis,
247 Segment_offset_base offset_base)
ead1e424 248{
75517b77
ILT
249 this->init_base_output_segment(name, version, os, type, binding, visibility,
250 nonvis, offset_base);
ead1e424
ILT
251 this->value_ = value;
252 this->symsize_ = symsize;
253}
254
255// Initialize the fields in Sized_symbol for a symbol defined as a
256// constant.
257
258template<int size>
259void
75517b77
ILT
260Sized_symbol<size>::init_constant(const char* name, const char* version,
261 Value_type value, Size_type symsize,
262 elfcpp::STT type, elfcpp::STB binding,
263 elfcpp::STV visibility, unsigned char nonvis)
ead1e424 264{
75517b77 265 this->init_base_constant(name, version, type, binding, visibility, nonvis);
ead1e424
ILT
266 this->value_ = value;
267 this->symsize_ = symsize;
14bfc3f5
ILT
268}
269
f3e9c5c5
ILT
270// Initialize the fields in Sized_symbol for an undefined symbol.
271
272template<int size>
273void
75517b77
ILT
274Sized_symbol<size>::init_undefined(const char* name, const char* version,
275 elfcpp::STT type, elfcpp::STB binding,
276 elfcpp::STV visibility, unsigned char nonvis)
f3e9c5c5 277{
75517b77 278 this->init_base_undefined(name, version, type, binding, visibility, nonvis);
f3e9c5c5
ILT
279 this->value_ = 0;
280 this->symsize_ = 0;
281}
282
8a5e3e08
ILT
283// Return true if SHNDX represents a common symbol.
284
285bool
286Symbol::is_common_shndx(unsigned int shndx)
287{
288 return (shndx == elfcpp::SHN_COMMON
289 || shndx == parameters->target().small_common_shndx()
290 || shndx == parameters->target().large_common_shndx());
291}
292
c7912668
ILT
293// Allocate a common symbol.
294
295template<int size>
296void
297Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
298{
299 this->allocate_base_common(od);
300 this->value_ = value;
301}
302
c82fbeee
CS
303// The ""'s around str ensure str is a string literal, so sizeof works.
304#define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
305
436ca963
ILT
306// Return true if this symbol should be added to the dynamic symbol
307// table.
308
309inline bool
310Symbol::should_add_dynsym_entry() const
311{
312 // If the symbol is used by a dynamic relocation, we need to add it.
313 if (this->needs_dynsym_entry())
314 return true;
315
6d03d481
ST
316 // If this symbol's section is not added, the symbol need not be added.
317 // The section may have been GCed. Note that export_dynamic is being
318 // overridden here. This should not be done for shared objects.
319 if (parameters->options().gc_sections()
320 && !parameters->options().shared()
321 && this->source() == Symbol::FROM_OBJECT
322 && !this->object()->is_dynamic())
323 {
324 Relobj* relobj = static_cast<Relobj*>(this->object());
325 bool is_ordinary;
326 unsigned int shndx = this->shndx(&is_ordinary);
327 if (is_ordinary && shndx != elfcpp::SHN_UNDEF
328 && !relobj->is_section_included(shndx))
329 return false;
330 }
331
55a93433
ILT
332 // If the symbol was forced local in a version script, do not add it.
333 if (this->is_forced_local())
334 return false;
335
c82fbeee
CS
336 // If the symbol was forced dynamic in a --dynamic-list file, add it.
337 if (parameters->options().in_dynamic_list(this->name()))
338 return true;
339
340 // If dynamic-list-data was specified, add any STT_OBJECT.
341 if (parameters->options().dynamic_list_data()
342 && !this->is_from_dynobj()
343 && this->type() == elfcpp::STT_OBJECT)
344 return true;
345
346 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
347 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
348 if ((parameters->options().dynamic_list_cpp_new()
349 || parameters->options().dynamic_list_cpp_typeinfo())
350 && !this->is_from_dynobj())
351 {
352 // TODO(csilvers): We could probably figure out if we're an operator
353 // new/delete or typeinfo without the need to demangle.
354 char* demangled_name = cplus_demangle(this->name(),
355 DMGL_ANSI | DMGL_PARAMS);
356 if (demangled_name == NULL)
357 {
358 // Not a C++ symbol, so it can't satisfy these flags
359 }
360 else if (parameters->options().dynamic_list_cpp_new()
361 && (strprefix(demangled_name, "operator new")
362 || strprefix(demangled_name, "operator delete")))
363 {
364 free(demangled_name);
365 return true;
366 }
367 else if (parameters->options().dynamic_list_cpp_typeinfo()
368 && (strprefix(demangled_name, "typeinfo name for")
369 || strprefix(demangled_name, "typeinfo for")))
370 {
371 free(demangled_name);
372 return true;
373 }
374 else
375 free(demangled_name);
376 }
377
436ca963
ILT
378 // If exporting all symbols or building a shared library,
379 // and the symbol is defined in a regular object and is
380 // externally visible, we need to add it.
8851ecca 381 if ((parameters->options().export_dynamic() || parameters->options().shared())
436ca963
ILT
382 && !this->is_from_dynobj()
383 && this->is_externally_visible())
384 return true;
385
386 return false;
387}
388
b3b74ddc
ILT
389// Return true if the final value of this symbol is known at link
390// time.
391
392bool
393Symbol::final_value_is_known() const
394{
395 // If we are not generating an executable, then no final values are
396 // known, since they will change at runtime.
8851ecca 397 if (parameters->options().shared() || parameters->options().relocatable())
b3b74ddc
ILT
398 return false;
399
f3e9c5c5
ILT
400 // If the symbol is not from an object file, and is not undefined,
401 // then it is defined, and known.
b3b74ddc 402 if (this->source_ != FROM_OBJECT)
f3e9c5c5
ILT
403 {
404 if (this->source_ != IS_UNDEFINED)
405 return true;
406 }
407 else
408 {
409 // If the symbol is from a dynamic object, then the final value
410 // is not known.
411 if (this->object()->is_dynamic())
412 return false;
b3b74ddc 413
f3e9c5c5
ILT
414 // If the symbol is not undefined (it is defined or common),
415 // then the final value is known.
416 if (!this->is_undefined())
417 return true;
418 }
b3b74ddc
ILT
419
420 // If the symbol is undefined, then whether the final value is known
421 // depends on whether we are doing a static link. If we are doing a
422 // dynamic link, then the final value could be filled in at runtime.
423 // This could reasonably be the case for a weak undefined symbol.
424 return parameters->doing_static_link();
425}
426
77e65537 427// Return the output section where this symbol is defined.
a445fddf 428
77e65537
ILT
429Output_section*
430Symbol::output_section() const
a445fddf
ILT
431{
432 switch (this->source_)
433 {
434 case FROM_OBJECT:
77e65537
ILT
435 {
436 unsigned int shndx = this->u_.from_object.shndx;
d491d34e 437 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
77e65537
ILT
438 {
439 gold_assert(!this->u_.from_object.object->is_dynamic());
89fc3421 440 gold_assert(this->u_.from_object.object->pluginobj() == NULL);
77e65537 441 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
ef9beddf 442 return relobj->output_section(shndx);
77e65537
ILT
443 }
444 return NULL;
445 }
446
a445fddf 447 case IN_OUTPUT_DATA:
77e65537
ILT
448 return this->u_.in_output_data.output_data->output_section();
449
a445fddf 450 case IN_OUTPUT_SEGMENT:
f3e9c5c5
ILT
451 case IS_CONSTANT:
452 case IS_UNDEFINED:
77e65537
ILT
453 return NULL;
454
455 default:
456 gold_unreachable();
457 }
458}
459
460// Set the symbol's output section. This is used for symbols defined
461// in scripts. This should only be called after the symbol table has
462// been finalized.
463
464void
465Symbol::set_output_section(Output_section* os)
466{
467 switch (this->source_)
468 {
469 case FROM_OBJECT:
470 case IN_OUTPUT_DATA:
471 gold_assert(this->output_section() == os);
472 break;
f3e9c5c5 473 case IS_CONSTANT:
77e65537
ILT
474 this->source_ = IN_OUTPUT_DATA;
475 this->u_.in_output_data.output_data = os;
476 this->u_.in_output_data.offset_is_from_end = false;
477 break;
478 case IN_OUTPUT_SEGMENT:
f3e9c5c5 479 case IS_UNDEFINED:
a445fddf
ILT
480 default:
481 gold_unreachable();
482 }
483}
484
14bfc3f5
ILT
485// Class Symbol_table.
486
09124467
ILT
487Symbol_table::Symbol_table(unsigned int count,
488 const Version_script_info& version_script)
6d013333 489 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
8a5e3e08
ILT
490 forwarders_(), commons_(), tls_commons_(), small_commons_(),
491 large_commons_(), forced_locals_(), warnings_(),
6d03d481 492 version_script_(version_script), gc_(NULL)
14bfc3f5 493{
6d013333 494 namepool_.reserve(count);
14bfc3f5
ILT
495}
496
497Symbol_table::~Symbol_table()
498{
499}
500
ad8f37d1 501// The hash function. The key values are Stringpool keys.
14bfc3f5 502
ad8f37d1 503inline size_t
14bfc3f5
ILT
504Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
505{
f0641a0b 506 return key.first ^ key.second;
14bfc3f5
ILT
507}
508
ad8f37d1
ILT
509// The symbol table key equality function. This is called with
510// Stringpool keys.
14bfc3f5 511
ad8f37d1 512inline bool
14bfc3f5
ILT
513Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
514 const Symbol_table_key& k2) const
515{
516 return k1.first == k2.first && k1.second == k2.second;
517}
518
6d03d481
ST
519// For symbols that have been listed with -u option, add them to the
520// work list to avoid gc'ing them.
521
522void
523Symbol_table::gc_mark_undef_symbols()
524{
525 for (options::String_set::const_iterator p =
526 parameters->options().undefined_begin();
527 p != parameters->options().undefined_end();
528 ++p)
529 {
530 const char* name = p->c_str();
531 Symbol* sym = this->lookup(name);
532 gold_assert (sym != NULL);
533 if (sym->source() == Symbol::FROM_OBJECT
534 && !sym->object()->is_dynamic())
535 {
536 Relobj* obj = static_cast<Relobj*>(sym->object());
537 bool is_ordinary;
538 unsigned int shndx = sym->shndx(&is_ordinary);
539 if (is_ordinary)
540 {
541 gold_assert(this->gc_ != NULL);
542 this->gc_->worklist().push(Section_id(obj, shndx));
543 }
544 }
545 }
546}
547
548void
549Symbol_table::gc_mark_symbol_for_shlib(Symbol* sym)
550{
551 if (!sym->is_from_dynobj()
552 && sym->is_externally_visible())
553 {
554 //Add the object and section to the work list.
555 Relobj* obj = static_cast<Relobj*>(sym->object());
556 bool is_ordinary;
557 unsigned int shndx = sym->shndx(&is_ordinary);
558 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
559 {
560 gold_assert(this->gc_!= NULL);
561 this->gc_->worklist().push(Section_id(obj, shndx));
562 }
563 }
564}
565
566// When doing garbage collection, keep symbols that have been seen in
567// dynamic objects.
568inline void
569Symbol_table::gc_mark_dyn_syms(Symbol* sym)
570{
571 if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
572 && !sym->object()->is_dynamic())
573 {
574 Relobj *obj = static_cast<Relobj*>(sym->object());
575 bool is_ordinary;
576 unsigned int shndx = sym->shndx(&is_ordinary);
577 if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
578 {
579 gold_assert(this->gc_ != NULL);
580 this->gc_->worklist().push(Section_id(obj, shndx));
581 }
582 }
583}
584
dd8670e5 585// Make TO a symbol which forwards to FROM.
14bfc3f5
ILT
586
587void
588Symbol_table::make_forwarder(Symbol* from, Symbol* to)
589{
a3ad94ed
ILT
590 gold_assert(from != to);
591 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
592 this->forwarders_[from] = to;
593 from->set_forwarder();
594}
595
61ba1cf9
ILT
596// Resolve the forwards from FROM, returning the real symbol.
597
14bfc3f5 598Symbol*
c06b7b0b 599Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 600{
a3ad94ed 601 gold_assert(from->is_forwarder());
c06b7b0b 602 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 603 this->forwarders_.find(from);
a3ad94ed 604 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
605 return p->second;
606}
607
61ba1cf9
ILT
608// Look up a symbol by name.
609
610Symbol*
611Symbol_table::lookup(const char* name, const char* version) const
612{
f0641a0b
ILT
613 Stringpool::Key name_key;
614 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
615 if (name == NULL)
616 return NULL;
f0641a0b
ILT
617
618 Stringpool::Key version_key = 0;
61ba1cf9
ILT
619 if (version != NULL)
620 {
f0641a0b 621 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
622 if (version == NULL)
623 return NULL;
624 }
625
f0641a0b 626 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
627 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
628 if (p == this->table_.end())
629 return NULL;
630 return p->second;
631}
632
14bfc3f5
ILT
633// Resolve a Symbol with another Symbol. This is only used in the
634// unusual case where there are references to both an unversioned
635// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
636// version is the default version. Because this is unusual, we do
637// this the slow way, by converting back to an ELF symbol.
14bfc3f5 638
1564db8d 639template<int size, bool big_endian>
14bfc3f5 640void
95d14cd3 641Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
14bfc3f5 642{
1564db8d
ILT
643 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
644 elfcpp::Sym_write<size, big_endian> esym(buf);
d491d34e 645 // We don't bother to set the st_name or the st_shndx field.
1564db8d
ILT
646 esym.put_st_value(from->value());
647 esym.put_st_size(from->symsize());
648 esym.put_st_info(from->binding(), from->type());
ead1e424 649 esym.put_st_other(from->visibility(), from->nonvis());
d491d34e
ILT
650 bool is_ordinary;
651 unsigned int shndx = from->shndx(&is_ordinary);
652 this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
95d14cd3 653 from->version());
1ebd95fd
ILT
654 if (from->in_reg())
655 to->set_in_reg();
656 if (from->in_dyn())
657 to->set_in_dyn();
6d03d481
ST
658 if (parameters->options().gc_sections())
659 this->gc_mark_dyn_syms(to);
14bfc3f5
ILT
660}
661
0602e05a
ILT
662// Record that a symbol is forced to be local by a version script or
663// by visibility.
55a93433
ILT
664
665void
666Symbol_table::force_local(Symbol* sym)
667{
668 if (!sym->is_defined() && !sym->is_common())
669 return;
670 if (sym->is_forced_local())
671 {
672 // We already got this one.
673 return;
674 }
675 sym->set_is_forced_local();
676 this->forced_locals_.push_back(sym);
677}
678
0864d551
ILT
679// Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
680// is only called for undefined symbols, when at least one --wrap
681// option was used.
682
683const char*
684Symbol_table::wrap_symbol(Object* object, const char* name,
685 Stringpool::Key* name_key)
686{
687 // For some targets, we need to ignore a specific character when
688 // wrapping, and add it back later.
689 char prefix = '\0';
690 if (name[0] == object->target()->wrap_char())
691 {
692 prefix = name[0];
693 ++name;
694 }
695
c5818ff1 696 if (parameters->options().is_wrap(name))
0864d551
ILT
697 {
698 // Turn NAME into __wrap_NAME.
699 std::string s;
700 if (prefix != '\0')
701 s += prefix;
702 s += "__wrap_";
703 s += name;
704
705 // This will give us both the old and new name in NAMEPOOL_, but
706 // that is OK. Only the versions we need will wind up in the
707 // real string table in the output file.
708 return this->namepool_.add(s.c_str(), true, name_key);
709 }
710
711 const char* const real_prefix = "__real_";
712 const size_t real_prefix_length = strlen(real_prefix);
713 if (strncmp(name, real_prefix, real_prefix_length) == 0
c5818ff1 714 && parameters->options().is_wrap(name + real_prefix_length))
0864d551
ILT
715 {
716 // Turn __real_NAME into NAME.
717 std::string s;
718 if (prefix != '\0')
719 s += prefix;
720 s += name + real_prefix_length;
721 return this->namepool_.add(s.c_str(), true, name_key);
722 }
723
724 return name;
725}
726
8c500701
ILT
727// This is called when we see a symbol NAME/VERSION, and the symbol
728// already exists in the symbol table, and VERSION is marked as being
729// the default version. SYM is the NAME/VERSION symbol we just added.
730// DEFAULT_IS_NEW is true if this is the first time we have seen the
731// symbol NAME/NULL. PDEF points to the entry for NAME/NULL.
732
733template<int size, bool big_endian>
734void
735Symbol_table::define_default_version(Sized_symbol<size>* sym,
736 bool default_is_new,
737 Symbol_table_type::iterator pdef)
738{
739 if (default_is_new)
740 {
741 // This is the first time we have seen NAME/NULL. Make
742 // NAME/NULL point to NAME/VERSION, and mark SYM as the default
743 // version.
744 pdef->second = sym;
745 sym->set_is_default();
746 }
747 else if (pdef->second == sym)
748 {
749 // NAME/NULL already points to NAME/VERSION. Don't mark the
750 // symbol as the default if it is not already the default.
751 }
752 else
753 {
754 // This is the unfortunate case where we already have entries
755 // for both NAME/VERSION and NAME/NULL. We now see a symbol
756 // NAME/VERSION where VERSION is the default version. We have
757 // already resolved this new symbol with the existing
758 // NAME/VERSION symbol.
759
760 // It's possible that NAME/NULL and NAME/VERSION are both
761 // defined in regular objects. This can only happen if one
762 // object file defines foo and another defines foo@@ver. This
763 // is somewhat obscure, but we call it a multiple definition
764 // error.
765
766 // It's possible that NAME/NULL actually has a version, in which
767 // case it won't be the same as VERSION. This happens with
768 // ver_test_7.so in the testsuite for the symbol t2_2. We see
769 // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We
770 // then see an unadorned t2_2 in an object file and give it
771 // version VER1 from the version script. This looks like a
772 // default definition for VER1, so it looks like we should merge
773 // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's
774 // not obvious that this is an error, either. So we just punt.
775
776 // If one of the symbols has non-default visibility, and the
777 // other is defined in a shared object, then they are different
778 // symbols.
779
780 // Otherwise, we just resolve the symbols as though they were
781 // the same.
782
783 if (pdef->second->version() != NULL)
784 gold_assert(pdef->second->version() != sym->version());
785 else if (sym->visibility() != elfcpp::STV_DEFAULT
786 && pdef->second->is_from_dynobj())
787 ;
788 else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
789 && sym->is_from_dynobj())
790 ;
791 else
792 {
793 const Sized_symbol<size>* symdef;
794 symdef = this->get_sized_symbol<size>(pdef->second);
795 Symbol_table::resolve<size, big_endian>(sym, symdef);
796 this->make_forwarder(pdef->second, sym);
797 pdef->second = sym;
798 sym->set_is_default();
799 }
800 }
801}
802
14bfc3f5
ILT
803// Add one symbol from OBJECT to the symbol table. NAME is symbol
804// name and VERSION is the version; both are canonicalized. DEF is
d491d34e
ILT
805// whether this is the default version. ST_SHNDX is the symbol's
806// section index; IS_ORDINARY is whether this is a normal section
807// rather than a special code.
14bfc3f5
ILT
808
809// If DEF is true, then this is the definition of a default version of
810// a symbol. That means that any lookup of NAME/NULL and any lookup
811// of NAME/VERSION should always return the same symbol. This is
812// obvious for references, but in particular we want to do this for
813// definitions: overriding NAME/NULL should also override
814// NAME/VERSION. If we don't do that, it would be very hard to
815// override functions in a shared library which uses versioning.
816
817// We implement this by simply making both entries in the hash table
818// point to the same Symbol structure. That is easy enough if this is
819// the first time we see NAME/NULL or NAME/VERSION, but it is possible
820// that we have seen both already, in which case they will both have
821// independent entries in the symbol table. We can't simply change
822// the symbol table entry, because we have pointers to the entries
823// attached to the object files. So we mark the entry attached to the
824// object file as a forwarder, and record it in the forwarders_ map.
825// Note that entries in the hash table will never be marked as
826// forwarders.
70e654ba 827//
d491d34e
ILT
828// ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
829// ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
830// for a special section code. ST_SHNDX may be modified if the symbol
831// is defined in a section being discarded.
14bfc3f5
ILT
832
833template<int size, bool big_endian>
aeddab66 834Sized_symbol<size>*
f6ce93d6 835Symbol_table::add_from_object(Object* object,
14bfc3f5 836 const char *name,
f0641a0b
ILT
837 Stringpool::Key name_key,
838 const char *version,
839 Stringpool::Key version_key,
840 bool def,
70e654ba 841 const elfcpp::Sym<size, big_endian>& sym,
d491d34e
ILT
842 unsigned int st_shndx,
843 bool is_ordinary,
844 unsigned int orig_st_shndx)
14bfc3f5 845{
c5818ff1
CC
846 // Print a message if this symbol is being traced.
847 if (parameters->options().is_trace_symbol(name))
848 {
d491d34e 849 if (orig_st_shndx == elfcpp::SHN_UNDEF)
c5818ff1
CC
850 gold_info(_("%s: reference to %s"), object->name().c_str(), name);
851 else
852 gold_info(_("%s: definition of %s"), object->name().c_str(), name);
853 }
854
0864d551
ILT
855 // For an undefined symbol, we may need to adjust the name using
856 // --wrap.
d491d34e 857 if (orig_st_shndx == elfcpp::SHN_UNDEF
c5818ff1 858 && parameters->options().any_wrap())
0864d551
ILT
859 {
860 const char* wrap_name = this->wrap_symbol(object, name, &name_key);
861 if (wrap_name != name)
862 {
863 // If we see a reference to malloc with version GLIBC_2.0,
864 // and we turn it into a reference to __wrap_malloc, then we
865 // discard the version number. Otherwise the user would be
866 // required to specify the correct version for
867 // __wrap_malloc.
868 version = NULL;
869 version_key = 0;
870 name = wrap_name;
871 }
872 }
873
14bfc3f5
ILT
874 Symbol* const snull = NULL;
875 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
876 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
877 snull));
14bfc3f5
ILT
878
879 std::pair<typename Symbol_table_type::iterator, bool> insdef =
880 std::make_pair(this->table_.end(), false);
881 if (def)
882 {
f0641a0b
ILT
883 const Stringpool::Key vnull_key = 0;
884 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
885 vnull_key),
14bfc3f5
ILT
886 snull));
887 }
888
889 // ins.first: an iterator, which is a pointer to a pair.
890 // ins.first->first: the key (a pair of name and version).
891 // ins.first->second: the value (Symbol*).
892 // ins.second: true if new entry was inserted, false if not.
893
1564db8d 894 Sized_symbol<size>* ret;
ead1e424
ILT
895 bool was_undefined;
896 bool was_common;
14bfc3f5
ILT
897 if (!ins.second)
898 {
899 // We already have an entry for NAME/VERSION.
7d1a9ebb 900 ret = this->get_sized_symbol<size>(ins.first->second);
a3ad94ed 901 gold_assert(ret != NULL);
ead1e424
ILT
902
903 was_undefined = ret->is_undefined();
904 was_common = ret->is_common();
905
d491d34e
ILT
906 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
907 version);
6d03d481
ST
908 if (parameters->options().gc_sections())
909 this->gc_mark_dyn_syms(ret);
14bfc3f5
ILT
910
911 if (def)
8c500701
ILT
912 this->define_default_version<size, big_endian>(ret, insdef.second,
913 insdef.first);
14bfc3f5
ILT
914 }
915 else
916 {
917 // This is the first time we have seen NAME/VERSION.
a3ad94ed 918 gold_assert(ins.first->second == NULL);
ead1e424 919
14bfc3f5
ILT
920 if (def && !insdef.second)
921 {
14b31740
ILT
922 // We already have an entry for NAME/NULL. If we override
923 // it, then change it to NAME/VERSION.
7d1a9ebb 924 ret = this->get_sized_symbol<size>(insdef.first->second);
18e6b24e
ILT
925
926 was_undefined = ret->is_undefined();
927 was_common = ret->is_common();
928
d491d34e
ILT
929 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
930 version);
6d03d481
ST
931 if (parameters->options().gc_sections())
932 this->gc_mark_dyn_syms(ret);
14bfc3f5
ILT
933 ins.first->second = ret;
934 }
935 else
936 {
18e6b24e
ILT
937 was_undefined = false;
938 was_common = false;
939
f6ce93d6 940 Sized_target<size, big_endian>* target =
7d1a9ebb 941 object->sized_target<size, big_endian>();
1564db8d
ILT
942 if (!target->has_make_symbol())
943 ret = new Sized_symbol<size>();
944 else
14bfc3f5 945 {
1564db8d
ILT
946 ret = target->make_symbol();
947 if (ret == NULL)
14bfc3f5
ILT
948 {
949 // This means that we don't want a symbol table
950 // entry after all.
951 if (!def)
952 this->table_.erase(ins.first);
953 else
954 {
955 this->table_.erase(insdef.first);
956 // Inserting insdef invalidated ins.
f0641a0b
ILT
957 this->table_.erase(std::make_pair(name_key,
958 version_key));
14bfc3f5
ILT
959 }
960 return NULL;
961 }
962 }
14bfc3f5 963
f3e9c5c5 964 ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
1564db8d 965
14bfc3f5
ILT
966 ins.first->second = ret;
967 if (def)
968 {
969 // This is the first time we have seen NAME/NULL. Point
970 // it at the new entry for NAME/VERSION.
a3ad94ed 971 gold_assert(insdef.second);
14bfc3f5
ILT
972 insdef.first->second = ret;
973 }
974 }
8c500701
ILT
975
976 if (def)
977 ret->set_is_default();
14bfc3f5
ILT
978 }
979
ead1e424
ILT
980 // Record every time we see a new undefined symbol, to speed up
981 // archive groups.
982 if (!was_undefined && ret->is_undefined())
983 ++this->saw_undefined_;
984
985 // Keep track of common symbols, to speed up common symbol
986 // allocation.
987 if (!was_common && ret->is_common())
155a0dd7 988 {
8a5e3e08 989 if (ret->type() == elfcpp::STT_TLS)
155a0dd7 990 this->tls_commons_.push_back(ret);
8a5e3e08
ILT
991 else if (!is_ordinary
992 && st_shndx == parameters->target().small_common_shndx())
993 this->small_commons_.push_back(ret);
994 else if (!is_ordinary
995 && st_shndx == parameters->target().large_common_shndx())
996 this->large_commons_.push_back(ret);
997 else
998 this->commons_.push_back(ret);
155a0dd7 999 }
ead1e424 1000
0602e05a
ILT
1001 // If we're not doing a relocatable link, then any symbol with
1002 // hidden or internal visibility is local.
1003 if ((ret->visibility() == elfcpp::STV_HIDDEN
1004 || ret->visibility() == elfcpp::STV_INTERNAL)
1005 && (ret->binding() == elfcpp::STB_GLOBAL
1006 || ret->binding() == elfcpp::STB_WEAK)
1007 && !parameters->options().relocatable())
1008 this->force_local(ret);
1009
14bfc3f5
ILT
1010 return ret;
1011}
1012
f6ce93d6 1013// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
1014
1015template<int size, bool big_endian>
1016void
dbe717ef
ILT
1017Symbol_table::add_from_relobj(
1018 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 1019 const unsigned char* syms,
14bfc3f5 1020 size_t count,
d491d34e 1021 size_t symndx_offset,
14bfc3f5
ILT
1022 const char* sym_names,
1023 size_t sym_name_size,
92de84a6
ILT
1024 typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1025 size_t *defined)
14bfc3f5 1026{
92de84a6
ILT
1027 *defined = 0;
1028
9025d29d 1029 gold_assert(size == relobj->target()->get_size());
8851ecca 1030 gold_assert(size == parameters->target().get_size());
14bfc3f5 1031
a783673b
ILT
1032 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1033
88dd47ac
ILT
1034 const bool just_symbols = relobj->just_symbols();
1035
f6ce93d6 1036 const unsigned char* p = syms;
a783673b 1037 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5 1038 {
92de84a6
ILT
1039 (*sympointers)[i] = NULL;
1040
14bfc3f5
ILT
1041 elfcpp::Sym<size, big_endian> sym(p);
1042
d491d34e 1043 unsigned int st_name = sym.get_st_name();
14bfc3f5
ILT
1044 if (st_name >= sym_name_size)
1045 {
75f2446e
ILT
1046 relobj->error(_("bad global symbol name offset %u at %zu"),
1047 st_name, i);
1048 continue;
14bfc3f5
ILT
1049 }
1050
dbe717ef
ILT
1051 const char* name = sym_names + st_name;
1052
d491d34e
ILT
1053 bool is_ordinary;
1054 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1055 sym.get_st_shndx(),
1056 &is_ordinary);
1057 unsigned int orig_st_shndx = st_shndx;
1058 if (!is_ordinary)
1059 orig_st_shndx = elfcpp::SHN_UNDEF;
1060
92de84a6
ILT
1061 if (st_shndx != elfcpp::SHN_UNDEF)
1062 ++*defined;
1063
a783673b
ILT
1064 // A symbol defined in a section which we are not including must
1065 // be treated as an undefined symbol.
a783673b 1066 if (st_shndx != elfcpp::SHN_UNDEF
d491d34e 1067 && is_ordinary
dbe717ef 1068 && !relobj->is_section_included(st_shndx))
d491d34e 1069 st_shndx = elfcpp::SHN_UNDEF;
a783673b 1070
14bfc3f5
ILT
1071 // In an object file, an '@' in the name separates the symbol
1072 // name from the version name. If there are two '@' characters,
1073 // this is the default version.
1074 const char* ver = strchr(name, '@');
057ead22 1075 Stringpool::Key ver_key = 0;
09124467 1076 int namelen = 0;
55a93433 1077 // DEF: is the version default? LOCAL: is the symbol forced local?
09124467 1078 bool def = false;
55a93433 1079 bool local = false;
09124467
ILT
1080
1081 if (ver != NULL)
1082 {
1083 // The symbol name is of the form foo@VERSION or foo@@VERSION
1084 namelen = ver - name;
1085 ++ver;
1086 if (*ver == '@')
1087 {
1088 def = true;
1089 ++ver;
1090 }
057ead22 1091 ver = this->namepool_.add(ver, true, &ver_key);
09124467 1092 }
5871526f
ILT
1093 // We don't want to assign a version to an undefined symbol,
1094 // even if it is listed in the version script. FIXME: What
1095 // about a common symbol?
057ead22
ILT
1096 else
1097 {
1098 namelen = strlen(name);
1099 if (!this->version_script_.empty()
1100 && st_shndx != elfcpp::SHN_UNDEF)
1101 {
1102 // The symbol name did not have a version, but the
1103 // version script may assign a version anyway.
1104 std::string version;
1105 if (this->version_script_.get_symbol_version(name, &version))
1106 {
1107 // The version can be empty if the version script is
1108 // only used to force some symbols to be local.
1109 if (!version.empty())
1110 {
1111 ver = this->namepool_.add_with_length(version.c_str(),
1112 version.length(),
1113 true,
1114 &ver_key);
1115 def = true;
1116 }
1117 }
1118 else if (this->version_script_.symbol_is_local(name))
1119 local = true;
1120 }
1121 }
14bfc3f5 1122
d491d34e
ILT
1123 elfcpp::Sym<size, big_endian>* psym = &sym;
1124 unsigned char symbuf[sym_size];
1125 elfcpp::Sym<size, big_endian> sym2(symbuf);
88dd47ac
ILT
1126 if (just_symbols)
1127 {
d491d34e 1128 memcpy(symbuf, p, sym_size);
88dd47ac 1129 elfcpp::Sym_write<size, big_endian> sw(symbuf);
d491d34e 1130 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
88dd47ac
ILT
1131 {
1132 // Symbol values in object files are section relative.
1133 // This is normally what we want, but since here we are
1134 // converting the symbol to absolute we need to add the
1135 // section address. The section address in an object
1136 // file is normally zero, but people can use a linker
1137 // script to change it.
d491d34e
ILT
1138 sw.put_st_value(sym.get_st_value()
1139 + relobj->section_address(orig_st_shndx));
88dd47ac 1140 }
d491d34e
ILT
1141 st_shndx = elfcpp::SHN_ABS;
1142 is_ordinary = false;
88dd47ac
ILT
1143 psym = &sym2;
1144 }
1145
65514900
CC
1146 // Fix up visibility if object has no-export set.
1147 if (relobj->no_export())
1148 {
1149 // We may have copied symbol already above.
1150 if (psym != &sym2)
1151 {
1152 memcpy(symbuf, p, sym_size);
1153 psym = &sym2;
1154 }
1155
1156 elfcpp::STV visibility = sym2.get_st_visibility();
1157 if (visibility == elfcpp::STV_DEFAULT
1158 || visibility == elfcpp::STV_PROTECTED)
1159 {
1160 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1161 unsigned char nonvis = sym2.get_st_nonvis();
1162 sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1163 }
1164 }
1165
057ead22
ILT
1166 Stringpool::Key name_key;
1167 name = this->namepool_.add_with_length(name, namelen, true,
1168 &name_key);
1169
aeddab66 1170 Sized_symbol<size>* res;
057ead22
ILT
1171 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1172 def, *psym, st_shndx, is_ordinary,
1173 orig_st_shndx);
6d03d481
ST
1174
1175 // If building a shared library using garbage collection, do not
1176 // treat externally visible symbols as garbage.
1177 if (parameters->options().gc_sections()
1178 && parameters->options().shared())
1179 this->gc_mark_symbol_for_shlib(res);
f0641a0b 1180
057ead22
ILT
1181 if (local)
1182 this->force_local(res);
14bfc3f5 1183
730cdc88 1184 (*sympointers)[i] = res;
14bfc3f5
ILT
1185 }
1186}
1187
89fc3421
CC
1188// Add a symbol from a plugin-claimed file.
1189
1190template<int size, bool big_endian>
1191Symbol*
1192Symbol_table::add_from_pluginobj(
1193 Sized_pluginobj<size, big_endian>* obj,
1194 const char* name,
1195 const char* ver,
1196 elfcpp::Sym<size, big_endian>* sym)
1197{
1198 unsigned int st_shndx = sym->get_st_shndx();
1199
1200 Stringpool::Key ver_key = 0;
1201 bool def = false;
1202 bool local = false;
1203
1204 if (ver != NULL)
1205 {
1206 ver = this->namepool_.add(ver, true, &ver_key);
1207 }
1208 // We don't want to assign a version to an undefined symbol,
1209 // even if it is listed in the version script. FIXME: What
1210 // about a common symbol?
1211 else
1212 {
1213 if (!this->version_script_.empty()
1214 && st_shndx != elfcpp::SHN_UNDEF)
1215 {
1216 // The symbol name did not have a version, but the
1217 // version script may assign a version anyway.
1218 std::string version;
1219 if (this->version_script_.get_symbol_version(name, &version))
1220 {
1221 // The version can be empty if the version script is
1222 // only used to force some symbols to be local.
1223 if (!version.empty())
1224 {
1225 ver = this->namepool_.add_with_length(version.c_str(),
1226 version.length(),
1227 true,
1228 &ver_key);
1229 def = true;
1230 }
1231 }
1232 else if (this->version_script_.symbol_is_local(name))
1233 local = true;
1234 }
1235 }
1236
1237 Stringpool::Key name_key;
1238 name = this->namepool_.add(name, true, &name_key);
1239
1240 Sized_symbol<size>* res;
1241 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1242 def, *sym, st_shndx, true, st_shndx);
1243
1244 if (local)
0602e05a 1245 this->force_local(res);
89fc3421
CC
1246
1247 return res;
1248}
1249
dbe717ef
ILT
1250// Add all the symbols in a dynamic object to the hash table.
1251
1252template<int size, bool big_endian>
1253void
1254Symbol_table::add_from_dynobj(
1255 Sized_dynobj<size, big_endian>* dynobj,
1256 const unsigned char* syms,
1257 size_t count,
1258 const char* sym_names,
1259 size_t sym_name_size,
1260 const unsigned char* versym,
1261 size_t versym_size,
92de84a6
ILT
1262 const std::vector<const char*>* version_map,
1263 typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1264 size_t* defined)
dbe717ef 1265{
92de84a6
ILT
1266 *defined = 0;
1267
9025d29d 1268 gold_assert(size == dynobj->target()->get_size());
8851ecca 1269 gold_assert(size == parameters->target().get_size());
dbe717ef 1270
88dd47ac
ILT
1271 if (dynobj->just_symbols())
1272 {
1273 gold_error(_("--just-symbols does not make sense with a shared object"));
1274 return;
1275 }
1276
dbe717ef
ILT
1277 if (versym != NULL && versym_size / 2 < count)
1278 {
75f2446e
ILT
1279 dynobj->error(_("too few symbol versions"));
1280 return;
dbe717ef
ILT
1281 }
1282
1283 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1284
aeddab66
ILT
1285 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1286 // weak aliases. This is necessary because if the dynamic object
1287 // provides the same variable under two names, one of which is a
1288 // weak definition, and the regular object refers to the weak
1289 // definition, we have to put both the weak definition and the
1290 // strong definition into the dynamic symbol table. Given a weak
1291 // definition, the only way that we can find the corresponding
1292 // strong definition, if any, is to search the symbol table.
1293 std::vector<Sized_symbol<size>*> object_symbols;
1294
dbe717ef
ILT
1295 const unsigned char* p = syms;
1296 const unsigned char* vs = versym;
1297 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1298 {
1299 elfcpp::Sym<size, big_endian> sym(p);
1300
92de84a6
ILT
1301 if (sympointers != NULL)
1302 (*sympointers)[i] = NULL;
1303
65778909
ILT
1304 // Ignore symbols with local binding or that have
1305 // internal or hidden visibility.
1306 if (sym.get_st_bind() == elfcpp::STB_LOCAL
1307 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1308 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
dbe717ef
ILT
1309 continue;
1310
8bdcdf2c
ILT
1311 // A protected symbol in a shared library must be treated as a
1312 // normal symbol when viewed from outside the shared library.
1313 // Implement this by overriding the visibility here.
1314 elfcpp::Sym<size, big_endian>* psym = &sym;
1315 unsigned char symbuf[sym_size];
1316 elfcpp::Sym<size, big_endian> sym2(symbuf);
1317 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1318 {
1319 memcpy(symbuf, p, sym_size);
1320 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1321 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1322 psym = &sym2;
1323 }
1324
1325 unsigned int st_name = psym->get_st_name();
dbe717ef
ILT
1326 if (st_name >= sym_name_size)
1327 {
75f2446e
ILT
1328 dynobj->error(_("bad symbol name offset %u at %zu"),
1329 st_name, i);
1330 continue;
dbe717ef
ILT
1331 }
1332
1333 const char* name = sym_names + st_name;
1334
d491d34e 1335 bool is_ordinary;
8bdcdf2c 1336 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
d491d34e
ILT
1337 &is_ordinary);
1338
92de84a6
ILT
1339 if (st_shndx != elfcpp::SHN_UNDEF)
1340 ++*defined;
1341
aeddab66
ILT
1342 Sized_symbol<size>* res;
1343
dbe717ef
ILT
1344 if (versym == NULL)
1345 {
1346 Stringpool::Key name_key;
cfd73a4e 1347 name = this->namepool_.add(name, true, &name_key);
aeddab66 1348 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1349 false, *psym, st_shndx, is_ordinary,
d491d34e 1350 st_shndx);
dbe717ef 1351 }
aeddab66
ILT
1352 else
1353 {
1354 // Read the version information.
dbe717ef 1355
aeddab66 1356 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
dbe717ef 1357
aeddab66
ILT
1358 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1359 v &= elfcpp::VERSYM_VERSION;
dbe717ef 1360
aeddab66
ILT
1361 // The Sun documentation says that V can be VER_NDX_LOCAL,
1362 // or VER_NDX_GLOBAL, or a version index. The meaning of
1363 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1364 // The old GNU linker will happily generate VER_NDX_LOCAL
1365 // for an undefined symbol. I don't know what the Sun
1366 // linker will generate.
dbe717ef 1367
aeddab66 1368 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
d491d34e 1369 && st_shndx != elfcpp::SHN_UNDEF)
aeddab66
ILT
1370 {
1371 // This symbol should not be visible outside the object.
1372 continue;
1373 }
64707334 1374
aeddab66
ILT
1375 // At this point we are definitely going to add this symbol.
1376 Stringpool::Key name_key;
1377 name = this->namepool_.add(name, true, &name_key);
dbe717ef 1378
aeddab66
ILT
1379 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1380 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1381 {
1382 // This symbol does not have a version.
1383 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1384 false, *psym, st_shndx, is_ordinary,
d491d34e 1385 st_shndx);
aeddab66
ILT
1386 }
1387 else
1388 {
1389 if (v >= version_map->size())
1390 {
1391 dynobj->error(_("versym for symbol %zu out of range: %u"),
1392 i, v);
1393 continue;
1394 }
dbe717ef 1395
aeddab66
ILT
1396 const char* version = (*version_map)[v];
1397 if (version == NULL)
1398 {
1399 dynobj->error(_("versym for symbol %zu has no name: %u"),
1400 i, v);
1401 continue;
1402 }
dbe717ef 1403
aeddab66
ILT
1404 Stringpool::Key version_key;
1405 version = this->namepool_.add(version, true, &version_key);
1406
1407 // If this is an absolute symbol, and the version name
1408 // and symbol name are the same, then this is the
1409 // version definition symbol. These symbols exist to
1410 // support using -u to pull in particular versions. We
1411 // do not want to record a version for them.
d491d34e
ILT
1412 if (st_shndx == elfcpp::SHN_ABS
1413 && !is_ordinary
aeddab66
ILT
1414 && name_key == version_key)
1415 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1416 false, *psym, st_shndx, is_ordinary,
d491d34e 1417 st_shndx);
aeddab66
ILT
1418 else
1419 {
1420 const bool def = (!hidden
d491d34e 1421 && st_shndx != elfcpp::SHN_UNDEF);
aeddab66 1422 res = this->add_from_object(dynobj, name, name_key, version,
8bdcdf2c 1423 version_key, def, *psym, st_shndx,
d491d34e 1424 is_ordinary, st_shndx);
aeddab66
ILT
1425 }
1426 }
dbe717ef
ILT
1427 }
1428
99a37bfd 1429 // Note that it is possible that RES was overridden by an
a4bb589a 1430 // earlier object, in which case it can't be aliased here.
d491d34e
ILT
1431 if (st_shndx != elfcpp::SHN_UNDEF
1432 && is_ordinary
8bdcdf2c 1433 && psym->get_st_type() == elfcpp::STT_OBJECT
99a37bfd
ILT
1434 && res->source() == Symbol::FROM_OBJECT
1435 && res->object() == dynobj)
aeddab66 1436 object_symbols.push_back(res);
92de84a6
ILT
1437
1438 if (sympointers != NULL)
1439 (*sympointers)[i] = res;
aeddab66
ILT
1440 }
1441
1442 this->record_weak_aliases(&object_symbols);
1443}
1444
1445// This is used to sort weak aliases. We sort them first by section
1446// index, then by offset, then by weak ahead of strong.
1447
1448template<int size>
1449class Weak_alias_sorter
1450{
1451 public:
1452 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1453};
1454
1455template<int size>
1456bool
1457Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1458 const Sized_symbol<size>* s2) const
1459{
d491d34e
ILT
1460 bool is_ordinary;
1461 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1462 gold_assert(is_ordinary);
1463 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1464 gold_assert(is_ordinary);
1465 if (s1_shndx != s2_shndx)
1466 return s1_shndx < s2_shndx;
1467
aeddab66
ILT
1468 if (s1->value() != s2->value())
1469 return s1->value() < s2->value();
1470 if (s1->binding() != s2->binding())
1471 {
1472 if (s1->binding() == elfcpp::STB_WEAK)
1473 return true;
1474 if (s2->binding() == elfcpp::STB_WEAK)
1475 return false;
1476 }
1477 return std::string(s1->name()) < std::string(s2->name());
1478}
dbe717ef 1479
aeddab66
ILT
1480// SYMBOLS is a list of object symbols from a dynamic object. Look
1481// for any weak aliases, and record them so that if we add the weak
1482// alias to the dynamic symbol table, we also add the corresponding
1483// strong symbol.
dbe717ef 1484
aeddab66
ILT
1485template<int size>
1486void
1487Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1488{
1489 // Sort the vector by section index, then by offset, then by weak
1490 // ahead of strong.
1491 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1492
1493 // Walk through the vector. For each weak definition, record
1494 // aliases.
1495 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1496 symbols->begin();
1497 p != symbols->end();
1498 ++p)
1499 {
1500 if ((*p)->binding() != elfcpp::STB_WEAK)
1501 continue;
1502
1503 // Build a circular list of weak aliases. Each symbol points to
1504 // the next one in the circular list.
1505
1506 Sized_symbol<size>* from_sym = *p;
1507 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1508 for (q = p + 1; q != symbols->end(); ++q)
dbe717ef 1509 {
d491d34e
ILT
1510 bool dummy;
1511 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
aeddab66
ILT
1512 || (*q)->value() != from_sym->value())
1513 break;
1514
1515 this->weak_aliases_[from_sym] = *q;
1516 from_sym->set_has_alias();
1517 from_sym = *q;
dbe717ef
ILT
1518 }
1519
aeddab66
ILT
1520 if (from_sym != *p)
1521 {
1522 this->weak_aliases_[from_sym] = *p;
1523 from_sym->set_has_alias();
1524 }
dbe717ef 1525
aeddab66 1526 p = q - 1;
dbe717ef
ILT
1527 }
1528}
1529
ead1e424
ILT
1530// Create and return a specially defined symbol. If ONLY_IF_REF is
1531// true, then only create the symbol if there is a reference to it.
86f2e683 1532// If this does not return NULL, it sets *POLDSYM to the existing
8c500701
ILT
1533// symbol if there is one. This sets *RESOLVE_OLDSYM if we should
1534// resolve the newly created symbol to the old one. This
1535// canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
1536
1537template<int size, bool big_endian>
1538Sized_symbol<size>*
9b07f471
ILT
1539Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1540 bool only_if_ref,
8c500701
ILT
1541 Sized_symbol<size>** poldsym,
1542 bool *resolve_oldsym)
ead1e424 1543{
8c500701 1544 *resolve_oldsym = false;
ead1e424 1545
55a93433
ILT
1546 // If the caller didn't give us a version, see if we get one from
1547 // the version script.
057ead22 1548 std::string v;
8c500701 1549 bool is_default_version = false;
55a93433
ILT
1550 if (*pversion == NULL)
1551 {
057ead22
ILT
1552 if (this->version_script_.get_symbol_version(*pname, &v))
1553 {
1554 if (!v.empty())
1555 *pversion = v.c_str();
8c500701
ILT
1556
1557 // If we get the version from a version script, then we are
1558 // also the default version.
1559 is_default_version = true;
057ead22 1560 }
55a93433
ILT
1561 }
1562
8c500701
ILT
1563 Symbol* oldsym;
1564 Sized_symbol<size>* sym;
1565
1566 bool add_to_table = false;
1567 typename Symbol_table_type::iterator add_loc = this->table_.end();
1568 bool add_def_to_table = false;
1569 typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1570
ead1e424
ILT
1571 if (only_if_ref)
1572 {
306d9ef0 1573 oldsym = this->lookup(*pname, *pversion);
8c500701
ILT
1574 if (oldsym == NULL && is_default_version)
1575 oldsym = this->lookup(*pname, NULL);
f6ce93d6 1576 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 1577 return NULL;
306d9ef0
ILT
1578
1579 *pname = oldsym->name();
8c500701
ILT
1580 if (!is_default_version)
1581 *pversion = oldsym->version();
ead1e424
ILT
1582 }
1583 else
1584 {
14b31740 1585 // Canonicalize NAME and VERSION.
f0641a0b 1586 Stringpool::Key name_key;
cfd73a4e 1587 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 1588
14b31740 1589 Stringpool::Key version_key = 0;
306d9ef0 1590 if (*pversion != NULL)
cfd73a4e 1591 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 1592
ead1e424 1593 Symbol* const snull = NULL;
ead1e424 1594 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
1595 this->table_.insert(std::make_pair(std::make_pair(name_key,
1596 version_key),
ead1e424
ILT
1597 snull));
1598
8c500701
ILT
1599 std::pair<typename Symbol_table_type::iterator, bool> insdef =
1600 std::make_pair(this->table_.end(), false);
1601 if (is_default_version)
1602 {
1603 const Stringpool::Key vnull = 0;
1604 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
1605 vnull),
1606 snull));
1607 }
1608
ead1e424
ILT
1609 if (!ins.second)
1610 {
14b31740 1611 // We already have a symbol table entry for NAME/VERSION.
ead1e424 1612 oldsym = ins.first->second;
a3ad94ed 1613 gold_assert(oldsym != NULL);
8c500701
ILT
1614
1615 if (is_default_version)
1616 {
1617 Sized_symbol<size>* soldsym =
1618 this->get_sized_symbol<size>(oldsym);
1619 this->define_default_version<size, big_endian>(soldsym,
1620 insdef.second,
1621 insdef.first);
1622 }
ead1e424
ILT
1623 }
1624 else
1625 {
1626 // We haven't seen this symbol before.
a3ad94ed 1627 gold_assert(ins.first->second == NULL);
8c500701
ILT
1628
1629 add_to_table = true;
1630 add_loc = ins.first;
1631
1632 if (is_default_version && !insdef.second)
1633 {
1634 // We are adding NAME/VERSION, and it is the default
1635 // version. We already have an entry for NAME/NULL.
1636 oldsym = insdef.first->second;
1637 *resolve_oldsym = true;
1638 }
1639 else
1640 {
1641 oldsym = NULL;
1642
1643 if (is_default_version)
1644 {
1645 add_def_to_table = true;
1646 add_def_loc = insdef.first;
1647 }
1648 }
ead1e424
ILT
1649 }
1650 }
1651
8851ecca
ILT
1652 const Target& target = parameters->target();
1653 if (!target.has_make_symbol())
86f2e683
ILT
1654 sym = new Sized_symbol<size>();
1655 else
ead1e424 1656 {
8851ecca
ILT
1657 gold_assert(target.get_size() == size);
1658 gold_assert(target.is_big_endian() ? big_endian : !big_endian);
86f2e683
ILT
1659 typedef Sized_target<size, big_endian> My_target;
1660 const My_target* sized_target =
8851ecca 1661 static_cast<const My_target*>(&target);
86f2e683
ILT
1662 sym = sized_target->make_symbol();
1663 if (sym == NULL)
1664 return NULL;
1665 }
ead1e424 1666
86f2e683
ILT
1667 if (add_to_table)
1668 add_loc->second = sym;
1669 else
1670 gold_assert(oldsym != NULL);
ead1e424 1671
8c500701
ILT
1672 if (add_def_to_table)
1673 add_def_loc->second = sym;
1674
7d1a9ebb 1675 *poldsym = this->get_sized_symbol<size>(oldsym);
ead1e424
ILT
1676
1677 return sym;
1678}
1679
1680// Define a symbol based on an Output_data.
1681
14b31740 1682Symbol*
9b07f471
ILT
1683Symbol_table::define_in_output_data(const char* name,
1684 const char* version,
1685 Output_data* od,
1686 uint64_t value,
1687 uint64_t symsize,
1688 elfcpp::STT type,
1689 elfcpp::STB binding,
ead1e424
ILT
1690 elfcpp::STV visibility,
1691 unsigned char nonvis,
1692 bool offset_is_from_end,
1693 bool only_if_ref)
1694{
8851ecca 1695 if (parameters->target().get_size() == 32)
86f2e683
ILT
1696 {
1697#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1698 return this->do_define_in_output_data<32>(name, version, od,
86f2e683
ILT
1699 value, symsize, type, binding,
1700 visibility, nonvis,
1701 offset_is_from_end,
1702 only_if_ref);
1703#else
1704 gold_unreachable();
1705#endif
1706 }
8851ecca 1707 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1708 {
1709#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1710 return this->do_define_in_output_data<64>(name, version, od,
86f2e683
ILT
1711 value, symsize, type, binding,
1712 visibility, nonvis,
1713 offset_is_from_end,
1714 only_if_ref);
1715#else
1716 gold_unreachable();
1717#endif
1718 }
ead1e424 1719 else
a3ad94ed 1720 gold_unreachable();
ead1e424
ILT
1721}
1722
1723// Define a symbol in an Output_data, sized version.
1724
1725template<int size>
14b31740 1726Sized_symbol<size>*
ead1e424 1727Symbol_table::do_define_in_output_data(
ead1e424 1728 const char* name,
14b31740 1729 const char* version,
ead1e424
ILT
1730 Output_data* od,
1731 typename elfcpp::Elf_types<size>::Elf_Addr value,
1732 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1733 elfcpp::STT type,
1734 elfcpp::STB binding,
1735 elfcpp::STV visibility,
1736 unsigned char nonvis,
1737 bool offset_is_from_end,
1738 bool only_if_ref)
1739{
1740 Sized_symbol<size>* sym;
86f2e683 1741 Sized_symbol<size>* oldsym;
8c500701 1742 bool resolve_oldsym;
ead1e424 1743
8851ecca 1744 if (parameters->target().is_big_endian())
193a53d9
ILT
1745 {
1746#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb 1747 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
1748 only_if_ref, &oldsym,
1749 &resolve_oldsym);
193a53d9
ILT
1750#else
1751 gold_unreachable();
1752#endif
1753 }
ead1e424 1754 else
193a53d9
ILT
1755 {
1756#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb 1757 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
1758 only_if_ref, &oldsym,
1759 &resolve_oldsym);
193a53d9
ILT
1760#else
1761 gold_unreachable();
1762#endif
1763 }
ead1e424
ILT
1764
1765 if (sym == NULL)
14b31740 1766 return NULL;
ead1e424 1767
75517b77
ILT
1768 sym->init_output_data(name, version, od, value, symsize, type, binding,
1769 visibility, nonvis, offset_is_from_end);
14b31740 1770
e5756efb 1771 if (oldsym == NULL)
55a93433
ILT
1772 {
1773 if (binding == elfcpp::STB_LOCAL
1774 || this->version_script_.symbol_is_local(name))
1775 this->force_local(sym);
75517b77
ILT
1776 else if (version != NULL)
1777 sym->set_is_default();
55a93433
ILT
1778 return sym;
1779 }
86f2e683 1780
e5756efb
ILT
1781 if (Symbol_table::should_override_with_special(oldsym))
1782 this->override_with_special(oldsym, sym);
8c500701
ILT
1783
1784 if (resolve_oldsym)
1785 return sym;
1786 else
1787 {
1788 delete sym;
1789 return oldsym;
1790 }
ead1e424
ILT
1791}
1792
1793// Define a symbol based on an Output_segment.
1794
14b31740 1795Symbol*
9b07f471 1796Symbol_table::define_in_output_segment(const char* name,
14b31740 1797 const char* version, Output_segment* os,
9b07f471
ILT
1798 uint64_t value,
1799 uint64_t symsize,
1800 elfcpp::STT type,
1801 elfcpp::STB binding,
ead1e424
ILT
1802 elfcpp::STV visibility,
1803 unsigned char nonvis,
1804 Symbol::Segment_offset_base offset_base,
1805 bool only_if_ref)
1806{
8851ecca 1807 if (parameters->target().get_size() == 32)
86f2e683
ILT
1808 {
1809#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1810 return this->do_define_in_output_segment<32>(name, version, os,
86f2e683
ILT
1811 value, symsize, type,
1812 binding, visibility, nonvis,
1813 offset_base, only_if_ref);
1814#else
1815 gold_unreachable();
1816#endif
1817 }
8851ecca 1818 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1819 {
1820#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1821 return this->do_define_in_output_segment<64>(name, version, os,
86f2e683
ILT
1822 value, symsize, type,
1823 binding, visibility, nonvis,
1824 offset_base, only_if_ref);
1825#else
1826 gold_unreachable();
1827#endif
1828 }
ead1e424 1829 else
a3ad94ed 1830 gold_unreachable();
ead1e424
ILT
1831}
1832
1833// Define a symbol in an Output_segment, sized version.
1834
1835template<int size>
14b31740 1836Sized_symbol<size>*
ead1e424 1837Symbol_table::do_define_in_output_segment(
ead1e424 1838 const char* name,
14b31740 1839 const char* version,
ead1e424
ILT
1840 Output_segment* os,
1841 typename elfcpp::Elf_types<size>::Elf_Addr value,
1842 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1843 elfcpp::STT type,
1844 elfcpp::STB binding,
1845 elfcpp::STV visibility,
1846 unsigned char nonvis,
1847 Symbol::Segment_offset_base offset_base,
1848 bool only_if_ref)
1849{
1850 Sized_symbol<size>* sym;
86f2e683 1851 Sized_symbol<size>* oldsym;
8c500701 1852 bool resolve_oldsym;
ead1e424 1853
8851ecca 1854 if (parameters->target().is_big_endian())
9025d29d
ILT
1855 {
1856#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb 1857 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
1858 only_if_ref, &oldsym,
1859 &resolve_oldsym);
9025d29d
ILT
1860#else
1861 gold_unreachable();
1862#endif
1863 }
ead1e424 1864 else
9025d29d
ILT
1865 {
1866#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb 1867 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
1868 only_if_ref, &oldsym,
1869 &resolve_oldsym);
9025d29d
ILT
1870#else
1871 gold_unreachable();
1872#endif
1873 }
ead1e424
ILT
1874
1875 if (sym == NULL)
14b31740 1876 return NULL;
ead1e424 1877
75517b77 1878 sym->init_output_segment(name, version, os, value, symsize, type, binding,
f3e9c5c5 1879 visibility, nonvis, offset_base);
14b31740 1880
e5756efb 1881 if (oldsym == NULL)
55a93433
ILT
1882 {
1883 if (binding == elfcpp::STB_LOCAL
1884 || this->version_script_.symbol_is_local(name))
1885 this->force_local(sym);
75517b77
ILT
1886 else if (version != NULL)
1887 sym->set_is_default();
55a93433
ILT
1888 return sym;
1889 }
86f2e683 1890
e5756efb
ILT
1891 if (Symbol_table::should_override_with_special(oldsym))
1892 this->override_with_special(oldsym, sym);
8c500701
ILT
1893
1894 if (resolve_oldsym)
1895 return sym;
1896 else
1897 {
1898 delete sym;
1899 return oldsym;
1900 }
ead1e424
ILT
1901}
1902
1903// Define a special symbol with a constant value. It is a multiple
1904// definition error if this symbol is already defined.
1905
14b31740 1906Symbol*
9b07f471
ILT
1907Symbol_table::define_as_constant(const char* name,
1908 const char* version,
1909 uint64_t value,
1910 uint64_t symsize,
1911 elfcpp::STT type,
1912 elfcpp::STB binding,
1913 elfcpp::STV visibility,
1914 unsigned char nonvis,
caa9d5d9
ILT
1915 bool only_if_ref,
1916 bool force_override)
ead1e424 1917{
8851ecca 1918 if (parameters->target().get_size() == 32)
86f2e683
ILT
1919 {
1920#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1921 return this->do_define_as_constant<32>(name, version, value,
86f2e683 1922 symsize, type, binding,
caa9d5d9
ILT
1923 visibility, nonvis, only_if_ref,
1924 force_override);
86f2e683
ILT
1925#else
1926 gold_unreachable();
1927#endif
1928 }
8851ecca 1929 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1930 {
1931#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1932 return this->do_define_as_constant<64>(name, version, value,
86f2e683 1933 symsize, type, binding,
caa9d5d9
ILT
1934 visibility, nonvis, only_if_ref,
1935 force_override);
86f2e683
ILT
1936#else
1937 gold_unreachable();
1938#endif
1939 }
ead1e424 1940 else
a3ad94ed 1941 gold_unreachable();
ead1e424
ILT
1942}
1943
1944// Define a symbol as a constant, sized version.
1945
1946template<int size>
14b31740 1947Sized_symbol<size>*
ead1e424 1948Symbol_table::do_define_as_constant(
ead1e424 1949 const char* name,
14b31740 1950 const char* version,
ead1e424
ILT
1951 typename elfcpp::Elf_types<size>::Elf_Addr value,
1952 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1953 elfcpp::STT type,
1954 elfcpp::STB binding,
1955 elfcpp::STV visibility,
1956 unsigned char nonvis,
caa9d5d9
ILT
1957 bool only_if_ref,
1958 bool force_override)
ead1e424
ILT
1959{
1960 Sized_symbol<size>* sym;
86f2e683 1961 Sized_symbol<size>* oldsym;
8c500701 1962 bool resolve_oldsym;
ead1e424 1963
8851ecca 1964 if (parameters->target().is_big_endian())
9025d29d
ILT
1965 {
1966#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb 1967 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
1968 only_if_ref, &oldsym,
1969 &resolve_oldsym);
9025d29d
ILT
1970#else
1971 gold_unreachable();
1972#endif
1973 }
ead1e424 1974 else
9025d29d
ILT
1975 {
1976#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb 1977 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
1978 only_if_ref, &oldsym,
1979 &resolve_oldsym);
9025d29d
ILT
1980#else
1981 gold_unreachable();
1982#endif
1983 }
ead1e424
ILT
1984
1985 if (sym == NULL)
14b31740 1986 return NULL;
ead1e424 1987
75517b77
ILT
1988 sym->init_constant(name, version, value, symsize, type, binding, visibility,
1989 nonvis);
14b31740 1990
e5756efb 1991 if (oldsym == NULL)
55a93433 1992 {
686c8caf
ILT
1993 // Version symbols are absolute symbols with name == version.
1994 // We don't want to force them to be local.
1995 if ((version == NULL
1996 || name != version
1997 || value != 0)
1998 && (binding == elfcpp::STB_LOCAL
1999 || this->version_script_.symbol_is_local(name)))
55a93433 2000 this->force_local(sym);
75517b77
ILT
2001 else if (version != NULL
2002 && (name != version || value != 0))
2003 sym->set_is_default();
55a93433
ILT
2004 return sym;
2005 }
86f2e683 2006
caa9d5d9 2007 if (force_override || Symbol_table::should_override_with_special(oldsym))
e5756efb 2008 this->override_with_special(oldsym, sym);
8c500701
ILT
2009
2010 if (resolve_oldsym)
2011 return sym;
2012 else
2013 {
2014 delete sym;
2015 return oldsym;
2016 }
ead1e424
ILT
2017}
2018
2019// Define a set of symbols in output sections.
2020
2021void
9b07f471 2022Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
2023 const Define_symbol_in_section* p,
2024 bool only_if_ref)
ead1e424
ILT
2025{
2026 for (int i = 0; i < count; ++i, ++p)
2027 {
2028 Output_section* os = layout->find_output_section(p->output_section);
2029 if (os != NULL)
9b07f471 2030 this->define_in_output_data(p->name, NULL, os, p->value,
14b31740
ILT
2031 p->size, p->type, p->binding,
2032 p->visibility, p->nonvis,
a445fddf
ILT
2033 p->offset_is_from_end,
2034 only_if_ref || p->only_if_ref);
ead1e424 2035 else
9b07f471 2036 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 2037 p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
2038 only_if_ref || p->only_if_ref,
2039 false);
ead1e424
ILT
2040 }
2041}
2042
2043// Define a set of symbols in output segments.
2044
2045void
9b07f471 2046Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
2047 const Define_symbol_in_segment* p,
2048 bool only_if_ref)
ead1e424
ILT
2049{
2050 for (int i = 0; i < count; ++i, ++p)
2051 {
2052 Output_segment* os = layout->find_output_segment(p->segment_type,
2053 p->segment_flags_set,
2054 p->segment_flags_clear);
2055 if (os != NULL)
9b07f471 2056 this->define_in_output_segment(p->name, NULL, os, p->value,
14b31740
ILT
2057 p->size, p->type, p->binding,
2058 p->visibility, p->nonvis,
a445fddf
ILT
2059 p->offset_base,
2060 only_if_ref || p->only_if_ref);
ead1e424 2061 else
9b07f471 2062 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 2063 p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
2064 only_if_ref || p->only_if_ref,
2065 false);
ead1e424
ILT
2066 }
2067}
2068
46fe1623
ILT
2069// Define CSYM using a COPY reloc. POSD is the Output_data where the
2070// symbol should be defined--typically a .dyn.bss section. VALUE is
2071// the offset within POSD.
2072
2073template<int size>
2074void
fe8718a4 2075Symbol_table::define_with_copy_reloc(
fe8718a4
ILT
2076 Sized_symbol<size>* csym,
2077 Output_data* posd,
2078 typename elfcpp::Elf_types<size>::Elf_Addr value)
46fe1623
ILT
2079{
2080 gold_assert(csym->is_from_dynobj());
2081 gold_assert(!csym->is_copied_from_dynobj());
2082 Object* object = csym->object();
2083 gold_assert(object->is_dynamic());
2084 Dynobj* dynobj = static_cast<Dynobj*>(object);
2085
2086 // Our copied variable has to override any variable in a shared
2087 // library.
2088 elfcpp::STB binding = csym->binding();
2089 if (binding == elfcpp::STB_WEAK)
2090 binding = elfcpp::STB_GLOBAL;
2091
9b07f471 2092 this->define_in_output_data(csym->name(), csym->version(),
46fe1623
ILT
2093 posd, value, csym->symsize(),
2094 csym->type(), binding,
2095 csym->visibility(), csym->nonvis(),
2096 false, false);
2097
2098 csym->set_is_copied_from_dynobj();
2099 csym->set_needs_dynsym_entry();
2100
2101 this->copied_symbol_dynobjs_[csym] = dynobj;
2102
2103 // We have now defined all aliases, but we have not entered them all
2104 // in the copied_symbol_dynobjs_ map.
2105 if (csym->has_alias())
2106 {
2107 Symbol* sym = csym;
2108 while (true)
2109 {
2110 sym = this->weak_aliases_[sym];
2111 if (sym == csym)
2112 break;
2113 gold_assert(sym->output_data() == posd);
2114
2115 sym->set_is_copied_from_dynobj();
2116 this->copied_symbol_dynobjs_[sym] = dynobj;
2117 }
2118 }
2119}
2120
2121// SYM is defined using a COPY reloc. Return the dynamic object where
2122// the original definition was found.
2123
2124Dynobj*
2125Symbol_table::get_copy_source(const Symbol* sym) const
2126{
2127 gold_assert(sym->is_copied_from_dynobj());
2128 Copied_symbol_dynobjs::const_iterator p =
2129 this->copied_symbol_dynobjs_.find(sym);
2130 gold_assert(p != this->copied_symbol_dynobjs_.end());
2131 return p->second;
2132}
2133
f3e9c5c5
ILT
2134// Add any undefined symbols named on the command line.
2135
2136void
2137Symbol_table::add_undefined_symbols_from_command_line()
2138{
2139 if (parameters->options().any_undefined())
2140 {
2141 if (parameters->target().get_size() == 32)
2142 {
5adf9721 2143#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
f3e9c5c5
ILT
2144 this->do_add_undefined_symbols_from_command_line<32>();
2145#else
2146 gold_unreachable();
2147#endif
2148 }
2149 else if (parameters->target().get_size() == 64)
2150 {
2151#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2152 this->do_add_undefined_symbols_from_command_line<64>();
2153#else
2154 gold_unreachable();
2155#endif
2156 }
2157 else
2158 gold_unreachable();
2159 }
2160}
2161
2162template<int size>
2163void
2164Symbol_table::do_add_undefined_symbols_from_command_line()
2165{
2166 for (options::String_set::const_iterator p =
2167 parameters->options().undefined_begin();
2168 p != parameters->options().undefined_end();
2169 ++p)
2170 {
2171 const char* name = p->c_str();
2172
2173 if (this->lookup(name) != NULL)
2174 continue;
2175
2176 const char* version = NULL;
2177
2178 Sized_symbol<size>* sym;
2179 Sized_symbol<size>* oldsym;
8c500701 2180 bool resolve_oldsym;
f3e9c5c5
ILT
2181 if (parameters->target().is_big_endian())
2182 {
2183#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2184 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
2185 false, &oldsym,
2186 &resolve_oldsym);
f3e9c5c5
ILT
2187#else
2188 gold_unreachable();
2189#endif
2190 }
2191 else
2192 {
2193#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2194 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
2195 false, &oldsym,
2196 &resolve_oldsym);
f3e9c5c5
ILT
2197#else
2198 gold_unreachable();
2199#endif
2200 }
2201
2202 gold_assert(oldsym == NULL);
2203
75517b77 2204 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
f3e9c5c5
ILT
2205 elfcpp::STV_DEFAULT, 0);
2206 ++this->saw_undefined_;
2207 }
2208}
2209
a3ad94ed
ILT
2210// Set the dynamic symbol indexes. INDEX is the index of the first
2211// global dynamic symbol. Pointers to the symbols are stored into the
2212// vector SYMS. The names are added to DYNPOOL. This returns an
2213// updated dynamic symbol index.
2214
2215unsigned int
9b07f471 2216Symbol_table::set_dynsym_indexes(unsigned int index,
a3ad94ed 2217 std::vector<Symbol*>* syms,
14b31740
ILT
2218 Stringpool* dynpool,
2219 Versions* versions)
a3ad94ed
ILT
2220{
2221 for (Symbol_table_type::iterator p = this->table_.begin();
2222 p != this->table_.end();
2223 ++p)
2224 {
2225 Symbol* sym = p->second;
16649710
ILT
2226
2227 // Note that SYM may already have a dynamic symbol index, since
2228 // some symbols appear more than once in the symbol table, with
2229 // and without a version.
2230
436ca963 2231 if (!sym->should_add_dynsym_entry())
16649710
ILT
2232 sym->set_dynsym_index(-1U);
2233 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
2234 {
2235 sym->set_dynsym_index(index);
2236 ++index;
2237 syms->push_back(sym);
cfd73a4e 2238 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
2239
2240 // Record any version information.
09124467
ILT
2241 if (sym->version() != NULL)
2242 versions->record_version(this, dynpool, sym);
a3ad94ed
ILT
2243 }
2244 }
2245
14b31740
ILT
2246 // Finish up the versions. In some cases this may add new dynamic
2247 // symbols.
9b07f471 2248 index = versions->finalize(this, index, syms);
14b31740 2249
a3ad94ed
ILT
2250 return index;
2251}
2252
c06b7b0b 2253// Set the final values for all the symbols. The index of the first
55a93433
ILT
2254// global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2255// file offset OFF. Add their names to POOL. Return the new file
2256// offset. Update *PLOCAL_SYMCOUNT if necessary.
54dc6425 2257
75f65a3e 2258off_t
55a93433
ILT
2259Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2260 size_t dyncount, Stringpool* pool,
2261 unsigned int *plocal_symcount)
54dc6425 2262{
f6ce93d6
ILT
2263 off_t ret;
2264
55a93433
ILT
2265 gold_assert(*plocal_symcount != 0);
2266 this->first_global_index_ = *plocal_symcount;
c06b7b0b 2267
16649710
ILT
2268 this->dynamic_offset_ = dynoff;
2269 this->first_dynamic_global_index_ = dyn_global_index;
2270 this->dynamic_count_ = dyncount;
2271
8851ecca 2272 if (parameters->target().get_size() == 32)
9025d29d
ILT
2273 {
2274#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
55a93433 2275 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
9025d29d
ILT
2276#else
2277 gold_unreachable();
2278#endif
2279 }
8851ecca 2280 else if (parameters->target().get_size() == 64)
9025d29d
ILT
2281 {
2282#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
55a93433 2283 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
9025d29d
ILT
2284#else
2285 gold_unreachable();
2286#endif
2287 }
61ba1cf9 2288 else
a3ad94ed 2289 gold_unreachable();
f6ce93d6
ILT
2290
2291 // Now that we have the final symbol table, we can reliably note
2292 // which symbols should get warnings.
cb295612 2293 this->warnings_.note_warnings(this);
f6ce93d6
ILT
2294
2295 return ret;
75f65a3e
ILT
2296}
2297
55a93433
ILT
2298// SYM is going into the symbol table at *PINDEX. Add the name to
2299// POOL, update *PINDEX and *POFF.
2300
2301template<int size>
2302void
2303Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2304 unsigned int* pindex, off_t* poff)
2305{
2306 sym->set_symtab_index(*pindex);
2307 pool->add(sym->name(), false, NULL);
2308 ++*pindex;
2309 *poff += elfcpp::Elf_sizes<size>::sym_size;
2310}
2311
ead1e424
ILT
2312// Set the final value for all the symbols. This is called after
2313// Layout::finalize, so all the output sections have their final
2314// address.
75f65a3e
ILT
2315
2316template<int size>
2317off_t
55a93433
ILT
2318Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2319 unsigned int* plocal_symcount)
75f65a3e 2320{
ead1e424 2321 off = align_address(off, size >> 3);
75f65a3e
ILT
2322 this->offset_ = off;
2323
55a93433
ILT
2324 unsigned int index = *plocal_symcount;
2325 const unsigned int orig_index = index;
c06b7b0b 2326
55a93433
ILT
2327 // First do all the symbols which have been forced to be local, as
2328 // they must appear before all global symbols.
2329 for (Forced_locals::iterator p = this->forced_locals_.begin();
2330 p != this->forced_locals_.end();
2331 ++p)
2332 {
2333 Symbol* sym = *p;
2334 gold_assert(sym->is_forced_local());
2335 if (this->sized_finalize_symbol<size>(sym))
2336 {
2337 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2338 ++*plocal_symcount;
2339 }
2340 }
2341
2342 // Now do all the remaining symbols.
c06b7b0b
ILT
2343 for (Symbol_table_type::iterator p = this->table_.begin();
2344 p != this->table_.end();
2345 ++p)
54dc6425 2346 {
55a93433
ILT
2347 Symbol* sym = p->second;
2348 if (this->sized_finalize_symbol<size>(sym))
2349 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2350 }
54dc6425 2351
55a93433 2352 this->output_count_ = index - orig_index;
a3ad94ed 2353
55a93433
ILT
2354 return off;
2355}
75f65a3e 2356
55a93433
ILT
2357// Finalize the symbol SYM. This returns true if the symbol should be
2358// added to the symbol table, false otherwise.
008db82e 2359
55a93433
ILT
2360template<int size>
2361bool
2362Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2363{
ef9beddf
ILT
2364 typedef typename Sized_symbol<size>::Value_type Value_type;
2365
55a93433 2366 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
75f65a3e 2367
55a93433
ILT
2368 // The default version of a symbol may appear twice in the symbol
2369 // table. We only need to finalize it once.
2370 if (sym->has_symtab_index())
2371 return false;
ead1e424 2372
55a93433
ILT
2373 if (!sym->in_reg())
2374 {
2375 gold_assert(!sym->has_symtab_index());
2376 sym->set_symtab_index(-1U);
2377 gold_assert(sym->dynsym_index() == -1U);
2378 return false;
2379 }
ead1e424 2380
ef9beddf 2381 Value_type value;
ead1e424 2382
55a93433
ILT
2383 switch (sym->source())
2384 {
2385 case Symbol::FROM_OBJECT:
2386 {
d491d34e
ILT
2387 bool is_ordinary;
2388 unsigned int shndx = sym->shndx(&is_ordinary);
ead1e424 2389
d491d34e 2390 if (!is_ordinary
0dfbdef4 2391 && shndx != elfcpp::SHN_ABS
8a5e3e08 2392 && !Symbol::is_common_shndx(shndx))
55a93433
ILT
2393 {
2394 gold_error(_("%s: unsupported symbol section 0x%x"),
2395 sym->demangled_name().c_str(), shndx);
2396 shndx = elfcpp::SHN_UNDEF;
ead1e424 2397 }
ead1e424 2398
55a93433
ILT
2399 Object* symobj = sym->object();
2400 if (symobj->is_dynamic())
ead1e424 2401 {
55a93433
ILT
2402 value = 0;
2403 shndx = elfcpp::SHN_UNDEF;
ead1e424 2404 }
89fc3421
CC
2405 else if (symobj->pluginobj() != NULL)
2406 {
2407 value = 0;
2408 shndx = elfcpp::SHN_UNDEF;
2409 }
55a93433
ILT
2410 else if (shndx == elfcpp::SHN_UNDEF)
2411 value = 0;
d491d34e 2412 else if (!is_ordinary
8a5e3e08
ILT
2413 && (shndx == elfcpp::SHN_ABS
2414 || Symbol::is_common_shndx(shndx)))
55a93433
ILT
2415 value = sym->value();
2416 else
ead1e424 2417 {
55a93433 2418 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 2419 Output_section* os = relobj->output_section(shndx);
55a93433
ILT
2420
2421 if (os == NULL)
ead1e424 2422 {
55a93433 2423 sym->set_symtab_index(-1U);
6d03d481
ST
2424 bool static_or_reloc = (parameters->doing_static_link() ||
2425 parameters->options().relocatable());
2426 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2427
55a93433 2428 return false;
ead1e424 2429 }
55a93433 2430
ef9beddf 2431 uint64_t secoff64 = relobj->output_section_offset(shndx);
eff45813
CC
2432 if (secoff64 == -1ULL)
2433 {
2434 // The section needs special handling (e.g., a merge section).
2435 value = os->output_address(relobj, shndx, sym->value());
2436 }
2437 else
2438 {
2439 Value_type secoff =
2440 convert_types<Value_type, uint64_t>(secoff64);
2441 if (sym->type() == elfcpp::STT_TLS)
2442 value = sym->value() + os->tls_offset() + secoff;
2443 else
2444 value = sym->value() + os->address() + secoff;
2445 }
ead1e424 2446 }
55a93433
ILT
2447 }
2448 break;
2449
2450 case Symbol::IN_OUTPUT_DATA:
2451 {
2452 Output_data* od = sym->output_data();
155a0dd7
ILT
2453 value = sym->value();
2454 if (sym->type() != elfcpp::STT_TLS)
2455 value += od->address();
2456 else
2457 {
2458 Output_section* os = od->output_section();
2459 gold_assert(os != NULL);
2460 value += os->tls_offset() + (od->address() - os->address());
2461 }
55a93433
ILT
2462 if (sym->offset_is_from_end())
2463 value += od->data_size();
2464 }
2465 break;
2466
2467 case Symbol::IN_OUTPUT_SEGMENT:
2468 {
2469 Output_segment* os = sym->output_segment();
edfbb029
CC
2470 value = sym->value();
2471 if (sym->type() != elfcpp::STT_TLS)
2472 value += os->vaddr();
55a93433
ILT
2473 switch (sym->offset_base())
2474 {
2475 case Symbol::SEGMENT_START:
2476 break;
2477 case Symbol::SEGMENT_END:
2478 value += os->memsz();
2479 break;
2480 case Symbol::SEGMENT_BSS:
2481 value += os->filesz();
2482 break;
2483 default:
2484 gold_unreachable();
2485 }
2486 }
2487 break;
ead1e424 2488
f3e9c5c5 2489 case Symbol::IS_CONSTANT:
55a93433
ILT
2490 value = sym->value();
2491 break;
ead1e424 2492
f3e9c5c5
ILT
2493 case Symbol::IS_UNDEFINED:
2494 value = 0;
2495 break;
2496
55a93433
ILT
2497 default:
2498 gold_unreachable();
2499 }
ead1e424 2500
55a93433 2501 sym->set_value(value);
9e2dcb77 2502
8851ecca 2503 if (parameters->options().strip_all())
55a93433
ILT
2504 {
2505 sym->set_symtab_index(-1U);
2506 return false;
54dc6425 2507 }
75f65a3e 2508
55a93433 2509 return true;
54dc6425
ILT
2510}
2511
61ba1cf9
ILT
2512// Write out the global symbols.
2513
2514void
fd9d194f 2515Symbol_table::write_globals(const Stringpool* sympool,
d491d34e
ILT
2516 const Stringpool* dynpool,
2517 Output_symtab_xindex* symtab_xindex,
2518 Output_symtab_xindex* dynsym_xindex,
2519 Output_file* of) const
61ba1cf9 2520{
8851ecca 2521 switch (parameters->size_and_endianness())
61ba1cf9 2522 {
9025d29d 2523#ifdef HAVE_TARGET_32_LITTLE
8851ecca 2524 case Parameters::TARGET_32_LITTLE:
fd9d194f 2525 this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
d491d34e 2526 dynsym_xindex, of);
8851ecca 2527 break;
9025d29d 2528#endif
8851ecca
ILT
2529#ifdef HAVE_TARGET_32_BIG
2530 case Parameters::TARGET_32_BIG:
fd9d194f 2531 this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
d491d34e 2532 dynsym_xindex, of);
8851ecca 2533 break;
9025d29d 2534#endif
9025d29d 2535#ifdef HAVE_TARGET_64_LITTLE
8851ecca 2536 case Parameters::TARGET_64_LITTLE:
fd9d194f 2537 this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
d491d34e 2538 dynsym_xindex, of);
8851ecca 2539 break;
9025d29d 2540#endif
8851ecca
ILT
2541#ifdef HAVE_TARGET_64_BIG
2542 case Parameters::TARGET_64_BIG:
fd9d194f 2543 this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
d491d34e 2544 dynsym_xindex, of);
8851ecca
ILT
2545 break;
2546#endif
2547 default:
2548 gold_unreachable();
61ba1cf9 2549 }
61ba1cf9
ILT
2550}
2551
2552// Write out the global symbols.
2553
2554template<int size, bool big_endian>
2555void
fd9d194f 2556Symbol_table::sized_write_globals(const Stringpool* sympool,
16649710 2557 const Stringpool* dynpool,
d491d34e
ILT
2558 Output_symtab_xindex* symtab_xindex,
2559 Output_symtab_xindex* dynsym_xindex,
61ba1cf9
ILT
2560 Output_file* of) const
2561{
8851ecca 2562 const Target& target = parameters->target();
9a2d6984 2563
61ba1cf9 2564 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
55a93433
ILT
2565
2566 const unsigned int output_count = this->output_count_;
2567 const section_size_type oview_size = output_count * sym_size;
2568 const unsigned int first_global_index = this->first_global_index_;
5fe2a0f5
ILT
2569 unsigned char* psyms;
2570 if (this->offset_ == 0 || output_count == 0)
2571 psyms = NULL;
2572 else
2573 psyms = of->get_output_view(this->offset_, oview_size);
16649710 2574
55a93433
ILT
2575 const unsigned int dynamic_count = this->dynamic_count_;
2576 const section_size_type dynamic_size = dynamic_count * sym_size;
2577 const unsigned int first_dynamic_global_index =
2578 this->first_dynamic_global_index_;
16649710 2579 unsigned char* dynamic_view;
5fe2a0f5 2580 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
16649710
ILT
2581 dynamic_view = NULL;
2582 else
2583 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 2584
61ba1cf9
ILT
2585 for (Symbol_table_type::const_iterator p = this->table_.begin();
2586 p != this->table_.end();
2587 ++p)
2588 {
2589 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2590
9a2d6984 2591 // Possibly warn about unresolved symbols in shared libraries.
fd9d194f 2592 this->warn_about_undefined_dynobj_symbol(sym);
e2827e5f 2593
a3ad94ed 2594 unsigned int sym_index = sym->symtab_index();
16649710
ILT
2595 unsigned int dynsym_index;
2596 if (dynamic_view == NULL)
2597 dynsym_index = -1U;
2598 else
2599 dynsym_index = sym->dynsym_index();
2600
2601 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
2602 {
2603 // This symbol is not included in the output file.
2604 continue;
2605 }
16649710 2606
ead1e424 2607 unsigned int shndx;
88dd47ac
ILT
2608 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2609 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
ead1e424
ILT
2610 switch (sym->source())
2611 {
2612 case Symbol::FROM_OBJECT:
2613 {
d491d34e
ILT
2614 bool is_ordinary;
2615 unsigned int in_shndx = sym->shndx(&is_ordinary);
ead1e424 2616
d491d34e 2617 if (!is_ordinary
0dfbdef4 2618 && in_shndx != elfcpp::SHN_ABS
8a5e3e08 2619 && !Symbol::is_common_shndx(in_shndx))
ead1e424 2620 {
75f2446e 2621 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 2622 sym->demangled_name().c_str(), in_shndx);
75f2446e 2623 shndx = in_shndx;
f6ce93d6 2624 }
ead1e424
ILT
2625 else
2626 {
75f2446e
ILT
2627 Object* symobj = sym->object();
2628 if (symobj->is_dynamic())
2629 {
2630 if (sym->needs_dynsym_value())
8851ecca 2631 dynsym_value = target.dynsym_value(sym);
75f2446e
ILT
2632 shndx = elfcpp::SHN_UNDEF;
2633 }
89fc3421
CC
2634 else if (symobj->pluginobj() != NULL)
2635 shndx = elfcpp::SHN_UNDEF;
75f2446e 2636 else if (in_shndx == elfcpp::SHN_UNDEF
d491d34e
ILT
2637 || (!is_ordinary
2638 && (in_shndx == elfcpp::SHN_ABS
8a5e3e08 2639 || Symbol::is_common_shndx(in_shndx))))
75f2446e
ILT
2640 shndx = in_shndx;
2641 else
2642 {
2643 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 2644 Output_section* os = relobj->output_section(in_shndx);
75f2446e
ILT
2645 gold_assert(os != NULL);
2646 shndx = os->out_shndx();
88dd47ac 2647
d491d34e
ILT
2648 if (shndx >= elfcpp::SHN_LORESERVE)
2649 {
2650 if (sym_index != -1U)
2651 symtab_xindex->add(sym_index, shndx);
2652 if (dynsym_index != -1U)
2653 dynsym_xindex->add(dynsym_index, shndx);
2654 shndx = elfcpp::SHN_XINDEX;
2655 }
2656
88dd47ac
ILT
2657 // In object files symbol values are section
2658 // relative.
8851ecca 2659 if (parameters->options().relocatable())
88dd47ac 2660 sym_value -= os->address();
75f2446e 2661 }
ead1e424
ILT
2662 }
2663 }
2664 break;
2665
2666 case Symbol::IN_OUTPUT_DATA:
2667 shndx = sym->output_data()->out_shndx();
d491d34e
ILT
2668 if (shndx >= elfcpp::SHN_LORESERVE)
2669 {
2670 if (sym_index != -1U)
2671 symtab_xindex->add(sym_index, shndx);
2672 if (dynsym_index != -1U)
2673 dynsym_xindex->add(dynsym_index, shndx);
2674 shndx = elfcpp::SHN_XINDEX;
2675 }
ead1e424
ILT
2676 break;
2677
2678 case Symbol::IN_OUTPUT_SEGMENT:
2679 shndx = elfcpp::SHN_ABS;
2680 break;
2681
f3e9c5c5 2682 case Symbol::IS_CONSTANT:
ead1e424
ILT
2683 shndx = elfcpp::SHN_ABS;
2684 break;
2685
f3e9c5c5
ILT
2686 case Symbol::IS_UNDEFINED:
2687 shndx = elfcpp::SHN_UNDEF;
2688 break;
2689
ead1e424 2690 default:
a3ad94ed 2691 gold_unreachable();
ead1e424 2692 }
61ba1cf9 2693
16649710
ILT
2694 if (sym_index != -1U)
2695 {
55a93433
ILT
2696 sym_index -= first_global_index;
2697 gold_assert(sym_index < output_count);
2698 unsigned char* ps = psyms + (sym_index * sym_size);
7d1a9ebb
ILT
2699 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2700 sympool, ps);
16649710 2701 }
61ba1cf9 2702
16649710
ILT
2703 if (dynsym_index != -1U)
2704 {
2705 dynsym_index -= first_dynamic_global_index;
2706 gold_assert(dynsym_index < dynamic_count);
2707 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
7d1a9ebb
ILT
2708 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2709 dynpool, pd);
16649710 2710 }
61ba1cf9
ILT
2711 }
2712
c06b7b0b 2713 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
2714 if (dynamic_view != NULL)
2715 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2716}
2717
2718// Write out the symbol SYM, in section SHNDX, to P. POOL is the
2719// strtab holding the name.
2720
2721template<int size, bool big_endian>
2722void
ab5c9e90
ILT
2723Symbol_table::sized_write_symbol(
2724 Sized_symbol<size>* sym,
2725 typename elfcpp::Elf_types<size>::Elf_Addr value,
2726 unsigned int shndx,
2727 const Stringpool* pool,
7d1a9ebb 2728 unsigned char* p) const
16649710
ILT
2729{
2730 elfcpp::Sym_write<size, big_endian> osym(p);
2731 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 2732 osym.put_st_value(value);
58e54ac2
CD
2733 // Use a symbol size of zero for undefined symbols from shared libraries.
2734 if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
2735 osym.put_st_size(0);
2736 else
2737 osym.put_st_size(sym->symsize());
55a93433
ILT
2738 // A version script may have overridden the default binding.
2739 if (sym->is_forced_local())
2740 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
2741 else
2742 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
16649710
ILT
2743 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2744 osym.put_st_shndx(shndx);
61ba1cf9
ILT
2745}
2746
9a2d6984
ILT
2747// Check for unresolved symbols in shared libraries. This is
2748// controlled by the --allow-shlib-undefined option.
2749
2750// We only warn about libraries for which we have seen all the
2751// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2752// which were not seen in this link. If we didn't see a DT_NEEDED
2753// entry, we aren't going to be able to reliably report whether the
2754// symbol is undefined.
2755
fd9d194f
ILT
2756// We also don't warn about libraries found in a system library
2757// directory (e.g., /lib or /usr/lib); we assume that those libraries
2758// are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
2759// can have undefined references satisfied by ld-linux.so.
9a2d6984
ILT
2760
2761inline void
fd9d194f 2762Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
9a2d6984 2763{
d491d34e 2764 bool dummy;
9a2d6984
ILT
2765 if (sym->source() == Symbol::FROM_OBJECT
2766 && sym->object()->is_dynamic()
d491d34e 2767 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
9a2d6984 2768 && sym->binding() != elfcpp::STB_WEAK
8851ecca
ILT
2769 && !parameters->options().allow_shlib_undefined()
2770 && !parameters->target().is_defined_by_abi(sym)
fd9d194f 2771 && !sym->object()->is_in_system_directory())
9a2d6984
ILT
2772 {
2773 // A very ugly cast.
2774 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2775 if (!dynobj->has_unknown_needed_entries())
f073bbf7 2776 gold_undefined_symbol(sym);
9a2d6984
ILT
2777 }
2778}
2779
a3ad94ed
ILT
2780// Write out a section symbol. Return the update offset.
2781
2782void
9025d29d 2783Symbol_table::write_section_symbol(const Output_section *os,
d491d34e 2784 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
2785 Output_file* of,
2786 off_t offset) const
2787{
8851ecca 2788 switch (parameters->size_and_endianness())
a3ad94ed 2789 {
9025d29d 2790#ifdef HAVE_TARGET_32_LITTLE
8851ecca 2791 case Parameters::TARGET_32_LITTLE:
d491d34e
ILT
2792 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2793 offset);
8851ecca 2794 break;
9025d29d 2795#endif
8851ecca
ILT
2796#ifdef HAVE_TARGET_32_BIG
2797 case Parameters::TARGET_32_BIG:
d491d34e
ILT
2798 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2799 offset);
8851ecca 2800 break;
9025d29d 2801#endif
9025d29d 2802#ifdef HAVE_TARGET_64_LITTLE
8851ecca 2803 case Parameters::TARGET_64_LITTLE:
d491d34e
ILT
2804 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2805 offset);
8851ecca 2806 break;
9025d29d 2807#endif
8851ecca
ILT
2808#ifdef HAVE_TARGET_64_BIG
2809 case Parameters::TARGET_64_BIG:
d491d34e
ILT
2810 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2811 offset);
8851ecca
ILT
2812 break;
2813#endif
2814 default:
2815 gold_unreachable();
a3ad94ed 2816 }
a3ad94ed
ILT
2817}
2818
2819// Write out a section symbol, specialized for size and endianness.
2820
2821template<int size, bool big_endian>
2822void
2823Symbol_table::sized_write_section_symbol(const Output_section* os,
d491d34e 2824 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
2825 Output_file* of,
2826 off_t offset) const
2827{
2828 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2829
2830 unsigned char* pov = of->get_output_view(offset, sym_size);
2831
2832 elfcpp::Sym_write<size, big_endian> osym(pov);
2833 osym.put_st_name(0);
b4ecf66b
ILT
2834 if (parameters->options().relocatable())
2835 osym.put_st_value(0);
2836 else
2837 osym.put_st_value(os->address());
a3ad94ed
ILT
2838 osym.put_st_size(0);
2839 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2840 elfcpp::STT_SECTION));
2841 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
d491d34e
ILT
2842
2843 unsigned int shndx = os->out_shndx();
2844 if (shndx >= elfcpp::SHN_LORESERVE)
2845 {
2846 symtab_xindex->add(os->symtab_index(), shndx);
2847 shndx = elfcpp::SHN_XINDEX;
2848 }
2849 osym.put_st_shndx(shndx);
a3ad94ed
ILT
2850
2851 of->write_output_view(offset, sym_size, pov);
2852}
2853
abaa3995
ILT
2854// Print statistical information to stderr. This is used for --stats.
2855
2856void
2857Symbol_table::print_stats() const
2858{
2859#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2860 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2861 program_name, this->table_.size(), this->table_.bucket_count());
2862#else
2863 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2864 program_name, this->table_.size());
2865#endif
ad8f37d1 2866 this->namepool_.print_stats("symbol table stringpool");
abaa3995
ILT
2867}
2868
ff541f30
ILT
2869// We check for ODR violations by looking for symbols with the same
2870// name for which the debugging information reports that they were
2871// defined in different source locations. When comparing the source
2872// location, we consider instances with the same base filename and
2873// line number to be the same. This is because different object
2874// files/shared libraries can include the same header file using
2875// different paths, and we don't want to report an ODR violation in
2876// that case.
2877
2878// This struct is used to compare line information, as returned by
7bf1f802 2879// Dwarf_line_info::one_addr2line. It implements a < comparison
ff541f30
ILT
2880// operator used with std::set.
2881
2882struct Odr_violation_compare
2883{
2884 bool
2885 operator()(const std::string& s1, const std::string& s2) const
2886 {
2887 std::string::size_type pos1 = s1.rfind('/');
2888 std::string::size_type pos2 = s2.rfind('/');
2889 if (pos1 == std::string::npos
2890 || pos2 == std::string::npos)
2891 return s1 < s2;
2892 return s1.compare(pos1, std::string::npos,
2893 s2, pos2, std::string::npos) < 0;
2894 }
2895};
2896
70e654ba
ILT
2897// Check candidate_odr_violations_ to find symbols with the same name
2898// but apparently different definitions (different source-file/line-no).
2899
2900void
17a1d0a9
ILT
2901Symbol_table::detect_odr_violations(const Task* task,
2902 const char* output_file_name) const
70e654ba
ILT
2903{
2904 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2905 it != candidate_odr_violations_.end();
2906 ++it)
2907 {
2908 const char* symbol_name = it->first;
2909 // We use a sorted set so the output is deterministic.
ff541f30 2910 std::set<std::string, Odr_violation_compare> line_nums;
70e654ba 2911
b01c0a4a
ILT
2912 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2913 locs = it->second.begin();
2914 locs != it->second.end();
2915 ++locs)
70e654ba
ILT
2916 {
2917 // We need to lock the object in order to read it. This
17a1d0a9
ILT
2918 // means that we have to run in a singleton Task. If we
2919 // want to run this in a general Task for better
2920 // performance, we will need one Task for object, plus
2921 // appropriate locking to ensure that we don't conflict with
e4e5049b
CS
2922 // other uses of the object. Also note, one_addr2line is not
2923 // currently thread-safe.
17a1d0a9 2924 Task_lock_obj<Object> tl(task, locs->object);
e4e5049b 2925 // 16 is the size of the object-cache that one_addr2line should use.
a55ce7fe 2926 std::string lineno = Dwarf_line_info::one_addr2line(
e4e5049b 2927 locs->object, locs->shndx, locs->offset, 16);
70e654ba
ILT
2928 if (!lineno.empty())
2929 line_nums.insert(lineno);
2930 }
2931
2932 if (line_nums.size() > 1)
2933 {
dd8670e5 2934 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
78f15696 2935 "places (possible ODR violation):"),
a2b1aa12 2936 output_file_name, demangle(symbol_name).c_str());
70e654ba
ILT
2937 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2938 it2 != line_nums.end();
2939 ++it2)
2940 fprintf(stderr, " %s\n", it2->c_str());
2941 }
2942 }
e4e5049b
CS
2943 // We only call one_addr2line() in this function, so we can clear its cache.
2944 Dwarf_line_info::clear_addr2line_cache();
70e654ba
ILT
2945}
2946
f6ce93d6
ILT
2947// Warnings functions.
2948
2949// Add a new warning.
2950
2951void
2952Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
cb295612 2953 const std::string& warning)
f6ce93d6
ILT
2954{
2955 name = symtab->canonicalize_name(name);
cb295612 2956 this->warnings_[name].set(obj, warning);
f6ce93d6
ILT
2957}
2958
2959// Look through the warnings and mark the symbols for which we should
2960// warn. This is called during Layout::finalize when we know the
2961// sources for all the symbols.
2962
2963void
cb295612 2964Warnings::note_warnings(Symbol_table* symtab)
f6ce93d6
ILT
2965{
2966 for (Warning_table::iterator p = this->warnings_.begin();
2967 p != this->warnings_.end();
2968 ++p)
2969 {
2970 Symbol* sym = symtab->lookup(p->first, NULL);
2971 if (sym != NULL
2972 && sym->source() == Symbol::FROM_OBJECT
2973 && sym->object() == p->second.object)
cb295612 2974 sym->set_has_warning();
f6ce93d6
ILT
2975 }
2976}
2977
2978// Issue a warning. This is called when we see a relocation against a
2979// symbol for which has a warning.
2980
75f2446e 2981template<int size, bool big_endian>
f6ce93d6 2982void
75f2446e
ILT
2983Warnings::issue_warning(const Symbol* sym,
2984 const Relocate_info<size, big_endian>* relinfo,
2985 size_t relnum, off_t reloffset) const
f6ce93d6 2986{
a3ad94ed 2987 gold_assert(sym->has_warning());
f6ce93d6 2988 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 2989 gold_assert(p != this->warnings_.end());
75f2446e
ILT
2990 gold_warning_at_location(relinfo, relnum, reloffset,
2991 "%s", p->second.text.c_str());
f6ce93d6
ILT
2992}
2993
14bfc3f5
ILT
2994// Instantiate the templates we need. We could use the configure
2995// script to restrict this to only the ones needed for implemented
2996// targets.
2997
c7912668
ILT
2998#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2999template
3000void
3001Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3002#endif
3003
3004#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3005template
3006void
3007Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3008#endif
3009
193a53d9 3010#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
3011template
3012void
193a53d9
ILT
3013Symbol_table::add_from_relobj<32, false>(
3014 Sized_relobj<32, false>* relobj,
f6ce93d6 3015 const unsigned char* syms,
14bfc3f5 3016 size_t count,
d491d34e 3017 size_t symndx_offset,
14bfc3f5
ILT
3018 const char* sym_names,
3019 size_t sym_name_size,
ae6dce4d 3020 Sized_relobj<32, false>::Symbols* sympointers,
92de84a6 3021 size_t* defined);
193a53d9 3022#endif
14bfc3f5 3023
193a53d9 3024#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
3025template
3026void
193a53d9
ILT
3027Symbol_table::add_from_relobj<32, true>(
3028 Sized_relobj<32, true>* relobj,
f6ce93d6 3029 const unsigned char* syms,
14bfc3f5 3030 size_t count,
d491d34e 3031 size_t symndx_offset,
14bfc3f5
ILT
3032 const char* sym_names,
3033 size_t sym_name_size,
ae6dce4d 3034 Sized_relobj<32, true>::Symbols* sympointers,
92de84a6 3035 size_t* defined);
193a53d9 3036#endif
14bfc3f5 3037
193a53d9 3038#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
3039template
3040void
193a53d9
ILT
3041Symbol_table::add_from_relobj<64, false>(
3042 Sized_relobj<64, false>* relobj,
f6ce93d6 3043 const unsigned char* syms,
14bfc3f5 3044 size_t count,
d491d34e 3045 size_t symndx_offset,
14bfc3f5
ILT
3046 const char* sym_names,
3047 size_t sym_name_size,
ae6dce4d 3048 Sized_relobj<64, false>::Symbols* sympointers,
92de84a6 3049 size_t* defined);
193a53d9 3050#endif
14bfc3f5 3051
193a53d9 3052#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
3053template
3054void
193a53d9
ILT
3055Symbol_table::add_from_relobj<64, true>(
3056 Sized_relobj<64, true>* relobj,
f6ce93d6 3057 const unsigned char* syms,
14bfc3f5 3058 size_t count,
d491d34e 3059 size_t symndx_offset,
14bfc3f5
ILT
3060 const char* sym_names,
3061 size_t sym_name_size,
ae6dce4d 3062 Sized_relobj<64, true>::Symbols* sympointers,
92de84a6 3063 size_t* defined);
193a53d9 3064#endif
14bfc3f5 3065
89fc3421
CC
3066#ifdef HAVE_TARGET_32_LITTLE
3067template
3068Symbol*
3069Symbol_table::add_from_pluginobj<32, false>(
3070 Sized_pluginobj<32, false>* obj,
3071 const char* name,
3072 const char* ver,
3073 elfcpp::Sym<32, false>* sym);
3074#endif
3075
3076#ifdef HAVE_TARGET_32_BIG
3077template
3078Symbol*
3079Symbol_table::add_from_pluginobj<32, true>(
3080 Sized_pluginobj<32, true>* obj,
3081 const char* name,
3082 const char* ver,
3083 elfcpp::Sym<32, true>* sym);
3084#endif
3085
3086#ifdef HAVE_TARGET_64_LITTLE
3087template
3088Symbol*
3089Symbol_table::add_from_pluginobj<64, false>(
3090 Sized_pluginobj<64, false>* obj,
3091 const char* name,
3092 const char* ver,
3093 elfcpp::Sym<64, false>* sym);
3094#endif
3095
3096#ifdef HAVE_TARGET_64_BIG
3097template
3098Symbol*
3099Symbol_table::add_from_pluginobj<64, true>(
3100 Sized_pluginobj<64, true>* obj,
3101 const char* name,
3102 const char* ver,
3103 elfcpp::Sym<64, true>* sym);
3104#endif
3105
193a53d9 3106#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
3107template
3108void
193a53d9
ILT
3109Symbol_table::add_from_dynobj<32, false>(
3110 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
3111 const unsigned char* syms,
3112 size_t count,
3113 const char* sym_names,
3114 size_t sym_name_size,
3115 const unsigned char* versym,
3116 size_t versym_size,
92de84a6
ILT
3117 const std::vector<const char*>* version_map,
3118 Sized_relobj<32, false>::Symbols* sympointers,
3119 size_t* defined);
193a53d9 3120#endif
dbe717ef 3121
193a53d9 3122#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
3123template
3124void
193a53d9
ILT
3125Symbol_table::add_from_dynobj<32, true>(
3126 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
3127 const unsigned char* syms,
3128 size_t count,
3129 const char* sym_names,
3130 size_t sym_name_size,
3131 const unsigned char* versym,
3132 size_t versym_size,
92de84a6
ILT
3133 const std::vector<const char*>* version_map,
3134 Sized_relobj<32, true>::Symbols* sympointers,
3135 size_t* defined);
193a53d9 3136#endif
dbe717ef 3137
193a53d9 3138#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
3139template
3140void
193a53d9
ILT
3141Symbol_table::add_from_dynobj<64, false>(
3142 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
3143 const unsigned char* syms,
3144 size_t count,
3145 const char* sym_names,
3146 size_t sym_name_size,
3147 const unsigned char* versym,
3148 size_t versym_size,
92de84a6
ILT
3149 const std::vector<const char*>* version_map,
3150 Sized_relobj<64, false>::Symbols* sympointers,
3151 size_t* defined);
193a53d9 3152#endif
dbe717ef 3153
193a53d9 3154#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
3155template
3156void
193a53d9
ILT
3157Symbol_table::add_from_dynobj<64, true>(
3158 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
3159 const unsigned char* syms,
3160 size_t count,
3161 const char* sym_names,
3162 size_t sym_name_size,
3163 const unsigned char* versym,
3164 size_t versym_size,
92de84a6
ILT
3165 const std::vector<const char*>* version_map,
3166 Sized_relobj<64, true>::Symbols* sympointers,
3167 size_t* defined);
193a53d9 3168#endif
dbe717ef 3169
46fe1623
ILT
3170#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3171template
3172void
fe8718a4 3173Symbol_table::define_with_copy_reloc<32>(
fe8718a4
ILT
3174 Sized_symbol<32>* sym,
3175 Output_data* posd,
3176 elfcpp::Elf_types<32>::Elf_Addr value);
46fe1623
ILT
3177#endif
3178
3179#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3180template
3181void
fe8718a4 3182Symbol_table::define_with_copy_reloc<64>(
fe8718a4
ILT
3183 Sized_symbol<64>* sym,
3184 Output_data* posd,
3185 elfcpp::Elf_types<64>::Elf_Addr value);
46fe1623
ILT
3186#endif
3187
75f2446e
ILT
3188#ifdef HAVE_TARGET_32_LITTLE
3189template
3190void
3191Warnings::issue_warning<32, false>(const Symbol* sym,
3192 const Relocate_info<32, false>* relinfo,
3193 size_t relnum, off_t reloffset) const;
3194#endif
3195
3196#ifdef HAVE_TARGET_32_BIG
3197template
3198void
3199Warnings::issue_warning<32, true>(const Symbol* sym,
3200 const Relocate_info<32, true>* relinfo,
3201 size_t relnum, off_t reloffset) const;
3202#endif
3203
3204#ifdef HAVE_TARGET_64_LITTLE
3205template
3206void
3207Warnings::issue_warning<64, false>(const Symbol* sym,
3208 const Relocate_info<64, false>* relinfo,
3209 size_t relnum, off_t reloffset) const;
3210#endif
3211
3212#ifdef HAVE_TARGET_64_BIG
3213template
3214void
3215Warnings::issue_warning<64, true>(const Symbol* sym,
3216 const Relocate_info<64, true>* relinfo,
3217 size_t relnum, off_t reloffset) const;
3218#endif
3219
14bfc3f5 3220} // End namespace gold.