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