2008-06-25 Cary Coutant <ccoutant@google.com>
[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   if (readsize == 0)
456     {
457       // No external symbols.  Also weird but also legal.
458       return;
459     }
460
461   File_view* fvsymtab = this->get_lasting_view(readoff, readsize, true, false);
462
463   // Read the section header for the symbol names.
464   unsigned int strtab_shndx = this->adjust_shndx(symtabshdr.get_sh_link());
465   if (strtab_shndx >= this->shnum())
466     {
467       this->error(_("invalid symbol table name index: %u"), strtab_shndx);
468       return;
469     }
470   typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
471   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
472     {
473       this->error(_("symbol table name section has wrong type: %u"),
474                   static_cast<unsigned int>(strtabshdr.get_sh_type()));
475       return;
476     }
477
478   // Read the symbol names.
479   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
480                                                strtabshdr.get_sh_size(),
481                                                false, true);
482
483   sd->symbols = fvsymtab;
484   sd->symbols_size = readsize;
485   sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
486   sd->symbol_names = fvstrtab;
487   sd->symbol_names_size =
488     convert_to_section_size_type(strtabshdr.get_sh_size());
489 }
490
491 // Return the section index of symbol SYM.  Set *VALUE to its value in
492 // the object file.  Set *IS_ORDINARY if this is an ordinary section
493 // index.  not a special cod between SHN_LORESERVE and SHN_HIRESERVE.
494 // Note that for a symbol which is not defined in this object file,
495 // this will set *VALUE to 0 and return SHN_UNDEF; it will not return
496 // the final value of the symbol in the link.
497
498 template<int size, bool big_endian>
499 unsigned int
500 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
501                                                          Address* value,
502                                                          bool* is_ordinary)
503 {
504   section_size_type symbols_size;
505   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
506                                                         &symbols_size,
507                                                         false);
508
509   const size_t count = symbols_size / This::sym_size;
510   gold_assert(sym < count);
511
512   elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
513   *value = elfsym.get_st_value();
514
515   return this->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
516 }
517
518 // Return whether to include a section group in the link.  LAYOUT is
519 // used to keep track of which section groups we have already seen.
520 // INDEX is the index of the section group and SHDR is the section
521 // header.  If we do not want to include this group, we set bits in
522 // OMIT for each section which should be discarded.
523
524 template<int size, bool big_endian>
525 bool
526 Sized_relobj<size, big_endian>::include_section_group(
527     Symbol_table* symtab,
528     Layout* layout,
529     unsigned int index,
530     const char* name,
531     const unsigned char* shdrs,
532     const char* section_names,
533     section_size_type section_names_size,
534     std::vector<bool>* omit)
535 {
536   // Read the section contents.
537   typename This::Shdr shdr(shdrs + index * This::shdr_size);
538   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
539                                              shdr.get_sh_size(), true, false);
540   const elfcpp::Elf_Word* pword =
541     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
542
543   // The first word contains flags.  We only care about COMDAT section
544   // groups.  Other section groups are always included in the link
545   // just like ordinary sections.
546   elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
547
548   // Look up the group signature, which is the name of a symbol.  This
549   // is a lot of effort to go to to read a string.  Why didn't they
550   // just have the group signature point into the string table, rather
551   // than indirect through a symbol?
552
553   // Get the appropriate symbol table header (this will normally be
554   // the single SHT_SYMTAB section, but in principle it need not be).
555   const unsigned int link = this->adjust_shndx(shdr.get_sh_link());
556   typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
557
558   // Read the symbol table entry.
559   unsigned int symndx = shdr.get_sh_info();
560   if (symndx >= symshdr.get_sh_size() / This::sym_size)
561     {
562       this->error(_("section group %u info %u out of range"),
563                   index, symndx);
564       return false;
565     }
566   off_t symoff = symshdr.get_sh_offset() + symndx * This::sym_size;
567   const unsigned char* psym = this->get_view(symoff, This::sym_size, true,
568                                              false);
569   elfcpp::Sym<size, big_endian> sym(psym);
570
571   // Read the symbol table names.
572   section_size_type symnamelen;
573   const unsigned char* psymnamesu;
574   psymnamesu = this->section_contents(this->adjust_shndx(symshdr.get_sh_link()),
575                                       &symnamelen, true);
576   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
577
578   // Get the section group signature.
579   if (sym.get_st_name() >= symnamelen)
580     {
581       this->error(_("symbol %u name offset %u out of range"),
582                   symndx, sym.get_st_name());
583       return false;
584     }
585
586   std::string signature(psymnames + sym.get_st_name());
587
588   // It seems that some versions of gas will create a section group
589   // associated with a section symbol, and then fail to give a name to
590   // the section symbol.  In such a case, use the name of the section.
591   if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
592     {
593       bool is_ordinary;
594       unsigned int sym_shndx = this->adjust_sym_shndx(symndx,
595                                                       sym.get_st_shndx(),
596                                                       &is_ordinary);
597       if (!is_ordinary || sym_shndx >= this->shnum())
598         {
599           this->error(_("symbol %u invalid section index %u"),
600                       symndx, sym_shndx);
601           return false;
602         }
603       typename This::Shdr member_shdr(shdrs + sym_shndx * This::shdr_size);
604       if (member_shdr.get_sh_name() < section_names_size)
605         signature = section_names + member_shdr.get_sh_name();
606     }
607
608   // Record this section group in the layout, and see whether we've already
609   // seen one with the same signature.
610   bool include_group = ((flags & elfcpp::GRP_COMDAT) == 0
611                         || layout->add_comdat(this, index, signature, true));
612
613   Relobj* kept_object = NULL;
614   Comdat_group* kept_group = NULL;
615
616   if (!include_group)
617     {
618       // This group is being discarded.  Find the object and group
619       // that was kept in its place.
620       unsigned int kept_group_index = 0;
621       kept_object = layout->find_kept_object(signature, &kept_group_index);
622       if (kept_object != NULL)
623         kept_group = kept_object->find_comdat_group(kept_group_index);
624     }
625   else if (flags & elfcpp::GRP_COMDAT)
626     {
627       // This group is being kept.  Create the table to map section names
628       // to section indexes and add it to the table of groups.
629       kept_group = new Comdat_group();
630       this->add_comdat_group(index, kept_group);
631     }
632
633   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
634
635   std::vector<unsigned int> shndxes;
636   bool relocate_group = include_group && parameters->options().relocatable();
637   if (relocate_group)
638     shndxes.reserve(count - 1);
639
640   for (size_t i = 1; i < count; ++i)
641     {
642       elfcpp::Elf_Word secnum =
643         this->adjust_shndx(elfcpp::Swap<32, big_endian>::readval(pword + i));
644
645       if (relocate_group)
646         shndxes.push_back(secnum);
647
648       if (secnum >= this->shnum())
649         {
650           this->error(_("section %u in section group %u out of range"),
651                       secnum, index);
652           continue;
653         }
654
655       // Check for an earlier section number, since we're going to get
656       // it wrong--we may have already decided to include the section.
657       if (secnum < index)
658         this->error(_("invalid section group %u refers to earlier section %u"),
659                     index, secnum);
660
661       // Get the name of the member section.
662       typename This::Shdr member_shdr(shdrs + secnum * This::shdr_size);
663       if (member_shdr.get_sh_name() >= section_names_size)
664         {
665           // This is an error, but it will be diagnosed eventually
666           // in do_layout, so we don't need to do anything here but
667           // ignore it.
668           continue;
669         }
670       std::string mname(section_names + member_shdr.get_sh_name());
671
672       if (!include_group)
673         {
674           (*omit)[secnum] = true;
675           if (kept_group != NULL)
676             {
677               // Find the corresponding kept section, and store that info
678               // in the discarded section table.
679               Comdat_group::const_iterator p = kept_group->find(mname);
680               if (p != kept_group->end())
681                 {
682                   Kept_comdat_section* kept =
683                     new Kept_comdat_section(kept_object, p->second);
684                   this->set_kept_comdat_section(secnum, kept);
685                 }
686             }
687         }
688       else if (flags & elfcpp::GRP_COMDAT)
689         {
690           // Add the section to the kept group table.
691           gold_assert(kept_group != NULL);
692           kept_group->insert(std::make_pair(mname, secnum));
693         }
694     }
695
696   if (relocate_group)
697     layout->layout_group(symtab, this, index, name, signature.c_str(),
698                          shdr, flags, &shndxes);
699
700   return include_group;
701 }
702
703 // Whether to include a linkonce section in the link.  NAME is the
704 // name of the section and SHDR is the section header.
705
706 // Linkonce sections are a GNU extension implemented in the original
707 // GNU linker before section groups were defined.  The semantics are
708 // that we only include one linkonce section with a given name.  The
709 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
710 // where T is the type of section and SYMNAME is the name of a symbol.
711 // In an attempt to make linkonce sections interact well with section
712 // groups, we try to identify SYMNAME and use it like a section group
713 // signature.  We want to block section groups with that signature,
714 // but not other linkonce sections with that signature.  We also use
715 // the full name of the linkonce section as a normal section group
716 // signature.
717
718 template<int size, bool big_endian>
719 bool
720 Sized_relobj<size, big_endian>::include_linkonce_section(
721     Layout* layout,
722     unsigned int index,
723     const char* name,
724     const elfcpp::Shdr<size, big_endian>&)
725 {
726   // In general the symbol name we want will be the string following
727   // the last '.'.  However, we have to handle the case of
728   // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
729   // some versions of gcc.  So we use a heuristic: if the name starts
730   // with ".gnu.linkonce.t.", we use everything after that.  Otherwise
731   // we look for the last '.'.  We can't always simply skip
732   // ".gnu.linkonce.X", because we have to deal with cases like
733   // ".gnu.linkonce.d.rel.ro.local".
734   const char* const linkonce_t = ".gnu.linkonce.t.";
735   const char* symname;
736   if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
737     symname = name + strlen(linkonce_t);
738   else
739     symname = strrchr(name, '.') + 1;
740   std::string sig1(symname);
741   std::string sig2(name);
742   bool include1 = layout->add_comdat(this, index, sig1, false);
743   bool include2 = layout->add_comdat(this, index, sig2, true);
744
745   if (!include2)
746     {
747       // The section is being discarded on the basis of its section
748       // name (i.e., the kept section was also a linkonce section).
749       // In this case, the section index stored with the layout object
750       // is the linkonce section that was kept.
751       unsigned int kept_group_index = 0;
752       Relobj* kept_object = layout->find_kept_object(sig2, &kept_group_index);
753       if (kept_object != NULL)
754         {
755           Kept_comdat_section* kept =
756             new Kept_comdat_section(kept_object, kept_group_index);
757           this->set_kept_comdat_section(index, kept);
758         }
759     }
760   else if (!include1)
761     {
762       // The section is being discarded on the basis of its symbol
763       // name.  This means that the corresponding kept section was
764       // part of a comdat group, and it will be difficult to identify
765       // the specific section within that group that corresponds to
766       // this linkonce section.  We'll handle the simple case where
767       // the group has only one member section.  Otherwise, it's not
768       // worth the effort.
769       unsigned int kept_group_index = 0;
770       Relobj* kept_object = layout->find_kept_object(sig1, &kept_group_index);
771       if (kept_object != NULL)
772         {
773           Comdat_group* kept_group =
774             kept_object->find_comdat_group(kept_group_index);
775           if (kept_group != NULL && kept_group->size() == 1)
776             {
777               Comdat_group::const_iterator p = kept_group->begin();
778               gold_assert(p != kept_group->end());
779               Kept_comdat_section* kept =
780                 new Kept_comdat_section(kept_object, p->second);
781               this->set_kept_comdat_section(index, kept);
782             }
783         }
784     }
785
786   return include1 && include2;
787 }
788
789 // Lay out the input sections.  We walk through the sections and check
790 // whether they should be included in the link.  If they should, we
791 // pass them to the Layout object, which will return an output section
792 // and an offset.
793
794 template<int size, bool big_endian>
795 void
796 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
797                                           Layout* layout,
798                                           Read_symbols_data* sd)
799 {
800   const unsigned int shnum = this->shnum();
801   if (shnum == 0)
802     return;
803
804   // Get the section headers.
805   const unsigned char* shdrs = sd->section_headers->data();
806   const unsigned char* pshdrs;
807
808   // Get the section names.
809   const unsigned char* pnamesu = sd->section_names->data();
810   const char* pnames = reinterpret_cast<const char*>(pnamesu);
811
812   // For each section, record the index of the reloc section if any.
813   // Use 0 to mean that there is no reloc section, -1U to mean that
814   // there is more than one.
815   std::vector<unsigned int> reloc_shndx(shnum, 0);
816   std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
817   // Skip the first, dummy, section.
818   pshdrs = shdrs + This::shdr_size;
819   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
820     {
821       typename This::Shdr shdr(pshdrs);
822
823       unsigned int sh_type = shdr.get_sh_type();
824       if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
825         {
826           unsigned int target_shndx = this->adjust_shndx(shdr.get_sh_info());
827           if (target_shndx == 0 || target_shndx >= shnum)
828             {
829               this->error(_("relocation section %u has bad info %u"),
830                           i, target_shndx);
831               continue;
832             }
833
834           if (reloc_shndx[target_shndx] != 0)
835             reloc_shndx[target_shndx] = -1U;
836           else
837             {
838               reloc_shndx[target_shndx] = i;
839               reloc_type[target_shndx] = sh_type;
840             }
841         }
842     }
843
844   std::vector<Map_to_output>& map_sections(this->map_to_output());
845   map_sections.resize(shnum);
846
847   // If we are only linking for symbols, then there is nothing else to
848   // do here.
849   if (this->input_file()->just_symbols())
850     {
851       delete sd->section_headers;
852       sd->section_headers = NULL;
853       delete sd->section_names;
854       sd->section_names = NULL;
855       return;
856     }
857
858   // Whether we've seen a .note.GNU-stack section.
859   bool seen_gnu_stack = false;
860   // The flags of a .note.GNU-stack section.
861   uint64_t gnu_stack_flags = 0;
862
863   // Keep track of which sections to omit.
864   std::vector<bool> omit(shnum, false);
865
866   // Keep track of reloc sections when emitting relocations.
867   const bool relocatable = parameters->options().relocatable();
868   const bool emit_relocs = (relocatable
869                             || parameters->options().emit_relocs());
870   std::vector<unsigned int> reloc_sections;
871
872   // Keep track of .eh_frame sections.
873   std::vector<unsigned int> eh_frame_sections;
874
875   // Skip the first, dummy, section.
876   pshdrs = shdrs + This::shdr_size;
877   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
878     {
879       typename This::Shdr shdr(pshdrs);
880
881       if (shdr.get_sh_name() >= sd->section_names_size)
882         {
883           this->error(_("bad section name offset for section %u: %lu"),
884                       i, static_cast<unsigned long>(shdr.get_sh_name()));
885           return;
886         }
887
888       const char* name = pnames + shdr.get_sh_name();
889
890       if (this->handle_gnu_warning_section(name, i, symtab))
891         {
892           if (!relocatable)
893             omit[i] = true;
894         }
895
896       // The .note.GNU-stack section is special.  It gives the
897       // protection flags that this object file requires for the stack
898       // in memory.
899       if (strcmp(name, ".note.GNU-stack") == 0)
900         {
901           seen_gnu_stack = true;
902           gnu_stack_flags |= shdr.get_sh_flags();
903           omit[i] = true;
904         }
905
906       bool discard = omit[i];
907       if (!discard)
908         {
909           if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
910             {
911               if (!this->include_section_group(symtab, layout, i, name, shdrs,
912                                                pnames, sd->section_names_size,
913                                                &omit))
914                 discard = true;
915             }
916           else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
917                    && Layout::is_linkonce(name))
918             {
919               if (!this->include_linkonce_section(layout, i, name, shdr))
920                 discard = true;
921             }
922         }
923
924       if (discard)
925         {
926           // Do not include this section in the link.
927           map_sections[i].output_section = NULL;
928           continue;
929         }
930
931       // When doing a relocatable link we are going to copy input
932       // reloc sections into the output.  We only want to copy the
933       // ones associated with sections which are not being discarded.
934       // However, we don't know that yet for all sections.  So save
935       // reloc sections and process them later.
936       if (emit_relocs
937           && (shdr.get_sh_type() == elfcpp::SHT_REL
938               || shdr.get_sh_type() == elfcpp::SHT_RELA))
939         {
940           reloc_sections.push_back(i);
941           continue;
942         }
943
944       if (relocatable && shdr.get_sh_type() == elfcpp::SHT_GROUP)
945         continue;
946
947       // The .eh_frame section is special.  It holds exception frame
948       // information that we need to read in order to generate the
949       // exception frame header.  We process these after all the other
950       // sections so that the exception frame reader can reliably
951       // determine which sections are being discarded, and discard the
952       // corresponding information.
953       if (!relocatable
954           && strcmp(name, ".eh_frame") == 0
955           && this->check_eh_frame_flags(&shdr))
956         {
957           eh_frame_sections.push_back(i);
958           continue;
959         }
960
961       off_t offset;
962       Output_section* os = layout->layout(this, i, name, shdr,
963                                           reloc_shndx[i], reloc_type[i],
964                                           &offset);
965
966       map_sections[i].output_section = os;
967       map_sections[i].offset = offset;
968
969       // If this section requires special handling, and if there are
970       // relocs that apply to it, then we must do the special handling
971       // before we apply the relocs.
972       if (offset == -1 && reloc_shndx[i] != 0)
973         this->set_relocs_must_follow_section_writes();
974     }
975
976   layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
977
978   // When doing a relocatable link handle the reloc sections at the
979   // end.
980   if (emit_relocs)
981     this->size_relocatable_relocs();
982   for (std::vector<unsigned int>::const_iterator p = reloc_sections.begin();
983        p != reloc_sections.end();
984        ++p)
985     {
986       unsigned int i = *p;
987       const unsigned char* pshdr;
988       pshdr = sd->section_headers->data() + i * This::shdr_size;
989       typename This::Shdr shdr(pshdr);
990
991       unsigned int data_shndx = this->adjust_shndx(shdr.get_sh_info());
992       if (data_shndx >= shnum)
993         {
994           // We already warned about this above.
995           continue;
996         }
997
998       Output_section* data_section = map_sections[data_shndx].output_section;
999       if (data_section == NULL)
1000         {
1001           map_sections[i].output_section = NULL;
1002           continue;
1003         }
1004
1005       Relocatable_relocs* rr = new Relocatable_relocs();
1006       this->set_relocatable_relocs(i, rr);
1007
1008       Output_section* os = layout->layout_reloc(this, i, shdr, data_section,
1009                                                 rr);
1010       map_sections[i].output_section = os;
1011       map_sections[i].offset = -1;
1012     }
1013
1014   // Handle the .eh_frame sections at the end.
1015   for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
1016        p != eh_frame_sections.end();
1017        ++p)
1018     {
1019       gold_assert(this->has_eh_frame_);
1020       gold_assert(sd->external_symbols_offset != 0);
1021
1022       unsigned int i = *p;
1023       const unsigned char *pshdr;
1024       pshdr = sd->section_headers->data() + i * This::shdr_size;
1025       typename This::Shdr shdr(pshdr);
1026
1027       off_t offset;
1028       Output_section* os = layout->layout_eh_frame(this,
1029                                                    sd->symbols->data(),
1030                                                    sd->symbols_size,
1031                                                    sd->symbol_names->data(),
1032                                                    sd->symbol_names_size,
1033                                                    i, shdr,
1034                                                    reloc_shndx[i],
1035                                                    reloc_type[i],
1036                                                    &offset);
1037       map_sections[i].output_section = os;
1038       map_sections[i].offset = offset;
1039
1040       // If this section requires special handling, and if there are
1041       // relocs that apply to it, then we must do the special handling
1042       // before we apply the relocs.
1043       if (offset == -1 && reloc_shndx[i] != 0)
1044         this->set_relocs_must_follow_section_writes();
1045     }
1046
1047   delete sd->section_headers;
1048   sd->section_headers = NULL;
1049   delete sd->section_names;
1050   sd->section_names = NULL;
1051 }
1052
1053 // Add the symbols to the symbol table.
1054
1055 template<int size, bool big_endian>
1056 void
1057 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1058                                                Read_symbols_data* sd)
1059 {
1060   if (sd->symbols == NULL)
1061     {
1062       gold_assert(sd->symbol_names == NULL);
1063       return;
1064     }
1065
1066   const int sym_size = This::sym_size;
1067   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
1068                      / sym_size);
1069   if (symcount * sym_size != sd->symbols_size - sd->external_symbols_offset)
1070     {
1071       this->error(_("size of symbols is not multiple of symbol size"));
1072       return;
1073     }
1074
1075   this->symbols_.resize(symcount);
1076
1077   const char* sym_names =
1078     reinterpret_cast<const char*>(sd->symbol_names->data());
1079   symtab->add_from_relobj(this,
1080                           sd->symbols->data() + sd->external_symbols_offset,
1081                           symcount, this->local_symbol_count_,
1082                           sym_names, sd->symbol_names_size,
1083                           &this->symbols_);
1084
1085   delete sd->symbols;
1086   sd->symbols = NULL;
1087   delete sd->symbol_names;
1088   sd->symbol_names = NULL;
1089 }
1090
1091 // First pass over the local symbols.  Here we add their names to
1092 // *POOL and *DYNPOOL, and we store the symbol value in
1093 // THIS->LOCAL_VALUES_.  This function is always called from a
1094 // singleton thread.  This is followed by a call to
1095 // finalize_local_symbols.
1096
1097 template<int size, bool big_endian>
1098 void
1099 Sized_relobj<size, big_endian>::do_count_local_symbols(Stringpool* pool,
1100                                                        Stringpool* dynpool)
1101 {
1102   gold_assert(this->symtab_shndx_ != -1U);
1103   if (this->symtab_shndx_ == 0)
1104     {
1105       // This object has no symbols.  Weird but legal.
1106       return;
1107     }
1108
1109   // Read the symbol table section header.
1110   const unsigned int symtab_shndx = this->symtab_shndx_;
1111   typename This::Shdr symtabshdr(this,
1112                                  this->elf_file_.section_header(symtab_shndx));
1113   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1114
1115   // Read the local symbols.
1116   const int sym_size = This::sym_size;
1117   const unsigned int loccount = this->local_symbol_count_;
1118   gold_assert(loccount == symtabshdr.get_sh_info());
1119   off_t locsize = loccount * sym_size;
1120   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1121                                               locsize, true, true);
1122
1123   // Read the symbol names.
1124   const unsigned int strtab_shndx =
1125     this->adjust_shndx(symtabshdr.get_sh_link());
1126   section_size_type strtab_size;
1127   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1128                                                         &strtab_size,
1129                                                         true);
1130   const char* pnames = reinterpret_cast<const char*>(pnamesu);
1131
1132   // Loop over the local symbols.
1133
1134   const std::vector<Map_to_output>& mo(this->map_to_output());
1135   unsigned int shnum = this->shnum();
1136   unsigned int count = 0;
1137   unsigned int dyncount = 0;
1138   // Skip the first, dummy, symbol.
1139   psyms += sym_size;
1140   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1141     {
1142       elfcpp::Sym<size, big_endian> sym(psyms);
1143
1144       Symbol_value<size>& lv(this->local_values_[i]);
1145
1146       bool is_ordinary;
1147       unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1148                                                   &is_ordinary);
1149       lv.set_input_shndx(shndx, is_ordinary);
1150
1151       if (sym.get_st_type() == elfcpp::STT_SECTION)
1152         lv.set_is_section_symbol();
1153       else if (sym.get_st_type() == elfcpp::STT_TLS)
1154         lv.set_is_tls_symbol();
1155
1156       // Save the input symbol value for use in do_finalize_local_symbols().
1157       lv.set_input_value(sym.get_st_value());
1158
1159       // Decide whether this symbol should go into the output file.
1160
1161       if (shndx < shnum && mo[shndx].output_section == NULL)
1162         {
1163           lv.set_no_output_symtab_entry();
1164           gold_assert(!lv.needs_output_dynsym_entry());
1165           continue;
1166         }
1167
1168       if (sym.get_st_type() == elfcpp::STT_SECTION)
1169         {
1170           lv.set_no_output_symtab_entry();
1171           gold_assert(!lv.needs_output_dynsym_entry());
1172           continue;
1173         }
1174
1175       if (sym.get_st_name() >= strtab_size)
1176         {
1177           this->error(_("local symbol %u section name out of range: %u >= %u"),
1178                       i, sym.get_st_name(),
1179                       static_cast<unsigned int>(strtab_size));
1180           lv.set_no_output_symtab_entry();
1181           continue;
1182         }
1183
1184       // Add the symbol to the symbol table string pool.
1185       const char* name = pnames + sym.get_st_name();
1186       pool->add(name, true, NULL);
1187       ++count;
1188
1189       // If needed, add the symbol to the dynamic symbol table string pool.
1190       if (lv.needs_output_dynsym_entry())
1191         {
1192           dynpool->add(name, true, NULL);
1193           ++dyncount;
1194         }
1195     }
1196
1197   this->output_local_symbol_count_ = count;
1198   this->output_local_dynsym_count_ = dyncount;
1199 }
1200
1201 // Finalize the local symbols.  Here we set the final value in
1202 // THIS->LOCAL_VALUES_ and set their output symbol table indexes.
1203 // This function is always called from a singleton thread.  The actual
1204 // output of the local symbols will occur in a separate task.
1205
1206 template<int size, bool big_endian>
1207 unsigned int
1208 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
1209                                                           off_t off)
1210 {
1211   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1212
1213   const unsigned int loccount = this->local_symbol_count_;
1214   this->local_symbol_offset_ = off;
1215
1216   const std::vector<Map_to_output>& mo(this->map_to_output());
1217   unsigned int shnum = this->shnum();
1218
1219   for (unsigned int i = 1; i < loccount; ++i)
1220     {
1221       Symbol_value<size>& lv(this->local_values_[i]);
1222
1223       bool is_ordinary;
1224       unsigned int shndx = lv.input_shndx(&is_ordinary);
1225
1226       // Set the output symbol value.
1227       
1228       if (!is_ordinary)
1229         {
1230           if (shndx == elfcpp::SHN_ABS || shndx == elfcpp::SHN_COMMON)
1231             lv.set_output_value(lv.input_value());
1232           else
1233             {
1234               this->error(_("unknown section index %u for local symbol %u"),
1235                           shndx, i);
1236               lv.set_output_value(0);
1237             }
1238         }
1239       else
1240         {
1241           if (shndx >= shnum)
1242             {
1243               this->error(_("local symbol %u section index %u out of range"),
1244                           i, shndx);
1245               shndx = 0;
1246             }
1247
1248           Output_section* os = mo[shndx].output_section;
1249
1250           if (os == NULL)
1251             {
1252               // This local symbol belongs to a section we are discarding.
1253               // In some cases when applying relocations later, we will
1254               // attempt to match it to the corresponding kept section,
1255               // so we leave the input value unchanged here.
1256               continue;
1257             }
1258           else if (mo[shndx].offset == -1)
1259             {
1260               // This is a SHF_MERGE section or one which otherwise
1261               // requires special handling.  We get the output address
1262               // of the start of the merged section.  If this is not a
1263               // section symbol, we can then determine the final
1264               // value.  If it is a section symbol, we can not, as in
1265               // that case we have to consider the addend to determine
1266               // the value to use in a relocation.
1267               if (!lv.is_section_symbol())
1268                 lv.set_output_value(os->output_address(this, shndx,
1269                                                        lv.input_value()));
1270               else
1271                 {
1272                   section_offset_type start =
1273                     os->starting_output_address(this, shndx);
1274                   Merged_symbol_value<size>* msv =
1275                     new Merged_symbol_value<size>(lv.input_value(), start);
1276                   lv.set_merged_symbol_value(msv);
1277                 }
1278             }
1279           else if (lv.is_tls_symbol())
1280             lv.set_output_value(os->tls_offset()
1281                                 + mo[shndx].offset
1282                                 + lv.input_value());
1283           else
1284             lv.set_output_value(os->address()
1285                                 + mo[shndx].offset
1286                                 + lv.input_value());
1287         }
1288
1289       if (lv.needs_output_symtab_entry())
1290         {
1291           lv.set_output_symtab_index(index);
1292           ++index;
1293         }
1294     }
1295   return index;
1296 }
1297
1298 // Set the output dynamic symbol table indexes for the local variables.
1299
1300 template<int size, bool big_endian>
1301 unsigned int
1302 Sized_relobj<size, big_endian>::do_set_local_dynsym_indexes(unsigned int index)
1303 {
1304   const unsigned int loccount = this->local_symbol_count_;
1305   for (unsigned int i = 1; i < loccount; ++i)
1306     {
1307       Symbol_value<size>& lv(this->local_values_[i]);
1308       if (lv.needs_output_dynsym_entry())
1309         {
1310           lv.set_output_dynsym_index(index);
1311           ++index;
1312         }
1313     }
1314   return index;
1315 }
1316
1317 // Set the offset where local dynamic symbol information will be stored.
1318 // Returns the count of local symbols contributed to the symbol table by
1319 // this object.
1320
1321 template<int size, bool big_endian>
1322 unsigned int
1323 Sized_relobj<size, big_endian>::do_set_local_dynsym_offset(off_t off)
1324 {
1325   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
1326   this->local_dynsym_offset_ = off;
1327   return this->output_local_dynsym_count_;
1328 }
1329
1330 // Write out the local symbols.
1331
1332 template<int size, bool big_endian>
1333 void
1334 Sized_relobj<size, big_endian>::write_local_symbols(
1335     Output_file* of,
1336     const Stringpool* sympool,
1337     const Stringpool* dynpool,
1338     Output_symtab_xindex* symtab_xindex,
1339     Output_symtab_xindex* dynsym_xindex)
1340 {
1341   if (parameters->options().strip_all()
1342       && this->output_local_dynsym_count_ == 0)
1343     return;
1344
1345   gold_assert(this->symtab_shndx_ != -1U);
1346   if (this->symtab_shndx_ == 0)
1347     {
1348       // This object has no symbols.  Weird but legal.
1349       return;
1350     }
1351
1352   // Read the symbol table section header.
1353   const unsigned int symtab_shndx = this->symtab_shndx_;
1354   typename This::Shdr symtabshdr(this,
1355                                  this->elf_file_.section_header(symtab_shndx));
1356   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
1357   const unsigned int loccount = this->local_symbol_count_;
1358   gold_assert(loccount == symtabshdr.get_sh_info());
1359
1360   // Read the local symbols.
1361   const int sym_size = This::sym_size;
1362   off_t locsize = loccount * sym_size;
1363   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
1364                                               locsize, true, false);
1365
1366   // Read the symbol names.
1367   const unsigned int strtab_shndx =
1368     this->adjust_shndx(symtabshdr.get_sh_link());
1369   section_size_type strtab_size;
1370   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
1371                                                         &strtab_size,
1372                                                         false);
1373   const char* pnames = reinterpret_cast<const char*>(pnamesu);
1374
1375   // Get views into the output file for the portions of the symbol table
1376   // and the dynamic symbol table that we will be writing.
1377   off_t output_size = this->output_local_symbol_count_ * sym_size;
1378   unsigned char* oview = NULL;
1379   if (output_size > 0)
1380     oview = of->get_output_view(this->local_symbol_offset_, output_size);
1381
1382   off_t dyn_output_size = this->output_local_dynsym_count_ * sym_size;
1383   unsigned char* dyn_oview = NULL;
1384   if (dyn_output_size > 0)
1385     dyn_oview = of->get_output_view(this->local_dynsym_offset_,
1386                                     dyn_output_size);
1387
1388   const std::vector<Map_to_output>& mo(this->map_to_output());
1389
1390   gold_assert(this->local_values_.size() == loccount);
1391
1392   unsigned char* ov = oview;
1393   unsigned char* dyn_ov = dyn_oview;
1394   psyms += sym_size;
1395   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
1396     {
1397       elfcpp::Sym<size, big_endian> isym(psyms);
1398
1399       Symbol_value<size>& lv(this->local_values_[i]);
1400
1401       bool is_ordinary;
1402       unsigned int st_shndx = this->adjust_sym_shndx(i, isym.get_st_shndx(),
1403                                                      &is_ordinary);
1404       if (is_ordinary)
1405         {
1406           gold_assert(st_shndx < mo.size());
1407           if (mo[st_shndx].output_section == NULL)
1408             continue;
1409           st_shndx = mo[st_shndx].output_section->out_shndx();
1410           if (st_shndx >= elfcpp::SHN_LORESERVE)
1411             {
1412               if (lv.needs_output_symtab_entry())
1413                 symtab_xindex->add(lv.output_symtab_index(), st_shndx);
1414               if (lv.needs_output_dynsym_entry())
1415                 dynsym_xindex->add(lv.output_dynsym_index(), st_shndx);
1416               st_shndx = elfcpp::SHN_XINDEX;
1417             }
1418         }
1419
1420       // Write the symbol to the output symbol table.
1421       if (!parameters->options().strip_all()
1422           && lv.needs_output_symtab_entry())
1423         {
1424           elfcpp::Sym_write<size, big_endian> osym(ov);
1425
1426           gold_assert(isym.get_st_name() < strtab_size);
1427           const char* name = pnames + isym.get_st_name();
1428           osym.put_st_name(sympool->get_offset(name));
1429           osym.put_st_value(this->local_values_[i].value(this, 0));
1430           osym.put_st_size(isym.get_st_size());
1431           osym.put_st_info(isym.get_st_info());
1432           osym.put_st_other(isym.get_st_other());
1433           osym.put_st_shndx(st_shndx);
1434
1435           ov += sym_size;
1436         }
1437
1438       // Write the symbol to the output dynamic symbol table.
1439       if (lv.needs_output_dynsym_entry())
1440         {
1441           gold_assert(dyn_ov < dyn_oview + dyn_output_size);
1442           elfcpp::Sym_write<size, big_endian> osym(dyn_ov);
1443
1444           gold_assert(isym.get_st_name() < strtab_size);
1445           const char* name = pnames + isym.get_st_name();
1446           osym.put_st_name(dynpool->get_offset(name));
1447           osym.put_st_value(this->local_values_[i].value(this, 0));
1448           osym.put_st_size(isym.get_st_size());
1449           osym.put_st_info(isym.get_st_info());
1450           osym.put_st_other(isym.get_st_other());
1451           osym.put_st_shndx(st_shndx);
1452
1453           dyn_ov += sym_size;
1454         }
1455     }
1456
1457
1458   if (output_size > 0)
1459     {
1460       gold_assert(ov - oview == output_size);
1461       of->write_output_view(this->local_symbol_offset_, output_size, oview);
1462     }
1463
1464   if (dyn_output_size > 0)
1465     {
1466       gold_assert(dyn_ov - dyn_oview == dyn_output_size);
1467       of->write_output_view(this->local_dynsym_offset_, dyn_output_size,
1468                             dyn_oview);
1469     }
1470 }
1471
1472 // Set *INFO to symbolic information about the offset OFFSET in the
1473 // section SHNDX.  Return true if we found something, false if we
1474 // found nothing.
1475
1476 template<int size, bool big_endian>
1477 bool
1478 Sized_relobj<size, big_endian>::get_symbol_location_info(
1479     unsigned int shndx,
1480     off_t offset,
1481     Symbol_location_info* info)
1482 {
1483   if (this->symtab_shndx_ == 0)
1484     return false;
1485
1486   section_size_type symbols_size;
1487   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1488                                                         &symbols_size,
1489                                                         false);
1490
1491   unsigned int symbol_names_shndx =
1492     this->adjust_shndx(this->section_link(this->symtab_shndx_));
1493   section_size_type names_size;
1494   const unsigned char* symbol_names_u =
1495     this->section_contents(symbol_names_shndx, &names_size, false);
1496   const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1497
1498   const int sym_size = This::sym_size;
1499   const size_t count = symbols_size / sym_size;
1500
1501   const unsigned char* p = symbols;
1502   for (size_t i = 0; i < count; ++i, p += sym_size)
1503     {
1504       elfcpp::Sym<size, big_endian> sym(p);
1505
1506       if (sym.get_st_type() == elfcpp::STT_FILE)
1507         {
1508           if (sym.get_st_name() >= names_size)
1509             info->source_file = "(invalid)";
1510           else
1511             info->source_file = symbol_names + sym.get_st_name();
1512           continue;
1513         }
1514
1515       bool is_ordinary;
1516       unsigned int st_shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
1517                                                      &is_ordinary);
1518       if (is_ordinary
1519           && st_shndx == shndx
1520           && static_cast<off_t>(sym.get_st_value()) <= offset
1521           && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1522               > offset))
1523         {
1524           if (sym.get_st_name() > names_size)
1525             info->enclosing_symbol_name = "(invalid)";
1526           else
1527             {
1528               info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1529               if (parameters->options().do_demangle())
1530                 {
1531                   char* demangled_name = cplus_demangle(
1532                       info->enclosing_symbol_name.c_str(),
1533                       DMGL_ANSI | DMGL_PARAMS);
1534                   if (demangled_name != NULL)
1535                     {
1536                       info->enclosing_symbol_name.assign(demangled_name);
1537                       free(demangled_name);
1538                     }
1539                 }
1540             }
1541           return true;
1542         }
1543     }
1544
1545   return false;
1546 }
1547
1548 // Look for a kept section corresponding to the given discarded section,
1549 // and return its output address.  This is used only for relocations in
1550 // debugging sections.  If we can't find the kept section, return 0.
1551
1552 template<int size, bool big_endian>
1553 typename Sized_relobj<size, big_endian>::Address
1554 Sized_relobj<size, big_endian>::map_to_kept_section(
1555     unsigned int shndx,
1556     bool* found) const
1557 {
1558   Kept_comdat_section *kept = this->get_kept_comdat_section(shndx);
1559   if (kept != NULL)
1560     {
1561       gold_assert(kept->object_ != NULL);
1562       *found = true;
1563       return (static_cast<Address>
1564               (kept->object_->output_section_address(kept->shndx_)));
1565     }
1566   *found = false;
1567   return 0;
1568 }
1569
1570 // Input_objects methods.
1571
1572 // Add a regular relocatable object to the list.  Return false if this
1573 // object should be ignored.
1574
1575 bool
1576 Input_objects::add_object(Object* obj)
1577 {
1578   // Set the global target from the first object file we recognize.
1579   Target* target = obj->target();
1580   if (!parameters->target_valid())
1581     set_parameters_target(target);
1582   else if (target != &parameters->target())
1583     {
1584       obj->error(_("incompatible target"));
1585       return false;
1586     }
1587
1588   // Print the filename if the -t/--trace option is selected.
1589   if (parameters->options().trace())
1590     gold_info("%s", obj->name().c_str());
1591
1592   if (!obj->is_dynamic())
1593     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1594   else
1595     {
1596       // See if this is a duplicate SONAME.
1597       Dynobj* dynobj = static_cast<Dynobj*>(obj);
1598       const char* soname = dynobj->soname();
1599
1600       std::pair<Unordered_set<std::string>::iterator, bool> ins =
1601         this->sonames_.insert(soname);
1602       if (!ins.second)
1603         {
1604           // We have already seen a dynamic object with this soname.
1605           return false;
1606         }
1607
1608       this->dynobj_list_.push_back(dynobj);
1609
1610       // If this is -lc, remember the directory in which we found it.
1611       // We use this when issuing warnings about undefined symbols: as
1612       // a heuristic, we don't warn about system libraries found in
1613       // the same directory as -lc.
1614       if (strncmp(soname, "libc.so", 7) == 0)
1615         {
1616           const char* object_name = dynobj->name().c_str();
1617           const char* base = lbasename(object_name);
1618           if (base != object_name)
1619             this->system_library_directory_.assign(object_name,
1620                                                    base - 1 - object_name);
1621         }
1622     }
1623
1624   return true;
1625 }
1626
1627 // Return whether an object was found in the system library directory.
1628
1629 bool
1630 Input_objects::found_in_system_library_directory(const Object* object) const
1631 {
1632   return (!this->system_library_directory_.empty()
1633           && object->name().compare(0,
1634                                     this->system_library_directory_.size(),
1635                                     this->system_library_directory_) == 0);
1636 }
1637
1638 // For each dynamic object, record whether we've seen all of its
1639 // explicit dependencies.
1640
1641 void
1642 Input_objects::check_dynamic_dependencies() const
1643 {
1644   for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1645        p != this->dynobj_list_.end();
1646        ++p)
1647     {
1648       const Dynobj::Needed& needed((*p)->needed());
1649       bool found_all = true;
1650       for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1651            pneeded != needed.end();
1652            ++pneeded)
1653         {
1654           if (this->sonames_.find(*pneeded) == this->sonames_.end())
1655             {
1656               found_all = false;
1657               break;
1658             }
1659         }
1660       (*p)->set_has_unknown_needed_entries(!found_all);
1661     }
1662 }
1663
1664 // Relocate_info methods.
1665
1666 // Return a string describing the location of a relocation.  This is
1667 // only used in error messages.
1668
1669 template<int size, bool big_endian>
1670 std::string
1671 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1672 {
1673   // See if we can get line-number information from debugging sections.
1674   std::string filename;
1675   std::string file_and_lineno;   // Better than filename-only, if available.
1676
1677   Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1678   // This will be "" if we failed to parse the debug info for any reason.
1679   file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1680
1681   std::string ret(this->object->name());
1682   ret += ':';
1683   Symbol_location_info info;
1684   if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1685     {
1686       ret += " in function ";
1687       ret += info.enclosing_symbol_name;
1688       ret += ":";
1689       filename = info.source_file;
1690     }
1691
1692   if (!file_and_lineno.empty())
1693     ret += file_and_lineno;
1694   else
1695     {
1696       if (!filename.empty())
1697         ret += filename;
1698       ret += "(";
1699       ret += this->object->section_name(this->data_shndx);
1700       char buf[100];
1701       // Offsets into sections have to be positive.
1702       snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1703       ret += buf;
1704       ret += ")";
1705     }
1706   return ret;
1707 }
1708
1709 } // End namespace gold.
1710
1711 namespace
1712 {
1713
1714 using namespace gold;
1715
1716 // Read an ELF file with the header and return the appropriate
1717 // instance of Object.
1718
1719 template<int size, bool big_endian>
1720 Object*
1721 make_elf_sized_object(const std::string& name, Input_file* input_file,
1722                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1723 {
1724   int et = ehdr.get_e_type();
1725   if (et == elfcpp::ET_REL)
1726     {
1727       Sized_relobj<size, big_endian>* obj =
1728         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1729       obj->setup(ehdr);
1730       return obj;
1731     }
1732   else if (et == elfcpp::ET_DYN)
1733     {
1734       Sized_dynobj<size, big_endian>* obj =
1735         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1736       obj->setup(ehdr);
1737       return obj;
1738     }
1739   else
1740     {
1741       gold_error(_("%s: unsupported ELF file type %d"),
1742                  name.c_str(), et);
1743       return NULL;
1744     }
1745 }
1746
1747 } // End anonymous namespace.
1748
1749 namespace gold
1750 {
1751
1752 // Read an ELF file and return the appropriate instance of Object.
1753
1754 Object*
1755 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1756                 const unsigned char* p, section_offset_type bytes)
1757 {
1758   if (bytes < elfcpp::EI_NIDENT)
1759     {
1760       gold_error(_("%s: ELF file too short"), name.c_str());
1761       return NULL;
1762     }
1763
1764   int v = p[elfcpp::EI_VERSION];
1765   if (v != elfcpp::EV_CURRENT)
1766     {
1767       if (v == elfcpp::EV_NONE)
1768         gold_error(_("%s: invalid ELF version 0"), name.c_str());
1769       else
1770         gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1771       return NULL;
1772     }
1773
1774   int c = p[elfcpp::EI_CLASS];
1775   if (c == elfcpp::ELFCLASSNONE)
1776     {
1777       gold_error(_("%s: invalid ELF class 0"), name.c_str());
1778       return NULL;
1779     }
1780   else if (c != elfcpp::ELFCLASS32
1781            && c != elfcpp::ELFCLASS64)
1782     {
1783       gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1784       return NULL;
1785     }
1786
1787   int d = p[elfcpp::EI_DATA];
1788   if (d == elfcpp::ELFDATANONE)
1789     {
1790       gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1791       return NULL;
1792     }
1793   else if (d != elfcpp::ELFDATA2LSB
1794            && d != elfcpp::ELFDATA2MSB)
1795     {
1796       gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1797       return NULL;
1798     }
1799
1800   bool big_endian = d == elfcpp::ELFDATA2MSB;
1801
1802   if (c == elfcpp::ELFCLASS32)
1803     {
1804       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1805         {
1806           gold_error(_("%s: ELF file too short"), name.c_str());
1807           return NULL;
1808         }
1809       if (big_endian)
1810         {
1811 #ifdef HAVE_TARGET_32_BIG
1812           elfcpp::Ehdr<32, true> ehdr(p);
1813           return make_elf_sized_object<32, true>(name, input_file,
1814                                                  offset, ehdr);
1815 #else
1816           gold_error(_("%s: not configured to support "
1817                        "32-bit big-endian object"),
1818                      name.c_str());
1819           return NULL;
1820 #endif
1821         }
1822       else
1823         {
1824 #ifdef HAVE_TARGET_32_LITTLE
1825           elfcpp::Ehdr<32, false> ehdr(p);
1826           return make_elf_sized_object<32, false>(name, input_file,
1827                                                   offset, ehdr);
1828 #else
1829           gold_error(_("%s: not configured to support "
1830                        "32-bit little-endian object"),
1831                      name.c_str());
1832           return NULL;
1833 #endif
1834         }
1835     }
1836   else
1837     {
1838       if (bytes < elfcpp::Elf_sizes<64>::ehdr_size)
1839         {
1840           gold_error(_("%s: ELF file too short"), name.c_str());
1841           return NULL;
1842         }
1843       if (big_endian)
1844         {
1845 #ifdef HAVE_TARGET_64_BIG
1846           elfcpp::Ehdr<64, true> ehdr(p);
1847           return make_elf_sized_object<64, true>(name, input_file,
1848                                                  offset, ehdr);
1849 #else
1850           gold_error(_("%s: not configured to support "
1851                        "64-bit big-endian object"),
1852                      name.c_str());
1853           return NULL;
1854 #endif
1855         }
1856       else
1857         {
1858 #ifdef HAVE_TARGET_64_LITTLE
1859           elfcpp::Ehdr<64, false> ehdr(p);
1860           return make_elf_sized_object<64, false>(name, input_file,
1861                                                   offset, ehdr);
1862 #else
1863           gold_error(_("%s: not configured to support "
1864                        "64-bit little-endian object"),
1865                      name.c_str());
1866           return NULL;
1867 #endif
1868         }
1869     }
1870 }
1871
1872 // Instantiate the templates we need.
1873
1874 #ifdef HAVE_TARGET_32_LITTLE
1875 template
1876 void
1877 Object::read_section_data<32, false>(elfcpp::Elf_file<32, false, Object>*,
1878                                      Read_symbols_data*);
1879 #endif
1880
1881 #ifdef HAVE_TARGET_32_BIG
1882 template
1883 void
1884 Object::read_section_data<32, true>(elfcpp::Elf_file<32, true, Object>*,
1885                                     Read_symbols_data*);
1886 #endif
1887
1888 #ifdef HAVE_TARGET_64_LITTLE
1889 template
1890 void
1891 Object::read_section_data<64, false>(elfcpp::Elf_file<64, false, Object>*,
1892                                      Read_symbols_data*);
1893 #endif
1894
1895 #ifdef HAVE_TARGET_64_BIG
1896 template
1897 void
1898 Object::read_section_data<64, true>(elfcpp::Elf_file<64, true, Object>*,
1899                                     Read_symbols_data*);
1900 #endif
1901
1902 #ifdef HAVE_TARGET_32_LITTLE
1903 template
1904 class Sized_relobj<32, false>;
1905 #endif
1906
1907 #ifdef HAVE_TARGET_32_BIG
1908 template
1909 class Sized_relobj<32, true>;
1910 #endif
1911
1912 #ifdef HAVE_TARGET_64_LITTLE
1913 template
1914 class Sized_relobj<64, false>;
1915 #endif
1916
1917 #ifdef HAVE_TARGET_64_BIG
1918 template
1919 class Sized_relobj<64, true>;
1920 #endif
1921
1922 #ifdef HAVE_TARGET_32_LITTLE
1923 template
1924 struct Relocate_info<32, false>;
1925 #endif
1926
1927 #ifdef HAVE_TARGET_32_BIG
1928 template
1929 struct Relocate_info<32, true>;
1930 #endif
1931
1932 #ifdef HAVE_TARGET_64_LITTLE
1933 template
1934 struct Relocate_info<64, false>;
1935 #endif
1936
1937 #ifdef HAVE_TARGET_64_BIG
1938 template
1939 struct Relocate_info<64, true>;
1940 #endif
1941
1942 } // End namespace gold.