Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // fileread.cc -- read files for gold |
2 | ||
3 | #include "gold.h" | |
4 | ||
5 | #include <cassert> | |
6 | #include <cstring> | |
7 | #include <cerrno> | |
8 | #include <fcntl.h> | |
9 | #include <unistd.h> | |
10 | ||
11 | #include "options.h" | |
12 | #include "dirsearch.h" | |
13 | #include "fileread.h" | |
14 | ||
15 | namespace gold | |
16 | { | |
17 | ||
18 | // Class File_read::View. | |
19 | ||
20 | File_read::View::~View() | |
21 | { | |
22 | assert(!this->is_locked()); | |
23 | delete[] this->data_; | |
24 | } | |
25 | ||
26 | void | |
27 | File_read::View::lock() | |
28 | { | |
29 | ++this->lock_count_; | |
30 | } | |
31 | ||
32 | void | |
33 | File_read::View::unlock() | |
34 | { | |
35 | assert(this->lock_count_ > 0); | |
36 | --this->lock_count_; | |
37 | } | |
38 | ||
39 | bool | |
40 | File_read::View::is_locked() | |
41 | { | |
42 | return this->lock_count_ > 0; | |
43 | } | |
44 | ||
45 | // Class File_read. | |
46 | ||
47 | // The File_read class is designed to support file descriptor caching, | |
48 | // but this is not currently implemented. | |
49 | ||
50 | File_read::~File_read() | |
51 | { | |
52 | assert(this->lock_count_ == 0); | |
53 | if (this->descriptor_ >= 0) | |
54 | { | |
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; | |
59 | } | |
60 | this->name_.clear(); | |
61 | this->clear_views(true); | |
62 | } | |
63 | ||
64 | bool | |
65 | File_read::open(const std::string& name) | |
66 | { | |
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; | |
74 | } | |
75 | ||
76 | int | |
77 | File_read::get_descriptor() | |
78 | { | |
79 | assert(this->lock_count_ > 0); | |
80 | return this->descriptor_; | |
81 | } | |
82 | ||
83 | void | |
84 | File_read::lock() | |
85 | { | |
86 | ++this->lock_count_; | |
87 | } | |
88 | ||
89 | void | |
90 | File_read::unlock() | |
91 | { | |
92 | assert(this->lock_count_ > 0); | |
93 | --this->lock_count_; | |
94 | } | |
95 | ||
96 | bool | |
97 | File_read::is_locked() | |
98 | { | |
99 | return this->lock_count_ > 0; | |
100 | } | |
101 | ||
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. | |
104 | ||
105 | File_read::View* | |
106 | File_read::find_view(off_t start, off_t size) | |
107 | { | |
108 | for (std::list<File_read::View*>::iterator p = this->view_list_.begin(); | |
109 | p != this->view_list_.end(); | |
110 | ++p) | |
111 | { | |
112 | if ((*p)->start() <= start | |
113 | && (*p)->start() + (*p)->size() >= start + size) | |
114 | return *p; | |
115 | } | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | // Read data from the file. Return the number of bytes read. If | |
120 | // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise | |
121 | // require that we read exactly the number of bytes requested. | |
122 | ||
123 | off_t | |
124 | File_read::do_read(off_t start, off_t size, void* p, off_t* pbytes) | |
125 | { | |
126 | assert(this->lock_count_ > 0); | |
127 | int o = this->descriptor_; | |
128 | ||
129 | if (lseek(o, start, SEEK_SET) < 0) | |
130 | { | |
131 | fprintf(stderr, _("%s: %s: lseek to %lld failed: %s"), | |
132 | program_name, this->filename().c_str(), | |
133 | static_cast<long long>(start), | |
134 | strerror(errno)); | |
135 | gold_exit(false); | |
136 | } | |
137 | ||
138 | off_t bytes = ::read(o, p, size); | |
139 | if (bytes < 0) | |
140 | { | |
141 | fprintf(stderr, _("%s: %s: read failed: %s\n"), | |
142 | program_name, this->filename().c_str(), strerror(errno)); | |
143 | gold_exit(false); | |
144 | } | |
145 | ||
146 | if (pbytes != NULL) | |
147 | *pbytes = bytes; | |
148 | else if (bytes != size) | |
149 | { | |
150 | fprintf(stderr, | |
151 | _("%s: %s: file too short: read only %lld of %lld " | |
152 | "bytes at %lld\n"), | |
153 | program_name, this->filename().c_str(), | |
154 | static_cast<long long>(bytes), | |
155 | static_cast<long long>(size), | |
156 | static_cast<long long>(start)); | |
157 | gold_exit(false); | |
158 | } | |
159 | ||
160 | return bytes; | |
161 | } | |
162 | ||
163 | void | |
164 | File_read::read(off_t start, off_t size, void* p, off_t* pbytes) | |
165 | { | |
166 | assert(this->lock_count_ > 0); | |
167 | ||
168 | File_read::View* pv = this->find_view(start, size); | |
169 | if (pv != NULL) | |
170 | { | |
171 | memcpy(p, pv->data() + (start - pv->start()), size); | |
172 | if (pbytes != NULL) | |
173 | *pbytes = size; | |
174 | return; | |
175 | } | |
176 | ||
177 | this->do_read(start, size, p, pbytes); | |
178 | } | |
179 | ||
180 | // Find an existing view or make a new one. | |
181 | ||
182 | File_read::View* | |
183 | File_read::find_or_make_view(off_t start, off_t size, off_t* pbytes) | |
184 | { | |
185 | assert(this->lock_count_ > 0); | |
186 | ||
187 | File_read::View* pv = this->find_view(start, size); | |
188 | if (pv != NULL) | |
189 | return pv; | |
190 | ||
191 | unsigned char* p = new unsigned char[size]; | |
192 | off_t bytes = this->do_read(start, size, p, pbytes); | |
193 | pv = new File_read::View(start, bytes, p); | |
194 | this->view_list_.push_back(pv); | |
195 | return pv; | |
196 | } | |
197 | ||
198 | // This implementation of get_view just reads into a memory buffer, | |
199 | // which we store on view_list_. At some point we should support | |
200 | // mmap. | |
201 | ||
202 | const unsigned char* | |
203 | File_read::get_view(off_t start, off_t size, off_t* pbytes) | |
204 | { | |
205 | assert(this->lock_count_ > 0); | |
206 | File_read::View* pv = this->find_or_make_view(start, size, pbytes); | |
207 | return pv->data() + (start - pv->start()); | |
208 | } | |
209 | ||
210 | File_view* | |
211 | File_read::get_lasting_view(off_t start, off_t size, off_t* pbytes) | |
212 | { | |
213 | assert(this->lock_count_ > 0); | |
214 | File_read::View* pv = this->find_or_make_view(start, size, pbytes); | |
215 | pv->lock(); | |
216 | return new File_view(*this, pv, pv->data() + (start - pv->start())); | |
217 | } | |
218 | ||
219 | // Remove all the file views. | |
220 | ||
221 | void | |
222 | File_read::clear_views(bool destroying) | |
223 | { | |
224 | std::list<File_read::View*>::iterator p = this->view_list_.begin(); | |
225 | while (p != this->view_list_.end()) | |
226 | { | |
227 | if ((*p)->is_locked()) | |
228 | { | |
229 | assert(!destroying); | |
230 | ++p; | |
231 | } | |
232 | else | |
233 | { | |
234 | delete *p; | |
235 | p = this->view_list_.erase(p); | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | // Class File_view. | |
241 | ||
242 | File_view::~File_view() | |
243 | { | |
244 | assert(this->file_.is_locked()); | |
245 | this->view_->unlock(); | |
246 | } | |
247 | ||
248 | // Class Input_file. | |
249 | ||
250 | void | |
251 | Input_file::open(const General_options& options, const Dirsearch& dirpath) | |
252 | { | |
253 | std::string name; | |
254 | if (!this->input_argument_.is_lib()) | |
255 | name = this->input_argument_.name(); | |
256 | else | |
257 | { | |
258 | std::string n1("lib"); | |
61ba1cf9 | 259 | n1 += this->input_argument_.name(); |
bae7f79e | 260 | std::string n2; |
61ba1cf9 | 261 | if (!options.is_static()) |
bae7f79e ILT |
262 | n2 = n1 + ".so"; |
263 | n1 += ".a"; | |
264 | name = dirpath.find(n1, n2); | |
265 | if (name.empty()) | |
266 | { | |
61ba1cf9 | 267 | fprintf(stderr, _("%s: cannot find %s\n"), program_name, |
bae7f79e ILT |
268 | this->input_argument_.name()); |
269 | gold_exit(false); | |
270 | } | |
271 | } | |
272 | ||
273 | if (!this->file_.open(name)) | |
274 | { | |
61ba1cf9 ILT |
275 | fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name, |
276 | name.c_str(), strerror(errno)); | |
bae7f79e ILT |
277 | gold_exit(false); |
278 | } | |
279 | } | |
280 | ||
281 | } // End namespace gold. |