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