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