Add licensing text to every source file.
[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 "options.h"
33
34 namespace gold
35 {
36
37 class Dirsearch;
38 class File_view;
39
40 // File_read manages a file descriptor for a file we are reading.  We
41 // close file descriptors if we run out of them, so this class reopens
42 // the file as needed.
43
44 class File_read
45 {
46  public:
47   File_read()
48     : name_(), descriptor_(-1), lock_count_(0), views_(),
49       saved_views_(), contents_(NULL), contents_size_(0)
50   { }
51
52   ~File_read();
53
54   // Open a file.
55   bool
56   open(const std::string& name);
57
58   // Pretend to open the file, but provide the file contents.  No
59   // actual file system activity will occur.  This is used for
60   // testing.
61   bool
62   open(const std::string& name, const unsigned char* contents, off_t size);
63
64   // Return the file name.
65   const std::string&
66   filename() const
67   { return this->name_; }
68
69   // Lock the file for access within a particular Task::run execution.
70   // This means that the descriptor can not be closed.  This routine
71   // may only be called from the main thread.
72   void
73   lock();
74
75   // Unlock the descriptor, permitting it to be closed if necessary.
76   void
77   unlock();
78
79   // Test whether the object is locked.
80   bool
81   is_locked();
82
83   // Return a view into the file.  The pointer will remain valid until
84   // the File_read is unlocked.  If PBYTES is NULL, it is an error if
85   // we can not read enough data.  Otherwise *PBYTES is set to the
86   // number of bytes read.
87   const unsigned char*
88   get_view(off_t start, off_t size, off_t* pbytes = NULL);
89
90   // Read data from the file into the buffer P.  PBYTES is as in
91   // get_view.
92   void
93   read(off_t start, off_t size, void* p, off_t* pbytes = NULL);
94
95   // Return a lasting view into the file.  This is allocated with new,
96   // and the caller is responsible for deleting it when done.  The
97   // data associated with this view will remain valid until the view
98   // is deleted.  PBYTES is handled as with get_view.
99   File_view*
100   get_lasting_view(off_t start, off_t size, off_t *pbytes = NULL);
101
102  private:
103   // This class may not be copied.
104   File_read(const File_read&);
105   File_read& operator=(const File_read&);
106
107   // A view into the file when not using mmap.
108   class View
109   {
110    public:
111     View(off_t start, off_t size, const unsigned char* data)
112       : start_(start), size_(size), data_(data), lock_count_(0)
113     { }
114
115     ~View();
116
117     off_t
118     start() const
119     { return this->start_; }
120
121     off_t
122     size() const
123     { return this->size_; }
124
125     const unsigned char*
126     data() const
127     { return this->data_; }
128
129     void
130     lock();
131
132     void
133     unlock();
134
135     bool
136     is_locked();
137
138    private:
139     View(const View&);
140     View& operator=(const View&);
141
142     off_t start_;
143     off_t size_;
144     const unsigned char* data_;
145     int lock_count_;
146   };
147
148   friend class File_view;
149
150   // Find a view into the file.
151   View*
152   find_view(off_t start, off_t size);
153
154   // Read data from the file into a buffer.
155   off_t
156   do_read(off_t start, off_t size, void* p, off_t* pbytes);
157
158   // Find or make a view into the file.
159   View*
160   find_or_make_view(off_t start, off_t size, off_t* pbytes);
161
162   // Clear the file views.
163   void
164   clear_views(bool);
165
166   // The size of a file page for buffering data.
167   static const off_t page_size = 8192;
168
169   // Given a file offset, return the page offset.
170   static off_t
171   page_offset(off_t file_offset)
172   { return file_offset & ~ (page_size - 1); }
173
174   // Given a file size, return the size to read integral pages.
175   static off_t
176   pages(off_t file_size)
177   { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
178
179   // The type of a mapping from page start to views.
180   typedef std::map<off_t, View*> Views;
181
182   // A simple list of Views.
183   typedef std::list<View*> Saved_views;
184
185   // File name.
186   std::string name_;
187   // File descriptor.
188   int descriptor_;
189   // Number of locks on the file.
190   int lock_count_;
191   // Buffered views into the file.
192   Views views_;
193   // List of views which were locked but had to be removed from views_
194   // because they were not large enough.
195   Saved_views saved_views_;
196   // Specified file contents.  Used only for testing purposes.
197   const unsigned char* contents_;
198   // Specified file size.  Used only for testing purposes.
199   off_t contents_size_;
200 };
201
202 // A view of file data that persists even when the file is unlocked.
203 // Callers should destroy these when no longer required.  These are
204 // obtained form File_read::get_lasting_view.  They may only be
205 // destroyed when the underlying File_read is locked.
206
207 class File_view
208 {
209  public:
210   // This may only be called when the underlying File_read is locked.
211   ~File_view();
212
213   // Return a pointer to the data associated with this view.
214   const unsigned char*
215   data() const
216   { return this->data_; }
217
218  private:
219   File_view(const File_view&);
220   File_view& operator=(const File_view&);
221
222   friend class File_read;
223
224   // Callers have to get these via File_read::get_lasting_view.
225   File_view(File_read& file, File_read::View* view, const unsigned char* data)
226     : file_(file), view_(view), data_(data)
227   { }
228
229   File_read& file_;
230   File_read::View* view_;
231   const unsigned char* data_;
232 };
233
234 // All the information we hold for a single input file.  This can be
235 // an object file, a shared library, or an archive.
236
237 class Input_file
238 {
239  public:
240   Input_file(const Input_file_argument* input_argument)
241     : input_argument_(input_argument), file_()
242   { }
243
244   // Create an input file with the contents already provided.  This is
245   // only used for testing.  With this path, don't call the open
246   // method.
247   Input_file(const char* name, const unsigned char* contents, off_t size);
248
249   // Open the file.
250   void
251   open(const General_options&, const Dirsearch&);
252
253   // Return the name given by the user.
254   const char*
255   name() const
256   { return this->input_argument_->name(); }
257
258   // Return the file name.
259   const std::string&
260   filename() const
261   { return this->file_.filename(); }
262
263   // Return the position dependent options.
264   const Position_dependent_options&
265   options() const
266   { return this->input_argument_->options(); }
267
268   // Return the file.
269   File_read&
270   file()
271   { return this->file_; }
272
273  private:
274   Input_file(const Input_file&);
275   Input_file& operator=(const Input_file&);
276
277   const Input_file_argument* input_argument_;
278   File_read file_;
279 };
280
281 } // end namespace gold
282
283 #endif // !defined(GOLD_FILEREAD_H)