* object.cc (is_elf_object): Define.
[external/binutils.git] / gold / readsyms.cc
1 // readsyms.cc -- read input file symbols for gold
2
3 // Copyright 2006, 2007, 2008, 2009 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 #include "plugin.h"
36
37 namespace gold
38 {
39
40 // If we fail to open the object, then we won't create an Add_symbols
41 // task.  However, we still need to unblock the token, or else the
42 // link won't proceed to generate more error messages.  We can only
43 // unblock tokens when the workqueue lock is held, so we need a dummy
44 // task to do that.  The dummy task has to maintain the right sequence
45 // of blocks, so we need both this_blocker and next_blocker.
46
47 class Unblock_token : public Task
48 {
49  public:
50   Unblock_token(Task_token* this_blocker, Task_token* next_blocker)
51     : this_blocker_(this_blocker), next_blocker_(next_blocker)
52   { }
53
54   ~Unblock_token()
55   {
56     if (this->this_blocker_ != NULL)
57       delete this->this_blocker_;
58   }
59
60   Task_token*
61   is_runnable()
62   {
63     if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
64       return this->this_blocker_;
65     return NULL;
66   }
67
68   void
69   locks(Task_locker* tl)
70   { tl->add(this, this->next_blocker_); }
71
72   void
73   run(Workqueue*)
74   { }
75
76   std::string
77   get_name() const
78   { return "Unblock_token"; }
79
80  private:
81   Task_token* this_blocker_;
82   Task_token* next_blocker_;
83 };
84
85 // Class read_symbols.
86
87 Read_symbols::~Read_symbols()
88 {
89   // The this_blocker_ and next_blocker_ pointers are passed on to the
90   // Add_symbols task.
91 }
92
93 // If appropriate, issue a warning about skipping an incompatible
94 // file.
95
96 void
97 Read_symbols::incompatible_warning(const Input_argument* input_argument,
98                                    const Input_file* input_file)
99 {
100   if (parameters->options().warn_search_mismatch())
101     gold_warning("skipping incompatible %s while searching for %s",
102                  input_file->filename().c_str(),
103                  input_argument->file().name());
104 }
105
106 // Requeue a Read_symbols task to search for the next object with the
107 // same name.
108
109 void
110 Read_symbols::requeue(Workqueue* workqueue, Input_objects* input_objects,
111                       Symbol_table* symtab, Layout* layout, Dirsearch* dirpath,
112                       int dirindex, Mapfile* mapfile,
113                       const Input_argument* input_argument,
114                       Input_group* input_group, Task_token* next_blocker)
115 {
116   // Bump the directory search index.
117   ++dirindex;
118
119   // We don't need to worry about this_blocker, since we already
120   // reached it.  However, we are removing the blocker on next_blocker
121   // because the calling task is completing.  So we need to add a new
122   // blocker.  Since next_blocker may be shared by several tasks, we
123   // need to increment the count with the workqueue lock held.
124   workqueue->add_blocker(next_blocker);
125
126   workqueue->queue(new Read_symbols(input_objects, symtab, layout, dirpath,
127                                     dirindex, mapfile, input_argument,
128                                     input_group, NULL, next_blocker));
129 }
130
131 // Return whether a Read_symbols task is runnable.  We can read an
132 // ordinary input file immediately.  For an archive specified using
133 // -l, we have to wait until the search path is complete.
134
135 Task_token*
136 Read_symbols::is_runnable()
137 {
138   if (this->input_argument_->is_file()
139       && this->input_argument_->file().may_need_search()
140       && this->dirpath_->token()->is_blocked())
141     return this->dirpath_->token();
142
143   return NULL;
144 }
145
146 // Return a Task_locker for a Read_symbols task.  We don't need any
147 // locks here.
148
149 void
150 Read_symbols::locks(Task_locker*)
151 {
152 }
153
154 // Run a Read_symbols task.
155
156 void
157 Read_symbols::run(Workqueue* workqueue)
158 {
159   // If we didn't queue a new task, then we need to explicitly unblock
160   // the token.
161   if (!this->do_read_symbols(workqueue))
162     workqueue->queue_soon(new Unblock_token(this->this_blocker_,
163                                             this->next_blocker_));
164 }
165
166 // Open the file and read the symbols.  Return true if a new task was
167 // queued, false if that could not happen due to some error.
168
169 bool
170 Read_symbols::do_read_symbols(Workqueue* workqueue)
171 {
172   if (this->input_argument_->is_group())
173     {
174       gold_assert(this->input_group_ == NULL);
175       this->do_group(workqueue);
176       return true;
177     }
178
179   Input_file* input_file = new Input_file(&this->input_argument_->file());
180   if (!input_file->open(*this->dirpath_, this, &this->dirindex_))
181     return false;
182
183   // Read enough of the file to pick up the entire ELF header.
184
185   off_t filesize = input_file->file().filesize();
186
187   if (filesize == 0)
188     {
189       gold_error(_("%s: file is empty"),
190                  input_file->file().filename().c_str());
191       return false;
192     }
193
194   const unsigned char* ehdr;
195   int read_size;
196   bool is_elf = is_elf_object(input_file, 0, &ehdr, &read_size);
197
198   if (read_size >= Archive::sarmag)
199     {
200       bool is_thin_archive
201           = memcmp(ehdr, Archive::armagt, Archive::sarmag) == 0;
202       if (is_thin_archive 
203           || memcmp(ehdr, Archive::armag, Archive::sarmag) == 0)
204         {
205           // This is an archive.
206           Archive* arch = new Archive(this->input_argument_->file().name(),
207                                       input_file, is_thin_archive,
208                                       this->dirpath_, this);
209           arch->setup();
210           
211           // Unlock the archive so it can be used in the next task.
212           arch->unlock(this);
213
214           workqueue->queue_next(new Add_archive_symbols(this->symtab_,
215                                                         this->layout_,
216                                                         this->input_objects_,
217                                                         this->dirpath_,
218                                                         this->dirindex_,
219                                                         this->mapfile_,
220                                                         this->input_argument_,
221                                                         arch,
222                                                         this->input_group_,
223                                                         this->this_blocker_,
224                                                         this->next_blocker_));
225           return true;
226         }
227     }
228
229   if (parameters->options().has_plugins())
230     {
231       Pluginobj* obj = parameters->options().plugins()->claim_file(input_file,
232                                                                    0, filesize);
233       if (obj != NULL)
234         {
235           // The input file was claimed by a plugin, and its symbols
236           // have been provided by the plugin.
237
238           // We are done with the file at this point, so unlock it.
239           obj->unlock(this);
240
241           workqueue->queue_next(new Add_symbols(this->input_objects_,
242                                                 this->symtab_,
243                                                 this->layout_,
244                                                 this->dirpath_,
245                                                 this->dirindex_,
246                                                 this->mapfile_,
247                                                 this->input_argument_,
248                                                 this->input_group_,
249                                                 obj,
250                                                 NULL,
251                                                 this->this_blocker_,
252                                                 this->next_blocker_));
253           return true;
254         }
255     }
256
257   if (is_elf)
258     {
259       // This is an ELF object.
260
261       bool unconfigured;
262       Object* obj = make_elf_object(input_file->filename(),
263                                     input_file, 0, ehdr, read_size,
264                                     &unconfigured);
265       if (obj == NULL)
266         {
267           if (unconfigured && input_file->will_search_for())
268             {
269               Read_symbols::incompatible_warning(this->input_argument_,
270                                                  input_file);
271               input_file->file().release();
272               input_file->file().unlock(this);
273               delete input_file;
274               ++this->dirindex_;
275               return this->do_read_symbols(workqueue);
276             }
277           return false;
278         }
279
280       Read_symbols_data* sd = new Read_symbols_data;
281       obj->read_symbols(sd);
282
283       // Opening the file locked it, so now we need to unlock it.  We
284       // need to unlock it before queuing the Add_symbols task,
285       // because the workqueue doesn't know about our lock on the
286       // file.  If we queue the Add_symbols task first, it will be
287       // stuck on the end of the file lock, but since the workqueue
288       // doesn't know about that lock, it will never release the
289       // Add_symbols task.
290
291       input_file->file().unlock(this);
292
293       // We use queue_next because everything is cached for this
294       // task to run right away if possible.
295
296       workqueue->queue_next(new Add_symbols(this->input_objects_,
297                                             this->symtab_, this->layout_,
298                                             this->dirpath_,
299                                             this->dirindex_,
300                                             this->mapfile_,
301                                             this->input_argument_,
302                                             this->input_group_,
303                                             obj,
304                                             sd,
305                                             this->this_blocker_,
306                                             this->next_blocker_));
307
308       return true;
309     }
310
311   // Queue up a task to try to parse this file as a script.  We use a
312   // separate task so that the script will be read in order with other
313   // objects named on the command line.  Also so that we don't try to
314   // read multiple scripts simultaneously, which could lead to
315   // unpredictable changes to the General_options structure.
316
317   workqueue->queue_soon(new Read_script(this->symtab_,
318                                         this->layout_,
319                                         this->dirpath_,
320                                         this->dirindex_,
321                                         this->input_objects_,
322                                         this->mapfile_,
323                                         this->input_group_,
324                                         this->input_argument_,
325                                         input_file,
326                                         this->this_blocker_,
327                                         this->next_blocker_));
328   return true;
329 }
330
331 // Handle a group.  We need to walk through the arguments over and
332 // over until we don't see any new undefined symbols.  We do this by
333 // setting off Read_symbols Tasks as usual, but recording the archive
334 // entries instead of deleting them.  We also start a Finish_group
335 // Task which runs after we've read all the symbols.  In that task we
336 // process the archives in a loop until we are done.
337
338 void
339 Read_symbols::do_group(Workqueue* workqueue)
340 {
341   Input_group* input_group = new Input_group();
342
343   const Input_file_group* group = this->input_argument_->group();
344   Task_token* this_blocker = this->this_blocker_;
345
346   for (Input_file_group::const_iterator p = group->begin();
347        p != group->end();
348        ++p)
349     {
350       const Input_argument* arg = &*p;
351       gold_assert(arg->is_file());
352
353       Task_token* next_blocker = new Task_token(true);
354       next_blocker->add_blocker();
355       workqueue->queue_soon(new Read_symbols(this->input_objects_,
356                                              this->symtab_, this->layout_,
357                                              this->dirpath_, this->dirindex_,
358                                              this->mapfile_, arg, input_group,
359                                              this_blocker, next_blocker));
360       this_blocker = next_blocker;
361     }
362
363   const int saw_undefined = this->symtab_->saw_undefined();
364   workqueue->queue_soon(new Finish_group(this->input_objects_,
365                                          this->symtab_,
366                                          this->layout_,
367                                          this->mapfile_,
368                                          input_group,
369                                          saw_undefined,
370                                          this_blocker,
371                                          this->next_blocker_));
372 }
373
374 // Return a debugging name for a Read_symbols task.
375
376 std::string
377 Read_symbols::get_name() const
378 {
379   if (!this->input_argument_->is_group())
380     {
381       std::string ret("Read_symbols ");
382       if (this->input_argument_->file().is_lib())
383         ret += "-l";
384       ret += this->input_argument_->file().name();
385       return ret;
386     }
387
388   std::string ret("Read_symbols group (");
389   bool add_space = false;
390   const Input_file_group* group = this->input_argument_->group();
391   for (Input_file_group::const_iterator p = group->begin();
392        p != group->end();
393        ++p)
394     {
395       if (add_space)
396         ret += ' ';
397       ret += p->file().name();
398       add_space = true;
399     }
400   return ret + ')';
401 }
402
403 // Class Add_symbols.
404
405 Add_symbols::~Add_symbols()
406 {
407   if (this->this_blocker_ != NULL)
408     delete this->this_blocker_;
409   // next_blocker_ is deleted by the task associated with the next
410   // input file.
411 }
412
413 // We are blocked by this_blocker_.  We block next_blocker_.  We also
414 // lock the file.
415
416 Task_token*
417 Add_symbols::is_runnable()
418 {
419   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
420     return this->this_blocker_;
421   if (this->object_->is_locked())
422     return this->object_->token();
423   return NULL;
424 }
425
426 void
427 Add_symbols::locks(Task_locker* tl)
428 {
429   tl->add(this, this->next_blocker_);
430   tl->add(this, this->object_->token());
431 }
432
433 // Add the symbols in the object to the symbol table.
434
435 void
436 Add_symbols::run(Workqueue* workqueue)
437 {
438   Pluginobj* pluginobj = this->object_->pluginobj();
439   if (pluginobj != NULL)
440     {
441       this->object_->add_symbols(this->symtab_, this->sd_, this->layout_);
442       return;
443     }
444
445   // If this file has an incompatible format, try for another file
446   // with the same name.
447   if (this->object_->searched_for()
448       && !parameters->is_compatible_target(this->object_->target()))
449     {
450       Read_symbols::incompatible_warning(this->input_argument_,
451                                          this->object_->input_file());
452       Read_symbols::requeue(workqueue, this->input_objects_, this->symtab_,
453                             this->layout_, this->dirpath_, this->dirindex_,
454                             this->mapfile_, this->input_argument_,
455                             this->input_group_, this->next_blocker_);
456       this->object_->release();
457       delete this->object_;
458     }
459   else if (!this->input_objects_->add_object(this->object_))
460     {
461       this->object_->release();
462       delete this->object_;
463     }
464   else
465     {
466       this->object_->layout(this->symtab_, this->layout_, this->sd_);
467       this->object_->add_symbols(this->symtab_, this->sd_, this->layout_);
468       this->object_->release();
469     }
470   delete this->sd_;
471   this->sd_ = NULL;
472 }
473
474 // Class Finish_group.
475
476 Finish_group::~Finish_group()
477 {
478   if (this->this_blocker_ != NULL)
479     delete this->this_blocker_;
480   // next_blocker_ is deleted by the task associated with the next
481   // input file following the group.
482 }
483
484 // We need to wait for THIS_BLOCKER_ and unblock NEXT_BLOCKER_.
485
486 Task_token*
487 Finish_group::is_runnable()
488 {
489   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
490     return this->this_blocker_;
491   return NULL;
492 }
493
494 void
495 Finish_group::locks(Task_locker* tl)
496 {
497   tl->add(this, this->next_blocker_);
498 }
499
500 // Loop over the archives until there are no new undefined symbols.
501
502 void
503 Finish_group::run(Workqueue*)
504 {
505   int saw_undefined = this->saw_undefined_;
506   while (saw_undefined != this->symtab_->saw_undefined())
507     {
508       saw_undefined = this->symtab_->saw_undefined();
509
510       for (Input_group::const_iterator p = this->input_group_->begin();
511            p != this->input_group_->end();
512            ++p)
513         {
514           Task_lock_obj<Archive> tl(this, *p);
515
516           (*p)->add_symbols(this->symtab_, this->layout_,
517                             this->input_objects_, this->mapfile_);
518         }
519     }
520
521   // Delete all the archives now that we no longer need them.
522   for (Input_group::const_iterator p = this->input_group_->begin();
523        p != this->input_group_->end();
524        ++p)
525     delete *p;
526   delete this->input_group_;
527 }
528
529 // Class Read_script
530
531 Read_script::~Read_script()
532 {
533   if (this->this_blocker_ != NULL)
534     delete this->this_blocker_;
535   // next_blocker_ is deleted by the task associated with the next
536   // input file.
537 }
538
539 // We are blocked by this_blocker_.
540
541 Task_token*
542 Read_script::is_runnable()
543 {
544   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
545     return this->this_blocker_;
546   return NULL;
547 }
548
549 // We don't unlock next_blocker_ here.  If the script names any input
550 // files, then the last file will be responsible for unlocking it.
551
552 void
553 Read_script::locks(Task_locker*)
554 {
555 }
556
557 // Read the script, if it is a script.
558
559 void
560 Read_script::run(Workqueue* workqueue)
561 {
562   bool used_next_blocker;
563   if (!read_input_script(workqueue, this->symtab_, this->layout_,
564                          this->dirpath_, this->dirindex_, this->input_objects_,
565                          this->mapfile_, this->input_group_,
566                          this->input_argument_, this->input_file_,
567                          this->next_blocker_, &used_next_blocker))
568     {
569       // Here we have to handle any other input file types we need.
570       gold_error(_("%s: not an object or archive"),
571                  this->input_file_->file().filename().c_str());
572     }
573
574   if (!used_next_blocker)
575     {
576       // Queue up a task to unlock next_blocker.  We can't just unlock
577       // it here, as we don't hold the workqueue lock.
578       workqueue->queue_soon(new Unblock_token(NULL, this->next_blocker_));
579     }
580 }
581
582 // Return a debugging name for a Read_script task.
583
584 std::string
585 Read_script::get_name() const
586 {
587   std::string ret("Read_script ");
588   if (this->input_argument_->file().is_lib())
589     ret += "-l";
590   ret += this->input_argument_->file().name();
591   return ret;
592 }
593
594 } // End namespace gold.