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