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