PR 6992
[platform/upstream/binutils.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 // Copyright 2006, 2007, 2008, 2009 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 <cerrno>
26 #include <cstring>
27 #include <cstdarg>
28 #include "demangle.h"
29 #include "libiberty.h"
30
31 #include "gc.h"
32 #include "target-select.h"
33 #include "dwarf_reader.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "symtab.h"
37 #include "cref.h"
38 #include "reloc.h"
39 #include "object.h"
40 #include "dynobj.h"
41 #include "plugin.h"
42
43 namespace gold
44 {
45
46 // Class Xindex.
47
48 // Initialize the symtab_xindex_ array.  Find the SHT_SYMTAB_SHNDX
49 // section and read it in.  SYMTAB_SHNDX is the index of the symbol
50 // table we care about.
51
52 template<int size, bool big_endian>
53 void
54 Xindex::initialize_symtab_xindex(Object* object, unsigned int symtab_shndx)
55 {
56   if (!this->symtab_xindex_.empty())
57     return;
58
59   gold_assert(symtab_shndx != 0);
60
61   // Look through the sections in reverse order, on the theory that it
62   // is more likely to be near the end than the beginning.
63   unsigned int i = object->shnum();
64   while (i > 0)
65     {
66       --i;
67       if (object->section_type(i) == elfcpp::SHT_SYMTAB_SHNDX
68           && this->adjust_shndx(object->section_link(i)) == symtab_shndx)
69         {
70           this->read_symtab_xindex<size, big_endian>(object, i, NULL);
71           return;
72         }
73     }
74
75   object->error(_("missing SHT_SYMTAB_SHNDX section"));
76 }
77
78 // Read in the symtab_xindex_ array, given the section index of the
79 // SHT_SYMTAB_SHNDX section.  If PSHDRS is not NULL, it points at the
80 // section headers.
81
82 template<int size, bool big_endian>
83 void
84 Xindex::read_symtab_xindex(Object* object, unsigned int xindex_shndx,
85                            const unsigned char* pshdrs)
86 {
87   section_size_type bytecount;
88   const unsigned char* contents;
89   if (pshdrs == NULL)
90     contents = object->section_contents(xindex_shndx, &bytecount, false);
91   else
92     {
93       const unsigned char* p = (pshdrs
94                                 + (xindex_shndx
95                                    * elfcpp::Elf_sizes<size>::shdr_size));
96       typename elfcpp::Shdr<size, big_endian> shdr(p);
97       bytecount = convert_to_section_size_type(shdr.get_sh_size());
98       contents = object->get_view(shdr.get_sh_offset(), bytecount, true, false);
99     }
100
101   gold_assert(this->symtab_xindex_.empty());
102   this->symtab_xindex_.reserve(bytecount / 4);
103   for (section_size_type i = 0; i < bytecount; i += 4)
104     {
105       unsigned int shndx = elfcpp::Swap<32, big_endian>::readval(contents + i);
106       // We preadjust the section indexes we save.
107       this->symtab_xindex_.push_back(this->adjust_shndx(shndx));
108     }
109 }
110
111 // Symbol symndx has a section of SHN_XINDEX; return the real section
112 // index.
113
114 unsigned int
115 Xindex::sym_xindex_to_shndx(Object* object, unsigned int symndx)
116 {
117   if (symndx >= this->symtab_xindex_.size())
118     {
119       object->error(_("symbol %u out of range for SHT_SYMTAB_SHNDX section"),
120                     symndx);
121       return elfcpp::SHN_UNDEF;
122     }
123   unsigned int shndx = this->symtab_xindex_[symndx];
124   if (shndx < elfcpp::SHN_LORESERVE || shndx >= object->shnum())
125     {
126       object->error(_("extended index for symbol %u out of range: %u"),
127                     symndx, shndx);
128       return elfcpp::SHN_UNDEF;
129     }
130   return shndx;
131 }
132
133 // Class Object.
134
135 // Set the target based on fields in the ELF file header.
136
137 void
138 Object::set_target(int machine, int size, bool big_endian, int osabi,
139                    int abiversion)
140 {
141   Target* target = select_target(machine, size, big_endian, osabi, abiversion);
142   if (target == NULL)
143     gold_fatal(_("%s: unsupported ELF machine number %d"),
144                this->name().c_str(), machine);
145   this->target_ = target;
146 }
147
148 // Report an error for this object file.  This is used by the
149 // elfcpp::Elf_file interface, and also called by the Object code
150 // itself.
151
152 void
153 Object::error(const char* format, ...) const
154 {
155   va_list args;
156   va_start(args, format);
157   char* buf = NULL;
158   if (vasprintf(&buf, format, args) < 0)
159     gold_nomem();
160   va_end(args);
161   gold_error(_("%s: %s"), this->name().c_str(), buf);
162   free(buf);
163 }
164
165 // Return a view of the contents of a section.
166
167 const unsigned char*
168 Object::section_contents(unsigned int shndx, section_size_type* plen,
169                          bool cache)
170 {
171   Location loc(this->do_section_contents(shndx));
172   *plen = convert_to_section_size_type(loc.data_size);
173   return this->get_view(loc.file_offset, *plen, true, cache);
174 }
175
176 // Read the section data into SD.  This is code common to Sized_relobj
177 // and Sized_dynobj, so we put it into Object.
178
179 template<int size, bool big_endian>
180 void
181 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
182                           Read_symbols_data* sd)
183 {
184   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
185
186   // Read the section headers.
187   const off_t shoff = elf_file->shoff();
188   const unsigned int shnum = this->shnum();
189   sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size,
190                                                true, true);
191
192   // Read the section names.
193   const unsigned char* pshdrs = sd->section_headers->data();
194   const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
195   typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
196
197   if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
198     this->error(_("section name section has wrong type: %u"),
199                 static_cast<unsigned int>(shdrnames.get_sh_type()));
200
201   sd->section_names_size =
202     convert_to_section_size_type(shdrnames.get_sh_size());
203   sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
204                                              sd->section_names_size, false,
205                                              false);
206 }
207
208 // If NAME is the name of a special .gnu.warning section, arrange for
209 // the warning to be issued.  SHNDX is the section index.  Return
210 // whether it is a warning section.
211
212 bool
213 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
214                                    Symbol_table* symtab)
215 {
216   const char warn_prefix[] = ".gnu.warning.";
217   const int warn_prefix_len = sizeof warn_prefix - 1;
218   if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
219     {
220       // Read the section contents to get the warning text.  It would
221       // be nicer if we only did this if we have to actually issue a
222       // warning.  Unfortunately, warnings are issued as we relocate
223       // sections.  That means that we can not lock the object then,
224       // as we might try to issue the same warning multiple times
225       // simultaneously.
226       section_size_type len;
227       const unsigned char* contents = this->section_contents(shndx, &len,
228                                                              false);
229       std::string warning(reinterpret_cast<const char*>(contents), len);
230       symtab->add_warning(name + warn_prefix_len, this, warning);
231       return true;
232     }
233   return false;
234 }
235
236 // Class Relobj
237
238 // To copy the symbols data read from the file to a local data structure.
239 // This function is called from do_layout only while doing garbage 
240 // collection.
241
242 void
243 Relobj::copy_symbols_data(Symbols_data* gc_sd, Read_symbols_data* sd, 
244                           unsigned int section_header_size)
245 {
246   gc_sd->section_headers_data = 
247          new unsigned char[(section_header_size)];
248   memcpy(gc_sd->section_headers_data, sd->section_headers->data(),
249          section_header_size);
250   gc_sd->section_names_data = 
251          new unsigned char[sd->section_names_size];
252   memcpy(gc_sd->section_names_data, sd->section_names->data(),
253          sd->section_names_size);
254   gc_sd->section_names_size = sd->section_names_size;
255   if (sd->symbols != NULL)
256     {
257       gc_sd->symbols_data = 
258              new unsigned char[sd->symbols_size];
259       memcpy(gc_sd->symbols_data, sd->symbols->data(),
260             sd->symbols_size);
261     }
262   else
263     {
264       gc_sd->symbols_data = NULL;
265     }
266   gc_sd->symbols_size = sd->symbols_size;
267   gc_sd->external_symbols_offset = sd->external_symbols_offset;
268   if (sd->symbol_names != NULL)
269     {
270       gc_sd->symbol_names_data =
271              new unsigned char[sd->symbol_names_size];
272       memcpy(gc_sd->symbol_names_data, sd->symbol_names->data(),
273             sd->symbol_names_size);
274     }
275   else
276     {
277       gc_sd->symbol_names_data = NULL;
278     }
279   gc_sd->symbol_names_size = sd->symbol_names_size;
280 }
281
282 // This function determines if a particular section name must be included
283 // in the link.  This is used during garbage collection to determine the
284 // roots of the worklist.
285
286 bool
287 Relobj::is_section_name_included(const char* name)
288 {
289   if (is_prefix_of(".ctors", name) 
290       || is_prefix_of(".dtors", name) 
291       || is_prefix_of(".note", name) 
292       || is_prefix_of(".init", name) 
293       || is_prefix_of(".fini", name) 
294       || is_prefix_of(".gcc_except_table", name) 
295       || is_prefix_of(".jcr", name) 
296       || is_prefix_of(".preinit_array", name) 
297       || (is_prefix_of(".text", name) 
298           && strstr(name, "personality")) 
299       || (is_prefix_of(".data", name) 
300           &&  strstr(name, "personality")) 
301       || (is_prefix_of(".gnu.linkonce.d", name) && 
302             strstr(name, "personality")))
303     {
304       return true; 
305     }
306   return false;
307 }
308
309 // Class Sized_relobj.
310
311 template<int size, bool big_endian>
312 Sized_relobj<size, big_endian>::Sized_relobj(
313     const std::string& name,
314     Input_file* input_file,
315     off_t offset,
316     const elfcpp::Ehdr<size, big_endian>& ehdr)
317   : Relobj(name, input_file, offset),
318     elf_file_(this, ehdr),
319     symtab_shndx_(-1U),
320     local_symbol_count_(0),
321     output_local_symbol_count_(0),
322     output_local_dynsym_count_(0),
323     symbols_(),
324     defined_count_(0),
325     local_symbol_offset_(0),
326     local_dynsym_offset_(0),
327     local_values_(),
328     local_got_offsets_(),
329     kept_comdat_sections_(),
330     comdat_groups_(),
331     has_eh_frame_(false)
332 {
333 }
334
335 template<int size, bool big_endian>
336 Sized_relobj<size, big_endian>::~Sized_relobj()
337 {
338 }
339
340 // Set up an object file based on the file header.  This sets up the
341 // target and reads the section information.
342
343 template<int size, bool big_endian>
344 void
345 Sized_relobj<size, big_endian>::setup(
346     const elfcpp::Ehdr<size, big_endian>& ehdr)
347 {
348   this->set_target(ehdr.get_e_machine(), size, big_endian,
349                    ehdr.get_e_ident()[elfcpp::EI_OSABI],
350                    ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
351
352   const unsigned int shnum = this->elf_file_.shnum();
353   this->set_shnum(shnum);
354 }
355
356 // Find the SHT_SYMTAB section, given the section headers.  The ELF
357 // standard says that maybe in the future there can be more than one
358 // SHT_SYMTAB section.  Until somebody figures out how that could
359 // work, we assume there is only one.
360
361 template<int size, bool big_endian>
362 void
363 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
364 {
365   const unsigned int shnum = this->shnum();
366   this->symtab_shndx_ = 0;
367   if (shnum > 0)
368     {
369       // Look through the sections in reverse order, since gas tends
370       // to put the symbol table at the end.
371       const unsigned char* p = pshdrs + shnum * This::shdr_size;
372       unsigned int i = shnum;
373       unsigned int xindex_shndx = 0;
374       unsigned int xindex_link = 0;
375       while (i > 0)
376         {
377           --i;
378           p -= This::shdr_size;
379           typename This::Shdr shdr(p);
380           if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
381             {
382               this->symtab_shndx_ = i;
383               if (xindex_shndx > 0 && xindex_link == i)
384                 {
385                   Xindex* xindex =
386                     new Xindex(this->elf_file_.large_shndx_offset());
387                   xindex->read_symtab_xindex<size, big_endian>(this,
388                                                                xindex_shndx,
389                                                                pshdrs);
390                   this->set_xindex(xindex);
391                 }
392               break;
393             }
394
395           // Try to pick up the SHT_SYMTAB_SHNDX section, if there is
396           // one.  This will work if it follows the SHT_SYMTAB
397           // section.
398           if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB_SHNDX)
399             {
400               xindex_shndx = i;
401               xindex_link = this->adjust_shndx(shdr.get_sh_link());
402             }
403         }
404     }
405 }
406
407 // Return the Xindex structure to use for object with lots of
408 // sections.
409
410 template<int size, bool big_endian>
411 Xindex*
412 Sized_relobj<size, big_endian>::do_initialize_xindex()
413 {
414   gold_assert(this->symtab_shndx_ != -1U);
415   Xindex* xindex = new Xindex(this->elf_file_.large_shndx_offset());
416   xindex->initialize_symtab_xindex<size, big_endian>(this, this->symtab_shndx_);
417   return xindex;
418 }
419
420 // Return whether SHDR has the right type and flags to be a GNU
421 // .eh_frame section.
422
423 template<int size, bool big_endian>
424 bool
425 Sized_relobj<size, big_endian>::check_eh_frame_flags(
426     const elfcpp::Shdr<size, big_endian>* shdr) const
427 {
428   return (shdr->get_sh_type() == elfcpp::SHT_PROGBITS
429           && (shdr->get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
430 }
431
432 // Return whether there is a GNU .eh_frame section, given the section
433 // headers and the section names.
434
435 template<int size, bool big_endian>
436 bool
437 Sized_relobj<size, big_endian>::find_eh_frame(
438     const unsigned char* pshdrs,
439     const char* names,
440     section_size_type names_size) const
441 {
442   const unsigned int shnum = this->shnum();
443   const unsigned char* p = pshdrs + This::shdr_size;
444   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
445     {
446       typename This::Shdr shdr(p);
447       if (this->check_eh_frame_flags(&shdr))
448         {
449           if (shdr.get_sh_name() >= names_size)
450             {
451               this->error(_("bad section name offset for section %u: %lu"),
452                           i, static_cast<unsigned long>(shdr.get_sh_name()));
453               continue;
454             }
455
456           const char* name = names + shdr.get_sh_name();
457           if (strcmp(name, ".eh_frame") == 0)
458             return true;
459         }
460     }
461   return false;
462 }
463
464 // Read the sections and symbols from an object file.
465
466 template<int size, bool big_endian>
467 void
468 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
469 {
470   this->read_section_data(&this->elf_file_, sd);
471
472   const unsigned char* const pshdrs = sd->section_headers->data();
473
474   this->find_symtab(pshdrs);
475
476   const unsigned char* namesu = sd->section_names->data();
477   const char* names = reinterpret_cast<const char*>(namesu);
478   if (memmem(names, sd->section_names_size, ".eh_frame", 10) != NULL)
479     {
480       if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
481         this->has_eh_frame_ = true;
482     }
483
484   sd->symbols = NULL;
485   sd->symbols_size = 0;
486   sd->external_symbols_offset = 0;
487   sd->symbol_names = NULL;
488   sd->symbol_names_size = 0;
489
490   if (this->symtab_shndx_ == 0)
491     {
492       // No symbol table.  Weird but legal.
493       return;
494     }
495
496   // Get the symbol table section header.
497   typename This::Shdr symtabshdr(pshdrs
498                                  + this->symtab_shndx_ * This::shdr_size);
499   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
500
501   // If this object has a .eh_frame section, we need all the symbols.
502   // Otherwise we only need the external symbols.  While it would be
503   // simpler to just always read all the symbols, I've seen object
504   // files with well over 2000 local symbols, which for a 64-bit
505   // object file format is over 5 pages that we don't need to read
506   // now.
507
508   const int sym_size = This::sym_size;
509   const unsigned int loccount = symtabshdr.get_sh_info();
510   this->local_symbol_count_ = loccount;
511   this->local_values_.resize(loccount);
512   section_offset_type locsize = loccount * sym_size;
513   off_t dataoff = symtabshdr.get_sh_offset();
514   section_size_type datasize =
515     convert_to_section_size_type(symtabshdr.get_sh_size());
516   off_t extoff = dataoff + locsize;
517   section_size_type extsize = datasize - locsize;
518
519   off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
520   section_size_type readsize = this->has_eh_frame_ ? datasize : extsize;
521
522   if (readsize == 0)
523     {
524       // No external symbols.  Also weird but also legal.
525       return;
526     }
527
528   File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
529
530   // Read the section header for the symbol names.
531   unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
532   if (strtab_shndx >= this->shnum())
533     {
534       this->error(_("invalid symbol table name index: %u"), strtab_shndx);
535       return;
536     }
537   typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
538   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
539     {
540       this->error(_("symbol table name section has wrong type: %u"),
541                   static_cast<unsigned int>(strtabshdr.get_sh_type()));
542       return;
543     }
544
545   // Read the symbol names.
546   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
547                                                strtabshdr.get_sh_size(),
548                                                false, true);
549
550   sd->symbols = fvsymtab;
551   sd->symbols_size = readsize;
552   sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
553   sd->symbol_names = fvstrtab;
554   sd->symbol_names_size =
555     convert_to_section_size_type(strtabshdr.get_sh_size());
556 }
557
558 // Return the section index of symbol SYM.  Set *VALUE to its value in
559 // the object file.  Set *IS_ORDINARY if this is an ordinary section
560 // index.  not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
561 // Note that for a symbol which is not defined in this object file,
562 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
563 // the final value of the symbol in the link.
564
565 template<int size, bool big_endian>
566 unsigned int
567 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
568                                                          Address* value,
569                                                          bool* is_ordinary)
570 {
571   section_size_type symbols_size;
572   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
573                                                         &symbols_size,
574                                                         false);
575
576   const size_t count = symbols_size / This::sym_size;
577   gold_assert(sym < count);
578
579   elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
580   *value = elfsym.get_st_value();
581
582   return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
583 }
584
585 // Return whether to include a section group in the link.  LAYOUT is
586 // used to keep track of which section groups we have already seen.
587 // INDEX is the index of the section group and SHDR is the section
588 // header.  If we do not want to include this group, we set bits in
589 // OMIT for each section which should be discarded.
590
591 template<int size, bool big_endian>
592 bool
593 Sized_relobj<size, big_endian>::include_section_group(
594     Symbol_table* symtab,
595     Layout* layout,
596     unsigned int index,
597     const char* name,
598     const unsigned char* shdrs,
599     const char* section_names,
600     section_size_type section_names_size,
601     std::vector<bool>* omit)
602 {
603   // Read the section contents.
604   typename This::Shdr shdr(shdrs + index * This::shdr_size);
605   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
606                                              shdr.get_sh_size(), true, false);
607   const elfcpp::Elf_Word* pword =
608     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
609
610   // The first word contains flags.  We only care about COMDAT section
611   // groups.  Other section groups are always included in the link
612   // just like ordinary sections.
613   elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
614
615   // Look up the group signature, which is the name of a symbol.  This
616   // is a lot of effort to go to to read a string.  Why didn't they
617   // just have the group signature point into the string table, rather
618   // than indirect through a symbol?
619
620   // Get the appropriate symbol table header (this will normally be
621   // the single SHT_SYMTAB section, but in principle it need not be).
622   const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
623   typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
624
625   // Read the symbol table entry.
626   unsigned int symndx = shdr.get_sh_info();
627   if (symndx >= symshdr.get_sh_size() / This::sym_size)
628     {
629       this->error(_("section group %u info %u out of range"),
630                   index, symndx);
631       return false;
632     }
633   off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
634   const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
635                                              false);
636   elfcpp::Sym<size, big_endian> sym(psym);
637
638   // Read the symbol table names.
639   section_size_type symnamelen;
640   const unsigned char* psymnamesu;
641   psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
642                                       &symnamelen, true);
643   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
644
645   // Get the section group signature.
646   if (sym.get_st_name() >= symnamelen)
647     {
648       this->error(_("symbol %u name offset %u out of range"),
649                   symndx, sym.get_st_name());
650       return false;
651     }
652
653   std::string signature(psymnames + sym.get_st_name());
654
655   // It seems that some versions of gas will create a section group
656   // associated with a section symbol, and then fail to give a name to
657   // the section symbol.  In such a case, use the name of the section.
658   if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
659     {
660       bool is_ordinary;
661       unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
662                                                       sym.get_st_shndx(),
663                                                       &is_ordinary);
664       if (!is_ordinary || sym_shndx >= this->shnum())
665         {
666           this->error(_("symbol %u invalid section index %u"),
667                       symndx, sym_shndx);
668           return false;
669         }
670       typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
671       if (member_shdr.get_sh_name() < section_names_size)
672         signature = section_names + member_shdr.get_sh_name();
673     }
674
675   // Record this section group in the layout, and see whether we've already
676   // seen one with the same signature.
677   bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
678                         || layout->add_comdat(this, index, signature, true));
679
680   Sized_relobj<size, big_endian>* kept_object = NULL;
681   Comdat_group* kept_group = NULL;
682
683   if (!include_group)
684     {
685       // This group is being discarded.  Find the object and group
686       // that was kept in its place.
687       unsigned int kept_group_index = 0;
688       Relobj* kept_relobj = layout->find_kept_object(signature,
689                                                      &kept_group_index);
690       kept_object = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
691       if (kept_object != NULL)
692         kept_group = kept_object->find_comdat_group(kept_group_index);
693     }
694   else if (flags & elfcpp::GRP_COMDAT)
695     {
696       // This group is being kept.  Create the table to map section names
697       // to section indexes and add it to the table of groups.
698       kept_group = new Comdat_group();
699       this->add_comdat_group(index, kept_group);
700     }
701
702   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
703
704   std::vector<unsigned int> shndxes;
705   bool relocate_group = include_group && parameters->options().relocatable();
706   if (relocate_group)
707     shndxes.reserve(count - 1);
708
709   for (size_t i = 1; i < count; ++i)
710     {
711       elfcpp::Elf_Word secnum =
712         this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
713
714       if (relocate_group)
715         shndxes.push_back(secnum);
716
717       if (secnum >= this->shnum())
718         {
719           this->error(_("section %u in section group %u out of range"),
720                       secnum, index);
721           continue;
722         }
723
724       // Check for an earlier section number, since we're going to get
725       // it wrong--we may have already decided to include the section.
726       if (secnum < index)
727         this->error(_("invalid section group %u refers to earlier section %u"),
728                     index, secnum);
729
730       // Get the name of the member section.
731       typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
732       if (member_shdr.get_sh_name() >= section_names_size)
733         {
734           // This is an error, but it will be diagnosed eventually
735           // in do_layout, so we don't need to do anything here but
736           // ignore it.
737           continue;
738         }
739       std::string mname(section_names + member_shdr.get_sh_name());
740
741       if (!include_group)
742         {
743           (*omit)[secnum] = true;
744           if (kept_group != NULL)
745             {
746               // Find the corresponding kept section, and store that info
747               // in the discarded section table.
748               Comdat_group::const_iterator p = kept_group->find(mname);
749               if (p != kept_group->end())
750                 {
751                   Kept_comdat_section* kept =
752                     new Kept_comdat_section(kept_object, p->second);
753                   this->set_kept_comdat_section(secnum, kept);
754                 }
755             }
756         }
757       else if (flags & elfcpp::GRP_COMDAT)
758         {
759           // Add the section to the kept group table.
760           gold_assert(kept_group != NULL);
761           kept_group->insert(std::make_pair(mname, secnum));
762         }
763     }
764
765   if (relocate_group)
766     layout->layout_group(symtab, this, index, name, signature.c_str(),
767                          shdr, flags, &shndxes);
768
769   return include_group;
770 }
771
772 // Whether to include a linkonce section in the link.  NAME is the
773 // name of the section and SHDR is the section header.
774
775 // Linkonce sections are a GNU extension implemented in the original
776 // GNU linker before section groups were defined.  The semantics are
777 // that we only include one linkonce section with a given name.  The
778 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
779 // where T is the type of section and SYMNAME is the name of a symbol.
780 // In an attempt to make linkonce sections interact well with section
781 // groups, we try to identify SYMNAME and use it like a section group
782 // signature.  We want to block section groups with that signature,
783 // but not other linkonce sections with that signature.  We also use
784 // the full name of the linkonce section as a normal section group
785 // signature.
786
787 template<int size, bool big_endian>
788 bool
789 Sized_relobj<size, big_endian>::include_linkonce_section(
790     Layout* layout,
791     unsigned int index,
792     const char* name,
793     const elfcpp::Shdr<size, big_endian>&)
794 {
795   // In general the symbol name we want will be the string following
796   // the last '.'.  However, we have to handle the case of
797   // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
798   // some versions of gcc.  So we use a heuristic: if the name starts
799   // with ".gnu.linkonce.t.", we use everything after that.  Otherwise
800   // we look for the last '.'.  We can't always simply skip
801   // ".gnu.linkonce.X", because we have to deal with cases like
802   // ".gnu.linkonce.d.rel.ro.local".
803   const char* const linkonce_t = ".gnu.linkonce.t.";
804   const char* symname;
805   if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
806     symname = name + strlen(linkonce_t);
807   else
808     symname = strrchr(name, '.') + 1;
809   std::string sig1(symname);
810   std::string sig2(name);
811   bool include1 = layout->add_comdat(this, index, sig1, false);
812   bool include2 = layout->add_comdat(this, index, sig2, true);
813
814   if (!include2)
815     {
816       // The section is being discarded on the basis of its section
817       // name (i.e., the kept section was also a linkonce section).
818       // In this case, the section index stored with the layout object
819       // is the linkonce section that was kept.
820       unsigned int kept_group_index = 0;
821       Relobj* kept_relobj = layout->find_kept_object(sig2, &kept_group_index);
822       if (kept_relobj != NULL)
823         {
824           Sized_relobj<size, big_endian>* kept_object
825               = static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
826           Kept_comdat_section* kept =
827             new Kept_comdat_section(kept_object, kept_group_index);
828           this->set_kept_comdat_section(index, kept);
829         }
830     }
831   else if (!include1)
832     {
833       // The section is being discarded on the basis of its symbol
834       // name.  This means that the corresponding kept section was
835       // part of a comdat group, and it will be difficult to identify
836       // the specific section within that group that corresponds to
837       // this linkonce section.  We'll handle the simple case where
838       // the group has only one member section.  Otherwise, it's not
839       // worth the effort.
840       unsigned int kept_group_index = 0;
841       Relobj* kept_relobj = layout->find_kept_object(sig1, &kept_group_index);
842       if (kept_relobj != NULL)
843         {
844           Sized_relobj<size, big_endian>* kept_object =
845               static_cast<Sized_relobj<size, big_endian>*>(kept_relobj);
846           Comdat_group* kept_group =
847             kept_object->find_comdat_group(kept_group_index);
848           if (kept_group != NULL && kept_group->size() == 1)
849             {
850               Comdat_group::const_iterator p = kept_group->begin();
851               gold_assert(p != kept_group->end());
852               Kept_comdat_section* kept =
853                 new Kept_comdat_section(kept_object, p->second);
854               this->set_kept_comdat_section(index, kept);
855             }
856         }
857     }
858
859   return include1 && include2;
860 }
861
862 // Layout an input section.
863
864 template<int size, bool big_endian>
865 inline void
866 Sized_relobj<size, big_endian>::layout_section(Layout* layout,
867                                                unsigned int shndx,
868                                                const char* name,
869                                                typename This::Shdr& shdr,
870                                                unsigned int reloc_shndx,
871                                                unsigned int reloc_type)
872 {
873   off_t offset;
874   Output_section* os = layout->layout(this, shndx, name, shdr,
875                                           reloc_shndx, reloc_type, &offset);
876
877   this->output_sections()[shndx] = os;
878   if (offset == -1)
879     this->section_offsets_[shndx] = invalid_address;
880   else
881     this->section_offsets_[shndx] = convert_types<Address, off_t>(offset);
882
883   // If this section requires special handling, and if there are
884   // relocs that apply to it, then we must do the special handling
885   // before we apply the relocs.
886   if (offset == -1 && reloc_shndx != 0)
887     this->set_relocs_must_follow_section_writes();
888 }
889
890 // Lay out the input sections.  We walk through the sections and check
891 // whether they should be included in the link.  If they should, we
892 // pass them to the Layout object, which will return an output section
893 // and an offset.  
894 // During garbage collection (gc-sections), this function is called
895 // twice.  When it is called the first time, it is for setting up some
896 // sections as roots to a work-list and to do comdat processing.  Actual
897 // layout happens the second time around after all the relevant sections
898 // have been determined.  The first time, is_worklist_ready is false.  
899 // It is then set to true after the worklist is processed and the relevant 
900 // sections are determined.  Then, this function is called again to 
901 // layout the sections.
902
903 template<int size, bool big_endian>
904 void
905 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
906                                           Layout* layout,
907                                           Read_symbols_data* sd)
908 {
909   const unsigned int shnum = this->shnum();
910   bool is_gc_pass_one = (parameters->options().gc_sections() 
911                          && !symtab->gc()->is_worklist_ready());
912   bool is_gc_pass_two = (parameters->options().gc_sections() 
913                          && symtab->gc()->is_worklist_ready());
914   if (shnum == 0)
915     return;
916   Symbols_data* gc_sd = NULL;
917   if (is_gc_pass_one)
918     {
919       // During garbage collection save the symbols data to use it when 
920       // re-entering this function.   
921       gc_sd = new Symbols_data;
922       this->copy_symbols_data(gc_sd, sd, This::shdr_size * shnum);
923       this->set_symbols_data(gc_sd);
924     }
925   else if (is_gc_pass_two)
926     {
927       gc_sd = this->get_symbols_data();
928     }
929
930   const unsigned char* section_headers_data = NULL;
931   section_size_type section_names_size;
932   const unsigned char* symbols_data = NULL;
933   section_size_type symbols_size;
934   section_offset_type external_symbols_offset;
935   const unsigned char* symbol_names_data = NULL;
936   section_size_type symbol_names_size;
937  
938   if (parameters->options().gc_sections())
939     {
940       section_headers_data = gc_sd->section_headers_data;
941       section_names_size = gc_sd->section_names_size;
942       symbols_data = gc_sd->symbols_data;
943       symbols_size = gc_sd->symbols_size;
944       external_symbols_offset = gc_sd->external_symbols_offset;
945       symbol_names_data = gc_sd->symbol_names_data;
946       symbol_names_size = gc_sd->symbol_names_size;
947     }
948   else
949     {
950       section_headers_data = sd->section_headers->data();
951       section_names_size = sd->section_names_size;
952       if (sd->symbols != NULL)
953         symbols_data = sd->symbols->data();
954       symbols_size = sd->symbols_size;
955       external_symbols_offset = sd->external_symbols_offset;
956       if (sd->symbol_names != NULL)
957         symbol_names_data = sd->symbol_names->data();
958       symbol_names_size = sd->symbol_names_size;
959     }
960
961   // Get the section headers.
962   const unsigned char* shdrs = section_headers_data;
963   const unsigned char* pshdrs;
964
965   // Get the section names.
966   const unsigned char* pnamesu = parameters->options().gc_sections() ?
967                                  gc_sd->section_names_data :
968                                  sd->section_names->data();
969   const char* pnames = reinterpret_cast<const char*>(pnamesu);
970
971   // If any input files have been claimed by plugins, we need to defer
972   // actual layout until the replacement files have arrived.
973   const bool should_defer_layout =
974       (parameters->options().has_plugins()
975        && parameters->options().plugins()->should_defer_layout());
976   unsigned int num_sections_to_defer = 0;
977
978   // For each section, record the index of the reloc section if any.
979   // Use 0 to mean that there is no reloc section, -1U to mean that
980   // there is more than one.
981   std::vector<unsigned int> reloc_shndx(shnum, 0);
982   std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
983   // Skip the first, dummy, section.
984   pshdrs = shdrs + This::shdr_size;
985   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
986     {
987       typename This::Shdr shdr(pshdrs);
988
989       // Count the number of sections whose layout will be deferred.
990       if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
991         ++num_sections_to_defer;
992
993       unsigned int sh_type = shdr.get_sh_type();
994       if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
995         {
996           unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
997           if (target_shndx == 0 || target_shndx >= shnum)
998             {
999               this->error(_("relocation section %u has bad info %u"),
1000                           i, target_shndx);
1001               continue;
1002             }
1003
1004           if (reloc_shndx[target_shndx] != 0)
1005             reloc_shndx[target_shndx] = -1U;
1006           else
1007             {
1008               reloc_shndx[target_shndx] = i;
1009               reloc_type[target_shndx] = sh_type;
1010             }
1011         }
1012     }
1013
1014   Output_sections& out_sections(this->output_sections());
1015   std::vector<Address>& out_section_offsets(this->section_offsets_);
1016
1017   if (!is_gc_pass_two)
1018     {
1019       out_sections.resize(shnum);
1020       out_section_offsets.resize(shnum);
1021     }
1022
1023   // If we are only linking for symbols, then there is nothing else to
1024   // do here.
1025   if (this->input_file()->just_symbols())
1026     {
1027       if (!is_gc_pass_two)
1028         {
1029           delete sd->section_headers;
1030           sd->section_headers = NULL;
1031           delete sd->section_names;
1032           sd->section_names = NULL;
1033         }
1034       return;
1035     }
1036
1037   if (num_sections_to_defer > 0)
1038     {
1039       parameters->options().plugins()->add_deferred_layout_object(this);
1040       this->deferred_layout_.reserve(num_sections_to_defer);
1041     }
1042
1043   // Whether we've seen a .note.GNU-stack section.
1044   bool seen_gnu_stack = false;
1045   // The flags of a .note.GNU-stack section.
1046   uint64_t gnu_stack_flags = 0;
1047
1048   // Keep track of which sections to omit.
1049   std::vector<bool> omit(shnum, false);
1050
1051   // Keep track of reloc sections when emitting relocations.
1052   const bool relocatable = parameters->options().relocatable();
1053   const bool emit_relocs = (relocatable
1054                             || parameters->options().emit_relocs());
1055   std::vector<unsigned int> reloc_sections;
1056
1057   // Keep track of .eh_frame sections.
1058   std::vector<unsigned int> eh_frame_sections;
1059
1060   // Skip the first, dummy, section.
1061   pshdrs = shdrs + This::shdr_size;
1062   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
1063     {
1064       typename This::Shdr shdr(pshdrs);
1065
1066       if (shdr.get_sh_name() >= section_names_size)
1067         {
1068           this->error(_("bad section name offset for section %u: %lu"),
1069                       i, static_cast<unsigned long>(shdr.get_sh_name()));
1070           return;
1071         }
1072
1073       const char* name = pnames + shdr.get_sh_name();
1074
1075       if (!is_gc_pass_two)
1076         { 
1077           if (this->handle_gnu_warning_section(name, i, symtab))
1078             { 
1079               if (!relocatable)
1080                 omit[i] = true;
1081             }
1082
1083           // The .note.GNU-stack section is special.  It gives the
1084           // protection flags that this object file requires for the stack
1085           // in memory.
1086           if (strcmp(name, ".note.GNU-stack") == 0)
1087             {
1088               seen_gnu_stack = true;
1089               gnu_stack_flags |= shdr.get_sh_flags();
1090               omit[i] = true;
1091             }
1092
1093           bool discard = omit[i];
1094           if (!discard)
1095             {
1096               if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
1097                 {
1098                   if (!this->include_section_group(symtab, layout, i, name, 
1099                                                    shdrs, pnames, 
1100                                                    section_names_size,
1101                                                    &omit))
1102                     discard = true;
1103                 }
1104               else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
1105                        && Layout::is_linkonce(name))
1106                 {
1107                   if (!this->include_linkonce_section(layout, i, name, shdr))
1108                     discard = true;
1109                 }
1110             }
1111
1112           if (discard)
1113             {
1114               // Do not include this section in the link.
1115               out_sections[i] = NULL;
1116               out_section_offsets[i] = invalid_address;
1117               continue;
1118             }
1119         }
1120  
1121       if (is_gc_pass_one)
1122         {
1123           if (is_section_name_included(name)
1124               || shdr.get_sh_type() == elfcpp::SHT_INIT_ARRAY 
1125               || shdr.get_sh_type() == elfcpp::SHT_FINI_ARRAY)
1126             {
1127               symtab->gc()->worklist().push(Section_id(this, i)); 
1128             }
1129         }
1130
1131       // When doing a relocatable link we are going to copy input
1132       // reloc sections into the output.  We only want to copy the
1133       // ones associated with sections which are not being discarded.
1134       // However, we don't know that yet for all sections.  So save
1135       // reloc sections and process them later. Garbage collection is
1136       // not triggered when relocatable code is desired.
1137       if (emit_relocs
1138           && (shdr.get_sh_type() == elfcpp::SHT_REL
1139               || shdr.get_sh_type() == elfcpp::SHT_RELA))
1140         {
1141           reloc_sections.push_back(i);
1142           continue;
1143         }
1144
1145       if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
1146         continue;
1147
1148       // The .eh_frame section is special.  It holds exception frame
1149       // information that we need to read in order to generate the
1150       // exception frame header.  We process these after all the other
1151       // sections so that the exception frame reader can reliably
1152       // determine which sections are being discarded, and discard the
1153       // corresponding information.
1154       if (!relocatable
1155           && strcmp(name, ".eh_frame") == 0
1156           && this->check_eh_frame_flags(&shdr))
1157         {
1158           if (is_gc_pass_one)
1159             {
1160               out_sections[i] = reinterpret_cast<Output_section*>(1);
1161               out_section_offsets[i] = invalid_address;
1162             }
1163           else
1164             eh_frame_sections.push_back(i);
1165           continue;
1166         }
1167
1168       if (is_gc_pass_two)
1169         {
1170           // This is executed during the second pass of garbage 
1171           // collection. do_layout has been called before and some 
1172           // sections have been already discarded. Simply ignore 
1173           // such sections this time around.
1174           if (out_sections[i] == NULL)
1175             {
1176               gold_assert(out_section_offsets[i] == invalid_address);
1177               continue; 
1178             }
1179           if ((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
1180             if (symtab->gc()->referenced_list().find(Section_id(this,i)) 
1181                 == symtab->gc()->referenced_list().end())
1182               {
1183                 if (parameters->options().print_gc_sections())
1184                   gold_info(_("%s: removing unused section from '%s'" 
1185                               " in file '%s"),
1186                             program_name, this->section_name(i).c_str(), 
1187                             this->name().c_str());
1188                 out_sections[i] = NULL;
1189                 out_section_offsets[i] = invalid_address;
1190                 continue;
1191               }
1192         }
1193       // Defer layout here if input files are claimed by plugins.  When gc
1194       // is turned on this function is called twice.  For the second call
1195       // should_defer_layout should be false.
1196       if (should_defer_layout && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC))
1197         {
1198           gold_assert(!is_gc_pass_two);
1199           this->deferred_layout_.push_back(Deferred_layout(i, name, 
1200                                                            pshdrs,
1201                                                            reloc_shndx[i],
1202                                                            reloc_type[i]));
1203           // Put dummy values here; real values will be supplied by
1204           // do_layout_deferred_sections.
1205           out_sections[i] = reinterpret_cast<Output_section*>(2);
1206           out_section_offsets[i] = invalid_address;
1207           continue;
1208               }
1209       // During gc_pass_two if a section that was previously deferred is
1210       // found, do not layout the section as layout_deferred_sections will
1211       // do it later from gold.cc.
1212       if (is_gc_pass_two 
1213           && (out_sections[i] == reinterpret_cast<Output_section*>(2)))
1214         continue;
1215
1216       if (is_gc_pass_one)
1217         {
1218           // This is during garbage collection. The out_sections are 
1219           // assigned in the second call to this function. 
1220           out_sections[i] = reinterpret_cast<Output_section*>(1);
1221           out_section_offsets[i] = invalid_address;
1222         }
1223       else
1224         {
1225           // When garbage collection is switched on the actual layout
1226           // only happens in the second call.
1227           this->layout_section(layout, i, name, shdr, reloc_shndx[i],
1228                                reloc_type[i]);
1229         }
1230     }
1231
1232   if (!is_gc_pass_one)
1233     layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
1234
1235   // When doing a relocatable link handle the reloc sections at the
1236   // end.  Garbage collection is not turned on for relocatable code. 
1237   if (emit_relocs)
1238     this->size_relocatable_relocs();
1239   gold_assert(!parameters->options().gc_sections() || reloc_sections.empty());
1240   for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
1241        p != reloc_sections.end();
1242        ++p)
1243     {
1244       unsigned int i = *p;
1245       const unsigned char* pshdr;
1246       pshdr = section_headers_data + i * This::shdr_size;
1247       typename This::Shdr shdr(pshdr);
1248
1249       unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
1250       if (data_shndx >= shnum)
1251         {
1252           // We already warned about this above.
1253           continue;
1254         }
1255
1256       Output_section* data_section = out_sections[data_shndx];
1257       if (data_section == NULL)
1258         {
1259           out_sections[i] = NULL;
1260           out_section_offsets[i] = invalid_address;
1261           continue;
1262         }
1263
1264       Relocatable_relocs* rr = new Relocatable_relocs();
1265       this->set_relocatable_relocs(i, rr);
1266
1267       Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1268                                                 rr);
1269       out_sections[i] = os;
1270       out_section_offsets[i] = invalid_address;
1271     }
1272
1273   // Handle the .eh_frame sections at the end.
1274   gold_assert(!is_gc_pass_one || eh_frame_sections.empty());
1275   for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1276        p != eh_frame_sections.end();
1277        ++p)
1278     {
1279       gold_assert(this->has_eh_frame_);
1280       gold_assert(external_symbols_offset != 0);
1281
1282       unsigned int i = *p;
1283       const unsigned char *pshdr;
1284       pshdr = section_headers_data + i * This::shdr_size;
1285       typename This::Shdr shdr(pshdr);
1286
1287       off_t offset;
1288       Output_section* os = layout->layout_eh_frame(this,
1289                                                    symbols_data,
1290                                                    symbols_size,
1291                                                    symbol_names_data,
1292                                                    symbol_names_size,
1293                                                    i, shdr,
1294                                                    reloc_shndx[i],
1295                                                    reloc_type[i],
1296                                                    &offset);
1297       out_sections[i] = os;
1298       if (offset == -1)
1299         out_section_offsets[i] = invalid_address;
1300       else
1301         out_section_offsets[i] = convert_types<Address, off_t>(offset);
1302
1303       // If this section requires special handling, and if there are
1304       // relocs that apply to it, then we must do the special handling
1305       // before we apply the relocs.
1306       if (offset == -1 && reloc_shndx[i] != 0)
1307         this->set_relocs_must_follow_section_writes();
1308     }
1309
1310   if (is_gc_pass_two)
1311     {
1312       delete[] gc_sd->section_headers_data;
1313       delete[] gc_sd->section_names_data;
1314       delete[] gc_sd->symbols_data;
1315       delete[] gc_sd->symbol_names_data;
1316     }
1317   else
1318     {
1319       delete sd->section_headers;
1320       sd->section_headers = NULL;
1321       delete sd->section_names;
1322       sd->section_names = NULL;
1323     }
1324 }
1325
1326 // Layout sections whose layout was deferred while waiting for
1327 // input files from a plugin.
1328
1329 template<int size, bool big_endian>
1330 void
1331 Sized_relobj<size, big_endian>::do_layout_deferred_sections(Layout* layout)
1332 {
1333   typename std::vector<Deferred_layout>::iterator deferred;
1334
1335   for (deferred = this->deferred_layout_.begin();
1336        deferred != this->deferred_layout_.end();
1337        ++deferred)
1338     {
1339       typename This::Shdr shdr(deferred->shdr_data_);
1340       this->layout_section(layout, deferred->shndx_, deferred->name_.c_str(),
1341                            shdr, deferred->reloc_shndx_, deferred->reloc_type_);
1342     }
1343
1344   this->deferred_layout_.clear();
1345 }
1346
1347 // Add the symbols to the symbol table.
1348
1349 template<int size, bool big_endian>
1350 void
1351 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1352                                                Read_symbols_data* sd,
1353                                                Layout*)
1354 {
1355   if (sd->symbols == NULL)
1356     {
1357       gold_assert(sd->symbol_names == NULL);
1358       return;
1359     }
1360
1361   const int sym_size = This::sym_size;
1362   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1363                      / sym_size);
1364   if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1365     {
1366       this->error(_("size of symbols is not multiple of symbol size"));
1367       return;
1368     }
1369
1370   this->symbols_.resize(symcount);
1371
1372   const char* sym_names =
1373     reinterpret_cast<const char*>(sd->symbol_names->data());
1374   symtab->add_from_relobj(this,
1375                           sd->symbols->data() + sd->external_symbols_offset,
1376                           symcount, this->local_symbol_count_,
1377                           sym_names, sd->symbol_names_size,
1378                           &this->symbols_,
1379                           &this->defined_count_);
1380
1381   delete sd->symbols;
1382   sd->symbols = NULL;
1383   delete sd->symbol_names;
1384   sd->symbol_names = NULL;
1385 }
1386
1387 // First pass over the local symbols.  Here we add their names to
1388 // *POOL and *DYNPOOL, and we store the symbol value in
1389 // THIS->LOCAL_VALUES_.  This function is always called from a
1390 // singleton thread.  This is followed by a call to
1391 // finalize_local_symbols.
1392
1393 template<int size, bool big_endian>
1394 void
1395 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1396                                                        Stringpool* dynpool)
1397 {
1398   gold_assert(this->symtab_shndx_ != -1U);
1399   if (this->symtab_shndx_ == 0)
1400     {
1401       // This object has no symbols.  Weird but legal.
1402       return;
1403     }
1404
1405   // Read the symbol table section header.
1406   const unsigned int symtab_shndx = this->symtab_shndx_;
1407   typename This::Shdr symtabshdr(this,
1408                                  this->elf_file_.section_header(symtab_shndx));
1409   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1410
1411   // Read the local symbols.
1412   const int sym_size = This::sym_size;
1413   const unsigned int loccount = this->local_symbol_count_;
1414   gold_assert(loccount == symtabshdr.get_sh_info());
1415   off_t locsize = loccount * sym_size;
1416   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1417                                               locsize, true, true);
1418
1419   // Read the symbol names.
1420   const unsigned int strtab_shndx =
1421     this->adjust_shndx(symtabshdr.get_sh_link());
1422   section_size_type strtab_size;
1423   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1424                                                         &strtab_size,
1425                                                         true);
1426   const char* pnames = reinterpret_cast<const char*>(pnamesu);
1427
1428   // Loop over the local symbols.
1429
1430   const Output_sections& out_sections(this->output_sections());
1431   unsigned int shnum = this->shnum();
1432   unsigned int count = 0;
1433   unsigned int dyncount = 0;
1434   // Skip the first, dummy, symbol.
1435   psyms += sym_size;
1436   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1437     {
1438       elfcpp::Sym<size, big_endian> sym(psyms);
1439
1440       Symbol_value<size>& lv(this->local_values_[i]);
1441
1442       bool is_ordinary;
1443       unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1444                                                   &is_ordinary);
1445       lv.set_input_shndx(shndx, is_ordinary);
1446
1447       if (sym.get_st_type() == elfcpp::STT_SECTION)
1448         lv.set_is_section_symbol();
1449       else if (sym.get_st_type() == elfcpp::STT_TLS)
1450         lv.set_is_tls_symbol();
1451
1452       // Save the input symbol value for use in do_finalize_local_symbols().
1453       lv.set_input_value(sym.get_st_value());
1454
1455       // Decide whether this symbol should go into the output file.
1456
1457       if (shndx < shnum && out_sections[shndx] == NULL)
1458         {
1459           lv.set_no_output_symtab_entry();
1460           gold_assert(!lv.needs_output_dynsym_entry());
1461           continue;
1462         }
1463
1464       if (sym.get_st_type() == elfcpp::STT_SECTION)
1465         {
1466           lv.set_no_output_symtab_entry();
1467           gold_assert(!lv.needs_output_dynsym_entry());
1468           continue;
1469         }
1470
1471       if (sym.get_st_name() >= strtab_size)
1472         {
1473           this->error(_("local symbol %u section name out of range: %u >= %u"),
1474                       i, sym.get_st_name(),
1475                       static_cast<unsigned int>(strtab_size));
1476           lv.set_no_output_symtab_entry();
1477           continue;
1478         }
1479
1480       // Add the symbol to the symbol table string pool.
1481       const char* name = pnames + sym.get_st_name();
1482       pool->add(name, true, NULL);
1483       ++count;
1484
1485       // If needed, add the symbol to the dynamic symbol table string pool.
1486       if (lv.needs_output_dynsym_entry())
1487         {
1488           dynpool->add(name, true, NULL);
1489           ++dyncount;
1490         }
1491     }
1492
1493   this->output_local_symbol_count_ = count;
1494   this->output_local_dynsym_count_ = dyncount;
1495 }
1496
1497 // Finalize the local symbols.  Here we set the final value in
1498 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1499 // This function is always called from a singleton thread.  The actual
1500 // output of the local symbols will occur in a separate task.
1501
1502 template<int size, bool big_endian>
1503 unsigned int
1504 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1505                                                           off_t off)
1506 {
1507   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1508
1509   const unsigned int loccount = this->local_symbol_count_;
1510   this->local_symbol_offset_ = off;
1511
1512   const bool relocatable = parameters->options().relocatable();
1513   const Output_sections& out_sections(this->output_sections());
1514   const std::vector<Address>& out_offsets(this->section_offsets_);
1515   unsigned int shnum = this->shnum();
1516
1517   for (unsigned int i = 1; i < loccount; ++i)
1518     {
1519       Symbol_value<size>& lv(this->local_values_[i]);
1520
1521       bool is_ordinary;
1522       unsigned int shndx = lv.input_shndx(&is_ordinary);
1523
1524       // Set the output symbol value.
1525
1526       if (!is_ordinary)
1527         {
1528           if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1529             lv.set_output_value(lv.input_value());
1530           else
1531             {
1532               this->error(_("unknown section index %u for local symbol %u"),
1533                           shndx, i);
1534               lv.set_output_value(0);
1535             }
1536         }
1537       else
1538         {
1539           if (shndx >= shnum)
1540             {
1541               this->error(_("local symbol %u section index %u out of range"),
1542                           i, shndx);
1543               shndx = 0;
1544             }
1545
1546           Output_section* os = out_sections[shndx];
1547
1548           if (os == NULL)
1549             {
1550               // This local symbol belongs to a section we are discarding.
1551               // In some cases when applying relocations later, we will
1552               // attempt to match it to the corresponding kept section,
1553               // so we leave the input value unchanged here.
1554               continue;
1555             }
1556           else if (out_offsets[shndx] == invalid_address)
1557             {
1558               uint64_t start;
1559
1560               // This is a SHF_MERGE section or one which otherwise
1561               // requires special handling.
1562               if (!lv.is_section_symbol())
1563                 {
1564                   // This is not a section symbol.  We can determine
1565                   // the final value now.
1566                   lv.set_output_value(os->output_address(this, shndx,
1567                                                          lv.input_value()));
1568                 }
1569               else if (!os->find_starting_output_address(this, shndx, &start))
1570                 {
1571                   // This is a section symbol, but apparently not one
1572                   // in a merged section.  Just use the start of the
1573                   // output section.  This happens with relocatable
1574                   // links when the input object has section symbols
1575                   // for arbitrary non-merge sections.
1576                   lv.set_output_value(os->address());
1577                 }
1578               else
1579                 {
1580                   // We have to consider the addend to determine the
1581                   // value to use in a relocation.  START is the start
1582                   // of this input section.
1583                   Merged_symbol_value<size>* msv =
1584                     new Merged_symbol_value<size>(lv.input_value(), start);
1585                   lv.set_merged_symbol_value(msv);
1586                 }
1587             }
1588           else if (lv.is_tls_symbol())
1589             lv.set_output_value(os->tls_offset()
1590                                 + out_offsets[shndx]
1591                                 + lv.input_value());
1592           else
1593             lv.set_output_value((relocatable ? 0 : os->address())
1594                                 + out_offsets[shndx]
1595                                 + lv.input_value());
1596         }
1597
1598       if (lv.needs_output_symtab_entry())
1599         {
1600           lv.set_output_symtab_index(index);
1601           ++index;
1602         }
1603     }
1604   return index;
1605 }
1606
1607 // Set the output dynamic symbol table indexes for the local variables.
1608
1609 template<int size, bool big_endian>
1610 unsigned int
1611 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1612 {
1613   const unsigned int loccount = this->local_symbol_count_;
1614   for (unsigned int i = 1; i < loccount; ++i)
1615     {
1616       Symbol_value<size>& lv(this->local_values_[i]);
1617       if (lv.needs_output_dynsym_entry())
1618         {
1619           lv.set_output_dynsym_index(index);
1620           ++index;
1621         }
1622     }
1623   return index;
1624 }
1625
1626 // Set the offset where local dynamic symbol information will be stored.
1627 // Returns the count of local symbols contributed to the symbol table by
1628 // this object.
1629
1630 template<int size, bool big_endian>
1631 unsigned int
1632 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1633 {
1634   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1635   this->local_dynsym_offset_ = off;
1636   return this->output_local_dynsym_count_;
1637 }
1638
1639 // Write out the local symbols.
1640
1641 template<int size, bool big_endian>
1642 void
1643 Sized_relobj<size, big_endian>::write_local_symbols(
1644     Output_file* of,
1645     const Stringpool* sympool,
1646     const Stringpool* dynpool,
1647     Output_symtab_xindex* symtab_xindex,
1648     Output_symtab_xindex* dynsym_xindex)
1649 {
1650   const bool strip_all = parameters->options().strip_all();
1651   if (strip_all)
1652     {
1653       if (this->output_local_dynsym_count_ == 0)
1654         return;
1655       this->output_local_symbol_count_ = 0;
1656     }
1657
1658   gold_assert(this->symtab_shndx_ != -1U);
1659   if (this->symtab_shndx_ == 0)
1660     {
1661       // This object has no symbols.  Weird but legal.
1662       return;
1663     }
1664
1665   // Read the symbol table section header.
1666   const unsigned int symtab_shndx = this->symtab_shndx_;
1667   typename This::Shdr symtabshdr(this,
1668                                  this->elf_file_.section_header(symtab_shndx));
1669   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1670   const unsigned int loccount = this->local_symbol_count_;
1671   gold_assert(loccount == symtabshdr.get_sh_info());
1672
1673   // Read the local symbols.
1674   const int sym_size = This::sym_size;
1675   off_t locsize = loccount * sym_size;
1676   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1677                                               locsize, true, false);
1678
1679   // Read the symbol names.
1680   const unsigned int strtab_shndx =
1681     this->adjust_shndx(symtabshdr.get_sh_link());
1682   section_size_type strtab_size;
1683   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1684                                                         &strtab_size,
1685                                                         false);
1686   const char* pnames = reinterpret_cast<const char*>(pnamesu);
1687
1688   // Get views into the output file for the portions of the symbol table
1689   // and the dynamic symbol table that we will be writing.
1690   off_t output_size = this->output_local_symbol_count_ * sym_size;
1691   unsigned char* oview = NULL;
1692   if (output_size > 0)
1693     oview = of->get_output_view(this->local_symbol_offset_, output_size);
1694
1695   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1696   unsigned char* dyn_oview = NULL;
1697   if (dyn_output_size > 0)
1698     dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1699                                     dyn_output_size);
1700
1701   const Output_sections out_sections(this->output_sections());
1702
1703   gold_assert(this->local_values_.size() == loccount);
1704
1705   unsigned char* ov = oview;
1706   unsigned char* dyn_ov = dyn_oview;
1707   psyms += sym_size;
1708   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1709     {
1710       elfcpp::Sym<size, big_endian> isym(psyms);
1711
1712       Symbol_value<size>& lv(this->local_values_[i]);
1713
1714       bool is_ordinary;
1715       unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1716                                                      &is_ordinary);
1717       if (is_ordinary)
1718         {
1719           gold_assert(st_shndx < out_sections.size());
1720           if (out_sections[st_shndx] == NULL)
1721             continue;
1722           st_shndx = out_sections[st_shndx]->out_shndx();
1723           if (st_shndx >= elfcpp::SHN_LORESERVE)
1724             {
1725               if (lv.needs_output_symtab_entry() && !strip_all)
1726                 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1727               if (lv.needs_output_dynsym_entry())
1728                 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1729               st_shndx = elfcpp::SHN_XINDEX;
1730             }
1731         }
1732
1733       // Write the symbol to the output symbol table.
1734       if (!strip_all && lv.needs_output_symtab_entry())
1735         {
1736           elfcpp::Sym_write<size, big_endian> osym(ov);
1737
1738           gold_assert(isym.get_st_name() < strtab_size);
1739           const char* name = pnames + isym.get_st_name();
1740           osym.put_st_name(sympool->get_offset(name));
1741           osym.put_st_value(this->local_values_[i].value(this, 0));
1742           osym.put_st_size(isym.get_st_size());
1743           osym.put_st_info(isym.get_st_info());
1744           osym.put_st_other(isym.get_st_other());
1745           osym.put_st_shndx(st_shndx);
1746
1747           ov += sym_size;
1748         }
1749
1750       // Write the symbol to the output dynamic symbol table.
1751       if (lv.needs_output_dynsym_entry())
1752         {
1753           gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1754           elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1755
1756           gold_assert(isym.get_st_name() < strtab_size);
1757           const char* name = pnames + isym.get_st_name();
1758           osym.put_st_name(dynpool->get_offset(name));
1759           osym.put_st_value(this->local_values_[i].value(this, 0));
1760           osym.put_st_size(isym.get_st_size());
1761           osym.put_st_info(isym.get_st_info());
1762           osym.put_st_other(isym.get_st_other());
1763           osym.put_st_shndx(st_shndx);
1764
1765           dyn_ov += sym_size;
1766         }
1767     }
1768
1769
1770   if (output_size > 0)
1771     {
1772       gold_assert(ov - oview == output_size);
1773       of->write_output_view(this->local_symbol_offset_, output_size, oview);
1774     }
1775
1776   if (dyn_output_size > 0)
1777     {
1778       gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1779       of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1780                             dyn_oview);
1781     }
1782 }
1783
1784 // Set *INFO to symbolic information about the offset OFFSET in the
1785 // section SHNDX.  Return true if we found something, false if we
1786 // found nothing.
1787
1788 template<int size, bool big_endian>
1789 bool
1790 Sized_relobj<size, big_endian>::get_symbol_location_info(
1791     unsigned int shndx,
1792     off_t offset,
1793     Symbol_location_info* info)
1794 {
1795   if (this->symtab_shndx_ == 0)
1796     return false;
1797
1798   section_size_type symbols_size;
1799   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1800                                                         &symbols_size,
1801                                                         false);
1802
1803   unsigned int symbol_names_shndx =
1804     this->adjust_shndx(this->section_link(this->symtab_shndx_));
1805   section_size_type names_size;
1806   const unsigned char* symbol_names_u =
1807     this->section_contents(symbol_names_shndx, &names_size, false);
1808   const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1809
1810   const int sym_size = This::sym_size;
1811   const size_t count = symbols_size / sym_size;
1812
1813   const unsigned char* p = symbols;
1814   for (size_t i = 0; i < count; ++i, p += sym_size)
1815     {
1816       elfcpp::Sym<size, big_endian> sym(p);
1817
1818       if (sym.get_st_type() == elfcpp::STT_FILE)
1819         {
1820           if (sym.get_st_name() >= names_size)
1821             info->source_file = "(invalid)";
1822           else
1823             info->source_file = symbol_names + sym.get_st_name();
1824           continue;
1825         }
1826
1827       bool is_ordinary;
1828       unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1829                                                      &is_ordinary);
1830       if (is_ordinary
1831           && st_shndx == shndx
1832           && static_cast<off_t>(sym.get_st_value()) <= offset
1833           && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1834               > offset))
1835         {
1836           if (sym.get_st_name() > names_size)
1837             info->enclosing_symbol_name = "(invalid)";
1838           else
1839             {
1840               info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1841               if (parameters->options().do_demangle())
1842                 {
1843                   char* demangled_name = cplus_demangle(
1844                       info->enclosing_symbol_name.c_str(),
1845                       DMGL_ANSI | DMGL_PARAMS);
1846                   if (demangled_name != NULL)
1847                     {
1848                       info->enclosing_symbol_name.assign(demangled_name);
1849                       free(demangled_name);
1850                     }
1851                 }
1852             }
1853           return true;
1854         }
1855     }
1856
1857   return false;
1858 }
1859
1860 // Look for a kept section corresponding to the given discarded section,
1861 // and return its output address.  This is used only for relocations in
1862 // debugging sections.  If we can't find the kept section, return 0.
1863
1864 template<int size, bool big_endian>
1865 typename Sized_relobj<size, big_endian>::Address
1866 Sized_relobj<size, big_endian>::map_to_kept_section(
1867     unsigned int shndx,
1868     bool* found) const
1869 {
1870   Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1871   if (kept != NULL)
1872     {
1873       gold_assert(kept->object_ != NULL);
1874       *found = true;
1875       Output_section* os = kept->object_->output_section(kept->shndx_);
1876       Address offset = kept->object_->get_output_section_offset(kept->shndx_);
1877       gold_assert(os != NULL && offset != invalid_address);
1878       return os->address() + offset;
1879     }
1880   *found = false;
1881   return 0;
1882 }
1883
1884 // Get symbol counts.
1885
1886 template<int size, bool big_endian>
1887 void
1888 Sized_relobj<size, big_endian>::do_get_global_symbol_counts(
1889     const Symbol_table*,
1890     size_t* defined,
1891     size_t* used) const
1892 {
1893   *defined = this->defined_count_;
1894   size_t count = 0;
1895   for (Symbols::const_iterator p = this->symbols_.begin();
1896        p != this->symbols_.end();
1897        ++p)
1898     if (*p != NULL
1899         && (*p)->source() == Symbol::FROM_OBJECT
1900         && (*p)->object() == this
1901         && (*p)->is_defined())
1902       ++count;
1903   *used = count;
1904 }
1905
1906 // Input_objects methods.
1907
1908 // Add a regular relocatable object to the list.  Return false if this
1909 // object should be ignored.
1910
1911 bool
1912 Input_objects::add_object(Object* obj)
1913 {
1914   // Set the global target from the first object file we recognize.
1915   Target* target = obj->target();
1916   if (!parameters->target_valid())
1917     set_parameters_target(target);
1918   else if (target != &parameters->target())
1919     {
1920       obj->error(_("incompatible target"));
1921       return false;
1922     }
1923
1924   // Print the filename if the -t/--trace option is selected.
1925   if (parameters->options().trace())
1926     gold_info("%s", obj->name().c_str());
1927
1928   if (!obj->is_dynamic())
1929     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1930   else
1931     {
1932       // See if this is a duplicate SONAME.
1933       Dynobj* dynobj = static_cast<Dynobj*>(obj);
1934       const char* soname = dynobj->soname();
1935
1936       std::pair<Unordered_set<std::string>::iterator, bool> ins =
1937         this->sonames_.insert(soname);
1938       if (!ins.second)
1939         {
1940           // We have already seen a dynamic object with this soname.
1941           return false;
1942         }
1943
1944       this->dynobj_list_.push_back(dynobj);
1945     }
1946
1947   // Add this object to the cross-referencer if requested.
1948   if (parameters->options().user_set_print_symbol_counts())
1949     {
1950       if (this->cref_ == NULL)
1951         this->cref_ = new Cref();
1952       this->cref_->add_object(obj);
1953     }
1954
1955   return true;
1956 }
1957
1958 // For each dynamic object, record whether we've seen all of its
1959 // explicit dependencies.
1960
1961 void
1962 Input_objects::check_dynamic_dependencies() const
1963 {
1964   for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1965        p != this->dynobj_list_.end();
1966        ++p)
1967     {
1968       const Dynobj::Needed& needed((*p)->needed());
1969       bool found_all = true;
1970       for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1971            pneeded != needed.end();
1972            ++pneeded)
1973         {
1974           if (this->sonames_.find(*pneeded) == this->sonames_.end())
1975             {
1976               found_all = false;
1977               break;
1978             }
1979         }
1980       (*p)->set_has_unknown_needed_entries(!found_all);
1981     }
1982 }
1983
1984 // Start processing an archive.
1985
1986 void
1987 Input_objects::archive_start(Archive* archive)
1988 {
1989   if (parameters->options().user_set_print_symbol_counts())
1990     {
1991       if (this->cref_ == NULL)
1992         this->cref_ = new Cref();
1993       this->cref_->add_archive_start(archive);
1994     }
1995 }
1996
1997 // Stop processing an archive.
1998
1999 void
2000 Input_objects::archive_stop(Archive* archive)
2001 {
2002   if (parameters->options().user_set_print_symbol_counts())
2003     this->cref_->add_archive_stop(archive);
2004 }
2005
2006 // Print symbol counts
2007
2008 void
2009 Input_objects::print_symbol_counts(const Symbol_table* symtab) const
2010 {
2011   if (parameters->options().user_set_print_symbol_counts()
2012       && this->cref_ != NULL)
2013     this->cref_->print_symbol_counts(symtab);
2014 }
2015
2016 // Relocate_info methods.
2017
2018 // Return a string describing the location of a relocation.  This is
2019 // only used in error messages.
2020
2021 template<int size, bool big_endian>
2022 std::string
2023 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
2024 {
2025   // See if we can get line-number information from debugging sections.
2026   std::string filename;
2027   std::string file_and_lineno;   // Better than filename-only, if available.
2028
2029   Sized_dwarf_line_info<size, big_endian> line_info(this->object);
2030   // This will be "" if we failed to parse the debug info for any reason.
2031   file_and_lineno = line_info.addr2line(this->data_shndx, offset);
2032
2033   std::string ret(this->object->name());
2034   ret += ':';
2035   Symbol_location_info info;
2036   if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
2037     {
2038       ret += " in function ";
2039       ret += info.enclosing_symbol_name;
2040       ret += ":";
2041       filename = info.source_file;
2042     }
2043
2044   if (!file_and_lineno.empty())
2045     ret += file_and_lineno;
2046   else
2047     {
2048       if (!filename.empty())
2049         ret += filename;
2050       ret += "(";
2051       ret += this->object->section_name(this->data_shndx);
2052       char buf[100];
2053       // Offsets into sections have to be positive.
2054       snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
2055       ret += buf;
2056       ret += ")";
2057     }
2058   return ret;
2059 }
2060
2061 } // End namespace gold.
2062
2063 namespace
2064 {
2065
2066 using namespace gold;
2067
2068 // Read an ELF file with the header and return the appropriate
2069 // instance of Object.
2070
2071 template<int size, bool big_endian>
2072 Object*
2073 make_elf_sized_object(const std::string& name, Input_file* input_file,
2074                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
2075 {
2076   int et = ehdr.get_e_type();
2077   if (et == elfcpp::ET_REL)
2078     {
2079       Sized_relobj<size, big_endian>* obj =
2080         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
2081       obj->setup(ehdr);
2082       return obj;
2083     }
2084   else if (et == elfcpp::ET_DYN)
2085     {
2086       Sized_dynobj<size, big_endian>* obj =
2087         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
2088       obj->setup(ehdr);
2089       return obj;
2090     }
2091   else
2092     {
2093       gold_error(_("%s: unsupported ELF file type %d"),
2094                  name.c_str(), et);
2095       return NULL;
2096     }
2097 }
2098
2099 } // End anonymous namespace.
2100
2101 namespace gold
2102 {
2103
2104 // Read an ELF file and return the appropriate instance of Object.
2105
2106 Object*
2107 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
2108                 const unsigned char* p, section_offset_type bytes)
2109 {
2110   if (bytes < elfcpp::EI_NIDENT)
2111     {
2112       gold_error(_("%s: ELF file too short"), name.c_str());
2113       return NULL;
2114     }
2115
2116   int v = p[elfcpp::EI_VERSION];
2117   if (v != elfcpp::EV_CURRENT)
2118     {
2119       if (v == elfcpp::EV_NONE)
2120         gold_error(_("%s: invalid ELF version 0"), name.c_str());
2121       else
2122         gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
2123       return NULL;
2124     }
2125
2126   int c = p[elfcpp::EI_CLASS];
2127   if (c == elfcpp::ELFCLASSNONE)
2128     {
2129       gold_error(_("%s: invalid ELF class 0"), name.c_str());
2130       return NULL;
2131     }
2132   else if (c != elfcpp::ELFCLASS32
2133            && c != elfcpp::ELFCLASS64)
2134     {
2135       gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
2136       return NULL;
2137     }
2138
2139   int d = p[elfcpp::EI_DATA];
2140   if (d == elfcpp::ELFDATANONE)
2141     {
2142       gold_error(_("%s: invalid ELF data encoding"), name.c_str());
2143       return NULL;
2144     }
2145   else if (d != elfcpp::ELFDATA2LSB
2146            && d != elfcpp::ELFDATA2MSB)
2147     {
2148       gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
2149       return NULL;
2150     }
2151
2152   bool big_endian = d == elfcpp::ELFDATA2MSB;
2153
2154   if (c == elfcpp::ELFCLASS32)
2155     {
2156       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
2157         {
2158           gold_error(_("%s: ELF file too short"), name.c_str());
2159           return NULL;
2160         }
2161       if (big_endian)
2162         {
2163 #ifdef HAVE_TARGET_32_BIG
2164           elfcpp::Ehdr<32, true> ehdr(p);
2165           return make_elf_sized_object<32, true>(name, input_file,
2166                                                  offset, ehdr);
2167 #else
2168           gold_error(_("%s: not configured to support "
2169                        "32-bit big-endian object"),
2170                      name.c_str());
2171           return NULL;
2172 #endif
2173         }
2174       else
2175         {
2176 #ifdef HAVE_TARGET_32_LITTLE
2177           elfcpp::Ehdr<32, false> ehdr(p);
2178           return make_elf_sized_object<32, false>(name, input_file,
2179                                                   offset, ehdr);
2180 #else
2181           gold_error(_("%s: not configured to support "
2182                        "32-bit little-endian object"),
2183                      name.c_str());
2184           return NULL;
2185 #endif
2186         }
2187     }
2188   else
2189     {
2190       if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
2191         {
2192           gold_error(_("%s: ELF file too short"), name.c_str());
2193           return NULL;
2194         }
2195       if (big_endian)
2196         {
2197 #ifdef HAVE_TARGET_64_BIG
2198           elfcpp::Ehdr<64, true> ehdr(p);
2199           return make_elf_sized_object<64, true>(name, input_file,
2200                                                  offset, ehdr);
2201 #else
2202           gold_error(_("%s: not configured to support "
2203                        "64-bit big-endian object"),
2204                      name.c_str());
2205           return NULL;
2206 #endif
2207         }
2208       else
2209         {
2210 #ifdef HAVE_TARGET_64_LITTLE
2211           elfcpp::Ehdr<64, false> ehdr(p);
2212           return make_elf_sized_object<64, false>(name, input_file,
2213                                                   offset, ehdr);
2214 #else
2215           gold_error(_("%s: not configured to support "
2216                        "64-bit little-endian object"),
2217                      name.c_str());
2218           return NULL;
2219 #endif
2220         }
2221     }
2222 }
2223
2224 // Instantiate the templates we need.
2225
2226 #ifdef HAVE_TARGET_32_LITTLE
2227 template
2228 void
2229 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
2230                                      Read_symbols_data*);
2231 #endif
2232
2233 #ifdef HAVE_TARGET_32_BIG
2234 template
2235 void
2236 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
2237                                     Read_symbols_data*);
2238 #endif
2239
2240 #ifdef HAVE_TARGET_64_LITTLE
2241 template
2242 void
2243 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
2244                                      Read_symbols_data*);
2245 #endif
2246
2247 #ifdef HAVE_TARGET_64_BIG
2248 template
2249 void
2250 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
2251                                     Read_symbols_data*);
2252 #endif
2253
2254 #ifdef HAVE_TARGET_32_LITTLE
2255 template
2256 class Sized_relobj<32, false>;
2257 #endif
2258
2259 #ifdef HAVE_TARGET_32_BIG
2260 template
2261 class Sized_relobj<32, true>;
2262 #endif
2263
2264 #ifdef HAVE_TARGET_64_LITTLE
2265 template
2266 class Sized_relobj<64, false>;
2267 #endif
2268
2269 #ifdef HAVE_TARGET_64_BIG
2270 template
2271 class Sized_relobj<64, true>;
2272 #endif
2273
2274 #ifdef HAVE_TARGET_32_LITTLE
2275 template
2276 struct Relocate_info<32, false>;
2277 #endif
2278
2279 #ifdef HAVE_TARGET_32_BIG
2280 template
2281 struct Relocate_info<32, true>;
2282 #endif
2283
2284 #ifdef HAVE_TARGET_64_LITTLE
2285 template
2286 struct Relocate_info<64, false>;
2287 #endif
2288
2289 #ifdef HAVE_TARGET_64_BIG
2290 template
2291 struct Relocate_info<64, true>;
2292 #endif
2293
2294 } // End namespace gold.