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