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