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