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