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 istrtab_shndx
;
83 typedef Incremental_binary::Location Location
;
84 typedef Incremental_binary::View View
;
85 typedef Incremental_inputs_reader
<size
, big_endian
> Inputs_reader
;
86 typedef typename
Inputs_reader::Incremental_input_entry_reader Entry_reader
;
88 // Find the .gnu_incremental_inputs, _symtab, _relocs, and _strtab sections.
90 t
= inc
->find_incremental_inputs_sections(&inputs_shndx
, &isymtab_shndx
,
91 &irelocs_shndx
, &istrtab_shndx
);
94 fprintf(stderr
, "%s: %s: no .gnu_incremental_inputs section\n", argv0
,
99 elfcpp::Elf_file
<size
, big_endian
, Incremental_binary
> elf_file(inc
);
101 // Get a view of the .gnu_incremental_inputs section.
103 Location
inputs_location(elf_file
.section_contents(inputs_shndx
));
104 View
inputs_view(inc
->view(inputs_location
));
106 // Get the .gnu_incremental_strtab section as a string table.
108 Location
istrtab_location(elf_file
.section_contents(istrtab_shndx
));
109 View
istrtab_view(inc
->view(istrtab_location
));
110 elfcpp::Elf_strtab
istrtab(istrtab_view
.data(), istrtab_location
.data_size
);
112 // Create a reader object for the .gnu_incremental_inputs section.
114 Incremental_inputs_reader
<size
, big_endian
>
115 incremental_inputs(inputs_view
.data(), istrtab
);
117 if (incremental_inputs
.version() != 1)
119 fprintf(stderr
, "%s: %s: unknown incremental version %d\n", argv0
,
120 filename
, incremental_inputs
.version());
124 const char* command_line
= incremental_inputs
.command_line();
125 if (command_line
== NULL
)
128 "%s: %s: failed to get link command line\n",
132 printf("Link command line: %s\n", command_line
);
134 printf("\nInput files:\n");
135 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
137 typedef Incremental_inputs_reader
<size
, big_endian
> Inputs_reader
;
138 typename
Inputs_reader::Incremental_input_entry_reader input_file
=
139 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
:
163 printf(" Input section count: %d\n",
164 input_file
.get_input_section_count());
165 printf(" Symbol count: %d\n",
166 input_file
.get_global_symbol_count());
169 case INCREMENTAL_INPUT_ARCHIVE_MEMBER
:
171 printf("Archive member\n");
172 printf(" Input section count: %d\n",
173 input_file
.get_input_section_count());
174 printf(" Symbol count: %d\n",
175 input_file
.get_global_symbol_count());
178 case INCREMENTAL_INPUT_ARCHIVE
:
181 printf(" Member count: %d\n", input_file
.get_member_count());
182 printf(" Unused symbol count: %d\n",
183 input_file
.get_unused_symbol_count());
186 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
188 printf("Shared library\n");
189 printf(" Symbol count: %d\n",
190 input_file
.get_global_symbol_count());
193 case INCREMENTAL_INPUT_SCRIPT
:
194 printf("Linker script\n");
197 fprintf(stderr
, "%s: invalid file type for object %u: %d\n",
198 argv0
, i
, input_type
);
203 printf("\nInput sections:\n");
204 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
206 typedef Incremental_inputs_reader
<size
, big_endian
> Inputs_reader
;
207 typedef typename
Inputs_reader::Incremental_input_entry_reader
210 Entry_reader
input_file(incremental_inputs
.input_file(i
));
212 if (input_file
.type() != INCREMENTAL_INPUT_OBJECT
213 && input_file
.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
216 const char* objname
= input_file
.filename();
219 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
224 printf("[%d] %s\n", i
, objname
);
226 printf(" %3s %6s %8s %8s %s\n",
227 "n", "outndx", "offset", "size", "name");
228 unsigned int nsections
= input_file
.get_input_section_count();
229 for (unsigned int shndx
= 0; shndx
< nsections
; ++shndx
)
231 typename
Entry_reader::Input_section_info
info(
232 input_file
.get_input_section(shndx
));
233 printf(" %3d %6d %8lld %8lld %s\n", shndx
,
235 static_cast<long long>(info
.sh_offset
),
236 static_cast<long long>(info
.sh_size
),
241 printf("\nGlobal symbols per input file:\n");
242 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
244 typedef Incremental_inputs_reader
<size
, big_endian
> Inputs_reader
;
245 typedef typename
Inputs_reader::Incremental_input_entry_reader
248 Entry_reader
input_file(incremental_inputs
.input_file(i
));
250 if (input_file
.type() != INCREMENTAL_INPUT_OBJECT
251 && input_file
.type() != INCREMENTAL_INPUT_ARCHIVE_MEMBER
)
254 const char* objname
= input_file
.filename();
257 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
262 printf("[%d] %s\n", i
, objname
);
264 unsigned int nsyms
= input_file
.get_global_symbol_count();
266 printf(" %6s %8s %8s %8s %8s\n",
267 "outndx", "offset", "chain", "#relocs", "rbase");
268 for (unsigned int symndx
= 0; symndx
< nsyms
; ++symndx
)
270 typename
Entry_reader::Global_symbol_info
info(
271 input_file
.get_global_symbol_info(symndx
));
272 printf(" %6d %8d %8d %8d %8d\n",
274 input_file
.get_symbol_offset(symndx
),
281 // Get a view of the .symtab section.
283 unsigned int symtab_shndx
= elf_file
.find_section_by_type(elfcpp::SHT_SYMTAB
);
284 if (symtab_shndx
== elfcpp::SHN_UNDEF
) // Not found.
286 fprintf(stderr
, "%s: %s: no symbol table section\n", argv0
, filename
);
289 Location
symtab_location(elf_file
.section_contents(symtab_shndx
));
290 View
symtab_view(inc
->view(symtab_location
));
292 // Get a view of the .strtab section.
294 unsigned int strtab_shndx
= elf_file
.section_link(symtab_shndx
);
295 if (strtab_shndx
== elfcpp::SHN_UNDEF
296 || strtab_shndx
> elf_file
.shnum()
297 || elf_file
.section_type(strtab_shndx
) != elfcpp::SHT_STRTAB
)
299 fprintf(stderr
, "%s: %s: no string table section\n", argv0
, filename
);
302 Location
strtab_location(elf_file
.section_contents(strtab_shndx
));
303 View
strtab_view(inc
->view(strtab_location
));
304 elfcpp::Elf_strtab
strtab(strtab_view
.data(), strtab_location
.data_size
);
306 // Get a view of the .gnu_incremental_symtab section.
308 Location
isymtab_location(elf_file
.section_contents(isymtab_shndx
));
309 View
isymtab_view(inc
->view(isymtab_location
));
311 // Get a view of the .gnu_incremental_relocs section.
313 Location
irelocs_location(elf_file
.section_contents(irelocs_shndx
));
314 View
irelocs_view(inc
->view(irelocs_location
));
316 // The .gnu_incremental_symtab section contains entries that parallel
317 // the global symbols of the main symbol table. The sh_info field
318 // of the main symbol table's section header tells us how many global
319 // symbols there are, but that count does not include any global
320 // symbols that were forced local during the link. Therefore, we
321 // use the size of the .gnu_incremental_symtab section to deduce
322 // the number of global symbols + forced-local symbols there are
323 // in the symbol table.
324 unsigned int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
325 unsigned int nsyms
= symtab_location
.data_size
/ sym_size
;
326 unsigned int nglobals
= isymtab_location
.data_size
/ 4;
327 unsigned int first_global
= nsyms
- nglobals
;
328 unsigned const char* sym_p
= symtab_view
.data() + first_global
* sym_size
;
329 unsigned const char* isym_p
= isymtab_view
.data();
331 Incremental_symtab_reader
<big_endian
> isymtab(isymtab_view
.data());
332 Incremental_relocs_reader
<size
, big_endian
> irelocs(irelocs_view
.data());
334 printf("\nGlobal symbol table:\n");
335 for (unsigned int i
= 0; i
< nglobals
; i
++)
337 elfcpp::Sym
<size
, big_endian
> sym(sym_p
);
339 if (!strtab
.get_c_string(sym
.get_st_name(), &symname
))
340 symname
= "<unknown>";
341 printf("[%d] %s\n", first_global
+ i
, symname
);
342 unsigned int offset
= isymtab
.get_list_head(i
);
345 unsigned int sym_ndx
;
346 Entry_reader input_file
=
347 find_input_containing_global
<size
, big_endian
>(incremental_inputs
,
349 typename
Entry_reader::Global_symbol_info
sym_info(
350 input_file
.get_global_symbol_info(sym_ndx
));
351 printf(" %s (first reloc: %d, reloc count: %d)",
352 input_file
.filename(), sym_info
.reloc_offset
,
353 sym_info
.reloc_count
);
354 if (sym_info
.output_symndx
!= first_global
+ i
)
355 printf(" ** wrong output symndx (%d) **", sym_info
.output_symndx
);
357 // Dump the relocations from this input file for this symbol.
358 unsigned int r_off
= sym_info
.reloc_offset
;
359 for (unsigned int j
= 0; j
< sym_info
.reloc_count
; j
++)
361 printf(" %4d relocation type %3d shndx %d"
362 " offset %016llx addend %016llx %s\n",
364 irelocs
.get_r_type(r_off
),
365 irelocs
.get_r_shndx(r_off
),
366 static_cast<long long>(irelocs
.get_r_offset(r_off
)),
367 static_cast<long long>(irelocs
.get_r_addend(r_off
)),
369 r_off
+= irelocs
.reloc_size
;
371 offset
= sym_info
.next_offset
;
377 printf("\nUnused archive symbols:\n");
378 for (unsigned int i
= 0; i
< incremental_inputs
.input_file_count(); ++i
)
380 Entry_reader
input_file(incremental_inputs
.input_file(i
));
382 if (input_file
.type() != INCREMENTAL_INPUT_ARCHIVE
)
385 const char* objname
= input_file
.filename();
388 fprintf(stderr
,"%s: %s: failed to get file name for object %u\n",
393 printf("[%d] %s\n", i
, objname
);
394 unsigned int nsyms
= input_file
.get_unused_symbol_count();
395 for (unsigned int symndx
= 0; symndx
< nsyms
; ++symndx
)
396 printf(" %s\n", input_file
.get_unused_symbol(symndx
));
402 main(int argc
, char** argv
)
406 fprintf(stderr
, "Usage: %s <file>\n", argv
[0]);
409 const char* filename
= argv
[1];
411 Output_file
* file
= new Output_file(filename
);
413 bool t
= file
->open_for_modification();
416 fprintf(stderr
, "%s: open_for_modification(%s): %s\n", argv
[0], filename
,
421 Incremental_binary
* inc
= open_incremental_binary(file
);
425 fprintf(stderr
, "%s: open_incremental_binary(%s): %s\n", argv
[0],
426 filename
, strerror(errno
));
430 switch (parameters
->size_and_endianness())
432 #ifdef HAVE_TARGET_32_LITTLE
433 case Parameters::TARGET_32_LITTLE
:
434 dump_incremental_inputs
<32, false>(argv
[0], filename
, inc
);
437 #ifdef HAVE_TARGET_32_BIG
438 case Parameters::TARGET_32_BIG
:
439 dump_incremental_inputs
<32, true>(argv
[0], filename
, inc
);
442 #ifdef HAVE_TARGET_64_LITTLE
443 case Parameters::TARGET_64_LITTLE
:
444 dump_incremental_inputs
<64, false>(argv
[0], filename
, inc
);
447 #ifdef HAVE_TARGET_64_BIG
448 case Parameters::TARGET_64_BIG
:
449 dump_incremental_inputs
<64, true>(argv
[0], filename
, inc
);