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