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