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