1 // incremental.cc -- incremental linking test/debug tool
3 // Copyright 2009, 2010 Free Software Foundation, Inc.
4 // Written by Rafael Avila de Espindola <rafael.espindola@gmail.com>
6 // This file is part of gold.
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.
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.
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.
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.
37 #include "incremental.h"
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
,
54 typedef Incremental_inputs_reader
<size
, big_endian
> Inputs_reader
;
55 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
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
)
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
))
66 *symndx
= (offset
- input_file
.get_symbol_offset(0)) / 16;
73 template<int size
, bool big_endian
>
75 dump_incremental_inputs(const char* argv0
, const char* filename
,
76 Incremental_binary
* inc
)
79 unsigned int inputs_shndx
;
80 unsigned int isymtab_shndx
;
81 unsigned int irelocs_shndx
;
82 unsigned int igot_plt_shndx
;
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
;
89 // Find the .gnu_incremental_inputs, _symtab, _relocs, and _strtab sections.
91 t
= inc
->find_incremental_inputs_sections(&inputs_shndx
, &isymtab_shndx
,
92 &irelocs_shndx
, &igot_plt_shndx
,
96 fprintf(stderr
, "%s: %s: no .gnu_incremental_inputs section\n", argv0
,
101 elfcpp::Elf_file
<size
, big_endian
, Incremental_binary
> elf_file(inc
);
103 // Get a view of the .gnu_incremental_inputs section.
105 Location
inputs_location(elf_file
.section_contents(inputs_shndx
));
106 View
inputs_view(inc
->view(inputs_location
));
108 // Get the .gnu_incremental_strtab section as a string table.
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
);
114 // Create a reader object for the .gnu_incremental_inputs section.
116 Incremental_inputs_reader
<size
, big_endian
>
117 incremental_inputs(inputs_view
.data(), istrtab
);
119 if (incremental_inputs
.version() != 1)
121 fprintf(stderr
, "%s: %s: unknown incremental version %d\n", argv0
,
122 filename
, incremental_inputs
.version());
126 const char* command_line
= incremental_inputs
.command_line();
127 if (command_line
== NULL
)
130 "%s: %s: failed to get link command line\n",
134 printf("Link command line: %s\n", command_line
);
136 printf("\nInput files:\n");
137 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
139 Entry_reader input_file
= incremental_inputs
.input_file(i
);
141 const char* objname
= input_file
.filename();
144 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
148 printf("[%d] %s\n", i
, objname
);
150 Timespec mtime
= input_file
.get_mtime();
151 printf(" Timestamp: %llu.%09d %s",
152 static_cast<unsigned long long>(mtime
.seconds
),
154 ctime(&mtime
.seconds
));
156 Incremental_input_type input_type
= input_file
.type();
160 case INCREMENTAL_INPUT_OBJECT
:
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());
167 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
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());
174 case INCREMENTAL_INPUT_ARCHIVE
:
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());
180 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
181 printf("Shared library\n");
182 printf(" Symbol count: %d\n",
183 input_file
.get_global_symbol_count());
185 case INCREMENTAL_INPUT_SCRIPT
:
186 printf("Linker script\n");
187 printf(" Object count: %d\n", input_file
.get_object_count());
190 fprintf(stderr
, "%s: invalid file type for object %u: %d\n",
191 argv0
, i
, input_type
);
196 printf("\nInput sections:\n");
197 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
199 Entry_reader
input_file(incremental_inputs
.input_file(i
));
201 if (input_file
.type() != INCREMENTAL_INPUT_OBJECT
202 && input_file
.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
205 const char* objname
= input_file
.filename();
208 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
213 printf("[%d] %s\n", i
, objname
);
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
)
220 typename
Entry_reader::Input_section_info
info(
221 input_file
.get_input_section(shndx
));
222 printf(" %3d %6d %8lld %8lld %s\n", shndx
,
224 static_cast<long long>(info
.sh_offset
),
225 static_cast<long long>(info
.sh_size
),
230 printf("\nGlobal symbols per input file:\n");
231 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
233 Entry_reader
input_file(incremental_inputs
.input_file(i
));
235 if (input_file
.type() != INCREMENTAL_INPUT_OBJECT
236 && input_file
.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
239 const char* objname
= input_file
.filename();
242 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
247 printf("[%d] %s\n", i
, objname
);
249 unsigned int nsyms
= input_file
.get_global_symbol_count();
251 printf(" %6s %8s %8s %8s %8s\n",
252 "outndx", "offset", "chain", "#relocs", "rbase");
253 for (unsigned int symndx
= 0; symndx
< nsyms
; ++symndx
)
255 typename
Entry_reader::Global_symbol_info
info(
256 input_file
.get_global_symbol_info(symndx
));
257 printf(" %6d %8d %8d %8d %8d\n",
259 input_file
.get_symbol_offset(symndx
),
266 // Get a view of the .symtab section.
268 unsigned int symtab_shndx
= elf_file
.find_section_by_type(elfcpp::SHT_SYMTAB
);
269 if (symtab_shndx
== elfcpp::SHN_UNDEF
) // Not found.
271 fprintf(stderr
, "%s: %s: no symbol table section\n", argv0
, filename
);
274 Location
symtab_location(elf_file
.section_contents(symtab_shndx
));
275 View
symtab_view(inc
->view(symtab_location
));
277 // Get a view of the .strtab section.
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
)
284 fprintf(stderr
, "%s: %s: no string table section\n", argv0
, filename
);
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
);
291 // Get a view of the .gnu_incremental_symtab section.
293 Location
isymtab_location(elf_file
.section_contents(isymtab_shndx
));
294 View
isymtab_view(inc
->view(isymtab_location
));
296 // Get a view of the .gnu_incremental_relocs section.
298 Location
irelocs_location(elf_file
.section_contents(irelocs_shndx
));
299 View
irelocs_view(inc
->view(irelocs_location
));
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();
316 Incremental_symtab_reader
<big_endian
> isymtab(isymtab_view
.data());
317 Incremental_relocs_reader
<size
, big_endian
> irelocs(irelocs_view
.data());
319 printf("\nGlobal symbol table:\n");
320 for (unsigned int i
= 0; i
< nglobals
; i
++)
322 elfcpp::Sym
<size
, big_endian
> sym(sym_p
);
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
);
330 unsigned int sym_ndx
;
331 Entry_reader input_file
=
332 find_input_containing_global
<size
, big_endian
>(incremental_inputs
,
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
);
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
++)
346 printf(" %4d relocation type %3d shndx %d"
347 " offset %016llx addend %016llx %s\n",
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
)),
354 r_off
+= irelocs
.reloc_size
;
356 offset
= sym_info
.next_offset
;
362 // Get a view of the .gnu_incremental_got_plt section.
364 Location
igot_plt_location(elf_file
.section_contents(igot_plt_shndx
));
365 View
igot_plt_view(inc
->view(igot_plt_location
));
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();
371 printf("\nGOT entries:\n");
372 for (unsigned int i
= 0; i
< ngot
; ++i
)
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)
379 else if (got_type
& 0x80)
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
);
387 sym_p
= symtab_view
.data() + got_desc
* sym_size
;
388 elfcpp::Sym
<size
, big_endian
> sym(sym_p
);
390 if (!strtab
.get_c_string(sym
.get_st_name(), &symname
))
391 symname
= "<unknown>";
392 printf("global %s (%d)", symname
, got_desc
);
397 printf("\nPLT entries:\n");
398 for (unsigned int i
= 0; i
< nplt
; ++i
)
400 unsigned int plt_desc
= igot_plt
.get_plt_desc(i
);
402 sym_p
= symtab_view
.data() + plt_desc
* sym_size
;
403 elfcpp::Sym
<size
, big_endian
> sym(sym_p
);
405 if (!strtab
.get_c_string(sym
.get_st_name(), &symname
))
406 symname
= "<unknown>";
407 printf("%s (%d)\n", symname
, plt_desc
);
410 printf("\nUnused archive symbols:\n");
411 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
413 Entry_reader
input_file(incremental_inputs
.input_file(i
));
415 if (input_file
.type() != INCREMENTAL_INPUT_ARCHIVE
)
418 const char* objname
= input_file
.filename();
421 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
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
));
435 main(int argc
, char** argv
)
439 fprintf(stderr
, "Usage: %s <file>\n", argv
[0]);
442 const char* filename
= argv
[1];
444 Output_file
* file
= new Output_file(filename
);
446 bool t
= file
->open_for_modification();
449 fprintf(stderr
, "%s: open_for_modification(%s): %s\n", argv
[0], filename
,
454 Incremental_binary
* inc
= open_incremental_binary(file
);
458 fprintf(stderr
, "%s: open_incremental_binary(%s): %s\n", argv
[0],
459 filename
, strerror(errno
));
463 switch (parameters
->size_and_endianness())
465 #ifdef HAVE_TARGET_32_LITTLE
466 case Parameters::TARGET_32_LITTLE
:
467 dump_incremental_inputs
<32, false>(argv
[0], filename
, inc
);
470 #ifdef HAVE_TARGET_32_BIG
471 case Parameters::TARGET_32_BIG
:
472 dump_incremental_inputs
<32, true>(argv
[0], filename
, inc
);
475 #ifdef HAVE_TARGET_64_LITTLE
476 case Parameters::TARGET_64_LITTLE
:
477 dump_incremental_inputs
<64, false>(argv
[0], filename
, inc
);
480 #ifdef HAVE_TARGET_64_BIG
481 case Parameters::TARGET_64_BIG
:
482 dump_incremental_inputs
<64, true>(argv
[0], filename
, inc
);