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