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