Merge branch 'vendor/HOSTAPD'
[dragonfly.git] / contrib / binutils-2.22 / gold / incremental-dump.cc
1 // incremental.cc -- incremental linking test/debug tool
2
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Rafael Avila de Espindola <rafael.espindola@gmail.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
24 // This file is a (still incomplete) test/debug tool that should display
25 // all information available in the incremental linking sections in a
26 // format that is easy to read.
27 // Once the format is a bit more stable, this should probably be moved to
28 // readelf. Because of that, the use of gold's data structures and functions
29 // is just a short term convenience and not a design decision.
30
31 #include "gold.h"
32
33 #include <stdio.h>
34 #include <errno.h>
35 #include <time.h>
36
37 #include "incremental.h"
38
39 namespace gold
40 {
41   class Output_file;
42 }
43
44 using namespace gold;
45
46 template<int size, bool big_endian>
47 static typename Incremental_inputs_reader<size, big_endian>::
48     Incremental_input_entry_reader
49 find_input_containing_global(
50     Incremental_inputs_reader<size, big_endian>& incremental_inputs,
51     unsigned int offset,
52     unsigned int* symndx)
53 {
54   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
55   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
56     {
57       typename Inputs_reader::Incremental_input_entry_reader input_file =
58           incremental_inputs.input_file(i);
59       if (input_file.type() != INCREMENTAL_INPUT_OBJECT
60           && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
61         continue;
62       unsigned int nsyms = input_file.get_global_symbol_count();
63       if (offset >= input_file.get_symbol_offset(0)
64           && offset < input_file.get_symbol_offset(nsyms))
65         {
66           *symndx = (offset - input_file.get_symbol_offset(0)) / 20;
67           return input_file;
68         }
69     }
70   gold_unreachable();
71 }
72
73 template<int size, bool big_endian>
74 static void
75 dump_incremental_inputs(const char* argv0, const char* filename,
76                         Sized_incremental_binary<size, big_endian>* inc)
77 {
78   typedef Incremental_binary::Location Location;
79   typedef Incremental_binary::View View;
80   typedef Incremental_inputs_reader<size, big_endian> Inputs_reader;
81   typedef typename Inputs_reader::Incremental_input_entry_reader Entry_reader;
82
83   if (!inc->has_incremental_info())
84     {
85       fprintf(stderr, "%s: %s: no .gnu_incremental_inputs section\n", argv0,
86               filename);
87       exit(1);
88     }
89
90   // Create a reader object for the .gnu_incremental_inputs section.
91
92   Incremental_inputs_reader<size, big_endian>
93       incremental_inputs(inc->inputs_reader());
94
95   if (incremental_inputs.version() != 1)
96     {
97       fprintf(stderr, "%s: %s: unknown incremental version %d\n", argv0,
98               filename, incremental_inputs.version());
99       exit(1);
100     }
101
102   const char* command_line = incremental_inputs.command_line();
103   if (command_line == NULL)
104     {
105       fprintf(stderr,
106               "%s: %s: failed to get link command line\n",
107               argv0, filename);
108       exit(1);
109     }
110   printf("Link command line: %s\n", command_line);
111
112   printf("\nInput files:\n");
113   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
114     {
115       Entry_reader input_file = incremental_inputs.input_file(i);
116
117       const char* objname = input_file.filename();
118       if (objname == NULL)
119         {
120           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
121                   argv0, filename, i);
122           exit(1);
123         }
124       printf("[%d] %s\n", i, objname);
125
126       Timespec mtime = input_file.get_mtime();
127       printf("    Timestamp: %llu.%09d  %s",
128              static_cast<unsigned long long>(mtime.seconds),
129              mtime.nanoseconds,
130              ctime(&mtime.seconds));
131
132       printf("    Serial Number: %d\n", input_file.arg_serial());
133       printf("    In System Directory: %s\n",
134              input_file.is_in_system_directory() ? "true" : "false");
135
136       Incremental_input_type input_type = input_file.type();
137       printf("    Type: ");
138       switch (input_type)
139         {
140         case INCREMENTAL_INPUT_OBJECT:
141         case INCREMENTAL_INPUT_ARCHIVE_MEMBER:
142           printf("%s\n", (input_type == INCREMENTAL_INPUT_OBJECT
143                           ? "Object" : "Archive member"));
144           printf("    Input section count: %d\n",
145                  input_file.get_input_section_count());
146           printf("    Global symbol count: %d\n",
147                  input_file.get_global_symbol_count());
148           printf("    Local symbol offset: %d\n",
149                  input_file.get_local_symbol_offset());
150           printf("    Local symbol count: %d\n",
151                  input_file.get_local_symbol_count());
152           printf("    First dynamic reloc: %d\n",
153                  input_file.get_first_dyn_reloc());
154           printf("    Dynamic reloc count: %d\n",
155                  input_file.get_dyn_reloc_count());
156           printf("    COMDAT group count: %d\n",
157                  input_file.get_comdat_group_count());
158           break;
159         case INCREMENTAL_INPUT_ARCHIVE:
160           printf("Archive\n");
161           printf("    Member count: %d\n", input_file.get_member_count());
162           printf("    Unused symbol count: %d\n",
163                  input_file.get_unused_symbol_count());
164           break;
165         case INCREMENTAL_INPUT_SHARED_LIBRARY:
166           printf("Shared library\n");
167           printf("    As needed: %s\n",
168                  input_file.as_needed() ? "true" : "false");
169           printf("    soname: %s\n",
170                  input_file.get_soname());
171           printf("    Symbol count: %d\n",
172                  input_file.get_global_symbol_count());
173           break;
174         case INCREMENTAL_INPUT_SCRIPT:
175           printf("Linker script\n");
176           printf("    Object count: %d\n", input_file.get_object_count());
177           break;
178         default:
179           fprintf(stderr, "%s: invalid file type for object %u: %d\n",
180                   argv0, i, input_type);
181           exit(1);
182         }
183     }
184
185   printf("\nInput sections:\n");
186   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
187     {
188       Entry_reader input_file(incremental_inputs.input_file(i));
189
190       if (input_file.type() != INCREMENTAL_INPUT_OBJECT
191           && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
192         continue;
193
194       const char* objname = input_file.filename();
195       if (objname == NULL)
196         {
197           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
198                   argv0, filename, i);
199           exit(1);
200         }
201
202       printf("[%d] %s\n", i, objname);
203
204       printf("    %3s  %6s  %8s  %8s  %s\n",
205              "n", "outndx", "offset", "size", "name");
206       unsigned int nsections = input_file.get_input_section_count();
207       for (unsigned int shndx = 0; shndx < nsections; ++shndx)
208         {
209           typename Entry_reader::Input_section_info info(
210               input_file.get_input_section(shndx));
211           printf("    %3d  %6d  %8lld  %8lld  %s\n", shndx + 1,
212                  info.output_shndx,
213                  static_cast<long long>(info.sh_offset),
214                  static_cast<long long>(info.sh_size),
215                  info.name);
216         }
217
218       unsigned int ncomdat = input_file.get_comdat_group_count();
219       for (unsigned int i = 0; i < ncomdat; ++i)
220         printf("    Comdat group: %s\n",
221                input_file.get_comdat_group_signature(i));
222     }
223
224   // Get a view of the .symtab section.
225
226   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file(inc);
227
228   unsigned int symtab_shndx = elf_file.find_section_by_type(elfcpp::SHT_SYMTAB);
229   if (symtab_shndx == elfcpp::SHN_UNDEF)  // Not found.
230     {
231       fprintf(stderr, "%s: %s: no symbol table section\n", argv0, filename);
232       exit(1);
233     }
234   Location symtab_location(elf_file.section_contents(symtab_shndx));
235   View symtab_view(inc->view(symtab_location));
236
237   // Get a view of the .strtab section.
238
239   unsigned int strtab_shndx = elf_file.section_link(symtab_shndx);
240   if (strtab_shndx == elfcpp::SHN_UNDEF
241       || strtab_shndx > elf_file.shnum()
242       || elf_file.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
243     {
244       fprintf(stderr, "%s: %s: no string table section\n", argv0, filename);
245       exit(1);
246     }
247   Location strtab_location(elf_file.section_contents(strtab_shndx));
248   View strtab_view(inc->view(strtab_location));
249   elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
250
251   // The .gnu_incremental_symtab section contains entries that parallel
252   // the global symbols of the main symbol table.  The sh_info field
253   // of the main symbol table's section header tells us how many global
254   // symbols there are, but that count does not include any global
255   // symbols that were forced local during the link.  Therefore, we
256   // use the size of the .gnu_incremental_symtab section to deduce
257   // the number of global symbols + forced-local symbols there are
258   // in the symbol table.
259   Incremental_symtab_reader<big_endian> isymtab(inc->symtab_reader());
260   Incremental_relocs_reader<size, big_endian> irelocs(inc->relocs_reader());
261   unsigned int sym_size = elfcpp::Elf_sizes<size>::sym_size;
262   unsigned int nsyms = symtab_location.data_size / sym_size;
263   unsigned int nglobals = isymtab.symbol_count();
264   unsigned int first_global = nsyms - nglobals;
265   unsigned const char* sym_p;
266
267   printf("\nGlobal symbols per input file:\n");
268   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
269     {
270       Entry_reader input_file(incremental_inputs.input_file(i));
271
272       if (input_file.type() != INCREMENTAL_INPUT_OBJECT
273           && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
274           && input_file.type() != INCREMENTAL_INPUT_SHARED_LIBRARY)
275         continue;
276
277       const char* objname = input_file.filename();
278       if (objname == NULL)
279         {
280           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
281                   argv0, filename, i);
282           exit(1);
283         }
284
285       printf("[%d] %s\n", i, objname);
286
287       unsigned int nsyms = input_file.get_global_symbol_count();
288       if (nsyms > 0)
289         printf("    %6s  %6s  %8s  %8s  %8s  %8s\n",
290                "outndx", "shndx", "offset", "chain", "#relocs", "rbase");
291       if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
292         {
293           for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
294             {
295               bool is_def;
296               bool is_copy;
297               unsigned int output_symndx =
298                   input_file.get_output_symbol_index(symndx, &is_def, &is_copy);
299               sym_p = symtab_view.data() + output_symndx * sym_size;
300               elfcpp::Sym<size, big_endian> sym(sym_p);
301               const char* symname;
302               if (!strtab.get_c_string(sym.get_st_name(), &symname))
303                 symname = "<unknown>";
304               printf("    %6d  %6s  %8s  %8s  %8s  %8s  %-5s  %s\n",
305                      output_symndx,
306                      "", "", "", "", "",
307                      is_copy ? "COPY" : (is_def ? "DEF" : "UNDEF"),
308                      symname);
309             }
310         }
311       else
312         {
313           for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
314             {
315               Incremental_global_symbol_reader<big_endian> info(
316                   input_file.get_global_symbol_reader(symndx));
317               unsigned int output_symndx = info.output_symndx();
318               sym_p = symtab_view.data() + output_symndx * sym_size;
319               elfcpp::Sym<size, big_endian> sym(sym_p);
320               const char* symname;
321               if (!strtab.get_c_string(sym.get_st_name(), &symname))
322                 symname = "<unknown>";
323               printf("    %6d  %6d  %8d  %8d  %8d  %8d  %-5s  %s\n",
324                      output_symndx,
325                      info.shndx() == -1U ? -1 : info.shndx(),
326                      input_file.get_symbol_offset(symndx),
327                      info.next_offset(),
328                      info.reloc_count(),
329                      info.reloc_offset(),
330                      (info.shndx() == -1U
331                       ? "BASE"
332                       : info.shndx() == 0 ? "UNDEF" : "DEF"),
333                      symname);
334             }
335         }
336     }
337
338   sym_p = symtab_view.data() + first_global * sym_size;
339   printf("\nGlobal symbol table:\n");
340   for (unsigned int i = 0; i < nglobals; i++)
341     {
342       elfcpp::Sym<size, big_endian> sym(sym_p);
343       const char* symname;
344       if (!strtab.get_c_string(sym.get_st_name(), &symname))
345         symname = "<unknown>";
346       printf("[%d] %s\n", first_global + i, symname);
347       unsigned int offset = isymtab.get_list_head(i);
348       while (offset > 0)
349         {
350           unsigned int sym_ndx;
351           Entry_reader input_file =
352               find_input_containing_global<size, big_endian>(incremental_inputs,
353                                                              offset, &sym_ndx);
354           Incremental_global_symbol_reader<big_endian> sym_info(
355               input_file.get_global_symbol_reader(sym_ndx));
356           printf("    %s (first reloc: %d, reloc count: %d)",
357                  input_file.filename(), sym_info.reloc_offset(),
358                  sym_info.reloc_count());
359           if (sym_info.output_symndx() != first_global + i)
360             printf(" ** wrong output symndx (%d) **", sym_info.output_symndx());
361           printf("\n");
362           // Dump the relocations from this input file for this symbol.
363           unsigned int r_off = sym_info.reloc_offset();
364           for (unsigned int j = 0; j < sym_info.reloc_count(); j++)
365             {
366               printf("      %4d  relocation type %3d  shndx %2d"
367                      "  offset %016llx  addend %016llx  %s\n",
368                      r_off,
369                      irelocs.get_r_type(r_off),
370                      irelocs.get_r_shndx(r_off),
371                      static_cast<long long>(irelocs.get_r_offset(r_off)),
372                      static_cast<long long>(irelocs.get_r_addend(r_off)),
373                      symname);
374               r_off += irelocs.reloc_size;
375             }
376           offset = sym_info.next_offset();
377         }
378       sym_p += sym_size;
379     }
380
381   Incremental_got_plt_reader<big_endian> igot_plt(inc->got_plt_reader());
382   unsigned int ngot = igot_plt.get_got_entry_count();
383   unsigned int nplt = igot_plt.get_plt_entry_count();
384   
385   printf("\nGOT entries:\n");
386   for (unsigned int i = 0; i < ngot; ++i)
387     {
388       unsigned int got_type = igot_plt.get_got_type(i);
389       unsigned int got_symndx = igot_plt.get_got_symndx(i);
390       unsigned int got_input_index = igot_plt.get_got_input_index(i);
391       printf("[%d] type %02x, ", i, got_type & 0x7f);
392       if ((got_type & 0x7f) == 0x7f)
393         printf("reserved");
394       else if (got_type & 0x80)
395         {
396           Entry_reader input_file =
397               incremental_inputs.input_file(got_input_index);
398           const char* objname = input_file.filename();
399           printf("local: %s (%d)", objname, got_symndx);
400         }
401       else
402         {
403           sym_p = symtab_view.data() + got_symndx * sym_size;
404           elfcpp::Sym<size, big_endian> sym(sym_p);
405           const char* symname;
406           if (!strtab.get_c_string(sym.get_st_name(), &symname))
407             symname = "<unknown>";
408           printf("global %s (%d)", symname, got_symndx);
409         }
410       printf("\n");
411     }
412
413   printf("\nPLT entries:\n");
414   for (unsigned int i = 0; i < nplt; ++i)
415     {
416       unsigned int plt_desc = igot_plt.get_plt_desc(i);
417       printf("[%d] ", i);
418       sym_p = symtab_view.data() + plt_desc * sym_size;
419       elfcpp::Sym<size, big_endian> sym(sym_p);
420       const char* symname;
421       if (!strtab.get_c_string(sym.get_st_name(), &symname))
422         symname = "<unknown>";
423       printf("%s (%d)\n", symname, plt_desc);
424     }
425
426   printf("\nUnused archive symbols:\n");
427   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
428     {
429       Entry_reader input_file(incremental_inputs.input_file(i));
430
431       if (input_file.type() != INCREMENTAL_INPUT_ARCHIVE)
432         continue;
433
434       const char* objname = input_file.filename();
435       if (objname == NULL)
436         {
437           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
438                   argv0, filename, i);
439           exit(1);
440         }
441
442       printf("[%d] %s\n", i, objname);
443       unsigned int nsyms = input_file.get_unused_symbol_count();
444       for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
445         printf("    %s\n", input_file.get_unused_symbol(symndx));
446     }
447
448 }
449
450 int
451 main(int argc, char** argv)
452 {
453   if (argc != 2)
454     {
455       fprintf(stderr, "Usage: %s <file>\n", argv[0]);
456       return 1;
457     }
458   const char* filename = argv[1];
459
460   Output_file* file = new Output_file(filename);
461
462   bool t = file->open_base_file(NULL, false);
463   if (!t)
464     {
465       fprintf(stderr, "%s: open_base_file(%s): %s\n", argv[0], filename,
466               strerror(errno));
467       return 1;
468     }
469
470   Incremental_binary* inc = open_incremental_binary(file);
471
472   if (inc == NULL)
473     {
474       fprintf(stderr, "%s: open_incremental_binary(%s): %s\n", argv[0],
475               filename, strerror(errno));
476       return 1;
477     }
478
479   switch (parameters->size_and_endianness())
480     {
481 #ifdef HAVE_TARGET_32_LITTLE
482     case Parameters::TARGET_32_LITTLE:
483       dump_incremental_inputs<32, false>(
484           argv[0], filename,
485           static_cast<Sized_incremental_binary<32, false>*>(inc));
486       break;
487 #endif
488 #ifdef HAVE_TARGET_32_BIG
489     case Parameters::TARGET_32_BIG:
490       dump_incremental_inputs<32, true>(
491           argv[0], filename,
492           static_cast<Sized_incremental_binary<32, true>*>(inc));
493       break;
494 #endif
495 #ifdef HAVE_TARGET_64_LITTLE
496     case Parameters::TARGET_64_LITTLE:
497       dump_incremental_inputs<64, false>(
498           argv[0], filename,
499           static_cast<Sized_incremental_binary<64, false>*>(inc));
500       break;
501 #endif
502 #ifdef HAVE_TARGET_64_BIG
503     case Parameters::TARGET_64_BIG:
504       dump_incremental_inputs<64, true>(
505           argv[0], filename,
506           static_cast<Sized_incremental_binary<64, true>*>(inc));
507       break;
508 #endif
509     default:
510       gold_unreachable();
511     }
512
513   return 0;
514 }