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