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