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