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