* fileread.cc (File_read::get_mtime): New method.
[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
29 using elfcpp::Convert;
30
31 namespace gold {
32
33 // Version information. Will change frequently during the development, later
34 // we could think about backward (and forward?) compatibility.
35 const int INCREMENTAL_LINK_VERSION = 1;
36
37 namespace internal {
38
39 // Header of the .gnu_incremental_input section.
40 struct Incremental_inputs_header_data
41 {
42 // Incremental linker version.
43 elfcpp::Elf_Word version;
44
45 // Numer of input files in the link.
46 elfcpp::Elf_Word input_file_count;
47
48 // Offset of command line options in .gnu_incremental_strtab.
49 elfcpp::Elf_Word command_line_offset;
50
51 // Padding.
52 elfcpp::Elf_Word reserved;
53 };
54
55 // Data stored in .gnu_incremental_input after the header for each of the
56 // Incremental_input_header_data::input_file_count input entries.
57 struct Incremental_inputs_entry_data
58 {
59 // Offset of file name in .gnu_incremental_strtab section.
60 elfcpp::Elf_Word filename_offset;
61
62 // Offset of data in .gnu_incremental_input.
63 elfcpp::Elf_Word data_offset;
64
65 // Timestamp (in seconds).
66 elfcpp::Elf_Xword timestamp_sec;
67
68 // Nano-second part of timestamp (if supported).
69 elfcpp::Elf_Word timestamp_nsec;
70
71 // Type of the input entry.
72 elfcpp::Elf_Half input_type;
73
74 // Padding.
75 elfcpp::Elf_Half reserved;
76 };
77
78 }
79
80 // Accessors.
81
82 // See internal::Incremental_input_header for fields descriptions.
83 template<int size, bool big_endian>
84 class Incremental_inputs_header_write
85 {
86 public:
87 Incremental_inputs_header_write(unsigned char *p)
88 : p_(reinterpret_cast<internal::Incremental_inputs_header_data*>(p))
89 { }
90
91 static const int data_size = sizeof(internal::Incremental_inputs_header_data);
92
93 void
94 put_version(elfcpp::Elf_Word v)
95 { this->p_->version = Convert<32, big_endian>::convert_host(v); }
96
97 void
98 put_input_file_count(elfcpp::Elf_Word v)
99 { this->p_->input_file_count = Convert<32, big_endian>::convert_host(v); }
100
101 void
102 put_command_line_offset(elfcpp::Elf_Word v)
103 { this->p_->command_line_offset = Convert<32, big_endian>::convert_host(v); }
104
105 void
106 put_reserved(elfcpp::Elf_Word v)
107 { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
108
109 private:
110 internal::Incremental_inputs_header_data* p_;
111 };
112
113 // See internal::Incremental_input_entry for fields descriptions.
114 template<int size, bool big_endian>
115 class Incremental_inputs_entry_write
116 {
117 public:
118 Incremental_inputs_entry_write(unsigned char *p)
119 : p_(reinterpret_cast<internal::Incremental_inputs_entry_data*>(p))
120 { }
121
122 static const int data_size = sizeof(internal::Incremental_inputs_entry_data);
123
124 void
125 put_filename_offset(elfcpp::Elf_Word v)
126 { this->p_->filename_offset = Convert<32, big_endian>::convert_host(v); }
127
128 void
129 put_data_offset(elfcpp::Elf_Word v)
130 { this->p_->data_offset = Convert<32, big_endian>::convert_host(v); }
131
132 void
133 put_timestamp_sec(elfcpp::Elf_Xword v)
134 { this->p_->timestamp_sec = Convert<64, big_endian>::convert_host(v); }
135
136 void
137 put_timestamp_nsec(elfcpp::Elf_Word v)
138 { this->p_->timestamp_nsec = Convert<32, big_endian>::convert_host(v); }
139
140 void
141 put_input_type(elfcpp::Elf_Word v)
142 { this->p_->input_type = Convert<32, big_endian>::convert_host(v); }
143
144 void
145 put_reserved(elfcpp::Elf_Word v)
146 { this->p_->reserved = Convert<32, big_endian>::convert_host(v); }
147
148 private:
149 internal::Incremental_inputs_entry_data* p_;
150 };
151
152 // Add the command line to the string table, setting
153 // command_line_key_. In incremental builds, the command line is
154 // stored in .gnu_incremental_inputs so that the next linker run can
155 // check if the command line options didn't change.
156
157 void
158 Incremental_inputs::report_command_line(int argc, const char* const* argv)
159 {
160 // Always store 'gold' as argv[0] to avoid a full relink if the user used a
161 // different path to the linker.
162 std::string args("gold");
163 // Copied from collect_argv in main.cc.
164 for (int i = 1; i < argc; ++i)
165 {
166 // Adding/removing these options should result in a full relink.
167 if (strcmp(argv[i], "--incremental-changed") == 0
168 || strcmp(argv[i], "--incremental-unchanged") == 0
169 || strcmp(argv[i], "--incremental-unknown") == 0)
170 continue;
171
172 args.append(" '");
173 // Now append argv[i], but with all single-quotes escaped
174 const char* argpos = argv[i];
175 while (1)
176 {
177 const int len = strcspn(argpos, "'");
178 args.append(argpos, len);
179 if (argpos[len] == '\0')
180 break;
181 args.append("'\"'\"'");
182 argpos += len + 1;
183 }
184 args.append("'");
185 }
186 this->strtab_->add(args.c_str(), true, &this->command_line_key_);
187 }
188
189 // Record that the input argument INPUT is an achive ARCHIVE. This is
190 // called by Read_symbols after finding out the type of the file.
191
192 void
193 Incremental_inputs::report_archive(const Input_argument* input,
194 Archive* archive)
195 {
196 Hold_lock hl(*this->lock_);
197
198 Input_info info;
199 info.type = INCREMENTAL_INPUT_ARCHIVE;
200 info.archive = archive;
201 info.mtime = archive->file().get_mtime();
202 this->inputs_map_.insert(std::make_pair(input, info));
203 }
204
205 // Record that the input argument INPUT is an object OBJ. This is
206 // called by Read_symbols after finding out the type of the file.
207
208 void
209 Incremental_inputs::report_object(const Input_argument* input,
210 Object* obj)
211 {
212 Hold_lock hl(*this->lock_);
213
214 Input_info info;
215 info.type = (obj->is_dynamic()
216 ? INCREMENTAL_INPUT_SHARED_LIBRARY
217 : INCREMENTAL_INPUT_OBJECT);
218 info.object = obj;
219 info.mtime = obj->input_file()->file().get_mtime();
220 this->inputs_map_.insert(std::make_pair(input, info));
221 }
222
223 // Record that the input argument INPUT is an script SCRIPT. This is
224 // called by read_script after parsing the script and reading the list
225 // of inputs added by this script.
226
227 void
228 Incremental_inputs::report_script(const Input_argument* input,
229 Timespec mtime,
230 Script_info* script)
231 {
232 Hold_lock hl(*this->lock_);
233
234 Input_info info;
235 info.type = INCREMENTAL_INPUT_SCRIPT;
236 info.script = script;
237 info.mtime = mtime;
238 this->inputs_map_.insert(std::make_pair(input, info));
239 }
240
241 // Compute indexes in the order in which the inputs should appear in
242 // .gnu_incremental_inputs. This needs to be done after all the
243 // scripts are parsed. The function is first called for the command
244 // line inputs arguments and may call itself recursively for e.g. a
245 // list of elements of a group or a list of inputs added by a script.
246 // The [BEGIN; END) interval to analyze and *INDEX is the current
247 // value of the index (that will be updated).
248
249 void
250 Incremental_inputs::finalize_inputs(
251 Input_argument_list::const_iterator begin,
252 Input_argument_list::const_iterator end,
253 unsigned int* index)
254 {
255 for (Input_argument_list::const_iterator p = begin; p != end; ++p)
256 {
257 if (p->is_group())
258 {
259 finalize_inputs(p->group()->begin(), p->group()->end(), index);
260 continue;
261 }
262
263 Inputs_info_map::iterator it = this->inputs_map_.find(&(*p));
264 // TODO: turn it into an assert when the code will be more stable.
265 if (it == this->inputs_map_.end())
266 {
267 gold_error("internal error: %s: incremental build info not provided",
268 (p->is_file() ? p->file().name() : "[group]"));
269 continue;
270 }
271 Input_info* info = &it->second;
272 info->index = *index;
273 (*index)++;
274 this->strtab_->add(p->file().name(), false, &info->filename_key);
275 if (info->type == INCREMENTAL_INPUT_SCRIPT)
276 {
277 finalize_inputs(info->script->inputs()->begin(),
278 info->script->inputs()->end(),
279 index);
280 }
281 }
282 }
283
284 // Finalize the incremental link information. Called from
285 // Layout::finalize.
286
287 void
288 Incremental_inputs::finalize()
289 {
290 unsigned int index = 0;
291 finalize_inputs(this->inputs_->begin(), this->inputs_->end(), &index);
292
293 // Sanity check.
294 for (Inputs_info_map::const_iterator p = this->inputs_map_.begin();
295 p != this->inputs_map_.end();
296 ++p)
297 {
298 gold_assert(p->second.filename_key != 0);
299 }
300
301 this->strtab_->set_string_offsets();
302 }
303
304 // Create the content of the .gnu_incremental_inputs section.
305
306 Output_section_data*
307 Incremental_inputs::create_incremental_inputs_section_data()
308 {
309 switch (parameters->size_and_endianness())
310 {
311 #ifdef HAVE_TARGET_32_LITTLE
312 case Parameters::TARGET_32_LITTLE:
313 return this->sized_create_inputs_section_data<32, false>();
314 #endif
315 #ifdef HAVE_TARGET_32_BIG
316 case Parameters::TARGET_32_BIG:
317 return this->sized_create_inputs_section_data<32, true>();
318 #endif
319 #ifdef HAVE_TARGET_64_LITTLE
320 case Parameters::TARGET_64_LITTLE:
321 return this->sized_create_inputs_section_data<64, false>();
322 #endif
323 #ifdef HAVE_TARGET_64_BIG
324 case Parameters::TARGET_64_BIG:
325 return this->sized_create_inputs_section_data<64, true>();
326 #endif
327 default:
328 gold_unreachable();
329 }
330 }
331
332 // Sized creation of .gnu_incremental_inputs section.
333
334 template<int size, bool big_endian>
335 Output_section_data*
336 Incremental_inputs::sized_create_inputs_section_data()
337 {
338 const int entry_size =
339 Incremental_inputs_entry_write<size, big_endian>::data_size;
340 const int header_size =
341 Incremental_inputs_header_write<size, big_endian>::data_size;
342
343 unsigned int sz = header_size + entry_size * this->inputs_map_.size();
344 unsigned char* buffer = new unsigned char[sz];
345 unsigned char* inputs_base = buffer + header_size;
346
347 Incremental_inputs_header_write<size, big_endian> header_writer(buffer);
348 gold_assert(this->command_line_key_ > 0);
349 int cmd_offset = this->strtab_->get_offset_from_key(this->command_line_key_);
350
351 header_writer.put_version(INCREMENTAL_LINK_VERSION);
352 header_writer.put_input_file_count(this->inputs_map_.size());
353 header_writer.put_command_line_offset(cmd_offset);
354 header_writer.put_reserved(0);
355
356 for (Inputs_info_map::const_iterator it = this->inputs_map_.begin();
357 it != this->inputs_map_.end();
358 ++it)
359 {
360 gold_assert(it->second.index < this->inputs_map_.size());
361
362 unsigned char* entry_buffer =
363 inputs_base + it->second.index * entry_size;
364 Incremental_inputs_entry_write<size, big_endian> entry(entry_buffer);
365 int filename_offset =
366 this->strtab_->get_offset_from_key(it->second.filename_key);
367 entry.put_filename_offset(filename_offset);
368 // TODO: add per input data and timestamp. Currently we store
369 // an out-of-bounds offset for future version of gold to reject
370 // such an incremental_inputs section.
371 entry.put_data_offset(0xffffffff);
372 entry.put_timestamp_sec(it->second.mtime.seconds);
373 entry.put_timestamp_nsec(it->second.mtime.nanoseconds);
374 entry.put_input_type(it->second.type);
375 entry.put_reserved(0);
376 }
377
378 return new Output_data_const_buffer(buffer, sz, 8,
379 "** incremental link inputs list");
380 }
381
382 } // End namespace gold.
This page took 0.037545 seconds and 5 git commands to generate.