* app.c (do_scrub_chars): Do not UNGET an EOF value.
[binutils.git] / gold / fileread.h
blob3e25f8b39b0c8189af3049106e03234c8ce3d2a4
1 // fileread.h -- read files for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008 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
28 #include <list>
29 #include <map>
30 #include <string>
31 #include <vector>
33 #include "token.h"
35 namespace gold
38 class Position_dependent_options;
39 class Input_file_argument;
40 class Dirsearch;
41 class File_view;
43 // File_read manages a file descriptor for a file we are reading. We
44 // close file descriptors if we run out of them, so this class reopens
45 // the file as needed.
47 class File_read
49 public:
50 File_read()
51 : name_(), descriptor_(-1), object_count_(0), size_(0), token_(false),
52 views_(), saved_views_(), contents_(NULL), mapped_bytes_(0),
53 released_(true)
54 { }
56 ~File_read();
58 // Open a file.
59 bool
60 open(const Task*, const std::string& name);
62 // Pretend to open the file, but provide the file contents. No
63 // actual file system activity will occur. This is used for
64 // testing.
65 bool
66 open(const Task*, const std::string& name, const unsigned char* contents,
67 off_t size);
69 // Return the file name.
70 const std::string&
71 filename() const
72 { return this->name_; }
74 // Add an object associated with a file.
75 void
76 add_object()
77 { ++this->object_count_; }
79 // Remove an object associated with a file.
80 void
81 remove_object()
82 { --this->object_count_; }
84 // Lock the file for exclusive access within a particular Task::run
85 // execution. This means that the descriptor can not be closed.
86 // This routine may only be called when the workqueue lock is held.
87 void
88 lock(const Task* t);
90 // Unlock the descriptor, permitting it to be closed if necessary.
91 void
92 unlock(const Task* t);
94 // Test whether the object is locked.
95 bool
96 is_locked() const;
98 // Return the token, so that the task can be queued.
99 Task_token*
100 token()
101 { return &this->token_; }
103 // Release the file. This indicates that we aren't going to do
104 // anything further with it until it is unlocked. This is used
105 // because a Task which locks the file never calls either lock or
106 // unlock; it just locks the token. The basic rule is that a Task
107 // which locks a file via the Task::locks interface must explicitly
108 // call release() when it is done. This is not necessary for code
109 // which calls unlock() on the file.
110 void
111 release();
113 // Return the size of the file.
114 off_t
115 filesize() const
116 { return this->size_; }
118 // Return a view into the file starting at file offset START for
119 // SIZE bytes. OFFSET is the offset into the input file for the
120 // file we are reading; this is zero for a normal object file,
121 // non-zero for an object file in an archive. ALIGNED is true if
122 // the data must be naturally aligned; this only matters when OFFSET
123 // is not zero. The pointer will remain valid until the File_read
124 // is unlocked. It is an error if we can not read enough data from
125 // the file. The CACHE parameter is a hint as to whether it will be
126 // useful to cache this data for later accesses--i.e., later calls
127 // to get_view, read, or get_lasting_view which retrieve the same
128 // data.
129 const unsigned char*
130 get_view(off_t offset, off_t start, section_size_type size, bool aligned,
131 bool cache);
133 // Read data from the file into the buffer P starting at file offset
134 // START for SIZE bytes.
135 void
136 read(off_t start, section_size_type size, void* p) const;
138 // Return a lasting view into the file starting at file offset START
139 // for SIZE bytes. This is allocated with new, and the caller is
140 // responsible for deleting it when done. The data associated with
141 // this view will remain valid until the view is deleted. It is an
142 // error if we can not read enough data from the file. The OFFSET,
143 // ALIGNED and CACHE parameters are as in get_view.
144 File_view*
145 get_lasting_view(off_t offset, off_t start, section_size_type size,
146 bool aligned, bool cache);
148 // Mark all views as no longer cached.
149 void
150 clear_view_cache_marks();
152 // Discard all uncached views. This is normally done by release(),
153 // but not for objects in archives. FIXME: This is a complicated
154 // interface, and it would be nice to have something more automatic.
155 void
156 clear_uncached_views()
157 { this->clear_views(false); }
159 // A struct used to do a multiple read.
160 struct Read_multiple_entry
162 // The file offset of the data to read.
163 off_t file_offset;
164 // The amount of data to read.
165 section_size_type size;
166 // The buffer where the data should be placed.
167 unsigned char* buffer;
169 Read_multiple_entry(off_t o, section_size_type s, unsigned char* b)
170 : file_offset(o), size(s), buffer(b)
174 typedef std::vector<Read_multiple_entry> Read_multiple;
176 // Read a bunch of data from the file into various different
177 // locations. The vector must be sorted by ascending file_offset.
178 // BASE is a base offset to be added to all the offsets in the
179 // vector.
180 void
181 read_multiple(off_t base, const Read_multiple&);
183 // Dump statistical information to stderr.
184 static void
185 print_stats();
187 private:
188 // This class may not be copied.
189 File_read(const File_read&);
190 File_read& operator=(const File_read&);
192 // Total bytes mapped into memory during the link. This variable
193 // may not be accurate when running multi-threaded.
194 static unsigned long long total_mapped_bytes;
196 // Current number of bytes mapped into memory during the link. This
197 // variable may not be accurate when running multi-threaded.
198 static unsigned long long current_mapped_bytes;
200 // High water mark of bytes mapped into memory during the link.
201 // This variable may not be accurate when running multi-threaded.
202 static unsigned long long maximum_mapped_bytes;
204 // A view into the file.
205 class View
207 public:
208 View(off_t start, section_size_type size, const unsigned char* data,
209 unsigned int byteshift, bool cache, bool mapped)
210 : start_(start), size_(size), data_(data), lock_count_(0),
211 byteshift_(byteshift), cache_(cache), mapped_(mapped), accessed_(true)
214 ~View();
216 off_t
217 start() const
218 { return this->start_; }
220 section_size_type
221 size() const
222 { return this->size_; }
224 const unsigned char*
225 data() const
226 { return this->data_; }
228 void
229 lock();
231 void
232 unlock();
234 bool
235 is_locked();
237 unsigned int
238 byteshift() const
239 { return this->byteshift_; }
241 void
242 set_cache()
243 { this->cache_ = true; }
245 void
246 clear_cache()
247 { this->cache_ = false; }
249 bool
250 should_cache() const
251 { return this->cache_; }
253 void
254 set_accessed()
255 { this->accessed_ = true; }
257 void
258 clear_accessed()
259 { this->accessed_= false; }
261 bool
262 accessed() const
263 { return this->accessed_; }
265 private:
266 View(const View&);
267 View& operator=(const View&);
269 // The file offset of the start of the view.
270 off_t start_;
271 // The size of the view.
272 section_size_type size_;
273 // A pointer to the actual bytes.
274 const unsigned char* data_;
275 // The number of locks on this view.
276 int lock_count_;
277 // The number of bytes that the view is shifted relative to the
278 // underlying file. This is used to align data. This is normally
279 // zero, except possibly for an object in an archive.
280 unsigned int byteshift_;
281 // Whether the view is cached.
282 bool cache_;
283 // Whether the view is mapped into memory. If not, data_ points
284 // to memory allocated using new[].
285 bool mapped_;
286 // Whether the view has been accessed recently.
287 bool accessed_;
290 friend class View;
291 friend class File_view;
293 // The type of a mapping from page start and byte shift to views.
294 typedef std::map<std::pair<off_t, unsigned int>, View*> Views;
296 // A simple list of Views.
297 typedef std::list<View*> Saved_views;
299 // Find a view into the file.
300 View*
301 find_view(off_t start, section_size_type size, unsigned int byteshift,
302 View** vshifted) const;
304 // Read data from the file into a buffer.
305 void
306 do_read(off_t start, section_size_type size, void* p) const;
308 // Add a view.
309 void
310 add_view(View*);
312 // Make a view into the file.
313 View*
314 make_view(off_t start, section_size_type size, unsigned int byteshift,
315 bool cache);
317 // Find or make a view into the file.
318 View*
319 find_or_make_view(off_t offset, off_t start, section_size_type size,
320 bool aligned, bool cache);
322 // Clear the file views.
323 void
324 clear_views(bool);
326 // The size of a file page for buffering data.
327 static const off_t page_size = 8192;
329 // Given a file offset, return the page offset.
330 static off_t
331 page_offset(off_t file_offset)
332 { return file_offset & ~ (page_size - 1); }
334 // Given a file size, return the size to read integral pages.
335 static off_t
336 pages(off_t file_size)
337 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
339 // The maximum number of entries we will pass to ::readv.
340 static const size_t max_readv_entries = 128;
342 // Use readv to read data.
343 void
344 do_readv(off_t base, const Read_multiple&, size_t start, size_t count);
346 // File name.
347 std::string name_;
348 // File descriptor.
349 int descriptor_;
350 // The number of objects associated with this file. This will be
351 // more than 1 in the case of an archive.
352 int object_count_;
353 // File size.
354 off_t size_;
355 // A token used to lock the file.
356 Task_token token_;
357 // Buffered views into the file.
358 Views views_;
359 // List of views which were locked but had to be removed from views_
360 // because they were not large enough.
361 Saved_views saved_views_;
362 // Specified file contents. Used only for testing purposes.
363 const unsigned char* contents_;
364 // Total amount of space mapped into memory. This is only changed
365 // while the file is locked. When we unlock the file, we transfer
366 // the total to total_mapped_bytes, and reset this to zero.
367 size_t mapped_bytes_;
368 // Whether the file was released.
369 bool released_;
372 // A view of file data that persists even when the file is unlocked.
373 // Callers should destroy these when no longer required. These are
374 // obtained form File_read::get_lasting_view. They may only be
375 // destroyed when the underlying File_read is locked.
377 class File_view
379 public:
380 // This may only be called when the underlying File_read is locked.
381 ~File_view();
383 // Return a pointer to the data associated with this view.
384 const unsigned char*
385 data() const
386 { return this->data_; }
388 private:
389 File_view(const File_view&);
390 File_view& operator=(const File_view&);
392 friend class File_read;
394 // Callers have to get these via File_read::get_lasting_view.
395 File_view(File_read& file, File_read::View* view, const unsigned char* data)
396 : file_(file), view_(view), data_(data)
399 File_read& file_;
400 File_read::View* view_;
401 const unsigned char* data_;
404 // All the information we hold for a single input file. This can be
405 // an object file, a shared library, or an archive.
407 class Input_file
409 public:
410 Input_file(const Input_file_argument* input_argument)
411 : input_argument_(input_argument), found_name_(), file_(),
412 is_in_sysroot_(false)
415 // Create an input file with the contents already provided. This is
416 // only used for testing. With this path, don't call the open
417 // method.
418 Input_file(const Task*, const char* name, const unsigned char* contents,
419 off_t size);
421 // Open the file. If the open fails, this will report an error and
422 // return false.
423 bool
424 open(const General_options&, const Dirsearch&, const Task*);
426 // Return the name given by the user. For -lc this will return "c".
427 const char*
428 name() const;
430 // Return the file name. For -lc this will return something like
431 // "/usr/lib/libc.so".
432 const std::string&
433 filename() const
434 { return this->file_.filename(); }
436 // Return the name under which we found the file, corresponding to
437 // the command line. For -lc this will return something like
438 // "libc.so".
439 const std::string&
440 found_name() const
441 { return this->found_name_; }
443 // Return the position dependent options.
444 const Position_dependent_options&
445 options() const;
447 // Return the file.
448 File_read&
449 file()
450 { return this->file_; }
452 const File_read&
453 file() const
454 { return this->file_; }
456 // Whether we found the file in a directory in the system root.
457 bool
458 is_in_sysroot() const
459 { return this->is_in_sysroot_; }
461 // Return whether this file is to be read only for its symbols.
462 bool
463 just_symbols() const;
465 private:
466 Input_file(const Input_file&);
467 Input_file& operator=(const Input_file&);
469 // Open a binary file.
470 bool
471 open_binary(const General_options&, const Task* task,
472 const std::string& name);
474 // The argument from the command line.
475 const Input_file_argument* input_argument_;
476 // The name under which we opened the file. This is like the name
477 // on the command line, but -lc turns into libc.so (or whatever).
478 // It only includes the full path if the path was on the command
479 // line.
480 std::string found_name_;
481 // The file after we open it.
482 File_read file_;
483 // Whether we found the file in a directory in the system root.
484 bool is_in_sysroot_;
487 } // end namespace gold
489 #endif // !defined(GOLD_FILEREAD_H)