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