* gold.cc: Include "incremental.h".
[deliverable/binutils-gdb.git] / gold / incremental.cc
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"
25 #include "output.h"
26 #include "incremental.h"
27 #include "archive.h"
28 #include "output.h"
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.
41 struct Incremental_inputs_header_data
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.
58 struct Incremental_inputs_entry_data
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).
70 elfcpp::Elf_Word timestamp_nsec;
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>
85 class Incremental_inputs_header_write
86 {
87 public:
88 Incremental_inputs_header_write(unsigned char *p)
89 : p_(reinterpret_cast<internal::Incremental_inputs_header_data*>(p))
90 { }
91
92 static const int data_size = sizeof(internal::Incremental_inputs_header_data);
93
94 void
95 put_version(elfcpp::Elf_Word v)
96 { this->p_->version = Convert<32, big_endian>::convert_host(v); }
97
98 void
99 put_input_file_count(elfcpp::Elf_Word v)
100 { this->p_->input_file_count = Convert<32, big_endian>::convert_host(v); }
101
102 void
103 put_command_line_offset(elfcpp::Elf_Word v)
104 { this->p_->command_line_offset = Convert<32, big_endian>::convert_host(v); }
105
106 void
107 put_reserved(elfcpp::Elf_Word v)
108 { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
109
110 private:
111 internal::Incremental_inputs_header_data* p_;
112 };
113
114 // See internal::Incremental_input_entry for fields descriptions.
115 template<int size, bool big_endian>
116 class Incremental_inputs_entry_write
117 {
118 public:
119 Incremental_inputs_entry_write(unsigned char *p)
120 : p_(reinterpret_cast<internal::Incremental_inputs_entry_data*>(p))
121 { }
122
123 static const int data_size = sizeof(internal::Incremental_inputs_entry_data);
124
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
134 put_timestamp_sec(elfcpp::Elf_Xword v)
135 { this->p_->timestamp_sec = Convert<64, big_endian>::convert_host(v); }
136
137 void
138 put_timestamp_nsec(elfcpp::Elf_Word v)
139 { this->p_->timestamp_nsec = Convert<32, big_endian>::convert_host(v); }
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:
150 internal::Incremental_inputs_entry_data* p_;
151 };
152
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
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 {
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
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
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;
214 info.mtime = archive->file().get_mtime();
215 this->inputs_map_.insert(std::make_pair(input, info));
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;
232 info.mtime = obj->input_file()->file().get_mtime();
233 this->inputs_map_.insert(std::make_pair(input, info));
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,
242 Timespec mtime,
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;
250 info.mtime = mtime;
251 this->inputs_map_.insert(std::make_pair(input, info));
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
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())
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
297 // Finalize the incremental link information. Called from
298 // Layout::finalize.
299
300 void
301 Incremental_inputs::finalize()
302 {
303 unsigned int index = 0;
304 finalize_inputs(this->inputs_->begin(), this->inputs_->end(), &index);
305
306 // Sanity check.
307 for (Inputs_info_map::const_iterator p = this->inputs_map_.begin();
308 p != this->inputs_map_.end();
309 ++p)
310 {
311 gold_assert(p->second.filename_key != 0);
312 }
313
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();
342 }
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()
350 {
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;
355
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;
359
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_);
363
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);
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);
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);
389 }
390
391 return new Output_data_const_buffer(buffer, sz, 8,
392 "** incremental link inputs list");
393 }
394
395 } // End namespace gold.
This page took 0.040672 seconds and 5 git commands to generate.