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