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