]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gold/fileread.cc
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 fprintf(stderr
, _("%s: munmap failed: %s\n"),
50 program_name
, strerror(errno
));
55 File_read::View::lock()
61 File_read::View::unlock()
63 gold_assert(this->lock_count_
> 0);
68 File_read::View::is_locked()
70 return this->lock_count_
> 0;
75 // The File_read class is designed to support file descriptor caching,
76 // but this is not currently implemented.
78 File_read::~File_read()
80 gold_assert(this->lock_count_
== 0);
81 if (this->descriptor_
>= 0)
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;
89 this->clear_views(true);
95 File_read::open(const std::string
& name
)
97 gold_assert(this->lock_count_
== 0
98 && this->descriptor_
< 0
99 && this->name_
.empty());
102 this->descriptor_
= ::open(this->name_
.c_str(), O_RDONLY
);
104 if (this->descriptor_
>= 0)
107 if (::fstat(this->descriptor_
, &s
) < 0)
109 fprintf(stderr
, _("%s: %s: fstat failed: %s"), program_name
,
110 this->name_
.c_str(), strerror(errno
));
113 this->size_
= s
.st_size
;
118 return this->descriptor_
>= 0;
121 // Open the file for testing purposes.
124 File_read::open(const std::string
& name
, const unsigned char* contents
,
127 gold_assert(this->lock_count_
== 0
128 && this->descriptor_
< 0
129 && this->name_
.empty());
131 this->contents_
= contents
;
146 gold_assert(this->lock_count_
> 0);
148 if (this->lock_count_
== 0)
149 this->clear_views(false);
153 File_read::is_locked()
155 return this->lock_count_
> 0;
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.
161 inline File_read::View
*
162 File_read::find_view(off_t start
, off_t size
)
164 off_t page
= File_read::page_offset(start
);
165 Views::iterator p
= this->views_
.find(page
);
166 if (p
== this->views_
.end())
168 if (p
->second
->size() - (start
- page
) < size
)
173 // Read SIZE bytes from the file starting at offset START. Read into
177 File_read::do_read(off_t start
, off_t size
, void* p
)
179 gold_assert(this->lock_count_
> 0);
182 if (this->contents_
!= NULL
)
184 bytes
= this->size_
- start
;
187 memcpy(p
, this->contents_
+ start
, size
);
193 bytes
= ::pread(this->descriptor_
, p
, size
, start
);
199 fprintf(stderr
, _("%s: %s: pread failed: %s\n"),
200 program_name
, this->filename().c_str(), strerror(errno
));
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
));
214 // Read data from the file.
217 File_read::read(off_t start
, off_t size
, void* p
)
219 gold_assert(this->lock_count_
> 0);
221 File_read::View
* pv
= this->find_view(start
, size
);
224 memcpy(p
, pv
->data() + (start
- pv
->start()), size
);
228 this->do_read(start
, size
, p
);
231 // Find an existing view or make a new one.
234 File_read::find_or_make_view(off_t start
, off_t size
, bool cache
)
236 gold_assert(this->lock_count_
> 0);
238 off_t poff
= File_read::page_offset(start
);
240 File_read::View
* const vnull
= NULL
;
241 std::pair
<Views::iterator
, bool> ins
=
242 this->views_
.insert(std::make_pair(poff
, vnull
));
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
)
255 // This view is not large enough.
256 this->saved_views_
.push_back(v
);
259 // We need to read data from the file. We read full pages for
260 // greater efficiency on small files.
262 off_t psize
= File_read::pages(size
+ (start
- poff
));
264 if (poff
+ psize
>= this->size_
)
266 psize
= this->size_
- poff
;
267 gold_assert(psize
>= size
);
272 if (this->contents_
!= NULL
)
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);
280 void* p
= ::mmap(NULL
, psize
, PROT_READ
, MAP_SHARED
,
281 this->descriptor_
, poff
);
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
),
292 const unsigned char* pbytes
= static_cast<const unsigned char*>(p
);
293 v
= new File_read::View(poff
, psize
, pbytes
, cache
, true);
296 ins
.first
->second
= v
;
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
305 File_read::get_view(off_t start
, off_t size
, bool cache
)
307 gold_assert(this->lock_count_
> 0);
308 File_read::View
* pv
= this->find_or_make_view(start
, size
, cache
);
309 return pv
->data() + (start
- pv
->start());
313 File_read::get_lasting_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
);
318 return new File_view(*this, pv
, pv
->data() + (start
- pv
->start()));
321 // Remove all the file views.
324 File_read::clear_views(bool destroying
)
326 for (Views::iterator p
= this->views_
.begin();
327 p
!= this->views_
.end();
330 if (!p
->second
->is_locked()
331 && (destroying
|| !p
->second
->should_cache()))
335 gold_assert(!destroying
);
336 this->saved_views_
.push_back(p
->second
);
339 this->views_
.clear();
341 Saved_views::iterator p
= this->saved_views_
.begin();
342 while (p
!= this->saved_views_
.end())
344 if (!(*p
)->is_locked()
345 && (destroying
|| !(*p
)->should_cache()))
348 p
= this->saved_views_
.erase(p
);
352 gold_assert(!destroying
);
360 File_view::~File_view()
362 gold_assert(this->file_
.is_locked());
363 this->view_
->unlock();
368 // Create a file for testing.
370 Input_file::Input_file(const char* name
, const unsigned char* contents
,
374 this->input_argument_
=
375 new Input_file_argument(name
, false, "", Position_dependent_options());
376 bool ok
= file_
.open(name
, contents
, size
);
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.
390 Input_file::open(const General_options
& options
, const Dirsearch
& dirpath
)
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
397 if (IS_ABSOLUTE_PATH (this->input_argument_
->name())
398 || (!this->input_argument_
->is_lib()
399 && this->input_argument_
->extra_search_path() == NULL
))
400 name
= this->input_argument_
->name();
402 // Case 3: is_lib is true
403 else if (this->input_argument_
->is_lib())
405 // We don't yet support extra_search_path with -l.
406 gold_assert(this->input_argument_
->extra_search_path() == NULL
);
407 std::string
n1("lib");
408 n1
+= this->input_argument_
->name();
410 if (options
.is_static())
417 name
= dirpath
.find(n1
, n2
, &this->is_in_sysroot_
);
420 fprintf(stderr
, _("%s: cannot find -l%s\n"), program_name
,
421 this->input_argument_
->name());
426 // Case 4: extra_search_path is not empty
429 gold_assert(this->input_argument_
->extra_search_path() != NULL
);
431 // First, check extra_search_path.
432 name
= this->input_argument_
->extra_search_path();
433 if (!IS_DIR_SEPARATOR (name
[name
.length() - 1]))
435 name
+= this->input_argument_
->name();
436 struct stat dummy_stat
;
437 if (::stat(name
.c_str(), &dummy_stat
) < 0)
439 // extra_search_path failed, so check the normal search-path.
440 name
= dirpath
.find(this->input_argument_
->name(), "",
441 &this->is_in_sysroot_
);
444 fprintf(stderr
, _("%s: cannot find %s\n"), program_name
,
445 this->input_argument_
->name());
451 // Now that we've figured out where the file lives, try to open it.
452 if (!this->file_
.open(name
))
454 fprintf(stderr
, _("%s: cannot open %s: %s\n"), program_name
,
455 name
.c_str(), strerror(errno
));
460 } // End namespace gold.