]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
infrun.c (set_step_over_info): Add comment.
[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()
e3f07b5b 885 && pdef->second->is_defined()
b60ecbc6
CC
886 && pdef->second->object() != sym->object())
887 ;
8c500701
ILT
888 else
889 {
890 const Sized_symbol<size>* symdef;
891 symdef = this->get_sized_symbol<size>(pdef->second);
892 Symbol_table::resolve<size, big_endian>(sym, symdef);
893 this->make_forwarder(pdef->second, sym);
894 pdef->second = sym;
895 sym->set_is_default();
896 }
897 }
898}
899
14bfc3f5
ILT
900// Add one symbol from OBJECT to the symbol table. NAME is symbol
901// name and VERSION is the version; both are canonicalized. DEF is
d491d34e
ILT
902// whether this is the default version. ST_SHNDX is the symbol's
903// section index; IS_ORDINARY is whether this is a normal section
904// rather than a special code.
14bfc3f5 905
8781f709
ILT
906// If IS_DEFAULT_VERSION is true, then this is the definition of a
907// default version of a symbol. That means that any lookup of
908// NAME/NULL and any lookup of NAME/VERSION should always return the
909// same symbol. This is obvious for references, but in particular we
910// want to do this for definitions: overriding NAME/NULL should also
911// override NAME/VERSION. If we don't do that, it would be very hard
912// to override functions in a shared library which uses versioning.
14bfc3f5
ILT
913
914// We implement this by simply making both entries in the hash table
915// point to the same Symbol structure. That is easy enough if this is
916// the first time we see NAME/NULL or NAME/VERSION, but it is possible
917// that we have seen both already, in which case they will both have
918// independent entries in the symbol table. We can't simply change
919// the symbol table entry, because we have pointers to the entries
920// attached to the object files. So we mark the entry attached to the
921// object file as a forwarder, and record it in the forwarders_ map.
922// Note that entries in the hash table will never be marked as
923// forwarders.
70e654ba 924//
d491d34e
ILT
925// ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
926// ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
927// for a special section code. ST_SHNDX may be modified if the symbol
928// is defined in a section being discarded.
14bfc3f5
ILT
929
930template<int size, bool big_endian>
aeddab66 931Sized_symbol<size>*
2ea97941 932Symbol_table::add_from_object(Object* object,
ca09d69a 933 const char* name,
f0641a0b 934 Stringpool::Key name_key,
ca09d69a 935 const char* version,
f0641a0b 936 Stringpool::Key version_key,
8781f709 937 bool is_default_version,
70e654ba 938 const elfcpp::Sym<size, big_endian>& sym,
d491d34e
ILT
939 unsigned int st_shndx,
940 bool is_ordinary,
941 unsigned int orig_st_shndx)
14bfc3f5 942{
c5818ff1 943 // Print a message if this symbol is being traced.
2ea97941 944 if (parameters->options().is_trace_symbol(name))
c5818ff1 945 {
d491d34e 946 if (orig_st_shndx == elfcpp::SHN_UNDEF)
2ea97941 947 gold_info(_("%s: reference to %s"), object->name().c_str(), name);
c5818ff1 948 else
2ea97941 949 gold_info(_("%s: definition of %s"), object->name().c_str(), name);
c5818ff1
CC
950 }
951
0864d551
ILT
952 // For an undefined symbol, we may need to adjust the name using
953 // --wrap.
d491d34e 954 if (orig_st_shndx == elfcpp::SHN_UNDEF
c5818ff1 955 && parameters->options().any_wrap())
0864d551 956 {
2ea97941
ILT
957 const char* wrap_name = this->wrap_symbol(name, &name_key);
958 if (wrap_name != name)
0864d551
ILT
959 {
960 // If we see a reference to malloc with version GLIBC_2.0,
961 // and we turn it into a reference to __wrap_malloc, then we
962 // discard the version number. Otherwise the user would be
963 // required to specify the correct version for
964 // __wrap_malloc.
2ea97941 965 version = NULL;
0864d551 966 version_key = 0;
2ea97941 967 name = wrap_name;
0864d551
ILT
968 }
969 }
970
14bfc3f5
ILT
971 Symbol* const snull = NULL;
972 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
973 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
974 snull));
14bfc3f5 975
8781f709 976 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
14bfc3f5 977 std::make_pair(this->table_.end(), false);
8781f709 978 if (is_default_version)
14bfc3f5 979 {
f0641a0b 980 const Stringpool::Key vnull_key = 0;
8781f709
ILT
981 insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
982 vnull_key),
983 snull));
14bfc3f5
ILT
984 }
985
986 // ins.first: an iterator, which is a pointer to a pair.
987 // ins.first->first: the key (a pair of name and version).
988 // ins.first->second: the value (Symbol*).
989 // ins.second: true if new entry was inserted, false if not.
990
1564db8d 991 Sized_symbol<size>* ret;
ead1e424
ILT
992 bool was_undefined;
993 bool was_common;
14bfc3f5
ILT
994 if (!ins.second)
995 {
996 // We already have an entry for NAME/VERSION.
7d1a9ebb 997 ret = this->get_sized_symbol<size>(ins.first->second);
a3ad94ed 998 gold_assert(ret != NULL);
ead1e424
ILT
999
1000 was_undefined = ret->is_undefined();
1707f183
CC
1001 // Commons from plugins are just placeholders.
1002 was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
ead1e424 1003
2ea97941 1004 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
b45e00b3 1005 version, is_default_version);
6d03d481
ST
1006 if (parameters->options().gc_sections())
1007 this->gc_mark_dyn_syms(ret);
14bfc3f5 1008
8781f709
ILT
1009 if (is_default_version)
1010 this->define_default_version<size, big_endian>(ret, insdefault.second,
1011 insdefault.first);
3ac0a36c 1012 else
b45e00b3 1013 {
b45e00b3 1014 bool dummy;
3ac0a36c
CC
1015 if (version != NULL
1016 && ret->source() == Symbol::FROM_OBJECT
b45e00b3
CC
1017 && ret->object() == object
1018 && is_ordinary
3ac0a36c
CC
1019 && ret->shndx(&dummy) == st_shndx
1020 && ret->is_default())
b45e00b3 1021 {
3ac0a36c
CC
1022 // We have seen NAME/VERSION already, and marked it as the
1023 // default version, but now we see a definition for
1024 // NAME/VERSION that is not the default version. This can
1025 // happen when the assembler generates two symbols for
1026 // a symbol as a result of a ".symver foo,foo@VER"
1027 // directive. We see the first unversioned symbol and
1028 // we may mark it as the default version (from a
1029 // version script); then we see the second versioned
1030 // symbol and we need to override the first.
1031 // In any other case, the two symbols should have generated
1032 // a multiple definition error.
1033 // (See PR gold/18703.)
b45e00b3
CC
1034 ret->set_is_not_default();
1035 const Stringpool::Key vnull_key = 0;
1036 this->table_.erase(std::make_pair(name_key, vnull_key));
1037 }
1038 }
14bfc3f5
ILT
1039 }
1040 else
1041 {
1042 // This is the first time we have seen NAME/VERSION.
a3ad94ed 1043 gold_assert(ins.first->second == NULL);
ead1e424 1044
8781f709 1045 if (is_default_version && !insdefault.second)
14bfc3f5 1046 {
14b31740
ILT
1047 // We already have an entry for NAME/NULL. If we override
1048 // it, then change it to NAME/VERSION.
8781f709 1049 ret = this->get_sized_symbol<size>(insdefault.first->second);
18e6b24e
ILT
1050
1051 was_undefined = ret->is_undefined();
1707f183
CC
1052 // Commons from plugins are just placeholders.
1053 was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
18e6b24e 1054
2ea97941 1055 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
b45e00b3 1056 version, is_default_version);
6d03d481
ST
1057 if (parameters->options().gc_sections())
1058 this->gc_mark_dyn_syms(ret);
14bfc3f5
ILT
1059 ins.first->second = ret;
1060 }
1061 else
1062 {
18e6b24e
ILT
1063 was_undefined = false;
1064 was_common = false;
1065
f6ce93d6 1066 Sized_target<size, big_endian>* target =
029ba973 1067 parameters->sized_target<size, big_endian>();
1564db8d
ILT
1068 if (!target->has_make_symbol())
1069 ret = new Sized_symbol<size>();
1070 else
14bfc3f5 1071 {
dc1c8a16
CC
1072 ret = target->make_symbol(name, sym.get_st_type(), object,
1073 st_shndx, sym.get_st_value());
1564db8d 1074 if (ret == NULL)
14bfc3f5
ILT
1075 {
1076 // This means that we don't want a symbol table
1077 // entry after all.
8781f709 1078 if (!is_default_version)
14bfc3f5
ILT
1079 this->table_.erase(ins.first);
1080 else
1081 {
8781f709
ILT
1082 this->table_.erase(insdefault.first);
1083 // Inserting INSDEFAULT invalidated INS.
f0641a0b
ILT
1084 this->table_.erase(std::make_pair(name_key,
1085 version_key));
14bfc3f5
ILT
1086 }
1087 return NULL;
1088 }
1089 }
14bfc3f5 1090
2ea97941 1091 ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
1564db8d 1092
14bfc3f5 1093 ins.first->second = ret;
8781f709 1094 if (is_default_version)
14bfc3f5
ILT
1095 {
1096 // This is the first time we have seen NAME/NULL. Point
1097 // it at the new entry for NAME/VERSION.
8781f709
ILT
1098 gold_assert(insdefault.second);
1099 insdefault.first->second = ret;
14bfc3f5
ILT
1100 }
1101 }
8c500701 1102
8781f709 1103 if (is_default_version)
8c500701 1104 ret->set_is_default();
14bfc3f5
ILT
1105 }
1106
ead1e424
ILT
1107 // Record every time we see a new undefined symbol, to speed up
1108 // archive groups.
1109 if (!was_undefined && ret->is_undefined())
0f3b89d8
ILT
1110 {
1111 ++this->saw_undefined_;
1112 if (parameters->options().has_plugins())
1113 parameters->options().plugins()->new_undefined_symbol(ret);
1114 }
ead1e424
ILT
1115
1116 // Keep track of common symbols, to speed up common symbol
1707f183
CC
1117 // allocation. Don't record commons from plugin objects;
1118 // we need to wait until we see the real symbol in the
1119 // replacement file.
1120 if (!was_common && ret->is_common() && ret->object()->pluginobj() == NULL)
155a0dd7 1121 {
8a5e3e08 1122 if (ret->type() == elfcpp::STT_TLS)
155a0dd7 1123 this->tls_commons_.push_back(ret);
8a5e3e08
ILT
1124 else if (!is_ordinary
1125 && st_shndx == parameters->target().small_common_shndx())
1126 this->small_commons_.push_back(ret);
1127 else if (!is_ordinary
1128 && st_shndx == parameters->target().large_common_shndx())
1129 this->large_commons_.push_back(ret);
1130 else
1131 this->commons_.push_back(ret);
155a0dd7 1132 }
ead1e424 1133
0602e05a
ILT
1134 // If we're not doing a relocatable link, then any symbol with
1135 // hidden or internal visibility is local.
1136 if ((ret->visibility() == elfcpp::STV_HIDDEN
1137 || ret->visibility() == elfcpp::STV_INTERNAL)
1138 && (ret->binding() == elfcpp::STB_GLOBAL
adcf2816 1139 || ret->binding() == elfcpp::STB_GNU_UNIQUE
0602e05a
ILT
1140 || ret->binding() == elfcpp::STB_WEAK)
1141 && !parameters->options().relocatable())
1142 this->force_local(ret);
1143
14bfc3f5
ILT
1144 return ret;
1145}
1146
f6ce93d6 1147// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
1148
1149template<int size, bool big_endian>
1150void
dbe717ef 1151Symbol_table::add_from_relobj(
6fa2a40b 1152 Sized_relobj_file<size, big_endian>* relobj,
f6ce93d6 1153 const unsigned char* syms,
14bfc3f5 1154 size_t count,
d491d34e 1155 size_t symndx_offset,
14bfc3f5
ILT
1156 const char* sym_names,
1157 size_t sym_name_size,
6fa2a40b 1158 typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
ca09d69a 1159 size_t* defined)
14bfc3f5 1160{
92de84a6
ILT
1161 *defined = 0;
1162
8851ecca 1163 gold_assert(size == parameters->target().get_size());
14bfc3f5 1164
a783673b
ILT
1165 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1166
88dd47ac
ILT
1167 const bool just_symbols = relobj->just_symbols();
1168
f6ce93d6 1169 const unsigned char* p = syms;
a783673b 1170 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5 1171 {
92de84a6
ILT
1172 (*sympointers)[i] = NULL;
1173
14bfc3f5
ILT
1174 elfcpp::Sym<size, big_endian> sym(p);
1175
d491d34e 1176 unsigned int st_name = sym.get_st_name();
14bfc3f5
ILT
1177 if (st_name >= sym_name_size)
1178 {
75f2446e
ILT
1179 relobj->error(_("bad global symbol name offset %u at %zu"),
1180 st_name, i);
1181 continue;
14bfc3f5
ILT
1182 }
1183
2ea97941 1184 const char* name = sym_names + st_name;
dbe717ef 1185
bebf4942
L
1186 if (!parameters->options().relocatable()
1187 && strcmp (name, "__gnu_lto_slim") == 0)
7cd4e5b7
AM
1188 gold_info(_("%s: plugin needed to handle lto object"),
1189 relobj->name().c_str());
1190
d491d34e
ILT
1191 bool is_ordinary;
1192 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1193 sym.get_st_shndx(),
1194 &is_ordinary);
1195 unsigned int orig_st_shndx = st_shndx;
1196 if (!is_ordinary)
1197 orig_st_shndx = elfcpp::SHN_UNDEF;
1198
92de84a6
ILT
1199 if (st_shndx != elfcpp::SHN_UNDEF)
1200 ++*defined;
1201
a783673b
ILT
1202 // A symbol defined in a section which we are not including must
1203 // be treated as an undefined symbol.
880cd20d 1204 bool is_defined_in_discarded_section = false;
a783673b 1205 if (st_shndx != elfcpp::SHN_UNDEF
d491d34e 1206 && is_ordinary
ce97fa81
ST
1207 && !relobj->is_section_included(st_shndx)
1208 && !this->is_section_folded(relobj, st_shndx))
880cd20d
ILT
1209 {
1210 st_shndx = elfcpp::SHN_UNDEF;
1211 is_defined_in_discarded_section = true;
1212 }
a783673b 1213
14bfc3f5
ILT
1214 // In an object file, an '@' in the name separates the symbol
1215 // name from the version name. If there are two '@' characters,
1216 // this is the default version.
2ea97941 1217 const char* ver = strchr(name, '@');
057ead22 1218 Stringpool::Key ver_key = 0;
09124467 1219 int namelen = 0;
8781f709
ILT
1220 // IS_DEFAULT_VERSION: is the version default?
1221 // IS_FORCED_LOCAL: is the symbol forced local?
1222 bool is_default_version = false;
1223 bool is_forced_local = false;
09124467 1224
a7dac153
CC
1225 // FIXME: For incremental links, we don't store version information,
1226 // so we need to ignore version symbols for now.
1227 if (parameters->incremental_update() && ver != NULL)
1228 {
1229 namelen = ver - name;
1230 ver = NULL;
1231 }
1232
09124467
ILT
1233 if (ver != NULL)
1234 {
1235 // The symbol name is of the form foo@VERSION or foo@@VERSION
2ea97941 1236 namelen = ver - name;
09124467
ILT
1237 ++ver;
1238 if (*ver == '@')
1239 {
8781f709 1240 is_default_version = true;
09124467
ILT
1241 ++ver;
1242 }
057ead22 1243 ver = this->namepool_.add(ver, true, &ver_key);
09124467 1244 }
5871526f
ILT
1245 // We don't want to assign a version to an undefined symbol,
1246 // even if it is listed in the version script. FIXME: What
1247 // about a common symbol?
057ead22
ILT
1248 else
1249 {
2ea97941 1250 namelen = strlen(name);
057ead22
ILT
1251 if (!this->version_script_.empty()
1252 && st_shndx != elfcpp::SHN_UNDEF)
1253 {
1254 // The symbol name did not have a version, but the
1255 // version script may assign a version anyway.
2ea97941 1256 std::string version;
98e090bd
ILT
1257 bool is_global;
1258 if (this->version_script_.get_symbol_version(name, &version,
1259 &is_global))
057ead22 1260 {
98e090bd
ILT
1261 if (!is_global)
1262 is_forced_local = true;
1263 else if (!version.empty())
057ead22 1264 {
2ea97941
ILT
1265 ver = this->namepool_.add_with_length(version.c_str(),
1266 version.length(),
057ead22
ILT
1267 true,
1268 &ver_key);
8781f709 1269 is_default_version = true;
057ead22
ILT
1270 }
1271 }
057ead22
ILT
1272 }
1273 }
14bfc3f5 1274
d491d34e
ILT
1275 elfcpp::Sym<size, big_endian>* psym = &sym;
1276 unsigned char symbuf[sym_size];
1277 elfcpp::Sym<size, big_endian> sym2(symbuf);
88dd47ac
ILT
1278 if (just_symbols)
1279 {
d491d34e 1280 memcpy(symbuf, p, sym_size);
88dd47ac 1281 elfcpp::Sym_write<size, big_endian> sw(symbuf);
9590bf25
CC
1282 if (orig_st_shndx != elfcpp::SHN_UNDEF
1283 && is_ordinary
1284 && relobj->e_type() == elfcpp::ET_REL)
88dd47ac 1285 {
9590bf25
CC
1286 // Symbol values in relocatable object files are section
1287 // relative. This is normally what we want, but since here
1288 // we are converting the symbol to absolute we need to add
1289 // the section address. The section address in an object
88dd47ac
ILT
1290 // file is normally zero, but people can use a linker
1291 // script to change it.
d491d34e
ILT
1292 sw.put_st_value(sym.get_st_value()
1293 + relobj->section_address(orig_st_shndx));
88dd47ac 1294 }
d491d34e
ILT
1295 st_shndx = elfcpp::SHN_ABS;
1296 is_ordinary = false;
88dd47ac
ILT
1297 psym = &sym2;
1298 }
1299
65514900 1300 // Fix up visibility if object has no-export set.
1c74fab0
ILT
1301 if (relobj->no_export()
1302 && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
65514900
CC
1303 {
1304 // We may have copied symbol already above.
1305 if (psym != &sym2)
1306 {
1307 memcpy(symbuf, p, sym_size);
1308 psym = &sym2;
1309 }
1310
1311 elfcpp::STV visibility = sym2.get_st_visibility();
1312 if (visibility == elfcpp::STV_DEFAULT
1313 || visibility == elfcpp::STV_PROTECTED)
1314 {
1315 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1316 unsigned char nonvis = sym2.get_st_nonvis();
1317 sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1318 }
1319 }
1320
057ead22 1321 Stringpool::Key name_key;
2ea97941 1322 name = this->namepool_.add_with_length(name, namelen, true,
057ead22
ILT
1323 &name_key);
1324
aeddab66 1325 Sized_symbol<size>* res;
2ea97941 1326 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
8781f709
ILT
1327 is_default_version, *psym, st_shndx,
1328 is_ordinary, orig_st_shndx);
2c00092d
JC
1329
1330 if (res == NULL)
1331 continue;
6d03d481 1332
804eb480
ST
1333 if (is_forced_local)
1334 this->force_local(res);
1335
7257cc92
ST
1336 // Do not treat this symbol as garbage if this symbol will be
1337 // exported to the dynamic symbol table. This is true when
1338 // building a shared library or using --export-dynamic and
1339 // the symbol is externally visible.
1340 if (parameters->options().gc_sections()
1341 && res->is_externally_visible()
1342 && !res->is_from_dynobj()
1343 && (parameters->options().shared()
fd834e57
CC
1344 || parameters->options().export_dynamic()
1345 || parameters->options().in_dynamic_list(res->name())))
7257cc92 1346 this->gc_mark_symbol(res);
f0641a0b 1347
880cd20d
ILT
1348 if (is_defined_in_discarded_section)
1349 res->set_is_defined_in_discarded_section();
1350
730cdc88 1351 (*sympointers)[i] = res;
14bfc3f5
ILT
1352 }
1353}
1354
89fc3421
CC
1355// Add a symbol from a plugin-claimed file.
1356
1357template<int size, bool big_endian>
1358Symbol*
1359Symbol_table::add_from_pluginobj(
1360 Sized_pluginobj<size, big_endian>* obj,
2ea97941 1361 const char* name,
89fc3421
CC
1362 const char* ver,
1363 elfcpp::Sym<size, big_endian>* sym)
1364{
1365 unsigned int st_shndx = sym->get_st_shndx();
24998053 1366 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
89fc3421
CC
1367
1368 Stringpool::Key ver_key = 0;
8781f709
ILT
1369 bool is_default_version = false;
1370 bool is_forced_local = false;
89fc3421
CC
1371
1372 if (ver != NULL)
1373 {
1374 ver = this->namepool_.add(ver, true, &ver_key);
1375 }
1376 // We don't want to assign a version to an undefined symbol,
1377 // even if it is listed in the version script. FIXME: What
1378 // about a common symbol?
1379 else
1380 {
1381 if (!this->version_script_.empty()
1382 && st_shndx != elfcpp::SHN_UNDEF)
1383 {
1384 // The symbol name did not have a version, but the
1385 // version script may assign a version anyway.
2ea97941 1386 std::string version;
98e090bd
ILT
1387 bool is_global;
1388 if (this->version_script_.get_symbol_version(name, &version,
1389 &is_global))
89fc3421 1390 {
98e090bd
ILT
1391 if (!is_global)
1392 is_forced_local = true;
1393 else if (!version.empty())
89fc3421 1394 {
2ea97941
ILT
1395 ver = this->namepool_.add_with_length(version.c_str(),
1396 version.length(),
89fc3421
CC
1397 true,
1398 &ver_key);
8781f709 1399 is_default_version = true;
89fc3421
CC
1400 }
1401 }
89fc3421
CC
1402 }
1403 }
1404
1405 Stringpool::Key name_key;
2ea97941 1406 name = this->namepool_.add(name, true, &name_key);
89fc3421
CC
1407
1408 Sized_symbol<size>* res;
2ea97941 1409 res = this->add_from_object(obj, name, name_key, ver, ver_key,
8781f709
ILT
1410 is_default_version, *sym, st_shndx,
1411 is_ordinary, st_shndx);
89fc3421 1412
2c00092d
JC
1413 if (res == NULL)
1414 return NULL;
1415
8781f709 1416 if (is_forced_local)
0602e05a 1417 this->force_local(res);
89fc3421
CC
1418
1419 return res;
1420}
1421
dbe717ef
ILT
1422// Add all the symbols in a dynamic object to the hash table.
1423
1424template<int size, bool big_endian>
1425void
1426Symbol_table::add_from_dynobj(
1427 Sized_dynobj<size, big_endian>* dynobj,
1428 const unsigned char* syms,
1429 size_t count,
1430 const char* sym_names,
1431 size_t sym_name_size,
1432 const unsigned char* versym,
1433 size_t versym_size,
92de84a6 1434 const std::vector<const char*>* version_map,
6fa2a40b 1435 typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
92de84a6 1436 size_t* defined)
dbe717ef 1437{
92de84a6
ILT
1438 *defined = 0;
1439
8851ecca 1440 gold_assert(size == parameters->target().get_size());
dbe717ef 1441
88dd47ac
ILT
1442 if (dynobj->just_symbols())
1443 {
1444 gold_error(_("--just-symbols does not make sense with a shared object"));
1445 return;
1446 }
1447
a7dac153
CC
1448 // FIXME: For incremental links, we don't store version information,
1449 // so we need to ignore version symbols for now.
1450 if (parameters->incremental_update())
1451 versym = NULL;
1452
dbe717ef
ILT
1453 if (versym != NULL && versym_size / 2 < count)
1454 {
75f2446e
ILT
1455 dynobj->error(_("too few symbol versions"));
1456 return;
dbe717ef
ILT
1457 }
1458
1459 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1460
aeddab66
ILT
1461 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1462 // weak aliases. This is necessary because if the dynamic object
1463 // provides the same variable under two names, one of which is a
1464 // weak definition, and the regular object refers to the weak
1465 // definition, we have to put both the weak definition and the
1466 // strong definition into the dynamic symbol table. Given a weak
1467 // definition, the only way that we can find the corresponding
1468 // strong definition, if any, is to search the symbol table.
1469 std::vector<Sized_symbol<size>*> object_symbols;
1470
dbe717ef
ILT
1471 const unsigned char* p = syms;
1472 const unsigned char* vs = versym;
1473 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1474 {
1475 elfcpp::Sym<size, big_endian> sym(p);
1476
92de84a6
ILT
1477 if (sympointers != NULL)
1478 (*sympointers)[i] = NULL;
1479
65778909
ILT
1480 // Ignore symbols with local binding or that have
1481 // internal or hidden visibility.
1482 if (sym.get_st_bind() == elfcpp::STB_LOCAL
1483 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1484 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
dbe717ef
ILT
1485 continue;
1486
8bdcdf2c
ILT
1487 // A protected symbol in a shared library must be treated as a
1488 // normal symbol when viewed from outside the shared library.
1489 // Implement this by overriding the visibility here.
3d4fde69
CC
1490 // Likewise, an IFUNC symbol in a shared library must be treated
1491 // as a normal FUNC symbol.
8bdcdf2c
ILT
1492 elfcpp::Sym<size, big_endian>* psym = &sym;
1493 unsigned char symbuf[sym_size];
1494 elfcpp::Sym<size, big_endian> sym2(symbuf);
3d4fde69
CC
1495 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED
1496 || sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
8bdcdf2c
ILT
1497 {
1498 memcpy(symbuf, p, sym_size);
1499 elfcpp::Sym_write<size, big_endian> sw(symbuf);
3d4fde69
CC
1500 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1501 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1502 if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1503 sw.put_st_info(sym.get_st_bind(), elfcpp::STT_FUNC);
8bdcdf2c
ILT
1504 psym = &sym2;
1505 }
1506
1507 unsigned int st_name = psym->get_st_name();
dbe717ef
ILT
1508 if (st_name >= sym_name_size)
1509 {
75f2446e
ILT
1510 dynobj->error(_("bad symbol name offset %u at %zu"),
1511 st_name, i);
1512 continue;
dbe717ef
ILT
1513 }
1514
2ea97941 1515 const char* name = sym_names + st_name;
dbe717ef 1516
d491d34e 1517 bool is_ordinary;
8bdcdf2c 1518 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
d491d34e
ILT
1519 &is_ordinary);
1520
92de84a6
ILT
1521 if (st_shndx != elfcpp::SHN_UNDEF)
1522 ++*defined;
1523
aeddab66
ILT
1524 Sized_symbol<size>* res;
1525
dbe717ef
ILT
1526 if (versym == NULL)
1527 {
1528 Stringpool::Key name_key;
2ea97941
ILT
1529 name = this->namepool_.add(name, true, &name_key);
1530 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1531 false, *psym, st_shndx, is_ordinary,
d491d34e 1532 st_shndx);
dbe717ef 1533 }
aeddab66
ILT
1534 else
1535 {
1536 // Read the version information.
dbe717ef 1537
aeddab66 1538 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
dbe717ef 1539
aeddab66
ILT
1540 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1541 v &= elfcpp::VERSYM_VERSION;
dbe717ef 1542
aeddab66
ILT
1543 // The Sun documentation says that V can be VER_NDX_LOCAL,
1544 // or VER_NDX_GLOBAL, or a version index. The meaning of
1545 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1546 // The old GNU linker will happily generate VER_NDX_LOCAL
1547 // for an undefined symbol. I don't know what the Sun
1548 // linker will generate.
dbe717ef 1549
aeddab66 1550 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
d491d34e 1551 && st_shndx != elfcpp::SHN_UNDEF)
aeddab66
ILT
1552 {
1553 // This symbol should not be visible outside the object.
1554 continue;
1555 }
64707334 1556
aeddab66
ILT
1557 // At this point we are definitely going to add this symbol.
1558 Stringpool::Key name_key;
2ea97941 1559 name = this->namepool_.add(name, true, &name_key);
dbe717ef 1560
aeddab66
ILT
1561 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1562 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1563 {
1564 // This symbol does not have a version.
2ea97941 1565 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1566 false, *psym, st_shndx, is_ordinary,
d491d34e 1567 st_shndx);
aeddab66
ILT
1568 }
1569 else
1570 {
1571 if (v >= version_map->size())
1572 {
1573 dynobj->error(_("versym for symbol %zu out of range: %u"),
1574 i, v);
1575 continue;
1576 }
dbe717ef 1577
2ea97941
ILT
1578 const char* version = (*version_map)[v];
1579 if (version == NULL)
aeddab66
ILT
1580 {
1581 dynobj->error(_("versym for symbol %zu has no name: %u"),
1582 i, v);
1583 continue;
1584 }
dbe717ef 1585
aeddab66 1586 Stringpool::Key version_key;
2ea97941 1587 version = this->namepool_.add(version, true, &version_key);
aeddab66
ILT
1588
1589 // If this is an absolute symbol, and the version name
1590 // and symbol name are the same, then this is the
1591 // version definition symbol. These symbols exist to
1592 // support using -u to pull in particular versions. We
1593 // do not want to record a version for them.
d491d34e
ILT
1594 if (st_shndx == elfcpp::SHN_ABS
1595 && !is_ordinary
aeddab66 1596 && name_key == version_key)
2ea97941 1597 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1598 false, *psym, st_shndx, is_ordinary,
d491d34e 1599 st_shndx);
aeddab66
ILT
1600 else
1601 {
8781f709
ILT
1602 const bool is_default_version =
1603 !hidden && st_shndx != elfcpp::SHN_UNDEF;
2ea97941 1604 res = this->add_from_object(dynobj, name, name_key, version,
8781f709
ILT
1605 version_key, is_default_version,
1606 *psym, st_shndx,
d491d34e 1607 is_ordinary, st_shndx);
aeddab66
ILT
1608 }
1609 }
dbe717ef
ILT
1610 }
1611
2c00092d
JC
1612 if (res == NULL)
1613 continue;
1614
99a37bfd 1615 // Note that it is possible that RES was overridden by an
a4bb589a 1616 // earlier object, in which case it can't be aliased here.
d491d34e
ILT
1617 if (st_shndx != elfcpp::SHN_UNDEF
1618 && is_ordinary
8bdcdf2c 1619 && psym->get_st_type() == elfcpp::STT_OBJECT
99a37bfd
ILT
1620 && res->source() == Symbol::FROM_OBJECT
1621 && res->object() == dynobj)
aeddab66 1622 object_symbols.push_back(res);
92de84a6 1623
6eeb0170
CC
1624 // If the symbol has protected visibility in the dynobj,
1625 // mark it as such if it was not overridden.
1626 if (res->source() == Symbol::FROM_OBJECT
1627 && res->object() == dynobj
1628 && sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1629 res->set_is_protected();
1630
92de84a6
ILT
1631 if (sympointers != NULL)
1632 (*sympointers)[i] = res;
aeddab66
ILT
1633 }
1634
1635 this->record_weak_aliases(&object_symbols);
1636}
1637
cdc29364
CC
1638// Add a symbol from a incremental object file.
1639
1640template<int size, bool big_endian>
26d3c67d 1641Sized_symbol<size>*
cdc29364
CC
1642Symbol_table::add_from_incrobj(
1643 Object* obj,
1644 const char* name,
1645 const char* ver,
1646 elfcpp::Sym<size, big_endian>* sym)
1647{
1648 unsigned int st_shndx = sym->get_st_shndx();
1649 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1650
1651 Stringpool::Key ver_key = 0;
1652 bool is_default_version = false;
cdc29364
CC
1653
1654 Stringpool::Key name_key;
1655 name = this->namepool_.add(name, true, &name_key);
1656
1657 Sized_symbol<size>* res;
1658 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1659 is_default_version, *sym, st_shndx,
1660 is_ordinary, st_shndx);
1661
cdc29364
CC
1662 return res;
1663}
1664
aeddab66
ILT
1665// This is used to sort weak aliases. We sort them first by section
1666// index, then by offset, then by weak ahead of strong.
1667
1668template<int size>
1669class Weak_alias_sorter
1670{
1671 public:
1672 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1673};
1674
1675template<int size>
1676bool
1677Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1678 const Sized_symbol<size>* s2) const
1679{
d491d34e
ILT
1680 bool is_ordinary;
1681 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1682 gold_assert(is_ordinary);
1683 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1684 gold_assert(is_ordinary);
1685 if (s1_shndx != s2_shndx)
1686 return s1_shndx < s2_shndx;
1687
aeddab66
ILT
1688 if (s1->value() != s2->value())
1689 return s1->value() < s2->value();
1690 if (s1->binding() != s2->binding())
1691 {
1692 if (s1->binding() == elfcpp::STB_WEAK)
1693 return true;
1694 if (s2->binding() == elfcpp::STB_WEAK)
1695 return false;
1696 }
1697 return std::string(s1->name()) < std::string(s2->name());
1698}
dbe717ef 1699
aeddab66
ILT
1700// SYMBOLS is a list of object symbols from a dynamic object. Look
1701// for any weak aliases, and record them so that if we add the weak
1702// alias to the dynamic symbol table, we also add the corresponding
1703// strong symbol.
dbe717ef 1704
aeddab66
ILT
1705template<int size>
1706void
1707Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1708{
1709 // Sort the vector by section index, then by offset, then by weak
1710 // ahead of strong.
1711 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1712
1713 // Walk through the vector. For each weak definition, record
1714 // aliases.
1715 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1716 symbols->begin();
1717 p != symbols->end();
1718 ++p)
1719 {
1720 if ((*p)->binding() != elfcpp::STB_WEAK)
1721 continue;
1722
1723 // Build a circular list of weak aliases. Each symbol points to
1724 // the next one in the circular list.
1725
1726 Sized_symbol<size>* from_sym = *p;
1727 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1728 for (q = p + 1; q != symbols->end(); ++q)
dbe717ef 1729 {
d491d34e
ILT
1730 bool dummy;
1731 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
aeddab66
ILT
1732 || (*q)->value() != from_sym->value())
1733 break;
1734
1735 this->weak_aliases_[from_sym] = *q;
1736 from_sym->set_has_alias();
1737 from_sym = *q;
dbe717ef
ILT
1738 }
1739
aeddab66
ILT
1740 if (from_sym != *p)
1741 {
1742 this->weak_aliases_[from_sym] = *p;
1743 from_sym->set_has_alias();
1744 }
dbe717ef 1745
aeddab66 1746 p = q - 1;
dbe717ef
ILT
1747 }
1748}
1749
ead1e424
ILT
1750// Create and return a specially defined symbol. If ONLY_IF_REF is
1751// true, then only create the symbol if there is a reference to it.
86f2e683 1752// If this does not return NULL, it sets *POLDSYM to the existing
8c500701
ILT
1753// symbol if there is one. This sets *RESOLVE_OLDSYM if we should
1754// resolve the newly created symbol to the old one. This
1755// canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
1756
1757template<int size, bool big_endian>
1758Sized_symbol<size>*
9b07f471
ILT
1759Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1760 bool only_if_ref,
8c500701 1761 Sized_symbol<size>** poldsym,
ca09d69a 1762 bool* resolve_oldsym)
ead1e424 1763{
8c500701 1764 *resolve_oldsym = false;
8cc69fb6 1765 *poldsym = NULL;
ead1e424 1766
55a93433
ILT
1767 // If the caller didn't give us a version, see if we get one from
1768 // the version script.
057ead22 1769 std::string v;
8c500701 1770 bool is_default_version = false;
55a93433
ILT
1771 if (*pversion == NULL)
1772 {
98e090bd
ILT
1773 bool is_global;
1774 if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
057ead22 1775 {
98e090bd
ILT
1776 if (is_global && !v.empty())
1777 {
1778 *pversion = v.c_str();
1779 // If we get the version from a version script, then we
1780 // are also the default version.
1781 is_default_version = true;
1782 }
057ead22 1783 }
55a93433
ILT
1784 }
1785
8c500701
ILT
1786 Symbol* oldsym;
1787 Sized_symbol<size>* sym;
1788
1789 bool add_to_table = false;
1790 typename Symbol_table_type::iterator add_loc = this->table_.end();
1791 bool add_def_to_table = false;
1792 typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1793
ead1e424
ILT
1794 if (only_if_ref)
1795 {
306d9ef0 1796 oldsym = this->lookup(*pname, *pversion);
8c500701
ILT
1797 if (oldsym == NULL && is_default_version)
1798 oldsym = this->lookup(*pname, NULL);
f6ce93d6 1799 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 1800 return NULL;
306d9ef0
ILT
1801
1802 *pname = oldsym->name();
eebd87a5
ILT
1803 if (is_default_version)
1804 *pversion = this->namepool_.add(*pversion, true, NULL);
1805 else
8c500701 1806 *pversion = oldsym->version();
ead1e424
ILT
1807 }
1808 else
1809 {
14b31740 1810 // Canonicalize NAME and VERSION.
f0641a0b 1811 Stringpool::Key name_key;
cfd73a4e 1812 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 1813
14b31740 1814 Stringpool::Key version_key = 0;
306d9ef0 1815 if (*pversion != NULL)
cfd73a4e 1816 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 1817
ead1e424 1818 Symbol* const snull = NULL;
ead1e424 1819 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
1820 this->table_.insert(std::make_pair(std::make_pair(name_key,
1821 version_key),
ead1e424
ILT
1822 snull));
1823
8781f709 1824 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
8c500701
ILT
1825 std::make_pair(this->table_.end(), false);
1826 if (is_default_version)
1827 {
1828 const Stringpool::Key vnull = 0;
8781f709
ILT
1829 insdefault =
1830 this->table_.insert(std::make_pair(std::make_pair(name_key,
1831 vnull),
1832 snull));
8c500701
ILT
1833 }
1834
ead1e424
ILT
1835 if (!ins.second)
1836 {
14b31740 1837 // We already have a symbol table entry for NAME/VERSION.
ead1e424 1838 oldsym = ins.first->second;
a3ad94ed 1839 gold_assert(oldsym != NULL);
8c500701
ILT
1840
1841 if (is_default_version)
1842 {
1843 Sized_symbol<size>* soldsym =
1844 this->get_sized_symbol<size>(oldsym);
1845 this->define_default_version<size, big_endian>(soldsym,
8781f709
ILT
1846 insdefault.second,
1847 insdefault.first);
8c500701 1848 }
ead1e424
ILT
1849 }
1850 else
1851 {
1852 // We haven't seen this symbol before.
a3ad94ed 1853 gold_assert(ins.first->second == NULL);
8c500701
ILT
1854
1855 add_to_table = true;
1856 add_loc = ins.first;
1857
8781f709 1858 if (is_default_version && !insdefault.second)
8c500701
ILT
1859 {
1860 // We are adding NAME/VERSION, and it is the default
1861 // version. We already have an entry for NAME/NULL.
8781f709 1862 oldsym = insdefault.first->second;
8c500701
ILT
1863 *resolve_oldsym = true;
1864 }
1865 else
1866 {
1867 oldsym = NULL;
1868
1869 if (is_default_version)
1870 {
1871 add_def_to_table = true;
8781f709 1872 add_def_loc = insdefault.first;
8c500701
ILT
1873 }
1874 }
ead1e424
ILT
1875 }
1876 }
1877
8851ecca
ILT
1878 const Target& target = parameters->target();
1879 if (!target.has_make_symbol())
86f2e683
ILT
1880 sym = new Sized_symbol<size>();
1881 else
ead1e424 1882 {
029ba973
ILT
1883 Sized_target<size, big_endian>* sized_target =
1884 parameters->sized_target<size, big_endian>();
dc1c8a16
CC
1885 sym = sized_target->make_symbol(*pname, elfcpp::STT_NOTYPE,
1886 NULL, elfcpp::SHN_UNDEF, 0);
86f2e683
ILT
1887 if (sym == NULL)
1888 return NULL;
1889 }
ead1e424 1890
86f2e683
ILT
1891 if (add_to_table)
1892 add_loc->second = sym;
1893 else
1894 gold_assert(oldsym != NULL);
ead1e424 1895
8c500701
ILT
1896 if (add_def_to_table)
1897 add_def_loc->second = sym;
1898
7d1a9ebb 1899 *poldsym = this->get_sized_symbol<size>(oldsym);
ead1e424
ILT
1900
1901 return sym;
1902}
1903
1904// Define a symbol based on an Output_data.
1905
14b31740 1906Symbol*
2ea97941
ILT
1907Symbol_table::define_in_output_data(const char* name,
1908 const char* version,
99fff23b 1909 Defined defined,
9b07f471 1910 Output_data* od,
2ea97941
ILT
1911 uint64_t value,
1912 uint64_t symsize,
9b07f471
ILT
1913 elfcpp::STT type,
1914 elfcpp::STB binding,
ead1e424
ILT
1915 elfcpp::STV visibility,
1916 unsigned char nonvis,
2ea97941 1917 bool offset_is_from_end,
ead1e424
ILT
1918 bool only_if_ref)
1919{
8851ecca 1920 if (parameters->target().get_size() == 32)
86f2e683
ILT
1921 {
1922#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
99fff23b 1923 return this->do_define_in_output_data<32>(name, version, defined, od,
2ea97941 1924 value, symsize, type, binding,
86f2e683 1925 visibility, nonvis,
2ea97941 1926 offset_is_from_end,
86f2e683
ILT
1927 only_if_ref);
1928#else
1929 gold_unreachable();
1930#endif
1931 }
8851ecca 1932 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1933 {
1934#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
99fff23b 1935 return this->do_define_in_output_data<64>(name, version, defined, od,
2ea97941 1936 value, symsize, type, binding,
86f2e683 1937 visibility, nonvis,
2ea97941 1938 offset_is_from_end,
86f2e683
ILT
1939 only_if_ref);
1940#else
1941 gold_unreachable();
1942#endif
1943 }
ead1e424 1944 else
a3ad94ed 1945 gold_unreachable();
ead1e424
ILT
1946}
1947
1948// Define a symbol in an Output_data, sized version.
1949
1950template<int size>
14b31740 1951Sized_symbol<size>*
ead1e424 1952Symbol_table::do_define_in_output_data(
2ea97941
ILT
1953 const char* name,
1954 const char* version,
99fff23b 1955 Defined defined,
ead1e424 1956 Output_data* od,
2ea97941
ILT
1957 typename elfcpp::Elf_types<size>::Elf_Addr value,
1958 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
ead1e424
ILT
1959 elfcpp::STT type,
1960 elfcpp::STB binding,
1961 elfcpp::STV visibility,
1962 unsigned char nonvis,
2ea97941 1963 bool offset_is_from_end,
ead1e424
ILT
1964 bool only_if_ref)
1965{
1966 Sized_symbol<size>* sym;
86f2e683 1967 Sized_symbol<size>* oldsym;
8c500701 1968 bool resolve_oldsym;
ead1e424 1969
8851ecca 1970 if (parameters->target().is_big_endian())
193a53d9
ILT
1971 {
1972#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2ea97941 1973 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
1974 only_if_ref, &oldsym,
1975 &resolve_oldsym);
193a53d9
ILT
1976#else
1977 gold_unreachable();
1978#endif
1979 }
ead1e424 1980 else
193a53d9
ILT
1981 {
1982#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2ea97941 1983 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
1984 only_if_ref, &oldsym,
1985 &resolve_oldsym);
193a53d9
ILT
1986#else
1987 gold_unreachable();
1988#endif
1989 }
ead1e424
ILT
1990
1991 if (sym == NULL)
14b31740 1992 return NULL;
ead1e424 1993
2ea97941 1994 sym->init_output_data(name, version, od, value, symsize, type, binding,
5146f448
CC
1995 visibility, nonvis, offset_is_from_end,
1996 defined == PREDEFINED);
14b31740 1997
e5756efb 1998 if (oldsym == NULL)
55a93433
ILT
1999 {
2000 if (binding == elfcpp::STB_LOCAL
2ea97941 2001 || this->version_script_.symbol_is_local(name))
55a93433 2002 this->force_local(sym);
2ea97941 2003 else if (version != NULL)
75517b77 2004 sym->set_is_default();
55a93433
ILT
2005 return sym;
2006 }
86f2e683 2007
62855347 2008 if (Symbol_table::should_override_with_special(oldsym, type, defined))
e5756efb 2009 this->override_with_special(oldsym, sym);
8c500701
ILT
2010
2011 if (resolve_oldsym)
2012 return sym;
2013 else
2014 {
db1ff028
CC
2015 if (defined == PREDEFINED
2016 && (binding == elfcpp::STB_LOCAL
2017 || this->version_script_.symbol_is_local(name)))
5417c94d 2018 this->force_local(oldsym);
8c500701
ILT
2019 delete sym;
2020 return oldsym;
2021 }
ead1e424
ILT
2022}
2023
2024// Define a symbol based on an Output_segment.
2025
14b31740 2026Symbol*
2ea97941 2027Symbol_table::define_in_output_segment(const char* name,
99fff23b
ILT
2028 const char* version,
2029 Defined defined,
2030 Output_segment* os,
2ea97941
ILT
2031 uint64_t value,
2032 uint64_t symsize,
9b07f471
ILT
2033 elfcpp::STT type,
2034 elfcpp::STB binding,
ead1e424
ILT
2035 elfcpp::STV visibility,
2036 unsigned char nonvis,
2ea97941 2037 Symbol::Segment_offset_base offset_base,
ead1e424
ILT
2038 bool only_if_ref)
2039{
8851ecca 2040 if (parameters->target().get_size() == 32)
86f2e683
ILT
2041 {
2042#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
99fff23b 2043 return this->do_define_in_output_segment<32>(name, version, defined, os,
2ea97941 2044 value, symsize, type,
86f2e683 2045 binding, visibility, nonvis,
2ea97941 2046 offset_base, only_if_ref);
86f2e683
ILT
2047#else
2048 gold_unreachable();
2049#endif
2050 }
8851ecca 2051 else if (parameters->target().get_size() == 64)
86f2e683
ILT
2052 {
2053#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
99fff23b 2054 return this->do_define_in_output_segment<64>(name, version, defined, os,
2ea97941 2055 value, symsize, type,
86f2e683 2056 binding, visibility, nonvis,
2ea97941 2057 offset_base, only_if_ref);
86f2e683
ILT
2058#else
2059 gold_unreachable();
2060#endif
2061 }
ead1e424 2062 else
a3ad94ed 2063 gold_unreachable();
ead1e424
ILT
2064}
2065
2066// Define a symbol in an Output_segment, sized version.
2067
2068template<int size>
14b31740 2069Sized_symbol<size>*
ead1e424 2070Symbol_table::do_define_in_output_segment(
2ea97941
ILT
2071 const char* name,
2072 const char* version,
99fff23b 2073 Defined defined,
ead1e424 2074 Output_segment* os,
2ea97941
ILT
2075 typename elfcpp::Elf_types<size>::Elf_Addr value,
2076 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
ead1e424
ILT
2077 elfcpp::STT type,
2078 elfcpp::STB binding,
2079 elfcpp::STV visibility,
2080 unsigned char nonvis,
2ea97941 2081 Symbol::Segment_offset_base offset_base,
ead1e424
ILT
2082 bool only_if_ref)
2083{
2084 Sized_symbol<size>* sym;
86f2e683 2085 Sized_symbol<size>* oldsym;
8c500701 2086 bool resolve_oldsym;
ead1e424 2087
8851ecca 2088 if (parameters->target().is_big_endian())
9025d29d
ILT
2089 {
2090#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2ea97941 2091 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
2092 only_if_ref, &oldsym,
2093 &resolve_oldsym);
9025d29d
ILT
2094#else
2095 gold_unreachable();
2096#endif
2097 }
ead1e424 2098 else
9025d29d
ILT
2099 {
2100#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2ea97941 2101 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
2102 only_if_ref, &oldsym,
2103 &resolve_oldsym);
9025d29d
ILT
2104#else
2105 gold_unreachable();
2106#endif
2107 }
ead1e424
ILT
2108
2109 if (sym == NULL)
14b31740 2110 return NULL;
ead1e424 2111
2ea97941 2112 sym->init_output_segment(name, version, os, value, symsize, type, binding,
5146f448
CC
2113 visibility, nonvis, offset_base,
2114 defined == PREDEFINED);
14b31740 2115
e5756efb 2116 if (oldsym == NULL)
55a93433
ILT
2117 {
2118 if (binding == elfcpp::STB_LOCAL
2ea97941 2119 || this->version_script_.symbol_is_local(name))
55a93433 2120 this->force_local(sym);
2ea97941 2121 else if (version != NULL)
75517b77 2122 sym->set_is_default();
55a93433
ILT
2123 return sym;
2124 }
86f2e683 2125
62855347 2126 if (Symbol_table::should_override_with_special(oldsym, type, defined))
e5756efb 2127 this->override_with_special(oldsym, sym);
8c500701
ILT
2128
2129 if (resolve_oldsym)
2130 return sym;
2131 else
2132 {
5417c94d
CC
2133 if (binding == elfcpp::STB_LOCAL
2134 || this->version_script_.symbol_is_local(name))
2135 this->force_local(oldsym);
8c500701
ILT
2136 delete sym;
2137 return oldsym;
2138 }
ead1e424
ILT
2139}
2140
2141// Define a special symbol with a constant value. It is a multiple
2142// definition error if this symbol is already defined.
2143
14b31740 2144Symbol*
2ea97941
ILT
2145Symbol_table::define_as_constant(const char* name,
2146 const char* version,
99fff23b 2147 Defined defined,
2ea97941
ILT
2148 uint64_t value,
2149 uint64_t symsize,
9b07f471
ILT
2150 elfcpp::STT type,
2151 elfcpp::STB binding,
2152 elfcpp::STV visibility,
2153 unsigned char nonvis,
caa9d5d9
ILT
2154 bool only_if_ref,
2155 bool force_override)
ead1e424 2156{
8851ecca 2157 if (parameters->target().get_size() == 32)
86f2e683
ILT
2158 {
2159#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
99fff23b 2160 return this->do_define_as_constant<32>(name, version, defined, value,
2ea97941 2161 symsize, type, binding,
caa9d5d9
ILT
2162 visibility, nonvis, only_if_ref,
2163 force_override);
86f2e683
ILT
2164#else
2165 gold_unreachable();
2166#endif
2167 }
8851ecca 2168 else if (parameters->target().get_size() == 64)
86f2e683
ILT
2169 {
2170#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
99fff23b 2171 return this->do_define_as_constant<64>(name, version, defined, value,
2ea97941 2172 symsize, type, binding,
caa9d5d9
ILT
2173 visibility, nonvis, only_if_ref,
2174 force_override);
86f2e683
ILT
2175#else
2176 gold_unreachable();
2177#endif
2178 }
ead1e424 2179 else
a3ad94ed 2180 gold_unreachable();
ead1e424
ILT
2181}
2182
2183// Define a symbol as a constant, sized version.
2184
2185template<int size>
14b31740 2186Sized_symbol<size>*
ead1e424 2187Symbol_table::do_define_as_constant(
2ea97941
ILT
2188 const char* name,
2189 const char* version,
99fff23b 2190 Defined defined,
2ea97941
ILT
2191 typename elfcpp::Elf_types<size>::Elf_Addr value,
2192 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
ead1e424
ILT
2193 elfcpp::STT type,
2194 elfcpp::STB binding,
2195 elfcpp::STV visibility,
2196 unsigned char nonvis,
caa9d5d9
ILT
2197 bool only_if_ref,
2198 bool force_override)
ead1e424
ILT
2199{
2200 Sized_symbol<size>* sym;
86f2e683 2201 Sized_symbol<size>* oldsym;
8c500701 2202 bool resolve_oldsym;
ead1e424 2203
8851ecca 2204 if (parameters->target().is_big_endian())
9025d29d
ILT
2205 {
2206#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2ea97941 2207 sym = this->define_special_symbol<size, true>(&name, &version,
8c500701
ILT
2208 only_if_ref, &oldsym,
2209 &resolve_oldsym);
9025d29d
ILT
2210#else
2211 gold_unreachable();
2212#endif
2213 }
ead1e424 2214 else
9025d29d
ILT
2215 {
2216#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2ea97941 2217 sym = this->define_special_symbol<size, false>(&name, &version,
8c500701
ILT
2218 only_if_ref, &oldsym,
2219 &resolve_oldsym);
9025d29d
ILT
2220#else
2221 gold_unreachable();
2222#endif
2223 }
ead1e424
ILT
2224
2225 if (sym == NULL)
14b31740 2226 return NULL;
ead1e424 2227
2ea97941 2228 sym->init_constant(name, version, value, symsize, type, binding, visibility,
5146f448 2229 nonvis, defined == PREDEFINED);
14b31740 2230
e5756efb 2231 if (oldsym == NULL)
55a93433 2232 {
686c8caf
ILT
2233 // Version symbols are absolute symbols with name == version.
2234 // We don't want to force them to be local.
2ea97941
ILT
2235 if ((version == NULL
2236 || name != version
2237 || value != 0)
686c8caf 2238 && (binding == elfcpp::STB_LOCAL
2ea97941 2239 || this->version_script_.symbol_is_local(name)))
55a93433 2240 this->force_local(sym);
2ea97941
ILT
2241 else if (version != NULL
2242 && (name != version || value != 0))
75517b77 2243 sym->set_is_default();
55a93433
ILT
2244 return sym;
2245 }
86f2e683 2246
99fff23b 2247 if (force_override
62855347 2248 || Symbol_table::should_override_with_special(oldsym, type, defined))
e5756efb 2249 this->override_with_special(oldsym, sym);
8c500701
ILT
2250
2251 if (resolve_oldsym)
2252 return sym;
2253 else
2254 {
5417c94d
CC
2255 if (binding == elfcpp::STB_LOCAL
2256 || this->version_script_.symbol_is_local(name))
2257 this->force_local(oldsym);
8c500701
ILT
2258 delete sym;
2259 return oldsym;
2260 }
ead1e424
ILT
2261}
2262
2263// Define a set of symbols in output sections.
2264
2265void
9b07f471 2266Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
2267 const Define_symbol_in_section* p,
2268 bool only_if_ref)
ead1e424
ILT
2269{
2270 for (int i = 0; i < count; ++i, ++p)
2271 {
2272 Output_section* os = layout->find_output_section(p->output_section);
2273 if (os != NULL)
99fff23b 2274 this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
14b31740
ILT
2275 p->size, p->type, p->binding,
2276 p->visibility, p->nonvis,
a445fddf
ILT
2277 p->offset_is_from_end,
2278 only_if_ref || p->only_if_ref);
ead1e424 2279 else
99fff23b
ILT
2280 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2281 p->type, p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
2282 only_if_ref || p->only_if_ref,
2283 false);
ead1e424
ILT
2284 }
2285}
2286
2287// Define a set of symbols in output segments.
2288
2289void
9b07f471 2290Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
2291 const Define_symbol_in_segment* p,
2292 bool only_if_ref)
ead1e424
ILT
2293{
2294 for (int i = 0; i < count; ++i, ++p)
2295 {
2296 Output_segment* os = layout->find_output_segment(p->segment_type,
2297 p->segment_flags_set,
2298 p->segment_flags_clear);
2299 if (os != NULL)
99fff23b 2300 this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
14b31740
ILT
2301 p->size, p->type, p->binding,
2302 p->visibility, p->nonvis,
a445fddf
ILT
2303 p->offset_base,
2304 only_if_ref || p->only_if_ref);
ead1e424 2305 else
99fff23b
ILT
2306 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2307 p->type, p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
2308 only_if_ref || p->only_if_ref,
2309 false);
ead1e424
ILT
2310 }
2311}
2312
46fe1623
ILT
2313// Define CSYM using a COPY reloc. POSD is the Output_data where the
2314// symbol should be defined--typically a .dyn.bss section. VALUE is
2315// the offset within POSD.
2316
2317template<int size>
2318void
fe8718a4 2319Symbol_table::define_with_copy_reloc(
fe8718a4
ILT
2320 Sized_symbol<size>* csym,
2321 Output_data* posd,
2ea97941 2322 typename elfcpp::Elf_types<size>::Elf_Addr value)
46fe1623
ILT
2323{
2324 gold_assert(csym->is_from_dynobj());
2325 gold_assert(!csym->is_copied_from_dynobj());
2ea97941
ILT
2326 Object* object = csym->object();
2327 gold_assert(object->is_dynamic());
2328 Dynobj* dynobj = static_cast<Dynobj*>(object);
46fe1623
ILT
2329
2330 // Our copied variable has to override any variable in a shared
2331 // library.
2332 elfcpp::STB binding = csym->binding();
2333 if (binding == elfcpp::STB_WEAK)
2334 binding = elfcpp::STB_GLOBAL;
2335
99fff23b 2336 this->define_in_output_data(csym->name(), csym->version(), COPY,
2ea97941 2337 posd, value, csym->symsize(),
46fe1623
ILT
2338 csym->type(), binding,
2339 csym->visibility(), csym->nonvis(),
2340 false, false);
2341
2342 csym->set_is_copied_from_dynobj();
2343 csym->set_needs_dynsym_entry();
2344
2345 this->copied_symbol_dynobjs_[csym] = dynobj;
2346
2347 // We have now defined all aliases, but we have not entered them all
2348 // in the copied_symbol_dynobjs_ map.
2349 if (csym->has_alias())
2350 {
2351 Symbol* sym = csym;
2352 while (true)
2353 {
2354 sym = this->weak_aliases_[sym];
2355 if (sym == csym)
2356 break;
2357 gold_assert(sym->output_data() == posd);
2358
2359 sym->set_is_copied_from_dynobj();
2360 this->copied_symbol_dynobjs_[sym] = dynobj;
2361 }
2362 }
2363}
2364
2365// SYM is defined using a COPY reloc. Return the dynamic object where
2366// the original definition was found.
2367
2368Dynobj*
2369Symbol_table::get_copy_source(const Symbol* sym) const
2370{
2371 gold_assert(sym->is_copied_from_dynobj());
2372 Copied_symbol_dynobjs::const_iterator p =
2373 this->copied_symbol_dynobjs_.find(sym);
2374 gold_assert(p != this->copied_symbol_dynobjs_.end());
2375 return p->second;
2376}
2377
f3e9c5c5
ILT
2378// Add any undefined symbols named on the command line.
2379
2380void
88a4108b 2381Symbol_table::add_undefined_symbols_from_command_line(Layout* layout)
f3e9c5c5 2382{
88a4108b
ILT
2383 if (parameters->options().any_undefined()
2384 || layout->script_options()->any_unreferenced())
f3e9c5c5
ILT
2385 {
2386 if (parameters->target().get_size() == 32)
2387 {
5adf9721 2388#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
88a4108b 2389 this->do_add_undefined_symbols_from_command_line<32>(layout);
f3e9c5c5
ILT
2390#else
2391 gold_unreachable();
2392#endif
2393 }
2394 else if (parameters->target().get_size() == 64)
2395 {
2396#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
88a4108b 2397 this->do_add_undefined_symbols_from_command_line<64>(layout);
f3e9c5c5
ILT
2398#else
2399 gold_unreachable();
2400#endif
2401 }
2402 else
2403 gold_unreachable();
2404 }
2405}
2406
2407template<int size>
2408void
88a4108b 2409Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout)
f3e9c5c5
ILT
2410{
2411 for (options::String_set::const_iterator p =
2412 parameters->options().undefined_begin();
2413 p != parameters->options().undefined_end();
2414 ++p)
88a4108b 2415 this->add_undefined_symbol_from_command_line<size>(p->c_str());
f3e9c5c5 2416
31821be0
CC
2417 for (options::String_set::const_iterator p =
2418 parameters->options().export_dynamic_symbol_begin();
2419 p != parameters->options().export_dynamic_symbol_end();
2420 ++p)
2421 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2422
88a4108b
ILT
2423 for (Script_options::referenced_const_iterator p =
2424 layout->script_options()->referenced_begin();
2425 p != layout->script_options()->referenced_end();
2426 ++p)
2427 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2428}
2429
2430template<int size>
2431void
2432Symbol_table::add_undefined_symbol_from_command_line(const char* name)
2433{
2434 if (this->lookup(name) != NULL)
2435 return;
f3e9c5c5 2436
88a4108b 2437 const char* version = NULL;
f3e9c5c5 2438
88a4108b
ILT
2439 Sized_symbol<size>* sym;
2440 Sized_symbol<size>* oldsym;
2441 bool resolve_oldsym;
2442 if (parameters->target().is_big_endian())
2443 {
f3e9c5c5 2444#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
88a4108b
ILT
2445 sym = this->define_special_symbol<size, true>(&name, &version,
2446 false, &oldsym,
2447 &resolve_oldsym);
f3e9c5c5 2448#else
88a4108b 2449 gold_unreachable();
f3e9c5c5 2450#endif
88a4108b
ILT
2451 }
2452 else
2453 {
f3e9c5c5 2454#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
88a4108b
ILT
2455 sym = this->define_special_symbol<size, false>(&name, &version,
2456 false, &oldsym,
2457 &resolve_oldsym);
f3e9c5c5 2458#else
88a4108b 2459 gold_unreachable();
f3e9c5c5 2460#endif
88a4108b 2461 }
f3e9c5c5 2462
88a4108b 2463 gold_assert(oldsym == NULL);
f3e9c5c5 2464
dc1c8a16 2465 sym->init_undefined(name, version, 0, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
88a4108b
ILT
2466 elfcpp::STV_DEFAULT, 0);
2467 ++this->saw_undefined_;
f3e9c5c5
ILT
2468}
2469
a3ad94ed
ILT
2470// Set the dynamic symbol indexes. INDEX is the index of the first
2471// global dynamic symbol. Pointers to the symbols are stored into the
2472// vector SYMS. The names are added to DYNPOOL. This returns an
2473// updated dynamic symbol index.
2474
2475unsigned int
9b07f471 2476Symbol_table::set_dynsym_indexes(unsigned int index,
a3ad94ed 2477 std::vector<Symbol*>* syms,
14b31740
ILT
2478 Stringpool* dynpool,
2479 Versions* versions)
a3ad94ed 2480{
32e2b61d
AM
2481 std::vector<Symbol*> as_needed_sym;
2482
98ff9231
CC
2483 // Allow a target to set dynsym indexes.
2484 if (parameters->target().has_custom_set_dynsym_indexes())
2485 {
2486 std::vector<Symbol*> dyn_symbols;
2487 for (Symbol_table_type::iterator p = this->table_.begin();
2488 p != this->table_.end();
2489 ++p)
2490 {
2491 Symbol* sym = p->second;
2492 if (!sym->should_add_dynsym_entry(this))
2493 sym->set_dynsym_index(-1U);
2494 else
2495 dyn_symbols.push_back(sym);
2496 }
2497
2498 return parameters->target().set_dynsym_indexes(&dyn_symbols, index, syms,
2499 dynpool, versions, this);
2500 }
2501
a3ad94ed
ILT
2502 for (Symbol_table_type::iterator p = this->table_.begin();
2503 p != this->table_.end();
2504 ++p)
2505 {
2506 Symbol* sym = p->second;
16649710
ILT
2507
2508 // Note that SYM may already have a dynamic symbol index, since
2509 // some symbols appear more than once in the symbol table, with
2510 // and without a version.
2511
ce97fa81 2512 if (!sym->should_add_dynsym_entry(this))
16649710
ILT
2513 sym->set_dynsym_index(-1U);
2514 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
2515 {
2516 sym->set_dynsym_index(index);
2517 ++index;
2518 syms->push_back(sym);
cfd73a4e 2519 dynpool->add(sym->name(), false, NULL);
14b31740 2520
594c8e5e 2521 // If the symbol is defined in a dynamic object and is
32e2b61d
AM
2522 // referenced strongly in a regular object, then mark the
2523 // dynamic object as needed. This is used to implement
2524 // --as-needed.
2525 if (sym->is_from_dynobj()
2526 && sym->in_reg()
2527 && !sym->is_undef_binding_weak())
594c8e5e 2528 sym->object()->set_is_needed();
32e2b61d
AM
2529
2530 // Record any version information, except those from
2531 // as-needed libraries not seen to be needed. Note that the
2532 // is_needed state for such libraries can change in this loop.
2533 if (sym->version() != NULL)
2534 {
2535 if (!sym->is_from_dynobj()
2536 || !sym->object()->as_needed()
2537 || sym->object()->is_needed())
2538 versions->record_version(this, dynpool, sym);
2539 else
2540 as_needed_sym.push_back(sym);
2541 }
a3ad94ed
ILT
2542 }
2543 }
2544
32e2b61d
AM
2545 // Process version information for symbols from as-needed libraries.
2546 for (std::vector<Symbol*>::iterator p = as_needed_sym.begin();
2547 p != as_needed_sym.end();
2548 ++p)
2549 {
2550 Symbol* sym = *p;
2551
2552 if (sym->object()->is_needed())
2553 versions->record_version(this, dynpool, sym);
2554 else
2555 sym->clear_version();
2556 }
2557
14b31740
ILT
2558 // Finish up the versions. In some cases this may add new dynamic
2559 // symbols.
9b07f471 2560 index = versions->finalize(this, index, syms);
14b31740 2561
dc1c8a16
CC
2562 // Process target-specific symbols.
2563 for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
2564 p != this->target_symbols_.end();
2565 ++p)
2566 {
2567 (*p)->set_dynsym_index(index);
2568 ++index;
2569 syms->push_back(*p);
2570 dynpool->add((*p)->name(), false, NULL);
2571 }
2572
a3ad94ed
ILT
2573 return index;
2574}
2575
c06b7b0b 2576// Set the final values for all the symbols. The index of the first
55a93433
ILT
2577// global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2578// file offset OFF. Add their names to POOL. Return the new file
2579// offset. Update *PLOCAL_SYMCOUNT if necessary.
54dc6425 2580
75f65a3e 2581off_t
55a93433
ILT
2582Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2583 size_t dyncount, Stringpool* pool,
ca09d69a 2584 unsigned int* plocal_symcount)
54dc6425 2585{
f6ce93d6
ILT
2586 off_t ret;
2587
55a93433
ILT
2588 gold_assert(*plocal_symcount != 0);
2589 this->first_global_index_ = *plocal_symcount;
c06b7b0b 2590
16649710
ILT
2591 this->dynamic_offset_ = dynoff;
2592 this->first_dynamic_global_index_ = dyn_global_index;
2593 this->dynamic_count_ = dyncount;
2594
8851ecca 2595 if (parameters->target().get_size() == 32)
9025d29d
ILT
2596 {
2597#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
55a93433 2598 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
9025d29d
ILT
2599#else
2600 gold_unreachable();
2601#endif
2602 }
8851ecca 2603 else if (parameters->target().get_size() == 64)
9025d29d
ILT
2604 {
2605#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
55a93433 2606 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
9025d29d
ILT
2607#else
2608 gold_unreachable();
2609#endif
2610 }
61ba1cf9 2611 else
a3ad94ed 2612 gold_unreachable();
f6ce93d6
ILT
2613
2614 // Now that we have the final symbol table, we can reliably note
2615 // which symbols should get warnings.
cb295612 2616 this->warnings_.note_warnings(this);
f6ce93d6
ILT
2617
2618 return ret;
75f65a3e
ILT
2619}
2620
55a93433
ILT
2621// SYM is going into the symbol table at *PINDEX. Add the name to
2622// POOL, update *PINDEX and *POFF.
2623
2624template<int size>
2625void
2626Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2627 unsigned int* pindex, off_t* poff)
2628{
2629 sym->set_symtab_index(*pindex);
6d1c4efb
ILT
2630 if (sym->version() == NULL || !parameters->options().relocatable())
2631 pool->add(sym->name(), false, NULL);
2632 else
2633 pool->add(sym->versioned_name(), true, NULL);
55a93433
ILT
2634 ++*pindex;
2635 *poff += elfcpp::Elf_sizes<size>::sym_size;
2636}
2637
ead1e424
ILT
2638// Set the final value for all the symbols. This is called after
2639// Layout::finalize, so all the output sections have their final
2640// address.
75f65a3e
ILT
2641
2642template<int size>
2643off_t
55a93433
ILT
2644Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2645 unsigned int* plocal_symcount)
75f65a3e 2646{
ead1e424 2647 off = align_address(off, size >> 3);
75f65a3e
ILT
2648 this->offset_ = off;
2649
55a93433
ILT
2650 unsigned int index = *plocal_symcount;
2651 const unsigned int orig_index = index;
c06b7b0b 2652
55a93433
ILT
2653 // First do all the symbols which have been forced to be local, as
2654 // they must appear before all global symbols.
2655 for (Forced_locals::iterator p = this->forced_locals_.begin();
2656 p != this->forced_locals_.end();
2657 ++p)
2658 {
2659 Symbol* sym = *p;
2660 gold_assert(sym->is_forced_local());
2661 if (this->sized_finalize_symbol<size>(sym))
2662 {
2663 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2664 ++*plocal_symcount;
2665 }
2666 }
2667
2668 // Now do all the remaining symbols.
c06b7b0b
ILT
2669 for (Symbol_table_type::iterator p = this->table_.begin();
2670 p != this->table_.end();
2671 ++p)
54dc6425 2672 {
55a93433
ILT
2673 Symbol* sym = p->second;
2674 if (this->sized_finalize_symbol<size>(sym))
2675 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2676 }
54dc6425 2677
dc1c8a16
CC
2678 // Now do target-specific symbols.
2679 for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
2680 p != this->target_symbols_.end();
2681 ++p)
2682 {
2683 this->add_to_final_symtab<size>(*p, pool, &index, &off);
2684 }
2685
55a93433 2686 this->output_count_ = index - orig_index;
a3ad94ed 2687
55a93433
ILT
2688 return off;
2689}
75f65a3e 2690
c0a62865
DK
2691// Compute the final value of SYM and store status in location PSTATUS.
2692// During relaxation, this may be called multiple times for a symbol to
2693// compute its would-be final value in each relaxation pass.
008db82e 2694
55a93433 2695template<int size>
c0a62865
DK
2696typename Sized_symbol<size>::Value_type
2697Symbol_table::compute_final_value(
2698 const Sized_symbol<size>* sym,
2699 Compute_final_value_status* pstatus) const
55a93433 2700{
ef9beddf 2701 typedef typename Sized_symbol<size>::Value_type Value_type;
2ea97941 2702 Value_type value;
ead1e424 2703
55a93433
ILT
2704 switch (sym->source())
2705 {
2706 case Symbol::FROM_OBJECT:
2707 {
d491d34e 2708 bool is_ordinary;
2ea97941 2709 unsigned int shndx = sym->shndx(&is_ordinary);
ead1e424 2710
d491d34e 2711 if (!is_ordinary
2ea97941
ILT
2712 && shndx != elfcpp::SHN_ABS
2713 && !Symbol::is_common_shndx(shndx))
55a93433 2714 {
c0a62865
DK
2715 *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2716 return 0;
ead1e424 2717 }
ead1e424 2718
55a93433
ILT
2719 Object* symobj = sym->object();
2720 if (symobj->is_dynamic())
ead1e424 2721 {
2ea97941
ILT
2722 value = 0;
2723 shndx = elfcpp::SHN_UNDEF;
ead1e424 2724 }
89fc3421
CC
2725 else if (symobj->pluginobj() != NULL)
2726 {
2ea97941
ILT
2727 value = 0;
2728 shndx = elfcpp::SHN_UNDEF;
89fc3421 2729 }
2ea97941
ILT
2730 else if (shndx == elfcpp::SHN_UNDEF)
2731 value = 0;
d491d34e 2732 else if (!is_ordinary
2ea97941
ILT
2733 && (shndx == elfcpp::SHN_ABS
2734 || Symbol::is_common_shndx(shndx)))
2735 value = sym->value();
55a93433 2736 else
ead1e424 2737 {
55a93433 2738 Relobj* relobj = static_cast<Relobj*>(symobj);
2ea97941 2739 Output_section* os = relobj->output_section(shndx);
55a93433 2740
2ea97941 2741 if (this->is_section_folded(relobj, shndx))
ef15dade
ST
2742 {
2743 gold_assert(os == NULL);
2744 // Get the os of the section it is folded onto.
2745 Section_id folded = this->icf_->get_folded_section(relobj,
2ea97941 2746 shndx);
ef15dade
ST
2747 gold_assert(folded.first != NULL);
2748 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
d6344fb5
DK
2749 unsigned folded_shndx = folded.second;
2750
2751 os = folded_obj->output_section(folded_shndx);
ef15dade 2752 gold_assert(os != NULL);
d6344fb5
DK
2753
2754 // Replace (relobj, shndx) with canonical ICF input section.
2755 shndx = folded_shndx;
2756 relobj = folded_obj;
ef15dade
ST
2757 }
2758
d6344fb5 2759 uint64_t secoff64 = relobj->output_section_offset(shndx);
ef15dade 2760 if (os == NULL)
ead1e424 2761 {
6d03d481
ST
2762 bool static_or_reloc = (parameters->doing_static_link() ||
2763 parameters->options().relocatable());
2764 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2765
c0a62865
DK
2766 *pstatus = CFVS_NO_OUTPUT_SECTION;
2767 return 0;
ead1e424 2768 }
55a93433 2769
eff45813
CC
2770 if (secoff64 == -1ULL)
2771 {
2772 // The section needs special handling (e.g., a merge section).
ef15dade 2773
2ea97941 2774 value = os->output_address(relobj, shndx, sym->value());
eff45813
CC
2775 }
2776 else
2777 {
2778 Value_type secoff =
2779 convert_types<Value_type, uint64_t>(secoff64);
2780 if (sym->type() == elfcpp::STT_TLS)
2ea97941 2781 value = sym->value() + os->tls_offset() + secoff;
eff45813 2782 else
2ea97941 2783 value = sym->value() + os->address() + secoff;
eff45813 2784 }
ead1e424 2785 }
55a93433
ILT
2786 }
2787 break;
2788
2789 case Symbol::IN_OUTPUT_DATA:
2790 {
2791 Output_data* od = sym->output_data();
2ea97941 2792 value = sym->value();
155a0dd7 2793 if (sym->type() != elfcpp::STT_TLS)
2ea97941 2794 value += od->address();
155a0dd7
ILT
2795 else
2796 {
2797 Output_section* os = od->output_section();
2798 gold_assert(os != NULL);
2ea97941 2799 value += os->tls_offset() + (od->address() - os->address());
155a0dd7 2800 }
55a93433 2801 if (sym->offset_is_from_end())
2ea97941 2802 value += od->data_size();
55a93433
ILT
2803 }
2804 break;
2805
2806 case Symbol::IN_OUTPUT_SEGMENT:
2807 {
2808 Output_segment* os = sym->output_segment();
2ea97941 2809 value = sym->value();
edfbb029 2810 if (sym->type() != elfcpp::STT_TLS)
2ea97941 2811 value += os->vaddr();
55a93433
ILT
2812 switch (sym->offset_base())
2813 {
2814 case Symbol::SEGMENT_START:
2815 break;
2816 case Symbol::SEGMENT_END:
2ea97941 2817 value += os->memsz();
55a93433
ILT
2818 break;
2819 case Symbol::SEGMENT_BSS:
2ea97941 2820 value += os->filesz();
55a93433
ILT
2821 break;
2822 default:
2823 gold_unreachable();
2824 }
2825 }
2826 break;
ead1e424 2827
f3e9c5c5 2828 case Symbol::IS_CONSTANT:
2ea97941 2829 value = sym->value();
55a93433 2830 break;
ead1e424 2831
f3e9c5c5 2832 case Symbol::IS_UNDEFINED:
2ea97941 2833 value = 0;
f3e9c5c5
ILT
2834 break;
2835
55a93433
ILT
2836 default:
2837 gold_unreachable();
2838 }
ead1e424 2839
c0a62865 2840 *pstatus = CFVS_OK;
2ea97941 2841 return value;
c0a62865
DK
2842}
2843
2844// Finalize the symbol SYM. This returns true if the symbol should be
2845// added to the symbol table, false otherwise.
2846
2847template<int size>
2848bool
2849Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2850{
2851 typedef typename Sized_symbol<size>::Value_type Value_type;
2852
2853 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2854
2855 // The default version of a symbol may appear twice in the symbol
2856 // table. We only need to finalize it once.
2857 if (sym->has_symtab_index())
2858 return false;
2859
2860 if (!sym->in_reg())
2861 {
2862 gold_assert(!sym->has_symtab_index());
2863 sym->set_symtab_index(-1U);
2864 gold_assert(sym->dynsym_index() == -1U);
2865 return false;
2866 }
2867
badc8139
RÁE
2868 // If the symbol is only present on plugin files, the plugin decided we
2869 // don't need it.
2870 if (!sym->in_real_elf())
2871 {
2872 gold_assert(!sym->has_symtab_index());
2873 sym->set_symtab_index(-1U);
2874 return false;
2875 }
2876
c0a62865
DK
2877 // Compute final symbol value.
2878 Compute_final_value_status status;
2ea97941 2879 Value_type value = this->compute_final_value(sym, &status);
c0a62865
DK
2880
2881 switch (status)
2882 {
2883 case CFVS_OK:
2884 break;
2885 case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2886 {
2887 bool is_ordinary;
2ea97941 2888 unsigned int shndx = sym->shndx(&is_ordinary);
c0a62865 2889 gold_error(_("%s: unsupported symbol section 0x%x"),
2ea97941 2890 sym->demangled_name().c_str(), shndx);
c0a62865
DK
2891 }
2892 break;
2893 case CFVS_NO_OUTPUT_SECTION:
2894 sym->set_symtab_index(-1U);
2895 return false;
2896 default:
2897 gold_unreachable();
2898 }
2899
2ea97941 2900 sym->set_value(value);
9e2dcb77 2901
8c604651
CS
2902 if (parameters->options().strip_all()
2903 || !parameters->options().should_retain_symbol(sym->name()))
55a93433
ILT
2904 {
2905 sym->set_symtab_index(-1U);
2906 return false;
54dc6425 2907 }
75f65a3e 2908
55a93433 2909 return true;
54dc6425
ILT
2910}
2911
61ba1cf9
ILT
2912// Write out the global symbols.
2913
2914void
fd9d194f 2915Symbol_table::write_globals(const Stringpool* sympool,
d491d34e
ILT
2916 const Stringpool* dynpool,
2917 Output_symtab_xindex* symtab_xindex,
2918 Output_symtab_xindex* dynsym_xindex,
2919 Output_file* of) const
61ba1cf9 2920{
8851ecca 2921 switch (parameters->size_and_endianness())
61ba1cf9 2922 {
9025d29d 2923#ifdef HAVE_TARGET_32_LITTLE
8851ecca 2924 case Parameters::TARGET_32_LITTLE:
fd9d194f 2925 this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
d491d34e 2926 dynsym_xindex, of);
8851ecca 2927 break;
9025d29d 2928#endif
8851ecca
ILT
2929#ifdef HAVE_TARGET_32_BIG
2930 case Parameters::TARGET_32_BIG:
fd9d194f 2931 this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
d491d34e 2932 dynsym_xindex, of);
8851ecca 2933 break;
9025d29d 2934#endif
9025d29d 2935#ifdef HAVE_TARGET_64_LITTLE
8851ecca 2936 case Parameters::TARGET_64_LITTLE:
fd9d194f 2937 this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
d491d34e 2938 dynsym_xindex, of);
8851ecca 2939 break;
9025d29d 2940#endif
8851ecca
ILT
2941#ifdef HAVE_TARGET_64_BIG
2942 case Parameters::TARGET_64_BIG:
fd9d194f 2943 this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
d491d34e 2944 dynsym_xindex, of);
8851ecca
ILT
2945 break;
2946#endif
2947 default:
2948 gold_unreachable();
61ba1cf9 2949 }
61ba1cf9
ILT
2950}
2951
2952// Write out the global symbols.
2953
2954template<int size, bool big_endian>
2955void
fd9d194f 2956Symbol_table::sized_write_globals(const Stringpool* sympool,
16649710 2957 const Stringpool* dynpool,
d491d34e
ILT
2958 Output_symtab_xindex* symtab_xindex,
2959 Output_symtab_xindex* dynsym_xindex,
61ba1cf9
ILT
2960 Output_file* of) const
2961{
8851ecca 2962 const Target& target = parameters->target();
9a2d6984 2963
61ba1cf9 2964 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
55a93433
ILT
2965
2966 const unsigned int output_count = this->output_count_;
2967 const section_size_type oview_size = output_count * sym_size;
2968 const unsigned int first_global_index = this->first_global_index_;
5fe2a0f5
ILT
2969 unsigned char* psyms;
2970 if (this->offset_ == 0 || output_count == 0)
2971 psyms = NULL;
2972 else
2973 psyms = of->get_output_view(this->offset_, oview_size);
16649710 2974
55a93433
ILT
2975 const unsigned int dynamic_count = this->dynamic_count_;
2976 const section_size_type dynamic_size = dynamic_count * sym_size;
2977 const unsigned int first_dynamic_global_index =
2978 this->first_dynamic_global_index_;
16649710 2979 unsigned char* dynamic_view;
5fe2a0f5 2980 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
16649710
ILT
2981 dynamic_view = NULL;
2982 else
2983 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 2984
61ba1cf9
ILT
2985 for (Symbol_table_type::const_iterator p = this->table_.begin();
2986 p != this->table_.end();
2987 ++p)
2988 {
2989 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2990
9a2d6984 2991 // Possibly warn about unresolved symbols in shared libraries.
fd9d194f 2992 this->warn_about_undefined_dynobj_symbol(sym);
e2827e5f 2993
a3ad94ed 2994 unsigned int sym_index = sym->symtab_index();
16649710
ILT
2995 unsigned int dynsym_index;
2996 if (dynamic_view == NULL)
2997 dynsym_index = -1U;
2998 else
2999 dynsym_index = sym->dynsym_index();
3000
3001 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
3002 {
3003 // This symbol is not included in the output file.
3004 continue;
3005 }
16649710 3006
2ea97941 3007 unsigned int shndx;
88dd47ac
ILT
3008 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
3009 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
ce279a62 3010 elfcpp::STB binding = sym->binding();
9634ed06 3011
a100d66f
ST
3012 // If --weak-unresolved-symbols is set, change binding of unresolved
3013 // global symbols to STB_WEAK.
3014 if (parameters->options().weak_unresolved_symbols()
3015 && binding == elfcpp::STB_GLOBAL
3016 && sym->is_undefined())
3017 binding = elfcpp::STB_WEAK;
3018
9634ed06
CC
3019 // If --no-gnu-unique is set, change STB_GNU_UNIQUE to STB_GLOBAL.
3020 if (binding == elfcpp::STB_GNU_UNIQUE
3021 && !parameters->options().gnu_unique())
3022 binding = elfcpp::STB_GLOBAL;
3023
ead1e424
ILT
3024 switch (sym->source())
3025 {
3026 case Symbol::FROM_OBJECT:
3027 {
d491d34e
ILT
3028 bool is_ordinary;
3029 unsigned int in_shndx = sym->shndx(&is_ordinary);
ead1e424 3030
d491d34e 3031 if (!is_ordinary
0dfbdef4 3032 && in_shndx != elfcpp::SHN_ABS
8a5e3e08 3033 && !Symbol::is_common_shndx(in_shndx))
ead1e424 3034 {
75f2446e 3035 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 3036 sym->demangled_name().c_str(), in_shndx);
2ea97941 3037 shndx = in_shndx;
f6ce93d6 3038 }
ead1e424
ILT
3039 else
3040 {
75f2446e
ILT
3041 Object* symobj = sym->object();
3042 if (symobj->is_dynamic())
3043 {
3044 if (sym->needs_dynsym_value())
8851ecca 3045 dynsym_value = target.dynsym_value(sym);
2ea97941 3046 shndx = elfcpp::SHN_UNDEF;
ce279a62
CC
3047 if (sym->is_undef_binding_weak())
3048 binding = elfcpp::STB_WEAK;
74f67560
DK
3049 else
3050 binding = elfcpp::STB_GLOBAL;
75f2446e 3051 }
89fc3421 3052 else if (symobj->pluginobj() != NULL)
2ea97941 3053 shndx = elfcpp::SHN_UNDEF;
75f2446e 3054 else if (in_shndx == elfcpp::SHN_UNDEF
d491d34e
ILT
3055 || (!is_ordinary
3056 && (in_shndx == elfcpp::SHN_ABS
8a5e3e08 3057 || Symbol::is_common_shndx(in_shndx))))
2ea97941 3058 shndx = in_shndx;
75f2446e
ILT
3059 else
3060 {
3061 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 3062 Output_section* os = relobj->output_section(in_shndx);
ef15dade
ST
3063 if (this->is_section_folded(relobj, in_shndx))
3064 {
3065 // This global symbol must be written out even though
3066 // it is folded.
3067 // Get the os of the section it is folded onto.
3068 Section_id folded =
3069 this->icf_->get_folded_section(relobj, in_shndx);
3070 gold_assert(folded.first !=NULL);
3071 Relobj* folded_obj =
3072 reinterpret_cast<Relobj*>(folded.first);
3073 os = folded_obj->output_section(folded.second);
3074 gold_assert(os != NULL);
3075 }
75f2446e 3076 gold_assert(os != NULL);
2ea97941 3077 shndx = os->out_shndx();
88dd47ac 3078
2ea97941 3079 if (shndx >= elfcpp::SHN_LORESERVE)
d491d34e
ILT
3080 {
3081 if (sym_index != -1U)
2ea97941 3082 symtab_xindex->add(sym_index, shndx);
d491d34e 3083 if (dynsym_index != -1U)
2ea97941
ILT
3084 dynsym_xindex->add(dynsym_index, shndx);
3085 shndx = elfcpp::SHN_XINDEX;
d491d34e
ILT
3086 }
3087
88dd47ac
ILT
3088 // In object files symbol values are section
3089 // relative.
8851ecca 3090 if (parameters->options().relocatable())
88dd47ac 3091 sym_value -= os->address();
75f2446e 3092 }
ead1e424
ILT
3093 }
3094 }
3095 break;
3096
3097 case Symbol::IN_OUTPUT_DATA:
502e8a84
CC
3098 {
3099 Output_data* od = sym->output_data();
3100
3101 shndx = od->out_shndx();
3102 if (shndx >= elfcpp::SHN_LORESERVE)
3103 {
3104 if (sym_index != -1U)
3105 symtab_xindex->add(sym_index, shndx);
3106 if (dynsym_index != -1U)
3107 dynsym_xindex->add(dynsym_index, shndx);
3108 shndx = elfcpp::SHN_XINDEX;
3109 }
3110
3111 // In object files symbol values are section
3112 // relative.
3113 if (parameters->options().relocatable())
89ede9f5
CC
3114 {
3115 Output_section* os = od->output_section();
3116 gold_assert(os != NULL);
3117 sym_value -= os->address();
3118 }
502e8a84 3119 }
ead1e424
ILT
3120 break;
3121
3122 case Symbol::IN_OUTPUT_SEGMENT:
eb390844
CC
3123 {
3124 Output_segment* oseg = sym->output_segment();
3125 Output_section* osect = oseg->first_section();
3126 if (osect == NULL)
3127 shndx = elfcpp::SHN_ABS;
3128 else
3129 shndx = osect->out_shndx();
3130 }
ead1e424
ILT
3131 break;
3132
f3e9c5c5 3133 case Symbol::IS_CONSTANT:
2ea97941 3134 shndx = elfcpp::SHN_ABS;
ead1e424
ILT
3135 break;
3136
f3e9c5c5 3137 case Symbol::IS_UNDEFINED:
2ea97941 3138 shndx = elfcpp::SHN_UNDEF;
f3e9c5c5
ILT
3139 break;
3140
ead1e424 3141 default:
a3ad94ed 3142 gold_unreachable();
ead1e424 3143 }
61ba1cf9 3144
16649710
ILT
3145 if (sym_index != -1U)
3146 {
55a93433
ILT
3147 sym_index -= first_global_index;
3148 gold_assert(sym_index < output_count);
3149 unsigned char* ps = psyms + (sym_index * sym_size);
2ea97941 3150 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
ce279a62 3151 binding, sympool, ps);
16649710 3152 }
61ba1cf9 3153
16649710
ILT
3154 if (dynsym_index != -1U)
3155 {
3156 dynsym_index -= first_dynamic_global_index;
3157 gold_assert(dynsym_index < dynamic_count);
3158 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2ea97941 3159 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
ce279a62 3160 binding, dynpool, pd);
800d9823
CC
3161 // Allow a target to adjust dynamic symbol value.
3162 parameters->target().adjust_dyn_symbol(sym, pd);
16649710 3163 }
61ba1cf9
ILT
3164 }
3165
dc1c8a16
CC
3166 // Write the target-specific symbols.
3167 for (std::vector<Symbol*>::const_iterator p = this->target_symbols_.begin();
3168 p != this->target_symbols_.end();
3169 ++p)
3170 {
3171 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(*p);
3172
3173 unsigned int sym_index = sym->symtab_index();
3174 unsigned int dynsym_index;
3175 if (dynamic_view == NULL)
3176 dynsym_index = -1U;
3177 else
3178 dynsym_index = sym->dynsym_index();
3179
3180 unsigned int shndx;
3181 switch (sym->source())
3182 {
3183 case Symbol::IS_CONSTANT:
3184 shndx = elfcpp::SHN_ABS;
3185 break;
3186 case Symbol::IS_UNDEFINED:
3187 shndx = elfcpp::SHN_UNDEF;
3188 break;
3189 default:
3190 gold_unreachable();
3191 }
3192
3193 if (sym_index != -1U)
3194 {
3195 sym_index -= first_global_index;
3196 gold_assert(sym_index < output_count);
3197 unsigned char* ps = psyms + (sym_index * sym_size);
3198 this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
3199 sym->binding(), sympool,
3200 ps);
3201 }
3202
3203 if (dynsym_index != -1U)
3204 {
3205 dynsym_index -= first_dynamic_global_index;
3206 gold_assert(dynsym_index < dynamic_count);
3207 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
3208 this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
3209 sym->binding(), dynpool,
3210 pd);
3211 }
3212 }
3213
c06b7b0b 3214 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
3215 if (dynamic_view != NULL)
3216 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
3217}
3218
3219// Write out the symbol SYM, in section SHNDX, to P. POOL is the
3220// strtab holding the name.
3221
3222template<int size, bool big_endian>
3223void
ab5c9e90
ILT
3224Symbol_table::sized_write_symbol(
3225 Sized_symbol<size>* sym,
2ea97941
ILT
3226 typename elfcpp::Elf_types<size>::Elf_Addr value,
3227 unsigned int shndx,
ce279a62 3228 elfcpp::STB binding,
ab5c9e90 3229 const Stringpool* pool,
7d1a9ebb 3230 unsigned char* p) const
16649710
ILT
3231{
3232 elfcpp::Sym_write<size, big_endian> osym(p);
6d1c4efb
ILT
3233 if (sym->version() == NULL || !parameters->options().relocatable())
3234 osym.put_st_name(pool->get_offset(sym->name()));
3235 else
3236 osym.put_st_name(pool->get_offset(sym->versioned_name()));
2ea97941 3237 osym.put_st_value(value);
58e54ac2 3238 // Use a symbol size of zero for undefined symbols from shared libraries.
2ea97941 3239 if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
58e54ac2
CD
3240 osym.put_st_size(0);
3241 else
3242 osym.put_st_size(sym->symsize());
53d7974c 3243 elfcpp::STT type = sym->type();
358de988 3244 gold_assert(type != elfcpp::STT_GNU_IFUNC || !sym->is_from_dynobj());
55a93433
ILT
3245 // A version script may have overridden the default binding.
3246 if (sym->is_forced_local())
53d7974c 3247 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
55a93433 3248 else
ce279a62 3249 osym.put_st_info(elfcpp::elf_st_info(binding, type));
16649710 3250 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2ea97941 3251 osym.put_st_shndx(shndx);
61ba1cf9
ILT
3252}
3253
9a2d6984
ILT
3254// Check for unresolved symbols in shared libraries. This is
3255// controlled by the --allow-shlib-undefined option.
3256
3257// We only warn about libraries for which we have seen all the
3258// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
3259// which were not seen in this link. If we didn't see a DT_NEEDED
3260// entry, we aren't going to be able to reliably report whether the
3261// symbol is undefined.
3262
fd9d194f
ILT
3263// We also don't warn about libraries found in a system library
3264// directory (e.g., /lib or /usr/lib); we assume that those libraries
3265// are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
3266// can have undefined references satisfied by ld-linux.so.
9a2d6984
ILT
3267
3268inline void
fd9d194f 3269Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
9a2d6984 3270{
d491d34e 3271 bool dummy;
9a2d6984
ILT
3272 if (sym->source() == Symbol::FROM_OBJECT
3273 && sym->object()->is_dynamic()
d491d34e 3274 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
9a2d6984 3275 && sym->binding() != elfcpp::STB_WEAK
8851ecca
ILT
3276 && !parameters->options().allow_shlib_undefined()
3277 && !parameters->target().is_defined_by_abi(sym)
fd9d194f 3278 && !sym->object()->is_in_system_directory())
9a2d6984
ILT
3279 {
3280 // A very ugly cast.
3281 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
3282 if (!dynobj->has_unknown_needed_entries())
f073bbf7 3283 gold_undefined_symbol(sym);
9a2d6984
ILT
3284 }
3285}
3286
a3ad94ed
ILT
3287// Write out a section symbol. Return the update offset.
3288
3289void
ca09d69a 3290Symbol_table::write_section_symbol(const Output_section* os,
d491d34e 3291 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
3292 Output_file* of,
3293 off_t offset) const
3294{
8851ecca 3295 switch (parameters->size_and_endianness())
a3ad94ed 3296 {
9025d29d 3297#ifdef HAVE_TARGET_32_LITTLE
8851ecca 3298 case Parameters::TARGET_32_LITTLE:
d491d34e
ILT
3299 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
3300 offset);
8851ecca 3301 break;
9025d29d 3302#endif
8851ecca
ILT
3303#ifdef HAVE_TARGET_32_BIG
3304 case Parameters::TARGET_32_BIG:
d491d34e
ILT
3305 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
3306 offset);
8851ecca 3307 break;
9025d29d 3308#endif
9025d29d 3309#ifdef HAVE_TARGET_64_LITTLE
8851ecca 3310 case Parameters::TARGET_64_LITTLE:
d491d34e
ILT
3311 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
3312 offset);
8851ecca 3313 break;
9025d29d 3314#endif
8851ecca
ILT
3315#ifdef HAVE_TARGET_64_BIG
3316 case Parameters::TARGET_64_BIG:
d491d34e
ILT
3317 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
3318 offset);
8851ecca
ILT
3319 break;
3320#endif
3321 default:
3322 gold_unreachable();
a3ad94ed 3323 }
a3ad94ed
ILT
3324}
3325
3326// Write out a section symbol, specialized for size and endianness.
3327
3328template<int size, bool big_endian>
3329void
3330Symbol_table::sized_write_section_symbol(const Output_section* os,
d491d34e 3331 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
3332 Output_file* of,
3333 off_t offset) const
3334{
3335 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
3336
3337 unsigned char* pov = of->get_output_view(offset, sym_size);
3338
3339 elfcpp::Sym_write<size, big_endian> osym(pov);
3340 osym.put_st_name(0);
b4ecf66b
ILT
3341 if (parameters->options().relocatable())
3342 osym.put_st_value(0);
3343 else
3344 osym.put_st_value(os->address());
a3ad94ed
ILT
3345 osym.put_st_size(0);
3346 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
3347 elfcpp::STT_SECTION));
3348 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
d491d34e 3349
2ea97941
ILT
3350 unsigned int shndx = os->out_shndx();
3351 if (shndx >= elfcpp::SHN_LORESERVE)
d491d34e 3352 {
2ea97941
ILT
3353 symtab_xindex->add(os->symtab_index(), shndx);
3354 shndx = elfcpp::SHN_XINDEX;
d491d34e 3355 }
2ea97941 3356 osym.put_st_shndx(shndx);
a3ad94ed
ILT
3357
3358 of->write_output_view(offset, sym_size, pov);
3359}
3360
abaa3995
ILT
3361// Print statistical information to stderr. This is used for --stats.
3362
3363void
3364Symbol_table::print_stats() const
3365{
3366#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
3367 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
3368 program_name, this->table_.size(), this->table_.bucket_count());
3369#else
3370 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
3371 program_name, this->table_.size());
3372#endif
ad8f37d1 3373 this->namepool_.print_stats("symbol table stringpool");
abaa3995
ILT
3374}
3375
ff541f30
ILT
3376// We check for ODR violations by looking for symbols with the same
3377// name for which the debugging information reports that they were
71ff8986 3378// defined in disjoint source locations. When comparing the source
55382fb7
ILT
3379// location, we consider instances with the same base filename to be
3380// the same. This is because different object files/shared libraries
3381// can include the same header file using different paths, and
3382// different optimization settings can make the line number appear to
3383// be a couple lines off, and we don't want to report an ODR violation
3384// in those cases.
ff541f30
ILT
3385
3386// This struct is used to compare line information, as returned by
7bf1f802 3387// Dwarf_line_info::one_addr2line. It implements a < comparison
71ff8986 3388// operator used with std::sort.
ff541f30
ILT
3389
3390struct Odr_violation_compare
3391{
3392 bool
3393 operator()(const std::string& s1, const std::string& s2) const
3394 {
55382fb7 3395 // Inputs should be of the form "dirname/filename:linenum" where
71ff8986 3396 // "dirname/" is optional. We want to compare just the filename:linenum.
55382fb7 3397
71ff8986 3398 // Find the last '/' in each string.
55382fb7
ILT
3399 std::string::size_type s1begin = s1.rfind('/');
3400 std::string::size_type s2begin = s2.rfind('/');
55382fb7
ILT
3401 // If there was no '/' in a string, start at the beginning.
3402 if (s1begin == std::string::npos)
3403 s1begin = 0;
3404 if (s2begin == std::string::npos)
3405 s2begin = 0;
71ff8986
ILT
3406 return s1.compare(s1begin, std::string::npos,
3407 s2, s2begin, std::string::npos) < 0;
ff541f30
ILT
3408 }
3409};
3410
71ff8986
ILT
3411// Returns all of the lines attached to LOC, not just the one the
3412// instruction actually came from.
3413std::vector<std::string>
3414Symbol_table::linenos_from_loc(const Task* task,
3415 const Symbol_location& loc)
3416{
3417 // We need to lock the object in order to read it. This
3418 // means that we have to run in a singleton Task. If we
3419 // want to run this in a general Task for better
3420 // performance, we will need one Task for object, plus
3421 // appropriate locking to ensure that we don't conflict with
3422 // other uses of the object. Also note, one_addr2line is not
3423 // currently thread-safe.
3424 Task_lock_obj<Object> tl(task, loc.object);
3425
3426 std::vector<std::string> result;
dc3714f3
AM
3427 Symbol_location code_loc = loc;
3428 parameters->target().function_location(&code_loc);
71ff8986
ILT
3429 // 16 is the size of the object-cache that one_addr2line should use.
3430 std::string canonical_result = Dwarf_line_info::one_addr2line(
dc3714f3 3431 code_loc.object, code_loc.shndx, code_loc.offset, 16, &result);
71ff8986
ILT
3432 if (!canonical_result.empty())
3433 result.push_back(canonical_result);
3434 return result;
3435}
3436
3437// OutputIterator that records if it was ever assigned to. This
3438// allows it to be used with std::set_intersection() to check for
3439// intersection rather than computing the intersection.
3440struct Check_intersection
3441{
3442 Check_intersection()
3443 : value_(false)
3444 {}
3445
3446 bool had_intersection() const
3447 { return this->value_; }
3448
3449 Check_intersection& operator++()
3450 { return *this; }
3451
3452 Check_intersection& operator*()
3453 { return *this; }
3454
3455 template<typename T>
3456 Check_intersection& operator=(const T&)
3457 {
3458 this->value_ = true;
3459 return *this;
3460 }
3461
3462 private:
3463 bool value_;
3464};
3465
70e654ba 3466// Check candidate_odr_violations_ to find symbols with the same name
71ff8986
ILT
3467// but apparently different definitions (different source-file/line-no
3468// for each line assigned to the first instruction).
70e654ba
ILT
3469
3470void
17a1d0a9
ILT
3471Symbol_table::detect_odr_violations(const Task* task,
3472 const char* output_file_name) const
70e654ba
ILT
3473{
3474 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3475 it != candidate_odr_violations_.end();
3476 ++it)
3477 {
71ff8986
ILT
3478 const char* const symbol_name = it->first;
3479
3480 std::string first_object_name;
3481 std::vector<std::string> first_object_linenos;
3482
3483 Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3484 locs = it->second.begin();
3485 const Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3486 locs_end = it->second.end();
3487 for (; locs != locs_end && first_object_linenos.empty(); ++locs)
70e654ba 3488 {
71ff8986
ILT
3489 // Save the line numbers from the first definition to
3490 // compare to the other definitions. Ideally, we'd compare
3491 // every definition to every other, but we don't want to
3492 // take O(N^2) time to do this. This shortcut may cause
3493 // false negatives that appear or disappear depending on the
3494 // link order, but it won't cause false positives.
3495 first_object_name = locs->object->name();
3496 first_object_linenos = this->linenos_from_loc(task, *locs);
70e654ba 3497 }
437ddf0c
CC
3498 if (first_object_linenos.empty())
3499 continue;
70e654ba 3500
71ff8986 3501 // Sort by Odr_violation_compare to make std::set_intersection work.
437ddf0c 3502 std::string first_object_canonical_result = first_object_linenos.back();
71ff8986
ILT
3503 std::sort(first_object_linenos.begin(), first_object_linenos.end(),
3504 Odr_violation_compare());
3505
3506 for (; locs != locs_end; ++locs)
70e654ba 3507 {
71ff8986
ILT
3508 std::vector<std::string> linenos =
3509 this->linenos_from_loc(task, *locs);
3510 // linenos will be empty if we couldn't parse the debug info.
3511 if (linenos.empty())
3512 continue;
3513 // Sort by Odr_violation_compare to make std::set_intersection work.
437ddf0c
CC
3514 gold_assert(!linenos.empty());
3515 std::string second_object_canonical_result = linenos.back();
71ff8986
ILT
3516 std::sort(linenos.begin(), linenos.end(), Odr_violation_compare());
3517
3518 Check_intersection intersection_result =
3519 std::set_intersection(first_object_linenos.begin(),
3520 first_object_linenos.end(),
3521 linenos.begin(),
3522 linenos.end(),
3523 Check_intersection(),
3524 Odr_violation_compare());
3525 if (!intersection_result.had_intersection())
3526 {
3527 gold_warning(_("while linking %s: symbol '%s' defined in "
3528 "multiple places (possible ODR violation):"),
3529 output_file_name, demangle(symbol_name).c_str());
3530 // This only prints one location from each definition,
3531 // which may not be the location we expect to intersect
3532 // with another definition. We could print the whole
3533 // set of locations, but that seems too verbose.
71ff8986 3534 fprintf(stderr, _(" %s from %s\n"),
437ddf0c 3535 first_object_canonical_result.c_str(),
71ff8986
ILT
3536 first_object_name.c_str());
3537 fprintf(stderr, _(" %s from %s\n"),
437ddf0c 3538 second_object_canonical_result.c_str(),
71ff8986
ILT
3539 locs->object->name().c_str());
3540 // Only print one broken pair, to avoid needing to
3541 // compare against a list of the disjoint definition
3542 // locations we've found so far. (If we kept comparing
3543 // against just the first one, we'd get a lot of
3544 // redundant complaints about the second definition
3545 // location.)
3546 break;
3547 }
70e654ba
ILT
3548 }
3549 }
e4e5049b
CS
3550 // We only call one_addr2line() in this function, so we can clear its cache.
3551 Dwarf_line_info::clear_addr2line_cache();
70e654ba
ILT
3552}
3553
f6ce93d6
ILT
3554// Warnings functions.
3555
3556// Add a new warning.
3557
3558void
2ea97941 3559Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
cb295612 3560 const std::string& warning)
f6ce93d6 3561{
2ea97941
ILT
3562 name = symtab->canonicalize_name(name);
3563 this->warnings_[name].set(obj, warning);
f6ce93d6
ILT
3564}
3565
3566// Look through the warnings and mark the symbols for which we should
3567// warn. This is called during Layout::finalize when we know the
3568// sources for all the symbols.
3569
3570void
cb295612 3571Warnings::note_warnings(Symbol_table* symtab)
f6ce93d6
ILT
3572{
3573 for (Warning_table::iterator p = this->warnings_.begin();
3574 p != this->warnings_.end();
3575 ++p)
3576 {
3577 Symbol* sym = symtab->lookup(p->first, NULL);
3578 if (sym != NULL
3579 && sym->source() == Symbol::FROM_OBJECT
3580 && sym->object() == p->second.object)
cb295612 3581 sym->set_has_warning();
f6ce93d6
ILT
3582 }
3583}
3584
3585// Issue a warning. This is called when we see a relocation against a
3586// symbol for which has a warning.
3587
75f2446e 3588template<int size, bool big_endian>
f6ce93d6 3589void
75f2446e
ILT
3590Warnings::issue_warning(const Symbol* sym,
3591 const Relocate_info<size, big_endian>* relinfo,
3592 size_t relnum, off_t reloffset) const
f6ce93d6 3593{
a3ad94ed 3594 gold_assert(sym->has_warning());
9d3b0698
ILT
3595
3596 // We don't want to issue a warning for a relocation against the
3597 // symbol in the same object file in which the symbol is defined.
3598 if (sym->object() == relinfo->object)
3599 return;
3600
f6ce93d6 3601 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 3602 gold_assert(p != this->warnings_.end());
75f2446e
ILT
3603 gold_warning_at_location(relinfo, relnum, reloffset,
3604 "%s", p->second.text.c_str());
f6ce93d6
ILT
3605}
3606
14bfc3f5
ILT
3607// Instantiate the templates we need. We could use the configure
3608// script to restrict this to only the ones needed for implemented
3609// targets.
3610
c7912668
ILT
3611#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3612template
3613void
3614Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3615#endif
3616
3617#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3618template
3619void
3620Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3621#endif
3622
193a53d9 3623#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
3624template
3625void
193a53d9 3626Symbol_table::add_from_relobj<32, false>(
6fa2a40b 3627 Sized_relobj_file<32, false>* relobj,
f6ce93d6 3628 const unsigned char* syms,
14bfc3f5 3629 size_t count,
d491d34e 3630 size_t symndx_offset,
14bfc3f5
ILT
3631 const char* sym_names,
3632 size_t sym_name_size,
6fa2a40b 3633 Sized_relobj_file<32, false>::Symbols* sympointers,
92de84a6 3634 size_t* defined);
193a53d9 3635#endif
14bfc3f5 3636
193a53d9 3637#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
3638template
3639void
193a53d9 3640Symbol_table::add_from_relobj<32, true>(
6fa2a40b 3641 Sized_relobj_file<32, true>* relobj,
f6ce93d6 3642 const unsigned char* syms,
14bfc3f5 3643 size_t count,
d491d34e 3644 size_t symndx_offset,
14bfc3f5
ILT
3645 const char* sym_names,
3646 size_t sym_name_size,
6fa2a40b 3647 Sized_relobj_file<32, true>::Symbols* sympointers,
92de84a6 3648 size_t* defined);
193a53d9 3649#endif
14bfc3f5 3650
193a53d9 3651#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
3652template
3653void
193a53d9 3654Symbol_table::add_from_relobj<64, false>(
6fa2a40b 3655 Sized_relobj_file<64, false>* relobj,
f6ce93d6 3656 const unsigned char* syms,
14bfc3f5 3657 size_t count,
d491d34e 3658 size_t symndx_offset,
14bfc3f5
ILT
3659 const char* sym_names,
3660 size_t sym_name_size,
6fa2a40b 3661 Sized_relobj_file<64, false>::Symbols* sympointers,
92de84a6 3662 size_t* defined);
193a53d9 3663#endif
14bfc3f5 3664
193a53d9 3665#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
3666template
3667void
193a53d9 3668Symbol_table::add_from_relobj<64, true>(
6fa2a40b 3669 Sized_relobj_file<64, true>* relobj,
f6ce93d6 3670 const unsigned char* syms,
14bfc3f5 3671 size_t count,
d491d34e 3672 size_t symndx_offset,
14bfc3f5
ILT
3673 const char* sym_names,
3674 size_t sym_name_size,
6fa2a40b 3675 Sized_relobj_file<64, true>::Symbols* sympointers,
92de84a6 3676 size_t* defined);
193a53d9 3677#endif
14bfc3f5 3678
89fc3421
CC
3679#ifdef HAVE_TARGET_32_LITTLE
3680template
3681Symbol*
3682Symbol_table::add_from_pluginobj<32, false>(
3683 Sized_pluginobj<32, false>* obj,
3684 const char* name,
3685 const char* ver,
3686 elfcpp::Sym<32, false>* sym);
3687#endif
3688
3689#ifdef HAVE_TARGET_32_BIG
3690template
3691Symbol*
3692Symbol_table::add_from_pluginobj<32, true>(
3693 Sized_pluginobj<32, true>* obj,
3694 const char* name,
3695 const char* ver,
3696 elfcpp::Sym<32, true>* sym);
3697#endif
3698
3699#ifdef HAVE_TARGET_64_LITTLE
3700template
3701Symbol*
3702Symbol_table::add_from_pluginobj<64, false>(
3703 Sized_pluginobj<64, false>* obj,
3704 const char* name,
3705 const char* ver,
3706 elfcpp::Sym<64, false>* sym);
3707#endif
3708
3709#ifdef HAVE_TARGET_64_BIG
3710template
3711Symbol*
3712Symbol_table::add_from_pluginobj<64, true>(
3713 Sized_pluginobj<64, true>* obj,
3714 const char* name,
3715 const char* ver,
3716 elfcpp::Sym<64, true>* sym);
3717#endif
3718
193a53d9 3719#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
3720template
3721void
193a53d9
ILT
3722Symbol_table::add_from_dynobj<32, false>(
3723 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
3724 const unsigned char* syms,
3725 size_t count,
3726 const char* sym_names,
3727 size_t sym_name_size,
3728 const unsigned char* versym,
3729 size_t versym_size,
92de84a6 3730 const std::vector<const char*>* version_map,
6fa2a40b 3731 Sized_relobj_file<32, false>::Symbols* sympointers,
92de84a6 3732 size_t* defined);
193a53d9 3733#endif
dbe717ef 3734
193a53d9 3735#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
3736template
3737void
193a53d9
ILT
3738Symbol_table::add_from_dynobj<32, true>(
3739 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
3740 const unsigned char* syms,
3741 size_t count,
3742 const char* sym_names,
3743 size_t sym_name_size,
3744 const unsigned char* versym,
3745 size_t versym_size,
92de84a6 3746 const std::vector<const char*>* version_map,
6fa2a40b 3747 Sized_relobj_file<32, true>::Symbols* sympointers,
92de84a6 3748 size_t* defined);
193a53d9 3749#endif
dbe717ef 3750
193a53d9 3751#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
3752template
3753void
193a53d9
ILT
3754Symbol_table::add_from_dynobj<64, false>(
3755 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
3756 const unsigned char* syms,
3757 size_t count,
3758 const char* sym_names,
3759 size_t sym_name_size,
3760 const unsigned char* versym,
3761 size_t versym_size,
92de84a6 3762 const std::vector<const char*>* version_map,
6fa2a40b 3763 Sized_relobj_file<64, false>::Symbols* sympointers,
92de84a6 3764 size_t* defined);
193a53d9 3765#endif
dbe717ef 3766
193a53d9 3767#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
3768template
3769void
193a53d9
ILT
3770Symbol_table::add_from_dynobj<64, true>(
3771 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
3772 const unsigned char* syms,
3773 size_t count,
3774 const char* sym_names,
3775 size_t sym_name_size,
3776 const unsigned char* versym,
3777 size_t versym_size,
92de84a6 3778 const std::vector<const char*>* version_map,
6fa2a40b 3779 Sized_relobj_file<64, true>::Symbols* sympointers,
92de84a6 3780 size_t* defined);
193a53d9 3781#endif
dbe717ef 3782
cdc29364
CC
3783#ifdef HAVE_TARGET_32_LITTLE
3784template
26d3c67d 3785Sized_symbol<32>*
cdc29364
CC
3786Symbol_table::add_from_incrobj(
3787 Object* obj,
3788 const char* name,
3789 const char* ver,
3790 elfcpp::Sym<32, false>* sym);
3791#endif
3792
3793#ifdef HAVE_TARGET_32_BIG
3794template
26d3c67d 3795Sized_symbol<32>*
cdc29364
CC
3796Symbol_table::add_from_incrobj(
3797 Object* obj,
3798 const char* name,
3799 const char* ver,
3800 elfcpp::Sym<32, true>* sym);
3801#endif
3802
3803#ifdef HAVE_TARGET_64_LITTLE
3804template
26d3c67d 3805Sized_symbol<64>*
cdc29364
CC
3806Symbol_table::add_from_incrobj(
3807 Object* obj,
3808 const char* name,
3809 const char* ver,
3810 elfcpp::Sym<64, false>* sym);
3811#endif
3812
3813#ifdef HAVE_TARGET_64_BIG
3814template
26d3c67d 3815Sized_symbol<64>*
cdc29364
CC
3816Symbol_table::add_from_incrobj(
3817 Object* obj,
3818 const char* name,
3819 const char* ver,
3820 elfcpp::Sym<64, true>* sym);
3821#endif
3822
46fe1623
ILT
3823#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3824template
3825void
fe8718a4 3826Symbol_table::define_with_copy_reloc<32>(
fe8718a4
ILT
3827 Sized_symbol<32>* sym,
3828 Output_data* posd,
2ea97941 3829 elfcpp::Elf_types<32>::Elf_Addr value);
46fe1623
ILT
3830#endif
3831
3832#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3833template
3834void
fe8718a4 3835Symbol_table::define_with_copy_reloc<64>(
fe8718a4
ILT
3836 Sized_symbol<64>* sym,
3837 Output_data* posd,
2ea97941 3838 elfcpp::Elf_types<64>::Elf_Addr value);
46fe1623
ILT
3839#endif
3840
beacaa96
CC
3841#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3842template
3843void
3844Sized_symbol<32>::init_output_data(const char* name, const char* version,
3845 Output_data* od, Value_type value,
3846 Size_type symsize, elfcpp::STT type,
3847 elfcpp::STB binding,
3848 elfcpp::STV visibility,
3849 unsigned char nonvis,
3850 bool offset_is_from_end,
3851 bool is_predefined);
8d04e81d
CC
3852
3853template
3854void
3855Sized_symbol<32>::init_constant(const char* name, const char* version,
3856 Value_type value, Size_type symsize,
3857 elfcpp::STT type, elfcpp::STB binding,
3858 elfcpp::STV visibility, unsigned char nonvis,
3859 bool is_predefined);
3860
3861template
3862void
3863Sized_symbol<32>::init_undefined(const char* name, const char* version,
3864 Value_type value, elfcpp::STT type,
3865 elfcpp::STB binding, elfcpp::STV visibility,
3866 unsigned char nonvis);
beacaa96
CC
3867#endif
3868
3869#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3870template
3871void
3872Sized_symbol<64>::init_output_data(const char* name, const char* version,
3873 Output_data* od, Value_type value,
3874 Size_type symsize, elfcpp::STT type,
3875 elfcpp::STB binding,
3876 elfcpp::STV visibility,
3877 unsigned char nonvis,
3878 bool offset_is_from_end,
3879 bool is_predefined);
8d04e81d
CC
3880
3881template
3882void
3883Sized_symbol<64>::init_constant(const char* name, const char* version,
3884 Value_type value, Size_type symsize,
3885 elfcpp::STT type, elfcpp::STB binding,
3886 elfcpp::STV visibility, unsigned char nonvis,
3887 bool is_predefined);
3888
3889template
3890void
3891Sized_symbol<64>::init_undefined(const char* name, const char* version,
3892 Value_type value, elfcpp::STT type,
3893 elfcpp::STB binding, elfcpp::STV visibility,
3894 unsigned char nonvis);
beacaa96
CC
3895#endif
3896
75f2446e
ILT
3897#ifdef HAVE_TARGET_32_LITTLE
3898template
3899void
3900Warnings::issue_warning<32, false>(const Symbol* sym,
3901 const Relocate_info<32, false>* relinfo,
3902 size_t relnum, off_t reloffset) const;
3903#endif
3904
3905#ifdef HAVE_TARGET_32_BIG
3906template
3907void
3908Warnings::issue_warning<32, true>(const Symbol* sym,
3909 const Relocate_info<32, true>* relinfo,
3910 size_t relnum, off_t reloffset) const;
3911#endif
3912
3913#ifdef HAVE_TARGET_64_LITTLE
3914template
3915void
3916Warnings::issue_warning<64, false>(const Symbol* sym,
3917 const Relocate_info<64, false>* relinfo,
3918 size_t relnum, off_t reloffset) const;
3919#endif
3920
3921#ifdef HAVE_TARGET_64_BIG
3922template
3923void
3924Warnings::issue_warning<64, true>(const Symbol* sym,
3925 const Relocate_info<64, true>* relinfo,
3926 size_t relnum, off_t reloffset) const;
3927#endif
3928
14bfc3f5 3929} // End namespace gold.