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.
31 #include "dirsearch.h"
37 // Class File_read::View.
39 File_read::View::~View()
41 gold_assert(!this->is_locked());
46 File_read::View::lock()
52 File_read::View::unlock()
54 gold_assert(this->lock_count_
> 0);
59 File_read::View::is_locked()
61 return this->lock_count_
> 0;
66 // The File_read class is designed to support file descriptor caching,
67 // but this is not currently implemented.
69 File_read::~File_read()
71 gold_assert(this->lock_count_
== 0);
72 if (this->descriptor_
>= 0)
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;
80 this->clear_views(true);
86 File_read::open(const std::string
& name
)
88 gold_assert(this->lock_count_
== 0
89 && this->descriptor_
< 0
90 && this->name_
.empty());
92 this->descriptor_
= ::open(this->name_
.c_str(), O_RDONLY
);
94 return this->descriptor_
>= 0;
97 // Open the file for testing purposes.
100 File_read::open(const std::string
& name
, const unsigned char* contents
,
103 gold_assert(this->lock_count_
== 0
104 && this->descriptor_
< 0
105 && this->name_
.empty());
107 this->contents_
= contents
;
108 this->contents_size_
= contents_size
;
122 gold_assert(this->lock_count_
> 0);
127 File_read::is_locked()
129 return this->lock_count_
> 0;
132 // See if we have a view which covers the file starting at START for
133 // SIZE bytes. Return a pointer to the View if found, NULL if not.
135 inline File_read::View
*
136 File_read::find_view(off_t start
, off_t size
)
138 off_t page
= File_read::page_offset(start
);
139 Views::iterator p
= this->views_
.find(page
);
140 if (p
== this->views_
.end())
142 if (p
->second
->size() - (start
- page
) < size
)
147 // Read data from the file. Return the number of bytes read. If
148 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
149 // require that we read exactly the number of bytes requested.
152 File_read::do_read(off_t start
, off_t size
, void* p
, off_t
* pbytes
)
154 gold_assert(this->lock_count_
> 0);
157 if (this->contents_
== NULL
)
159 int o
= this->descriptor_
;
161 if (lseek(o
, start
, SEEK_SET
) < 0)
163 fprintf(stderr
, _("%s: %s: lseek to %lld failed: %s"),
164 program_name
, this->filename().c_str(),
165 static_cast<long long>(start
),
170 bytes
= ::read(o
, p
, size
);
173 fprintf(stderr
, _("%s: %s: read failed: %s\n"),
174 program_name
, this->filename().c_str(), strerror(errno
));
180 bytes
= this->contents_size_
- start
;
183 else if (bytes
> size
)
185 memcpy(p
, this->contents_
+ start
, bytes
);
190 else if (bytes
!= size
)
193 _("%s: %s: file too short: read only %lld of %lld "
195 program_name
, this->filename().c_str(),
196 static_cast<long long>(bytes
),
197 static_cast<long long>(size
),
198 static_cast<long long>(start
));
206 File_read::read(off_t start
, off_t size
, void* p
, off_t
* pbytes
)
208 gold_assert(this->lock_count_
> 0);
210 File_read::View
* pv
= this->find_view(start
, size
);
213 memcpy(p
, pv
->data() + (start
- pv
->start()), size
);
219 this->do_read(start
, size
, p
, pbytes
);
222 // Find an existing view or make a new one.
225 File_read::find_or_make_view(off_t start
, off_t size
, off_t
* pbytes
)
227 gold_assert(this->lock_count_
> 0);
229 off_t poff
= File_read::page_offset(start
);
231 File_read::View
* const vnull
= NULL
;
232 std::pair
<Views::iterator
, bool> ins
=
233 this->views_
.insert(std::make_pair(poff
, vnull
));
237 // There was an existing view at this offset.
238 File_read::View
* v
= ins
.first
->second
;
239 if (v
->size() - (start
- v
->start()) >= size
)
246 // This view is not large enough.
247 this->saved_views_
.push_back(v
);
250 // We need to read data from the file.
252 off_t psize
= File_read::pages(size
+ (start
- poff
));
253 unsigned char* p
= new unsigned char[psize
];
256 off_t bytes
= this->do_read(poff
, psize
, p
, &got_bytes
);
258 File_read::View
* v
= new File_read::View(poff
, bytes
, p
);
260 ins
.first
->second
= v
;
262 if (bytes
- (start
- poff
) >= size
)
271 *pbytes
= bytes
- (start
- poff
);
276 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
277 program_name
, this->filename().c_str(),
278 static_cast<long long>(bytes
- (start
- poff
)),
279 static_cast<long long>(size
),
280 static_cast<long long>(start
));
284 // This implementation of get_view just reads into a memory buffer,
285 // which we store on view_list_. At some point we should support
289 File_read::get_view(off_t start
, off_t size
, off_t
* pbytes
)
291 gold_assert(this->lock_count_
> 0);
292 File_read::View
* pv
= this->find_or_make_view(start
, size
, pbytes
);
293 return pv
->data() + (start
- pv
->start());
297 File_read::get_lasting_view(off_t start
, off_t size
, off_t
* pbytes
)
299 gold_assert(this->lock_count_
> 0);
300 File_read::View
* pv
= this->find_or_make_view(start
, size
, pbytes
);
302 return new File_view(*this, pv
, pv
->data() + (start
- pv
->start()));
305 // Remove all the file views.
308 File_read::clear_views(bool destroying
)
310 for (Views::iterator p
= this->views_
.begin();
311 p
!= this->views_
.end();
314 if (!p
->second
->is_locked())
318 gold_assert(!destroying
);
319 this->saved_views_
.push_back(p
->second
);
322 this->views_
.clear();
324 Saved_views::iterator p
= this->saved_views_
.begin();
325 while (p
!= this->saved_views_
.end())
327 if (!(*p
)->is_locked())
330 p
= this->saved_views_
.erase(p
);
334 gold_assert(!destroying
);
342 File_view::~File_view()
344 gold_assert(this->file_
.is_locked());
345 this->view_
->unlock();
350 // Create a file for testing.
352 Input_file::Input_file(const char* name
, const unsigned char* contents
,
356 this->input_argument_
=
357 new Input_file_argument(name
, false, Position_dependent_options());
358 bool ok
= file_
.open(name
, contents
, size
);
365 Input_file::open(const General_options
& options
, const Dirsearch
& dirpath
)
368 if (!this->input_argument_
->is_lib())
369 name
= this->input_argument_
->name();
372 std::string
n1("lib");
373 n1
+= this->input_argument_
->name();
375 if (options
.is_static())
382 name
= dirpath
.find(n1
, n2
);
385 fprintf(stderr
, _("%s: cannot find %s\n"), program_name
,
386 this->input_argument_
->name());
391 if (!this->file_
.open(name
))
393 fprintf(stderr
, _("%s: cannot open %s: %s\n"), program_name
,
394 name
.c_str(), strerror(errno
));
399 } // End namespace gold.