1 // fileread.h -- read files for gold -*- C++ -*-
3 // Copyright 2006, 2007 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
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.
49 : name_(), descriptor_(-1), size_(0), token_(false), views_(),
50 saved_views_(), contents_(NULL), mapped_bytes_(0), released_(true)
57 open(const Task*, const std::string& name);
59 // Pretend to open the file, but provide the file contents. No
60 // actual file system activity will occur. This is used for
63 open(const Task*, const std::string& name, const unsigned char* contents,
66 // Return the file name.
69 { return this->name_; }
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.
77 // Unlock the descriptor, permitting it to be closed if necessary.
79 unlock(const Task* t);
81 // Test whether the object is locked.
85 // Return the token, so that the task can be queued.
88 { return &this->token_; }
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.
100 // Return the size of the file.
103 { return this->size_; }
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
113 get_view(off_t start, section_size_type size, bool cache);
115 // Read data from the file into the buffer P starting at file offset
116 // START for SIZE bytes.
118 read(off_t start, off_t size, void* p) const;
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.
127 get_lasting_view(off_t start, section_size_type size, bool cache);
129 // Dump statistical information to stderr.
134 // This class may not be copied.
135 File_read(const File_read&);
136 File_read& operator=(const File_read&);
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;
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;
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;
150 // A view into the file.
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)
164 { return this->start_; }
168 { return this->size_; }
172 { return this->data_; }
185 { this->cache_ = true; }
189 { return this->cache_; }
193 View& operator=(const View&);
196 section_size_type size_;
197 const unsigned char* data_;
204 friend class File_view;
206 // Find a view into the file.
208 find_view(off_t start, section_size_type size) const;
210 // Read data from the file into a buffer.
212 do_read(off_t start, off_t size, void* p) const;
214 // Find or make a view into the file.
216 find_or_make_view(off_t start, section_size_type size, bool cache);
218 // Clear the file views.
222 // The size of a file page for buffering data.
223 static const off_t page_size = 8192;
225 // Given a file offset, return the page offset.
227 page_offset(off_t file_offset)
228 { return file_offset & ~ (page_size - 1); }
230 // Given a file size, return the size to read integral pages.
232 pages(off_t file_size)
233 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
235 // The type of a mapping from page start to views.
236 typedef std::map<off_t, View*> Views;
238 // A simple list of Views.
239 typedef std::list<View*> Saved_views;
247 // A token used to lock the file.
249 // Buffered views into the file.
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.
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.
272 // This may only be called when the underlying File_read is locked.
275 // Return a pointer to the data associated with this view.
278 { return this->data_; }
281 File_view(const File_view&);
282 File_view& operator=(const File_view&);
284 friend class File_read;
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)
292 File_read::View* view_;
293 const unsigned char* data_;
296 // All the information we hold for a single input file. This can be
297 // an object file, a shared library, or an archive.
302 Input_file(const Input_file_argument* input_argument)
303 : input_argument_(input_argument), found_name_(), file_(),
304 is_in_sysroot_(false)
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
310 Input_file(const Task*, const char* name, const unsigned char* contents,
313 // Open the file. If the open fails, this will report an error and
316 open(const General_options&, const Dirsearch&, const Task*);
318 // Return the name given by the user. For -lc this will return "c".
321 { return this->input_argument_->name(); }
323 // Return the file name. For -lc this will return something like
324 // "/usr/lib/libc.so".
327 { return this->file_.filename(); }
329 // Return the name under which we found the file, corresponding to
330 // the command line. For -lc this will return something like
334 { return this->found_name_; }
336 // Return the position dependent options.
337 const Position_dependent_options&
339 { return this->input_argument_->options(); }
344 { return this->file_; }
348 { return this->file_; }
350 // Whether we found the file in a directory in the system root.
352 is_in_sysroot() const
353 { return this->is_in_sysroot_; }
356 Input_file(const Input_file&);
357 Input_file& operator=(const Input_file&);
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
365 std::string found_name_;
366 // The file after we open it.
368 // Whether we found the file in a directory in the system root.
372 } // end namespace gold
374 #endif // !defined(GOLD_FILEREAD_H)