*** empty log message ***
[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
88dd47ac
ILT
637 const bool just_symbols = relobj->just_symbols();
638
f6ce93d6 639 const unsigned char* p = syms;
a783673b 640 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
641 {
642 elfcpp::Sym<size, big_endian> sym(p);
a783673b 643 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 644
a783673b 645 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
646 if (st_name >= sym_name_size)
647 {
75f2446e
ILT
648 relobj->error(_("bad global symbol name offset %u at %zu"),
649 st_name, i);
650 continue;
14bfc3f5
ILT
651 }
652
dbe717ef
ILT
653 const char* name = sym_names + st_name;
654
a783673b
ILT
655 // A symbol defined in a section which we are not including must
656 // be treated as an undefined symbol.
657 unsigned char symbuf[sym_size];
658 elfcpp::Sym<size, big_endian> sym2(symbuf);
659 unsigned int st_shndx = psym->get_st_shndx();
660 if (st_shndx != elfcpp::SHN_UNDEF
661 && st_shndx < elfcpp::SHN_LORESERVE
dbe717ef 662 && !relobj->is_section_included(st_shndx))
a783673b
ILT
663 {
664 memcpy(symbuf, p, sym_size);
665 elfcpp::Sym_write<size, big_endian> sw(symbuf);
666 sw.put_st_shndx(elfcpp::SHN_UNDEF);
667 psym = &sym2;
668 }
669
14bfc3f5
ILT
670 // In an object file, an '@' in the name separates the symbol
671 // name from the version name. If there are two '@' characters,
672 // this is the default version.
673 const char* ver = strchr(name, '@');
09124467 674 int namelen = 0;
55a93433 675 // DEF: is the version default? LOCAL: is the symbol forced local?
09124467 676 bool def = false;
55a93433 677 bool local = false;
09124467
ILT
678
679 if (ver != NULL)
680 {
681 // The symbol name is of the form foo@VERSION or foo@@VERSION
682 namelen = ver - name;
683 ++ver;
684 if (*ver == '@')
685 {
686 def = true;
687 ++ver;
688 }
689 }
690 else if (!version_script_.empty())
691 {
692 // The symbol name did not have a version, but
693 // the version script may assign a version anyway.
694 namelen = strlen(name);
695 def = true;
55a93433 696 // Check the global: entries from the version script.
09124467
ILT
697 const std::string& version =
698 version_script_.get_symbol_version(name);
699 if (!version.empty())
700 ver = version.c_str();
55a93433
ILT
701 // Check the local: entries from the version script
702 if (version_script_.symbol_is_local(name))
703 local = true;
09124467 704 }
14bfc3f5 705
88dd47ac
ILT
706 if (just_symbols)
707 {
708 if (psym != &sym2)
709 memcpy(symbuf, p, sym_size);
710 elfcpp::Sym_write<size, big_endian> sw(symbuf);
711 sw.put_st_shndx(elfcpp::SHN_ABS);
712 if (st_shndx != elfcpp::SHN_UNDEF
713 && st_shndx < elfcpp::SHN_LORESERVE)
714 {
715 // Symbol values in object files are section relative.
716 // This is normally what we want, but since here we are
717 // converting the symbol to absolute we need to add the
718 // section address. The section address in an object
719 // file is normally zero, but people can use a linker
720 // script to change it.
721 sw.put_st_value(sym2.get_st_value()
722 + relobj->section_address(st_shndx));
723 }
724 psym = &sym2;
725 }
726
aeddab66 727 Sized_symbol<size>* res;
14bfc3f5
ILT
728 if (ver == NULL)
729 {
f0641a0b 730 Stringpool::Key name_key;
cfd73a4e 731 name = this->namepool_.add(name, true, &name_key);
dbe717ef 732 res = this->add_from_object(relobj, name, name_key, NULL, 0,
70e654ba 733 false, *psym, sym);
55a93433
ILT
734 if (local)
735 this->force_local(res);
14bfc3f5
ILT
736 }
737 else
738 {
f0641a0b 739 Stringpool::Key name_key;
09124467 740 name = this->namepool_.add_with_length(name, namelen, true,
c0873094 741 &name_key);
f0641a0b 742 Stringpool::Key ver_key;
cfd73a4e 743 ver = this->namepool_.add(ver, true, &ver_key);
f0641a0b 744
dbe717ef 745 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
70e654ba 746 def, *psym, sym);
14bfc3f5
ILT
747 }
748
730cdc88 749 (*sympointers)[i] = res;
14bfc3f5
ILT
750 }
751}
752
dbe717ef
ILT
753// Add all the symbols in a dynamic object to the hash table.
754
755template<int size, bool big_endian>
756void
757Symbol_table::add_from_dynobj(
758 Sized_dynobj<size, big_endian>* dynobj,
759 const unsigned char* syms,
760 size_t count,
761 const char* sym_names,
762 size_t sym_name_size,
763 const unsigned char* versym,
764 size_t versym_size,
765 const std::vector<const char*>* version_map)
766{
9025d29d
ILT
767 gold_assert(size == dynobj->target()->get_size());
768 gold_assert(size == parameters->get_size());
dbe717ef 769
88dd47ac
ILT
770 if (dynobj->just_symbols())
771 {
772 gold_error(_("--just-symbols does not make sense with a shared object"));
773 return;
774 }
775
dbe717ef
ILT
776 if (versym != NULL && versym_size / 2 < count)
777 {
75f2446e
ILT
778 dynobj->error(_("too few symbol versions"));
779 return;
dbe717ef
ILT
780 }
781
782 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
783
aeddab66
ILT
784 // We keep a list of all STT_OBJECT symbols, so that we can resolve
785 // weak aliases. This is necessary because if the dynamic object
786 // provides the same variable under two names, one of which is a
787 // weak definition, and the regular object refers to the weak
788 // definition, we have to put both the weak definition and the
789 // strong definition into the dynamic symbol table. Given a weak
790 // definition, the only way that we can find the corresponding
791 // strong definition, if any, is to search the symbol table.
792 std::vector<Sized_symbol<size>*> object_symbols;
793
dbe717ef
ILT
794 const unsigned char* p = syms;
795 const unsigned char* vs = versym;
796 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
797 {
798 elfcpp::Sym<size, big_endian> sym(p);
799
65778909
ILT
800 // Ignore symbols with local binding or that have
801 // internal or hidden visibility.
802 if (sym.get_st_bind() == elfcpp::STB_LOCAL
803 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
804 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
dbe717ef
ILT
805 continue;
806
807 unsigned int st_name = sym.get_st_name();
808 if (st_name >= sym_name_size)
809 {
75f2446e
ILT
810 dynobj->error(_("bad symbol name offset %u at %zu"),
811 st_name, i);
812 continue;
dbe717ef
ILT
813 }
814
815 const char* name = sym_names + st_name;
816
aeddab66
ILT
817 Sized_symbol<size>* res;
818
dbe717ef
ILT
819 if (versym == NULL)
820 {
821 Stringpool::Key name_key;
cfd73a4e 822 name = this->namepool_.add(name, true, &name_key);
aeddab66 823 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 824 false, sym, sym);
dbe717ef 825 }
aeddab66
ILT
826 else
827 {
828 // Read the version information.
dbe717ef 829
aeddab66 830 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
dbe717ef 831
aeddab66
ILT
832 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
833 v &= elfcpp::VERSYM_VERSION;
dbe717ef 834
aeddab66
ILT
835 // The Sun documentation says that V can be VER_NDX_LOCAL,
836 // or VER_NDX_GLOBAL, or a version index. The meaning of
837 // VER_NDX_LOCAL is defined as "Symbol has local scope."
838 // The old GNU linker will happily generate VER_NDX_LOCAL
839 // for an undefined symbol. I don't know what the Sun
840 // linker will generate.
dbe717ef 841
aeddab66
ILT
842 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
843 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
844 {
845 // This symbol should not be visible outside the object.
846 continue;
847 }
64707334 848
aeddab66
ILT
849 // At this point we are definitely going to add this symbol.
850 Stringpool::Key name_key;
851 name = this->namepool_.add(name, true, &name_key);
dbe717ef 852
aeddab66
ILT
853 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
854 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
855 {
856 // This symbol does not have a version.
857 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 858 false, sym, sym);
aeddab66
ILT
859 }
860 else
861 {
862 if (v >= version_map->size())
863 {
864 dynobj->error(_("versym for symbol %zu out of range: %u"),
865 i, v);
866 continue;
867 }
dbe717ef 868
aeddab66
ILT
869 const char* version = (*version_map)[v];
870 if (version == NULL)
871 {
872 dynobj->error(_("versym for symbol %zu has no name: %u"),
873 i, v);
874 continue;
875 }
dbe717ef 876
aeddab66
ILT
877 Stringpool::Key version_key;
878 version = this->namepool_.add(version, true, &version_key);
879
880 // If this is an absolute symbol, and the version name
881 // and symbol name are the same, then this is the
882 // version definition symbol. These symbols exist to
883 // support using -u to pull in particular versions. We
884 // do not want to record a version for them.
885 if (sym.get_st_shndx() == elfcpp::SHN_ABS
886 && name_key == version_key)
887 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
70e654ba 888 false, sym, sym);
aeddab66
ILT
889 else
890 {
891 const bool def = (!hidden
892 && (sym.get_st_shndx()
893 != elfcpp::SHN_UNDEF));
894 res = this->add_from_object(dynobj, name, name_key, version,
70e654ba 895 version_key, def, sym, sym);
aeddab66
ILT
896 }
897 }
dbe717ef
ILT
898 }
899
aeddab66
ILT
900 if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
901 && sym.get_st_type() == elfcpp::STT_OBJECT)
902 object_symbols.push_back(res);
903 }
904
905 this->record_weak_aliases(&object_symbols);
906}
907
908// This is used to sort weak aliases. We sort them first by section
909// index, then by offset, then by weak ahead of strong.
910
911template<int size>
912class Weak_alias_sorter
913{
914 public:
915 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
916};
917
918template<int size>
919bool
920Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
921 const Sized_symbol<size>* s2) const
922{
923 if (s1->shndx() != s2->shndx())
924 return s1->shndx() < s2->shndx();
925 if (s1->value() != s2->value())
926 return s1->value() < s2->value();
927 if (s1->binding() != s2->binding())
928 {
929 if (s1->binding() == elfcpp::STB_WEAK)
930 return true;
931 if (s2->binding() == elfcpp::STB_WEAK)
932 return false;
933 }
934 return std::string(s1->name()) < std::string(s2->name());
935}
dbe717ef 936
aeddab66
ILT
937// SYMBOLS is a list of object symbols from a dynamic object. Look
938// for any weak aliases, and record them so that if we add the weak
939// alias to the dynamic symbol table, we also add the corresponding
940// strong symbol.
dbe717ef 941
aeddab66
ILT
942template<int size>
943void
944Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
945{
946 // Sort the vector by section index, then by offset, then by weak
947 // ahead of strong.
948 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
949
950 // Walk through the vector. For each weak definition, record
951 // aliases.
952 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
953 symbols->begin();
954 p != symbols->end();
955 ++p)
956 {
957 if ((*p)->binding() != elfcpp::STB_WEAK)
958 continue;
959
960 // Build a circular list of weak aliases. Each symbol points to
961 // the next one in the circular list.
962
963 Sized_symbol<size>* from_sym = *p;
964 typename std::vector<Sized_symbol<size>*>::const_iterator q;
965 for (q = p + 1; q != symbols->end(); ++q)
dbe717ef 966 {
aeddab66
ILT
967 if ((*q)->shndx() != from_sym->shndx()
968 || (*q)->value() != from_sym->value())
969 break;
970
971 this->weak_aliases_[from_sym] = *q;
972 from_sym->set_has_alias();
973 from_sym = *q;
dbe717ef
ILT
974 }
975
aeddab66
ILT
976 if (from_sym != *p)
977 {
978 this->weak_aliases_[from_sym] = *p;
979 from_sym->set_has_alias();
980 }
dbe717ef 981
aeddab66 982 p = q - 1;
dbe717ef
ILT
983 }
984}
985
ead1e424
ILT
986// Create and return a specially defined symbol. If ONLY_IF_REF is
987// true, then only create the symbol if there is a reference to it.
86f2e683 988// If this does not return NULL, it sets *POLDSYM to the existing
306d9ef0 989// symbol if there is one. This canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
990
991template<int size, bool big_endian>
992Sized_symbol<size>*
9b07f471
ILT
993Symbol_table::define_special_symbol(const char** pname, const char** pversion,
994 bool only_if_ref,
86f2e683 995 Sized_symbol<size>** poldsym
593f47df 996 ACCEPT_SIZE_ENDIAN)
ead1e424 997{
ead1e424
ILT
998 Symbol* oldsym;
999 Sized_symbol<size>* sym;
86f2e683
ILT
1000 bool add_to_table = false;
1001 typename Symbol_table_type::iterator add_loc = this->table_.end();
ead1e424 1002
55a93433
ILT
1003 // If the caller didn't give us a version, see if we get one from
1004 // the version script.
1005 if (*pversion == NULL)
1006 {
1007 const std::string& v(this->version_script_.get_symbol_version(*pname));
1008 if (!v.empty())
1009 *pversion = v.c_str();
1010 }
1011
ead1e424
ILT
1012 if (only_if_ref)
1013 {
306d9ef0 1014 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 1015 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 1016 return NULL;
306d9ef0
ILT
1017
1018 *pname = oldsym->name();
1019 *pversion = oldsym->version();
ead1e424
ILT
1020 }
1021 else
1022 {
14b31740 1023 // Canonicalize NAME and VERSION.
f0641a0b 1024 Stringpool::Key name_key;
cfd73a4e 1025 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 1026
14b31740 1027 Stringpool::Key version_key = 0;
306d9ef0 1028 if (*pversion != NULL)
cfd73a4e 1029 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 1030
ead1e424 1031 Symbol* const snull = NULL;
ead1e424 1032 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
1033 this->table_.insert(std::make_pair(std::make_pair(name_key,
1034 version_key),
ead1e424
ILT
1035 snull));
1036
1037 if (!ins.second)
1038 {
14b31740 1039 // We already have a symbol table entry for NAME/VERSION.
ead1e424 1040 oldsym = ins.first->second;
a3ad94ed 1041 gold_assert(oldsym != NULL);
ead1e424
ILT
1042 }
1043 else
1044 {
1045 // We haven't seen this symbol before.
a3ad94ed 1046 gold_assert(ins.first->second == NULL);
86f2e683
ILT
1047 add_to_table = true;
1048 add_loc = ins.first;
ead1e424
ILT
1049 oldsym = NULL;
1050 }
1051 }
1052
9b07f471 1053 const Target* target = parameters->target();
86f2e683
ILT
1054 if (!target->has_make_symbol())
1055 sym = new Sized_symbol<size>();
1056 else
ead1e424 1057 {
86f2e683
ILT
1058 gold_assert(target->get_size() == size);
1059 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
1060 typedef Sized_target<size, big_endian> My_target;
1061 const My_target* sized_target =
1062 static_cast<const My_target*>(target);
1063 sym = sized_target->make_symbol();
1064 if (sym == NULL)
1065 return NULL;
1066 }
ead1e424 1067
86f2e683
ILT
1068 if (add_to_table)
1069 add_loc->second = sym;
1070 else
1071 gold_assert(oldsym != NULL);
ead1e424 1072
86f2e683
ILT
1073 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
1074 SELECT_SIZE(size));
ead1e424
ILT
1075
1076 return sym;
1077}
1078
1079// Define a symbol based on an Output_data.
1080
14b31740 1081Symbol*
9b07f471
ILT
1082Symbol_table::define_in_output_data(const char* name,
1083 const char* version,
1084 Output_data* od,
1085 uint64_t value,
1086 uint64_t symsize,
1087 elfcpp::STT type,
1088 elfcpp::STB binding,
ead1e424
ILT
1089 elfcpp::STV visibility,
1090 unsigned char nonvis,
1091 bool offset_is_from_end,
1092 bool only_if_ref)
1093{
9025d29d 1094 if (parameters->get_size() == 32)
86f2e683
ILT
1095 {
1096#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1097 return this->do_define_in_output_data<32>(name, version, od,
86f2e683
ILT
1098 value, symsize, type, binding,
1099 visibility, nonvis,
1100 offset_is_from_end,
1101 only_if_ref);
1102#else
1103 gold_unreachable();
1104#endif
1105 }
9025d29d 1106 else if (parameters->get_size() == 64)
86f2e683
ILT
1107 {
1108#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1109 return this->do_define_in_output_data<64>(name, version, od,
86f2e683
ILT
1110 value, symsize, type, binding,
1111 visibility, nonvis,
1112 offset_is_from_end,
1113 only_if_ref);
1114#else
1115 gold_unreachable();
1116#endif
1117 }
ead1e424 1118 else
a3ad94ed 1119 gold_unreachable();
ead1e424
ILT
1120}
1121
1122// Define a symbol in an Output_data, sized version.
1123
1124template<int size>
14b31740 1125Sized_symbol<size>*
ead1e424 1126Symbol_table::do_define_in_output_data(
ead1e424 1127 const char* name,
14b31740 1128 const char* version,
ead1e424
ILT
1129 Output_data* od,
1130 typename elfcpp::Elf_types<size>::Elf_Addr value,
1131 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1132 elfcpp::STT type,
1133 elfcpp::STB binding,
1134 elfcpp::STV visibility,
1135 unsigned char nonvis,
1136 bool offset_is_from_end,
1137 bool only_if_ref)
1138{
1139 Sized_symbol<size>* sym;
86f2e683 1140 Sized_symbol<size>* oldsym;
ead1e424 1141
9025d29d 1142 if (parameters->is_big_endian())
193a53d9
ILT
1143 {
1144#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1145 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
9b07f471 1146 &name, &version, only_if_ref, &oldsym
193a53d9
ILT
1147 SELECT_SIZE_ENDIAN(size, true));
1148#else
1149 gold_unreachable();
1150#endif
1151 }
ead1e424 1152 else
193a53d9
ILT
1153 {
1154#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1155 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
9b07f471 1156 &name, &version, only_if_ref, &oldsym
193a53d9
ILT
1157 SELECT_SIZE_ENDIAN(size, false));
1158#else
1159 gold_unreachable();
1160#endif
1161 }
ead1e424
ILT
1162
1163 if (sym == NULL)
14b31740 1164 return NULL;
ead1e424 1165
d4f5281b 1166 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
1167 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1168 offset_is_from_end);
14b31740 1169
e5756efb 1170 if (oldsym == NULL)
55a93433
ILT
1171 {
1172 if (binding == elfcpp::STB_LOCAL
1173 || this->version_script_.symbol_is_local(name))
1174 this->force_local(sym);
1175 return sym;
1176 }
86f2e683 1177
e5756efb
ILT
1178 if (Symbol_table::should_override_with_special(oldsym))
1179 this->override_with_special(oldsym, sym);
1180 delete sym;
1181 return oldsym;
ead1e424
ILT
1182}
1183
1184// Define a symbol based on an Output_segment.
1185
14b31740 1186Symbol*
9b07f471 1187Symbol_table::define_in_output_segment(const char* name,
14b31740 1188 const char* version, Output_segment* os,
9b07f471
ILT
1189 uint64_t value,
1190 uint64_t symsize,
1191 elfcpp::STT type,
1192 elfcpp::STB binding,
ead1e424
ILT
1193 elfcpp::STV visibility,
1194 unsigned char nonvis,
1195 Symbol::Segment_offset_base offset_base,
1196 bool only_if_ref)
1197{
9025d29d 1198 if (parameters->get_size() == 32)
86f2e683
ILT
1199 {
1200#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1201 return this->do_define_in_output_segment<32>(name, version, os,
86f2e683
ILT
1202 value, symsize, type,
1203 binding, visibility, nonvis,
1204 offset_base, only_if_ref);
1205#else
1206 gold_unreachable();
1207#endif
1208 }
9025d29d 1209 else if (parameters->get_size() == 64)
86f2e683
ILT
1210 {
1211#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1212 return this->do_define_in_output_segment<64>(name, version, os,
86f2e683
ILT
1213 value, symsize, type,
1214 binding, visibility, nonvis,
1215 offset_base, only_if_ref);
1216#else
1217 gold_unreachable();
1218#endif
1219 }
ead1e424 1220 else
a3ad94ed 1221 gold_unreachable();
ead1e424
ILT
1222}
1223
1224// Define a symbol in an Output_segment, sized version.
1225
1226template<int size>
14b31740 1227Sized_symbol<size>*
ead1e424 1228Symbol_table::do_define_in_output_segment(
ead1e424 1229 const char* name,
14b31740 1230 const char* version,
ead1e424
ILT
1231 Output_segment* os,
1232 typename elfcpp::Elf_types<size>::Elf_Addr value,
1233 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1234 elfcpp::STT type,
1235 elfcpp::STB binding,
1236 elfcpp::STV visibility,
1237 unsigned char nonvis,
1238 Symbol::Segment_offset_base offset_base,
1239 bool only_if_ref)
1240{
1241 Sized_symbol<size>* sym;
86f2e683 1242 Sized_symbol<size>* oldsym;
ead1e424 1243
9025d29d
ILT
1244 if (parameters->is_big_endian())
1245 {
1246#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1247 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
9b07f471 1248 &name, &version, only_if_ref, &oldsym
9025d29d
ILT
1249 SELECT_SIZE_ENDIAN(size, true));
1250#else
1251 gold_unreachable();
1252#endif
1253 }
ead1e424 1254 else
9025d29d
ILT
1255 {
1256#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1257 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
9b07f471 1258 &name, &version, only_if_ref, &oldsym
9025d29d
ILT
1259 SELECT_SIZE_ENDIAN(size, false));
1260#else
1261 gold_unreachable();
1262#endif
1263 }
ead1e424
ILT
1264
1265 if (sym == NULL)
14b31740 1266 return NULL;
ead1e424 1267
d4f5281b 1268 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
1269 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1270 offset_base);
14b31740 1271
e5756efb 1272 if (oldsym == NULL)
55a93433
ILT
1273 {
1274 if (binding == elfcpp::STB_LOCAL
1275 || this->version_script_.symbol_is_local(name))
1276 this->force_local(sym);
1277 return sym;
1278 }
86f2e683 1279
e5756efb
ILT
1280 if (Symbol_table::should_override_with_special(oldsym))
1281 this->override_with_special(oldsym, sym);
1282 delete sym;
1283 return oldsym;
ead1e424
ILT
1284}
1285
1286// Define a special symbol with a constant value. It is a multiple
1287// definition error if this symbol is already defined.
1288
14b31740 1289Symbol*
9b07f471
ILT
1290Symbol_table::define_as_constant(const char* name,
1291 const char* version,
1292 uint64_t value,
1293 uint64_t symsize,
1294 elfcpp::STT type,
1295 elfcpp::STB binding,
1296 elfcpp::STV visibility,
1297 unsigned char nonvis,
1298 bool only_if_ref)
ead1e424 1299{
9025d29d 1300 if (parameters->get_size() == 32)
86f2e683
ILT
1301 {
1302#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
9b07f471 1303 return this->do_define_as_constant<32>(name, version, value,
86f2e683
ILT
1304 symsize, type, binding,
1305 visibility, nonvis, only_if_ref);
1306#else
1307 gold_unreachable();
1308#endif
1309 }
9025d29d 1310 else if (parameters->get_size() == 64)
86f2e683
ILT
1311 {
1312#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
9b07f471 1313 return this->do_define_as_constant<64>(name, version, value,
86f2e683
ILT
1314 symsize, type, binding,
1315 visibility, nonvis, only_if_ref);
1316#else
1317 gold_unreachable();
1318#endif
1319 }
ead1e424 1320 else
a3ad94ed 1321 gold_unreachable();
ead1e424
ILT
1322}
1323
1324// Define a symbol as a constant, sized version.
1325
1326template<int size>
14b31740 1327Sized_symbol<size>*
ead1e424 1328Symbol_table::do_define_as_constant(
ead1e424 1329 const char* name,
14b31740 1330 const char* version,
ead1e424
ILT
1331 typename elfcpp::Elf_types<size>::Elf_Addr value,
1332 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1333 elfcpp::STT type,
1334 elfcpp::STB binding,
1335 elfcpp::STV visibility,
1336 unsigned char nonvis,
1337 bool only_if_ref)
1338{
1339 Sized_symbol<size>* sym;
86f2e683 1340 Sized_symbol<size>* oldsym;
ead1e424 1341
9025d29d
ILT
1342 if (parameters->is_big_endian())
1343 {
1344#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1345 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
9b07f471 1346 &name, &version, only_if_ref, &oldsym
9025d29d
ILT
1347 SELECT_SIZE_ENDIAN(size, true));
1348#else
1349 gold_unreachable();
1350#endif
1351 }
ead1e424 1352 else
9025d29d
ILT
1353 {
1354#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1355 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
9b07f471 1356 &name, &version, only_if_ref, &oldsym
9025d29d
ILT
1357 SELECT_SIZE_ENDIAN(size, false));
1358#else
1359 gold_unreachable();
1360#endif
1361 }
ead1e424
ILT
1362
1363 if (sym == NULL)
14b31740 1364 return NULL;
ead1e424 1365
09124467 1366 gold_assert(version == NULL || version == name || oldsym != NULL);
ead1e424 1367 sym->init(name, value, symsize, type, binding, visibility, nonvis);
14b31740 1368
e5756efb 1369 if (oldsym == NULL)
55a93433
ILT
1370 {
1371 if (binding == elfcpp::STB_LOCAL
1372 || this->version_script_.symbol_is_local(name))
1373 this->force_local(sym);
1374 return sym;
1375 }
86f2e683 1376
e5756efb
ILT
1377 if (Symbol_table::should_override_with_special(oldsym))
1378 this->override_with_special(oldsym, sym);
1379 delete sym;
1380 return oldsym;
ead1e424
ILT
1381}
1382
1383// Define a set of symbols in output sections.
1384
1385void
9b07f471 1386Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
1387 const Define_symbol_in_section* p,
1388 bool only_if_ref)
ead1e424
ILT
1389{
1390 for (int i = 0; i < count; ++i, ++p)
1391 {
1392 Output_section* os = layout->find_output_section(p->output_section);
1393 if (os != NULL)
9b07f471 1394 this->define_in_output_data(p->name, NULL, os, p->value,
14b31740
ILT
1395 p->size, p->type, p->binding,
1396 p->visibility, p->nonvis,
a445fddf
ILT
1397 p->offset_is_from_end,
1398 only_if_ref || p->only_if_ref);
ead1e424 1399 else
9b07f471 1400 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 1401 p->binding, p->visibility, p->nonvis,
a445fddf 1402 only_if_ref || p->only_if_ref);
ead1e424
ILT
1403 }
1404}
1405
1406// Define a set of symbols in output segments.
1407
1408void
9b07f471 1409Symbol_table::define_symbols(const Layout* layout, int count,
a445fddf
ILT
1410 const Define_symbol_in_segment* p,
1411 bool only_if_ref)
ead1e424
ILT
1412{
1413 for (int i = 0; i < count; ++i, ++p)
1414 {
1415 Output_segment* os = layout->find_output_segment(p->segment_type,
1416 p->segment_flags_set,
1417 p->segment_flags_clear);
1418 if (os != NULL)
9b07f471 1419 this->define_in_output_segment(p->name, NULL, os, p->value,
14b31740
ILT
1420 p->size, p->type, p->binding,
1421 p->visibility, p->nonvis,
a445fddf
ILT
1422 p->offset_base,
1423 only_if_ref || p->only_if_ref);
ead1e424 1424 else
9b07f471 1425 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
ead1e424 1426 p->binding, p->visibility, p->nonvis,
a445fddf 1427 only_if_ref || p->only_if_ref);
ead1e424
ILT
1428 }
1429}
1430
46fe1623
ILT
1431// Define CSYM using a COPY reloc. POSD is the Output_data where the
1432// symbol should be defined--typically a .dyn.bss section. VALUE is
1433// the offset within POSD.
1434
1435template<int size>
1436void
fe8718a4 1437Symbol_table::define_with_copy_reloc(
fe8718a4
ILT
1438 Sized_symbol<size>* csym,
1439 Output_data* posd,
1440 typename elfcpp::Elf_types<size>::Elf_Addr value)
46fe1623
ILT
1441{
1442 gold_assert(csym->is_from_dynobj());
1443 gold_assert(!csym->is_copied_from_dynobj());
1444 Object* object = csym->object();
1445 gold_assert(object->is_dynamic());
1446 Dynobj* dynobj = static_cast<Dynobj*>(object);
1447
1448 // Our copied variable has to override any variable in a shared
1449 // library.
1450 elfcpp::STB binding = csym->binding();
1451 if (binding == elfcpp::STB_WEAK)
1452 binding = elfcpp::STB_GLOBAL;
1453
9b07f471 1454 this->define_in_output_data(csym->name(), csym->version(),
46fe1623
ILT
1455 posd, value, csym->symsize(),
1456 csym->type(), binding,
1457 csym->visibility(), csym->nonvis(),
1458 false, false);
1459
1460 csym->set_is_copied_from_dynobj();
1461 csym->set_needs_dynsym_entry();
1462
1463 this->copied_symbol_dynobjs_[csym] = dynobj;
1464
1465 // We have now defined all aliases, but we have not entered them all
1466 // in the copied_symbol_dynobjs_ map.
1467 if (csym->has_alias())
1468 {
1469 Symbol* sym = csym;
1470 while (true)
1471 {
1472 sym = this->weak_aliases_[sym];
1473 if (sym == csym)
1474 break;
1475 gold_assert(sym->output_data() == posd);
1476
1477 sym->set_is_copied_from_dynobj();
1478 this->copied_symbol_dynobjs_[sym] = dynobj;
1479 }
1480 }
1481}
1482
1483// SYM is defined using a COPY reloc. Return the dynamic object where
1484// the original definition was found.
1485
1486Dynobj*
1487Symbol_table::get_copy_source(const Symbol* sym) const
1488{
1489 gold_assert(sym->is_copied_from_dynobj());
1490 Copied_symbol_dynobjs::const_iterator p =
1491 this->copied_symbol_dynobjs_.find(sym);
1492 gold_assert(p != this->copied_symbol_dynobjs_.end());
1493 return p->second;
1494}
1495
a3ad94ed
ILT
1496// Set the dynamic symbol indexes. INDEX is the index of the first
1497// global dynamic symbol. Pointers to the symbols are stored into the
1498// vector SYMS. The names are added to DYNPOOL. This returns an
1499// updated dynamic symbol index.
1500
1501unsigned int
9b07f471 1502Symbol_table::set_dynsym_indexes(unsigned int index,
a3ad94ed 1503 std::vector<Symbol*>* syms,
14b31740
ILT
1504 Stringpool* dynpool,
1505 Versions* versions)
a3ad94ed
ILT
1506{
1507 for (Symbol_table_type::iterator p = this->table_.begin();
1508 p != this->table_.end();
1509 ++p)
1510 {
1511 Symbol* sym = p->second;
16649710
ILT
1512
1513 // Note that SYM may already have a dynamic symbol index, since
1514 // some symbols appear more than once in the symbol table, with
1515 // and without a version.
1516
436ca963 1517 if (!sym->should_add_dynsym_entry())
16649710
ILT
1518 sym->set_dynsym_index(-1U);
1519 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1520 {
1521 sym->set_dynsym_index(index);
1522 ++index;
1523 syms->push_back(sym);
cfd73a4e 1524 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
1525
1526 // Record any version information.
09124467
ILT
1527 if (sym->version() != NULL)
1528 versions->record_version(this, dynpool, sym);
a3ad94ed
ILT
1529 }
1530 }
1531
14b31740
ILT
1532 // Finish up the versions. In some cases this may add new dynamic
1533 // symbols.
9b07f471 1534 index = versions->finalize(this, index, syms);
14b31740 1535
a3ad94ed
ILT
1536 return index;
1537}
1538
c06b7b0b 1539// Set the final values for all the symbols. The index of the first
55a93433
ILT
1540// global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1541// file offset OFF. Add their names to POOL. Return the new file
1542// offset. Update *PLOCAL_SYMCOUNT if necessary.
54dc6425 1543
75f65a3e 1544off_t
55a93433
ILT
1545Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1546 size_t dyncount, Stringpool* pool,
1547 unsigned int *plocal_symcount)
54dc6425 1548{
f6ce93d6
ILT
1549 off_t ret;
1550
55a93433
ILT
1551 gold_assert(*plocal_symcount != 0);
1552 this->first_global_index_ = *plocal_symcount;
c06b7b0b 1553
16649710
ILT
1554 this->dynamic_offset_ = dynoff;
1555 this->first_dynamic_global_index_ = dyn_global_index;
1556 this->dynamic_count_ = dyncount;
1557
9025d29d
ILT
1558 if (parameters->get_size() == 32)
1559 {
1560#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
55a93433 1561 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
9025d29d
ILT
1562#else
1563 gold_unreachable();
1564#endif
1565 }
1566 else if (parameters->get_size() == 64)
1567 {
1568#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
55a93433 1569 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
9025d29d
ILT
1570#else
1571 gold_unreachable();
1572#endif
1573 }
61ba1cf9 1574 else
a3ad94ed 1575 gold_unreachable();
f6ce93d6
ILT
1576
1577 // Now that we have the final symbol table, we can reliably note
1578 // which symbols should get warnings.
cb295612 1579 this->warnings_.note_warnings(this);
f6ce93d6
ILT
1580
1581 return ret;
75f65a3e
ILT
1582}
1583
55a93433
ILT
1584// SYM is going into the symbol table at *PINDEX. Add the name to
1585// POOL, update *PINDEX and *POFF.
1586
1587template<int size>
1588void
1589Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1590 unsigned int* pindex, off_t* poff)
1591{
1592 sym->set_symtab_index(*pindex);
1593 pool->add(sym->name(), false, NULL);
1594 ++*pindex;
1595 *poff += elfcpp::Elf_sizes<size>::sym_size;
1596}
1597
ead1e424
ILT
1598// Set the final value for all the symbols. This is called after
1599// Layout::finalize, so all the output sections have their final
1600// address.
75f65a3e
ILT
1601
1602template<int size>
1603off_t
55a93433
ILT
1604Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1605 unsigned int* plocal_symcount)
75f65a3e 1606{
ead1e424 1607 off = align_address(off, size >> 3);
75f65a3e
ILT
1608 this->offset_ = off;
1609
55a93433
ILT
1610 unsigned int index = *plocal_symcount;
1611 const unsigned int orig_index = index;
c06b7b0b 1612
55a93433
ILT
1613 // First do all the symbols which have been forced to be local, as
1614 // they must appear before all global symbols.
1615 for (Forced_locals::iterator p = this->forced_locals_.begin();
1616 p != this->forced_locals_.end();
1617 ++p)
1618 {
1619 Symbol* sym = *p;
1620 gold_assert(sym->is_forced_local());
1621 if (this->sized_finalize_symbol<size>(sym))
1622 {
1623 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1624 ++*plocal_symcount;
1625 }
1626 }
1627
1628 // Now do all the remaining symbols.
c06b7b0b
ILT
1629 for (Symbol_table_type::iterator p = this->table_.begin();
1630 p != this->table_.end();
1631 ++p)
54dc6425 1632 {
55a93433
ILT
1633 Symbol* sym = p->second;
1634 if (this->sized_finalize_symbol<size>(sym))
1635 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1636 }
54dc6425 1637
55a93433 1638 this->output_count_ = index - orig_index;
a3ad94ed 1639
55a93433
ILT
1640 return off;
1641}
75f65a3e 1642
55a93433
ILT
1643// Finalize the symbol SYM. This returns true if the symbol should be
1644// added to the symbol table, false otherwise.
008db82e 1645
55a93433
ILT
1646template<int size>
1647bool
1648Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1649{
1650 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
75f65a3e 1651
55a93433
ILT
1652 // The default version of a symbol may appear twice in the symbol
1653 // table. We only need to finalize it once.
1654 if (sym->has_symtab_index())
1655 return false;
ead1e424 1656
55a93433
ILT
1657 if (!sym->in_reg())
1658 {
1659 gold_assert(!sym->has_symtab_index());
1660 sym->set_symtab_index(-1U);
1661 gold_assert(sym->dynsym_index() == -1U);
1662 return false;
1663 }
ead1e424 1664
55a93433 1665 typename Sized_symbol<size>::Value_type value;
ead1e424 1666
55a93433
ILT
1667 switch (sym->source())
1668 {
1669 case Symbol::FROM_OBJECT:
1670 {
1671 unsigned int shndx = sym->shndx();
ead1e424 1672
55a93433
ILT
1673 // FIXME: We need some target specific support here.
1674 if (shndx >= elfcpp::SHN_LORESERVE
1675 && shndx != elfcpp::SHN_ABS)
1676 {
1677 gold_error(_("%s: unsupported symbol section 0x%x"),
1678 sym->demangled_name().c_str(), shndx);
1679 shndx = elfcpp::SHN_UNDEF;
ead1e424 1680 }
ead1e424 1681
55a93433
ILT
1682 Object* symobj = sym->object();
1683 if (symobj->is_dynamic())
ead1e424 1684 {
55a93433
ILT
1685 value = 0;
1686 shndx = elfcpp::SHN_UNDEF;
ead1e424 1687 }
55a93433
ILT
1688 else if (shndx == elfcpp::SHN_UNDEF)
1689 value = 0;
1690 else if (shndx == elfcpp::SHN_ABS)
1691 value = sym->value();
1692 else
ead1e424 1693 {
55a93433
ILT
1694 Relobj* relobj = static_cast<Relobj*>(symobj);
1695 section_offset_type secoff;
1696 Output_section* os = relobj->output_section(shndx, &secoff);
1697
1698 if (os == NULL)
ead1e424 1699 {
55a93433
ILT
1700 sym->set_symtab_index(-1U);
1701 gold_assert(sym->dynsym_index() == -1U);
1702 return false;
ead1e424 1703 }
55a93433
ILT
1704
1705 if (sym->type() == elfcpp::STT_TLS)
1706 value = sym->value() + os->tls_offset() + secoff;
1707 else
1708 value = sym->value() + os->address() + secoff;
ead1e424 1709 }
55a93433
ILT
1710 }
1711 break;
1712
1713 case Symbol::IN_OUTPUT_DATA:
1714 {
1715 Output_data* od = sym->output_data();
1716 value = sym->value() + od->address();
1717 if (sym->offset_is_from_end())
1718 value += od->data_size();
1719 }
1720 break;
1721
1722 case Symbol::IN_OUTPUT_SEGMENT:
1723 {
1724 Output_segment* os = sym->output_segment();
1725 value = sym->value() + os->vaddr();
1726 switch (sym->offset_base())
1727 {
1728 case Symbol::SEGMENT_START:
1729 break;
1730 case Symbol::SEGMENT_END:
1731 value += os->memsz();
1732 break;
1733 case Symbol::SEGMENT_BSS:
1734 value += os->filesz();
1735 break;
1736 default:
1737 gold_unreachable();
1738 }
1739 }
1740 break;
ead1e424 1741
55a93433
ILT
1742 case Symbol::CONSTANT:
1743 value = sym->value();
1744 break;
ead1e424 1745
55a93433
ILT
1746 default:
1747 gold_unreachable();
1748 }
ead1e424 1749
55a93433 1750 sym->set_value(value);
9e2dcb77 1751
55a93433
ILT
1752 if (parameters->strip_all())
1753 {
1754 sym->set_symtab_index(-1U);
1755 return false;
54dc6425 1756 }
75f65a3e 1757
55a93433 1758 return true;
54dc6425
ILT
1759}
1760
61ba1cf9
ILT
1761// Write out the global symbols.
1762
1763void
9a2d6984
ILT
1764Symbol_table::write_globals(const Input_objects* input_objects,
1765 const Stringpool* sympool,
16649710 1766 const Stringpool* dynpool, Output_file* of) const
61ba1cf9 1767{
9025d29d 1768 if (parameters->get_size() == 32)
61ba1cf9 1769 {
9025d29d
ILT
1770 if (parameters->is_big_endian())
1771 {
1772#ifdef HAVE_TARGET_32_BIG
9a2d6984
ILT
1773 this->sized_write_globals<32, true>(input_objects, sympool,
1774 dynpool, of);
9025d29d
ILT
1775#else
1776 gold_unreachable();
1777#endif
1778 }
61ba1cf9 1779 else
9025d29d
ILT
1780 {
1781#ifdef HAVE_TARGET_32_LITTLE
9a2d6984
ILT
1782 this->sized_write_globals<32, false>(input_objects, sympool,
1783 dynpool, of);
9025d29d
ILT
1784#else
1785 gold_unreachable();
1786#endif
1787 }
61ba1cf9 1788 }
9025d29d 1789 else if (parameters->get_size() == 64)
61ba1cf9 1790 {
9025d29d
ILT
1791 if (parameters->is_big_endian())
1792 {
1793#ifdef HAVE_TARGET_64_BIG
9a2d6984
ILT
1794 this->sized_write_globals<64, true>(input_objects, sympool,
1795 dynpool, of);
9025d29d
ILT
1796#else
1797 gold_unreachable();
1798#endif
1799 }
61ba1cf9 1800 else
9025d29d
ILT
1801 {
1802#ifdef HAVE_TARGET_64_LITTLE
9a2d6984
ILT
1803 this->sized_write_globals<64, false>(input_objects, sympool,
1804 dynpool, of);
9025d29d
ILT
1805#else
1806 gold_unreachable();
1807#endif
1808 }
61ba1cf9
ILT
1809 }
1810 else
a3ad94ed 1811 gold_unreachable();
61ba1cf9
ILT
1812}
1813
1814// Write out the global symbols.
1815
1816template<int size, bool big_endian>
1817void
9a2d6984 1818Symbol_table::sized_write_globals(const Input_objects* input_objects,
61ba1cf9 1819 const Stringpool* sympool,
16649710 1820 const Stringpool* dynpool,
61ba1cf9
ILT
1821 Output_file* of) const
1822{
fbfba508 1823 const Target* const target = parameters->target();
9a2d6984 1824
61ba1cf9 1825 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
55a93433
ILT
1826
1827 const unsigned int output_count = this->output_count_;
1828 const section_size_type oview_size = output_count * sym_size;
1829 const unsigned int first_global_index = this->first_global_index_;
5fe2a0f5
ILT
1830 unsigned char* psyms;
1831 if (this->offset_ == 0 || output_count == 0)
1832 psyms = NULL;
1833 else
1834 psyms = of->get_output_view(this->offset_, oview_size);
16649710 1835
55a93433
ILT
1836 const unsigned int dynamic_count = this->dynamic_count_;
1837 const section_size_type dynamic_size = dynamic_count * sym_size;
1838 const unsigned int first_dynamic_global_index =
1839 this->first_dynamic_global_index_;
16649710 1840 unsigned char* dynamic_view;
5fe2a0f5 1841 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
16649710
ILT
1842 dynamic_view = NULL;
1843 else
1844 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 1845
61ba1cf9
ILT
1846 for (Symbol_table_type::const_iterator p = this->table_.begin();
1847 p != this->table_.end();
1848 ++p)
1849 {
1850 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1851
9a2d6984
ILT
1852 // Possibly warn about unresolved symbols in shared libraries.
1853 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
e2827e5f 1854
a3ad94ed 1855 unsigned int sym_index = sym->symtab_index();
16649710
ILT
1856 unsigned int dynsym_index;
1857 if (dynamic_view == NULL)
1858 dynsym_index = -1U;
1859 else
1860 dynsym_index = sym->dynsym_index();
1861
1862 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
1863 {
1864 // This symbol is not included in the output file.
1865 continue;
1866 }
16649710 1867
ead1e424 1868 unsigned int shndx;
88dd47ac
ILT
1869 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
1870 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
ead1e424
ILT
1871 switch (sym->source())
1872 {
1873 case Symbol::FROM_OBJECT:
1874 {
16649710 1875 unsigned int in_shndx = sym->shndx();
ead1e424
ILT
1876
1877 // FIXME: We need some target specific support here.
16649710
ILT
1878 if (in_shndx >= elfcpp::SHN_LORESERVE
1879 && in_shndx != elfcpp::SHN_ABS)
ead1e424 1880 {
75f2446e 1881 gold_error(_("%s: unsupported symbol section 0x%x"),
a2b1aa12 1882 sym->demangled_name().c_str(), in_shndx);
75f2446e 1883 shndx = in_shndx;
f6ce93d6 1884 }
ead1e424
ILT
1885 else
1886 {
75f2446e
ILT
1887 Object* symobj = sym->object();
1888 if (symobj->is_dynamic())
1889 {
1890 if (sym->needs_dynsym_value())
88dd47ac 1891 dynsym_value = target->dynsym_value(sym);
75f2446e
ILT
1892 shndx = elfcpp::SHN_UNDEF;
1893 }
1894 else if (in_shndx == elfcpp::SHN_UNDEF
1895 || in_shndx == elfcpp::SHN_ABS)
1896 shndx = in_shndx;
1897 else
1898 {
1899 Relobj* relobj = static_cast<Relobj*>(symobj);
8383303e 1900 section_offset_type secoff;
75f2446e
ILT
1901 Output_section* os = relobj->output_section(in_shndx,
1902 &secoff);
1903 gold_assert(os != NULL);
1904 shndx = os->out_shndx();
88dd47ac
ILT
1905
1906 // In object files symbol values are section
1907 // relative.
1908 if (parameters->output_is_object())
1909 sym_value -= os->address();
75f2446e 1910 }
ead1e424
ILT
1911 }
1912 }
1913 break;
1914
1915 case Symbol::IN_OUTPUT_DATA:
1916 shndx = sym->output_data()->out_shndx();
1917 break;
1918
1919 case Symbol::IN_OUTPUT_SEGMENT:
1920 shndx = elfcpp::SHN_ABS;
1921 break;
1922
1923 case Symbol::CONSTANT:
1924 shndx = elfcpp::SHN_ABS;
1925 break;
1926
1927 default:
a3ad94ed 1928 gold_unreachable();
ead1e424 1929 }
61ba1cf9 1930
16649710
ILT
1931 if (sym_index != -1U)
1932 {
55a93433
ILT
1933 sym_index -= first_global_index;
1934 gold_assert(sym_index < output_count);
1935 unsigned char* ps = psyms + (sym_index * sym_size);
6a469986 1936 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
88dd47ac 1937 sym, sym_value, shndx, sympool, ps
6a469986 1938 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1939 }
61ba1cf9 1940
16649710
ILT
1941 if (dynsym_index != -1U)
1942 {
1943 dynsym_index -= first_dynamic_global_index;
1944 gold_assert(dynsym_index < dynamic_count);
1945 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
6a469986 1946 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
88dd47ac 1947 sym, dynsym_value, shndx, dynpool, pd
6a469986 1948 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1949 }
61ba1cf9
ILT
1950 }
1951
c06b7b0b 1952 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
1953 if (dynamic_view != NULL)
1954 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1955}
1956
1957// Write out the symbol SYM, in section SHNDX, to P. POOL is the
1958// strtab holding the name.
1959
1960template<int size, bool big_endian>
1961void
ab5c9e90
ILT
1962Symbol_table::sized_write_symbol(
1963 Sized_symbol<size>* sym,
1964 typename elfcpp::Elf_types<size>::Elf_Addr value,
1965 unsigned int shndx,
1966 const Stringpool* pool,
1967 unsigned char* p
1968 ACCEPT_SIZE_ENDIAN) const
16649710
ILT
1969{
1970 elfcpp::Sym_write<size, big_endian> osym(p);
1971 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 1972 osym.put_st_value(value);
16649710 1973 osym.put_st_size(sym->symsize());
55a93433
ILT
1974 // A version script may have overridden the default binding.
1975 if (sym->is_forced_local())
1976 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
1977 else
1978 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
16649710
ILT
1979 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1980 osym.put_st_shndx(shndx);
61ba1cf9
ILT
1981}
1982
9a2d6984
ILT
1983// Check for unresolved symbols in shared libraries. This is
1984// controlled by the --allow-shlib-undefined option.
1985
1986// We only warn about libraries for which we have seen all the
1987// DT_NEEDED entries. We don't try to track down DT_NEEDED entries
1988// which were not seen in this link. If we didn't see a DT_NEEDED
1989// entry, we aren't going to be able to reliably report whether the
1990// symbol is undefined.
1991
1992// We also don't warn about libraries found in the system library
1993// directory (the directory were we find libc.so); we assume that
1994// those libraries are OK. This heuristic avoids problems in
1995// GNU/Linux, in which -ldl can have undefined references satisfied by
1996// ld-linux.so.
1997
1998inline void
1999Symbol_table::warn_about_undefined_dynobj_symbol(
2000 const Input_objects* input_objects,
2001 Symbol* sym) const
2002{
2003 if (sym->source() == Symbol::FROM_OBJECT
2004 && sym->object()->is_dynamic()
2005 && sym->shndx() == elfcpp::SHN_UNDEF
2006 && sym->binding() != elfcpp::STB_WEAK
2007 && !parameters->allow_shlib_undefined()
fbfba508 2008 && !parameters->target()->is_defined_by_abi(sym)
9a2d6984
ILT
2009 && !input_objects->found_in_system_library_directory(sym->object()))
2010 {
2011 // A very ugly cast.
2012 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2013 if (!dynobj->has_unknown_needed_entries())
2014 gold_error(_("%s: undefined reference to '%s'"),
a2b1aa12
ILT
2015 sym->object()->name().c_str(),
2016 sym->demangled_name().c_str());
9a2d6984
ILT
2017 }
2018}
2019
a3ad94ed
ILT
2020// Write out a section symbol. Return the update offset.
2021
2022void
9025d29d 2023Symbol_table::write_section_symbol(const Output_section *os,
a3ad94ed
ILT
2024 Output_file* of,
2025 off_t offset) const
2026{
9025d29d 2027 if (parameters->get_size() == 32)
a3ad94ed 2028 {
9025d29d
ILT
2029 if (parameters->is_big_endian())
2030 {
2031#ifdef HAVE_TARGET_32_BIG
2032 this->sized_write_section_symbol<32, true>(os, of, offset);
2033#else
2034 gold_unreachable();
2035#endif
2036 }
a3ad94ed 2037 else
9025d29d
ILT
2038 {
2039#ifdef HAVE_TARGET_32_LITTLE
2040 this->sized_write_section_symbol<32, false>(os, of, offset);
2041#else
2042 gold_unreachable();
2043#endif
2044 }
a3ad94ed 2045 }
9025d29d 2046 else if (parameters->get_size() == 64)
a3ad94ed 2047 {
9025d29d
ILT
2048 if (parameters->is_big_endian())
2049 {
2050#ifdef HAVE_TARGET_64_BIG
2051 this->sized_write_section_symbol<64, true>(os, of, offset);
2052#else
2053 gold_unreachable();
2054#endif
2055 }
a3ad94ed 2056 else
9025d29d
ILT
2057 {
2058#ifdef HAVE_TARGET_64_LITTLE
2059 this->sized_write_section_symbol<64, false>(os, of, offset);
2060#else
2061 gold_unreachable();
2062#endif
2063 }
a3ad94ed
ILT
2064 }
2065 else
2066 gold_unreachable();
2067}
2068
2069// Write out a section symbol, specialized for size and endianness.
2070
2071template<int size, bool big_endian>
2072void
2073Symbol_table::sized_write_section_symbol(const Output_section* os,
2074 Output_file* of,
2075 off_t offset) const
2076{
2077 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2078
2079 unsigned char* pov = of->get_output_view(offset, sym_size);
2080
2081 elfcpp::Sym_write<size, big_endian> osym(pov);
2082 osym.put_st_name(0);
2083 osym.put_st_value(os->address());
2084 osym.put_st_size(0);
2085 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2086 elfcpp::STT_SECTION));
2087 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2088 osym.put_st_shndx(os->out_shndx());
2089
2090 of->write_output_view(offset, sym_size, pov);
2091}
2092
abaa3995
ILT
2093// Print statistical information to stderr. This is used for --stats.
2094
2095void
2096Symbol_table::print_stats() const
2097{
2098#if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2099 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2100 program_name, this->table_.size(), this->table_.bucket_count());
2101#else
2102 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2103 program_name, this->table_.size());
2104#endif
ad8f37d1 2105 this->namepool_.print_stats("symbol table stringpool");
abaa3995
ILT
2106}
2107
ff541f30
ILT
2108// We check for ODR violations by looking for symbols with the same
2109// name for which the debugging information reports that they were
2110// defined in different source locations. When comparing the source
2111// location, we consider instances with the same base filename and
2112// line number to be the same. This is because different object
2113// files/shared libraries can include the same header file using
2114// different paths, and we don't want to report an ODR violation in
2115// that case.
2116
2117// This struct is used to compare line information, as returned by
7bf1f802 2118// Dwarf_line_info::one_addr2line. It implements a < comparison
ff541f30
ILT
2119// operator used with std::set.
2120
2121struct Odr_violation_compare
2122{
2123 bool
2124 operator()(const std::string& s1, const std::string& s2) const
2125 {
2126 std::string::size_type pos1 = s1.rfind('/');
2127 std::string::size_type pos2 = s2.rfind('/');
2128 if (pos1 == std::string::npos
2129 || pos2 == std::string::npos)
2130 return s1 < s2;
2131 return s1.compare(pos1, std::string::npos,
2132 s2, pos2, std::string::npos) < 0;
2133 }
2134};
2135
70e654ba
ILT
2136// Check candidate_odr_violations_ to find symbols with the same name
2137// but apparently different definitions (different source-file/line-no).
2138
2139void
17a1d0a9
ILT
2140Symbol_table::detect_odr_violations(const Task* task,
2141 const char* output_file_name) const
70e654ba
ILT
2142{
2143 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2144 it != candidate_odr_violations_.end();
2145 ++it)
2146 {
2147 const char* symbol_name = it->first;
2148 // We use a sorted set so the output is deterministic.
ff541f30 2149 std::set<std::string, Odr_violation_compare> line_nums;
70e654ba 2150
b01c0a4a
ILT
2151 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2152 locs = it->second.begin();
2153 locs != it->second.end();
2154 ++locs)
70e654ba
ILT
2155 {
2156 // We need to lock the object in order to read it. This
17a1d0a9
ILT
2157 // means that we have to run in a singleton Task. If we
2158 // want to run this in a general Task for better
2159 // performance, we will need one Task for object, plus
2160 // appropriate locking to ensure that we don't conflict with
2161 // other uses of the object.
2162 Task_lock_obj<Object> tl(task, locs->object);
a55ce7fe
ILT
2163 std::string lineno = Dwarf_line_info::one_addr2line(
2164 locs->object, locs->shndx, locs->offset);
70e654ba
ILT
2165 if (!lineno.empty())
2166 line_nums.insert(lineno);
2167 }
2168
2169 if (line_nums.size() > 1)
2170 {
dd8670e5 2171 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
78f15696 2172 "places (possible ODR violation):"),
a2b1aa12 2173 output_file_name, demangle(symbol_name).c_str());
70e654ba
ILT
2174 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2175 it2 != line_nums.end();
2176 ++it2)
2177 fprintf(stderr, " %s\n", it2->c_str());
2178 }
2179 }
2180}
2181
f6ce93d6
ILT
2182// Warnings functions.
2183
2184// Add a new warning.
2185
2186void
2187Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
cb295612 2188 const std::string& warning)
f6ce93d6
ILT
2189{
2190 name = symtab->canonicalize_name(name);
cb295612 2191 this->warnings_[name].set(obj, warning);
f6ce93d6
ILT
2192}
2193
2194// Look through the warnings and mark the symbols for which we should
2195// warn. This is called during Layout::finalize when we know the
2196// sources for all the symbols.
2197
2198void
cb295612 2199Warnings::note_warnings(Symbol_table* symtab)
f6ce93d6
ILT
2200{
2201 for (Warning_table::iterator p = this->warnings_.begin();
2202 p != this->warnings_.end();
2203 ++p)
2204 {
2205 Symbol* sym = symtab->lookup(p->first, NULL);
2206 if (sym != NULL
2207 && sym->source() == Symbol::FROM_OBJECT
2208 && sym->object() == p->second.object)
cb295612 2209 sym->set_has_warning();
f6ce93d6
ILT
2210 }
2211}
2212
2213// Issue a warning. This is called when we see a relocation against a
2214// symbol for which has a warning.
2215
75f2446e 2216template<int size, bool big_endian>
f6ce93d6 2217void
75f2446e
ILT
2218Warnings::issue_warning(const Symbol* sym,
2219 const Relocate_info<size, big_endian>* relinfo,
2220 size_t relnum, off_t reloffset) const
f6ce93d6 2221{
a3ad94ed 2222 gold_assert(sym->has_warning());
f6ce93d6 2223 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 2224 gold_assert(p != this->warnings_.end());
75f2446e
ILT
2225 gold_warning_at_location(relinfo, relnum, reloffset,
2226 "%s", p->second.text.c_str());
f6ce93d6
ILT
2227}
2228
14bfc3f5
ILT
2229// Instantiate the templates we need. We could use the configure
2230// script to restrict this to only the ones needed for implemented
2231// targets.
2232
c7912668
ILT
2233#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2234template
2235void
2236Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2237#endif
2238
2239#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2240template
2241void
2242Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2243#endif
2244
193a53d9 2245#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
2246template
2247void
193a53d9
ILT
2248Symbol_table::add_from_relobj<32, false>(
2249 Sized_relobj<32, false>* relobj,
f6ce93d6 2250 const unsigned char* syms,
14bfc3f5
ILT
2251 size_t count,
2252 const char* sym_names,
2253 size_t sym_name_size,
730cdc88 2254 Sized_relobj<32, true>::Symbols* sympointers);
193a53d9 2255#endif
14bfc3f5 2256
193a53d9 2257#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
2258template
2259void
193a53d9
ILT
2260Symbol_table::add_from_relobj<32, true>(
2261 Sized_relobj<32, true>* relobj,
f6ce93d6 2262 const unsigned char* syms,
14bfc3f5
ILT
2263 size_t count,
2264 const char* sym_names,
2265 size_t sym_name_size,
730cdc88 2266 Sized_relobj<32, false>::Symbols* sympointers);
193a53d9 2267#endif
14bfc3f5 2268
193a53d9 2269#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
2270template
2271void
193a53d9
ILT
2272Symbol_table::add_from_relobj<64, false>(
2273 Sized_relobj<64, false>* relobj,
f6ce93d6 2274 const unsigned char* syms,
14bfc3f5
ILT
2275 size_t count,
2276 const char* sym_names,
2277 size_t sym_name_size,
730cdc88 2278 Sized_relobj<64, true>::Symbols* sympointers);
193a53d9 2279#endif
14bfc3f5 2280
193a53d9 2281#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
2282template
2283void
193a53d9
ILT
2284Symbol_table::add_from_relobj<64, true>(
2285 Sized_relobj<64, true>* relobj,
f6ce93d6 2286 const unsigned char* syms,
14bfc3f5
ILT
2287 size_t count,
2288 const char* sym_names,
2289 size_t sym_name_size,
730cdc88 2290 Sized_relobj<64, false>::Symbols* sympointers);
193a53d9 2291#endif
14bfc3f5 2292
193a53d9 2293#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
2294template
2295void
193a53d9
ILT
2296Symbol_table::add_from_dynobj<32, false>(
2297 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
2298 const unsigned char* syms,
2299 size_t count,
2300 const char* sym_names,
2301 size_t sym_name_size,
2302 const unsigned char* versym,
2303 size_t versym_size,
2304 const std::vector<const char*>* version_map);
193a53d9 2305#endif
dbe717ef 2306
193a53d9 2307#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
2308template
2309void
193a53d9
ILT
2310Symbol_table::add_from_dynobj<32, true>(
2311 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
2312 const unsigned char* syms,
2313 size_t count,
2314 const char* sym_names,
2315 size_t sym_name_size,
2316 const unsigned char* versym,
2317 size_t versym_size,
2318 const std::vector<const char*>* version_map);
193a53d9 2319#endif
dbe717ef 2320
193a53d9 2321#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
2322template
2323void
193a53d9
ILT
2324Symbol_table::add_from_dynobj<64, false>(
2325 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
2326 const unsigned char* syms,
2327 size_t count,
2328 const char* sym_names,
2329 size_t sym_name_size,
2330 const unsigned char* versym,
2331 size_t versym_size,
2332 const std::vector<const char*>* version_map);
193a53d9 2333#endif
dbe717ef 2334
193a53d9 2335#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
2336template
2337void
193a53d9
ILT
2338Symbol_table::add_from_dynobj<64, true>(
2339 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
2340 const unsigned char* syms,
2341 size_t count,
2342 const char* sym_names,
2343 size_t sym_name_size,
2344 const unsigned char* versym,
2345 size_t versym_size,
2346 const std::vector<const char*>* version_map);
193a53d9 2347#endif
dbe717ef 2348
46fe1623
ILT
2349#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2350template
2351void
fe8718a4 2352Symbol_table::define_with_copy_reloc<32>(
fe8718a4
ILT
2353 Sized_symbol<32>* sym,
2354 Output_data* posd,
2355 elfcpp::Elf_types<32>::Elf_Addr value);
46fe1623
ILT
2356#endif
2357
2358#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2359template
2360void
fe8718a4 2361Symbol_table::define_with_copy_reloc<64>(
fe8718a4
ILT
2362 Sized_symbol<64>* sym,
2363 Output_data* posd,
2364 elfcpp::Elf_types<64>::Elf_Addr value);
46fe1623
ILT
2365#endif
2366
75f2446e
ILT
2367#ifdef HAVE_TARGET_32_LITTLE
2368template
2369void
2370Warnings::issue_warning<32, false>(const Symbol* sym,
2371 const Relocate_info<32, false>* relinfo,
2372 size_t relnum, off_t reloffset) const;
2373#endif
2374
2375#ifdef HAVE_TARGET_32_BIG
2376template
2377void
2378Warnings::issue_warning<32, true>(const Symbol* sym,
2379 const Relocate_info<32, true>* relinfo,
2380 size_t relnum, off_t reloffset) const;
2381#endif
2382
2383#ifdef HAVE_TARGET_64_LITTLE
2384template
2385void
2386Warnings::issue_warning<64, false>(const Symbol* sym,
2387 const Relocate_info<64, false>* relinfo,
2388 size_t relnum, off_t reloffset) const;
2389#endif
2390
2391#ifdef HAVE_TARGET_64_BIG
2392template
2393void
2394Warnings::issue_warning<64, true>(const Symbol* sym,
2395 const Relocate_info<64, true>* relinfo,
2396 size_t relnum, off_t reloffset) const;
2397#endif
2398
14bfc3f5 2399} // End namespace gold.
This page took 0.199508 seconds and 4 git commands to generate.