Hash tables, dynamic section, i386 PLT, gold_assert.
[external/binutils.git] / gold / fileread.cc
1 // fileread.cc -- read files for gold
2
3 #include "gold.h"
4
5 #include <cstring>
6 #include <cerrno>
7 #include <fcntl.h>
8 #include <unistd.h>
9
10 #include "options.h"
11 #include "dirsearch.h"
12 #include "fileread.h"
13
14 namespace gold
15 {
16
17 // Class File_read::View.
18
19 File_read::View::~View()
20 {
21   gold_assert(!this->is_locked());
22   delete[] this->data_;
23 }
24
25 void
26 File_read::View::lock()
27 {
28   ++this->lock_count_;
29 }
30
31 void
32 File_read::View::unlock()
33 {
34   gold_assert(this->lock_count_ > 0);
35   --this->lock_count_;
36 }
37
38 bool
39 File_read::View::is_locked()
40 {
41   return this->lock_count_ > 0;
42 }
43
44 // Class File_read.
45
46 // The File_read class is designed to support file descriptor caching,
47 // but this is not currently implemented.
48
49 File_read::~File_read()
50 {
51   gold_assert(this->lock_count_ == 0);
52   if (this->descriptor_ >= 0)
53     {
54       if (close(this->descriptor_) < 0)
55         fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
56                 program_name, this->name_.c_str(), strerror(errno));
57       this->descriptor_ = -1;
58     }
59   this->name_.clear();
60   this->clear_views(true);
61 }
62
63 bool
64 File_read::open(const std::string& name)
65 {
66   gold_assert(this->lock_count_ == 0
67               && this->descriptor_ < 0
68               && this->name_.empty());
69   this->name_ = name;
70   this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
71   ++this->lock_count_;
72   return this->descriptor_ >= 0;
73 }
74
75 int
76 File_read::get_descriptor()
77 {
78   gold_assert(this->lock_count_ > 0);
79   return this->descriptor_;
80 }
81
82 void
83 File_read::lock()
84 {
85   ++this->lock_count_;
86 }
87
88 void
89 File_read::unlock()
90 {
91   gold_assert(this->lock_count_ > 0);
92   --this->lock_count_;
93 }
94
95 bool
96 File_read::is_locked()
97 {
98   return this->lock_count_ > 0;
99 }
100
101 // See if we have a view which covers the file starting at START for
102 // SIZE bytes.  Return a pointer to the View if found, NULL if not.
103
104 inline File_read::View*
105 File_read::find_view(off_t start, off_t size)
106 {
107   off_t page = File_read::page_offset(start);
108   Views::iterator p = this->views_.find(page);
109   if (p == this->views_.end())
110     return NULL;
111   if (p->second->size() - (start - page) < size)
112     return NULL;
113   return p->second;
114 }
115
116 // Read data from the file.  Return the number of bytes read.  If
117 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
118 // require that we read exactly the number of bytes requested.
119
120 off_t
121 File_read::do_read(off_t start, off_t size, void* p, off_t* pbytes)
122 {
123   gold_assert(this->lock_count_ > 0);
124   int o = this->descriptor_;
125
126   if (lseek(o, start, SEEK_SET) < 0)
127     {
128       fprintf(stderr, _("%s: %s: lseek to %lld failed: %s"),
129               program_name, this->filename().c_str(),
130               static_cast<long long>(start),
131               strerror(errno));
132       gold_exit(false);
133     }
134
135   off_t bytes = ::read(o, p, size);
136   if (bytes < 0)
137     {
138       fprintf(stderr, _("%s: %s: read failed: %s\n"),
139               program_name, this->filename().c_str(), strerror(errno));
140       gold_exit(false);
141     }
142
143   if (pbytes != NULL)
144     *pbytes = bytes;
145   else if (bytes != size)
146     {
147       fprintf(stderr,
148               _("%s: %s: file too short: read only %lld of %lld "
149                 "bytes at %lld\n"),
150               program_name, this->filename().c_str(),
151               static_cast<long long>(bytes),
152               static_cast<long long>(size),
153               static_cast<long long>(start));
154       gold_exit(false);
155     }
156
157   return bytes;
158 }
159
160 void
161 File_read::read(off_t start, off_t size, void* p, off_t* pbytes)
162 {
163   gold_assert(this->lock_count_ > 0);
164
165   File_read::View* pv = this->find_view(start, size);
166   if (pv != NULL)
167     {
168       memcpy(p, pv->data() + (start - pv->start()), size);
169       if (pbytes != NULL)
170         *pbytes = size;
171       return;
172     }
173
174   this->do_read(start, size, p, pbytes);
175 }
176
177 // Find an existing view or make a new one.
178
179 File_read::View*
180 File_read::find_or_make_view(off_t start, off_t size, off_t* pbytes)
181 {
182   gold_assert(this->lock_count_ > 0);
183
184   off_t poff = File_read::page_offset(start);
185
186   File_read::View* const vnull = NULL;
187   std::pair<Views::iterator, bool> ins =
188     this->views_.insert(std::make_pair(poff, vnull));
189
190   if (!ins.second)
191     {
192       // There was an existing view at this offset.
193       File_read::View* v = ins.first->second;
194       if (v->size() - (start - v->start()) >= size)
195         {
196           if (pbytes != NULL)
197             *pbytes = size;
198           return v;
199         }
200
201       // This view is not large enough.
202       this->saved_views_.push_back(v);
203     }
204
205   // We need to read data from the file.
206
207   off_t psize = File_read::pages(size + (start - poff));
208   unsigned char* p = new unsigned char[psize];
209
210   off_t got_bytes;
211   off_t bytes = this->do_read(poff, psize, p, &got_bytes);
212
213   File_read::View* v = new File_read::View(poff, bytes, p);
214
215   ins.first->second = v;
216
217   if (bytes - (start - poff) >= size)
218     {
219       if (pbytes != NULL)
220         *pbytes = size;
221       return v;
222     }
223
224   if (pbytes != NULL)
225     {
226       *pbytes = bytes - (start - poff);
227       return v;
228     }
229
230   fprintf(stderr,
231           _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
232           program_name, this->filename().c_str(),
233           static_cast<long long>(bytes - (start - poff)),
234           static_cast<long long>(size),
235           static_cast<long long>(start));
236   gold_exit(false);
237 }
238
239 // This implementation of get_view just reads into a memory buffer,
240 // which we store on view_list_.  At some point we should support
241 // mmap.
242
243 const unsigned char*
244 File_read::get_view(off_t start, off_t size, off_t* pbytes)
245 {
246   gold_assert(this->lock_count_ > 0);
247   File_read::View* pv = this->find_or_make_view(start, size, pbytes);
248   return pv->data() + (start - pv->start());
249 }
250
251 File_view*
252 File_read::get_lasting_view(off_t start, off_t size, off_t* pbytes)
253 {
254   gold_assert(this->lock_count_ > 0);
255   File_read::View* pv = this->find_or_make_view(start, size, pbytes);
256   pv->lock();
257   return new File_view(*this, pv, pv->data() + (start - pv->start()));
258 }
259
260 // Remove all the file views.
261
262 void
263 File_read::clear_views(bool destroying)
264 {
265   for (Views::iterator p = this->views_.begin();
266        p != this->views_.end();
267        ++p)
268     {
269       if (!p->second->is_locked())
270         delete p->second;
271       else
272         {
273           gold_assert(!destroying);
274           this->saved_views_.push_back(p->second);
275         }
276     }
277   this->views_.clear();
278
279   Saved_views::iterator p = this->saved_views_.begin();
280   while (p != this->saved_views_.end())
281     {
282       if (!(*p)->is_locked())
283         {
284           delete *p;
285           p = this->saved_views_.erase(p);
286         }
287       else
288         {
289           gold_assert(!destroying);
290           ++p;
291         }
292     }
293 }
294
295 // Class File_view.
296
297 File_view::~File_view()
298 {
299   gold_assert(this->file_.is_locked());
300   this->view_->unlock();
301 }
302
303 // Class Input_file.
304
305 void
306 Input_file::open(const General_options& options, const Dirsearch& dirpath)
307 {
308   std::string name;
309   if (!this->input_argument_.is_lib())
310     name = this->input_argument_.name();
311   else
312     {
313       std::string n1("lib");
314       n1 += this->input_argument_.name();
315       std::string n2;
316       if (options.is_static())
317         n1 += ".a";
318       else
319         {
320           n2 = n1 + ".a";
321           n1 += ".so";
322         }
323       name = dirpath.find(n1, n2);
324       if (name.empty())
325         {
326           fprintf(stderr, _("%s: cannot find %s\n"), program_name,
327                   this->input_argument_.name());
328           gold_exit(false);
329         }
330     }
331
332   if (!this->file_.open(name))
333     {
334       fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
335               name.c_str(), strerror(errno));
336       gold_exit(false);
337     }
338 }
339
340 } // End namespace gold.