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