]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/fileread.h
*** empty log message ***
[thirdparty/binutils-gdb.git] / gold / fileread.h
CommitLineData
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
35namespace gold
36{
37
38class Dirsearch;
bae7f79e
ILT
39class 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
45class File_read
46{
47 public:
48 File_read()
17a1d0a9
ILT
49 : name_(), descriptor_(-1), size_(0), token_(false), views_(),
50 saved_views_(), contents_(NULL), mapped_bytes_(0), released_(true)
bae7f79e 51 { }
5a6f7e2d 52
bae7f79e
ILT
53 ~File_read();
54
55 // Open a file.
56 bool
17a1d0a9 57 open(const Task*, const std::string& name);
bae7f79e 58
5a6f7e2d
ILT
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
17a1d0a9
ILT
63 open(const Task*, const std::string& name, const unsigned char* contents,
64 off_t size);
5a6f7e2d 65
bae7f79e
ILT
66 // Return the file name.
67 const std::string&
68 filename() const
69 { return this->name_; }
70
17a1d0a9
ILT
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.
bae7f79e 74 void
17a1d0a9 75 lock(const Task* t);
bae7f79e
ILT
76
77 // Unlock the descriptor, permitting it to be closed if necessary.
78 void
17a1d0a9 79 unlock(const Task* t);
4973341a 80
bae7f79e
ILT
81 // Test whether the object is locked.
82 bool
7004837e 83 is_locked() const;
bae7f79e 84
17a1d0a9
ILT
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
82dcae9d
ILT
100 // Return the size of the file.
101 off_t
102 filesize() const
103 { return this->size_; }
104
ba45d247
ILT
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
9eb9fa57
ILT
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.
bae7f79e 112 const unsigned char*
9eb9fa57 113 get_view(off_t start, off_t size, bool cache);
bae7f79e 114
ba45d247
ILT
115 // Read data from the file into the buffer P starting at file offset
116 // START for SIZE bytes.
117 void
c71c6f56 118 read(off_t start, off_t size, void* p) const;
ba45d247 119
ba45d247
ILT
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
9eb9fa57
ILT
124 // error if we can not read enough data from the file. The CACHE
125 // parameter is as in get_view.
bae7f79e 126 File_view*
9eb9fa57 127 get_lasting_view(off_t start, off_t size, bool cache);
bae7f79e 128
e44fcf3b
ILT
129 // Dump statistical information to stderr.
130 static void
131 print_stats();
132
bae7f79e
ILT
133 private:
134 // This class may not be copied.
135 File_read(const File_read&);
136 File_read& operator=(const File_read&);
137
17a1d0a9
ILT
138 // Total bytes mapped into memory during the link. This variable
139 // may not be accurate when running multi-threaded.
e44fcf3b
ILT
140 static unsigned long long total_mapped_bytes;
141
142 // Current number of bytes mapped into memory during the link. This
17a1d0a9 143 // variable may not be accurate when running multi-threaded.
e44fcf3b
ILT
144 static unsigned long long current_mapped_bytes;
145
146 // High water mark of bytes mapped into memory during the link.
17a1d0a9 147 // This variable may not be accurate when running multi-threaded.
e44fcf3b
ILT
148 static unsigned long long maximum_mapped_bytes;
149
d1038c21 150 // A view into the file.
bae7f79e
ILT
151 class View
152 {
153 public:
d1038c21
ILT
154 View(off_t start, off_t size, const unsigned char* data, bool cache,
155 bool mapped)
9eb9fa57 156 : start_(start), size_(size), data_(data), lock_count_(0),
d1038c21 157 cache_(cache), mapped_(mapped)
bae7f79e
ILT
158 { }
159
160 ~View();
161
162 off_t
163 start() const
164 { return this->start_; }
165
166 off_t
167 size() const
168 { return this->size_; }
169
e214a02b 170 const unsigned char*
bae7f79e
ILT
171 data() const
172 { return this->data_; }
173
174 void
175 lock();
176
177 void
178 unlock();
179
180 bool
181 is_locked();
182
9eb9fa57
ILT
183 void
184 set_cache()
185 { this->cache_ = true; }
186
187 bool
188 should_cache() const
189 { return this->cache_; }
190
bae7f79e
ILT
191 private:
192 View(const View&);
193 View& operator=(const View&);
194
195 off_t start_;
196 off_t size_;
e214a02b 197 const unsigned char* data_;
bae7f79e 198 int lock_count_;
9eb9fa57 199 bool cache_;
d1038c21 200 bool mapped_;
bae7f79e
ILT
201 };
202
e44fcf3b 203 friend class View;
bae7f79e
ILT
204 friend class File_view;
205
ead1e424 206 // Find a view into the file.
bae7f79e 207 View*
c71c6f56 208 find_view(off_t start, off_t size) const;
bae7f79e 209
ead1e424 210 // Read data from the file into a buffer.
82dcae9d 211 void
c71c6f56 212 do_read(off_t start, off_t size, void* p) const;
bae7f79e 213
ead1e424 214 // Find or make a view into the file.
bae7f79e 215 View*
9eb9fa57 216 find_or_make_view(off_t start, off_t size, bool cache);
bae7f79e 217
ead1e424 218 // Clear the file views.
bae7f79e
ILT
219 void
220 clear_views(bool);
221
ead1e424
ILT
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.
bae7f79e 242 std::string name_;
ead1e424 243 // File descriptor.
bae7f79e 244 int descriptor_;
82dcae9d
ILT
245 // File size.
246 off_t size_;
17a1d0a9
ILT
247 // A token used to lock the file.
248 Task_token token_;
ead1e424
ILT
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_;
5a6f7e2d
ILT
254 // Specified file contents. Used only for testing purposes.
255 const unsigned char* contents_;
e44fcf3b
ILT
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_;
17a1d0a9
ILT
260 // Whether the file was released.
261 bool released_;
bae7f79e
ILT
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
269class 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
bae7f79e
ILT
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
299class Input_file
300{
301 public:
5a6f7e2d 302 Input_file(const Input_file_argument* input_argument)
e2aacd2c
ILT
303 : input_argument_(input_argument), found_name_(), file_(),
304 is_in_sysroot_(false)
bae7f79e
ILT
305 { }
306
5a6f7e2d
ILT
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.
17a1d0a9
ILT
310 Input_file(const Task*, const char* name, const unsigned char* contents,
311 off_t size);
5a6f7e2d 312
75f2446e
ILT
313 // Open the file. If the open fails, this will report an error and
314 // return false.
315 bool
17a1d0a9 316 open(const General_options&, const Dirsearch&, const Task*);
bae7f79e 317
e2aacd2c 318 // Return the name given by the user. For -lc this will return "c".
bae7f79e
ILT
319 const char*
320 name() const
5a6f7e2d 321 { return this->input_argument_->name(); }
bae7f79e 322
e2aacd2c
ILT
323 // Return the file name. For -lc this will return something like
324 // "/usr/lib/libc.so".
bae7f79e
ILT
325 const std::string&
326 filename() const
327 { return this->file_.filename(); }
328
e2aacd2c
ILT
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
4973341a
ILT
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.
bae7f79e
ILT
342 File_read&
343 file()
344 { return this->file_; }
345
7004837e
ILT
346 const File_read&
347 file() const
348 { return this->file_; }
349
ad2d6943
ILT
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
bae7f79e 355 private:
ead1e424
ILT
356 Input_file(const Input_file&);
357 Input_file& operator=(const Input_file&);
358
ad2d6943 359 // The argument from the command line.
5a6f7e2d 360 const Input_file_argument* input_argument_;
e2aacd2c
ILT
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_;
ad2d6943 366 // The file after we open it.
bae7f79e 367 File_read file_;
ad2d6943
ILT
368 // Whether we found the file in a directory in the system root.
369 bool is_in_sysroot_;
bae7f79e
ILT
370};
371
372} // end namespace gold
373
374#endif // !defined(GOLD_FILEREAD_H)