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