]>
Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // fileread.cc -- read files for gold |
2 | ||
6cb15b7f ILT |
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 | ||
bae7f79e ILT |
23 | #include "gold.h" |
24 | ||
bae7f79e ILT |
25 | #include <cstring> |
26 | #include <cerrno> | |
27 | #include <fcntl.h> | |
28 | #include <unistd.h> | |
29 | ||
30 | #include "options.h" | |
31 | #include "dirsearch.h" | |
32 | #include "fileread.h" | |
33 | ||
34 | namespace gold | |
35 | { | |
36 | ||
37 | // Class File_read::View. | |
38 | ||
39 | File_read::View::~View() | |
40 | { | |
a3ad94ed | 41 | gold_assert(!this->is_locked()); |
bae7f79e ILT |
42 | delete[] this->data_; |
43 | } | |
44 | ||
45 | void | |
46 | File_read::View::lock() | |
47 | { | |
48 | ++this->lock_count_; | |
49 | } | |
50 | ||
51 | void | |
52 | File_read::View::unlock() | |
53 | { | |
a3ad94ed | 54 | gold_assert(this->lock_count_ > 0); |
bae7f79e ILT |
55 | --this->lock_count_; |
56 | } | |
57 | ||
58 | bool | |
59 | File_read::View::is_locked() | |
60 | { | |
61 | return this->lock_count_ > 0; | |
62 | } | |
63 | ||
64 | // Class File_read. | |
65 | ||
66 | // The File_read class is designed to support file descriptor caching, | |
67 | // but this is not currently implemented. | |
68 | ||
69 | File_read::~File_read() | |
70 | { | |
a3ad94ed | 71 | gold_assert(this->lock_count_ == 0); |
bae7f79e ILT |
72 | if (this->descriptor_ >= 0) |
73 | { | |
74 | if (close(this->descriptor_) < 0) | |
75 | fprintf(stderr, _("%s: warning: close(%s) failed: %s"), | |
76 | program_name, this->name_.c_str(), strerror(errno)); | |
77 | this->descriptor_ = -1; | |
78 | } | |
79 | this->name_.clear(); | |
80 | this->clear_views(true); | |
81 | } | |
82 | ||
5a6f7e2d ILT |
83 | // Open the file. |
84 | ||
bae7f79e ILT |
85 | bool |
86 | File_read::open(const std::string& name) | |
87 | { | |
a3ad94ed ILT |
88 | gold_assert(this->lock_count_ == 0 |
89 | && this->descriptor_ < 0 | |
90 | && this->name_.empty()); | |
bae7f79e | 91 | this->name_ = name; |
82dcae9d | 92 | |
bae7f79e | 93 | this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY); |
82dcae9d ILT |
94 | |
95 | if (this->descriptor_ >= 0) | |
96 | { | |
97 | struct stat s; | |
98 | if (::fstat(this->descriptor_, &s) < 0) | |
99 | { | |
100 | fprintf(stderr, _("%s: %s: fstat failed: %s"), program_name, | |
101 | this->name_.c_str(), strerror(errno)); | |
102 | gold_exit(false); | |
103 | } | |
104 | this->size_ = s.st_size; | |
105 | } | |
106 | ||
bae7f79e | 107 | ++this->lock_count_; |
82dcae9d | 108 | |
bae7f79e ILT |
109 | return this->descriptor_ >= 0; |
110 | } | |
111 | ||
5a6f7e2d ILT |
112 | // Open the file for testing purposes. |
113 | ||
114 | bool | |
115 | File_read::open(const std::string& name, const unsigned char* contents, | |
82dcae9d | 116 | off_t size) |
bae7f79e | 117 | { |
5a6f7e2d ILT |
118 | gold_assert(this->lock_count_ == 0 |
119 | && this->descriptor_ < 0 | |
120 | && this->name_.empty()); | |
121 | this->name_ = name; | |
122 | this->contents_ = contents; | |
82dcae9d | 123 | this->size_ = size; |
5a6f7e2d ILT |
124 | ++this->lock_count_; |
125 | return true; | |
bae7f79e ILT |
126 | } |
127 | ||
128 | void | |
129 | File_read::lock() | |
130 | { | |
131 | ++this->lock_count_; | |
132 | } | |
133 | ||
134 | void | |
135 | File_read::unlock() | |
136 | { | |
a3ad94ed | 137 | gold_assert(this->lock_count_ > 0); |
bae7f79e ILT |
138 | --this->lock_count_; |
139 | } | |
140 | ||
141 | bool | |
142 | File_read::is_locked() | |
143 | { | |
144 | return this->lock_count_ > 0; | |
145 | } | |
146 | ||
147 | // See if we have a view which covers the file starting at START for | |
148 | // SIZE bytes. Return a pointer to the View if found, NULL if not. | |
149 | ||
ead1e424 | 150 | inline File_read::View* |
bae7f79e ILT |
151 | File_read::find_view(off_t start, off_t size) |
152 | { | |
ead1e424 ILT |
153 | off_t page = File_read::page_offset(start); |
154 | Views::iterator p = this->views_.find(page); | |
155 | if (p == this->views_.end()) | |
156 | return NULL; | |
157 | if (p->second->size() - (start - page) < size) | |
158 | return NULL; | |
159 | return p->second; | |
bae7f79e ILT |
160 | } |
161 | ||
82dcae9d ILT |
162 | // Read SIZE bytes from the file starting at offset START. Read into |
163 | // the buffer at P. Return the number of bytes read, which should | |
164 | // always be at least SIZE except at the end of the file. | |
bae7f79e ILT |
165 | |
166 | off_t | |
82dcae9d | 167 | File_read::do_read(off_t start, off_t size, void* p) |
bae7f79e | 168 | { |
a3ad94ed | 169 | gold_assert(this->lock_count_ > 0); |
bae7f79e | 170 | |
82dcae9d | 171 | if (this->contents_ != NULL) |
bae7f79e | 172 | { |
82dcae9d | 173 | off_t bytes = this->size_ - start; |
5a6f7e2d ILT |
174 | if (bytes < 0) |
175 | bytes = 0; | |
176 | else if (bytes > size) | |
177 | bytes = size; | |
178 | memcpy(p, this->contents_ + start, bytes); | |
82dcae9d | 179 | return bytes; |
bae7f79e ILT |
180 | } |
181 | ||
82dcae9d ILT |
182 | off_t bytes = ::pread(this->descriptor_, p, size, start); |
183 | if (bytes < 0) | |
184 | { | |
185 | fprintf(stderr, _("%s: %s: pread failed: %s\n"), | |
186 | program_name, this->filename().c_str(), strerror(errno)); | |
187 | gold_exit(false); | |
188 | } | |
189 | ||
190 | return bytes; | |
191 | } | |
192 | ||
193 | // Read exactly SIZE bytes from the file starting at offset START. | |
194 | // Read into the buffer at P. | |
195 | ||
196 | void | |
197 | File_read::do_read_exact(off_t start, off_t size, void* p) | |
198 | { | |
199 | off_t bytes = this->do_read(start, size, p); | |
200 | if (bytes != size) | |
bae7f79e ILT |
201 | { |
202 | fprintf(stderr, | |
203 | _("%s: %s: file too short: read only %lld of %lld " | |
204 | "bytes at %lld\n"), | |
205 | program_name, this->filename().c_str(), | |
206 | static_cast<long long>(bytes), | |
207 | static_cast<long long>(size), | |
208 | static_cast<long long>(start)); | |
209 | gold_exit(false); | |
210 | } | |
bae7f79e ILT |
211 | } |
212 | ||
ba45d247 ILT |
213 | // Read data from the file. |
214 | ||
bae7f79e | 215 | void |
ba45d247 ILT |
216 | File_read::read(off_t start, off_t size, void* p) |
217 | { | |
218 | gold_assert(this->lock_count_ > 0); | |
219 | ||
220 | File_read::View* pv = this->find_view(start, size); | |
221 | if (pv != NULL) | |
222 | { | |
223 | memcpy(p, pv->data() + (start - pv->start()), size); | |
224 | return; | |
225 | } | |
226 | ||
82dcae9d | 227 | this->do_read_exact(start, size, p); |
bae7f79e ILT |
228 | } |
229 | ||
230 | // Find an existing view or make a new one. | |
231 | ||
232 | File_read::View* | |
bae3688d | 233 | File_read::find_or_make_view(off_t start, off_t size) |
bae7f79e | 234 | { |
a3ad94ed | 235 | gold_assert(this->lock_count_ > 0); |
bae7f79e | 236 | |
ead1e424 ILT |
237 | off_t poff = File_read::page_offset(start); |
238 | ||
239 | File_read::View* const vnull = NULL; | |
240 | std::pair<Views::iterator, bool> ins = | |
241 | this->views_.insert(std::make_pair(poff, vnull)); | |
242 | ||
243 | if (!ins.second) | |
244 | { | |
245 | // There was an existing view at this offset. | |
246 | File_read::View* v = ins.first->second; | |
247 | if (v->size() - (start - v->start()) >= size) | |
bae3688d | 248 | return v; |
ead1e424 ILT |
249 | |
250 | // This view is not large enough. | |
251 | this->saved_views_.push_back(v); | |
252 | } | |
253 | ||
82dcae9d ILT |
254 | // We need to read data from the file. We read full pages for |
255 | // greater efficiency on small files. | |
ead1e424 ILT |
256 | |
257 | off_t psize = File_read::pages(size + (start - poff)); | |
bae7f79e | 258 | |
82dcae9d ILT |
259 | if (poff + psize >= this->size_) |
260 | { | |
261 | psize = this->size_ - poff; | |
262 | gold_assert(psize >= size); | |
263 | } | |
ead1e424 | 264 | |
82dcae9d | 265 | unsigned char* p = new unsigned char[psize]; |
ead1e424 | 266 | |
82dcae9d | 267 | this->do_read_exact(poff, psize, p); |
ead1e424 | 268 | |
82dcae9d ILT |
269 | File_read::View* v = new File_read::View(poff, psize, p); |
270 | ins.first->second = v; | |
271 | return v; | |
bae7f79e ILT |
272 | } |
273 | ||
274 | // This implementation of get_view just reads into a memory buffer, | |
275 | // which we store on view_list_. At some point we should support | |
276 | // mmap. | |
277 | ||
278 | const unsigned char* | |
ba45d247 ILT |
279 | File_read::get_view(off_t start, off_t size) |
280 | { | |
281 | gold_assert(this->lock_count_ > 0); | |
bae3688d | 282 | File_read::View* pv = this->find_or_make_view(start, size); |
bae7f79e ILT |
283 | return pv->data() + (start - pv->start()); |
284 | } | |
285 | ||
286 | File_view* | |
ba45d247 | 287 | File_read::get_lasting_view(off_t start, off_t size) |
bae7f79e | 288 | { |
a3ad94ed | 289 | gold_assert(this->lock_count_ > 0); |
bae3688d | 290 | File_read::View* pv = this->find_or_make_view(start, size); |
bae7f79e ILT |
291 | pv->lock(); |
292 | return new File_view(*this, pv, pv->data() + (start - pv->start())); | |
293 | } | |
294 | ||
295 | // Remove all the file views. | |
296 | ||
297 | void | |
298 | File_read::clear_views(bool destroying) | |
299 | { | |
ead1e424 ILT |
300 | for (Views::iterator p = this->views_.begin(); |
301 | p != this->views_.end(); | |
302 | ++p) | |
bae7f79e | 303 | { |
ead1e424 ILT |
304 | if (!p->second->is_locked()) |
305 | delete p->second; | |
306 | else | |
bae7f79e | 307 | { |
a3ad94ed | 308 | gold_assert(!destroying); |
ead1e424 | 309 | this->saved_views_.push_back(p->second); |
bae7f79e | 310 | } |
ead1e424 ILT |
311 | } |
312 | this->views_.clear(); | |
313 | ||
314 | Saved_views::iterator p = this->saved_views_.begin(); | |
315 | while (p != this->saved_views_.end()) | |
316 | { | |
317 | if (!(*p)->is_locked()) | |
bae7f79e ILT |
318 | { |
319 | delete *p; | |
ead1e424 ILT |
320 | p = this->saved_views_.erase(p); |
321 | } | |
322 | else | |
323 | { | |
a3ad94ed | 324 | gold_assert(!destroying); |
ead1e424 | 325 | ++p; |
bae7f79e ILT |
326 | } |
327 | } | |
328 | } | |
329 | ||
330 | // Class File_view. | |
331 | ||
332 | File_view::~File_view() | |
333 | { | |
a3ad94ed | 334 | gold_assert(this->file_.is_locked()); |
bae7f79e ILT |
335 | this->view_->unlock(); |
336 | } | |
337 | ||
338 | // Class Input_file. | |
339 | ||
5a6f7e2d ILT |
340 | // Create a file for testing. |
341 | ||
342 | Input_file::Input_file(const char* name, const unsigned char* contents, | |
343 | off_t size) | |
344 | : file_() | |
345 | { | |
346 | this->input_argument_ = | |
347 | new Input_file_argument(name, false, Position_dependent_options()); | |
348 | bool ok = file_.open(name, contents, size); | |
349 | gold_assert(ok); | |
350 | } | |
351 | ||
352 | // Open the file. | |
353 | ||
bae7f79e ILT |
354 | void |
355 | Input_file::open(const General_options& options, const Dirsearch& dirpath) | |
356 | { | |
357 | std::string name; | |
5a6f7e2d ILT |
358 | if (!this->input_argument_->is_lib()) |
359 | name = this->input_argument_->name(); | |
bae7f79e ILT |
360 | else |
361 | { | |
362 | std::string n1("lib"); | |
5a6f7e2d | 363 | n1 += this->input_argument_->name(); |
bae7f79e | 364 | std::string n2; |
f6ce93d6 ILT |
365 | if (options.is_static()) |
366 | n1 += ".a"; | |
367 | else | |
368 | { | |
369 | n2 = n1 + ".a"; | |
370 | n1 += ".so"; | |
371 | } | |
bae7f79e ILT |
372 | name = dirpath.find(n1, n2); |
373 | if (name.empty()) | |
374 | { | |
61ba1cf9 | 375 | fprintf(stderr, _("%s: cannot find %s\n"), program_name, |
5a6f7e2d | 376 | this->input_argument_->name()); |
bae7f79e ILT |
377 | gold_exit(false); |
378 | } | |
379 | } | |
380 | ||
381 | if (!this->file_.open(name)) | |
382 | { | |
61ba1cf9 ILT |
383 | fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name, |
384 | name.c_str(), strerror(errno)); | |
bae7f79e ILT |
385 | gold_exit(false); |
386 | } | |
387 | } | |
388 | ||
389 | } // End namespace gold. |