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