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