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