1 // readsyms.cc -- read input file symbols for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
29 #include "dirsearch.h"
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.
46 class Unblock_token
: public Task
49 Unblock_token(Task_token
* this_blocker
, Task_token
* next_blocker
)
50 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
55 if (this->this_blocker_
!= NULL
)
56 delete this->this_blocker_
;
62 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
63 return this->this_blocker_
;
68 locks(Task_locker
* tl
)
69 { tl
->add(this, this->next_blocker_
); }
77 { return "Unblock_token"; }
80 Task_token
* this_blocker_
;
81 Task_token
* next_blocker_
;
84 // Class read_symbols.
86 Read_symbols::~Read_symbols()
88 // The this_blocker_ and next_blocker_ pointers are passed on to the
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.
97 Read_symbols::is_runnable()
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();
107 // Return a Task_locker for a Read_symbols task. We don't need any
111 Read_symbols::locks(Task_locker
*)
115 // Run a Read_symbols task.
118 Read_symbols::run(Workqueue
* workqueue
)
120 // If we didn't queue a new task, then we need to explicitly unblock
122 if (!this->do_read_symbols(workqueue
))
123 workqueue
->queue_soon(new Unblock_token(this->this_blocker_
,
124 this->next_blocker_
));
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.
131 Read_symbols::do_read_symbols(Workqueue
* workqueue
)
133 if (this->input_argument_
->is_group())
135 gold_assert(this->input_group_
== NULL
);
136 this->do_group(workqueue
);
140 Input_file
* input_file
= new Input_file(&this->input_argument_
->file());
141 if (!input_file
->open(this->options_
, *this->dirpath_
, this))
144 // Read enough of the file to pick up the entire ELF header.
146 off_t filesize
= input_file
->file().filesize();
150 gold_error(_("%s: file is empty"),
151 input_file
->file().filename().c_str());
155 unsigned char ehdr_buf
[elfcpp::Elf_sizes
<64>::ehdr_size
];
157 int read_size
= elfcpp::Elf_sizes
<64>::ehdr_size
;
158 if (filesize
< read_size
)
159 read_size
= filesize
;
161 input_file
->file().read(0, read_size
, ehdr_buf
);
165 static unsigned char elfmagic
[4] =
167 elfcpp::ELFMAG0
, elfcpp::ELFMAG1
,
168 elfcpp::ELFMAG2
, elfcpp::ELFMAG3
170 if (memcmp(ehdr_buf
, elfmagic
, 4) == 0)
172 // This is an ELF object.
174 Object
* obj
= make_elf_object(input_file
->filename(),
175 input_file
, 0, ehdr_buf
, read_size
);
179 Read_symbols_data
* sd
= new Read_symbols_data
;
180 obj
->read_symbols(sd
);
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.
190 input_file
->file().unlock(this);
192 // We use queue_next because everything is cached for this
193 // task to run right away if possible.
195 workqueue
->queue_next(new Add_symbols(this->input_objects_
,
196 this->symtab_
, this->layout_
,
199 this->next_blocker_
));
205 if (read_size
>= Archive::sarmag
)
207 if (memcmp(ehdr_buf
, Archive::armag
, Archive::sarmag
) == 0)
209 // This is an archive.
210 Archive
* arch
= new Archive(this->input_argument_
->file().name(),
214 workqueue
->queue_next(new Add_archive_symbols(this->symtab_
,
216 this->input_objects_
,
220 this->next_blocker_
));
225 // Queue up a task to try to parse this file as a script. We use a
226 // separate task so that the script will be read in order with other
227 // objects named on the command line. Also so that we don't try to
228 // read multiple scripts simultaneously, which could lead to
229 // unpredictable changes to the General_options structure.
231 workqueue
->queue_soon(new Read_script(this->options_
,
235 this->input_objects_
,
237 this->input_argument_
,
240 this->next_blocker_
));
244 // Handle a group. We need to walk through the arguments over and
245 // over until we don't see any new undefined symbols. We do this by
246 // setting off Read_symbols Tasks as usual, but recording the archive
247 // entries instead of deleting them. We also start a Finish_group
248 // Task which runs after we've read all the symbols. In that task we
249 // process the archives in a loop until we are done.
252 Read_symbols::do_group(Workqueue
* workqueue
)
254 Input_group
* input_group
= new Input_group();
256 const Input_file_group
* group
= this->input_argument_
->group();
257 Task_token
* this_blocker
= this->this_blocker_
;
259 for (Input_file_group::const_iterator p
= group
->begin();
263 const Input_argument
* arg
= &*p
;
264 gold_assert(arg
->is_file());
266 Task_token
* next_blocker
= new Task_token(true);
267 next_blocker
->add_blocker();
268 workqueue
->queue_soon(new Read_symbols(this->options_
,
269 this->input_objects_
,
270 this->symtab_
, this->layout_
,
271 this->dirpath_
, arg
, input_group
,
272 this_blocker
, next_blocker
));
273 this_blocker
= next_blocker
;
276 const int saw_undefined
= this->symtab_
->saw_undefined();
277 workqueue
->queue_soon(new Finish_group(this->input_objects_
,
283 this->next_blocker_
));
286 // Return a debugging name for a Read_symbols task.
289 Read_symbols::get_name() const
291 if (!this->input_argument_
->is_group())
293 std::string
ret("Read_symbols ");
294 if (this->input_argument_
->file().is_lib())
296 ret
+= this->input_argument_
->file().name();
300 std::string
ret("Read_symbols group (");
301 bool add_space
= false;
302 const Input_file_group
* group
= this->input_argument_
->group();
303 for (Input_file_group::const_iterator p
= group
->begin();
309 ret
+= p
->file().name();
315 // Class Add_symbols.
317 Add_symbols::~Add_symbols()
319 if (this->this_blocker_
!= NULL
)
320 delete this->this_blocker_
;
321 // next_blocker_ is deleted by the task associated with the next
325 // We are blocked by this_blocker_. We block next_blocker_. We also
329 Add_symbols::is_runnable()
331 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
332 return this->this_blocker_
;
333 if (this->object_
->is_locked())
334 return this->object_
->token();
339 Add_symbols::locks(Task_locker
* tl
)
341 tl
->add(this, this->next_blocker_
);
342 tl
->add(this, this->object_
->token());
345 // Add the symbols in the object to the symbol table.
348 Add_symbols::run(Workqueue
*)
350 if (!this->input_objects_
->add_object(this->object_
))
352 // FIXME: We need to close the descriptor here.
353 delete this->object_
;
357 this->object_
->layout(this->symtab_
, this->layout_
, this->sd_
);
358 this->object_
->add_symbols(this->symtab_
, this->sd_
);
359 this->object_
->release();
365 // Class Finish_group.
367 Finish_group::~Finish_group()
369 if (this->this_blocker_
!= NULL
)
370 delete this->this_blocker_
;
371 // next_blocker_ is deleted by the task associated with the next
372 // input file following the group.
375 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
378 Finish_group::is_runnable()
380 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
381 return this->this_blocker_
;
386 Finish_group::locks(Task_locker
* tl
)
388 tl
->add(this, this->next_blocker_
);
391 // Loop over the archives until there are no new undefined symbols.
394 Finish_group::run(Workqueue
*)
396 int saw_undefined
= this->saw_undefined_
;
397 while (saw_undefined
!= this->symtab_
->saw_undefined())
399 saw_undefined
= this->symtab_
->saw_undefined();
401 for (Input_group::const_iterator p
= this->input_group_
->begin();
402 p
!= this->input_group_
->end();
405 Task_lock_obj
<Archive
> tl(this, *p
);
407 (*p
)->add_symbols(this->symtab_
, this->layout_
,
408 this->input_objects_
);
412 // Delete all the archives now that we no longer need them.
413 for (Input_group::const_iterator p
= this->input_group_
->begin();
414 p
!= this->input_group_
->end();
417 delete this->input_group_
;
422 Read_script::~Read_script()
424 if (this->this_blocker_
!= NULL
)
425 delete this->this_blocker_
;
426 // next_blocker_ is deleted by the task associated with the next
430 // We are blocked by this_blocker_.
433 Read_script::is_runnable()
435 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
436 return this->this_blocker_
;
440 // We don't unlock next_blocker_ here. If the script names any input
441 // files, then the last file will be responsible for unlocking it.
444 Read_script::locks(Task_locker
*)
448 // Read the script, if it is a script.
451 Read_script::run(Workqueue
* workqueue
)
453 bool used_next_blocker
;
454 if (!read_input_script(workqueue
, this->options_
, this->symtab_
,
455 this->layout_
, this->dirpath_
, this->input_objects_
,
456 this->input_group_
, this->input_argument_
,
457 this->input_file_
, this->next_blocker_
,
460 // Here we have to handle any other input file types we need.
461 gold_error(_("%s: not an object or archive"),
462 this->input_file_
->file().filename().c_str());
465 if (!used_next_blocker
)
467 // Queue up a task to unlock next_blocker. We can't just unlock
468 // it here, as we don't hold the workqueue lock.
469 workqueue
->queue_soon(new Unblock_token(NULL
, this->next_blocker_
));
473 // Return a debugging name for a Read_script task.
476 Read_script::get_name() const
478 std::string
ret("Read_script ");
479 if (this->input_argument_
->file().is_lib())
481 ret
+= this->input_argument_
->file().name();
485 } // End namespace gold.
This page took 0.039474 seconds and 4 git commands to generate.