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