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