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