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