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