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