Fix problem where version script causes predefined hidden symbol to be defined twice.
[deliverable/binutils-gdb.git] / gold / symtab.cc
1 // symtab.cc -- the gold symbol table
2
3 // Copyright (C) 2006-2016 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 "gc.h"
34 #include "object.h"
35 #include "dwarf_reader.h"
36 #include "dynobj.h"
37 #include "output.h"
38 #include "target.h"
39 #include "workqueue.h"
40 #include "symtab.h"
41 #include "script.h"
42 #include "plugin.h"
43 #include "incremental.h"
44
45 namespace gold
46 {
47
48 // Class Symbol.
49
50 // Initialize fields in Symbol. This initializes everything except u_
51 // and source_.
52
53 void
54 Symbol::init_fields(const char* name, const char* version,
55 elfcpp::STT type, elfcpp::STB binding,
56 elfcpp::STV visibility, unsigned char nonvis)
57 {
58 this->name_ = name;
59 this->version_ = version;
60 this->symtab_index_ = 0;
61 this->dynsym_index_ = 0;
62 this->got_offsets_.init();
63 this->plt_offset_ = -1U;
64 this->type_ = type;
65 this->binding_ = binding;
66 this->visibility_ = visibility;
67 this->nonvis_ = nonvis;
68 this->is_def_ = false;
69 this->is_forwarder_ = false;
70 this->has_alias_ = false;
71 this->needs_dynsym_entry_ = false;
72 this->in_reg_ = false;
73 this->in_dyn_ = false;
74 this->has_warning_ = false;
75 this->is_copied_from_dynobj_ = false;
76 this->is_forced_local_ = false;
77 this->is_ordinary_shndx_ = false;
78 this->in_real_elf_ = false;
79 this->is_defined_in_discarded_section_ = false;
80 this->undef_binding_set_ = false;
81 this->undef_binding_weak_ = false;
82 this->is_predefined_ = false;
83 this->is_protected_ = false;
84 }
85
86 // Return the demangled version of the symbol's name, but only
87 // if the --demangle flag was set.
88
89 static std::string
90 demangle(const char* name)
91 {
92 if (!parameters->options().do_demangle())
93 return name;
94
95 // cplus_demangle allocates memory for the result it returns,
96 // and returns NULL if the name is already demangled.
97 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
98 if (demangled_name == NULL)
99 return name;
100
101 std::string retval(demangled_name);
102 free(demangled_name);
103 return retval;
104 }
105
106 std::string
107 Symbol::demangled_name() const
108 {
109 return demangle(this->name());
110 }
111
112 // Initialize the fields in the base class Symbol for SYM in OBJECT.
113
114 template<int size, bool big_endian>
115 void
116 Symbol::init_base_object(const char* name, const char* version, Object* object,
117 const elfcpp::Sym<size, big_endian>& sym,
118 unsigned int st_shndx, bool is_ordinary)
119 {
120 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
121 sym.get_st_visibility(), sym.get_st_nonvis());
122 this->u_.from_object.object = object;
123 this->u_.from_object.shndx = st_shndx;
124 this->is_ordinary_shndx_ = is_ordinary;
125 this->source_ = FROM_OBJECT;
126 this->in_reg_ = !object->is_dynamic();
127 this->in_dyn_ = object->is_dynamic();
128 this->in_real_elf_ = object->pluginobj() == NULL;
129 }
130
131 // Initialize the fields in the base class Symbol for a symbol defined
132 // in an Output_data.
133
134 void
135 Symbol::init_base_output_data(const char* name, const char* version,
136 Output_data* od, elfcpp::STT type,
137 elfcpp::STB binding, elfcpp::STV visibility,
138 unsigned char nonvis, bool offset_is_from_end,
139 bool is_predefined)
140 {
141 this->init_fields(name, version, type, binding, visibility, nonvis);
142 this->u_.in_output_data.output_data = od;
143 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
144 this->source_ = IN_OUTPUT_DATA;
145 this->in_reg_ = true;
146 this->in_real_elf_ = true;
147 this->is_predefined_ = is_predefined;
148 }
149
150 // Initialize the fields in the base class Symbol for a symbol defined
151 // in an Output_segment.
152
153 void
154 Symbol::init_base_output_segment(const char* name, const char* version,
155 Output_segment* os, elfcpp::STT type,
156 elfcpp::STB binding, elfcpp::STV visibility,
157 unsigned char nonvis,
158 Segment_offset_base offset_base,
159 bool is_predefined)
160 {
161 this->init_fields(name, version, type, binding, visibility, nonvis);
162 this->u_.in_output_segment.output_segment = os;
163 this->u_.in_output_segment.offset_base = offset_base;
164 this->source_ = IN_OUTPUT_SEGMENT;
165 this->in_reg_ = true;
166 this->in_real_elf_ = true;
167 this->is_predefined_ = is_predefined;
168 }
169
170 // Initialize the fields in the base class Symbol for a symbol defined
171 // as a constant.
172
173 void
174 Symbol::init_base_constant(const char* name, const char* version,
175 elfcpp::STT type, elfcpp::STB binding,
176 elfcpp::STV visibility, unsigned char nonvis,
177 bool is_predefined)
178 {
179 this->init_fields(name, version, type, binding, visibility, nonvis);
180 this->source_ = IS_CONSTANT;
181 this->in_reg_ = true;
182 this->in_real_elf_ = true;
183 this->is_predefined_ = is_predefined;
184 }
185
186 // Initialize the fields in the base class Symbol for an undefined
187 // symbol.
188
189 void
190 Symbol::init_base_undefined(const char* name, const char* version,
191 elfcpp::STT type, elfcpp::STB binding,
192 elfcpp::STV visibility, unsigned char nonvis)
193 {
194 this->init_fields(name, version, type, binding, visibility, nonvis);
195 this->dynsym_index_ = -1U;
196 this->source_ = IS_UNDEFINED;
197 this->in_reg_ = true;
198 this->in_real_elf_ = true;
199 }
200
201 // Allocate a common symbol in the base.
202
203 void
204 Symbol::allocate_base_common(Output_data* od)
205 {
206 gold_assert(this->is_common());
207 this->source_ = IN_OUTPUT_DATA;
208 this->u_.in_output_data.output_data = od;
209 this->u_.in_output_data.offset_is_from_end = false;
210 }
211
212 // Initialize the fields in Sized_symbol for SYM in OBJECT.
213
214 template<int size>
215 template<bool big_endian>
216 void
217 Sized_symbol<size>::init_object(const char* name, const char* version,
218 Object* object,
219 const elfcpp::Sym<size, big_endian>& sym,
220 unsigned int st_shndx, bool is_ordinary)
221 {
222 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
223 this->value_ = sym.get_st_value();
224 this->symsize_ = sym.get_st_size();
225 }
226
227 // Initialize the fields in Sized_symbol for a symbol defined in an
228 // Output_data.
229
230 template<int size>
231 void
232 Sized_symbol<size>::init_output_data(const char* name, const char* version,
233 Output_data* od, Value_type value,
234 Size_type symsize, elfcpp::STT type,
235 elfcpp::STB binding,
236 elfcpp::STV visibility,
237 unsigned char nonvis,
238 bool offset_is_from_end,
239 bool is_predefined)
240 {
241 this->init_base_output_data(name, version, od, type, binding, visibility,
242 nonvis, offset_is_from_end, is_predefined);
243 this->value_ = value;
244 this->symsize_ = symsize;
245 }
246
247 // Initialize the fields in Sized_symbol for a symbol defined in an
248 // Output_segment.
249
250 template<int size>
251 void
252 Sized_symbol<size>::init_output_segment(const char* name, const char* version,
253 Output_segment* os, Value_type value,
254 Size_type symsize, elfcpp::STT type,
255 elfcpp::STB binding,
256 elfcpp::STV visibility,
257 unsigned char nonvis,
258 Segment_offset_base offset_base,
259 bool is_predefined)
260 {
261 this->init_base_output_segment(name, version, os, type, binding, visibility,
262 nonvis, offset_base, is_predefined);
263 this->value_ = value;
264 this->symsize_ = symsize;
265 }
266
267 // Initialize the fields in Sized_symbol for a symbol defined as a
268 // constant.
269
270 template<int size>
271 void
272 Sized_symbol<size>::init_constant(const char* name, const char* version,
273 Value_type value, Size_type symsize,
274 elfcpp::STT type, elfcpp::STB binding,
275 elfcpp::STV visibility, unsigned char nonvis,
276 bool is_predefined)
277 {
278 this->init_base_constant(name, version, type, binding, visibility, nonvis,
279 is_predefined);
280 this->value_ = value;
281 this->symsize_ = symsize;
282 }
283
284 // Initialize the fields in Sized_symbol for an undefined symbol.
285
286 template<int size>
287 void
288 Sized_symbol<size>::init_undefined(const char* name, const char* version,
289 Value_type value, elfcpp::STT type,
290 elfcpp::STB binding, elfcpp::STV visibility,
291 unsigned char nonvis)
292 {
293 this->init_base_undefined(name, version, type, binding, visibility, nonvis);
294 this->value_ = value;
295 this->symsize_ = 0;
296 }
297
298 // Return an allocated string holding the symbol's name as
299 // name@version. This is used for relocatable links.
300
301 std::string
302 Symbol::versioned_name() const
303 {
304 gold_assert(this->version_ != NULL);
305 std::string ret = this->name_;
306 ret.push_back('@');
307 if (this->is_def_)
308 ret.push_back('@');
309 ret += this->version_;
310 return ret;
311 }
312
313 // Return true if SHNDX represents a common symbol.
314
315 bool
316 Symbol::is_common_shndx(unsigned int shndx)
317 {
318 return (shndx == elfcpp::SHN_COMMON
319 || shndx == parameters->target().small_common_shndx()
320 || shndx == parameters->target().large_common_shndx());
321 }
322
323 // Allocate a common symbol.
324
325 template<int size>
326 void
327 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
328 {
329 this->allocate_base_common(od);
330 this->value_ = value;
331 }
332
333 // The ""'s around str ensure str is a string literal, so sizeof works.
334 #define strprefix(var, str) (strncmp(var, str, sizeof("" str "") - 1) == 0)
335
336 // Return true if this symbol should be added to the dynamic symbol
337 // table.
338
339 bool
340 Symbol::should_add_dynsym_entry(Symbol_table* symtab) const
341 {
342 // If the symbol is only present on plugin files, the plugin decided we
343 // don't need it.
344 if (!this->in_real_elf())
345 return false;
346
347 // If the symbol is used by a dynamic relocation, we need to add it.
348 if (this->needs_dynsym_entry())
349 return true;
350
351 // If this symbol's section is not added, the symbol need not be added.
352 // The section may have been GCed. Note that export_dynamic is being
353 // overridden here. This should not be done for shared objects.
354 if (parameters->options().gc_sections()
355 && !parameters->options().shared()
356 && this->source() == Symbol::FROM_OBJECT
357 && !this->object()->is_dynamic())
358 {
359 Relobj* relobj = static_cast<Relobj*>(this->object());
360 bool is_ordinary;
361 unsigned int shndx = this->shndx(&is_ordinary);
362 if (is_ordinary && shndx != elfcpp::SHN_UNDEF
363 && !relobj->is_section_included(shndx)
364 && !symtab->is_section_folded(relobj, shndx))
365 return false;
366 }
367
368 // If the symbol was forced dynamic in a --dynamic-list file
369 // or an --export-dynamic-symbol option, add it.
370 if (!this->is_from_dynobj()
371 && (parameters->options().in_dynamic_list(this->name())
372 || parameters->options().is_export_dynamic_symbol(this->name())))
373 {
374 if (!this->is_forced_local())
375 return true;
376 gold_warning(_("Cannot export local symbol '%s'"),
377 this->demangled_name().c_str());
378 return false;
379 }
380
381 // If the symbol was forced local in a version script, do not add it.
382 if (this->is_forced_local())
383 return false;
384
385 // If dynamic-list-data was specified, add any STT_OBJECT.
386 if (parameters->options().dynamic_list_data()
387 && !this->is_from_dynobj()
388 && this->type() == elfcpp::STT_OBJECT)
389 return true;
390
391 // If --dynamic-list-cpp-new was specified, add any new/delete symbol.
392 // If --dynamic-list-cpp-typeinfo was specified, add any typeinfo symbols.
393 if ((parameters->options().dynamic_list_cpp_new()
394 || parameters->options().dynamic_list_cpp_typeinfo())
395 && !this->is_from_dynobj())
396 {
397 // TODO(csilvers): We could probably figure out if we're an operator
398 // new/delete or typeinfo without the need to demangle.
399 char* demangled_name = cplus_demangle(this->name(),
400 DMGL_ANSI | DMGL_PARAMS);
401 if (demangled_name == NULL)
402 {
403 // Not a C++ symbol, so it can't satisfy these flags
404 }
405 else if (parameters->options().dynamic_list_cpp_new()
406 && (strprefix(demangled_name, "operator new")
407 || strprefix(demangled_name, "operator delete")))
408 {
409 free(demangled_name);
410 return true;
411 }
412 else if (parameters->options().dynamic_list_cpp_typeinfo()
413 && (strprefix(demangled_name, "typeinfo name for")
414 || strprefix(demangled_name, "typeinfo for")))
415 {
416 free(demangled_name);
417 return true;
418 }
419 else
420 free(demangled_name);
421 }
422
423 // If exporting all symbols or building a shared library,
424 // or the symbol should be globally unique (GNU_UNIQUE),
425 // and the symbol is defined in a regular object and is
426 // externally visible, we need to add it.
427 if ((parameters->options().export_dynamic()
428 || parameters->options().shared()
429 || (parameters->options().gnu_unique()
430 && this->binding() == elfcpp::STB_GNU_UNIQUE))
431 && !this->is_from_dynobj()
432 && !this->is_undefined()
433 && this->is_externally_visible())
434 return true;
435
436 return false;
437 }
438
439 // Return true if the final value of this symbol is known at link
440 // time.
441
442 bool
443 Symbol::final_value_is_known() const
444 {
445 // If we are not generating an executable, then no final values are
446 // known, since they will change at runtime, with the exception of
447 // TLS symbols in a position-independent executable.
448 if ((parameters->options().output_is_position_independent()
449 || parameters->options().relocatable())
450 && !(this->type() == elfcpp::STT_TLS
451 && parameters->options().pie()))
452 return false;
453
454 // If the symbol is not from an object file, and is not undefined,
455 // then it is defined, and known.
456 if (this->source_ != FROM_OBJECT)
457 {
458 if (this->source_ != IS_UNDEFINED)
459 return true;
460 }
461 else
462 {
463 // If the symbol is from a dynamic object, then the final value
464 // is not known.
465 if (this->object()->is_dynamic())
466 return false;
467
468 // If the symbol is not undefined (it is defined or common),
469 // then the final value is known.
470 if (!this->is_undefined())
471 return true;
472 }
473
474 // If the symbol is undefined, then whether the final value is known
475 // depends on whether we are doing a static link. If we are doing a
476 // dynamic link, then the final value could be filled in at runtime.
477 // This could reasonably be the case for a weak undefined symbol.
478 return parameters->doing_static_link();
479 }
480
481 // Return the output section where this symbol is defined.
482
483 Output_section*
484 Symbol::output_section() const
485 {
486 switch (this->source_)
487 {
488 case FROM_OBJECT:
489 {
490 unsigned int shndx = this->u_.from_object.shndx;
491 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
492 {
493 gold_assert(!this->u_.from_object.object->is_dynamic());
494 gold_assert(this->u_.from_object.object->pluginobj() == NULL);
495 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
496 return relobj->output_section(shndx);
497 }
498 return NULL;
499 }
500
501 case IN_OUTPUT_DATA:
502 return this->u_.in_output_data.output_data->output_section();
503
504 case IN_OUTPUT_SEGMENT:
505 case IS_CONSTANT:
506 case IS_UNDEFINED:
507 return NULL;
508
509 default:
510 gold_unreachable();
511 }
512 }
513
514 // Set the symbol's output section. This is used for symbols defined
515 // in scripts. This should only be called after the symbol table has
516 // been finalized.
517
518 void
519 Symbol::set_output_section(Output_section* os)
520 {
521 switch (this->source_)
522 {
523 case FROM_OBJECT:
524 case IN_OUTPUT_DATA:
525 gold_assert(this->output_section() == os);
526 break;
527 case IS_CONSTANT:
528 this->source_ = IN_OUTPUT_DATA;
529 this->u_.in_output_data.output_data = os;
530 this->u_.in_output_data.offset_is_from_end = false;
531 break;
532 case IN_OUTPUT_SEGMENT:
533 case IS_UNDEFINED:
534 default:
535 gold_unreachable();
536 }
537 }
538
539 // Set the symbol's output segment. This is used for pre-defined
540 // symbols whose segments aren't known until after layout is done
541 // (e.g., __ehdr_start).
542
543 void
544 Symbol::set_output_segment(Output_segment* os, Segment_offset_base base)
545 {
546 gold_assert(this->is_predefined_);
547 this->source_ = IN_OUTPUT_SEGMENT;
548 this->u_.in_output_segment.output_segment = os;
549 this->u_.in_output_segment.offset_base = base;
550 }
551
552 // Set the symbol to undefined. This is used for pre-defined
553 // symbols whose segments aren't known until after layout is done
554 // (e.g., __ehdr_start).
555
556 void
557 Symbol::set_undefined()
558 {
559 this->source_ = IS_UNDEFINED;
560 this->is_predefined_ = false;
561 }
562
563 // Class Symbol_table.
564
565 Symbol_table::Symbol_table(unsigned int count,
566 const Version_script_info& version_script)
567 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
568 forwarders_(), commons_(), tls_commons_(), small_commons_(),
569 large_commons_(), forced_locals_(), warnings_(),
570 version_script_(version_script), gc_(NULL), icf_(NULL),
571 target_symbols_()
572 {
573 namepool_.reserve(count);
574 }
575
576 Symbol_table::~Symbol_table()
577 {
578 }
579
580 // The symbol table key equality function. This is called with
581 // Stringpool keys.
582
583 inline bool
584 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
585 const Symbol_table_key& k2) const
586 {
587 return k1.first == k2.first && k1.second == k2.second;
588 }
589
590 bool
591 Symbol_table::is_section_folded(Relobj* obj, unsigned int shndx) const
592 {
593 return (parameters->options().icf_enabled()
594 && this->icf_->is_section_folded(obj, shndx));
595 }
596
597 // For symbols that have been listed with a -u or --export-dynamic-symbol
598 // option, add them to the work list to avoid gc'ing them.
599
600 void
601 Symbol_table::gc_mark_undef_symbols(Layout* layout)
602 {
603 for (options::String_set::const_iterator p =
604 parameters->options().undefined_begin();
605 p != parameters->options().undefined_end();
606 ++p)
607 {
608 const char* name = p->c_str();
609 Symbol* sym = this->lookup(name);
610 gold_assert(sym != NULL);
611 if (sym->source() == Symbol::FROM_OBJECT
612 && !sym->object()->is_dynamic())
613 {
614 this->gc_mark_symbol(sym);
615 }
616 }
617
618 for (options::String_set::const_iterator p =
619 parameters->options().export_dynamic_symbol_begin();
620 p != parameters->options().export_dynamic_symbol_end();
621 ++p)
622 {
623 const char* name = p->c_str();
624 Symbol* sym = this->lookup(name);
625 // It's not an error if a symbol named by --export-dynamic-symbol
626 // is undefined.
627 if (sym != NULL
628 && sym->source() == Symbol::FROM_OBJECT
629 && !sym->object()->is_dynamic())
630 {
631 this->gc_mark_symbol(sym);
632 }
633 }
634
635 for (Script_options::referenced_const_iterator p =
636 layout->script_options()->referenced_begin();
637 p != layout->script_options()->referenced_end();
638 ++p)
639 {
640 Symbol* sym = this->lookup(p->c_str());
641 gold_assert(sym != NULL);
642 if (sym->source() == Symbol::FROM_OBJECT
643 && !sym->object()->is_dynamic())
644 {
645 this->gc_mark_symbol(sym);
646 }
647 }
648 }
649
650 void
651 Symbol_table::gc_mark_symbol(Symbol* sym)
652 {
653 // Add the object and section to the work list.
654 bool is_ordinary;
655 unsigned int shndx = sym->shndx(&is_ordinary);
656 if (is_ordinary && shndx != elfcpp::SHN_UNDEF && !sym->object()->is_dynamic())
657 {
658 gold_assert(this->gc_!= NULL);
659 Relobj* relobj = static_cast<Relobj*>(sym->object());
660 this->gc_->worklist().push_back(Section_id(relobj, shndx));
661 }
662 parameters->target().gc_mark_symbol(this, sym);
663 }
664
665 // When doing garbage collection, keep symbols that have been seen in
666 // dynamic objects.
667 inline void
668 Symbol_table::gc_mark_dyn_syms(Symbol* sym)
669 {
670 if (sym->in_dyn() && sym->source() == Symbol::FROM_OBJECT
671 && !sym->object()->is_dynamic())
672 this->gc_mark_symbol(sym);
673 }
674
675 // Make TO a symbol which forwards to FROM.
676
677 void
678 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
679 {
680 gold_assert(from != to);
681 gold_assert(!from->is_forwarder() && !to->is_forwarder());
682 this->forwarders_[from] = to;
683 from->set_forwarder();
684 }
685
686 // Resolve the forwards from FROM, returning the real symbol.
687
688 Symbol*
689 Symbol_table::resolve_forwards(const Symbol* from) const
690 {
691 gold_assert(from->is_forwarder());
692 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
693 this->forwarders_.find(from);
694 gold_assert(p != this->forwarders_.end());
695 return p->second;
696 }
697
698 // Look up a symbol by name.
699
700 Symbol*
701 Symbol_table::lookup(const char* name, const char* version) const
702 {
703 Stringpool::Key name_key;
704 name = this->namepool_.find(name, &name_key);
705 if (name == NULL)
706 return NULL;
707
708 Stringpool::Key version_key = 0;
709 if (version != NULL)
710 {
711 version = this->namepool_.find(version, &version_key);
712 if (version == NULL)
713 return NULL;
714 }
715
716 Symbol_table_key key(name_key, version_key);
717 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
718 if (p == this->table_.end())
719 return NULL;
720 return p->second;
721 }
722
723 // Resolve a Symbol with another Symbol. This is only used in the
724 // unusual case where there are references to both an unversioned
725 // symbol and a symbol with a version, and we then discover that that
726 // version is the default version. Because this is unusual, we do
727 // this the slow way, by converting back to an ELF symbol.
728
729 template<int size, bool big_endian>
730 void
731 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from)
732 {
733 unsigned char buf[elfcpp::Elf_sizes<size>::sym_size];
734 elfcpp::Sym_write<size, big_endian> esym(buf);
735 // We don't bother to set the st_name or the st_shndx field.
736 esym.put_st_value(from->value());
737 esym.put_st_size(from->symsize());
738 esym.put_st_info(from->binding(), from->type());
739 esym.put_st_other(from->visibility(), from->nonvis());
740 bool is_ordinary;
741 unsigned int shndx = from->shndx(&is_ordinary);
742 this->resolve(to, esym.sym(), shndx, is_ordinary, shndx, from->object(),
743 from->version(), true);
744 if (from->in_reg())
745 to->set_in_reg();
746 if (from->in_dyn())
747 to->set_in_dyn();
748 if (parameters->options().gc_sections())
749 this->gc_mark_dyn_syms(to);
750 }
751
752 // Record that a symbol is forced to be local by a version script or
753 // by visibility.
754
755 void
756 Symbol_table::force_local(Symbol* sym)
757 {
758 if (!sym->is_defined() && !sym->is_common())
759 return;
760 if (sym->is_forced_local())
761 {
762 // We already got this one.
763 return;
764 }
765 sym->set_is_forced_local();
766 this->forced_locals_.push_back(sym);
767 }
768
769 // Adjust NAME for wrapping, and update *NAME_KEY if necessary. This
770 // is only called for undefined symbols, when at least one --wrap
771 // option was used.
772
773 const char*
774 Symbol_table::wrap_symbol(const char* name, Stringpool::Key* name_key)
775 {
776 // For some targets, we need to ignore a specific character when
777 // wrapping, and add it back later.
778 char prefix = '\0';
779 if (name[0] == parameters->target().wrap_char())
780 {
781 prefix = name[0];
782 ++name;
783 }
784
785 if (parameters->options().is_wrap(name))
786 {
787 // Turn NAME into __wrap_NAME.
788 std::string s;
789 if (prefix != '\0')
790 s += prefix;
791 s += "__wrap_";
792 s += name;
793
794 // This will give us both the old and new name in NAMEPOOL_, but
795 // that is OK. Only the versions we need will wind up in the
796 // real string table in the output file.
797 return this->namepool_.add(s.c_str(), true, name_key);
798 }
799
800 const char* const real_prefix = "__real_";
801 const size_t real_prefix_length = strlen(real_prefix);
802 if (strncmp(name, real_prefix, real_prefix_length) == 0
803 && parameters->options().is_wrap(name + real_prefix_length))
804 {
805 // Turn __real_NAME into NAME.
806 std::string s;
807 if (prefix != '\0')
808 s += prefix;
809 s += name + real_prefix_length;
810 return this->namepool_.add(s.c_str(), true, name_key);
811 }
812
813 return name;
814 }
815
816 // This is called when we see a symbol NAME/VERSION, and the symbol
817 // already exists in the symbol table, and VERSION is marked as being
818 // the default version. SYM is the NAME/VERSION symbol we just added.
819 // DEFAULT_IS_NEW is true if this is the first time we have seen the
820 // symbol NAME/NULL. PDEF points to the entry for NAME/NULL.
821
822 template<int size, bool big_endian>
823 void
824 Symbol_table::define_default_version(Sized_symbol<size>* sym,
825 bool default_is_new,
826 Symbol_table_type::iterator pdef)
827 {
828 if (default_is_new)
829 {
830 // This is the first time we have seen NAME/NULL. Make
831 // NAME/NULL point to NAME/VERSION, and mark SYM as the default
832 // version.
833 pdef->second = sym;
834 sym->set_is_default();
835 }
836 else if (pdef->second == sym)
837 {
838 // NAME/NULL already points to NAME/VERSION. Don't mark the
839 // symbol as the default if it is not already the default.
840 }
841 else
842 {
843 // This is the unfortunate case where we already have entries
844 // for both NAME/VERSION and NAME/NULL. We now see a symbol
845 // NAME/VERSION where VERSION is the default version. We have
846 // already resolved this new symbol with the existing
847 // NAME/VERSION symbol.
848
849 // It's possible that NAME/NULL and NAME/VERSION are both
850 // defined in regular objects. This can only happen if one
851 // object file defines foo and another defines foo@@ver. This
852 // is somewhat obscure, but we call it a multiple definition
853 // error.
854
855 // It's possible that NAME/NULL actually has a version, in which
856 // case it won't be the same as VERSION. This happens with
857 // ver_test_7.so in the testsuite for the symbol t2_2. We see
858 // t2_2@@VER2, so we define both t2_2/VER2 and t2_2/NULL. We
859 // then see an unadorned t2_2 in an object file and give it
860 // version VER1 from the version script. This looks like a
861 // default definition for VER1, so it looks like we should merge
862 // t2_2/NULL with t2_2/VER1. That doesn't make sense, but it's
863 // not obvious that this is an error, either. So we just punt.
864
865 // If one of the symbols has non-default visibility, and the
866 // other is defined in a shared object, then they are different
867 // symbols.
868
869 // If the two symbols are from different shared objects,
870 // they are different symbols.
871
872 // Otherwise, we just resolve the symbols as though they were
873 // the same.
874
875 if (pdef->second->version() != NULL)
876 gold_assert(pdef->second->version() != sym->version());
877 else if (sym->visibility() != elfcpp::STV_DEFAULT
878 && pdef->second->is_from_dynobj())
879 ;
880 else if (pdef->second->visibility() != elfcpp::STV_DEFAULT
881 && sym->is_from_dynobj())
882 ;
883 else if (pdef->second->is_from_dynobj()
884 && sym->is_from_dynobj()
885 && pdef->second->is_defined()
886 && pdef->second->object() != sym->object())
887 ;
888 else
889 {
890 const Sized_symbol<size>* symdef;
891 symdef = this->get_sized_symbol<size>(pdef->second);
892 Symbol_table::resolve<size, big_endian>(sym, symdef);
893 this->make_forwarder(pdef->second, sym);
894 pdef->second = sym;
895 sym->set_is_default();
896 }
897 }
898 }
899
900 // Add one symbol from OBJECT to the symbol table. NAME is symbol
901 // name and VERSION is the version; both are canonicalized. DEF is
902 // whether this is the default version. ST_SHNDX is the symbol's
903 // section index; IS_ORDINARY is whether this is a normal section
904 // rather than a special code.
905
906 // If IS_DEFAULT_VERSION is true, then this is the definition of a
907 // default version of a symbol. That means that any lookup of
908 // NAME/NULL and any lookup of NAME/VERSION should always return the
909 // same symbol. This is obvious for references, but in particular we
910 // want to do this for definitions: overriding NAME/NULL should also
911 // override NAME/VERSION. If we don't do that, it would be very hard
912 // to override functions in a shared library which uses versioning.
913
914 // We implement this by simply making both entries in the hash table
915 // point to the same Symbol structure. That is easy enough if this is
916 // the first time we see NAME/NULL or NAME/VERSION, but it is possible
917 // that we have seen both already, in which case they will both have
918 // independent entries in the symbol table. We can't simply change
919 // the symbol table entry, because we have pointers to the entries
920 // attached to the object files. So we mark the entry attached to the
921 // object file as a forwarder, and record it in the forwarders_ map.
922 // Note that entries in the hash table will never be marked as
923 // forwarders.
924 //
925 // ORIG_ST_SHNDX and ST_SHNDX are almost always the same.
926 // ORIG_ST_SHNDX is the section index in the input file, or SHN_UNDEF
927 // for a special section code. ST_SHNDX may be modified if the symbol
928 // is defined in a section being discarded.
929
930 template<int size, bool big_endian>
931 Sized_symbol<size>*
932 Symbol_table::add_from_object(Object* object,
933 const char* name,
934 Stringpool::Key name_key,
935 const char* version,
936 Stringpool::Key version_key,
937 bool is_default_version,
938 const elfcpp::Sym<size, big_endian>& sym,
939 unsigned int st_shndx,
940 bool is_ordinary,
941 unsigned int orig_st_shndx)
942 {
943 // Print a message if this symbol is being traced.
944 if (parameters->options().is_trace_symbol(name))
945 {
946 if (orig_st_shndx == elfcpp::SHN_UNDEF)
947 gold_info(_("%s: reference to %s"), object->name().c_str(), name);
948 else
949 gold_info(_("%s: definition of %s"), object->name().c_str(), name);
950 }
951
952 // For an undefined symbol, we may need to adjust the name using
953 // --wrap.
954 if (orig_st_shndx == elfcpp::SHN_UNDEF
955 && parameters->options().any_wrap())
956 {
957 const char* wrap_name = this->wrap_symbol(name, &name_key);
958 if (wrap_name != name)
959 {
960 // If we see a reference to malloc with version GLIBC_2.0,
961 // and we turn it into a reference to __wrap_malloc, then we
962 // discard the version number. Otherwise the user would be
963 // required to specify the correct version for
964 // __wrap_malloc.
965 version = NULL;
966 version_key = 0;
967 name = wrap_name;
968 }
969 }
970
971 Symbol* const snull = NULL;
972 std::pair<typename Symbol_table_type::iterator, bool> ins =
973 this->table_.insert(std::make_pair(std::make_pair(name_key, version_key),
974 snull));
975
976 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
977 std::make_pair(this->table_.end(), false);
978 if (is_default_version)
979 {
980 const Stringpool::Key vnull_key = 0;
981 insdefault = this->table_.insert(std::make_pair(std::make_pair(name_key,
982 vnull_key),
983 snull));
984 }
985
986 // ins.first: an iterator, which is a pointer to a pair.
987 // ins.first->first: the key (a pair of name and version).
988 // ins.first->second: the value (Symbol*).
989 // ins.second: true if new entry was inserted, false if not.
990
991 Sized_symbol<size>* ret;
992 bool was_undefined;
993 bool was_common;
994 if (!ins.second)
995 {
996 // We already have an entry for NAME/VERSION.
997 ret = this->get_sized_symbol<size>(ins.first->second);
998 gold_assert(ret != NULL);
999
1000 was_undefined = ret->is_undefined();
1001 // Commons from plugins are just placeholders.
1002 was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
1003
1004 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
1005 version, is_default_version);
1006 if (parameters->options().gc_sections())
1007 this->gc_mark_dyn_syms(ret);
1008
1009 if (is_default_version)
1010 this->define_default_version<size, big_endian>(ret, insdefault.second,
1011 insdefault.first);
1012 else
1013 {
1014 bool dummy;
1015 if (version != NULL
1016 && ret->source() == Symbol::FROM_OBJECT
1017 && ret->object() == object
1018 && is_ordinary
1019 && ret->shndx(&dummy) == st_shndx
1020 && ret->is_default())
1021 {
1022 // We have seen NAME/VERSION already, and marked it as the
1023 // default version, but now we see a definition for
1024 // NAME/VERSION that is not the default version. This can
1025 // happen when the assembler generates two symbols for
1026 // a symbol as a result of a ".symver foo,foo@VER"
1027 // directive. We see the first unversioned symbol and
1028 // we may mark it as the default version (from a
1029 // version script); then we see the second versioned
1030 // symbol and we need to override the first.
1031 // In any other case, the two symbols should have generated
1032 // a multiple definition error.
1033 // (See PR gold/18703.)
1034 ret->set_is_not_default();
1035 const Stringpool::Key vnull_key = 0;
1036 this->table_.erase(std::make_pair(name_key, vnull_key));
1037 }
1038 }
1039 }
1040 else
1041 {
1042 // This is the first time we have seen NAME/VERSION.
1043 gold_assert(ins.first->second == NULL);
1044
1045 if (is_default_version && !insdefault.second)
1046 {
1047 // We already have an entry for NAME/NULL. If we override
1048 // it, then change it to NAME/VERSION.
1049 ret = this->get_sized_symbol<size>(insdefault.first->second);
1050
1051 was_undefined = ret->is_undefined();
1052 // Commons from plugins are just placeholders.
1053 was_common = ret->is_common() && ret->object()->pluginobj() == NULL;
1054
1055 this->resolve(ret, sym, st_shndx, is_ordinary, orig_st_shndx, object,
1056 version, is_default_version);
1057 if (parameters->options().gc_sections())
1058 this->gc_mark_dyn_syms(ret);
1059 ins.first->second = ret;
1060 }
1061 else
1062 {
1063 was_undefined = false;
1064 was_common = false;
1065
1066 Sized_target<size, big_endian>* target =
1067 parameters->sized_target<size, big_endian>();
1068 if (!target->has_make_symbol())
1069 ret = new Sized_symbol<size>();
1070 else
1071 {
1072 ret = target->make_symbol(name, sym.get_st_type(), object,
1073 st_shndx, sym.get_st_value());
1074 if (ret == NULL)
1075 {
1076 // This means that we don't want a symbol table
1077 // entry after all.
1078 if (!is_default_version)
1079 this->table_.erase(ins.first);
1080 else
1081 {
1082 this->table_.erase(insdefault.first);
1083 // Inserting INSDEFAULT invalidated INS.
1084 this->table_.erase(std::make_pair(name_key,
1085 version_key));
1086 }
1087 return NULL;
1088 }
1089 }
1090
1091 ret->init_object(name, version, object, sym, st_shndx, is_ordinary);
1092
1093 ins.first->second = ret;
1094 if (is_default_version)
1095 {
1096 // This is the first time we have seen NAME/NULL. Point
1097 // it at the new entry for NAME/VERSION.
1098 gold_assert(insdefault.second);
1099 insdefault.first->second = ret;
1100 }
1101 }
1102
1103 if (is_default_version)
1104 ret->set_is_default();
1105 }
1106
1107 // Record every time we see a new undefined symbol, to speed up
1108 // archive groups.
1109 if (!was_undefined && ret->is_undefined())
1110 {
1111 ++this->saw_undefined_;
1112 if (parameters->options().has_plugins())
1113 parameters->options().plugins()->new_undefined_symbol(ret);
1114 }
1115
1116 // Keep track of common symbols, to speed up common symbol
1117 // allocation. Don't record commons from plugin objects;
1118 // we need to wait until we see the real symbol in the
1119 // replacement file.
1120 if (!was_common && ret->is_common() && ret->object()->pluginobj() == NULL)
1121 {
1122 if (ret->type() == elfcpp::STT_TLS)
1123 this->tls_commons_.push_back(ret);
1124 else if (!is_ordinary
1125 && st_shndx == parameters->target().small_common_shndx())
1126 this->small_commons_.push_back(ret);
1127 else if (!is_ordinary
1128 && st_shndx == parameters->target().large_common_shndx())
1129 this->large_commons_.push_back(ret);
1130 else
1131 this->commons_.push_back(ret);
1132 }
1133
1134 // If we're not doing a relocatable link, then any symbol with
1135 // hidden or internal visibility is local.
1136 if ((ret->visibility() == elfcpp::STV_HIDDEN
1137 || ret->visibility() == elfcpp::STV_INTERNAL)
1138 && (ret->binding() == elfcpp::STB_GLOBAL
1139 || ret->binding() == elfcpp::STB_GNU_UNIQUE
1140 || ret->binding() == elfcpp::STB_WEAK)
1141 && !parameters->options().relocatable())
1142 this->force_local(ret);
1143
1144 return ret;
1145 }
1146
1147 // Add all the symbols in a relocatable object to the hash table.
1148
1149 template<int size, bool big_endian>
1150 void
1151 Symbol_table::add_from_relobj(
1152 Sized_relobj_file<size, big_endian>* relobj,
1153 const unsigned char* syms,
1154 size_t count,
1155 size_t symndx_offset,
1156 const char* sym_names,
1157 size_t sym_name_size,
1158 typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
1159 size_t* defined)
1160 {
1161 *defined = 0;
1162
1163 gold_assert(size == parameters->target().get_size());
1164
1165 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1166
1167 const bool just_symbols = relobj->just_symbols();
1168
1169 const unsigned char* p = syms;
1170 for (size_t i = 0; i < count; ++i, p += sym_size)
1171 {
1172 (*sympointers)[i] = NULL;
1173
1174 elfcpp::Sym<size, big_endian> sym(p);
1175
1176 unsigned int st_name = sym.get_st_name();
1177 if (st_name >= sym_name_size)
1178 {
1179 relobj->error(_("bad global symbol name offset %u at %zu"),
1180 st_name, i);
1181 continue;
1182 }
1183
1184 const char* name = sym_names + st_name;
1185
1186 if (!parameters->options().relocatable()
1187 && strcmp (name, "__gnu_lto_slim") == 0)
1188 gold_info(_("%s: plugin needed to handle lto object"),
1189 relobj->name().c_str());
1190
1191 bool is_ordinary;
1192 unsigned int st_shndx = relobj->adjust_sym_shndx(i + symndx_offset,
1193 sym.get_st_shndx(),
1194 &is_ordinary);
1195 unsigned int orig_st_shndx = st_shndx;
1196 if (!is_ordinary)
1197 orig_st_shndx = elfcpp::SHN_UNDEF;
1198
1199 if (st_shndx != elfcpp::SHN_UNDEF)
1200 ++*defined;
1201
1202 // A symbol defined in a section which we are not including must
1203 // be treated as an undefined symbol.
1204 bool is_defined_in_discarded_section = false;
1205 if (st_shndx != elfcpp::SHN_UNDEF
1206 && is_ordinary
1207 && !relobj->is_section_included(st_shndx)
1208 && !this->is_section_folded(relobj, st_shndx))
1209 {
1210 st_shndx = elfcpp::SHN_UNDEF;
1211 is_defined_in_discarded_section = true;
1212 }
1213
1214 // In an object file, an '@' in the name separates the symbol
1215 // name from the version name. If there are two '@' characters,
1216 // this is the default version.
1217 const char* ver = strchr(name, '@');
1218 Stringpool::Key ver_key = 0;
1219 int namelen = 0;
1220 // IS_DEFAULT_VERSION: is the version default?
1221 // IS_FORCED_LOCAL: is the symbol forced local?
1222 bool is_default_version = false;
1223 bool is_forced_local = false;
1224
1225 // FIXME: For incremental links, we don't store version information,
1226 // so we need to ignore version symbols for now.
1227 if (parameters->incremental_update() && ver != NULL)
1228 {
1229 namelen = ver - name;
1230 ver = NULL;
1231 }
1232
1233 if (ver != NULL)
1234 {
1235 // The symbol name is of the form foo@VERSION or foo@@VERSION
1236 namelen = ver - name;
1237 ++ver;
1238 if (*ver == '@')
1239 {
1240 is_default_version = true;
1241 ++ver;
1242 }
1243 ver = this->namepool_.add(ver, true, &ver_key);
1244 }
1245 // We don't want to assign a version to an undefined symbol,
1246 // even if it is listed in the version script. FIXME: What
1247 // about a common symbol?
1248 else
1249 {
1250 namelen = strlen(name);
1251 if (!this->version_script_.empty()
1252 && st_shndx != elfcpp::SHN_UNDEF)
1253 {
1254 // The symbol name did not have a version, but the
1255 // version script may assign a version anyway.
1256 std::string version;
1257 bool is_global;
1258 if (this->version_script_.get_symbol_version(name, &version,
1259 &is_global))
1260 {
1261 if (!is_global)
1262 is_forced_local = true;
1263 else if (!version.empty())
1264 {
1265 ver = this->namepool_.add_with_length(version.c_str(),
1266 version.length(),
1267 true,
1268 &ver_key);
1269 is_default_version = true;
1270 }
1271 }
1272 }
1273 }
1274
1275 elfcpp::Sym<size, big_endian>* psym = &sym;
1276 unsigned char symbuf[sym_size];
1277 elfcpp::Sym<size, big_endian> sym2(symbuf);
1278 if (just_symbols)
1279 {
1280 memcpy(symbuf, p, sym_size);
1281 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1282 if (orig_st_shndx != elfcpp::SHN_UNDEF
1283 && is_ordinary
1284 && relobj->e_type() == elfcpp::ET_REL)
1285 {
1286 // Symbol values in relocatable object files are section
1287 // relative. This is normally what we want, but since here
1288 // we are converting the symbol to absolute we need to add
1289 // the section address. The section address in an object
1290 // file is normally zero, but people can use a linker
1291 // script to change it.
1292 sw.put_st_value(sym.get_st_value()
1293 + relobj->section_address(orig_st_shndx));
1294 }
1295 st_shndx = elfcpp::SHN_ABS;
1296 is_ordinary = false;
1297 psym = &sym2;
1298 }
1299
1300 // Fix up visibility if object has no-export set.
1301 if (relobj->no_export()
1302 && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
1303 {
1304 // We may have copied symbol already above.
1305 if (psym != &sym2)
1306 {
1307 memcpy(symbuf, p, sym_size);
1308 psym = &sym2;
1309 }
1310
1311 elfcpp::STV visibility = sym2.get_st_visibility();
1312 if (visibility == elfcpp::STV_DEFAULT
1313 || visibility == elfcpp::STV_PROTECTED)
1314 {
1315 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1316 unsigned char nonvis = sym2.get_st_nonvis();
1317 sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1318 }
1319 }
1320
1321 Stringpool::Key name_key;
1322 name = this->namepool_.add_with_length(name, namelen, true,
1323 &name_key);
1324
1325 Sized_symbol<size>* res;
1326 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1327 is_default_version, *psym, st_shndx,
1328 is_ordinary, orig_st_shndx);
1329
1330 if (res == NULL)
1331 continue;
1332
1333 if (is_forced_local)
1334 this->force_local(res);
1335
1336 // Do not treat this symbol as garbage if this symbol will be
1337 // exported to the dynamic symbol table. This is true when
1338 // building a shared library or using --export-dynamic and
1339 // the symbol is externally visible.
1340 if (parameters->options().gc_sections()
1341 && res->is_externally_visible()
1342 && !res->is_from_dynobj()
1343 && (parameters->options().shared()
1344 || parameters->options().export_dynamic()
1345 || parameters->options().in_dynamic_list(res->name())))
1346 this->gc_mark_symbol(res);
1347
1348 if (is_defined_in_discarded_section)
1349 res->set_is_defined_in_discarded_section();
1350
1351 (*sympointers)[i] = res;
1352 }
1353 }
1354
1355 // Add a symbol from a plugin-claimed file.
1356
1357 template<int size, bool big_endian>
1358 Symbol*
1359 Symbol_table::add_from_pluginobj(
1360 Sized_pluginobj<size, big_endian>* obj,
1361 const char* name,
1362 const char* ver,
1363 elfcpp::Sym<size, big_endian>* sym)
1364 {
1365 unsigned int st_shndx = sym->get_st_shndx();
1366 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1367
1368 Stringpool::Key ver_key = 0;
1369 bool is_default_version = false;
1370 bool is_forced_local = false;
1371
1372 if (ver != NULL)
1373 {
1374 ver = this->namepool_.add(ver, true, &ver_key);
1375 }
1376 // We don't want to assign a version to an undefined symbol,
1377 // even if it is listed in the version script. FIXME: What
1378 // about a common symbol?
1379 else
1380 {
1381 if (!this->version_script_.empty()
1382 && st_shndx != elfcpp::SHN_UNDEF)
1383 {
1384 // The symbol name did not have a version, but the
1385 // version script may assign a version anyway.
1386 std::string version;
1387 bool is_global;
1388 if (this->version_script_.get_symbol_version(name, &version,
1389 &is_global))
1390 {
1391 if (!is_global)
1392 is_forced_local = true;
1393 else if (!version.empty())
1394 {
1395 ver = this->namepool_.add_with_length(version.c_str(),
1396 version.length(),
1397 true,
1398 &ver_key);
1399 is_default_version = true;
1400 }
1401 }
1402 }
1403 }
1404
1405 Stringpool::Key name_key;
1406 name = this->namepool_.add(name, true, &name_key);
1407
1408 Sized_symbol<size>* res;
1409 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1410 is_default_version, *sym, st_shndx,
1411 is_ordinary, st_shndx);
1412
1413 if (res == NULL)
1414 return NULL;
1415
1416 if (is_forced_local)
1417 this->force_local(res);
1418
1419 return res;
1420 }
1421
1422 // Add all the symbols in a dynamic object to the hash table.
1423
1424 template<int size, bool big_endian>
1425 void
1426 Symbol_table::add_from_dynobj(
1427 Sized_dynobj<size, big_endian>* dynobj,
1428 const unsigned char* syms,
1429 size_t count,
1430 const char* sym_names,
1431 size_t sym_name_size,
1432 const unsigned char* versym,
1433 size_t versym_size,
1434 const std::vector<const char*>* version_map,
1435 typename Sized_relobj_file<size, big_endian>::Symbols* sympointers,
1436 size_t* defined)
1437 {
1438 *defined = 0;
1439
1440 gold_assert(size == parameters->target().get_size());
1441
1442 if (dynobj->just_symbols())
1443 {
1444 gold_error(_("--just-symbols does not make sense with a shared object"));
1445 return;
1446 }
1447
1448 // FIXME: For incremental links, we don't store version information,
1449 // so we need to ignore version symbols for now.
1450 if (parameters->incremental_update())
1451 versym = NULL;
1452
1453 if (versym != NULL && versym_size / 2 < count)
1454 {
1455 dynobj->error(_("too few symbol versions"));
1456 return;
1457 }
1458
1459 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1460
1461 // We keep a list of all STT_OBJECT symbols, so that we can resolve
1462 // weak aliases. This is necessary because if the dynamic object
1463 // provides the same variable under two names, one of which is a
1464 // weak definition, and the regular object refers to the weak
1465 // definition, we have to put both the weak definition and the
1466 // strong definition into the dynamic symbol table. Given a weak
1467 // definition, the only way that we can find the corresponding
1468 // strong definition, if any, is to search the symbol table.
1469 std::vector<Sized_symbol<size>*> object_symbols;
1470
1471 const unsigned char* p = syms;
1472 const unsigned char* vs = versym;
1473 for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1474 {
1475 elfcpp::Sym<size, big_endian> sym(p);
1476
1477 if (sympointers != NULL)
1478 (*sympointers)[i] = NULL;
1479
1480 // Ignore symbols with local binding or that have
1481 // internal or hidden visibility.
1482 if (sym.get_st_bind() == elfcpp::STB_LOCAL
1483 || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1484 || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
1485 continue;
1486
1487 // A protected symbol in a shared library must be treated as a
1488 // normal symbol when viewed from outside the shared library.
1489 // Implement this by overriding the visibility here.
1490 // Likewise, an IFUNC symbol in a shared library must be treated
1491 // as a normal FUNC symbol.
1492 elfcpp::Sym<size, big_endian>* psym = &sym;
1493 unsigned char symbuf[sym_size];
1494 elfcpp::Sym<size, big_endian> sym2(symbuf);
1495 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED
1496 || sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1497 {
1498 memcpy(symbuf, p, sym_size);
1499 elfcpp::Sym_write<size, big_endian> sw(symbuf);
1500 if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1501 sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1502 if (sym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1503 sw.put_st_info(sym.get_st_bind(), elfcpp::STT_FUNC);
1504 psym = &sym2;
1505 }
1506
1507 unsigned int st_name = psym->get_st_name();
1508 if (st_name >= sym_name_size)
1509 {
1510 dynobj->error(_("bad symbol name offset %u at %zu"),
1511 st_name, i);
1512 continue;
1513 }
1514
1515 const char* name = sym_names + st_name;
1516
1517 bool is_ordinary;
1518 unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
1519 &is_ordinary);
1520
1521 if (st_shndx != elfcpp::SHN_UNDEF)
1522 ++*defined;
1523
1524 Sized_symbol<size>* res;
1525
1526 if (versym == NULL)
1527 {
1528 Stringpool::Key name_key;
1529 name = this->namepool_.add(name, true, &name_key);
1530 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1531 false, *psym, st_shndx, is_ordinary,
1532 st_shndx);
1533 }
1534 else
1535 {
1536 // Read the version information.
1537
1538 unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
1539
1540 bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1541 v &= elfcpp::VERSYM_VERSION;
1542
1543 // The Sun documentation says that V can be VER_NDX_LOCAL,
1544 // or VER_NDX_GLOBAL, or a version index. The meaning of
1545 // VER_NDX_LOCAL is defined as "Symbol has local scope."
1546 // The old GNU linker will happily generate VER_NDX_LOCAL
1547 // for an undefined symbol. I don't know what the Sun
1548 // linker will generate.
1549
1550 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1551 && st_shndx != elfcpp::SHN_UNDEF)
1552 {
1553 // This symbol should not be visible outside the object.
1554 continue;
1555 }
1556
1557 // At this point we are definitely going to add this symbol.
1558 Stringpool::Key name_key;
1559 name = this->namepool_.add(name, true, &name_key);
1560
1561 if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1562 || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1563 {
1564 // This symbol does not have a version.
1565 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1566 false, *psym, st_shndx, is_ordinary,
1567 st_shndx);
1568 }
1569 else
1570 {
1571 if (v >= version_map->size())
1572 {
1573 dynobj->error(_("versym for symbol %zu out of range: %u"),
1574 i, v);
1575 continue;
1576 }
1577
1578 const char* version = (*version_map)[v];
1579 if (version == NULL)
1580 {
1581 dynobj->error(_("versym for symbol %zu has no name: %u"),
1582 i, v);
1583 continue;
1584 }
1585
1586 Stringpool::Key version_key;
1587 version = this->namepool_.add(version, true, &version_key);
1588
1589 // If this is an absolute symbol, and the version name
1590 // and symbol name are the same, then this is the
1591 // version definition symbol. These symbols exist to
1592 // support using -u to pull in particular versions. We
1593 // do not want to record a version for them.
1594 if (st_shndx == elfcpp::SHN_ABS
1595 && !is_ordinary
1596 && name_key == version_key)
1597 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1598 false, *psym, st_shndx, is_ordinary,
1599 st_shndx);
1600 else
1601 {
1602 const bool is_default_version =
1603 !hidden && st_shndx != elfcpp::SHN_UNDEF;
1604 res = this->add_from_object(dynobj, name, name_key, version,
1605 version_key, is_default_version,
1606 *psym, st_shndx,
1607 is_ordinary, st_shndx);
1608 }
1609 }
1610 }
1611
1612 if (res == NULL)
1613 continue;
1614
1615 // Note that it is possible that RES was overridden by an
1616 // earlier object, in which case it can't be aliased here.
1617 if (st_shndx != elfcpp::SHN_UNDEF
1618 && is_ordinary
1619 && psym->get_st_type() == elfcpp::STT_OBJECT
1620 && res->source() == Symbol::FROM_OBJECT
1621 && res->object() == dynobj)
1622 object_symbols.push_back(res);
1623
1624 // If the symbol has protected visibility in the dynobj,
1625 // mark it as such if it was not overridden.
1626 if (res->source() == Symbol::FROM_OBJECT
1627 && res->object() == dynobj
1628 && sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1629 res->set_is_protected();
1630
1631 if (sympointers != NULL)
1632 (*sympointers)[i] = res;
1633 }
1634
1635 this->record_weak_aliases(&object_symbols);
1636 }
1637
1638 // Add a symbol from a incremental object file.
1639
1640 template<int size, bool big_endian>
1641 Sized_symbol<size>*
1642 Symbol_table::add_from_incrobj(
1643 Object* obj,
1644 const char* name,
1645 const char* ver,
1646 elfcpp::Sym<size, big_endian>* sym)
1647 {
1648 unsigned int st_shndx = sym->get_st_shndx();
1649 bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1650
1651 Stringpool::Key ver_key = 0;
1652 bool is_default_version = false;
1653
1654 Stringpool::Key name_key;
1655 name = this->namepool_.add(name, true, &name_key);
1656
1657 Sized_symbol<size>* res;
1658 res = this->add_from_object(obj, name, name_key, ver, ver_key,
1659 is_default_version, *sym, st_shndx,
1660 is_ordinary, st_shndx);
1661
1662 return res;
1663 }
1664
1665 // This is used to sort weak aliases. We sort them first by section
1666 // index, then by offset, then by weak ahead of strong.
1667
1668 template<int size>
1669 class Weak_alias_sorter
1670 {
1671 public:
1672 bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1673 };
1674
1675 template<int size>
1676 bool
1677 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1678 const Sized_symbol<size>* s2) const
1679 {
1680 bool is_ordinary;
1681 unsigned int s1_shndx = s1->shndx(&is_ordinary);
1682 gold_assert(is_ordinary);
1683 unsigned int s2_shndx = s2->shndx(&is_ordinary);
1684 gold_assert(is_ordinary);
1685 if (s1_shndx != s2_shndx)
1686 return s1_shndx < s2_shndx;
1687
1688 if (s1->value() != s2->value())
1689 return s1->value() < s2->value();
1690 if (s1->binding() != s2->binding())
1691 {
1692 if (s1->binding() == elfcpp::STB_WEAK)
1693 return true;
1694 if (s2->binding() == elfcpp::STB_WEAK)
1695 return false;
1696 }
1697 return std::string(s1->name()) < std::string(s2->name());
1698 }
1699
1700 // SYMBOLS is a list of object symbols from a dynamic object. Look
1701 // for any weak aliases, and record them so that if we add the weak
1702 // alias to the dynamic symbol table, we also add the corresponding
1703 // strong symbol.
1704
1705 template<int size>
1706 void
1707 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1708 {
1709 // Sort the vector by section index, then by offset, then by weak
1710 // ahead of strong.
1711 std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1712
1713 // Walk through the vector. For each weak definition, record
1714 // aliases.
1715 for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1716 symbols->begin();
1717 p != symbols->end();
1718 ++p)
1719 {
1720 if ((*p)->binding() != elfcpp::STB_WEAK)
1721 continue;
1722
1723 // Build a circular list of weak aliases. Each symbol points to
1724 // the next one in the circular list.
1725
1726 Sized_symbol<size>* from_sym = *p;
1727 typename std::vector<Sized_symbol<size>*>::const_iterator q;
1728 for (q = p + 1; q != symbols->end(); ++q)
1729 {
1730 bool dummy;
1731 if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1732 || (*q)->value() != from_sym->value())
1733 break;
1734
1735 this->weak_aliases_[from_sym] = *q;
1736 from_sym->set_has_alias();
1737 from_sym = *q;
1738 }
1739
1740 if (from_sym != *p)
1741 {
1742 this->weak_aliases_[from_sym] = *p;
1743 from_sym->set_has_alias();
1744 }
1745
1746 p = q - 1;
1747 }
1748 }
1749
1750 // Create and return a specially defined symbol. If ONLY_IF_REF is
1751 // true, then only create the symbol if there is a reference to it.
1752 // If this does not return NULL, it sets *POLDSYM to the existing
1753 // symbol if there is one. This sets *RESOLVE_OLDSYM if we should
1754 // resolve the newly created symbol to the old one. This
1755 // canonicalizes *PNAME and *PVERSION.
1756
1757 template<int size, bool big_endian>
1758 Sized_symbol<size>*
1759 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1760 bool only_if_ref,
1761 Sized_symbol<size>** poldsym,
1762 bool* resolve_oldsym, bool is_forced_local)
1763 {
1764 *resolve_oldsym = false;
1765 *poldsym = NULL;
1766
1767 // If the caller didn't give us a version, see if we get one from
1768 // the version script.
1769 std::string v;
1770 bool is_default_version = false;
1771 if (!is_forced_local && *pversion == NULL)
1772 {
1773 bool is_global;
1774 if (this->version_script_.get_symbol_version(*pname, &v, &is_global))
1775 {
1776 if (is_global && !v.empty())
1777 {
1778 *pversion = v.c_str();
1779 // If we get the version from a version script, then we
1780 // are also the default version.
1781 is_default_version = true;
1782 }
1783 }
1784 }
1785
1786 Symbol* oldsym;
1787 Sized_symbol<size>* sym;
1788
1789 bool add_to_table = false;
1790 typename Symbol_table_type::iterator add_loc = this->table_.end();
1791 bool add_def_to_table = false;
1792 typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1793
1794 if (only_if_ref)
1795 {
1796 oldsym = this->lookup(*pname, *pversion);
1797 if (oldsym == NULL && is_default_version)
1798 oldsym = this->lookup(*pname, NULL);
1799 if (oldsym == NULL || !oldsym->is_undefined())
1800 return NULL;
1801
1802 *pname = oldsym->name();
1803 if (is_default_version)
1804 *pversion = this->namepool_.add(*pversion, true, NULL);
1805 else
1806 *pversion = oldsym->version();
1807 }
1808 else
1809 {
1810 // Canonicalize NAME and VERSION.
1811 Stringpool::Key name_key;
1812 *pname = this->namepool_.add(*pname, true, &name_key);
1813
1814 Stringpool::Key version_key = 0;
1815 if (*pversion != NULL)
1816 *pversion = this->namepool_.add(*pversion, true, &version_key);
1817
1818 Symbol* const snull = NULL;
1819 std::pair<typename Symbol_table_type::iterator, bool> ins =
1820 this->table_.insert(std::make_pair(std::make_pair(name_key,
1821 version_key),
1822 snull));
1823
1824 std::pair<typename Symbol_table_type::iterator, bool> insdefault =
1825 std::make_pair(this->table_.end(), false);
1826 if (is_default_version)
1827 {
1828 const Stringpool::Key vnull = 0;
1829 insdefault =
1830 this->table_.insert(std::make_pair(std::make_pair(name_key,
1831 vnull),
1832 snull));
1833 }
1834
1835 if (!ins.second)
1836 {
1837 // We already have a symbol table entry for NAME/VERSION.
1838 oldsym = ins.first->second;
1839 gold_assert(oldsym != NULL);
1840
1841 if (is_default_version)
1842 {
1843 Sized_symbol<size>* soldsym =
1844 this->get_sized_symbol<size>(oldsym);
1845 this->define_default_version<size, big_endian>(soldsym,
1846 insdefault.second,
1847 insdefault.first);
1848 }
1849 }
1850 else
1851 {
1852 // We haven't seen this symbol before.
1853 gold_assert(ins.first->second == NULL);
1854
1855 add_to_table = true;
1856 add_loc = ins.first;
1857
1858 if (is_default_version && !insdefault.second)
1859 {
1860 // We are adding NAME/VERSION, and it is the default
1861 // version. We already have an entry for NAME/NULL.
1862 oldsym = insdefault.first->second;
1863 *resolve_oldsym = true;
1864 }
1865 else
1866 {
1867 oldsym = NULL;
1868
1869 if (is_default_version)
1870 {
1871 add_def_to_table = true;
1872 add_def_loc = insdefault.first;
1873 }
1874 }
1875 }
1876 }
1877
1878 const Target& target = parameters->target();
1879 if (!target.has_make_symbol())
1880 sym = new Sized_symbol<size>();
1881 else
1882 {
1883 Sized_target<size, big_endian>* sized_target =
1884 parameters->sized_target<size, big_endian>();
1885 sym = sized_target->make_symbol(*pname, elfcpp::STT_NOTYPE,
1886 NULL, elfcpp::SHN_UNDEF, 0);
1887 if (sym == NULL)
1888 return NULL;
1889 }
1890
1891 if (add_to_table)
1892 add_loc->second = sym;
1893 else
1894 gold_assert(oldsym != NULL);
1895
1896 if (add_def_to_table)
1897 add_def_loc->second = sym;
1898
1899 *poldsym = this->get_sized_symbol<size>(oldsym);
1900
1901 return sym;
1902 }
1903
1904 // Define a symbol based on an Output_data.
1905
1906 Symbol*
1907 Symbol_table::define_in_output_data(const char* name,
1908 const char* version,
1909 Defined defined,
1910 Output_data* od,
1911 uint64_t value,
1912 uint64_t symsize,
1913 elfcpp::STT type,
1914 elfcpp::STB binding,
1915 elfcpp::STV visibility,
1916 unsigned char nonvis,
1917 bool offset_is_from_end,
1918 bool only_if_ref)
1919 {
1920 if (parameters->target().get_size() == 32)
1921 {
1922 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1923 return this->do_define_in_output_data<32>(name, version, defined, od,
1924 value, symsize, type, binding,
1925 visibility, nonvis,
1926 offset_is_from_end,
1927 only_if_ref);
1928 #else
1929 gold_unreachable();
1930 #endif
1931 }
1932 else if (parameters->target().get_size() == 64)
1933 {
1934 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1935 return this->do_define_in_output_data<64>(name, version, defined, od,
1936 value, symsize, type, binding,
1937 visibility, nonvis,
1938 offset_is_from_end,
1939 only_if_ref);
1940 #else
1941 gold_unreachable();
1942 #endif
1943 }
1944 else
1945 gold_unreachable();
1946 }
1947
1948 // Define a symbol in an Output_data, sized version.
1949
1950 template<int size>
1951 Sized_symbol<size>*
1952 Symbol_table::do_define_in_output_data(
1953 const char* name,
1954 const char* version,
1955 Defined defined,
1956 Output_data* od,
1957 typename elfcpp::Elf_types<size>::Elf_Addr value,
1958 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1959 elfcpp::STT type,
1960 elfcpp::STB binding,
1961 elfcpp::STV visibility,
1962 unsigned char nonvis,
1963 bool offset_is_from_end,
1964 bool only_if_ref)
1965 {
1966 Sized_symbol<size>* sym;
1967 Sized_symbol<size>* oldsym;
1968 bool resolve_oldsym;
1969 const bool is_forced_local = binding == elfcpp::STB_LOCAL;
1970
1971 if (parameters->target().is_big_endian())
1972 {
1973 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1974 sym = this->define_special_symbol<size, true>(&name, &version,
1975 only_if_ref, &oldsym,
1976 &resolve_oldsym,
1977 is_forced_local);
1978 #else
1979 gold_unreachable();
1980 #endif
1981 }
1982 else
1983 {
1984 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1985 sym = this->define_special_symbol<size, false>(&name, &version,
1986 only_if_ref, &oldsym,
1987 &resolve_oldsym,
1988 is_forced_local);
1989 #else
1990 gold_unreachable();
1991 #endif
1992 }
1993
1994 if (sym == NULL)
1995 return NULL;
1996
1997 sym->init_output_data(name, version, od, value, symsize, type, binding,
1998 visibility, nonvis, offset_is_from_end,
1999 defined == PREDEFINED);
2000
2001 if (oldsym == NULL)
2002 {
2003 if (is_forced_local || this->version_script_.symbol_is_local(name))
2004 this->force_local(sym);
2005 else if (version != NULL)
2006 sym->set_is_default();
2007 return sym;
2008 }
2009
2010 if (Symbol_table::should_override_with_special(oldsym, type, defined))
2011 this->override_with_special(oldsym, sym);
2012
2013 if (resolve_oldsym)
2014 return sym;
2015 else
2016 {
2017 if (defined == PREDEFINED
2018 && (is_forced_local || this->version_script_.symbol_is_local(name)))
2019 this->force_local(oldsym);
2020 delete sym;
2021 return oldsym;
2022 }
2023 }
2024
2025 // Define a symbol based on an Output_segment.
2026
2027 Symbol*
2028 Symbol_table::define_in_output_segment(const char* name,
2029 const char* version,
2030 Defined defined,
2031 Output_segment* os,
2032 uint64_t value,
2033 uint64_t symsize,
2034 elfcpp::STT type,
2035 elfcpp::STB binding,
2036 elfcpp::STV visibility,
2037 unsigned char nonvis,
2038 Symbol::Segment_offset_base offset_base,
2039 bool only_if_ref)
2040 {
2041 if (parameters->target().get_size() == 32)
2042 {
2043 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2044 return this->do_define_in_output_segment<32>(name, version, defined, os,
2045 value, symsize, type,
2046 binding, visibility, nonvis,
2047 offset_base, only_if_ref);
2048 #else
2049 gold_unreachable();
2050 #endif
2051 }
2052 else if (parameters->target().get_size() == 64)
2053 {
2054 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2055 return this->do_define_in_output_segment<64>(name, version, defined, os,
2056 value, symsize, type,
2057 binding, visibility, nonvis,
2058 offset_base, only_if_ref);
2059 #else
2060 gold_unreachable();
2061 #endif
2062 }
2063 else
2064 gold_unreachable();
2065 }
2066
2067 // Define a symbol in an Output_segment, sized version.
2068
2069 template<int size>
2070 Sized_symbol<size>*
2071 Symbol_table::do_define_in_output_segment(
2072 const char* name,
2073 const char* version,
2074 Defined defined,
2075 Output_segment* os,
2076 typename elfcpp::Elf_types<size>::Elf_Addr value,
2077 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
2078 elfcpp::STT type,
2079 elfcpp::STB binding,
2080 elfcpp::STV visibility,
2081 unsigned char nonvis,
2082 Symbol::Segment_offset_base offset_base,
2083 bool only_if_ref)
2084 {
2085 Sized_symbol<size>* sym;
2086 Sized_symbol<size>* oldsym;
2087 bool resolve_oldsym;
2088 const bool is_forced_local = binding == elfcpp::STB_LOCAL;
2089
2090 if (parameters->target().is_big_endian())
2091 {
2092 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2093 sym = this->define_special_symbol<size, true>(&name, &version,
2094 only_if_ref, &oldsym,
2095 &resolve_oldsym,
2096 is_forced_local);
2097 #else
2098 gold_unreachable();
2099 #endif
2100 }
2101 else
2102 {
2103 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2104 sym = this->define_special_symbol<size, false>(&name, &version,
2105 only_if_ref, &oldsym,
2106 &resolve_oldsym,
2107 is_forced_local);
2108 #else
2109 gold_unreachable();
2110 #endif
2111 }
2112
2113 if (sym == NULL)
2114 return NULL;
2115
2116 sym->init_output_segment(name, version, os, value, symsize, type, binding,
2117 visibility, nonvis, offset_base,
2118 defined == PREDEFINED);
2119
2120 if (oldsym == NULL)
2121 {
2122 if (is_forced_local || this->version_script_.symbol_is_local(name))
2123 this->force_local(sym);
2124 else if (version != NULL)
2125 sym->set_is_default();
2126 return sym;
2127 }
2128
2129 if (Symbol_table::should_override_with_special(oldsym, type, defined))
2130 this->override_with_special(oldsym, sym);
2131
2132 if (resolve_oldsym)
2133 return sym;
2134 else
2135 {
2136 if (is_forced_local || this->version_script_.symbol_is_local(name))
2137 this->force_local(oldsym);
2138 delete sym;
2139 return oldsym;
2140 }
2141 }
2142
2143 // Define a special symbol with a constant value. It is a multiple
2144 // definition error if this symbol is already defined.
2145
2146 Symbol*
2147 Symbol_table::define_as_constant(const char* name,
2148 const char* version,
2149 Defined defined,
2150 uint64_t value,
2151 uint64_t symsize,
2152 elfcpp::STT type,
2153 elfcpp::STB binding,
2154 elfcpp::STV visibility,
2155 unsigned char nonvis,
2156 bool only_if_ref,
2157 bool force_override)
2158 {
2159 if (parameters->target().get_size() == 32)
2160 {
2161 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2162 return this->do_define_as_constant<32>(name, version, defined, value,
2163 symsize, type, binding,
2164 visibility, nonvis, only_if_ref,
2165 force_override);
2166 #else
2167 gold_unreachable();
2168 #endif
2169 }
2170 else if (parameters->target().get_size() == 64)
2171 {
2172 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2173 return this->do_define_as_constant<64>(name, version, defined, value,
2174 symsize, type, binding,
2175 visibility, nonvis, only_if_ref,
2176 force_override);
2177 #else
2178 gold_unreachable();
2179 #endif
2180 }
2181 else
2182 gold_unreachable();
2183 }
2184
2185 // Define a symbol as a constant, sized version.
2186
2187 template<int size>
2188 Sized_symbol<size>*
2189 Symbol_table::do_define_as_constant(
2190 const char* name,
2191 const char* version,
2192 Defined defined,
2193 typename elfcpp::Elf_types<size>::Elf_Addr value,
2194 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
2195 elfcpp::STT type,
2196 elfcpp::STB binding,
2197 elfcpp::STV visibility,
2198 unsigned char nonvis,
2199 bool only_if_ref,
2200 bool force_override)
2201 {
2202 Sized_symbol<size>* sym;
2203 Sized_symbol<size>* oldsym;
2204 bool resolve_oldsym;
2205 const bool is_forced_local = binding == elfcpp::STB_LOCAL;
2206
2207 if (parameters->target().is_big_endian())
2208 {
2209 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2210 sym = this->define_special_symbol<size, true>(&name, &version,
2211 only_if_ref, &oldsym,
2212 &resolve_oldsym,
2213 is_forced_local);
2214 #else
2215 gold_unreachable();
2216 #endif
2217 }
2218 else
2219 {
2220 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2221 sym = this->define_special_symbol<size, false>(&name, &version,
2222 only_if_ref, &oldsym,
2223 &resolve_oldsym,
2224 is_forced_local);
2225 #else
2226 gold_unreachable();
2227 #endif
2228 }
2229
2230 if (sym == NULL)
2231 return NULL;
2232
2233 sym->init_constant(name, version, value, symsize, type, binding, visibility,
2234 nonvis, defined == PREDEFINED);
2235
2236 if (oldsym == NULL)
2237 {
2238 // Version symbols are absolute symbols with name == version.
2239 // We don't want to force them to be local.
2240 if ((version == NULL
2241 || name != version
2242 || value != 0)
2243 && (is_forced_local || this->version_script_.symbol_is_local(name)))
2244 this->force_local(sym);
2245 else if (version != NULL
2246 && (name != version || value != 0))
2247 sym->set_is_default();
2248 return sym;
2249 }
2250
2251 if (force_override
2252 || Symbol_table::should_override_with_special(oldsym, type, defined))
2253 this->override_with_special(oldsym, sym);
2254
2255 if (resolve_oldsym)
2256 return sym;
2257 else
2258 {
2259 if (is_forced_local || this->version_script_.symbol_is_local(name))
2260 this->force_local(oldsym);
2261 delete sym;
2262 return oldsym;
2263 }
2264 }
2265
2266 // Define a set of symbols in output sections.
2267
2268 void
2269 Symbol_table::define_symbols(const Layout* layout, int count,
2270 const Define_symbol_in_section* p,
2271 bool only_if_ref)
2272 {
2273 for (int i = 0; i < count; ++i, ++p)
2274 {
2275 Output_section* os = layout->find_output_section(p->output_section);
2276 if (os != NULL)
2277 this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
2278 p->size, p->type, p->binding,
2279 p->visibility, p->nonvis,
2280 p->offset_is_from_end,
2281 only_if_ref || p->only_if_ref);
2282 else
2283 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2284 p->type, p->binding, p->visibility, p->nonvis,
2285 only_if_ref || p->only_if_ref,
2286 false);
2287 }
2288 }
2289
2290 // Define a set of symbols in output segments.
2291
2292 void
2293 Symbol_table::define_symbols(const Layout* layout, int count,
2294 const Define_symbol_in_segment* p,
2295 bool only_if_ref)
2296 {
2297 for (int i = 0; i < count; ++i, ++p)
2298 {
2299 Output_segment* os = layout->find_output_segment(p->segment_type,
2300 p->segment_flags_set,
2301 p->segment_flags_clear);
2302 if (os != NULL)
2303 this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
2304 p->size, p->type, p->binding,
2305 p->visibility, p->nonvis,
2306 p->offset_base,
2307 only_if_ref || p->only_if_ref);
2308 else
2309 this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2310 p->type, p->binding, p->visibility, p->nonvis,
2311 only_if_ref || p->only_if_ref,
2312 false);
2313 }
2314 }
2315
2316 // Define CSYM using a COPY reloc. POSD is the Output_data where the
2317 // symbol should be defined--typically a .dyn.bss section. VALUE is
2318 // the offset within POSD.
2319
2320 template<int size>
2321 void
2322 Symbol_table::define_with_copy_reloc(
2323 Sized_symbol<size>* csym,
2324 Output_data* posd,
2325 typename elfcpp::Elf_types<size>::Elf_Addr value)
2326 {
2327 gold_assert(csym->is_from_dynobj());
2328 gold_assert(!csym->is_copied_from_dynobj());
2329 Object* object = csym->object();
2330 gold_assert(object->is_dynamic());
2331 Dynobj* dynobj = static_cast<Dynobj*>(object);
2332
2333 // Our copied variable has to override any variable in a shared
2334 // library.
2335 elfcpp::STB binding = csym->binding();
2336 if (binding == elfcpp::STB_WEAK)
2337 binding = elfcpp::STB_GLOBAL;
2338
2339 this->define_in_output_data(csym->name(), csym->version(), COPY,
2340 posd, value, csym->symsize(),
2341 csym->type(), binding,
2342 csym->visibility(), csym->nonvis(),
2343 false, false);
2344
2345 csym->set_is_copied_from_dynobj();
2346 csym->set_needs_dynsym_entry();
2347
2348 this->copied_symbol_dynobjs_[csym] = dynobj;
2349
2350 // We have now defined all aliases, but we have not entered them all
2351 // in the copied_symbol_dynobjs_ map.
2352 if (csym->has_alias())
2353 {
2354 Symbol* sym = csym;
2355 while (true)
2356 {
2357 sym = this->weak_aliases_[sym];
2358 if (sym == csym)
2359 break;
2360 gold_assert(sym->output_data() == posd);
2361
2362 sym->set_is_copied_from_dynobj();
2363 this->copied_symbol_dynobjs_[sym] = dynobj;
2364 }
2365 }
2366 }
2367
2368 // SYM is defined using a COPY reloc. Return the dynamic object where
2369 // the original definition was found.
2370
2371 Dynobj*
2372 Symbol_table::get_copy_source(const Symbol* sym) const
2373 {
2374 gold_assert(sym->is_copied_from_dynobj());
2375 Copied_symbol_dynobjs::const_iterator p =
2376 this->copied_symbol_dynobjs_.find(sym);
2377 gold_assert(p != this->copied_symbol_dynobjs_.end());
2378 return p->second;
2379 }
2380
2381 // Add any undefined symbols named on the command line.
2382
2383 void
2384 Symbol_table::add_undefined_symbols_from_command_line(Layout* layout)
2385 {
2386 if (parameters->options().any_undefined()
2387 || layout->script_options()->any_unreferenced())
2388 {
2389 if (parameters->target().get_size() == 32)
2390 {
2391 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2392 this->do_add_undefined_symbols_from_command_line<32>(layout);
2393 #else
2394 gold_unreachable();
2395 #endif
2396 }
2397 else if (parameters->target().get_size() == 64)
2398 {
2399 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2400 this->do_add_undefined_symbols_from_command_line<64>(layout);
2401 #else
2402 gold_unreachable();
2403 #endif
2404 }
2405 else
2406 gold_unreachable();
2407 }
2408 }
2409
2410 template<int size>
2411 void
2412 Symbol_table::do_add_undefined_symbols_from_command_line(Layout* layout)
2413 {
2414 for (options::String_set::const_iterator p =
2415 parameters->options().undefined_begin();
2416 p != parameters->options().undefined_end();
2417 ++p)
2418 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2419
2420 for (options::String_set::const_iterator p =
2421 parameters->options().export_dynamic_symbol_begin();
2422 p != parameters->options().export_dynamic_symbol_end();
2423 ++p)
2424 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2425
2426 for (Script_options::referenced_const_iterator p =
2427 layout->script_options()->referenced_begin();
2428 p != layout->script_options()->referenced_end();
2429 ++p)
2430 this->add_undefined_symbol_from_command_line<size>(p->c_str());
2431 }
2432
2433 template<int size>
2434 void
2435 Symbol_table::add_undefined_symbol_from_command_line(const char* name)
2436 {
2437 if (this->lookup(name) != NULL)
2438 return;
2439
2440 const char* version = NULL;
2441
2442 Sized_symbol<size>* sym;
2443 Sized_symbol<size>* oldsym;
2444 bool resolve_oldsym;
2445 if (parameters->target().is_big_endian())
2446 {
2447 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2448 sym = this->define_special_symbol<size, true>(&name, &version,
2449 false, &oldsym,
2450 &resolve_oldsym,
2451 false);
2452 #else
2453 gold_unreachable();
2454 #endif
2455 }
2456 else
2457 {
2458 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2459 sym = this->define_special_symbol<size, false>(&name, &version,
2460 false, &oldsym,
2461 &resolve_oldsym,
2462 false);
2463 #else
2464 gold_unreachable();
2465 #endif
2466 }
2467
2468 gold_assert(oldsym == NULL);
2469
2470 sym->init_undefined(name, version, 0, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2471 elfcpp::STV_DEFAULT, 0);
2472 ++this->saw_undefined_;
2473 }
2474
2475 // Set the dynamic symbol indexes. INDEX is the index of the first
2476 // global dynamic symbol. Pointers to the symbols are stored into the
2477 // vector SYMS. The names are added to DYNPOOL. This returns an
2478 // updated dynamic symbol index.
2479
2480 unsigned int
2481 Symbol_table::set_dynsym_indexes(unsigned int index,
2482 std::vector<Symbol*>* syms,
2483 Stringpool* dynpool,
2484 Versions* versions)
2485 {
2486 std::vector<Symbol*> as_needed_sym;
2487
2488 // Allow a target to set dynsym indexes.
2489 if (parameters->target().has_custom_set_dynsym_indexes())
2490 {
2491 std::vector<Symbol*> dyn_symbols;
2492 for (Symbol_table_type::iterator p = this->table_.begin();
2493 p != this->table_.end();
2494 ++p)
2495 {
2496 Symbol* sym = p->second;
2497 if (!sym->should_add_dynsym_entry(this))
2498 sym->set_dynsym_index(-1U);
2499 else
2500 dyn_symbols.push_back(sym);
2501 }
2502
2503 return parameters->target().set_dynsym_indexes(&dyn_symbols, index, syms,
2504 dynpool, versions, this);
2505 }
2506
2507 for (Symbol_table_type::iterator p = this->table_.begin();
2508 p != this->table_.end();
2509 ++p)
2510 {
2511 Symbol* sym = p->second;
2512
2513 // Note that SYM may already have a dynamic symbol index, since
2514 // some symbols appear more than once in the symbol table, with
2515 // and without a version.
2516
2517 if (!sym->should_add_dynsym_entry(this))
2518 sym->set_dynsym_index(-1U);
2519 else if (!sym->has_dynsym_index())
2520 {
2521 sym->set_dynsym_index(index);
2522 ++index;
2523 syms->push_back(sym);
2524 dynpool->add(sym->name(), false, NULL);
2525
2526 // If the symbol is defined in a dynamic object and is
2527 // referenced strongly in a regular object, then mark the
2528 // dynamic object as needed. This is used to implement
2529 // --as-needed.
2530 if (sym->is_from_dynobj()
2531 && sym->in_reg()
2532 && !sym->is_undef_binding_weak())
2533 sym->object()->set_is_needed();
2534
2535 // Record any version information, except those from
2536 // as-needed libraries not seen to be needed. Note that the
2537 // is_needed state for such libraries can change in this loop.
2538 if (sym->version() != NULL)
2539 {
2540 if (!sym->is_from_dynobj()
2541 || !sym->object()->as_needed()
2542 || sym->object()->is_needed())
2543 versions->record_version(this, dynpool, sym);
2544 else
2545 as_needed_sym.push_back(sym);
2546 }
2547 }
2548 }
2549
2550 // Process version information for symbols from as-needed libraries.
2551 for (std::vector<Symbol*>::iterator p = as_needed_sym.begin();
2552 p != as_needed_sym.end();
2553 ++p)
2554 {
2555 Symbol* sym = *p;
2556
2557 if (sym->object()->is_needed())
2558 versions->record_version(this, dynpool, sym);
2559 else
2560 sym->clear_version();
2561 }
2562
2563 // Finish up the versions. In some cases this may add new dynamic
2564 // symbols.
2565 index = versions->finalize(this, index, syms);
2566
2567 // Process target-specific symbols.
2568 for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
2569 p != this->target_symbols_.end();
2570 ++p)
2571 {
2572 (*p)->set_dynsym_index(index);
2573 ++index;
2574 syms->push_back(*p);
2575 dynpool->add((*p)->name(), false, NULL);
2576 }
2577
2578 return index;
2579 }
2580
2581 // Set the final values for all the symbols. The index of the first
2582 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
2583 // file offset OFF. Add their names to POOL. Return the new file
2584 // offset. Update *PLOCAL_SYMCOUNT if necessary.
2585
2586 off_t
2587 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2588 size_t dyncount, Stringpool* pool,
2589 unsigned int* plocal_symcount)
2590 {
2591 off_t ret;
2592
2593 gold_assert(*plocal_symcount != 0);
2594 this->first_global_index_ = *plocal_symcount;
2595
2596 this->dynamic_offset_ = dynoff;
2597 this->first_dynamic_global_index_ = dyn_global_index;
2598 this->dynamic_count_ = dyncount;
2599
2600 if (parameters->target().get_size() == 32)
2601 {
2602 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2603 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
2604 #else
2605 gold_unreachable();
2606 #endif
2607 }
2608 else if (parameters->target().get_size() == 64)
2609 {
2610 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2611 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
2612 #else
2613 gold_unreachable();
2614 #endif
2615 }
2616 else
2617 gold_unreachable();
2618
2619 // Now that we have the final symbol table, we can reliably note
2620 // which symbols should get warnings.
2621 this->warnings_.note_warnings(this);
2622
2623 return ret;
2624 }
2625
2626 // SYM is going into the symbol table at *PINDEX. Add the name to
2627 // POOL, update *PINDEX and *POFF.
2628
2629 template<int size>
2630 void
2631 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2632 unsigned int* pindex, off_t* poff)
2633 {
2634 sym->set_symtab_index(*pindex);
2635 if (sym->version() == NULL || !parameters->options().relocatable())
2636 pool->add(sym->name(), false, NULL);
2637 else
2638 pool->add(sym->versioned_name(), true, NULL);
2639 ++*pindex;
2640 *poff += elfcpp::Elf_sizes<size>::sym_size;
2641 }
2642
2643 // Set the final value for all the symbols. This is called after
2644 // Layout::finalize, so all the output sections have their final
2645 // address.
2646
2647 template<int size>
2648 off_t
2649 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2650 unsigned int* plocal_symcount)
2651 {
2652 off = align_address(off, size >> 3);
2653 this->offset_ = off;
2654
2655 unsigned int index = *plocal_symcount;
2656 const unsigned int orig_index = index;
2657
2658 // First do all the symbols which have been forced to be local, as
2659 // they must appear before all global symbols.
2660 for (Forced_locals::iterator p = this->forced_locals_.begin();
2661 p != this->forced_locals_.end();
2662 ++p)
2663 {
2664 Symbol* sym = *p;
2665 gold_assert(sym->is_forced_local());
2666 if (this->sized_finalize_symbol<size>(sym))
2667 {
2668 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2669 ++*plocal_symcount;
2670 }
2671 }
2672
2673 // Now do all the remaining symbols.
2674 for (Symbol_table_type::iterator p = this->table_.begin();
2675 p != this->table_.end();
2676 ++p)
2677 {
2678 Symbol* sym = p->second;
2679 if (this->sized_finalize_symbol<size>(sym))
2680 this->add_to_final_symtab<size>(sym, pool, &index, &off);
2681 }
2682
2683 // Now do target-specific symbols.
2684 for (std::vector<Symbol*>::iterator p = this->target_symbols_.begin();
2685 p != this->target_symbols_.end();
2686 ++p)
2687 {
2688 this->add_to_final_symtab<size>(*p, pool, &index, &off);
2689 }
2690
2691 this->output_count_ = index - orig_index;
2692
2693 return off;
2694 }
2695
2696 // Compute the final value of SYM and store status in location PSTATUS.
2697 // During relaxation, this may be called multiple times for a symbol to
2698 // compute its would-be final value in each relaxation pass.
2699
2700 template<int size>
2701 typename Sized_symbol<size>::Value_type
2702 Symbol_table::compute_final_value(
2703 const Sized_symbol<size>* sym,
2704 Compute_final_value_status* pstatus) const
2705 {
2706 typedef typename Sized_symbol<size>::Value_type Value_type;
2707 Value_type value;
2708
2709 switch (sym->source())
2710 {
2711 case Symbol::FROM_OBJECT:
2712 {
2713 bool is_ordinary;
2714 unsigned int shndx = sym->shndx(&is_ordinary);
2715
2716 if (!is_ordinary
2717 && shndx != elfcpp::SHN_ABS
2718 && !Symbol::is_common_shndx(shndx))
2719 {
2720 *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2721 return 0;
2722 }
2723
2724 Object* symobj = sym->object();
2725 if (symobj->is_dynamic())
2726 {
2727 value = 0;
2728 shndx = elfcpp::SHN_UNDEF;
2729 }
2730 else if (symobj->pluginobj() != NULL)
2731 {
2732 value = 0;
2733 shndx = elfcpp::SHN_UNDEF;
2734 }
2735 else if (shndx == elfcpp::SHN_UNDEF)
2736 value = 0;
2737 else if (!is_ordinary
2738 && (shndx == elfcpp::SHN_ABS
2739 || Symbol::is_common_shndx(shndx)))
2740 value = sym->value();
2741 else
2742 {
2743 Relobj* relobj = static_cast<Relobj*>(symobj);
2744 Output_section* os = relobj->output_section(shndx);
2745
2746 if (this->is_section_folded(relobj, shndx))
2747 {
2748 gold_assert(os == NULL);
2749 // Get the os of the section it is folded onto.
2750 Section_id folded = this->icf_->get_folded_section(relobj,
2751 shndx);
2752 gold_assert(folded.first != NULL);
2753 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
2754 unsigned folded_shndx = folded.second;
2755
2756 os = folded_obj->output_section(folded_shndx);
2757 gold_assert(os != NULL);
2758
2759 // Replace (relobj, shndx) with canonical ICF input section.
2760 shndx = folded_shndx;
2761 relobj = folded_obj;
2762 }
2763
2764 uint64_t secoff64 = relobj->output_section_offset(shndx);
2765 if (os == NULL)
2766 {
2767 bool static_or_reloc = (parameters->doing_static_link() ||
2768 parameters->options().relocatable());
2769 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2770
2771 *pstatus = CFVS_NO_OUTPUT_SECTION;
2772 return 0;
2773 }
2774
2775 if (secoff64 == -1ULL)
2776 {
2777 // The section needs special handling (e.g., a merge section).
2778
2779 value = os->output_address(relobj, shndx, sym->value());
2780 }
2781 else
2782 {
2783 Value_type secoff =
2784 convert_types<Value_type, uint64_t>(secoff64);
2785 if (sym->type() == elfcpp::STT_TLS)
2786 value = sym->value() + os->tls_offset() + secoff;
2787 else
2788 value = sym->value() + os->address() + secoff;
2789 }
2790 }
2791 }
2792 break;
2793
2794 case Symbol::IN_OUTPUT_DATA:
2795 {
2796 Output_data* od = sym->output_data();
2797 value = sym->value();
2798 if (sym->type() != elfcpp::STT_TLS)
2799 value += od->address();
2800 else
2801 {
2802 Output_section* os = od->output_section();
2803 gold_assert(os != NULL);
2804 value += os->tls_offset() + (od->address() - os->address());
2805 }
2806 if (sym->offset_is_from_end())
2807 value += od->data_size();
2808 }
2809 break;
2810
2811 case Symbol::IN_OUTPUT_SEGMENT:
2812 {
2813 Output_segment* os = sym->output_segment();
2814 value = sym->value();
2815 if (sym->type() != elfcpp::STT_TLS)
2816 value += os->vaddr();
2817 switch (sym->offset_base())
2818 {
2819 case Symbol::SEGMENT_START:
2820 break;
2821 case Symbol::SEGMENT_END:
2822 value += os->memsz();
2823 break;
2824 case Symbol::SEGMENT_BSS:
2825 value += os->filesz();
2826 break;
2827 default:
2828 gold_unreachable();
2829 }
2830 }
2831 break;
2832
2833 case Symbol::IS_CONSTANT:
2834 value = sym->value();
2835 break;
2836
2837 case Symbol::IS_UNDEFINED:
2838 value = 0;
2839 break;
2840
2841 default:
2842 gold_unreachable();
2843 }
2844
2845 *pstatus = CFVS_OK;
2846 return value;
2847 }
2848
2849 // Finalize the symbol SYM. This returns true if the symbol should be
2850 // added to the symbol table, false otherwise.
2851
2852 template<int size>
2853 bool
2854 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2855 {
2856 typedef typename Sized_symbol<size>::Value_type Value_type;
2857
2858 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2859
2860 // The default version of a symbol may appear twice in the symbol
2861 // table. We only need to finalize it once.
2862 if (sym->has_symtab_index())
2863 return false;
2864
2865 if (!sym->in_reg())
2866 {
2867 gold_assert(!sym->has_symtab_index());
2868 sym->set_symtab_index(-1U);
2869 gold_assert(sym->dynsym_index() == -1U);
2870 return false;
2871 }
2872
2873 // If the symbol is only present on plugin files, the plugin decided we
2874 // don't need it.
2875 if (!sym->in_real_elf())
2876 {
2877 gold_assert(!sym->has_symtab_index());
2878 sym->set_symtab_index(-1U);
2879 return false;
2880 }
2881
2882 // Compute final symbol value.
2883 Compute_final_value_status status;
2884 Value_type value = this->compute_final_value(sym, &status);
2885
2886 switch (status)
2887 {
2888 case CFVS_OK:
2889 break;
2890 case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2891 {
2892 bool is_ordinary;
2893 unsigned int shndx = sym->shndx(&is_ordinary);
2894 gold_error(_("%s: unsupported symbol section 0x%x"),
2895 sym->demangled_name().c_str(), shndx);
2896 }
2897 break;
2898 case CFVS_NO_OUTPUT_SECTION:
2899 sym->set_symtab_index(-1U);
2900 return false;
2901 default:
2902 gold_unreachable();
2903 }
2904
2905 sym->set_value(value);
2906
2907 if (parameters->options().strip_all()
2908 || !parameters->options().should_retain_symbol(sym->name()))
2909 {
2910 sym->set_symtab_index(-1U);
2911 return false;
2912 }
2913
2914 return true;
2915 }
2916
2917 // Write out the global symbols.
2918
2919 void
2920 Symbol_table::write_globals(const Stringpool* sympool,
2921 const Stringpool* dynpool,
2922 Output_symtab_xindex* symtab_xindex,
2923 Output_symtab_xindex* dynsym_xindex,
2924 Output_file* of) const
2925 {
2926 switch (parameters->size_and_endianness())
2927 {
2928 #ifdef HAVE_TARGET_32_LITTLE
2929 case Parameters::TARGET_32_LITTLE:
2930 this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
2931 dynsym_xindex, of);
2932 break;
2933 #endif
2934 #ifdef HAVE_TARGET_32_BIG
2935 case Parameters::TARGET_32_BIG:
2936 this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
2937 dynsym_xindex, of);
2938 break;
2939 #endif
2940 #ifdef HAVE_TARGET_64_LITTLE
2941 case Parameters::TARGET_64_LITTLE:
2942 this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
2943 dynsym_xindex, of);
2944 break;
2945 #endif
2946 #ifdef HAVE_TARGET_64_BIG
2947 case Parameters::TARGET_64_BIG:
2948 this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
2949 dynsym_xindex, of);
2950 break;
2951 #endif
2952 default:
2953 gold_unreachable();
2954 }
2955 }
2956
2957 // Write out the global symbols.
2958
2959 template<int size, bool big_endian>
2960 void
2961 Symbol_table::sized_write_globals(const Stringpool* sympool,
2962 const Stringpool* dynpool,
2963 Output_symtab_xindex* symtab_xindex,
2964 Output_symtab_xindex* dynsym_xindex,
2965 Output_file* of) const
2966 {
2967 const Target& target = parameters->target();
2968
2969 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2970
2971 const unsigned int output_count = this->output_count_;
2972 const section_size_type oview_size = output_count * sym_size;
2973 const unsigned int first_global_index = this->first_global_index_;
2974 unsigned char* psyms;
2975 if (this->offset_ == 0 || output_count == 0)
2976 psyms = NULL;
2977 else
2978 psyms = of->get_output_view(this->offset_, oview_size);
2979
2980 const unsigned int dynamic_count = this->dynamic_count_;
2981 const section_size_type dynamic_size = dynamic_count * sym_size;
2982 const unsigned int first_dynamic_global_index =
2983 this->first_dynamic_global_index_;
2984 unsigned char* dynamic_view;
2985 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2986 dynamic_view = NULL;
2987 else
2988 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2989
2990 for (Symbol_table_type::const_iterator p = this->table_.begin();
2991 p != this->table_.end();
2992 ++p)
2993 {
2994 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2995
2996 // Possibly warn about unresolved symbols in shared libraries.
2997 this->warn_about_undefined_dynobj_symbol(sym);
2998
2999 unsigned int sym_index = sym->symtab_index();
3000 unsigned int dynsym_index;
3001 if (dynamic_view == NULL)
3002 dynsym_index = -1U;
3003 else
3004 dynsym_index = sym->dynsym_index();
3005
3006 if (sym_index == -1U && dynsym_index == -1U)
3007 {
3008 // This symbol is not included in the output file.
3009 continue;
3010 }
3011
3012 unsigned int shndx;
3013 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
3014 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
3015 elfcpp::STB binding = sym->binding();
3016
3017 // If --weak-unresolved-symbols is set, change binding of unresolved
3018 // global symbols to STB_WEAK.
3019 if (parameters->options().weak_unresolved_symbols()
3020 && binding == elfcpp::STB_GLOBAL
3021 && sym->is_undefined())
3022 binding = elfcpp::STB_WEAK;
3023
3024 // If --no-gnu-unique is set, change STB_GNU_UNIQUE to STB_GLOBAL.
3025 if (binding == elfcpp::STB_GNU_UNIQUE
3026 && !parameters->options().gnu_unique())
3027 binding = elfcpp::STB_GLOBAL;
3028
3029 switch (sym->source())
3030 {
3031 case Symbol::FROM_OBJECT:
3032 {
3033 bool is_ordinary;
3034 unsigned int in_shndx = sym->shndx(&is_ordinary);
3035
3036 if (!is_ordinary
3037 && in_shndx != elfcpp::SHN_ABS
3038 && !Symbol::is_common_shndx(in_shndx))
3039 {
3040 gold_error(_("%s: unsupported symbol section 0x%x"),
3041 sym->demangled_name().c_str(), in_shndx);
3042 shndx = in_shndx;
3043 }
3044 else
3045 {
3046 Object* symobj = sym->object();
3047 if (symobj->is_dynamic())
3048 {
3049 if (sym->needs_dynsym_value())
3050 dynsym_value = target.dynsym_value(sym);
3051 shndx = elfcpp::SHN_UNDEF;
3052 if (sym->is_undef_binding_weak())
3053 binding = elfcpp::STB_WEAK;
3054 else
3055 binding = elfcpp::STB_GLOBAL;
3056 }
3057 else if (symobj->pluginobj() != NULL)
3058 shndx = elfcpp::SHN_UNDEF;
3059 else if (in_shndx == elfcpp::SHN_UNDEF
3060 || (!is_ordinary
3061 && (in_shndx == elfcpp::SHN_ABS
3062 || Symbol::is_common_shndx(in_shndx))))
3063 shndx = in_shndx;
3064 else
3065 {
3066 Relobj* relobj = static_cast<Relobj*>(symobj);
3067 Output_section* os = relobj->output_section(in_shndx);
3068 if (this->is_section_folded(relobj, in_shndx))
3069 {
3070 // This global symbol must be written out even though
3071 // it is folded.
3072 // Get the os of the section it is folded onto.
3073 Section_id folded =
3074 this->icf_->get_folded_section(relobj, in_shndx);
3075 gold_assert(folded.first !=NULL);
3076 Relobj* folded_obj =
3077 reinterpret_cast<Relobj*>(folded.first);
3078 os = folded_obj->output_section(folded.second);
3079 gold_assert(os != NULL);
3080 }
3081 gold_assert(os != NULL);
3082 shndx = os->out_shndx();
3083
3084 if (shndx >= elfcpp::SHN_LORESERVE)
3085 {
3086 if (sym_index != -1U)
3087 symtab_xindex->add(sym_index, shndx);
3088 if (dynsym_index != -1U)
3089 dynsym_xindex->add(dynsym_index, shndx);
3090 shndx = elfcpp::SHN_XINDEX;
3091 }
3092
3093 // In object files symbol values are section
3094 // relative.
3095 if (parameters->options().relocatable())
3096 sym_value -= os->address();
3097 }
3098 }
3099 }
3100 break;
3101
3102 case Symbol::IN_OUTPUT_DATA:
3103 {
3104 Output_data* od = sym->output_data();
3105
3106 shndx = od->out_shndx();
3107 if (shndx >= elfcpp::SHN_LORESERVE)
3108 {
3109 if (sym_index != -1U)
3110 symtab_xindex->add(sym_index, shndx);
3111 if (dynsym_index != -1U)
3112 dynsym_xindex->add(dynsym_index, shndx);
3113 shndx = elfcpp::SHN_XINDEX;
3114 }
3115
3116 // In object files symbol values are section
3117 // relative.
3118 if (parameters->options().relocatable())
3119 {
3120 Output_section* os = od->output_section();
3121 gold_assert(os != NULL);
3122 sym_value -= os->address();
3123 }
3124 }
3125 break;
3126
3127 case Symbol::IN_OUTPUT_SEGMENT:
3128 {
3129 Output_segment* oseg = sym->output_segment();
3130 Output_section* osect = oseg->first_section();
3131 if (osect == NULL)
3132 shndx = elfcpp::SHN_ABS;
3133 else
3134 shndx = osect->out_shndx();
3135 }
3136 break;
3137
3138 case Symbol::IS_CONSTANT:
3139 shndx = elfcpp::SHN_ABS;
3140 break;
3141
3142 case Symbol::IS_UNDEFINED:
3143 shndx = elfcpp::SHN_UNDEF;
3144 break;
3145
3146 default:
3147 gold_unreachable();
3148 }
3149
3150 if (sym_index != -1U)
3151 {
3152 sym_index -= first_global_index;
3153 gold_assert(sym_index < output_count);
3154 unsigned char* ps = psyms + (sym_index * sym_size);
3155 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
3156 binding, sympool, ps);
3157 }
3158
3159 if (dynsym_index != -1U)
3160 {
3161 dynsym_index -= first_dynamic_global_index;
3162 gold_assert(dynsym_index < dynamic_count);
3163 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
3164 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
3165 binding, dynpool, pd);
3166 // Allow a target to adjust dynamic symbol value.
3167 parameters->target().adjust_dyn_symbol(sym, pd);
3168 }
3169 }
3170
3171 // Write the target-specific symbols.
3172 for (std::vector<Symbol*>::const_iterator p = this->target_symbols_.begin();
3173 p != this->target_symbols_.end();
3174 ++p)
3175 {
3176 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(*p);
3177
3178 unsigned int sym_index = sym->symtab_index();
3179 unsigned int dynsym_index;
3180 if (dynamic_view == NULL)
3181 dynsym_index = -1U;
3182 else
3183 dynsym_index = sym->dynsym_index();
3184
3185 unsigned int shndx;
3186 switch (sym->source())
3187 {
3188 case Symbol::IS_CONSTANT:
3189 shndx = elfcpp::SHN_ABS;
3190 break;
3191 case Symbol::IS_UNDEFINED:
3192 shndx = elfcpp::SHN_UNDEF;
3193 break;
3194 default:
3195 gold_unreachable();
3196 }
3197
3198 if (sym_index != -1U)
3199 {
3200 sym_index -= first_global_index;
3201 gold_assert(sym_index < output_count);
3202 unsigned char* ps = psyms + (sym_index * sym_size);
3203 this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
3204 sym->binding(), sympool,
3205 ps);
3206 }
3207
3208 if (dynsym_index != -1U)
3209 {
3210 dynsym_index -= first_dynamic_global_index;
3211 gold_assert(dynsym_index < dynamic_count);
3212 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
3213 this->sized_write_symbol<size, big_endian>(sym, sym->value(), shndx,
3214 sym->binding(), dynpool,
3215 pd);
3216 }
3217 }
3218
3219 of->write_output_view(this->offset_, oview_size, psyms);
3220 if (dynamic_view != NULL)
3221 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
3222 }
3223
3224 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
3225 // strtab holding the name.
3226
3227 template<int size, bool big_endian>
3228 void
3229 Symbol_table::sized_write_symbol(
3230 Sized_symbol<size>* sym,
3231 typename elfcpp::Elf_types<size>::Elf_Addr value,
3232 unsigned int shndx,
3233 elfcpp::STB binding,
3234 const Stringpool* pool,
3235 unsigned char* p) const
3236 {
3237 elfcpp::Sym_write<size, big_endian> osym(p);
3238 if (sym->version() == NULL || !parameters->options().relocatable())
3239 osym.put_st_name(pool->get_offset(sym->name()));
3240 else
3241 osym.put_st_name(pool->get_offset(sym->versioned_name()));
3242 osym.put_st_value(value);
3243 // Use a symbol size of zero for undefined symbols from shared libraries.
3244 if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
3245 osym.put_st_size(0);
3246 else
3247 osym.put_st_size(sym->symsize());
3248 elfcpp::STT type = sym->type();
3249 gold_assert(type != elfcpp::STT_GNU_IFUNC || !sym->is_from_dynobj());
3250 // A version script may have overridden the default binding.
3251 if (sym->is_forced_local())
3252 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
3253 else
3254 osym.put_st_info(elfcpp::elf_st_info(binding, type));
3255 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
3256 osym.put_st_shndx(shndx);
3257 }
3258
3259 // Check for unresolved symbols in shared libraries. This is
3260 // controlled by the --allow-shlib-undefined option.
3261
3262 // We only warn about libraries for which we have seen all the
3263 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
3264 // which were not seen in this link. If we didn't see a DT_NEEDED
3265 // entry, we aren't going to be able to reliably report whether the
3266 // symbol is undefined.
3267
3268 // We also don't warn about libraries found in a system library
3269 // directory (e.g., /lib or /usr/lib); we assume that those libraries
3270 // are OK. This heuristic avoids problems on GNU/Linux, in which -ldl
3271 // can have undefined references satisfied by ld-linux.so.
3272
3273 inline void
3274 Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
3275 {
3276 bool dummy;
3277 if (sym->source() == Symbol::FROM_OBJECT
3278 && sym->object()->is_dynamic()
3279 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
3280 && sym->binding() != elfcpp::STB_WEAK
3281 && !parameters->options().allow_shlib_undefined()
3282 && !parameters->target().is_defined_by_abi(sym)
3283 && !sym->object()->is_in_system_directory())
3284 {
3285 // A very ugly cast.
3286 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
3287 if (!dynobj->has_unknown_needed_entries())
3288 gold_undefined_symbol(sym);
3289 }
3290 }
3291
3292 // Write out a section symbol. Return the update offset.
3293
3294 void
3295 Symbol_table::write_section_symbol(const Output_section* os,
3296 Output_symtab_xindex* symtab_xindex,
3297 Output_file* of,
3298 off_t offset) const
3299 {
3300 switch (parameters->size_and_endianness())
3301 {
3302 #ifdef HAVE_TARGET_32_LITTLE
3303 case Parameters::TARGET_32_LITTLE:
3304 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
3305 offset);
3306 break;
3307 #endif
3308 #ifdef HAVE_TARGET_32_BIG
3309 case Parameters::TARGET_32_BIG:
3310 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
3311 offset);
3312 break;
3313 #endif
3314 #ifdef HAVE_TARGET_64_LITTLE
3315 case Parameters::TARGET_64_LITTLE:
3316 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
3317 offset);
3318 break;
3319 #endif
3320 #ifdef HAVE_TARGET_64_BIG
3321 case Parameters::TARGET_64_BIG:
3322 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
3323 offset);
3324 break;
3325 #endif
3326 default:
3327 gold_unreachable();
3328 }
3329 }
3330
3331 // Write out a section symbol, specialized for size and endianness.
3332
3333 template<int size, bool big_endian>
3334 void
3335 Symbol_table::sized_write_section_symbol(const Output_section* os,
3336 Output_symtab_xindex* symtab_xindex,
3337 Output_file* of,
3338 off_t offset) const
3339 {
3340 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
3341
3342 unsigned char* pov = of->get_output_view(offset, sym_size);
3343
3344 elfcpp::Sym_write<size, big_endian> osym(pov);
3345 osym.put_st_name(0);
3346 if (parameters->options().relocatable())
3347 osym.put_st_value(0);
3348 else
3349 osym.put_st_value(os->address());
3350 osym.put_st_size(0);
3351 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
3352 elfcpp::STT_SECTION));
3353 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
3354
3355 unsigned int shndx = os->out_shndx();
3356 if (shndx >= elfcpp::SHN_LORESERVE)
3357 {
3358 symtab_xindex->add(os->symtab_index(), shndx);
3359 shndx = elfcpp::SHN_XINDEX;
3360 }
3361 osym.put_st_shndx(shndx);
3362
3363 of->write_output_view(offset, sym_size, pov);
3364 }
3365
3366 // Print statistical information to stderr. This is used for --stats.
3367
3368 void
3369 Symbol_table::print_stats() const
3370 {
3371 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
3372 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
3373 program_name, this->table_.size(), this->table_.bucket_count());
3374 #else
3375 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
3376 program_name, this->table_.size());
3377 #endif
3378 this->namepool_.print_stats("symbol table stringpool");
3379 }
3380
3381 // We check for ODR violations by looking for symbols with the same
3382 // name for which the debugging information reports that they were
3383 // defined in disjoint source locations. When comparing the source
3384 // location, we consider instances with the same base filename to be
3385 // the same. This is because different object files/shared libraries
3386 // can include the same header file using different paths, and
3387 // different optimization settings can make the line number appear to
3388 // be a couple lines off, and we don't want to report an ODR violation
3389 // in those cases.
3390
3391 // This struct is used to compare line information, as returned by
3392 // Dwarf_line_info::one_addr2line. It implements a < comparison
3393 // operator used with std::sort.
3394
3395 struct Odr_violation_compare
3396 {
3397 bool
3398 operator()(const std::string& s1, const std::string& s2) const
3399 {
3400 // Inputs should be of the form "dirname/filename:linenum" where
3401 // "dirname/" is optional. We want to compare just the filename:linenum.
3402
3403 // Find the last '/' in each string.
3404 std::string::size_type s1begin = s1.rfind('/');
3405 std::string::size_type s2begin = s2.rfind('/');
3406 // If there was no '/' in a string, start at the beginning.
3407 if (s1begin == std::string::npos)
3408 s1begin = 0;
3409 if (s2begin == std::string::npos)
3410 s2begin = 0;
3411 return s1.compare(s1begin, std::string::npos,
3412 s2, s2begin, std::string::npos) < 0;
3413 }
3414 };
3415
3416 // Returns all of the lines attached to LOC, not just the one the
3417 // instruction actually came from.
3418 std::vector<std::string>
3419 Symbol_table::linenos_from_loc(const Task* task,
3420 const Symbol_location& loc)
3421 {
3422 // We need to lock the object in order to read it. This
3423 // means that we have to run in a singleton Task. If we
3424 // want to run this in a general Task for better
3425 // performance, we will need one Task for object, plus
3426 // appropriate locking to ensure that we don't conflict with
3427 // other uses of the object. Also note, one_addr2line is not
3428 // currently thread-safe.
3429 Task_lock_obj<Object> tl(task, loc.object);
3430
3431 std::vector<std::string> result;
3432 Symbol_location code_loc = loc;
3433 parameters->target().function_location(&code_loc);
3434 // 16 is the size of the object-cache that one_addr2line should use.
3435 std::string canonical_result = Dwarf_line_info::one_addr2line(
3436 code_loc.object, code_loc.shndx, code_loc.offset, 16, &result);
3437 if (!canonical_result.empty())
3438 result.push_back(canonical_result);
3439 return result;
3440 }
3441
3442 // OutputIterator that records if it was ever assigned to. This
3443 // allows it to be used with std::set_intersection() to check for
3444 // intersection rather than computing the intersection.
3445 struct Check_intersection
3446 {
3447 Check_intersection()
3448 : value_(false)
3449 {}
3450
3451 bool had_intersection() const
3452 { return this->value_; }
3453
3454 Check_intersection& operator++()
3455 { return *this; }
3456
3457 Check_intersection& operator*()
3458 { return *this; }
3459
3460 template<typename T>
3461 Check_intersection& operator=(const T&)
3462 {
3463 this->value_ = true;
3464 return *this;
3465 }
3466
3467 private:
3468 bool value_;
3469 };
3470
3471 // Check candidate_odr_violations_ to find symbols with the same name
3472 // but apparently different definitions (different source-file/line-no
3473 // for each line assigned to the first instruction).
3474
3475 void
3476 Symbol_table::detect_odr_violations(const Task* task,
3477 const char* output_file_name) const
3478 {
3479 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3480 it != candidate_odr_violations_.end();
3481 ++it)
3482 {
3483 const char* const symbol_name = it->first;
3484
3485 std::string first_object_name;
3486 std::vector<std::string> first_object_linenos;
3487
3488 Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3489 locs = it->second.begin();
3490 const Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3491 locs_end = it->second.end();
3492 for (; locs != locs_end && first_object_linenos.empty(); ++locs)
3493 {
3494 // Save the line numbers from the first definition to
3495 // compare to the other definitions. Ideally, we'd compare
3496 // every definition to every other, but we don't want to
3497 // take O(N^2) time to do this. This shortcut may cause
3498 // false negatives that appear or disappear depending on the
3499 // link order, but it won't cause false positives.
3500 first_object_name = locs->object->name();
3501 first_object_linenos = this->linenos_from_loc(task, *locs);
3502 }
3503 if (first_object_linenos.empty())
3504 continue;
3505
3506 // Sort by Odr_violation_compare to make std::set_intersection work.
3507 std::string first_object_canonical_result = first_object_linenos.back();
3508 std::sort(first_object_linenos.begin(), first_object_linenos.end(),
3509 Odr_violation_compare());
3510
3511 for (; locs != locs_end; ++locs)
3512 {
3513 std::vector<std::string> linenos =
3514 this->linenos_from_loc(task, *locs);
3515 // linenos will be empty if we couldn't parse the debug info.
3516 if (linenos.empty())
3517 continue;
3518 // Sort by Odr_violation_compare to make std::set_intersection work.
3519 gold_assert(!linenos.empty());
3520 std::string second_object_canonical_result = linenos.back();
3521 std::sort(linenos.begin(), linenos.end(), Odr_violation_compare());
3522
3523 Check_intersection intersection_result =
3524 std::set_intersection(first_object_linenos.begin(),
3525 first_object_linenos.end(),
3526 linenos.begin(),
3527 linenos.end(),
3528 Check_intersection(),
3529 Odr_violation_compare());
3530 if (!intersection_result.had_intersection())
3531 {
3532 gold_warning(_("while linking %s: symbol '%s' defined in "
3533 "multiple places (possible ODR violation):"),
3534 output_file_name, demangle(symbol_name).c_str());
3535 // This only prints one location from each definition,
3536 // which may not be the location we expect to intersect
3537 // with another definition. We could print the whole
3538 // set of locations, but that seems too verbose.
3539 fprintf(stderr, _(" %s from %s\n"),
3540 first_object_canonical_result.c_str(),
3541 first_object_name.c_str());
3542 fprintf(stderr, _(" %s from %s\n"),
3543 second_object_canonical_result.c_str(),
3544 locs->object->name().c_str());
3545 // Only print one broken pair, to avoid needing to
3546 // compare against a list of the disjoint definition
3547 // locations we've found so far. (If we kept comparing
3548 // against just the first one, we'd get a lot of
3549 // redundant complaints about the second definition
3550 // location.)
3551 break;
3552 }
3553 }
3554 }
3555 // We only call one_addr2line() in this function, so we can clear its cache.
3556 Dwarf_line_info::clear_addr2line_cache();
3557 }
3558
3559 // Warnings functions.
3560
3561 // Add a new warning.
3562
3563 void
3564 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
3565 const std::string& warning)
3566 {
3567 name = symtab->canonicalize_name(name);
3568 this->warnings_[name].set(obj, warning);
3569 }
3570
3571 // Look through the warnings and mark the symbols for which we should
3572 // warn. This is called during Layout::finalize when we know the
3573 // sources for all the symbols.
3574
3575 void
3576 Warnings::note_warnings(Symbol_table* symtab)
3577 {
3578 for (Warning_table::iterator p = this->warnings_.begin();
3579 p != this->warnings_.end();
3580 ++p)
3581 {
3582 Symbol* sym = symtab->lookup(p->first, NULL);
3583 if (sym != NULL
3584 && sym->source() == Symbol::FROM_OBJECT
3585 && sym->object() == p->second.object)
3586 sym->set_has_warning();
3587 }
3588 }
3589
3590 // Issue a warning. This is called when we see a relocation against a
3591 // symbol for which has a warning.
3592
3593 template<int size, bool big_endian>
3594 void
3595 Warnings::issue_warning(const Symbol* sym,
3596 const Relocate_info<size, big_endian>* relinfo,
3597 size_t relnum, off_t reloffset) const
3598 {
3599 gold_assert(sym->has_warning());
3600
3601 // We don't want to issue a warning for a relocation against the
3602 // symbol in the same object file in which the symbol is defined.
3603 if (sym->object() == relinfo->object)
3604 return;
3605
3606 Warning_table::const_iterator p = this->warnings_.find(sym->name());
3607 gold_assert(p != this->warnings_.end());
3608 gold_warning_at_location(relinfo, relnum, reloffset,
3609 "%s", p->second.text.c_str());
3610 }
3611
3612 // Instantiate the templates we need. We could use the configure
3613 // script to restrict this to only the ones needed for implemented
3614 // targets.
3615
3616 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3617 template
3618 void
3619 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3620 #endif
3621
3622 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3623 template
3624 void
3625 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3626 #endif
3627
3628 #ifdef HAVE_TARGET_32_LITTLE
3629 template
3630 void
3631 Symbol_table::add_from_relobj<32, false>(
3632 Sized_relobj_file<32, false>* relobj,
3633 const unsigned char* syms,
3634 size_t count,
3635 size_t symndx_offset,
3636 const char* sym_names,
3637 size_t sym_name_size,
3638 Sized_relobj_file<32, false>::Symbols* sympointers,
3639 size_t* defined);
3640 #endif
3641
3642 #ifdef HAVE_TARGET_32_BIG
3643 template
3644 void
3645 Symbol_table::add_from_relobj<32, true>(
3646 Sized_relobj_file<32, true>* relobj,
3647 const unsigned char* syms,
3648 size_t count,
3649 size_t symndx_offset,
3650 const char* sym_names,
3651 size_t sym_name_size,
3652 Sized_relobj_file<32, true>::Symbols* sympointers,
3653 size_t* defined);
3654 #endif
3655
3656 #ifdef HAVE_TARGET_64_LITTLE
3657 template
3658 void
3659 Symbol_table::add_from_relobj<64, false>(
3660 Sized_relobj_file<64, false>* relobj,
3661 const unsigned char* syms,
3662 size_t count,
3663 size_t symndx_offset,
3664 const char* sym_names,
3665 size_t sym_name_size,
3666 Sized_relobj_file<64, false>::Symbols* sympointers,
3667 size_t* defined);
3668 #endif
3669
3670 #ifdef HAVE_TARGET_64_BIG
3671 template
3672 void
3673 Symbol_table::add_from_relobj<64, true>(
3674 Sized_relobj_file<64, true>* relobj,
3675 const unsigned char* syms,
3676 size_t count,
3677 size_t symndx_offset,
3678 const char* sym_names,
3679 size_t sym_name_size,
3680 Sized_relobj_file<64, true>::Symbols* sympointers,
3681 size_t* defined);
3682 #endif
3683
3684 #ifdef HAVE_TARGET_32_LITTLE
3685 template
3686 Symbol*
3687 Symbol_table::add_from_pluginobj<32, false>(
3688 Sized_pluginobj<32, false>* obj,
3689 const char* name,
3690 const char* ver,
3691 elfcpp::Sym<32, false>* sym);
3692 #endif
3693
3694 #ifdef HAVE_TARGET_32_BIG
3695 template
3696 Symbol*
3697 Symbol_table::add_from_pluginobj<32, true>(
3698 Sized_pluginobj<32, true>* obj,
3699 const char* name,
3700 const char* ver,
3701 elfcpp::Sym<32, true>* sym);
3702 #endif
3703
3704 #ifdef HAVE_TARGET_64_LITTLE
3705 template
3706 Symbol*
3707 Symbol_table::add_from_pluginobj<64, false>(
3708 Sized_pluginobj<64, false>* obj,
3709 const char* name,
3710 const char* ver,
3711 elfcpp::Sym<64, false>* sym);
3712 #endif
3713
3714 #ifdef HAVE_TARGET_64_BIG
3715 template
3716 Symbol*
3717 Symbol_table::add_from_pluginobj<64, true>(
3718 Sized_pluginobj<64, true>* obj,
3719 const char* name,
3720 const char* ver,
3721 elfcpp::Sym<64, true>* sym);
3722 #endif
3723
3724 #ifdef HAVE_TARGET_32_LITTLE
3725 template
3726 void
3727 Symbol_table::add_from_dynobj<32, false>(
3728 Sized_dynobj<32, false>* dynobj,
3729 const unsigned char* syms,
3730 size_t count,
3731 const char* sym_names,
3732 size_t sym_name_size,
3733 const unsigned char* versym,
3734 size_t versym_size,
3735 const std::vector<const char*>* version_map,
3736 Sized_relobj_file<32, false>::Symbols* sympointers,
3737 size_t* defined);
3738 #endif
3739
3740 #ifdef HAVE_TARGET_32_BIG
3741 template
3742 void
3743 Symbol_table::add_from_dynobj<32, true>(
3744 Sized_dynobj<32, true>* dynobj,
3745 const unsigned char* syms,
3746 size_t count,
3747 const char* sym_names,
3748 size_t sym_name_size,
3749 const unsigned char* versym,
3750 size_t versym_size,
3751 const std::vector<const char*>* version_map,
3752 Sized_relobj_file<32, true>::Symbols* sympointers,
3753 size_t* defined);
3754 #endif
3755
3756 #ifdef HAVE_TARGET_64_LITTLE
3757 template
3758 void
3759 Symbol_table::add_from_dynobj<64, false>(
3760 Sized_dynobj<64, false>* dynobj,
3761 const unsigned char* syms,
3762 size_t count,
3763 const char* sym_names,
3764 size_t sym_name_size,
3765 const unsigned char* versym,
3766 size_t versym_size,
3767 const std::vector<const char*>* version_map,
3768 Sized_relobj_file<64, false>::Symbols* sympointers,
3769 size_t* defined);
3770 #endif
3771
3772 #ifdef HAVE_TARGET_64_BIG
3773 template
3774 void
3775 Symbol_table::add_from_dynobj<64, true>(
3776 Sized_dynobj<64, true>* dynobj,
3777 const unsigned char* syms,
3778 size_t count,
3779 const char* sym_names,
3780 size_t sym_name_size,
3781 const unsigned char* versym,
3782 size_t versym_size,
3783 const std::vector<const char*>* version_map,
3784 Sized_relobj_file<64, true>::Symbols* sympointers,
3785 size_t* defined);
3786 #endif
3787
3788 #ifdef HAVE_TARGET_32_LITTLE
3789 template
3790 Sized_symbol<32>*
3791 Symbol_table::add_from_incrobj(
3792 Object* obj,
3793 const char* name,
3794 const char* ver,
3795 elfcpp::Sym<32, false>* sym);
3796 #endif
3797
3798 #ifdef HAVE_TARGET_32_BIG
3799 template
3800 Sized_symbol<32>*
3801 Symbol_table::add_from_incrobj(
3802 Object* obj,
3803 const char* name,
3804 const char* ver,
3805 elfcpp::Sym<32, true>* sym);
3806 #endif
3807
3808 #ifdef HAVE_TARGET_64_LITTLE
3809 template
3810 Sized_symbol<64>*
3811 Symbol_table::add_from_incrobj(
3812 Object* obj,
3813 const char* name,
3814 const char* ver,
3815 elfcpp::Sym<64, false>* sym);
3816 #endif
3817
3818 #ifdef HAVE_TARGET_64_BIG
3819 template
3820 Sized_symbol<64>*
3821 Symbol_table::add_from_incrobj(
3822 Object* obj,
3823 const char* name,
3824 const char* ver,
3825 elfcpp::Sym<64, true>* sym);
3826 #endif
3827
3828 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3829 template
3830 void
3831 Symbol_table::define_with_copy_reloc<32>(
3832 Sized_symbol<32>* sym,
3833 Output_data* posd,
3834 elfcpp::Elf_types<32>::Elf_Addr value);
3835 #endif
3836
3837 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3838 template
3839 void
3840 Symbol_table::define_with_copy_reloc<64>(
3841 Sized_symbol<64>* sym,
3842 Output_data* posd,
3843 elfcpp::Elf_types<64>::Elf_Addr value);
3844 #endif
3845
3846 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3847 template
3848 void
3849 Sized_symbol<32>::init_output_data(const char* name, const char* version,
3850 Output_data* od, Value_type value,
3851 Size_type symsize, elfcpp::STT type,
3852 elfcpp::STB binding,
3853 elfcpp::STV visibility,
3854 unsigned char nonvis,
3855 bool offset_is_from_end,
3856 bool is_predefined);
3857
3858 template
3859 void
3860 Sized_symbol<32>::init_constant(const char* name, const char* version,
3861 Value_type value, Size_type symsize,
3862 elfcpp::STT type, elfcpp::STB binding,
3863 elfcpp::STV visibility, unsigned char nonvis,
3864 bool is_predefined);
3865
3866 template
3867 void
3868 Sized_symbol<32>::init_undefined(const char* name, const char* version,
3869 Value_type value, elfcpp::STT type,
3870 elfcpp::STB binding, elfcpp::STV visibility,
3871 unsigned char nonvis);
3872 #endif
3873
3874 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3875 template
3876 void
3877 Sized_symbol<64>::init_output_data(const char* name, const char* version,
3878 Output_data* od, Value_type value,
3879 Size_type symsize, elfcpp::STT type,
3880 elfcpp::STB binding,
3881 elfcpp::STV visibility,
3882 unsigned char nonvis,
3883 bool offset_is_from_end,
3884 bool is_predefined);
3885
3886 template
3887 void
3888 Sized_symbol<64>::init_constant(const char* name, const char* version,
3889 Value_type value, Size_type symsize,
3890 elfcpp::STT type, elfcpp::STB binding,
3891 elfcpp::STV visibility, unsigned char nonvis,
3892 bool is_predefined);
3893
3894 template
3895 void
3896 Sized_symbol<64>::init_undefined(const char* name, const char* version,
3897 Value_type value, elfcpp::STT type,
3898 elfcpp::STB binding, elfcpp::STV visibility,
3899 unsigned char nonvis);
3900 #endif
3901
3902 #ifdef HAVE_TARGET_32_LITTLE
3903 template
3904 void
3905 Warnings::issue_warning<32, false>(const Symbol* sym,
3906 const Relocate_info<32, false>* relinfo,
3907 size_t relnum, off_t reloffset) const;
3908 #endif
3909
3910 #ifdef HAVE_TARGET_32_BIG
3911 template
3912 void
3913 Warnings::issue_warning<32, true>(const Symbol* sym,
3914 const Relocate_info<32, true>* relinfo,
3915 size_t relnum, off_t reloffset) const;
3916 #endif
3917
3918 #ifdef HAVE_TARGET_64_LITTLE
3919 template
3920 void
3921 Warnings::issue_warning<64, false>(const Symbol* sym,
3922 const Relocate_info<64, false>* relinfo,
3923 size_t relnum, off_t reloffset) const;
3924 #endif
3925
3926 #ifdef HAVE_TARGET_64_BIG
3927 template
3928 void
3929 Warnings::issue_warning<64, true>(const Symbol* sym,
3930 const Relocate_info<64, true>* relinfo,
3931 size_t relnum, off_t reloffset) const;
3932 #endif
3933
3934 } // End namespace gold.
This page took 0.15502 seconds and 5 git commands to generate.