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