Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // readsyms.cc -- read input file symbols for gold |
2 | ||
6cb15b7f ILT |
3 | // Copyright 2006, 2007 Free Software Foundation, Inc. |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
bae7f79e ILT |
23 | #include "gold.h" |
24 | ||
25 | #include <cstring> | |
26 | ||
27 | #include "elfcpp.h" | |
28 | #include "options.h" | |
29 | #include "dirsearch.h" | |
f6ce93d6 | 30 | #include "symtab.h" |
a2fb1b05 | 31 | #include "object.h" |
61ba1cf9 | 32 | #include "archive.h" |
dbe717ef | 33 | #include "script.h" |
61ba1cf9 | 34 | #include "readsyms.h" |
bae7f79e ILT |
35 | |
36 | namespace gold | |
37 | { | |
38 | ||
ee6d2efe ILT |
39 | // If we fail to open the object, then we won't create an Add_symbols |
40 | // task. However, we still need to unblock the token, or else the | |
41 | // link won't proceed to generate more error messages. We can only | |
42 | // unblock tokens in the main thread, so we need a dummy task to do | |
43 | // that. The dummy task has to maintain the right sequence of blocks, | |
44 | // so we need both this_blocker and next_blocker. | |
45 | ||
46 | class Unblock_token : public Task | |
47 | { | |
48 | public: | |
49 | Unblock_token(Task_token* this_blocker, Task_token* next_blocker) | |
50 | : this_blocker_(this_blocker), next_blocker_(next_blocker) | |
51 | { } | |
52 | ||
53 | ~Unblock_token() | |
54 | { | |
55 | if (this->this_blocker_ != NULL) | |
56 | delete this->this_blocker_; | |
57 | } | |
58 | ||
59 | Is_runnable_type | |
60 | is_runnable(Workqueue*) | |
61 | { | |
62 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
63 | return IS_BLOCKED; | |
64 | return IS_RUNNABLE; | |
65 | } | |
66 | ||
67 | Task_locker* | |
68 | locks(Workqueue* workqueue) | |
69 | { return new Task_locker_block(*this->next_blocker_, workqueue); } | |
70 | ||
71 | void | |
72 | run(Workqueue*) | |
73 | { } | |
74 | ||
75 | private: | |
76 | Task_token* this_blocker_; | |
77 | Task_token* next_blocker_; | |
78 | }; | |
79 | ||
bae7f79e ILT |
80 | // Class read_symbols. |
81 | ||
82 | Read_symbols::~Read_symbols() | |
83 | { | |
84 | // The this_blocker_ and next_blocker_ pointers are passed on to the | |
85 | // Add_symbols task. | |
86 | } | |
87 | ||
ead1e424 ILT |
88 | // Return whether a Read_symbols task is runnable. We can read an |
89 | // ordinary input file immediately. For an archive specified using | |
90 | // -l, we have to wait until the search path is complete. | |
bae7f79e ILT |
91 | |
92 | Task::Is_runnable_type | |
93 | Read_symbols::is_runnable(Workqueue*) | |
94 | { | |
dbe717ef | 95 | if (this->input_argument_->is_file() |
51dee2fe | 96 | && this->input_argument_->file().may_need_search() |
ead1e424 | 97 | && this->dirpath_.token().is_blocked()) |
bae7f79e ILT |
98 | return IS_BLOCKED; |
99 | ||
100 | return IS_RUNNABLE; | |
101 | } | |
102 | ||
103 | // Return a Task_locker for a Read_symbols task. We don't need any | |
104 | // locks here. | |
105 | ||
106 | Task_locker* | |
107 | Read_symbols::locks(Workqueue*) | |
108 | { | |
109 | return NULL; | |
110 | } | |
111 | ||
ee6d2efe | 112 | // Run a Read_symbols task. |
bae7f79e ILT |
113 | |
114 | void | |
115 | Read_symbols::run(Workqueue* workqueue) | |
ee6d2efe ILT |
116 | { |
117 | // If we didn't queue a new task, then we need to explicitly unblock | |
118 | // the token. | |
119 | if (!this->do_read_symbols(workqueue)) | |
120 | workqueue->queue_front(new Unblock_token(this->this_blocker_, | |
121 | this->next_blocker_)); | |
122 | } | |
123 | ||
124 | // Open the file and read the symbols. Return true if a new task was | |
125 | // queued, false if that could not happen due to some error. | |
126 | ||
127 | bool | |
128 | Read_symbols::do_read_symbols(Workqueue* workqueue) | |
bae7f79e | 129 | { |
dbe717ef | 130 | if (this->input_argument_->is_group()) |
ead1e424 | 131 | { |
a3ad94ed | 132 | gold_assert(this->input_group_ == NULL); |
ead1e424 | 133 | this->do_group(workqueue); |
ee6d2efe | 134 | return true; |
ead1e424 ILT |
135 | } |
136 | ||
5a6f7e2d | 137 | Input_file* input_file = new Input_file(&this->input_argument_->file()); |
75f2446e | 138 | if (!input_file->open(this->options_, this->dirpath_)) |
ee6d2efe | 139 | return false; |
bae7f79e ILT |
140 | |
141 | // Read enough of the file to pick up the entire ELF header. | |
142 | ||
82dcae9d | 143 | off_t filesize = input_file->file().filesize(); |
bae3688d | 144 | |
82dcae9d ILT |
145 | if (filesize == 0) |
146 | { | |
75f2446e ILT |
147 | gold_error(_("%s: file is empty"), |
148 | input_file->file().filename().c_str()); | |
ee6d2efe | 149 | return false; |
82dcae9d ILT |
150 | } |
151 | ||
152 | unsigned char ehdr_buf[elfcpp::Elf_sizes<64>::ehdr_size]; | |
153 | ||
154 | int read_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
155 | if (filesize < read_size) | |
156 | read_size = filesize; | |
157 | ||
158 | input_file->file().read(0, read_size, ehdr_buf); | |
159 | ||
160 | if (read_size >= 4) | |
bae7f79e ILT |
161 | { |
162 | static unsigned char elfmagic[4] = | |
163 | { | |
164 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, | |
165 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 | |
166 | }; | |
bae3688d | 167 | if (memcmp(ehdr_buf, elfmagic, 4) == 0) |
bae7f79e ILT |
168 | { |
169 | // This is an ELF object. | |
a2fb1b05 | 170 | |
dbe717ef | 171 | Object* obj = make_elf_object(input_file->filename(), |
82dcae9d | 172 | input_file, 0, ehdr_buf, read_size); |
75f2446e | 173 | if (obj == NULL) |
ee6d2efe | 174 | return false; |
dbe717ef ILT |
175 | |
176 | // We don't have a way to record a non-archive in an input | |
177 | // group. If this is an ordinary object file, we can't | |
178 | // include it more than once anyhow. If this is a dynamic | |
179 | // object, then including it a second time changes nothing. | |
180 | if (this->input_group_ != NULL && !obj->is_dynamic()) | |
ead1e424 | 181 | { |
75f2446e ILT |
182 | gold_error(_("%s: ordinary object found in input group"), |
183 | input_file->name()); | |
ee6d2efe | 184 | return false; |
ead1e424 ILT |
185 | } |
186 | ||
12e14209 ILT |
187 | Read_symbols_data* sd = new Read_symbols_data; |
188 | obj->read_symbols(sd); | |
7e1edb90 | 189 | workqueue->queue_front(new Add_symbols(this->input_objects_, |
ead1e424 | 190 | this->symtab_, this->layout_, |
92e059d8 ILT |
191 | obj, sd, |
192 | this->this_blocker_, | |
193 | this->next_blocker_)); | |
bae7f79e ILT |
194 | |
195 | // Opening the file locked it, so now we need to unlock it. | |
196 | input_file->file().unlock(); | |
197 | ||
ee6d2efe | 198 | return true; |
bae7f79e ILT |
199 | } |
200 | } | |
201 | ||
82dcae9d | 202 | if (read_size >= Archive::sarmag) |
61ba1cf9 | 203 | { |
bae3688d | 204 | if (memcmp(ehdr_buf, Archive::armag, Archive::sarmag) == 0) |
61ba1cf9 ILT |
205 | { |
206 | // This is an archive. | |
dbe717ef ILT |
207 | Archive* arch = new Archive(this->input_argument_->file().name(), |
208 | input_file); | |
61ba1cf9 | 209 | arch->setup(); |
7e1edb90 | 210 | workqueue->queue(new Add_archive_symbols(this->symtab_, |
12e14209 | 211 | this->layout_, |
61ba1cf9 ILT |
212 | this->input_objects_, |
213 | arch, | |
ead1e424 | 214 | this->input_group_, |
61ba1cf9 ILT |
215 | this->this_blocker_, |
216 | this->next_blocker_)); | |
ee6d2efe | 217 | return true; |
61ba1cf9 ILT |
218 | } |
219 | } | |
220 | ||
dbe717ef ILT |
221 | // Try to parse this file as a script. |
222 | if (read_input_script(workqueue, this->options_, this->symtab_, | |
223 | this->layout_, this->dirpath_, this->input_objects_, | |
224 | this->input_group_, this->input_argument_, input_file, | |
82dcae9d | 225 | ehdr_buf, read_size, this->this_blocker_, |
bae3688d | 226 | this->next_blocker_)) |
ee6d2efe | 227 | return true; |
dbe717ef | 228 | |
92e059d8 | 229 | // Here we have to handle any other input file types we need. |
75f2446e ILT |
230 | gold_error(_("%s: not an object or archive"), |
231 | input_file->file().filename().c_str()); | |
ee6d2efe ILT |
232 | |
233 | return false; | |
bae7f79e ILT |
234 | } |
235 | ||
ead1e424 ILT |
236 | // Handle a group. We need to walk through the arguments over and |
237 | // over until we don't see any new undefined symbols. We do this by | |
238 | // setting off Read_symbols Tasks as usual, but recording the archive | |
239 | // entries instead of deleting them. We also start a Finish_group | |
240 | // Task which runs after we've read all the symbols. In that task we | |
241 | // process the archives in a loop until we are done. | |
242 | ||
243 | void | |
244 | Read_symbols::do_group(Workqueue* workqueue) | |
245 | { | |
246 | Input_group* input_group = new Input_group(); | |
247 | ||
dbe717ef | 248 | const Input_file_group* group = this->input_argument_->group(); |
ead1e424 ILT |
249 | Task_token* this_blocker = this->this_blocker_; |
250 | for (Input_file_group::const_iterator p = group->begin(); | |
251 | p != group->end(); | |
252 | ++p) | |
253 | { | |
dbe717ef | 254 | const Input_argument* arg = &*p; |
a3ad94ed | 255 | gold_assert(arg->is_file()); |
ead1e424 ILT |
256 | |
257 | Task_token* next_blocker = new Task_token(); | |
258 | next_blocker->add_blocker(); | |
259 | workqueue->queue(new Read_symbols(this->options_, this->input_objects_, | |
260 | this->symtab_, this->layout_, | |
261 | this->dirpath_, arg, input_group, | |
262 | this_blocker, next_blocker)); | |
263 | this_blocker = next_blocker; | |
264 | } | |
265 | ||
266 | const int saw_undefined = this->symtab_->saw_undefined(); | |
7e1edb90 | 267 | workqueue->queue(new Finish_group(this->input_objects_, |
ead1e424 ILT |
268 | this->symtab_, |
269 | this->layout_, | |
270 | input_group, | |
271 | saw_undefined, | |
272 | this_blocker, | |
273 | this->next_blocker_)); | |
274 | } | |
275 | ||
bae7f79e ILT |
276 | // Class Add_symbols. |
277 | ||
278 | Add_symbols::~Add_symbols() | |
279 | { | |
280 | if (this->this_blocker_ != NULL) | |
281 | delete this->this_blocker_; | |
282 | // next_blocker_ is deleted by the task associated with the next | |
283 | // input file. | |
284 | } | |
285 | ||
a2fb1b05 ILT |
286 | // We are blocked by this_blocker_. We block next_blocker_. We also |
287 | // lock the file. | |
bae7f79e ILT |
288 | |
289 | Task::Is_runnable_type | |
290 | Add_symbols::is_runnable(Workqueue*) | |
291 | { | |
292 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
293 | return IS_BLOCKED; | |
a2fb1b05 ILT |
294 | if (this->object_->is_locked()) |
295 | return IS_LOCKED; | |
bae7f79e ILT |
296 | return IS_RUNNABLE; |
297 | } | |
298 | ||
a2fb1b05 ILT |
299 | class Add_symbols::Add_symbols_locker : public Task_locker |
300 | { | |
301 | public: | |
302 | Add_symbols_locker(Task_token& token, Workqueue* workqueue, | |
303 | Object* object) | |
304 | : blocker_(token, workqueue), objlock_(*object) | |
305 | { } | |
306 | ||
307 | private: | |
308 | Task_locker_block blocker_; | |
309 | Task_locker_obj<Object> objlock_; | |
310 | }; | |
311 | ||
bae7f79e ILT |
312 | Task_locker* |
313 | Add_symbols::locks(Workqueue* workqueue) | |
314 | { | |
a2fb1b05 ILT |
315 | return new Add_symbols_locker(*this->next_blocker_, workqueue, |
316 | this->object_); | |
bae7f79e ILT |
317 | } |
318 | ||
ead1e424 ILT |
319 | // Add the symbols in the object to the symbol table. |
320 | ||
bae7f79e ILT |
321 | void |
322 | Add_symbols::run(Workqueue*) | |
323 | { | |
008db82e ILT |
324 | if (!this->input_objects_->add_object(this->object_)) |
325 | { | |
326 | // FIXME: We need to close the descriptor here. | |
327 | delete this->object_; | |
328 | } | |
329 | else | |
330 | { | |
7e1edb90 | 331 | this->object_->layout(this->symtab_, this->layout_, this->sd_); |
008db82e ILT |
332 | this->object_->add_symbols(this->symtab_, this->sd_); |
333 | } | |
12e14209 ILT |
334 | delete this->sd_; |
335 | this->sd_ = NULL; | |
bae7f79e ILT |
336 | } |
337 | ||
ead1e424 ILT |
338 | // Class Finish_group. |
339 | ||
340 | Finish_group::~Finish_group() | |
341 | { | |
342 | if (this->this_blocker_ != NULL) | |
343 | delete this->this_blocker_; | |
344 | // next_blocker_ is deleted by the task associated with the next | |
345 | // input file following the group. | |
346 | } | |
347 | ||
348 | // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_. | |
349 | ||
350 | Task::Is_runnable_type | |
351 | Finish_group::is_runnable(Workqueue*) | |
352 | { | |
353 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
354 | return IS_BLOCKED; | |
355 | return IS_RUNNABLE; | |
356 | } | |
357 | ||
358 | Task_locker* | |
359 | Finish_group::locks(Workqueue* workqueue) | |
360 | { | |
361 | return new Task_locker_block(*this->next_blocker_, workqueue); | |
362 | } | |
363 | ||
364 | // Loop over the archives until there are no new undefined symbols. | |
365 | ||
366 | void | |
367 | Finish_group::run(Workqueue*) | |
368 | { | |
369 | int saw_undefined = this->saw_undefined_; | |
370 | while (saw_undefined != this->symtab_->saw_undefined()) | |
371 | { | |
372 | saw_undefined = this->symtab_->saw_undefined(); | |
373 | ||
374 | for (Input_group::const_iterator p = this->input_group_->begin(); | |
375 | p != this->input_group_->end(); | |
376 | ++p) | |
377 | { | |
378 | Task_lock_obj<Archive> tl(**p); | |
379 | ||
7e1edb90 | 380 | (*p)->add_symbols(this->symtab_, this->layout_, |
ead1e424 ILT |
381 | this->input_objects_); |
382 | } | |
383 | } | |
384 | ||
385 | // Delete all the archives now that we no longer need them. | |
386 | for (Input_group::const_iterator p = this->input_group_->begin(); | |
387 | p != this->input_group_->end(); | |
388 | ++p) | |
389 | delete *p; | |
390 | delete this->input_group_; | |
391 | } | |
392 | ||
bae7f79e | 393 | } // End namespace gold. |