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