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