Rewrite workqueue. This version eliminates the master thread, and
[deliverable/binutils-gdb.git] / gold / readsyms.cc
1 // readsyms.cc -- read input file symbols for gold
2
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
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "options.h"
29 #include "dirsearch.h"
30 #include "symtab.h"
31 #include "object.h"
32 #include "archive.h"
33 #include "script.h"
34 #include "readsyms.h"
35
36 namespace gold
37 {
38
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 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.
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 Task_token*
60 is_runnable()
61 {
62 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
63 return this->this_blocker_;
64 return NULL;
65 }
66
67 void
68 locks(Task_locker* tl)
69 { tl->add(this, this->next_blocker_); }
70
71 void
72 run(Workqueue*)
73 { }
74
75 std::string
76 get_name() const
77 { return "Unblock_token"; }
78
79 private:
80 Task_token* this_blocker_;
81 Task_token* next_blocker_;
82 };
83
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
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.
95
96 Task_token*
97 Read_symbols::is_runnable()
98 {
99 if (this->input_argument_->is_file()
100 && this->input_argument_->file().may_need_search()
101 && this->dirpath_->token()->is_blocked())
102 return this->dirpath_->token();
103
104 return NULL;
105 }
106
107 // Return a Task_locker for a Read_symbols task. We don't need any
108 // locks here.
109
110 void
111 Read_symbols::locks(Task_locker*)
112 {
113 }
114
115 // Run a Read_symbols task.
116
117 void
118 Read_symbols::run(Workqueue* workqueue)
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))
123 workqueue->queue_front(new Unblock_token(this->this_blocker_,
124 this->next_blocker_));
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)
132 {
133 if (this->input_argument_->is_group())
134 {
135 gold_assert(this->input_group_ == NULL);
136 this->do_group(workqueue);
137 return true;
138 }
139
140 Input_file* input_file = new Input_file(&this->input_argument_->file());
141 if (!input_file->open(this->options_, *this->dirpath_, this))
142 return false;
143
144 // Read enough of the file to pick up the entire ELF header.
145
146 off_t filesize = input_file->file().filesize();
147
148 if (filesize == 0)
149 {
150 gold_error(_("%s: file is empty"),
151 input_file->file().filename().c_str());
152 return false;
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)
164 {
165 static unsigned char elfmagic[4] =
166 {
167 elfcpp::ELFMAG0, elfcpp::ELFMAG1,
168 elfcpp::ELFMAG2, elfcpp::ELFMAG3
169 };
170 if (memcmp(ehdr_buf, elfmagic, 4) == 0)
171 {
172 // This is an ELF object.
173
174 Object* obj = make_elf_object(input_file->filename(),
175 input_file, 0, ehdr_buf, read_size);
176 if (obj == NULL)
177 return false;
178
179 // We don't have a way to record a non-archive in an input
180 // group. If this is an ordinary object file, we can't
181 // include it more than once anyhow. If this is a dynamic
182 // object, then including it a second time changes nothing.
183 if (this->input_group_ != NULL && !obj->is_dynamic())
184 {
185 gold_error(_("%s: ordinary object found in input group"),
186 input_file->name());
187 return false;
188 }
189
190 Read_symbols_data* sd = new Read_symbols_data;
191 obj->read_symbols(sd);
192
193 // Opening the file locked it, so now we need to unlock it.
194 // We need to unlock it before queuing the Add_symbols task,
195 // because the workqueue doesn't know about our lock on the
196 // file. If we queue the Add_symbols task first, it will be
197 // stuck on the end of the file lock, but since the
198 // workqueue doesn't know about that lock, it will never
199 // release the Add_symbols task.
200
201 input_file->file().unlock(this);
202
203 workqueue->queue_front(new Add_symbols(this->input_objects_,
204 this->symtab_, this->layout_,
205 obj, sd,
206 this->this_blocker_,
207 this->next_blocker_));
208
209 return true;
210 }
211 }
212
213 if (read_size >= Archive::sarmag)
214 {
215 if (memcmp(ehdr_buf, Archive::armag, Archive::sarmag) == 0)
216 {
217 // This is an archive.
218 Archive* arch = new Archive(this->input_argument_->file().name(),
219 input_file);
220 arch->setup(this);
221
222 workqueue->queue_front(new Add_archive_symbols(this->symtab_,
223 this->layout_,
224 this->input_objects_,
225 arch,
226 this->input_group_,
227 this->this_blocker_,
228 this->next_blocker_));
229 return true;
230 }
231 }
232
233 // Try to parse this file as a script.
234 if (read_input_script(workqueue, this->options_, this->symtab_,
235 this->layout_, this->dirpath_, this->input_objects_,
236 this->input_group_, this->input_argument_, input_file,
237 ehdr_buf, read_size, this->this_blocker_,
238 this->next_blocker_))
239 return true;
240
241 // Here we have to handle any other input file types we need.
242 gold_error(_("%s: not an object or archive"),
243 input_file->file().filename().c_str());
244
245 return false;
246 }
247
248 // Handle a group. We need to walk through the arguments over and
249 // over until we don't see any new undefined symbols. We do this by
250 // setting off Read_symbols Tasks as usual, but recording the archive
251 // entries instead of deleting them. We also start a Finish_group
252 // Task which runs after we've read all the symbols. In that task we
253 // process the archives in a loop until we are done.
254
255 void
256 Read_symbols::do_group(Workqueue* workqueue)
257 {
258 Input_group* input_group = new Input_group();
259
260 const Input_file_group* group = this->input_argument_->group();
261 Task_token* this_blocker = this->this_blocker_;
262
263 for (Input_file_group::const_iterator p = group->begin();
264 p != group->end();
265 ++p)
266 {
267 const Input_argument* arg = &*p;
268 gold_assert(arg->is_file());
269
270 Task_token* next_blocker = new Task_token(true);
271 next_blocker->add_blocker();
272 workqueue->queue(new Read_symbols(this->options_, this->input_objects_,
273 this->symtab_, this->layout_,
274 this->dirpath_, arg, input_group,
275 this_blocker, next_blocker));
276 this_blocker = next_blocker;
277 }
278
279 const int saw_undefined = this->symtab_->saw_undefined();
280 workqueue->queue(new Finish_group(this->input_objects_,
281 this->symtab_,
282 this->layout_,
283 input_group,
284 saw_undefined,
285 this_blocker,
286 this->next_blocker_));
287 }
288
289 // Return a debugging name for a Read_symbols task.
290
291 std::string
292 Read_symbols::get_name() const
293 {
294 if (!this->input_argument_->is_group())
295 {
296 std::string ret("Read_symbols ");
297 if (this->input_argument_->file().is_lib())
298 ret += "-l";
299 ret += this->input_argument_->file().name();
300 return ret;
301 }
302
303 std::string ret("Read_symbols group (");
304 bool add_space = false;
305 const Input_file_group* group = this->input_argument_->group();
306 for (Input_file_group::const_iterator p = group->begin();
307 p != group->end();
308 ++p)
309 {
310 if (add_space)
311 ret += ' ';
312 ret += p->file().name();
313 add_space = true;
314 }
315 return ret + ')';
316 }
317
318 // Class Add_symbols.
319
320 Add_symbols::~Add_symbols()
321 {
322 if (this->this_blocker_ != NULL)
323 delete this->this_blocker_;
324 // next_blocker_ is deleted by the task associated with the next
325 // input file.
326 }
327
328 // We are blocked by this_blocker_. We block next_blocker_. We also
329 // lock the file.
330
331 Task_token*
332 Add_symbols::is_runnable()
333 {
334 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
335 return this->this_blocker_;
336 if (this->object_->is_locked())
337 return this->object_->token();
338 return NULL;
339 }
340
341 void
342 Add_symbols::locks(Task_locker* tl)
343 {
344 tl->add(this, this->next_blocker_);
345 tl->add(this, this->object_->token());
346 }
347
348 // Add the symbols in the object to the symbol table.
349
350 void
351 Add_symbols::run(Workqueue*)
352 {
353 if (!this->input_objects_->add_object(this->object_))
354 {
355 // FIXME: We need to close the descriptor here.
356 delete this->object_;
357 }
358 else
359 {
360 this->object_->layout(this->symtab_, this->layout_, this->sd_);
361 this->object_->add_symbols(this->symtab_, this->sd_);
362 this->object_->release();
363 }
364 delete this->sd_;
365 this->sd_ = NULL;
366 }
367
368 // Class Finish_group.
369
370 Finish_group::~Finish_group()
371 {
372 if (this->this_blocker_ != NULL)
373 delete this->this_blocker_;
374 // next_blocker_ is deleted by the task associated with the next
375 // input file following the group.
376 }
377
378 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
379
380 Task_token*
381 Finish_group::is_runnable()
382 {
383 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
384 return this->this_blocker_;
385 return NULL;
386 }
387
388 void
389 Finish_group::locks(Task_locker* tl)
390 {
391 tl->add(this, this->next_blocker_);
392 }
393
394 // Loop over the archives until there are no new undefined symbols.
395
396 void
397 Finish_group::run(Workqueue*)
398 {
399 int saw_undefined = this->saw_undefined_;
400 while (saw_undefined != this->symtab_->saw_undefined())
401 {
402 saw_undefined = this->symtab_->saw_undefined();
403
404 for (Input_group::const_iterator p = this->input_group_->begin();
405 p != this->input_group_->end();
406 ++p)
407 {
408 Task_lock_obj<Archive> tl(this, *p);
409
410 (*p)->add_symbols(this->symtab_, this->layout_,
411 this->input_objects_);
412 }
413 }
414
415 // Delete all the archives now that we no longer need them.
416 for (Input_group::const_iterator p = this->input_group_->begin();
417 p != this->input_group_->end();
418 ++p)
419 delete *p;
420 delete this->input_group_;
421 }
422
423 } // End namespace gold.
This page took 0.040982 seconds and 5 git commands to generate.