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