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