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