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