Implement -s and -S options which strip symbols.
[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           fprintf(stderr,
518                   _("%s: %s: bad global symbol name offset %u at %lu\n"),
519                   program_name, relobj->name().c_str(), st_name,
520                   static_cast<unsigned long>(i));
521           gold_exit(false);
522         }
523
524       const char* name = sym_names + st_name;
525
526       // A symbol defined in a section which we are not including must
527       // be treated as an undefined symbol.
528       unsigned char symbuf[sym_size];
529       elfcpp::Sym<size, big_endian> sym2(symbuf);
530       unsigned int st_shndx = psym->get_st_shndx();
531       if (st_shndx != elfcpp::SHN_UNDEF
532           && st_shndx < elfcpp::SHN_LORESERVE
533           && !relobj->is_section_included(st_shndx))
534         {
535           memcpy(symbuf, p, sym_size);
536           elfcpp::Sym_write<size, big_endian> sw(symbuf);
537           sw.put_st_shndx(elfcpp::SHN_UNDEF);
538           psym = &sym2;
539         }
540
541       // In an object file, an '@' in the name separates the symbol
542       // name from the version name.  If there are two '@' characters,
543       // this is the default version.
544       const char* ver = strchr(name, '@');
545
546       Symbol* res;
547       if (ver == NULL)
548         {
549           Stringpool::Key name_key;
550           name = this->namepool_.add(name, &name_key);
551           res = this->add_from_object(relobj, name, name_key, NULL, 0,
552                                       false, *psym);
553         }
554       else
555         {
556           Stringpool::Key name_key;
557           name = this->namepool_.add(name, ver - name, &name_key);
558
559           bool def = false;
560           ++ver;
561           if (*ver == '@')
562             {
563               def = true;
564               ++ver;
565             }
566
567           Stringpool::Key ver_key;
568           ver = this->namepool_.add(ver, &ver_key);
569
570           res = this->add_from_object(relobj, name, name_key, ver, ver_key,
571                                       def, *psym);
572         }
573
574       *sympointers++ = res;
575     }
576 }
577
578 // Add all the symbols in a dynamic object to the hash table.
579
580 template<int size, bool big_endian>
581 void
582 Symbol_table::add_from_dynobj(
583     Sized_dynobj<size, big_endian>* dynobj,
584     const unsigned char* syms,
585     size_t count,
586     const char* sym_names,
587     size_t sym_name_size,
588     const unsigned char* versym,
589     size_t versym_size,
590     const std::vector<const char*>* version_map)
591 {
592   gold_assert(size == dynobj->target()->get_size());
593   gold_assert(size == parameters->get_size());
594
595   if (versym != NULL && versym_size / 2 < count)
596     {
597       fprintf(stderr, _("%s: %s: too few symbol versions\n"),
598               program_name, dynobj->name().c_str());
599       gold_exit(false);
600     }
601
602   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
603
604   const unsigned char* p = syms;
605   const unsigned char* vs = versym;
606   for (size_t i = 0; i < count; ++i, p += sym_size, vs += 2)
607     {
608       elfcpp::Sym<size, big_endian> sym(p);
609
610       // Ignore symbols with local binding.
611       if (sym.get_st_bind() == elfcpp::STB_LOCAL)
612         continue;
613
614       unsigned int st_name = sym.get_st_name();
615       if (st_name >= sym_name_size)
616         {
617           fprintf(stderr, _("%s: %s: bad symbol name offset %u at %lu\n"),
618                   program_name, dynobj->name().c_str(), st_name,
619                   static_cast<unsigned long>(i));
620           gold_exit(false);
621         }
622
623       const char* name = sym_names + st_name;
624
625       if (versym == NULL)
626         {
627           Stringpool::Key name_key;
628           name = this->namepool_.add(name, &name_key);
629           this->add_from_object(dynobj, name, name_key, NULL, 0,
630                                 false, sym);
631           continue;
632         }
633
634       // Read the version information.
635
636       unsigned int v = elfcpp::Swap<16, big_endian>::readval(vs);
637
638       bool hidden = (v & elfcpp::VERSYM_HIDDEN) != 0;
639       v &= elfcpp::VERSYM_VERSION;
640
641       // The Sun documentation says that V can be VER_NDX_LOCAL, or
642       // VER_NDX_GLOBAL, or a version index.  The meaning of
643       // VER_NDX_LOCAL is defined as "Symbol has local scope."  The
644       // old GNU linker will happily generate VER_NDX_LOCAL for an
645       // undefined symbol.  I don't know what the Sun linker will
646       // generate.
647
648       if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
649           && sym.get_st_shndx() != elfcpp::SHN_UNDEF)
650         {
651           // This symbol should not be visible outside the object.
652           continue;
653         }
654
655       // At this point we are definitely going to add this symbol.
656       Stringpool::Key name_key;
657       name = this->namepool_.add(name, &name_key);
658
659       if (v == static_cast<unsigned int>(elfcpp::VER_NDX_LOCAL)
660           || v == static_cast<unsigned int>(elfcpp::VER_NDX_GLOBAL))
661         {
662           // This symbol does not have a version.
663           this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
664           continue;
665         }
666
667       if (v >= version_map->size())
668         {
669           fprintf(stderr,
670                   _("%s: %s: versym for symbol %zu out of range: %u\n"),
671                   program_name, dynobj->name().c_str(), i, v);
672           gold_exit(false);
673         }
674
675       const char* version = (*version_map)[v];
676       if (version == NULL)
677         {
678           fprintf(stderr, _("%s: %s: versym for symbol %zu has no name: %u\n"),
679                   program_name, dynobj->name().c_str(), i, v);
680           gold_exit(false);
681         }
682
683       Stringpool::Key version_key;
684       version = this->namepool_.add(version, &version_key);
685
686       // If this is an absolute symbol, and the version name and
687       // symbol name are the same, then this is the version definition
688       // symbol.  These symbols exist to support using -u to pull in
689       // particular versions.  We do not want to record a version for
690       // them.
691       if (sym.get_st_shndx() == elfcpp::SHN_ABS && name_key == version_key)
692         {
693           this->add_from_object(dynobj, name, name_key, NULL, 0, false, sym);
694           continue;
695         }
696
697       const bool def = !hidden && sym.get_st_shndx() != elfcpp::SHN_UNDEF;
698
699       this->add_from_object(dynobj, name, name_key, version, version_key,
700                             def, sym);
701     }
702 }
703
704 // Create and return a specially defined symbol.  If ONLY_IF_REF is
705 // true, then only create the symbol if there is a reference to it.
706 // If this does not return NULL, it sets *POLDSYM to the existing
707 // symbol if there is one.  This canonicalizes *PNAME and *PVERSION.
708
709 template<int size, bool big_endian>
710 Sized_symbol<size>*
711 Symbol_table::define_special_symbol(const Target* target, const char** pname,
712                                     const char** pversion, bool only_if_ref,
713                                     Sized_symbol<size>** poldsym
714                                     ACCEPT_SIZE_ENDIAN)
715 {
716   Symbol* oldsym;
717   Sized_symbol<size>* sym;
718   bool add_to_table = false;
719   typename Symbol_table_type::iterator add_loc = this->table_.end();
720
721   if (only_if_ref)
722     {
723       oldsym = this->lookup(*pname, *pversion);
724       if (oldsym == NULL || !oldsym->is_undefined())
725         return NULL;
726
727       *pname = oldsym->name();
728       *pversion = oldsym->version();
729     }
730   else
731     {
732       // Canonicalize NAME and VERSION.
733       Stringpool::Key name_key;
734       *pname = this->namepool_.add(*pname, &name_key);
735
736       Stringpool::Key version_key = 0;
737       if (*pversion != NULL)
738         *pversion = this->namepool_.add(*pversion, &version_key);
739
740       Symbol* const snull = NULL;
741       std::pair<typename Symbol_table_type::iterator, bool> ins =
742         this->table_.insert(std::make_pair(std::make_pair(name_key,
743                                                           version_key),
744                                            snull));
745
746       if (!ins.second)
747         {
748           // We already have a symbol table entry for NAME/VERSION.
749           oldsym = ins.first->second;
750           gold_assert(oldsym != NULL);
751         }
752       else
753         {
754           // We haven't seen this symbol before.
755           gold_assert(ins.first->second == NULL);
756           add_to_table = true;
757           add_loc = ins.first;
758           oldsym = NULL;
759         }
760     }
761
762   if (!target->has_make_symbol())
763     sym = new Sized_symbol<size>();
764   else
765     {
766       gold_assert(target->get_size() == size);
767       gold_assert(target->is_big_endian() ? big_endian : !big_endian);
768       typedef Sized_target<size, big_endian> My_target;
769       const My_target* sized_target =
770           static_cast<const My_target*>(target);
771       sym = sized_target->make_symbol();
772       if (sym == NULL)
773         return NULL;
774     }
775
776   if (add_to_table)
777     add_loc->second = sym;
778   else
779     gold_assert(oldsym != NULL);
780
781   *poldsym = this->get_sized_symbol SELECT_SIZE_NAME(size) (oldsym
782                                                             SELECT_SIZE(size));
783
784   return sym;
785 }
786
787 // Define a symbol based on an Output_data.
788
789 Symbol*
790 Symbol_table::define_in_output_data(const Target* target, const char* name,
791                                     const char* version, Output_data* od,
792                                     uint64_t value, uint64_t symsize,
793                                     elfcpp::STT type, elfcpp::STB binding,
794                                     elfcpp::STV visibility,
795                                     unsigned char nonvis,
796                                     bool offset_is_from_end,
797                                     bool only_if_ref)
798 {
799   if (parameters->get_size() == 32)
800     {
801 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
802       return this->do_define_in_output_data<32>(target, name, version, od,
803                                                 value, symsize, type, binding,
804                                                 visibility, nonvis,
805                                                 offset_is_from_end,
806                                                 only_if_ref);
807 #else
808       gold_unreachable();
809 #endif
810     }
811   else if (parameters->get_size() == 64)
812     {
813 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
814       return this->do_define_in_output_data<64>(target, name, version, od,
815                                                 value, symsize, type, binding,
816                                                 visibility, nonvis,
817                                                 offset_is_from_end,
818                                                 only_if_ref);
819 #else
820       gold_unreachable();
821 #endif
822     }
823   else
824     gold_unreachable();
825 }
826
827 // Define a symbol in an Output_data, sized version.
828
829 template<int size>
830 Sized_symbol<size>*
831 Symbol_table::do_define_in_output_data(
832     const Target* target,
833     const char* name,
834     const char* version,
835     Output_data* od,
836     typename elfcpp::Elf_types<size>::Elf_Addr value,
837     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
838     elfcpp::STT type,
839     elfcpp::STB binding,
840     elfcpp::STV visibility,
841     unsigned char nonvis,
842     bool offset_is_from_end,
843     bool only_if_ref)
844 {
845   Sized_symbol<size>* sym;
846   Sized_symbol<size>* oldsym;
847
848   if (parameters->is_big_endian())
849     {
850 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
851       sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
852           target, &name, &version, only_if_ref, &oldsym
853           SELECT_SIZE_ENDIAN(size, true));
854 #else
855       gold_unreachable();
856 #endif
857     }
858   else
859     {
860 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
861       sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
862           target, &name, &version, only_if_ref, &oldsym
863           SELECT_SIZE_ENDIAN(size, false));
864 #else
865       gold_unreachable();
866 #endif
867     }
868
869   if (sym == NULL)
870     return NULL;
871
872   gold_assert(version == NULL || oldsym != NULL);
873   sym->init(name, od, value, symsize, type, binding, visibility, nonvis,
874             offset_is_from_end);
875
876   if (oldsym != NULL
877       && Symbol_table::should_override_with_special(oldsym))
878     oldsym->override_with_special(sym);
879
880   return sym;
881 }
882
883 // Define a symbol based on an Output_segment.
884
885 Symbol*
886 Symbol_table::define_in_output_segment(const Target* target, const char* name,
887                                        const char* version, Output_segment* os,
888                                        uint64_t value, uint64_t symsize,
889                                        elfcpp::STT type, elfcpp::STB binding,
890                                        elfcpp::STV visibility,
891                                        unsigned char nonvis,
892                                        Symbol::Segment_offset_base offset_base,
893                                        bool only_if_ref)
894 {
895   if (parameters->get_size() == 32)
896     {
897 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
898       return this->do_define_in_output_segment<32>(target, name, version, os,
899                                                    value, symsize, type,
900                                                    binding, visibility, nonvis,
901                                                    offset_base, only_if_ref);
902 #else
903       gold_unreachable();
904 #endif
905     }
906   else if (parameters->get_size() == 64)
907     {
908 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
909       return this->do_define_in_output_segment<64>(target, name, version, os,
910                                                    value, symsize, type,
911                                                    binding, visibility, nonvis,
912                                                    offset_base, only_if_ref);
913 #else
914       gold_unreachable();
915 #endif
916     }
917   else
918     gold_unreachable();
919 }
920
921 // Define a symbol in an Output_segment, sized version.
922
923 template<int size>
924 Sized_symbol<size>*
925 Symbol_table::do_define_in_output_segment(
926     const Target* target,
927     const char* name,
928     const char* version,
929     Output_segment* os,
930     typename elfcpp::Elf_types<size>::Elf_Addr value,
931     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
932     elfcpp::STT type,
933     elfcpp::STB binding,
934     elfcpp::STV visibility,
935     unsigned char nonvis,
936     Symbol::Segment_offset_base offset_base,
937     bool only_if_ref)
938 {
939   Sized_symbol<size>* sym;
940   Sized_symbol<size>* oldsym;
941
942   if (parameters->is_big_endian())
943     {
944 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
945       sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
946           target, &name, &version, only_if_ref, &oldsym
947           SELECT_SIZE_ENDIAN(size, true));
948 #else
949       gold_unreachable();
950 #endif
951     }
952   else
953     {
954 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
955       sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
956           target, &name, &version, only_if_ref, &oldsym
957           SELECT_SIZE_ENDIAN(size, false));
958 #else
959       gold_unreachable();
960 #endif
961     }
962
963   if (sym == NULL)
964     return NULL;
965
966   gold_assert(version == NULL || oldsym != NULL);
967   sym->init(name, os, value, symsize, type, binding, visibility, nonvis,
968             offset_base);
969
970   if (oldsym != NULL
971       && Symbol_table::should_override_with_special(oldsym))
972     oldsym->override_with_special(sym);
973
974   return sym;
975 }
976
977 // Define a special symbol with a constant value.  It is a multiple
978 // definition error if this symbol is already defined.
979
980 Symbol*
981 Symbol_table::define_as_constant(const Target* target, const char* name,
982                                  const char* version, uint64_t value,
983                                  uint64_t symsize, elfcpp::STT type,
984                                  elfcpp::STB binding, elfcpp::STV visibility,
985                                  unsigned char nonvis, bool only_if_ref)
986 {
987   if (parameters->get_size() == 32)
988     {
989 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
990       return this->do_define_as_constant<32>(target, name, version, value,
991                                              symsize, type, binding,
992                                              visibility, nonvis, only_if_ref);
993 #else
994       gold_unreachable();
995 #endif
996     }
997   else if (parameters->get_size() == 64)
998     {
999 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1000       return this->do_define_as_constant<64>(target, name, version, value,
1001                                              symsize, type, binding,
1002                                              visibility, nonvis, only_if_ref);
1003 #else
1004       gold_unreachable();
1005 #endif
1006     }
1007   else
1008     gold_unreachable();
1009 }
1010
1011 // Define a symbol as a constant, sized version.
1012
1013 template<int size>
1014 Sized_symbol<size>*
1015 Symbol_table::do_define_as_constant(
1016     const Target* target,
1017     const char* name,
1018     const char* version,
1019     typename elfcpp::Elf_types<size>::Elf_Addr value,
1020     typename elfcpp::Elf_types<size>::Elf_WXword symsize,
1021     elfcpp::STT type,
1022     elfcpp::STB binding,
1023     elfcpp::STV visibility,
1024     unsigned char nonvis,
1025     bool only_if_ref)
1026 {
1027   Sized_symbol<size>* sym;
1028   Sized_symbol<size>* oldsym;
1029
1030   if (parameters->is_big_endian())
1031     {
1032 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_64_BIG)
1033       sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, true) (
1034           target, &name, &version, only_if_ref, &oldsym
1035           SELECT_SIZE_ENDIAN(size, true));
1036 #else
1037       gold_unreachable();
1038 #endif
1039     }
1040   else
1041     {
1042 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_64_LITTLE)
1043       sym = this->define_special_symbol SELECT_SIZE_ENDIAN_NAME(size, false) (
1044           target, &name, &version, only_if_ref, &oldsym
1045           SELECT_SIZE_ENDIAN(size, false));
1046 #else
1047       gold_unreachable();
1048 #endif
1049     }
1050
1051   if (sym == NULL)
1052     return NULL;
1053
1054   gold_assert(version == NULL || oldsym != NULL);
1055   sym->init(name, value, symsize, type, binding, visibility, nonvis);
1056
1057   if (oldsym != NULL
1058       && Symbol_table::should_override_with_special(oldsym))
1059     oldsym->override_with_special(sym);
1060
1061   return sym;
1062 }
1063
1064 // Define a set of symbols in output sections.
1065
1066 void
1067 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1068                              int count, const Define_symbol_in_section* p)
1069 {
1070   for (int i = 0; i < count; ++i, ++p)
1071     {
1072       Output_section* os = layout->find_output_section(p->output_section);
1073       if (os != NULL)
1074         this->define_in_output_data(target, p->name, NULL, os, p->value,
1075                                     p->size, p->type, p->binding,
1076                                     p->visibility, p->nonvis,
1077                                     p->offset_is_from_end, p->only_if_ref);
1078       else
1079         this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1080                                  p->binding, p->visibility, p->nonvis,
1081                                  p->only_if_ref);
1082     }
1083 }
1084
1085 // Define a set of symbols in output segments.
1086
1087 void
1088 Symbol_table::define_symbols(const Layout* layout, const Target* target,
1089                              int count, const Define_symbol_in_segment* p)
1090 {
1091   for (int i = 0; i < count; ++i, ++p)
1092     {
1093       Output_segment* os = layout->find_output_segment(p->segment_type,
1094                                                        p->segment_flags_set,
1095                                                        p->segment_flags_clear);
1096       if (os != NULL)
1097         this->define_in_output_segment(target, p->name, NULL, os, p->value,
1098                                        p->size, p->type, p->binding,
1099                                        p->visibility, p->nonvis,
1100                                        p->offset_base, p->only_if_ref);
1101       else
1102         this->define_as_constant(target, p->name, NULL, 0, p->size, p->type,
1103                                  p->binding, p->visibility, p->nonvis,
1104                                  p->only_if_ref);
1105     }
1106 }
1107
1108 // Set the dynamic symbol indexes.  INDEX is the index of the first
1109 // global dynamic symbol.  Pointers to the symbols are stored into the
1110 // vector SYMS.  The names are added to DYNPOOL.  This returns an
1111 // updated dynamic symbol index.
1112
1113 unsigned int
1114 Symbol_table::set_dynsym_indexes(const General_options* options,
1115                                  const Target* target,
1116                                  unsigned int index,
1117                                  std::vector<Symbol*>* syms,
1118                                  Stringpool* dynpool,
1119                                  Versions* versions)
1120 {
1121   for (Symbol_table_type::iterator p = this->table_.begin();
1122        p != this->table_.end();
1123        ++p)
1124     {
1125       Symbol* sym = p->second;
1126
1127       // Note that SYM may already have a dynamic symbol index, since
1128       // some symbols appear more than once in the symbol table, with
1129       // and without a version.
1130
1131       if (!sym->needs_dynsym_entry()
1132           && (!options->export_dynamic()
1133               || !sym->in_reg()
1134               || !sym->is_externally_visible()))
1135         sym->set_dynsym_index(-1U);
1136       else if (!sym->has_dynsym_index())
1137         {
1138           sym->set_dynsym_index(index);
1139           ++index;
1140           syms->push_back(sym);
1141           dynpool->add(sym->name(), NULL);
1142
1143           // Record any version information.
1144           if (sym->version() != NULL)
1145             versions->record_version(options, dynpool, sym);
1146         }
1147     }
1148
1149   // Finish up the versions.  In some cases this may add new dynamic
1150   // symbols.
1151   index = versions->finalize(target, this, index, syms);
1152
1153   return index;
1154 }
1155
1156 // Set the final values for all the symbols.  The index of the first
1157 // global symbol in the output file is INDEX.  Record the file offset
1158 // OFF.  Add their names to POOL.  Return the new file offset.
1159
1160 off_t
1161 Symbol_table::finalize(unsigned int index, off_t off, off_t dynoff,
1162                        size_t dyn_global_index, size_t dyncount,
1163                        Stringpool* pool)
1164 {
1165   off_t ret;
1166
1167   gold_assert(index != 0);
1168   this->first_global_index_ = index;
1169
1170   this->dynamic_offset_ = dynoff;
1171   this->first_dynamic_global_index_ = dyn_global_index;
1172   this->dynamic_count_ = dyncount;
1173
1174   if (parameters->get_size() == 32)
1175     {
1176 #if defined(HAVE_TARGET_32_BIG) || defined(HAVE_TARGET_32_LITTLE)
1177       ret = this->sized_finalize<32>(index, off, pool);
1178 #else
1179       gold_unreachable();
1180 #endif
1181     }
1182   else if (parameters->get_size() == 64)
1183     {
1184 #if defined(HAVE_TARGET_64_BIG) || defined(HAVE_TARGET_64_LITTLE)
1185       ret = this->sized_finalize<64>(index, off, pool);
1186 #else
1187       gold_unreachable();
1188 #endif
1189     }
1190   else
1191     gold_unreachable();
1192
1193   // Now that we have the final symbol table, we can reliably note
1194   // which symbols should get warnings.
1195   this->warnings_.note_warnings(this);
1196
1197   return ret;
1198 }
1199
1200 // Set the final value for all the symbols.  This is called after
1201 // Layout::finalize, so all the output sections have their final
1202 // address.
1203
1204 template<int size>
1205 off_t
1206 Symbol_table::sized_finalize(unsigned index, off_t off, Stringpool* pool)
1207 {
1208   off = align_address(off, size >> 3);
1209   this->offset_ = off;
1210
1211   size_t orig_index = index;
1212
1213   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1214   for (Symbol_table_type::iterator p = this->table_.begin();
1215        p != this->table_.end();
1216        ++p)
1217     {
1218       Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1219
1220       // FIXME: Here we need to decide which symbols should go into
1221       // the output file, based on --strip.
1222
1223       // The default version of a symbol may appear twice in the
1224       // symbol table.  We only need to finalize it once.
1225       if (sym->has_symtab_index())
1226         continue;
1227
1228       if (!sym->in_reg())
1229         {
1230           gold_assert(!sym->has_symtab_index());
1231           sym->set_symtab_index(-1U);
1232           gold_assert(sym->dynsym_index() == -1U);
1233           continue;
1234         }
1235
1236       typename Sized_symbol<size>::Value_type value;
1237
1238       switch (sym->source())
1239         {
1240         case Symbol::FROM_OBJECT:
1241           {
1242             unsigned int shndx = sym->shndx();
1243
1244             // FIXME: We need some target specific support here.
1245             if (shndx >= elfcpp::SHN_LORESERVE
1246                 && shndx != elfcpp::SHN_ABS)
1247               {
1248                 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1249                         program_name, sym->name(), shndx);
1250                 gold_exit(false);
1251               }
1252
1253             Object* symobj = sym->object();
1254             if (symobj->is_dynamic())
1255               {
1256                 value = 0;
1257                 shndx = elfcpp::SHN_UNDEF;
1258               }
1259             else if (shndx == elfcpp::SHN_UNDEF)
1260               value = 0;
1261             else if (shndx == elfcpp::SHN_ABS)
1262               value = sym->value();
1263             else
1264               {
1265                 Relobj* relobj = static_cast<Relobj*>(symobj);
1266                 off_t secoff;
1267                 Output_section* os = relobj->output_section(shndx, &secoff);
1268
1269                 if (os == NULL)
1270                   {
1271                     sym->set_symtab_index(-1U);
1272                     gold_assert(sym->dynsym_index() == -1U);
1273                     continue;
1274                   }
1275
1276                 value = sym->value() + os->address() + secoff;
1277               }
1278           }
1279           break;
1280
1281         case Symbol::IN_OUTPUT_DATA:
1282           {
1283             Output_data* od = sym->output_data();
1284             value = sym->value() + od->address();
1285             if (sym->offset_is_from_end())
1286               value += od->data_size();
1287           }
1288           break;
1289
1290         case Symbol::IN_OUTPUT_SEGMENT:
1291           {
1292             Output_segment* os = sym->output_segment();
1293             value = sym->value() + os->vaddr();
1294             switch (sym->offset_base())
1295               {
1296               case Symbol::SEGMENT_START:
1297                 break;
1298               case Symbol::SEGMENT_END:
1299                 value += os->memsz();
1300                 break;
1301               case Symbol::SEGMENT_BSS:
1302                 value += os->filesz();
1303                 break;
1304               default:
1305                 gold_unreachable();
1306               }
1307           }
1308           break;
1309
1310         case Symbol::CONSTANT:
1311           value = sym->value();
1312           break;
1313
1314         default:
1315           gold_unreachable();
1316         }
1317
1318       sym->set_value(value);
1319
1320       if (parameters->strip_all())
1321         sym->set_symtab_index(-1U);
1322       else
1323         {
1324           sym->set_symtab_index(index);
1325           pool->add(sym->name(), NULL);
1326           ++index;
1327           off += sym_size;
1328         }
1329     }
1330
1331   this->output_count_ = index - orig_index;
1332
1333   return off;
1334 }
1335
1336 // Write out the global symbols.
1337
1338 void
1339 Symbol_table::write_globals(const Target* target, const Stringpool* sympool,
1340                             const Stringpool* dynpool, Output_file* of) const
1341 {
1342   if (parameters->get_size() == 32)
1343     {
1344       if (parameters->is_big_endian())
1345         {
1346 #ifdef HAVE_TARGET_32_BIG
1347           this->sized_write_globals<32, true>(target, sympool, dynpool, of);
1348 #else
1349           gold_unreachable();
1350 #endif
1351         }
1352       else
1353         {
1354 #ifdef HAVE_TARGET_32_LITTLE
1355           this->sized_write_globals<32, false>(target, sympool, dynpool, of);
1356 #else
1357           gold_unreachable();
1358 #endif
1359         }
1360     }
1361   else if (parameters->get_size() == 64)
1362     {
1363       if (parameters->is_big_endian())
1364         {
1365 #ifdef HAVE_TARGET_64_BIG
1366           this->sized_write_globals<64, true>(target, sympool, dynpool, of);
1367 #else
1368           gold_unreachable();
1369 #endif
1370         }
1371       else
1372         {
1373 #ifdef HAVE_TARGET_64_LITTLE
1374           this->sized_write_globals<64, false>(target, sympool, dynpool, of);
1375 #else
1376           gold_unreachable();
1377 #endif
1378         }
1379     }
1380   else
1381     gold_unreachable();
1382 }
1383
1384 // Write out the global symbols.
1385
1386 template<int size, bool big_endian>
1387 void
1388 Symbol_table::sized_write_globals(const Target* target,
1389                                   const Stringpool* sympool,
1390                                   const Stringpool* dynpool,
1391                                   Output_file* of) const
1392 {
1393   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1394   unsigned int index = this->first_global_index_;
1395   const off_t oview_size = this->output_count_ * sym_size;
1396   unsigned char* const psyms = of->get_output_view(this->offset_, oview_size);
1397
1398   unsigned int dynamic_count = this->dynamic_count_;
1399   off_t dynamic_size = dynamic_count * sym_size;
1400   unsigned int first_dynamic_global_index = this->first_dynamic_global_index_;
1401   unsigned char* dynamic_view;
1402   if (this->dynamic_offset_ == 0)
1403     dynamic_view = NULL;
1404   else
1405     dynamic_view = of->get_output_view(this->dynamic_offset_, dynamic_size);
1406
1407   unsigned char* ps = psyms;
1408   for (Symbol_table_type::const_iterator p = this->table_.begin();
1409        p != this->table_.end();
1410        ++p)
1411     {
1412       Sized_symbol<size>* sym = static_cast<Sized_symbol<size>*>(p->second);
1413
1414       unsigned int sym_index = sym->symtab_index();
1415       unsigned int dynsym_index;
1416       if (dynamic_view == NULL)
1417         dynsym_index = -1U;
1418       else
1419         dynsym_index = sym->dynsym_index();
1420
1421       if (sym_index == -1U && dynsym_index == -1U)
1422         {
1423           // This symbol is not included in the output file.
1424           continue;
1425         }
1426
1427       if (sym_index == index)
1428         ++index;
1429       else if (sym_index != -1U)
1430         {
1431           // We have already seen this symbol, because it has a
1432           // default version.
1433           gold_assert(sym_index < index);
1434           if (dynsym_index == -1U)
1435             continue;
1436           sym_index = -1U;
1437         }
1438
1439       unsigned int shndx;
1440       typename elfcpp::Elf_types<32>::Elf_Addr value = sym->value();
1441       switch (sym->source())
1442         {
1443         case Symbol::FROM_OBJECT:
1444           {
1445             unsigned int in_shndx = sym->shndx();
1446
1447             // FIXME: We need some target specific support here.
1448             if (in_shndx >= elfcpp::SHN_LORESERVE
1449                 && in_shndx != elfcpp::SHN_ABS)
1450               {
1451                 fprintf(stderr, _("%s: %s: unsupported symbol section 0x%x\n"),
1452                         program_name, sym->name(), in_shndx);
1453                 gold_exit(false);
1454               }
1455
1456             Object* symobj = sym->object();
1457             if (symobj->is_dynamic())
1458               {
1459                 if (sym->needs_dynsym_value())
1460                   value = target->dynsym_value(sym);
1461                 shndx = elfcpp::SHN_UNDEF;
1462               }
1463             else if (in_shndx == elfcpp::SHN_UNDEF
1464                      || in_shndx == elfcpp::SHN_ABS)
1465               shndx = in_shndx;
1466             else
1467               {
1468                 Relobj* relobj = static_cast<Relobj*>(symobj);
1469                 off_t secoff;
1470                 Output_section* os = relobj->output_section(in_shndx, &secoff);
1471                 gold_assert(os != NULL);
1472                 shndx = os->out_shndx();
1473               }
1474           }
1475           break;
1476
1477         case Symbol::IN_OUTPUT_DATA:
1478           shndx = sym->output_data()->out_shndx();
1479           break;
1480
1481         case Symbol::IN_OUTPUT_SEGMENT:
1482           shndx = elfcpp::SHN_ABS;
1483           break;
1484
1485         case Symbol::CONSTANT:
1486           shndx = elfcpp::SHN_ABS;
1487           break;
1488
1489         default:
1490           gold_unreachable();
1491         }
1492
1493       if (sym_index != -1U)
1494         {
1495           this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1496               sym, sym->value(), shndx, sympool, ps
1497               SELECT_SIZE_ENDIAN(size, big_endian));
1498           ps += sym_size;
1499         }
1500
1501       if (dynsym_index != -1U)
1502         {
1503           dynsym_index -= first_dynamic_global_index;
1504           gold_assert(dynsym_index < dynamic_count);
1505           unsigned char* pd = dynamic_view + (dynsym_index * sym_size);
1506           this->sized_write_symbol SELECT_SIZE_ENDIAN_NAME(size, big_endian) (
1507               sym, value, shndx, dynpool, pd
1508               SELECT_SIZE_ENDIAN(size, big_endian));
1509         }
1510     }
1511
1512   gold_assert(ps - psyms == oview_size);
1513
1514   of->write_output_view(this->offset_, oview_size, psyms);
1515   if (dynamic_view != NULL)
1516     of->write_output_view(this->dynamic_offset_, dynamic_size, dynamic_view);
1517 }
1518
1519 // Write out the symbol SYM, in section SHNDX, to P.  POOL is the
1520 // strtab holding the name.
1521
1522 template<int size, bool big_endian>
1523 void
1524 Symbol_table::sized_write_symbol(
1525     Sized_symbol<size>* sym,
1526     typename elfcpp::Elf_types<size>::Elf_Addr value,
1527     unsigned int shndx,
1528     const Stringpool* pool,
1529     unsigned char* p
1530     ACCEPT_SIZE_ENDIAN) const
1531 {
1532   elfcpp::Sym_write<size, big_endian> osym(p);
1533   osym.put_st_name(pool->get_offset(sym->name()));
1534   osym.put_st_value(value);
1535   osym.put_st_size(sym->symsize());
1536   osym.put_st_info(elfcpp::elf_st_info(sym->binding(), sym->type()));
1537   osym.put_st_other(elfcpp::elf_st_other(sym->visibility(), sym->nonvis()));
1538   osym.put_st_shndx(shndx);
1539 }
1540
1541 // Write out a section symbol.  Return the update offset.
1542
1543 void
1544 Symbol_table::write_section_symbol(const Output_section *os,
1545                                    Output_file* of,
1546                                    off_t offset) const
1547 {
1548   if (parameters->get_size() == 32)
1549     {
1550       if (parameters->is_big_endian())
1551         {
1552 #ifdef HAVE_TARGET_32_BIG
1553           this->sized_write_section_symbol<32, true>(os, of, offset);
1554 #else
1555           gold_unreachable();
1556 #endif
1557         }
1558       else
1559         {
1560 #ifdef HAVE_TARGET_32_LITTLE
1561           this->sized_write_section_symbol<32, false>(os, of, offset);
1562 #else
1563           gold_unreachable();
1564 #endif
1565         }
1566     }
1567   else if (parameters->get_size() == 64)
1568     {
1569       if (parameters->is_big_endian())
1570         {
1571 #ifdef HAVE_TARGET_64_BIG
1572           this->sized_write_section_symbol<64, true>(os, of, offset);
1573 #else
1574           gold_unreachable();
1575 #endif
1576         }
1577       else
1578         {
1579 #ifdef HAVE_TARGET_64_LITTLE
1580           this->sized_write_section_symbol<64, false>(os, of, offset);
1581 #else
1582           gold_unreachable();
1583 #endif
1584         }
1585     }
1586   else
1587     gold_unreachable();
1588 }
1589
1590 // Write out a section symbol, specialized for size and endianness.
1591
1592 template<int size, bool big_endian>
1593 void
1594 Symbol_table::sized_write_section_symbol(const Output_section* os,
1595                                          Output_file* of,
1596                                          off_t offset) const
1597 {
1598   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1599
1600   unsigned char* pov = of->get_output_view(offset, sym_size);
1601
1602   elfcpp::Sym_write<size, big_endian> osym(pov);
1603   osym.put_st_name(0);
1604   osym.put_st_value(os->address());
1605   osym.put_st_size(0);
1606   osym.put_st_info(elfcpp::elf_st_info(elfcpp::STB_LOCAL,
1607                                        elfcpp::STT_SECTION));
1608   osym.put_st_other(elfcpp::elf_st_other(elfcpp::STV_DEFAULT, 0));
1609   osym.put_st_shndx(os->out_shndx());
1610
1611   of->write_output_view(offset, sym_size, pov);
1612 }
1613
1614 // Warnings functions.
1615
1616 // Add a new warning.
1617
1618 void
1619 Warnings::add_warning(Symbol_table* symtab, const char* name, Object* obj,
1620                       unsigned int shndx)
1621 {
1622   name = symtab->canonicalize_name(name);
1623   this->warnings_[name].set(obj, shndx);
1624 }
1625
1626 // Look through the warnings and mark the symbols for which we should
1627 // warn.  This is called during Layout::finalize when we know the
1628 // sources for all the symbols.
1629
1630 void
1631 Warnings::note_warnings(Symbol_table* symtab)
1632 {
1633   for (Warning_table::iterator p = this->warnings_.begin();
1634        p != this->warnings_.end();
1635        ++p)
1636     {
1637       Symbol* sym = symtab->lookup(p->first, NULL);
1638       if (sym != NULL
1639           && sym->source() == Symbol::FROM_OBJECT
1640           && sym->object() == p->second.object)
1641         {
1642           sym->set_has_warning();
1643
1644           // Read the section contents to get the warning text.  It
1645           // would be nicer if we only did this if we have to actually
1646           // issue a warning.  Unfortunately, warnings are issued as
1647           // we relocate sections.  That means that we can not lock
1648           // the object then, as we might try to issue the same
1649           // warning multiple times simultaneously.
1650           {
1651             Task_locker_obj<Object> tl(*p->second.object);
1652             const unsigned char* c;
1653             off_t len;
1654             c = p->second.object->section_contents(p->second.shndx, &len,
1655                                                    false);
1656             p->second.set_text(reinterpret_cast<const char*>(c), len);
1657           }
1658         }
1659     }
1660 }
1661
1662 // Issue a warning.  This is called when we see a relocation against a
1663 // symbol for which has a warning.
1664
1665 void
1666 Warnings::issue_warning(const Symbol* sym, const std::string& location) const
1667 {
1668   gold_assert(sym->has_warning());
1669   Warning_table::const_iterator p = this->warnings_.find(sym->name());
1670   gold_assert(p != this->warnings_.end());
1671   fprintf(stderr, _("%s: %s: warning: %s\n"), program_name, location.c_str(),
1672           p->second.text.c_str());
1673 }
1674
1675 // Instantiate the templates we need.  We could use the configure
1676 // script to restrict this to only the ones needed for implemented
1677 // targets.
1678
1679 #ifdef HAVE_TARGET_32_LITTLE
1680 template
1681 void
1682 Symbol_table::add_from_relobj<32, false>(
1683     Sized_relobj<32, false>* relobj,
1684     const unsigned char* syms,
1685     size_t count,
1686     const char* sym_names,
1687     size_t sym_name_size,
1688     Symbol** sympointers);
1689 #endif
1690
1691 #ifdef HAVE_TARGET_32_BIG
1692 template
1693 void
1694 Symbol_table::add_from_relobj<32, true>(
1695     Sized_relobj<32, true>* relobj,
1696     const unsigned char* syms,
1697     size_t count,
1698     const char* sym_names,
1699     size_t sym_name_size,
1700     Symbol** sympointers);
1701 #endif
1702
1703 #ifdef HAVE_TARGET_64_LITTLE
1704 template
1705 void
1706 Symbol_table::add_from_relobj<64, false>(
1707     Sized_relobj<64, false>* relobj,
1708     const unsigned char* syms,
1709     size_t count,
1710     const char* sym_names,
1711     size_t sym_name_size,
1712     Symbol** sympointers);
1713 #endif
1714
1715 #ifdef HAVE_TARGET_64_BIG
1716 template
1717 void
1718 Symbol_table::add_from_relobj<64, true>(
1719     Sized_relobj<64, true>* relobj,
1720     const unsigned char* syms,
1721     size_t count,
1722     const char* sym_names,
1723     size_t sym_name_size,
1724     Symbol** sympointers);
1725 #endif
1726
1727 #ifdef HAVE_TARGET_32_LITTLE
1728 template
1729 void
1730 Symbol_table::add_from_dynobj<32, false>(
1731     Sized_dynobj<32, false>* dynobj,
1732     const unsigned char* syms,
1733     size_t count,
1734     const char* sym_names,
1735     size_t sym_name_size,
1736     const unsigned char* versym,
1737     size_t versym_size,
1738     const std::vector<const char*>* version_map);
1739 #endif
1740
1741 #ifdef HAVE_TARGET_32_BIG
1742 template
1743 void
1744 Symbol_table::add_from_dynobj<32, true>(
1745     Sized_dynobj<32, true>* dynobj,
1746     const unsigned char* syms,
1747     size_t count,
1748     const char* sym_names,
1749     size_t sym_name_size,
1750     const unsigned char* versym,
1751     size_t versym_size,
1752     const std::vector<const char*>* version_map);
1753 #endif
1754
1755 #ifdef HAVE_TARGET_64_LITTLE
1756 template
1757 void
1758 Symbol_table::add_from_dynobj<64, false>(
1759     Sized_dynobj<64, false>* dynobj,
1760     const unsigned char* syms,
1761     size_t count,
1762     const char* sym_names,
1763     size_t sym_name_size,
1764     const unsigned char* versym,
1765     size_t versym_size,
1766     const std::vector<const char*>* version_map);
1767 #endif
1768
1769 #ifdef HAVE_TARGET_64_BIG
1770 template
1771 void
1772 Symbol_table::add_from_dynobj<64, true>(
1773     Sized_dynobj<64, true>* dynobj,
1774     const unsigned char* syms,
1775     size_t count,
1776     const char* sym_names,
1777     size_t sym_name_size,
1778     const unsigned char* versym,
1779     size_t versym_size,
1780     const std::vector<const char*>* version_map);
1781 #endif
1782
1783 } // End namespace gold.