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