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