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