Commit | Line | Data |
---|---|---|
3ce2c28e ILT |
1 | // inremental.h -- incremental linking support for gold -*- C++ -*- |
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 | #ifndef GOLD_INCREMENTAL_H | |
24 | #define GOLD_INCREMENTAL_H | |
25 | ||
072fe7ce | 26 | #include <map> |
3ce2c28e ILT |
27 | #include <vector> |
28 | ||
c549a694 | 29 | #include "elfcpp_file.h" |
3ce2c28e ILT |
30 | #include "stringpool.h" |
31 | #include "workqueue.h" | |
98fa85cb | 32 | #include "fileread.h" |
c549a694 | 33 | #include "output.h" |
3ce2c28e ILT |
34 | |
35 | namespace gold | |
36 | { | |
37 | ||
38 | class Archive; | |
39 | class Input_argument; | |
40 | class Incremental_inputs_checker; | |
41 | class Object; | |
42 | class Output_section_data; | |
43 | ||
072fe7ce ILT |
44 | // Incremental input type as stored in .gnu_incremental_inputs. |
45 | ||
46 | enum Incremental_input_type | |
47 | { | |
48 | INCREMENTAL_INPUT_INVALID = 0, | |
49 | INCREMENTAL_INPUT_OBJECT = 1, | |
50 | INCREMENTAL_INPUT_ARCHIVE = 2, | |
51 | INCREMENTAL_INPUT_SHARED_LIBRARY = 3, | |
52 | INCREMENTAL_INPUT_SCRIPT = 4 | |
53 | }; | |
54 | ||
e2b8f3c4 RÁE |
55 | // Header of the .gnu_incremental_input section. |
56 | struct Incremental_inputs_header_data | |
57 | { | |
58 | // Incremental linker version. | |
59 | elfcpp::Elf_Word version; | |
60 | ||
61 | // Numer of input files in the link. | |
62 | elfcpp::Elf_Word input_file_count; | |
63 | ||
64 | // Offset of command line options in .gnu_incremental_strtab. | |
65 | elfcpp::Elf_Word command_line_offset; | |
66 | ||
67 | // Padding. | |
68 | elfcpp::Elf_Word reserved; | |
69 | }; | |
70 | ||
71 | // Data stored in .gnu_incremental_input after the header for each of the | |
72 | // Incremental_input_header_data::input_file_count input entries. | |
73 | struct Incremental_inputs_entry_data | |
74 | { | |
75 | // Offset of file name in .gnu_incremental_strtab section. | |
76 | elfcpp::Elf_Word filename_offset; | |
77 | ||
78 | // Offset of data in .gnu_incremental_input. | |
79 | elfcpp::Elf_Word data_offset; | |
80 | ||
81 | // Timestamp (in seconds). | |
82 | elfcpp::Elf_Xword timestamp_sec; | |
83 | ||
84 | // Nano-second part of timestamp (if supported). | |
85 | elfcpp::Elf_Word timestamp_nsec; | |
86 | ||
87 | // Type of the input entry. | |
88 | elfcpp::Elf_Half input_type; | |
89 | ||
90 | // Padding. | |
91 | elfcpp::Elf_Half reserved; | |
92 | }; | |
93 | ||
c549a694 ILT |
94 | // An object representing the ELF file we edit during an incremental build. |
95 | // Similar to Object or Dynobj, but operates on Output_file and contains | |
96 | // method specific to file edition (TBD). This is the abstract parent class | |
97 | // implemented in Sized_incremental_binary<size, big_endian> for a specific | |
98 | // endianness and size. | |
99 | ||
100 | class Incremental_binary | |
101 | { | |
102 | public: | |
103 | Incremental_binary(Output_file* output, Target* target) | |
104 | : output_(output), target_(target) | |
105 | { } | |
106 | ||
107 | virtual | |
108 | ~Incremental_binary() | |
109 | { } | |
110 | ||
111 | // Functions and types for the elfcpp::Elf_file interface. This | |
112 | // permit us to use Incremental_binary as the File template parameter for | |
113 | // elfcpp::Elf_file. | |
114 | ||
115 | // The View class is returned by view. It must support a single | |
116 | // method, data(). This is trivial, because Output_file::get_output_view | |
117 | // does what we need. | |
118 | class View | |
119 | { | |
120 | public: | |
121 | View(const unsigned char* p) | |
122 | : p_(p) | |
123 | { } | |
124 | ||
125 | const unsigned char* | |
126 | data() const | |
127 | { return this->p_; } | |
128 | ||
129 | private: | |
130 | const unsigned char* p_; | |
131 | }; | |
132 | ||
133 | // Return a View. | |
134 | View | |
135 | view(off_t file_offset, section_size_type data_size) | |
136 | { return View(this->output_->get_input_view(file_offset, data_size)); } | |
137 | ||
138 | // A location in the file. | |
139 | struct Location | |
140 | { | |
141 | off_t file_offset; | |
142 | off_t data_size; | |
143 | ||
144 | Location(off_t fo, section_size_type ds) | |
145 | : file_offset(fo), data_size(ds) | |
146 | { } | |
147 | ||
148 | Location() | |
149 | : file_offset(0), data_size(0) | |
150 | { } | |
151 | }; | |
152 | ||
153 | // Get a View given a Location. | |
154 | View view(Location loc) | |
155 | { return View(this->view(loc.file_offset, loc.data_size)); } | |
156 | ||
157 | // Report an error. | |
158 | void | |
159 | error(const char* format, ...) const ATTRIBUTE_PRINTF_2; | |
160 | ||
c549a694 ILT |
161 | // Find the .gnu_incremental_inputs section. It selects the first section |
162 | // of type SHT_GNU_INCREMENTAL_INPUTS. Returns false if such a section | |
163 | // is not found. | |
164 | bool | |
c4aa1e2d ILT |
165 | find_incremental_inputs_section(Location* location, |
166 | unsigned int* strtab_shndx) | |
167 | { return do_find_incremental_inputs_section(location, strtab_shndx); } | |
168 | ||
169 | // Check the .gnu_incremental_inputs section to see whether an incremental | |
170 | // build is possible. | |
171 | // TODO: on success, should report what files needs to be rebuilt. | |
172 | // INCREMENTAL_INPUTS is used to read the canonical form of the command line | |
173 | // and read the input arguments. TODO: for items that don't need to be | |
174 | // rebuilt, we should also copy the incremental input information. | |
175 | virtual bool | |
176 | check_inputs(Incremental_inputs* incremental_inputs) | |
177 | { return do_check_inputs(incremental_inputs); } | |
c549a694 ILT |
178 | |
179 | protected: | |
180 | // Find incremental inputs section. | |
181 | virtual bool | |
c4aa1e2d ILT |
182 | do_find_incremental_inputs_section(Location* location, |
183 | unsigned int* strtab_shndx) = 0; | |
184 | ||
185 | // Check the .gnu_incremental_inputs section to see whether an incremental | |
186 | // build is possible. | |
187 | virtual bool | |
188 | do_check_inputs(Incremental_inputs* incremental_inputs) = 0; | |
c549a694 ILT |
189 | |
190 | private: | |
191 | // Edited output file object. | |
192 | Output_file* output_; | |
193 | // Target of the output file. | |
194 | Target* target_; | |
195 | }; | |
196 | ||
197 | template<int size, bool big_endian> | |
198 | class Sized_incremental_binary : public Incremental_binary | |
199 | { | |
200 | public: | |
201 | Sized_incremental_binary(Output_file* output, | |
202 | const elfcpp::Ehdr<size, big_endian>& ehdr, | |
203 | Target* target) | |
204 | : Incremental_binary(output, target), elf_file_(this, ehdr) | |
205 | { } | |
206 | ||
207 | protected: | |
208 | virtual bool | |
c4aa1e2d ILT |
209 | do_find_incremental_inputs_section(Location* location, |
210 | unsigned int* strtab_shndx); | |
211 | ||
212 | virtual bool | |
213 | do_check_inputs(Incremental_inputs* incremental_inputs); | |
c549a694 ILT |
214 | |
215 | private: | |
216 | // Output as an ELF file. | |
217 | elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_; | |
218 | }; | |
219 | ||
220 | // Create an Incremental_binary object for FILE. Returns NULL is this is not | |
221 | // possible, e.g. FILE is not an ELF file or has an unsupported target. | |
222 | Incremental_binary* | |
223 | open_incremental_binary(Output_file* file); | |
224 | ||
44453f85 ILT |
225 | // Code invoked early during an incremental link that checks what files need |
226 | // to be relinked. | |
227 | class Incremental_checker | |
228 | { | |
229 | public: | |
c4aa1e2d ILT |
230 | // Check if the file named OUTPUT_NAME can be linked incrementally. |
231 | // INCREMENTAL_INPUTS must have the canonical form of the command line | |
232 | // and input arguments filled - at this point of linking other fields are | |
233 | // probably not filled yet. TODO: for inputs that don't need to be | |
234 | // rebuilt, this function should fill the incremental input information. | |
235 | Incremental_checker(const char* output_name, | |
236 | Incremental_inputs* incremental_inputs) | |
237 | : output_name_(output_name), incremental_inputs_(incremental_inputs) | |
44453f85 ILT |
238 | { } |
239 | ||
240 | // Analyzes the output file to check if incremental linking is possible and | |
241 | // what files needs to be relinked. | |
242 | bool | |
243 | can_incrementally_link_output_file(); | |
244 | ||
245 | private: | |
c4aa1e2d | 246 | // Name of the output file to analyze. |
44453f85 | 247 | const char* output_name_; |
c4aa1e2d ILT |
248 | |
249 | // The Incremental_inputs object. At this stage of link, only the command | |
250 | // line and inputs are filled. | |
251 | Incremental_inputs* incremental_inputs_; | |
44453f85 ILT |
252 | }; |
253 | ||
3ce2c28e ILT |
254 | // This class contains the information needed during an incremental |
255 | // build about the inputs necessary to build the .gnu_incremental_inputs. | |
256 | class Incremental_inputs | |
257 | { | |
258 | public: | |
259 | Incremental_inputs() | |
072fe7ce ILT |
260 | : lock_(new Lock()), inputs_(NULL), command_line_key_(0), |
261 | strtab_(new Stringpool()) | |
3ce2c28e ILT |
262 | { } |
263 | ~Incremental_inputs() { delete this->strtab_; } | |
264 | ||
265 | // Record the command line. | |
266 | void | |
267 | report_command_line(int argc, const char* const* argv); | |
072fe7ce ILT |
268 | |
269 | // Record the input arguments obtained from parsing the command line. | |
270 | void | |
271 | report_inputs(const Input_arguments& inputs) | |
272 | { this->inputs_ = &inputs; } | |
273 | ||
274 | // Record that the input argument INPUT is an archive ARCHIVE. | |
275 | void | |
276 | report_archive(const Input_argument* input, Archive* archive); | |
277 | ||
278 | // Record that the input argument INPUT is to an object OBJ. | |
279 | void | |
280 | report_object(const Input_argument* input, Object* obj); | |
281 | ||
282 | // Record that the input argument INPUT is to an script SCRIPT. | |
283 | void | |
98fa85cb ILT |
284 | report_script(const Input_argument* input, Timespec mtime, |
285 | Script_info* script); | |
072fe7ce | 286 | |
3ce2c28e ILT |
287 | // Prepare for layout. Called from Layout::finalize. |
288 | void | |
289 | finalize(); | |
290 | ||
291 | // Create the content of the .gnu_incremental_inputs section. | |
292 | Output_section_data* | |
293 | create_incremental_inputs_section_data(); | |
072fe7ce ILT |
294 | |
295 | // Return the .gnu_incremental_strtab stringpool. | |
3ce2c28e ILT |
296 | Stringpool* |
297 | get_stringpool() | |
298 | { return this->strtab_; } | |
299 | ||
c4aa1e2d ILT |
300 | // Return the canonical form of the command line, as will be stored in |
301 | // .gnu_incremental_strtab. | |
302 | const std::string& | |
303 | command_line() | |
304 | { return this->command_line_; } | |
305 | ||
306 | // Return the input files found in the command line. | |
307 | const Input_arguments* | |
308 | inputs() | |
309 | { return this->inputs_; } | |
310 | ||
3ce2c28e ILT |
311 | private: |
312 | // Code for each of the four possible variants of create_inputs_section_data. | |
313 | template<int size, bool big_endian> | |
314 | Output_section_data* | |
072fe7ce ILT |
315 | sized_create_inputs_section_data(); |
316 | ||
317 | // Compute indexes in the order in which the inputs should appear in | |
318 | // .gnu_incremental_inputs and put file names to the stringtable. | |
319 | // This needs to be done after all the scripts are parsed. | |
320 | ||
321 | void | |
322 | finalize_inputs(Input_argument_list::const_iterator begin, | |
323 | Input_argument_list::const_iterator end, | |
324 | unsigned int* index); | |
325 | ||
326 | // Additional data about an input needed for an incremental link. | |
327 | // None of these pointers is owned by the structure. | |
328 | struct Input_info | |
329 | { | |
330 | Input_info() | |
98fa85cb ILT |
331 | : type(INCREMENTAL_INPUT_INVALID), archive(NULL), filename_key(0), |
332 | index(0) | |
072fe7ce ILT |
333 | { } |
334 | ||
335 | // Type of the file pointed by this argument. | |
336 | Incremental_input_type type; | |
337 | ||
98fa85cb ILT |
338 | union |
339 | { | |
340 | // Present if type == INCREMENTAL_INPUT_ARCHIVE. | |
341 | Archive* archive; | |
44453f85 | 342 | |
98fa85cb ILT |
343 | // Present if type == INCREMENTAL_INPUT_OBJECT or |
344 | // INCREMENTAL_INPUT_SHARED_LIBRARY. | |
345 | Object* object; | |
44453f85 | 346 | |
98fa85cb ILT |
347 | // Present if type == INCREMENTAL_INPUT_SCRIPT. |
348 | Script_info* script; | |
349 | }; | |
072fe7ce ILT |
350 | |
351 | // Key of the filename string in the section stringtable. | |
352 | Stringpool::Key filename_key; | |
353 | ||
354 | // Position of the entry information in the output section. | |
355 | unsigned int index; | |
44453f85 | 356 | |
98fa85cb ILT |
357 | // Last modification time of the file. |
358 | Timespec mtime; | |
072fe7ce ILT |
359 | }; |
360 | ||
361 | typedef std::map<const Input_argument*, Input_info> Inputs_info_map; | |
362 | ||
363 | // A lock guarding access to inputs_ during the first phase of linking, when | |
364 | // report_ function may be called from multiple threads. | |
365 | Lock* lock_; | |
44453f85 | 366 | |
072fe7ce ILT |
367 | // The list of input arguments obtained from parsing the command line. |
368 | const Input_arguments* inputs_; | |
369 | ||
370 | // A map containing additional information about the input elements. | |
371 | Inputs_info_map inputs_map_; | |
3ce2c28e | 372 | |
c4aa1e2d ILT |
373 | // Canonical form of the command line, as will be stored in |
374 | // .gnu_incremental_strtab. | |
375 | std::string command_line_; | |
376 | ||
3ce2c28e ILT |
377 | // The key of the command line string in the string pool. |
378 | Stringpool::Key command_line_key_; | |
c4aa1e2d | 379 | |
3ce2c28e ILT |
380 | // The .gnu_incremental_strtab string pool associated with the |
381 | // .gnu_incremental_inputs. | |
382 | Stringpool* strtab_; | |
383 | }; | |
384 | ||
385 | } // End namespace gold. | |
386 | ||
387 | #endif // !defined(GOLD_INCREMENTAL_H) |