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