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