]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
2008-07-22 Simon Baldwin <simonb@google.com>
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
e5756efb 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
14bfc3f5
ILT
23#include "gold.h"
24
04bf7072 25#include <cstring>
14bfc3f5 26#include <stdint.h>
04bf7072 27#include <algorithm>
70e654ba 28#include <set>
14bfc3f5
ILT
29#include <string>
30#include <utility>
a2b1aa12 31#include "demangle.h"
14bfc3f5
ILT
32
33#include "object.h"
70e654ba 34#include "dwarf_reader.h"
dbe717ef 35#include "dynobj.h"
75f65a3e 36#include "output.h"
61ba1cf9 37#include "target.h"
645f8123 38#include "workqueue.h"
14bfc3f5
ILT
39#include "symtab.h"
40
41namespace gold
42{
43
44// Class Symbol.
45
ead1e424
ILT
46// Initialize fields in Symbol. This initializes everything except u_
47// and source_.
14bfc3f5 48
14bfc3f5 49void
ead1e424
ILT
50Symbol::init_fields(const char* name, const char* version,
51 elfcpp::STT type, elfcpp::STB binding,
52 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
53{
54 this->name_ = name;
55 this->version_ = version;
c06b7b0b
ILT
56 this->symtab_index_ = 0;
57 this->dynsym_index_ = 0;
0a65a3a7 58 this->got_offsets_.init();
f4151f89 59 this->plt_offset_ = 0;
ead1e424
ILT
60 this->type_ = type;
61 this->binding_ = binding;
62 this->visibility_ = visibility;
63 this->nonvis_ = nonvis;
64 this->is_target_special_ = false;
1564db8d
ILT
65 this->is_def_ = false;
66 this->is_forwarder_ = false;
aeddab66 67 this->has_alias_ = false;
c06b7b0b 68 this->needs_dynsym_entry_ = false;
008db82e 69 this->in_reg_ = false;
ead1e424 70 this->in_dyn_ = false;
f4151f89 71 this->has_plt_offset_ = false;
f6ce93d6 72 this->has_warning_ = false;
46fe1623 73 this->is_copied_from_dynobj_ = false;
55a93433 74 this->is_forced_local_ = false;
d491d34e 75 this->is_ordinary_shndx_ = false;
ead1e424
ILT
76}
77
a2b1aa12
ILT
78// Return the demangled version of the symbol's name, but only
79// if the --demangle flag was set.
80
81static std::string
82demangle(const char* name)
83{
086a1841 84 if (!parameters->options().do_demangle())
ff541f30
ILT
85 return name;
86
a2b1aa12
ILT
87 // cplus_demangle allocates memory for the result it returns,
88 // and returns NULL if the name is already demangled.
89 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
90 if (demangled_name == NULL)
91 return name;
92
93 std::string retval(demangled_name);
94 free(demangled_name);
95 return retval;
96}
97
98std::string
99Symbol::demangled_name() const
100{
ff541f30 101 return demangle(this->name());
a2b1aa12
ILT
102}
103
ead1e424
ILT
104// Initialize the fields in the base class Symbol for SYM in OBJECT.
105
106template<int size, bool big_endian>
107void
f3e9c5c5
ILT
108Symbol::init_base_object(const char* name, const char* version, Object* object,
109 const elfcpp::Sym<size, big_endian>& sym,
110 unsigned int st_shndx, bool is_ordinary)
ead1e424
ILT
111{
112 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
113 sym.get_st_visibility(), sym.get_st_nonvis());
114 this->u_.from_object.object = object;
d491d34e
ILT
115 this->u_.from_object.shndx = st_shndx;
116 this->is_ordinary_shndx_ = is_ordinary;
ead1e424 117 this->source_ = FROM_OBJECT;
008db82e 118 this->in_reg_ = !object->is_dynamic();
1564db8d 119 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
120}
121
ead1e424
ILT
122// Initialize the fields in the base class Symbol for a symbol defined
123// in an Output_data.
124
125void
75517b77
ILT
126Symbol::init_base_output_data(const char* name, const char* version,
127 Output_data* od, elfcpp::STT type,
128 elfcpp::STB binding, elfcpp::STV visibility,
129 unsigned char nonvis, bool offset_is_from_end)
ead1e424 130{
75517b77 131 this->init_fields(name, version, type, binding, visibility, nonvis);
ead1e424
ILT
132 this->u_.in_output_data.output_data = od;
133 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
134 this->source_ = IN_OUTPUT_DATA;
008db82e 135 this->in_reg_ = true;
ead1e424
ILT
136}
137
138// Initialize the fields in the base class Symbol for a symbol defined
139// in an Output_segment.
140
141void
75517b77
ILT
142Symbol::init_base_output_segment(const char* name, const char* version,
143 Output_segment* os, elfcpp::STT type,
144 elfcpp::STB binding, elfcpp::STV visibility,
145 unsigned char nonvis,
f3e9c5c5 146 Segment_offset_base offset_base)
ead1e424 147{
75517b77 148 this->init_fields(name, version, type, binding, visibility, nonvis);
ead1e424
ILT
149 this->u_.in_output_segment.output_segment = os;
150 this->u_.in_output_segment.offset_base = offset_base;
151 this->source_ = IN_OUTPUT_SEGMENT;
008db82e 152 this->in_reg_ = true;
ead1e424
ILT
153}
154
155// Initialize the fields in the base class Symbol for a symbol defined
156// as a constant.
157
158void
75517b77
ILT
159Symbol::init_base_constant(const char* name, const char* version,
160 elfcpp::STT type, elfcpp::STB binding,
161 elfcpp::STV visibility, unsigned char nonvis)
f3e9c5c5 162{
75517b77 163 this->init_fields(name, version, type, binding, visibility, nonvis);
f3e9c5c5
ILT
164 this->source_ = IS_CONSTANT;
165 this->in_reg_ = true;
166}
167
168// Initialize the fields in the base class Symbol for an undefined
169// symbol.
170
171void
75517b77
ILT
172Symbol::init_base_undefined(const char* name, const char* version,
173 elfcpp::STT type, elfcpp::STB binding,
174 elfcpp::STV visibility, unsigned char nonvis)
ead1e424 175{
75517b77 176 this->init_fields(name, version, type, binding, visibility, nonvis);
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,
730cdc88 838 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
14bfc3f5 839{
9025d29d 840 gold_assert(size == relobj->target()->get_size());
8851ecca 841 gold_assert(size == parameters->target().get_size());
14bfc3f5 842
a783673b
ILT
843 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
844
88dd47ac
ILT
845 const bool just_symbols = relobj->just_symbols();
846
f6ce93d6 847 const unsigned char* p = syms;
a783673b 848 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
849 {
850 elfcpp::Sym<size, big_endian> sym(p);
851
d491d34e 852 unsigned int st_name = sym.get_st_name();
14bfc3f5
ILT
853 if (st_name >= sym_name_size)
854 {
75f2446e
ILT
855 relobj->error(_("bad global symbol name offset %u at %zu"),
856 st_name, i);
857 continue;
14bfc3f5
ILT
858 }
859
dbe717ef
ILT
860 const char* name = sym_names + st_name;
861
d491d34e
ILT
862 bool is_ordinary;
863 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
864 sym.get_st_shndx(),
865 &is_ordinary);
866 unsigned int orig_st_shndx = st_shndx;
867 if (!is_ordinary)
868 orig_st_shndx = elfcpp::SHN_UNDEF;
869
a783673b
ILT
870 // A symbol defined in a section which we are not including must
871 // be treated as an undefined symbol.
a783673b 872 if (st_shndx != elfcpp::SHN_UNDEF
d491d34e 873 && is_ordinary
dbe717ef 874 && !relobj->is_section_included(st_shndx))
d491d34e 875 st_shndx = elfcpp::SHN_UNDEF;
a783673b 876
14bfc3f5
ILT
877 // In an object file, an '@' in the name separates the symbol
878 // name from the version name. If there are two '@' characters,
879 // this is the default version.
880 const char* ver = strchr(name, '@');
09124467 881 int namelen = 0;
55a93433 882 // DEF: is the version default? LOCAL: is the symbol forced local?
09124467 883 bool def = false;
55a93433 884 bool local = false;
09124467
ILT
885
886 if (ver != NULL)
887 {
888 // The symbol name is of the form foo@VERSION or foo@@VERSION
889 namelen = ver - name;
890 ++ver;
891 if (*ver == '@')
892 {
893 def = true;
894 ++ver;
895 }
896 }
5871526f
ILT
897 // We don't want to assign a version to an undefined symbol,
898 // even if it is listed in the version script. FIXME: What
899 // about a common symbol?
900 else if (!version_script_.empty()
d491d34e 901 && st_shndx != elfcpp::SHN_UNDEF)
09124467
ILT
902 {
903 // The symbol name did not have a version, but
904 // the version script may assign a version anyway.
905 namelen = strlen(name);
906 def = true;
55a93433 907 // Check the global: entries from the version script.
09124467
ILT
908 const std::string& version =
909 version_script_.get_symbol_version(name);
910 if (!version.empty())
911 ver = version.c_str();
55a93433
ILT
912 // Check the local: entries from the version script
913 if (version_script_.symbol_is_local(name))
914 local = true;
09124467 915 }
14bfc3f5 916
d491d34e
ILT
917 elfcpp::Sym<size, big_endian>* psym = &sym;
918 unsigned char symbuf[sym_size];
919 elfcpp::Sym<size, big_endian> sym2(symbuf);
88dd47ac
ILT
920 if (just_symbols)
921 {
d491d34e 922 memcpy(symbuf, p, sym_size);
88dd47ac 923 elfcpp::Sym_write<size, big_endian> sw(symbuf);
d491d34e 924 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
88dd47ac
ILT
925 {
926 // Symbol values in object files are section relative.
927 // This is normally what we want, but since here we are
928 // converting the symbol to absolute we need to add the
929 // section address. The section address in an object
930 // file is normally zero, but people can use a linker
931 // script to change it.
d491d34e
ILT
932 sw.put_st_value(sym.get_st_value()
933 + relobj->section_address(orig_st_shndx));
88dd47ac 934 }
d491d34e
ILT
935 st_shndx = elfcpp::SHN_ABS;
936 is_ordinary = false;
88dd47ac
ILT
937 psym = &sym2;
938 }
939
aeddab66 940 Sized_symbol<size>* res;
14bfc3f5
ILT
941 if (ver == NULL)
942 {
f0641a0b 943 Stringpool::Key name_key;
cfd73a4e 944 name = this->namepool_.add(name, true, &name_key);
dbe717ef 945 res = this->add_from_object(relobj, name, name_key, NULL, 0,
d491d34e
ILT
946 false, *psym, st_shndx, is_ordinary,
947 orig_st_shndx);
55a93433
ILT
948 if (local)
949 this->force_local(res);
14bfc3f5
ILT
950 }
951 else
952 {
f0641a0b 953 Stringpool::Key name_key;
09124467 954 name = this->namepool_.add_with_length(name, namelen, true,
c0873094 955 &name_key);
f0641a0b 956 Stringpool::Key ver_key;
cfd73a4e 957 ver = this->namepool_.add(ver, true, &ver_key);
f0641a0b 958
dbe717ef 959 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
d491d34e
ILT
960 def, *psym, st_shndx, is_ordinary,
961 orig_st_shndx);
14bfc3f5
ILT
962 }
963
730cdc88 964 (*sympointers)[i] = res;
14bfc3f5
ILT
965 }
966}
967
dbe717ef
ILT
968// Add all the symbols in a dynamic object to the hash table.
969
970template<int size, bool big_endian>
971void
972Symbol_table::add_from_dynobj(
973 Sized_dynobj<size, big_endian>* dynobj,
974 const unsigned char* syms,
975 size_t count,
976 const char* sym_names,
977 size_t sym_name_size,
978 const unsigned char* versym,
979 size_t versym_size,
980 const std::vector<const char*>* version_map)
981{
9025d29d 982 gold_assert(size == dynobj->target()->get_size());
8851ecca 983 gold_assert(size == parameters->target().get_size());
dbe717ef 984
88dd47ac
ILT
985 if (dynobj->just_symbols())
986 {
987 gold_error(_("--just-symbols does not make sense with a shared object"));
988 return;
989 }
990
dbe717ef
ILT
991 if (versym != NULL && versym_size / 2 < count)
992 {
75f2446e
ILT
993 dynobj->error(_("too few symbol versions"));
994 return;
dbe717ef
ILT
995 }
996
997 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
998
aeddab66
ILT
999 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1000 // weak aliases. This is necessary because if the dynamic object
1001 // provides the same variable under two names, one of which is a
1002 // weak definition, and the regular object refers to the weak
1003 // definition, we have to put both the weak definition and the
1004 // strong definition into the dynamic symbol table. Given a weak
1005 // definition, the only way that we can find the corresponding
1006 // strong definition, if any, is to search the symbol table.
1007 std::vector<Sized_symbol<size>*> object_symbols;
1008
dbe717ef
ILT
1009 const unsigned char* p = syms;
1010 const unsigned char* vs = versym;
1011 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1012 {
1013 elfcpp::Sym<size, big_endian> sym(p);
1014
65778909
ILT
1015 // Ignore symbols with local binding or that have
1016 // internal or hidden visibility.
1017 if (sym.get_st_bind() == elfcpp::STB_LOCAL
1018 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1019 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
dbe717ef
ILT
1020 continue;
1021
8bdcdf2c
ILT
1022 // A protected symbol in a shared library must be treated as a
1023 // normal symbol when viewed from outside the shared library.
1024 // Implement this by overriding the visibility here.
1025 elfcpp::Sym<size, big_endian>* psym = &sym;
1026 unsigned char symbuf[sym_size];
1027 elfcpp::Sym<size, big_endian> sym2(symbuf);
1028 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1029 {
1030 memcpy(symbuf, p, sym_size);
1031 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1032 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1033 psym = &sym2;
1034 }
1035
1036 unsigned int st_name = psym->get_st_name();
dbe717ef
ILT
1037 if (st_name >= sym_name_size)
1038 {
75f2446e
ILT
1039 dynobj->error(_("bad symbol name offset %u at %zu"),
1040 st_name, i);
1041 continue;
dbe717ef
ILT
1042 }
1043
1044 const char* name = sym_names + st_name;
1045
d491d34e 1046 bool is_ordinary;
8bdcdf2c 1047 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
d491d34e
ILT
1048 &is_ordinary);
1049
aeddab66
ILT
1050 Sized_symbol<size>* res;
1051
dbe717ef
ILT
1052 if (versym == NULL)
1053 {
1054 Stringpool::Key name_key;
cfd73a4e 1055 name = this->namepool_.add(name, true, &name_key);
aeddab66 1056 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1057 false, *psym, st_shndx, is_ordinary,
d491d34e 1058 st_shndx);
dbe717ef 1059 }
aeddab66
ILT
1060 else
1061 {
1062 // Read the version information.
dbe717ef 1063
aeddab66 1064 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
dbe717ef 1065
aeddab66
ILT
1066 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1067 v &= elfcpp::VERSYM_VERSION;
dbe717ef 1068
aeddab66
ILT
1069 // The Sun documentation says that V can be VER_NDX_LOCAL,
1070 // or VER_NDX_GLOBAL, or a version index. The meaning of
1071 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1072 // The old GNU linker will happily generate VER_NDX_LOCAL
1073 // for an undefined symbol. I don't know what the Sun
1074 // linker will generate.
dbe717ef 1075
aeddab66 1076 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
d491d34e 1077 && st_shndx != elfcpp::SHN_UNDEF)
aeddab66
ILT
1078 {
1079 // This symbol should not be visible outside the object.
1080 continue;
1081 }
64707334 1082
aeddab66
ILT
1083 // At this point we are definitely going to add this symbol.
1084 Stringpool::Key name_key;
1085 name = this->namepool_.add(name, true, &name_key);
dbe717ef 1086
aeddab66
ILT
1087 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1088 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1089 {
1090 // This symbol does not have a version.
1091 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1092 false, *psym, st_shndx, is_ordinary,
d491d34e 1093 st_shndx);
aeddab66
ILT
1094 }
1095 else
1096 {
1097 if (v >= version_map->size())
1098 {
1099 dynobj->error(_("versym for symbol %zu out of range: %u"),
1100 i, v);
1101 continue;
1102 }
dbe717ef 1103
aeddab66
ILT
1104 const char* version = (*version_map)[v];
1105 if (version == NULL)
1106 {
1107 dynobj->error(_("versym for symbol %zu has no name: %u"),
1108 i, v);
1109 continue;
1110 }
dbe717ef 1111
aeddab66
ILT
1112 Stringpool::Key version_key;
1113 version = this->namepool_.add(version, true, &version_key);
1114
1115 // If this is an absolute symbol, and the version name
1116 // and symbol name are the same, then this is the
1117 // version definition symbol. These symbols exist to
1118 // support using -u to pull in particular versions. We
1119 // do not want to record a version for them.
d491d34e
ILT
1120 if (st_shndx == elfcpp::SHN_ABS
1121 && !is_ordinary
aeddab66
ILT
1122 && name_key == version_key)
1123 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
8bdcdf2c 1124 false, *psym, st_shndx, is_ordinary,
d491d34e 1125 st_shndx);
aeddab66
ILT
1126 else
1127 {
1128 const bool def = (!hidden
d491d34e 1129 && st_shndx != elfcpp::SHN_UNDEF);
aeddab66 1130 res = this->add_from_object(dynobj, name, name_key, version,
8bdcdf2c 1131 version_key, def, *psym, st_shndx,
d491d34e 1132 is_ordinary, st_shndx);
aeddab66
ILT
1133 }
1134 }
dbe717ef
ILT
1135 }
1136
99a37bfd 1137 // Note that it is possible that RES was overridden by an
a4bb589a 1138 // earlier object, in which case it can't be aliased here.
d491d34e
ILT
1139 if (st_shndx != elfcpp::SHN_UNDEF
1140 && is_ordinary
8bdcdf2c 1141 && psym->get_st_type() == elfcpp::STT_OBJECT
99a37bfd
ILT
1142 && res->source() == Symbol::FROM_OBJECT
1143 && res->object() == dynobj)
aeddab66
ILT
1144 object_symbols.push_back(res);
1145 }
1146
1147 this->record_weak_aliases(&object_symbols);
1148}
1149
1150// This is used to sort weak aliases. We sort them first by section
1151// index, then by offset, then by weak ahead of strong.
1152
1153template<int size>
1154class Weak_alias_sorter
1155{
1156 public:
1157 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1158};
1159
1160template<int size>
1161bool
1162Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1163 const Sized_symbol<size>* s2) const
1164{
d491d34e
ILT
1165 bool is_ordinary;
1166 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1167 gold_assert(is_ordinary);
1168 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1169 gold_assert(is_ordinary);
1170 if (s1_shndx != s2_shndx)
1171 return s1_shndx < s2_shndx;
1172
aeddab66
ILT
1173 if (s1->value() != s2->value())
1174 return s1->value() < s2->value();
1175 if (s1->binding() != s2->binding())
1176 {
1177 if (s1->binding() == elfcpp::STB_WEAK)
1178 return true;
1179 if (s2->binding() == elfcpp::STB_WEAK)
1180 return false;
1181 }
1182 return std::string(s1->name()) < std::string(s2->name());
1183}
dbe717ef 1184
aeddab66
ILT
1185// SYMBOLS is a list of object symbols from a dynamic object. Look
1186// for any weak aliases, and record them so that if we add the weak
1187// alias to the dynamic symbol table, we also add the corresponding
1188// strong symbol.
dbe717ef 1189
aeddab66
ILT
1190template<int size>
1191void
1192Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1193{
1194 // Sort the vector by section index, then by offset, then by weak
1195 // ahead of strong.
1196 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1197
1198 // Walk through the vector. For each weak definition, record
1199 // aliases.
1200 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1201 symbols->begin();
1202 p != symbols->end();
1203 ++p)
1204 {
1205 if ((*p)->binding() != elfcpp::STB_WEAK)
1206 continue;
1207
1208 // Build a circular list of weak aliases. Each symbol points to
1209 // the next one in the circular list.
1210
1211 Sized_symbol<size>* from_sym = *p;
1212 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1213 for (q = p + 1; q != symbols->end(); ++q)
dbe717ef 1214 {
d491d34e
ILT
1215 bool dummy;
1216 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
aeddab66
ILT
1217 || (*q)->value() != from_sym->value())
1218 break;
1219
1220 this->weak_aliases_[from_sym] = *q;
1221 from_sym->set_has_alias();
1222 from_sym = *q;
dbe717ef
ILT
1223 }
1224
aeddab66
ILT
1225 if (from_sym != *p)
1226 {
1227 this->weak_aliases_[from_sym] = *p;
1228 from_sym->set_has_alias();
1229 }
dbe717ef 1230
aeddab66 1231 p = q - 1;
dbe717ef
ILT
1232 }
1233}
1234
ead1e424
ILT
1235// Create and return a specially defined symbol. If ONLY_IF_REF is
1236// true, then only create the symbol if there is a reference to it.
86f2e683 1237// If this does not return NULL, it sets *POLDSYM to the existing
306d9ef0 1238// symbol if there is one. This canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
1239
1240template<int size, bool big_endian>
1241Sized_symbol<size>*
9b07f471
ILT
1242Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1243 bool only_if_ref,
7d1a9ebb 1244 Sized_symbol<size>** poldsym)
ead1e424 1245{
ead1e424
ILT
1246 Symbol* oldsym;
1247 Sized_symbol<size>* sym;
86f2e683
ILT
1248 bool add_to_table = false;
1249 typename Symbol_table_type::iterator add_loc = this->table_.end();
ead1e424 1250
55a93433
ILT
1251 // If the caller didn't give us a version, see if we get one from
1252 // the version script.
1253 if (*pversion == NULL)
1254 {
1255 const std::string& v(this->version_script_.get_symbol_version(*pname));
1256 if (!v.empty())
1257 *pversion = v.c_str();
1258 }
1259
ead1e424
ILT
1260 if (only_if_ref)
1261 {
306d9ef0 1262 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 1263 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 1264 return NULL;
306d9ef0
ILT
1265
1266 *pname = oldsym->name();
1267 *pversion = oldsym->version();
ead1e424
ILT
1268 }
1269 else
1270 {
14b31740 1271 // Canonicalize NAME and VERSION.
f0641a0b 1272 Stringpool::Key name_key;
cfd73a4e 1273 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 1274
14b31740 1275 Stringpool::Key version_key = 0;
306d9ef0 1276 if (*pversion != NULL)
cfd73a4e 1277 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 1278
ead1e424 1279 Symbol* const snull = NULL;
ead1e424 1280 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
1281 this->table_.insert(std::make_pair(std::make_pair(name_key,
1282 version_key),
ead1e424
ILT
1283 snull));
1284
1285 if (!ins.second)
1286 {
14b31740 1287 // We already have a symbol table entry for NAME/VERSION.
ead1e424 1288 oldsym = ins.first->second;
a3ad94ed 1289 gold_assert(oldsym != NULL);
ead1e424
ILT
1290 }
1291 else
1292 {
1293 // We haven't seen this symbol before.
a3ad94ed 1294 gold_assert(ins.first->second == NULL);
86f2e683
ILT
1295 add_to_table = true;
1296 add_loc = ins.first;
ead1e424
ILT
1297 oldsym = NULL;
1298 }
1299 }
1300
8851ecca
ILT
1301 const Target& target = parameters->target();
1302 if (!target.has_make_symbol())
86f2e683
ILT
1303 sym = new Sized_symbol<size>();
1304 else
ead1e424 1305 {
8851ecca
ILT
1306 gold_assert(target.get_size() == size);
1307 gold_assert(target.is_big_endian() ? big_endian : !big_endian);
86f2e683
ILT
1308 typedef Sized_target<size, big_endian> My_target;
1309 const My_target* sized_target =
8851ecca 1310 static_cast<const My_target*>(&target);
86f2e683
ILT
1311 sym = sized_target->make_symbol();
1312 if (sym == NULL)
1313 return NULL;
1314 }
ead1e424 1315
86f2e683
ILT
1316 if (add_to_table)
1317 add_loc->second = sym;
1318 else
1319 gold_assert(oldsym != NULL);
ead1e424 1320
7d1a9ebb 1321 *poldsym = this->get_sized_symbol<size>(oldsym);
ead1e424
ILT
1322
1323 return sym;
1324}
1325
1326// Define a symbol based on an Output_data.
1327
14b31740 1328Symbol*
9b07f471
ILT
1329Symbol_table::define_in_output_data(const char* name,
1330 const char* version,
1331 Output_data* od,
1332 uint64_t value,
1333 uint64_t symsize,
1334 elfcpp::STT type,
1335 elfcpp::STB binding,
ead1e424
ILT
1336 elfcpp::STV visibility,
1337 unsigned char nonvis,
1338 bool offset_is_from_end,
1339 bool only_if_ref)
1340{
8851ecca 1341 if (parameters->target().get_size() == 32)
86f2e683
ILT
1342 {
1343#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1344 return this->do_define_in_output_data<32>(name, version, od,
86f2e683
ILT
1345 value, symsize, type, binding,
1346 visibility, nonvis,
1347 offset_is_from_end,
1348 only_if_ref);
1349#else
1350 gold_unreachable();
1351#endif
1352 }
8851ecca 1353 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1354 {
1355#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1356 return this->do_define_in_output_data<64>(name, version, od,
86f2e683
ILT
1357 value, symsize, type, binding,
1358 visibility, nonvis,
1359 offset_is_from_end,
1360 only_if_ref);
1361#else
1362 gold_unreachable();
1363#endif
1364 }
ead1e424 1365 else
a3ad94ed 1366 gold_unreachable();
ead1e424
ILT
1367}
1368
1369// Define a symbol in an Output_data, sized version.
1370
1371template<int size>
14b31740 1372Sized_symbol<size>*
ead1e424 1373Symbol_table::do_define_in_output_data(
ead1e424 1374 const char* name,
14b31740 1375 const char* version,
ead1e424
ILT
1376 Output_data* od,
1377 typename elfcpp::Elf_types<size>::Elf_Addr value,
1378 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1379 elfcpp::STT type,
1380 elfcpp::STB binding,
1381 elfcpp::STV visibility,
1382 unsigned char nonvis,
1383 bool offset_is_from_end,
1384 bool only_if_ref)
1385{
1386 Sized_symbol<size>* sym;
86f2e683 1387 Sized_symbol<size>* oldsym;
ead1e424 1388
8851ecca 1389 if (parameters->target().is_big_endian())
193a53d9
ILT
1390 {
1391#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb
ILT
1392 sym = this->define_special_symbol<size, true>(&name, &version,
1393 only_if_ref, &oldsym);
193a53d9
ILT
1394#else
1395 gold_unreachable();
1396#endif
1397 }
ead1e424 1398 else
193a53d9
ILT
1399 {
1400#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb
ILT
1401 sym = this->define_special_symbol<size, false>(&name, &version,
1402 only_if_ref, &oldsym);
193a53d9
ILT
1403#else
1404 gold_unreachable();
1405#endif
1406 }
ead1e424
ILT
1407
1408 if (sym == NULL)
14b31740 1409 return NULL;
ead1e424 1410
75517b77
ILT
1411 sym->init_output_data(name, version, od, value, symsize, type, binding,
1412 visibility, nonvis, offset_is_from_end);
14b31740 1413
e5756efb 1414 if (oldsym == NULL)
55a93433
ILT
1415 {
1416 if (binding == elfcpp::STB_LOCAL
1417 || this->version_script_.symbol_is_local(name))
1418 this->force_local(sym);
75517b77
ILT
1419 else if (version != NULL)
1420 sym->set_is_default();
55a93433
ILT
1421 return sym;
1422 }
86f2e683 1423
e5756efb
ILT
1424 if (Symbol_table::should_override_with_special(oldsym))
1425 this->override_with_special(oldsym, sym);
1426 delete sym;
1427 return oldsym;
ead1e424
ILT
1428}
1429
1430// Define a symbol based on an Output_segment.
1431
14b31740 1432Symbol*
9b07f471 1433Symbol_table::define_in_output_segment(const char* name,
14b31740 1434 const char* version, Output_segment* os,
9b07f471
ILT
1435 uint64_t value,
1436 uint64_t symsize,
1437 elfcpp::STT type,
1438 elfcpp::STB binding,
ead1e424
ILT
1439 elfcpp::STV visibility,
1440 unsigned char nonvis,
1441 Symbol::Segment_offset_base offset_base,
1442 bool only_if_ref)
1443{
8851ecca 1444 if (parameters->target().get_size() == 32)
86f2e683
ILT
1445 {
1446#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1447 return this->do_define_in_output_segment<32>(name, version, os,
86f2e683
ILT
1448 value, symsize, type,
1449 binding, visibility, nonvis,
1450 offset_base, only_if_ref);
1451#else
1452 gold_unreachable();
1453#endif
1454 }
8851ecca 1455 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1456 {
1457#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1458 return this->do_define_in_output_segment<64>(name, version, os,
86f2e683
ILT
1459 value, symsize, type,
1460 binding, visibility, nonvis,
1461 offset_base, only_if_ref);
1462#else
1463 gold_unreachable();
1464#endif
1465 }
ead1e424 1466 else
a3ad94ed 1467 gold_unreachable();
ead1e424
ILT
1468}
1469
1470// Define a symbol in an Output_segment, sized version.
1471
1472template<int size>
14b31740 1473Sized_symbol<size>*
ead1e424 1474Symbol_table::do_define_in_output_segment(
ead1e424 1475 const char* name,
14b31740 1476 const char* version,
ead1e424
ILT
1477 Output_segment* os,
1478 typename elfcpp::Elf_types<size>::Elf_Addr value,
1479 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1480 elfcpp::STT type,
1481 elfcpp::STB binding,
1482 elfcpp::STV visibility,
1483 unsigned char nonvis,
1484 Symbol::Segment_offset_base offset_base,
1485 bool only_if_ref)
1486{
1487 Sized_symbol<size>* sym;
86f2e683 1488 Sized_symbol<size>* oldsym;
ead1e424 1489
8851ecca 1490 if (parameters->target().is_big_endian())
9025d29d
ILT
1491 {
1492#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb
ILT
1493 sym = this->define_special_symbol<size, true>(&name, &version,
1494 only_if_ref, &oldsym);
9025d29d
ILT
1495#else
1496 gold_unreachable();
1497#endif
1498 }
ead1e424 1499 else
9025d29d
ILT
1500 {
1501#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb
ILT
1502 sym = this->define_special_symbol<size, false>(&name, &version,
1503 only_if_ref, &oldsym);
9025d29d
ILT
1504#else
1505 gold_unreachable();
1506#endif
1507 }
ead1e424
ILT
1508
1509 if (sym == NULL)
14b31740 1510 return NULL;
ead1e424 1511
75517b77 1512 sym->init_output_segment(name, version, os, value, symsize, type, binding,
f3e9c5c5 1513 visibility, nonvis, offset_base);
14b31740 1514
e5756efb 1515 if (oldsym == NULL)
55a93433
ILT
1516 {
1517 if (binding == elfcpp::STB_LOCAL
1518 || this->version_script_.symbol_is_local(name))
1519 this->force_local(sym);
75517b77
ILT
1520 else if (version != NULL)
1521 sym->set_is_default();
55a93433
ILT
1522 return sym;
1523 }
86f2e683 1524
e5756efb
ILT
1525 if (Symbol_table::should_override_with_special(oldsym))
1526 this->override_with_special(oldsym, sym);
1527 delete sym;
1528 return oldsym;
ead1e424
ILT
1529}
1530
1531// Define a special symbol with a constant value. It is a multiple
1532// definition error if this symbol is already defined.
1533
14b31740 1534Symbol*
9b07f471
ILT
1535Symbol_table::define_as_constant(const char* name,
1536 const char* version,
1537 uint64_t value,
1538 uint64_t symsize,
1539 elfcpp::STT type,
1540 elfcpp::STB binding,
1541 elfcpp::STV visibility,
1542 unsigned char nonvis,
caa9d5d9
ILT
1543 bool only_if_ref,
1544 bool force_override)
ead1e424 1545{
8851ecca 1546 if (parameters->target().get_size() == 32)
86f2e683
ILT
1547 {
1548#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1549 return this->do_define_as_constant<32>(name, version, value,
86f2e683 1550 symsize, type, binding,
caa9d5d9
ILT
1551 visibility, nonvis, only_if_ref,
1552 force_override);
86f2e683
ILT
1553#else
1554 gold_unreachable();
1555#endif
1556 }
8851ecca 1557 else if (parameters->target().get_size() == 64)
86f2e683
ILT
1558 {
1559#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1560 return this->do_define_as_constant<64>(name, version, value,
86f2e683 1561 symsize, type, binding,
caa9d5d9
ILT
1562 visibility, nonvis, only_if_ref,
1563 force_override);
86f2e683
ILT
1564#else
1565 gold_unreachable();
1566#endif
1567 }
ead1e424 1568 else
a3ad94ed 1569 gold_unreachable();
ead1e424
ILT
1570}
1571
1572// Define a symbol as a constant, sized version.
1573
1574template<int size>
14b31740 1575Sized_symbol<size>*
ead1e424 1576Symbol_table::do_define_as_constant(
ead1e424 1577 const char* name,
14b31740 1578 const char* version,
ead1e424
ILT
1579 typename elfcpp::Elf_types<size>::Elf_Addr value,
1580 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1581 elfcpp::STT type,
1582 elfcpp::STB binding,
1583 elfcpp::STV visibility,
1584 unsigned char nonvis,
caa9d5d9
ILT
1585 bool only_if_ref,
1586 bool force_override)
ead1e424
ILT
1587{
1588 Sized_symbol<size>* sym;
86f2e683 1589 Sized_symbol<size>* oldsym;
ead1e424 1590
8851ecca 1591 if (parameters->target().is_big_endian())
9025d29d
ILT
1592 {
1593#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
7d1a9ebb
ILT
1594 sym = this->define_special_symbol<size, true>(&name, &version,
1595 only_if_ref, &oldsym);
9025d29d
ILT
1596#else
1597 gold_unreachable();
1598#endif
1599 }
ead1e424 1600 else
9025d29d
ILT
1601 {
1602#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
7d1a9ebb
ILT
1603 sym = this->define_special_symbol<size, false>(&name, &version,
1604 only_if_ref, &oldsym);
9025d29d
ILT
1605#else
1606 gold_unreachable();
1607#endif
1608 }
ead1e424
ILT
1609
1610 if (sym == NULL)
14b31740 1611 return NULL;
ead1e424 1612
75517b77
ILT
1613 sym->init_constant(name, version, value, symsize, type, binding, visibility,
1614 nonvis);
14b31740 1615
e5756efb 1616 if (oldsym == NULL)
55a93433 1617 {
686c8caf
ILT
1618 // Version symbols are absolute symbols with name == version.
1619 // We don't want to force them to be local.
1620 if ((version == NULL
1621 || name != version
1622 || value != 0)
1623 && (binding == elfcpp::STB_LOCAL
1624 || this->version_script_.symbol_is_local(name)))
55a93433 1625 this->force_local(sym);
75517b77
ILT
1626 else if (version != NULL
1627 && (name != version || value != 0))
1628 sym->set_is_default();
55a93433
ILT
1629 return sym;
1630 }
86f2e683 1631
caa9d5d9 1632 if (force_override || Symbol_table::should_override_with_special(oldsym))
e5756efb
ILT
1633 this->override_with_special(oldsym, sym);
1634 delete sym;
1635 return oldsym;
ead1e424
ILT
1636}
1637
1638// Define a set of symbols in output sections.
1639
1640void
9b07f471 1641Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
1642 const Define_symbol_in_section* p,
1643 bool only_if_ref)
ead1e424
ILT
1644{
1645 for (int i = 0; i < count; ++i, ++p)
1646 {
1647 Output_section* os = layout->find_output_section(p->output_section);
1648 if (os != NULL)
9b07f471 1649 this->define_in_output_data(p->name, NULL, os, p->value,
14b31740
ILT
1650 p->size, p->type, p->binding,
1651 p->visibility, p->nonvis,
a445fddf
ILT
1652 p->offset_is_from_end,
1653 only_if_ref || p->only_if_ref);
ead1e424 1654 else
9b07f471 1655 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 1656 p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
1657 only_if_ref || p->only_if_ref,
1658 false);
ead1e424
ILT
1659 }
1660}
1661
1662// Define a set of symbols in output segments.
1663
1664void
9b07f471 1665Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
1666 const Define_symbol_in_segment* p,
1667 bool only_if_ref)
ead1e424
ILT
1668{
1669 for (int i = 0; i < count; ++i, ++p)
1670 {
1671 Output_segment* os = layout->find_output_segment(p->segment_type,
1672 p->segment_flags_set,
1673 p->segment_flags_clear);
1674 if (os != NULL)
9b07f471 1675 this->define_in_output_segment(p->name, NULL, os, p->value,
14b31740
ILT
1676 p->size, p->type, p->binding,
1677 p->visibility, p->nonvis,
a445fddf
ILT
1678 p->offset_base,
1679 only_if_ref || p->only_if_ref);
ead1e424 1680 else
9b07f471 1681 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 1682 p->binding, p->visibility, p->nonvis,
caa9d5d9
ILT
1683 only_if_ref || p->only_if_ref,
1684 false);
ead1e424
ILT
1685 }
1686}
1687
46fe1623
ILT
1688// Define CSYM using a COPY reloc. POSD is the Output_data where the
1689// symbol should be defined--typically a .dyn.bss section. VALUE is
1690// the offset within POSD.
1691
1692template<int size>
1693void
fe8718a4 1694Symbol_table::define_with_copy_reloc(
fe8718a4
ILT
1695 Sized_symbol<size>* csym,
1696 Output_data* posd,
1697 typename elfcpp::Elf_types<size>::Elf_Addr value)
46fe1623
ILT
1698{
1699 gold_assert(csym->is_from_dynobj());
1700 gold_assert(!csym->is_copied_from_dynobj());
1701 Object* object = csym->object();
1702 gold_assert(object->is_dynamic());
1703 Dynobj* dynobj = static_cast<Dynobj*>(object);
1704
1705 // Our copied variable has to override any variable in a shared
1706 // library.
1707 elfcpp::STB binding = csym->binding();
1708 if (binding == elfcpp::STB_WEAK)
1709 binding = elfcpp::STB_GLOBAL;
1710
9b07f471 1711 this->define_in_output_data(csym->name(), csym->version(),
46fe1623
ILT
1712 posd, value, csym->symsize(),
1713 csym->type(), binding,
1714 csym->visibility(), csym->nonvis(),
1715 false, false);
1716
1717 csym->set_is_copied_from_dynobj();
1718 csym->set_needs_dynsym_entry();
1719
1720 this->copied_symbol_dynobjs_[csym] = dynobj;
1721
1722 // We have now defined all aliases, but we have not entered them all
1723 // in the copied_symbol_dynobjs_ map.
1724 if (csym->has_alias())
1725 {
1726 Symbol* sym = csym;
1727 while (true)
1728 {
1729 sym = this->weak_aliases_[sym];
1730 if (sym == csym)
1731 break;
1732 gold_assert(sym->output_data() == posd);
1733
1734 sym->set_is_copied_from_dynobj();
1735 this->copied_symbol_dynobjs_[sym] = dynobj;
1736 }
1737 }
1738}
1739
1740// SYM is defined using a COPY reloc. Return the dynamic object where
1741// the original definition was found.
1742
1743Dynobj*
1744Symbol_table::get_copy_source(const Symbol* sym) const
1745{
1746 gold_assert(sym->is_copied_from_dynobj());
1747 Copied_symbol_dynobjs::const_iterator p =
1748 this->copied_symbol_dynobjs_.find(sym);
1749 gold_assert(p != this->copied_symbol_dynobjs_.end());
1750 return p->second;
1751}
1752
f3e9c5c5
ILT
1753// Add any undefined symbols named on the command line.
1754
1755void
1756Symbol_table::add_undefined_symbols_from_command_line()
1757{
1758 if (parameters->options().any_undefined())
1759 {
1760 if (parameters->target().get_size() == 32)
1761 {
5adf9721 1762#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
f3e9c5c5
ILT
1763 this->do_add_undefined_symbols_from_command_line<32>();
1764#else
1765 gold_unreachable();
1766#endif
1767 }
1768 else if (parameters->target().get_size() == 64)
1769 {
1770#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1771 this->do_add_undefined_symbols_from_command_line<64>();
1772#else
1773 gold_unreachable();
1774#endif
1775 }
1776 else
1777 gold_unreachable();
1778 }
1779}
1780
1781template<int size>
1782void
1783Symbol_table::do_add_undefined_symbols_from_command_line()
1784{
1785 for (options::String_set::const_iterator p =
1786 parameters->options().undefined_begin();
1787 p != parameters->options().undefined_end();
1788 ++p)
1789 {
1790 const char* name = p->c_str();
1791
1792 if (this->lookup(name) != NULL)
1793 continue;
1794
1795 const char* version = NULL;
1796
1797 Sized_symbol<size>* sym;
1798 Sized_symbol<size>* oldsym;
1799 if (parameters->target().is_big_endian())
1800 {
1801#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1802 sym = this->define_special_symbol<size, true>(&name, &version,
1803 false, &oldsym);
1804#else
1805 gold_unreachable();
1806#endif
1807 }
1808 else
1809 {
1810#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1811 sym = this->define_special_symbol<size, false>(&name, &version,
1812 false, &oldsym);
1813#else
1814 gold_unreachable();
1815#endif
1816 }
1817
1818 gold_assert(oldsym == NULL);
1819
75517b77 1820 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
f3e9c5c5
ILT
1821 elfcpp::STV_DEFAULT, 0);
1822 ++this->saw_undefined_;
1823 }
1824}
1825
a3ad94ed
ILT
1826// Set the dynamic symbol indexes. INDEX is the index of the first
1827// global dynamic symbol. Pointers to the symbols are stored into the
1828// vector SYMS. The names are added to DYNPOOL. This returns an
1829// updated dynamic symbol index.
1830
1831unsigned int
9b07f471 1832Symbol_table::set_dynsym_indexes(unsigned int index,
a3ad94ed 1833 std::vector<Symbol*>* syms,
14b31740
ILT
1834 Stringpool* dynpool,
1835 Versions* versions)
a3ad94ed
ILT
1836{
1837 for (Symbol_table_type::iterator p = this->table_.begin();
1838 p != this->table_.end();
1839 ++p)
1840 {
1841 Symbol* sym = p->second;
16649710
ILT
1842
1843 // Note that SYM may already have a dynamic symbol index, since
1844 // some symbols appear more than once in the symbol table, with
1845 // and without a version.
1846
436ca963 1847 if (!sym->should_add_dynsym_entry())
16649710
ILT
1848 sym->set_dynsym_index(-1U);
1849 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1850 {
1851 sym->set_dynsym_index(index);
1852 ++index;
1853 syms->push_back(sym);
cfd73a4e 1854 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
1855
1856 // Record any version information.
09124467
ILT
1857 if (sym->version() != NULL)
1858 versions->record_version(this, dynpool, sym);
a3ad94ed
ILT
1859 }
1860 }
1861
14b31740
ILT
1862 // Finish up the versions. In some cases this may add new dynamic
1863 // symbols.
9b07f471 1864 index = versions->finalize(this, index, syms);
14b31740 1865
a3ad94ed
ILT
1866 return index;
1867}
1868
c06b7b0b 1869// Set the final values for all the symbols. The index of the first
55a93433
ILT
1870// global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1871// file offset OFF. Add their names to POOL. Return the new file
1872// offset. Update *PLOCAL_SYMCOUNT if necessary.
54dc6425 1873
75f65a3e 1874off_t
55a93433
ILT
1875Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1876 size_t dyncount, Stringpool* pool,
1877 unsigned int *plocal_symcount)
54dc6425 1878{
f6ce93d6
ILT
1879 off_t ret;
1880
55a93433
ILT
1881 gold_assert(*plocal_symcount != 0);
1882 this->first_global_index_ = *plocal_symcount;
c06b7b0b 1883
16649710
ILT
1884 this->dynamic_offset_ = dynoff;
1885 this->first_dynamic_global_index_ = dyn_global_index;
1886 this->dynamic_count_ = dyncount;
1887
8851ecca 1888 if (parameters->target().get_size() == 32)
9025d29d
ILT
1889 {
1890#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
55a93433 1891 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
9025d29d
ILT
1892#else
1893 gold_unreachable();
1894#endif
1895 }
8851ecca 1896 else if (parameters->target().get_size() == 64)
9025d29d
ILT
1897 {
1898#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
55a93433 1899 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
9025d29d
ILT
1900#else
1901 gold_unreachable();
1902#endif
1903 }
61ba1cf9 1904 else
a3ad94ed 1905 gold_unreachable();
f6ce93d6
ILT
1906
1907 // Now that we have the final symbol table, we can reliably note
1908 // which symbols should get warnings.
cb295612 1909 this->warnings_.note_warnings(this);
f6ce93d6
ILT
1910
1911 return ret;
75f65a3e
ILT
1912}
1913
55a93433
ILT
1914// SYM is going into the symbol table at *PINDEX. Add the name to
1915// POOL, update *PINDEX and *POFF.
1916
1917template<int size>
1918void
1919Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1920 unsigned int* pindex, off_t* poff)
1921{
1922 sym->set_symtab_index(*pindex);
1923 pool->add(sym->name(), false, NULL);
1924 ++*pindex;
1925 *poff += elfcpp::Elf_sizes<size>::sym_size;
1926}
1927
ead1e424
ILT
1928// Set the final value for all the symbols. This is called after
1929// Layout::finalize, so all the output sections have their final
1930// address.
75f65a3e
ILT
1931
1932template<int size>
1933off_t
55a93433
ILT
1934Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1935 unsigned int* plocal_symcount)
75f65a3e 1936{
ead1e424 1937 off = align_address(off, size >> 3);
75f65a3e
ILT
1938 this->offset_ = off;
1939
55a93433
ILT
1940 unsigned int index = *plocal_symcount;
1941 const unsigned int orig_index = index;
c06b7b0b 1942
55a93433
ILT
1943 // First do all the symbols which have been forced to be local, as
1944 // they must appear before all global symbols.
1945 for (Forced_locals::iterator p = this->forced_locals_.begin();
1946 p != this->forced_locals_.end();
1947 ++p)
1948 {
1949 Symbol* sym = *p;
1950 gold_assert(sym->is_forced_local());
1951 if (this->sized_finalize_symbol<size>(sym))
1952 {
1953 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1954 ++*plocal_symcount;
1955 }
1956 }
1957
1958 // Now do all the remaining symbols.
c06b7b0b
ILT
1959 for (Symbol_table_type::iterator p = this->table_.begin();
1960 p != this->table_.end();
1961 ++p)
54dc6425 1962 {
55a93433
ILT
1963 Symbol* sym = p->second;
1964 if (this->sized_finalize_symbol<size>(sym))
1965 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1966 }
54dc6425 1967
55a93433 1968 this->output_count_ = index - orig_index;
a3ad94ed 1969
55a93433
ILT
1970 return off;
1971}
75f65a3e 1972
55a93433
ILT
1973// Finalize the symbol SYM. This returns true if the symbol should be
1974// added to the symbol table, false otherwise.
008db82e 1975
55a93433
ILT
1976template<int size>
1977bool
1978Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1979{
ef9beddf
ILT
1980 typedef typename Sized_symbol<size>::Value_type Value_type;
1981
55a93433 1982 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
75f65a3e 1983
55a93433
ILT
1984 // The default version of a symbol may appear twice in the symbol
1985 // table. We only need to finalize it once.
1986 if (sym->has_symtab_index())
1987 return false;
ead1e424 1988
55a93433
ILT
1989 if (!sym->in_reg())
1990 {
1991 gold_assert(!sym->has_symtab_index());
1992 sym->set_symtab_index(-1U);
1993 gold_assert(sym->dynsym_index() == -1U);
1994 return false;
1995 }
ead1e424 1996
ef9beddf 1997 Value_type value;
ead1e424 1998
55a93433
ILT
1999 switch (sym->source())
2000 {
2001 case Symbol::FROM_OBJECT:
2002 {
d491d34e
ILT
2003 bool is_ordinary;
2004 unsigned int shndx = sym->shndx(&is_ordinary);
ead1e424 2005
55a93433 2006 // FIXME: We need some target specific support here.
d491d34e 2007 if (!is_ordinary
0dfbdef4
ILT
2008 && shndx != elfcpp::SHN_ABS
2009 && shndx != elfcpp::SHN_COMMON)
55a93433
ILT
2010 {
2011 gold_error(_("%s: unsupported symbol section 0x%x"),
2012 sym->demangled_name().c_str(), shndx);
2013 shndx = elfcpp::SHN_UNDEF;
ead1e424 2014 }
ead1e424 2015
55a93433
ILT
2016 Object* symobj = sym->object();
2017 if (symobj->is_dynamic())
ead1e424 2018 {
55a93433
ILT
2019 value = 0;
2020 shndx = elfcpp::SHN_UNDEF;
ead1e424 2021 }
55a93433
ILT
2022 else if (shndx == elfcpp::SHN_UNDEF)
2023 value = 0;
d491d34e
ILT
2024 else if (!is_ordinary
2025 && (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON))
55a93433
ILT
2026 value = sym->value();
2027 else
ead1e424 2028 {
55a93433 2029 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 2030 Output_section* os = relobj->output_section(shndx);
55a93433
ILT
2031
2032 if (os == NULL)
ead1e424 2033 {
55a93433
ILT
2034 sym->set_symtab_index(-1U);
2035 gold_assert(sym->dynsym_index() == -1U);
2036 return false;
ead1e424 2037 }
55a93433 2038
ef9beddf
ILT
2039 uint64_t secoff64 = relobj->output_section_offset(shndx);
2040 Value_type secoff = convert_types<Value_type, uint64_t>(secoff64);
55a93433
ILT
2041 if (sym->type() == elfcpp::STT_TLS)
2042 value = sym->value() + os->tls_offset() + secoff;
2043 else
2044 value = sym->value() + os->address() + secoff;
ead1e424 2045 }
55a93433
ILT
2046 }
2047 break;
2048
2049 case Symbol::IN_OUTPUT_DATA:
2050 {
2051 Output_data* od = sym->output_data();
155a0dd7
ILT
2052 value = sym->value();
2053 if (sym->type() != elfcpp::STT_TLS)
2054 value += od->address();
2055 else
2056 {
2057 Output_section* os = od->output_section();
2058 gold_assert(os != NULL);
2059 value += os->tls_offset() + (od->address() - os->address());
2060 }
55a93433
ILT
2061 if (sym->offset_is_from_end())
2062 value += od->data_size();
2063 }
2064 break;
2065
2066 case Symbol::IN_OUTPUT_SEGMENT:
2067 {
2068 Output_segment* os = sym->output_segment();
edfbb029
CC
2069 value = sym->value();
2070 if (sym->type() != elfcpp::STT_TLS)
2071 value += os->vaddr();
55a93433
ILT
2072 switch (sym->offset_base())
2073 {
2074 case Symbol::SEGMENT_START:
2075 break;
2076 case Symbol::SEGMENT_END:
2077 value += os->memsz();
2078 break;
2079 case Symbol::SEGMENT_BSS:
2080 value += os->filesz();
2081 break;
2082 default:
2083 gold_unreachable();
2084 }
2085 }
2086 break;
ead1e424 2087
f3e9c5c5 2088 case Symbol::IS_CONSTANT:
55a93433
ILT
2089 value = sym->value();
2090 break;
ead1e424 2091
f3e9c5c5
ILT
2092 case Symbol::IS_UNDEFINED:
2093 value = 0;
2094 break;
2095
55a93433
ILT
2096 default:
2097 gold_unreachable();
2098 }
ead1e424 2099
55a93433 2100 sym->set_value(value);
9e2dcb77 2101
8851ecca 2102 if (parameters->options().strip_all())
55a93433
ILT
2103 {
2104 sym->set_symtab_index(-1U);
2105 return false;
54dc6425 2106 }
75f65a3e 2107
55a93433 2108 return true;
54dc6425
ILT
2109}
2110
61ba1cf9
ILT
2111// Write out the global symbols.
2112
2113void
9a2d6984
ILT
2114Symbol_table::write_globals(const Input_objects* input_objects,
2115 const Stringpool* sympool,
d491d34e
ILT
2116 const Stringpool* dynpool,
2117 Output_symtab_xindex* symtab_xindex,
2118 Output_symtab_xindex* dynsym_xindex,
2119 Output_file* of) const
61ba1cf9 2120{
8851ecca 2121 switch (parameters->size_and_endianness())
61ba1cf9 2122 {
9025d29d 2123#ifdef HAVE_TARGET_32_LITTLE
8851ecca
ILT
2124 case Parameters::TARGET_32_LITTLE:
2125 this->sized_write_globals<32, false>(input_objects, sympool,
d491d34e
ILT
2126 dynpool, symtab_xindex,
2127 dynsym_xindex, of);
8851ecca 2128 break;
9025d29d 2129#endif
8851ecca
ILT
2130#ifdef HAVE_TARGET_32_BIG
2131 case Parameters::TARGET_32_BIG:
2132 this->sized_write_globals<32, true>(input_objects, sympool,
d491d34e
ILT
2133 dynpool, symtab_xindex,
2134 dynsym_xindex, of);
8851ecca 2135 break;
9025d29d 2136#endif
9025d29d 2137#ifdef HAVE_TARGET_64_LITTLE
8851ecca
ILT
2138 case Parameters::TARGET_64_LITTLE:
2139 this->sized_write_globals<64, false>(input_objects, sympool,
d491d34e
ILT
2140 dynpool, symtab_xindex,
2141 dynsym_xindex, of);
8851ecca 2142 break;
9025d29d 2143#endif
8851ecca
ILT
2144#ifdef HAVE_TARGET_64_BIG
2145 case Parameters::TARGET_64_BIG:
2146 this->sized_write_globals<64, true>(input_objects, sympool,
d491d34e
ILT
2147 dynpool, symtab_xindex,
2148 dynsym_xindex, of);
8851ecca
ILT
2149 break;
2150#endif
2151 default:
2152 gold_unreachable();
61ba1cf9 2153 }
61ba1cf9
ILT
2154}
2155
2156// Write out the global symbols.
2157
2158template<int size, bool big_endian>
2159void
9a2d6984 2160Symbol_table::sized_write_globals(const Input_objects* input_objects,
61ba1cf9 2161 const Stringpool* sympool,
16649710 2162 const Stringpool* dynpool,
d491d34e
ILT
2163 Output_symtab_xindex* symtab_xindex,
2164 Output_symtab_xindex* dynsym_xindex,
61ba1cf9
ILT
2165 Output_file* of) const
2166{
8851ecca 2167 const Target& target = parameters->target();
9a2d6984 2168
61ba1cf9 2169 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
55a93433
ILT
2170
2171 const unsigned int output_count = this->output_count_;
2172 const section_size_type oview_size = output_count * sym_size;
2173 const unsigned int first_global_index = this->first_global_index_;
5fe2a0f5
ILT
2174 unsigned char* psyms;
2175 if (this->offset_ == 0 || output_count == 0)
2176 psyms = NULL;
2177 else
2178 psyms = of->get_output_view(this->offset_, oview_size);
16649710 2179
55a93433
ILT
2180 const unsigned int dynamic_count = this->dynamic_count_;
2181 const section_size_type dynamic_size = dynamic_count * sym_size;
2182 const unsigned int first_dynamic_global_index =
2183 this->first_dynamic_global_index_;
16649710 2184 unsigned char* dynamic_view;
5fe2a0f5 2185 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
16649710
ILT
2186 dynamic_view = NULL;
2187 else
2188 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 2189
61ba1cf9
ILT
2190 for (Symbol_table_type::const_iterator p = this->table_.begin();
2191 p != this->table_.end();
2192 ++p)
2193 {
2194 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2195
9a2d6984
ILT
2196 // Possibly warn about unresolved symbols in shared libraries.
2197 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
e2827e5f 2198
a3ad94ed 2199 unsigned int sym_index = sym->symtab_index();
16649710
ILT
2200 unsigned int dynsym_index;
2201 if (dynamic_view == NULL)
2202 dynsym_index = -1U;
2203 else
2204 dynsym_index = sym->dynsym_index();
2205
2206 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
2207 {
2208 // This symbol is not included in the output file.
2209 continue;
2210 }
16649710 2211
ead1e424 2212 unsigned int shndx;
88dd47ac
ILT
2213 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2214 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
ead1e424
ILT
2215 switch (sym->source())
2216 {
2217 case Symbol::FROM_OBJECT:
2218 {
d491d34e
ILT
2219 bool is_ordinary;
2220 unsigned int in_shndx = sym->shndx(&is_ordinary);
ead1e424
ILT
2221
2222 // FIXME: We need some target specific support here.
d491d34e 2223 if (!is_ordinary
0dfbdef4
ILT
2224 && in_shndx != elfcpp::SHN_ABS
2225 && in_shndx != elfcpp::SHN_COMMON)
ead1e424 2226 {
75f2446e 2227 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 2228 sym->demangled_name().c_str(), in_shndx);
75f2446e 2229 shndx = in_shndx;
f6ce93d6 2230 }
ead1e424
ILT
2231 else
2232 {
75f2446e
ILT
2233 Object* symobj = sym->object();
2234 if (symobj->is_dynamic())
2235 {
2236 if (sym->needs_dynsym_value())
8851ecca 2237 dynsym_value = target.dynsym_value(sym);
75f2446e
ILT
2238 shndx = elfcpp::SHN_UNDEF;
2239 }
2240 else if (in_shndx == elfcpp::SHN_UNDEF
d491d34e
ILT
2241 || (!is_ordinary
2242 && (in_shndx == elfcpp::SHN_ABS
2243 || in_shndx == elfcpp::SHN_COMMON)))
75f2446e
ILT
2244 shndx = in_shndx;
2245 else
2246 {
2247 Relobj* relobj = static_cast<Relobj*>(symobj);
ef9beddf 2248 Output_section* os = relobj->output_section(in_shndx);
75f2446e
ILT
2249 gold_assert(os != NULL);
2250 shndx = os->out_shndx();
88dd47ac 2251
d491d34e
ILT
2252 if (shndx >= elfcpp::SHN_LORESERVE)
2253 {
2254 if (sym_index != -1U)
2255 symtab_xindex->add(sym_index, shndx);
2256 if (dynsym_index != -1U)
2257 dynsym_xindex->add(dynsym_index, shndx);
2258 shndx = elfcpp::SHN_XINDEX;
2259 }
2260
88dd47ac
ILT
2261 // In object files symbol values are section
2262 // relative.
8851ecca 2263 if (parameters->options().relocatable())
88dd47ac 2264 sym_value -= os->address();
75f2446e 2265 }
ead1e424
ILT
2266 }
2267 }
2268 break;
2269
2270 case Symbol::IN_OUTPUT_DATA:
2271 shndx = sym->output_data()->out_shndx();
d491d34e
ILT
2272 if (shndx >= elfcpp::SHN_LORESERVE)
2273 {
2274 if (sym_index != -1U)
2275 symtab_xindex->add(sym_index, shndx);
2276 if (dynsym_index != -1U)
2277 dynsym_xindex->add(dynsym_index, shndx);
2278 shndx = elfcpp::SHN_XINDEX;
2279 }
ead1e424
ILT
2280 break;
2281
2282 case Symbol::IN_OUTPUT_SEGMENT:
2283 shndx = elfcpp::SHN_ABS;
2284 break;
2285
f3e9c5c5 2286 case Symbol::IS_CONSTANT:
ead1e424
ILT
2287 shndx = elfcpp::SHN_ABS;
2288 break;
2289
f3e9c5c5
ILT
2290 case Symbol::IS_UNDEFINED:
2291 shndx = elfcpp::SHN_UNDEF;
2292 break;
2293
ead1e424 2294 default:
a3ad94ed 2295 gold_unreachable();
ead1e424 2296 }
61ba1cf9 2297
16649710
ILT
2298 if (sym_index != -1U)
2299 {
55a93433
ILT
2300 sym_index -= first_global_index;
2301 gold_assert(sym_index < output_count);
2302 unsigned char* ps = psyms + (sym_index * sym_size);
7d1a9ebb
ILT
2303 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2304 sympool, ps);
16649710 2305 }
61ba1cf9 2306
16649710
ILT
2307 if (dynsym_index != -1U)
2308 {
2309 dynsym_index -= first_dynamic_global_index;
2310 gold_assert(dynsym_index < dynamic_count);
2311 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
7d1a9ebb
ILT
2312 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2313 dynpool, pd);
16649710 2314 }
61ba1cf9
ILT
2315 }
2316
c06b7b0b 2317 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
2318 if (dynamic_view != NULL)
2319 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2320}
2321
2322// Write out the symbol SYM, in section SHNDX, to P. POOL is the
2323// strtab holding the name.
2324
2325template<int size, bool big_endian>
2326void
ab5c9e90
ILT
2327Symbol_table::sized_write_symbol(
2328 Sized_symbol<size>* sym,
2329 typename elfcpp::Elf_types<size>::Elf_Addr value,
2330 unsigned int shndx,
2331 const Stringpool* pool,
7d1a9ebb 2332 unsigned char* p) const
16649710
ILT
2333{
2334 elfcpp::Sym_write<size, big_endian> osym(p);
2335 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 2336 osym.put_st_value(value);
3f7c5e1d
CD
2337 // Use a symbol size of zero for undefined symbols.
2338 osym.put_st_size(shndx == elfcpp::SHN_UNDEF ? 0 : sym->symsize());
55a93433
ILT
2339 // A version script may have overridden the default binding.
2340 if (sym->is_forced_local())
2341 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
2342 else
2343 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
16649710
ILT
2344 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2345 osym.put_st_shndx(shndx);
61ba1cf9
ILT
2346}
2347
9a2d6984
ILT
2348// Check for unresolved symbols in shared libraries. This is
2349// controlled by the --allow-shlib-undefined option.
2350
2351// We only warn about libraries for which we have seen all the
2352// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2353// which were not seen in this link. If we didn't see a DT_NEEDED
2354// entry, we aren't going to be able to reliably report whether the
2355// symbol is undefined.
2356
2357// We also don't warn about libraries found in the system library
2358// directory (the directory were we find libc.so); we assume that
2359// those libraries are OK. This heuristic avoids problems in
2360// GNU/Linux, in which -ldl can have undefined references satisfied by
2361// ld-linux.so.
2362
2363inline void
2364Symbol_table::warn_about_undefined_dynobj_symbol(
2365 const Input_objects* input_objects,
2366 Symbol* sym) const
2367{
d491d34e 2368 bool dummy;
9a2d6984
ILT
2369 if (sym->source() == Symbol::FROM_OBJECT
2370 && sym->object()->is_dynamic()
d491d34e 2371 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
9a2d6984 2372 && sym->binding() != elfcpp::STB_WEAK
8851ecca
ILT
2373 && !parameters->options().allow_shlib_undefined()
2374 && !parameters->target().is_defined_by_abi(sym)
9a2d6984
ILT
2375 && !input_objects->found_in_system_library_directory(sym->object()))
2376 {
2377 // A very ugly cast.
2378 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2379 if (!dynobj->has_unknown_needed_entries())
096b02cf
CS
2380 {
2381 if (sym->version())
2382 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2383 sym->object()->name().c_str(),
2384 sym->demangled_name().c_str(),
2385 sym->version());
2386 else
2387 gold_error(_("%s: undefined reference to '%s'"),
2388 sym->object()->name().c_str(),
2389 sym->demangled_name().c_str());
2390 }
9a2d6984
ILT
2391 }
2392}
2393
a3ad94ed
ILT
2394// Write out a section symbol. Return the update offset.
2395
2396void
9025d29d 2397Symbol_table::write_section_symbol(const Output_section *os,
d491d34e 2398 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
2399 Output_file* of,
2400 off_t offset) const
2401{
8851ecca 2402 switch (parameters->size_and_endianness())
a3ad94ed 2403 {
9025d29d 2404#ifdef HAVE_TARGET_32_LITTLE
8851ecca 2405 case Parameters::TARGET_32_LITTLE:
d491d34e
ILT
2406 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2407 offset);
8851ecca 2408 break;
9025d29d 2409#endif
8851ecca
ILT
2410#ifdef HAVE_TARGET_32_BIG
2411 case Parameters::TARGET_32_BIG:
d491d34e
ILT
2412 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2413 offset);
8851ecca 2414 break;
9025d29d 2415#endif
9025d29d 2416#ifdef HAVE_TARGET_64_LITTLE
8851ecca 2417 case Parameters::TARGET_64_LITTLE:
d491d34e
ILT
2418 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2419 offset);
8851ecca 2420 break;
9025d29d 2421#endif
8851ecca
ILT
2422#ifdef HAVE_TARGET_64_BIG
2423 case Parameters::TARGET_64_BIG:
d491d34e
ILT
2424 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2425 offset);
8851ecca
ILT
2426 break;
2427#endif
2428 default:
2429 gold_unreachable();
a3ad94ed 2430 }
a3ad94ed
ILT
2431}
2432
2433// Write out a section symbol, specialized for size and endianness.
2434
2435template<int size, bool big_endian>
2436void
2437Symbol_table::sized_write_section_symbol(const Output_section* os,
d491d34e 2438 Output_symtab_xindex* symtab_xindex,
a3ad94ed
ILT
2439 Output_file* of,
2440 off_t offset) const
2441{
2442 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2443
2444 unsigned char* pov = of->get_output_view(offset, sym_size);
2445
2446 elfcpp::Sym_write<size, big_endian> osym(pov);
2447 osym.put_st_name(0);
2448 osym.put_st_value(os->address());
2449 osym.put_st_size(0);
2450 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2451 elfcpp::STT_SECTION));
2452 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
d491d34e
ILT
2453
2454 unsigned int shndx = os->out_shndx();
2455 if (shndx >= elfcpp::SHN_LORESERVE)
2456 {
2457 symtab_xindex->add(os->symtab_index(), shndx);
2458 shndx = elfcpp::SHN_XINDEX;
2459 }
2460 osym.put_st_shndx(shndx);
a3ad94ed
ILT
2461
2462 of->write_output_view(offset, sym_size, pov);
2463}
2464
abaa3995
ILT
2465// Print statistical information to stderr. This is used for --stats.
2466
2467void
2468Symbol_table::print_stats() const
2469{
2470#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2471 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2472 program_name, this->table_.size(), this->table_.bucket_count());
2473#else
2474 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2475 program_name, this->table_.size());
2476#endif
ad8f37d1 2477 this->namepool_.print_stats("symbol table stringpool");
abaa3995
ILT
2478}
2479
ff541f30
ILT
2480// We check for ODR violations by looking for symbols with the same
2481// name for which the debugging information reports that they were
2482// defined in different source locations. When comparing the source
2483// location, we consider instances with the same base filename and
2484// line number to be the same. This is because different object
2485// files/shared libraries can include the same header file using
2486// different paths, and we don't want to report an ODR violation in
2487// that case.
2488
2489// This struct is used to compare line information, as returned by
7bf1f802 2490// Dwarf_line_info::one_addr2line. It implements a < comparison
ff541f30
ILT
2491// operator used with std::set.
2492
2493struct Odr_violation_compare
2494{
2495 bool
2496 operator()(const std::string& s1, const std::string& s2) const
2497 {
2498 std::string::size_type pos1 = s1.rfind('/');
2499 std::string::size_type pos2 = s2.rfind('/');
2500 if (pos1 == std::string::npos
2501 || pos2 == std::string::npos)
2502 return s1 < s2;
2503 return s1.compare(pos1, std::string::npos,
2504 s2, pos2, std::string::npos) < 0;
2505 }
2506};
2507
70e654ba
ILT
2508// Check candidate_odr_violations_ to find symbols with the same name
2509// but apparently different definitions (different source-file/line-no).
2510
2511void
17a1d0a9
ILT
2512Symbol_table::detect_odr_violations(const Task* task,
2513 const char* output_file_name) const
70e654ba
ILT
2514{
2515 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2516 it != candidate_odr_violations_.end();
2517 ++it)
2518 {
2519 const char* symbol_name = it->first;
2520 // We use a sorted set so the output is deterministic.
ff541f30 2521 std::set<std::string, Odr_violation_compare> line_nums;
70e654ba 2522
b01c0a4a
ILT
2523 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2524 locs = it->second.begin();
2525 locs != it->second.end();
2526 ++locs)
70e654ba
ILT
2527 {
2528 // We need to lock the object in order to read it. This
17a1d0a9
ILT
2529 // means that we have to run in a singleton Task. If we
2530 // want to run this in a general Task for better
2531 // performance, we will need one Task for object, plus
2532 // appropriate locking to ensure that we don't conflict with
e4e5049b
CS
2533 // other uses of the object. Also note, one_addr2line is not
2534 // currently thread-safe.
17a1d0a9 2535 Task_lock_obj<Object> tl(task, locs->object);
e4e5049b 2536 // 16 is the size of the object-cache that one_addr2line should use.
a55ce7fe 2537 std::string lineno = Dwarf_line_info::one_addr2line(
e4e5049b 2538 locs->object, locs->shndx, locs->offset, 16);
70e654ba
ILT
2539 if (!lineno.empty())
2540 line_nums.insert(lineno);
2541 }
2542
2543 if (line_nums.size() > 1)
2544 {
dd8670e5 2545 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
78f15696 2546 "places (possible ODR violation):"),
a2b1aa12 2547 output_file_name, demangle(symbol_name).c_str());
70e654ba
ILT
2548 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2549 it2 != line_nums.end();
2550 ++it2)
2551 fprintf(stderr, " %s\n", it2->c_str());
2552 }
2553 }
e4e5049b
CS
2554 // We only call one_addr2line() in this function, so we can clear its cache.
2555 Dwarf_line_info::clear_addr2line_cache();
70e654ba
ILT
2556}
2557
f6ce93d6
ILT
2558// Warnings functions.
2559
2560// Add a new warning.
2561
2562void
2563Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
cb295612 2564 const std::string& warning)
f6ce93d6
ILT
2565{
2566 name = symtab->canonicalize_name(name);
cb295612 2567 this->warnings_[name].set(obj, warning);
f6ce93d6
ILT
2568}
2569
2570// Look through the warnings and mark the symbols for which we should
2571// warn. This is called during Layout::finalize when we know the
2572// sources for all the symbols.
2573
2574void
cb295612 2575Warnings::note_warnings(Symbol_table* symtab)
f6ce93d6
ILT
2576{
2577 for (Warning_table::iterator p = this->warnings_.begin();
2578 p != this->warnings_.end();
2579 ++p)
2580 {
2581 Symbol* sym = symtab->lookup(p->first, NULL);
2582 if (sym != NULL
2583 && sym->source() == Symbol::FROM_OBJECT
2584 && sym->object() == p->second.object)
cb295612 2585 sym->set_has_warning();
f6ce93d6
ILT
2586 }
2587}
2588
2589// Issue a warning. This is called when we see a relocation against a
2590// symbol for which has a warning.
2591
75f2446e 2592template<int size, bool big_endian>
f6ce93d6 2593void
75f2446e
ILT
2594Warnings::issue_warning(const Symbol* sym,
2595 const Relocate_info<size, big_endian>* relinfo,
2596 size_t relnum, off_t reloffset) const
f6ce93d6 2597{
a3ad94ed 2598 gold_assert(sym->has_warning());
f6ce93d6 2599 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 2600 gold_assert(p != this->warnings_.end());
75f2446e
ILT
2601 gold_warning_at_location(relinfo, relnum, reloffset,
2602 "%s", p->second.text.c_str());
f6ce93d6
ILT
2603}
2604
14bfc3f5
ILT
2605// Instantiate the templates we need. We could use the configure
2606// script to restrict this to only the ones needed for implemented
2607// targets.
2608
c7912668
ILT
2609#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2610template
2611void
2612Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2613#endif
2614
2615#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2616template
2617void
2618Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2619#endif
2620
193a53d9 2621#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
2622template
2623void
193a53d9
ILT
2624Symbol_table::add_from_relobj<32, false>(
2625 Sized_relobj<32, false>* relobj,
f6ce93d6 2626 const unsigned char* syms,
14bfc3f5 2627 size_t count,
d491d34e 2628 size_t symndx_offset,
14bfc3f5
ILT
2629 const char* sym_names,
2630 size_t sym_name_size,
730cdc88 2631 Sized_relobj<32, true>::Symbols* sympointers);
193a53d9 2632#endif
14bfc3f5 2633
193a53d9 2634#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
2635template
2636void
193a53d9
ILT
2637Symbol_table::add_from_relobj<32, true>(
2638 Sized_relobj<32, true>* relobj,
f6ce93d6 2639 const unsigned char* syms,
14bfc3f5 2640 size_t count,
d491d34e 2641 size_t symndx_offset,
14bfc3f5
ILT
2642 const char* sym_names,
2643 size_t sym_name_size,
730cdc88 2644 Sized_relobj<32, false>::Symbols* sympointers);
193a53d9 2645#endif
14bfc3f5 2646
193a53d9 2647#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
2648template
2649void
193a53d9
ILT
2650Symbol_table::add_from_relobj<64, false>(
2651 Sized_relobj<64, false>* relobj,
f6ce93d6 2652 const unsigned char* syms,
14bfc3f5 2653 size_t count,
d491d34e 2654 size_t symndx_offset,
14bfc3f5
ILT
2655 const char* sym_names,
2656 size_t sym_name_size,
730cdc88 2657 Sized_relobj<64, true>::Symbols* sympointers);
193a53d9 2658#endif
14bfc3f5 2659
193a53d9 2660#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
2661template
2662void
193a53d9
ILT
2663Symbol_table::add_from_relobj<64, true>(
2664 Sized_relobj<64, true>* relobj,
f6ce93d6 2665 const unsigned char* syms,
14bfc3f5 2666 size_t count,
d491d34e 2667 size_t symndx_offset,
14bfc3f5
ILT
2668 const char* sym_names,
2669 size_t sym_name_size,
730cdc88 2670 Sized_relobj<64, false>::Symbols* sympointers);
193a53d9 2671#endif
14bfc3f5 2672
193a53d9 2673#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
2674template
2675void
193a53d9
ILT
2676Symbol_table::add_from_dynobj<32, false>(
2677 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
2678 const unsigned char* syms,
2679 size_t count,
2680 const char* sym_names,
2681 size_t sym_name_size,
2682 const unsigned char* versym,
2683 size_t versym_size,
2684 const std::vector<const char*>* version_map);
193a53d9 2685#endif
dbe717ef 2686
193a53d9 2687#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
2688template
2689void
193a53d9
ILT
2690Symbol_table::add_from_dynobj<32, true>(
2691 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
2692 const unsigned char* syms,
2693 size_t count,
2694 const char* sym_names,
2695 size_t sym_name_size,
2696 const unsigned char* versym,
2697 size_t versym_size,
2698 const std::vector<const char*>* version_map);
193a53d9 2699#endif
dbe717ef 2700
193a53d9 2701#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
2702template
2703void
193a53d9
ILT
2704Symbol_table::add_from_dynobj<64, false>(
2705 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
2706 const unsigned char* syms,
2707 size_t count,
2708 const char* sym_names,
2709 size_t sym_name_size,
2710 const unsigned char* versym,
2711 size_t versym_size,
2712 const std::vector<const char*>* version_map);
193a53d9 2713#endif
dbe717ef 2714
193a53d9 2715#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
2716template
2717void
193a53d9
ILT
2718Symbol_table::add_from_dynobj<64, true>(
2719 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
2720 const unsigned char* syms,
2721 size_t count,
2722 const char* sym_names,
2723 size_t sym_name_size,
2724 const unsigned char* versym,
2725 size_t versym_size,
2726 const std::vector<const char*>* version_map);
193a53d9 2727#endif
dbe717ef 2728
46fe1623
ILT
2729#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2730template
2731void
fe8718a4 2732Symbol_table::define_with_copy_reloc<32>(
fe8718a4
ILT
2733 Sized_symbol<32>* sym,
2734 Output_data* posd,
2735 elfcpp::Elf_types<32>::Elf_Addr value);
46fe1623
ILT
2736#endif
2737
2738#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2739template
2740void
fe8718a4 2741Symbol_table::define_with_copy_reloc<64>(
fe8718a4
ILT
2742 Sized_symbol<64>* sym,
2743 Output_data* posd,
2744 elfcpp::Elf_types<64>::Elf_Addr value);
46fe1623
ILT
2745#endif
2746
75f2446e
ILT
2747#ifdef HAVE_TARGET_32_LITTLE
2748template
2749void
2750Warnings::issue_warning<32, false>(const Symbol* sym,
2751 const Relocate_info<32, false>* relinfo,
2752 size_t relnum, off_t reloffset) const;
2753#endif
2754
2755#ifdef HAVE_TARGET_32_BIG
2756template
2757void
2758Warnings::issue_warning<32, true>(const Symbol* sym,
2759 const Relocate_info<32, true>* relinfo,
2760 size_t relnum, off_t reloffset) const;
2761#endif
2762
2763#ifdef HAVE_TARGET_64_LITTLE
2764template
2765void
2766Warnings::issue_warning<64, false>(const Symbol* sym,
2767 const Relocate_info<64, false>* relinfo,
2768 size_t relnum, off_t reloffset) const;
2769#endif
2770
2771#ifdef HAVE_TARGET_64_BIG
2772template
2773void
2774Warnings::issue_warning<64, true>(const Symbol* sym,
2775 const Relocate_info<64, true>* relinfo,
2776 size_t relnum, off_t reloffset) const;
2777#endif
2778
14bfc3f5 2779} // End namespace gold.