]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/symtab.cc
Fix typo in comment.
[thirdparty/binutils-gdb.git] / gold / symtab.cc
CommitLineData
14bfc3f5
ILT
1// symtab.cc -- the gold symbol table
2
6cb15b7f
ILT
3// Copyright 2006, 2007 Free Software Foundation, Inc.
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
ILT
25#include <stdint.h>
26#include <string>
27#include <utility>
28
29#include "object.h"
dbe717ef 30#include "dynobj.h"
75f65a3e 31#include "output.h"
61ba1cf9 32#include "target.h"
645f8123 33#include "workqueue.h"
14bfc3f5
ILT
34#include "symtab.h"
35
36namespace gold
37{
38
39// Class Symbol.
40
ead1e424
ILT
41// Initialize fields in Symbol. This initializes everything except u_
42// and source_.
14bfc3f5 43
14bfc3f5 44void
ead1e424
ILT
45Symbol::init_fields(const char* name, const char* version,
46 elfcpp::STT type, elfcpp::STB binding,
47 elfcpp::STV visibility, unsigned char nonvis)
14bfc3f5
ILT
48{
49 this->name_ = name;
50 this->version_ = version;
c06b7b0b
ILT
51 this->symtab_index_ = 0;
52 this->dynsym_index_ = 0;
ead1e424 53 this->got_offset_ = 0;
f4151f89 54 this->plt_offset_ = 0;
ead1e424
ILT
55 this->type_ = type;
56 this->binding_ = binding;
57 this->visibility_ = visibility;
58 this->nonvis_ = nonvis;
59 this->is_target_special_ = false;
1564db8d
ILT
60 this->is_def_ = false;
61 this->is_forwarder_ = false;
c06b7b0b 62 this->needs_dynsym_entry_ = false;
008db82e 63 this->in_reg_ = false;
ead1e424
ILT
64 this->in_dyn_ = false;
65 this->has_got_offset_ = false;
f4151f89 66 this->has_plt_offset_ = false;
f6ce93d6 67 this->has_warning_ = false;
ead1e424
ILT
68}
69
70// Initialize the fields in the base class Symbol for SYM in OBJECT.
71
72template<int size, bool big_endian>
73void
74Symbol::init_base(const char* name, const char* version, Object* object,
75 const elfcpp::Sym<size, big_endian>& sym)
76{
77 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
78 sym.get_st_visibility(), sym.get_st_nonvis());
79 this->u_.from_object.object = object;
80 // FIXME: Handle SHN_XINDEX.
16649710 81 this->u_.from_object.shndx = sym.get_st_shndx();
ead1e424 82 this->source_ = FROM_OBJECT;
008db82e 83 this->in_reg_ = !object->is_dynamic();
1564db8d 84 this->in_dyn_ = object->is_dynamic();
14bfc3f5
ILT
85}
86
ead1e424
ILT
87// Initialize the fields in the base class Symbol for a symbol defined
88// in an Output_data.
89
90void
91Symbol::init_base(const char* name, Output_data* od, elfcpp::STT type,
92 elfcpp::STB binding, elfcpp::STV visibility,
93 unsigned char nonvis, bool offset_is_from_end)
94{
95 this->init_fields(name, NULL, type, binding, visibility, nonvis);
96 this->u_.in_output_data.output_data = od;
97 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
98 this->source_ = IN_OUTPUT_DATA;
008db82e 99 this->in_reg_ = true;
ead1e424
ILT
100}
101
102// Initialize the fields in the base class Symbol for a symbol defined
103// in an Output_segment.
104
105void
106Symbol::init_base(const char* name, Output_segment* os, elfcpp::STT type,
107 elfcpp::STB binding, elfcpp::STV visibility,
108 unsigned char nonvis, Segment_offset_base offset_base)
109{
110 this->init_fields(name, NULL, type, binding, visibility, nonvis);
111 this->u_.in_output_segment.output_segment = os;
112 this->u_.in_output_segment.offset_base = offset_base;
113 this->source_ = IN_OUTPUT_SEGMENT;
008db82e 114 this->in_reg_ = true;
ead1e424
ILT
115}
116
117// Initialize the fields in the base class Symbol for a symbol defined
118// as a constant.
119
120void
121Symbol::init_base(const char* name, elfcpp::STT type,
122 elfcpp::STB binding, elfcpp::STV visibility,
123 unsigned char nonvis)
124{
125 this->init_fields(name, NULL, type, binding, visibility, nonvis);
126 this->source_ = CONSTANT;
008db82e 127 this->in_reg_ = true;
ead1e424
ILT
128}
129
130// Initialize the fields in Sized_symbol for SYM in OBJECT.
14bfc3f5
ILT
131
132template<int size>
133template<bool big_endian>
134void
135Sized_symbol<size>::init(const char* name, const char* version, Object* object,
136 const elfcpp::Sym<size, big_endian>& sym)
137{
138 this->init_base(name, version, object, sym);
139 this->value_ = sym.get_st_value();
ead1e424
ILT
140 this->symsize_ = sym.get_st_size();
141}
142
143// Initialize the fields in Sized_symbol for a symbol defined in an
144// Output_data.
145
146template<int size>
147void
148Sized_symbol<size>::init(const char* name, Output_data* od,
149 Value_type value, Size_type symsize,
150 elfcpp::STT type, elfcpp::STB binding,
151 elfcpp::STV visibility, unsigned char nonvis,
152 bool offset_is_from_end)
153{
154 this->init_base(name, od, type, binding, visibility, nonvis,
155 offset_is_from_end);
156 this->value_ = value;
157 this->symsize_ = symsize;
158}
159
160// Initialize the fields in Sized_symbol for a symbol defined in an
161// Output_segment.
162
163template<int size>
164void
165Sized_symbol<size>::init(const char* name, Output_segment* os,
166 Value_type value, Size_type symsize,
167 elfcpp::STT type, elfcpp::STB binding,
168 elfcpp::STV visibility, unsigned char nonvis,
169 Segment_offset_base offset_base)
170{
171 this->init_base(name, os, type, binding, visibility, nonvis, offset_base);
172 this->value_ = value;
173 this->symsize_ = symsize;
174}
175
176// Initialize the fields in Sized_symbol for a symbol defined as a
177// constant.
178
179template<int size>
180void
181Sized_symbol<size>::init(const char* name, Value_type value, Size_type symsize,
182 elfcpp::STT type, elfcpp::STB binding,
183 elfcpp::STV visibility, unsigned char nonvis)
184{
185 this->init_base(name, type, binding, visibility, nonvis);
186 this->value_ = value;
187 this->symsize_ = symsize;
14bfc3f5
ILT
188}
189
b3b74ddc
ILT
190// Return true if the final value of this symbol is known at link
191// time.
192
193bool
194Symbol::final_value_is_known() const
195{
196 // If we are not generating an executable, then no final values are
197 // known, since they will change at runtime.
198 if (!parameters->output_is_executable())
199 return false;
200
201 // If the symbol is not from an object file, then it is defined, and
202 // known.
203 if (this->source_ != FROM_OBJECT)
204 return true;
205
206 // If the symbol is from a dynamic object, then the final value is
207 // not known.
208 if (this->object()->is_dynamic())
209 return false;
210
211 // If the symbol is not undefined (it is defined or common), then
212 // the final value is known.
213 if (!this->is_undefined())
214 return true;
215
216 // If the symbol is undefined, then whether the final value is known
217 // depends on whether we are doing a static link. If we are doing a
218 // dynamic link, then the final value could be filled in at runtime.
219 // This could reasonably be the case for a weak undefined symbol.
220 return parameters->doing_static_link();
221}
222
14bfc3f5
ILT
223// Class Symbol_table.
224
225Symbol_table::Symbol_table()
9025d29d 226 : saw_undefined_(0), offset_(0), table_(), namepool_(),
f6ce93d6 227 forwarders_(), commons_(), warnings_()
14bfc3f5
ILT
228{
229}
230
231Symbol_table::~Symbol_table()
232{
233}
234
235// The hash function. The key is always canonicalized, so we use a
236// simple combination of the pointers.
237
238size_t
239Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
240{
f0641a0b 241 return key.first ^ key.second;
14bfc3f5
ILT
242}
243
244// The symbol table key equality function. This is only called with
245// canonicalized name and version strings, so we can use pointer
246// comparison.
247
248bool
249Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
250 const Symbol_table_key& k2) const
251{
252 return k1.first == k2.first && k1.second == k2.second;
253}
254
255// Make TO a symbol which forwards to FROM.
256
257void
258Symbol_table::make_forwarder(Symbol* from, Symbol* to)
259{
a3ad94ed
ILT
260 gold_assert(from != to);
261 gold_assert(!from->is_forwarder() && !to->is_forwarder());
14bfc3f5
ILT
262 this->forwarders_[from] = to;
263 from->set_forwarder();
264}
265
61ba1cf9
ILT
266// Resolve the forwards from FROM, returning the real symbol.
267
14bfc3f5 268Symbol*
c06b7b0b 269Symbol_table::resolve_forwards(const Symbol* from) const
14bfc3f5 270{
a3ad94ed 271 gold_assert(from->is_forwarder());
c06b7b0b 272 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
14bfc3f5 273 this->forwarders_.find(from);
a3ad94ed 274 gold_assert(p != this->forwarders_.end());
14bfc3f5
ILT
275 return p->second;
276}
277
61ba1cf9
ILT
278// Look up a symbol by name.
279
280Symbol*
281Symbol_table::lookup(const char* name, const char* version) const
282{
f0641a0b
ILT
283 Stringpool::Key name_key;
284 name = this->namepool_.find(name, &name_key);
61ba1cf9
ILT
285 if (name == NULL)
286 return NULL;
f0641a0b
ILT
287
288 Stringpool::Key version_key = 0;
61ba1cf9
ILT
289 if (version != NULL)
290 {
f0641a0b 291 version = this->namepool_.find(version, &version_key);
61ba1cf9
ILT
292 if (version == NULL)
293 return NULL;
294 }
295
f0641a0b 296 Symbol_table_key key(name_key, version_key);
61ba1cf9
ILT
297 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
298 if (p == this->table_.end())
299 return NULL;
300 return p->second;
301}
302
14bfc3f5
ILT
303// Resolve a Symbol with another Symbol. This is only used in the
304// unusual case where there are references to both an unversioned
305// symbol and a symbol with a version, and we then discover that that
1564db8d
ILT
306// version is the default version. Because this is unusual, we do
307// this the slow way, by converting back to an ELF symbol.
14bfc3f5 308
1564db8d 309template<int size, bool big_endian>
14bfc3f5 310void
14b31740
ILT
311Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
312 const char* version ACCEPT_SIZE_ENDIAN)
14bfc3f5 313{
1564db8d
ILT
314 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
315 elfcpp::Sym_write<size, big_endian> esym(buf);
316 // We don't bother to set the st_name field.
317 esym.put_st_value(from->value());
318 esym.put_st_size(from->symsize());
319 esym.put_st_info(from->binding(), from->type());
ead1e424 320 esym.put_st_other(from->visibility(), from->nonvis());
16649710 321 esym.put_st_shndx(from->shndx());
14b31740 322 Symbol_table::resolve(to, esym.sym(), from->object(), version);
1ebd95fd
ILT
323 if (from->in_reg())
324 to->set_in_reg();
325 if (from->in_dyn())
326 to->set_in_dyn();
14bfc3f5
ILT
327}
328
329// Add one symbol from OBJECT to the symbol table. NAME is symbol
330// name and VERSION is the version; both are canonicalized. DEF is
331// whether this is the default version.
332
333// If DEF is true, then this is the definition of a default version of
334// a symbol. That means that any lookup of NAME/NULL and any lookup
335// of NAME/VERSION should always return the same symbol. This is
336// obvious for references, but in particular we want to do this for
337// definitions: overriding NAME/NULL should also override
338// NAME/VERSION. If we don't do that, it would be very hard to
339// override functions in a shared library which uses versioning.
340
341// We implement this by simply making both entries in the hash table
342// point to the same Symbol structure. That is easy enough if this is
343// the first time we see NAME/NULL or NAME/VERSION, but it is possible
344// that we have seen both already, in which case they will both have
345// independent entries in the symbol table. We can't simply change
346// the symbol table entry, because we have pointers to the entries
347// attached to the object files. So we mark the entry attached to the
348// object file as a forwarder, and record it in the forwarders_ map.
349// Note that entries in the hash table will never be marked as
350// forwarders.
351
352template<int size, bool big_endian>
353Symbol*
f6ce93d6 354Symbol_table::add_from_object(Object* object,
14bfc3f5 355 const char *name,
f0641a0b
ILT
356 Stringpool::Key name_key,
357 const char *version,
358 Stringpool::Key version_key,
359 bool def,
14bfc3f5
ILT
360 const elfcpp::Sym<size, big_endian>& sym)
361{
362 Symbol* const snull = NULL;
363 std::pair<typename Symbol_table_type::iterator, bool> ins =
f0641a0b
ILT
364 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
365 snull));
14bfc3f5
ILT
366
367 std::pair<typename Symbol_table_type::iterator, bool> insdef =
368 std::make_pair(this->table_.end(), false);
369 if (def)
370 {
f0641a0b
ILT
371 const Stringpool::Key vnull_key = 0;
372 insdef = this->table_.insert(std::make_pair(std::make_pair(name_key,
373 vnull_key),
14bfc3f5
ILT
374 snull));
375 }
376
377 // ins.first: an iterator, which is a pointer to a pair.
378 // ins.first->first: the key (a pair of name and version).
379 // ins.first->second: the value (Symbol*).
380 // ins.second: true if new entry was inserted, false if not.
381
1564db8d 382 Sized_symbol<size>* ret;
ead1e424
ILT
383 bool was_undefined;
384 bool was_common;
14bfc3f5
ILT
385 if (!ins.second)
386 {
387 // We already have an entry for NAME/VERSION.
593f47df
ILT
388 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (ins.first->second
389 SELECT_SIZE(size));
a3ad94ed 390 gold_assert(ret != NULL);
ead1e424
ILT
391
392 was_undefined = ret->is_undefined();
393 was_common = ret->is_common();
394
14b31740 395 Symbol_table::resolve(ret, sym, object, version);
14bfc3f5
ILT
396
397 if (def)
398 {
399 if (insdef.second)
400 {
401 // This is the first time we have seen NAME/NULL. Make
402 // NAME/NULL point to NAME/VERSION.
403 insdef.first->second = ret;
404 }
dbe717ef 405 else if (insdef.first->second != ret)
14bfc3f5
ILT
406 {
407 // This is the unfortunate case where we already have
408 // entries for both NAME/VERSION and NAME/NULL.
274e99f9 409 const Sized_symbol<size>* sym2;
593f47df 410 sym2 = this->get_sized_symbol SELECT_SIZE_NAME(size) (
5482377d
ILT
411 insdef.first->second
412 SELECT_SIZE(size));
593f47df 413 Symbol_table::resolve SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
14b31740 414 ret, sym2, version SELECT_SIZE_ENDIAN(size, big_endian));
14bfc3f5
ILT
415 this->make_forwarder(insdef.first->second, ret);
416 insdef.first->second = ret;
417 }
418 }
419 }
420 else
421 {
422 // This is the first time we have seen NAME/VERSION.
a3ad94ed 423 gold_assert(ins.first->second == NULL);
ead1e424
ILT
424
425 was_undefined = false;
426 was_common = false;
427
14bfc3f5
ILT
428 if (def && !insdef.second)
429 {
14b31740
ILT
430 // We already have an entry for NAME/NULL. If we override
431 // it, then change it to NAME/VERSION.
593f47df
ILT
432 ret = this->get_sized_symbol SELECT_SIZE_NAME(size) (
433 insdef.first->second
434 SELECT_SIZE(size));
14b31740 435 Symbol_table::resolve(ret, sym, object, version);
14bfc3f5
ILT
436 ins.first->second = ret;
437 }
438 else
439 {
f6ce93d6
ILT
440 Sized_target<size, big_endian>* target =
441 object->sized_target SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
442 SELECT_SIZE_ENDIAN_ONLY(size, big_endian));
1564db8d
ILT
443 if (!target->has_make_symbol())
444 ret = new Sized_symbol<size>();
445 else
14bfc3f5 446 {
1564db8d
ILT
447 ret = target->make_symbol();
448 if (ret == NULL)
14bfc3f5
ILT
449 {
450 // This means that we don't want a symbol table
451 // entry after all.
452 if (!def)
453 this->table_.erase(ins.first);
454 else
455 {
456 this->table_.erase(insdef.first);
457 // Inserting insdef invalidated ins.
f0641a0b
ILT
458 this->table_.erase(std::make_pair(name_key,
459 version_key));
14bfc3f5
ILT
460 }
461 return NULL;
462 }
463 }
14bfc3f5 464
1564db8d
ILT
465 ret->init(name, version, object, sym);
466
14bfc3f5
ILT
467 ins.first->second = ret;
468 if (def)
469 {
470 // This is the first time we have seen NAME/NULL. Point
471 // it at the new entry for NAME/VERSION.
a3ad94ed 472 gold_assert(insdef.second);
14bfc3f5
ILT
473 insdef.first->second = ret;
474 }
475 }
476 }
477
ead1e424
ILT
478 // Record every time we see a new undefined symbol, to speed up
479 // archive groups.
480 if (!was_undefined && ret->is_undefined())
481 ++this->saw_undefined_;
482
483 // Keep track of common symbols, to speed up common symbol
484 // allocation.
485 if (!was_common && ret->is_common())
486 this->commons_.push_back(ret);
487
14bfc3f5
ILT
488 return ret;
489}
490
f6ce93d6 491// Add all the symbols in a relocatable object to the hash table.
14bfc3f5
ILT
492
493template<int size, bool big_endian>
494void
dbe717ef
ILT
495Symbol_table::add_from_relobj(
496 Sized_relobj<size, big_endian>* relobj,
f6ce93d6 497 const unsigned char* syms,
14bfc3f5
ILT
498 size_t count,
499 const char* sym_names,
500 size_t sym_name_size,
501 Symbol** sympointers)
502{
9025d29d
ILT
503 gold_assert(size == relobj->target()->get_size());
504 gold_assert(size == parameters->get_size());
14bfc3f5 505
a783673b
ILT
506 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
507
f6ce93d6 508 const unsigned char* p = syms;
a783673b 509 for (size_t i = 0; i < count; ++i, p += sym_size)
14bfc3f5
ILT
510 {
511 elfcpp::Sym<size, big_endian> sym(p);
a783673b 512 elfcpp::Sym<size, big_endian>* psym = &sym;
14bfc3f5 513
a783673b 514 unsigned int st_name = psym->get_st_name();
14bfc3f5
ILT
515 if (st_name >= sym_name_size)
516 {
54dc6425
ILT
517 fprintf(stderr,
518 _("%s: %s: bad global symbol name offset %u at %lu\n"),
dbe717ef 519 program_name, relobj->name().c_str(), st_name,
14bfc3f5
ILT
520 static_cast<unsigned long>(i));
521 gold_exit(false);
522 }
523
dbe717ef
ILT
524 const char* name = sym_names + st_name;
525
a783673b
ILT
526 // A symbol defined in a section which we are not including must
527 // be treated as an undefined symbol.
528 unsigned char symbuf[sym_size];
529 elfcpp::Sym<size, big_endian> sym2(symbuf);
530 unsigned int st_shndx = psym->get_st_shndx();
531 if (st_shndx != elfcpp::SHN_UNDEF
532 && st_shndx < elfcpp::SHN_LORESERVE
dbe717ef 533 && !relobj->is_section_included(st_shndx))
a783673b
ILT
534 {
535 memcpy(symbuf, p, sym_size);
536 elfcpp::Sym_write<size, big_endian> sw(symbuf);
537 sw.put_st_shndx(elfcpp::SHN_UNDEF);
538 psym = &sym2;
539 }
540
14bfc3f5
ILT
541 // In an object file, an '@' in the name separates the symbol
542 // name from the version name. If there are two '@' characters,
543 // this is the default version.
544 const char* ver = strchr(name, '@');
545
546 Symbol* res;
547 if (ver == NULL)
548 {
f0641a0b 549 Stringpool::Key name_key;
cfd73a4e 550 name = this->namepool_.add(name, true, &name_key);
dbe717ef 551 res = this->add_from_object(relobj, name, name_key, NULL, 0,
f0641a0b 552 false, *psym);
14bfc3f5
ILT
553 }
554 else
555 {
f0641a0b 556 Stringpool::Key name_key;
cfd73a4e 557 name = this->namepool_.add_prefix(name, ver - name, &name_key);
f0641a0b 558
14bfc3f5
ILT
559 bool def = false;
560 ++ver;
561 if (*ver == '@')
562 {
563 def = true;
564 ++ver;
565 }
f0641a0b
ILT
566
567 Stringpool::Key ver_key;
cfd73a4e 568 ver = this->namepool_.add(ver, true, &ver_key);
f0641a0b 569
dbe717ef 570 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
f0641a0b 571 def, *psym);
14bfc3f5
ILT
572 }
573
574 *sympointers++ = res;
14bfc3f5
ILT
575 }
576}
577
dbe717ef
ILT
578// Add all the symbols in a dynamic object to the hash table.
579
580template<int size, bool big_endian>
581void
582Symbol_table::add_from_dynobj(
583 Sized_dynobj<size, big_endian>* dynobj,
584 const unsigned char* syms,
585 size_t count,
586 const char* sym_names,
587 size_t sym_name_size,
588 const unsigned char* versym,
589 size_t versym_size,
590 const std::vector<const char*>* version_map)
591{
9025d29d
ILT
592 gold_assert(size == dynobj->target()->get_size());
593 gold_assert(size == parameters->get_size());
dbe717ef
ILT
594
595 if (versym != NULL && versym_size / 2 < count)
596 {
597 fprintf(stderr, _("%s: %s: too few symbol versions\n"),
598 program_name, dynobj->name().c_str());
599 gold_exit(false);
600 }
601
602 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
603
604 const unsigned char* p = syms;
605 const unsigned char* vs = versym;
606 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
607 {
608 elfcpp::Sym<size, big_endian> sym(p);
609
610 // Ignore symbols with local binding.
611 if (sym.get_st_bind() == elfcpp::STB_LOCAL)
612 continue;
613
614 unsigned int st_name = sym.get_st_name();
615 if (st_name >= sym_name_size)
616 {
617 fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
618 program_name, dynobj->name().c_str(), st_name,
619 static_cast<unsigned long>(i));
620 gold_exit(false);
621 }
622
623 const char* name = sym_names + st_name;
624
625 if (versym == NULL)
626 {
627 Stringpool::Key name_key;
cfd73a4e 628 name = this->namepool_.add(name, true, &name_key);
dbe717ef
ILT
629 this->add_from_object(dynobj, name, name_key, NULL, 0,
630 false, sym);
631 continue;
632 }
633
634 // Read the version information.
635
636 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
637
638 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
639 v &= elfcpp::VERSYM_VERSION;
640
64707334
ILT
641 // The Sun documentation says that V can be VER_NDX_LOCAL, or
642 // VER_NDX_GLOBAL, or a version index. The meaning of
643 // VER_NDX_LOCAL is defined as "Symbol has local scope." The
644 // old GNU linker will happily generate VER_NDX_LOCAL for an
645 // undefined symbol. I don't know what the Sun linker will
646 // generate.
647
648 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
649 && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
dbe717ef
ILT
650 {
651 // This symbol should not be visible outside the object.
652 continue;
653 }
654
655 // At this point we are definitely going to add this symbol.
656 Stringpool::Key name_key;
cfd73a4e 657 name = this->namepool_.add(name, true, &name_key);
dbe717ef 658
64707334
ILT
659 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
660 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
dbe717ef
ILT
661 {
662 // This symbol does not have a version.
663 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
664 continue;
665 }
666
667 if (v >= version_map->size())
668 {
669 fprintf(stderr,
670 _("%s: %s: versym for symbol %zu out of range: %u\n"),
671 program_name, dynobj->name().c_str(), i, v);
672 gold_exit(false);
673 }
674
675 const char* version = (*version_map)[v];
676 if (version == NULL)
677 {
678 fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
679 program_name, dynobj->name().c_str(), i, v);
680 gold_exit(false);
681 }
682
683 Stringpool::Key version_key;
cfd73a4e 684 version = this->namepool_.add(version, true, &version_key);
dbe717ef
ILT
685
686 // If this is an absolute symbol, and the version name and
687 // symbol name are the same, then this is the version definition
688 // symbol. These symbols exist to support using -u to pull in
689 // particular versions. We do not want to record a version for
690 // them.
691 if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
692 {
693 this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
694 continue;
695 }
696
697 const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
698
699 this->add_from_object(dynobj, name, name_key, version, version_key,
700 def, sym);
701 }
702}
703
ead1e424
ILT
704// Create and return a specially defined symbol. If ONLY_IF_REF is
705// true, then only create the symbol if there is a reference to it.
86f2e683 706// If this does not return NULL, it sets *POLDSYM to the existing
306d9ef0 707// symbol if there is one. This canonicalizes *PNAME and *PVERSION.
ead1e424
ILT
708
709template<int size, bool big_endian>
710Sized_symbol<size>*
306d9ef0
ILT
711Symbol_table::define_special_symbol(const Target* target, const char** pname,
712 const char** pversion, bool only_if_ref,
86f2e683 713 Sized_symbol<size>** poldsym
593f47df 714 ACCEPT_SIZE_ENDIAN)
ead1e424 715{
ead1e424
ILT
716 Symbol* oldsym;
717 Sized_symbol<size>* sym;
86f2e683
ILT
718 bool add_to_table = false;
719 typename Symbol_table_type::iterator add_loc = this->table_.end();
ead1e424
ILT
720
721 if (only_if_ref)
722 {
306d9ef0 723 oldsym = this->lookup(*pname, *pversion);
f6ce93d6 724 if (oldsym == NULL || !oldsym->is_undefined())
ead1e424 725 return NULL;
306d9ef0
ILT
726
727 *pname = oldsym->name();
728 *pversion = oldsym->version();
ead1e424
ILT
729 }
730 else
731 {
14b31740 732 // Canonicalize NAME and VERSION.
f0641a0b 733 Stringpool::Key name_key;
cfd73a4e 734 *pname = this->namepool_.add(*pname, true, &name_key);
ead1e424 735
14b31740 736 Stringpool::Key version_key = 0;
306d9ef0 737 if (*pversion != NULL)
cfd73a4e 738 *pversion = this->namepool_.add(*pversion, true, &version_key);
14b31740 739
ead1e424 740 Symbol* const snull = NULL;
ead1e424 741 std::pair<typename Symbol_table_type::iterator, bool> ins =
14b31740
ILT
742 this->table_.insert(std::make_pair(std::make_pair(name_key,
743 version_key),
ead1e424
ILT
744 snull));
745
746 if (!ins.second)
747 {
14b31740 748 // We already have a symbol table entry for NAME/VERSION.
ead1e424 749 oldsym = ins.first->second;
a3ad94ed 750 gold_assert(oldsym != NULL);
ead1e424
ILT
751 }
752 else
753 {
754 // We haven't seen this symbol before.
a3ad94ed 755 gold_assert(ins.first->second == NULL);
86f2e683
ILT
756 add_to_table = true;
757 add_loc = ins.first;
ead1e424
ILT
758 oldsym = NULL;
759 }
760 }
761
86f2e683
ILT
762 if (!target->has_make_symbol())
763 sym = new Sized_symbol<size>();
764 else
ead1e424 765 {
86f2e683
ILT
766 gold_assert(target->get_size() == size);
767 gold_assert(target->is_big_endian() ? big_endian : !big_endian);
768 typedef Sized_target<size, big_endian> My_target;
769 const My_target* sized_target =
770 static_cast<const My_target*>(target);
771 sym = sized_target->make_symbol();
772 if (sym == NULL)
773 return NULL;
774 }
ead1e424 775
86f2e683
ILT
776 if (add_to_table)
777 add_loc->second = sym;
778 else
779 gold_assert(oldsym != NULL);
ead1e424 780
86f2e683
ILT
781 *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
782 SELECT_SIZE(size));
ead1e424
ILT
783
784 return sym;
785}
786
787// Define a symbol based on an Output_data.
788
14b31740
ILT
789Symbol*
790Symbol_table::define_in_output_data(const Target* target, const char* name,
791 const char* version, Output_data* od,
ead1e424
ILT
792 uint64_t value, uint64_t symsize,
793 elfcpp::STT type, elfcpp::STB binding,
794 elfcpp::STV visibility,
795 unsigned char nonvis,
796 bool offset_is_from_end,
797 bool only_if_ref)
798{
9025d29d 799 if (parameters->get_size() == 32)
86f2e683
ILT
800 {
801#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
802 return this->do_define_in_output_data<32>(target, name, version, od,
803 value, symsize, type, binding,
804 visibility, nonvis,
805 offset_is_from_end,
806 only_if_ref);
807#else
808 gold_unreachable();
809#endif
810 }
9025d29d 811 else if (parameters->get_size() == 64)
86f2e683
ILT
812 {
813#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
814 return this->do_define_in_output_data<64>(target, name, version, od,
815 value, symsize, type, binding,
816 visibility, nonvis,
817 offset_is_from_end,
818 only_if_ref);
819#else
820 gold_unreachable();
821#endif
822 }
ead1e424 823 else
a3ad94ed 824 gold_unreachable();
ead1e424
ILT
825}
826
827// Define a symbol in an Output_data, sized version.
828
829template<int size>
14b31740 830Sized_symbol<size>*
ead1e424 831Symbol_table::do_define_in_output_data(
14b31740 832 const Target* target,
ead1e424 833 const char* name,
14b31740 834 const char* version,
ead1e424
ILT
835 Output_data* od,
836 typename elfcpp::Elf_types<size>::Elf_Addr value,
837 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
838 elfcpp::STT type,
839 elfcpp::STB binding,
840 elfcpp::STV visibility,
841 unsigned char nonvis,
842 bool offset_is_from_end,
843 bool only_if_ref)
844{
845 Sized_symbol<size>* sym;
86f2e683 846 Sized_symbol<size>* oldsym;
ead1e424 847
9025d29d 848 if (parameters->is_big_endian())
193a53d9
ILT
849 {
850#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
851 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
306d9ef0 852 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
853 SELECT_SIZE_ENDIAN(size, true));
854#else
855 gold_unreachable();
856#endif
857 }
ead1e424 858 else
193a53d9
ILT
859 {
860#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
861 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
306d9ef0 862 target, &name, &version, only_if_ref, &oldsym
193a53d9
ILT
863 SELECT_SIZE_ENDIAN(size, false));
864#else
865 gold_unreachable();
866#endif
867 }
ead1e424
ILT
868
869 if (sym == NULL)
14b31740 870 return NULL;
ead1e424 871
d4f5281b 872 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
873 sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
874 offset_is_from_end);
14b31740 875
86f2e683
ILT
876 if (oldsym != NULL
877 && Symbol_table::should_override_with_special(oldsym))
878 oldsym->override_with_special(sym);
879
14b31740 880 return sym;
ead1e424
ILT
881}
882
883// Define a symbol based on an Output_segment.
884
14b31740
ILT
885Symbol*
886Symbol_table::define_in_output_segment(const Target* target, const char* name,
887 const char* version, Output_segment* os,
ead1e424
ILT
888 uint64_t value, uint64_t symsize,
889 elfcpp::STT type, elfcpp::STB binding,
890 elfcpp::STV visibility,
891 unsigned char nonvis,
892 Symbol::Segment_offset_base offset_base,
893 bool only_if_ref)
894{
9025d29d 895 if (parameters->get_size() == 32)
86f2e683
ILT
896 {
897#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
898 return this->do_define_in_output_segment<32>(target, name, version, os,
899 value, symsize, type,
900 binding, visibility, nonvis,
901 offset_base, only_if_ref);
902#else
903 gold_unreachable();
904#endif
905 }
9025d29d 906 else if (parameters->get_size() == 64)
86f2e683
ILT
907 {
908#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
909 return this->do_define_in_output_segment<64>(target, name, version, os,
910 value, symsize, type,
911 binding, visibility, nonvis,
912 offset_base, only_if_ref);
913#else
914 gold_unreachable();
915#endif
916 }
ead1e424 917 else
a3ad94ed 918 gold_unreachable();
ead1e424
ILT
919}
920
921// Define a symbol in an Output_segment, sized version.
922
923template<int size>
14b31740 924Sized_symbol<size>*
ead1e424 925Symbol_table::do_define_in_output_segment(
14b31740 926 const Target* target,
ead1e424 927 const char* name,
14b31740 928 const char* version,
ead1e424
ILT
929 Output_segment* os,
930 typename elfcpp::Elf_types<size>::Elf_Addr value,
931 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
932 elfcpp::STT type,
933 elfcpp::STB binding,
934 elfcpp::STV visibility,
935 unsigned char nonvis,
936 Symbol::Segment_offset_base offset_base,
937 bool only_if_ref)
938{
939 Sized_symbol<size>* sym;
86f2e683 940 Sized_symbol<size>* oldsym;
ead1e424 941
9025d29d
ILT
942 if (parameters->is_big_endian())
943 {
944#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
945 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
946 target, &name, &version, only_if_ref, &oldsym
947 SELECT_SIZE_ENDIAN(size, true));
948#else
949 gold_unreachable();
950#endif
951 }
ead1e424 952 else
9025d29d
ILT
953 {
954#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
955 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
956 target, &name, &version, only_if_ref, &oldsym
957 SELECT_SIZE_ENDIAN(size, false));
958#else
959 gold_unreachable();
960#endif
961 }
ead1e424
ILT
962
963 if (sym == NULL)
14b31740 964 return NULL;
ead1e424 965
d4f5281b 966 gold_assert(version == NULL || oldsym != NULL);
ead1e424
ILT
967 sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
968 offset_base);
14b31740 969
86f2e683
ILT
970 if (oldsym != NULL
971 && Symbol_table::should_override_with_special(oldsym))
972 oldsym->override_with_special(sym);
973
14b31740 974 return sym;
ead1e424
ILT
975}
976
977// Define a special symbol with a constant value. It is a multiple
978// definition error if this symbol is already defined.
979
14b31740
ILT
980Symbol*
981Symbol_table::define_as_constant(const Target* target, const char* name,
982 const char* version, uint64_t value,
983 uint64_t symsize, elfcpp::STT type,
984 elfcpp::STB binding, elfcpp::STV visibility,
985 unsigned char nonvis, bool only_if_ref)
ead1e424 986{
9025d29d 987 if (parameters->get_size() == 32)
86f2e683
ILT
988 {
989#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
990 return this->do_define_as_constant<32>(target, name, version, value,
991 symsize, type, binding,
992 visibility, nonvis, only_if_ref);
993#else
994 gold_unreachable();
995#endif
996 }
9025d29d 997 else if (parameters->get_size() == 64)
86f2e683
ILT
998 {
999#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1000 return this->do_define_as_constant<64>(target, name, version, value,
1001 symsize, type, binding,
1002 visibility, nonvis, only_if_ref);
1003#else
1004 gold_unreachable();
1005#endif
1006 }
ead1e424 1007 else
a3ad94ed 1008 gold_unreachable();
ead1e424
ILT
1009}
1010
1011// Define a symbol as a constant, sized version.
1012
1013template<int size>
14b31740 1014Sized_symbol<size>*
ead1e424 1015Symbol_table::do_define_as_constant(
14b31740 1016 const Target* target,
ead1e424 1017 const char* name,
14b31740 1018 const char* version,
ead1e424
ILT
1019 typename elfcpp::Elf_types<size>::Elf_Addr value,
1020 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1021 elfcpp::STT type,
1022 elfcpp::STB binding,
1023 elfcpp::STV visibility,
1024 unsigned char nonvis,
1025 bool only_if_ref)
1026{
1027 Sized_symbol<size>* sym;
86f2e683 1028 Sized_symbol<size>* oldsym;
ead1e424 1029
9025d29d
ILT
1030 if (parameters->is_big_endian())
1031 {
1032#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1033 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1034 target, &name, &version, only_if_ref, &oldsym
1035 SELECT_SIZE_ENDIAN(size, true));
1036#else
1037 gold_unreachable();
1038#endif
1039 }
ead1e424 1040 else
9025d29d
ILT
1041 {
1042#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1043 sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1044 target, &name, &version, only_if_ref, &oldsym
1045 SELECT_SIZE_ENDIAN(size, false));
1046#else
1047 gold_unreachable();
1048#endif
1049 }
ead1e424
ILT
1050
1051 if (sym == NULL)
14b31740 1052 return NULL;
ead1e424 1053
d4f5281b 1054 gold_assert(version == NULL || oldsym != NULL);
ead1e424 1055 sym->init(name, value, symsize, type, binding, visibility, nonvis);
14b31740 1056
86f2e683
ILT
1057 if (oldsym != NULL
1058 && Symbol_table::should_override_with_special(oldsym))
1059 oldsym->override_with_special(sym);
1060
14b31740 1061 return sym;
ead1e424
ILT
1062}
1063
1064// Define a set of symbols in output sections.
1065
1066void
14b31740
ILT
1067Symbol_table::define_symbols(const Layout* layout, const Target* target,
1068 int count, const Define_symbol_in_section* p)
ead1e424
ILT
1069{
1070 for (int i = 0; i < count; ++i, ++p)
1071 {
1072 Output_section* os = layout->find_output_section(p->output_section);
1073 if (os != NULL)
14b31740
ILT
1074 this->define_in_output_data(target, p->name, NULL, os, p->value,
1075 p->size, p->type, p->binding,
1076 p->visibility, p->nonvis,
1077 p->offset_is_from_end, p->only_if_ref);
ead1e424 1078 else
14b31740 1079 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1080 p->binding, p->visibility, p->nonvis,
1081 p->only_if_ref);
1082 }
1083}
1084
1085// Define a set of symbols in output segments.
1086
1087void
14b31740
ILT
1088Symbol_table::define_symbols(const Layout* layout, const Target* target,
1089 int count, const Define_symbol_in_segment* p)
ead1e424
ILT
1090{
1091 for (int i = 0; i < count; ++i, ++p)
1092 {
1093 Output_segment* os = layout->find_output_segment(p->segment_type,
1094 p->segment_flags_set,
1095 p->segment_flags_clear);
1096 if (os != NULL)
14b31740
ILT
1097 this->define_in_output_segment(target, p->name, NULL, os, p->value,
1098 p->size, p->type, p->binding,
1099 p->visibility, p->nonvis,
1100 p->offset_base, p->only_if_ref);
ead1e424 1101 else
14b31740 1102 this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
ead1e424
ILT
1103 p->binding, p->visibility, p->nonvis,
1104 p->only_if_ref);
1105 }
1106}
1107
a3ad94ed
ILT
1108// Set the dynamic symbol indexes. INDEX is the index of the first
1109// global dynamic symbol. Pointers to the symbols are stored into the
1110// vector SYMS. The names are added to DYNPOOL. This returns an
1111// updated dynamic symbol index.
1112
1113unsigned int
14b31740
ILT
1114Symbol_table::set_dynsym_indexes(const General_options* options,
1115 const Target* target,
1116 unsigned int index,
a3ad94ed 1117 std::vector<Symbol*>* syms,
14b31740
ILT
1118 Stringpool* dynpool,
1119 Versions* versions)
a3ad94ed
ILT
1120{
1121 for (Symbol_table_type::iterator p = this->table_.begin();
1122 p != this->table_.end();
1123 ++p)
1124 {
1125 Symbol* sym = p->second;
16649710
ILT
1126
1127 // Note that SYM may already have a dynamic symbol index, since
1128 // some symbols appear more than once in the symbol table, with
1129 // and without a version.
1130
a6badf5a
ILT
1131 if (!sym->needs_dynsym_entry()
1132 && (!options->export_dynamic()
1133 || !sym->in_reg()
1134 || !sym->is_externally_visible()))
16649710
ILT
1135 sym->set_dynsym_index(-1U);
1136 else if (!sym->has_dynsym_index())
a3ad94ed
ILT
1137 {
1138 sym->set_dynsym_index(index);
1139 ++index;
1140 syms->push_back(sym);
cfd73a4e 1141 dynpool->add(sym->name(), false, NULL);
14b31740
ILT
1142
1143 // Record any version information.
1144 if (sym->version() != NULL)
1145 versions->record_version(options, dynpool, sym);
a3ad94ed
ILT
1146 }
1147 }
1148
14b31740
ILT
1149 // Finish up the versions. In some cases this may add new dynamic
1150 // symbols.
1151 index = versions->finalize(target, this, index, syms);
1152
a3ad94ed
ILT
1153 return index;
1154}
1155
c06b7b0b
ILT
1156// Set the final values for all the symbols. The index of the first
1157// global symbol in the output file is INDEX. Record the file offset
75f65a3e 1158// OFF. Add their names to POOL. Return the new file offset.
54dc6425 1159
75f65a3e 1160off_t
16649710
ILT
1161Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1162 size_t dyn_global_index, size_t dyncount,
1163 Stringpool* pool)
54dc6425 1164{
f6ce93d6
ILT
1165 off_t ret;
1166
a3ad94ed 1167 gold_assert(index != 0);
c06b7b0b
ILT
1168 this->first_global_index_ = index;
1169
16649710
ILT
1170 this->dynamic_offset_ = dynoff;
1171 this->first_dynamic_global_index_ = dyn_global_index;
1172 this->dynamic_count_ = dyncount;
1173
9025d29d
ILT
1174 if (parameters->get_size() == 32)
1175 {
1176#if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1177 ret = this->sized_finalize<32>(index, off, pool);
1178#else
1179 gold_unreachable();
1180#endif
1181 }
1182 else if (parameters->get_size() == 64)
1183 {
1184#if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1185 ret = this->sized_finalize<64>(index, off, pool);
1186#else
1187 gold_unreachable();
1188#endif
1189 }
61ba1cf9 1190 else
a3ad94ed 1191 gold_unreachable();
f6ce93d6
ILT
1192
1193 // Now that we have the final symbol table, we can reliably note
1194 // which symbols should get warnings.
1195 this->warnings_.note_warnings(this);
1196
1197 return ret;
75f65a3e
ILT
1198}
1199
ead1e424
ILT
1200// Set the final value for all the symbols. This is called after
1201// Layout::finalize, so all the output sections have their final
1202// address.
75f65a3e
ILT
1203
1204template<int size>
1205off_t
c06b7b0b 1206Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
75f65a3e 1207{
ead1e424 1208 off = align_address(off, size >> 3);
75f65a3e
ILT
1209 this->offset_ = off;
1210
c06b7b0b
ILT
1211 size_t orig_index = index;
1212
75f65a3e 1213 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1214 for (Symbol_table_type::iterator p = this->table_.begin();
1215 p != this->table_.end();
1216 ++p)
54dc6425 1217 {
75f65a3e 1218 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
54dc6425 1219
75f65a3e 1220 // FIXME: Here we need to decide which symbols should go into
a3ad94ed
ILT
1221 // the output file, based on --strip.
1222
1223 // The default version of a symbol may appear twice in the
1224 // symbol table. We only need to finalize it once.
1225 if (sym->has_symtab_index())
1226 continue;
75f65a3e 1227
008db82e
ILT
1228 if (!sym->in_reg())
1229 {
1230 gold_assert(!sym->has_symtab_index());
1231 sym->set_symtab_index(-1U);
1232 gold_assert(sym->dynsym_index() == -1U);
1233 continue;
1234 }
1235
ead1e424 1236 typename Sized_symbol<size>::Value_type value;
75f65a3e 1237
ead1e424 1238 switch (sym->source())
75f65a3e 1239 {
ead1e424
ILT
1240 case Symbol::FROM_OBJECT:
1241 {
16649710 1242 unsigned int shndx = sym->shndx();
ead1e424
ILT
1243
1244 // FIXME: We need some target specific support here.
16649710
ILT
1245 if (shndx >= elfcpp::SHN_LORESERVE
1246 && shndx != elfcpp::SHN_ABS)
ead1e424
ILT
1247 {
1248 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
16649710 1249 program_name, sym->name(), shndx);
ead1e424
ILT
1250 gold_exit(false);
1251 }
1252
f6ce93d6
ILT
1253 Object* symobj = sym->object();
1254 if (symobj->is_dynamic())
1255 {
1256 value = 0;
16649710 1257 shndx = elfcpp::SHN_UNDEF;
f6ce93d6 1258 }
16649710 1259 else if (shndx == elfcpp::SHN_UNDEF)
ead1e424 1260 value = 0;
16649710 1261 else if (shndx == elfcpp::SHN_ABS)
ead1e424
ILT
1262 value = sym->value();
1263 else
1264 {
f6ce93d6 1265 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1266 off_t secoff;
16649710 1267 Output_section* os = relobj->output_section(shndx, &secoff);
ead1e424
ILT
1268
1269 if (os == NULL)
1270 {
c06b7b0b 1271 sym->set_symtab_index(-1U);
16649710 1272 gold_assert(sym->dynsym_index() == -1U);
ead1e424
ILT
1273 continue;
1274 }
1275
1276 value = sym->value() + os->address() + secoff;
1277 }
1278 }
1279 break;
1280
1281 case Symbol::IN_OUTPUT_DATA:
1282 {
1283 Output_data* od = sym->output_data();
1284 value = sym->value() + od->address();
1285 if (sym->offset_is_from_end())
1286 value += od->data_size();
1287 }
1288 break;
1289
1290 case Symbol::IN_OUTPUT_SEGMENT:
1291 {
1292 Output_segment* os = sym->output_segment();
1293 value = sym->value() + os->vaddr();
1294 switch (sym->offset_base())
1295 {
1296 case Symbol::SEGMENT_START:
1297 break;
1298 case Symbol::SEGMENT_END:
1299 value += os->memsz();
1300 break;
1301 case Symbol::SEGMENT_BSS:
1302 value += os->filesz();
1303 break;
1304 default:
a3ad94ed 1305 gold_unreachable();
ead1e424
ILT
1306 }
1307 }
1308 break;
1309
1310 case Symbol::CONSTANT:
1311 value = sym->value();
1312 break;
1313
1314 default:
a3ad94ed 1315 gold_unreachable();
54dc6425 1316 }
ead1e424
ILT
1317
1318 sym->set_value(value);
9e2dcb77
ILT
1319
1320 if (parameters->strip_all())
1321 sym->set_symtab_index(-1U);
1322 else
1323 {
1324 sym->set_symtab_index(index);
cfd73a4e 1325 pool->add(sym->name(), false, NULL);
9e2dcb77
ILT
1326 ++index;
1327 off += sym_size;
1328 }
54dc6425 1329 }
75f65a3e 1330
c06b7b0b 1331 this->output_count_ = index - orig_index;
61ba1cf9 1332
75f65a3e 1333 return off;
54dc6425
ILT
1334}
1335
61ba1cf9
ILT
1336// Write out the global symbols.
1337
1338void
1339Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
16649710 1340 const Stringpool* dynpool, Output_file* of) const
61ba1cf9 1341{
9025d29d 1342 if (parameters->get_size() == 32)
61ba1cf9 1343 {
9025d29d
ILT
1344 if (parameters->is_big_endian())
1345 {
1346#ifdef HAVE_TARGET_32_BIG
1347 this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1348#else
1349 gold_unreachable();
1350#endif
1351 }
61ba1cf9 1352 else
9025d29d
ILT
1353 {
1354#ifdef HAVE_TARGET_32_LITTLE
1355 this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1356#else
1357 gold_unreachable();
1358#endif
1359 }
61ba1cf9 1360 }
9025d29d 1361 else if (parameters->get_size() == 64)
61ba1cf9 1362 {
9025d29d
ILT
1363 if (parameters->is_big_endian())
1364 {
1365#ifdef HAVE_TARGET_64_BIG
1366 this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1367#else
1368 gold_unreachable();
1369#endif
1370 }
61ba1cf9 1371 else
9025d29d
ILT
1372 {
1373#ifdef HAVE_TARGET_64_LITTLE
1374 this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1375#else
1376 gold_unreachable();
1377#endif
1378 }
61ba1cf9
ILT
1379 }
1380 else
a3ad94ed 1381 gold_unreachable();
61ba1cf9
ILT
1382}
1383
1384// Write out the global symbols.
1385
1386template<int size, bool big_endian>
1387void
ab5c9e90 1388Symbol_table::sized_write_globals(const Target* target,
61ba1cf9 1389 const Stringpool* sympool,
16649710 1390 const Stringpool* dynpool,
61ba1cf9
ILT
1391 Output_file* of) const
1392{
1393 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
c06b7b0b
ILT
1394 unsigned int index = this->first_global_index_;
1395 const off_t oview_size = this->output_count_ * sym_size;
16649710
ILT
1396 unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1397
1398 unsigned int dynamic_count = this->dynamic_count_;
1399 off_t dynamic_size = dynamic_count * sym_size;
1400 unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1401 unsigned char* dynamic_view;
1402 if (this->dynamic_offset_ == 0)
1403 dynamic_view = NULL;
1404 else
1405 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
c06b7b0b 1406
61ba1cf9
ILT
1407 unsigned char* ps = psyms;
1408 for (Symbol_table_type::const_iterator p = this->table_.begin();
1409 p != this->table_.end();
1410 ++p)
1411 {
1412 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1413
a3ad94ed 1414 unsigned int sym_index = sym->symtab_index();
16649710
ILT
1415 unsigned int dynsym_index;
1416 if (dynamic_view == NULL)
1417 dynsym_index = -1U;
1418 else
1419 dynsym_index = sym->dynsym_index();
1420
1421 if (sym_index == -1U && dynsym_index == -1U)
a3ad94ed
ILT
1422 {
1423 // This symbol is not included in the output file.
1424 continue;
1425 }
16649710
ILT
1426
1427 if (sym_index == index)
1428 ++index;
1429 else if (sym_index != -1U)
a3ad94ed
ILT
1430 {
1431 // We have already seen this symbol, because it has a
1432 // default version.
1433 gold_assert(sym_index < index);
16649710
ILT
1434 if (dynsym_index == -1U)
1435 continue;
1436 sym_index = -1U;
a3ad94ed 1437 }
c06b7b0b 1438
ead1e424 1439 unsigned int shndx;
ab5c9e90 1440 typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
ead1e424
ILT
1441 switch (sym->source())
1442 {
1443 case Symbol::FROM_OBJECT:
1444 {
16649710 1445 unsigned int in_shndx = sym->shndx();
ead1e424
ILT
1446
1447 // FIXME: We need some target specific support here.
16649710
ILT
1448 if (in_shndx >= elfcpp::SHN_LORESERVE
1449 && in_shndx != elfcpp::SHN_ABS)
ead1e424
ILT
1450 {
1451 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
16649710 1452 program_name, sym->name(), in_shndx);
ead1e424
ILT
1453 gold_exit(false);
1454 }
1455
f6ce93d6
ILT
1456 Object* symobj = sym->object();
1457 if (symobj->is_dynamic())
1458 {
ab5c9e90
ILT
1459 if (sym->needs_dynsym_value())
1460 value = target->dynsym_value(sym);
f6ce93d6
ILT
1461 shndx = elfcpp::SHN_UNDEF;
1462 }
16649710
ILT
1463 else if (in_shndx == elfcpp::SHN_UNDEF
1464 || in_shndx == elfcpp::SHN_ABS)
1465 shndx = in_shndx;
ead1e424
ILT
1466 else
1467 {
f6ce93d6 1468 Relobj* relobj = static_cast<Relobj*>(symobj);
ead1e424 1469 off_t secoff;
16649710 1470 Output_section* os = relobj->output_section(in_shndx, &secoff);
a3ad94ed 1471 gold_assert(os != NULL);
ead1e424
ILT
1472 shndx = os->out_shndx();
1473 }
1474 }
1475 break;
1476
1477 case Symbol::IN_OUTPUT_DATA:
1478 shndx = sym->output_data()->out_shndx();
1479 break;
1480
1481 case Symbol::IN_OUTPUT_SEGMENT:
1482 shndx = elfcpp::SHN_ABS;
1483 break;
1484
1485 case Symbol::CONSTANT:
1486 shndx = elfcpp::SHN_ABS;
1487 break;
1488
1489 default:
a3ad94ed 1490 gold_unreachable();
ead1e424 1491 }
61ba1cf9 1492
16649710
ILT
1493 if (sym_index != -1U)
1494 {
6a469986 1495 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1496 sym, sym->value(), shndx, sympool, ps
6a469986 1497 SELECT_SIZE_ENDIAN(size, big_endian));
16649710
ILT
1498 ps += sym_size;
1499 }
61ba1cf9 1500
16649710
ILT
1501 if (dynsym_index != -1U)
1502 {
1503 dynsym_index -= first_dynamic_global_index;
1504 gold_assert(dynsym_index < dynamic_count);
1505 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
6a469986 1506 this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
ab5c9e90 1507 sym, value, shndx, dynpool, pd
6a469986 1508 SELECT_SIZE_ENDIAN(size, big_endian));
16649710 1509 }
61ba1cf9
ILT
1510 }
1511
a3ad94ed 1512 gold_assert(ps - psyms == oview_size);
c06b7b0b
ILT
1513
1514 of->write_output_view(this->offset_, oview_size, psyms);
16649710
ILT
1515 if (dynamic_view != NULL)
1516 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1517}
1518
1519// Write out the symbol SYM, in section SHNDX, to P. POOL is the
1520// strtab holding the name.
1521
1522template<int size, bool big_endian>
1523void
ab5c9e90
ILT
1524Symbol_table::sized_write_symbol(
1525 Sized_symbol<size>* sym,
1526 typename elfcpp::Elf_types<size>::Elf_Addr value,
1527 unsigned int shndx,
1528 const Stringpool* pool,
1529 unsigned char* p
1530 ACCEPT_SIZE_ENDIAN) const
16649710
ILT
1531{
1532 elfcpp::Sym_write<size, big_endian> osym(p);
1533 osym.put_st_name(pool->get_offset(sym->name()));
ab5c9e90 1534 osym.put_st_value(value);
16649710
ILT
1535 osym.put_st_size(sym->symsize());
1536 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1537 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1538 osym.put_st_shndx(shndx);
61ba1cf9
ILT
1539}
1540
a3ad94ed
ILT
1541// Write out a section symbol. Return the update offset.
1542
1543void
9025d29d 1544Symbol_table::write_section_symbol(const Output_section *os,
a3ad94ed
ILT
1545 Output_file* of,
1546 off_t offset) const
1547{
9025d29d 1548 if (parameters->get_size() == 32)
a3ad94ed 1549 {
9025d29d
ILT
1550 if (parameters->is_big_endian())
1551 {
1552#ifdef HAVE_TARGET_32_BIG
1553 this->sized_write_section_symbol<32, true>(os, of, offset);
1554#else
1555 gold_unreachable();
1556#endif
1557 }
a3ad94ed 1558 else
9025d29d
ILT
1559 {
1560#ifdef HAVE_TARGET_32_LITTLE
1561 this->sized_write_section_symbol<32, false>(os, of, offset);
1562#else
1563 gold_unreachable();
1564#endif
1565 }
a3ad94ed 1566 }
9025d29d 1567 else if (parameters->get_size() == 64)
a3ad94ed 1568 {
9025d29d
ILT
1569 if (parameters->is_big_endian())
1570 {
1571#ifdef HAVE_TARGET_64_BIG
1572 this->sized_write_section_symbol<64, true>(os, of, offset);
1573#else
1574 gold_unreachable();
1575#endif
1576 }
a3ad94ed 1577 else
9025d29d
ILT
1578 {
1579#ifdef HAVE_TARGET_64_LITTLE
1580 this->sized_write_section_symbol<64, false>(os, of, offset);
1581#else
1582 gold_unreachable();
1583#endif
1584 }
a3ad94ed
ILT
1585 }
1586 else
1587 gold_unreachable();
1588}
1589
1590// Write out a section symbol, specialized for size and endianness.
1591
1592template<int size, bool big_endian>
1593void
1594Symbol_table::sized_write_section_symbol(const Output_section* os,
1595 Output_file* of,
1596 off_t offset) const
1597{
1598 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1599
1600 unsigned char* pov = of->get_output_view(offset, sym_size);
1601
1602 elfcpp::Sym_write<size, big_endian> osym(pov);
1603 osym.put_st_name(0);
1604 osym.put_st_value(os->address());
1605 osym.put_st_size(0);
1606 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1607 elfcpp::STT_SECTION));
1608 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1609 osym.put_st_shndx(os->out_shndx());
1610
1611 of->write_output_view(offset, sym_size, pov);
1612}
1613
f6ce93d6
ILT
1614// Warnings functions.
1615
1616// Add a new warning.
1617
1618void
1619Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1620 unsigned int shndx)
1621{
1622 name = symtab->canonicalize_name(name);
1623 this->warnings_[name].set(obj, shndx);
1624}
1625
1626// Look through the warnings and mark the symbols for which we should
1627// warn. This is called during Layout::finalize when we know the
1628// sources for all the symbols.
1629
1630void
1631Warnings::note_warnings(Symbol_table* symtab)
1632{
1633 for (Warning_table::iterator p = this->warnings_.begin();
1634 p != this->warnings_.end();
1635 ++p)
1636 {
1637 Symbol* sym = symtab->lookup(p->first, NULL);
1638 if (sym != NULL
1639 && sym->source() == Symbol::FROM_OBJECT
1640 && sym->object() == p->second.object)
1641 {
1642 sym->set_has_warning();
1643
1644 // Read the section contents to get the warning text. It
1645 // would be nicer if we only did this if we have to actually
1646 // issue a warning. Unfortunately, warnings are issued as
1647 // we relocate sections. That means that we can not lock
1648 // the object then, as we might try to issue the same
1649 // warning multiple times simultaneously.
645f8123
ILT
1650 {
1651 Task_locker_obj<Object> tl(*p->second.object);
1652 const unsigned char* c;
1653 off_t len;
9eb9fa57
ILT
1654 c = p->second.object->section_contents(p->second.shndx, &len,
1655 false);
645f8123
ILT
1656 p->second.set_text(reinterpret_cast<const char*>(c), len);
1657 }
f6ce93d6
ILT
1658 }
1659 }
1660}
1661
1662// Issue a warning. This is called when we see a relocation against a
1663// symbol for which has a warning.
1664
1665void
c06b7b0b 1666Warnings::issue_warning(const Symbol* sym, const std::string& location) const
f6ce93d6 1667{
a3ad94ed 1668 gold_assert(sym->has_warning());
f6ce93d6 1669 Warning_table::const_iterator p = this->warnings_.find(sym->name());
a3ad94ed 1670 gold_assert(p != this->warnings_.end());
f6ce93d6
ILT
1671 fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1672 p->second.text.c_str());
1673}
1674
14bfc3f5
ILT
1675// Instantiate the templates we need. We could use the configure
1676// script to restrict this to only the ones needed for implemented
1677// targets.
1678
193a53d9 1679#ifdef HAVE_TARGET_32_LITTLE
14bfc3f5
ILT
1680template
1681void
193a53d9
ILT
1682Symbol_table::add_from_relobj<32, false>(
1683 Sized_relobj<32, false>* relobj,
f6ce93d6 1684 const unsigned char* syms,
14bfc3f5
ILT
1685 size_t count,
1686 const char* sym_names,
1687 size_t sym_name_size,
1688 Symbol** sympointers);
193a53d9 1689#endif
14bfc3f5 1690
193a53d9 1691#ifdef HAVE_TARGET_32_BIG
14bfc3f5
ILT
1692template
1693void
193a53d9
ILT
1694Symbol_table::add_from_relobj<32, true>(
1695 Sized_relobj<32, true>* relobj,
f6ce93d6 1696 const unsigned char* syms,
14bfc3f5
ILT
1697 size_t count,
1698 const char* sym_names,
1699 size_t sym_name_size,
1700 Symbol** sympointers);
193a53d9 1701#endif
14bfc3f5 1702
193a53d9 1703#ifdef HAVE_TARGET_64_LITTLE
14bfc3f5
ILT
1704template
1705void
193a53d9
ILT
1706Symbol_table::add_from_relobj<64, false>(
1707 Sized_relobj<64, false>* relobj,
f6ce93d6 1708 const unsigned char* syms,
14bfc3f5
ILT
1709 size_t count,
1710 const char* sym_names,
1711 size_t sym_name_size,
1712 Symbol** sympointers);
193a53d9 1713#endif
14bfc3f5 1714
193a53d9 1715#ifdef HAVE_TARGET_64_BIG
14bfc3f5
ILT
1716template
1717void
193a53d9
ILT
1718Symbol_table::add_from_relobj<64, true>(
1719 Sized_relobj<64, true>* relobj,
f6ce93d6 1720 const unsigned char* syms,
14bfc3f5
ILT
1721 size_t count,
1722 const char* sym_names,
1723 size_t sym_name_size,
1724 Symbol** sympointers);
193a53d9 1725#endif
14bfc3f5 1726
193a53d9 1727#ifdef HAVE_TARGET_32_LITTLE
dbe717ef
ILT
1728template
1729void
193a53d9
ILT
1730Symbol_table::add_from_dynobj<32, false>(
1731 Sized_dynobj<32, false>* dynobj,
dbe717ef
ILT
1732 const unsigned char* syms,
1733 size_t count,
1734 const char* sym_names,
1735 size_t sym_name_size,
1736 const unsigned char* versym,
1737 size_t versym_size,
1738 const std::vector<const char*>* version_map);
193a53d9 1739#endif
dbe717ef 1740
193a53d9 1741#ifdef HAVE_TARGET_32_BIG
dbe717ef
ILT
1742template
1743void
193a53d9
ILT
1744Symbol_table::add_from_dynobj<32, true>(
1745 Sized_dynobj<32, true>* dynobj,
dbe717ef
ILT
1746 const unsigned char* syms,
1747 size_t count,
1748 const char* sym_names,
1749 size_t sym_name_size,
1750 const unsigned char* versym,
1751 size_t versym_size,
1752 const std::vector<const char*>* version_map);
193a53d9 1753#endif
dbe717ef 1754
193a53d9 1755#ifdef HAVE_TARGET_64_LITTLE
dbe717ef
ILT
1756template
1757void
193a53d9
ILT
1758Symbol_table::add_from_dynobj<64, false>(
1759 Sized_dynobj<64, false>* dynobj,
dbe717ef
ILT
1760 const unsigned char* syms,
1761 size_t count,
1762 const char* sym_names,
1763 size_t sym_name_size,
1764 const unsigned char* versym,
1765 size_t versym_size,
1766 const std::vector<const char*>* version_map);
193a53d9 1767#endif
dbe717ef 1768
193a53d9 1769#ifdef HAVE_TARGET_64_BIG
dbe717ef
ILT
1770template
1771void
193a53d9
ILT
1772Symbol_table::add_from_dynobj<64, true>(
1773 Sized_dynobj<64, true>* dynobj,
dbe717ef
ILT
1774 const unsigned char* syms,
1775 size_t count,
1776 const char* sym_names,
1777 size_t sym_name_size,
1778 const unsigned char* versym,
1779 size_t versym_size,
1780 const std::vector<const char*>* version_map);
193a53d9 1781#endif
dbe717ef 1782
14bfc3f5 1783} // End namespace gold.