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