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