elfcpp/:
[external/binutils.git] / gold / incremental.h
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
26 #include <map>
27 #include <vector>
28
29 #include "elfcpp_file.h"
30 #include "stringpool.h"
31 #include "workqueue.h"
32 #include "fileread.h"
33 #include "output.h"
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
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
55 // An object representing the ELF file we edit during an incremental build.
56 // Similar to Object or Dynobj, but operates on Output_file and contains
57 // method specific to file edition (TBD). This is the abstract parent class
58 // implemented in Sized_incremental_binary<size, big_endian> for a specific
59 // endianness and size.
60
61 class Incremental_binary
62 {
63  public:
64   Incremental_binary(Output_file* output, Target* target)
65     : output_(output), target_(target)
66   { }
67
68   virtual
69   ~Incremental_binary()
70   { }
71
72   // Functions and types for the elfcpp::Elf_file interface.  This
73   // permit us to use Incremental_binary as the File template parameter for
74   // elfcpp::Elf_file.
75
76   // The View class is returned by view.  It must support a single
77   // method, data().  This is trivial, because Output_file::get_output_view
78   // does what we need.
79   class View
80   {
81    public:
82     View(const unsigned char* p)
83       : p_(p)
84     { }
85
86     const unsigned char*
87     data() const
88     { return this->p_; }
89
90    private:
91     const unsigned char* p_;
92   };
93
94   // Return a View.
95   View
96   view(off_t file_offset, section_size_type data_size)
97   { return View(this->output_->get_input_view(file_offset, data_size)); }
98
99   // A location in the file.
100   struct Location
101   {
102     off_t file_offset;
103     off_t data_size;
104
105     Location(off_t fo, section_size_type ds)
106       : file_offset(fo), data_size(ds)
107     { }
108
109     Location()
110       : file_offset(0), data_size(0)
111     { }
112   };
113
114   // Get a View given a Location.
115   View view(Location loc)
116   { return View(this->view(loc.file_offset, loc.data_size)); }
117
118   // Report an error.
119   void
120   error(const char* format, ...) const ATTRIBUTE_PRINTF_2;
121
122
123   // Find the .gnu_incremental_inputs section.  It selects the first section
124   // of type SHT_GNU_INCREMENTAL_INPUTS.  Returns false if such a section
125   // is not found.
126   bool
127   find_incremental_inputs_section(Location* location)
128   { return do_find_incremental_inputs_section(location); }
129
130  protected:
131   // Find incremental inputs section.
132   virtual bool
133   do_find_incremental_inputs_section(Location* location) = 0;
134
135  private:
136   // Edited output file object.
137   Output_file* output_;
138   // Target of the output file.
139   Target* target_;
140 };
141
142 template<int size, bool big_endian>
143 class Sized_incremental_binary : public Incremental_binary
144 {
145  public:
146   Sized_incremental_binary(Output_file* output,
147                            const elfcpp::Ehdr<size, big_endian>& ehdr,
148                            Target* target)
149     : Incremental_binary(output, target), elf_file_(this, ehdr)
150   { }
151
152  protected:
153   virtual bool
154   do_find_incremental_inputs_section(Location* location);
155
156  private:
157   // Output as an ELF file.
158   elfcpp::Elf_file<size, big_endian, Incremental_binary> elf_file_;
159 };
160
161 // Create an Incremental_binary object for FILE. Returns NULL is this is not
162 // possible, e.g. FILE is not an ELF file or has an unsupported target.
163 Incremental_binary*
164 open_incremental_binary(Output_file* file);
165
166 // Code invoked early during an incremental link that checks what files need
167 // to be relinked.
168 class Incremental_checker
169 {
170  public:
171   Incremental_checker(const char* output_name)
172     : output_name_(output_name)
173   { }
174
175   // Analyzes the output file to check if incremental linking is possible and
176   // what files needs to be relinked.
177   bool
178   can_incrementally_link_output_file();
179
180  private:
181   const char* output_name_;
182 };
183
184 // This class contains the information needed during an incremental
185 // build about the inputs necessary to build the .gnu_incremental_inputs.
186 class Incremental_inputs
187 {
188  public:
189   Incremental_inputs()
190     : lock_(new Lock()), inputs_(NULL), command_line_key_(0),
191       strtab_(new Stringpool())
192   { }
193   ~Incremental_inputs() { delete this->strtab_; }
194
195   // Record the command line.
196   void
197   report_command_line(int argc, const char* const* argv);
198
199   // Record the input arguments obtained from parsing the command line.
200   void
201   report_inputs(const Input_arguments& inputs)
202   { this->inputs_ = &inputs; }
203
204   // Record that the input argument INPUT is an archive ARCHIVE.
205   void
206   report_archive(const Input_argument* input, Archive* archive);
207
208   // Record that the input argument INPUT is to an object OBJ.
209   void
210   report_object(const Input_argument* input, Object* obj);
211
212   // Record that the input argument INPUT is to an script SCRIPT.
213   void
214   report_script(const Input_argument* input, Timespec mtime,
215                 Script_info* script);
216
217   // Prepare for layout.  Called from Layout::finalize.
218   void
219   finalize();
220
221   // Create the content of the .gnu_incremental_inputs section.
222   Output_section_data*
223   create_incremental_inputs_section_data();
224
225   // Return the .gnu_incremental_strtab stringpool.
226   Stringpool*
227   get_stringpool()
228   { return this->strtab_; }
229
230  private:
231   // Code for each of the four possible variants of create_inputs_section_data.
232   template<int size, bool big_endian>
233   Output_section_data*
234   sized_create_inputs_section_data();
235
236   // Compute indexes in the order in which the inputs should appear in
237   // .gnu_incremental_inputs and put file names to the stringtable.
238   // This needs to be done after all the scripts are parsed.
239
240   void
241   finalize_inputs(Input_argument_list::const_iterator begin,
242                   Input_argument_list::const_iterator end,
243                   unsigned int* index);
244
245   // Additional data about an input needed for an incremental link.
246   // None of these pointers is owned by the structure.
247   struct Input_info
248   {
249     Input_info()
250       : type(INCREMENTAL_INPUT_INVALID), archive(NULL), filename_key(0),
251         index(0)
252     { }
253
254     // Type of the file pointed by this argument.
255     Incremental_input_type type;
256
257     union
258     {
259       // Present if type == INCREMENTAL_INPUT_ARCHIVE.
260       Archive* archive;
261
262       // Present if type == INCREMENTAL_INPUT_OBJECT or
263       // INCREMENTAL_INPUT_SHARED_LIBRARY.
264       Object* object;
265
266       // Present if type == INCREMENTAL_INPUT_SCRIPT.
267       Script_info* script;
268     };
269
270     // Key of the filename string in the section stringtable.
271     Stringpool::Key filename_key;
272
273     // Position of the entry information in the output section.
274     unsigned int index;
275
276     // Last modification time of the file.
277     Timespec mtime;
278   };
279
280   typedef std::map<const Input_argument*, Input_info> Inputs_info_map;
281
282   // A lock guarding access to inputs_ during the first phase of linking, when
283   // report_ function may be called from multiple threads.
284   Lock* lock_;
285
286   // The list of input arguments obtained from parsing the command line.
287   const Input_arguments* inputs_;
288
289   // A map containing additional information about the input elements.
290   Inputs_info_map inputs_map_;
291
292   // The key of the command line string in the string pool.
293   Stringpool::Key command_line_key_;
294   // The .gnu_incremental_strtab string pool associated with the
295   // .gnu_incremental_inputs.
296   Stringpool* strtab_;
297 };
298
299 } // End namespace gold.
300
301 #endif // !defined(GOLD_INCREMENTAL_H)