* gold.cc (queue_initial_tasks): Pass incremental base filename
[external/binutils.git] / 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           break;
157         case INCREMENTAL_INPUT_ARCHIVE:
158           printf("Archive\n");
159           printf("    Member count: %d\n", input_file.get_member_count());
160           printf("    Unused symbol count: %d\n",
161                  input_file.get_unused_symbol_count());
162           break;
163         case INCREMENTAL_INPUT_SHARED_LIBRARY:
164           printf("Shared library\n");
165           printf("    As needed: %s\n",
166                  input_file.as_needed() ? "true" : "false");
167           printf("    soname: %s\n",
168                  input_file.get_soname());
169           printf("    Symbol count: %d\n",
170                  input_file.get_global_symbol_count());
171           break;
172         case INCREMENTAL_INPUT_SCRIPT:
173           printf("Linker script\n");
174           printf("    Object count: %d\n", input_file.get_object_count());
175           break;
176         default:
177           fprintf(stderr, "%s: invalid file type for object %u: %d\n",
178                   argv0, i, input_type);
179           exit(1);
180         }
181     }
182
183   printf("\nInput sections:\n");
184   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
185     {
186       Entry_reader input_file(incremental_inputs.input_file(i));
187
188       if (input_file.type() != INCREMENTAL_INPUT_OBJECT
189           && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER)
190         continue;
191
192       const char* objname = input_file.filename();
193       if (objname == NULL)
194         {
195           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
196                   argv0, filename, i);
197           exit(1);
198         }
199
200       printf("[%d] %s\n", i, objname);
201
202       printf("    %3s  %6s  %8s  %8s  %s\n",
203              "n", "outndx", "offset", "size", "name");
204       unsigned int nsections = input_file.get_input_section_count();
205       for (unsigned int shndx = 0; shndx < nsections; ++shndx)
206         {
207           typename Entry_reader::Input_section_info info(
208               input_file.get_input_section(shndx));
209           printf("    %3d  %6d  %8lld  %8lld  %s\n", shndx + 1,
210                  info.output_shndx,
211                  static_cast<long long>(info.sh_offset),
212                  static_cast<long long>(info.sh_size),
213                  info.name);
214         }
215     }
216
217   // Get a view of the .symtab section.
218
219   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file(inc);
220
221   unsigned int symtab_shndx = elf_file.find_section_by_type(elfcpp::SHT_SYMTAB);
222   if (symtab_shndx == elfcpp::SHN_UNDEF)  // Not found.
223     {
224       fprintf(stderr, "%s: %s: no symbol table section\n", argv0, filename);
225       exit(1);
226     }
227   Location symtab_location(elf_file.section_contents(symtab_shndx));
228   View symtab_view(inc->view(symtab_location));
229
230   // Get a view of the .strtab section.
231
232   unsigned int strtab_shndx = elf_file.section_link(symtab_shndx);
233   if (strtab_shndx == elfcpp::SHN_UNDEF
234       || strtab_shndx > elf_file.shnum()
235       || elf_file.section_type(strtab_shndx) != elfcpp::SHT_STRTAB)
236     {
237       fprintf(stderr, "%s: %s: no string table section\n", argv0, filename);
238       exit(1);
239     }
240   Location strtab_location(elf_file.section_contents(strtab_shndx));
241   View strtab_view(inc->view(strtab_location));
242   elfcpp::Elf_strtab strtab(strtab_view.data(), strtab_location.data_size);
243
244   // The .gnu_incremental_symtab section contains entries that parallel
245   // the global symbols of the main symbol table.  The sh_info field
246   // of the main symbol table's section header tells us how many global
247   // symbols there are, but that count does not include any global
248   // symbols that were forced local during the link.  Therefore, we
249   // use the size of the .gnu_incremental_symtab section to deduce
250   // the number of global symbols + forced-local symbols there are
251   // in the symbol table.
252   Incremental_symtab_reader<big_endian> isymtab(inc->symtab_reader());
253   Incremental_relocs_reader<size, big_endian> irelocs(inc->relocs_reader());
254   unsigned int sym_size = elfcpp::Elf_sizes<size>::sym_size;
255   unsigned int nsyms = symtab_location.data_size / sym_size;
256   unsigned int nglobals = isymtab.symbol_count();
257   unsigned int first_global = nsyms - nglobals;
258   unsigned const char* sym_p;
259
260   printf("\nGlobal symbols per input file:\n");
261   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
262     {
263       Entry_reader input_file(incremental_inputs.input_file(i));
264
265       if (input_file.type() != INCREMENTAL_INPUT_OBJECT
266           && input_file.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
267           && input_file.type() != INCREMENTAL_INPUT_SHARED_LIBRARY)
268         continue;
269
270       const char* objname = input_file.filename();
271       if (objname == NULL)
272         {
273           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
274                   argv0, filename, i);
275           exit(1);
276         }
277
278       printf("[%d] %s\n", i, objname);
279
280       unsigned int nsyms = input_file.get_global_symbol_count();
281       if (nsyms > 0)
282         printf("    %6s  %6s  %8s  %8s  %8s  %8s\n",
283                "outndx", "shndx", "offset", "chain", "#relocs", "rbase");
284       if (input_file.type() == INCREMENTAL_INPUT_SHARED_LIBRARY)
285         {
286           for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
287             {
288               bool is_def;
289               unsigned int output_symndx =
290                   input_file.get_output_symbol_index(symndx, &is_def);
291               sym_p = symtab_view.data() + output_symndx * sym_size;
292               elfcpp::Sym<size, big_endian> sym(sym_p);
293               const char* symname;
294               if (!strtab.get_c_string(sym.get_st_name(), &symname))
295                 symname = "<unknown>";
296               printf("    %6d  %6s  %8s  %8s  %8s  %8s  %-5s  %s\n",
297                      output_symndx,
298                      "", "", "", "", "",
299                      is_def ? "DEF" : "UNDEF",
300                      symname);
301             }
302         }
303       else
304         {
305           for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
306             {
307               Incremental_global_symbol_reader<big_endian> info(
308                   input_file.get_global_symbol_reader(symndx));
309               unsigned int output_symndx = info.output_symndx();
310               sym_p = symtab_view.data() + output_symndx * sym_size;
311               elfcpp::Sym<size, big_endian> sym(sym_p);
312               const char* symname;
313               if (!strtab.get_c_string(sym.get_st_name(), &symname))
314                 symname = "<unknown>";
315               printf("    %6d  %6d  %8d  %8d  %8d  %8d  %-5s  %s\n",
316                      output_symndx,
317                      info.shndx(),
318                      input_file.get_symbol_offset(symndx),
319                      info.next_offset(),
320                      info.reloc_count(),
321                      info.reloc_offset(),
322                      info.shndx() != elfcpp::SHN_UNDEF ? "DEF" : "UNDEF",
323                      symname);
324             }
325         }
326     }
327
328   sym_p = symtab_view.data() + first_global * sym_size;
329   printf("\nGlobal symbol table:\n");
330   for (unsigned int i = 0; i < nglobals; i++)
331     {
332       elfcpp::Sym<size, big_endian> sym(sym_p);
333       const char* symname;
334       if (!strtab.get_c_string(sym.get_st_name(), &symname))
335         symname = "<unknown>";
336       printf("[%d] %s\n", first_global + i, symname);
337       unsigned int offset = isymtab.get_list_head(i);
338       while (offset > 0)
339         {
340           unsigned int sym_ndx;
341           Entry_reader input_file =
342               find_input_containing_global<size, big_endian>(incremental_inputs,
343                                                              offset, &sym_ndx);
344           Incremental_global_symbol_reader<big_endian> sym_info(
345               input_file.get_global_symbol_reader(sym_ndx));
346           printf("    %s (first reloc: %d, reloc count: %d)",
347                  input_file.filename(), sym_info.reloc_offset(),
348                  sym_info.reloc_count());
349           if (sym_info.output_symndx() != first_global + i)
350             printf(" ** wrong output symndx (%d) **", sym_info.output_symndx());
351           printf("\n");
352           // Dump the relocations from this input file for this symbol.
353           unsigned int r_off = sym_info.reloc_offset();
354           for (unsigned int j = 0; j < sym_info.reloc_count(); j++)
355             {
356               printf("      %4d  relocation type %3d  shndx %2d"
357                      "  offset %016llx  addend %016llx  %s\n",
358                      r_off,
359                      irelocs.get_r_type(r_off),
360                      irelocs.get_r_shndx(r_off),
361                      static_cast<long long>(irelocs.get_r_offset(r_off)),
362                      static_cast<long long>(irelocs.get_r_addend(r_off)),
363                      symname);
364               r_off += irelocs.reloc_size;
365             }
366           offset = sym_info.next_offset();
367         }
368       sym_p += sym_size;
369     }
370
371   Incremental_got_plt_reader<big_endian> igot_plt(inc->got_plt_reader());
372   unsigned int ngot = igot_plt.get_got_entry_count();
373   unsigned int nplt = igot_plt.get_plt_entry_count();
374   
375   printf("\nGOT entries:\n");
376   for (unsigned int i = 0; i < ngot; ++i)
377     {
378       unsigned int got_type = igot_plt.get_got_type(i);
379       unsigned int got_symndx = igot_plt.get_got_symndx(i);
380       unsigned int got_input_index = igot_plt.get_got_input_index(i);
381       printf("[%d] type %02x, ", i, got_type & 0x7f);
382       if ((got_type & 0x7f) == 0x7f)
383         printf("reserved");
384       else if (got_type & 0x80)
385         {
386           Entry_reader input_file =
387               incremental_inputs.input_file(got_input_index);
388           const char* objname = input_file.filename();
389           printf("local: %s (%d)", objname, got_symndx);
390         }
391       else
392         {
393           sym_p = symtab_view.data() + got_symndx * sym_size;
394           elfcpp::Sym<size, big_endian> sym(sym_p);
395           const char* symname;
396           if (!strtab.get_c_string(sym.get_st_name(), &symname))
397             symname = "<unknown>";
398           printf("global %s (%d)", symname, got_symndx);
399         }
400       printf("\n");
401     }
402
403   printf("\nPLT entries:\n");
404   for (unsigned int i = 0; i < nplt; ++i)
405     {
406       unsigned int plt_desc = igot_plt.get_plt_desc(i);
407       printf("[%d] ", i);
408       sym_p = symtab_view.data() + plt_desc * sym_size;
409       elfcpp::Sym<size, big_endian> sym(sym_p);
410       const char* symname;
411       if (!strtab.get_c_string(sym.get_st_name(), &symname))
412         symname = "<unknown>";
413       printf("%s (%d)\n", symname, plt_desc);
414     }
415
416   printf("\nUnused archive symbols:\n");
417   for (unsigned int i = 0; i < incremental_inputs.input_file_count(); ++i)
418     {
419       Entry_reader input_file(incremental_inputs.input_file(i));
420
421       if (input_file.type() != INCREMENTAL_INPUT_ARCHIVE)
422         continue;
423
424       const char* objname = input_file.filename();
425       if (objname == NULL)
426         {
427           fprintf(stderr,"%s: %s: failed to get file name for object %u\n",
428                   argv0, filename, i);
429           exit(1);
430         }
431
432       printf("[%d] %s\n", i, objname);
433       unsigned int nsyms = input_file.get_unused_symbol_count();
434       for (unsigned int symndx = 0; symndx < nsyms; ++symndx)
435         printf("    %s\n", input_file.get_unused_symbol(symndx));
436     }
437
438 }
439
440 int
441 main(int argc, char** argv)
442 {
443   if (argc != 2)
444     {
445       fprintf(stderr, "Usage: %s <file>\n", argv[0]);
446       return 1;
447     }
448   const char* filename = argv[1];
449
450   Output_file* file = new Output_file(filename);
451
452   bool t = file->open_base_file(NULL, false);
453   if (!t)
454     {
455       fprintf(stderr, "%s: open_base_file(%s): %s\n", argv[0], filename,
456               strerror(errno));
457       return 1;
458     }
459
460   Incremental_binary* inc = open_incremental_binary(file);
461
462   if (inc == NULL)
463     {
464       fprintf(stderr, "%s: open_incremental_binary(%s): %s\n", argv[0],
465               filename, strerror(errno));
466       return 1;
467     }
468
469   switch (parameters->size_and_endianness())
470     {
471 #ifdef HAVE_TARGET_32_LITTLE
472     case Parameters::TARGET_32_LITTLE:
473       dump_incremental_inputs<32, false>(
474           argv[0], filename,
475           static_cast<Sized_incremental_binary<32, false>*>(inc));
476       break;
477 #endif
478 #ifdef HAVE_TARGET_32_BIG
479     case Parameters::TARGET_32_BIG:
480       dump_incremental_inputs<32, true>(
481           argv[0], filename,
482           static_cast<Sized_incremental_binary<32, true>*>(inc));
483       break;
484 #endif
485 #ifdef HAVE_TARGET_64_LITTLE
486     case Parameters::TARGET_64_LITTLE:
487       dump_incremental_inputs<64, false>(
488           argv[0], filename,
489           static_cast<Sized_incremental_binary<64, false>*>(inc));
490       break;
491 #endif
492 #ifdef HAVE_TARGET_64_BIG
493     case Parameters::TARGET_64_BIG:
494       dump_incremental_inputs<64, true>(
495           argv[0], filename,
496           static_cast<Sized_incremental_binary<64, true>*>(inc));
497       break;
498 #endif
499     default:
500       gold_unreachable();
501     }
502
503   return 0;
504 }