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