]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gold/readsyms.cc
Add threading support.
[thirdparty/binutils-gdb.git] / gold / readsyms.cc
index 58e3385131b2f3bc2caf647a9fd45aa2b47ab80e..5625f59f0e6a3991f154ceae299abe0596170cf6 100644 (file)
 namespace gold
 {
 
+// If we fail to open the object, then we won't create an Add_symbols
+// task.  However, we still need to unblock the token, or else the
+// link won't proceed to generate more error messages.  We can only
+// unblock tokens in the main thread, so we need a dummy task to do
+// that.  The dummy task has to maintain the right sequence of blocks,
+// so we need both this_blocker and next_blocker.
+
+class Unblock_token : public Task
+{
+ public:
+  Unblock_token(Task_token* this_blocker, Task_token* next_blocker)
+    : this_blocker_(this_blocker), next_blocker_(next_blocker)
+  { }
+
+  ~Unblock_token()
+  {
+    if (this->this_blocker_ != NULL)
+      delete this->this_blocker_;
+  }
+
+  Is_runnable_type
+  is_runnable(Workqueue*)
+  {
+    if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
+      return IS_BLOCKED;
+    return IS_RUNNABLE;
+  }
+
+  Task_locker*
+  locks(Workqueue* workqueue)
+  { return new Task_locker_block(*this->next_blocker_, workqueue); }
+
+  void
+  run(Workqueue*)
+  { }
+
+  std::string
+  get_name() const
+  { return "Unblock_token"; }
+
+ private:
+  Task_token* this_blocker_;
+  Task_token* next_blocker_;
+};
+
 // Class read_symbols.
 
 Read_symbols::~Read_symbols()
@@ -52,7 +97,7 @@ Task::Is_runnable_type
 Read_symbols::is_runnable(Workqueue*)
 {
   if (this->input_argument_->is_file()
-      && this->input_argument_->file().is_lib()
+      && this->input_argument_->file().may_need_search()
       && this->dirpath_.token().is_blocked())
     return IS_BLOCKED;
 
@@ -68,41 +113,69 @@ Read_symbols::locks(Workqueue*)
   return NULL;
 }
 
-// Run a Read_symbols task.  This is where we actually read the
-// symbols and relocations.
+// Run a Read_symbols task.
 
 void
 Read_symbols::run(Workqueue* workqueue)
+{
+  // If we didn't queue a new task, then we need to explicitly unblock
+  // the token.
+  if (!this->do_read_symbols(workqueue))
+    workqueue->queue_front(new Unblock_token(this->this_blocker_,
+                                            this->next_blocker_));
+}
+
+// Open the file and read the symbols.  Return true if a new task was
+// queued, false if that could not happen due to some error.
+
+bool
+Read_symbols::do_read_symbols(Workqueue* workqueue)
 {
   if (this->input_argument_->is_group())
     {
       gold_assert(this->input_group_ == NULL);
       this->do_group(workqueue);
-      return;
+      return true;
     }
 
   Input_file* input_file = new Input_file(&this->input_argument_->file());
-  input_file->open(this->options_, this->dirpath_);
+  if (!input_file->open(this->options_, this->dirpath_))
+    return false;
 
   // Read enough of the file to pick up the entire ELF header.
 
-  int ehdr_size = elfcpp::Elf_sizes<64>::ehdr_size;
-  off_t bytes;
-  const unsigned char* p = input_file->file().get_view_and_size(0, ehdr_size,
-                                                               &bytes);
-  if (bytes >= 4)
+  off_t filesize = input_file->file().filesize();
+
+  if (filesize == 0)
+    {
+      gold_error(_("%s: file is empty"),
+                input_file->file().filename().c_str());
+      return false;
+    }
+
+  unsigned char ehdr_buf[elfcpp::Elf_sizes<64>::ehdr_size];
+
+  int read_size = elfcpp::Elf_sizes<64>::ehdr_size;
+  if (filesize < read_size)
+    read_size = filesize;
+
+  input_file->file().read(0, read_size, ehdr_buf);
+
+  if (read_size >= 4)
     {
       static unsigned char elfmagic[4] =
        {
          elfcpp::ELFMAG0, elfcpp::ELFMAG1,
          elfcpp::ELFMAG2, elfcpp::ELFMAG3
        };
-      if (memcmp(p, elfmagic, 4) == 0)
+      if (memcmp(ehdr_buf, elfmagic, 4) == 0)
        {
          // This is an ELF object.
 
          Object* obj = make_elf_object(input_file->filename(),
-                                       input_file, 0, p, bytes);
+                                       input_file, 0, ehdr_buf, read_size);
+         if (obj == NULL)
+           return false;
 
          // We don't have a way to record a non-archive in an input
          // group.  If this is an ordinary object file, we can't
@@ -110,10 +183,9 @@ Read_symbols::run(Workqueue* workqueue)
          // object, then including it a second time changes nothing.
          if (this->input_group_ != NULL && !obj->is_dynamic())
            {
-             fprintf(stderr,
-                     _("%s: %s: ordinary object found in input group\n"),
-                     program_name, input_file->name());
-             gold_exit(false);
+             gold_error(_("%s: ordinary object found in input group"),
+                        input_file->name());
+             return false;
            }
 
          Read_symbols_data* sd = new Read_symbols_data;
@@ -127,13 +199,13 @@ Read_symbols::run(Workqueue* workqueue)
          // Opening the file locked it, so now we need to unlock it.
          input_file->file().unlock();
 
-         return;
+         return true;
        }
     }
 
-  if (bytes >= Archive::sarmag)
+  if (read_size >= Archive::sarmag)
     {
-      if (memcmp(p, Archive::armag, Archive::sarmag) == 0)
+      if (memcmp(ehdr_buf, Archive::armag, Archive::sarmag) == 0)
        {
          // This is an archive.
          Archive* arch = new Archive(this->input_argument_->file().name(),
@@ -146,28 +218,23 @@ Read_symbols::run(Workqueue* workqueue)
                                                   this->input_group_,
                                                   this->this_blocker_,
                                                   this->next_blocker_));
-         return;
+         return true;
        }
     }
 
