daily update
[deliverable/binutils-gdb.git] / gold / fileread.cc
CommitLineData
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>
d1038c21 29#include <sys/mman.h>
51dee2fe 30#include "filenames.h"
bae7f79e
ILT
31
32#include "options.h"
33#include "dirsearch.h"
34#include "fileread.h"
35
36namespace gold
37{
38
39// Class File_read::View.
40
41File_read::View::~View()
42{
a3ad94ed 43 gold_assert(!this->is_locked());
d1038c21
ILT
44 if (!this->mapped_)
45 delete[] this->data_;
46 else
47 {
48 if (::munmap(const_cast<unsigned char*>(this->data_), this->size_) != 0)
49 fprintf(stderr, _("%s: munmap failed: %s\n"),
50 program_name, strerror(errno));
51 }
bae7f79e
ILT
52}
53
54void
55File_read::View::lock()
56{
57 ++this->lock_count_;
58}
59
60void
61File_read::View::unlock()
62{
a3ad94ed 63 gold_assert(this->lock_count_ > 0);
bae7f79e
ILT
64 --this->lock_count_;
65}
66
67bool
68File_read::View::is_locked()
69{
70 return this->lock_count_ > 0;
71}
72
73// Class File_read.
74
75// The File_read class is designed to support file descriptor caching,
76// but this is not currently implemented.
77
78File_read::~File_read()
79{
a3ad94ed 80 gold_assert(this->lock_count_ == 0);
bae7f79e
ILT
81 if (this->descriptor_ >= 0)
82 {
83 if (close(this->descriptor_) < 0)
84 fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
85 program_name, this->name_.c_str(), strerror(errno));
86 this->descriptor_ = -1;
87 }
88 this->name_.clear();
89 this->clear_views(true);
90}
91
5a6f7e2d
ILT
92// Open the file.
93
bae7f79e
ILT
94bool
95File_read::open(const std::string& name)
96{
a3ad94ed
ILT
97 gold_assert(this->lock_count_ == 0
98 && this->descriptor_ < 0
99 && this->name_.empty());
bae7f79e 100 this->name_ = name;
82dcae9d 101
bae7f79e 102 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
82dcae9d
ILT
103
104 if (this->descriptor_ >= 0)
105 {
106 struct stat s;
107 if (::fstat(this->descriptor_, &s) < 0)
108 {
109 fprintf(stderr, _("%s: %s: fstat failed: %s"), program_name,
110 this->name_.c_str(), strerror(errno));
111 gold_exit(false);
112 }
113 this->size_ = s.st_size;
114 }
115
bae7f79e 116 ++this->lock_count_;
82dcae9d 117
bae7f79e
ILT
118 return this->descriptor_ >= 0;
119}
120
5a6f7e2d
ILT
121// Open the file for testing purposes.
122
123bool
124File_read::open(const std::string& name, const unsigned char* contents,
82dcae9d 125 off_t size)
bae7f79e 126{
5a6f7e2d
ILT
127 gold_assert(this->lock_count_ == 0
128 && this->descriptor_ < 0
129 && this->name_.empty());
130 this->name_ = name;
131 this->contents_ = contents;
82dcae9d 132 this->size_ = size;
5a6f7e2d
ILT
133 ++this->lock_count_;
134 return true;
bae7f79e
ILT
135}
136
137void
138File_read::lock()
139{
140 ++this->lock_count_;
141}
142
143void
144File_read::unlock()
145{
a3ad94ed 146 gold_assert(this->lock_count_ > 0);
bae7f79e 147 --this->lock_count_;
9eb9fa57
ILT
148 if (this->lock_count_ == 0)
149 this->clear_views(false);
bae7f79e
ILT
150}
151
152bool
153File_read::is_locked()
154{
155 return this->lock_count_ > 0;
156}
157
158// See if we have a view which covers the file starting at START for
159// SIZE bytes. Return a pointer to the View if found, NULL if not.
160
ead1e424 161inline File_read::View*
bae7f79e
ILT
162File_read::find_view(off_t start, off_t size)
163{
ead1e424
ILT
164 off_t page = File_read::page_offset(start);
165 Views::iterator p = this->views_.find(page);
166 if (p == this->views_.end())
167 return NULL;
168 if (p->second->size() - (start - page) < size)
169 return NULL;
170 return p->second;
bae7f79e
ILT
171}
172
82dcae9d 173// Read SIZE bytes from the file starting at offset START. Read into
9eb9fa57 174// the buffer at P.
bae7f79e 175
9eb9fa57 176void
82dcae9d 177File_read::do_read(off_t start, off_t size, void* p)
bae7f79e 178{
a3ad94ed 179 gold_assert(this->lock_count_ > 0);
bae7f79e 180
9eb9fa57 181 off_t bytes;
82dcae9d 182 if (this->contents_ != NULL)
bae7f79e 183 {
9eb9fa57
ILT
184 bytes = this->size_ - start;
185 if (bytes >= size)
186 {
187 memcpy(p, this->contents_ + start, size);
188 return;
189 }
bae7f79e 190 }
9eb9fa57 191 else
82dcae9d 192 {
9eb9fa57
ILT
193 bytes = ::pread(this->descriptor_, p, size, start);
194 if (bytes == size)
195 return;
82dcae9d 196
9eb9fa57
ILT
197 if (bytes < 0)
198 {
199 fprintf(stderr, _("%s: %s: pread failed: %s\n"),
200 program_name, this->filename().c_str(), strerror(errno));
201 gold_exit(false);
202 }
bae7f79e 203 }
9eb9fa57
ILT
204
205 fprintf(stderr,
206 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
207 program_name, this->filename().c_str(),
208 static_cast<long long>(bytes),
209 static_cast<long long>(size),
210 static_cast<long long>(start));
211 gold_exit(false);
bae7f79e
ILT
212}
213
ba45d247
ILT
214// Read data from the file.
215
bae7f79e 216void
ba45d247
ILT
217File_read::read(off_t start, off_t size, void* p)
218{
219 gold_assert(this->lock_count_ > 0);
220
221 File_read::View* pv = this->find_view(start, size);
222 if (pv != NULL)
223 {
224 memcpy(p, pv->data() + (start - pv->start()), size);
225 return;
226 }
227
9eb9fa57 228 this->do_read(start, size, p);
bae7f79e
ILT
229}
230
231// Find an existing view or make a new one.
232
233File_read::View*
9eb9fa57 234File_read::find_or_make_view(off_t start, off_t size, bool cache)
bae7f79e 235{
a3ad94ed 236 gold_assert(this->lock_count_ > 0);
bae7f79e 237
ead1e424
ILT
238 off_t poff = File_read::page_offset(start);
239
240 File_read::View* const vnull = NULL;
241 std::pair<Views::iterator, bool> ins =
242 this->views_.insert(std::make_pair(poff, vnull));
243
244 if (!ins.second)
245 {
246 // There was an existing view at this offset.
247 File_read::View* v = ins.first->second;
248 if (v->size() - (start - v->start()) >= size)
9eb9fa57
ILT
249 {
250 if (cache)
251 v->set_cache();
252 return v;
253 }
ead1e424
ILT
254
255 // This view is not large enough.
256 this->saved_views_.push_back(v);
257 }
258
82dcae9d
ILT
259 // We need to read data from the file. We read full pages for
260 // greater efficiency on small files.
ead1e424
ILT
261
262 off_t psize = File_read::pages(size + (start - poff));
bae7f79e 263
82dcae9d
ILT
264 if (poff + psize >= this->size_)
265 {
266 psize = this->size_ - poff;
267 gold_assert(psize >= size);
268 }
ead1e424 269
d1038c21 270 File_read::View* v;
ead1e424 271
d1038c21
ILT
272 if (this->contents_ != NULL)
273 {
274 unsigned char* p = new unsigned char[psize];
275 this->do_read(poff, psize, p);
276 v = new File_read::View(poff, psize, p, cache, false);
277 }
278 else
279 {
280 void* p = ::mmap(NULL, psize, PROT_READ, MAP_SHARED,
281 this->descriptor_, poff);
282 if (p == MAP_FAILED)
283 {
284 fprintf(stderr, _("%s: %s: mmap offset %lld size %lld failed: %s\n"),
285 program_name, this->filename().c_str(),
286 static_cast<long long>(poff),
287 static_cast<long long>(psize),
288 strerror(errno));
289 gold_exit(false);
290 }
291
292 const unsigned char* pbytes = static_cast<const unsigned char*>(p);
293 v = new File_read::View(poff, psize, pbytes, cache, true);
294 }
ead1e424 295
82dcae9d
ILT
296 ins.first->second = v;
297 return v;
bae7f79e
ILT
298}
299
300// This implementation of get_view just reads into a memory buffer,
301// which we store on view_list_. At some point we should support
302// mmap.
303
304const unsigned char*
9eb9fa57 305File_read::get_view(off_t start, off_t size, bool cache)
ba45d247
ILT
306{
307 gold_assert(this->lock_count_ > 0);
9eb9fa57 308 File_read::View* pv = this->find_or_make_view(start, size, cache);
bae7f79e
ILT
309 return pv->data() + (start - pv->start());
310}
311
312File_view*
9eb9fa57 313File_read::get_lasting_view(off_t start, off_t size, bool cache)
bae7f79e 314{
a3ad94ed 315 gold_assert(this->lock_count_ > 0);
9eb9fa57 316 File_read::View* pv = this->find_or_make_view(start, size, cache);
bae7f79e
ILT
317 pv->lock();
318 return new File_view(*this, pv, pv->data() + (start - pv->start()));
319}
320
321// Remove all the file views.
322
323void
324File_read::clear_views(bool destroying)
325{
ead1e424
ILT
326 for (Views::iterator p = this->views_.begin();
327 p != this->views_.end();
328 ++p)
bae7f79e 329 {
9eb9fa57
ILT
330 if (!p->second->is_locked()
331 && (destroying || !p->second->should_cache()))
ead1e424
ILT
332 delete p->second;
333 else
bae7f79e 334 {
a3ad94ed 335 gold_assert(!destroying);
ead1e424 336 this->saved_views_.push_back(p->second);
bae7f79e 337 }
ead1e424
ILT
338 }
339 this->views_.clear();
340
341 Saved_views::iterator p = this->saved_views_.begin();
342 while (p != this->saved_views_.end())
343 {
9eb9fa57
ILT
344 if (!(*p)->is_locked()
345 && (destroying || !(*p)->should_cache()))
bae7f79e
ILT
346 {
347 delete *p;
ead1e424
ILT
348 p = this->saved_views_.erase(p);
349 }
350 else
351 {
a3ad94ed 352 gold_assert(!destroying);
ead1e424 353 ++p;
bae7f79e
ILT
354 }
355 }
356}
357
358// Class File_view.
359
360File_view::~File_view()
361{
a3ad94ed 362 gold_assert(this->file_.is_locked());
bae7f79e
ILT
363 this->view_->unlock();
364}
365
366// Class Input_file.
367
5a6f7e2d
ILT
368// Create a file for testing.
369
370Input_file::Input_file(const char* name, const unsigned char* contents,
371 off_t size)
372 : file_()
373{
374 this->input_argument_ =
51dee2fe 375 new Input_file_argument(name, false, "", Position_dependent_options());
5a6f7e2d
ILT
376 bool ok = file_.open(name, contents, size);
377 gold_assert(ok);
378}
379
380// Open the file.
381
51dee2fe
ILT
382// If the filename is not absolute, we assume it is in the current
383// directory *except* when:
384// A) input_argument_->is_lib() is true; or
385// B) input_argument_->extra_search_path() is not empty.
386// In both cases, we look in extra_search_path + library_path to find
387// the file location, rather than the current directory.
388
bae7f79e
ILT
389void
390Input_file::open(const General_options& options, const Dirsearch& dirpath)
391{
392 std::string name;
51dee2fe
ILT
393
394 // Case 1: name is an absolute file, just try to open it
395 // Case 2: name is relative but is_lib is false and extra_search_path
396 // is empty
397 if (IS_ABSOLUTE_PATH (this->input_argument_->name())
398 || (!this->input_argument_->is_lib()
399 && this->input_argument_->extra_search_path() == NULL))
5a6f7e2d 400 name = this->input_argument_->name();
51dee2fe
ILT
401
402 // Case 3: is_lib is true
403 else if (this->input_argument_->is_lib())
bae7f79e 404 {
51dee2fe
ILT
405 // We don't yet support extra_search_path with -l.
406 gold_assert(this->input_argument_->extra_search_path() == NULL);
bae7f79e 407 std::string n1("lib");
5a6f7e2d 408 n1 += this->input_argument_->name();
bae7f79e 409 std::string n2;
f6ce93d6
ILT
410 if (options.is_static())
411 n1 += ".a";
412 else
413 {
414 n2 = n1 + ".a";
415 n1 += ".so";
416 }
bae7f79e
ILT
417 name = dirpath.find(n1, n2);
418 if (name.empty())
419 {
61ba1cf9 420 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
5a6f7e2d 421 this->input_argument_->name());
bae7f79e
ILT
422 gold_exit(false);
423 }
424 }
425
51dee2fe
ILT
426 // Case 4: extra_search_path is not empty
427 else
428 {
429 gold_assert(this->input_argument_->extra_search_path() != NULL);
430
431 // First, check extra_search_path.
432 name = this->input_argument_->extra_search_path();
433 if (!IS_DIR_SEPARATOR (name[name.length() - 1]))
434 name += '/';
435 name += this->input_argument_->name();
436 struct stat dummy_stat;
437 if (::stat(name.c_str(), &dummy_stat) < 0)
438 {
439 // extra_search_path failed, so check the normal search-path.
440 name = dirpath.find(this->input_argument_->name(), "");
441 if (name.empty())
442 {
443 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
444 this->input_argument_->name());
445 gold_exit(false);
446 }
447 }
448 }
449
450 // Now that we've figured out where the file lives, try to open it.
bae7f79e
ILT
451 if (!this->file_.open(name))
452 {
61ba1cf9
ILT
453 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
454 name.c_str(), strerror(errno));
bae7f79e
ILT
455 gold_exit(false);
456 }
457}
458
459} // End namespace gold.
This page took 0.096146 seconds and 4 git commands to generate.