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