-  if (bytes == 0)
-    {
-      fprintf(stderr, _("%s: %s: file is empty\n"),
-             program_name, input_file->file().filename().c_str());
-      gold_exit(false);
-    }
-
   // Try to parse this file as a script.
   if (read_input_script(workqueue, this->options_, this->symtab_,
                        this->layout_, this->dirpath_, this->input_objects_,
                        this->input_group_, this->input_argument_, input_file,
-                       p, bytes, this->this_blocker_, this->next_blocker_))
-    return;
+                       ehdr_buf, read_size, this->this_blocker_,
+                       this->next_blocker_))
+    return true;
 
   // Here we have to handle any other input file types we need.
-  fprintf(stderr, _("%s: %s: not an object or archive\n"),
-         program_name, input_file->file().filename().c_str());
-  gold_exit(false);
+  gold_error(_("%s: not an object or archive"),
+            input_file->file().filename().c_str());
+
+  return false;
 }
 
 // Handle a group.  We need to walk through the arguments over and
@@ -210,6 +277,35 @@ Read_symbols::do_group(Workqueue* workqueue)
                                    this->next_blocker_));
 }
 
+// Return a debugging name for a Read_symbols task.
+
+std::string
+Read_symbols::get_name() const
+{
+  if (!this->input_argument_->is_group())
+    {
+      std::string ret("Read_symbols ");
+      if (this->input_argument_->file().is_lib())
+       ret += "-l";
+      ret += this->input_argument_->file().name();
+      return ret;
+    }
+
+  std::string ret("Read_symbols group (");
+  bool add_space = false;
+  const Input_file_group* group = this->input_argument_->group();
+  for (Input_file_group::const_iterator p = group->begin();
+       p != group->end();
+       ++p)
+    {
+      if (add_space)
+       ret += ' ';
+      ret += p->file().name();
+      add_space = true;
+    }
+  return ret + ')';
+}
+
 // Class Add_symbols.
 
 Add_symbols::~Add_symbols()