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