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