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