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