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