]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
* nto-procfs.c (procfs_xfer_memory): Changed signature.
[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);
f3e9c5c5 177 this->source_ = IS_UNDEFINED;
008db82e 178 this->in_reg_ = true;
ead1e424
ILT
179}
180
c7912668
ILT
181// Allocate a common symbol in the base.
182
183void
184Symbol::allocate_base_common(Output_data* od)
185{
186 gold_assert(this->is_common());
187 this->source_ = IN_OUTPUT_DATA;
188 this->u_.in_output_data.output_data = od;
189 this->u_.in_output_data.offset_is_from_end = false;
190}
191
ead1e424 192// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
193
194template<int size>
195template<bool big_endian>
196void
f3e9c5c5
ILT
197Sized_symbol<size>::init_object(const char* name, const char* version,
198 Object* object,
199 const elfcpp::Sym<size, big_endian>& sym,
200 unsigned int st_shndx, bool is_ordinary)
14bfc3f5 201{
f3e9c5c5 202 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
14bfc3f5 203 this->value_ = sym.get_st_value();
ead1e424
ILT
204 this->symsize_ = sym.get_st_size();
205}
206
207// Initialize the fields in Sized_symbol for a symbol defined in an
208// Output_data.
209
210template<int size>
211void
75517b77
ILT
212Sized_symbol<size>::init_output_data(const char* name, const char* version,
213 Output_data* od, Value_type value,
214 Size_type symsize, elfcpp::STT type,
215 elfcpp::STB binding,
f3e9c5c5
ILT
216 elfcpp::STV visibility,
217 unsigned char nonvis,
218 bool offset_is_from_end)
ead1e424 219{
75517b77
ILT
220 this->init_base_output_data(name, version, od, type, binding, visibility,
221 nonvis, offset_is_from_end);
ead1e424
ILT
222 this->value_ = value;
223 this->symsize_ = symsize;
224}
225
226// Initialize the fields in Sized_symbol for a symbol defined in an
227// Output_segment.
228
229template<int size>
230void
75517b77
ILT
231Sized_symbol<size>::init_output_segment(const char* name, const char* version,
232 Output_segment* os, Value_type value,
233 Size_type symsize, elfcpp::STT type,
234 elfcpp::STB binding,
f3e9c5c5
ILT
235 elfcpp::STV visibility,
236 unsigned char nonvis,
237 Segment_offset_base offset_base)
ead1e424 238{
75517b77
ILT
239 this->init_base_output_segment(name, version, os, type, binding, visibility,
240 nonvis, offset_base);
ead1e424
ILT
241 this->value_ = value;
242 this->symsize_ = symsize;
243}
244
245// Initialize the fields in Sized_symbol for a symbol defined as a
246// constant.
247
248template<int size>
249void
75517b77
ILT
250Sized_symbol<size>::init_constant(const char* name, const char* version,
251 Value_type value, Size_type symsize,
252 elfcpp::STT type, elfcpp::STB binding,
253 elfcpp::STV visibility, unsigned char nonvis)
ead1e424 254{
75517b77 255 this->init_base_constant(name, version, type, binding, visibility, nonvis);
ead1e424
ILT
256 this->value_ = value;
257 this->symsize_ = symsize;
14bfc3f5
ILT
258}
259
f3e9c5c5
ILT
260// Initialize the fields in Sized_symbol for an undefined symbol.
261
262template<int size>
263void
75517b77
ILT
264Sized_symbol<size>::init_undefined(const char* name, const char* version,
265 elfcpp::STT type, elfcpp::STB binding,
266 elfcpp::STV visibility, unsigned char nonvis)
f3e9c5c5 267{
75517b77 268 this->init_base_undefined(name, version, type, binding, visibility, nonvis);
f3e9c5c5
ILT
269 this->value_ = 0;
270 this->symsize_ = 0;
271}
272
c7912668
ILT
273// Allocate a common symbol.
274
275template<int size>
276void
277Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
278{
279 this->allocate_base_common(od);
280 this->value_ = value;
281}
282
436ca963
ILT
283// Return true if this symbol should be added to the dynamic symbol
284// table.
285
286inline bool
287Symbol::should_add_dynsym_entry() const
288{
289 // If the symbol is used by a dynamic relocation, we need to add it.
290 if (this->needs_dynsym_entry())
291 return true;
292
55a93433
ILT
293 // If the symbol was forced local in a version script, do not add it.
294 if (this->is_forced_local())
295 return false;
296
436ca963
ILT
297 // If exporting all symbols or building a shared library,
298 // and the symbol is defined in a regular object and is
299 // externally visible, we need to add it.
8851ecca 300 if ((parameters->options().export_dynamic() || parameters->options().shared())
436ca963
ILT
301 && !this->is_from_dynobj()
302 && this->is_externally_visible())
303 return true;
304
305 return false;
306}
307
b3b74ddc
ILT
308// Return true if the final value of this symbol is known at link
309// time.
310
311bool
312Symbol::final_value_is_known() const
313{
314 // If we are not generating an executable, then no final values are
315 // known, since they will change at runtime.
8851ecca 316 if (parameters->options().shared() || parameters->options().relocatable())
b3b74ddc
ILT
317 return false;
318
f3e9c5c5
ILT
319 // If the symbol is not from an object file, and is not undefined,
320 // then it is defined, and known.
b3b74ddc 321 if (this->source_ != FROM_OBJECT)
f3e9c5c5
ILT
322 {
323 if (this->source_ != IS_UNDEFINED)
324 return true;
325 }
326 else
327 {
328 // If the symbol is from a dynamic object, then the final value
329 // is not known.
330 if (this->object()->is_dynamic())
331 return false;
b3b74ddc 332
f3e9c5c5
ILT
333 // If the symbol is not undefined (it is defined or common),
334 // then the final value is known.
335 if (!this->is_undefined())
336 return true;
337 }
b3b74ddc
ILT
338
339 // If the symbol is undefined, then whether the final value is known
340 // depends on whether we are doing a static link. If we are doing a
341 // dynamic link, then the final value could be filled in at runtime.
342 // This could reasonably be the case for a weak undefined symbol.
343 return parameters->doing_static_link();
344}
345
77e65537 346// Return the output section where this symbol is defined.
a445fddf 347
77e65537
ILT
348Output_section*
349Symbol::output_section() const
a445fddf
ILT
350{
351 switch (this->source_)
352 {
353 case FROM_OBJECT:
77e65537
ILT
354 {
355 unsigned int shndx = this->u_.from_object.shndx;
d491d34e 356 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
77e65537
ILT
357 {
358 gold_assert(!this->u_.from_object.object->is_dynamic());
359 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
ef9beddf 360 return relobj->output_section(shndx);
77e65537
ILT
361 }
362 return NULL;
363 }
364
a445fddf 365 case IN_OUTPUT_DATA:
77e65537
ILT
366 return this->u_.in_output_data.output_data->output_section();
367
a445fddf 368 case IN_OUTPUT_SEGMENT:
f3e9c5c5
ILT
369 case IS_CONSTANT:
370 case IS_UNDEFINED:
77e65537
ILT
371 return NULL;
372
373 default:
374 gold_unreachable();
375 }
376}
377
378// Set the symbol's output section. This is used for symbols defined
379// in scripts. This should only be called after the symbol table has
380// been finalized.
381
382void
383Symbol::set_output_section(Output_section* os)
384{
385 switch (this->source_)
386 {
387 case FROM_OBJECT:
388 case IN_OUTPUT_DATA:
389 gold_assert(this->output_section() == os);
390 break;
f3e9c5c5 391 case IS_CONSTANT:
77e65537
ILT
392 this->source_ = IN_OUTPUT_DATA;
393 this->u_.in_output_data.output_data = os;
394 this->u_.in_output_data.offset_is_from_end = false;
395 break;
396 case IN_OUTPUT_SEGMENT:
f3e9c5c5 397 case IS_UNDEFINED:
a445fddf
ILT
398 default:
399 gold_unreachable();
400 }
401}
402
14bfc3f5
ILT
403// Class Symbol_table.
404
09124467
ILT
405Symbol_table::Symbol_table(unsigned int count,
406 const Version_script_info& version_script)
6d013333 407 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
155a0dd7 408 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
55a93433 409 version_script_(version_script)
14bfc3f5 410{
6d013333 411 namepool_.reserve(count);
14bfc3f5
ILT
412}
413
414Symbol_table::~Symbol_table()
415{
416}
417
ad8f37d1 418// The hash function. The key values are Stringpool keys.
14bfc3f5 419
ad8f37d1 420inline size_t
14bfc3f5
ILT
421Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
422{
f0641a0b 423 return key.first ^ key.second;
14bfc3f5
ILT
424}
425
ad8f37d1
ILT
426// The symbol table key equality function. This is called with
427// Stringpool keys.
14bfc3f5 428
ad8f37d1 429inline bool
14bfc3f5
ILT
430Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
431 const Symbol_table_key& k2) const
432{
433 return k1.first == k2.first && k1.second == k2.second;
434}
435
dd8670e5 436// Make TO a symbol which forwards to FROM.
14bfc3f5
ILT
437
438void
439Symbol_table::make_forwarder(Symbol* from, Symbol* to)
440{
a3ad94ed
ILT
441 gold_assert(from != to);
442 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
443 this->forwarders_[from] = to;
444 from->set_forwarder();
445}
446
61ba1cf9
ILT
447// Resolve the forwards from FROM, returning the real symbol.
448
14bfc3f5 449Symbol*
c06b7b0b 450Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 451{
a3ad94ed 452 gold_assert(from->is_forwarder());
c06b7b0b 453 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 454 this->forwarders_.find(from);
a3ad94ed 455 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
456 return p->second;
457}
458
61ba1cf9
ILT
459// Look up a symbol by name.
460
461Symbol*
462Symbol_table::lookup(const char* name, const char* version) const
463{
f0641a0b
ILT
464 Stringpool::Key name_key;
465 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
466 if (name == NULL)
467 return NULL;
f0641a0b
ILT
468
469 Stringpool::Key version_key = 0;
61ba1cf9
ILT
470 if (version != NULL)
471 {
f0641a0b 472 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
473 if (version == NULL)
474 return NULL;
475 }
476
f0641a0b 477 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
478 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
479 if (p == this->table_.end())
480 return NULL;
481 return p->second;
482}
483
14bfc3f5
ILT
484// Resolve a Symbol with another Symbol. This is only used in the
485// unusual case where there are references to both an unversioned
486// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
487// version is the default version. Because this is unusual, we do
488// this the slow way, by converting back to an ELF symbol.
14bfc3f5 489
1564db8d 490template<int size, bool big_endian>
14bfc3f5 491void
14b31740 492Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
7d1a9ebb 493 const char* version)
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(),
505 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);
738 Symbol_table::resolve<size, big_endian>(ret, sym2, version);
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, '@');
09124467 889 int namelen = 0;
55a93433 890 // DEF: is the version default? LOCAL: is the symbol forced local?
09124467 891 bool def = false;
55a93433 892 bool local = false;
09124467
ILT
893
894 if (ver != NULL)
895 {
896 // The symbol name is of the form foo@VERSION or foo@@VERSION
897 namelen = ver - name;
898 ++ver;
899 if (*ver == '@')
900 {
901 def = true;
902 ++ver;
903 }
904 }
5871526f
ILT
905 // We don't want to assign a version to an undefined symbol,
906 // even if it is listed in the version script. FIXME: What
907 // about a common symbol?
908 else if (!version_script_.empty()
d491d34e 909 && st_shndx != elfcpp::SHN_UNDEF)
09124467
ILT
910 {
911 // The symbol name did not have a version, but
912 // the version script may assign a version anyway.
913 namelen = strlen(name);
914 def = true;
55a93433 915 // Check the global: entries from the version script.
09124467
ILT
916 const std::string& version =
917 version_script_.get_symbol_version(name);
918 if (!version.empty())
919 ver = version.c_str();
55a93433
ILT
920 // Check the local: entries from the version script
921 if (version_script_.symbol_is_local(name))
922 local = true;
09124467 923 }
14bfc3f5 924
d491d34e
ILT
925 elfcpp::Sym<size, big_endian>* psym = &sym;
926 unsigned char symbuf[sym_size];
927 elfcpp::Sym<size, big_endian> sym2(symbuf);
88dd47ac
ILT
928 if (just_symbols)
929 {
d491d34e 930 memcpy(symbuf, p, sym_size);
88dd47ac 931 elfcpp::Sym_write<size, big_endian> sw(symbuf);
d491d34e 932 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
88dd47ac
ILT
933 {
934 // Symbol values in object files are section relative.
935 // This is normally what we want, but since here we are
936 // converting the symbol to absolute we need to add the
937 // section address. The section address in an object
938 // file is normally zero, but people can use a linker
939 // script to change it.
d491d34e
ILT
940 sw.put_st_value(sym.get_st_value()
941 + relobj->section_address(orig_st_shndx));
88dd47ac 942 }
d491d34e
ILT
943 st_shndx = elfcpp::SHN_ABS;
944 is_ordinary = false;
88dd47ac
ILT
945 psym = &sym2;
946 }
947
aeddab66 948 Sized_symbol<size>* res;
14bfc3f5
ILT
949 if (ver == NULL)
950 {
f0641a0b 951 Stringpool::Key name_key;
cfd73a4e 952 name = this->namepool_.add(name, true, &name_key);
dbe717ef 953 res = this->add_from_object(relobj, name, name_key, NULL, 0,
d491d34e
ILT
954 false, *psym, st_shndx, is_ordinary,
955 orig_st_shndx);
55a93433
ILT
956 if (local)
957 this->force_local(res);
14bfc3f5
ILT
958 }
959 else
960 {
f0641a0b 961 Stringpool::Key name_key;
09124467 962 name = this->namepool_.add_with_length(name, namelen, true,
c0873094 963 &name_key);
f0641a0b 964 Stringpool::Key ver_key;
cfd73a4e 965 ver = this->namepool_.add(ver, true, &ver_key);
f0641a0b 966
dbe717ef 967 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
d491d34e
ILT
968 def, *psym, st_shndx, is_ordinary,
969 orig_st_shndx);
14bfc3f5
ILT
970 }
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.
1274 if (*pversion == NULL)
1275 {
1276 const std::string& v(this->version_script_.get_symbol_version(*pname));
1277 if (!v.empty())
1278 *pversion = v.c_str();
1279 }
1280
ead1e424
ILT
1281 if (only_if_ref)
1282 {
306d9ef0 1283 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 1284 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 1285 return NULL;
306d9ef0
ILT
1286
1287 *pname = oldsym->name();
1288 *pversion = oldsym->version();
ead1e424
ILT
1289 }
1290 else
1291 {
14b31740 1292 // Canonicalize NAME and VERSION.
f0641a0b 1293 Stringpool::Key name_key;
cfd73a4e 1294 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 1295
14b31740 1296 Stringpool::Key version_key = 0;
306d9ef0 1297 if (*pversion != NULL)
cfd73a4e 1298 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 1299
ead1e424 1300 Symbol* const snull = NULL;
ead1e424 1301 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
1302 this->table_.insert(std::make_pair(std::make_pair(name_key,
1303 version_key),
ead1e424
ILT
1304 snull));
1305
1306 if (!ins.second)
1307 {
14b31740 1308 // We already have a symbol table entry for NAME/VERSION.
ead1e424 1309 oldsym = ins.first->second;
a3ad94ed 1310 gold_assert(oldsym != NULL);
ead1e424
ILT
1311 }
1312 else
1313 {
1314 // We haven't seen this symbol before.
a3ad94ed 1315 gold_assert(ins.first->second == NULL);
86f2e683
ILT
1316 add_to_table = true;
1317 add_loc = ins.first;
ead1e424
ILT
1318 oldsym = NULL;
1319 }
1320 }
1321
8851ecca
ILT
1322 const Target& target = parameters->target();
1323 if (!target.has_make_symbol())
86f2e683
ILT
1324 sym = new Sized_symbol<size>();
1325 else
ead1e424 1326 {
8851ecca
ILT
1327 gold_assert(target.get_size() == size);
1328 gold_assert(target.is_big_endian() ? big_endian : !big_endian);
86f2e683
ILT
1329 typedef Sized_target<size, big_endian> My_target;
1330 const My_target* sized_target =
8851ecca 1331 static_cast<const My_target*>(&target);
86f2e683
ILT
1332 sym = sized_target->make_symbol();
1333 if (sym == NULL)
1334 return NULL;
1335 }
ead1e424 1336
86f2e683
ILT
1337 if (add_to_table)
1338 add_loc->second = sym;
1339 else
1340 gold_assert(oldsym != NULL);
ead1e424 1341
7d1a9ebb 1342 *poldsym = this->get_sized_symbol<size>(oldsym);
ead1e424
ILT
1343
1344 return sym;
1345}
1346
1347// Define a symbol based on an Output_data.
1348
14b31740 1349Symbol*
9b07f471
ILT
1350Symbol_table::define_in_output_data(const char* name,
1351 const char* version,
1352 Output_data* od,
1353 uint64_t value,
1354 uint64_t symsize,
1355 elfcpp::STT type,
1356 elfcpp::STB binding,
ead1e424
ILT
1357 elfcpp::STV visibility,
1358 unsigned char nonvis,
1359 bool offset_is_from_end,
1360 bool only_if_ref)
1361{
8851ecca 1362 if (parameters->target().get_size() == 32)
86f2e683
ILT
1363 {
1364#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1365 return this->do_define_in_output_data<32>(name, version, od,
86f2e683
ILT
1366 value, symsize, type, binding,
1367 visibility, nonvis,
1368 offset_is_from_end,
1369 only_if_ref);
1370#else
1371 gold_unreachable();
1372#endif
1373 }
8851ecca 1374 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1375 {
1376#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1377 return this->do_define_in_output_data<64>(name, version, od,
86f2e683
ILT
1378 value, symsize, type, binding,
1379 visibility, nonvis,
1380 offset_is_from_end,
1381 only_if_ref);
1382#else
1383 gold_unreachable();
1384#endif
1385 }
ead1e424 1386 else
a3ad94ed 1387 gold_unreachable();
ead1e424
ILT
1388}
1389
1390// Define a symbol in an Output_data, sized version.
1391
1392template<int size>
14b31740 1393Sized_symbol<size>*
ead1e424 1394Symbol_table::do_define_in_output_data(
ead1e424 1395 const char* name,
14b31740 1396 const char* version,
ead1e424
ILT
1397 Output_data* od,
1398 typename elfcpp::Elf_types<size>::Elf_Addr value,
1399 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1400 elfcpp::STT type,
1401 elfcpp::STB binding,
1402 elfcpp::STV visibility,
1403 unsigned char nonvis,
1404 bool offset_is_from_end,
1405 bool only_if_ref)
1406{
1407 Sized_symbol<size>* sym;
86f2e683 1408 Sized_symbol<size>* oldsym;
ead1e424 1409
8851ecca 1410 if (parameters->target().is_big_endian())
193a53d9
ILT
1411 {
1412#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb
ILT
1413 sym = this->define_special_symbol<size, true>(&name, &version,
1414 only_if_ref, &oldsym);
193a53d9
ILT
1415#else
1416 gold_unreachable();
1417#endif
1418 }
ead1e424 1419 else
193a53d9
ILT
1420 {
1421#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb
ILT
1422 sym = this->define_special_symbol<size, false>(&name, &version,
1423 only_if_ref, &oldsym);
193a53d9
ILT
1424#else
1425 gold_unreachable();
1426#endif
1427 }
ead1e424
ILT
1428
1429 if (sym == NULL)
14b31740 1430 return NULL;
ead1e424 1431
75517b77
ILT
1432 sym->init_output_data(name, version, od, value, symsize, type, binding,
1433 visibility, nonvis, offset_is_from_end);
14b31740 1434
e5756efb 1435 if (oldsym == NULL)
55a93433
ILT
1436 {
1437 if (binding == elfcpp::STB_LOCAL
1438 || this->version_script_.symbol_is_local(name))
1439 this->force_local(sym);
75517b77
ILT
1440 else if (version != NULL)
1441 sym->set_is_default();
55a93433
ILT
1442 return sym;
1443 }
86f2e683 1444
e5756efb
ILT
1445 if (Symbol_table::should_override_with_special(oldsym))
1446 this->override_with_special(oldsym, sym);
1447 delete sym;
1448 return oldsym;
ead1e424
ILT
1449}
1450
1451// Define a symbol based on an Output_segment.
1452
14b31740 1453Symbol*
9b07f471 1454Symbol_table::define_in_output_segment(const char* name,
14b31740 1455 const char* version, Output_segment* os,
9b07f471
ILT
1456 uint64_t value,
1457 uint64_t symsize,
1458 elfcpp::STT type,
1459 elfcpp::STB binding,
ead1e424
ILT
1460 elfcpp::STV visibility,
1461 unsigned char nonvis,
1462 Symbol::Segment_offset_base offset_base,
1463 bool only_if_ref)
1464{
8851ecca 1465 if (parameters->target().get_size() == 32)
86f2e683
ILT
1466 {
1467#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1468 return this->do_define_in_output_segment<32>(name, version, os,
86f2e683
ILT
1469 value, symsize, type,
1470 binding, visibility, nonvis,
1471 offset_base, only_if_ref);
1472#else
1473 gold_unreachable();
1474#endif
1475 }
8851ecca 1476 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1477 {
1478#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1479 return this->do_define_in_output_segment<64>(name, version, os,
86f2e683
ILT
1480 value, symsize, type,
1481 binding, visibility, nonvis,
1482 offset_base, only_if_ref);
1483#else
1484 gold_unreachable();
1485#endif
1486 }
ead1e424 1487 else
a3ad94ed 1488 gold_unreachable();
ead1e424
ILT
1489}
1490
1491// Define a symbol in an Output_segment, sized version.
1492
1493template<int size>
14b31740 1494Sized_symbol<size>*
ead1e424 1495Symbol_table::do_define_in_output_segment(
ead1e424 1496 const char* name,
14b31740 1497 const char* version,
ead1e424
ILT
1498 Output_segment* os,
1499 typename elfcpp::Elf_types<size>::Elf_Addr value,
1500 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1501 elfcpp::STT type,
1502 elfcpp::STB binding,
1503 elfcpp::STV visibility,
1504 unsigned char nonvis,
1505 Symbol::Segment_offset_base offset_base,
1506 bool only_if_ref)
1507{
1508 Sized_symbol<size>* sym;
86f2e683 1509 Sized_symbol<size>* oldsym;
ead1e424 1510
8851ecca 1511 if (parameters->target().is_big_endian())
9025d29d
ILT
1512 {
1513#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb
ILT
1514 sym = this->define_special_symbol<size, true>(&name, &version,
1515 only_if_ref, &oldsym);
9025d29d
ILT
1516#else
1517 gold_unreachable();
1518#endif
1519 }
ead1e424 1520 else
9025d29d
ILT
1521 {
1522#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb
ILT
1523 sym = this->define_special_symbol<size, false>(&name, &version,
1524 only_if_ref, &oldsym);
9025d29d
ILT
1525#else
1526 gold_unreachable();
1527#endif
1528 }
ead1e424
ILT
1529
1530 if (sym == NULL)
14b31740 1531 return NULL;
ead1e424 1532
75517b77 1533 sym->init_output_segment(name, version, os, value, symsize, type, binding,
f3e9c5c5 1534 visibility, nonvis, offset_base);
14b31740 1535
e5756efb 1536 if (oldsym == NULL)
55a93433
ILT
1537 {
1538 if (binding == elfcpp::STB_LOCAL
1539 || this->version_script_.symbol_is_local(name))
1540 this->force_local(sym);
75517b77
ILT
1541 else if (version != NULL)
1542 sym->set_is_default();
55a93433
ILT
1543 return sym;
1544 }
86f2e683 1545
e5756efb
ILT
1546 if (Symbol_table::should_override_with_special(oldsym))
1547 this->override_with_special(oldsym, sym);
1548 delete sym;
1549 return oldsym;
ead1e424
ILT
1550}
1551
1552// Define a special symbol with a constant value. It is a multiple
1553// definition error if this symbol is already defined.
1554
14b31740 1555Symbol*
9b07f471
ILT
1556Symbol_table::define_as_constant(const char* name,
1557 const char* version,
1558 uint64_t value,
1559 uint64_t symsize,
1560 elfcpp::STT type,
1561 elfcpp::STB binding,
1562 elfcpp::STV visibility,
1563 unsigned char nonvis,
caa9d5d9
ILT
1564 bool only_if_ref,
1565 bool force_override)
ead1e424 1566{
8851ecca 1567 if (parameters->target().get_size() == 32)
86f2e683
ILT
1568 {
1569#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1570 return this->do_define_as_constant<32>(name, version, value,
86f2e683 1571 symsize, type, binding,
caa9d5d9
ILT
1572 visibility, nonvis, only_if_ref,
1573 force_override);
86f2e683
ILT
1574#else
1575 gold_unreachable();
1576#endif
1577 }
8851ecca 1578 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1579 {
1580#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1581 return this->do_define_as_constant<64>(name, version, value,
86f2e683 1582 symsize, type, binding,
caa9d5d9
ILT
1583 visibility, nonvis, only_if_ref,
1584 force_override);
86f2e683
ILT
1585#else
1586 gold_unreachable();
1587#endif
1588 }
ead1e424 1589 else
a3ad94ed 1590 gold_unreachable();
ead1e424
ILT
1591}
1592
1593// Define a symbol as a constant, sized version.
1594
1595template<int size>
14b31740 1596Sized_symbol<size>*
ead1e424 1597Symbol_table::do_define_as_constant(
ead1e424 1598 const char* name,
14b31740 1599 const char* version,
ead1e424
ILT
1600 typename elfcpp::Elf_types<size>::Elf_Addr value,
1601 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1602 elfcpp::STT type,
1603 elfcpp::STB binding,
1604 elfcpp::STV visibility,
1605 unsigned char nonvis,
caa9d5d9
ILT
1606 bool only_if_ref,
1607 bool force_override)
ead1e424
ILT
1608{
1609 Sized_symbol<size>* sym;
86f2e683 1610 Sized_symbol<size>* oldsym;
ead1e424 1611
8851ecca 1612 if (parameters->target().is_big_endian())
9025d29d
ILT
1613 {
1614#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb
ILT
1615 sym = this->define_special_symbol<size, true>(&name, &version,
1616 only_if_ref, &oldsym);
9025d29d
ILT
1617#else
1618 gold_unreachable();
1619#endif
1620 }
ead1e424 1621 else
9025d29d
ILT
1622 {
1623#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb
ILT
1624 sym = this->define_special_symbol<size, false>(&name, &version,
1625 only_if_ref, &oldsym);
9025d29d
ILT
1626#else
1627 gold_unreachable();
1628#endif
1629 }
ead1e424
ILT
1630
1631 if (sym == NULL)
14b31740 1632 return NULL;
ead1e424 1633
75517b77
ILT
1634 sym->init_constant(name, version, value, symsize, type, binding, visibility,
1635 nonvis);
14b31740 1636
e5756efb 1637 if (oldsym == NULL)
55a93433 1638 {
686c8caf
ILT
1639 // Version symbols are absolute symbols with name == version.
1640 // We don't want to force them to be local.
1641 if ((version == NULL
1642 || name != version
1643 || value != 0)
1644 && (binding == elfcpp::STB_LOCAL
1645 || this->version_script_.symbol_is_local(name)))
55a93433 1646 this->force_local(sym);
75517b77
ILT
1647 else if (version != NULL
1648 && (name != version || value != 0))
1649 sym->set_is_default();
55a93433
ILT
1650 return sym;
1651 }
86f2e683 1652
caa9d5d9 1653 if (force_override || Symbol_table::should_override_with_special(oldsym))
e5756efb
ILT
1654 this->override_with_special(oldsym, sym);
1655 delete sym;
1656 return oldsym;
ead1e424
ILT
1657}
1658
1659// Define a set of symbols in output sections.
1660
1661void
9b07f471 1662Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
1663 const Define_symbol_in_section* p,
1664 bool only_if_ref)
ead1e424
ILT
1665{
1666 for (int i = 0; i < count; ++i, ++p)
1667 {
1668 Output_section* os = layout->find_output_section(p->output_section);
1669 if (os != NULL)
9b07f471 1670 this->define_in_output_data(p->name, NULL, os, p->value,
14b31740
ILT
1671 p->size, p->type, p->binding,
1672 p->visibility, p->nonvis,
a445fddf
ILT
1673 p->offset_is_from_end,
1674 only_if_ref || p->only_if_ref);
ead1e424 1675 else
9b07f471 1676 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 1677 p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
1678 only_if_ref || p->only_if_ref,
1679 false);
ead1e424
ILT
1680 }
1681}
1682
1683// Define a set of symbols in output segments.
1684
1685void
9b07f471 1686Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
1687 const Define_symbol_in_segment* p,
1688 bool only_if_ref)
ead1e424
ILT
1689{
1690 for (int i = 0; i < count; ++i, ++p)
1691 {
1692 Output_segment* os = layout->find_output_segment(p->segment_type,
1693 p->segment_flags_set,
1694 p->segment_flags_clear);
1695 if (os != NULL)
9b07f471 1696 this->define_in_output_segment(p->name, NULL, os, p->value,
14b31740
ILT
1697 p->size, p->type, p->binding,
1698 p->visibility, p->nonvis,
a445fddf
ILT
1699 p->offset_base,
1700 only_if_ref || p->only_if_ref);
ead1e424 1701 else
9b07f471 1702 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 1703 p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
1704 only_if_ref || p->only_if_ref,
1705 false);
ead1e424
ILT
1706 }
1707}
1708
46fe1623
ILT
1709// Define CSYM using a COPY reloc. POSD is the Output_data where the
1710// symbol should be defined--typically a .dyn.bss section. VALUE is
1711// the offset within POSD.
1712
1713template<int size>
1714void
fe8718a4 1715Symbol_table::define_with_copy_reloc(
fe8718a4
ILT
1716 Sized_symbol<size>* csym,
1717 Output_data* posd,
1718 typename elfcpp::Elf_types<size>::Elf_Addr value)
46fe1623
ILT
1719{
1720 gold_assert(csym->is_from_dynobj());
1721 gold_assert(!csym->is_copied_from_dynobj());
1722 Object* object = csym->object();
1723 gold_assert(object->is_dynamic());
1724 Dynobj* dynobj = static_cast<Dynobj*>(object);
1725
1726 // Our copied variable has to override any variable in a shared
1727 // library.
1728 elfcpp::STB binding = csym->binding();
1729 if (binding == elfcpp::STB_WEAK)
1730 binding = elfcpp::STB_GLOBAL;
1731
9b07f471 1732 this->define_in_output_data(csym->name(), csym->version(),
46fe1623
ILT
1733 posd, value, csym->symsize(),
1734 csym->type(), binding,
1735 csym->visibility(), csym->nonvis(),
1736 false, false);
1737
1738 csym->set_is_copied_from_dynobj();
1739 csym->set_needs_dynsym_entry();
1740
1741 this->copied_symbol_dynobjs_[csym] = dynobj;
1742
1743 // We have now defined all aliases, but we have not entered them all
1744 // in the copied_symbol_dynobjs_ map.
1745 if (csym->has_alias())
1746 {
1747 Symbol* sym = csym;
1748 while (true)
1749 {
1750 sym = this->weak_aliases_[sym];
1751 if (sym == csym)
1752 break;
1753 gold_assert(sym->output_data() == posd);
1754
1755 sym->set_is_copied_from_dynobj();
1756 this->copied_symbol_dynobjs_[sym] = dynobj;
1757 }
1758 }
1759}
1760
1761// SYM is defined using a COPY reloc. Return the dynamic object where
1762// the original definition was found.
1763
1764Dynobj*
1765Symbol_table::get_copy_source(const Symbol* sym) const
1766{
1767 gold_assert(sym->is_copied_from_dynobj());
1768 Copied_symbol_dynobjs::const_iterator p =
1769 this->copied_symbol_dynobjs_.find(sym);
1770 gold_assert(p != this->copied_symbol_dynobjs_.end());
1771 return p->second;
1772}
1773
f3e9c5c5
ILT
1774// Add any undefined symbols named on the command line.
1775
1776void
1777Symbol_table::add_undefined_symbols_from_command_line()
1778{
1779 if (parameters->options().any_undefined())
1780 {
1781 if (parameters->target().get_size() == 32)
1782 {
5adf9721 1783#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
f3e9c5c5
ILT
1784 this->do_add_undefined_symbols_from_command_line<32>();
1785#else
1786 gold_unreachable();
1787#endif
1788 }
1789 else if (parameters->target().get_size() == 64)
1790 {
1791#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1792 this->do_add_undefined_symbols_from_command_line<64>();
1793#else
1794 gold_unreachable();
1795#endif
1796 }
1797 else
1798 gold_unreachable();
1799 }
1800}
1801
1802template<int size>
1803void
1804Symbol_table::do_add_undefined_symbols_from_command_line()
1805{
1806 for (options::String_set::const_iterator p =
1807 parameters->options().undefined_begin();
1808 p != parameters->options().undefined_end();
1809 ++p)
1810 {
1811 const char* name = p->c_str();
1812
1813 if (this->lookup(name) != NULL)
1814 continue;
1815
1816 const char* version = NULL;
1817
1818 Sized_symbol<size>* sym;
1819 Sized_symbol<size>* oldsym;
1820 if (parameters->target().is_big_endian())
1821 {
1822#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1823 sym = this->define_special_symbol<size, true>(&name, &version,
1824 false, &oldsym);
1825#else
1826 gold_unreachable();
1827#endif
1828 }
1829 else
1830 {
1831#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1832 sym = this->define_special_symbol<size, false>(&name, &version,
1833 false, &oldsym);
1834#else
1835 gold_unreachable();
1836#endif
1837 }
1838
1839 gold_assert(oldsym == NULL);
1840
75517b77 1841 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
f3e9c5c5
ILT
1842 elfcpp::STV_DEFAULT, 0);
1843 ++this->saw_undefined_;
1844 }
1845}
1846
a3ad94ed
ILT
1847// Set the dynamic symbol indexes. INDEX is the index of the first
1848// global dynamic symbol. Pointers to the symbols are stored into the
1849// vector SYMS. The names are added to DYNPOOL. This returns an
1850// updated dynamic symbol index.
1851
1852unsigned int
9b07f471 1853Symbol_table::set_dynsym_indexes(unsigned int index,
a3ad94ed 1854 std::vector<Symbol*>* syms,
14b31740
ILT
1855 Stringpool* dynpool,
1856 Versions* versions)
a3ad94ed
ILT
1857{
1858 for (Symbol_table_type::iterator p = this->table_.begin();
1859 p != this->table_.end();
1860 ++p)
1861 {
1862 Symbol* sym = p->second;
16649710
ILT
1863
1864 // Note that SYM may already have a dynamic symbol index, since
1865 // some symbols appear more than once in the symbol table, with
1866 // and without a version.
1867
436ca963 1868 if (!sym->should_add_dynsym_entry())
16649710
ILT
1869 sym->set_dynsym_index(-1U);
1870 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1871 {
1872 sym->set_dynsym_index(index);
1873 ++index;
1874 syms->push_back(sym);
cfd73a4e 1875 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
1876
1877 // Record any version information.
09124467
ILT
1878 if (sym->version() != NULL)
1879 versions->record_version(this, dynpool, sym);
a3ad94ed
ILT
1880 }
1881 }
1882
14b31740
ILT
1883 // Finish up the versions. In some cases this may add new dynamic
1884 // symbols.
9b07f471 1885 index = versions->finalize(this, index, syms);
14b31740 1886
a3ad94ed
ILT
1887 return index;
1888}
1889
c06b7b0b 1890// Set the final values for all the symbols. The index of the first
55a93433
ILT
1891// global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1892// file offset OFF. Add their names to POOL. Return the new file
1893// offset. Update *PLOCAL_SYMCOUNT if necessary.
54dc6425 1894
75f65a3e 1895off_t
55a93433
ILT
1896Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1897 size_t dyncount, Stringpool* pool,
1898 unsigned int *plocal_symcount)
54dc6425 1899{
f6ce93d6
ILT
1900 off_t ret;
1901
55a93433
ILT
1902 gold_assert(*plocal_symcount != 0);
1903 this->first_global_index_ = *plocal_symcount;
c06b7b0b 1904
16649710
ILT
1905 this->dynamic_offset_ = dynoff;
1906 this->first_dynamic_global_index_ = dyn_global_index;
1907 this->dynamic_count_ = dyncount;
1908
8851ecca 1909 if (parameters->target().get_size() == 32)
9025d29d
ILT
1910 {
1911#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
55a93433 1912 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
9025d29d
ILT
1913#else
1914 gold_unreachable();
1915#endif
1916 }
8851ecca 1917 else if (parameters->target().get_size() == 64)
9025d29d
ILT
1918 {
1919#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
55a93433 1920 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
9025d29d
ILT
1921#else
1922 gold_unreachable();
1923#endif
1924 }
61ba1cf9 1925 else
a3ad94ed 1926 gold_unreachable();
f6ce93d6
ILT
1927
1928 // Now that we have the final symbol table, we can reliably note
1929 // which symbols should get warnings.
cb295612 1930 this->warnings_.note_warnings(this);
f6ce93d6
ILT
1931
1932 return ret;
75f65a3e
ILT
1933}
1934
55a93433
ILT
1935// SYM is going into the symbol table at *PINDEX. Add the name to
1936// POOL, update *PINDEX and *POFF.
1937
1938template<int size>
1939void
1940Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1941 unsigned int* pindex, off_t* poff)
1942{
1943 sym->set_symtab_index(*pindex);
1944 pool->add(sym->name(), false, NULL);
1945 ++*pindex;
1946 *poff += elfcpp::Elf_sizes<size>::sym_size;
1947}
1948
ead1e424
ILT
1949// Set the final value for all the symbols. This is called after
1950// Layout::finalize, so all the output sections have their final
1951// address.
75f65a3e
ILT
1952
1953template<int size>
1954off_t
55a93433
ILT
1955Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1956 unsigned int* plocal_symcount)
75f65a3e 1957{
ead1e424 1958 off = align_address(off, size >> 3);
75f65a3e
ILT
1959 this->offset_ = off;
1960
55a93433
ILT
1961 unsigned int index = *plocal_symcount;
1962 const unsigned int orig_index = index;
c06b7b0b 1963
55a93433
ILT
1964 // First do all the symbols which have been forced to be local, as
1965 // they must appear before all global symbols.
1966 for (Forced_locals::iterator p = this->forced_locals_.begin();
1967 p != this->forced_locals_.end();
1968 ++p)
1969 {
1970 Symbol* sym = *p;
1971 gold_assert(sym->is_forced_local());
1972 if (this->sized_finalize_symbol<size>(sym))
1973 {
1974 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1975 ++*plocal_symcount;
1976 }
1977 }
1978
1979 // Now do all the remaining symbols.
c06b7b0b
ILT
1980 for (Symbol_table_type::iterator p = this->table_.begin();
1981 p != this->table_.end();
1982 ++p)
54dc6425 1983 {
55a93433
ILT
1984 Symbol* sym = p->second;
1985 if (this->sized_finalize_symbol<size>(sym))
1986 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1987 }
54dc6425 1988
55a93433 1989 this->output_count_ = index - orig_index;
a3ad94ed 1990
55a93433
ILT
1991 return off;
1992}
75f65a3e 1993
55a93433
ILT
1994// Finalize the symbol SYM. This returns true if the symbol should be
1995// added to the symbol table, false otherwise.
008db82e 1996
55a93433
ILT
1997template<int size>
1998bool
1999Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2000{
ef9beddf
ILT
2001 typedef typename Sized_symbol<size>::Value_type Value_type;
2002
55a93433 2003 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
75f65a3e 2004
55a93433
ILT
2005 // The default version of a symbol may appear twice in the symbol
2006 // table. We only need to finalize it once.
2007 if (sym->has_symtab_index())
2008 return false;
ead1e424 2009
55a93433
ILT
2010 if (!sym->in_reg())
2011 {
2012 gold_assert(!sym->has_symtab_index());
2013 sym->set_symtab_index(-1U);
2014 gold_assert(sym->dynsym_index() == -1U);
2015 return false;
2016 }
ead1e424 2017
ef9beddf 2018 Value_type value;
ead1e424 2019
55a93433
ILT
2020 switch (sym->source())
2021 {
2022 case Symbol::FROM_OBJECT:
2023 {
d491d34e
ILT
2024 bool is_ordinary;
2025 unsigned int shndx = sym->shndx(&is_ordinary);
ead1e424 2026
55a93433 2027 // FIXME: We need some target specific support here.
d491d34e 2028 if (!is_ordinary
0dfbdef4
ILT
2029 && shndx != elfcpp::SHN_ABS
2030 && shndx != elfcpp::SHN_COMMON)
55a93433
ILT
2031 {
2032 gold_error(_("%s: unsupported symbol section 0x%x"),
2033 sym->demangled_name().c_str(), shndx);
2034 shndx = elfcpp::SHN_UNDEF;
ead1e424 2035 }
ead1e424 2036
55a93433
ILT
2037 Object* symobj = sym->object();
2038 if (symobj->is_dynamic())
ead1e424 2039 {
55a93433
ILT
2040 value = 0;
2041 shndx = elfcpp::SHN_UNDEF;
ead1e424 2042 }
55a93433
ILT
2043 else if (shndx == elfcpp::SHN_UNDEF)
2044 value = 0;
d491d34e
ILT
2045 else if (!is_ordinary
2046 && (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON))
55a93433
ILT
2047 value = sym->value();
2048 else
ead1e424 2049 {
55a93433 2050 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 2051 Output_section* os = relobj->output_section(shndx);
55a93433
ILT
2052
2053 if (os == NULL)
ead1e424 2054 {
55a93433
ILT
2055 sym->set_symtab_index(-1U);
2056 gold_assert(sym->dynsym_index() == -1U);
2057 return false;
ead1e424 2058 }
55a93433 2059
ef9beddf
ILT
2060 uint64_t secoff64 = relobj->output_section_offset(shndx);
2061 Value_type secoff = convert_types<Value_type, uint64_t>(secoff64);
55a93433
ILT
2062 if (sym->type() == elfcpp::STT_TLS)
2063 value = sym->value() + os->tls_offset() + secoff;
2064 else
2065 value = sym->value() + os->address() + secoff;
ead1e424 2066 }
55a93433
ILT
2067 }
2068 break;
2069
2070 case Symbol::IN_OUTPUT_DATA:
2071 {
2072 Output_data* od = sym->output_data();
155a0dd7
ILT
2073 value = sym->value();
2074 if (sym->type() != elfcpp::STT_TLS)
2075 value += od->address();
2076 else
2077 {
2078 Output_section* os = od->output_section();
2079 gold_assert(os != NULL);
2080 value += os->tls_offset() + (od->address() - os->address());
2081 }
55a93433
ILT
2082 if (sym->offset_is_from_end())
2083 value += od->data_size();
2084 }
2085 break;
2086
2087 case Symbol::IN_OUTPUT_SEGMENT:
2088 {
2089 Output_segment* os = sym->output_segment();
edfbb029
CC
2090 value = sym->value();
2091 if (sym->type() != elfcpp::STT_TLS)
2092 value += os->vaddr();
55a93433
ILT
2093 switch (sym->offset_base())
2094 {
2095 case Symbol::SEGMENT_START:
2096 break;
2097 case Symbol::SEGMENT_END:
2098 value += os->memsz();
2099 break;
2100 case Symbol::SEGMENT_BSS:
2101 value += os->filesz();
2102 break;
2103 default:
2104 gold_unreachable();
2105 }
2106 }
2107 break;
ead1e424 2108
f3e9c5c5 2109 case Symbol::IS_CONSTANT:
55a93433
ILT
2110 value = sym->value();
2111 break;
ead1e424 2112
f3e9c5c5
ILT
2113 case Symbol::IS_UNDEFINED:
2114 value = 0;
2115 break;
2116
55a93433
ILT
2117 default:
2118 gold_unreachable();
2119 }
ead1e424 2120
55a93433 2121 sym->set_value(value);
9e2dcb77 2122
8851ecca 2123 if (parameters->options().strip_all())
55a93433
ILT
2124 {
2125 sym->set_symtab_index(-1U);
2126 return false;
54dc6425 2127 }
75f65a3e 2128
55a93433 2129 return true;
54dc6425
ILT
2130}
2131
61ba1cf9
ILT
2132// Write out the global symbols.
2133
2134void
9a2d6984
ILT
2135Symbol_table::write_globals(const Input_objects* input_objects,
2136 const Stringpool* sympool,
d491d34e
ILT
2137 const Stringpool* dynpool,
2138 Output_symtab_xindex* symtab_xindex,
2139 Output_symtab_xindex* dynsym_xindex,
2140 Output_file* of) const
61ba1cf9 2141{
8851ecca 2142 switch (parameters->size_and_endianness())
61ba1cf9 2143 {
9025d29d 2144#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
2145 case Parameters::TARGET_32_LITTLE:
2146 this->sized_write_globals<32, false>(input_objects, sympool,
d491d34e
ILT
2147 dynpool, symtab_xindex,
2148 dynsym_xindex, of);
8851ecca 2149 break;
9025d29d 2150#endif
8851ecca
ILT
2151#ifdef HAVE_TARGET_32_BIG
2152 case Parameters::TARGET_32_BIG:
2153 this->sized_write_globals<32, true>(input_objects, sympool,
d491d34e
ILT
2154 dynpool, symtab_xindex,
2155 dynsym_xindex, of);
8851ecca 2156 break;
9025d29d 2157#endif
9025d29d 2158#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
2159 case Parameters::TARGET_64_LITTLE:
2160 this->sized_write_globals<64, false>(input_objects, sympool,
d491d34e
ILT
2161 dynpool, symtab_xindex,
2162 dynsym_xindex, of);
8851ecca 2163 break;
9025d29d 2164#endif
8851ecca
ILT
2165#ifdef HAVE_TARGET_64_BIG
2166 case Parameters::TARGET_64_BIG:
2167 this->sized_write_globals<64, true>(input_objects, sympool,
d491d34e
ILT
2168 dynpool, symtab_xindex,
2169 dynsym_xindex, of);
8851ecca
ILT
2170 break;
2171#endif
2172 default:
2173 gold_unreachable();
61ba1cf9 2174 }
61ba1cf9
ILT
2175}
2176
2177// Write out the global symbols.
2178
2179template<int size, bool big_endian>
2180void
9a2d6984 2181Symbol_table::sized_write_globals(const Input_objects* input_objects,
61ba1cf9 2182 const Stringpool* sympool,
16649710 2183 const Stringpool* dynpool,
d491d34e
ILT
2184 Output_symtab_xindex* symtab_xindex,
2185 Output_symtab_xindex* dynsym_xindex,
61ba1cf9
ILT
2186 Output_file* of) const
2187{
8851ecca 2188 const Target& target = parameters->target();
9a2d6984 2189
61ba1cf9 2190 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
55a93433
ILT
2191
2192 const unsigned int output_count = this->output_count_;
2193 const section_size_type oview_size = output_count * sym_size;
2194 const unsigned int first_global_index = this->first_global_index_;
5fe2a0f5
ILT
2195 unsigned char* psyms;
2196 if (this->offset_ == 0 || output_count == 0)
2197 psyms = NULL;
2198 else
2199 psyms = of->get_output_view(this->offset_, oview_size);
16649710 2200
55a93433
ILT
2201 const unsigned int dynamic_count = this->dynamic_count_;
2202 const section_size_type dynamic_size = dynamic_count * sym_size;
2203 const unsigned int first_dynamic_global_index =
2204 this->first_dynamic_global_index_;
16649710 2205 unsigned char* dynamic_view;
5fe2a0f5 2206 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
16649710
ILT
2207 dynamic_view = NULL;
2208 else
2209 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 2210
61ba1cf9
ILT
2211 for (Symbol_table_type::const_iterator p = this->table_.begin();
2212 p != this->table_.end();
2213 ++p)
2214 {
2215 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2216
9a2d6984
ILT
2217 // Possibly warn about unresolved symbols in shared libraries.
2218 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
e2827e5f 2219
a3ad94ed 2220 unsigned int sym_index = sym->symtab_index();
16649710
ILT
2221 unsigned int dynsym_index;
2222 if (dynamic_view == NULL)
2223 dynsym_index = -1U;
2224 else
2225 dynsym_index = sym->dynsym_index();
2226
2227 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
2228 {
2229 // This symbol is not included in the output file.
2230 continue;
2231 }
16649710 2232
ead1e424 2233 unsigned int shndx;
88dd47ac
ILT
2234 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2235 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
ead1e424
ILT
2236 switch (sym->source())
2237 {
2238 case Symbol::FROM_OBJECT:
2239 {
d491d34e
ILT
2240 bool is_ordinary;
2241 unsigned int in_shndx = sym->shndx(&is_ordinary);
ead1e424
ILT
2242
2243 // FIXME: We need some target specific support here.
d491d34e 2244 if (!is_ordinary
0dfbdef4
ILT
2245 && in_shndx != elfcpp::SHN_ABS
2246 && in_shndx != elfcpp::SHN_COMMON)
ead1e424 2247 {
75f2446e 2248 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 2249 sym->demangled_name().c_str(), in_shndx);
75f2446e 2250 shndx = in_shndx;
f6ce93d6 2251 }
ead1e424
ILT
2252 else
2253 {
75f2446e
ILT
2254 Object* symobj = sym->object();
2255 if (symobj->is_dynamic())
2256 {
2257 if (sym->needs_dynsym_value())
8851ecca 2258 dynsym_value = target.dynsym_value(sym);
75f2446e
ILT
2259 shndx = elfcpp::SHN_UNDEF;
2260 }
2261 else if (in_shndx == elfcpp::SHN_UNDEF
d491d34e
ILT
2262 || (!is_ordinary
2263 && (in_shndx == elfcpp::SHN_ABS
2264 || in_shndx == elfcpp::SHN_COMMON)))
75f2446e
ILT
2265 shndx = in_shndx;
2266 else
2267 {
2268 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 2269 Output_section* os = relobj->output_section(in_shndx);
75f2446e
ILT
2270 gold_assert(os != NULL);
2271 shndx = os->out_shndx();
88dd47ac 2272
d491d34e
ILT
2273 if (shndx >= elfcpp::SHN_LORESERVE)
2274 {
2275 if (sym_index != -1U)
2276 symtab_xindex->add(sym_index, shndx);
2277 if (dynsym_index != -1U)
2278 dynsym_xindex->add(dynsym_index, shndx);
2279 shndx = elfcpp::SHN_XINDEX;
2280 }
2281
88dd47ac
ILT
2282 // In object files symbol values are section
2283 // relative.
8851ecca 2284 if (parameters->options().relocatable())
88dd47ac 2285 sym_value -= os->address();
75f2446e 2286 }
ead1e424
ILT
2287 }
2288 }
2289 break;
2290
2291 case Symbol::IN_OUTPUT_DATA:
2292 shndx = sym->output_data()->out_shndx();
d491d34e
ILT
2293 if (shndx >= elfcpp::SHN_LORESERVE)
2294 {
2295 if (sym_index != -1U)
2296 symtab_xindex->add(sym_index, shndx);
2297 if (dynsym_index != -1U)
2298 dynsym_xindex->add(dynsym_index, shndx);
2299 shndx = elfcpp::SHN_XINDEX;
2300 }
ead1e424
ILT
2301 break;
2302
2303 case Symbol::IN_OUTPUT_SEGMENT:
2304 shndx = elfcpp::SHN_ABS;
2305 break;
2306
f3e9c5c5 2307 case Symbol::IS_CONSTANT:
ead1e424
ILT
2308 shndx = elfcpp::SHN_ABS;
2309 break;
2310
f3e9c5c5
ILT
2311 case Symbol::IS_UNDEFINED:
2312 shndx = elfcpp::SHN_UNDEF;
2313 break;
2314
ead1e424 2315 default:
a3ad94ed 2316 gold_unreachable();
ead1e424 2317 }
61ba1cf9 2318
16649710
ILT
2319 if (sym_index != -1U)
2320 {
55a93433
ILT
2321 sym_index -= first_global_index;
2322 gold_assert(sym_index < output_count);
2323 unsigned char* ps = psyms + (sym_index * sym_size);
7d1a9ebb
ILT
2324 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2325 sympool, ps);
16649710 2326 }
61ba1cf9 2327
16649710
ILT
2328 if (dynsym_index != -1U)
2329 {
2330 dynsym_index -= first_dynamic_global_index;
2331 gold_assert(dynsym_index < dynamic_count);
2332 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
7d1a9ebb
ILT
2333 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2334 dynpool, pd);
16649710 2335 }
61ba1cf9
ILT
2336 }
2337
c06b7b0b 2338 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
2339 if (dynamic_view != NULL)
2340 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2341}
2342
2343// Write out the symbol SYM, in section SHNDX, to P. POOL is the
2344// strtab holding the name.
2345
2346template<int size, bool big_endian>
2347void
ab5c9e90
ILT
2348Symbol_table::sized_write_symbol(
2349 Sized_symbol<size>* sym,
2350 typename elfcpp::Elf_types<size>::Elf_Addr value,
2351 unsigned int shndx,
2352 const Stringpool* pool,
7d1a9ebb 2353 unsigned char* p) const
16649710
ILT
2354{
2355 elfcpp::Sym_write<size, big_endian> osym(p);
2356 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 2357 osym.put_st_value(value);
3f7c5e1d
CD
2358 // Use a symbol size of zero for undefined symbols.
2359 osym.put_st_size(shndx == elfcpp::SHN_UNDEF ? 0 : sym->symsize());
55a93433
ILT
2360 // A version script may have overridden the default binding.
2361 if (sym->is_forced_local())
2362 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
2363 else
2364 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
16649710
ILT
2365 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2366 osym.put_st_shndx(shndx);
61ba1cf9
ILT
2367}
2368
9a2d6984
ILT
2369// Check for unresolved symbols in shared libraries. This is
2370// controlled by the --allow-shlib-undefined option.
2371
2372// We only warn about libraries for which we have seen all the
2373// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2374// which were not seen in this link. If we didn't see a DT_NEEDED
2375// entry, we aren't going to be able to reliably report whether the
2376// symbol is undefined.
2377
2378// We also don't warn about libraries found in the system library
2379// directory (the directory were we find libc.so); we assume that
2380// those libraries are OK. This heuristic avoids problems in
2381// GNU/Linux, in which -ldl can have undefined references satisfied by
2382// ld-linux.so.
2383
2384inline void
2385Symbol_table::warn_about_undefined_dynobj_symbol(
2386 const Input_objects* input_objects,
2387 Symbol* sym) const
2388{
d491d34e 2389 bool dummy;
9a2d6984
ILT
2390 if (sym->source() == Symbol::FROM_OBJECT
2391 && sym->object()->is_dynamic()
d491d34e 2392 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
9a2d6984 2393 && sym->binding() != elfcpp::STB_WEAK
8851ecca
ILT
2394 && !parameters->options().allow_shlib_undefined()
2395 && !parameters->target().is_defined_by_abi(sym)
9a2d6984
ILT
2396 && !input_objects->found_in_system_library_directory(sym->object()))
2397 {
2398 // A very ugly cast.
2399 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2400 if (!dynobj->has_unknown_needed_entries())
096b02cf
CS
2401 {
2402 if (sym->version())
2403 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2404 sym->object()->name().c_str(),
2405 sym->demangled_name().c_str(),
2406 sym->version());
2407 else
2408 gold_error(_("%s: undefined reference to '%s'"),
2409 sym->object()->name().c_str(),
2410 sym->demangled_name().c_str());
2411 }
9a2d6984
ILT
2412 }
2413}
2414
a3ad94ed
ILT
2415// Write out a section symbol. Return the update offset.
2416
2417void
9025d29d 2418Symbol_table::write_section_symbol(const Output_section *os,
d491d34e 2419 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
2420 Output_file* of,
2421 off_t offset) const
2422{
8851ecca 2423 switch (parameters->size_and_endianness())
a3ad94ed 2424 {
9025d29d 2425#ifdef HAVE_TARGET_32_LITTLE
8851ecca 2426 case Parameters::TARGET_32_LITTLE:
d491d34e
ILT
2427 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2428 offset);
8851ecca 2429 break;
9025d29d 2430#endif
8851ecca
ILT
2431#ifdef HAVE_TARGET_32_BIG
2432 case Parameters::TARGET_32_BIG:
d491d34e
ILT
2433 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2434 offset);
8851ecca 2435 break;
9025d29d 2436#endif
9025d29d 2437#ifdef HAVE_TARGET_64_LITTLE
8851ecca 2438 case Parameters::TARGET_64_LITTLE:
d491d34e
ILT
2439 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2440 offset);
8851ecca 2441 break;
9025d29d 2442#endif
8851ecca
ILT
2443#ifdef HAVE_TARGET_64_BIG
2444 case Parameters::TARGET_64_BIG:
d491d34e
ILT
2445 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2446 offset);
8851ecca
ILT
2447 break;
2448#endif
2449 default:
2450 gold_unreachable();
a3ad94ed 2451 }
a3ad94ed
ILT
2452}
2453
2454// Write out a section symbol, specialized for size and endianness.
2455
2456template<int size, bool big_endian>
2457void
2458Symbol_table::sized_write_section_symbol(const Output_section* os,
d491d34e 2459 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
2460 Output_file* of,
2461 off_t offset) const
2462{
2463 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2464
2465 unsigned char* pov = of->get_output_view(offset, sym_size);
2466
2467 elfcpp::Sym_write<size, big_endian> osym(pov);
2468 osym.put_st_name(0);
2469 osym.put_st_value(os->address());
2470 osym.put_st_size(0);
2471 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2472 elfcpp::STT_SECTION));
2473 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
d491d34e
ILT
2474
2475 unsigned int shndx = os->out_shndx();
2476 if (shndx >= elfcpp::SHN_LORESERVE)
2477 {
2478 symtab_xindex->add(os->symtab_index(), shndx);
2479 shndx = elfcpp::SHN_XINDEX;
2480 }
2481 osym.put_st_shndx(shndx);
a3ad94ed
ILT
2482
2483 of->write_output_view(offset, sym_size, pov);
2484}
2485
abaa3995
ILT
2486// Print statistical information to stderr. This is used for --stats.
2487
2488void
2489Symbol_table::print_stats() const
2490{
2491#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2492 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2493 program_name, this->table_.size(), this->table_.bucket_count());
2494#else
2495 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2496 program_name, this->table_.size());
2497#endif
ad8f37d1 2498 this->namepool_.print_stats("symbol table stringpool");
abaa3995
ILT
2499}
2500
ff541f30
ILT
2501// We check for ODR violations by looking for symbols with the same
2502// name for which the debugging information reports that they were
2503// defined in different source locations. When comparing the source
2504// location, we consider instances with the same base filename and
2505// line number to be the same. This is because different object
2506// files/shared libraries can include the same header file using
2507// different paths, and we don't want to report an ODR violation in
2508// that case.
2509
2510// This struct is used to compare line information, as returned by
7bf1f802 2511// Dwarf_line_info::one_addr2line. It implements a < comparison
ff541f30
ILT
2512// operator used with std::set.
2513
2514struct Odr_violation_compare
2515{
2516 bool
2517 operator()(const std::string& s1, const std::string& s2) const
2518 {
2519 std::string::size_type pos1 = s1.rfind('/');
2520 std::string::size_type pos2 = s2.rfind('/');
2521 if (pos1 == std::string::npos
2522 || pos2 == std::string::npos)
2523 return s1 < s2;
2524 return s1.compare(pos1, std::string::npos,
2525 s2, pos2, std::string::npos) < 0;
2526 }
2527};
2528
70e654ba
ILT
2529// Check candidate_odr_violations_ to find symbols with the same name
2530// but apparently different definitions (different source-file/line-no).
2531
2532void
17a1d0a9
ILT
2533Symbol_table::detect_odr_violations(const Task* task,
2534 const char* output_file_name) const
70e654ba
ILT
2535{
2536 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2537 it != candidate_odr_violations_.end();
2538 ++it)
2539 {
2540 const char* symbol_name = it->first;
2541 // We use a sorted set so the output is deterministic.
ff541f30 2542 std::set<std::string, Odr_violation_compare> line_nums;
70e654ba 2543
b01c0a4a
ILT
2544 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2545 locs = it->second.begin();
2546 locs != it->second.end();
2547 ++locs)
70e654ba
ILT
2548 {
2549 // We need to lock the object in order to read it. This
17a1d0a9
ILT
2550 // means that we have to run in a singleton Task. If we
2551 // want to run this in a general Task for better
2552 // performance, we will need one Task for object, plus
2553 // appropriate locking to ensure that we don't conflict with
e4e5049b
CS
2554 // other uses of the object. Also note, one_addr2line is not
2555 // currently thread-safe.
17a1d0a9 2556 Task_lock_obj<Object> tl(task, locs->object);
e4e5049b 2557 // 16 is the size of the object-cache that one_addr2line should use.
a55ce7fe 2558 std::string lineno = Dwarf_line_info::one_addr2line(
e4e5049b 2559 locs->object, locs->shndx, locs->offset, 16);
70e654ba
ILT
2560 if (!lineno.empty())
2561 line_nums.insert(lineno);
2562 }
2563
2564 if (line_nums.size() > 1)
2565 {
dd8670e5 2566 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
78f15696 2567 "places (possible ODR violation):"),
a2b1aa12 2568 output_file_name, demangle(symbol_name).c_str());
70e654ba
ILT
2569 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2570 it2 != line_nums.end();
2571 ++it2)
2572 fprintf(stderr, " %s\n", it2->c_str());
2573 }
2574 }
e4e5049b
CS
2575 // We only call one_addr2line() in this function, so we can clear its cache.
2576 Dwarf_line_info::clear_addr2line_cache();
70e654ba
ILT
2577}
2578
f6ce93d6
ILT
2579// Warnings functions.
2580
2581// Add a new warning.
2582
2583void
2584Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
cb295612 2585 const std::string& warning)
f6ce93d6
ILT
2586{
2587 name = symtab->canonicalize_name(name);
cb295612 2588 this->warnings_[name].set(obj, warning);
f6ce93d6
ILT
2589}
2590
2591// Look through the warnings and mark the symbols for which we should
2592// warn. This is called during Layout::finalize when we know the
2593// sources for all the symbols.
2594
2595void
cb295612 2596Warnings::note_warnings(Symbol_table* symtab)
f6ce93d6
ILT
2597{
2598 for (Warning_table::iterator p = this->warnings_.begin();
2599 p != this->warnings_.end();
2600 ++p)
2601 {
2602 Symbol* sym = symtab->lookup(p->first, NULL);
2603 if (sym != NULL
2604 && sym->source() == Symbol::FROM_OBJECT
2605 && sym->object() == p->second.object)
cb295612 2606 sym->set_has_warning();
f6ce93d6
ILT
2607 }
2608}
2609
2610// Issue a warning. This is called when we see a relocation against a
2611// symbol for which has a warning.
2612
75f2446e 2613template<int size, bool big_endian>
f6ce93d6 2614void
75f2446e
ILT
2615Warnings::issue_warning(const Symbol* sym,
2616 const Relocate_info<size, big_endian>* relinfo,
2617 size_t relnum, off_t reloffset) const
f6ce93d6 2618{
a3ad94ed 2619 gold_assert(sym->has_warning());
f6ce93d6 2620 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 2621 gold_assert(p != this->warnings_.end());
75f2446e
ILT
2622 gold_warning_at_location(relinfo, relnum, reloffset,
2623 "%s", p->second.text.c_str());
f6ce93d6
ILT
2624}
2625
14bfc3f5
ILT
2626// Instantiate the templates we need. We could use the configure
2627// script to restrict this to only the ones needed for implemented
2628// targets.
2629
c7912668
ILT
2630#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2631template
2632void
2633Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2634#endif
2635
2636#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2637template
2638void
2639Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2640#endif
2641
193a53d9 2642#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
2643template
2644void
193a53d9
ILT
2645Symbol_table::add_from_relobj<32, false>(
2646 Sized_relobj<32, false>* relobj,
f6ce93d6 2647 const unsigned char* syms,
14bfc3f5 2648 size_t count,
d491d34e 2649 size_t symndx_offset,
14bfc3f5
ILT
2650 const char* sym_names,
2651 size_t sym_name_size,
92de84a6
ILT
2652 Sized_relobj<32, true>::Symbols* sympointers,
2653 size_t* defined);
193a53d9 2654#endif
14bfc3f5 2655
193a53d9 2656#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
2657template
2658void
193a53d9
ILT
2659Symbol_table::add_from_relobj<32, true>(
2660 Sized_relobj<32, true>* relobj,
f6ce93d6 2661 const unsigned char* syms,
14bfc3f5 2662 size_t count,
d491d34e 2663 size_t symndx_offset,
14bfc3f5
ILT
2664 const char* sym_names,
2665 size_t sym_name_size,
92de84a6
ILT
2666 Sized_relobj<32, false>::Symbols* sympointers,
2667 size_t* defined);
193a53d9 2668#endif
14bfc3f5 2669
193a53d9 2670#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
2671template
2672void
193a53d9
ILT
2673Symbol_table::add_from_relobj<64, false>(
2674 Sized_relobj<64, false>* relobj,
f6ce93d6 2675 const unsigned char* syms,
14bfc3f5 2676 size_t count,
d491d34e 2677 size_t symndx_offset,
14bfc3f5
ILT
2678 const char* sym_names,
2679 size_t sym_name_size,
92de84a6
ILT
2680 Sized_relobj<64, true>::Symbols* sympointers,
2681 size_t* defined);
193a53d9 2682#endif
14bfc3f5 2683
193a53d9 2684#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
2685template
2686void
193a53d9
ILT
2687Symbol_table::add_from_relobj<64, true>(
2688 Sized_relobj<64, true>* relobj,
f6ce93d6 2689 const unsigned char* syms,
14bfc3f5 2690 size_t count,
d491d34e 2691 size_t symndx_offset,
14bfc3f5
ILT
2692 const char* sym_names,
2693 size_t sym_name_size,
92de84a6
ILT
2694 Sized_relobj<64, false>::Symbols* sympointers,
2695 size_t* defined);
193a53d9 2696#endif
14bfc3f5 2697
193a53d9 2698#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
2699template
2700void
193a53d9
ILT
2701Symbol_table::add_from_dynobj<32, false>(
2702 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
2703 const unsigned char* syms,
2704 size_t count,
2705 const char* sym_names,
2706 size_t sym_name_size,
2707 const unsigned char* versym,
2708 size_t versym_size,
92de84a6
ILT
2709 const std::vector<const char*>* version_map,
2710 Sized_relobj<32, false>::Symbols* sympointers,
2711 size_t* defined);
193a53d9 2712#endif
dbe717ef 2713
193a53d9 2714#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
2715template
2716void
193a53d9
ILT
2717Symbol_table::add_from_dynobj<32, true>(
2718 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
2719 const unsigned char* syms,
2720 size_t count,
2721 const char* sym_names,
2722 size_t sym_name_size,
2723 const unsigned char* versym,
2724 size_t versym_size,
92de84a6
ILT
2725 const std::vector<const char*>* version_map,
2726 Sized_relobj<32, true>::Symbols* sympointers,
2727 size_t* defined);
193a53d9 2728#endif
dbe717ef 2729
193a53d9 2730#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
2731template
2732void
193a53d9
ILT
2733Symbol_table::add_from_dynobj<64, false>(
2734 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
2735 const unsigned char* syms,
2736 size_t count,
2737 const char* sym_names,
2738 size_t sym_name_size,
2739 const unsigned char* versym,
2740 size_t versym_size,
92de84a6
ILT
2741 const std::vector<const char*>* version_map,
2742 Sized_relobj<64, false>::Symbols* sympointers,
2743 size_t* defined);
193a53d9 2744#endif
dbe717ef 2745
193a53d9 2746#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
2747template
2748void
193a53d9
ILT
2749Symbol_table::add_from_dynobj<64, true>(
2750 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
2751 const unsigned char* syms,
2752 size_t count,
2753 const char* sym_names,
2754 size_t sym_name_size,
2755 const unsigned char* versym,
2756 size_t versym_size,
92de84a6
ILT
2757 const std::vector<const char*>* version_map,
2758 Sized_relobj<64, true>::Symbols* sympointers,
2759 size_t* defined);
193a53d9 2760#endif
dbe717ef 2761
46fe1623
ILT
2762#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2763template
2764void
fe8718a4 2765Symbol_table::define_with_copy_reloc<32>(
fe8718a4
ILT
2766 Sized_symbol<32>* sym,
2767 Output_data* posd,
2768 elfcpp::Elf_types<32>::Elf_Addr value);
46fe1623
ILT
2769#endif
2770
2771#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2772template
2773void
fe8718a4 2774Symbol_table::define_with_copy_reloc<64>(
fe8718a4
ILT
2775 Sized_symbol<64>* sym,
2776 Output_data* posd,
2777 elfcpp::Elf_types<64>::Elf_Addr value);
46fe1623
ILT
2778#endif
2779
75f2446e
ILT
2780#ifdef HAVE_TARGET_32_LITTLE
2781template
2782void
2783Warnings::issue_warning<32, false>(const Symbol* sym,
2784 const Relocate_info<32, false>* relinfo,
2785 size_t relnum, off_t reloffset) const;
2786#endif
2787
2788#ifdef HAVE_TARGET_32_BIG
2789template
2790void
2791Warnings::issue_warning<32, true>(const Symbol* sym,
2792 const Relocate_info<32, true>* relinfo,
2793 size_t relnum, off_t reloffset) const;
2794#endif
2795
2796#ifdef HAVE_TARGET_64_LITTLE
2797template
2798void
2799Warnings::issue_warning<64, false>(const Symbol* sym,
2800 const Relocate_info<64, false>* relinfo,
2801 size_t relnum, off_t reloffset) const;
2802#endif
2803
2804#ifdef HAVE_TARGET_64_BIG
2805template
2806void
2807Warnings::issue_warning<64, true>(const Symbol* sym,
2808 const Relocate_info<64, true>* relinfo,
2809 size_t relnum, off_t reloffset) const;
2810#endif
2811
14bfc3f5 2812} // End namespace gold.