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