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