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