Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // fileread.h -- read files for gold -*- C++ -*- |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@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 | ||
bae7f79e ILT |
23 | // Classes used to read data from binary input files. |
24 | ||
25 | #ifndef GOLD_FILEREAD_H | |
26 | #define GOLD_FILEREAD_H | |
27 | ||
bae7f79e | 28 | #include <list> |
ead1e424 ILT |
29 | #include <map> |
30 | #include <string> | |
bae7f79e ILT |
31 | |
32 | #include "options.h" | |
17a1d0a9 | 33 | #include "token.h" |
bae7f79e ILT |
34 | |
35 | namespace gold | |
36 | { | |
37 | ||
38 | class Dirsearch; | |
bae7f79e ILT |
39 | class File_view; |
40 | ||
41 | // File_read manages a file descriptor for a file we are reading. We | |
42 | // close file descriptors if we run out of them, so this class reopens | |
43 | // the file as needed. | |
44 | ||
45 | class File_read | |
46 | { | |
47 | public: | |
48 | File_read() | |
cb295612 ILT |
49 | : name_(), descriptor_(-1), object_count_(0), size_(0), token_(false), |
50 | views_(), saved_views_(), contents_(NULL), mapped_bytes_(0), | |
51 | released_(true) | |
bae7f79e | 52 | { } |
5a6f7e2d | 53 | |
bae7f79e ILT |
54 | ~File_read(); |
55 | ||
56 | // Open a file. | |
57 | bool | |
17a1d0a9 | 58 | open(const Task*, const std::string& name); |
bae7f79e | 59 | |
5a6f7e2d ILT |
60 | // Pretend to open the file, but provide the file contents. No |
61 | // actual file system activity will occur. This is used for | |
62 | // testing. | |
63 | bool | |
17a1d0a9 ILT |
64 | open(const Task*, const std::string& name, const unsigned char* contents, |
65 | off_t size); | |
5a6f7e2d | 66 | |
bae7f79e ILT |
67 | // Return the file name. |
68 | const std::string& | |
69 | filename() const | |
70 | { return this->name_; } | |
71 | ||
cb295612 ILT |
72 | // Add an object associated with a file. |
73 | void | |
74 | add_object() | |
75 | { ++this->object_count_; } | |
76 | ||
77 | // Remove an object associated with a file. | |
78 | void | |
79 | remove_object() | |
80 | { --this->object_count_; } | |
81 | ||
17a1d0a9 ILT |
82 | // Lock the file for exclusive access within a particular Task::run |
83 | // execution. This means that the descriptor can not be closed. | |
84 | // This routine may only be called when the workqueue lock is held. | |
bae7f79e | 85 | void |
17a1d0a9 | 86 | lock(const Task* t); |
bae7f79e ILT |
87 | |
88 | // Unlock the descriptor, permitting it to be closed if necessary. | |
89 | void | |
17a1d0a9 | 90 | unlock(const Task* t); |
4973341a | 91 | |
bae7f79e ILT |
92 | // Test whether the object is locked. |
93 | bool | |
7004837e | 94 | is_locked() const; |
bae7f79e | 95 | |
17a1d0a9 ILT |
96 | // Return the token, so that the task can be queued. |
97 | Task_token* | |
98 | token() | |
99 | { return &this->token_; } | |
100 | ||
101 | // Release the file. This indicates that we aren't going to do | |
102 | // anything further with it until it is unlocked. This is used | |
103 | // because a Task which locks the file never calls either lock or | |
104 | // unlock; it just locks the token. The basic rule is that a Task | |
105 | // which locks a file via the Task::locks interface must explicitly | |
106 | // call release() when it is done. This is not necessary for code | |
107 | // which calls unlock() on the file. | |
108 | void | |
109 | release(); | |
110 | ||
82dcae9d ILT |
111 | // Return the size of the file. |
112 | off_t | |
113 | filesize() const | |
114 | { return this->size_; } | |
115 | ||
ba45d247 ILT |
116 | // Return a view into the file starting at file offset START for |
117 | // SIZE bytes. The pointer will remain valid until the File_read is | |
118 | // unlocked. It is an error if we can not read enough data from the | |
9eb9fa57 ILT |
119 | // file. The CACHE parameter is a hint as to whether it will be |
120 | // useful to cache this data for later accesses--i.e., later calls | |
121 | // to get_view, read, or get_lasting_view which retrieve the same | |
122 | // data. | |
bae7f79e | 123 | const unsigned char* |
8383303e | 124 | get_view(off_t start, section_size_type size, bool cache); |
bae7f79e | 125 | |
ba45d247 ILT |
126 | // Read data from the file into the buffer P starting at file offset |
127 | // START for SIZE bytes. | |
128 | void | |
fe8718a4 | 129 | read(off_t start, section_size_type size, void* p) const; |
ba45d247 | 130 | |
ba45d247 ILT |
131 | // Return a lasting view into the file starting at file offset START |
132 | // for SIZE bytes. This is allocated with new, and the caller is | |
133 | // responsible for deleting it when done. The data associated with | |
134 | // this view will remain valid until the view is deleted. It is an | |
9eb9fa57 ILT |
135 | // error if we can not read enough data from the file. The CACHE |
136 | // parameter is as in get_view. | |
bae7f79e | 137 | File_view* |
8383303e | 138 | get_lasting_view(off_t start, section_size_type size, bool cache); |
bae7f79e | 139 | |
cb295612 ILT |
140 | // Mark all views as no longer cached. |
141 | void | |
142 | clear_view_cache_marks(); | |
143 | ||
144 | // A struct used to do a multiple read. | |
145 | struct Read_multiple_entry | |
146 | { | |
147 | // The file offset of the data to read. | |
148 | off_t file_offset; | |
149 | // The amount of data to read. | |
150 | section_size_type size; | |
151 | // The buffer where the data should be placed. | |
152 | unsigned char* buffer; | |
153 | ||
154 | Read_multiple_entry(off_t o, section_size_type s, unsigned char* b) | |
155 | : file_offset(o), size(s), buffer(b) | |
156 | { } | |
157 | }; | |
158 | ||
159 | typedef std::vector<Read_multiple_entry> Read_multiple; | |
160 | ||
161 | // Read a bunch of data from the file into various different | |
162 | // locations. The vector must be sorted by ascending file_offset. | |
163 | // BASE is a base offset to be added to all the offsets in the | |
164 | // vector. | |
165 | void | |
166 | read_multiple(off_t base, const Read_multiple&); | |
167 | ||
e44fcf3b ILT |
168 | // Dump statistical information to stderr. |
169 | static void | |
170 | print_stats(); | |
171 | ||
bae7f79e ILT |
172 | private: |
173 | // This class may not be copied. | |
174 | File_read(const File_read&); | |
175 | File_read& operator=(const File_read&); | |
176 | ||
17a1d0a9 ILT |
177 | // Total bytes mapped into memory during the link. This variable |
178 | // may not be accurate when running multi-threaded. | |
e44fcf3b ILT |
179 | static unsigned long long total_mapped_bytes; |
180 | ||
181 | // Current number of bytes mapped into memory during the link. This | |
17a1d0a9 | 182 | // variable may not be accurate when running multi-threaded. |
e44fcf3b ILT |
183 | static unsigned long long current_mapped_bytes; |
184 | ||
185 | // High water mark of bytes mapped into memory during the link. | |
17a1d0a9 | 186 | // This variable may not be accurate when running multi-threaded. |
e44fcf3b ILT |
187 | static unsigned long long maximum_mapped_bytes; |
188 | ||
d1038c21 | 189 | // A view into the file. |
bae7f79e ILT |
190 | class View |
191 | { | |
192 | public: | |
8383303e ILT |
193 | View(off_t start, section_size_type size, const unsigned char* data, |
194 | bool cache, bool mapped) | |
9eb9fa57 | 195 | : start_(start), size_(size), data_(data), lock_count_(0), |
cb295612 | 196 | cache_(cache), mapped_(mapped), accessed_(true) |
bae7f79e ILT |
197 | { } |
198 | ||
199 | ~View(); | |
200 | ||
201 | off_t | |
202 | start() const | |
203 | { return this->start_; } | |
204 | ||
8383303e | 205 | section_size_type |
bae7f79e ILT |
206 | size() const |
207 | { return this->size_; } | |
208 | ||
e214a02b | 209 | const unsigned char* |
bae7f79e ILT |
210 | data() const |
211 | { return this->data_; } | |
212 | ||
213 | void | |
214 | lock(); | |
215 | ||
216 | void | |
217 | unlock(); | |
218 | ||
219 | bool | |
220 | is_locked(); | |
221 | ||
9eb9fa57 ILT |
222 | void |
223 | set_cache() | |
224 | { this->cache_ = true; } | |
225 | ||
cb295612 ILT |
226 | void |
227 | clear_cache() | |
228 | { this->cache_ = false; } | |
229 | ||
9eb9fa57 ILT |
230 | bool |
231 | should_cache() const | |
232 | { return this->cache_; } | |
233 | ||
cb295612 ILT |
234 | void |
235 | set_accessed() | |
236 | { this->accessed_ = true; } | |
237 | ||
238 | void | |
239 | clear_accessed() | |
240 | { this->accessed_= false; } | |
241 | ||
242 | bool | |
243 | accessed() const | |
244 | { return this->accessed_; } | |
245 | ||
bae7f79e ILT |
246 | private: |
247 | View(const View&); | |
248 | View& operator=(const View&); | |
249 | ||
250 | off_t start_; | |
8383303e | 251 | section_size_type size_; |
e214a02b | 252 | const unsigned char* data_; |
bae7f79e | 253 | int lock_count_; |
9eb9fa57 | 254 | bool cache_; |
d1038c21 | 255 | bool mapped_; |
cb295612 | 256 | bool accessed_; |
bae7f79e ILT |
257 | }; |
258 | ||
e44fcf3b | 259 | friend class View; |
bae7f79e ILT |
260 | friend class File_view; |
261 | ||
ead1e424 | 262 | // Find a view into the file. |
bae7f79e | 263 | View* |
8383303e | 264 | find_view(off_t start, section_size_type size) const; |
bae7f79e | 265 | |
ead1e424 | 266 | // Read data from the file into a buffer. |
82dcae9d | 267 | void |
fe8718a4 | 268 | do_read(off_t start, section_size_type size, void* p) const; |
bae7f79e | 269 | |
ead1e424 | 270 | // Find or make a view into the file. |
bae7f79e | 271 | View* |
8383303e | 272 | find_or_make_view(off_t start, section_size_type size, bool cache); |
bae7f79e | 273 | |
ead1e424 | 274 | // Clear the file views. |
bae7f79e ILT |
275 | void |
276 | clear_views(bool); | |
277 | ||
ead1e424 ILT |
278 | // The size of a file page for buffering data. |
279 | static const off_t page_size = 8192; | |
280 | ||
281 | // Given a file offset, return the page offset. | |
282 | static off_t | |
283 | page_offset(off_t file_offset) | |
284 | { return file_offset & ~ (page_size - 1); } | |
285 | ||
286 | // Given a file size, return the size to read integral pages. | |
287 | static off_t | |
288 | pages(off_t file_size) | |
289 | { return (file_size + (page_size - 1)) & ~ (page_size - 1); } | |
290 | ||
291 | // The type of a mapping from page start to views. | |
292 | typedef std::map<off_t, View*> Views; | |
293 | ||
294 | // A simple list of Views. | |
295 | typedef std::list<View*> Saved_views; | |
296 | ||
cb295612 ILT |
297 | // The maximum number of entries we will pass to ::readv. |
298 | static const size_t max_readv_entries = 128; | |
299 | ||
300 | // Use readv to read data. | |
301 | void | |
302 | do_readv(off_t base, const Read_multiple&, size_t start, size_t count); | |
303 | ||
ead1e424 | 304 | // File name. |
bae7f79e | 305 | std::string name_; |
ead1e424 | 306 | // File descriptor. |
bae7f79e | 307 | int descriptor_; |
cb295612 ILT |
308 | // The number of objects associated with this file. This will be |
309 | // more than 1 in the case of an archive. | |
310 | int object_count_; | |
82dcae9d ILT |
311 | // File size. |
312 | off_t size_; | |
17a1d0a9 ILT |
313 | // A token used to lock the file. |
314 | Task_token token_; | |
ead1e424 ILT |
315 | // Buffered views into the file. |
316 | Views views_; | |
317 | // List of views which were locked but had to be removed from views_ | |
318 | // because they were not large enough. | |
319 | Saved_views saved_views_; | |
5a6f7e2d ILT |
320 | // Specified file contents. Used only for testing purposes. |
321 | const unsigned char* contents_; | |
e44fcf3b ILT |
322 | // Total amount of space mapped into memory. This is only changed |
323 | // while the file is locked. When we unlock the file, we transfer | |
324 | // the total to total_mapped_bytes, and reset this to zero. | |
325 | size_t mapped_bytes_; | |
17a1d0a9 ILT |
326 | // Whether the file was released. |
327 | bool released_; | |
bae7f79e ILT |
328 | }; |
329 | ||
330 | // A view of file data that persists even when the file is unlocked. | |
331 | // Callers should destroy these when no longer required. These are | |
332 | // obtained form File_read::get_lasting_view. They may only be | |
333 | // destroyed when the underlying File_read is locked. | |
334 | ||
335 | class File_view | |
336 | { | |
337 | public: | |
338 | // This may only be called when the underlying File_read is locked. | |
339 | ~File_view(); | |
340 | ||
341 | // Return a pointer to the data associated with this view. | |
342 | const unsigned char* | |
343 | data() const | |
344 | { return this->data_; } | |
345 | ||
346 | private: | |
347 | File_view(const File_view&); | |
348 | File_view& operator=(const File_view&); | |
349 | ||
350 | friend class File_read; | |
351 | ||
352 | // Callers have to get these via File_read::get_lasting_view. | |
353 | File_view(File_read& file, File_read::View* view, const unsigned char* data) | |
354 | : file_(file), view_(view), data_(data) | |
355 | { } | |
356 | ||
357 | File_read& file_; | |
358 | File_read::View* view_; | |
359 | const unsigned char* data_; | |
360 | }; | |
361 | ||
bae7f79e ILT |
362 | // All the information we hold for a single input file. This can be |
363 | // an object file, a shared library, or an archive. | |
364 | ||
365 | class Input_file | |
366 | { | |
367 | public: | |
5a6f7e2d | 368 | Input_file(const Input_file_argument* input_argument) |
e2aacd2c ILT |
369 | : input_argument_(input_argument), found_name_(), file_(), |
370 | is_in_sysroot_(false) | |
bae7f79e ILT |
371 | { } |
372 | ||
5a6f7e2d ILT |
373 | // Create an input file with the contents already provided. This is |
374 | // only used for testing. With this path, don't call the open | |
375 | // method. | |
17a1d0a9 ILT |
376 | Input_file(const Task*, const char* name, const unsigned char* contents, |
377 | off_t size); | |
5a6f7e2d | 378 | |
75f2446e ILT |
379 | // Open the file. If the open fails, this will report an error and |
380 | // return false. | |
381 | bool | |
17a1d0a9 | 382 | open(const General_options&, const Dirsearch&, const Task*); |
bae7f79e | 383 | |
e2aacd2c | 384 | // Return the name given by the user. For -lc this will return "c". |
bae7f79e ILT |
385 | const char* |
386 | name() const | |
5a6f7e2d | 387 | { return this->input_argument_->name(); } |
bae7f79e | 388 | |
e2aacd2c ILT |
389 | // Return the file name. For -lc this will return something like |
390 | // "/usr/lib/libc.so". | |
bae7f79e ILT |
391 | const std::string& |
392 | filename() const | |
393 | { return this->file_.filename(); } | |
394 | ||
e2aacd2c ILT |
395 | // Return the name under which we found the file, corresponding to |
396 | // the command line. For -lc this will return something like | |
397 | // "libc.so". | |
398 | const std::string& | |
399 | found_name() const | |
400 | { return this->found_name_; } | |
401 | ||
4973341a ILT |
402 | // Return the position dependent options. |
403 | const Position_dependent_options& | |
404 | options() const | |
405 | { return this->input_argument_->options(); } | |
406 | ||
407 | // Return the file. | |
bae7f79e ILT |
408 | File_read& |
409 | file() | |
410 | { return this->file_; } | |
411 | ||
7004837e ILT |
412 | const File_read& |
413 | file() const | |
414 | { return this->file_; } | |
415 | ||
ad2d6943 ILT |
416 | // Whether we found the file in a directory in the system root. |
417 | bool | |
418 | is_in_sysroot() const | |
419 | { return this->is_in_sysroot_; } | |
420 | ||
bae7f79e | 421 | private: |
ead1e424 ILT |
422 | Input_file(const Input_file&); |
423 | Input_file& operator=(const Input_file&); | |
424 | ||
ad2d6943 | 425 | // The argument from the command line. |
5a6f7e2d | 426 | const Input_file_argument* input_argument_; |
e2aacd2c ILT |
427 | // The name under which we opened the file. This is like the name |
428 | // on the command line, but -lc turns into libc.so (or whatever). | |
429 | // It only includes the full path if the path was on the command | |
430 | // line. | |
431 | std::string found_name_; | |
ad2d6943 | 432 | // The file after we open it. |
bae7f79e | 433 | File_read file_; |
ad2d6943 ILT |
434 | // Whether we found the file in a directory in the system root. |
435 | bool is_in_sysroot_; | |
bae7f79e ILT |
436 | }; |
437 | ||
438 | } // end namespace gold | |
439 | ||
440 | #endif // !defined(GOLD_FILEREAD_H) |