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