1 // fileread.h -- read files for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 // Classes used to read data from binary input files.
25 #ifndef GOLD_FILEREAD_H
26 #define GOLD_FILEREAD_H
38 // Since not all system supports stat.st_mtim and struct timespec,
39 // we define our own structure and fill the nanoseconds if we can.
44 : seconds(0), nanoseconds(0)
47 Timespec(time_t a_seconds
, int a_nanoseconds
)
48 : seconds(a_seconds
), nanoseconds(a_nanoseconds
)
55 class Position_dependent_options
;
56 class Input_file_argument
;
60 // File_read manages a file descriptor and mappings for a file we are
67 : name_(), descriptor_(-1), is_descriptor_opened_(false), object_count_(0),
68 size_(0), token_(false), views_(), saved_views_(), contents_(NULL
),
69 mapped_bytes_(0), released_(true)
76 open(const Task
*, const std::string
& name
);
78 // Pretend to open the file, but provide the file contents. No
79 // actual file system activity will occur. This is used for
82 open(const Task
*, const std::string
& name
, const unsigned char* contents
,
85 // Return the file name.
88 { return this->name_
; }
90 // Add an object associated with a file.
93 { ++this->object_count_
; }
95 // Remove an object associated with a file.
98 { --this->object_count_
; }
100 // Lock the file for exclusive access within a particular Task::run
101 // execution. This routine may only be called when the workqueue
108 unlock(const Task
* t
);
110 // Test whether the object is locked.
114 // Return the token, so that the task can be queued.
117 { return &this->token_
; }
119 // Release the file. This indicates that we aren't going to do
120 // anything further with it until it is unlocked. This is used
121 // because a Task which locks the file never calls either lock or
122 // unlock; it just locks the token. The basic rule is that a Task
123 // which locks a file via the Task::locks interface must explicitly
124 // call release() when it is done. This is not necessary for code
125 // which calls unlock() on the file.
129 // Return the size of the file.
132 { return this->size_
; }
134 // Return a view into the file starting at file offset START for
135 // SIZE bytes. OFFSET is the offset into the input file for the
136 // file we are reading; this is zero for a normal object file,
137 // non-zero for an object file in an archive. ALIGNED is true if
138 // the data must be naturally aligned; this only matters when OFFSET
139 // is not zero. The pointer will remain valid until the File_read
140 // is unlocked. It is an error if we can not read enough data from
141 // the file. The CACHE parameter is a hint as to whether it will be
142 // useful to cache this data for later accesses--i.e., later calls
143 // to get_view, read, or get_lasting_view which retrieve the same
146 get_view(off_t offset
, off_t start
, section_size_type size
, bool aligned
,
149 // Read data from the file into the buffer P starting at file offset
150 // START for SIZE bytes.
152 read(off_t start
, section_size_type size
, void* p
);
154 // Return a lasting view into the file starting at file offset START
155 // for SIZE bytes. This is allocated with new, and the caller is
156 // responsible for deleting it when done. The data associated with
157 // this view will remain valid until the view is deleted. It is an
158 // error if we can not read enough data from the file. The OFFSET,
159 // ALIGNED and CACHE parameters are as in get_view.
161 get_lasting_view(off_t offset
, off_t start
, section_size_type size
,
162 bool aligned
, bool cache
);
164 // Mark all views as no longer cached.
166 clear_view_cache_marks();
168 // Discard all uncached views. This is normally done by release(),
169 // but not for objects in archives. FIXME: This is a complicated
170 // interface, and it would be nice to have something more automatic.
172 clear_uncached_views()
173 { this->clear_views(false); }
175 // A struct used to do a multiple read.
176 struct Read_multiple_entry
178 // The file offset of the data to read.
180 // The amount of data to read.
181 section_size_type size
;
182 // The buffer where the data should be placed.
183 unsigned char* buffer
;
185 Read_multiple_entry(off_t o
, section_size_type s
, unsigned char* b
)
186 : file_offset(o
), size(s
), buffer(b
)
190 typedef std::vector
<Read_multiple_entry
> Read_multiple
;
192 // Read a bunch of data from the file into various different
193 // locations. The vector must be sorted by ascending file_offset.
194 // BASE is a base offset to be added to all the offsets in the
197 read_multiple(off_t base
, const Read_multiple
&);
199 // Dump statistical information to stderr.
203 // Return the open file descriptor (for plugins).
207 this->reopen_descriptor();
208 return this->descriptor_
;
211 // Return the file last modification time. Calls gold_fatal if the stat
212 // system call failed.
217 // This class may not be copied.
218 File_read(const File_read
&);
219 File_read
& operator=(const File_read
&);
221 // Total bytes mapped into memory during the link. This variable
222 // may not be accurate when running multi-threaded.
223 static unsigned long long total_mapped_bytes
;
225 // Current number of bytes mapped into memory during the link. This
226 // variable may not be accurate when running multi-threaded.
227 static unsigned long long current_mapped_bytes
;
229 // High water mark of bytes mapped into memory during the link.
230 // This variable may not be accurate when running multi-threaded.
231 static unsigned long long maximum_mapped_bytes
;
233 // A view into the file.
237 View(off_t start
, section_size_type size
, const unsigned char* data
,
238 unsigned int byteshift
, bool cache
, bool mapped
)
239 : start_(start
), size_(size
), data_(data
), lock_count_(0),
240 byteshift_(byteshift
), cache_(cache
), mapped_(mapped
), accessed_(true)
247 { return this->start_
; }
251 { return this->size_
; }
255 { return this->data_
; }
268 { return this->byteshift_
; }
272 { this->cache_
= true; }
276 { this->cache_
= false; }
280 { return this->cache_
; }
284 { this->accessed_
= true; }
288 { this->accessed_
= false; }
292 { return this->accessed_
; }
296 View
& operator=(const View
&);
298 // The file offset of the start of the view.
300 // The size of the view.
301 section_size_type size_
;
302 // A pointer to the actual bytes.
303 const unsigned char* data_
;
304 // The number of locks on this view.
306 // The number of bytes that the view is shifted relative to the
307 // underlying file. This is used to align data. This is normally
308 // zero, except possibly for an object in an archive.
309 unsigned int byteshift_
;
310 // Whether the view is cached.
312 // Whether the view is mapped into memory. If not, data_ points
313 // to memory allocated using new[].
315 // Whether the view has been accessed recently.
320 friend class File_view
;
322 // The type of a mapping from page start and byte shift to views.
323 typedef std::map
<std::pair
<off_t
, unsigned int>, View
*> Views
;
325 // A simple list of Views.
326 typedef std::list
<View
*> Saved_views
;
328 // Open the descriptor if necessary.
332 // Find a view into the file.
334 find_view(off_t start
, section_size_type size
, unsigned int byteshift
,
335 View
** vshifted
) const;
337 // Read data from the file into a buffer.
339 do_read(off_t start
, section_size_type size
, void* p
);
345 // Make a view into the file.
347 make_view(off_t start
, section_size_type size
, unsigned int byteshift
,
350 // Find or make a view into the file.
352 find_or_make_view(off_t offset
, off_t start
, section_size_type size
,
353 bool aligned
, bool cache
);
355 // Clear the file views.
359 // The size of a file page for buffering data.
360 static const off_t page_size
= 8192;
362 // Given a file offset, return the page offset.
364 page_offset(off_t file_offset
)
365 { return file_offset
& ~ (page_size
- 1); }
367 // Given a file size, return the size to read integral pages.
369 pages(off_t file_size
)
370 { return (file_size
+ (page_size
- 1)) & ~ (page_size
- 1); }
372 // The maximum number of entries we will pass to ::readv.
374 static const size_t max_readv_entries
= 128;
376 // On targets that don't have readv set the max to 1 so readv is not
378 static const size_t max_readv_entries
= 1;
381 // Use readv to read data.
383 do_readv(off_t base
, const Read_multiple
&, size_t start
, size_t count
);
389 // Whether we have regained the descriptor after releasing the file.
390 bool is_descriptor_opened_
;
391 // The number of objects associated with this file. This will be
392 // more than 1 in the case of an archive.
396 // A token used to lock the file.
398 // Buffered views into the file.
400 // List of views which were locked but had to be removed from views_
401 // because they were not large enough.
402 Saved_views saved_views_
;
403 // Specified file contents. Used only for testing purposes.
404 const unsigned char* contents_
;
405 // Total amount of space mapped into memory. This is only changed
406 // while the file is locked. When we unlock the file, we transfer
407 // the total to total_mapped_bytes, and reset this to zero.
408 size_t mapped_bytes_
;
409 // Whether the file was released.
413 // A view of file data that persists even when the file is unlocked.
414 // Callers should destroy these when no longer required. These are
415 // obtained form File_read::get_lasting_view. They may only be
416 // destroyed when the underlying File_read is locked.
421 // This may only be called when the underlying File_read is locked.
424 // Return a pointer to the data associated with this view.
427 { return this->data_
; }
430 File_view(const File_view
&);
431 File_view
& operator=(const File_view
&);
433 friend class File_read
;
435 // Callers have to get these via File_read::get_lasting_view.
436 File_view(File_read
& file
, File_read::View
* view
, const unsigned char* data
)
437 : file_(file
), view_(view
), data_(data
)
441 File_read::View
* view_
;
442 const unsigned char* data_
;
445 // All the information we hold for a single input file. This can be
446 // an object file, a shared library, or an archive.
451 Input_file(const Input_file_argument
* input_argument
)
452 : input_argument_(input_argument
), found_name_(), file_(),
453 is_in_sysroot_(false)
456 // Create an input file with the contents already provided. This is
457 // only used for testing. With this path, don't call the open
459 Input_file(const Task
*, const char* name
, const unsigned char* contents
,
462 // Return the command line argument.
463 const Input_file_argument
*
464 input_file_argument() const
465 { return this->input_argument_
; }
467 // Return whether this is a file that we will search for in the list
470 will_search_for() const;
472 // Open the file. If the open fails, this will report an error and
473 // return false. If there is a search, it starts at directory
474 // *PINDEX. *PINDEX should be initialized to zero. It may be
475 // restarted to find the next file with a matching name by
476 // incrementing the result and calling this again.
478 open(const Dirsearch
&, const Task
*, int *pindex
);
480 // Return the name given by the user. For -lc this will return "c".
484 // Return the file name. For -lc this will return something like
485 // "/usr/lib/libc.so".
488 { return this->file_
.filename(); }
490 // Return the name under which we found the file, corresponding to
491 // the command line. For -lc this will return something like
495 { return this->found_name_
; }
497 // Return the position dependent options.
498 const Position_dependent_options
&
504 { return this->file_
; }
508 { return this->file_
; }
510 // Whether we found the file in a directory in the system root.
512 is_in_sysroot() const
513 { return this->is_in_sysroot_
; }
515 // Whether this file is in a system directory.
517 is_in_system_directory() const;
519 // Return whether this file is to be read only for its symbols.
521 just_symbols() const;
524 Input_file(const Input_file
&);
525 Input_file
& operator=(const Input_file
&);
527 // Open a binary file.
529 open_binary(const Task
* task
, const std::string
& name
);
531 // The argument from the command line.
532 const Input_file_argument
* input_argument_
;
533 // The name under which we opened the file. This is like the name
534 // on the command line, but -lc turns into libc.so (or whatever).
535 // It only includes the full path if the path was on the command
537 std::string found_name_
;
538 // The file after we open it.
540 // Whether we found the file in a directory in the system root.
544 } // end namespace gold
546 #endif // !defined(GOLD_FILEREAD_H)