* cref.cc: New file.
[deliverable/binutils-gdb.git] / gold / symtab.cc
1 // symtab.cc -- the gold symbol table
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26 #include <stdint.h>
27 #include <algorithm>
28 #include <set>
29 #include <string>
30 #include <utility>
31 #include "demangle.h"
32
33 #include "object.h"
34 #include "dwarf_reader.h"
35 #include "dynobj.h"
36 #include "output.h"
37 #include "target.h"
38 #include "workqueue.h"
39 #include "symtab.h"
40
41 namespace gold
42 {
43
44 // Class Symbol.
45
46 // Initialize fields in Symbol. This initializes everything except u_
47 // and source_.
48
49 void
50 Symbol::init_fields(const char* name, const char* version,
51 elfcpp::STT type, elfcpp::STB binding,
52 elfcpp::STV visibility, unsigned char nonvis)
53 {
54 this->name_ = name;
55 this->version_ = version;
56 this->symtab_index_ = 0;
57 this->dynsym_index_ = 0;
58 this->got_offsets_.init();
59 this->plt_offset_ = 0;
60 this->type_ = type;
61 this->binding_ = binding;
62 this->visibility_ = visibility;
63 this->nonvis_ = nonvis;
64 this->is_target_special_ = false;
65 this->is_def_ = false;
66 this->is_forwarder_ = false;
67 this->has_alias_ = false;
68 this->needs_dynsym_entry_ = false;
69 this->in_reg_ = false;
70 this->in_dyn_ = false;
71 this->has_plt_offset_ = false;
72 this->has_warning_ = false;
73 this->is_copied_from_dynobj_ = false;
74 this->is_forced_local_ = false;
75 this->is_ordinary_shndx_ = false;
76 }
77
78 // Return the demangled version of the symbol's name, but only
79 // if the --demangle flag was set.
80
81 static std::string
82 demangle(const char* name)
83 {
84 if (!parameters->options().do_demangle())
85 return name;
86
87 // cplus_demangle allocates memory for the result it returns,
88 // and returns NULL if the name is already demangled.
89 char* demangled_name = cplus_demangle(name, DMGL_ANSI | DMGL_PARAMS);
90 if (demangled_name == NULL)
91 return name;
92
93 std::string retval(demangled_name);
94 free(demangled_name);
95 return retval;
96 }
97
98 std::string
99 Symbol::demangled_name() const
100 {
101 return demangle(this->name());
102 }
103
104 // Initialize the fields in the base class Symbol for SYM in OBJECT.
105
106 template<int size, bool big_endian>
107 void
108 Symbol::init_base_object(const char* name, const char* version, Object* object,
109 const elfcpp::Sym<size, big_endian>& sym,
110 unsigned int st_shndx, bool is_ordinary)
111 {
112 this->init_fields(name, version, sym.get_st_type(), sym.get_st_bind(),
113 sym.get_st_visibility(), sym.get_st_nonvis());
114 this->u_.from_object.object = object;
115 this->u_.from_object.shndx = st_shndx;
116 this->is_ordinary_shndx_ = is_ordinary;
117 this->source_ = FROM_OBJECT;
118 this->in_reg_ = !object->is_dynamic();
119 this->in_dyn_ = object->is_dynamic();
120 }
121
122 // Initialize the fields in the base class Symbol for a symbol defined
123 // in an Output_data.
124
125 void
126 Symbol::init_base_output_data(const char* name, const char* version,
127 Output_data* od, elfcpp::STT type,
128 elfcpp::STB binding, elfcpp::STV visibility,
129 unsigned char nonvis, bool offset_is_from_end)
130 {
131 this->init_fields(name, version, type, binding, visibility, nonvis);
132 this->u_.in_output_data.output_data = od;
133 this->u_.in_output_data.offset_is_from_end = offset_is_from_end;
134 this->source_ = IN_OUTPUT_DATA;
135 this->in_reg_ = true;
136 }
137
138 // Initialize the fields in the base class Symbol for a symbol defined
139 // in an Output_segment.
140
141 void
142 Symbol::init_base_output_segment(const char* name, const char* version,
143 Output_segment* os, elfcpp::STT type,
144 elfcpp::STB binding, elfcpp::STV visibility,
145 unsigned char nonvis,
146 Segment_offset_base offset_base)
147 {
148 this->init_fields(name, version, type, binding, visibility, nonvis);
149 this->u_.in_output_segment.output_segment = os;
150 this->u_.in_output_segment.offset_base = offset_base;
151 this->source_ = IN_OUTPUT_SEGMENT;
152 this->in_reg_ = true;
153 }
154
155 // Initialize the fields in the base class Symbol for a symbol defined
156 // as a constant.
157
158 void
159 Symbol::init_base_constant(const char* name, const char* version,
160 elfcpp::STT type, elfcpp::STB binding,
161 elfcpp::STV visibility, unsigned char nonvis)
162 {
163 this->init_fields(name, version, type, binding, visibility, nonvis);
164 this->source_ = IS_CONSTANT;
165 this->in_reg_ = true;
166 }
167
168 // Initialize the fields in the base class Symbol for an undefined
169 // symbol.
170
171 void
172 Symbol::init_base_undefined(const char* name, const char* version,
173 elfcpp::STT type, elfcpp::STB binding,
174 elfcpp::STV visibility, unsigned char nonvis)
175 {
176 this->init_fields(name, version, type, binding, visibility, nonvis);
177 this->source_ = IS_UNDEFINED;
178 this->in_reg_ = true;
179 }
180
181 // Allocate a common symbol in the base.
182
183 void
184 Symbol::allocate_base_common(Output_data* od)
185 {
186 gold_assert(this->is_common());
187 this->source_ = IN_OUTPUT_DATA;
188 this->u_.in_output_data.output_data = od;
189 this->u_.in_output_data.offset_is_from_end = false;
190 }
191
192 // Initialize the fields in Sized_symbol for SYM in OBJECT.
193
194 template<int size>
195 template<bool big_endian>
196 void
197 Sized_symbol<size>::init_object(const char* name, const char* version,
198 Object* object,
199 const elfcpp::Sym<size, big_endian>& sym,
200 unsigned int st_shndx, bool is_ordinary)
201 {
202 this->init_base_object(name, version, object, sym, st_shndx, is_ordinary);
203 this->value_ = sym.get_st_value();
204 this->symsize_ = sym.get_st_size();
205 }
206
207 // Initialize the fields in Sized_symbol for a symbol defined in an
208 // Output_data.
209
210 template<int size>
211 void
212 Sized_symbol<size>::init_output_data(const char* name, const char* version,
213 Output_data* od, Value_type value,
214 Size_type symsize, elfcpp::STT type,
215 elfcpp::STB binding,
216 elfcpp::STV visibility,
217 unsigned char nonvis,
218 bool offset_is_from_end)
219 {
220 this->init_base_output_data(name, version, od, type, binding, visibility,
221 nonvis, offset_is_from_end);
222 this->value_ = value;
223 this->symsize_ = symsize;
224 }
225
226 // Initialize the fields in Sized_symbol for a symbol defined in an
227 // Output_segment.
228
229 template<int size>
230 void
231 Sized_symbol<size>::init_output_segment(const char* name, const char* version,
232 Output_segment* os, Value_type value,
233 Size_type symsize, elfcpp::STT type,
234 elfcpp::STB binding,
235 elfcpp::STV visibility,
236 unsigned char nonvis,
237 Segment_offset_base offset_base)
238 {
239 this->init_base_output_segment(name, version, os, type, binding, visibility,
240 nonvis, offset_base);
241 this->value_ = value;
242 this->symsize_ = symsize;
243 }
244
245 // Initialize the fields in Sized_symbol for a symbol defined as a
246 // constant.
247
248 template<int size>
249 void
250 Sized_symbol<size>::init_constant(const char* name, const char* version,
251 Value_type value, Size_type symsize,
252 elfcpp::STT type, elfcpp::STB binding,
253 elfcpp::STV visibility, unsigned char nonvis)
254 {
255 this->init_base_constant(name, version, type, binding, visibility, nonvis);
256 this->value_ = value;
257 this->symsize_ = symsize;
258 }
259
260 // Initialize the fields in Sized_symbol for an undefined symbol.
261
262 template<int size>
263 void
264 Sized_symbol<size>::init_undefined(const char* name, const char* version,
265 elfcpp::STT type, elfcpp::STB binding,
266 elfcpp::STV visibility, unsigned char nonvis)
267 {
268 this->init_base_undefined(name, version, type, binding, visibility, nonvis);
269 this->value_ = 0;
270 this->symsize_ = 0;
271 }
272
273 // Allocate a common symbol.
274
275 template<int size>
276 void
277 Sized_symbol<size>::allocate_common(Output_data* od, Value_type value)
278 {
279 this->allocate_base_common(od);
280 this->value_ = value;
281 }
282
283 // Return true if this symbol should be added to the dynamic symbol
284 // table.
285
286 inline bool
287 Symbol::should_add_dynsym_entry() const
288 {
289 // If the symbol is used by a dynamic relocation, we need to add it.
290 if (this->needs_dynsym_entry())
291 return true;
292
293 // If the symbol was forced local in a version script, do not add it.
294 if (this->is_forced_local())
295 return false;
296
297 // If exporting all symbols or building a shared library,
298 // and the symbol is defined in a regular object and is
299 // externally visible, we need to add it.
300 if ((parameters->options().export_dynamic() || parameters->options().shared())
301 && !this->is_from_dynobj()
302 && this->is_externally_visible())
303 return true;
304
305 return false;
306 }
307
308 // Return true if the final value of this symbol is known at link
309 // time.
310
311 bool
312 Symbol::final_value_is_known() const
313 {
314 // If we are not generating an executable, then no final values are
315 // known, since they will change at runtime.
316 if (parameters->options().shared() || parameters->options().relocatable())
317 return false;
318
319 // If the symbol is not from an object file, and is not undefined,
320 // then it is defined, and known.
321 if (this->source_ != FROM_OBJECT)
322 {
323 if (this->source_ != IS_UNDEFINED)
324 return true;
325 }
326 else
327 {
328 // If the symbol is from a dynamic object, then the final value
329 // is not known.
330 if (this->object()->is_dynamic())
331 return false;
332
333 // If the symbol is not undefined (it is defined or common),
334 // then the final value is known.
335 if (!this->is_undefined())
336 return true;
337 }
338
339 // If the symbol is undefined, then whether the final value is known
340 // depends on whether we are doing a static link. If we are doing a
341 // dynamic link, then the final value could be filled in at runtime.
342 // This could reasonably be the case for a weak undefined symbol.
343 return parameters->doing_static_link();
344 }
345
346 // Return the output section where this symbol is defined.
347
348 Output_section*
349 Symbol::output_section() const
350 {
351 switch (this->source_)
352 {
353 case FROM_OBJECT:
354 {
355 unsigned int shndx = this->u_.from_object.shndx;
356 if (shndx != elfcpp::SHN_UNDEF && this->is_ordinary_shndx_)
357 {
358 gold_assert(!this->u_.from_object.object->is_dynamic());
359 Relobj* relobj = static_cast<Relobj*>(this->u_.from_object.object);
360 return relobj->output_section(shndx);
361 }
362 return NULL;
363 }
364
365 case IN_OUTPUT_DATA:
366 return this->u_.in_output_data.output_data->output_section();
367
368 case IN_OUTPUT_SEGMENT:
369 case IS_CONSTANT:
370 case IS_UNDEFINED:
371 return NULL;
372
373 default:
374 gold_unreachable();
375 }
376 }
377
378 // Set the symbol's output section. This is used for symbols defined
379 // in scripts. This should only be called after the symbol table has
380 // been finalized.
381
382 void
383 Symbol::set_output_section(Output_section* os)
384 {
385 switch (this->source_)
386 {
387 case FROM_OBJECT:
388 case IN_OUTPUT_DATA:
389 gold_assert(this->output_section() == os);
390 break;
391 case IS_CONSTANT:
392 this->source_ = IN_OUTPUT_DATA;
393 this->u_.in_output_data.output_data = os;
394 this->u_.in_output_data.offset_is_from_end = false;
395 break;
396 case IN_OUTPUT_SEGMENT:
397 case IS_UNDEFINED:
398 default:
399 gold_unreachable();
400 }
401 }
402
403 // Class Symbol_table.
404
405 Symbol_table::Symbol_table(unsigned int count,
406 const Version_script_info& version_script)
407 : saw_undefined_(0), offset_(0), table_(count), namepool_(),
408 forwarders_(), commons_(), tls_commons_(), forced_locals_(), warnings_(),
409 version_script_(version_script)
410 {
411 namepool_.reserve(count);
412 }
413
414 Symbol_table::~Symbol_table()
415 {
416 }
417
418 // The hash function. The key values are Stringpool keys.
419
420 inline size_t
421 Symbol_table::Symbol_table_hash::operator()(const Symbol_table_key& key) const
422 {
423 return key.first ^ key.second;
424 }
425
426 // The symbol table key equality function. This is called with
427 // Stringpool keys.
428
429 inline bool
430 Symbol_table::Symbol_table_eq::operator()(const Symbol_table_key& k1,
431 const Symbol_table_key& k2) const
432 {
433 return k1.first == k2.first && k1.second == k2.second;
434 }
435
436 // Make TO a symbol which forwards to FROM.
437
438 void
439 Symbol_table::make_forwarder(Symbol* from, Symbol* to)
440 {
441 gold_assert(from != to);
442 gold_assert(!from->is_forwarder() && !to->is_forwarder());
443 this->forwarders_[from] = to;
444 from->set_forwarder();
445 }
446
447 // Resolve the forwards from FROM, returning the real symbol.
448
449 Symbol*
450 Symbol_table::resolve_forwards(const Symbol* from) const
451 {
452 gold_assert(from->is_forwarder());
453 Unordered_map<const Symbol*, Symbol*>::const_iterator p =
454 this->forwarders_.find(from);
455 gold_assert(p != this->forwarders_.end());
456 return p->second;
457 }
458
459 // Look up a symbol by name.
460
461 Symbol*
462 Symbol_table::lookup(const char* name, const char* version) const
463 {
464 Stringpool::Key name_key;
465 name = this->namepool_.find(name, &name_key);
466 if (name == NULL)
467 return NULL;
468
469 Stringpool::Key version_key = 0;
470 if (version != NULL)
471 {
472 version = this->namepool_.find(version, &version_key);
473 if (version == NULL)
474 return NULL;
475 }
476
477 Symbol_table_key key(name_key, version_key);
478 Symbol_table::Symbol_table_type::const_iterator p = this->table_.find(key);
479 if (p == this->table_.end())
480 return NULL;
481 return p->second;
482 }
483
484 // Resolve a Symbol with another Symbol. This is only used in the
485 // unusual case where there are references to both an unversioned
486 // symbol and a symbol with a version, and we then discover that that
487 // version is the default version. Because this is unusual, we do
488 // this the slow way, by converting back to an ELF symbol.
489
490 template<int size, bool big_endian>
491 void
492 Symbol_table::resolve(Sized_symbol<size>* to, const Sized_symbol<size>* from,
493 const char* version)
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 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, version);
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 int namelen = 0;
890 // DEF: is the version default? LOCAL: is the symbol forced local?
891 bool def = false;
892 bool local = false;
893
894 if (ver != NULL)
895 {
896 // The symbol name is of the form foo@VERSION or foo@@VERSION
897 namelen = ver - name;
898 ++ver;
899 if (*ver == '@')
900 {
901 def = true;
902 ++ver;
903 }
904 }
905 // We don't want to assign a version to an undefined symbol,
906 // even if it is listed in the version script. FIXME: What
907 // about a common symbol?
908 else if (!version_script_.empty()
909 && st_shndx != elfcpp::SHN_UNDEF)
910 {
911 // The symbol name did not have a version, but
912 // the version script may assign a version anyway.
913 namelen = strlen(name);
914 def = true;
915 // Check the global: entries from the version script.
916 const std::string& version =
917 version_script_.get_symbol_version(name);
918 if (!version.empty())
919 ver = version.c_str();
920 // Check the local: entries from the version script
921 if (version_script_.symbol_is_local(name))
922 local = true;
923 }
924
925 elfcpp::Sym<size, big_endian>* psym = &sym;
926 unsigned char symbuf[sym_size];
927 elfcpp::Sym<size, big_endian> sym2(symbuf);
928 if (just_symbols)
929 {
930 memcpy(symbuf, p, sym_size);
931 elfcpp::Sym_write<size, big_endian> sw(symbuf);
932 if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
933 {
934 // Symbol values in object files are section relative.
935 // This is normally what we want, but since here we are
936 // converting the symbol to absolute we need to add the
937 // section address. The section address in an object
938 // file is normally zero, but people can use a linker
939 // script to change it.
940 sw.put_st_value(sym.get_st_value()
941 + relobj->section_address(orig_st_shndx));
942 }
943 st_shndx = elfcpp::SHN_ABS;
944 is_ordinary = false;
945 psym = &sym2;
946 }
947
948 Sized_symbol<size>* res;
949 if (ver == NULL)
950 {
951 Stringpool::Key name_key;
952 name = this->namepool_.add(name, true, &name_key);
953 res = this->add_from_object(relobj, name, name_key, NULL, 0,
954 false, *psym, st_shndx, is_ordinary,
955 orig_st_shndx);
956 if (local)
957 this->force_local(res);
958 }
959 else
960 {
961 Stringpool::Key name_key;
962 name = this->namepool_.add_with_length(name, namelen, true,
963 &name_key);
964 Stringpool::Key ver_key;
965 ver = this->namepool_.add(ver, true, &ver_key);
966
967 res = this->add_from_object(relobj, name, name_key, ver, ver_key,
968 def, *psym, st_shndx, is_ordinary,
969 orig_st_shndx);
970 }
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 if (*pversion == NULL)
1275 {
1276 const std::string& v(this->version_script_.get_symbol_version(*pname));
1277 if (!v.empty())
1278 *pversion = v.c_str();
1279 }
1280
1281 if (only_if_ref)
1282 {
1283 oldsym = this->lookup(*pname, *pversion);
1284 if (oldsym == NULL || !oldsym->is_undefined())
1285 return NULL;
1286
1287 *pname = oldsym->name();
1288 *pversion = oldsym->version();
1289 }
1290 else
1291 {
1292 // Canonicalize NAME and VERSION.
1293 Stringpool::Key name_key;
1294 *pname = this->namepool_.add(*pname, true, &name_key);
1295
1296 Stringpool::Key version_key = 0;
1297 if (*pversion != NULL)
1298 *pversion = this->namepool_.add(*pversion, true, &version_key);
1299
1300 Symbol* const snull = NULL;
1301 std::pair<typename Symbol_table_type::iterator, bool> ins =
1302 this->table_.insert(std::make_pair(std::make_pair(name_key,
1303 version_key),
1304 snull));
1305
1306 if (!ins.second)
1307 {
1308 // We already have a symbol table entry for NAME/VERSION.
1309 oldsym = ins.first->second;
1310 gold_assert(oldsym != NULL);
1311 }
1312 else
1313 {
1314 // We haven't seen this symbol before.
1315 gold_assert(ins.first->second == NULL);
1316 add_to_table = true;
1317 add_loc = ins.first;
1318 oldsym = NULL;
1319 }
1320 }
1321
1322 const Target& target = parameters->target();
1323 if (!target.has_make_symbol())
1324 sym = new Sized_symbol<size>();
1325 else
1326 {
1327 gold_assert(target.get_size() == size);
1328 gold_assert(target.is_big_endian() ? big_endian : !big_endian);
1329 typedef Sized_target<size, big_endian> My_target;
1330 const My_target* sized_target =
1331 static_cast<const My_target*>(&target);
1332 sym = sized_target->make_symbol();
1333 if (sym == NULL)
1334 return NULL;
1335 }
1336
1337 if (add_to_table)
1338 add_loc->second = sym;
1339 else
1340 gold_assert(oldsym != NULL);
1341
1342 *poldsym = this->get_sized_symbol<size>(oldsym);
1343
1344 return sym;
1345 }
1346
1347 // Define a symbol based on an Output_data.
1348
1349 Symbol*
1350 Symbol_table::define_in_output_data(const char* name,
1351 const char* version,
1352 Output_data* od,
1353 uint64_t value,
1354 uint64_t symsize,
1355 elfcpp::STT type,
1356 elfcpp::STB binding,
1357 elfcpp::STV visibility,
1358 unsigned char nonvis,
1359 bool offset_is_from_end,
1360 bool only_if_ref)
1361 {
1362 if (parameters->target().get_size() == 32)
1363 {
1364 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1365 return this->do_define_in_output_data<32>(name, version, od,
1366 value, symsize, type, binding,
1367 visibility, nonvis,
1368 offset_is_from_end,
1369 only_if_ref);
1370 #else
1371 gold_unreachable();
1372 #endif
1373 }
1374 else if (parameters->target().get_size() == 64)
1375 {
1376 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1377 return this->do_define_in_output_data<64>(name, version, od,
1378 value, symsize, type, binding,
1379 visibility, nonvis,
1380 offset_is_from_end,
1381 only_if_ref);
1382 #else
1383 gold_unreachable();
1384 #endif
1385 }
1386 else
1387 gold_unreachable();
1388 }
1389
1390 // Define a symbol in an Output_data, sized version.
1391
1392 template<int size>
1393 Sized_symbol<size>*
1394 Symbol_table::do_define_in_output_data(
1395 const char* name,
1396 const char* version,
1397 Output_data* od,
1398 typename elfcpp::Elf_types<size>::Elf_Addr value,
1399 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1400 elfcpp::STT type,
1401 elfcpp::STB binding,
1402 elfcpp::STV visibility,
1403 unsigned char nonvis,
1404 bool offset_is_from_end,
1405 bool only_if_ref)
1406 {
1407 Sized_symbol<size>* sym;
1408 Sized_symbol<size>* oldsym;
1409
1410 if (parameters->target().is_big_endian())
1411 {
1412 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1413 sym = this->define_special_symbol<size, true>(&name, &version,
1414 only_if_ref, &oldsym);
1415 #else
1416 gold_unreachable();
1417 #endif
1418 }
1419 else
1420 {
1421 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1422 sym = this->define_special_symbol<size, false>(&name, &version,
1423 only_if_ref, &oldsym);
1424 #else
1425 gold_unreachable();
1426 #endif
1427 }
1428
1429 if (sym == NULL)
1430 return NULL;
1431
1432 sym->init_output_data(name, version, od, value, symsize, type, binding,
1433 visibility, nonvis, offset_is_from_end);
1434
1435 if (oldsym == NULL)
1436 {
1437 if (binding == elfcpp::STB_LOCAL
1438 || this->version_script_.symbol_is_local(name))
1439 this->force_local(sym);
1440 else if (version != NULL)
1441 sym->set_is_default();
1442 return sym;
1443 }
1444
1445 if (Symbol_table::should_override_with_special(oldsym))
1446 this->override_with_special(oldsym, sym);
1447 delete sym;
1448 return oldsym;
1449 }
1450
1451 // Define a symbol based on an Output_segment.
1452
1453 Symbol*
1454 Symbol_table::define_in_output_segment(const char* name,
1455 const char* version, Output_segment* os,
1456 uint64_t value,
1457 uint64_t symsize,
1458 elfcpp::STT type,
1459 elfcpp::STB binding,
1460 elfcpp::STV visibility,
1461 unsigned char nonvis,
1462 Symbol::Segment_offset_base offset_base,
1463 bool only_if_ref)
1464 {
1465 if (parameters->target().get_size() == 32)
1466 {
1467 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1468 return this->do_define_in_output_segment<32>(name, version, os,
1469 value, symsize, type,
1470 binding, visibility, nonvis,
1471 offset_base, only_if_ref);
1472 #else
1473 gold_unreachable();
1474 #endif
1475 }
1476 else if (parameters->target().get_size() == 64)
1477 {
1478 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1479 return this->do_define_in_output_segment<64>(name, version, os,
1480 value, symsize, type,
1481 binding, visibility, nonvis,
1482 offset_base, only_if_ref);
1483 #else
1484 gold_unreachable();
1485 #endif
1486 }
1487 else
1488 gold_unreachable();
1489 }
1490
1491 // Define a symbol in an Output_segment, sized version.
1492
1493 template<int size>
1494 Sized_symbol<size>*
1495 Symbol_table::do_define_in_output_segment(
1496 const char* name,
1497 const char* version,
1498 Output_segment* os,
1499 typename elfcpp::Elf_types<size>::Elf_Addr value,
1500 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1501 elfcpp::STT type,
1502 elfcpp::STB binding,
1503 elfcpp::STV visibility,
1504 unsigned char nonvis,
1505 Symbol::Segment_offset_base offset_base,
1506 bool only_if_ref)
1507 {
1508 Sized_symbol<size>* sym;
1509 Sized_symbol<size>* oldsym;
1510
1511 if (parameters->target().is_big_endian())
1512 {
1513 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1514 sym = this->define_special_symbol<size, true>(&name, &version,
1515 only_if_ref, &oldsym);
1516 #else
1517 gold_unreachable();
1518 #endif
1519 }
1520 else
1521 {
1522 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1523 sym = this->define_special_symbol<size, false>(&name, &version,
1524 only_if_ref, &oldsym);
1525 #else
1526 gold_unreachable();
1527 #endif
1528 }
1529
1530 if (sym == NULL)
1531 return NULL;
1532
1533 sym->init_output_segment(name, version, os, value, symsize, type, binding,
1534 visibility, nonvis, offset_base);
1535
1536 if (oldsym == NULL)
1537 {
1538 if (binding == elfcpp::STB_LOCAL
1539 || this->version_script_.symbol_is_local(name))
1540 this->force_local(sym);
1541 else if (version != NULL)
1542 sym->set_is_default();
1543 return sym;
1544 }
1545
1546 if (Symbol_table::should_override_with_special(oldsym))
1547 this->override_with_special(oldsym, sym);
1548 delete sym;
1549 return oldsym;
1550 }
1551
1552 // Define a special symbol with a constant value. It is a multiple
1553 // definition error if this symbol is already defined.
1554
1555 Symbol*
1556 Symbol_table::define_as_constant(const char* name,
1557 const char* version,
1558 uint64_t value,
1559 uint64_t symsize,
1560 elfcpp::STT type,
1561 elfcpp::STB binding,
1562 elfcpp::STV visibility,
1563 unsigned char nonvis,
1564 bool only_if_ref,
1565 bool force_override)
1566 {
1567 if (parameters->target().get_size() == 32)
1568 {
1569 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1570 return this->do_define_as_constant<32>(name, version, value,
1571 symsize, type, binding,
1572 visibility, nonvis, only_if_ref,
1573 force_override);
1574 #else
1575 gold_unreachable();
1576 #endif
1577 }
1578 else if (parameters->target().get_size() == 64)
1579 {
1580 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1581 return this->do_define_as_constant<64>(name, version, value,
1582 symsize, type, binding,
1583 visibility, nonvis, only_if_ref,
1584 force_override);
1585 #else
1586 gold_unreachable();
1587 #endif
1588 }
1589 else
1590 gold_unreachable();
1591 }
1592
1593 // Define a symbol as a constant, sized version.
1594
1595 template<int size>
1596 Sized_symbol<size>*
1597 Symbol_table::do_define_as_constant(
1598 const char* name,
1599 const char* version,
1600 typename elfcpp::Elf_types<size>::Elf_Addr value,
1601 typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1602 elfcpp::STT type,
1603 elfcpp::STB binding,
1604 elfcpp::STV visibility,
1605 unsigned char nonvis,
1606 bool only_if_ref,
1607 bool force_override)
1608 {
1609 Sized_symbol<size>* sym;
1610 Sized_symbol<size>* oldsym;
1611
1612 if (parameters->target().is_big_endian())
1613 {
1614 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1615 sym = this->define_special_symbol<size, true>(&name, &version,
1616 only_if_ref, &oldsym);
1617 #else
1618 gold_unreachable();
1619 #endif
1620 }
1621 else
1622 {
1623 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1624 sym = this->define_special_symbol<size, false>(&name, &version,
1625 only_if_ref, &oldsym);
1626 #else
1627 gold_unreachable();
1628 #endif
1629 }
1630
1631 if (sym == NULL)
1632 return NULL;
1633
1634 sym->init_constant(name, version, value, symsize, type, binding, visibility,
1635 nonvis);
1636
1637 if (oldsym == NULL)
1638 {
1639 // Version symbols are absolute symbols with name == version.
1640 // We don't want to force them to be local.
1641 if ((version == NULL
1642 || name != version
1643 || value != 0)
1644 && (binding == elfcpp::STB_LOCAL
1645 || this->version_script_.symbol_is_local(name)))
1646 this->force_local(sym);
1647 else if (version != NULL
1648 && (name != version || value != 0))
1649 sym->set_is_default();
1650 return sym;
1651 }
1652
1653 if (force_override || Symbol_table::should_override_with_special(oldsym))
1654 this->override_with_special(oldsym, sym);
1655 delete sym;
1656 return oldsym;
1657 }
1658
1659 // Define a set of symbols in output sections.
1660
1661 void
1662 Symbol_table::define_symbols(const Layout* layout, int count,
1663 const Define_symbol_in_section* p,
1664 bool only_if_ref)
1665 {
1666 for (int i = 0; i < count; ++i, ++p)
1667 {
1668 Output_section* os = layout->find_output_section(p->output_section);
1669 if (os != NULL)
1670 this->define_in_output_data(p->name, NULL, os, p->value,
1671 p->size, p->type, p->binding,
1672 p->visibility, p->nonvis,
1673 p->offset_is_from_end,
1674 only_if_ref || p->only_if_ref);
1675 else
1676 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1677 p->binding, p->visibility, p->nonvis,
1678 only_if_ref || p->only_if_ref,
1679 false);
1680 }
1681 }
1682
1683 // Define a set of symbols in output segments.
1684
1685 void
1686 Symbol_table::define_symbols(const Layout* layout, int count,
1687 const Define_symbol_in_segment* p,
1688 bool only_if_ref)
1689 {
1690 for (int i = 0; i < count; ++i, ++p)
1691 {
1692 Output_segment* os = layout->find_output_segment(p->segment_type,
1693 p->segment_flags_set,
1694 p->segment_flags_clear);
1695 if (os != NULL)
1696 this->define_in_output_segment(p->name, NULL, os, p->value,
1697 p->size, p->type, p->binding,
1698 p->visibility, p->nonvis,
1699 p->offset_base,
1700 only_if_ref || p->only_if_ref);
1701 else
1702 this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1703 p->binding, p->visibility, p->nonvis,
1704 only_if_ref || p->only_if_ref,
1705 false);
1706 }
1707 }
1708
1709 // Define CSYM using a COPY reloc. POSD is the Output_data where the
1710 // symbol should be defined--typically a .dyn.bss section. VALUE is
1711 // the offset within POSD.
1712
1713 template<int size>
1714 void
1715 Symbol_table::define_with_copy_reloc(
1716 Sized_symbol<size>* csym,
1717 Output_data* posd,
1718 typename elfcpp::Elf_types<size>::Elf_Addr value)
1719 {
1720 gold_assert(csym->is_from_dynobj());
1721 gold_assert(!csym->is_copied_from_dynobj());
1722 Object* object = csym->object();
1723 gold_assert(object->is_dynamic());
1724 Dynobj* dynobj = static_cast<Dynobj*>(object);
1725
1726 // Our copied variable has to override any variable in a shared
1727 // library.
1728 elfcpp::STB binding = csym->binding();
1729 if (binding == elfcpp::STB_WEAK)
1730 binding = elfcpp::STB_GLOBAL;
1731
1732 this->define_in_output_data(csym->name(), csym->version(),
1733 posd, value, csym->symsize(),
1734 csym->type(), binding,
1735 csym->visibility(), csym->nonvis(),
1736 false, false);
1737
1738 csym->set_is_copied_from_dynobj();
1739 csym->set_needs_dynsym_entry();
1740
1741 this->copied_symbol_dynobjs_[csym] = dynobj;
1742
1743 // We have now defined all aliases, but we have not entered them all
1744 // in the copied_symbol_dynobjs_ map.
1745 if (csym->has_alias())
1746 {
1747 Symbol* sym = csym;
1748 while (true)
1749 {
1750 sym = this->weak_aliases_[sym];
1751 if (sym == csym)
1752 break;
1753 gold_assert(sym->output_data() == posd);
1754
1755 sym->set_is_copied_from_dynobj();
1756 this->copied_symbol_dynobjs_[sym] = dynobj;
1757 }
1758 }
1759 }
1760
1761 // SYM is defined using a COPY reloc. Return the dynamic object where
1762 // the original definition was found.
1763
1764 Dynobj*
1765 Symbol_table::get_copy_source(const Symbol* sym) const
1766 {
1767 gold_assert(sym->is_copied_from_dynobj());
1768 Copied_symbol_dynobjs::const_iterator p =
1769 this->copied_symbol_dynobjs_.find(sym);
1770 gold_assert(p != this->copied_symbol_dynobjs_.end());
1771 return p->second;
1772 }
1773
1774 // Add any undefined symbols named on the command line.
1775
1776 void
1777 Symbol_table::add_undefined_symbols_from_command_line()
1778 {
1779 if (parameters->options().any_undefined())
1780 {
1781 if (parameters->target().get_size() == 32)
1782 {
1783 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1784 this->do_add_undefined_symbols_from_command_line<32>();
1785 #else
1786 gold_unreachable();
1787 #endif
1788 }
1789 else if (parameters->target().get_size() == 64)
1790 {
1791 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1792 this->do_add_undefined_symbols_from_command_line<64>();
1793 #else
1794 gold_unreachable();
1795 #endif
1796 }
1797 else
1798 gold_unreachable();
1799 }
1800 }
1801
1802 template<int size>
1803 void
1804 Symbol_table::do_add_undefined_symbols_from_command_line()
1805 {
1806 for (options::String_set::const_iterator p =
1807 parameters->options().undefined_begin();
1808 p != parameters->options().undefined_end();
1809 ++p)
1810 {
1811 const char* name = p->c_str();
1812
1813 if (this->lookup(name) != NULL)
1814 continue;
1815
1816 const char* version = NULL;
1817
1818 Sized_symbol<size>* sym;
1819 Sized_symbol<size>* oldsym;
1820 if (parameters->target().is_big_endian())
1821 {
1822 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1823 sym = this->define_special_symbol<size, true>(&name, &version,
1824 false, &oldsym);
1825 #else
1826 gold_unreachable();
1827 #endif
1828 }
1829 else
1830 {
1831 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1832 sym = this->define_special_symbol<size, false>(&name, &version,
1833 false, &oldsym);
1834 #else
1835 gold_unreachable();
1836 #endif
1837 }
1838
1839 gold_assert(oldsym == NULL);
1840
1841 sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
1842 elfcpp::STV_DEFAULT, 0);
1843 ++this->saw_undefined_;
1844 }
1845 }
1846
1847 // Set the dynamic symbol indexes. INDEX is the index of the first
1848 // global dynamic symbol. Pointers to the symbols are stored into the
1849 // vector SYMS. The names are added to DYNPOOL. This returns an
1850 // updated dynamic symbol index.
1851
1852 unsigned int
1853 Symbol_table::set_dynsym_indexes(unsigned int index,
1854 std::vector<Symbol*>* syms,
1855 Stringpool* dynpool,
1856 Versions* versions)
1857 {
1858 for (Symbol_table_type::iterator p = this->table_.begin();
1859 p != this->table_.end();
1860 ++p)
1861 {
1862 Symbol* sym = p->second;
1863
1864 // Note that SYM may already have a dynamic symbol index, since
1865 // some symbols appear more than once in the symbol table, with
1866 // and without a version.
1867
1868 if (!sym->should_add_dynsym_entry())
1869 sym->set_dynsym_index(-1U);
1870 else if (!sym->has_dynsym_index())
1871 {
1872 sym->set_dynsym_index(index);
1873 ++index;
1874 syms->push_back(sym);
1875 dynpool->add(sym->name(), false, NULL);
1876
1877 // Record any version information.
1878 if (sym->version() != NULL)
1879 versions->record_version(this, dynpool, sym);
1880 }
1881 }
1882
1883 // Finish up the versions. In some cases this may add new dynamic
1884 // symbols.
1885 index = versions->finalize(this, index, syms);
1886
1887 return index;
1888 }
1889
1890 // Set the final values for all the symbols. The index of the first
1891 // global symbol in the output file is *PLOCAL_SYMCOUNT. Record the
1892 // file offset OFF. Add their names to POOL. Return the new file
1893 // offset. Update *PLOCAL_SYMCOUNT if necessary.
1894
1895 off_t
1896 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1897 size_t dyncount, Stringpool* pool,
1898 unsigned int *plocal_symcount)
1899 {
1900 off_t ret;
1901
1902 gold_assert(*plocal_symcount != 0);
1903 this->first_global_index_ = *plocal_symcount;
1904
1905 this->dynamic_offset_ = dynoff;
1906 this->first_dynamic_global_index_ = dyn_global_index;
1907 this->dynamic_count_ = dyncount;
1908
1909 if (parameters->target().get_size() == 32)
1910 {
1911 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1912 ret = this->sized_finalize<32>(off, pool, plocal_symcount);
1913 #else
1914 gold_unreachable();
1915 #endif
1916 }
1917 else if (parameters->target().get_size() == 64)
1918 {
1919 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1920 ret = this->sized_finalize<64>(off, pool, plocal_symcount);
1921 #else
1922 gold_unreachable();
1923 #endif
1924 }
1925 else
1926 gold_unreachable();
1927
1928 // Now that we have the final symbol table, we can reliably note
1929 // which symbols should get warnings.
1930 this->warnings_.note_warnings(this);
1931
1932 return ret;
1933 }
1934
1935 // SYM is going into the symbol table at *PINDEX. Add the name to
1936 // POOL, update *PINDEX and *POFF.
1937
1938 template<int size>
1939 void
1940 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1941 unsigned int* pindex, off_t* poff)
1942 {
1943 sym->set_symtab_index(*pindex);
1944 pool->add(sym->name(), false, NULL);
1945 ++*pindex;
1946 *poff += elfcpp::Elf_sizes<size>::sym_size;
1947 }
1948
1949 // Set the final value for all the symbols. This is called after
1950 // Layout::finalize, so all the output sections have their final
1951 // address.
1952
1953 template<int size>
1954 off_t
1955 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1956 unsigned int* plocal_symcount)
1957 {
1958 off = align_address(off, size >> 3);
1959 this->offset_ = off;
1960
1961 unsigned int index = *plocal_symcount;
1962 const unsigned int orig_index = index;
1963
1964 // First do all the symbols which have been forced to be local, as
1965 // they must appear before all global symbols.
1966 for (Forced_locals::iterator p = this->forced_locals_.begin();
1967 p != this->forced_locals_.end();
1968 ++p)
1969 {
1970 Symbol* sym = *p;
1971 gold_assert(sym->is_forced_local());
1972 if (this->sized_finalize_symbol<size>(sym))
1973 {
1974 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1975 ++*plocal_symcount;
1976 }
1977 }
1978
1979 // Now do all the remaining symbols.
1980 for (Symbol_table_type::iterator p = this->table_.begin();
1981 p != this->table_.end();
1982 ++p)
1983 {
1984 Symbol* sym = p->second;
1985 if (this->sized_finalize_symbol<size>(sym))
1986 this->add_to_final_symtab<size>(sym, pool, &index, &off);
1987 }
1988
1989 this->output_count_ = index - orig_index;
1990
1991 return off;
1992 }
1993
1994 // Finalize the symbol SYM. This returns true if the symbol should be
1995 // added to the symbol table, false otherwise.
1996
1997 template<int size>
1998 bool
1999 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2000 {
2001 typedef typename Sized_symbol<size>::Value_type Value_type;
2002
2003 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2004
2005 // The default version of a symbol may appear twice in the symbol
2006 // table. We only need to finalize it once.
2007 if (sym->has_symtab_index())
2008 return false;
2009
2010 if (!sym->in_reg())
2011 {
2012 gold_assert(!sym->has_symtab_index());
2013 sym->set_symtab_index(-1U);
2014 gold_assert(sym->dynsym_index() == -1U);
2015 return false;
2016 }
2017
2018 Value_type value;
2019
2020 switch (sym->source())
2021 {
2022 case Symbol::FROM_OBJECT:
2023 {
2024 bool is_ordinary;
2025 unsigned int shndx = sym->shndx(&is_ordinary);
2026
2027 // FIXME: We need some target specific support here.
2028 if (!is_ordinary
2029 && shndx != elfcpp::SHN_ABS
2030 && shndx != elfcpp::SHN_COMMON)
2031 {
2032 gold_error(_("%s: unsupported symbol section 0x%x"),
2033 sym->demangled_name().c_str(), shndx);
2034 shndx = elfcpp::SHN_UNDEF;
2035 }
2036
2037 Object* symobj = sym->object();
2038 if (symobj->is_dynamic())
2039 {
2040 value = 0;
2041 shndx = elfcpp::SHN_UNDEF;
2042 }
2043 else if (shndx == elfcpp::SHN_UNDEF)
2044 value = 0;
2045 else if (!is_ordinary
2046 && (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON))
2047 value = sym->value();
2048 else
2049 {
2050 Relobj* relobj = static_cast<Relobj*>(symobj);
2051 Output_section* os = relobj->output_section(shndx);
2052
2053 if (os == NULL)
2054 {
2055 sym->set_symtab_index(-1U);
2056 gold_assert(sym->dynsym_index() == -1U);
2057 return false;
2058 }
2059
2060 uint64_t secoff64 = relobj->output_section_offset(shndx);
2061 Value_type secoff = convert_types<Value_type, uint64_t>(secoff64);
2062 if (sym->type() == elfcpp::STT_TLS)
2063 value = sym->value() + os->tls_offset() + secoff;
2064 else
2065 value = sym->value() + os->address() + secoff;
2066 }
2067 }
2068 break;
2069
2070 case Symbol::IN_OUTPUT_DATA:
2071 {
2072 Output_data* od = sym->output_data();
2073 value = sym->value();
2074 if (sym->type() != elfcpp::STT_TLS)
2075 value += od->address();
2076 else
2077 {
2078 Output_section* os = od->output_section();
2079 gold_assert(os != NULL);
2080 value += os->tls_offset() + (od->address() - os->address());
2081 }
2082 if (sym->offset_is_from_end())
2083 value += od->data_size();
2084 }
2085 break;
2086
2087 case Symbol::IN_OUTPUT_SEGMENT:
2088 {
2089 Output_segment* os = sym->output_segment();
2090 value = sym->value();
2091 if (sym->type() != elfcpp::STT_TLS)
2092 value += os->vaddr();
2093 switch (sym->offset_base())
2094 {
2095 case Symbol::SEGMENT_START:
2096 break;
2097 case Symbol::SEGMENT_END:
2098 value += os->memsz();
2099 break;
2100 case Symbol::SEGMENT_BSS:
2101 value += os->filesz();
2102 break;
2103 default:
2104 gold_unreachable();
2105 }
2106 }
2107 break;
2108
2109 case Symbol::IS_CONSTANT:
2110 value = sym->value();
2111 break;
2112
2113 case Symbol::IS_UNDEFINED:
2114 value = 0;
2115 break;
2116
2117 default:
2118 gold_unreachable();
2119 }
2120
2121 sym->set_value(value);
2122
2123 if (parameters->options().strip_all())
2124 {
2125 sym->set_symtab_index(-1U);
2126 return false;
2127 }
2128
2129 return true;
2130 }
2131
2132 // Write out the global symbols.
2133
2134 void
2135 Symbol_table::write_globals(const Input_objects* input_objects,
2136 const Stringpool* sympool,
2137 const Stringpool* dynpool,
2138 Output_symtab_xindex* symtab_xindex,
2139 Output_symtab_xindex* dynsym_xindex,
2140 Output_file* of) const
2141 {
2142 switch (parameters->size_and_endianness())
2143 {
2144 #ifdef HAVE_TARGET_32_LITTLE
2145 case Parameters::TARGET_32_LITTLE:
2146 this->sized_write_globals<32, false>(input_objects, sympool,
2147 dynpool, symtab_xindex,
2148 dynsym_xindex, of);
2149 break;
2150 #endif
2151 #ifdef HAVE_TARGET_32_BIG
2152 case Parameters::TARGET_32_BIG:
2153 this->sized_write_globals<32, true>(input_objects, sympool,
2154 dynpool, symtab_xindex,
2155 dynsym_xindex, of);
2156 break;
2157 #endif
2158 #ifdef HAVE_TARGET_64_LITTLE
2159 case Parameters::TARGET_64_LITTLE:
2160 this->sized_write_globals<64, false>(input_objects, sympool,
2161 dynpool, symtab_xindex,
2162 dynsym_xindex, of);
2163 break;
2164 #endif
2165 #ifdef HAVE_TARGET_64_BIG
2166 case Parameters::TARGET_64_BIG:
2167 this->sized_write_globals<64, true>(input_objects, sympool,
2168 dynpool, symtab_xindex,
2169 dynsym_xindex, of);
2170 break;
2171 #endif
2172 default:
2173 gold_unreachable();
2174 }
2175 }
2176
2177 // Write out the global symbols.
2178
2179 template<int size, bool big_endian>
2180 void
2181 Symbol_table::sized_write_globals(const Input_objects* input_objects,
2182 const Stringpool* sympool,
2183 const Stringpool* dynpool,
2184 Output_symtab_xindex* symtab_xindex,
2185 Output_symtab_xindex* dynsym_xindex,
2186 Output_file* of) const
2187 {
2188 const Target& target = parameters->target();
2189
2190 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2191
2192 const unsigned int output_count = this->output_count_;
2193 const section_size_type oview_size = output_count * sym_size;
2194 const unsigned int first_global_index = this->first_global_index_;
2195 unsigned char* psyms;
2196 if (this->offset_ == 0 || output_count == 0)
2197 psyms = NULL;
2198 else
2199 psyms = of->get_output_view(this->offset_, oview_size);
2200
2201 const unsigned int dynamic_count = this->dynamic_count_;
2202 const section_size_type dynamic_size = dynamic_count * sym_size;
2203 const unsigned int first_dynamic_global_index =
2204 this->first_dynamic_global_index_;
2205 unsigned char* dynamic_view;
2206 if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2207 dynamic_view = NULL;
2208 else
2209 dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2210
2211 for (Symbol_table_type::const_iterator p = this->table_.begin();
2212 p != this->table_.end();
2213 ++p)
2214 {
2215 Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2216
2217 // Possibly warn about unresolved symbols in shared libraries.
2218 this->warn_about_undefined_dynobj_symbol(input_objects, sym);
2219
2220 unsigned int sym_index = sym->symtab_index();
2221 unsigned int dynsym_index;
2222 if (dynamic_view == NULL)
2223 dynsym_index = -1U;
2224 else
2225 dynsym_index = sym->dynsym_index();
2226
2227 if (sym_index == -1U && dynsym_index == -1U)
2228 {
2229 // This symbol is not included in the output file.
2230 continue;
2231 }
2232
2233 unsigned int shndx;
2234 typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2235 typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
2236 switch (sym->source())
2237 {
2238 case Symbol::FROM_OBJECT:
2239 {
2240 bool is_ordinary;
2241 unsigned int in_shndx = sym->shndx(&is_ordinary);
2242
2243 // FIXME: We need some target specific support here.
2244 if (!is_ordinary
2245 && in_shndx != elfcpp::SHN_ABS
2246 && in_shndx != elfcpp::SHN_COMMON)
2247 {
2248 gold_error(_("%s: unsupported symbol section 0x%x"),
2249 sym->demangled_name().c_str(), in_shndx);
2250 shndx = in_shndx;
2251 }
2252 else
2253 {
2254 Object* symobj = sym->object();
2255 if (symobj->is_dynamic())
2256 {
2257 if (sym->needs_dynsym_value())
2258 dynsym_value = target.dynsym_value(sym);
2259 shndx = elfcpp::SHN_UNDEF;
2260 }
2261 else if (in_shndx == elfcpp::SHN_UNDEF
2262 || (!is_ordinary
2263 && (in_shndx == elfcpp::SHN_ABS
2264 || in_shndx == elfcpp::SHN_COMMON)))
2265 shndx = in_shndx;
2266 else
2267 {
2268 Relobj* relobj = static_cast<Relobj*>(symobj);
2269 Output_section* os = relobj->output_section(in_shndx);
2270 gold_assert(os != NULL);
2271 shndx = os->out_shndx();
2272
2273 if (shndx >= elfcpp::SHN_LORESERVE)
2274 {
2275 if (sym_index != -1U)
2276 symtab_xindex->add(sym_index, shndx);
2277 if (dynsym_index != -1U)
2278 dynsym_xindex->add(dynsym_index, shndx);
2279 shndx = elfcpp::SHN_XINDEX;
2280 }
2281
2282 // In object files symbol values are section
2283 // relative.
2284 if (parameters->options().relocatable())
2285 sym_value -= os->address();
2286 }
2287 }
2288 }
2289 break;
2290
2291 case Symbol::IN_OUTPUT_DATA:
2292 shndx = sym->output_data()->out_shndx();
2293 if (shndx >= elfcpp::SHN_LORESERVE)
2294 {
2295 if (sym_index != -1U)
2296 symtab_xindex->add(sym_index, shndx);
2297 if (dynsym_index != -1U)
2298 dynsym_xindex->add(dynsym_index, shndx);
2299 shndx = elfcpp::SHN_XINDEX;
2300 }
2301 break;
2302
2303 case Symbol::IN_OUTPUT_SEGMENT:
2304 shndx = elfcpp::SHN_ABS;
2305 break;
2306
2307 case Symbol::IS_CONSTANT:
2308 shndx = elfcpp::SHN_ABS;
2309 break;
2310
2311 case Symbol::IS_UNDEFINED:
2312 shndx = elfcpp::SHN_UNDEF;
2313 break;
2314
2315 default:
2316 gold_unreachable();
2317 }
2318
2319 if (sym_index != -1U)
2320 {
2321 sym_index -= first_global_index;
2322 gold_assert(sym_index < output_count);
2323 unsigned char* ps = psyms + (sym_index * sym_size);
2324 this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2325 sympool, ps);
2326 }
2327
2328 if (dynsym_index != -1U)
2329 {
2330 dynsym_index -= first_dynamic_global_index;
2331 gold_assert(dynsym_index < dynamic_count);
2332 unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2333 this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2334 dynpool, pd);
2335 }
2336 }
2337
2338 of->write_output_view(this->offset_, oview_size, psyms);
2339 if (dynamic_view != NULL)
2340 of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2341 }
2342
2343 // Write out the symbol SYM, in section SHNDX, to P. POOL is the
2344 // strtab holding the name.
2345
2346 template<int size, bool big_endian>
2347 void
2348 Symbol_table::sized_write_symbol(
2349 Sized_symbol<size>* sym,
2350 typename elfcpp::Elf_types<size>::Elf_Addr value,
2351 unsigned int shndx,
2352 const Stringpool* pool,
2353 unsigned char* p) const
2354 {
2355 elfcpp::Sym_write<size, big_endian> osym(p);
2356 osym.put_st_name(pool->get_offset(sym->name()));
2357 osym.put_st_value(value);
2358 // Use a symbol size of zero for undefined symbols.
2359 osym.put_st_size(shndx == elfcpp::SHN_UNDEF ? 0 : sym->symsize());
2360 // A version script may have overridden the default binding.
2361 if (sym->is_forced_local())
2362 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
2363 else
2364 osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
2365 osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2366 osym.put_st_shndx(shndx);
2367 }
2368
2369 // Check for unresolved symbols in shared libraries. This is
2370 // controlled by the --allow-shlib-undefined option.
2371
2372 // We only warn about libraries for which we have seen all the
2373 // DT_NEEDED entries. We don't try to track down DT_NEEDED entries
2374 // which were not seen in this link. If we didn't see a DT_NEEDED
2375 // entry, we aren't going to be able to reliably report whether the
2376 // symbol is undefined.
2377
2378 // We also don't warn about libraries found in the system library
2379 // directory (the directory were we find libc.so); we assume that
2380 // those libraries are OK. This heuristic avoids problems in
2381 // GNU/Linux, in which -ldl can have undefined references satisfied by
2382 // ld-linux.so.
2383
2384 inline void
2385 Symbol_table::warn_about_undefined_dynobj_symbol(
2386 const Input_objects* input_objects,
2387 Symbol* sym) const
2388 {
2389 bool dummy;
2390 if (sym->source() == Symbol::FROM_OBJECT
2391 && sym->object()->is_dynamic()
2392 && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
2393 && sym->binding() != elfcpp::STB_WEAK
2394 && !parameters->options().allow_shlib_undefined()
2395 && !parameters->target().is_defined_by_abi(sym)
2396 && !input_objects->found_in_system_library_directory(sym->object()))
2397 {
2398 // A very ugly cast.
2399 Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2400 if (!dynobj->has_unknown_needed_entries())
2401 {
2402 if (sym->version())
2403 gold_error(_("%s: undefined reference to '%s', version '%s'"),
2404 sym->object()->name().c_str(),
2405 sym->demangled_name().c_str(),
2406 sym->version());
2407 else
2408 gold_error(_("%s: undefined reference to '%s'"),
2409 sym->object()->name().c_str(),
2410 sym->demangled_name().c_str());
2411 }
2412 }
2413 }
2414
2415 // Write out a section symbol. Return the update offset.
2416
2417 void
2418 Symbol_table::write_section_symbol(const Output_section *os,
2419 Output_symtab_xindex* symtab_xindex,
2420 Output_file* of,
2421 off_t offset) const
2422 {
2423 switch (parameters->size_and_endianness())
2424 {
2425 #ifdef HAVE_TARGET_32_LITTLE
2426 case Parameters::TARGET_32_LITTLE:
2427 this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2428 offset);
2429 break;
2430 #endif
2431 #ifdef HAVE_TARGET_32_BIG
2432 case Parameters::TARGET_32_BIG:
2433 this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2434 offset);
2435 break;
2436 #endif
2437 #ifdef HAVE_TARGET_64_LITTLE
2438 case Parameters::TARGET_64_LITTLE:
2439 this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2440 offset);
2441 break;
2442 #endif
2443 #ifdef HAVE_TARGET_64_BIG
2444 case Parameters::TARGET_64_BIG:
2445 this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2446 offset);
2447 break;
2448 #endif
2449 default:
2450 gold_unreachable();
2451 }
2452 }
2453
2454 // Write out a section symbol, specialized for size and endianness.
2455
2456 template<int size, bool big_endian>
2457 void
2458 Symbol_table::sized_write_section_symbol(const Output_section* os,
2459 Output_symtab_xindex* symtab_xindex,
2460 Output_file* of,
2461 off_t offset) const
2462 {
2463 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2464
2465 unsigned char* pov = of->get_output_view(offset, sym_size);
2466
2467 elfcpp::Sym_write<size, big_endian> osym(pov);
2468 osym.put_st_name(0);
2469 osym.put_st_value(os->address());
2470 osym.put_st_size(0);
2471 osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2472 elfcpp::STT_SECTION));
2473 osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2474
2475 unsigned int shndx = os->out_shndx();
2476 if (shndx >= elfcpp::SHN_LORESERVE)
2477 {
2478 symtab_xindex->add(os->symtab_index(), shndx);
2479 shndx = elfcpp::SHN_XINDEX;
2480 }
2481 osym.put_st_shndx(shndx);
2482
2483 of->write_output_view(offset, sym_size, pov);
2484 }
2485
2486 // Print statistical information to stderr. This is used for --stats.
2487
2488 void
2489 Symbol_table::print_stats() const
2490 {
2491 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2492 fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2493 program_name, this->table_.size(), this->table_.bucket_count());
2494 #else
2495 fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2496 program_name, this->table_.size());
2497 #endif
2498 this->namepool_.print_stats("symbol table stringpool");
2499 }
2500
2501 // We check for ODR violations by looking for symbols with the same
2502 // name for which the debugging information reports that they were
2503 // defined in different source locations. When comparing the source
2504 // location, we consider instances with the same base filename and
2505 // line number to be the same. This is because different object
2506 // files/shared libraries can include the same header file using
2507 // different paths, and we don't want to report an ODR violation in
2508 // that case.
2509
2510 // This struct is used to compare line information, as returned by
2511 // Dwarf_line_info::one_addr2line. It implements a < comparison
2512 // operator used with std::set.
2513
2514 struct Odr_violation_compare
2515 {
2516 bool
2517 operator()(const std::string& s1, const std::string& s2) const
2518 {
2519 std::string::size_type pos1 = s1.rfind('/');
2520 std::string::size_type pos2 = s2.rfind('/');
2521 if (pos1 == std::string::npos
2522 || pos2 == std::string::npos)
2523 return s1 < s2;
2524 return s1.compare(pos1, std::string::npos,
2525 s2, pos2, std::string::npos) < 0;
2526 }
2527 };
2528
2529 // Check candidate_odr_violations_ to find symbols with the same name
2530 // but apparently different definitions (different source-file/line-no).
2531
2532 void
2533 Symbol_table::detect_odr_violations(const Task* task,
2534 const char* output_file_name) const
2535 {
2536 for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2537 it != candidate_odr_violations_.end();
2538 ++it)
2539 {
2540 const char* symbol_name = it->first;
2541 // We use a sorted set so the output is deterministic.
2542 std::set<std::string, Odr_violation_compare> line_nums;
2543
2544 for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2545 locs = it->second.begin();
2546 locs != it->second.end();
2547 ++locs)
2548 {
2549 // We need to lock the object in order to read it. This
2550 // means that we have to run in a singleton Task. If we
2551 // want to run this in a general Task for better
2552 // performance, we will need one Task for object, plus
2553 // appropriate locking to ensure that we don't conflict with
2554 // other uses of the object. Also note, one_addr2line is not
2555 // currently thread-safe.
2556 Task_lock_obj<Object> tl(task, locs->object);
2557 // 16 is the size of the object-cache that one_addr2line should use.
2558 std::string lineno = Dwarf_line_info::one_addr2line(
2559 locs->object, locs->shndx, locs->offset, 16);
2560 if (!lineno.empty())
2561 line_nums.insert(lineno);
2562 }
2563
2564 if (line_nums.size() > 1)
2565 {
2566 gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2567 "places (possible ODR violation):"),
2568 output_file_name, demangle(symbol_name).c_str());
2569 for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2570 it2 != line_nums.end();
2571 ++it2)
2572 fprintf(stderr, " %s\n", it2->c_str());
2573 }
2574 }
2575 // We only call one_addr2line() in this function, so we can clear its cache.
2576 Dwarf_line_info::clear_addr2line_cache();
2577 }
2578
2579 // Warnings functions.
2580
2581 // Add a new warning.
2582
2583 void
2584 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2585 const std::string& warning)
2586 {
2587 name = symtab->canonicalize_name(name);
2588 this->warnings_[name].set(obj, warning);
2589 }
2590
2591 // Look through the warnings and mark the symbols for which we should
2592 // warn. This is called during Layout::finalize when we know the
2593 // sources for all the symbols.
2594
2595 void
2596 Warnings::note_warnings(Symbol_table* symtab)
2597 {
2598 for (Warning_table::iterator p = this->warnings_.begin();
2599 p != this->warnings_.end();
2600 ++p)
2601 {
2602 Symbol* sym = symtab->lookup(p->first, NULL);
2603 if (sym != NULL
2604 && sym->source() == Symbol::FROM_OBJECT
2605 && sym->object() == p->second.object)
2606 sym->set_has_warning();
2607 }
2608 }
2609
2610 // Issue a warning. This is called when we see a relocation against a
2611 // symbol for which has a warning.
2612
2613 template<int size, bool big_endian>
2614 void
2615 Warnings::issue_warning(const Symbol* sym,
2616 const Relocate_info<size, big_endian>* relinfo,
2617 size_t relnum, off_t reloffset) const
2618 {
2619 gold_assert(sym->has_warning());
2620 Warning_table::const_iterator p = this->warnings_.find(sym->name());
2621 gold_assert(p != this->warnings_.end());
2622 gold_warning_at_location(relinfo, relnum, reloffset,
2623 "%s", p->second.text.c_str());
2624 }
2625
2626 // Instantiate the templates we need. We could use the configure
2627 // script to restrict this to only the ones needed for implemented
2628 // targets.
2629
2630 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2631 template
2632 void
2633 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2634 #endif
2635
2636 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2637 template
2638 void
2639 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2640 #endif
2641
2642 #ifdef HAVE_TARGET_32_LITTLE
2643 template
2644 void
2645 Symbol_table::add_from_relobj<32, false>(
2646 Sized_relobj<32, false>* relobj,
2647 const unsigned char* syms,
2648 size_t count,
2649 size_t symndx_offset,
2650 const char* sym_names,
2651 size_t sym_name_size,
2652 Sized_relobj<32, true>::Symbols* sympointers,
2653 size_t* defined);
2654 #endif
2655
2656 #ifdef HAVE_TARGET_32_BIG
2657 template
2658 void
2659 Symbol_table::add_from_relobj<32, true>(
2660 Sized_relobj<32, true>* relobj,
2661 const unsigned char* syms,
2662 size_t count,
2663 size_t symndx_offset,
2664 const char* sym_names,
2665 size_t sym_name_size,
2666 Sized_relobj<32, false>::Symbols* sympointers,
2667 size_t* defined);
2668 #endif
2669
2670 #ifdef HAVE_TARGET_64_LITTLE
2671 template
2672 void
2673 Symbol_table::add_from_relobj<64, false>(
2674 Sized_relobj<64, false>* relobj,
2675 const unsigned char* syms,
2676 size_t count,
2677 size_t symndx_offset,
2678 const char* sym_names,
2679 size_t sym_name_size,
2680 Sized_relobj<64, true>::Symbols* sympointers,
2681 size_t* defined);
2682 #endif
2683
2684 #ifdef HAVE_TARGET_64_BIG
2685 template
2686 void
2687 Symbol_table::add_from_relobj<64, true>(
2688 Sized_relobj<64, true>* relobj,
2689 const unsigned char* syms,
2690 size_t count,
2691 size_t symndx_offset,
2692 const char* sym_names,
2693 size_t sym_name_size,
2694 Sized_relobj<64, false>::Symbols* sympointers,
2695 size_t* defined);
2696 #endif
2697
2698 #ifdef HAVE_TARGET_32_LITTLE
2699 template
2700 void
2701 Symbol_table::add_from_dynobj<32, false>(
2702 Sized_dynobj<32, false>* dynobj,
2703 const unsigned char* syms,
2704 size_t count,
2705 const char* sym_names,
2706 size_t sym_name_size,
2707 const unsigned char* versym,
2708 size_t versym_size,
2709 const std::vector<const char*>* version_map,
2710 Sized_relobj<32, false>::Symbols* sympointers,
2711 size_t* defined);
2712 #endif
2713
2714 #ifdef HAVE_TARGET_32_BIG
2715 template
2716 void
2717 Symbol_table::add_from_dynobj<32, true>(
2718 Sized_dynobj<32, true>* dynobj,
2719 const unsigned char* syms,
2720 size_t count,
2721 const char* sym_names,
2722 size_t sym_name_size,
2723 const unsigned char* versym,
2724 size_t versym_size,
2725 const std::vector<const char*>* version_map,
2726 Sized_relobj<32, true>::Symbols* sympointers,
2727 size_t* defined);
2728 #endif
2729
2730 #ifdef HAVE_TARGET_64_LITTLE
2731 template
2732 void
2733 Symbol_table::add_from_dynobj<64, false>(
2734 Sized_dynobj<64, false>* dynobj,
2735 const unsigned char* syms,
2736 size_t count,
2737 const char* sym_names,
2738 size_t sym_name_size,
2739 const unsigned char* versym,
2740 size_t versym_size,
2741 const std::vector<const char*>* version_map,
2742 Sized_relobj<64, false>::Symbols* sympointers,
2743 size_t* defined);
2744 #endif
2745
2746 #ifdef HAVE_TARGET_64_BIG
2747 template
2748 void
2749 Symbol_table::add_from_dynobj<64, true>(
2750 Sized_dynobj<64, true>* dynobj,
2751 const unsigned char* syms,
2752 size_t count,
2753 const char* sym_names,
2754 size_t sym_name_size,
2755 const unsigned char* versym,
2756 size_t versym_size,
2757 const std::vector<const char*>* version_map,
2758 Sized_relobj<64, true>::Symbols* sympointers,
2759 size_t* defined);
2760 #endif
2761
2762 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2763 template
2764 void
2765 Symbol_table::define_with_copy_reloc<32>(
2766 Sized_symbol<32>* sym,
2767 Output_data* posd,
2768 elfcpp::Elf_types<32>::Elf_Addr value);
2769 #endif
2770
2771 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2772 template
2773 void
2774 Symbol_table::define_with_copy_reloc<64>(
2775 Sized_symbol<64>* sym,
2776 Output_data* posd,
2777 elfcpp::Elf_types<64>::Elf_Addr value);
2778 #endif
2779
2780 #ifdef HAVE_TARGET_32_LITTLE
2781 template
2782 void
2783 Warnings::issue_warning<32, false>(const Symbol* sym,
2784 const Relocate_info<32, false>* relinfo,
2785 size_t relnum, off_t reloffset) const;
2786 #endif
2787
2788 #ifdef HAVE_TARGET_32_BIG
2789 template
2790 void
2791 Warnings::issue_warning<32, true>(const Symbol* sym,
2792 const Relocate_info<32, true>* relinfo,
2793 size_t relnum, off_t reloffset) const;
2794 #endif
2795
2796 #ifdef HAVE_TARGET_64_LITTLE
2797 template
2798 void
2799 Warnings::issue_warning<64, false>(const Symbol* sym,
2800 const Relocate_info<64, false>* relinfo,
2801 size_t relnum, off_t reloffset) const;
2802 #endif
2803
2804 #ifdef HAVE_TARGET_64_BIG
2805 template
2806 void
2807 Warnings::issue_warning<64, true>(const Symbol* sym,
2808 const Relocate_info<64, true>* relinfo,
2809 size_t relnum, off_t reloffset) const;
2810 #endif
2811
2812 } // End namespace gold.
This page took 0.139904 seconds and 5 git commands to generate.