1 // fileread.cc -- read files for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
30 #include "filenames.h"
33 #include "dirsearch.h"
39 // Class File_read::View.
41 File_read::View::~View()
43 gold_assert(!this->is_locked());
48 if (::munmap(const_cast<unsigned char*>(this->data_
), this->size_
) != 0)
49 gold_warning(_("munmap failed: %s"), strerror(errno
));
51 File_read::current_mapped_bytes
-= this->size_
;
56 File_read::View::lock()
62 File_read::View::unlock()
64 gold_assert(this->lock_count_
> 0);
69 File_read::View::is_locked()
71 return this->lock_count_
> 0;
76 // The File_read static variables.
77 unsigned long long File_read::total_mapped_bytes
;
78 unsigned long long File_read::current_mapped_bytes
;
79 unsigned long long File_read::maximum_mapped_bytes
;
81 // The File_read class is designed to support file descriptor caching,
82 // but this is not currently implemented.
84 File_read::~File_read()
86 gold_assert(this->lock_count_
== 0);
87 if (this->descriptor_
>= 0)
89 if (close(this->descriptor_
) < 0)
90 gold_warning(_("close of %s failed: %s"),
91 this->name_
.c_str(), strerror(errno
));
92 this->descriptor_
= -1;
95 this->clear_views(true);
101 File_read::open(const std::string
& name
)
103 gold_assert(this->lock_count_
== 0
104 && this->descriptor_
< 0
105 && this->name_
.empty());
108 this->descriptor_
= ::open(this->name_
.c_str(), O_RDONLY
);
110 if (this->descriptor_
>= 0)
113 if (::fstat(this->descriptor_
, &s
) < 0)
114 gold_error(_("%s: fstat failed: %s"),
115 this->name_
.c_str(), strerror(errno
));
116 this->size_
= s
.st_size
;
121 return this->descriptor_
>= 0;
124 // Open the file for testing purposes.
127 File_read::open(const std::string
& name
, const unsigned char* contents
,
130 gold_assert(this->lock_count_
== 0
131 && this->descriptor_
< 0
132 && this->name_
.empty());
134 this->contents_
= contents
;
149 gold_assert(this->lock_count_
> 0);
151 if (this->lock_count_
== 0)
153 File_read::total_mapped_bytes
+= this->mapped_bytes_
;
154 File_read::current_mapped_bytes
+= this->mapped_bytes_
;
155 this->mapped_bytes_
= 0;
156 if (File_read::current_mapped_bytes
> File_read::maximum_mapped_bytes
)
157 File_read::maximum_mapped_bytes
= File_read::current_mapped_bytes
;
159 this->clear_views(false);
164 File_read::is_locked()
166 return this->lock_count_
> 0;
169 // See if we have a view which covers the file starting at START for
170 // SIZE bytes. Return a pointer to the View if found, NULL if not.
172 inline File_read::View
*
173 File_read::find_view(off_t start
, off_t size
)
175 off_t page
= File_read::page_offset(start
);
176 Views::iterator p
= this->views_
.find(page
);
177 if (p
== this->views_
.end())
179 if (p
->second
->size() - (start
- page
) < size
)
184 // Read SIZE bytes from the file starting at offset START. Read into
188 File_read::do_read(off_t start
, off_t size
, void* p
)
190 gold_assert(this->lock_count_
> 0);
193 if (this->contents_
!= NULL
)
195 bytes
= this->size_
- start
;
198 memcpy(p
, this->contents_
+ start
, size
);
204 bytes
= ::pread(this->descriptor_
, p
, size
, start
);
210 gold_fatal(_("%s: pread failed: %s"),
211 this->filename().c_str(), strerror(errno
));
216 gold_fatal(_("%s: file too short: read only %lld of %lld bytes at %lld"),
217 this->filename().c_str(),
218 static_cast<long long>(bytes
),
219 static_cast<long long>(size
),
220 static_cast<long long>(start
));
223 // Read data from the file.
226 File_read::read(off_t start
, off_t size
, void* p
)
228 gold_assert(this->lock_count_
> 0);
230 File_read::View
* pv
= this->find_view(start
, size
);
233 memcpy(p
, pv
->data() + (start
- pv
->start()), size
);
237 this->do_read(start
, size
, p
);
240 // Find an existing view or make a new one.
243 File_read::find_or_make_view(off_t start
, off_t size
, bool cache
)
245 gold_assert(this->lock_count_
> 0);
247 off_t poff
= File_read::page_offset(start
);
249 File_read::View
* const vnull
= NULL
;
250 std::pair
<Views::iterator
, bool> ins
=
251 this->views_
.insert(std::make_pair(poff
, vnull
));
255 // There was an existing view at this offset.
256 File_read::View
* v
= ins
.first
->second
;
257 if (v
->size() - (start
- v
->start()) >= size
)
264 // This view is not large enough.
265 this->saved_views_
.push_back(v
);
268 // We need to read data from the file. We read full pages for
269 // greater efficiency on small files.
271 off_t psize
= File_read::pages(size
+ (start
- poff
));
273 if (poff
+ psize
>= this->size_
)
275 psize
= this->size_
- poff
;
276 gold_assert(psize
>= size
);
281 if (this->contents_
!= NULL
)
283 unsigned char* p
= new unsigned char[psize
];
284 this->do_read(poff
, psize
, p
);
285 v
= new File_read::View(poff
, psize
, p
, cache
, false);
289 void* p
= ::mmap(NULL
, psize
, PROT_READ
, MAP_SHARED
,
290 this->descriptor_
, poff
);
292 gold_fatal(_("%s: mmap offset %lld size %lld failed: %s"),
293 this->filename().c_str(),
294 static_cast<long long>(poff
),
295 static_cast<long long>(psize
),
298 this->mapped_bytes_
+= psize
;
300 const unsigned char* pbytes
= static_cast<const unsigned char*>(p
);
301 v
= new File_read::View(poff
, psize
, pbytes
, cache
, true);
304 ins
.first
->second
= v
;
308 // This implementation of get_view just reads into a memory buffer,
309 // which we store on view_list_. At some point we should support
313 File_read::get_view(off_t start
, off_t size
, bool cache
)
315 gold_assert(this->lock_count_
> 0);
316 File_read::View
* pv
= this->find_or_make_view(start
, size
, cache
);
317 return pv
->data() + (start
- pv
->start());
321 File_read::get_lasting_view(off_t start
, off_t size
, bool cache
)
323 gold_assert(this->lock_count_
> 0);
324 File_read::View
* pv
= this->find_or_make_view(start
, size
, cache
);
326 return new File_view(*this, pv
, pv
->data() + (start
- pv
->start()));
329 // Remove all the file views.
332 File_read::clear_views(bool destroying
)
334 for (Views::iterator p
= this->views_
.begin();
335 p
!= this->views_
.end();
338 if (!p
->second
->is_locked()
339 && (destroying
|| !p
->second
->should_cache()))
343 gold_assert(!destroying
);
344 this->saved_views_
.push_back(p
->second
);
347 this->views_
.clear();
349 Saved_views::iterator p
= this->saved_views_
.begin();
350 while (p
!= this->saved_views_
.end())
352 if (!(*p
)->is_locked()
353 && (destroying
|| !(*p
)->should_cache()))
356 p
= this->saved_views_
.erase(p
);
360 gold_assert(!destroying
);
366 // Print statistical information to stderr. This is used for --stats.
369 File_read::print_stats()
371 fprintf(stderr
, _("%s: total bytes mapped for read: %llu\n"),
372 program_name
, File_read::total_mapped_bytes
);
373 fprintf(stderr
, _("%s: maximum bytes mapped for read at one time: %llu\n"),
374 program_name
, File_read::maximum_mapped_bytes
);
379 File_view::~File_view()
381 gold_assert(this->file_
.is_locked());
382 this->view_
->unlock();
387 // Create a file for testing.
389 Input_file::Input_file(const char* name
, const unsigned char* contents
,
393 this->input_argument_
=
394 new Input_file_argument(name
, false, "", Position_dependent_options());
395 bool ok
= file_
.open(name
, contents
, size
);
401 // If the filename is not absolute, we assume it is in the current
402 // directory *except* when:
403 // A) input_argument_->is_lib() is true; or
404 // B) input_argument_->extra_search_path() is not empty.
405 // In both cases, we look in extra_search_path + library_path to find
406 // the file location, rather than the current directory.
409 Input_file::open(const General_options
& options
, const Dirsearch
& dirpath
)
413 // Case 1: name is an absolute file, just try to open it
414 // Case 2: name is relative but is_lib is false and extra_search_path
416 if (IS_ABSOLUTE_PATH (this->input_argument_
->name())
417 || (!this->input_argument_
->is_lib()
418 && this->input_argument_
->extra_search_path() == NULL
))
420 name
= this->input_argument_
->name();
421 this->found_name_
= name
;
423 // Case 3: is_lib is true
424 else if (this->input_argument_
->is_lib())
426 // We don't yet support extra_search_path with -l.
427 gold_assert(this->input_argument_
->extra_search_path() == NULL
);
428 std::string
n1("lib");
429 n1
+= this->input_argument_
->name();
431 if (options
.is_static()
432 || this->input_argument_
->options().do_static_search())
439 name
= dirpath
.find(n1
, n2
, &this->is_in_sysroot_
);
442 gold_error(_("cannot find -l%s"),
443 this->input_argument_
->name());
446 if (n2
.empty() || name
[name
.length() - 1] == 'o')
447 this->found_name_
= n1
;
449 this->found_name_
= n2
;
451 // Case 4: extra_search_path is not empty
454 gold_assert(this->input_argument_
->extra_search_path() != NULL
);
456 // First, check extra_search_path.
457 name
= this->input_argument_
->extra_search_path();
458 if (!IS_DIR_SEPARATOR (name
[name
.length() - 1]))
460 name
+= this->input_argument_
->name();
461 struct stat dummy_stat
;
462 if (::stat(name
.c_str(), &dummy_stat
) < 0)
464 // extra_search_path failed, so check the normal search-path.
465 name
= dirpath
.find(this->input_argument_
->name(), "",
466 &this->is_in_sysroot_
);
469 gold_error(_("cannot find %s"),
470 this->input_argument_
->name());
474 this->found_name_
= this->input_argument_
->name();
477 // Now that we've figured out where the file lives, try to open it.
478 if (!this->file_
.open(name
))
480 gold_error(_("cannot open %s: %s"),
481 name
.c_str(), strerror(errno
));
488 } // End namespace gold.
This page took 0.040048 seconds and 4 git commands to generate.