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