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