Add heuristics for undefined symbol warnings.
[platform/upstream/binutils.git] / gold / object.cc
1 // object.cc -- support for an object file for linking in gold
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cerrno>
26 #include <cstring>
27 #include <cstdarg>
28 #include "libiberty.h"
29
30 #include "target-select.h"
31 #include "dwarf_reader.h"
32 #include "layout.h"
33 #include "output.h"
34 #include "symtab.h"
35 #include "reloc.h"
36 #include "object.h"
37 #include "dynobj.h"
38
39 namespace gold
40 {
41
42 // Class Object.
43
44 // Set the target based on fields in the ELF file header.
45
46 void
47 Object::set_target(int machine, int size, bool big_endian, int osabi,
48                    int abiversion)
49 {
50   Target* target = select_target(machine, size, big_endian, osabi, abiversion);
51   if (target == NULL)
52     gold_fatal(_("%s: unsupported ELF machine number %d"),
53                this->name().c_str(), machine);
54   this->target_ = target;
55 }
56
57 // Report an error for this object file.  This is used by the
58 // elfcpp::Elf_file interface, and also called by the Object code
59 // itself.
60
61 void
62 Object::error(const char* format, ...) const
63 {
64   va_list args;
65   va_start(args, format);
66   char* buf = NULL;
67   if (vasprintf(&buf, format, args) < 0)
68     gold_nomem();
69   va_end(args);
70   gold_error(_("%s: %s"), this->name().c_str(), buf);
71   free(buf);
72 }
73
74 // Return a view of the contents of a section.
75
76 const unsigned char*
77 Object::section_contents(unsigned int shndx, off_t* plen, bool cache)
78 {
79   Location loc(this->do_section_contents(shndx));
80   *plen = loc.data_size;
81   return this->get_view(loc.file_offset, loc.data_size, cache);
82 }
83
84 // Read the section data into SD.  This is code common to Sized_relobj
85 // and Sized_dynobj, so we put it into Object.
86
87 template<int size, bool big_endian>
88 void
89 Object::read_section_data(elfcpp::Elf_file<size, big_endian, Object>* elf_file,
90                           Read_symbols_data* sd)
91 {
92   const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
93
94   // Read the section headers.
95   const off_t shoff = elf_file->shoff();
96   const unsigned int shnum = this->shnum();
97   sd->section_headers = this->get_lasting_view(shoff, shnum * shdr_size, true);
98
99   // Read the section names.
100   const unsigned char* pshdrs = sd->section_headers->data();
101   const unsigned char* pshdrnames = pshdrs + elf_file->shstrndx() * shdr_size;
102   typename elfcpp::Shdr<size, big_endian> shdrnames(pshdrnames);
103
104   if (shdrnames.get_sh_type() != elfcpp::SHT_STRTAB)
105     this->error(_("section name section has wrong type: %u"),
106                 static_cast<unsigned int>(shdrnames.get_sh_type()));
107
108   sd->section_names_size = shdrnames.get_sh_size();
109   sd->section_names = this->get_lasting_view(shdrnames.get_sh_offset(),
110                                              sd->section_names_size, false);
111 }
112
113 // If NAME is the name of a special .gnu.warning section, arrange for
114 // the warning to be issued.  SHNDX is the section index.  Return
115 // whether it is a warning section.
116
117 bool
118 Object::handle_gnu_warning_section(const char* name, unsigned int shndx,
119                                    Symbol_table* symtab)
120 {
121   const char warn_prefix[] = ".gnu.warning.";
122   const int warn_prefix_len = sizeof warn_prefix - 1;
123   if (strncmp(name, warn_prefix, warn_prefix_len) == 0)
124     {
125       symtab->add_warning(name + warn_prefix_len, this, shndx);
126       return true;
127     }
128   return false;
129 }
130
131 // Class Sized_relobj.
132
133 template<int size, bool big_endian>
134 Sized_relobj<size, big_endian>::Sized_relobj(
135     const std::string& name,
136     Input_file* input_file,
137     off_t offset,
138     const elfcpp::Ehdr<size, big_endian>& ehdr)
139   : Relobj(name, input_file, offset),
140     elf_file_(this, ehdr),
141     symtab_shndx_(-1U),
142     local_symbol_count_(0),
143     output_local_symbol_count_(0),
144     symbols_(),
145     local_symbol_offset_(0),
146     local_values_(),
147     local_got_offsets_(),
148     has_eh_frame_(false)
149 {
150 }
151
152 template<int size, bool big_endian>
153 Sized_relobj<size, big_endian>::~Sized_relobj()
154 {
155 }
156
157 // Set up an object file based on the file header.  This sets up the
158 // target and reads the section information.
159
160 template<int size, bool big_endian>
161 void
162 Sized_relobj<size, big_endian>::setup(
163     const elfcpp::Ehdr<size, big_endian>& ehdr)
164 {
165   this->set_target(ehdr.get_e_machine(), size, big_endian,
166                    ehdr.get_e_ident()[elfcpp::EI_OSABI],
167                    ehdr.get_e_ident()[elfcpp::EI_ABIVERSION]);
168
169   const unsigned int shnum = this->elf_file_.shnum();
170   this->set_shnum(shnum);
171 }
172
173 // Find the SHT_SYMTAB section, given the section headers.  The ELF
174 // standard says that maybe in the future there can be more than one
175 // SHT_SYMTAB section.  Until somebody figures out how that could
176 // work, we assume there is only one.
177
178 template<int size, bool big_endian>
179 void
180 Sized_relobj<size, big_endian>::find_symtab(const unsigned char* pshdrs)
181 {
182   const unsigned int shnum = this->shnum();
183   this->symtab_shndx_ = 0;
184   if (shnum > 0)
185     {
186       // Look through the sections in reverse order, since gas tends
187       // to put the symbol table at the end.
188       const unsigned char* p = pshdrs + shnum * This::shdr_size;
189       unsigned int i = shnum;
190       while (i > 0)
191         {
192           --i;
193           p -= This::shdr_size;
194           typename This::Shdr shdr(p);
195           if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
196             {
197               this->symtab_shndx_ = i;
198               break;
199             }
200         }
201     }
202 }
203
204 // Return whether SHDR has the right type and flags to be a GNU
205 // .eh_frame section.
206
207 template<int size, bool big_endian>
208 bool
209 Sized_relobj<size, big_endian>::check_eh_frame_flags(
210     const elfcpp::Shdr<size, big_endian>* shdr) const
211 {
212   return (shdr->get_sh_size() > 0
213           && shdr->get_sh_type() == elfcpp::SHT_PROGBITS
214           && shdr->get_sh_flags() == elfcpp::SHF_ALLOC);
215 }
216
217 // Return whether there is a GNU .eh_frame section, given the section
218 // headers and the section names.
219
220 template<int size, bool big_endian>
221 bool
222 Sized_relobj<size, big_endian>::find_eh_frame(const unsigned char* pshdrs,
223                                               const char* names,
224                                               off_t names_size) const
225 {
226   const unsigned int shnum = this->shnum();
227   const unsigned char* p = pshdrs + This::shdr_size;
228   for (unsigned int i = 1; i < shnum; ++i, p += This::shdr_size)
229     {
230       typename This::Shdr shdr(p);
231       if (this->check_eh_frame_flags(&shdr))
232         {
233           if (shdr.get_sh_name() >= names_size)
234             {
235               this->error(_("bad section name offset for section %u: %lu"),
236                           i, static_cast<unsigned long>(shdr.get_sh_name()));
237               continue;
238             }
239
240           const char* name = names + shdr.get_sh_name();
241           if (strcmp(name, ".eh_frame") == 0)
242             return true;
243         }
244     }
245   return false;
246 }
247
248 // Read the sections and symbols from an object file.
249
250 template<int size, bool big_endian>
251 void
252 Sized_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
253 {
254   this->read_section_data(&this->elf_file_, sd);
255
256   const unsigned char* const pshdrs = sd->section_headers->data();
257
258   this->find_symtab(pshdrs);
259
260   const unsigned char* namesu = sd->section_names->data();
261   const char* names = reinterpret_cast<const char*>(namesu);
262   if (this->find_eh_frame(pshdrs, names, sd->section_names_size))
263     this->has_eh_frame_ = true;
264
265   sd->symbols = NULL;
266   sd->symbols_size = 0;
267   sd->external_symbols_offset = 0;
268   sd->symbol_names = NULL;
269   sd->symbol_names_size = 0;
270
271   if (this->symtab_shndx_ == 0)
272     {
273       // No symbol table.  Weird but legal.
274       return;
275     }
276
277   // Get the symbol table section header.
278   typename This::Shdr symtabshdr(pshdrs
279                                  + this->symtab_shndx_ * This::shdr_size);
280   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
281
282   // If this object has a .eh_frame section, we need all the symbols.
283   // Otherwise we only need the external symbols.  While it would be
284   // simpler to just always read all the symbols, I've seen object
285   // files with well over 2000 local symbols, which for a 64-bit
286   // object file format is over 5 pages that we don't need to read
287   // now.
288
289   const int sym_size = This::sym_size;
290   const unsigned int loccount = symtabshdr.get_sh_info();
291   this->local_symbol_count_ = loccount;
292   off_t locsize = loccount * sym_size;
293   off_t dataoff = symtabshdr.get_sh_offset();
294   off_t datasize = symtabshdr.get_sh_size();
295   off_t extoff = dataoff + locsize;
296   off_t extsize = datasize - locsize;
297
298   off_t readoff = this->has_eh_frame_ ? dataoff : extoff;
299   off_t readsize = this->has_eh_frame_ ? datasize : extsize;
300
301   File_view* fvsymtab = this->get_lasting_view(readoff, readsize, false);
302
303   // Read the section header for the symbol names.
304   unsigned int strtab_shndx = symtabshdr.get_sh_link();
305   if (strtab_shndx >= this->shnum())
306     {
307       this->error(_("invalid symbol table name index: %u"), strtab_shndx);
308       return;
309     }
310   typename This::Shdr strtabshdr(pshdrs + strtab_shndx * This::shdr_size);
311   if (strtabshdr.get_sh_type() != elfcpp::SHT_STRTAB)
312     {
313       this->error(_("symbol table name section has wrong type: %u"),
314                   static_cast<unsigned int>(strtabshdr.get_sh_type()));
315       return;
316     }
317
318   // Read the symbol names.
319   File_view* fvstrtab = this->get_lasting_view(strtabshdr.get_sh_offset(),
320                                                strtabshdr.get_sh_size(), true);
321
322   sd->symbols = fvsymtab;
323   sd->symbols_size = readsize;
324   sd->external_symbols_offset = this->has_eh_frame_ ? locsize : 0;
325   sd->symbol_names = fvstrtab;
326   sd->symbol_names_size = strtabshdr.get_sh_size();
327 }
328
329 // Return the section index of symbol SYM.  Set *VALUE to its value in
330 // the object file.  Note that for a symbol which is not defined in
331 // this object file, this will set *VALUE to 0 and return SHN_UNDEF;
332 // it will not return the final value of the symbol in the link.
333
334 template<int size, bool big_endian>
335 unsigned int
336 Sized_relobj<size, big_endian>::symbol_section_and_value(unsigned int sym,
337                                                          Address* value)
338 {
339   off_t symbols_size;
340   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
341                                                         &symbols_size,
342                                                         false);
343
344   const size_t count = symbols_size / This::sym_size;
345   gold_assert(sym < count);
346
347   elfcpp::Sym<size, big_endian> elfsym(symbols + sym * This::sym_size);
348   *value = elfsym.get_st_value();
349   // FIXME: Handle SHN_XINDEX.
350   return elfsym.get_st_shndx();
351 }
352
353 // Return whether to include a section group in the link.  LAYOUT is
354 // used to keep track of which section groups we have already seen.
355 // INDEX is the index of the section group and SHDR is the section
356 // header.  If we do not want to include this group, we set bits in
357 // OMIT for each section which should be discarded.
358
359 template<int size, bool big_endian>
360 bool
361 Sized_relobj<size, big_endian>::include_section_group(
362     Layout* layout,
363     unsigned int index,
364     const elfcpp::Shdr<size, big_endian>& shdr,
365     std::vector<bool>* omit)
366 {
367   // Read the section contents.
368   const unsigned char* pcon = this->get_view(shdr.get_sh_offset(),
369                                              shdr.get_sh_size(), false);
370   const elfcpp::Elf_Word* pword =
371     reinterpret_cast<const elfcpp::Elf_Word*>(pcon);
372
373   // The first word contains flags.  We only care about COMDAT section
374   // groups.  Other section groups are always included in the link
375   // just like ordinary sections.
376   elfcpp::Elf_Word flags = elfcpp::Swap<32, big_endian>::readval(pword);
377   if ((flags & elfcpp::GRP_COMDAT) == 0)
378     return true;
379
380   // Look up the group signature, which is the name of a symbol.  This
381   // is a lot of effort to go to to read a string.  Why didn't they
382   // just use the name of the SHT_GROUP section as the group
383   // signature?
384
385   // Get the appropriate symbol table header (this will normally be
386   // the single SHT_SYMTAB section, but in principle it need not be).
387   const unsigned int link = shdr.get_sh_link();
388   typename This::Shdr symshdr(this, this->elf_file_.section_header(link));
389
390   // Read the symbol table entry.
391   if (shdr.get_sh_info() >= symshdr.get_sh_size() / This::sym_size)
392     {
393       this->error(_("section group %u info %u out of range"),
394                   index, shdr.get_sh_info());
395       return false;
396     }
397   off_t symoff = symshdr.get_sh_offset() + shdr.get_sh_info() * This::sym_size;
398   const unsigned char* psym = this->get_view(symoff, This::sym_size, true);
399   elfcpp::Sym<size, big_endian> sym(psym);
400
401   // Read the symbol table names.
402   off_t symnamelen;
403   const unsigned char* psymnamesu;
404   psymnamesu = this->section_contents(symshdr.get_sh_link(), &symnamelen,
405                                       true);
406   const char* psymnames = reinterpret_cast<const char*>(psymnamesu);
407
408   // Get the section group signature.
409   if (sym.get_st_name() >= symnamelen)
410     {
411       this->error(_("symbol %u name offset %u out of range"),
412                   shdr.get_sh_info(), sym.get_st_name());
413       return false;
414     }
415
416   const char* signature = psymnames + sym.get_st_name();
417
418   // It seems that some versions of gas will create a section group
419   // associated with a section symbol, and then fail to give a name to
420   // the section symbol.  In such a case, use the name of the section.
421   // FIXME.
422   std::string secname;
423   if (signature[0] == '\0' && sym.get_st_type() == elfcpp::STT_SECTION)
424     {
425       secname = this->section_name(sym.get_st_shndx());
426       signature = secname.c_str();
427     }
428
429   // Record this section group, and see whether we've already seen one
430   // with the same signature.
431   if (layout->add_comdat(signature, true))
432     return true;
433
434   // This is a duplicate.  We want to discard the sections in this
435   // group.
436   size_t count = shdr.get_sh_size() / sizeof(elfcpp::Elf_Word);
437   for (size_t i = 1; i < count; ++i)
438     {
439       elfcpp::Elf_Word secnum =
440         elfcpp::Swap<32, big_endian>::readval(pword + i);
441       if (secnum >= this->shnum())
442         {
443           this->error(_("section %u in section group %u out of range"),
444                       secnum, index);
445           continue;
446         }
447       (*omit)[secnum] = true;
448     }
449
450   return false;
451 }
452
453 // Whether to include a linkonce section in the link.  NAME is the
454 // name of the section and SHDR is the section header.
455
456 // Linkonce sections are a GNU extension implemented in the original
457 // GNU linker before section groups were defined.  The semantics are
458 // that we only include one linkonce section with a given name.  The
459 // name of a linkonce section is normally .gnu.linkonce.T.SYMNAME,
460 // where T is the type of section and SYMNAME is the name of a symbol.
461 // In an attempt to make linkonce sections interact well with section
462 // groups, we try to identify SYMNAME and use it like a section group
463 // signature.  We want to block section groups with that signature,
464 // but not other linkonce sections with that signature.  We also use
465 // the full name of the linkonce section as a normal section group
466 // signature.
467
468 template<int size, bool big_endian>
469 bool
470 Sized_relobj<size, big_endian>::include_linkonce_section(
471     Layout* layout,
472     const char* name,
473     const elfcpp::Shdr<size, big_endian>&)
474 {
475   // In general the symbol name we want will be the string following
476   // the last '.'.  However, we have to handle the case of
477   // .gnu.linkonce.t.__i686.get_pc_thunk.bx, which was generated by
478   // some versions of gcc.  So we use a heuristic: if the name starts
479   // with ".gnu.linkonce.t.", we use everything after that.  Otherwise
480   // we look for the last '.'.  We can't always simply skip
481   // ".gnu.linkonce.X", because we have to deal with cases like
482   // ".gnu.linkonce.d.rel.ro.local".
483   const char* const linkonce_t = ".gnu.linkonce.t.";
484   const char* symname;
485   if (strncmp(name, linkonce_t, strlen(linkonce_t)) == 0)
486     symname = name + strlen(linkonce_t);
487   else
488     symname = strrchr(name, '.') + 1;
489   bool include1 = layout->add_comdat(symname, false);
490   bool include2 = layout->add_comdat(name, true);
491   return include1 && include2;
492 }
493
494 // Lay out the input sections.  We walk through the sections and check
495 // whether they should be included in the link.  If they should, we
496 // pass them to the Layout object, which will return an output section
497 // and an offset.
498
499 template<int size, bool big_endian>
500 void
501 Sized_relobj<size, big_endian>::do_layout(Symbol_table* symtab,
502                                           Layout* layout,
503                                           Read_symbols_data* sd)
504 {
505   const unsigned int shnum = this->shnum();
506   if (shnum == 0)
507     return;
508
509   // Get the section headers.
510   const unsigned char* pshdrs = sd->section_headers->data();
511
512   // Get the section names.
513   const unsigned char* pnamesu = sd->section_names->data();
514   const char* pnames = reinterpret_cast<const char*>(pnamesu);
515
516   // For each section, record the index of the reloc section if any.
517   // Use 0 to mean that there is no reloc section, -1U to mean that
518   // there is more than one.
519   std::vector<unsigned int> reloc_shndx(shnum, 0);
520   std::vector<unsigned int> reloc_type(shnum, elfcpp::SHT_NULL);
521   // Skip the first, dummy, section.
522   pshdrs += This::shdr_size;
523   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
524     {
525       typename This::Shdr shdr(pshdrs);
526
527       unsigned int sh_type = shdr.get_sh_type();
528       if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
529         {
530           unsigned int target_shndx = shdr.get_sh_info();
531           if (target_shndx == 0 || target_shndx >= shnum)
532             {
533               this->error(_("relocation section %u has bad info %u"),
534                           i, target_shndx);
535               continue;
536             }
537
538           if (reloc_shndx[target_shndx] != 0)
539             reloc_shndx[target_shndx] = -1U;
540           else
541             {
542               reloc_shndx[target_shndx] = i;
543               reloc_type[target_shndx] = sh_type;
544             }
545         }
546     }
547
548   std::vector<Map_to_output>& map_sections(this->map_to_output());
549   map_sections.resize(shnum);
550
551   // Whether we've seen a .note.GNU-stack section.
552   bool seen_gnu_stack = false;
553   // The flags of a .note.GNU-stack section.
554   uint64_t gnu_stack_flags = 0;
555
556   // Keep track of which sections to omit.
557   std::vector<bool> omit(shnum, false);
558
559   // Keep track of .eh_frame sections.
560   std::vector<unsigned int> eh_frame_sections;
561
562   // Skip the first, dummy, section.
563   pshdrs = sd->section_headers->data() + This::shdr_size;
564   for (unsigned int i = 1; i < shnum; ++i, pshdrs += This::shdr_size)
565     {
566       typename This::Shdr shdr(pshdrs);
567
568       if (shdr.get_sh_name() >= sd->section_names_size)
569         {
570           this->error(_("bad section name offset for section %u: %lu"),
571                       i, static_cast<unsigned long>(shdr.get_sh_name()));
572           return;
573         }
574
575       const char* name = pnames + shdr.get_sh_name();
576
577       if (this->handle_gnu_warning_section(name, i, symtab))
578         {
579           if (!parameters->output_is_object())
580             omit[i] = true;
581         }
582
583       // The .note.GNU-stack section is special.  It gives the
584       // protection flags that this object file requires for the stack
585       // in memory.
586       if (strcmp(name, ".note.GNU-stack") == 0)
587         {
588           seen_gnu_stack = true;
589           gnu_stack_flags |= shdr.get_sh_flags();
590           omit[i] = true;
591         }
592
593       bool discard = omit[i];
594       if (!discard)
595         {
596           if (shdr.get_sh_type() == elfcpp::SHT_GROUP)
597             {
598               if (!this->include_section_group(layout, i, shdr, &omit))
599                 discard = true;
600             }
601           else if ((shdr.get_sh_flags() & elfcpp::SHF_GROUP) == 0
602                    && Layout::is_linkonce(name))
603             {
604               if (!this->include_linkonce_section(layout, name, shdr))
605                 discard = true;
606             }
607         }
608
609       if (discard)
610         {
611           // Do not include this section in the link.
612           map_sections[i].output_section = NULL;
613           continue;
614         }
615
616       // The .eh_frame section is special.  It holds exception frame
617       // information that we need to read in order to generate the
618       // exception frame header.  We process these after all the other
619       // sections so that the exception frame reader can reliably
620       // determine which sections are being discarded, and discard the
621       // corresponding information.
622       if (!parameters->output_is_object()
623           && strcmp(name, ".eh_frame") == 0
624           && this->check_eh_frame_flags(&shdr))
625         {
626           eh_frame_sections.push_back(i);
627           continue;
628         }
629
630       off_t offset;
631       Output_section* os = layout->layout(this, i, name, shdr,
632                                           reloc_shndx[i], reloc_type[i],
633                                           &offset);
634
635       map_sections[i].output_section = os;
636       map_sections[i].offset = offset;
637
638       // If this section requires special handling, and if there are
639       // relocs that apply to it, then we must do the special handling
640       // before we apply the relocs.
641       if (offset == -1 && reloc_shndx[i] != 0)
642         this->set_relocs_must_follow_section_writes();
643     }
644
645   layout->layout_gnu_stack(seen_gnu_stack, gnu_stack_flags);
646
647   // Handle the .eh_frame sections at the end.
648   for (std::vector<unsigned int>::const_iterator p = eh_frame_sections.begin();
649        p != eh_frame_sections.end();
650        ++p)
651     {
652       gold_assert(this->has_eh_frame_);
653       gold_assert(sd->external_symbols_offset != 0);
654
655       unsigned int i = *p;
656       const unsigned char *pshdr;
657       pshdr = sd->section_headers->data() + i * This::shdr_size;
658       typename This::Shdr shdr(pshdr);
659
660       off_t offset;
661       Output_section* os = layout->layout_eh_frame(this,
662                                                    sd->symbols->data(),
663                                                    sd->symbols_size,
664                                                    sd->symbol_names->data(),
665                                                    sd->symbol_names_size,
666                                                    i, shdr,
667                                                    reloc_shndx[i],
668                                                    reloc_type[i],
669                                                    &offset);
670       map_sections[i].output_section = os;
671       map_sections[i].offset = offset;
672
673       // If this section requires special handling, and if there are
674       // relocs that apply to it, then we must do the special handling
675       // before we apply the relocs.
676       if (offset == -1 && reloc_shndx[i] != 0)
677         this->set_relocs_must_follow_section_writes();
678     }
679
680   delete sd->section_headers;
681   sd->section_headers = NULL;
682   delete sd->section_names;
683   sd->section_names = NULL;
684 }
685
686 // Add the symbols to the symbol table.
687
688 template<int size, bool big_endian>
689 void
690 Sized_relobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
691                                                Read_symbols_data* sd)
692 {
693   if (sd->symbols == NULL)
694     {
695       gold_assert(sd->symbol_names == NULL);
696       return;
697     }
698
699   const int sym_size = This::sym_size;
700   size_t symcount = ((sd->symbols_size - sd->external_symbols_offset)
701                      / sym_size);
702   if (static_cast<off_t>(symcount * sym_size)
703       != sd->symbols_size - sd->external_symbols_offset)
704     {
705       this->error(_("size of symbols is not multiple of symbol size"));
706       return;
707     }
708
709   this->symbols_.resize(symcount);
710
711   const char* sym_names =
712     reinterpret_cast<const char*>(sd->symbol_names->data());
713   symtab->add_from_relobj(this,
714                           sd->symbols->data() + sd->external_symbols_offset,
715                           symcount, sym_names, sd->symbol_names_size,
716                           &this->symbols_);
717
718   delete sd->symbols;
719   sd->symbols = NULL;
720   delete sd->symbol_names;
721   sd->symbol_names = NULL;
722 }
723
724 // Finalize the local symbols.  Here we record the file offset at
725 // which they should be output, we add their names to *POOL, and we
726 // add their values to THIS->LOCAL_VALUES_.  Return the symbol index.
727 // This function is always called from the main thread.  The actual
728 // output of the local symbols will occur in a separate task.
729
730 template<int size, bool big_endian>
731 unsigned int
732 Sized_relobj<size, big_endian>::do_finalize_local_symbols(unsigned int index,
733                                                           off_t off,
734                                                           Stringpool* pool)
735 {
736   gold_assert(this->symtab_shndx_ != -1U);
737   if (this->symtab_shndx_ == 0)
738     {
739       // This object has no symbols.  Weird but legal.
740       return index;
741     }
742
743   gold_assert(off == static_cast<off_t>(align_address(off, size >> 3)));
744
745   this->local_symbol_offset_ = off;
746
747   // Read the symbol table section header.
748   const unsigned int symtab_shndx = this->symtab_shndx_;
749   typename This::Shdr symtabshdr(this,
750                                  this->elf_file_.section_header(symtab_shndx));
751   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
752
753   // Read the local symbols.
754   const int sym_size = This::sym_size;
755   const unsigned int loccount = this->local_symbol_count_;
756   gold_assert(loccount == symtabshdr.get_sh_info());
757   off_t locsize = loccount * sym_size;
758   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
759                                               locsize, true);
760
761   this->local_values_.resize(loccount);
762
763   // Read the symbol names.
764   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
765   off_t strtab_size;
766   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
767                                                         &strtab_size,
768                                                         true);
769   const char* pnames = reinterpret_cast<const char*>(pnamesu);
770
771   // Loop over the local symbols.
772
773   const std::vector<Map_to_output>& mo(this->map_to_output());
774   unsigned int shnum = this->shnum();
775   unsigned int count = 0;
776   // Skip the first, dummy, symbol.
777   psyms += sym_size;
778   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
779     {
780       elfcpp::Sym<size, big_endian> sym(psyms);
781
782       Symbol_value<size>& lv(this->local_values_[i]);
783
784       unsigned int shndx = sym.get_st_shndx();
785       lv.set_input_shndx(shndx);
786
787       if (sym.get_st_type() == elfcpp::STT_SECTION)
788         lv.set_is_section_symbol();
789
790       if (shndx >= elfcpp::SHN_LORESERVE)
791         {
792           if (shndx == elfcpp::SHN_ABS)
793             lv.set_output_value(sym.get_st_value());
794           else
795             {
796               // FIXME: Handle SHN_XINDEX.
797               this->error(_("unknown section index %u for local symbol %u"),
798                           shndx, i);
799               lv.set_output_value(0);
800             }
801         }
802       else
803         {
804           if (shndx >= shnum)
805             {
806               this->error(_("local symbol %u section index %u out of range"),
807                           i, shndx);
808               shndx = 0;
809             }
810
811           Output_section* os = mo[shndx].output_section;
812
813           if (os == NULL)
814             {
815               lv.set_output_value(0);
816               lv.set_no_output_symtab_entry();
817               continue;
818             }
819
820           if (mo[shndx].offset == -1)
821             lv.set_input_value(sym.get_st_value());
822           else
823             lv.set_output_value(mo[shndx].output_section->address()
824                                 + mo[shndx].offset
825                                 + sym.get_st_value());
826         }
827
828       // Decide whether this symbol should go into the output file.
829
830       if (sym.get_st_type() == elfcpp::STT_SECTION)
831         {
832           lv.set_no_output_symtab_entry();
833           continue;
834         }
835
836       if (sym.get_st_name() >= strtab_size)
837         {
838           this->error(_("local symbol %u section name out of range: %u >= %u"),
839                       i, sym.get_st_name(),
840                       static_cast<unsigned int>(strtab_size));
841           lv.set_no_output_symtab_entry();
842           continue;
843         }
844
845       const char* name = pnames + sym.get_st_name();
846       pool->add(name, true, NULL);
847       lv.set_output_symtab_index(index);
848       ++index;
849       ++count;
850     }
851
852   this->output_local_symbol_count_ = count;
853
854   return index;
855 }
856
857 // Return the value of the local symbol symndx.
858 template<int size, bool big_endian>
859 typename elfcpp::Elf_types<size>::Elf_Addr
860 Sized_relobj<size, big_endian>::local_symbol_value(unsigned int symndx) const
861 {
862   gold_assert(symndx < this->local_symbol_count_);
863   gold_assert(symndx < this->local_values_.size());
864   const Symbol_value<size>& lv(this->local_values_[symndx]);
865   return lv.value(this, 0);
866 }
867
868 // Return the value of a local symbol defined in input section SHNDX,
869 // with value VALUE, adding addend ADDEND.  IS_SECTION_SYMBOL
870 // indicates whether the symbol is a section symbol.  This handles
871 // SHF_MERGE sections.
872 template<int size, bool big_endian>
873 typename elfcpp::Elf_types<size>::Elf_Addr
874 Sized_relobj<size, big_endian>::local_value(unsigned int shndx,
875                                             Address value,
876                                             bool is_section_symbol,
877                                             Address addend) const
878 {
879   const std::vector<Map_to_output>& mo(this->map_to_output());
880   Output_section* os = mo[shndx].output_section;
881   if (os == NULL)
882     return addend;
883   gold_assert(mo[shndx].offset == -1);
884
885   // Do the mapping required by the output section.  If this is not a
886   // section symbol, then we want to map the symbol value, and then
887   // include the addend.  If this is a section symbol, then we need to
888   // include the addend to figure out where in the section we are,
889   // before we do the mapping.  This will do the right thing provided
890   // the assembler is careful to only convert a relocation in a merged
891   // section to a section symbol if there is a zero addend.  If the
892   // assembler does not do this, then in general we can't know what to
893   // do, because we can't distinguish the addend for the instruction
894   // format from the addend for the section offset.
895
896   if (is_section_symbol)
897     return os->output_address(this, shndx, value + addend);
898   else
899     return addend + os->output_address(this, shndx, value);
900 }
901
902 // Write out the local symbols.
903
904 template<int size, bool big_endian>
905 void
906 Sized_relobj<size, big_endian>::write_local_symbols(Output_file* of,
907                                                     const Stringpool* sympool)
908 {
909   if (parameters->strip_all())
910     return;
911
912   gold_assert(this->symtab_shndx_ != -1U);
913   if (this->symtab_shndx_ == 0)
914     {
915       // This object has no symbols.  Weird but legal.
916       return;
917     }
918
919   // Read the symbol table section header.
920   const unsigned int symtab_shndx = this->symtab_shndx_;
921   typename This::Shdr symtabshdr(this,
922                                  this->elf_file_.section_header(symtab_shndx));
923   gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
924   const unsigned int loccount = this->local_symbol_count_;
925   gold_assert(loccount == symtabshdr.get_sh_info());
926
927   // Read the local symbols.
928   const int sym_size = This::sym_size;
929   off_t locsize = loccount * sym_size;
930   const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
931                                               locsize, false);
932
933   // Read the symbol names.
934   const unsigned int strtab_shndx = symtabshdr.get_sh_link();
935   off_t strtab_size;
936   const unsigned char* pnamesu = this->section_contents(strtab_shndx,
937                                                         &strtab_size,
938                                                         true);
939   const char* pnames = reinterpret_cast<const char*>(pnamesu);
940
941   // Get a view into the output file.
942   off_t output_size = this->output_local_symbol_count_ * sym_size;
943   unsigned char* oview = of->get_output_view(this->local_symbol_offset_,
944                                              output_size);
945
946   const std::vector<Map_to_output>& mo(this->map_to_output());
947
948   gold_assert(this->local_values_.size() == loccount);
949
950   unsigned char* ov = oview;
951   psyms += sym_size;
952   for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
953     {
954       elfcpp::Sym<size, big_endian> isym(psyms);
955
956       if (!this->local_values_[i].needs_output_symtab_entry())
957         continue;
958
959       unsigned int st_shndx = isym.get_st_shndx();
960       if (st_shndx < elfcpp::SHN_LORESERVE)
961         {
962           gold_assert(st_shndx < mo.size());
963           if (mo[st_shndx].output_section == NULL)
964             continue;
965           st_shndx = mo[st_shndx].output_section->out_shndx();
966         }
967
968       elfcpp::Sym_write<size, big_endian> osym(ov);
969
970       gold_assert(isym.get_st_name() < strtab_size);
971       const char* name = pnames + isym.get_st_name();
972       osym.put_st_name(sympool->get_offset(name));
973       osym.put_st_value(this->local_values_[i].value(this, 0));
974       osym.put_st_size(isym.get_st_size());
975       osym.put_st_info(isym.get_st_info());
976       osym.put_st_other(isym.get_st_other());
977       osym.put_st_shndx(st_shndx);
978
979       ov += sym_size;
980     }
981
982   gold_assert(ov - oview == output_size);
983
984   of->write_output_view(this->local_symbol_offset_, output_size, oview);
985 }
986
987 // Set *INFO to symbolic information about the offset OFFSET in the
988 // section SHNDX.  Return true if we found something, false if we
989 // found nothing.
990
991 template<int size, bool big_endian>
992 bool
993 Sized_relobj<size, big_endian>::get_symbol_location_info(
994     unsigned int shndx,
995     off_t offset,
996     Symbol_location_info* info)
997 {
998   if (this->symtab_shndx_ == 0)
999     return false;
1000
1001   off_t symbols_size;
1002   const unsigned char* symbols = this->section_contents(this->symtab_shndx_,
1003                                                         &symbols_size,
1004                                                         false);
1005
1006   unsigned int symbol_names_shndx = this->section_link(this->symtab_shndx_);
1007   off_t names_size;
1008   const unsigned char* symbol_names_u =
1009     this->section_contents(symbol_names_shndx, &names_size, false);
1010   const char* symbol_names = reinterpret_cast<const char*>(symbol_names_u);
1011
1012   const int sym_size = This::sym_size;
1013   const size_t count = symbols_size / sym_size;
1014
1015   const unsigned char* p = symbols;
1016   for (size_t i = 0; i < count; ++i, p += sym_size)
1017     {
1018       elfcpp::Sym<size, big_endian> sym(p);
1019
1020       if (sym.get_st_type() == elfcpp::STT_FILE)
1021         {
1022           if (sym.get_st_name() >= names_size)
1023             info->source_file = "(invalid)";
1024           else
1025             info->source_file = symbol_names + sym.get_st_name();
1026         }
1027       else if (sym.get_st_shndx() == shndx
1028                && static_cast<off_t>(sym.get_st_value()) <= offset
1029                && (static_cast<off_t>(sym.get_st_value() + sym.get_st_size())
1030                    > offset))
1031         {
1032           if (sym.get_st_name() > names_size)
1033             info->enclosing_symbol_name = "(invalid)";
1034           else
1035             info->enclosing_symbol_name = symbol_names + sym.get_st_name();
1036           return true;
1037         }
1038     }
1039
1040   return false;
1041 }
1042
1043 // Input_objects methods.
1044
1045 // Add a regular relocatable object to the list.  Return false if this
1046 // object should be ignored.
1047
1048 bool
1049 Input_objects::add_object(Object* obj)
1050 {
1051   Target* target = obj->target();
1052   if (this->target_ == NULL)
1053     this->target_ = target;
1054   else if (this->target_ != target)
1055     {
1056       gold_error(_("%s: incompatible target"), obj->name().c_str());
1057       return false;
1058     }
1059
1060   if (!obj->is_dynamic())
1061     this->relobj_list_.push_back(static_cast<Relobj*>(obj));
1062   else
1063     {
1064       // See if this is a duplicate SONAME.
1065       Dynobj* dynobj = static_cast<Dynobj*>(obj);
1066       const char* soname = dynobj->soname();
1067
1068       std::pair<Unordered_set<std::string>::iterator, bool> ins =
1069         this->sonames_.insert(soname);
1070       if (!ins.second)
1071         {
1072           // We have already seen a dynamic object with this soname.
1073           return false;
1074         }
1075
1076       this->dynobj_list_.push_back(dynobj);
1077
1078       // If this is -lc, remember the directory in which we found it.
1079       // We use this when issuing warnings about undefined symbols: as
1080       // a heuristic, we don't warn about system libraries found in
1081       // the same directory as -lc.
1082       if (strncmp(soname, "libc.so", 7) == 0)
1083         {
1084           const char* object_name = dynobj->name().c_str();
1085           const char* base = lbasename(object_name);
1086           if (base != object_name)
1087             this->system_library_directory_.assign(object_name,
1088                                                    base - 1 - object_name);
1089         }
1090     }
1091
1092   set_parameters_size_and_endianness(target->get_size(),
1093                                      target->is_big_endian());
1094
1095   return true;
1096 }
1097
1098 // Return whether an object was found in the system library directory.
1099
1100 bool
1101 Input_objects::found_in_system_library_directory(const Object* object) const
1102 {
1103   return (!this->system_library_directory_.empty()
1104           && object->name().compare(0,
1105                                     this->system_library_directory_.size(),
1106                                     this->system_library_directory_) == 0);
1107 }
1108
1109 // For each dynamic object, record whether we've seen all of its
1110 // explicit dependencies.
1111
1112 void
1113 Input_objects::check_dynamic_dependencies() const
1114 {
1115   for (Dynobj_list::const_iterator p = this->dynobj_list_.begin();
1116        p != this->dynobj_list_.end();
1117        ++p)
1118     {
1119       const Dynobj::Needed& needed((*p)->needed());
1120       bool found_all = true;
1121       for (Dynobj::Needed::const_iterator pneeded = needed.begin();
1122            pneeded != needed.end();
1123            ++pneeded)
1124         {
1125           if (this->sonames_.find(*pneeded) == this->sonames_.end())
1126             {
1127               found_all = false;
1128               break;
1129             }
1130         }
1131       (*p)->set_has_unknown_needed_entries(!found_all);
1132     }
1133 }
1134
1135 // Relocate_info methods.
1136
1137 // Return a string describing the location of a relocation.  This is
1138 // only used in error messages.
1139
1140 template<int size, bool big_endian>
1141 std::string
1142 Relocate_info<size, big_endian>::location(size_t, off_t offset) const
1143 {
1144   // See if we can get line-number information from debugging sections.
1145   std::string filename;
1146   std::string file_and_lineno;   // Better than filename-only, if available.
1147
1148   Sized_dwarf_line_info<size, big_endian> line_info(this->object);
1149   // This will be "" if we failed to parse the debug info for any reason.
1150   file_and_lineno = line_info.addr2line(this->data_shndx, offset);
1151
1152   std::string ret(this->object->name());
1153   ret += ':';
1154   Symbol_location_info info;
1155   if (this->object->get_symbol_location_info(this->data_shndx, offset, &info))
1156     {
1157       ret += " in function ";
1158       // We could demangle this name before printing, but we don't
1159       // bother because gcc runs linker output through a demangle
1160       // filter itself.  The only advantage to demangling here is if
1161       // someone might call ld directly, rather than via gcc.  If we
1162       // did want to demangle, cplus_demangle() is in libiberty.
1163       ret += info.enclosing_symbol_name;
1164       ret += ":";
1165       filename = info.source_file;
1166     }
1167
1168   if (!file_and_lineno.empty())
1169     ret += file_and_lineno;
1170   else
1171     {
1172       if (!filename.empty())
1173         ret += filename;
1174       ret += "(";
1175       ret += this->object->section_name(this->data_shndx);
1176       char buf[100];
1177       // Offsets into sections have to be positive.
1178       snprintf(buf, sizeof(buf), "+0x%lx", static_cast<long>(offset));
1179       ret += buf;
1180       ret += ")";
1181     }
1182   return ret;
1183 }
1184
1185 } // End namespace gold.
1186
1187 namespace
1188 {
1189
1190 using namespace gold;
1191
1192 // Read an ELF file with the header and return the appropriate
1193 // instance of Object.
1194
1195 template<int size, bool big_endian>
1196 Object*
1197 make_elf_sized_object(const std::string& name, Input_file* input_file,
1198                       off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
1199 {
1200   int et = ehdr.get_e_type();
1201   if (et == elfcpp::ET_REL)
1202     {
1203       Sized_relobj<size, big_endian>* obj =
1204         new Sized_relobj<size, big_endian>(name, input_file, offset, ehdr);
1205       obj->setup(ehdr);
1206       return obj;
1207     }
1208   else if (et == elfcpp::ET_DYN)
1209     {
1210       Sized_dynobj<size, big_endian>* obj =
1211         new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
1212       obj->setup(ehdr);
1213       return obj;
1214     }
1215   else
1216     {
1217       gold_error(_("%s: unsupported ELF file type %d"),
1218                  name.c_str(), et);
1219       return NULL;
1220     }
1221 }
1222
1223 } // End anonymous namespace.
1224
1225 namespace gold
1226 {
1227
1228 // Read an ELF file and return the appropriate instance of Object.
1229
1230 Object*
1231 make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
1232                 const unsigned char* p, off_t bytes)
1233 {
1234   if (bytes < elfcpp::EI_NIDENT)
1235     {
1236       gold_error(_("%s: ELF file too short"), name.c_str());
1237       return NULL;
1238     }
1239
1240   int v = p[elfcpp::EI_VERSION];
1241   if (v != elfcpp::EV_CURRENT)
1242     {
1243       if (v == elfcpp::EV_NONE)
1244         gold_error(_("%s: invalid ELF version 0"), name.c_str());
1245       else
1246         gold_error(_("%s: unsupported ELF version %d"), name.c_str(), v);
1247       return NULL;
1248     }
1249
1250   int c = p[elfcpp::EI_CLASS];
1251   if (c == elfcpp::ELFCLASSNONE)
1252     {
1253       gold_error(_("%s: invalid ELF class 0"), name.c_str());
1254       return NULL;
1255     }
1256   else if (c != elfcpp::ELFCLASS32
1257            && c != elfcpp::ELFCLASS64)
1258     {
1259       gold_error(_("%s: unsupported ELF class %d"), name.c_str(), c);
1260       return NULL;
1261     }
1262
1263   int d = p[elfcpp::EI_DATA];
1264   if (d == elfcpp::ELFDATANONE)
1265     {
1266       gold_error(_("%s: invalid ELF data encoding"), name.c_str());
1267       return NULL;
1268     }
1269   else if (d != elfcpp::ELFDATA2LSB
1270            && d != elfcpp::ELFDATA2MSB)
1271     {
1272       gold_error(_("%s: unsupported ELF data encoding %d"), name.c_str(), d);
1273       return NULL;
1274     }
1275
1276   bool big_endian = d == elfcpp::ELFDATA2MSB;
1277
1278   if (c == elfcpp::ELFCLASS32)
1279     {
1280       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1281         {
1282           gold_error(_("%s: ELF file too short"), name.c_str());
1283           return NULL;
1284         }
1285       if (big_endian)
1286         {
1287 #ifdef HAVE_TARGET_32_BIG
1288           elfcpp::Ehdr<32, true> ehdr(p);
1289           return make_elf_sized_object<32, true>(name, input_file,
1290                                                  offset, ehdr);
1291 #else
1292           gold_error(_("%s: not configured to support "
1293                        "32-bit big-endian object"),
1294                      name.c_str());
1295           return NULL;
1296 #endif
1297         }
1298       else
1299         {
1300 #ifdef HAVE_TARGET_32_LITTLE
1301           elfcpp::Ehdr<32, false> ehdr(p);
1302           return make_elf_sized_object<32, false>(name, input_file,
1303                                                   offset, ehdr);
1304 #else
1305           gold_error(_("%s: not configured to support "
1306                        "32-bit little-endian object"),
1307                      name.c_str());
1308           return NULL;
1309 #endif
1310         }
1311     }
1312   else
1313     {
1314       if (bytes < elfcpp::Elf_sizes<32>::ehdr_size)
1315         {
1316           gold_error(_("%s: ELF file too short"), name.c_str());
1317           return NULL;
1318         }
1319       if (big_endian)
1320         {
1321 #ifdef HAVE_TARGET_64_BIG
1322           elfcpp::Ehdr<64, true> ehdr(p);
1323           return make_elf_sized_object<64, true>(name, input_file,
1324                                                  offset, ehdr);
1325 #else
1326           gold_error(_("%s: not configured to support "
1327                        "64-bit big-endian object"),
1328                      name.c_str());
1329           return NULL;
1330 #endif
1331         }
1332       else
1333         {
1334 #ifdef HAVE_TARGET_64_LITTLE
1335           elfcpp::Ehdr<64, false> ehdr(p);
1336           return make_elf_sized_object<64, false>(name, input_file,
1337                                                   offset, ehdr);
1338 #else
1339           gold_error(_("%s: not configured to support "
1340                        "64-bit little-endian object"),
1341                      name.c_str());
1342           return NULL;
1343 #endif
1344         }
1345     }
1346 }
1347
1348 // Instantiate the templates we need.  We could use the configure
1349 // script to restrict this to only the ones for implemented targets.
1350
1351 #ifdef HAVE_TARGET_32_LITTLE
1352 template
1353 class Sized_relobj<32, false>;
1354 #endif
1355
1356 #ifdef HAVE_TARGET_32_BIG
1357 template
1358 class Sized_relobj<32, true>;
1359 #endif
1360
1361 #ifdef HAVE_TARGET_64_LITTLE
1362 template
1363 class Sized_relobj<64, false>;
1364 #endif
1365
1366 #ifdef HAVE_TARGET_64_BIG
1367 template
1368 class Sized_relobj<64, true>;
1369 #endif
1370
1371 #ifdef HAVE_TARGET_32_LITTLE
1372 template
1373 struct Relocate_info<32, false>;
1374 #endif
1375
1376 #ifdef HAVE_TARGET_32_BIG
1377 template
1378 struct Relocate_info<32, true>;
1379 #endif
1380
1381 #ifdef HAVE_TARGET_64_LITTLE
1382 template
1383 struct Relocate_info<64, false>;
1384 #endif
1385
1386 #ifdef HAVE_TARGET_64_BIG
1387 template
1388 struct Relocate_info<64, true>;
1389 #endif
1390
1391 } // End namespace gold.