1 // inremental.cc -- incremental linking test/deubg tool
3 // Copyright 2009 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.
36 #include "incremental.h"
45 template<int size
, bool big_endian
>
47 dump_incremental_inputs(const char* argv0
,
48 const char* filename
, Incremental_binary
* inc
)
51 unsigned int strtab_shndx
;
52 Incremental_binary::Location location
;
54 t
= inc
->find_incremental_inputs_section(&location
, &strtab_shndx
);
57 fprintf(stderr
, "%s: %s: no .gnu_incremental_inputs section\n", argv0
,
62 Incremental_binary::View
inputs_view(inc
->view(location
));
63 const unsigned char* p
= inputs_view
.data();
65 Incremental_inputs_header
<size
, big_endian
> incremental_header(p
);
67 const unsigned char* incremental_inputs_base
=
68 (p
+ sizeof(Incremental_inputs_header_data
));
70 if (incremental_header
.get_version() != 1)
72 fprintf(stderr
, "%s: %s: unknown incremental version %d\n", argv0
,
73 filename
, incremental_header
.get_version());
77 elfcpp::Elf_file
<size
, big_endian
, Incremental_binary
> elf_file(inc
);
79 if (elf_file
.section_type(strtab_shndx
) != elfcpp::SHT_STRTAB
)
82 "%s: %s: invalid string table section %u (type %d != %d)\n",
83 argv0
, filename
, strtab_shndx
,
84 elf_file
.section_type(strtab_shndx
), elfcpp::SHT_STRTAB
);
88 Incremental_binary::Location
89 strtab_location(elf_file
.section_contents(strtab_shndx
));
91 Incremental_binary::View
strtab_view(inc
->view(strtab_location
));
92 p
= strtab_view
.data();
94 elfcpp::Elf_strtab
strtab(strtab_view
.data(), strtab_location
.data_size
);
95 const char* command_line
;
96 elfcpp::Elf_Word command_line_offset
=
97 incremental_header
.get_command_line_offset();
98 t
= strtab
.get_c_string(command_line_offset
, &command_line
);
103 "%s: %s: failed to get link command line: %zu out of range\n",
105 static_cast<size_t>(command_line_offset
));
109 printf("Link command line: %s\n", command_line
);
111 printf("Input files:\n");
112 for (unsigned i
= 0; i
< incremental_header
.get_input_file_count(); ++i
)
114 const unsigned char* input_p
= incremental_inputs_base
+
115 i
* sizeof(Incremental_inputs_entry_data
);
116 Incremental_inputs_entry
<size
, big_endian
> input(input_p
);
119 t
= strtab
.get_c_string(input
.get_filename_offset(), &objname
);
122 fprintf(stderr
,"%s: %s: failed to get file name for object %u:"
123 " %zu out of range\n", argv0
, filename
, i
,
124 static_cast<size_t>(input
.get_filename_offset()));
127 printf(" %s\n", objname
);
128 printf(" Timestamp sec = %llu\n",
129 static_cast<unsigned long long>(input
.get_timestamp_sec()));
130 printf(" Timestamp nsec = %d\n", input
.get_timestamp_nsec());
132 // TODO: print the data at input->data_offset once we have it.
133 elfcpp::Elf_Word input_type
= input
.get_input_type();
136 case INCREMENTAL_INPUT_OBJECT
:
139 case INCREMENTAL_INPUT_ARCHIVE
:
142 case INCREMENTAL_INPUT_SHARED_LIBRARY
:
143 printf("Shared library\n");
145 case INCREMENTAL_INPUT_SCRIPT
:
146 printf("Linker script\n");
147 if (input
.get_data_offset() != 0)
149 fprintf(stderr
,"%s: %s: %u is a script but offset is not zero",
154 case INCREMENTAL_INPUT_INVALID
:
156 fprintf(stderr
, "%s: invalid file type for object %u: %d\n",
157 argv0
, i
, input_type
);
164 main(int argc
, char** argv
)
168 fprintf(stderr
, "Usage: %s <file>\n", argv
[0]);
171 const char* filename
= argv
[1];
173 Output_file
* file
= new Output_file(filename
);
175 bool t
= file
->open_for_modification();
178 fprintf(stderr
, "%s: open_for_modification(%s): %s\n", argv
[0], filename
,
183 Incremental_binary
* inc
= open_incremental_binary(file
);
187 fprintf(stderr
, "%s: open_incremental_binary(%s): %s\n", argv
[0],
188 filename
, strerror(errno
));
192 switch (parameters
->size_and_endianness())
194 #ifdef HAVE_TARGET_32_LITTLE
195 case Parameters::TARGET_32_LITTLE
:
196 dump_incremental_inputs
<32, false>(argv
[0], filename
, inc
);
199 #ifdef HAVE_TARGET_32_BIG
200 case Parameters::TARGET_32_BIG
:
201 dump_incremental_inputs
<32, true>(argv
[0], filename
, inc
);
204 #ifdef HAVE_TARGET_64_LITTLE
205 case Parameters::TARGET_64_LITTLE
:
206 dump_incremental_inputs
<64, false>(argv
[0], filename
, inc
);
209 #ifdef HAVE_TARGET_64_BIG
210 case Parameters::TARGET_64_BIG
:
211 dump_incremental_inputs
<64, true>(argv
[0], filename
, inc
);