* plugin-api.h (LDPS_BAD_HANDLE): New constant.
[deliverable/binutils-gdb.git] / gold / fileread.h
CommitLineData
bae7f79e
ILT
1// fileread.h -- read files for gold -*- C++ -*-
2
ebdbb458 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
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
35namespace gold
36{
37
14144f39
ILT
38class Position_dependent_options;
39class Input_file_argument;
bae7f79e 40class Dirsearch;
bae7f79e
ILT
41class File_view;
42
2a00e4fb
ILT
43// File_read manages a file descriptor and mappings for a file we are
44// reading.
bae7f79e
ILT
45
46class File_read
47{
48 public:
49 File_read()
2a00e4fb
ILT
50 : name_(), descriptor_(-1), is_descriptor_opened_(false), object_count_(0),
51 size_(0), token_(false), views_(), saved_views_(), contents_(NULL),
52 mapped_bytes_(0), 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 83 // Lock the file for exclusive access within a particular Task::run
2a00e4fb
ILT
84 // execution. This routine may only be called when the workqueue
85 // lock is held.
bae7f79e 86 void
17a1d0a9 87 lock(const Task* t);
bae7f79e 88
2a00e4fb 89 // Unlock the file.
bae7f79e 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
89fc3421
CC
112 // Claim the file for a plugin. This effectively releases the file without
113 // closing it; the plugin will assume responsibility for closing it.
114 void
115 claim_for_plugin();
116
82dcae9d
ILT
117 // Return the size of the file.
118 off_t
119 filesize() const
120 { return this->size_; }
121
ba45d247 122 // Return a view into the file starting at file offset START for
39d0cb0e
ILT
123 // SIZE bytes. OFFSET is the offset into the input file for the
124 // file we are reading; this is zero for a normal object file,
125 // non-zero for an object file in an archive. ALIGNED is true if
126 // the data must be naturally aligned; this only matters when OFFSET
127 // is not zero. The pointer will remain valid until the File_read
128 // is unlocked. It is an error if we can not read enough data from
129 // the file. The CACHE parameter is a hint as to whether it will be
9eb9fa57
ILT
130 // useful to cache this data for later accesses--i.e., later calls
131 // to get_view, read, or get_lasting_view which retrieve the same
132 // data.
bae7f79e 133 const unsigned char*
39d0cb0e
ILT
134 get_view(off_t offset, off_t start, section_size_type size, bool aligned,
135 bool cache);
bae7f79e 136
ba45d247
ILT
137 // Read data from the file into the buffer P starting at file offset
138 // START for SIZE bytes.
139 void
2a00e4fb 140 read(off_t start, section_size_type size, void* p);
ba45d247 141
ba45d247
ILT
142 // Return a lasting view into the file starting at file offset START
143 // for SIZE bytes. This is allocated with new, and the caller is
144 // responsible for deleting it when done. The data associated with
145 // this view will remain valid until the view is deleted. It is an
39d0cb0e
ILT
146 // error if we can not read enough data from the file. The OFFSET,
147 // ALIGNED and CACHE parameters are as in get_view.
bae7f79e 148 File_view*
39d0cb0e
ILT
149 get_lasting_view(off_t offset, off_t start, section_size_type size,
150 bool aligned, bool cache);
bae7f79e 151
cb295612
ILT
152 // Mark all views as no longer cached.
153 void
154 clear_view_cache_marks();
155
39d0cb0e
ILT
156 // Discard all uncached views. This is normally done by release(),
157 // but not for objects in archives. FIXME: This is a complicated
158 // interface, and it would be nice to have something more automatic.
159 void
160 clear_uncached_views()
161 { this->clear_views(false); }
162
cb295612
ILT
163 // A struct used to do a multiple read.
164 struct Read_multiple_entry
165 {
166 // The file offset of the data to read.
167 off_t file_offset;
168 // The amount of data to read.
169 section_size_type size;
170 // The buffer where the data should be placed.
171 unsigned char* buffer;
172
173 Read_multiple_entry(off_t o, section_size_type s, unsigned char* b)
174 : file_offset(o), size(s), buffer(b)
175 { }
176 };
177
178 typedef std::vector<Read_multiple_entry> Read_multiple;
179
180 // Read a bunch of data from the file into various different
181 // locations. The vector must be sorted by ascending file_offset.
182 // BASE is a base offset to be added to all the offsets in the
183 // vector.
184 void
185 read_multiple(off_t base, const Read_multiple&);
186
e44fcf3b
ILT
187 // Dump statistical information to stderr.
188 static void
189 print_stats();
190
89fc3421
CC
191 // Return the open file descriptor (for plugins).
192 int
193 descriptor() const
194 {
195 gold_assert(this->descriptor_ >= 0);
196 return this->descriptor_;
197 }
198
bae7f79e
ILT
199 private:
200 // This class may not be copied.
201 File_read(const File_read&);
202 File_read& operator=(const File_read&);
203
17a1d0a9
ILT
204 // Total bytes mapped into memory during the link. This variable
205 // may not be accurate when running multi-threaded.
e44fcf3b
ILT
206 static unsigned long long total_mapped_bytes;
207
208 // Current number of bytes mapped into memory during the link. This
17a1d0a9 209 // variable may not be accurate when running multi-threaded.
e44fcf3b
ILT
210 static unsigned long long current_mapped_bytes;
211
212 // High water mark of bytes mapped into memory during the link.
17a1d0a9 213 // This variable may not be accurate when running multi-threaded.
e44fcf3b
ILT
214 static unsigned long long maximum_mapped_bytes;
215
d1038c21 216 // A view into the file.
bae7f79e
ILT
217 class View
218 {
219 public:
8383303e 220 View(off_t start, section_size_type size, const unsigned char* data,
39d0cb0e 221 unsigned int byteshift, bool cache, bool mapped)
9eb9fa57 222 : start_(start), size_(size), data_(data), lock_count_(0),
39d0cb0e 223 byteshift_(byteshift), cache_(cache), mapped_(mapped), accessed_(true)
bae7f79e
ILT
224 { }
225
226 ~View();
227
228 off_t
229 start() const
230 { return this->start_; }
231
8383303e 232 section_size_type
bae7f79e
ILT
233 size() const
234 { return this->size_; }
235
e214a02b 236 const unsigned char*
bae7f79e
ILT
237 data() const
238 { return this->data_; }
239
240 void
241 lock();
242
243 void
244 unlock();
245
246 bool
247 is_locked();
248
39d0cb0e
ILT
249 unsigned int
250 byteshift() const
251 { return this->byteshift_; }
252
9eb9fa57
ILT
253 void
254 set_cache()
255 { this->cache_ = true; }
256
cb295612
ILT
257 void
258 clear_cache()
259 { this->cache_ = false; }
260
9eb9fa57
ILT
261 bool
262 should_cache() const
263 { return this->cache_; }
264
cb295612
ILT
265 void
266 set_accessed()
267 { this->accessed_ = true; }
268
269 void
270 clear_accessed()
271 { this->accessed_= false; }
272
273 bool
274 accessed() const
275 { return this->accessed_; }
276
bae7f79e
ILT
277 private:
278 View(const View&);
279 View& operator=(const View&);
280
39d0cb0e 281 // The file offset of the start of the view.
bae7f79e 282 off_t start_;
39d0cb0e 283 // The size of the view.
8383303e 284 section_size_type size_;
39d0cb0e 285 // A pointer to the actual bytes.
e214a02b 286 const unsigned char* data_;
39d0cb0e 287 // The number of locks on this view.
bae7f79e 288 int lock_count_;
39d0cb0e
ILT
289 // The number of bytes that the view is shifted relative to the
290 // underlying file. This is used to align data. This is normally
291 // zero, except possibly for an object in an archive.
292 unsigned int byteshift_;
293 // Whether the view is cached.
9eb9fa57 294 bool cache_;
39d0cb0e
ILT
295 // Whether the view is mapped into memory. If not, data_ points
296 // to memory allocated using new[].
d1038c21 297 bool mapped_;
39d0cb0e 298 // Whether the view has been accessed recently.
cb295612 299 bool accessed_;
bae7f79e
ILT
300 };
301
e44fcf3b 302 friend class View;
bae7f79e
ILT
303 friend class File_view;
304
39d0cb0e
ILT
305 // The type of a mapping from page start and byte shift to views.
306 typedef std::map<std::pair<off_t, unsigned int>, View*> Views;
307
308 // A simple list of Views.
309 typedef std::list<View*> Saved_views;
310
2a00e4fb
ILT
311 // Open the descriptor if necessary.
312 void
313 reopen_descriptor();
314
ead1e424 315 // Find a view into the file.
bae7f79e 316 View*
39d0cb0e
ILT
317 find_view(off_t start, section_size_type size, unsigned int byteshift,
318 View** vshifted) const;
bae7f79e 319
ead1e424 320 // Read data from the file into a buffer.
82dcae9d 321 void
2a00e4fb 322 do_read(off_t start, section_size_type size, void* p);
bae7f79e 323
39d0cb0e
ILT
324 // Add a view.
325 void
326 add_view(View*);
327
328 // Make a view into the file.
329 View*
330 make_view(off_t start, section_size_type size, unsigned int byteshift,
331 bool cache);
332
ead1e424 333 // Find or make a view into the file.
bae7f79e 334 View*
39d0cb0e
ILT
335 find_or_make_view(off_t offset, off_t start, section_size_type size,
336 bool aligned, bool cache);
bae7f79e 337
ead1e424 338 // Clear the file views.
bae7f79e
ILT
339 void
340 clear_views(bool);
341
ead1e424
ILT
342 // The size of a file page for buffering data.
343 static const off_t page_size = 8192;
344
345 // Given a file offset, return the page offset.
346 static off_t
347 page_offset(off_t file_offset)
348 { return file_offset & ~ (page_size - 1); }
349
350 // Given a file size, return the size to read integral pages.
351 static off_t
352 pages(off_t file_size)
353 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
354
cb295612
ILT
355 // The maximum number of entries we will pass to ::readv.
356 static const size_t max_readv_entries = 128;
357
358 // Use readv to read data.
359 void
360 do_readv(off_t base, const Read_multiple&, size_t start, size_t count);
361
ead1e424 362 // File name.
bae7f79e 363 std::string name_;
ead1e424 364 // File descriptor.
bae7f79e 365 int descriptor_;
2a00e4fb
ILT
366 // Whether we have regained the descriptor after releasing the file.
367 bool is_descriptor_opened_;
cb295612
ILT
368 // The number of objects associated with this file. This will be
369 // more than 1 in the case of an archive.
370 int object_count_;
82dcae9d
ILT
371 // File size.
372 off_t size_;
17a1d0a9
ILT
373 // A token used to lock the file.
374 Task_token token_;
ead1e424
ILT
375 // Buffered views into the file.
376 Views views_;
377 // List of views which were locked but had to be removed from views_
378 // because they were not large enough.
379 Saved_views saved_views_;
5a6f7e2d
ILT
380 // Specified file contents. Used only for testing purposes.
381 const unsigned char* contents_;
e44fcf3b
ILT
382 // Total amount of space mapped into memory. This is only changed
383 // while the file is locked. When we unlock the file, we transfer
384 // the total to total_mapped_bytes, and reset this to zero.
385 size_t mapped_bytes_;
17a1d0a9
ILT
386 // Whether the file was released.
387 bool released_;
bae7f79e
ILT
388};
389
390// A view of file data that persists even when the file is unlocked.
391// Callers should destroy these when no longer required. These are
392// obtained form File_read::get_lasting_view. They may only be
393// destroyed when the underlying File_read is locked.
394
395class File_view
396{
397 public:
398 // This may only be called when the underlying File_read is locked.
399 ~File_view();
400
401 // Return a pointer to the data associated with this view.
402 const unsigned char*
403 data() const
404 { return this->data_; }
405
406 private:
407 File_view(const File_view&);
408 File_view& operator=(const File_view&);
409
410 friend class File_read;
411
412 // Callers have to get these via File_read::get_lasting_view.
413 File_view(File_read& file, File_read::View* view, const unsigned char* data)
414 : file_(file), view_(view), data_(data)
415 { }
416
417 File_read& file_;
418 File_read::View* view_;
419 const unsigned char* data_;
420};
421
bae7f79e
ILT
422// All the information we hold for a single input file. This can be
423// an object file, a shared library, or an archive.
424
425class Input_file
426{
427 public:
5a6f7e2d 428 Input_file(const Input_file_argument* input_argument)
e2aacd2c
ILT
429 : input_argument_(input_argument), found_name_(), file_(),
430 is_in_sysroot_(false)
bae7f79e
ILT
431 { }
432
5a6f7e2d
ILT
433 // Create an input file with the contents already provided. This is
434 // only used for testing. With this path, don't call the open
435 // method.
17a1d0a9
ILT
436 Input_file(const Task*, const char* name, const unsigned char* contents,
437 off_t size);
5a6f7e2d 438
75f2446e
ILT
439 // Open the file. If the open fails, this will report an error and
440 // return false.
441 bool
17a1d0a9 442 open(const General_options&, const Dirsearch&, const Task*);
bae7f79e 443
e2aacd2c 444 // Return the name given by the user. For -lc this will return "c".
bae7f79e 445 const char*
14144f39 446 name() const;
bae7f79e 447
e2aacd2c
ILT
448 // Return the file name. For -lc this will return something like
449 // "/usr/lib/libc.so".
bae7f79e
ILT
450 const std::string&
451 filename() const
452 { return this->file_.filename(); }
453
e2aacd2c
ILT
454 // Return the name under which we found the file, corresponding to
455 // the command line. For -lc this will return something like
456 // "libc.so".
457 const std::string&
458 found_name() const
459 { return this->found_name_; }
460
4973341a
ILT
461 // Return the position dependent options.
462 const Position_dependent_options&
14144f39 463 options() const;
4973341a
ILT
464
465 // Return the file.
bae7f79e
ILT
466 File_read&
467 file()
468 { return this->file_; }
469
7004837e
ILT
470 const File_read&
471 file() const
472 { return this->file_; }
473
ad2d6943
ILT
474 // Whether we found the file in a directory in the system root.
475 bool
476 is_in_sysroot() const
477 { return this->is_in_sysroot_; }
478
88dd47ac
ILT
479 // Return whether this file is to be read only for its symbols.
480 bool
481 just_symbols() const;
482
bae7f79e 483 private:
ead1e424
ILT
484 Input_file(const Input_file&);
485 Input_file& operator=(const Input_file&);
486
bc644c6c
ILT
487 // Open a binary file.
488 bool
0daa6f62
ILT
489 open_binary(const General_options&, const Task* task,
490 const std::string& name);
bc644c6c 491
ad2d6943 492 // The argument from the command line.
5a6f7e2d 493 const Input_file_argument* input_argument_;
e2aacd2c
ILT
494 // The name under which we opened the file. This is like the name
495 // on the command line, but -lc turns into libc.so (or whatever).
496 // It only includes the full path if the path was on the command
497 // line.
498 std::string found_name_;
ad2d6943 499 // The file after we open it.
bae7f79e 500 File_read file_;
ad2d6943
ILT
501 // Whether we found the file in a directory in the system root.
502 bool is_in_sysroot_;
bae7f79e
ILT
503};
504
505} // end namespace gold
506
507#endif // !defined(GOLD_FILEREAD_H)
This page took 0.150263 seconds and 4 git commands to generate.