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