Full support for --sysroot.
[deliverable/binutils-gdb.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), size_(0), lock_count_(0), views_(),
49 saved_views_(), contents_(NULL)
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 the size of the file.
84 off_t
85 filesize() const
86 { return this->size_; }
87
88 // Return a view into the file starting at file offset START for
89 // SIZE bytes. The pointer will remain valid until the File_read is
90 // unlocked. It is an error if we can not read enough data from the
91 // file. The CACHE parameter is a hint as to whether it will be
92 // useful to cache this data for later accesses--i.e., later calls
93 // to get_view, read, or get_lasting_view which retrieve the same
94 // data.
95 const unsigned char*
96 get_view(off_t start, off_t size, bool cache);
97
98 // Read data from the file into the buffer P starting at file offset
99 // START for SIZE bytes.
100 void
101 read(off_t start, off_t size, void* p);
102
103 // Return a lasting view into the file starting at file offset START
104 // for SIZE bytes. This is allocated with new, and the caller is
105 // responsible for deleting it when done. The data associated with
106 // this view will remain valid until the view is deleted. It is an
107 // error if we can not read enough data from the file. The CACHE
108 // parameter is as in get_view.
109 File_view*
110 get_lasting_view(off_t start, off_t size, bool cache);
111
112 private:
113 // This class may not be copied.
114 File_read(const File_read&);
115 File_read& operator=(const File_read&);
116
117 // A view into the file.
118 class View
119 {
120 public:
121 View(off_t start, off_t size, const unsigned char* data, bool cache,
122 bool mapped)
123 : start_(start), size_(size), data_(data), lock_count_(0),
124 cache_(cache), mapped_(mapped)
125 { }
126
127 ~View();
128
129 off_t
130 start() const
131 { return this->start_; }
132
133 off_t
134 size() const
135 { return this->size_; }
136
137 const unsigned char*
138 data() const
139 { return this->data_; }
140
141 void
142 lock();
143
144 void
145 unlock();
146
147 bool
148 is_locked();
149
150 void
151 set_cache()
152 { this->cache_ = true; }
153
154 bool
155 should_cache() const
156 { return this->cache_; }
157
158 private:
159 View(const View&);
160 View& operator=(const View&);
161
162 off_t start_;
163 off_t size_;
164 const unsigned char* data_;
165 int lock_count_;
166 bool cache_;
167 bool mapped_;
168 };
169
170 friend class File_view;
171
172 // Find a view into the file.
173 View*
174 find_view(off_t start, off_t size);
175
176 // Read data from the file into a buffer.
177 void
178 do_read(off_t start, off_t size, void* p);
179
180 // Find or make a view into the file.
181 View*
182 find_or_make_view(off_t start, off_t size, bool cache);
183
184 // Clear the file views.
185 void
186 clear_views(bool);
187
188 // The size of a file page for buffering data.
189 static const off_t page_size = 8192;
190
191 // Given a file offset, return the page offset.
192 static off_t
193 page_offset(off_t file_offset)
194 { return file_offset & ~ (page_size - 1); }
195
196 // Given a file size, return the size to read integral pages.
197 static off_t
198 pages(off_t file_size)
199 { return (file_size + (page_size - 1)) & ~ (page_size - 1); }
200
201 // The type of a mapping from page start to views.
202 typedef std::map<off_t, View*> Views;
203
204 // A simple list of Views.
205 typedef std::list<View*> Saved_views;
206
207 // File name.
208 std::string name_;
209 // File descriptor.
210 int descriptor_;
211 // File size.
212 off_t size_;
213 // Number of locks on the file.
214 int lock_count_;
215 // Buffered views into the file.
216 Views views_;
217 // List of views which were locked but had to be removed from views_
218 // because they were not large enough.
219 Saved_views saved_views_;
220 // Specified file contents. Used only for testing purposes.
221 const unsigned char* contents_;
222 };
223
224 // A view of file data that persists even when the file is unlocked.
225 // Callers should destroy these when no longer required. These are
226 // obtained form File_read::get_lasting_view. They may only be
227 // destroyed when the underlying File_read is locked.
228
229 class File_view
230 {
231 public:
232 // This may only be called when the underlying File_read is locked.
233 ~File_view();
234
235 // Return a pointer to the data associated with this view.
236 const unsigned char*
237 data() const
238 { return this->data_; }
239
240 private:
241 File_view(const File_view&);
242 File_view& operator=(const File_view&);
243
244 friend class File_read;
245
246 // Callers have to get these via File_read::get_lasting_view.
247 File_view(File_read& file, File_read::View* view, const unsigned char* data)
248 : file_(file), view_(view), data_(data)
249 { }
250
251 File_read& file_;
252 File_read::View* view_;
253 const unsigned char* data_;
254 };
255
256 // All the information we hold for a single input file. This can be
257 // an object file, a shared library, or an archive.
258
259 class Input_file
260 {
261 public:
262 Input_file(const Input_file_argument* input_argument)
263 : input_argument_(input_argument), file_(), is_in_sysroot_(false)
264 { }
265
266 // Create an input file with the contents already provided. This is
267 // only used for testing. With this path, don't call the open
268 // method.
269 Input_file(const char* name, const unsigned char* contents, off_t size);
270
271 // Open the file.
272 void
273 open(const General_options&, const Dirsearch&);
274
275 // Return the name given by the user.
276 const char*
277 name() const
278 { return this->input_argument_->name(); }
279
280 // Return the file name.
281 const std::string&
282 filename() const
283 { return this->file_.filename(); }
284
285 // Return the position dependent options.
286 const Position_dependent_options&
287 options() const
288 { return this->input_argument_->options(); }
289
290 // Return the file.
291 File_read&
292 file()
293 { return this->file_; }
294
295 // Whether we found the file in a directory in the system root.
296 bool
297 is_in_sysroot() const
298 { return this->is_in_sysroot_; }
299
300 private:
301 Input_file(const Input_file&);
302 Input_file& operator=(const Input_file&);
303
304 // The argument from the command line.
305 const Input_file_argument* input_argument_;
306 // The file after we open it.
307 File_read file_;
308 // Whether we found the file in a directory in the system root.
309 bool is_in_sysroot_;
310 };
311
312 } // end namespace gold
313
314 #endif // !defined(GOLD_FILEREAD_H)
This page took 0.045178 seconds and 5 git commands to generate.