]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
*** empty log message ***
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
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
14bfc3f5 25#include <stdint.h>
70e654ba 26#include <set>
14bfc3f5
ILT
27#include <string>
28#include <utility>
a2b1aa12 29#include "demangle.h"
14bfc3f5
ILT
30
31#include "object.h"
70e654ba 32#include "dwarf_reader.h"
dbe717ef 33#include "dynobj.h"
75f65a3e 34#include "output.h"
61ba1cf9 35#include "target.h"
645f8123 36#include "workqueue.h"
14bfc3f5
ILT
37#include "symtab.h"
38
39namespace gold
40{
41
42// Class Symbol.
43
ead1e424
ILT
44// Initialize fields in Symbol. This initializes everything except u_
45// and source_.
14bfc3f5 46
14bfc3f5 47void
ead1e424
ILT
48Symbol::init_fields(const char* name, const char* version,
49 elfcpp::STT type, elfcpp::STB binding,
50 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
51{
52 this->name_ = name;
53 this->version_ = version;
c06b7b0b
ILT
54 this->symtab_index_ = 0;
55 this->dynsym_index_ = 0;
ead1e424 56 this->got_offset_ = 0;
f4151f89 57 this->plt_offset_ = 0;
ead1e424
ILT
58 this->type_ = type;
59 this->binding_ = binding;
60 this->visibility_ = visibility;
61 this->nonvis_ = nonvis;
62 this->is_target_special_ = false;
1564db8d
ILT
63 this->is_def_ = false;
64 this->is_forwarder_ = false;
aeddab66 65 this->has_alias_ = false;
c06b7b0b 66 this->needs_dynsym_entry_ = false;
008db82e 67 this->in_reg_ = false;
ead1e424
ILT
68 this->in_dyn_ = false;
69 this->has_got_offset_ = false;
f4151f89 70 this->has_plt_offset_ = false;
f6ce93d6 71 this->has_warning_ = false;
46fe1623 72 this->is_copied_from_dynobj_ = false;
d61c6bd4 73 this->needs_value_in_got_ = false;
ead1e424
ILT
74}
75
a2b1aa12
ILT
76// Return the demangled version of the symbol's name, but only
77// if the --demangle flag was set.
78
79static std::string
80demangle(const char* name)
81{
ff541f30
ILT
82 if (!parameters->demangle())
83 return name;
84
a2b1aa12
ILT
85 // cplus_demangle allocates memory for the result it returns,
86 // and returns NULL if the name is already demangled.
87 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
88 if (demangled_name == NULL)
89 return name;
90
91 std::string retval(demangled_name);
92 free(demangled_name);
93 return retval;
94}
95
96std::string
97Symbol::demangled_name() const
98{
ff541f30 99 return demangle(this->name());
a2b1aa12
ILT
100}
101
ead1e424
ILT
102// Initialize the fields in the base class Symbol for SYM in OBJECT.
103
104template<int size, bool big_endian>
105void
106Symbol::init_base(const char* name, const char* version, Object* object,
107 const elfcpp::Sym<size, big_endian>& sym)
108{
109 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
110 sym.get_st_visibility(), sym.get_st_nonvis());
111 this->u_.from_object.object = object;
112 // FIXME: Handle SHN_XINDEX.
16649710 113 this->u_.from_object.shndx = sym.get_st_shndx();
ead1e424 114 this->source_ = FROM_OBJECT;
008db82e 115 this->in_reg_ = !object->is_dynamic();
1564db8d 116 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
117}
118
ead1e424
ILT
119// Initialize the fields in the base class Symbol for a symbol defined
120// in an Output_data.
121
122void
123Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
124 elfcpp::STB binding, elfcpp::STV visibility,
125 unsigned char nonvis, bool offset_is_from_end)
126{
127 this->init_fields(name, NULL, type, binding, visibility, nonvis);
128 this->u_.in_output_data.output_data = od;
129 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
130 this->source_ = IN_OUTPUT_DATA;
008db82e 131 this->in_reg_ = true;
ead1e424
ILT
132}
133
134// Initialize the fields in the base class Symbol for a symbol defined
135// in an Output_segment.
136
137void
138Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
139 elfcpp::STB binding, elfcpp::STV visibility,
140 unsigned char nonvis, Segment_offset_base offset_base)
141{
142 this->init_fields(name, NULL, type, binding, visibility, nonvis);
143 this->u_.in_output_segment.output_segment = os;
144 this->u_.in_output_segment.offset_base = offset_base;
145 this->source_ = IN_OUTPUT_SEGMENT;
008db82e 146 this->in_reg_ = true;
ead1e424
ILT
147}
148
149// Initialize the fields in the base class Symbol for a symbol defined
150// as a constant.
151
152void
153Symbol::init_base(const char* name, elfcpp::STT type,
154 elfcpp::STB binding, elfcpp::STV visibility,
155 unsigned char nonvis)
156{
157 this->init_fields(name, NULL, type, binding, visibility, nonvis);
158 this->source_ = CONSTANT;
008db82e 159 this->in_reg_ = true;
ead1e424
ILT
160}
161
c7912668
ILT
162// Allocate a common symbol in the base.
163
164void
165Symbol::allocate_base_common(Output_data* od)
166{
167 gold_assert(this->is_common());
168 this->source_ = IN_OUTPUT_DATA;
169 this->u_.in_output_data.output_data = od;
170 this->u_.in_output_data.offset_is_from_end = false;
171}
172
ead1e424 173// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
174
175template<int size>
176template<bool big_endian>
177void
178Sized_symbol<size>::init(const char* name, const char* version, Object* object,
179 const elfcpp::Sym<size, big_endian>& sym)
180{
181 this->init_base(name, version, object, sym);
182 this->value_ = sym.get_st_value();
ead1e424
ILT
183 this->symsize_ = sym.get_st_size();
184}
185
186// Initialize the fields in Sized_symbol for a symbol defined in an
187// Output_data.
188
189template<int size>
190void
191Sized_symbol<size>::init(const char* name, Output_data* od,
192 Value_type value, Size_type symsize,
193 elfcpp::STT type, elfcpp::STB binding,
194 elfcpp::STV visibility, unsigned char nonvis,
195 bool offset_is_from_end)
196{
197 this->init_base(name, od, type, binding, visibility, nonvis,
198 offset_is_from_end);
199 this->value_ = value;
200 this->symsize_ = symsize;
201}
202
203// Initialize the fields in Sized_symbol for a symbol defined in an
204// Output_segment.
205
206template<int size>
207void
208Sized_symbol<size>::init(const char* name, Output_segment* os,
209 Value_type value, Size_type symsize,
210 elfcpp::STT type, elfcpp::STB binding,
211 elfcpp::STV visibility, unsigned char nonvis,
212 Segment_offset_base offset_base)
213{
214 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
215 this->value_ = value;
216 this->symsize_ = symsize;
217}
218
219// Initialize the fields in Sized_symbol for a symbol defined as a
220// constant.
221
222template<int size>
223void
224Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
225 elfcpp::STT type, elfcpp::STB binding,
226 elfcpp::STV visibility, unsigned char nonvis)
227{
228 this->init_base(name, type, binding, visibility, nonvis);
229 this->value_ = value;
230 this->symsize_ = symsize;
14bfc3f5
ILT
231}
232
c7912668
ILT
233// Allocate a common symbol.
234
235template<int size>
236void
237Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
238{
239 this->allocate_base_common(od);
240 this->value_ = value;
241}
242
436ca963
ILT
243// Return true if this symbol should be added to the dynamic symbol
244// table.
245
246inline bool
247Symbol::should_add_dynsym_entry() const
248{
249 // If the symbol is used by a dynamic relocation, we need to add it.
250 if (this->needs_dynsym_entry())
251 return true;
252
253 // If exporting all symbols or building a shared library,
254 // and the symbol is defined in a regular object and is
255 // externally visible, we need to add it.
256 if ((parameters->export_dynamic() || parameters->output_is_shared())
257 && !this->is_from_dynobj()
258 && this->is_externally_visible())
259 return true;
260
261 return false;
262}
263
b3b74ddc
ILT
264// Return true if the final value of this symbol is known at link
265// time.
266
267bool
268Symbol::final_value_is_known() const
269{
270 // If we are not generating an executable, then no final values are
271 // known, since they will change at runtime.
272 if (!parameters->output_is_executable())
273 return false;
274
275 // If the symbol is not from an object file, then it is defined, and
276 // known.
277 if (this->source_ != FROM_OBJECT)
278 return true;
279
280 // If the symbol is from a dynamic object, then the final value is
281 // not known.
282 if (this->object()->is_dynamic())
283 return false;
284
285 // If the symbol is not undefined (it is defined or common), then
286 // the final value is known.
287 if (!this->is_undefined())
288 return true;
289
290 // If the symbol is undefined, then whether the final value is known
291 // depends on whether we are doing a static link. If we are doing a
292 // dynamic link, then the final value could be filled in at runtime.
293 // This could reasonably be the case for a weak undefined symbol.
294 return parameters->doing_static_link();
295}
296
14bfc3f5
ILT
297// Class Symbol_table.
298
299Symbol_table::Symbol_table()
9025d29d 300 : saw_undefined_(0), offset_(0), table_(), namepool_(),
f6ce93d6 301 forwarders_(), commons_(), warnings_()
14bfc3f5
ILT
302{
303}
304
305Symbol_table::~Symbol_table()
306{
307}
308
309// The hash function. The key is always canonicalized, so we use a
310// simple combination of the pointers.
311
312size_t
313Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
314{
f0641a0b 315 return key.first ^ key.second;
14bfc3f5
ILT
316}
317
318// The symbol table key equality function. This is only called with
319// canonicalized name and version strings, so we can use pointer
320// comparison.
321
322bool
323Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
324 const Symbol_table_key& k2) const
325{
326 return k1.first == k2.first && k1.second == k2.second;
327}
328
dd8670e5 329// Make TO a symbol which forwards to FROM.
14bfc3f5
ILT
330
331void
332Symbol_table::make_forwarder(Symbol* from, Symbol* to)
333{
a3ad94ed
ILT
334 gold_assert(from != to);
335 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
336 this->forwarders_[from] = to;
337 from->set_forwarder();
338}
339
61ba1cf9
ILT
340// Resolve the forwards from FROM, returning the real symbol.
341
14bfc3f5 342Symbol*
c06b7b0b 343Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 344{
a3ad94ed 345 gold_assert(from->is_forwarder());
c06b7b0b 346 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 347 this->forwarders_.find(from);
a3ad94ed 348 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
349 return p->second;
350}
351
61ba1cf9
ILT
352// Look up a symbol by name.
353
354Symbol*
355Symbol_table::lookup(const char* name, const char* version) const
356{
f0641a0b
ILT
357 Stringpool::Key name_key;
358 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
359 if (name == NULL)
360 return NULL;
f0641a0b
ILT
361
362 Stringpool::Key version_key = 0;
61ba1cf9
ILT
363 if (version != NULL)
364 {
f0641a0b 365 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
366 if (version == NULL)
367 return NULL;
368 }
369
f0641a0b 370 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
371 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
372 if (p == this->table_.end())
373 return NULL;
374 return p->second;
375}
376
14bfc3f5
ILT
377// Resolve a Symbol with another Symbol. This is only used in the
378// unusual case where there are references to both an unversioned
379// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
380// version is the default version. Because this is unusual, we do
381// this the slow way, by converting back to an ELF symbol.
14bfc3f5 382
1564db8d 383template<int size, bool big_endian>
14bfc3f5 384void
14b31740
ILT
385Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
386 const char* version ACCEPT_SIZE_ENDIAN)
14bfc3f5 387{
1564db8d
ILT
388 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
389 elfcpp::Sym_write<size, big_endian> esym(buf);
390 // We don't bother to set the st_name field.
391 esym.put_st_value(from->value());
392 esym.put_st_size(from->symsize());
393 esym.put_st_info(from->binding(), from->type());
ead1e424 394 esym.put_st_other(from->visibility(), from->nonvis());
16649710 395 esym.put_st_shndx(from->shndx());
70e654ba 396 this->resolve(to, esym.sym(), esym.sym(), from->object(), version);
1ebd95fd
ILT
397 if (from->in_reg())
398 to->set_in_reg();
399 if (from->in_dyn())
400 to->set_in_dyn();
14bfc3f5
ILT
401}
402
403// Add one symbol from OBJECT to the symbol table. NAME is symbol
404// name and VERSION is the version; both are canonicalized. DEF is
405// whether this is the default version.
406
407// If DEF is true, then this is the definition of a default version of
408// a symbol. That means that any lookup of NAME/NULL and any lookup
409// of NAME/VERSION should always return the same symbol. This is
410// obvious for references, but in particular we want to do this for
411// definitions: overriding NAME/NULL should also override
412// NAME/VERSION. If we don't do that, it would be very hard to
413// override functions in a shared library which uses versioning.
414
415// We implement this by simply making both entries in the hash table
416// point to the same Symbol structure. That is easy enough if this is
417// the first time we see NAME/NULL or NAME/VERSION, but it is possible
418// that we have seen both already, in which case they will both have
419// independent entries in the symbol table. We can't simply change
420// the symbol table entry, because we have pointers to the entries
421// attached to the object files. So we mark the entry attached to the
422// object file as a forwarder, and record it in the forwarders_ map.
423// Note that entries in the hash table will never be marked as
424// forwarders.
70e654ba
ILT
425//
426// SYM and ORIG_SYM are almost always the same. ORIG_SYM is the
427// symbol exactly as it existed in the input file. SYM is usually
428// that as well, but can be modified, for instance if we determine
429// it's in a to-be-discarded section.
14bfc3f5
ILT
430
431template<int size, bool big_endian>
aeddab66 432Sized_symbol<size>*
f6ce93d6 433Symbol_table::add_from_object(Object* object,
14bfc3f5 434 const char *name,
f0641a0b
ILT
435 Stringpool::Key name_key,
436 const char *version,
437 Stringpool::Key version_key,
438 bool def,
70e654ba
ILT
439 const elfcpp::Sym<size, big_endian>& sym,
440 const elfcpp::Sym<size, big_endian>& orig_sym)
14bfc3f5
ILT
441{
442 Symbol* const snull = NULL;
443 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
444 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
445 snull));
14bfc3f5
ILT
446
447 std::pair<typename Symbol_table_type::iterator, bool> insdef =
448 std::make_pair(this->table_.end(), false);
449 if (def)
450 {
f0641a0b
ILT
451 const Stringpool::Key vnull_key = 0;
452 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
453 vnull_key),
14bfc3f5
ILT
454 snull));
455 }
456
457 // ins.first: an iterator, which is a pointer to a pair.
458 // ins.first->first: the key (a pair of name and version).
459 // ins.first->second: the value (Symbol*).
460 // ins.second: true if new entry was inserted, false if not.
461
1564db8d 462 Sized_symbol<size>* ret;
ead1e424
ILT
463 bool was_undefined;
464 bool was_common;
14bfc3f5
ILT
465 if (!ins.second)
466 {
467 // We already have an entry for NAME/VERSION.
593f47df
ILT
468 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
469 SELECT_SIZE(size));
a3ad94ed 470 gold_assert(ret != NULL);
ead1e424
ILT
471
472 was_undefined = ret->is_undefined();
473 was_common = ret->is_common();
474
70e654ba 475 this->resolve(ret, sym, orig_sym, object, version);
14bfc3f5
ILT
476
477 if (def)
478 {
479 if (insdef.second)
480 {
481 // This is the first time we have seen NAME/NULL. Make
482 // NAME/NULL point to NAME/VERSION.
483 insdef.first->second = ret;
484 }
dbe717ef 485 else if (insdef.first->second != ret)
14bfc3f5
ILT
486 {
487 // This is the unfortunate case where we already have
488 // entries for both NAME/VERSION and NAME/NULL.
274e99f9 489 const Sized_symbol<size>* sym2;
593f47df 490 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d
ILT
491 insdef.first->second
492 SELECT_SIZE(size));
593f47df 493 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
14b31740 494 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
14bfc3f5
ILT
495 this->make_forwarder(insdef.first->second, ret);
496 insdef.first->second = ret;
497 }
498 }
499 }
500 else
501 {
502 // This is the first time we have seen NAME/VERSION.
a3ad94ed 503 gold_assert(ins.first->second == NULL);
ead1e424
ILT
504
505 was_undefined = false;
506 was_common = false;
507
14bfc3f5
ILT
508 if (def && !insdef.second)
509 {
14b31740
ILT
510 // We already have an entry for NAME/NULL. If we override
511 // it, then change it to NAME/VERSION.
593f47df
ILT
512 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
513 insdef.first->second
514 SELECT_SIZE(size));
70e654ba 515 this->resolve(ret, sym, orig_sym, object, version);
14bfc3f5
ILT
516 ins.first->second = ret;
517 }
518 else
519 {
f6ce93d6
ILT
520 Sized_target<size, big_endian>* target =
521 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
522 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
1564db8d
ILT
523 if (!target->has_make_symbol())
524 ret = new Sized_symbol<size>();
525 else
14bfc3f5 526 {
1564db8d
ILT
527 ret = target->make_symbol();
528 if (ret == NULL)
14bfc3f5
ILT
529 {
530 // This means that we don't want a symbol table
531 // entry after all.
532 if (!def)
533 this->table_.erase(ins.first);
534 else
535 {
536 this->table_.erase(insdef.first);
537 // Inserting insdef invalidated ins.
f0641a0b
ILT
538 this->table_.erase(std::make_pair(name_key,
539 version_key));
14bfc3f5
ILT
540 }
541 return NULL;
542 }
543 }
14bfc3f5 544
1564db8d
ILT
545 ret->init(name, version, object, sym);
546
14bfc3f5
ILT
547 ins.first->second = ret;
548 if (def)
549 {
550 // This is the first time we have seen NAME/NULL. Point
551 // it at the new entry for NAME/VERSION.
a3ad94ed 552 gold_assert(insdef.second);
14bfc3f5
ILT
553 insdef.first->second = ret;
554 }
555 }
556 }
557
ead1e424
ILT
558 // Record every time we see a new undefined symbol, to speed up
559 // archive groups.
560 if (!was_undefined && ret->is_undefined())
561 ++this->saw_undefined_;
562
563 // Keep track of common symbols, to speed up common symbol
564 // allocation.
565 if (!was_common && ret->is_common())
566 this->commons_.push_back(ret);
567
14bfc3f5
ILT
568 return ret;
569}
570
f6ce93d6 571// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
572
573template<int size, bool big_endian>
574void
dbe717ef
ILT
575Symbol_table::add_from_relobj(
576 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 577 const unsigned char* syms,
14bfc3f5
ILT
578 size_t count,
579 const char* sym_names,
580 size_t sym_name_size,
730cdc88 581 typename Sized_relobj<size, big_endian>::Symbols* sympointers)
14bfc3f5 582{
9025d29d
ILT
583 gold_assert(size == relobj->target()->get_size());
584 gold_assert(size == parameters->get_size());
14bfc3f5 585
a783673b
ILT
586 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
587
f6ce93d6 588 const unsigned char* p = syms;
a783673b 589 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
590 {
591 elfcpp::Sym<size, big_endian> sym(p);
a783673b 592 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 593
a783673b 594 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
595 if (st_name >= sym_name_size)
596 {
75f2446e
ILT
597 relobj->error(_("bad global symbol name offset %u at %zu"),
598 st_name, i);
599 continue;
14bfc3f5
ILT
600 }
601
dbe717ef
ILT
602 const char* name = sym_names + st_name;
603
a783673b
ILT
604 // A symbol defined in a section which we are not including must
605 // be treated as an undefined symbol.
606 unsigned char symbuf[sym_size];
607 elfcpp::Sym<size, big_endian> sym2(symbuf);
608 unsigned int st_shndx = psym->get_st_shndx();
609 if (st_shndx != elfcpp::SHN_UNDEF
610 && st_shndx < elfcpp::SHN_LORESERVE
dbe717ef 611 && !relobj->is_section_included(st_shndx))
a783673b
ILT
612 {
613 memcpy(symbuf, p, sym_size);
614 elfcpp::Sym_write<size, big_endian> sw(symbuf);
615 sw.put_st_shndx(elfcpp::SHN_UNDEF);
616 psym = &sym2;
617 }
618
14bfc3f5
ILT
619 // In an object file, an '@' in the name separates the symbol
620 // name from the version name. If there are two '@' characters,
621 // this is the default version.
622 const char* ver = strchr(name, '@');
623
aeddab66 624 Sized_symbol<size>* res;
14bfc3f5
ILT
625 if (ver == NULL)
626 {
f0641a0b 627 Stringpool::Key name_key;
cfd73a4e 628 name = this->namepool_.add(name, true, &name_key);
dbe717ef 629 res = this->add_from_object(relobj, name, name_key, NULL, 0,
70e654ba 630 false, *psym, sym);
14bfc3f5
ILT
631 }
632 else
633 {
f0641a0b 634 Stringpool::Key name_key;
cfd73a4e 635 name = this->namepool_.add_prefix(name, ver - name, &name_key);
f0641a0b 636
14bfc3f5
ILT
637 bool def = false;
638 ++ver;
639 if (*ver == '@')
640 {
641 def = true;
642 ++ver;
643 }
f0641a0b
ILT
644
645 Stringpool::Key ver_key;
cfd73a4e 646 ver = this->namepool_.add(ver, true, &ver_key);
f0641a0b 647
dbe717ef 648 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
70e654ba 649 def, *psym, sym);
14bfc3f5
ILT
650 }
651
730cdc88 652 (*sympointers)[i] = res;
14bfc3f5
ILT
653 }
654}
655
dbe717ef
ILT
656// Add all the symbols in a dynamic object to the hash table.
657
658template<int size, bool big_endian>
659void
660Symbol_table::add_from_dynobj(
661 Sized_dynobj<size, big_endian>* dynobj,
662 const unsigned char* syms,
663 size_t count,
664 const char* sym_names,
665 size_t sym_name_size,
666 const unsigned char* versym,
667 size_t versym_size,
668 const std::vector<const char*>* version_map)
669{
9025d29d
ILT
670 gold_assert(size == dynobj->target()->get_size());
671 gold_assert(size == parameters->get_size());
dbe717ef
ILT
672
673 if (versym != NULL && versym_size / 2 < count)
674 {
75f2446e
ILT
675 dynobj->error(_("too few symbol versions"));
676 return;
dbe717ef
ILT
677 }
678
679 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
680
aeddab66
ILT
681 // We keep a list of all STT_OBJECT symbols, so that we can resolve
682 // weak aliases. This is necessary because if the dynamic object
683 // provides the same variable under two names, one of which is a
684 // weak definition, and the regular object refers to the weak
685 // definition, we have to put both the weak definition and the
686 // strong definition into the dynamic symbol table. Given a weak
687 // definition, the only way that we can find the corresponding
688 // strong definition, if any, is to search the symbol table.
689 std::vector<Sized_symbol<size>*> object_symbols;
690
dbe717ef
ILT
691 const unsigned char* p = syms;
692 const unsigned char* vs = versym;
693 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
694 {
695 elfcpp::Sym<size, big_endian> sym(p);
696
697 // Ignore symbols with local binding.
698 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
699 continue;
700
701 unsigned int st_name = sym.get_st_name();
702 if (st_name >= sym_name_size)
703 {
75f2446e
ILT
704 dynobj->error(_("bad symbol name offset %u at %zu"),
705 st_name, i);
706 continue;
dbe717ef
ILT
707 }
708
709 const char* name = sym_names + st_name;
710
aeddab66
ILT
711 Sized_symbol<size>* res;
712
dbe717ef
ILT
713 if (versym == NULL)
714 {
715 Stringpool::Key name_key;
cfd73a4e 716 name = this->namepool_.add(name, true, &name_key);
aeddab66 717 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 718 false, sym, sym);
dbe717ef 719 }
aeddab66
ILT
720 else
721 {
722 // Read the version information.
dbe717ef 723
aeddab66 724 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
dbe717ef 725
aeddab66
ILT
726 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
727 v &= elfcpp::VERSYM_VERSION;
dbe717ef 728
aeddab66
ILT
729 // The Sun documentation says that V can be VER_NDX_LOCAL,
730 // or VER_NDX_GLOBAL, or a version index. The meaning of
731 // VER_NDX_LOCAL is defined as "Symbol has local scope."
732 // The old GNU linker will happily generate VER_NDX_LOCAL
733 // for an undefined symbol. I don't know what the Sun
734 // linker will generate.
dbe717ef 735
aeddab66
ILT
736 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
737 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
738 {
739 // This symbol should not be visible outside the object.
740 continue;
741 }
64707334 742
aeddab66
ILT
743 // At this point we are definitely going to add this symbol.
744 Stringpool::Key name_key;
745 name = this->namepool_.add(name, true, &name_key);
dbe717ef 746
aeddab66
ILT
747 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
748 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
749 {
750 // This symbol does not have a version.
751 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 752 false, sym, sym);
aeddab66
ILT
753 }
754 else
755 {
756 if (v >= version_map->size())
757 {
758 dynobj->error(_("versym for symbol %zu out of range: %u"),
759 i, v);
760 continue;
761 }
dbe717ef 762
aeddab66
ILT
763 const char* version = (*version_map)[v];
764 if (version == NULL)
765 {
766 dynobj->error(_("versym for symbol %zu has no name: %u"),
767 i, v);
768 continue;
769 }
dbe717ef 770
aeddab66
ILT
771 Stringpool::Key version_key;
772 version = this->namepool_.add(version, true, &version_key);
773
774 // If this is an absolute symbol, and the version name
775 // and symbol name are the same, then this is the
776 // version definition symbol. These symbols exist to
777 // support using -u to pull in particular versions. We
778 // do not want to record a version for them.
779 if (sym.get_st_shndx() == elfcpp::SHN_ABS
780 && name_key == version_key)
781 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 782 false, sym, sym);
aeddab66
ILT
783 else
784 {
785 const bool def = (!hidden
786 && (sym.get_st_shndx()
787 != elfcpp::SHN_UNDEF));
788 res = this->add_from_object(dynobj, name, name_key, version,
70e654ba 789 version_key, def, sym, sym);
aeddab66
ILT
790 }
791 }
dbe717ef
ILT
792 }
793
aeddab66
ILT
794 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
795 && sym.get_st_type() == elfcpp::STT_OBJECT)
796 object_symbols.push_back(res);
797 }
798
799 this->record_weak_aliases(&object_symbols);
800}
801
802// This is used to sort weak aliases. We sort them first by section
803// index, then by offset, then by weak ahead of strong.
804
805template<int size>
806class Weak_alias_sorter
807{
808 public:
809 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
810};
811
812template<int size>
813bool
814Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
815 const Sized_symbol<size>* s2) const
816{
817 if (s1->shndx() != s2->shndx())
818 return s1->shndx() < s2->shndx();
819 if (s1->value() != s2->value())
820 return s1->value() < s2->value();
821 if (s1->binding() != s2->binding())
822 {
823 if (s1->binding() == elfcpp::STB_WEAK)
824 return true;
825 if (s2->binding() == elfcpp::STB_WEAK)
826 return false;
827 }
828 return std::string(s1->name()) < std::string(s2->name());
829}
dbe717ef 830
aeddab66
ILT
831// SYMBOLS is a list of object symbols from a dynamic object. Look
832// for any weak aliases, and record them so that if we add the weak
833// alias to the dynamic symbol table, we also add the corresponding
834// strong symbol.
dbe717ef 835
aeddab66
ILT
836template<int size>
837void
838Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
839{
840 // Sort the vector by section index, then by offset, then by weak
841 // ahead of strong.
842 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
843
844 // Walk through the vector. For each weak definition, record
845 // aliases.
846 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
847 symbols->begin();
848 p != symbols->end();
849 ++p)
850 {
851 if ((*p)->binding() != elfcpp::STB_WEAK)
852 continue;
853
854 // Build a circular list of weak aliases. Each symbol points to
855 // the next one in the circular list.
856
857 Sized_symbol<size>* from_sym = *p;
858 typename std::vector<Sized_symbol<size>*>::const_iterator q;
859 for (q = p + 1; q != symbols->end(); ++q)
dbe717ef 860 {
aeddab66
ILT
861 if ((*q)->shndx() != from_sym->shndx()
862 || (*q)->value() != from_sym->value())
863 break;
864
865 this->weak_aliases_[from_sym] = *q;
866 from_sym->set_has_alias();
867 from_sym = *q;
dbe717ef
ILT
868 }
869
aeddab66
ILT
870 if (from_sym != *p)
871 {
872 this->weak_aliases_[from_sym] = *p;
873 from_sym->set_has_alias();
874 }
dbe717ef 875
aeddab66 876 p = q - 1;
dbe717ef
ILT
877 }
878}
879
ead1e424
ILT
880// Create and return a specially defined symbol. If ONLY_IF_REF is
881// true, then only create the symbol if there is a reference to it.
86f2e683 882// If this does not return NULL, it sets *POLDSYM to the existing
306d9ef0 883// symbol if there is one. This canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
884
885template<int size, bool big_endian>
886Sized_symbol<size>*
306d9ef0
ILT
887Symbol_table::define_special_symbol(const Target* target, const char** pname,
888 const char** pversion, bool only_if_ref,
86f2e683 889 Sized_symbol<size>** poldsym
593f47df 890 ACCEPT_SIZE_ENDIAN)
ead1e424 891{
ead1e424
ILT
892 Symbol* oldsym;
893 Sized_symbol<size>* sym;
86f2e683
ILT
894 bool add_to_table = false;
895 typename Symbol_table_type::iterator add_loc = this->table_.end();
ead1e424
ILT
896
897 if (only_if_ref)
898 {
306d9ef0 899 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 900 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 901 return NULL;
306d9ef0
ILT
902
903 *pname = oldsym->name();
904 *pversion = oldsym->version();
ead1e424
ILT
905 }
906 else
907 {
14b31740 908 // Canonicalize NAME and VERSION.
f0641a0b 909 Stringpool::Key name_key;
cfd73a4e 910 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 911
14b31740 912 Stringpool::Key version_key = 0;
306d9ef0 913 if (*pversion != NULL)
cfd73a4e 914 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 915
ead1e424 916 Symbol* const snull = NULL;
ead1e424 917 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
918 this->table_.insert(std::make_pair(std::make_pair(name_key,
919 version_key),
ead1e424
ILT
920 snull));
921
922 if (!ins.second)
923 {
14b31740 924 // We already have a symbol table entry for NAME/VERSION.
ead1e424 925 oldsym = ins.first->second;
a3ad94ed 926 gold_assert(oldsym != NULL);
ead1e424
ILT
927 }
928 else
929 {
930 // We haven't seen this symbol before.
a3ad94ed 931 gold_assert(ins.first->second == NULL);
86f2e683
ILT
932 add_to_table = true;
933 add_loc = ins.first;
ead1e424
ILT
934 oldsym = NULL;
935 }
936 }
937
86f2e683
ILT
938 if (!target->has_make_symbol())
939 sym = new Sized_symbol<size>();
940 else
ead1e424 941 {
86f2e683
ILT
942 gold_assert(target->get_size() == size);
943 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
944 typedef Sized_target<size, big_endian> My_target;
945 const My_target* sized_target =
946 static_cast<const My_target*>(target);
947 sym = sized_target->make_symbol();
948 if (sym == NULL)
949 return NULL;
950 }
ead1e424 951
86f2e683
ILT
952 if (add_to_table)
953 add_loc->second = sym;
954 else
955 gold_assert(oldsym != NULL);
ead1e424 956
86f2e683
ILT
957 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
958 SELECT_SIZE(size));
ead1e424
ILT
959
960 return sym;
961}
962
963// Define a symbol based on an Output_data.
964
14b31740
ILT
965Symbol*
966Symbol_table::define_in_output_data(const Target* target, const char* name,
967 const char* version, Output_data* od,
ead1e424
ILT
968 uint64_t value, uint64_t symsize,
969 elfcpp::STT type, elfcpp::STB binding,
970 elfcpp::STV visibility,
971 unsigned char nonvis,
972 bool offset_is_from_end,
973 bool only_if_ref)
974{
9025d29d 975 if (parameters->get_size() == 32)
86f2e683
ILT
976 {
977#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
978 return this->do_define_in_output_data<32>(target, name, version, od,
979 value, symsize, type, binding,
980 visibility, nonvis,
981 offset_is_from_end,
982 only_if_ref);
983#else
984 gold_unreachable();
985#endif
986 }
9025d29d 987 else if (parameters->get_size() == 64)
86f2e683
ILT
988 {
989#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
990 return this->do_define_in_output_data<64>(target, name, version, od,
991 value, symsize, type, binding,
992 visibility, nonvis,
993 offset_is_from_end,
994 only_if_ref);
995#else
996 gold_unreachable();
997#endif
998 }
ead1e424 999 else
a3ad94ed 1000 gold_unreachable();
ead1e424
ILT
1001}
1002
1003// Define a symbol in an Output_data, sized version.
1004
1005template<int size>
14b31740 1006Sized_symbol<size>*
ead1e424 1007Symbol_table::do_define_in_output_data(
14b31740 1008 const Target* target,
ead1e424 1009 const char* name,
14b31740 1010 const char* version,
ead1e424
ILT
1011 Output_data* od,
1012 typename elfcpp::Elf_types<size>::Elf_Addr value,
1013 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1014 elfcpp::STT type,
1015 elfcpp::STB binding,
1016 elfcpp::STV visibility,
1017 unsigned char nonvis,
1018 bool offset_is_from_end,
1019 bool only_if_ref)
1020{
1021 Sized_symbol<size>* sym;
86f2e683 1022 Sized_symbol<size>* oldsym;
ead1e424 1023
9025d29d 1024 if (parameters->is_big_endian())
193a53d9
ILT
1025 {
1026#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1027 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
306d9ef0 1028 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
1029 SELECT_SIZE_ENDIAN(size, true));
1030#else
1031 gold_unreachable();
1032#endif
1033 }
ead1e424 1034 else
193a53d9
ILT
1035 {
1036#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1037 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
306d9ef0 1038 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
1039 SELECT_SIZE_ENDIAN(size, false));
1040#else
1041 gold_unreachable();
1042#endif
1043 }
ead1e424
ILT
1044
1045 if (sym == NULL)
14b31740 1046 return NULL;
ead1e424 1047
d4f5281b 1048 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
1049 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1050 offset_is_from_end);
14b31740 1051
86f2e683
ILT
1052 if (oldsym != NULL
1053 && Symbol_table::should_override_with_special(oldsym))
aeddab66 1054 this->override_with_special(oldsym, sym);
86f2e683 1055
14b31740 1056 return sym;
ead1e424
ILT
1057}
1058
1059// Define a symbol based on an Output_segment.
1060
14b31740
ILT
1061Symbol*
1062Symbol_table::define_in_output_segment(const Target* target, const char* name,
1063 const char* version, Output_segment* os,
ead1e424
ILT
1064 uint64_t value, uint64_t symsize,
1065 elfcpp::STT type, elfcpp::STB binding,
1066 elfcpp::STV visibility,
1067 unsigned char nonvis,
1068 Symbol::Segment_offset_base offset_base,
1069 bool only_if_ref)
1070{
9025d29d 1071 if (parameters->get_size() == 32)
86f2e683
ILT
1072 {
1073#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1074 return this->do_define_in_output_segment<32>(target, name, version, os,
1075 value, symsize, type,
1076 binding, visibility, nonvis,
1077 offset_base, only_if_ref);
1078#else
1079 gold_unreachable();
1080#endif
1081 }
9025d29d 1082 else if (parameters->get_size() == 64)
86f2e683
ILT
1083 {
1084#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1085 return this->do_define_in_output_segment<64>(target, name, version, os,
1086 value, symsize, type,
1087 binding, visibility, nonvis,
1088 offset_base, only_if_ref);
1089#else
1090 gold_unreachable();
1091#endif
1092 }
ead1e424 1093 else
a3ad94ed 1094 gold_unreachable();
ead1e424
ILT
1095}
1096
1097// Define a symbol in an Output_segment, sized version.
1098
1099template<int size>
14b31740 1100Sized_symbol<size>*
ead1e424 1101Symbol_table::do_define_in_output_segment(
14b31740 1102 const Target* target,
ead1e424 1103 const char* name,
14b31740 1104 const char* version,
ead1e424
ILT
1105 Output_segment* os,
1106 typename elfcpp::Elf_types<size>::Elf_Addr value,
1107 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1108 elfcpp::STT type,
1109 elfcpp::STB binding,
1110 elfcpp::STV visibility,
1111 unsigned char nonvis,
1112 Symbol::Segment_offset_base offset_base,
1113 bool only_if_ref)
1114{
1115 Sized_symbol<size>* sym;
86f2e683 1116 Sized_symbol<size>* oldsym;
ead1e424 1117
9025d29d
ILT
1118 if (parameters->is_big_endian())
1119 {
1120#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1121 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1122 target, &name, &version, only_if_ref, &oldsym
1123 SELECT_SIZE_ENDIAN(size, true));
1124#else
1125 gold_unreachable();
1126#endif
1127 }
ead1e424 1128 else
9025d29d
ILT
1129 {
1130#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1131 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1132 target, &name, &version, only_if_ref, &oldsym
1133 SELECT_SIZE_ENDIAN(size, false));
1134#else
1135 gold_unreachable();
1136#endif
1137 }
ead1e424
ILT
1138
1139 if (sym == NULL)
14b31740 1140 return NULL;
ead1e424 1141
d4f5281b 1142 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
1143 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1144 offset_base);
14b31740 1145
86f2e683
ILT
1146 if (oldsym != NULL
1147 && Symbol_table::should_override_with_special(oldsym))
aeddab66 1148 this->override_with_special(oldsym, sym);
86f2e683 1149
14b31740 1150 return sym;
ead1e424
ILT
1151}
1152
1153// Define a special symbol with a constant value. It is a multiple
1154// definition error if this symbol is already defined.
1155
14b31740
ILT
1156Symbol*
1157Symbol_table::define_as_constant(const Target* target, const char* name,
1158 const char* version, uint64_t value,
1159 uint64_t symsize, elfcpp::STT type,
1160 elfcpp::STB binding, elfcpp::STV visibility,
1161 unsigned char nonvis, bool only_if_ref)
ead1e424 1162{
9025d29d 1163 if (parameters->get_size() == 32)
86f2e683
ILT
1164 {
1165#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1166 return this->do_define_as_constant<32>(target, name, version, value,
1167 symsize, type, binding,
1168 visibility, nonvis, only_if_ref);
1169#else
1170 gold_unreachable();
1171#endif
1172 }
9025d29d 1173 else if (parameters->get_size() == 64)
86f2e683
ILT
1174 {
1175#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1176 return this->do_define_as_constant<64>(target, name, version, value,
1177 symsize, type, binding,
1178 visibility, nonvis, only_if_ref);
1179#else
1180 gold_unreachable();
1181#endif
1182 }
ead1e424 1183 else
a3ad94ed 1184 gold_unreachable();
ead1e424
ILT
1185}
1186
1187// Define a symbol as a constant, sized version.
1188
1189template<int size>
14b31740 1190Sized_symbol<size>*
ead1e424 1191Symbol_table::do_define_as_constant(
14b31740 1192 const Target* target,
ead1e424 1193 const char* name,
14b31740 1194 const char* version,
ead1e424
ILT
1195 typename elfcpp::Elf_types<size>::Elf_Addr value,
1196 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1197 elfcpp::STT type,
1198 elfcpp::STB binding,
1199 elfcpp::STV visibility,
1200 unsigned char nonvis,
1201 bool only_if_ref)
1202{
1203 Sized_symbol<size>* sym;
86f2e683 1204 Sized_symbol<size>* oldsym;
ead1e424 1205
9025d29d
ILT
1206 if (parameters->is_big_endian())
1207 {
1208#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1209 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1210 target, &name, &version, only_if_ref, &oldsym
1211 SELECT_SIZE_ENDIAN(size, true));
1212#else
1213 gold_unreachable();
1214#endif
1215 }
ead1e424 1216 else
9025d29d
ILT
1217 {
1218#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1219 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1220 target, &name, &version, only_if_ref, &oldsym
1221 SELECT_SIZE_ENDIAN(size, false));
1222#else
1223 gold_unreachable();
1224#endif
1225 }
ead1e424
ILT
1226
1227 if (sym == NULL)
14b31740 1228 return NULL;
ead1e424 1229
d4f5281b 1230 gold_assert(version == NULL || oldsym != NULL);
ead1e424 1231 sym->init(name, value, symsize, type, binding, visibility, nonvis);
14b31740 1232
86f2e683
ILT
1233 if (oldsym != NULL
1234 && Symbol_table::should_override_with_special(oldsym))
aeddab66 1235 this->override_with_special(oldsym, sym);
86f2e683 1236
14b31740 1237 return sym;
ead1e424
ILT
1238}
1239
1240// Define a set of symbols in output sections.
1241
1242void
14b31740
ILT
1243Symbol_table::define_symbols(const Layout* layout, const Target* target,
1244 int count, const Define_symbol_in_section* p)
ead1e424
ILT
1245{
1246 for (int i = 0; i < count; ++i, ++p)
1247 {
1248 Output_section* os = layout->find_output_section(p->output_section);
1249 if (os != NULL)
14b31740
ILT
1250 this->define_in_output_data(target, p->name, NULL, os, p->value,
1251 p->size, p->type, p->binding,
1252 p->visibility, p->nonvis,
1253 p->offset_is_from_end, p->only_if_ref);
ead1e424 1254 else
14b31740 1255 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1256 p->binding, p->visibility, p->nonvis,
1257 p->only_if_ref);
1258 }
1259}
1260
1261// Define a set of symbols in output segments.
1262
1263void
14b31740
ILT
1264Symbol_table::define_symbols(const Layout* layout, const Target* target,
1265 int count, const Define_symbol_in_segment* p)
ead1e424
ILT
1266{
1267 for (int i = 0; i < count; ++i, ++p)
1268 {
1269 Output_segment* os = layout->find_output_segment(p->segment_type,
1270 p->segment_flags_set,
1271 p->segment_flags_clear);
1272 if (os != NULL)
14b31740
ILT
1273 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1274 p->size, p->type, p->binding,
1275 p->visibility, p->nonvis,
1276 p->offset_base, p->only_if_ref);
ead1e424 1277 else
14b31740 1278 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1279 p->binding, p->visibility, p->nonvis,
1280 p->only_if_ref);
1281 }
1282}
1283
46fe1623
ILT
1284// Define CSYM using a COPY reloc. POSD is the Output_data where the
1285// symbol should be defined--typically a .dyn.bss section. VALUE is
1286// the offset within POSD.
1287
1288template<int size>
1289void
1290Symbol_table::define_with_copy_reloc(const Target* target,
1291 Sized_symbol<size>* csym,
1292 Output_data* posd, uint64_t value)
1293{
1294 gold_assert(csym->is_from_dynobj());
1295 gold_assert(!csym->is_copied_from_dynobj());
1296 Object* object = csym->object();
1297 gold_assert(object->is_dynamic());
1298 Dynobj* dynobj = static_cast<Dynobj*>(object);
1299
1300 // Our copied variable has to override any variable in a shared
1301 // library.
1302 elfcpp::STB binding = csym->binding();
1303 if (binding == elfcpp::STB_WEAK)
1304 binding = elfcpp::STB_GLOBAL;
1305
1306 this->define_in_output_data(target, csym->name(), csym->version(),
1307 posd, value, csym->symsize(),
1308 csym->type(), binding,
1309 csym->visibility(), csym->nonvis(),
1310 false, false);
1311
1312 csym->set_is_copied_from_dynobj();
1313 csym->set_needs_dynsym_entry();
1314
1315 this->copied_symbol_dynobjs_[csym] = dynobj;
1316
1317 // We have now defined all aliases, but we have not entered them all
1318 // in the copied_symbol_dynobjs_ map.
1319 if (csym->has_alias())
1320 {
1321 Symbol* sym = csym;
1322 while (true)
1323 {
1324 sym = this->weak_aliases_[sym];
1325 if (sym == csym)
1326 break;
1327 gold_assert(sym->output_data() == posd);
1328
1329 sym->set_is_copied_from_dynobj();
1330 this->copied_symbol_dynobjs_[sym] = dynobj;
1331 }
1332 }
1333}
1334
1335// SYM is defined using a COPY reloc. Return the dynamic object where
1336// the original definition was found.
1337
1338Dynobj*
1339Symbol_table::get_copy_source(const Symbol* sym) const
1340{
1341 gold_assert(sym->is_copied_from_dynobj());
1342 Copied_symbol_dynobjs::const_iterator p =
1343 this->copied_symbol_dynobjs_.find(sym);
1344 gold_assert(p != this->copied_symbol_dynobjs_.end());
1345 return p->second;
1346}
1347
a3ad94ed
ILT
1348// Set the dynamic symbol indexes. INDEX is the index of the first
1349// global dynamic symbol. Pointers to the symbols are stored into the
1350// vector SYMS. The names are added to DYNPOOL. This returns an
1351// updated dynamic symbol index.
1352
1353unsigned int
35cdfc9a 1354Symbol_table::set_dynsym_indexes(const Target* target,
14b31740 1355 unsigned int index,
a3ad94ed 1356 std::vector<Symbol*>* syms,
14b31740
ILT
1357 Stringpool* dynpool,
1358 Versions* versions)
a3ad94ed
ILT
1359{
1360 for (Symbol_table_type::iterator p = this->table_.begin();
1361 p != this->table_.end();
1362 ++p)
1363 {
1364 Symbol* sym = p->second;
16649710
ILT
1365
1366 // Note that SYM may already have a dynamic symbol index, since
1367 // some symbols appear more than once in the symbol table, with
1368 // and without a version.
1369
436ca963 1370 if (!sym->should_add_dynsym_entry())
16649710
ILT
1371 sym->set_dynsym_index(-1U);
1372 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1373 {
1374 sym->set_dynsym_index(index);
1375 ++index;
1376 syms->push_back(sym);
cfd73a4e 1377 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
1378
1379 // Record any version information.
1380 if (sym->version() != NULL)
35cdfc9a 1381 versions->record_version(this, dynpool, sym);
a3ad94ed
ILT
1382 }
1383 }
1384
14b31740
ILT
1385 // Finish up the versions. In some cases this may add new dynamic
1386 // symbols.
1387 index = versions->finalize(target, this, index, syms);
1388
a3ad94ed
ILT
1389 return index;
1390}
1391
c06b7b0b
ILT
1392// Set the final values for all the symbols. The index of the first
1393// global symbol in the output file is INDEX. Record the file offset
75f65a3e 1394// OFF. Add their names to POOL. Return the new file offset.
54dc6425 1395
75f65a3e 1396off_t
16649710
ILT
1397Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1398 size_t dyn_global_index, size_t dyncount,
1399 Stringpool* pool)
54dc6425 1400{
f6ce93d6
ILT
1401 off_t ret;
1402
a3ad94ed 1403 gold_assert(index != 0);
c06b7b0b
ILT
1404 this->first_global_index_ = index;
1405
16649710
ILT
1406 this->dynamic_offset_ = dynoff;
1407 this->first_dynamic_global_index_ = dyn_global_index;
1408 this->dynamic_count_ = dyncount;
1409
9025d29d
ILT
1410 if (parameters->get_size() == 32)
1411 {
1412#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1413 ret = this->sized_finalize<32>(index, off, pool);
1414#else
1415 gold_unreachable();
1416#endif
1417 }
1418 else if (parameters->get_size() == 64)
1419 {
1420#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1421 ret = this->sized_finalize<64>(index, off, pool);
1422#else
1423 gold_unreachable();
1424#endif
1425 }
61ba1cf9 1426 else
a3ad94ed 1427 gold_unreachable();
f6ce93d6
ILT
1428
1429 // Now that we have the final symbol table, we can reliably note
1430 // which symbols should get warnings.
1431 this->warnings_.note_warnings(this);
1432
1433 return ret;
75f65a3e
ILT
1434}
1435
ead1e424
ILT
1436// Set the final value for all the symbols. This is called after
1437// Layout::finalize, so all the output sections have their final
1438// address.
75f65a3e
ILT
1439
1440template<int size>
1441off_t
c06b7b0b 1442Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
75f65a3e 1443{
ead1e424 1444 off = align_address(off, size >> 3);
75f65a3e
ILT
1445 this->offset_ = off;
1446
c06b7b0b
ILT
1447 size_t orig_index = index;
1448
75f65a3e 1449 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1450 for (Symbol_table_type::iterator p = this->table_.begin();
1451 p != this->table_.end();
1452 ++p)
54dc6425 1453 {
75f65a3e 1454 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 1455
75f65a3e 1456 // FIXME: Here we need to decide which symbols should go into
a3ad94ed
ILT
1457 // the output file, based on --strip.
1458
1459 // The default version of a symbol may appear twice in the
1460 // symbol table. We only need to finalize it once.
1461 if (sym->has_symtab_index())
1462 continue;
75f65a3e 1463
008db82e
ILT
1464 if (!sym->in_reg())
1465 {
1466 gold_assert(!sym->has_symtab_index());
1467 sym->set_symtab_index(-1U);
1468 gold_assert(sym->dynsym_index() == -1U);
1469 continue;
1470 }
1471
ead1e424 1472 typename Sized_symbol<size>::Value_type value;
75f65a3e 1473
ead1e424 1474 switch (sym->source())
75f65a3e 1475 {
ead1e424
ILT
1476 case Symbol::FROM_OBJECT:
1477 {
16649710 1478 unsigned int shndx = sym->shndx();
ead1e424
ILT
1479
1480 // FIXME: We need some target specific support here.
16649710
ILT
1481 if (shndx >= elfcpp::SHN_LORESERVE
1482 && shndx != elfcpp::SHN_ABS)
ead1e424 1483 {
75f2446e 1484 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 1485 sym->demangled_name().c_str(), shndx);
75f2446e 1486 shndx = elfcpp::SHN_UNDEF;
ead1e424
ILT
1487 }
1488
f6ce93d6
ILT
1489 Object* symobj = sym->object();
1490 if (symobj->is_dynamic())
1491 {
1492 value = 0;
16649710 1493 shndx = elfcpp::SHN_UNDEF;
f6ce93d6 1494 }
16649710 1495 else if (shndx == elfcpp::SHN_UNDEF)
ead1e424 1496 value = 0;
16649710 1497 else if (shndx == elfcpp::SHN_ABS)
ead1e424
ILT
1498 value = sym->value();
1499 else
1500 {
f6ce93d6 1501 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1502 off_t secoff;
16649710 1503 Output_section* os = relobj->output_section(shndx, &secoff);
ead1e424
ILT
1504
1505 if (os == NULL)
1506 {
c06b7b0b 1507 sym->set_symtab_index(-1U);
16649710 1508 gold_assert(sym->dynsym_index() == -1U);
ead1e424
ILT
1509 continue;
1510 }
1511
1512 value = sym->value() + os->address() + secoff;
1513 }
1514 }
1515 break;
1516
1517 case Symbol::IN_OUTPUT_DATA:
1518 {
1519 Output_data* od = sym->output_data();
1520 value = sym->value() + od->address();
1521 if (sym->offset_is_from_end())
1522 value += od->data_size();
1523 }
1524 break;
1525
1526 case Symbol::IN_OUTPUT_SEGMENT:
1527 {
1528 Output_segment* os = sym->output_segment();
1529 value = sym->value() + os->vaddr();
1530 switch (sym->offset_base())
1531 {
1532 case Symbol::SEGMENT_START:
1533 break;
1534 case Symbol::SEGMENT_END:
1535 value += os->memsz();
1536 break;
1537 case Symbol::SEGMENT_BSS:
1538 value += os->filesz();
1539 break;
1540 default:
a3ad94ed 1541 gold_unreachable();
ead1e424
ILT
1542 }
1543 }
1544 break;
1545
1546 case Symbol::CONSTANT:
1547 value = sym->value();
1548 break;
1549
1550 default:
a3ad94ed 1551 gold_unreachable();
54dc6425 1552 }
ead1e424
ILT
1553
1554 sym->set_value(value);
9e2dcb77
ILT
1555
1556 if (parameters->strip_all())
1557 sym->set_symtab_index(-1U);
1558 else
1559 {
1560 sym->set_symtab_index(index);
cfd73a4e 1561 pool->add(sym->name(), false, NULL);
9e2dcb77
ILT
1562 ++index;
1563 off += sym_size;
1564 }
54dc6425 1565 }
75f65a3e 1566
c06b7b0b 1567 this->output_count_ = index - orig_index;
61ba1cf9 1568
75f65a3e 1569 return off;
54dc6425
ILT
1570}
1571
61ba1cf9
ILT
1572// Write out the global symbols.
1573
1574void
9a2d6984
ILT
1575Symbol_table::write_globals(const Input_objects* input_objects,
1576 const Stringpool* sympool,
16649710 1577 const Stringpool* dynpool, Output_file* of) const
61ba1cf9 1578{
9025d29d 1579 if (parameters->get_size() == 32)
61ba1cf9 1580 {
9025d29d
ILT
1581 if (parameters->is_big_endian())
1582 {
1583#ifdef HAVE_TARGET_32_BIG
9a2d6984
ILT
1584 this->sized_write_globals<32, true>(input_objects, sympool,
1585 dynpool, of);
9025d29d
ILT
1586#else
1587 gold_unreachable();
1588#endif
1589 }
61ba1cf9 1590 else
9025d29d
ILT
1591 {
1592#ifdef HAVE_TARGET_32_LITTLE
9a2d6984
ILT
1593 this->sized_write_globals<32, false>(input_objects, sympool,
1594 dynpool, of);
9025d29d
ILT
1595#else
1596 gold_unreachable();
1597#endif
1598 }
61ba1cf9 1599 }
9025d29d 1600 else if (parameters->get_size() == 64)
61ba1cf9 1601 {
9025d29d
ILT
1602 if (parameters->is_big_endian())
1603 {
1604#ifdef HAVE_TARGET_64_BIG
9a2d6984
ILT
1605 this->sized_write_globals<64, true>(input_objects, sympool,
1606 dynpool, of);
9025d29d
ILT
1607#else
1608 gold_unreachable();
1609#endif
1610 }
61ba1cf9 1611 else
9025d29d
ILT
1612 {
1613#ifdef HAVE_TARGET_64_LITTLE
9a2d6984
ILT
1614 this->sized_write_globals<64, false>(input_objects, sympool,
1615 dynpool, of);
9025d29d
ILT
1616#else
1617 gold_unreachable();
1618#endif
1619 }
61ba1cf9
ILT
1620 }
1621 else
a3ad94ed 1622 gold_unreachable();
61ba1cf9
ILT
1623}
1624
1625// Write out the global symbols.
1626
1627template<int size, bool big_endian>
1628void
9a2d6984 1629Symbol_table::sized_write_globals(const Input_objects* input_objects,
61ba1cf9 1630 const Stringpool* sympool,
16649710 1631 const Stringpool* dynpool,
61ba1cf9
ILT
1632 Output_file* of) const
1633{
9a2d6984
ILT
1634 const Target* const target = input_objects->target();
1635
61ba1cf9 1636 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1637 unsigned int index = this->first_global_index_;
1638 const off_t oview_size = this->output_count_ * sym_size;
16649710
ILT
1639 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1640
1641 unsigned int dynamic_count = this->dynamic_count_;
1642 off_t dynamic_size = dynamic_count * sym_size;
1643 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1644 unsigned char* dynamic_view;
1645 if (this->dynamic_offset_ == 0)
1646 dynamic_view = NULL;
1647 else
1648 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 1649
61ba1cf9
ILT
1650 unsigned char* ps = psyms;
1651 for (Symbol_table_type::const_iterator p = this->table_.begin();
1652 p != this->table_.end();
1653 ++p)
1654 {
1655 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1656
9a2d6984
ILT
1657 // Possibly warn about unresolved symbols in shared libraries.
1658 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
e2827e5f 1659
a3ad94ed 1660 unsigned int sym_index = sym->symtab_index();
16649710
ILT
1661 unsigned int dynsym_index;
1662 if (dynamic_view == NULL)
1663 dynsym_index = -1U;
1664 else
1665 dynsym_index = sym->dynsym_index();
1666
1667 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
1668 {
1669 // This symbol is not included in the output file.
1670 continue;
1671 }
16649710
ILT
1672
1673 if (sym_index == index)
1674 ++index;
1675 else if (sym_index != -1U)
a3ad94ed
ILT
1676 {
1677 // We have already seen this symbol, because it has a
1678 // default version.
1679 gold_assert(sym_index < index);
16649710
ILT
1680 if (dynsym_index == -1U)
1681 continue;
1682 sym_index = -1U;
a3ad94ed 1683 }
c06b7b0b 1684
ead1e424 1685 unsigned int shndx;
ab5c9e90 1686 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
ead1e424
ILT
1687 switch (sym->source())
1688 {
1689 case Symbol::FROM_OBJECT:
1690 {
16649710 1691 unsigned int in_shndx = sym->shndx();
ead1e424
ILT
1692
1693 // FIXME: We need some target specific support here.
16649710
ILT
1694 if (in_shndx >= elfcpp::SHN_LORESERVE
1695 && in_shndx != elfcpp::SHN_ABS)
ead1e424 1696 {
75f2446e 1697 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 1698 sym->demangled_name().c_str(), in_shndx);
75f2446e 1699 shndx = in_shndx;
f6ce93d6 1700 }
ead1e424
ILT
1701 else
1702 {
75f2446e
ILT
1703 Object* symobj = sym->object();
1704 if (symobj->is_dynamic())
1705 {
1706 if (sym->needs_dynsym_value())
1707 value = target->dynsym_value(sym);
1708 shndx = elfcpp::SHN_UNDEF;
1709 }
1710 else if (in_shndx == elfcpp::SHN_UNDEF
1711 || in_shndx == elfcpp::SHN_ABS)
1712 shndx = in_shndx;
1713 else
1714 {
1715 Relobj* relobj = static_cast<Relobj*>(symobj);
1716 off_t secoff;
1717 Output_section* os = relobj->output_section(in_shndx,
1718 &secoff);
1719 gold_assert(os != NULL);
1720 shndx = os->out_shndx();
1721 }
ead1e424
ILT
1722 }
1723 }
1724 break;
1725
1726 case Symbol::IN_OUTPUT_DATA:
1727 shndx = sym->output_data()->out_shndx();
1728 break;
1729
1730 case Symbol::IN_OUTPUT_SEGMENT:
1731 shndx = elfcpp::SHN_ABS;
1732 break;
1733
1734 case Symbol::CONSTANT:
1735 shndx = elfcpp::SHN_ABS;
1736 break;
1737
1738 default:
a3ad94ed 1739 gold_unreachable();
ead1e424 1740 }
61ba1cf9 1741
16649710
ILT
1742 if (sym_index != -1U)
1743 {
6a469986 1744 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1745 sym, sym->value(), shndx, sympool, ps
6a469986 1746 SELECT_SIZE_ENDIAN(size, big_endian));
16649710
ILT
1747 ps += sym_size;
1748 }
61ba1cf9 1749
16649710
ILT
1750 if (dynsym_index != -1U)
1751 {
1752 dynsym_index -= first_dynamic_global_index;
1753 gold_assert(dynsym_index < dynamic_count);
1754 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
6a469986 1755 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1756 sym, value, shndx, dynpool, pd
6a469986 1757 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1758 }
61ba1cf9
ILT
1759 }
1760
a3ad94ed 1761 gold_assert(ps - psyms == oview_size);
c06b7b0b
ILT
1762
1763 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
1764 if (dynamic_view != NULL)
1765 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1766}
1767
1768// Write out the symbol SYM, in section SHNDX, to P. POOL is the
1769// strtab holding the name.
1770
1771template<int size, bool big_endian>
1772void
ab5c9e90
ILT
1773Symbol_table::sized_write_symbol(
1774 Sized_symbol<size>* sym,
1775 typename elfcpp::Elf_types<size>::Elf_Addr value,
1776 unsigned int shndx,
1777 const Stringpool* pool,
1778 unsigned char* p
1779 ACCEPT_SIZE_ENDIAN) const
16649710
ILT
1780{
1781 elfcpp::Sym_write<size, big_endian> osym(p);
1782 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 1783 osym.put_st_value(value);
16649710
ILT
1784 osym.put_st_size(sym->symsize());
1785 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1786 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1787 osym.put_st_shndx(shndx);
61ba1cf9
ILT
1788}
1789
9a2d6984
ILT
1790// Check for unresolved symbols in shared libraries. This is
1791// controlled by the --allow-shlib-undefined option.
1792
1793// We only warn about libraries for which we have seen all the
1794// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1795// which were not seen in this link. If we didn't see a DT_NEEDED
1796// entry, we aren't going to be able to reliably report whether the
1797// symbol is undefined.
1798
1799// We also don't warn about libraries found in the system library
1800// directory (the directory were we find libc.so); we assume that
1801// those libraries are OK. This heuristic avoids problems in
1802// GNU/Linux, in which -ldl can have undefined references satisfied by
1803// ld-linux.so.
1804
1805inline void
1806Symbol_table::warn_about_undefined_dynobj_symbol(
1807 const Input_objects* input_objects,
1808 Symbol* sym) const
1809{
1810 if (sym->source() == Symbol::FROM_OBJECT
1811 && sym->object()->is_dynamic()
1812 && sym->shndx() == elfcpp::SHN_UNDEF
1813 && sym->binding() != elfcpp::STB_WEAK
1814 && !parameters->allow_shlib_undefined()
1815 && !input_objects->target()->is_defined_by_abi(sym)
1816 && !input_objects->found_in_system_library_directory(sym->object()))
1817 {
1818 // A very ugly cast.
1819 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
1820 if (!dynobj->has_unknown_needed_entries())
1821 gold_error(_("%s: undefined reference to '%s'"),
a2b1aa12
ILT
1822 sym->object()->name().c_str(),
1823 sym->demangled_name().c_str());
9a2d6984
ILT
1824 }
1825}
1826
a3ad94ed
ILT
1827// Write out a section symbol. Return the update offset.
1828
1829void
9025d29d 1830Symbol_table::write_section_symbol(const Output_section *os,
a3ad94ed
ILT
1831 Output_file* of,
1832 off_t offset) const
1833{
9025d29d 1834 if (parameters->get_size() == 32)
a3ad94ed 1835 {
9025d29d
ILT
1836 if (parameters->is_big_endian())
1837 {
1838#ifdef HAVE_TARGET_32_BIG
1839 this->sized_write_section_symbol<32, true>(os, of, offset);
1840#else
1841 gold_unreachable();
1842#endif
1843 }
a3ad94ed 1844 else
9025d29d
ILT
1845 {
1846#ifdef HAVE_TARGET_32_LITTLE
1847 this->sized_write_section_symbol<32, false>(os, of, offset);
1848#else
1849 gold_unreachable();
1850#endif
1851 }
a3ad94ed 1852 }
9025d29d 1853 else if (parameters->get_size() == 64)
a3ad94ed 1854 {
9025d29d
ILT
1855 if (parameters->is_big_endian())
1856 {
1857#ifdef HAVE_TARGET_64_BIG
1858 this->sized_write_section_symbol<64, true>(os, of, offset);
1859#else
1860 gold_unreachable();
1861#endif
1862 }
a3ad94ed 1863 else
9025d29d
ILT
1864 {
1865#ifdef HAVE_TARGET_64_LITTLE
1866 this->sized_write_section_symbol<64, false>(os, of, offset);
1867#else
1868 gold_unreachable();
1869#endif
1870 }
a3ad94ed
ILT
1871 }
1872 else
1873 gold_unreachable();
1874}
1875
1876// Write out a section symbol, specialized for size and endianness.
1877
1878template<int size, bool big_endian>
1879void
1880Symbol_table::sized_write_section_symbol(const Output_section* os,
1881 Output_file* of,
1882 off_t offset) const
1883{
1884 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1885
1886 unsigned char* pov = of->get_output_view(offset, sym_size);
1887
1888 elfcpp::Sym_write<size, big_endian> osym(pov);
1889 osym.put_st_name(0);
1890 osym.put_st_value(os->address());
1891 osym.put_st_size(0);
1892 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1893 elfcpp::STT_SECTION));
1894 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1895 osym.put_st_shndx(os->out_shndx());
1896
1897 of->write_output_view(offset, sym_size, pov);
1898}
1899
abaa3995
ILT
1900// Print statistical information to stderr. This is used for --stats.
1901
1902void
1903Symbol_table::print_stats() const
1904{
1905#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
1906 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
1907 program_name, this->table_.size(), this->table_.bucket_count());
1908#else
1909 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
1910 program_name, this->table_.size());
1911#endif
1912}
1913
ff541f30
ILT
1914// We check for ODR violations by looking for symbols with the same
1915// name for which the debugging information reports that they were
1916// defined in different source locations. When comparing the source
1917// location, we consider instances with the same base filename and
1918// line number to be the same. This is because different object
1919// files/shared libraries can include the same header file using
1920// different paths, and we don't want to report an ODR violation in
1921// that case.
1922
1923// This struct is used to compare line information, as returned by
1924// Dwarf_line_info::one_addr2line. It imlements a < comparison
1925// operator used with std::set.
1926
1927struct Odr_violation_compare
1928{
1929 bool
1930 operator()(const std::string& s1, const std::string& s2) const
1931 {
1932 std::string::size_type pos1 = s1.rfind('/');
1933 std::string::size_type pos2 = s2.rfind('/');
1934 if (pos1 == std::string::npos
1935 || pos2 == std::string::npos)
1936 return s1 < s2;
1937 return s1.compare(pos1, std::string::npos,
1938 s2, pos2, std::string::npos) < 0;
1939 }
1940};
1941
70e654ba
ILT
1942// Check candidate_odr_violations_ to find symbols with the same name
1943// but apparently different definitions (different source-file/line-no).
1944
1945void
78f15696 1946Symbol_table::detect_odr_violations(const char* output_file_name) const
70e654ba
ILT
1947{
1948 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
1949 it != candidate_odr_violations_.end();
1950 ++it)
1951 {
1952 const char* symbol_name = it->first;
1953 // We use a sorted set so the output is deterministic.
ff541f30 1954 std::set<std::string, Odr_violation_compare> line_nums;
70e654ba 1955
b01c0a4a
ILT
1956 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
1957 locs = it->second.begin();
1958 locs != it->second.end();
1959 ++locs)
70e654ba
ILT
1960 {
1961 // We need to lock the object in order to read it. This
1962 // means that we can not run inside a Task. If we want to
1963 // run this in a Task for better performance, we will need
1964 // one Task for object, plus appropriate locking to ensure
1965 // that we don't conflict with other uses of the object.
1966 locs->object->lock();
a55ce7fe
ILT
1967 std::string lineno = Dwarf_line_info::one_addr2line(
1968 locs->object, locs->shndx, locs->offset);
70e654ba 1969 locs->object->unlock();
70e654ba
ILT
1970 if (!lineno.empty())
1971 line_nums.insert(lineno);
1972 }
1973
1974 if (line_nums.size() > 1)
1975 {
dd8670e5 1976 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
78f15696 1977 "places (possible ODR violation):"),
a2b1aa12 1978 output_file_name, demangle(symbol_name).c_str());
70e654ba
ILT
1979 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
1980 it2 != line_nums.end();
1981 ++it2)
1982 fprintf(stderr, " %s\n", it2->c_str());
1983 }
1984 }
1985}
1986
f6ce93d6
ILT
1987// Warnings functions.
1988
1989// Add a new warning.
1990
1991void
1992Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1993 unsigned int shndx)
1994{
1995 name = symtab->canonicalize_name(name);
1996 this->warnings_[name].set(obj, shndx);
1997}
1998
1999// Look through the warnings and mark the symbols for which we should
2000// warn. This is called during Layout::finalize when we know the
2001// sources for all the symbols.
2002
2003void
2004Warnings::note_warnings(Symbol_table* symtab)
2005{
2006 for (Warning_table::iterator p = this->warnings_.begin();
2007 p != this->warnings_.end();
2008 ++p)
2009 {
2010 Symbol* sym = symtab->lookup(p->first, NULL);
2011 if (sym != NULL
2012 && sym->source() == Symbol::FROM_OBJECT
2013 && sym->object() == p->second.object)
2014 {
2015 sym->set_has_warning();
2016
2017 // Read the section contents to get the warning text. It
2018 // would be nicer if we only did this if we have to actually
2019 // issue a warning. Unfortunately, warnings are issued as
2020 // we relocate sections. That means that we can not lock
2021 // the object then, as we might try to issue the same
2022 // warning multiple times simultaneously.
645f8123
ILT
2023 {
2024 Task_locker_obj<Object> tl(*p->second.object);
2025 const unsigned char* c;
2026 off_t len;
9eb9fa57
ILT
2027 c = p->second.object->section_contents(p->second.shndx, &len,
2028 false);
645f8123
ILT
2029 p->second.set_text(reinterpret_cast<const char*>(c), len);
2030 }
f6ce93d6
ILT
2031 }
2032 }
2033}
2034
2035// Issue a warning. This is called when we see a relocation against a
2036// symbol for which has a warning.
2037
75f2446e 2038template<int size, bool big_endian>
f6ce93d6 2039void
75f2446e
ILT
2040Warnings::issue_warning(const Symbol* sym,
2041 const Relocate_info<size, big_endian>* relinfo,
2042 size_t relnum, off_t reloffset) const
f6ce93d6 2043{
a3ad94ed 2044 gold_assert(sym->has_warning());
f6ce93d6 2045 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 2046 gold_assert(p != this->warnings_.end());
75f2446e
ILT
2047 gold_warning_at_location(relinfo, relnum, reloffset,
2048 "%s", p->second.text.c_str());
f6ce93d6
ILT
2049}
2050
14bfc3f5
ILT
2051// Instantiate the templates we need. We could use the configure
2052// script to restrict this to only the ones needed for implemented
2053// targets.
2054
c7912668
ILT
2055#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2056template
2057void
2058Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2059#endif
2060
2061#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2062template
2063void
2064Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2065#endif
2066
193a53d9 2067#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
2068template
2069void
193a53d9
ILT
2070Symbol_table::add_from_relobj<32, false>(
2071 Sized_relobj<32, false>* relobj,
f6ce93d6 2072 const unsigned char* syms,
14bfc3f5
ILT
2073 size_t count,
2074 const char* sym_names,
2075 size_t sym_name_size,
730cdc88 2076 Sized_relobj<32, true>::Symbols* sympointers);
193a53d9 2077#endif
14bfc3f5 2078
193a53d9 2079#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
2080template
2081void
193a53d9
ILT
2082Symbol_table::add_from_relobj<32, true>(
2083 Sized_relobj<32, true>* relobj,
f6ce93d6 2084 const unsigned char* syms,
14bfc3f5
ILT
2085 size_t count,
2086 const char* sym_names,
2087 size_t sym_name_size,
730cdc88 2088 Sized_relobj<32, false>::Symbols* sympointers);
193a53d9 2089#endif
14bfc3f5 2090
193a53d9 2091#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
2092template
2093void
193a53d9
ILT
2094Symbol_table::add_from_relobj<64, false>(
2095 Sized_relobj<64, false>* relobj,
f6ce93d6 2096 const unsigned char* syms,
14bfc3f5
ILT
2097 size_t count,
2098 const char* sym_names,
2099 size_t sym_name_size,
730cdc88 2100 Sized_relobj<64, true>::Symbols* sympointers);
193a53d9 2101#endif
14bfc3f5 2102
193a53d9 2103#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
2104template
2105void
193a53d9
ILT
2106Symbol_table::add_from_relobj<64, true>(
2107 Sized_relobj<64, true>* relobj,
f6ce93d6 2108 const unsigned char* syms,
14bfc3f5
ILT
2109 size_t count,
2110 const char* sym_names,
2111 size_t sym_name_size,
730cdc88 2112 Sized_relobj<64, false>::Symbols* sympointers);
193a53d9 2113#endif
14bfc3f5 2114
193a53d9 2115#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
2116template
2117void
193a53d9
ILT
2118Symbol_table::add_from_dynobj<32, false>(
2119 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
2120 const unsigned char* syms,
2121 size_t count,
2122 const char* sym_names,
2123 size_t sym_name_size,
2124 const unsigned char* versym,
2125 size_t versym_size,
2126 const std::vector<const char*>* version_map);
193a53d9 2127#endif
dbe717ef 2128
193a53d9 2129#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
2130template
2131void
193a53d9
ILT
2132Symbol_table::add_from_dynobj<32, true>(
2133 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
2134 const unsigned char* syms,
2135 size_t count,
2136 const char* sym_names,
2137 size_t sym_name_size,
2138 const unsigned char* versym,
2139 size_t versym_size,
2140 const std::vector<const char*>* version_map);
193a53d9 2141#endif
dbe717ef 2142
193a53d9 2143#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
2144template
2145void
193a53d9
ILT
2146Symbol_table::add_from_dynobj<64, false>(
2147 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
2148 const unsigned char* syms,
2149 size_t count,
2150 const char* sym_names,
2151 size_t sym_name_size,
2152 const unsigned char* versym,
2153 size_t versym_size,
2154 const std::vector<const char*>* version_map);
193a53d9 2155#endif
dbe717ef 2156
193a53d9 2157#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
2158template
2159void
193a53d9
ILT
2160Symbol_table::add_from_dynobj<64, true>(
2161 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
2162 const unsigned char* syms,
2163 size_t count,
2164 const char* sym_names,
2165 size_t sym_name_size,
2166 const unsigned char* versym,
2167 size_t versym_size,
2168 const std::vector<const char*>* version_map);
193a53d9 2169#endif
dbe717ef 2170
46fe1623
ILT
2171#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2172template
2173void
2174Symbol_table::define_with_copy_reloc<32>(const Target* target,
2175 Sized_symbol<32>* sym,
2176 Output_data* posd, uint64_t value);
2177#endif
2178
2179#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2180template
2181void
2182Symbol_table::define_with_copy_reloc<64>(const Target* target,
2183 Sized_symbol<64>* sym,
2184 Output_data* posd, uint64_t value);
2185#endif
2186
75f2446e
ILT
2187#ifdef HAVE_TARGET_32_LITTLE
2188template
2189void
2190Warnings::issue_warning<32, false>(const Symbol* sym,
2191 const Relocate_info<32, false>* relinfo,
2192 size_t relnum, off_t reloffset) const;
2193#endif
2194
2195#ifdef HAVE_TARGET_32_BIG
2196template
2197void
2198Warnings::issue_warning<32, true>(const Symbol* sym,
2199 const Relocate_info<32, true>* relinfo,
2200 size_t relnum, off_t reloffset) const;
2201#endif
2202
2203#ifdef HAVE_TARGET_64_LITTLE
2204template
2205void
2206Warnings::issue_warning<64, false>(const Symbol* sym,
2207 const Relocate_info<64, false>* relinfo,
2208 size_t relnum, off_t reloffset) const;
2209#endif
2210
2211#ifdef HAVE_TARGET_64_BIG
2212template
2213void
2214Warnings::issue_warning<64, true>(const Symbol* sym,
2215 const Relocate_info<64, true>* relinfo,
2216 size_t relnum, off_t reloffset) const;
2217#endif
2218
14bfc3f5 2219} // End namespace gold.