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