* symtab.cc (Symbol_table::add_from_relobj): Don't set the version
[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       // We don't want to assign a version to an undefined symbol,
723       // even if it is listed in the version script.  FIXME: What
724       // about a common symbol?
725       else if (!version_script_.empty()
726                && psym->get_st_shndx() != elfcpp::SHN_UNDEF)
727         {
728           // The symbol name did not have a version, but
729           // the version script may assign a version anyway.
730           namelen = strlen(name);
731           def = true;
732           // Check the global: entries from the version script.
733           const std::string& version =
734               version_script_.get_symbol_version(name);
735           if (!version.empty())
736             ver = version.c_str();
737           // Check the local: entries from the version script
738           if (version_script_.symbol_is_local(name))
739             local = true;
740         }
741
742       if (just_symbols)
743         {
744           if (psym != &sym2)
745             memcpy(symbuf, p, sym_size);
746           elfcpp::Sym_write<size, big_endian> sw(symbuf);
747           sw.put_st_shndx(elfcpp::SHN_ABS);
748           if (st_shndx != elfcpp::SHN_UNDEF
749               && st_shndx < elfcpp::SHN_LORESERVE)
750             {
751               // Symbol values in object files are section relative.
752               // This is normally what we want, but since here we are
753               // converting the symbol to absolute we need to add the
754               // section address.  The section address in an object
755               // file is normally zero, but people can use a linker
756               // script to change it.
757               sw.put_st_value(sym2.get_st_value()
758                               + relobj->section_address(st_shndx));
759             }
760           psym = &sym2;
761         }
762
763       Sized_symbol<size>* res;
764       if (ver == NULL)
765         {
766           Stringpool::Key name_key;
767           name = this->namepool_.add(name, true, &name_key);
768           res = this->add_from_object(relobj, name, name_key, NULL, 0,
769                                       false, *psym, sym);
770           if (local)
771             this->force_local(res);
772         }
773       else
774         {
775           Stringpool::Key name_key;
776           name = this->namepool_.add_with_length(name, namelen, true,
777                                                  &name_key);
778           Stringpool::Key ver_key;
779           ver = this->namepool_.add(ver, true, &ver_key);
780
781           res = this->add_from_object(relobj, name, name_key, ver, ver_key,
782                                       def, *psym, sym);
783         }
784
785       (*sympointers)[i] = res;
786     }
787 }
788
789 // Add all the symbols in a dynamic object to the hash table.
790
791 template<int size, bool big_endian>
792 void
793 Symbol_table::add_from_dynobj(
794     Sized_dynobj<size, big_endian>* dynobj,
795     const unsigned char* syms,
796     size_t count,
797     const char* sym_names,
798     size_t sym_name_size,
799     const unsigned char* versym,
800     size_t versym_size,
801     const std::vector<const char*>* version_map)
802 {
803   gold_assert(size == dynobj->target()->get_size());
804   gold_assert(size == parameters->target().get_size());
805
806   if (dynobj->just_symbols())
807     {
808       gold_error(_("--just-symbols does not make sense with a shared object"));
809       return;
810     }
811
812   if (versym != NULL && versym_size / 2 < count)
813     {
814       dynobj->error(_("too few symbol versions"));
815       return;
816     }
817
818   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
819
820   // We keep a list of all STT_OBJECT symbols, so that we can resolve
821   // weak aliases.  This is necessary because if the dynamic object
822   // provides the same variable under two names, one of which is a
823   // weak definition, and the regular object refers to the weak
824   // definition, we have to put both the weak definition and the
825   // strong definition into the dynamic symbol table.  Given a weak
826   // definition, the only way that we can find the corresponding
827   // strong definition, if any, is to search the symbol table.
828   std::vector<Sized_symbol<size>*> object_symbols;
829
830   const unsigned char* p = syms;
831   const unsigned char* vs = versym;
832   for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
833     {
834       elfcpp::Sym<size, big_endian> sym(p);
835
836       // Ignore symbols with local binding or that have
837       // internal or hidden visibility.
838       if (sym.get_st_bind() == elfcpp::STB_LOCAL
839           || sym.get_st_visibility() == elfcpp::STV_INTERNAL
840           || sym.get_st_visibility() == elfcpp::STV_HIDDEN)
841         continue;
842
843       unsigned int st_name = sym.get_st_name();
844       if (st_name >= sym_name_size)
845         {
846           dynobj->error(_("bad symbol name offset %u at %zu"),
847                         st_name, i);
848           continue;
849         }
850
851       const char* name = sym_names + st_name;
852
853       Sized_symbol<size>* res;
854
855       if (versym == NULL)
856         {
857           Stringpool::Key name_key;
858           name = this->namepool_.add(name, true, &name_key);
859           res = this->add_from_object(dynobj, name, name_key, NULL, 0,
860                                       false, sym, sym);
861         }
862       else
863         {
864           // Read the version information.
865
866           unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
867
868           bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
869           v &= elfcpp::VERSYM_VERSION;
870
871           // The Sun documentation says that V can be VER_NDX_LOCAL,
872           // or VER_NDX_GLOBAL, or a version index.  The meaning of
873           // VER_NDX_LOCAL is defined as "Symbol has local scope."
874           // The old GNU linker will happily generate VER_NDX_LOCAL
875           // for an undefined symbol.  I don't know what the Sun
876           // linker will generate.
877
878           if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
879               && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
880             {
881               // This symbol should not be visible outside the object.
882               continue;
883             }
884
885           // At this point we are definitely going to add this symbol.
886           Stringpool::Key name_key;
887           name = this->namepool_.add(name, true, &name_key);
888
889           if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
890               || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
891             {
892               // This symbol does not have a version.
893               res = this->add_from_object(dynobj, name, name_key, NULL, 0,
894                                           false, sym, sym);
895             }
896           else
897             {
898               if (v >= version_map->size())
899                 {
900                   dynobj->error(_("versym for symbol %zu out of range: %u"),
901                                 i, v);
902                   continue;
903                 }
904
905               const char* version = (*version_map)[v];
906               if (version == NULL)
907                 {
908                   dynobj->error(_("versym for symbol %zu has no name: %u"),
909                                 i, v);
910                   continue;
911                 }
912
913               Stringpool::Key version_key;
914               version = this->namepool_.add(version, true, &version_key);
915
916               // If this is an absolute symbol, and the version name
917               // and symbol name are the same, then this is the
918               // version definition symbol.  These symbols exist to
919               // support using -u to pull in particular versions.  We
920               // do not want to record a version for them.
921               if (sym.get_st_shndx() == elfcpp::SHN_ABS
922                   && name_key == version_key)
923                 res = this->add_from_object(dynobj, name, name_key, NULL, 0,
924                                             false, sym, sym);
925               else
926                 {
927                   const bool def = (!hidden
928                                     && (sym.get_st_shndx()
929                                         != elfcpp::SHN_UNDEF));
930                   res = this->add_from_object(dynobj, name, name_key, version,
931                                               version_key, def, sym, sym);
932                 }
933             }
934         }
935
936       if (sym.get_st_shndx() != elfcpp::SHN_UNDEF
937           && sym.get_st_type() == elfcpp::STT_OBJECT)
938         object_symbols.push_back(res);
939     }
940
941   this->record_weak_aliases(&object_symbols);
942 }
943
944 // This is used to sort weak aliases.  We sort them first by section
945 // index, then by offset, then by weak ahead of strong.
946
947 template<int size>
948 class Weak_alias_sorter
949 {
950  public:
951   bool operator()(const Sized_symbol<size>*, const Sized_symbol<size>*) const;
952 };
953
954 template<int size>
955 bool
956 Weak_alias_sorter<size>::operator()(const Sized_symbol<size>* s1,
957                                     const Sized_symbol<size>* s2) const
958 {
959   if (s1->shndx() != s2->shndx())
960     return s1->shndx() < s2->shndx();
961   if (s1->value() != s2->value())
962     return s1->value() < s2->value();
963   if (s1->binding() != s2->binding())
964     {
965       if (s1->binding() == elfcpp::STB_WEAK)
966         return true;
967       if (s2->binding() == elfcpp::STB_WEAK)
968         return false;
969     }
970   return std::string(s1->name()) < std::string(s2->name());
971 }
972
973 // SYMBOLS is a list of object symbols from a dynamic object.  Look
974 // for any weak aliases, and record them so that if we add the weak
975 // alias to the dynamic symbol table, we also add the corresponding
976 // strong symbol.
977
978 template<int size>
979 void
980 Symbol_table::record_weak_aliases(std::vector<Sized_symbol<size>*>* symbols)
981 {
982   // Sort the vector by section index, then by offset, then by weak
983   // ahead of strong.
984   std::sort(symbols->begin(), symbols->end(), Weak_alias_sorter<size>());
985
986   // Walk through the vector.  For each weak definition, record
987   // aliases.
988   for (typename std::vector<Sized_symbol<size>*>::const_iterator p =
989          symbols->begin();
990        p != symbols->end();
991        ++p)
992     {
993       if ((*p)->binding() != elfcpp::STB_WEAK)
994         continue;
995
996       // Build a circular list of weak aliases.  Each symbol points to
997       // the next one in the circular list.
998
999       Sized_symbol<size>* from_sym = *p;
1000       typename std::vector<Sized_symbol<size>*>::const_iterator q;
1001       for (q = p + 1; q != symbols->end(); ++q)
1002         {
1003           if ((*q)->shndx() != from_sym->shndx()
1004               || (*q)->value() != from_sym->value())
1005             break;
1006
1007           this->weak_aliases_[from_sym] = *q;
1008           from_sym->set_has_alias();
1009           from_sym = *q;
1010         }
1011
1012       if (from_sym != *p)
1013         {
1014           this->weak_aliases_[from_sym] = *p;
1015           from_sym->set_has_alias();
1016         }
1017
1018       p = q - 1;
1019     }
1020 }
1021
1022 // Create and return a specially defined symbol.  If ONLY_IF_REF is
1023 // true, then only create the symbol if there is a reference to it.
1024 // If this does not return NULL, it sets *POLDSYM to the existing
1025 // symbol if there is one.  This canonicalizes *PNAME and *PVERSION.
1026
1027 template<int size, bool big_endian>
1028 Sized_symbol<size>*
1029 Symbol_table::define_special_symbol(const char** pname, const char** pversion,
1030                                     bool only_if_ref,
1031                                     Sized_symbol<size>** poldsym)
1032 {
1033   Symbol* oldsym;
1034   Sized_symbol<size>* sym;
1035   bool add_to_table = false;
1036   typename Symbol_table_type::iterator add_loc = this->table_.end();
1037
1038   // If the caller didn't give us a version, see if we get one from
1039   // the version script.
1040   if (*pversion == NULL)
1041     {
1042       const std::string& v(this->version_script_.get_symbol_version(*pname));
1043       if (!v.empty())
1044         *pversion = v.c_str();
1045     }
1046
1047   if (only_if_ref)
1048     {
1049       oldsym = this->lookup(*pname, *pversion);
1050       if (oldsym == NULL || !oldsym->is_undefined())
1051         return NULL;
1052
1053       *pname = oldsym->name();
1054       *pversion = oldsym->version();
1055     }
1056   else
1057     {
1058       // Canonicalize NAME and VERSION.
1059       Stringpool::Key name_key;
1060       *pname = this->namepool_.add(*pname, true, &name_key);
1061
1062       Stringpool::Key version_key = 0;
1063       if (*pversion != NULL)
1064         *pversion = this->namepool_.add(*pversion, true, &version_key);
1065
1066       Symbol* const snull = NULL;
1067       std::pair<typename Symbol_table_type::iterator, bool> ins =
1068         this->table_.insert(std::make_pair(std::make_pair(name_key,
1069                                                           version_key),
1070                                            snull));
1071
1072       if (!ins.second)
1073         {
1074           // We already have a symbol table entry for NAME/VERSION.
1075           oldsym = ins.first->second;
1076           gold_assert(oldsym != NULL);
1077         }
1078       else
1079         {
1080           // We haven't seen this symbol before.
1081           gold_assert(ins.first->second == NULL);
1082           add_to_table = true;
1083           add_loc = ins.first;
1084           oldsym = NULL;
1085         }
1086     }
1087
1088   const Target& target = parameters->target();
1089   if (!target.has_make_symbol())
1090     sym = new Sized_symbol<size>();
1091   else
1092     {
1093       gold_assert(target.get_size() == size);
1094       gold_assert(target.is_big_endian() ? big_endian : !big_endian);
1095       typedef Sized_target<size, big_endian> My_target;
1096       const My_target* sized_target =
1097           static_cast<const My_target*>(&target);
1098       sym = sized_target->make_symbol();
1099       if (sym == NULL)
1100         return NULL;
1101     }
1102
1103   if (add_to_table)
1104     add_loc->second = sym;
1105   else
1106     gold_assert(oldsym != NULL);
1107
1108   *poldsym = this->get_sized_symbol<size>(oldsym);
1109
1110   return sym;
1111 }
1112
1113 // Define a symbol based on an Output_data.
1114
1115 Symbol*
1116 Symbol_table::define_in_output_data(const char* name,
1117                                     const char* version,
1118                                     Output_data* od,
1119                                     uint64_t value,
1120                                     uint64_t symsize,
1121                                     elfcpp::STT type,
1122                                     elfcpp::STB binding,
1123                                     elfcpp::STV visibility,
1124                                     unsigned char nonvis,
1125                                     bool offset_is_from_end,
1126                                     bool only_if_ref)
1127 {
1128   if (parameters->target().get_size() == 32)
1129     {
1130 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1131       return this->do_define_in_output_data<32>(name, version, od,
1132                                                 value, symsize, type, binding,
1133                                                 visibility, nonvis,
1134                                                 offset_is_from_end,
1135                                                 only_if_ref);
1136 #else
1137       gold_unreachable();
1138 #endif
1139     }
1140   else if (parameters->target().get_size() == 64)
1141     {
1142 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1143       return this->do_define_in_output_data<64>(name, version, od,
1144                                                 value, symsize, type, binding,
1145                                                 visibility, nonvis,
1146                                                 offset_is_from_end,
1147                                                 only_if_ref);
1148 #else
1149       gold_unreachable();
1150 #endif
1151     }
1152   else
1153     gold_unreachable();
1154 }
1155
1156 // Define a symbol in an Output_data, sized version.
1157
1158 template<int size>
1159 Sized_symbol<size>*
1160 Symbol_table::do_define_in_output_data(
1161     const char* name,
1162     const char* version,
1163     Output_data* od,
1164     typename elfcpp::Elf_types<size>::Elf_Addr value,
1165     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1166     elfcpp::STT type,
1167     elfcpp::STB binding,
1168     elfcpp::STV visibility,
1169     unsigned char nonvis,
1170     bool offset_is_from_end,
1171     bool only_if_ref)
1172 {
1173   Sized_symbol<size>* sym;
1174   Sized_symbol<size>* oldsym;
1175
1176   if (parameters->target().is_big_endian())
1177     {
1178 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1179       sym = this->define_special_symbol<size, true>(&name, &version,
1180                                                     only_if_ref, &oldsym);
1181 #else
1182       gold_unreachable();
1183 #endif
1184     }
1185   else
1186     {
1187 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1188       sym = this->define_special_symbol<size, false>(&name, &version,
1189                                                      only_if_ref, &oldsym);
1190 #else
1191       gold_unreachable();
1192 #endif
1193     }
1194
1195   if (sym == NULL)
1196     return NULL;
1197
1198   gold_assert(version == NULL || oldsym != NULL);
1199   sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
1200             offset_is_from_end);
1201
1202   if (oldsym == NULL)
1203     {
1204       if (binding == elfcpp::STB_LOCAL
1205           || this->version_script_.symbol_is_local(name))
1206         this->force_local(sym);
1207       return sym;
1208     }
1209
1210   if (Symbol_table::should_override_with_special(oldsym))
1211     this->override_with_special(oldsym, sym);
1212   delete sym;
1213   return oldsym;
1214 }
1215
1216 // Define a symbol based on an Output_segment.
1217
1218 Symbol*
1219 Symbol_table::define_in_output_segment(const char* name,
1220                                        const char* version, Output_segment* os,
1221                                        uint64_t value,
1222                                        uint64_t symsize,
1223                                        elfcpp::STT type,
1224                                        elfcpp::STB binding,
1225                                        elfcpp::STV visibility,
1226                                        unsigned char nonvis,
1227                                        Symbol::Segment_offset_base offset_base,
1228                                        bool only_if_ref)
1229 {
1230   if (parameters->target().get_size() == 32)
1231     {
1232 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1233       return this->do_define_in_output_segment<32>(name, version, os,
1234                                                    value, symsize, type,
1235                                                    binding, visibility, nonvis,
1236                                                    offset_base, only_if_ref);
1237 #else
1238       gold_unreachable();
1239 #endif
1240     }
1241   else if (parameters->target().get_size() == 64)
1242     {
1243 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1244       return this->do_define_in_output_segment<64>(name, version, os,
1245                                                    value, symsize, type,
1246                                                    binding, visibility, nonvis,
1247                                                    offset_base, only_if_ref);
1248 #else
1249       gold_unreachable();
1250 #endif
1251     }
1252   else
1253     gold_unreachable();
1254 }
1255
1256 // Define a symbol in an Output_segment, sized version.
1257
1258 template<int size>
1259 Sized_symbol<size>*
1260 Symbol_table::do_define_in_output_segment(
1261     const char* name,
1262     const char* version,
1263     Output_segment* os,
1264     typename elfcpp::Elf_types<size>::Elf_Addr value,
1265     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1266     elfcpp::STT type,
1267     elfcpp::STB binding,
1268     elfcpp::STV visibility,
1269     unsigned char nonvis,
1270     Symbol::Segment_offset_base offset_base,
1271     bool only_if_ref)
1272 {
1273   Sized_symbol<size>* sym;
1274   Sized_symbol<size>* oldsym;
1275
1276   if (parameters->target().is_big_endian())
1277     {
1278 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1279       sym = this->define_special_symbol<size, true>(&name, &version,
1280                                                     only_if_ref, &oldsym);
1281 #else
1282       gold_unreachable();
1283 #endif
1284     }
1285   else
1286     {
1287 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1288       sym = this->define_special_symbol<size, false>(&name, &version,
1289                                                      only_if_ref, &oldsym);
1290 #else
1291       gold_unreachable();
1292 #endif
1293     }
1294
1295   if (sym == NULL)
1296     return NULL;
1297
1298   gold_assert(version == NULL || oldsym != NULL);
1299   sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
1300             offset_base);
1301
1302   if (oldsym == NULL)
1303     {
1304       if (binding == elfcpp::STB_LOCAL
1305           || this->version_script_.symbol_is_local(name))
1306         this->force_local(sym);
1307       return sym;
1308     }
1309
1310   if (Symbol_table::should_override_with_special(oldsym))
1311     this->override_with_special(oldsym, sym);
1312   delete sym;
1313   return oldsym;
1314 }
1315
1316 // Define a special symbol with a constant value.  It is a multiple
1317 // definition error if this symbol is already defined.
1318
1319 Symbol*
1320 Symbol_table::define_as_constant(const char* name,
1321                                  const char* version,
1322                                  uint64_t value,
1323                                  uint64_t symsize,
1324                                  elfcpp::STT type,
1325                                  elfcpp::STB binding,
1326                                  elfcpp::STV visibility,
1327                                  unsigned char nonvis,
1328                                  bool only_if_ref,
1329                                  bool force_override)
1330 {
1331   if (parameters->target().get_size() == 32)
1332     {
1333 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1334       return this->do_define_as_constant<32>(name, version, value,
1335                                              symsize, type, binding,
1336                                              visibility, nonvis, only_if_ref,
1337                                              force_override);
1338 #else
1339       gold_unreachable();
1340 #endif
1341     }
1342   else if (parameters->target().get_size() == 64)
1343     {
1344 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1345       return this->do_define_as_constant<64>(name, version, value,
1346                                              symsize, type, binding,
1347                                              visibility, nonvis, only_if_ref,
1348                                              force_override);
1349 #else
1350       gold_unreachable();
1351 #endif
1352     }
1353   else
1354     gold_unreachable();
1355 }
1356
1357 // Define a symbol as a constant, sized version.
1358
1359 template<int size>
1360 Sized_symbol<size>*
1361 Symbol_table::do_define_as_constant(
1362     const char* name,
1363     const char* version,
1364     typename elfcpp::Elf_types<size>::Elf_Addr value,
1365     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1366     elfcpp::STT type,
1367     elfcpp::STB binding,
1368     elfcpp::STV visibility,
1369     unsigned char nonvis,
1370     bool only_if_ref,
1371     bool force_override)
1372 {
1373   Sized_symbol<size>* sym;
1374   Sized_symbol<size>* oldsym;
1375
1376   if (parameters->target().is_big_endian())
1377     {
1378 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1379       sym = this->define_special_symbol<size, true>(&name, &version,
1380                                                     only_if_ref, &oldsym);
1381 #else
1382       gold_unreachable();
1383 #endif
1384     }
1385   else
1386     {
1387 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1388       sym = this->define_special_symbol<size, false>(&name, &version,
1389                                                      only_if_ref, &oldsym);
1390 #else
1391       gold_unreachable();
1392 #endif
1393     }
1394
1395   if (sym == NULL)
1396     return NULL;
1397
1398   gold_assert(version == NULL || version == name || oldsym != NULL);
1399   sym->init(name, value, symsize, type, binding, visibility, nonvis);
1400
1401   if (oldsym == NULL)
1402     {
1403       if (binding == elfcpp::STB_LOCAL
1404           || this->version_script_.symbol_is_local(name))
1405         this->force_local(sym);
1406       return sym;
1407     }
1408
1409   if (force_override || Symbol_table::should_override_with_special(oldsym))
1410     this->override_with_special(oldsym, sym);
1411   delete sym;
1412   return oldsym;
1413 }
1414
1415 // Define a set of symbols in output sections.
1416
1417 void
1418 Symbol_table::define_symbols(const Layout* layout, int count,
1419                              const Define_symbol_in_section* p,
1420                              bool only_if_ref)
1421 {
1422   for (int i = 0; i < count; ++i, ++p)
1423     {
1424       Output_section* os = layout->find_output_section(p->output_section);
1425       if (os != NULL)
1426         this->define_in_output_data(p->name, NULL, os, p->value,
1427                                     p->size, p->type, p->binding,
1428                                     p->visibility, p->nonvis,
1429                                     p->offset_is_from_end,
1430                                     only_if_ref || p->only_if_ref);
1431       else
1432         this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1433                                  p->binding, p->visibility, p->nonvis,
1434                                  only_if_ref || p->only_if_ref,
1435                                  false);
1436     }
1437 }
1438
1439 // Define a set of symbols in output segments.
1440
1441 void
1442 Symbol_table::define_symbols(const Layout* layout, int count,
1443                              const Define_symbol_in_segment* p,
1444                              bool only_if_ref)
1445 {
1446   for (int i = 0; i < count; ++i, ++p)
1447     {
1448       Output_segment* os = layout->find_output_segment(p->segment_type,
1449                                                        p->segment_flags_set,
1450                                                        p->segment_flags_clear);
1451       if (os != NULL)
1452         this->define_in_output_segment(p->name, NULL, os, p->value,
1453                                        p->size, p->type, p->binding,
1454                                        p->visibility, p->nonvis,
1455                                        p->offset_base,
1456                                        only_if_ref || p->only_if_ref);
1457       else
1458         this->define_as_constant(p->name, NULL, 0, p->size, p->type,
1459                                  p->binding, p->visibility, p->nonvis,
1460                                  only_if_ref || p->only_if_ref,
1461                                  false);
1462     }
1463 }
1464
1465 // Define CSYM using a COPY reloc.  POSD is the Output_data where the
1466 // symbol should be defined--typically a .dyn.bss section.  VALUE is
1467 // the offset within POSD.
1468
1469 template<int size>
1470 void
1471 Symbol_table::define_with_copy_reloc(
1472     Sized_symbol<size>* csym,
1473     Output_data* posd,
1474     typename elfcpp::Elf_types<size>::Elf_Addr value)
1475 {
1476   gold_assert(csym->is_from_dynobj());
1477   gold_assert(!csym->is_copied_from_dynobj());
1478   Object* object = csym->object();
1479   gold_assert(object->is_dynamic());
1480   Dynobj* dynobj = static_cast<Dynobj*>(object);
1481
1482   // Our copied variable has to override any variable in a shared
1483   // library.
1484   elfcpp::STB binding = csym->binding();
1485   if (binding == elfcpp::STB_WEAK)
1486     binding = elfcpp::STB_GLOBAL;
1487
1488   this->define_in_output_data(csym->name(), csym->version(),
1489                               posd, value, csym->symsize(),
1490                               csym->type(), binding,
1491                               csym->visibility(), csym->nonvis(),
1492                               false, false);
1493
1494   csym->set_is_copied_from_dynobj();
1495   csym->set_needs_dynsym_entry();
1496
1497   this->copied_symbol_dynobjs_[csym] = dynobj;
1498
1499   // We have now defined all aliases, but we have not entered them all
1500   // in the copied_symbol_dynobjs_ map.
1501   if (csym->has_alias())
1502     {
1503       Symbol* sym = csym;
1504       while (true)
1505         {
1506           sym = this->weak_aliases_[sym];
1507           if (sym == csym)
1508             break;
1509           gold_assert(sym->output_data() == posd);
1510
1511           sym->set_is_copied_from_dynobj();
1512           this->copied_symbol_dynobjs_[sym] = dynobj;
1513         }
1514     }
1515 }
1516
1517 // SYM is defined using a COPY reloc.  Return the dynamic object where
1518 // the original definition was found.
1519
1520 Dynobj*
1521 Symbol_table::get_copy_source(const Symbol* sym) const
1522 {
1523   gold_assert(sym->is_copied_from_dynobj());
1524   Copied_symbol_dynobjs::const_iterator p =
1525     this->copied_symbol_dynobjs_.find(sym);
1526   gold_assert(p != this->copied_symbol_dynobjs_.end());
1527   return p->second;
1528 }
1529
1530 // Set the dynamic symbol indexes.  INDEX is the index of the first
1531 // global dynamic symbol.  Pointers to the symbols are stored into the
1532 // vector SYMS.  The names are added to DYNPOOL.  This returns an
1533 // updated dynamic symbol index.
1534
1535 unsigned int
1536 Symbol_table::set_dynsym_indexes(unsigned int index,
1537                                  std::vector<Symbol*>* syms,
1538                                  Stringpool* dynpool,
1539                                  Versions* versions)
1540 {
1541   for (Symbol_table_type::iterator p = this->table_.begin();
1542        p != this->table_.end();
1543        ++p)
1544     {
1545       Symbol* sym = p->second;
1546
1547       // Note that SYM may already have a dynamic symbol index, since
1548       // some symbols appear more than once in the symbol table, with
1549       // and without a version.
1550
1551       if (!sym->should_add_dynsym_entry())
1552         sym->set_dynsym_index(-1U);
1553       else if (!sym->has_dynsym_index())
1554         {
1555           sym->set_dynsym_index(index);
1556           ++index;
1557           syms->push_back(sym);
1558           dynpool->add(sym->name(), false, NULL);
1559
1560           // Record any version information.
1561           if (sym->version() != NULL)
1562             versions->record_version(this, dynpool, sym);
1563         }
1564     }
1565
1566   // Finish up the versions.  In some cases this may add new dynamic
1567   // symbols.
1568   index = versions->finalize(this, index, syms);
1569
1570   return index;
1571 }
1572
1573 // Set the final values for all the symbols.  The index of the first
1574 // global symbol in the output file is *PLOCAL_SYMCOUNT.  Record the
1575 // file offset OFF.  Add their names to POOL.  Return the new file
1576 // offset.  Update *PLOCAL_SYMCOUNT if necessary.
1577
1578 off_t
1579 Symbol_table::finalize(off_t off, off_t dynoff, size_t dyn_global_index,
1580                        size_t dyncount, Stringpool* pool,
1581                        unsigned int *plocal_symcount)
1582 {
1583   off_t ret;
1584
1585   gold_assert(*plocal_symcount != 0);
1586   this->first_global_index_ = *plocal_symcount;
1587
1588   this->dynamic_offset_ = dynoff;
1589   this->first_dynamic_global_index_ = dyn_global_index;
1590   this->dynamic_count_ = dyncount;
1591
1592   if (parameters->target().get_size() == 32)
1593     {
1594 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1595       ret = this->sized_finalize<32>(off, pool, plocal_symcount);
1596 #else
1597       gold_unreachable();
1598 #endif
1599     }
1600   else if (parameters->target().get_size() == 64)
1601     {
1602 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1603       ret = this->sized_finalize<64>(off, pool, plocal_symcount);
1604 #else
1605       gold_unreachable();
1606 #endif
1607     }
1608   else
1609     gold_unreachable();
1610
1611   // Now that we have the final symbol table, we can reliably note
1612   // which symbols should get warnings.
1613   this->warnings_.note_warnings(this);
1614
1615   return ret;
1616 }
1617
1618 // SYM is going into the symbol table at *PINDEX.  Add the name to
1619 // POOL, update *PINDEX and *POFF.
1620
1621 template<int size>
1622 void
1623 Symbol_table::add_to_final_symtab(Symbol* sym, Stringpool* pool,
1624                                   unsigned int* pindex, off_t* poff)
1625 {
1626   sym->set_symtab_index(*pindex);
1627   pool->add(sym->name(), false, NULL);
1628   ++*pindex;
1629   *poff += elfcpp::Elf_sizes<size>::sym_size;
1630 }
1631
1632 // Set the final value for all the symbols.  This is called after
1633 // Layout::finalize, so all the output sections have their final
1634 // address.
1635
1636 template<int size>
1637 off_t
1638 Symbol_table::sized_finalize(off_t off, Stringpool* pool,
1639                              unsigned int* plocal_symcount)
1640 {
1641   off = align_address(off, size >> 3);
1642   this->offset_ = off;
1643
1644   unsigned int index = *plocal_symcount;
1645   const unsigned int orig_index = index;
1646
1647   // First do all the symbols which have been forced to be local, as
1648   // they must appear before all global symbols.
1649   for (Forced_locals::iterator p = this->forced_locals_.begin();
1650        p != this->forced_locals_.end();
1651        ++p)
1652     {
1653       Symbol* sym = *p;
1654       gold_assert(sym->is_forced_local());
1655       if (this->sized_finalize_symbol<size>(sym))
1656         {
1657           this->add_to_final_symtab<size>(sym, pool, &index, &off);
1658           ++*plocal_symcount;
1659         }
1660     }
1661
1662   // Now do all the remaining symbols.
1663   for (Symbol_table_type::iterator p = this->table_.begin();
1664        p != this->table_.end();
1665        ++p)
1666     {
1667       Symbol* sym = p->second;
1668       if (this->sized_finalize_symbol<size>(sym))
1669         this->add_to_final_symtab<size>(sym, pool, &index, &off);
1670     }
1671
1672   this->output_count_ = index - orig_index;
1673
1674   return off;
1675 }
1676
1677 // Finalize the symbol SYM.  This returns true if the symbol should be
1678 // added to the symbol table, false otherwise.
1679
1680 template<int size>
1681 bool
1682 Symbol_table::sized_finalize_symbol(Symbol* unsized_sym)
1683 {
1684   Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(unsized_sym);
1685
1686   // The default version of a symbol may appear twice in the symbol
1687   // table.  We only need to finalize it once.
1688   if (sym->has_symtab_index())
1689     return false;
1690
1691   if (!sym->in_reg())
1692     {
1693       gold_assert(!sym->has_symtab_index());
1694       sym->set_symtab_index(-1U);
1695       gold_assert(sym->dynsym_index() == -1U);
1696       return false;
1697     }
1698
1699   typename Sized_symbol<size>::Value_type value;
1700
1701   switch (sym->source())
1702     {
1703     case Symbol::FROM_OBJECT:
1704       {
1705         unsigned int shndx = sym->shndx();
1706
1707         // FIXME: We need some target specific support here.
1708         if (shndx >= elfcpp::SHN_LORESERVE
1709             && shndx != elfcpp::SHN_ABS
1710             && shndx != elfcpp::SHN_COMMON)
1711           {
1712             gold_error(_("%s: unsupported symbol section 0x%x"),
1713                        sym->demangled_name().c_str(), shndx);
1714             shndx = elfcpp::SHN_UNDEF;
1715           }
1716
1717         Object* symobj = sym->object();
1718         if (symobj->is_dynamic())
1719           {
1720             value = 0;
1721             shndx = elfcpp::SHN_UNDEF;
1722           }
1723         else if (shndx == elfcpp::SHN_UNDEF)
1724           value = 0;
1725         else if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1726           value = sym->value();
1727         else
1728           {
1729             Relobj* relobj = static_cast<Relobj*>(symobj);
1730             section_offset_type secoff;
1731             Output_section* os = relobj->output_section(shndx, &secoff);
1732
1733             if (os == NULL)
1734               {
1735                 sym->set_symtab_index(-1U);
1736                 gold_assert(sym->dynsym_index() == -1U);
1737                 return false;
1738               }
1739
1740             if (sym->type() == elfcpp::STT_TLS)
1741               value = sym->value() + os->tls_offset() + secoff;
1742             else
1743               value = sym->value() + os->address() + secoff;
1744           }
1745       }
1746       break;
1747
1748     case Symbol::IN_OUTPUT_DATA:
1749       {
1750         Output_data* od = sym->output_data();
1751         value = sym->value() + od->address();
1752         if (sym->offset_is_from_end())
1753           value += od->data_size();
1754       }
1755       break;
1756
1757     case Symbol::IN_OUTPUT_SEGMENT:
1758       {
1759         Output_segment* os = sym->output_segment();
1760         value = sym->value() + os->vaddr();
1761         switch (sym->offset_base())
1762           {
1763           case Symbol::SEGMENT_START:
1764             break;
1765           case Symbol::SEGMENT_END:
1766             value += os->memsz();
1767             break;
1768           case Symbol::SEGMENT_BSS:
1769             value += os->filesz();
1770             break;
1771           default:
1772             gold_unreachable();
1773           }
1774       }
1775       break;
1776
1777     case Symbol::CONSTANT:
1778       value = sym->value();
1779       break;
1780
1781     default:
1782       gold_unreachable();
1783     }
1784
1785   sym->set_value(value);
1786
1787   if (parameters->options().strip_all())
1788     {
1789       sym->set_symtab_index(-1U);
1790       return false;
1791     }
1792
1793   return true;
1794 }
1795
1796 // Write out the global symbols.
1797
1798 void
1799 Symbol_table::write_globals(const Input_objects* input_objects,
1800                             const Stringpool* sympool,
1801                             const Stringpool* dynpool, Output_file* of) const
1802 {
1803   switch (parameters->size_and_endianness())
1804     {
1805 #ifdef HAVE_TARGET_32_LITTLE
1806     case Parameters::TARGET_32_LITTLE:
1807       this->sized_write_globals<32, false>(input_objects, sympool,
1808                                            dynpool, of);
1809       break;
1810 #endif
1811 #ifdef HAVE_TARGET_32_BIG
1812     case Parameters::TARGET_32_BIG:
1813       this->sized_write_globals<32, true>(input_objects, sympool,
1814                                           dynpool, of);
1815       break;
1816 #endif
1817 #ifdef HAVE_TARGET_64_LITTLE
1818     case Parameters::TARGET_64_LITTLE:
1819       this->sized_write_globals<64, false>(input_objects, sympool,
1820                                            dynpool, of);
1821       break;
1822 #endif
1823 #ifdef HAVE_TARGET_64_BIG
1824     case Parameters::TARGET_64_BIG:
1825       this->sized_write_globals<64, true>(input_objects, sympool,
1826                                           dynpool, of);
1827       break;
1828 #endif
1829     default:
1830       gold_unreachable();
1831     }
1832 }
1833
1834 // Write out the global symbols.
1835
1836 template<int size, bool big_endian>
1837 void
1838 Symbol_table::sized_write_globals(const Input_objects* input_objects,
1839                                   const Stringpool* sympool,
1840                                   const Stringpool* dynpool,
1841                                   Output_file* of) const
1842 {
1843   const Target& target = parameters->target();
1844
1845   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1846
1847   const unsigned int output_count = this->output_count_;
1848   const section_size_type oview_size = output_count * sym_size;
1849   const unsigned int first_global_index = this->first_global_index_;
1850   unsigned char* psyms;
1851   if (this->offset_ == 0 || output_count == 0)
1852     psyms = NULL;
1853   else
1854     psyms = of->get_output_view(this->offset_, oview_size);
1855
1856   const unsigned int dynamic_count = this->dynamic_count_;
1857   const section_size_type dynamic_size = dynamic_count * sym_size;
1858   const unsigned int first_dynamic_global_index =
1859     this->first_dynamic_global_index_;
1860   unsigned char* dynamic_view;
1861   if (this->dynamic_offset_ == 0 || dynamic_count == 0)
1862     dynamic_view = NULL;
1863   else
1864     dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1865
1866   for (Symbol_table_type::const_iterator p = this->table_.begin();
1867        p != this->table_.end();
1868        ++p)
1869     {
1870       Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1871
1872       // Possibly warn about unresolved symbols in shared libraries.
1873       this->warn_about_undefined_dynobj_symbol(input_objects, sym);
1874
1875       unsigned int sym_index = sym->symtab_index();
1876       unsigned int dynsym_index;
1877       if (dynamic_view == NULL)
1878         dynsym_index = -1U;
1879       else
1880         dynsym_index = sym->dynsym_index();
1881
1882       if (sym_index == -1U && dynsym_index == -1U)
1883         {
1884           // This symbol is not included in the output file.
1885           continue;
1886         }
1887
1888       unsigned int shndx;
1889       typename elfcpp::Elf_types<size>::Elf_Addr sym_value = sym->value();
1890       typename elfcpp::Elf_types<size>::Elf_Addr dynsym_value = sym_value;
1891       switch (sym->source())
1892         {
1893         case Symbol::FROM_OBJECT:
1894           {
1895             unsigned int in_shndx = sym->shndx();
1896
1897             // FIXME: We need some target specific support here.
1898             if (in_shndx >= elfcpp::SHN_LORESERVE
1899                 && in_shndx != elfcpp::SHN_ABS
1900                 && in_shndx != elfcpp::SHN_COMMON)
1901               {
1902                 gold_error(_("%s: unsupported symbol section 0x%x"),
1903                            sym->demangled_name().c_str(), in_shndx);
1904                 shndx = in_shndx;
1905               }
1906             else
1907               {
1908                 Object* symobj = sym->object();
1909                 if (symobj->is_dynamic())
1910                   {
1911                     if (sym->needs_dynsym_value())
1912                       dynsym_value = target.dynsym_value(sym);
1913                     shndx = elfcpp::SHN_UNDEF;
1914                   }
1915                 else if (in_shndx == elfcpp::SHN_UNDEF
1916                          || in_shndx == elfcpp::SHN_ABS
1917                          || in_shndx == elfcpp::SHN_COMMON)
1918                   shndx = in_shndx;
1919                 else
1920                   {
1921                     Relobj* relobj = static_cast<Relobj*>(symobj);
1922                     section_offset_type secoff;
1923                     Output_section* os = relobj->output_section(in_shndx,
1924                                                                 &secoff);
1925                     gold_assert(os != NULL);
1926                     shndx = os->out_shndx();
1927
1928                     // In object files symbol values are section
1929                     // relative.
1930                     if (parameters->options().relocatable())
1931                       sym_value -= os->address();
1932                   }
1933               }
1934           }
1935           break;
1936
1937         case Symbol::IN_OUTPUT_DATA:
1938           shndx = sym->output_data()->out_shndx();
1939           break;
1940
1941         case Symbol::IN_OUTPUT_SEGMENT:
1942           shndx = elfcpp::SHN_ABS;
1943           break;
1944
1945         case Symbol::CONSTANT:
1946           shndx = elfcpp::SHN_ABS;
1947           break;
1948
1949         default:
1950           gold_unreachable();
1951         }
1952
1953       if (sym_index != -1U)
1954         {
1955           sym_index -= first_global_index;
1956           gold_assert(sym_index < output_count);
1957           unsigned char* ps = psyms + (sym_index * sym_size);
1958           this->sized_write_symbol<size, big_endian>(sym, sym_value, shndx,
1959                                                      sympool, ps);
1960         }
1961
1962       if (dynsym_index != -1U)
1963         {
1964           dynsym_index -= first_dynamic_global_index;
1965           gold_assert(dynsym_index < dynamic_count);
1966           unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1967           this->sized_write_symbol<size, big_endian>(sym, dynsym_value, shndx,
1968                                                      dynpool, pd);
1969         }
1970     }
1971
1972   of->write_output_view(this->offset_, oview_size, psyms);
1973   if (dynamic_view != NULL)
1974     of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1975 }
1976
1977 // Write out the symbol SYM, in section SHNDX, to P.  POOL is the
1978 // strtab holding the name.
1979
1980 template<int size, bool big_endian>
1981 void
1982 Symbol_table::sized_write_symbol(
1983     Sized_symbol<size>* sym,
1984     typename elfcpp::Elf_types<size>::Elf_Addr value,
1985     unsigned int shndx,
1986     const Stringpool* pool,
1987     unsigned char* p) const
1988 {
1989   elfcpp::Sym_write<size, big_endian> osym(p);
1990   osym.put_st_name(pool->get_offset(sym->name()));
1991   osym.put_st_value(value);
1992   osym.put_st_size(sym->symsize());
1993   // A version script may have overridden the default binding.
1994   if (sym->is_forced_local())
1995     osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL, sym->type()));
1996   else
1997     osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1998   osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1999   osym.put_st_shndx(shndx);
2000 }
2001
2002 // Check for unresolved symbols in shared libraries.  This is
2003 // controlled by the --allow-shlib-undefined option.
2004
2005 // We only warn about libraries for which we have seen all the
2006 // DT_NEEDED entries.  We don't try to track down DT_NEEDED entries
2007 // which were not seen in this link.  If we didn't see a DT_NEEDED
2008 // entry, we aren't going to be able to reliably report whether the
2009 // symbol is undefined.
2010
2011 // We also don't warn about libraries found in the system library
2012 // directory (the directory were we find libc.so); we assume that
2013 // those libraries are OK.  This heuristic avoids problems in
2014 // GNU/Linux, in which -ldl can have undefined references satisfied by
2015 // ld-linux.so.
2016
2017 inline void
2018 Symbol_table::warn_about_undefined_dynobj_symbol(
2019     const Input_objects* input_objects,
2020     Symbol* sym) const
2021 {
2022   if (sym->source() == Symbol::FROM_OBJECT
2023       && sym->object()->is_dynamic()
2024       && sym->shndx() == elfcpp::SHN_UNDEF
2025       && sym->binding() != elfcpp::STB_WEAK
2026       && !parameters->options().allow_shlib_undefined()
2027       && !parameters->target().is_defined_by_abi(sym)
2028       && !input_objects->found_in_system_library_directory(sym->object()))
2029     {
2030       // A very ugly cast.
2031       Dynobj* dynobj = static_cast<Dynobj*>(sym->object());
2032       if (!dynobj->has_unknown_needed_entries())
2033         gold_error(_("%s: undefined reference to '%s'"),
2034                    sym->object()->name().c_str(),
2035                    sym->demangled_name().c_str());
2036     }
2037 }
2038
2039 // Write out a section symbol.  Return the update offset.
2040
2041 void
2042 Symbol_table::write_section_symbol(const Output_section *os,
2043                                    Output_file* of,
2044                                    off_t offset) const
2045 {
2046   switch (parameters->size_and_endianness())
2047     {
2048 #ifdef HAVE_TARGET_32_LITTLE
2049     case Parameters::TARGET_32_LITTLE:
2050       this->sized_write_section_symbol<32, false>(os, of, offset);
2051       break;
2052 #endif
2053 #ifdef HAVE_TARGET_32_BIG
2054     case Parameters::TARGET_32_BIG:
2055       this->sized_write_section_symbol<32, true>(os, of, offset);
2056       break;
2057 #endif
2058 #ifdef HAVE_TARGET_64_LITTLE
2059     case Parameters::TARGET_64_LITTLE:
2060       this->sized_write_section_symbol<64, false>(os, of, offset);
2061       break;
2062 #endif
2063 #ifdef HAVE_TARGET_64_BIG
2064     case Parameters::TARGET_64_BIG:
2065       this->sized_write_section_symbol<64, true>(os, of, offset);
2066       break;
2067 #endif
2068     default:
2069       gold_unreachable();
2070     }
2071 }
2072
2073 // Write out a section symbol, specialized for size and endianness.
2074
2075 template<int size, bool big_endian>
2076 void
2077 Symbol_table::sized_write_section_symbol(const Output_section* os,
2078                                          Output_file* of,
2079                                          off_t offset) const
2080 {
2081   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
2082
2083   unsigned char* pov = of->get_output_view(offset, sym_size);
2084
2085   elfcpp::Sym_write<size, big_endian> osym(pov);
2086   osym.put_st_name(0);
2087   osym.put_st_value(os->address());
2088   osym.put_st_size(0);
2089   osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
2090                                        elfcpp::STT_SECTION));
2091   osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
2092   osym.put_st_shndx(os->out_shndx());
2093
2094   of->write_output_view(offset, sym_size, pov);
2095 }
2096
2097 // Print statistical information to stderr.  This is used for --stats.
2098
2099 void
2100 Symbol_table::print_stats() const
2101 {
2102 #if defined(HAVE_TR1_UNORDERED_MAP) || defined(HAVE_EXT_HASH_MAP)
2103   fprintf(stderr, _("%s: symbol table entries: %zu; buckets: %zu\n"),
2104           program_name, this->table_.size(), this->table_.bucket_count());
2105 #else
2106   fprintf(stderr, _("%s: symbol table entries: %zu\n"),
2107           program_name, this->table_.size());
2108 #endif
2109   this->namepool_.print_stats("symbol table stringpool");
2110 }
2111
2112 // We check for ODR violations by looking for symbols with the same
2113 // name for which the debugging information reports that they were
2114 // defined in different source locations.  When comparing the source
2115 // location, we consider instances with the same base filename and
2116 // line number to be the same.  This is because different object
2117 // files/shared libraries can include the same header file using
2118 // different paths, and we don't want to report an ODR violation in
2119 // that case.
2120
2121 // This struct is used to compare line information, as returned by
2122 // Dwarf_line_info::one_addr2line.  It implements a < comparison
2123 // operator used with std::set.
2124
2125 struct Odr_violation_compare
2126 {
2127   bool
2128   operator()(const std::string& s1, const std::string& s2) const
2129   {
2130     std::string::size_type pos1 = s1.rfind('/');
2131     std::string::size_type pos2 = s2.rfind('/');
2132     if (pos1 == std::string::npos
2133         || pos2 == std::string::npos)
2134       return s1 < s2;
2135     return s1.compare(pos1, std::string::npos,
2136                       s2, pos2, std::string::npos) < 0;
2137   }
2138 };
2139
2140 // Check candidate_odr_violations_ to find symbols with the same name
2141 // but apparently different definitions (different source-file/line-no).
2142
2143 void
2144 Symbol_table::detect_odr_violations(const Task* task,
2145                                     const char* output_file_name) const
2146 {
2147   for (Odr_map::const_iterator it = candidate_odr_violations_.begin();
2148        it != candidate_odr_violations_.end();
2149        ++it)
2150     {
2151       const char* symbol_name = it->first;
2152       // We use a sorted set so the output is deterministic.
2153       std::set<std::string, Odr_violation_compare> line_nums;
2154
2155       for (Unordered_set<Symbol_location, Symbol_location_hash>::const_iterator
2156                locs = it->second.begin();
2157            locs != it->second.end();
2158            ++locs)
2159         {
2160           // We need to lock the object in order to read it.  This
2161           // means that we have to run in a singleton Task.  If we
2162           // want to run this in a general Task for better
2163           // performance, we will need one Task for object, plus
2164           // appropriate locking to ensure that we don't conflict with
2165           // other uses of the object.
2166           Task_lock_obj<Object> tl(task, locs->object);
2167           std::string lineno = Dwarf_line_info::one_addr2line(
2168               locs->object, locs->shndx, locs->offset);
2169           if (!lineno.empty())
2170             line_nums.insert(lineno);
2171         }
2172
2173       if (line_nums.size() > 1)
2174         {
2175           gold_warning(_("while linking %s: symbol '%s' defined in multiple "
2176                          "places (possible ODR violation):"),
2177                        output_file_name, demangle(symbol_name).c_str());
2178           for (std::set<std::string>::const_iterator it2 = line_nums.begin();
2179                it2 != line_nums.end();
2180                ++it2)
2181             fprintf(stderr, "  %s\n", it2->c_str());
2182         }
2183     }
2184 }
2185
2186 // Warnings functions.
2187
2188 // Add a new warning.
2189
2190 void
2191 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
2192                       const std::string& warning)
2193 {
2194   name = symtab->canonicalize_name(name);
2195   this->warnings_[name].set(obj, warning);
2196 }
2197
2198 // Look through the warnings and mark the symbols for which we should
2199 // warn.  This is called during Layout::finalize when we know the
2200 // sources for all the symbols.
2201
2202 void
2203 Warnings::note_warnings(Symbol_table* symtab)
2204 {
2205   for (Warning_table::iterator p = this->warnings_.begin();
2206        p != this->warnings_.end();
2207        ++p)
2208     {
2209       Symbol* sym = symtab->lookup(p->first, NULL);
2210       if (sym != NULL
2211           && sym->source() == Symbol::FROM_OBJECT
2212           && sym->object() == p->second.object)
2213         sym->set_has_warning();
2214     }
2215 }
2216
2217 // Issue a warning.  This is called when we see a relocation against a
2218 // symbol for which has a warning.
2219
2220 template<int size, bool big_endian>
2221 void
2222 Warnings::issue_warning(const Symbol* sym,
2223                         const Relocate_info<size, big_endian>* relinfo,
2224                         size_t relnum, off_t reloffset) const
2225 {
2226   gold_assert(sym->has_warning());
2227   Warning_table::const_iterator p = this->warnings_.find(sym->name());
2228   gold_assert(p != this->warnings_.end());
2229   gold_warning_at_location(relinfo, relnum, reloffset,
2230                            "%s", p->second.text.c_str());
2231 }
2232
2233 // Instantiate the templates we need.  We could use the configure
2234 // script to restrict this to only the ones needed for implemented
2235 // targets.
2236
2237 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2238 template
2239 void
2240 Sized_symbol<32>::allocate_common(Output_data*, Value_type);
2241 #endif
2242
2243 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2244 template
2245 void
2246 Sized_symbol<64>::allocate_common(Output_data*, Value_type);
2247 #endif
2248
2249 #ifdef HAVE_TARGET_32_LITTLE
2250 template
2251 void
2252 Symbol_table::add_from_relobj<32, false>(
2253     Sized_relobj<32, false>* relobj,
2254     const unsigned char* syms,
2255     size_t count,
2256     const char* sym_names,
2257     size_t sym_name_size,
2258     Sized_relobj<32, true>::Symbols* sympointers);
2259 #endif
2260
2261 #ifdef HAVE_TARGET_32_BIG
2262 template
2263 void
2264 Symbol_table::add_from_relobj<32, true>(
2265     Sized_relobj<32, true>* relobj,
2266     const unsigned char* syms,
2267     size_t count,
2268     const char* sym_names,
2269     size_t sym_name_size,
2270     Sized_relobj<32, false>::Symbols* sympointers);
2271 #endif
2272
2273 #ifdef HAVE_TARGET_64_LITTLE
2274 template
2275 void
2276 Symbol_table::add_from_relobj<64, false>(
2277     Sized_relobj<64, false>* relobj,
2278     const unsigned char* syms,
2279     size_t count,
2280     const char* sym_names,
2281     size_t sym_name_size,
2282     Sized_relobj<64, true>::Symbols* sympointers);
2283 #endif
2284
2285 #ifdef HAVE_TARGET_64_BIG
2286 template
2287 void
2288 Symbol_table::add_from_relobj<64, true>(
2289     Sized_relobj<64, true>* relobj,
2290     const unsigned char* syms,
2291     size_t count,
2292     const char* sym_names,
2293     size_t sym_name_size,
2294     Sized_relobj<64, false>::Symbols* sympointers);
2295 #endif
2296
2297 #ifdef HAVE_TARGET_32_LITTLE
2298 template
2299 void
2300 Symbol_table::add_from_dynobj<32, false>(
2301     Sized_dynobj<32, false>* dynobj,
2302     const unsigned char* syms,
2303     size_t count,
2304     const char* sym_names,
2305     size_t sym_name_size,
2306     const unsigned char* versym,
2307     size_t versym_size,
2308     const std::vector<const char*>* version_map);
2309 #endif
2310
2311 #ifdef HAVE_TARGET_32_BIG
2312 template
2313 void
2314 Symbol_table::add_from_dynobj<32, true>(
2315     Sized_dynobj<32, true>* dynobj,
2316     const unsigned char* syms,
2317     size_t count,
2318     const char* sym_names,
2319     size_t sym_name_size,
2320     const unsigned char* versym,
2321     size_t versym_size,
2322     const std::vector<const char*>* version_map);
2323 #endif
2324
2325 #ifdef HAVE_TARGET_64_LITTLE
2326 template
2327 void
2328 Symbol_table::add_from_dynobj<64, false>(
2329     Sized_dynobj<64, false>* dynobj,
2330     const unsigned char* syms,
2331     size_t count,
2332     const char* sym_names,
2333     size_t sym_name_size,
2334     const unsigned char* versym,
2335     size_t versym_size,
2336     const std::vector<const char*>* version_map);
2337 #endif
2338
2339 #ifdef HAVE_TARGET_64_BIG
2340 template
2341 void
2342 Symbol_table::add_from_dynobj<64, true>(
2343     Sized_dynobj<64, true>* dynobj,
2344     const unsigned char* syms,
2345     size_t count,
2346     const char* sym_names,
2347     size_t sym_name_size,
2348     const unsigned char* versym,
2349     size_t versym_size,
2350     const std::vector<const char*>* version_map);
2351 #endif
2352
2353 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
2354 template
2355 void
2356 Symbol_table::define_with_copy_reloc<32>(
2357     Sized_symbol<32>* sym,
2358     Output_data* posd,
2359     elfcpp::Elf_types<32>::Elf_Addr value);
2360 #endif
2361
2362 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
2363 template
2364 void
2365 Symbol_table::define_with_copy_reloc<64>(
2366     Sized_symbol<64>* sym,
2367     Output_data* posd,
2368     elfcpp::Elf_types<64>::Elf_Addr value);
2369 #endif
2370
2371 #ifdef HAVE_TARGET_32_LITTLE
2372 template
2373 void
2374 Warnings::issue_warning<32, false>(const Symbol* sym,
2375                                    const Relocate_info<32, false>* relinfo,
2376                                    size_t relnum, off_t reloffset) const;
2377 #endif
2378
2379 #ifdef HAVE_TARGET_32_BIG
2380 template
2381 void
2382 Warnings::issue_warning<32, true>(const Symbol* sym,
2383                                   const Relocate_info<32, true>* relinfo,
2384                                   size_t relnum, off_t reloffset) const;
2385 #endif
2386
2387 #ifdef HAVE_TARGET_64_LITTLE
2388 template
2389 void
2390 Warnings::issue_warning<64, false>(const Symbol* sym,
2391                                    const Relocate_info<64, false>* relinfo,
2392                                    size_t relnum, off_t reloffset) const;
2393 #endif
2394
2395 #ifdef HAVE_TARGET_64_BIG
2396 template
2397 void
2398 Warnings::issue_warning<64, true>(const Symbol* sym,
2399                                   const Relocate_info<64, true>* relinfo,
2400                                   size_t relnum, off_t reloffset) const;
2401 #endif
2402
2403 } // End namespace gold.