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