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