Don't include options.h in fileread.h. Remove General_options
[external/binutils.git] / gold / fileread.h
1 // fileread.h -- read files for gold   -*- C++ -*-
2
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
23 // Classes used to read data from binary input files.
24
25 #ifndef GOLD_FILEREAD_H
26 #define GOLD_FILEREAD_H
27
28 #include <list>
29 #include <map>
30 #include <string>
31
32 #include "token.h"
33
34 namespace gold
35 {
36
37 class Position_dependent_options;
38 class Input_file_argument;
39 class Dirsearch;
40 class 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
46 class File_read
47 {
48  public:
49   File_read()
50     : name_(), descriptor_(-1), object_count_(0), size_(0), token_(false),
51       views_(), saved_views_(), contents_(NULL), mapped_bytes_(0),
52       released_(true)
53   { }
54
55   ~File_read();
56
57   // Open a file.
58   bool
59   open(const Task*, const std::string& name);
60
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
65   open(const Task*, const std::string& name, const unsigned char* contents,
66        off_t size);
67
68   // Return the file name.
69   const std::string&
70   filename() const
71   { return this->name_; }
72
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
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.
86   void
87   lock(const Task* t);
88
89   // Unlock the descriptor, permitting it to be closed if necessary.
90   void
91   unlock(const Task* t);
92
93   // Test whether the object is locked.
94   bool
95   is_locked() const;
96
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
112   // Return the size of the file.
113   off_t
114   filesize() const
115   { return this->size_; }
116
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
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.
124   const unsigned char*
125   get_view(off_t start, section_size_type size, bool cache);
126
127   // Read data from the file into the buffer P starting at file offset
128   // START for SIZE bytes.
129   void
130   read(off_t start, section_size_type size, void* p) const;
131
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
136   // error if we can not read enough data from the file.  The CACHE
137   // parameter is as in get_view.
138   File_view*
139   get_lasting_view(off_t start, section_size_type size, bool cache);
140
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
169   // Dump statistical information to stderr.
170   static void
171   print_stats();
172
173  private:
174   // This class may not be copied.
175   File_read(const File_read&);
176   File_read& operator=(const File_read&);
177
178   // Total bytes mapped into memory during the link.  This variable
179   // may not be accurate when running multi-threaded.
180   static unsigned long long total_mapped_bytes;
181
182   // Current number of bytes mapped into memory during the link.  This
183   // variable may not be accurate when running multi-threaded.
184   static unsigned long long current_mapped_bytes;
185
186   // High water mark of bytes mapped into memory during the link.
187   // This variable may not be accurate when running multi-threaded.
188   static unsigned long long maximum_mapped_bytes;
189
190   // A view into the file.
191   class View
192   {
193    public:
194     View(off_t start, section_size_type size, const unsigned char* data,
195          bool cache, bool mapped)
196       : start_(start), size_(size), data_(data), lock_count_(0),
197         cache_(cache), mapped_(mapped), accessed_(true)
198     { }
199
200     ~View();
201
202     off_t
203     start() const
204     { return this->start_; }
205
206     section_size_type
207     size() const
208     { return this->size_; }
209
210     const unsigned char*
211     data() const
212     { return this->data_; }
213
214     void
215     lock();
216
217     void
218     unlock();
219
220     bool
221     is_locked();
222
223     void
224     set_cache()
225     { this->cache_ = true; }
226
227     void
228     clear_cache()
229     { this->cache_ = false; }
230
231     bool
232     should_cache() const
233     { return this->cache_; }
234
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
247    private:
248     View(const View&);
249     View& operator=(const View&);
250
251     off_t start_;
252     section_size_type size_;
253     const unsigned char* data_;
254     int lock_count_;
255     bool cache_;
256     bool mapped_;
257     bool accessed_;
258   };
259
260   friend class View;
261   friend class File_view;
262
263   // Find a view into the file.
264   View*
265   find_view(off_t start, section_size_type size) const;
266
267   // Read data from the file into a buffer.
268   void
269   do_read(off_t start, section_size_type size, void* p) const;
270
271   // Find or make a view into the file.
272   View*
273   find_or_make_view(off_t start, section_size_type size, bool cache);
274
275   // Clear the file views.
276   void
277   clear_views(bool);
278
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
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
305   // File name.
306   std::string name_;
307   // File descriptor.
308   int descriptor_;
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_;
312   // File size.
313   off_t size_;
314   // A token used to lock the file.
315   Task_token token_;
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_;
321   // Specified file contents.  Used only for testing purposes.
322   const unsigned char* contents_;
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_;
327   // Whether the file was released.
328   bool released_;
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
336 class 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
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
366 class Input_file
367 {
368  public:
369   Input_file(const Input_file_argument* input_argument)
370     : input_argument_(input_argument), found_name_(), file_(),
371       is_in_sysroot_(false)
372   { }
373
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.
377   Input_file(const Task*, const char* name, const unsigned char* contents,
378              off_t size);
379
380   // Open the file.  If the open fails, this will report an error and
381   // return false.
382   bool
383   open(const General_options&, const Dirsearch&, const Task*);
384
385   // Return the name given by the user.  For -lc this will return "c".
386   const char*
387   name() const;
388
389   // Return the file name.  For -lc this will return something like
390   // "/usr/lib/libc.so".
391   const std::string&
392   filename() const
393   { return this->file_.filename(); }
394
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
402   // Return the position dependent options.
403   const Position_dependent_options&
404   options() const;
405
406   // Return the file.
407   File_read&
408   file()
409   { return this->file_; }
410
411   const File_read&
412   file() const
413   { return this->file_; }
414
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
420  private:
421   Input_file(const Input_file&);
422   Input_file& operator=(const Input_file&);
423
424   // The argument from the command line.
425   const Input_file_argument* input_argument_;
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_;
431   // The file after we open it.
432   File_read file_;
433   // Whether we found the file in a directory in the system root.
434   bool is_in_sysroot_;
435 };
436
437 } // end namespace gold
438
439 #endif // !defined(GOLD_FILEREAD_H)