1 // inremental.cc -- incremental linking support for gold
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.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.
26 #include "incremental.h"
30 using elfcpp::Convert
;
34 // Version information. Will change frequently during the development, later
35 // we could think about backward (and forward?) compatibility.
36 const int INCREMENTAL_LINK_VERSION
= 1;
40 // Header of the .gnu_incremental_input section.
41 struct Incremental_inputs_header_data
43 // Incremental linker version.
44 elfcpp::Elf_Word version
;
46 // Numer of input files in the link.
47 elfcpp::Elf_Word input_file_count
;
49 // Offset of command line options in .gnu_incremental_strtab.
50 elfcpp::Elf_Word command_line_offset
;
53 elfcpp::Elf_Word reserved
;
56 // Data stored in .gnu_incremental_input after the header for each of the
57 // Incremental_input_header_data::input_file_count input entries.
58 struct Incremental_inputs_entry_data
60 // Offset of file name in .gnu_incremental_strtab section.
61 elfcpp::Elf_Word filename_offset
;
63 // Offset of data in .gnu_incremental_input.
64 elfcpp::Elf_Word data_offset
;
66 // Timestamp (in seconds).
67 elfcpp::Elf_Xword timestamp_sec
;
69 // Nano-second part of timestamp (if supported).
70 elfcpp::Elf_Word timestamp_nsec
;
72 // Type of the input entry.
73 elfcpp::Elf_Half input_type
;
76 elfcpp::Elf_Half reserved
;
83 // See internal::Incremental_input_header for fields descriptions.
84 template<int size
, bool big_endian
>
85 class Incremental_inputs_header_write
88 Incremental_inputs_header_write(unsigned char *p
)
89 : p_(reinterpret_cast<internal::Incremental_inputs_header_data
*>(p
))
92 static const int data_size
= sizeof(internal::Incremental_inputs_header_data
);
95 put_version(elfcpp::Elf_Word v
)
96 { this->p_
->version
= Convert
<32, big_endian
>::convert_host(v
); }
99 put_input_file_count(elfcpp::Elf_Word v
)
100 { this->p_
->input_file_count
= Convert
<32, big_endian
>::convert_host(v
); }
103 put_command_line_offset(elfcpp::Elf_Word v
)
104 { this->p_
->command_line_offset
= Convert
<32, big_endian
>::convert_host(v
); }
107 put_reserved(elfcpp::Elf_Word v
)
108 { this->p_
->reserved
= Convert
<32, big_endian
>::convert_host(v
); }
111 internal::Incremental_inputs_header_data
* p_
;
114 // See internal::Incremental_input_entry for fields descriptions.
115 template<int size
, bool big_endian
>
116 class Incremental_inputs_entry_write
119 Incremental_inputs_entry_write(unsigned char *p
)
120 : p_(reinterpret_cast<internal::Incremental_inputs_entry_data
*>(p
))
123 static const int data_size
= sizeof(internal::Incremental_inputs_entry_data
);
126 put_filename_offset(elfcpp::Elf_Word v
)
127 { this->p_
->filename_offset
= Convert
<32, big_endian
>::convert_host(v
); }
130 put_data_offset(elfcpp::Elf_Word v
)
131 { this->p_
->data_offset
= Convert
<32, big_endian
>::convert_host(v
); }
134 put_timestamp_sec(elfcpp::Elf_Xword v
)
135 { this->p_
->timestamp_sec
= Convert
<64, big_endian
>::convert_host(v
); }
138 put_timestamp_nsec(elfcpp::Elf_Word v
)
139 { this->p_
->timestamp_nsec
= Convert
<32, big_endian
>::convert_host(v
); }
142 put_input_type(elfcpp::Elf_Word v
)
143 { this->p_
->input_type
= Convert
<32, big_endian
>::convert_host(v
); }
146 put_reserved(elfcpp::Elf_Word v
)
147 { this->p_
->reserved
= Convert
<32, big_endian
>::convert_host(v
); }
150 internal::Incremental_inputs_entry_data
* p_
;
153 // Analyzes the output file to check if incremental linking is possible and
154 // (to be done) what files need to be relinked.
157 Incremental_checker::can_incrementally_link_output_file()
159 Output_file
output(this->output_name_
);
160 if (!output
.open_for_modification())
165 // Add the command line to the string table, setting
166 // command_line_key_. In incremental builds, the command line is
167 // stored in .gnu_incremental_inputs so that the next linker run can
168 // check if the command line options didn't change.
171 Incremental_inputs::report_command_line(int argc
, const char* const* argv
)
173 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
174 // different path to the linker.
175 std::string
args("gold");
176 // Copied from collect_argv in main.cc.
177 for (int i
= 1; i
< argc
; ++i
)
179 // Adding/removing these options should result in a full relink.
180 if (strcmp(argv
[i
], "--incremental-changed") == 0
181 || strcmp(argv
[i
], "--incremental-unchanged") == 0
182 || strcmp(argv
[i
], "--incremental-unknown") == 0)
186 // Now append argv[i], but with all single-quotes escaped
187 const char* argpos
= argv
[i
];
190 const int len
= strcspn(argpos
, "'");
191 args
.append(argpos
, len
);
192 if (argpos
[len
] == '\0')
194 args
.append("'\"'\"'");
199 this->strtab_
->add(args
.c_str(), true, &this->command_line_key_
);
202 // Record that the input argument INPUT is an achive ARCHIVE. This is
203 // called by Read_symbols after finding out the type of the file.
206 Incremental_inputs::report_archive(const Input_argument
* input
,
209 Hold_lock
hl(*this->lock_
);
212 info
.type
= INCREMENTAL_INPUT_ARCHIVE
;
213 info
.archive
= archive
;
214 info
.mtime
= archive
->file().get_mtime();
215 this->inputs_map_
.insert(std::make_pair(input
, info
));
218 // Record that the input argument INPUT is an object OBJ. This is
219 // called by Read_symbols after finding out the type of the file.
222 Incremental_inputs::report_object(const Input_argument
* input
,
225 Hold_lock
hl(*this->lock_
);
228 info
.type
= (obj
->is_dynamic()
229 ? INCREMENTAL_INPUT_SHARED_LIBRARY
230 : INCREMENTAL_INPUT_OBJECT
);
232 info
.mtime
= obj
->input_file()->file().get_mtime();
233 this->inputs_map_
.insert(std::make_pair(input
, info
));
236 // Record that the input argument INPUT is an script SCRIPT. This is
237 // called by read_script after parsing the script and reading the list
238 // of inputs added by this script.
241 Incremental_inputs::report_script(const Input_argument
* input
,
245 Hold_lock
hl(*this->lock_
);
248 info
.type
= INCREMENTAL_INPUT_SCRIPT
;
249 info
.script
= script
;
251 this->inputs_map_
.insert(std::make_pair(input
, info
));
254 // Compute indexes in the order in which the inputs should appear in
255 // .gnu_incremental_inputs. This needs to be done after all the
256 // scripts are parsed. The function is first called for the command
257 // line inputs arguments and may call itself recursively for e.g. a
258 // list of elements of a group or a list of inputs added by a script.
259 // The [BEGIN; END) interval to analyze and *INDEX is the current
260 // value of the index (that will be updated).
263 Incremental_inputs::finalize_inputs(
264 Input_argument_list::const_iterator begin
,
265 Input_argument_list::const_iterator end
,
268 for (Input_argument_list::const_iterator p
= begin
; p
!= end
; ++p
)
272 finalize_inputs(p
->group()->begin(), p
->group()->end(), index
);
276 Inputs_info_map::iterator it
= this->inputs_map_
.find(&(*p
));
277 // TODO: turn it into an assert when the code will be more stable.
278 if (it
== this->inputs_map_
.end())
280 gold_error("internal error: %s: incremental build info not provided",
281 (p
->is_file() ? p
->file().name() : "[group]"));
284 Input_info
* info
= &it
->second
;
285 info
->index
= *index
;
287 this->strtab_
->add(p
->file().name(), false, &info
->filename_key
);
288 if (info
->type
== INCREMENTAL_INPUT_SCRIPT
)
290 finalize_inputs(info
->script
->inputs()->begin(),
291 info
->script
->inputs()->end(),
297 // Finalize the incremental link information. Called from
301 Incremental_inputs::finalize()
303 unsigned int index
= 0;
304 finalize_inputs(this->inputs_
->begin(), this->inputs_
->end(), &index
);
307 for (Inputs_info_map::const_iterator p
= this->inputs_map_
.begin();
308 p
!= this->inputs_map_
.end();
311 gold_assert(p
->second
.filename_key
!= 0);
314 this->strtab_
->set_string_offsets();
317 // Create the content of the .gnu_incremental_inputs section.
320 Incremental_inputs::create_incremental_inputs_section_data()
322 switch (parameters
->size_and_endianness())
324 #ifdef HAVE_TARGET_32_LITTLE
325 case Parameters::TARGET_32_LITTLE
:
326 return this->sized_create_inputs_section_data
<32, false>();
328 #ifdef HAVE_TARGET_32_BIG
329 case Parameters::TARGET_32_BIG
:
330 return this->sized_create_inputs_section_data
<32, true>();
332 #ifdef HAVE_TARGET_64_LITTLE
333 case Parameters::TARGET_64_LITTLE
:
334 return this->sized_create_inputs_section_data
<64, false>();
336 #ifdef HAVE_TARGET_64_BIG
337 case Parameters::TARGET_64_BIG
:
338 return this->sized_create_inputs_section_data
<64, true>();
345 // Sized creation of .gnu_incremental_inputs section.
347 template<int size
, bool big_endian
>
349 Incremental_inputs::sized_create_inputs_section_data()
351 const int entry_size
=
352 Incremental_inputs_entry_write
<size
, big_endian
>::data_size
;
353 const int header_size
=
354 Incremental_inputs_header_write
<size
, big_endian
>::data_size
;
356 unsigned int sz
= header_size
+ entry_size
* this->inputs_map_
.size();
357 unsigned char* buffer
= new unsigned char[sz
];
358 unsigned char* inputs_base
= buffer
+ header_size
;
360 Incremental_inputs_header_write
<size
, big_endian
> header_writer(buffer
);
361 gold_assert(this->command_line_key_
> 0);
362 int cmd_offset
= this->strtab_
->get_offset_from_key(this->command_line_key_
);
364 header_writer
.put_version(INCREMENTAL_LINK_VERSION
);
365 header_writer
.put_input_file_count(this->inputs_map_
.size());
366 header_writer
.put_command_line_offset(cmd_offset
);
367 header_writer
.put_reserved(0);
369 for (Inputs_info_map::const_iterator it
= this->inputs_map_
.begin();
370 it
!= this->inputs_map_
.end();
373 gold_assert(it
->second
.index
< this->inputs_map_
.size());
375 unsigned char* entry_buffer
=
376 inputs_base
+ it
->second
.index
* entry_size
;
377 Incremental_inputs_entry_write
<size
, big_endian
> entry(entry_buffer
);
378 int filename_offset
=
379 this->strtab_
->get_offset_from_key(it
->second
.filename_key
);
380 entry
.put_filename_offset(filename_offset
);
381 // TODO: add per input data and timestamp. Currently we store
382 // an out-of-bounds offset for future version of gold to reject
383 // such an incremental_inputs section.
384 entry
.put_data_offset(0xffffffff);
385 entry
.put_timestamp_sec(it
->second
.mtime
.seconds
);
386 entry
.put_timestamp_nsec(it
->second
.mtime
.nanoseconds
);
387 entry
.put_input_type(it
->second
.type
);
388 entry
.put_reserved(0);
391 return new Output_data_const_buffer(buffer
, sz
, 8,
392 "** incremental link inputs list");
395 } // End namespace gold.