Commit | Line | Data |
---|---|---|
0e879927 ILT |
1 | // inremental.cc -- incremental linking support for gold |
2 | ||
3 | // Copyright 2009 Free Software Foundation, Inc. | |
4 | // Written by Mikolaj Zalewski <mikolajz@google.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 | #include "gold.h" | |
24 | #include "elfcpp.h" | |
3ce2c28e ILT |
25 | #include "output.h" |
26 | #include "incremental.h" | |
98fa85cb | 27 | #include "archive.h" |
44453f85 | 28 | #include "output.h" |
0e879927 ILT |
29 | |
30 | using elfcpp::Convert; | |
31 | ||
32 | namespace gold { | |
33 | ||
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; | |
37 | ||
38 | namespace internal { | |
39 | ||
40 | // Header of the .gnu_incremental_input section. | |
3ce2c28e | 41 | struct Incremental_inputs_header_data |
0e879927 ILT |
42 | { |
43 | // Incremental linker version. | |
44 | elfcpp::Elf_Word version; | |
45 | ||
46 | // Numer of input files in the link. | |
47 | elfcpp::Elf_Word input_file_count; | |
48 | ||
49 | // Offset of command line options in .gnu_incremental_strtab. | |
50 | elfcpp::Elf_Word command_line_offset; | |
51 | ||
52 | // Padding. | |
53 | elfcpp::Elf_Word reserved; | |
54 | }; | |
55 | ||
56 | // Data stored in .gnu_incremental_input after the header for each of the | |
57 | // Incremental_input_header_data::input_file_count input entries. | |
3ce2c28e | 58 | struct Incremental_inputs_entry_data |
0e879927 ILT |
59 | { |
60 | // Offset of file name in .gnu_incremental_strtab section. | |
61 | elfcpp::Elf_Word filename_offset; | |
62 | ||
63 | // Offset of data in .gnu_incremental_input. | |
64 | elfcpp::Elf_Word data_offset; | |
65 | ||
66 | // Timestamp (in seconds). | |
67 | elfcpp::Elf_Xword timestamp_sec; | |
68 | ||
69 | // Nano-second part of timestamp (if supported). | |
98fa85cb | 70 | elfcpp::Elf_Word timestamp_nsec; |
0e879927 ILT |
71 | |
72 | // Type of the input entry. | |
73 | elfcpp::Elf_Half input_type; | |
74 | ||
75 | // Padding. | |
76 | elfcpp::Elf_Half reserved; | |
77 | }; | |
78 | ||
79 | } | |
80 | ||
81 | // Accessors. | |
82 | ||
83 | // See internal::Incremental_input_header for fields descriptions. | |
84 | template<int size, bool big_endian> | |
3ce2c28e | 85 | class Incremental_inputs_header_write |
0e879927 ILT |
86 | { |
87 | public: | |
3ce2c28e ILT |
88 | Incremental_inputs_header_write(unsigned char *p) |
89 | : p_(reinterpret_cast<internal::Incremental_inputs_header_data*>(p)) | |
0e879927 | 90 | { } |
072fe7ce | 91 | |
3ce2c28e | 92 | static const int data_size = sizeof(internal::Incremental_inputs_header_data); |
0e879927 ILT |
93 | |
94 | void | |
95 | put_version(elfcpp::Elf_Word v) | |
96 | { this->p_->version = Convert<32, big_endian>::convert_host(v); } | |
97 | ||
98 | void | |
3ce2c28e | 99 | put_input_file_count(elfcpp::Elf_Word v) |
0e879927 ILT |
100 | { this->p_->input_file_count = Convert<32, big_endian>::convert_host(v); } |
101 | ||
102 | void | |
3ce2c28e | 103 | put_command_line_offset(elfcpp::Elf_Word v) |
0e879927 ILT |
104 | { this->p_->command_line_offset = Convert<32, big_endian>::convert_host(v); } |
105 | ||
106 | void | |
3ce2c28e | 107 | put_reserved(elfcpp::Elf_Word v) |
0e879927 ILT |
108 | { this->p_->reserved = Convert<32, big_endian>::convert_host(v); } |
109 | ||
110 | private: | |
3ce2c28e | 111 | internal::Incremental_inputs_header_data* p_; |
0e879927 ILT |
112 | }; |
113 | ||
114 | // See internal::Incremental_input_entry for fields descriptions. | |
115 | template<int size, bool big_endian> | |
3ce2c28e | 116 | class Incremental_inputs_entry_write |
0e879927 ILT |
117 | { |
118 | public: | |
3ce2c28e ILT |
119 | Incremental_inputs_entry_write(unsigned char *p) |
120 | : p_(reinterpret_cast<internal::Incremental_inputs_entry_data*>(p)) | |
0e879927 ILT |
121 | { } |
122 | ||
3ce2c28e ILT |
123 | static const int data_size = sizeof(internal::Incremental_inputs_entry_data); |
124 | ||
0e879927 ILT |
125 | void |
126 | put_filename_offset(elfcpp::Elf_Word v) | |
127 | { this->p_->filename_offset = Convert<32, big_endian>::convert_host(v); } | |
128 | ||
129 | void | |
130 | put_data_offset(elfcpp::Elf_Word v) | |
131 | { this->p_->data_offset = Convert<32, big_endian>::convert_host(v); } | |
132 | ||
133 | void | |
98fa85cb ILT |
134 | put_timestamp_sec(elfcpp::Elf_Xword v) |
135 | { this->p_->timestamp_sec = Convert<64, big_endian>::convert_host(v); } | |
0e879927 ILT |
136 | |
137 | void | |
98fa85cb ILT |
138 | put_timestamp_nsec(elfcpp::Elf_Word v) |
139 | { this->p_->timestamp_nsec = Convert<32, big_endian>::convert_host(v); } | |
0e879927 ILT |
140 | |
141 | void | |
142 | put_input_type(elfcpp::Elf_Word v) | |
143 | { this->p_->input_type = Convert<32, big_endian>::convert_host(v); } | |
144 | ||
145 | void | |
146 | put_reserved(elfcpp::Elf_Word v) | |
147 | { this->p_->reserved = Convert<32, big_endian>::convert_host(v); } | |
148 | ||
149 | private: | |
3ce2c28e | 150 | internal::Incremental_inputs_entry_data* p_; |
0e879927 ILT |
151 | }; |
152 | ||
44453f85 ILT |
153 | // Analyzes the output file to check if incremental linking is possible and |
154 | // (to be done) what files need to be relinked. | |
155 | ||
156 | bool | |
157 | Incremental_checker::can_incrementally_link_output_file() | |
158 | { | |
159 | Output_file output(this->output_name_); | |
160 | if (!output.open_for_modification()) | |
161 | return false; | |
162 | return true; | |
163 | } | |
164 | ||
3ce2c28e ILT |
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. | |
169 | ||
170 | void | |
171 | Incremental_inputs::report_command_line(int argc, const char* const* argv) | |
172 | { | |
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) | |
178 | { | |
b19b0c6d ILT |
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) | |
183 | continue; | |
184 | ||
3ce2c28e ILT |
185 | args.append(" '"); |
186 | // Now append argv[i], but with all single-quotes escaped | |
187 | const char* argpos = argv[i]; | |
188 | while (1) | |
189 | { | |
190 | const int len = strcspn(argpos, "'"); | |
191 | args.append(argpos, len); | |
192 | if (argpos[len] == '\0') | |
193 | break; | |
194 | args.append("'\"'\"'"); | |
195 | argpos += len + 1; | |
196 | } | |
197 | args.append("'"); | |
198 | } | |
199 | this->strtab_->add(args.c_str(), true, &this->command_line_key_); | |
200 | } | |
201 | ||
072fe7ce ILT |
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. | |
204 | ||
205 | void | |
206 | Incremental_inputs::report_archive(const Input_argument* input, | |
207 | Archive* archive) | |
208 | { | |
209 | Hold_lock hl(*this->lock_); | |
210 | ||
211 | Input_info info; | |
212 | info.type = INCREMENTAL_INPUT_ARCHIVE; | |
213 | info.archive = archive; | |
98fa85cb ILT |
214 | info.mtime = archive->file().get_mtime(); |
215 | this->inputs_map_.insert(std::make_pair(input, info)); | |
072fe7ce ILT |
216 | } |
217 | ||
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. | |
220 | ||
221 | void | |
222 | Incremental_inputs::report_object(const Input_argument* input, | |
223 | Object* obj) | |
224 | { | |
225 | Hold_lock hl(*this->lock_); | |
226 | ||
227 | Input_info info; | |
228 | info.type = (obj->is_dynamic() | |
229 | ? INCREMENTAL_INPUT_SHARED_LIBRARY | |
230 | : INCREMENTAL_INPUT_OBJECT); | |
231 | info.object = obj; | |
98fa85cb ILT |
232 | info.mtime = obj->input_file()->file().get_mtime(); |
233 | this->inputs_map_.insert(std::make_pair(input, info)); | |
072fe7ce ILT |
234 | } |
235 | ||
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. | |
239 | ||
240 | void | |
241 | Incremental_inputs::report_script(const Input_argument* input, | |
98fa85cb | 242 | Timespec mtime, |
072fe7ce ILT |
243 | Script_info* script) |
244 | { | |
245 | Hold_lock hl(*this->lock_); | |
246 | ||
247 | Input_info info; | |
248 | info.type = INCREMENTAL_INPUT_SCRIPT; | |
249 | info.script = script; | |
98fa85cb ILT |
250 | info.mtime = mtime; |
251 | this->inputs_map_.insert(std::make_pair(input, info)); | |
072fe7ce ILT |
252 | } |
253 | ||
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). | |
261 | ||
262 | void | |
263 | Incremental_inputs::finalize_inputs( | |
264 | Input_argument_list::const_iterator begin, | |
265 | Input_argument_list::const_iterator end, | |
266 | unsigned int* index) | |
267 | { | |
268 | for (Input_argument_list::const_iterator p = begin; p != end; ++p) | |
269 | { | |
270 | if (p->is_group()) | |
271 | { | |
272 | finalize_inputs(p->group()->begin(), p->group()->end(), index); | |
273 | continue; | |
274 | } | |
275 | ||
98fa85cb | 276 | Inputs_info_map::iterator it = this->inputs_map_.find(&(*p)); |
072fe7ce | 277 | // TODO: turn it into an assert when the code will be more stable. |
98fa85cb | 278 | if (it == this->inputs_map_.end()) |
072fe7ce ILT |
279 | { |
280 | gold_error("internal error: %s: incremental build info not provided", | |
281 | (p->is_file() ? p->file().name() : "[group]")); | |
282 | continue; | |
283 | } | |
284 | Input_info* info = &it->second; | |
285 | info->index = *index; | |
286 | (*index)++; | |
287 | this->strtab_->add(p->file().name(), false, &info->filename_key); | |
288 | if (info->type == INCREMENTAL_INPUT_SCRIPT) | |
289 | { | |
290 | finalize_inputs(info->script->inputs()->begin(), | |
291 | info->script->inputs()->end(), | |
292 | index); | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
3ce2c28e ILT |
297 | // Finalize the incremental link information. Called from |
298 | // Layout::finalize. | |
299 | ||
300 | void | |
301 | Incremental_inputs::finalize() | |
302 | { | |
072fe7ce ILT |
303 | unsigned int index = 0; |
304 | finalize_inputs(this->inputs_->begin(), this->inputs_->end(), &index); | |
305 | ||
306 | // Sanity check. | |
98fa85cb ILT |
307 | for (Inputs_info_map::const_iterator p = this->inputs_map_.begin(); |
308 | p != this->inputs_map_.end(); | |
072fe7ce ILT |
309 | ++p) |
310 | { | |
311 | gold_assert(p->second.filename_key != 0); | |
312 | } | |
313 | ||
3ce2c28e ILT |
314 | this->strtab_->set_string_offsets(); |
315 | } | |
316 | ||
317 | // Create the content of the .gnu_incremental_inputs section. | |
318 | ||
319 | Output_section_data* | |
320 | Incremental_inputs::create_incremental_inputs_section_data() | |
321 | { | |
322 | switch (parameters->size_and_endianness()) | |
323 | { | |
324 | #ifdef HAVE_TARGET_32_LITTLE | |
325 | case Parameters::TARGET_32_LITTLE: | |
326 | return this->sized_create_inputs_section_data<32, false>(); | |
327 | #endif | |
328 | #ifdef HAVE_TARGET_32_BIG | |
329 | case Parameters::TARGET_32_BIG: | |
330 | return this->sized_create_inputs_section_data<32, true>(); | |
331 | #endif | |
332 | #ifdef HAVE_TARGET_64_LITTLE | |
333 | case Parameters::TARGET_64_LITTLE: | |
334 | return this->sized_create_inputs_section_data<64, false>(); | |
335 | #endif | |
336 | #ifdef HAVE_TARGET_64_BIG | |
337 | case Parameters::TARGET_64_BIG: | |
338 | return this->sized_create_inputs_section_data<64, true>(); | |
339 | #endif | |
340 | default: | |
341 | gold_unreachable(); | |
072fe7ce | 342 | } |
3ce2c28e ILT |
343 | } |
344 | ||
345 | // Sized creation of .gnu_incremental_inputs section. | |
346 | ||
347 | template<int size, bool big_endian> | |
348 | Output_section_data* | |
349 | Incremental_inputs::sized_create_inputs_section_data() | |
072fe7ce ILT |
350 | { |
351 | const int entry_size = | |
352 | Incremental_inputs_entry_write<size, big_endian>::data_size; | |
353 | const int header_size = | |
3ce2c28e | 354 | Incremental_inputs_header_write<size, big_endian>::data_size; |
072fe7ce ILT |
355 | |
356 | unsigned int sz = header_size + entry_size * this->inputs_map_.size(); | |
3ce2c28e | 357 | unsigned char* buffer = new unsigned char[sz]; |
072fe7ce ILT |
358 | unsigned char* inputs_base = buffer + header_size; |
359 | ||
3ce2c28e | 360 | Incremental_inputs_header_write<size, big_endian> header_writer(buffer); |
3ce2c28e ILT |
361 | gold_assert(this->command_line_key_ > 0); |
362 | int cmd_offset = this->strtab_->get_offset_from_key(this->command_line_key_); | |
072fe7ce | 363 | |
3ce2c28e | 364 | header_writer.put_version(INCREMENTAL_LINK_VERSION); |
072fe7ce | 365 | header_writer.put_input_file_count(this->inputs_map_.size()); |
3ce2c28e ILT |
366 | header_writer.put_command_line_offset(cmd_offset); |
367 | header_writer.put_reserved(0); | |
072fe7ce ILT |
368 | |
369 | for (Inputs_info_map::const_iterator it = this->inputs_map_.begin(); | |
370 | it != this->inputs_map_.end(); | |
371 | ++it) | |
372 | { | |
373 | gold_assert(it->second.index < this->inputs_map_.size()); | |
374 | ||
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); | |
98fa85cb ILT |
385 | entry.put_timestamp_sec(it->second.mtime.seconds); |
386 | entry.put_timestamp_nsec(it->second.mtime.nanoseconds); | |
072fe7ce ILT |
387 | entry.put_input_type(it->second.type); |
388 | entry.put_reserved(0); | |
389 | } | |
390 | ||
3ce2c28e | 391 | return new Output_data_const_buffer(buffer, sz, 8, |
072fe7ce | 392 | "** incremental link inputs list"); |
3ce2c28e ILT |
393 | } |
394 | ||
0e879927 | 395 | } // End namespace gold. |