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