Split Object into Dynobj and Relobj, incorporate elfcpp swapping changes.
[binutils.git] / gold / fileread.cc
blob43c69b3cf417e2e6ad852687cb28cbfa8a9a5aea
1 // fileread.cc -- read files for gold
3 #include "gold.h"
5 #include <cassert>
6 #include <cstring>
7 #include <cerrno>
8 #include <fcntl.h>
9 #include <unistd.h>
11 #include "options.h"
12 #include "dirsearch.h"
13 #include "fileread.h"
15 namespace gold
18 // Class File_read::View.
20 File_read::View::~View()
22 assert(!this->is_locked());
23 delete[] this->data_;
26 void
27 File_read::View::lock()
29 ++this->lock_count_;
32 void
33 File_read::View::unlock()
35 assert(this->lock_count_ > 0);
36 --this->lock_count_;
39 bool
40 File_read::View::is_locked()
42 return this->lock_count_ > 0;
45 // Class File_read.
47 // The File_read class is designed to support file descriptor caching,
48 // but this is not currently implemented.
50 File_read::~File_read()
52 assert(this->lock_count_ == 0);
53 if (this->descriptor_ >= 0)
55 if (close(this->descriptor_) < 0)
56 fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
57 program_name, this->name_.c_str(), strerror(errno));
58 this->descriptor_ = -1;
60 this->name_.clear();
61 this->clear_views(true);
64 bool
65 File_read::open(const std::string& name)
67 assert(this->lock_count_ == 0
68 && this->descriptor_ < 0
69 && this->name_.empty());
70 this->name_ = name;
71 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
72 ++this->lock_count_;
73 return this->descriptor_ >= 0;
76 int
77 File_read::get_descriptor()
79 assert(this->lock_count_ > 0);
80 return this->descriptor_;
83 void
84 File_read::lock()
86 ++this->lock_count_;
89 void
90 File_read::unlock()
92 assert(this->lock_count_ > 0);
93 --this->lock_count_;
96 bool
97 File_read::is_locked()
99 return this->lock_count_ > 0;
102 // See if we have a view which covers the file starting at START for
103 // SIZE bytes. Return a pointer to the View if found, NULL if not.
105 inline File_read::View*
106 File_read::find_view(off_t start, off_t size)
108 off_t page = File_read::page_offset(start);
109 Views::iterator p = this->views_.find(page);
110 if (p == this->views_.end())
111 return NULL;
112 if (p->second->size() - (start - page) < size)
113 return NULL;
114 return p->second;
117 // Read data from the file. Return the number of bytes read. If
118 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
119 // require that we read exactly the number of bytes requested.
121 off_t
122 File_read::do_read(off_t start, off_t size, void* p, off_t* pbytes)
124 assert(this->lock_count_ > 0);
125 int o = this->descriptor_;
127 if (lseek(o, start, SEEK_SET) < 0)
129 fprintf(stderr, _("%s: %s: lseek to %lld failed: %s"),
130 program_name, this->filename().c_str(),
131 static_cast<long long>(start),
132 strerror(errno));
133 gold_exit(false);
136 off_t bytes = ::read(o, p, size);
137 if (bytes < 0)
139 fprintf(stderr, _("%s: %s: read failed: %s\n"),
140 program_name, this->filename().c_str(), strerror(errno));
141 gold_exit(false);
144 if (pbytes != NULL)
145 *pbytes = bytes;
146 else if (bytes != size)
148 fprintf(stderr,
149 _("%s: %s: file too short: read only %lld of %lld "
150 "bytes at %lld\n"),
151 program_name, this->filename().c_str(),
152 static_cast<long long>(bytes),
153 static_cast<long long>(size),
154 static_cast<long long>(start));
155 gold_exit(false);
158 return bytes;
161 void
162 File_read::read(off_t start, off_t size, void* p, off_t* pbytes)
164 assert(this->lock_count_ > 0);
166 File_read::View* pv = this->find_view(start, size);
167 if (pv != NULL)
169 memcpy(p, pv->data() + (start - pv->start()), size);
170 if (pbytes != NULL)
171 *pbytes = size;
172 return;
175 this->do_read(start, size, p, pbytes);
178 // Find an existing view or make a new one.
180 File_read::View*
181 File_read::find_or_make_view(off_t start, off_t size, off_t* pbytes)
183 assert(this->lock_count_ > 0);
185 off_t poff = File_read::page_offset(start);
187 File_read::View* const vnull = NULL;
188 std::pair<Views::iterator, bool> ins =
189 this->views_.insert(std::make_pair(poff, vnull));
191 if (!ins.second)
193 // There was an existing view at this offset.
194 File_read::View* v = ins.first->second;
195 if (v->size() - (start - v->start()) >= size)
197 if (pbytes != NULL)
198 *pbytes = size;
199 return v;
202 // This view is not large enough.
203 this->saved_views_.push_back(v);
206 // We need to read data from the file.
208 off_t psize = File_read::pages(size + (start - poff));
209 unsigned char* p = new unsigned char[psize];
211 off_t got_bytes;
212 off_t bytes = this->do_read(poff, psize, p, &got_bytes);
214 File_read::View* v = new File_read::View(poff, bytes, p);
216 ins.first->second = v;
218 if (bytes - (start - poff) >= size)
220 if (pbytes != NULL)
221 *pbytes = size;
222 return v;
225 if (pbytes != NULL)
227 *pbytes = bytes - (start - poff);
228 return v;
231 fprintf(stderr,
232 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
233 program_name, this->filename().c_str(),
234 static_cast<long long>(bytes - (start - poff)),
235 static_cast<long long>(size),
236 static_cast<long long>(start));
237 gold_exit(false);
240 // This implementation of get_view just reads into a memory buffer,
241 // which we store on view_list_. At some point we should support
242 // mmap.
244 const unsigned char*
245 File_read::get_view(off_t start, off_t size, off_t* pbytes)
247 assert(this->lock_count_ > 0);
248 File_read::View* pv = this->find_or_make_view(start, size, pbytes);
249 return pv->data() + (start - pv->start());
252 File_view*
253 File_read::get_lasting_view(off_t start, off_t size, off_t* pbytes)
255 assert(this->lock_count_ > 0);
256 File_read::View* pv = this->find_or_make_view(start, size, pbytes);
257 pv->lock();
258 return new File_view(*this, pv, pv->data() + (start - pv->start()));
261 // Remove all the file views.
263 void
264 File_read::clear_views(bool destroying)
266 for (Views::iterator p = this->views_.begin();
267 p != this->views_.end();
268 ++p)
270 if (!p->second->is_locked())
271 delete p->second;
272 else
274 assert(!destroying);
275 this->saved_views_.push_back(p->second);
278 this->views_.clear();
280 Saved_views::iterator p = this->saved_views_.begin();
281 while (p != this->saved_views_.end())
283 if (!(*p)->is_locked())
285 delete *p;
286 p = this->saved_views_.erase(p);
288 else
290 assert(!destroying);
291 ++p;
296 // Class File_view.
298 File_view::~File_view()
300 assert(this->file_.is_locked());
301 this->view_->unlock();
304 // Class Input_file.
306 void
307 Input_file::open(const General_options& options, const Dirsearch& dirpath)
309 std::string name;
310 if (!this->input_argument_.is_lib())
311 name = this->input_argument_.name();
312 else
314 std::string n1("lib");
315 n1 += this->input_argument_.name();
316 std::string n2;
317 if (options.is_static())
318 n1 += ".a";
319 else
321 n2 = n1 + ".a";
322 n1 += ".so";
324 name = dirpath.find(n1, n2);
325 if (name.empty())
327 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
328 this->input_argument_.name());
329 gold_exit(false);
333 if (!this->file_.open(name))
335 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
336 name.c_str(), strerror(errno));
337 gold_exit(false);
341 } // End namespace gold.