* symtab.cc (Symbol_table::add_from_object): Rename def parameter
[external/binutils.git] / gold / symtab.cc
1 // symtab.cc -- the gold symbol table
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26 #include <stdint.h>
27 #include <algorithm>
28 #include <set>
29 #include <string>
30 #include <utility>
31 #include "demangle.h"
32
33 #include "gc.h"
34 #include "object.h"
35 #include "dwarf_reader.h"
36 #include "dynobj.h"
37 #include "output.h"
38 #include "target.h"
39 #include "workqueue.h"
40 #include "symtab.h"
41 #include "demangle.h"   // needed for --dynamic-list-cpp-new
42 #include "plugin.h"
43
44 namespace gold
45 {
46
47 // Class Symbol.
48
49 // Initialize fields in Symbol.  This initializes everything except u_
50 // and source_.
51
52 void
53 Symbol::init_fields(const char* name, const char* version,
54                     elfcpp::STT type, elfcpp::STB binding,
55                     elfcpp::STV visibility, unsigned char nonvis)
56 {
57   this->name_ = name;
58   this->version_ = version;
59   this->symtab_index_ = 0;
60   this->dynsym_index_ = 0;
61   this->got_offsets_.init();
62   this->plt_offset_ = 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 IS_DEFAULT_VERSION is true, then this is the definition of a
817 // default version of a symbol.  That means that any lookup of
818 // NAME/NULL and any lookup of NAME/VERSION should always return the
819 // same symbol.  This is obvious for references, but in particular we
820 // want to do this for definitions: overriding NAME/NULL should also
821 // override NAME/VERSION.  If we don't do that, it would be very hard
822 // to 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 is_default_version,
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> insdefault =
887     std::make_pair(this->table_.end(), false);
888   if (is_default_version)
889     {
890       const Stringpool::Key vnull_key = 0;
891       insdefault = 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 (is_default_version)
919         this->define_default_version<size, big_endian>(ret, insdefault.second,
920                                                        insdefault.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 (is_default_version && !insdefault.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>(insdefault.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 (!is_default_version)
959                     this->table_.erase(ins.first);
960                   else
961                     {
962                       this->table_.erase(insdefault.first);
963                       // Inserting INSDEFAULT 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 (is_default_version)
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(insdefault.second);
979               insdefault.first->second = ret;
980             }
981         }
982
983       if (is_default_version)
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       // IS_DEFAULT_VERSION: is the version default?
1085       // IS_FORCED_LOCAL: is the symbol forced local?
1086       bool is_default_version = false;
1087       bool is_forced_local = false;
1088
1089       if (ver != NULL)
1090         {
1091           // The symbol name is of the form foo@VERSION or foo@@VERSION
1092           namelen = ver - name;
1093           ++ver;
1094           if (*ver == '@')
1095             {
1096               is_default_version = true;
1097               ++ver;
1098             }
1099           ver = this->namepool_.add(ver, true, &ver_key);
1100         }
1101       // We don't want to assign a version to an undefined symbol,
1102       // even if it is listed in the version script.  FIXME: What
1103       // about a common symbol?
1104       else
1105         {
1106           namelen = strlen(name);
1107           if (!this->version_script_.empty()
1108               && st_shndx != elfcpp::SHN_UNDEF)
1109             {
1110               // The symbol name did not have a version, but the
1111               // version script may assign a version anyway.
1112               std::string version;
1113               if (this->version_script_.get_symbol_version(name, &version))
1114                 {
1115                   // The version can be empty if the version script is
1116                   // only used to force some symbols to be local.
1117                   if (!version.empty())
1118                     {
1119                       ver = this->namepool_.add_with_length(version.c_str(),
1120                                                             version.length(),
1121                                                             true,
1122                                                             &ver_key);
1123                       is_default_version = true;
1124                     }
1125                 }
1126               else if (this->version_script_.symbol_is_local(name))
1127                 is_forced_local = true;
1128             }
1129         }
1130
1131       elfcpp::Sym<size, big_endian>* psym = &sym;
1132       unsigned char symbuf[sym_size];
1133       elfcpp::Sym<size, big_endian> sym2(symbuf);
1134       if (just_symbols)
1135         {
1136           memcpy(symbuf, p, sym_size);
1137           elfcpp::Sym_write<size, big_endian> sw(symbuf);
1138           if (orig_st_shndx != elfcpp::SHN_UNDEF && is_ordinary)
1139             {
1140               // Symbol values in object files are section relative.
1141               // This is normally what we want, but since here we are
1142               // converting the symbol to absolute we need to add the
1143               // section address.  The section address in an object
1144               // file is normally zero, but people can use a linker
1145               // script to change it.
1146               sw.put_st_value(sym.get_st_value()
1147                               + relobj->section_address(orig_st_shndx));
1148             }
1149           st_shndx = elfcpp::SHN_ABS;
1150           is_ordinary = false;
1151           psym = &sym2;
1152         }
1153
1154       // Fix up visibility if object has no-export set.
1155       if (relobj->no_export()
1156           && (orig_st_shndx != elfcpp::SHN_UNDEF || !is_ordinary))
1157         {
1158           // We may have copied symbol already above.
1159           if (psym != &sym2)
1160             {
1161               memcpy(symbuf, p, sym_size);
1162               psym = &sym2;
1163             }
1164
1165           elfcpp::STV visibility = sym2.get_st_visibility();
1166           if (visibility == elfcpp::STV_DEFAULT
1167               || visibility == elfcpp::STV_PROTECTED)
1168             {
1169               elfcpp::Sym_write<size, big_endian> sw(symbuf);
1170               unsigned char nonvis = sym2.get_st_nonvis();
1171               sw.put_st_other(elfcpp::STV_HIDDEN, nonvis);
1172             }
1173         }
1174
1175       Stringpool::Key name_key;
1176       name = this->namepool_.add_with_length(name, namelen, true,
1177                                              &name_key);
1178
1179       Sized_symbol<size>* res;
1180       res = this->add_from_object(relobj, name, name_key, ver, ver_key,
1181                                   is_default_version, *psym, st_shndx,
1182                                   is_ordinary, orig_st_shndx);
1183       
1184       // If building a shared library using garbage collection, do not 
1185       // treat externally visible symbols as garbage.
1186       if (parameters->options().gc_sections() 
1187           && parameters->options().shared())
1188         this->gc_mark_symbol_for_shlib(res);
1189
1190       if (is_forced_local)
1191         this->force_local(res);
1192
1193       (*sympointers)[i] = res;
1194     }
1195 }
1196
1197 // Add a symbol from a plugin-claimed file.
1198
1199 template<int size, bool big_endian>
1200 Symbol*
1201 Symbol_table::add_from_pluginobj(
1202     Sized_pluginobj<size, big_endian>* obj,
1203     const char* name,
1204     const char* ver,
1205     elfcpp::Sym<size, big_endian>* sym)
1206 {
1207   unsigned int st_shndx = sym->get_st_shndx();
1208   bool is_ordinary = st_shndx < elfcpp::SHN_LORESERVE;
1209
1210   Stringpool::Key ver_key = 0;
1211   bool is_default_version = false;
1212   bool is_forced_local = false;
1213
1214   if (ver != NULL)
1215     {
1216       ver = this->namepool_.add(ver, true, &ver_key);
1217     }
1218   // We don't want to assign a version to an undefined symbol,
1219   // even if it is listed in the version script.  FIXME: What
1220   // about a common symbol?
1221   else
1222     {
1223       if (!this->version_script_.empty()
1224           && st_shndx != elfcpp::SHN_UNDEF)
1225         {
1226           // The symbol name did not have a version, but the
1227           // version script may assign a version anyway.
1228           std::string version;
1229           if (this->version_script_.get_symbol_version(name, &version))
1230             {
1231               // The version can be empty if the version script is
1232               // only used to force some symbols to be local.
1233               if (!version.empty())
1234                 {
1235                   ver = this->namepool_.add_with_length(version.c_str(),
1236                                                         version.length(),
1237                                                         true,
1238                                                         &ver_key);
1239                   is_default_version = true;
1240                 }
1241             }
1242           else if (this->version_script_.symbol_is_local(name))
1243             is_forced_local = true;
1244         }
1245     }
1246
1247   Stringpool::Key name_key;
1248   name = this->namepool_.add(name, true, &name_key);
1249
1250   Sized_symbol<size>* res;
1251   res = this->add_from_object(obj, name, name_key, ver, ver_key,
1252                               is_default_version, *sym, st_shndx,
1253                               is_ordinary, st_shndx);
1254
1255   if (is_forced_local)
1256     this->force_local(res);
1257
1258   return res;
1259 }
1260
1261 // Add all the symbols in a dynamic object to the hash table.
1262
1263 template<int size, bool big_endian>
1264 void
1265 Symbol_table::add_from_dynobj(
1266     Sized_dynobj<size, big_endian>* dynobj,
1267     const unsigned char* syms,
1268     size_t count,
1269     const char* sym_names,
1270     size_t sym_name_size,
1271     const unsigned char* versym,
1272     size_t versym_size,
1273     const std::vector<const char*>* version_map,
1274     typename Sized_relobj<size, big_endian>::Symbols* sympointers,
1275     size_t* defined)
1276 {
1277   *defined = 0;
1278
1279   gold_assert(size == parameters->target().get_size());
1280
1281   if (dynobj->just_symbols())
1282     {
1283       gold_error(_("--just-symbols does not make sense with a shared object"));
1284       return;
1285     }
1286
1287   if (versym != NULL && versym_size / 2 < count)
1288     {
1289       dynobj->error(_("too few symbol versions"));
1290       return;
1291     }
1292
1293   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1294
1295   // We keep a list of all STT_OBJECT symbols, so that we can resolve
1296   // weak aliases.  This is necessary because if the dynamic object
1297   // provides the same variable under two names, one of which is a
1298   // weak definition, and the regular object refers to the weak
1299   // definition, we have to put both the weak definition and the
1300   // strong definition into the dynamic symbol table.  Given a weak
1301   // definition, the only way that we can find the corresponding
1302   // strong definition, if any, is to search the symbol table.
1303   std::vector<Sized_symbol<size>*> object_symbols;
1304
1305   const unsigned char* p = syms;
1306   const unsigned char* vs = versym;
1307   for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
1308     {
1309       elfcpp::Sym<size, big_endian> sym(p);
1310
1311       if (sympointers != NULL)
1312         (*sympointers)[i] = NULL;
1313
1314       // Ignore symbols with local binding or that have
1315       // internal or hidden visibility.
1316       if (sym.get_st_bind() == elfcpp::STB_LOCAL
1317           || sym.get_st_visibility() == elfcpp::STV_INTERNAL
1318           || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
1319         continue;
1320
1321       // A protected symbol in a shared library must be treated as a
1322       // normal symbol when viewed from outside the shared library.
1323       // Implement this by overriding the visibility here.
1324       elfcpp::Sym<size, big_endian>* psym = &sym;
1325       unsigned char symbuf[sym_size];
1326       elfcpp::Sym<size, big_endian> sym2(symbuf);
1327       if (sym.get_st_visibility() == elfcpp::STV_PROTECTED)
1328         {
1329           memcpy(symbuf, p, sym_size);
1330           elfcpp::Sym_write<size, big_endian> sw(symbuf);
1331           sw.put_st_other(elfcpp::STV_DEFAULT, sym.get_st_nonvis());
1332           psym = &sym2;
1333         }
1334
1335       unsigned int st_name = psym->get_st_name();
1336       if (st_name >= sym_name_size)
1337         {
1338           dynobj->error(_("bad symbol name offset %u at %zu"),
1339                         st_name, i);
1340           continue;
1341         }
1342
1343       const char* name = sym_names + st_name;
1344
1345       bool is_ordinary;
1346       unsigned int st_shndx = dynobj->adjust_sym_shndx(i, psym->get_st_shndx(),
1347                                                        &is_ordinary);
1348
1349       if (st_shndx != elfcpp::SHN_UNDEF)
1350         ++*defined;
1351
1352       Sized_symbol<size>* res;
1353
1354       if (versym == NULL)
1355         {
1356           Stringpool::Key name_key;
1357           name = this->namepool_.add(name, true, &name_key);
1358           res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1359                                       false, *psym, st_shndx, is_ordinary,
1360                                       st_shndx);
1361         }
1362       else
1363         {
1364           // Read the version information.
1365
1366           unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
1367
1368           bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
1369           v &= elfcpp::VERSYM_VERSION;
1370
1371           // The Sun documentation says that V can be VER_NDX_LOCAL,
1372           // or VER_NDX_GLOBAL, or a version index.  The meaning of
1373           // VER_NDX_LOCAL is defined as "Symbol has local scope."
1374           // The old GNU linker will happily generate VER_NDX_LOCAL
1375           // for an undefined symbol.  I don't know what the Sun
1376           // linker will generate.
1377
1378           if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1379               && st_shndx != elfcpp::SHN_UNDEF)
1380             {
1381               // This symbol should not be visible outside the object.
1382               continue;
1383             }
1384
1385           // At this point we are definitely going to add this symbol.
1386           Stringpool::Key name_key;
1387           name = this->namepool_.add(name, true, &name_key);
1388
1389           if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
1390               || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
1391             {
1392               // This symbol does not have a version.
1393               res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1394                                           false, *psym, st_shndx, is_ordinary,
1395                                           st_shndx);
1396             }
1397           else
1398             {
1399               if (v >= version_map->size())
1400                 {
1401                   dynobj->error(_("versym for symbol %zu out of range: %u"),
1402                                 i, v);
1403                   continue;
1404                 }
1405
1406               const char* version = (*version_map)[v];
1407               if (version == NULL)
1408                 {
1409                   dynobj->error(_("versym for symbol %zu has no name: %u"),
1410                                 i, v);
1411                   continue;
1412                 }
1413
1414               Stringpool::Key version_key;
1415               version = this->namepool_.add(version, true, &version_key);
1416
1417               // If this is an absolute symbol, and the version name
1418               // and symbol name are the same, then this is the
1419               // version definition symbol.  These symbols exist to
1420               // support using -u to pull in particular versions.  We
1421               // do not want to record a version for them.
1422               if (st_shndx == elfcpp::SHN_ABS
1423                   && !is_ordinary
1424                   && name_key == version_key)
1425                 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
1426                                             false, *psym, st_shndx, is_ordinary,
1427                                             st_shndx);
1428               else
1429                 {
1430                   const bool is_default_version =
1431                     !hidden && st_shndx != elfcpp::SHN_UNDEF;
1432                   res = this->add_from_object(dynobj, name, name_key, version,
1433                                               version_key, is_default_version,
1434                                               *psym, st_shndx,
1435                                               is_ordinary, st_shndx);
1436                 }
1437             }
1438         }
1439
1440       // Note that it is possible that RES was overridden by an
1441       // earlier object, in which case it can't be aliased here.
1442       if (st_shndx != elfcpp::SHN_UNDEF
1443           && is_ordinary
1444           && psym->get_st_type() == elfcpp::STT_OBJECT
1445           && res->source() == Symbol::FROM_OBJECT
1446           && res->object() == dynobj)
1447         object_symbols.push_back(res);
1448
1449       if (sympointers != NULL)
1450         (*sympointers)[i] = res;
1451     }
1452
1453   this->record_weak_aliases(&object_symbols);
1454 }
1455
1456 // This is used to sort weak aliases.  We sort them first by section
1457 // index, then by offset, then by weak ahead of strong.
1458
1459 template<int size>
1460 class Weak_alias_sorter
1461 {
1462  public:
1463   bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
1464 };
1465
1466 template<int size>
1467 bool
1468 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
1469                                     const Sized_symbol<size>* s2) const
1470 {
1471   bool is_ordinary;
1472   unsigned int s1_shndx = s1->shndx(&is_ordinary);
1473   gold_assert(is_ordinary);
1474   unsigned int s2_shndx = s2->shndx(&is_ordinary);
1475   gold_assert(is_ordinary);
1476   if (s1_shndx != s2_shndx)
1477     return s1_shndx < s2_shndx;
1478
1479   if (s1->value() != s2->value())
1480     return s1->value() < s2->value();
1481   if (s1->binding() != s2->binding())
1482     {
1483       if (s1->binding() == elfcpp::STB_WEAK)
1484         return true;
1485       if (s2->binding() == elfcpp::STB_WEAK)
1486         return false;
1487     }
1488   return std::string(s1->name()) < std::string(s2->name());
1489 }
1490
1491 // SYMBOLS is a list of object symbols from a dynamic object.  Look
1492 // for any weak aliases, and record them so that if we add the weak
1493 // alias to the dynamic symbol table, we also add the corresponding
1494 // strong symbol.
1495
1496 template<int size>
1497 void
1498 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
1499 {
1500   // Sort the vector by section index, then by offset, then by weak
1501   // ahead of strong.
1502   std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
1503
1504   // Walk through the vector.  For each weak definition, record
1505   // aliases.
1506   for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
1507          symbols->begin();
1508        p != symbols->end();
1509        ++p)
1510     {
1511       if ((*p)->binding() != elfcpp::STB_WEAK)
1512         continue;
1513
1514       // Build a circular list of weak aliases.  Each symbol points to
1515       // the next one in the circular list.
1516
1517       Sized_symbol<size>* from_sym = *p;
1518       typename std::vector<Sized_symbol<size>*>::const_iterator q;
1519       for (q = p + 1; q != symbols->end(); ++q)
1520         {
1521           bool dummy;
1522           if ((*q)->shndx(&dummy) != from_sym->shndx(&dummy)
1523               || (*q)->value() != from_sym->value())
1524             break;
1525
1526           this->weak_aliases_[from_sym] = *q;
1527           from_sym->set_has_alias();
1528           from_sym = *q;
1529         }
1530
1531       if (from_sym != *p)
1532         {
1533           this->weak_aliases_[from_sym] = *p;
1534           from_sym->set_has_alias();
1535         }
1536
1537       p = q - 1;
1538     }
1539 }
1540
1541 // Create and return a specially defined symbol.  If ONLY_IF_REF is
1542 // true, then only create the symbol if there is a reference to it.
1543 // If this does not return NULL, it sets *POLDSYM to the existing
1544 // symbol if there is one.  This sets *RESOLVE_OLDSYM if we should
1545 // resolve the newly created symbol to the old one.  This
1546 // canonicalizes *PNAME and *PVERSION.
1547
1548 template<int size, bool big_endian>
1549 Sized_symbol<size>*
1550 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1551                                     bool only_if_ref,
1552                                     Sized_symbol<size>** poldsym,
1553                                     bool *resolve_oldsym)
1554 {
1555   *resolve_oldsym = false;
1556
1557   // If the caller didn't give us a version, see if we get one from
1558   // the version script.
1559   std::string v;
1560   bool is_default_version = false;
1561   if (*pversion == NULL)
1562     {
1563       if (this->version_script_.get_symbol_version(*pname, &v))
1564         {
1565           if (!v.empty())
1566             *pversion = v.c_str();
1567
1568           // If we get the version from a version script, then we are
1569           // also the default version.
1570           is_default_version = true;
1571         }
1572     }
1573
1574   Symbol* oldsym;
1575   Sized_symbol<size>* sym;
1576
1577   bool add_to_table = false;
1578   typename Symbol_table_type::iterator add_loc = this->table_.end();
1579   bool add_def_to_table = false;
1580   typename Symbol_table_type::iterator add_def_loc = this->table_.end();
1581
1582   if (only_if_ref)
1583     {
1584       oldsym = this->lookup(*pname, *pversion);
1585       if (oldsym == NULL && is_default_version)
1586         oldsym = this->lookup(*pname, NULL);
1587       if (oldsym == NULL || !oldsym->is_undefined())
1588         return NULL;
1589
1590       *pname = oldsym->name();
1591       if (!is_default_version)
1592         *pversion = oldsym->version();
1593     }
1594   else
1595     {
1596       // Canonicalize NAME and VERSION.
1597       Stringpool::Key name_key;
1598       *pname = this->namepool_.add(*pname, true, &name_key);
1599
1600       Stringpool::Key version_key = 0;
1601       if (*pversion != NULL)
1602         *pversion = this->namepool_.add(*pversion, true, &version_key);
1603
1604       Symbol* const snull = NULL;
1605       std::pair<typename Symbol_table_type::iterator, bool> ins =
1606         this->table_.insert(std::make_pair(std::make_pair(name_key,
1607                                                           version_key),
1608                                            snull));
1609
1610       std::pair<typename Symbol_table_type::iterator, bool> insdefault =
1611         std::make_pair(this->table_.end(), false);
1612       if (is_default_version)
1613         {
1614           const Stringpool::Key vnull = 0;
1615           insdefault =
1616             this->table_.insert(std::make_pair(std::make_pair(name_key,
1617                                                               vnull),
1618                                                snull));
1619         }
1620
1621       if (!ins.second)
1622         {
1623           // We already have a symbol table entry for NAME/VERSION.
1624           oldsym = ins.first->second;
1625           gold_assert(oldsym != NULL);
1626
1627           if (is_default_version)
1628             {
1629               Sized_symbol<size>* soldsym =
1630                 this->get_sized_symbol<size>(oldsym);
1631               this->define_default_version<size, big_endian>(soldsym,
1632                                                              insdefault.second,
1633                                                              insdefault.first);
1634             }
1635         }
1636       else
1637         {
1638           // We haven't seen this symbol before.
1639           gold_assert(ins.first->second == NULL);
1640
1641           add_to_table = true;
1642           add_loc = ins.first;
1643
1644           if (is_default_version && !insdefault.second)
1645             {
1646               // We are adding NAME/VERSION, and it is the default
1647               // version.  We already have an entry for NAME/NULL.
1648               oldsym = insdefault.first->second;
1649               *resolve_oldsym = true;
1650             }
1651           else
1652             {
1653               oldsym = NULL;
1654
1655               if (is_default_version)
1656                 {
1657                   add_def_to_table = true;
1658                   add_def_loc = insdefault.first;
1659                 }
1660             }
1661         }
1662     }
1663
1664   const Target& target = parameters->target();
1665   if (!target.has_make_symbol())
1666     sym = new Sized_symbol<size>();
1667   else
1668     {
1669       Sized_target<size, big_endian>* sized_target =
1670         parameters->sized_target<size, big_endian>();
1671       sym = sized_target->make_symbol();
1672       if (sym == NULL)
1673         return NULL;
1674     }
1675
1676   if (add_to_table)
1677     add_loc->second = sym;
1678   else
1679     gold_assert(oldsym != NULL);
1680
1681   if (add_def_to_table)
1682     add_def_loc->second = sym;
1683
1684   *poldsym = this->get_sized_symbol<size>(oldsym);
1685
1686   return sym;
1687 }
1688
1689 // Define a symbol based on an Output_data.
1690
1691 Symbol*
1692 Symbol_table::define_in_output_data(const char* name,
1693                                     const char* version,
1694                                     Defined defined,
1695                                     Output_data* od,
1696                                     uint64_t value,
1697                                     uint64_t symsize,
1698                                     elfcpp::STT type,
1699                                     elfcpp::STB binding,
1700                                     elfcpp::STV visibility,
1701                                     unsigned char nonvis,
1702                                     bool offset_is_from_end,
1703                                     bool only_if_ref)
1704 {
1705   if (parameters->target().get_size() == 32)
1706     {
1707 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1708       return this->do_define_in_output_data<32>(name, version, defined, od,
1709                                                 value, symsize, type, binding,
1710                                                 visibility, nonvis,
1711                                                 offset_is_from_end,
1712                                                 only_if_ref);
1713 #else
1714       gold_unreachable();
1715 #endif
1716     }
1717   else if (parameters->target().get_size() == 64)
1718     {
1719 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1720       return this->do_define_in_output_data<64>(name, version, defined, od,
1721                                                 value, symsize, type, binding,
1722                                                 visibility, nonvis,
1723                                                 offset_is_from_end,
1724                                                 only_if_ref);
1725 #else
1726       gold_unreachable();
1727 #endif
1728     }
1729   else
1730     gold_unreachable();
1731 }
1732
1733 // Define a symbol in an Output_data, sized version.
1734
1735 template<int size>
1736 Sized_symbol<size>*
1737 Symbol_table::do_define_in_output_data(
1738     const char* name,
1739     const char* version,
1740     Defined defined,
1741     Output_data* od,
1742     typename elfcpp::Elf_types<size>::Elf_Addr value,
1743     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1744     elfcpp::STT type,
1745     elfcpp::STB binding,
1746     elfcpp::STV visibility,
1747     unsigned char nonvis,
1748     bool offset_is_from_end,
1749     bool only_if_ref)
1750 {
1751   Sized_symbol<size>* sym;
1752   Sized_symbol<size>* oldsym;
1753   bool resolve_oldsym;
1754
1755   if (parameters->target().is_big_endian())
1756     {
1757 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1758       sym = this->define_special_symbol<size, true>(&name, &version,
1759                                                     only_if_ref, &oldsym,
1760                                                     &resolve_oldsym);
1761 #else
1762       gold_unreachable();
1763 #endif
1764     }
1765   else
1766     {
1767 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1768       sym = this->define_special_symbol<size, false>(&name, &version,
1769                                                      only_if_ref, &oldsym,
1770                                                      &resolve_oldsym);
1771 #else
1772       gold_unreachable();
1773 #endif
1774     }
1775
1776   if (sym == NULL)
1777     return NULL;
1778
1779   sym->init_output_data(name, version, od, value, symsize, type, binding,
1780                         visibility, nonvis, offset_is_from_end);
1781
1782   if (oldsym == NULL)
1783     {
1784       if (binding == elfcpp::STB_LOCAL
1785           || this->version_script_.symbol_is_local(name))
1786         this->force_local(sym);
1787       else if (version != NULL)
1788         sym->set_is_default();
1789       return sym;
1790     }
1791
1792   if (Symbol_table::should_override_with_special(oldsym, defined))
1793     this->override_with_special(oldsym, sym);
1794
1795   if (resolve_oldsym)
1796     return sym;
1797   else
1798     {
1799       delete sym;
1800       return oldsym;
1801     }
1802 }
1803
1804 // Define a symbol based on an Output_segment.
1805
1806 Symbol*
1807 Symbol_table::define_in_output_segment(const char* name,
1808                                        const char* version,
1809                                        Defined defined,
1810                                        Output_segment* os,
1811                                        uint64_t value,
1812                                        uint64_t symsize,
1813                                        elfcpp::STT type,
1814                                        elfcpp::STB binding,
1815                                        elfcpp::STV visibility,
1816                                        unsigned char nonvis,
1817                                        Symbol::Segment_offset_base offset_base,
1818                                        bool only_if_ref)
1819 {
1820   if (parameters->target().get_size() == 32)
1821     {
1822 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1823       return this->do_define_in_output_segment<32>(name, version, defined, os,
1824                                                    value, symsize, type,
1825                                                    binding, visibility, nonvis,
1826                                                    offset_base, only_if_ref);
1827 #else
1828       gold_unreachable();
1829 #endif
1830     }
1831   else if (parameters->target().get_size() == 64)
1832     {
1833 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1834       return this->do_define_in_output_segment<64>(name, version, defined, os,
1835                                                    value, symsize, type,
1836                                                    binding, visibility, nonvis,
1837                                                    offset_base, only_if_ref);
1838 #else
1839       gold_unreachable();
1840 #endif
1841     }
1842   else
1843     gold_unreachable();
1844 }
1845
1846 // Define a symbol in an Output_segment, sized version.
1847
1848 template<int size>
1849 Sized_symbol<size>*
1850 Symbol_table::do_define_in_output_segment(
1851     const char* name,
1852     const char* version,
1853     Defined defined,
1854     Output_segment* os,
1855     typename elfcpp::Elf_types<size>::Elf_Addr value,
1856     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1857     elfcpp::STT type,
1858     elfcpp::STB binding,
1859     elfcpp::STV visibility,
1860     unsigned char nonvis,
1861     Symbol::Segment_offset_base offset_base,
1862     bool only_if_ref)
1863 {
1864   Sized_symbol<size>* sym;
1865   Sized_symbol<size>* oldsym;
1866   bool resolve_oldsym;
1867
1868   if (parameters->target().is_big_endian())
1869     {
1870 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1871       sym = this->define_special_symbol<size, true>(&name, &version,
1872                                                     only_if_ref, &oldsym,
1873                                                     &resolve_oldsym);
1874 #else
1875       gold_unreachable();
1876 #endif
1877     }
1878   else
1879     {
1880 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1881       sym = this->define_special_symbol<size, false>(&name, &version,
1882                                                      only_if_ref, &oldsym,
1883                                                      &resolve_oldsym);
1884 #else
1885       gold_unreachable();
1886 #endif
1887     }
1888
1889   if (sym == NULL)
1890     return NULL;
1891
1892   sym->init_output_segment(name, version, os, value, symsize, type, binding,
1893                            visibility, nonvis, offset_base);
1894
1895   if (oldsym == NULL)
1896     {
1897       if (binding == elfcpp::STB_LOCAL
1898           || this->version_script_.symbol_is_local(name))
1899         this->force_local(sym);
1900       else if (version != NULL)
1901         sym->set_is_default();
1902       return sym;
1903     }
1904
1905   if (Symbol_table::should_override_with_special(oldsym, defined))
1906     this->override_with_special(oldsym, sym);
1907
1908   if (resolve_oldsym)
1909     return sym;
1910   else
1911     {
1912       delete sym;
1913       return oldsym;
1914     }
1915 }
1916
1917 // Define a special symbol with a constant value.  It is a multiple
1918 // definition error if this symbol is already defined.
1919
1920 Symbol*
1921 Symbol_table::define_as_constant(const char* name,
1922                                  const char* version,
1923                                  Defined defined,
1924                                  uint64_t value,
1925                                  uint64_t symsize,
1926                                  elfcpp::STT type,
1927                                  elfcpp::STB binding,
1928                                  elfcpp::STV visibility,
1929                                  unsigned char nonvis,
1930                                  bool only_if_ref,
1931                                  bool force_override)
1932 {
1933   if (parameters->target().get_size() == 32)
1934     {
1935 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1936       return this->do_define_as_constant<32>(name, version, defined, value,
1937                                              symsize, type, binding,
1938                                              visibility, nonvis, only_if_ref,
1939                                              force_override);
1940 #else
1941       gold_unreachable();
1942 #endif
1943     }
1944   else if (parameters->target().get_size() == 64)
1945     {
1946 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1947       return this->do_define_as_constant<64>(name, version, defined, value,
1948                                              symsize, type, binding,
1949                                              visibility, nonvis, only_if_ref,
1950                                              force_override);
1951 #else
1952       gold_unreachable();
1953 #endif
1954     }
1955   else
1956     gold_unreachable();
1957 }
1958
1959 // Define a symbol as a constant, sized version.
1960
1961 template<int size>
1962 Sized_symbol<size>*
1963 Symbol_table::do_define_as_constant(
1964     const char* name,
1965     const char* version,
1966     Defined defined,
1967     typename elfcpp::Elf_types<size>::Elf_Addr value,
1968     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1969     elfcpp::STT type,
1970     elfcpp::STB binding,
1971     elfcpp::STV visibility,
1972     unsigned char nonvis,
1973     bool only_if_ref,
1974     bool force_override)
1975 {
1976   Sized_symbol<size>* sym;
1977   Sized_symbol<size>* oldsym;
1978   bool resolve_oldsym;
1979
1980   if (parameters->target().is_big_endian())
1981     {
1982 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1983       sym = this->define_special_symbol<size, true>(&name, &version,
1984                                                     only_if_ref, &oldsym,
1985                                                     &resolve_oldsym);
1986 #else
1987       gold_unreachable();
1988 #endif
1989     }
1990   else
1991     {
1992 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1993       sym = this->define_special_symbol<size, false>(&name, &version,
1994                                                      only_if_ref, &oldsym,
1995                                                      &resolve_oldsym);
1996 #else
1997       gold_unreachable();
1998 #endif
1999     }
2000
2001   if (sym == NULL)
2002     return NULL;
2003
2004   sym->init_constant(name, version, value, symsize, type, binding, visibility,
2005                      nonvis);
2006
2007   if (oldsym == NULL)
2008     {
2009       // Version symbols are absolute symbols with name == version.
2010       // We don't want to force them to be local.
2011       if ((version == NULL
2012            || name != version
2013            || value != 0)
2014           && (binding == elfcpp::STB_LOCAL
2015               || this->version_script_.symbol_is_local(name)))
2016         this->force_local(sym);
2017       else if (version != NULL
2018                && (name != version || value != 0))
2019         sym->set_is_default();
2020       return sym;
2021     }
2022
2023   if (force_override
2024       || Symbol_table::should_override_with_special(oldsym, defined))
2025     this->override_with_special(oldsym, sym);
2026
2027   if (resolve_oldsym)
2028     return sym;
2029   else
2030     {
2031       delete sym;
2032       return oldsym;
2033     }
2034 }
2035
2036 // Define a set of symbols in output sections.
2037
2038 void
2039 Symbol_table::define_symbols(const Layout* layout, int count,
2040                              const Define_symbol_in_section* p,
2041                              bool only_if_ref)
2042 {
2043   for (int i = 0; i < count; ++i, ++p)
2044     {
2045       Output_section* os = layout->find_output_section(p->output_section);
2046       if (os != NULL)
2047         this->define_in_output_data(p->name, NULL, PREDEFINED, os, p->value,
2048                                     p->size, p->type, p->binding,
2049                                     p->visibility, p->nonvis,
2050                                     p->offset_is_from_end,
2051                                     only_if_ref || p->only_if_ref);
2052       else
2053         this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2054                                  p->type, p->binding, p->visibility, p->nonvis,
2055                                  only_if_ref || p->only_if_ref,
2056                                  false);
2057     }
2058 }
2059
2060 // Define a set of symbols in output segments.
2061
2062 void
2063 Symbol_table::define_symbols(const Layout* layout, int count,
2064                              const Define_symbol_in_segment* p,
2065                              bool only_if_ref)
2066 {
2067   for (int i = 0; i < count; ++i, ++p)
2068     {
2069       Output_segment* os = layout->find_output_segment(p->segment_type,
2070                                                        p->segment_flags_set,
2071                                                        p->segment_flags_clear);
2072       if (os != NULL)
2073         this->define_in_output_segment(p->name, NULL, PREDEFINED, os, p->value,
2074                                        p->size, p->type, p->binding,
2075                                        p->visibility, p->nonvis,
2076                                        p->offset_base,
2077                                        only_if_ref || p->only_if_ref);
2078       else
2079         this->define_as_constant(p->name, NULL, PREDEFINED, 0, p->size,
2080                                  p->type, p->binding, p->visibility, p->nonvis,
2081                                  only_if_ref || p->only_if_ref,
2082                                  false);
2083     }
2084 }
2085
2086 // Define CSYM using a COPY reloc.  POSD is the Output_data where the
2087 // symbol should be defined--typically a .dyn.bss section.  VALUE is
2088 // the offset within POSD.
2089
2090 template<int size>
2091 void
2092 Symbol_table::define_with_copy_reloc(
2093     Sized_symbol<size>* csym,
2094     Output_data* posd,
2095     typename elfcpp::Elf_types<size>::Elf_Addr value)
2096 {
2097   gold_assert(csym->is_from_dynobj());
2098   gold_assert(!csym->is_copied_from_dynobj());
2099   Object* object = csym->object();
2100   gold_assert(object->is_dynamic());
2101   Dynobj* dynobj = static_cast<Dynobj*>(object);
2102
2103   // Our copied variable has to override any variable in a shared
2104   // library.
2105   elfcpp::STB binding = csym->binding();
2106   if (binding == elfcpp::STB_WEAK)
2107     binding = elfcpp::STB_GLOBAL;
2108
2109   this->define_in_output_data(csym->name(), csym->version(), COPY,
2110                               posd, value, csym->symsize(),
2111                               csym->type(), binding,
2112                               csym->visibility(), csym->nonvis(),
2113                               false, false);
2114
2115   csym->set_is_copied_from_dynobj();
2116   csym->set_needs_dynsym_entry();
2117
2118   this->copied_symbol_dynobjs_[csym] = dynobj;
2119
2120   // We have now defined all aliases, but we have not entered them all
2121   // in the copied_symbol_dynobjs_ map.
2122   if (csym->has_alias())
2123     {
2124       Symbol* sym = csym;
2125       while (true)
2126         {
2127           sym = this->weak_aliases_[sym];
2128           if (sym == csym)
2129             break;
2130           gold_assert(sym->output_data() == posd);
2131
2132           sym->set_is_copied_from_dynobj();
2133           this->copied_symbol_dynobjs_[sym] = dynobj;
2134         }
2135     }
2136 }
2137
2138 // SYM is defined using a COPY reloc.  Return the dynamic object where
2139 // the original definition was found.
2140
2141 Dynobj*
2142 Symbol_table::get_copy_source(const Symbol* sym) const
2143 {
2144   gold_assert(sym->is_copied_from_dynobj());
2145   Copied_symbol_dynobjs::const_iterator p =
2146     this->copied_symbol_dynobjs_.find(sym);
2147   gold_assert(p != this->copied_symbol_dynobjs_.end());
2148   return p->second;
2149 }
2150
2151 // Add any undefined symbols named on the command line.
2152
2153 void
2154 Symbol_table::add_undefined_symbols_from_command_line()
2155 {
2156   if (parameters->options().any_undefined())
2157     {
2158       if (parameters->target().get_size() == 32)
2159         {
2160 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2161           this->do_add_undefined_symbols_from_command_line<32>();
2162 #else
2163           gold_unreachable();
2164 #endif
2165         }
2166       else if (parameters->target().get_size() == 64)
2167         {
2168 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2169           this->do_add_undefined_symbols_from_command_line<64>();
2170 #else
2171           gold_unreachable();
2172 #endif
2173         }
2174       else
2175         gold_unreachable();
2176     }
2177 }
2178
2179 template<int size>
2180 void
2181 Symbol_table::do_add_undefined_symbols_from_command_line()
2182 {
2183   for (options::String_set::const_iterator p =
2184          parameters->options().undefined_begin();
2185        p != parameters->options().undefined_end();
2186        ++p)
2187     {
2188       const char* name = p->c_str();
2189
2190       if (this->lookup(name) != NULL)
2191         continue;
2192
2193       const char* version = NULL;
2194
2195       Sized_symbol<size>* sym;
2196       Sized_symbol<size>* oldsym;
2197       bool resolve_oldsym;
2198       if (parameters->target().is_big_endian())
2199         {
2200 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
2201           sym = this->define_special_symbol<size, true>(&name, &version,
2202                                                         false, &oldsym,
2203                                                         &resolve_oldsym);
2204 #else
2205           gold_unreachable();
2206 #endif
2207         }
2208       else
2209         {
2210 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
2211           sym = this->define_special_symbol<size, false>(&name, &version,
2212                                                          false, &oldsym,
2213                                                          &resolve_oldsym);
2214 #else
2215           gold_unreachable();
2216 #endif
2217         }
2218
2219       gold_assert(oldsym == NULL);
2220
2221       sym->init_undefined(name, version, elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
2222                           elfcpp::STV_DEFAULT, 0);
2223       ++this->saw_undefined_;
2224     }
2225 }
2226
2227 // Set the dynamic symbol indexes.  INDEX is the index of the first
2228 // global dynamic symbol.  Pointers to the symbols are stored into the
2229 // vector SYMS.  The names are added to DYNPOOL.  This returns an
2230 // updated dynamic symbol index.
2231
2232 unsigned int
2233 Symbol_table::set_dynsym_indexes(unsigned int index,
2234                                  std::vector<Symbol*>* syms,
2235                                  Stringpool* dynpool,
2236                                  Versions* versions)
2237 {
2238   for (Symbol_table_type::iterator p = this->table_.begin();
2239        p != this->table_.end();
2240        ++p)
2241     {
2242       Symbol* sym = p->second;
2243
2244       // Note that SYM may already have a dynamic symbol index, since
2245       // some symbols appear more than once in the symbol table, with
2246       // and without a version.
2247
2248       if (!sym->should_add_dynsym_entry())
2249         sym->set_dynsym_index(-1U);
2250       else if (!sym->has_dynsym_index())
2251         {
2252           sym->set_dynsym_index(index);
2253           ++index;
2254           syms->push_back(sym);
2255           dynpool->add(sym->name(), false, NULL);
2256
2257           // Record any version information.
2258           if (sym->version() != NULL)
2259             versions->record_version(this, dynpool, sym);
2260
2261           // If the symbol is defined in a dynamic object and is
2262           // referenced in a regular object, then mark the dynamic
2263           // object as needed.  This is used to implement --as-needed.
2264           if (sym->is_from_dynobj() && sym->in_reg())
2265             sym->object()->set_is_needed();
2266         }
2267     }
2268
2269   // Finish up the versions.  In some cases this may add new dynamic
2270   // symbols.
2271   index = versions->finalize(this, index, syms);
2272
2273   return index;
2274 }
2275
2276 // Set the final values for all the symbols.  The index of the first
2277 // global symbol in the output file is *PLOCAL_SYMCOUNT.  Record the
2278 // file offset OFF.  Add their names to POOL.  Return the new file
2279 // offset.  Update *PLOCAL_SYMCOUNT if necessary.
2280
2281 off_t
2282 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
2283                        size_t dyncount, Stringpool* pool,
2284                        unsigned int *plocal_symcount)
2285 {
2286   off_t ret;
2287
2288   gold_assert(*plocal_symcount != 0);
2289   this->first_global_index_ = *plocal_symcount;
2290
2291   this->dynamic_offset_ = dynoff;
2292   this->first_dynamic_global_index_ = dyn_global_index;
2293   this->dynamic_count_ = dyncount;
2294
2295   if (parameters->target().get_size() == 32)
2296     {
2297 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
2298       ret = this->sized_finalize<32>(off, pool, plocal_symcount);
2299 #else
2300       gold_unreachable();
2301 #endif
2302     }
2303   else if (parameters->target().get_size() == 64)
2304     {
2305 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
2306       ret = this->sized_finalize<64>(off, pool, plocal_symcount);
2307 #else
2308       gold_unreachable();
2309 #endif
2310     }
2311   else
2312     gold_unreachable();
2313
2314   // Now that we have the final symbol table, we can reliably note
2315   // which symbols should get warnings.
2316   this->warnings_.note_warnings(this);
2317
2318   return ret;
2319 }
2320
2321 // SYM is going into the symbol table at *PINDEX.  Add the name to
2322 // POOL, update *PINDEX and *POFF.
2323
2324 template<int size>
2325 void
2326 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
2327                                   unsigned int* pindex, off_t* poff)
2328 {
2329   sym->set_symtab_index(*pindex);
2330   pool->add(sym->name(), false, NULL);
2331   ++*pindex;
2332   *poff += elfcpp::Elf_sizes<size>::sym_size;
2333 }
2334
2335 // Set the final value for all the symbols.  This is called after
2336 // Layout::finalize, so all the output sections have their final
2337 // address.
2338
2339 template<int size>
2340 off_t
2341 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
2342                              unsigned int* plocal_symcount)
2343 {
2344   off = align_address(off, size >> 3);
2345   this->offset_ = off;
2346
2347   unsigned int index = *plocal_symcount;
2348   const unsigned int orig_index = index;
2349
2350   // First do all the symbols which have been forced to be local, as
2351   // they must appear before all global symbols.
2352   for (Forced_locals::iterator p = this->forced_locals_.begin();
2353        p != this->forced_locals_.end();
2354        ++p)
2355     {
2356       Symbol* sym = *p;
2357       gold_assert(sym->is_forced_local());
2358       if (this->sized_finalize_symbol<size>(sym))
2359         {
2360           this->add_to_final_symtab<size>(sym, pool, &index, &off);
2361           ++*plocal_symcount;
2362         }
2363     }
2364
2365   // Now do all the remaining symbols.
2366   for (Symbol_table_type::iterator p = this->table_.begin();
2367        p != this->table_.end();
2368        ++p)
2369     {
2370       Symbol* sym = p->second;
2371       if (this->sized_finalize_symbol<size>(sym))
2372         this->add_to_final_symtab<size>(sym, pool, &index, &off);
2373     }
2374
2375   this->output_count_ = index - orig_index;
2376
2377   return off;
2378 }
2379
2380 // Compute the final value of SYM and store status in location PSTATUS.
2381 // During relaxation, this may be called multiple times for a symbol to
2382 // compute its would-be final value in each relaxation pass.
2383
2384 template<int size>
2385 typename Sized_symbol<size>::Value_type
2386 Symbol_table::compute_final_value(
2387     const Sized_symbol<size>* sym,
2388     Compute_final_value_status* pstatus) const
2389 {
2390   typedef typename Sized_symbol<size>::Value_type Value_type;
2391   Value_type value;
2392
2393   switch (sym->source())
2394     {
2395     case Symbol::FROM_OBJECT:
2396       {
2397         bool is_ordinary;
2398         unsigned int shndx = sym->shndx(&is_ordinary);
2399
2400         if (!is_ordinary
2401             && shndx != elfcpp::SHN_ABS
2402             && !Symbol::is_common_shndx(shndx))
2403           {
2404             *pstatus = CFVS_UNSUPPORTED_SYMBOL_SECTION;
2405             return 0;
2406           }
2407
2408         Object* symobj = sym->object();
2409         if (symobj->is_dynamic())
2410           {
2411             value = 0;
2412             shndx = elfcpp::SHN_UNDEF;
2413           }
2414         else if (symobj->pluginobj() != NULL)
2415           {
2416             value = 0;
2417             shndx = elfcpp::SHN_UNDEF;
2418           }
2419         else if (shndx == elfcpp::SHN_UNDEF)
2420           value = 0;
2421         else if (!is_ordinary
2422                  && (shndx == elfcpp::SHN_ABS
2423                      || Symbol::is_common_shndx(shndx)))
2424           value = sym->value();
2425         else
2426           {
2427             Relobj* relobj = static_cast<Relobj*>(symobj);
2428             Output_section* os = relobj->output_section(shndx);
2429
2430             if (this->is_section_folded(relobj, shndx))
2431               {
2432                 gold_assert(os == NULL);
2433                 // Get the os of the section it is folded onto.
2434                 Section_id folded = this->icf_->get_folded_section(relobj,
2435                                                                    shndx);
2436                 gold_assert(folded.first != NULL);
2437                 Relobj* folded_obj = reinterpret_cast<Relobj*>(folded.first);
2438                 unsigned folded_shndx = folded.second;
2439
2440                 os = folded_obj->output_section(folded_shndx);  
2441                 gold_assert(os != NULL);
2442
2443                 // Replace (relobj, shndx) with canonical ICF input section.
2444                 shndx = folded_shndx;
2445                 relobj = folded_obj;
2446               }
2447
2448             uint64_t secoff64 = relobj->output_section_offset(shndx);
2449             if (os == NULL)
2450               {
2451                 bool static_or_reloc = (parameters->doing_static_link() ||
2452                                         parameters->options().relocatable());
2453                 gold_assert(static_or_reloc || sym->dynsym_index() == -1U);
2454
2455                 *pstatus = CFVS_NO_OUTPUT_SECTION;
2456                 return 0;
2457               }
2458
2459             if (secoff64 == -1ULL)
2460               {
2461                 // The section needs special handling (e.g., a merge section).
2462
2463                 value = os->output_address(relobj, shndx, sym->value());
2464               }
2465             else
2466               {
2467                 Value_type secoff =
2468                   convert_types<Value_type, uint64_t>(secoff64);
2469                 if (sym->type() == elfcpp::STT_TLS)
2470                   value = sym->value() + os->tls_offset() + secoff;
2471                 else
2472                   value = sym->value() + os->address() + secoff;
2473               }
2474           }
2475       }
2476       break;
2477
2478     case Symbol::IN_OUTPUT_DATA:
2479       {
2480         Output_data* od = sym->output_data();
2481         value = sym->value();
2482         if (sym->type() != elfcpp::STT_TLS)
2483           value += od->address();
2484         else
2485           {
2486             Output_section* os = od->output_section();
2487             gold_assert(os != NULL);
2488             value += os->tls_offset() + (od->address() - os->address());
2489           }
2490         if (sym->offset_is_from_end())
2491           value += od->data_size();
2492       }
2493       break;
2494
2495     case Symbol::IN_OUTPUT_SEGMENT:
2496       {
2497         Output_segment* os = sym->output_segment();
2498         value = sym->value();
2499         if (sym->type() != elfcpp::STT_TLS)
2500           value += os->vaddr();
2501         switch (sym->offset_base())
2502           {
2503           case Symbol::SEGMENT_START:
2504             break;
2505           case Symbol::SEGMENT_END:
2506             value += os->memsz();
2507             break;
2508           case Symbol::SEGMENT_BSS:
2509             value += os->filesz();
2510             break;
2511           default:
2512             gold_unreachable();
2513           }
2514       }
2515       break;
2516
2517     case Symbol::IS_CONSTANT:
2518       value = sym->value();
2519       break;
2520
2521     case Symbol::IS_UNDEFINED:
2522       value = 0;
2523       break;
2524
2525     default:
2526       gold_unreachable();
2527     }
2528
2529   *pstatus = CFVS_OK;
2530   return value;
2531 }
2532
2533 // Finalize the symbol SYM.  This returns true if the symbol should be
2534 // added to the symbol table, false otherwise.
2535
2536 template<int size>
2537 bool
2538 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
2539 {
2540   typedef typename Sized_symbol<size>::Value_type Value_type;
2541
2542   Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
2543
2544   // The default version of a symbol may appear twice in the symbol
2545   // table.  We only need to finalize it once.
2546   if (sym->has_symtab_index())
2547     return false;
2548
2549   if (!sym->in_reg())
2550     {
2551       gold_assert(!sym->has_symtab_index());
2552       sym->set_symtab_index(-1U);
2553       gold_assert(sym->dynsym_index() == -1U);
2554       return false;
2555     }
2556
2557   // Compute final symbol value.
2558   Compute_final_value_status status;
2559   Value_type value = this->compute_final_value(sym, &status);
2560
2561   switch (status)
2562     {
2563     case CFVS_OK:
2564       break;
2565     case CFVS_UNSUPPORTED_SYMBOL_SECTION:
2566       {
2567         bool is_ordinary;
2568         unsigned int shndx = sym->shndx(&is_ordinary);
2569         gold_error(_("%s: unsupported symbol section 0x%x"),
2570                    sym->demangled_name().c_str(), shndx);
2571       }
2572       break;
2573     case CFVS_NO_OUTPUT_SECTION:
2574       sym->set_symtab_index(-1U);
2575       return false;
2576     default:
2577       gold_unreachable();
2578     }
2579
2580   sym->set_value(value);
2581
2582   if (parameters->options().strip_all()
2583       || !parameters->options().should_retain_symbol(sym->name()))
2584     {
2585       sym->set_symtab_index(-1U);
2586       return false;
2587     }
2588
2589   return true;
2590 }
2591
2592 // Write out the global symbols.
2593
2594 void
2595 Symbol_table::write_globals(const Stringpool* sympool,
2596                             const Stringpool* dynpool,
2597                             Output_symtab_xindex* symtab_xindex,
2598                             Output_symtab_xindex* dynsym_xindex,
2599                             Output_file* of) const
2600 {
2601   switch (parameters->size_and_endianness())
2602     {
2603 #ifdef HAVE_TARGET_32_LITTLE
2604     case Parameters::TARGET_32_LITTLE:
2605       this->sized_write_globals<32, false>(sympool, dynpool, symtab_xindex,
2606                                            dynsym_xindex, of);
2607       break;
2608 #endif
2609 #ifdef HAVE_TARGET_32_BIG
2610     case Parameters::TARGET_32_BIG:
2611       this->sized_write_globals<32, true>(sympool, dynpool, symtab_xindex,
2612                                           dynsym_xindex, of);
2613       break;
2614 #endif
2615 #ifdef HAVE_TARGET_64_LITTLE
2616     case Parameters::TARGET_64_LITTLE:
2617       this->sized_write_globals<64, false>(sympool, dynpool, symtab_xindex,
2618                                            dynsym_xindex, of);
2619       break;
2620 #endif
2621 #ifdef HAVE_TARGET_64_BIG
2622     case Parameters::TARGET_64_BIG:
2623       this->sized_write_globals<64, true>(sympool, dynpool, symtab_xindex,
2624                                           dynsym_xindex, of);
2625       break;
2626 #endif
2627     default:
2628       gold_unreachable();
2629     }
2630 }
2631
2632 // Write out the global symbols.
2633
2634 template<int size, bool big_endian>
2635 void
2636 Symbol_table::sized_write_globals(const Stringpool* sympool,
2637                                   const Stringpool* dynpool,
2638                                   Output_symtab_xindex* symtab_xindex,
2639                                   Output_symtab_xindex* dynsym_xindex,
2640                                   Output_file* of) const
2641 {
2642   const Target& target = parameters->target();
2643
2644   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2645
2646   const unsigned int output_count = this->output_count_;
2647   const section_size_type oview_size = output_count * sym_size;
2648   const unsigned int first_global_index = this->first_global_index_;
2649   unsigned char* psyms;
2650   if (this->offset_ == 0 || output_count == 0)
2651     psyms = NULL;
2652   else
2653     psyms = of->get_output_view(this->offset_, oview_size);
2654
2655   const unsigned int dynamic_count = this->dynamic_count_;
2656   const section_size_type dynamic_size = dynamic_count * sym_size;
2657   const unsigned int first_dynamic_global_index =
2658     this->first_dynamic_global_index_;
2659   unsigned char* dynamic_view;
2660   if (this->dynamic_offset_ == 0 || dynamic_count == 0)
2661     dynamic_view = NULL;
2662   else
2663     dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
2664
2665   for (Symbol_table_type::const_iterator p = this->table_.begin();
2666        p != this->table_.end();
2667        ++p)
2668     {
2669       Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
2670
2671       // Possibly warn about unresolved symbols in shared libraries.
2672       this->warn_about_undefined_dynobj_symbol(sym);
2673
2674       unsigned int sym_index = sym->symtab_index();
2675       unsigned int dynsym_index;
2676       if (dynamic_view == NULL)
2677         dynsym_index = -1U;
2678       else
2679         dynsym_index = sym->dynsym_index();
2680
2681       if (sym_index == -1U && dynsym_index == -1U)
2682         {
2683           // This symbol is not included in the output file.
2684           continue;
2685         }
2686
2687       unsigned int shndx;
2688       typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
2689       typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
2690       switch (sym->source())
2691         {
2692         case Symbol::FROM_OBJECT:
2693           {
2694             bool is_ordinary;
2695             unsigned int in_shndx = sym->shndx(&is_ordinary);
2696
2697             if (!is_ordinary
2698                 && in_shndx != elfcpp::SHN_ABS
2699                 && !Symbol::is_common_shndx(in_shndx))
2700               {
2701                 gold_error(_("%s: unsupported symbol section 0x%x"),
2702                            sym->demangled_name().c_str(), in_shndx);
2703                 shndx = in_shndx;
2704               }
2705             else
2706               {
2707                 Object* symobj = sym->object();
2708                 if (symobj->is_dynamic())
2709                   {
2710                     if (sym->needs_dynsym_value())
2711                       dynsym_value = target.dynsym_value(sym);
2712                     shndx = elfcpp::SHN_UNDEF;
2713                   }
2714                 else if (symobj->pluginobj() != NULL)
2715                   shndx = elfcpp::SHN_UNDEF;
2716                 else if (in_shndx == elfcpp::SHN_UNDEF
2717                          || (!is_ordinary
2718                              && (in_shndx == elfcpp::SHN_ABS
2719                                  || Symbol::is_common_shndx(in_shndx))))
2720                   shndx = in_shndx;
2721                 else
2722                   {
2723                     Relobj* relobj = static_cast<Relobj*>(symobj);
2724                     Output_section* os = relobj->output_section(in_shndx);
2725                     if (this->is_section_folded(relobj, in_shndx))
2726                       {
2727                         // This global symbol must be written out even though
2728                         // it is folded.
2729                         // Get the os of the section it is folded onto.
2730                         Section_id folded =
2731                              this->icf_->get_folded_section(relobj, in_shndx);
2732                         gold_assert(folded.first !=NULL);
2733                         Relobj* folded_obj = 
2734                           reinterpret_cast<Relobj*>(folded.first);
2735                         os = folded_obj->output_section(folded.second);  
2736                         gold_assert(os != NULL);
2737                       }
2738                     gold_assert(os != NULL);
2739                     shndx = os->out_shndx();
2740
2741                     if (shndx >= elfcpp::SHN_LORESERVE)
2742                       {
2743                         if (sym_index != -1U)
2744                           symtab_xindex->add(sym_index, shndx);
2745                         if (dynsym_index != -1U)
2746                           dynsym_xindex->add(dynsym_index, shndx);
2747                         shndx = elfcpp::SHN_XINDEX;
2748                       }
2749
2750                     // In object files symbol values are section
2751                     // relative.
2752                     if (parameters->options().relocatable())
2753                       sym_value -= os->address();
2754                   }
2755               }
2756           }
2757           break;
2758
2759         case Symbol::IN_OUTPUT_DATA:
2760           shndx = sym->output_data()->out_shndx();
2761           if (shndx >= elfcpp::SHN_LORESERVE)
2762             {
2763               if (sym_index != -1U)
2764                 symtab_xindex->add(sym_index, shndx);
2765               if (dynsym_index != -1U)
2766                 dynsym_xindex->add(dynsym_index, shndx);
2767               shndx = elfcpp::SHN_XINDEX;
2768             }
2769           break;
2770
2771         case Symbol::IN_OUTPUT_SEGMENT:
2772           shndx = elfcpp::SHN_ABS;
2773           break;
2774
2775         case Symbol::IS_CONSTANT:
2776           shndx = elfcpp::SHN_ABS;
2777           break;
2778
2779         case Symbol::IS_UNDEFINED:
2780           shndx = elfcpp::SHN_UNDEF;
2781           break;
2782
2783         default:
2784           gold_unreachable();
2785         }
2786
2787       if (sym_index != -1U)
2788         {
2789           sym_index -= first_global_index;
2790           gold_assert(sym_index < output_count);
2791           unsigned char* ps = psyms + (sym_index * sym_size);
2792           this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
2793                                                      sympool, ps);
2794         }
2795
2796       if (dynsym_index != -1U)
2797         {
2798           dynsym_index -= first_dynamic_global_index;
2799           gold_assert(dynsym_index < dynamic_count);
2800           unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
2801           this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
2802                                                      dynpool, pd);
2803         }
2804     }
2805
2806   of->write_output_view(this->offset_, oview_size, psyms);
2807   if (dynamic_view != NULL)
2808     of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
2809 }
2810
2811 // Write out the symbol SYM, in section SHNDX, to P.  POOL is the
2812 // strtab holding the name.
2813
2814 template<int size, bool big_endian>
2815 void
2816 Symbol_table::sized_write_symbol(
2817     Sized_symbol<size>* sym,
2818     typename elfcpp::Elf_types<size>::Elf_Addr value,
2819     unsigned int shndx,
2820     const Stringpool* pool,
2821     unsigned char* p) const
2822 {
2823   elfcpp::Sym_write<size, big_endian> osym(p);
2824   osym.put_st_name(pool->get_offset(sym->name()));
2825   osym.put_st_value(value);
2826   // Use a symbol size of zero for undefined symbols from shared libraries.
2827   if (shndx == elfcpp::SHN_UNDEF && sym->is_from_dynobj())
2828     osym.put_st_size(0);
2829   else
2830     osym.put_st_size(sym->symsize());
2831   elfcpp::STT type = sym->type();
2832   // Turn IFUNC symbols from shared libraries into normal FUNC symbols.
2833   if (type == elfcpp::STT_GNU_IFUNC
2834       && sym->is_from_dynobj())
2835     type = elfcpp::STT_FUNC;
2836   // A version script may have overridden the default binding.
2837   if (sym->is_forced_local())
2838     osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, type));
2839   else
2840     osym.put_st_info(elfcpp::elf_st_info(sym->binding(), type));
2841   osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
2842   osym.put_st_shndx(shndx);
2843 }
2844
2845 // Check for unresolved symbols in shared libraries.  This is
2846 // controlled by the --allow-shlib-undefined option.
2847
2848 // We only warn about libraries for which we have seen all the
2849 // DT_NEEDED entries.  We don't try to track down DT_NEEDED entries
2850 // which were not seen in this link.  If we didn't see a DT_NEEDED
2851 // entry, we aren't going to be able to reliably report whether the
2852 // symbol is undefined.
2853
2854 // We also don't warn about libraries found in a system library
2855 // directory (e.g., /lib or /usr/lib); we assume that those libraries
2856 // are OK.  This heuristic avoids problems on GNU/Linux, in which -ldl
2857 // can have undefined references satisfied by ld-linux.so.
2858
2859 inline void
2860 Symbol_table::warn_about_undefined_dynobj_symbol(Symbol* sym) const
2861 {
2862   bool dummy;
2863   if (sym->source() == Symbol::FROM_OBJECT
2864       && sym->object()->is_dynamic()
2865       && sym->shndx(&dummy) == elfcpp::SHN_UNDEF
2866       && sym->binding() != elfcpp::STB_WEAK
2867       && !parameters->options().allow_shlib_undefined()
2868       && !parameters->target().is_defined_by_abi(sym)
2869       && !sym->object()->is_in_system_directory())
2870     {
2871       // A very ugly cast.
2872       Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2873       if (!dynobj->has_unknown_needed_entries())
2874         gold_undefined_symbol(sym);
2875     }
2876 }
2877
2878 // Write out a section symbol.  Return the update offset.
2879
2880 void
2881 Symbol_table::write_section_symbol(const Output_section *os,
2882                                    Output_symtab_xindex* symtab_xindex,
2883                                    Output_file* of,
2884                                    off_t offset) const
2885 {
2886   switch (parameters->size_and_endianness())
2887     {
2888 #ifdef HAVE_TARGET_32_LITTLE
2889     case Parameters::TARGET_32_LITTLE:
2890       this->sized_write_section_symbol<32, false>(os, symtab_xindex, of,
2891                                                   offset);
2892       break;
2893 #endif
2894 #ifdef HAVE_TARGET_32_BIG
2895     case Parameters::TARGET_32_BIG:
2896       this->sized_write_section_symbol<32, true>(os, symtab_xindex, of,
2897                                                  offset);
2898       break;
2899 #endif
2900 #ifdef HAVE_TARGET_64_LITTLE
2901     case Parameters::TARGET_64_LITTLE:
2902       this->sized_write_section_symbol<64, false>(os, symtab_xindex, of,
2903                                                   offset);
2904       break;
2905 #endif
2906 #ifdef HAVE_TARGET_64_BIG
2907     case Parameters::TARGET_64_BIG:
2908       this->sized_write_section_symbol<64, true>(os, symtab_xindex, of,
2909                                                  offset);
2910       break;
2911 #endif
2912     default:
2913       gold_unreachable();
2914     }
2915 }
2916
2917 // Write out a section symbol, specialized for size and endianness.
2918
2919 template<int size, bool big_endian>
2920 void
2921 Symbol_table::sized_write_section_symbol(const Output_section* os,
2922                                          Output_symtab_xindex* symtab_xindex,
2923                                          Output_file* of,
2924                                          off_t offset) const
2925 {
2926   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2927
2928   unsigned char* pov = of->get_output_view(offset, sym_size);
2929
2930   elfcpp::Sym_write<size, big_endian> osym(pov);
2931   osym.put_st_name(0);
2932   if (parameters->options().relocatable())
2933     osym.put_st_value(0);
2934   else
2935     osym.put_st_value(os->address());
2936   osym.put_st_size(0);
2937   osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2938                                        elfcpp::STT_SECTION));
2939   osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2940
2941   unsigned int shndx = os->out_shndx();
2942   if (shndx >= elfcpp::SHN_LORESERVE)
2943     {
2944       symtab_xindex->add(os->symtab_index(), shndx);
2945       shndx = elfcpp::SHN_XINDEX;
2946     }
2947   osym.put_st_shndx(shndx);
2948
2949   of->write_output_view(offset, sym_size, pov);
2950 }
2951
2952 // Print statistical information to stderr.  This is used for --stats.
2953
2954 void
2955 Symbol_table::print_stats() const
2956 {
2957 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2958   fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2959           program_name, this->table_.size(), this->table_.bucket_count());
2960 #else
2961   fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2962           program_name, this->table_.size());
2963 #endif
2964   this->namepool_.print_stats("symbol table stringpool");
2965 }
2966
2967 // We check for ODR violations by looking for symbols with the same
2968 // name for which the debugging information reports that they were
2969 // defined in different source locations.  When comparing the source
2970 // location, we consider instances with the same base filename and
2971 // line number to be the same.  This is because different object
2972 // files/shared libraries can include the same header file using
2973 // different paths, and we don't want to report an ODR violation in
2974 // that case.
2975
2976 // This struct is used to compare line information, as returned by
2977 // Dwarf_line_info::one_addr2line.  It implements a < comparison
2978 // operator used with std::set.
2979
2980 struct Odr_violation_compare
2981 {
2982   bool
2983   operator()(const std::string& s1, const std::string& s2) const
2984   {
2985     std::string::size_type pos1 = s1.rfind('/');
2986     std::string::size_type pos2 = s2.rfind('/');
2987     if (pos1 == std::string::npos
2988         || pos2 == std::string::npos)
2989       return s1 < s2;
2990     return s1.compare(pos1, std::string::npos,
2991                       s2, pos2, std::string::npos) < 0;
2992   }
2993 };
2994
2995 // Check candidate_odr_violations_ to find symbols with the same name
2996 // but apparently different definitions (different source-file/line-no).
2997
2998 void
2999 Symbol_table::detect_odr_violations(const Task* task,
3000                                     const char* output_file_name) const
3001 {
3002   for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
3003        it != candidate_odr_violations_.end();
3004        ++it)
3005     {
3006       const char* symbol_name = it->first;
3007       // We use a sorted set so the output is deterministic.
3008       std::set<std::string, Odr_violation_compare> line_nums;
3009
3010       for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
3011                locs = it->second.begin();
3012            locs != it->second.end();
3013            ++locs)
3014         {
3015           // We need to lock the object in order to read it.  This
3016           // means that we have to run in a singleton Task.  If we
3017           // want to run this in a general Task for better
3018           // performance, we will need one Task for object, plus
3019           // appropriate locking to ensure that we don't conflict with
3020           // other uses of the object.  Also note, one_addr2line is not
3021           // currently thread-safe.
3022           Task_lock_obj<Object> tl(task, locs->object);
3023           // 16 is the size of the object-cache that one_addr2line should use.
3024           std::string lineno = Dwarf_line_info::one_addr2line(
3025               locs->object, locs->shndx, locs->offset, 16);
3026           if (!lineno.empty())
3027             line_nums.insert(lineno);
3028         }
3029
3030       if (line_nums.size() > 1)
3031         {
3032           gold_warning(_("while linking %s: symbol '%s' defined in multiple "
3033                          "places (possible ODR violation):"),
3034                        output_file_name, demangle(symbol_name).c_str());
3035           for (std::set<std::string>::const_iterator it2 = line_nums.begin();
3036                it2 != line_nums.end();
3037                ++it2)
3038             fprintf(stderr, "  %s\n", it2->c_str());
3039         }
3040     }
3041   // We only call one_addr2line() in this function, so we can clear its cache.
3042   Dwarf_line_info::clear_addr2line_cache();
3043 }
3044
3045 // Warnings functions.
3046
3047 // Add a new warning.
3048
3049 void
3050 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
3051                       const std::string& warning)
3052 {
3053   name = symtab->canonicalize_name(name);
3054   this->warnings_[name].set(obj, warning);
3055 }
3056
3057 // Look through the warnings and mark the symbols for which we should
3058 // warn.  This is called during Layout::finalize when we know the
3059 // sources for all the symbols.
3060
3061 void
3062 Warnings::note_warnings(Symbol_table* symtab)
3063 {
3064   for (Warning_table::iterator p = this->warnings_.begin();
3065        p != this->warnings_.end();
3066        ++p)
3067     {
3068       Symbol* sym = symtab->lookup(p->first, NULL);
3069       if (sym != NULL
3070           && sym->source() == Symbol::FROM_OBJECT
3071           && sym->object() == p->second.object)
3072         sym->set_has_warning();
3073     }
3074 }
3075
3076 // Issue a warning.  This is called when we see a relocation against a
3077 // symbol for which has a warning.
3078
3079 template<int size, bool big_endian>
3080 void
3081 Warnings::issue_warning(const Symbol* sym,
3082                         const Relocate_info<size, big_endian>* relinfo,
3083                         size_t relnum, off_t reloffset) const
3084 {
3085   gold_assert(sym->has_warning());
3086   Warning_table::const_iterator p = this->warnings_.find(sym->name());
3087   gold_assert(p != this->warnings_.end());
3088   gold_warning_at_location(relinfo, relnum, reloffset,
3089                            "%s", p->second.text.c_str());
3090 }
3091
3092 // Instantiate the templates we need.  We could use the configure
3093 // script to restrict this to only the ones needed for implemented
3094 // targets.
3095
3096 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3097 template
3098 void
3099 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
3100 #endif
3101
3102 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3103 template
3104 void
3105 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
3106 #endif
3107
3108 #ifdef HAVE_TARGET_32_LITTLE
3109 template
3110 void
3111 Symbol_table::add_from_relobj<32, false>(
3112     Sized_relobj<32, false>* relobj,
3113     const unsigned char* syms,
3114     size_t count,
3115     size_t symndx_offset,
3116     const char* sym_names,
3117     size_t sym_name_size,
3118     Sized_relobj<32, false>::Symbols* sympointers,
3119     size_t* defined);
3120 #endif
3121
3122 #ifdef HAVE_TARGET_32_BIG
3123 template
3124 void
3125 Symbol_table::add_from_relobj<32, true>(
3126     Sized_relobj<32, true>* relobj,
3127     const unsigned char* syms,
3128     size_t count,
3129     size_t symndx_offset,
3130     const char* sym_names,
3131     size_t sym_name_size,
3132     Sized_relobj<32, true>::Symbols* sympointers,
3133     size_t* defined);
3134 #endif
3135
3136 #ifdef HAVE_TARGET_64_LITTLE
3137 template
3138 void
3139 Symbol_table::add_from_relobj<64, false>(
3140     Sized_relobj<64, false>* relobj,
3141     const unsigned char* syms,
3142     size_t count,
3143     size_t symndx_offset,
3144     const char* sym_names,
3145     size_t sym_name_size,
3146     Sized_relobj<64, false>::Symbols* sympointers,
3147     size_t* defined);
3148 #endif
3149
3150 #ifdef HAVE_TARGET_64_BIG
3151 template
3152 void
3153 Symbol_table::add_from_relobj<64, true>(
3154     Sized_relobj<64, true>* relobj,
3155     const unsigned char* syms,
3156     size_t count,
3157     size_t symndx_offset,
3158     const char* sym_names,
3159     size_t sym_name_size,
3160     Sized_relobj<64, true>::Symbols* sympointers,
3161     size_t* defined);
3162 #endif
3163
3164 #ifdef HAVE_TARGET_32_LITTLE
3165 template
3166 Symbol*
3167 Symbol_table::add_from_pluginobj<32, false>(
3168     Sized_pluginobj<32, false>* obj,
3169     const char* name,
3170     const char* ver,
3171     elfcpp::Sym<32, false>* sym);
3172 #endif
3173
3174 #ifdef HAVE_TARGET_32_BIG
3175 template
3176 Symbol*
3177 Symbol_table::add_from_pluginobj<32, true>(
3178     Sized_pluginobj<32, true>* obj,
3179     const char* name,
3180     const char* ver,
3181     elfcpp::Sym<32, true>* sym);
3182 #endif
3183
3184 #ifdef HAVE_TARGET_64_LITTLE
3185 template
3186 Symbol*
3187 Symbol_table::add_from_pluginobj<64, false>(
3188     Sized_pluginobj<64, false>* obj,
3189     const char* name,
3190     const char* ver,
3191     elfcpp::Sym<64, false>* sym);
3192 #endif
3193
3194 #ifdef HAVE_TARGET_64_BIG
3195 template
3196 Symbol*
3197 Symbol_table::add_from_pluginobj<64, true>(
3198     Sized_pluginobj<64, true>* obj,
3199     const char* name,
3200     const char* ver,
3201     elfcpp::Sym<64, true>* sym);
3202 #endif
3203
3204 #ifdef HAVE_TARGET_32_LITTLE
3205 template
3206 void
3207 Symbol_table::add_from_dynobj<32, false>(
3208     Sized_dynobj<32, false>* dynobj,
3209     const unsigned char* syms,
3210     size_t count,
3211     const char* sym_names,
3212     size_t sym_name_size,
3213     const unsigned char* versym,
3214     size_t versym_size,
3215     const std::vector<const char*>* version_map,
3216     Sized_relobj<32, false>::Symbols* sympointers,
3217     size_t* defined);
3218 #endif
3219
3220 #ifdef HAVE_TARGET_32_BIG
3221 template
3222 void
3223 Symbol_table::add_from_dynobj<32, true>(
3224     Sized_dynobj<32, true>* dynobj,
3225     const unsigned char* syms,
3226     size_t count,
3227     const char* sym_names,
3228     size_t sym_name_size,
3229     const unsigned char* versym,
3230     size_t versym_size,
3231     const std::vector<const char*>* version_map,
3232     Sized_relobj<32, true>::Symbols* sympointers,
3233     size_t* defined);
3234 #endif
3235
3236 #ifdef HAVE_TARGET_64_LITTLE
3237 template
3238 void
3239 Symbol_table::add_from_dynobj<64, false>(
3240     Sized_dynobj<64, false>* dynobj,
3241     const unsigned char* syms,
3242     size_t count,
3243     const char* sym_names,
3244     size_t sym_name_size,
3245     const unsigned char* versym,
3246     size_t versym_size,
3247     const std::vector<const char*>* version_map,
3248     Sized_relobj<64, false>::Symbols* sympointers,
3249     size_t* defined);
3250 #endif
3251
3252 #ifdef HAVE_TARGET_64_BIG
3253 template
3254 void
3255 Symbol_table::add_from_dynobj<64, true>(
3256     Sized_dynobj<64, true>* dynobj,
3257     const unsigned char* syms,
3258     size_t count,
3259     const char* sym_names,
3260     size_t sym_name_size,
3261     const unsigned char* versym,
3262     size_t versym_size,
3263     const std::vector<const char*>* version_map,
3264     Sized_relobj<64, true>::Symbols* sympointers,
3265     size_t* defined);
3266 #endif
3267
3268 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
3269 template
3270 void
3271 Symbol_table::define_with_copy_reloc<32>(
3272     Sized_symbol<32>* sym,
3273     Output_data* posd,
3274     elfcpp::Elf_types<32>::Elf_Addr value);
3275 #endif
3276
3277 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
3278 template
3279 void
3280 Symbol_table::define_with_copy_reloc<64>(
3281     Sized_symbol<64>* sym,
3282     Output_data* posd,
3283     elfcpp::Elf_types<64>::Elf_Addr value);
3284 #endif
3285
3286 #ifdef HAVE_TARGET_32_LITTLE
3287 template
3288 void
3289 Warnings::issue_warning<32, false>(const Symbol* sym,
3290                                    const Relocate_info<32, false>* relinfo,
3291                                    size_t relnum, off_t reloffset) const;
3292 #endif
3293
3294 #ifdef HAVE_TARGET_32_BIG
3295 template
3296 void
3297 Warnings::issue_warning<32, true>(const Symbol* sym,
3298                                   const Relocate_info<32, true>* relinfo,
3299                                   size_t relnum, off_t reloffset) const;
3300 #endif
3301
3302 #ifdef HAVE_TARGET_64_LITTLE
3303 template
3304 void
3305 Warnings::issue_warning<64, false>(const Symbol* sym,
3306                                    const Relocate_info<64, false>* relinfo,
3307                                    size_t relnum, off_t reloffset) const;
3308 #endif
3309
3310 #ifdef HAVE_TARGET_64_BIG
3311 template
3312 void
3313 Warnings::issue_warning<64, true>(const Symbol* sym,
3314                                   const Relocate_info<64, true>* relinfo,
3315                                   size_t relnum, off_t reloffset) const;
3316 #endif
3317
3318 } // End namespace gold.