Commit | Line | Data |
---|---|---|
bae7f79e ILT |
1 | // readsyms.cc -- read input file symbols for gold |
2 | ||
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
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 | |
17a1d0a9 ILT |
42 | // unblock tokens when the workqueue lock is held, so we need a dummy |
43 | // task to do that. The dummy task has to maintain the right sequence | |
44 | // of blocks, so we need both this_blocker and next_blocker. | |
ee6d2efe ILT |
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 | ||
17a1d0a9 ILT |
59 | Task_token* |
60 | is_runnable() | |
ee6d2efe ILT |
61 | { |
62 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
17a1d0a9 ILT |
63 | return this->this_blocker_; |
64 | return NULL; | |
ee6d2efe ILT |
65 | } |
66 | ||
17a1d0a9 ILT |
67 | void |
68 | locks(Task_locker* tl) | |
69 | { tl->add(this, this->next_blocker_); } | |
ee6d2efe ILT |
70 | |
71 | void | |
72 | run(Workqueue*) | |
73 | { } | |
74 | ||
c7912668 ILT |
75 | std::string |
76 | get_name() const | |
77 | { return "Unblock_token"; } | |
78 | ||
ee6d2efe ILT |
79 | private: |
80 | Task_token* this_blocker_; | |
81 | Task_token* next_blocker_; | |
82 | }; | |
83 | ||
bae7f79e ILT |
84 | // Class read_symbols. |
85 | ||
86 | Read_symbols::~Read_symbols() | |
87 | { | |
88 | // The this_blocker_ and next_blocker_ pointers are passed on to the | |
89 | // Add_symbols task. | |
90 | } | |
91 | ||
ead1e424 ILT |
92 | // Return whether a Read_symbols task is runnable. We can read an |
93 | // ordinary input file immediately. For an archive specified using | |
94 | // -l, we have to wait until the search path is complete. | |
bae7f79e | 95 | |
17a1d0a9 ILT |
96 | Task_token* |
97 | Read_symbols::is_runnable() | |
bae7f79e | 98 | { |
dbe717ef | 99 | if (this->input_argument_->is_file() |
51dee2fe | 100 | && this->input_argument_->file().may_need_search() |
17a1d0a9 ILT |
101 | && this->dirpath_->token()->is_blocked()) |
102 | return this->dirpath_->token(); | |
bae7f79e | 103 | |
17a1d0a9 | 104 | return NULL; |
bae7f79e ILT |
105 | } |
106 | ||
107 | // Return a Task_locker for a Read_symbols task. We don't need any | |
108 | // locks here. | |
109 | ||
17a1d0a9 ILT |
110 | void |
111 | Read_symbols::locks(Task_locker*) | |
bae7f79e | 112 | { |
bae7f79e ILT |
113 | } |
114 | ||
ee6d2efe | 115 | // Run a Read_symbols task. |
bae7f79e ILT |
116 | |
117 | void | |
118 | Read_symbols::run(Workqueue* workqueue) | |
ee6d2efe ILT |
119 | { |
120 | // If we didn't queue a new task, then we need to explicitly unblock | |
121 | // the token. | |
122 | if (!this->do_read_symbols(workqueue)) | |
da769d56 ILT |
123 | workqueue->queue_soon(new Unblock_token(this->this_blocker_, |
124 | this->next_blocker_)); | |
ee6d2efe ILT |
125 | } |
126 | ||
127 | // Open the file and read the symbols. Return true if a new task was | |
128 | // queued, false if that could not happen due to some error. | |
129 | ||
130 | bool | |
131 | Read_symbols::do_read_symbols(Workqueue* workqueue) | |
bae7f79e | 132 | { |
dbe717ef | 133 | if (this->input_argument_->is_group()) |
ead1e424 | 134 | { |
a3ad94ed | 135 | gold_assert(this->input_group_ == NULL); |
ead1e424 | 136 | this->do_group(workqueue); |
ee6d2efe | 137 | return true; |
ead1e424 ILT |
138 | } |
139 | ||
5a6f7e2d | 140 | Input_file* input_file = new Input_file(&this->input_argument_->file()); |
17a1d0a9 | 141 | if (!input_file->open(this->options_, *this->dirpath_, this)) |
ee6d2efe | 142 | return false; |
bae7f79e ILT |
143 | |
144 | // Read enough of the file to pick up the entire ELF header. | |
145 | ||
82dcae9d | 146 | off_t filesize = input_file->file().filesize(); |
bae3688d | 147 | |
82dcae9d ILT |
148 | if (filesize == 0) |
149 | { | |
75f2446e ILT |
150 | gold_error(_("%s: file is empty"), |
151 | input_file->file().filename().c_str()); | |
ee6d2efe | 152 | return false; |
82dcae9d ILT |
153 | } |
154 | ||
155 | unsigned char ehdr_buf[elfcpp::Elf_sizes<64>::ehdr_size]; | |
156 | ||
157 | int read_size = elfcpp::Elf_sizes<64>::ehdr_size; | |
158 | if (filesize < read_size) | |
159 | read_size = filesize; | |
160 | ||
161 | input_file->file().read(0, read_size, ehdr_buf); | |
162 | ||
163 | if (read_size >= 4) | |
bae7f79e ILT |
164 | { |
165 | static unsigned char elfmagic[4] = | |
166 | { | |
167 | elfcpp::ELFMAG0, elfcpp::ELFMAG1, | |
168 | elfcpp::ELFMAG2, elfcpp::ELFMAG3 | |
169 | }; | |
bae3688d | 170 | if (memcmp(ehdr_buf, elfmagic, 4) == 0) |
bae7f79e ILT |
171 | { |
172 | // This is an ELF object. | |
a2fb1b05 | 173 | |
dbe717ef | 174 | Object* obj = make_elf_object(input_file->filename(), |
82dcae9d | 175 | input_file, 0, ehdr_buf, read_size); |
75f2446e | 176 | if (obj == NULL) |
ee6d2efe | 177 | return false; |
dbe717ef | 178 | |
12e14209 ILT |
179 | Read_symbols_data* sd = new Read_symbols_data; |
180 | obj->read_symbols(sd); | |
17a1d0a9 ILT |
181 | |
182 | // Opening the file locked it, so now we need to unlock it. | |
183 | // We need to unlock it before queuing the Add_symbols task, | |
184 | // because the workqueue doesn't know about our lock on the | |
185 | // file. If we queue the Add_symbols task first, it will be | |
186 | // stuck on the end of the file lock, but since the | |
187 | // workqueue doesn't know about that lock, it will never | |
188 | // release the Add_symbols task. | |
189 | ||
190 | input_file->file().unlock(this); | |
191 | ||
da769d56 ILT |
192 | // We use queue_next because everything is cached for this |
193 | // task to run right away if possible. | |
194 | ||
195 | workqueue->queue_next(new Add_symbols(this->input_objects_, | |
196 | this->symtab_, this->layout_, | |
197 | obj, sd, | |
198 | this->this_blocker_, | |
199 | this->next_blocker_)); | |
bae7f79e | 200 | |
ee6d2efe | 201 | return true; |
bae7f79e ILT |
202 | } |
203 | } | |
204 | ||
82dcae9d | 205 | if (read_size >= Archive::sarmag) |
61ba1cf9 | 206 | { |
a1207466 CC |
207 | bool is_thin_archive |
208 | = memcmp(ehdr_buf, Archive::armagt, Archive::sarmag) == 0; | |
209 | if (is_thin_archive | |
210 | || memcmp(ehdr_buf, Archive::armag, Archive::sarmag) == 0) | |
61ba1cf9 ILT |
211 | { |
212 | // This is an archive. | |
dbe717ef | 213 | Archive* arch = new Archive(this->input_argument_->file().name(), |
a1207466 CC |
214 | input_file, is_thin_archive, |
215 | this->dirpath_, this); | |
216 | arch->setup(); | |
217 | ||
218 | // Unlock the archive so it can be used in the next task. | |
219 | arch->unlock(this); | |
17a1d0a9 | 220 | |
da769d56 ILT |
221 | workqueue->queue_next(new Add_archive_symbols(this->symtab_, |
222 | this->layout_, | |
223 | this->input_objects_, | |
224 | arch, | |
225 | this->input_group_, | |
226 | this->this_blocker_, | |
227 | this->next_blocker_)); | |
ee6d2efe | 228 | return true; |
61ba1cf9 ILT |
229 | } |
230 | } | |
231 | ||
da769d56 ILT |
232 | // Queue up a task to try to parse this file as a script. We use a |
233 | // separate task so that the script will be read in order with other | |
234 | // objects named on the command line. Also so that we don't try to | |
235 | // read multiple scripts simultaneously, which could lead to | |
236 | // unpredictable changes to the General_options structure. | |
237 | ||
238 | workqueue->queue_soon(new Read_script(this->options_, | |
239 | this->symtab_, | |
240 | this->layout_, | |
241 | this->dirpath_, | |
242 | this->input_objects_, | |
243 | this->input_group_, | |
244 | this->input_argument_, | |
245 | input_file, | |
246 | this->this_blocker_, | |
247 | this->next_blocker_)); | |
248 | return true; | |
bae7f79e ILT |
249 | } |
250 | ||
ead1e424 ILT |
251 | // Handle a group. We need to walk through the arguments over and |
252 | // over until we don't see any new undefined symbols. We do this by | |
253 | // setting off Read_symbols Tasks as usual, but recording the archive | |
254 | // entries instead of deleting them. We also start a Finish_group | |
255 | // Task which runs after we've read all the symbols. In that task we | |
256 | // process the archives in a loop until we are done. | |
257 | ||
258 | void | |
259 | Read_symbols::do_group(Workqueue* workqueue) | |
260 | { | |
261 | Input_group* input_group = new Input_group(); | |
262 | ||
dbe717ef | 263 | const Input_file_group* group = this->input_argument_->group(); |
ead1e424 | 264 | Task_token* this_blocker = this->this_blocker_; |
17a1d0a9 | 265 | |
ead1e424 ILT |
266 | for (Input_file_group::const_iterator p = group->begin(); |
267 | p != group->end(); | |
268 | ++p) | |
269 | { | |
dbe717ef | 270 | const Input_argument* arg = &*p; |
a3ad94ed | 271 | gold_assert(arg->is_file()); |
ead1e424 | 272 | |
17a1d0a9 | 273 | Task_token* next_blocker = new Task_token(true); |
ead1e424 | 274 | next_blocker->add_blocker(); |
da769d56 ILT |
275 | workqueue->queue_soon(new Read_symbols(this->options_, |
276 | this->input_objects_, | |
277 | this->symtab_, this->layout_, | |
278 | this->dirpath_, arg, input_group, | |
279 | this_blocker, next_blocker)); | |
ead1e424 ILT |
280 | this_blocker = next_blocker; |
281 | } | |
282 | ||
283 | const int saw_undefined = this->symtab_->saw_undefined(); | |
da769d56 ILT |
284 | workqueue->queue_soon(new Finish_group(this->input_objects_, |
285 | this->symtab_, | |
286 | this->layout_, | |
287 | input_group, | |
288 | saw_undefined, | |
289 | this_blocker, | |
290 | this->next_blocker_)); | |
ead1e424 ILT |
291 | } |
292 | ||
c7912668 ILT |
293 | // Return a debugging name for a Read_symbols task. |
294 | ||
295 | std::string | |
296 | Read_symbols::get_name() const | |
297 | { | |
298 | if (!this->input_argument_->is_group()) | |
299 | { | |
300 | std::string ret("Read_symbols "); | |
301 | if (this->input_argument_->file().is_lib()) | |
302 | ret += "-l"; | |
303 | ret += this->input_argument_->file().name(); | |
304 | return ret; | |
305 | } | |
306 | ||
307 | std::string ret("Read_symbols group ("); | |
308 | bool add_space = false; | |
309 | const Input_file_group* group = this->input_argument_->group(); | |
310 | for (Input_file_group::const_iterator p = group->begin(); | |
311 | p != group->end(); | |
312 | ++p) | |
313 | { | |
314 | if (add_space) | |
315 | ret += ' '; | |
316 | ret += p->file().name(); | |
317 | add_space = true; | |
318 | } | |
319 | return ret + ')'; | |
320 | } | |
321 | ||
bae7f79e ILT |
322 | // Class Add_symbols. |
323 | ||
324 | Add_symbols::~Add_symbols() | |
325 | { | |
326 | if (this->this_blocker_ != NULL) | |
327 | delete this->this_blocker_; | |
328 | // next_blocker_ is deleted by the task associated with the next | |
329 | // input file. | |
330 | } | |
331 | ||
a2fb1b05 ILT |
332 | // We are blocked by this_blocker_. We block next_blocker_. We also |
333 | // lock the file. | |
bae7f79e | 334 | |
17a1d0a9 ILT |
335 | Task_token* |
336 | Add_symbols::is_runnable() | |
bae7f79e ILT |
337 | { |
338 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
17a1d0a9 | 339 | return this->this_blocker_; |
a2fb1b05 | 340 | if (this->object_->is_locked()) |
17a1d0a9 ILT |
341 | return this->object_->token(); |
342 | return NULL; | |
bae7f79e ILT |
343 | } |
344 | ||
17a1d0a9 ILT |
345 | void |
346 | Add_symbols::locks(Task_locker* tl) | |
bae7f79e | 347 | { |
17a1d0a9 ILT |
348 | tl->add(this, this->next_blocker_); |
349 | tl->add(this, this->object_->token()); | |
bae7f79e ILT |
350 | } |
351 | ||
ead1e424 ILT |
352 | // Add the symbols in the object to the symbol table. |
353 | ||
bae7f79e ILT |
354 | void |
355 | Add_symbols::run(Workqueue*) | |
356 | { | |
008db82e ILT |
357 | if (!this->input_objects_->add_object(this->object_)) |
358 | { | |
359 | // FIXME: We need to close the descriptor here. | |
360 | delete this->object_; | |
361 | } | |
362 | else | |
363 | { | |
7e1edb90 | 364 | this->object_->layout(this->symtab_, this->layout_, this->sd_); |
008db82e | 365 | this->object_->add_symbols(this->symtab_, this->sd_); |
17a1d0a9 | 366 | this->object_->release(); |
008db82e | 367 | } |
12e14209 ILT |
368 | delete this->sd_; |
369 | this->sd_ = NULL; | |
bae7f79e ILT |
370 | } |
371 | ||
ead1e424 ILT |
372 | // Class Finish_group. |
373 | ||
374 | Finish_group::~Finish_group() | |
375 | { | |
376 | if (this->this_blocker_ != NULL) | |
377 | delete this->this_blocker_; | |
378 | // next_blocker_ is deleted by the task associated with the next | |
379 | // input file following the group. | |
380 | } | |
381 | ||
382 | // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_. | |
383 | ||
17a1d0a9 ILT |
384 | Task_token* |
385 | Finish_group::is_runnable() | |
ead1e424 ILT |
386 | { |
387 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
17a1d0a9 ILT |
388 | return this->this_blocker_; |
389 | return NULL; | |
ead1e424 ILT |
390 | } |
391 | ||
17a1d0a9 ILT |
392 | void |
393 | Finish_group::locks(Task_locker* tl) | |
ead1e424 | 394 | { |
17a1d0a9 | 395 | tl->add(this, this->next_blocker_); |
ead1e424 ILT |
396 | } |
397 | ||
398 | // Loop over the archives until there are no new undefined symbols. | |
399 | ||
400 | void | |
401 | Finish_group::run(Workqueue*) | |
402 | { | |
403 | int saw_undefined = this->saw_undefined_; | |
404 | while (saw_undefined != this->symtab_->saw_undefined()) | |
405 | { | |
406 | saw_undefined = this->symtab_->saw_undefined(); | |
407 | ||
408 | for (Input_group::const_iterator p = this->input_group_->begin(); | |
409 | p != this->input_group_->end(); | |
410 | ++p) | |
411 | { | |
17a1d0a9 | 412 | Task_lock_obj<Archive> tl(this, *p); |
ead1e424 | 413 | |
7e1edb90 | 414 | (*p)->add_symbols(this->symtab_, this->layout_, |
ead1e424 ILT |
415 | this->input_objects_); |
416 | } | |
417 | } | |
418 | ||
419 | // Delete all the archives now that we no longer need them. | |
420 | for (Input_group::const_iterator p = this->input_group_->begin(); | |
421 | p != this->input_group_->end(); | |
422 | ++p) | |
423 | delete *p; | |
424 | delete this->input_group_; | |
425 | } | |
426 | ||
da769d56 ILT |
427 | // Class Read_script |
428 | ||
429 | Read_script::~Read_script() | |
430 | { | |
431 | if (this->this_blocker_ != NULL) | |
432 | delete this->this_blocker_; | |
433 | // next_blocker_ is deleted by the task associated with the next | |
434 | // input file. | |
435 | } | |
436 | ||
437 | // We are blocked by this_blocker_. | |
438 | ||
439 | Task_token* | |
440 | Read_script::is_runnable() | |
441 | { | |
442 | if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked()) | |
443 | return this->this_blocker_; | |
444 | return NULL; | |
445 | } | |
446 | ||
447 | // We don't unlock next_blocker_ here. If the script names any input | |
448 | // files, then the last file will be responsible for unlocking it. | |
449 | ||
450 | void | |
451 | Read_script::locks(Task_locker*) | |
452 | { | |
453 | } | |
454 | ||
455 | // Read the script, if it is a script. | |
456 | ||
457 | void | |
458 | Read_script::run(Workqueue* workqueue) | |
459 | { | |
460 | bool used_next_blocker; | |
461 | if (!read_input_script(workqueue, this->options_, this->symtab_, | |
462 | this->layout_, this->dirpath_, this->input_objects_, | |
463 | this->input_group_, this->input_argument_, | |
464 | this->input_file_, this->next_blocker_, | |
465 | &used_next_blocker)) | |
466 | { | |
467 | // Here we have to handle any other input file types we need. | |
468 | gold_error(_("%s: not an object or archive"), | |
469 | this->input_file_->file().filename().c_str()); | |
470 | } | |
471 | ||
472 | if (!used_next_blocker) | |
473 | { | |
474 | // Queue up a task to unlock next_blocker. We can't just unlock | |
475 | // it here, as we don't hold the workqueue lock. | |
476 | workqueue->queue_soon(new Unblock_token(NULL, this->next_blocker_)); | |
477 | } | |
478 | } | |
479 | ||
480 | // Return a debugging name for a Read_script task. | |
481 | ||
482 | std::string | |
483 | Read_script::get_name() const | |
484 | { | |
485 | std::string ret("Read_script "); | |
486 | if (this->input_argument_->file().is_lib()) | |
487 | ret += "-l"; | |
488 | ret += this->input_argument_->file().name(); | |
489 | return ret; | |
490 | } | |
491 | ||
bae7f79e | 492 | } // End namespace gold. |