* configure.ac (AC_CHECK_FUNCS): Sort into alphabetical order.
[binutils.git] / gold / incremental.h
blobf6e36faba80bca17d4caeb46d2372080443cdc44
1 // inremental.h -- incremental linking support for gold -*- C++ -*-
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Mikolaj Zalewski <mikolajz@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #ifndef GOLD_INCREMENTAL_H
24 #define GOLD_INCREMENTAL_H
26 #include <map>
27 #include <vector>
29 #include "elfcpp_file.h"
30 #include "stringpool.h"
31 #include "workqueue.h"
32 #include "fileread.h"
33 #include "output.h"
35 namespace gold
38 class Archive;
39 class Input_argument;
40 class Incremental_inputs_checker;
41 class Object;
42 class Output_section_data;
44 // Incremental input type as stored in .gnu_incremental_inputs.
46 enum Incremental_input_type
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
55 // Header of the .gnu_incremental_input section.
56 struct Incremental_inputs_header_data
58 // Incremental linker version.
59 elfcpp::Elf_Word version;
61 // Numer of input files in the link.
62 elfcpp::Elf_Word input_file_count;
64 // Offset of command line options in .gnu_incremental_strtab.
65 elfcpp::Elf_Word command_line_offset;
67 // Padding.
68 elfcpp::Elf_Word reserved;
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
75 // Offset of file name in .gnu_incremental_strtab section.
76 elfcpp::Elf_Word filename_offset;
78 // Offset of data in .gnu_incremental_input.
79 elfcpp::Elf_Word data_offset;
81 // Timestamp (in seconds).
82 elfcpp::Elf_Xword timestamp_sec;
84 // Nano-second part of timestamp (if supported).
85 elfcpp::Elf_Word timestamp_nsec;
87 // Type of the input entry.
88 elfcpp::Elf_Half input_type;
90 // Padding.
91 elfcpp::Elf_Half reserved;
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.
100 class Incremental_binary
102 public:
103 Incremental_binary(Output_file* output, Target* target)
104 : output_(output), target_(target)
107 virtual
108 ~Incremental_binary()
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.
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
120 public:
121 View(const unsigned char* p)
122 : p_(p)
125 const unsigned char*
126 data() const
127 { return this->p_; }
129 private:
130 const unsigned char* p_;
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)); }
138 // A location in the file.
139 struct Location
141 off_t file_offset;
142 off_t data_size;
144 Location(off_t fo, section_size_type ds)
145 : file_offset(fo), data_size(ds)
148 Location()
149 : file_offset(0), data_size(0)
153 // Get a View given a Location.
154 View view(Location loc)
155 { return View(this->view(loc.file_offset, loc.data_size)); }
157 // Report an error.
158 void
159 error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
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
165 find_incremental_inputs_section(Location* location,
166 unsigned int* strtab_shndx)
167 { return do_find_incremental_inputs_section(location, strtab_shndx); }
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); }
179 protected:
180 // Find incremental inputs section.
181 virtual bool
182 do_find_incremental_inputs_section(Location* location,
183 unsigned int* strtab_shndx) = 0;
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;
190 private:
191 // Edited output file object.
192 Output_file* output_;
193 // Target of the output file.
194 Target* target_;
197 template<int size, bool big_endian>
198 class Sized_incremental_binary : public Incremental_binary
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)
207 protected:
208 virtual bool
209 do_find_incremental_inputs_section(Location* location,
210 unsigned int* strtab_shndx);
212 virtual bool
213 do_check_inputs(Incremental_inputs* incremental_inputs);
215 private:
216 // Output as an ELF file.
217 elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
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);
225 // Code invoked early during an incremental link that checks what files need
226 // to be relinked.
227 class Incremental_checker
229 public:
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)
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();
245 private:
246 // Name of the output file to analyze.
247 const char* output_name_;
249 // The Incremental_inputs object. At this stage of link, only the command
250 // line and inputs are filled.
251 Incremental_inputs* incremental_inputs_;
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
258 public:
259 Incremental_inputs()
260 : lock_(new Lock()), inputs_(NULL), command_line_key_(0),
261 strtab_(new Stringpool())
263 ~Incremental_inputs() { delete this->strtab_; }
265 // Record the command line.
266 void
267 report_command_line(int argc, const char* const* argv);
269 // Record the input arguments obtained from parsing the command line.
270 void
271 report_inputs(const Input_arguments& inputs)
272 { this->inputs_ = &inputs; }
274 // Record that the input argument INPUT is an archive ARCHIVE.
275 void
276 report_archive(const Input_argument* input, Archive* archive);
278 // Record that the input argument INPUT is to an object OBJ.
279 void
280 report_object(const Input_argument* input, Object* obj);
282 // Record that the input argument INPUT is to an script SCRIPT.
283 void
284 report_script(const Input_argument* input, Timespec mtime,
285 Script_info* script);
287 // Prepare for layout. Called from Layout::finalize.
288 void
289 finalize();
291 // Create the content of the .gnu_incremental_inputs section.
292 Output_section_data*
293 create_incremental_inputs_section_data();
295 // Return the .gnu_incremental_strtab stringpool.
296 Stringpool*
297 get_stringpool()
298 { return this->strtab_; }
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_; }
306 // Return the input files found in the command line.
307 const Input_arguments*
308 inputs()
309 { return this->inputs_; }
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*
315 sized_create_inputs_section_data();
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.
321 void
322 finalize_inputs(Input_argument_list::const_iterator begin,
323 Input_argument_list::const_iterator end,
324 unsigned int* index);
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
330 Input_info()
331 : type(INCREMENTAL_INPUT_INVALID), archive(NULL), filename_key(0),
332 index(0)
335 // Type of the file pointed by this argument.
336 Incremental_input_type type;
338 union
340 // Present if type == INCREMENTAL_INPUT_ARCHIVE.
341 Archive* archive;
343 // Present if type == INCREMENTAL_INPUT_OBJECT or
344 // INCREMENTAL_INPUT_SHARED_LIBRARY.
345 Object* object;
347 // Present if type == INCREMENTAL_INPUT_SCRIPT.
348 Script_info* script;
351 // Key of the filename string in the section stringtable.
352 Stringpool::Key filename_key;
354 // Position of the entry information in the output section.
355 unsigned int index;
357 // Last modification time of the file.
358 Timespec mtime;
361 typedef std::map<const Input_argument*, Input_info> Inputs_info_map;
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_;
367 // The list of input arguments obtained from parsing the command line.
368 const Input_arguments* inputs_;
370 // A map containing additional information about the input elements.
371 Inputs_info_map inputs_map_;
373 // Canonical form of the command line, as will be stored in
374 // .gnu_incremental_strtab.
375 std::string command_line_;
377 // The key of the command line string in the string pool.
378 Stringpool::Key command_line_key_;
380 // The .gnu_incremental_strtab string pool associated with the
381 // .gnu_incremental_inputs.
382 Stringpool* strtab_;
385 } // End namespace gold.
387 #endif // !defined(GOLD_INCREMENTAL_H)