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" | |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
37 | class Dirsearch; | |
bae7f79e ILT |
38 | class File_view; |
39 | ||
40 | // File_read manages a file descriptor for a file we are reading. We | |
41 | // close file descriptors if we run out of them, so this class reopens | |
42 | // the file as needed. | |
43 | ||
44 | class File_read | |
45 | { | |
46 | public: | |
47 | File_read() | |
82dcae9d | 48 | : name_(), descriptor_(-1), size_(0), lock_count_(0), views_(), |
e44fcf3b | 49 | saved_views_(), contents_(NULL), mapped_bytes_(0) |
bae7f79e | 50 | { } |
5a6f7e2d | 51 | |
bae7f79e ILT |
52 | ~File_read(); |
53 | ||
54 | // Open a file. | |
55 | bool | |
56 | open(const std::string& name); | |
57 | ||
5a6f7e2d ILT |
58 | // Pretend to open the file, but provide the file contents. No |
59 | // actual file system activity will occur. This is used for | |
60 | // testing. | |
61 | bool | |
62 | open(const std::string& name, const unsigned char* contents, off_t size); | |
63 | ||
bae7f79e ILT |
64 | // Return the file name. |
65 | const std::string& | |
66 | filename() const | |
67 | { return this->name_; } | |
68 | ||
bae7f79e ILT |
69 | // Lock the file for access within a particular Task::run execution. |
70 | // This means that the descriptor can not be closed. This routine | |
71 | // may only be called from the main thread. | |
72 | void | |
73 | lock(); | |
74 | ||
75 | // Unlock the descriptor, permitting it to be closed if necessary. | |
76 | void | |
77 | unlock(); | |
4973341a | 78 | |
bae7f79e ILT |
79 | // Test whether the object is locked. |
80 | bool | |
81 | is_locked(); | |
82 | ||
82dcae9d ILT |
83 | // Return the size of the file. |
84 | off_t | |
85 | filesize() const | |
86 | { return this->size_; } | |
87 | ||
ba45d247 ILT |
88 | // Return a view into the file starting at file offset START for |
89 | // SIZE bytes. The pointer will remain valid until the File_read is | |
90 | // unlocked. It is an error if we can not read enough data from the | |
9eb9fa57 ILT |
91 | // file. The CACHE parameter is a hint as to whether it will be |
92 | // useful to cache this data for later accesses--i.e., later calls | |
93 | // to get_view, read, or get_lasting_view which retrieve the same | |
94 | // data. | |
bae7f79e | 95 | const unsigned char* |
9eb9fa57 | 96 | get_view(off_t start, off_t size, bool cache); |
bae7f79e | 97 | |
ba45d247 ILT |
98 | // Read data from the file into the buffer P starting at file offset |
99 | // START for SIZE bytes. | |
100 | void | |
101 | read(off_t start, off_t size, void* p); | |
102 | ||
ba45d247 ILT |
103 | // Return a lasting view into the file starting at file offset START |
104 | // for SIZE bytes. This is allocated with new, and the caller is | |
105 | // responsible for deleting it when done. The data associated with | |
106 | // this view will remain valid until the view is deleted. It is an | |
9eb9fa57 ILT |
107 | // error if we can not read enough data from the file. The CACHE |
108 | // parameter is as in get_view. | |
bae7f79e | 109 | File_view* |
9eb9fa57 | 110 | get_lasting_view(off_t start, off_t size, bool cache); |
bae7f79e | 111 | |
e44fcf3b ILT |
112 | // Dump statistical information to stderr. |
113 | static void | |
114 | print_stats(); | |
115 | ||
bae7f79e ILT |
116 | private: |
117 | // This class may not be copied. | |
118 | File_read(const File_read&); | |
119 | File_read& operator=(const File_read&); | |
120 | ||
e44fcf3b ILT |
121 | // Total bytes mapped into memory during the link. This variable is |
122 | // only accessed from the main thread, when unlocking the object. | |
123 | static unsigned long long total_mapped_bytes; | |
124 | ||
125 | // Current number of bytes mapped into memory during the link. This | |
126 | // variable is only accessed from the main thread. | |
127 | static unsigned long long current_mapped_bytes; | |
128 | ||
129 | // High water mark of bytes mapped into memory during the link. | |
130 | // This variable is only accessed from the main thread. | |
131 | static unsigned long long maximum_mapped_bytes; | |
132 | ||
d1038c21 | 133 | // A view into the file. |
bae7f79e ILT |
134 | class View |
135 | { | |
136 | public: | |
d1038c21 ILT |
137 | View(off_t start, off_t size, const unsigned char* data, bool cache, |
138 | bool mapped) | |
9eb9fa57 | 139 | : start_(start), size_(size), data_(data), lock_count_(0), |
d1038c21 | 140 | cache_(cache), mapped_(mapped) |
bae7f79e ILT |
141 | { } |
142 | ||
143 | ~View(); | |
144 | ||
145 | off_t | |
146 | start() const | |
147 | { return this->start_; } | |
148 | ||
149 | off_t | |
150 | size() const | |
151 | { return this->size_; } | |
152 | ||
e214a02b | 153 | const unsigned char* |
bae7f79e ILT |
154 | data() const |
155 | { return this->data_; } | |
156 | ||
157 | void | |
158 | lock(); | |
159 | ||
160 | void | |
161 | unlock(); | |
162 | ||
163 | bool | |
164 | is_locked(); | |
165 | ||
9eb9fa57 ILT |
166 | void |
167 | set_cache() | |
168 | { this->cache_ = true; } | |
169 | ||
170 | bool | |
171 | should_cache() const | |
172 | { return this->cache_; } | |
173 | ||
bae7f79e ILT |
174 | private: |
175 | View(const View&); | |
176 | View& operator=(const View&); | |
177 | ||
178 | off_t start_; | |
179 | off_t size_; | |
e214a02b | 180 | const unsigned char* data_; |
bae7f79e | 181 | int lock_count_; |
9eb9fa57 | 182 | bool cache_; |
d1038c21 | 183 | bool mapped_; |
bae7f79e ILT |
184 | }; |
185 | ||
e44fcf3b | 186 | friend class View; |
bae7f79e ILT |
187 | friend class File_view; |
188 | ||
ead1e424 | 189 | // Find a view into the file. |
bae7f79e ILT |
190 | View* |
191 | find_view(off_t start, off_t size); | |
192 | ||
ead1e424 | 193 | // Read data from the file into a buffer. |
82dcae9d | 194 | void |
9eb9fa57 | 195 | do_read(off_t start, off_t size, void* p); |
bae7f79e | 196 | |
ead1e424 | 197 | // Find or make a view into the file. |
bae7f79e | 198 | View* |
9eb9fa57 | 199 | find_or_make_view(off_t start, off_t size, bool cache); |
bae7f79e | 200 | |
ead1e424 | 201 | // Clear the file views. |
bae7f79e ILT |
202 | void |
203 | clear_views(bool); | |
204 | ||
ead1e424 ILT |
205 | // The size of a file page for buffering data. |
206 | static const off_t page_size = 8192; | |
207 | ||
208 | // Given a file offset, return the page offset. | |
209 | static off_t | |
210 | page_offset(off_t file_offset) | |
211 | { return file_offset & ~ (page_size - 1); } | |
212 | ||
213 | // Given a file size, return the size to read integral pages. | |
214 | static off_t | |
215 | pages(off_t file_size) | |
216 | { return (file_size + (page_size - 1)) & ~ (page_size - 1); } | |
217 | ||
218 | // The type of a mapping from page start to views. | |
219 | typedef std::map<off_t, View*> Views; | |
220 | ||
221 | // A simple list of Views. | |
222 | typedef std::list<View*> Saved_views; | |
223 | ||
224 | // File name. | |
bae7f79e | 225 | std::string name_; |
ead1e424 | 226 | // File descriptor. |
bae7f79e | 227 | int descriptor_; |
82dcae9d ILT |
228 | // File size. |
229 | off_t size_; | |
ead1e424 | 230 | // Number of locks on the file. |
bae7f79e | 231 | int lock_count_; |
ead1e424 ILT |
232 | // Buffered views into the file. |
233 | Views views_; | |
234 | // List of views which were locked but had to be removed from views_ | |
235 | // because they were not large enough. | |
236 | Saved_views saved_views_; | |
5a6f7e2d ILT |
237 | // Specified file contents. Used only for testing purposes. |
238 | const unsigned char* contents_; | |
e44fcf3b ILT |
239 | // Total amount of space mapped into memory. This is only changed |
240 | // while the file is locked. When we unlock the file, we transfer | |
241 | // the total to total_mapped_bytes, and reset this to zero. | |
242 | size_t mapped_bytes_; | |
bae7f79e ILT |
243 | }; |
244 | ||
245 | // A view of file data that persists even when the file is unlocked. | |
246 | // Callers should destroy these when no longer required. These are | |
247 | // obtained form File_read::get_lasting_view. They may only be | |
248 | // destroyed when the underlying File_read is locked. | |
249 | ||
250 | class File_view | |
251 | { | |
252 | public: | |
253 | // This may only be called when the underlying File_read is locked. | |
254 | ~File_view(); | |
255 | ||
256 | // Return a pointer to the data associated with this view. | |
257 | const unsigned char* | |
258 | data() const | |
259 | { return this->data_; } | |
260 | ||
261 | private: | |
262 | File_view(const File_view&); | |
263 | File_view& operator=(const File_view&); | |
264 | ||
265 | friend class File_read; | |
266 | ||
267 | // Callers have to get these via File_read::get_lasting_view. | |
268 | File_view(File_read& file, File_read::View* view, const unsigned char* data) | |
269 | : file_(file), view_(view), data_(data) | |
270 | { } | |
271 | ||
272 | File_read& file_; | |
273 | File_read::View* view_; | |
274 | const unsigned char* data_; | |
275 | }; | |
276 | ||
bae7f79e ILT |
277 | // All the information we hold for a single input file. This can be |
278 | // an object file, a shared library, or an archive. | |
279 | ||
280 | class Input_file | |
281 | { | |
282 | public: | |
5a6f7e2d | 283 | Input_file(const Input_file_argument* input_argument) |
e2aacd2c ILT |
284 | : input_argument_(input_argument), found_name_(), file_(), |
285 | is_in_sysroot_(false) | |
bae7f79e ILT |
286 | { } |
287 | ||
5a6f7e2d ILT |
288 | // Create an input file with the contents already provided. This is |
289 | // only used for testing. With this path, don't call the open | |
290 | // method. | |
291 | Input_file(const char* name, const unsigned char* contents, off_t size); | |
292 | ||
75f2446e ILT |
293 | // Open the file. If the open fails, this will report an error and |
294 | // return false. | |
295 | bool | |
bae7f79e ILT |
296 | open(const General_options&, const Dirsearch&); |
297 | ||
e2aacd2c | 298 | // Return the name given by the user. For -lc this will return "c". |
bae7f79e ILT |
299 | const char* |
300 | name() const | |
5a6f7e2d | 301 | { return this->input_argument_->name(); } |
bae7f79e | 302 | |
e2aacd2c ILT |
303 | // Return the file name. For -lc this will return something like |
304 | // "/usr/lib/libc.so". | |
bae7f79e ILT |
305 | const std::string& |
306 | filename() const | |
307 | { return this->file_.filename(); } | |
308 | ||
e2aacd2c ILT |
309 | // Return the name under which we found the file, corresponding to |
310 | // the command line. For -lc this will return something like | |
311 | // "libc.so". | |
312 | const std::string& | |
313 | found_name() const | |
314 | { return this->found_name_; } | |
315 | ||
4973341a ILT |
316 | // Return the position dependent options. |
317 | const Position_dependent_options& | |
318 | options() const | |
319 | { return this->input_argument_->options(); } | |
320 | ||
321 | // Return the file. | |
bae7f79e ILT |
322 | File_read& |
323 | file() | |
324 | { return this->file_; } | |
325 | ||
ad2d6943 ILT |
326 | // Whether we found the file in a directory in the system root. |
327 | bool | |
328 | is_in_sysroot() const | |
329 | { return this->is_in_sysroot_; } | |
330 | ||
bae7f79e | 331 | private: |
ead1e424 ILT |
332 | Input_file(const Input_file&); |
333 | Input_file& operator=(const Input_file&); | |
334 | ||
ad2d6943 | 335 | // The argument from the command line. |
5a6f7e2d | 336 | const Input_file_argument* input_argument_; |
e2aacd2c ILT |
337 | // The name under which we opened the file. This is like the name |
338 | // on the command line, but -lc turns into libc.so (or whatever). | |
339 | // It only includes the full path if the path was on the command | |
340 | // line. | |
341 | std::string found_name_; | |
ad2d6943 | 342 | // The file after we open it. |
bae7f79e | 343 | File_read file_; |
ad2d6943 ILT |
344 | // Whether we found the file in a directory in the system root. |
345 | bool is_in_sysroot_; | |
bae7f79e ILT |
346 | }; |
347 | ||
348 | } // end namespace gold | |
349 | ||
350 | #endif // !defined(GOLD_FILEREAD_H) |