* object.cc (is_elf_object): Define.
authorIan Lance Taylor <ian@airs.com>
Tue, 24 Mar 2009 18:42:10 +0000 (18:42 +0000)
committerIan Lance Taylor <ian@airs.com>
Tue, 24 Mar 2009 18:42:10 +0000 (18:42 +0000)
* object.h (is_elf_object): Declare.
* archive.cc (Archive::get_elf_object_for_member): Call
is_elf_object.
* readsymc.cc (Read_symbols::do_read_symbols): Likewise.

gold/ChangeLog
gold/archive.cc
gold/object.cc
gold/object.h
gold/readsyms.cc

index d233ab560b0e399f101e588b4adfaf0abe027836..d380aa1657f1d274df77f628b62350a90bf9b2ec 100644 (file)
@@ -1,3 +1,11 @@
+2009-03-24  Rafael Avila de Espindola  <espindola@google.com>
+
+       * object.cc (is_elf_object): Define.
+       * object.h (is_elf_object): Declare.
+       * archive.cc (Archive::get_elf_object_for_member): Call
+       is_elf_object.
+       * readsymc.cc (Read_symbols::do_read_symbols): Likewise.
+
 2009-03-24  Elliott Hughes  <enh@google.com>
 
        * output.cc (Output_file::map_anonymous): Define.
index 1d83d6a2f79e85c364bc2828470552eb59da53a6..2dec571e13ace7ace2ad28b3164c21b05739262f 100644 (file)
@@ -540,27 +540,9 @@ Archive::get_elf_object_for_member(off_t off, bool* punconfigured)
         }
     }
 
-  off_t filesize = input_file->file().filesize();
-  int read_size = elfcpp::Elf_sizes<64>::ehdr_size;
-  if (filesize - memoff < read_size)
-    read_size = filesize - memoff;
-
-  if (read_size < 4)
-    {
-      gold_error(_("%s: member at %zu is not an ELF object"),
-                this->name().c_str(), static_cast<size_t>(off));
-      return NULL;
-    }
-
-  const unsigned char* ehdr = input_file->file().get_view(memoff, 0, read_size,
-                                                         true, false);
-
-  static unsigned char elfmagic[4] =
-    {
-      elfcpp::ELFMAG0, elfcpp::ELFMAG1,
-      elfcpp::ELFMAG2, elfcpp::ELFMAG3
-    };
-  if (memcmp(ehdr, elfmagic, 4) != 0)
+  const unsigned char* ehdr;
+  int read_size;
+  if (!is_elf_object(input_file, memoff, &ehdr, &read_size))
     {
       gold_error(_("%s: member at %zu is not an ELF object"),
                 this->name().c_str(), static_cast<size_t>(off));
index d1e16727032d83f6f69a6bcd5bed30b676ba20b9..9e61831e199af258285741adf24c9a027d3dc3b8 100644 (file)
@@ -2100,6 +2100,33 @@ make_elf_sized_object(const std::string& name, Input_file* input_file,
 namespace gold
 {
 
+// Return whether INPUT_FILE is an ELF object.
+
+bool
+is_elf_object(Input_file* input_file, off_t offset,
+             const unsigned char** start, int *read_size)
+{
+  off_t filesize = input_file->file().filesize();
+  int want = elfcpp::Elf_sizes<64>::ehdr_size;
+  if (filesize - offset < want)
+    want = filesize - offset;
+
+  const unsigned char* p = input_file->file().get_view(offset, 0, want,
+                                                      true, false);
+  *start = p;
+  *read_size = want;
+
+  if (want < 4)
+    return false;
+
+  static unsigned char elfmagic[4] =
+    {
+      elfcpp::ELFMAG0, elfcpp::ELFMAG1,
+      elfcpp::ELFMAG2, elfcpp::ELFMAG3
+    };
+  return memcmp(p, elfmagic, 4) == 0;
+}
+
 // Read an ELF file and return the appropriate instance of Object.
 
 Object*
index 53d19444cf993d34c19280caa60ae910b9dcd9bf..97c126d52b1ffc2aa0e4a9b2dfc761ff523b1e1e 100644 (file)
@@ -1921,6 +1921,14 @@ struct Relocate_info
   location(size_t relnum, off_t reloffset) const;
 };
 
+// Return whether INPUT_FILE contains an ELF object start at file
+// offset OFFSET.  This sets *START to point to a view of the start of
+// the file.  It sets *READ_SIZE to the number of bytes in the view.
+
+extern bool
+is_elf_object(Input_file* input_file, off_t offset,
+             const unsigned char** start, int *read_size);
+
 // Return an Object appropriate for the input file.  P is BYTES long,
 // and holds the ELF header.  If PUNCONFIGURED is not NULL, then if
 // this sees an object the linker is not configured to support, it
index dc85898c0b2d02862459a2a3a56d8a1a0eea1374..b6da88d45ce4f5ddd9bf13f5d766040c2e86bd5c 100644 (file)
@@ -191,12 +191,9 @@ Read_symbols::do_read_symbols(Workqueue* workqueue)
       return false;
     }
 
-  int read_size = elfcpp::Elf_sizes<64>::ehdr_size;
-  if (filesize < read_size)
-    read_size = filesize;
-
-  const unsigned char* ehdr = input_file->file().get_view(0, 0, read_size,
-                                                         true, false);
+  const unsigned char* ehdr;
+  int read_size;
+  bool is_elf = is_elf_object(input_file, 0, &ehdr, &read_size);
 
   if (read_size >= Archive::sarmag)
     {
@@ -257,66 +254,58 @@ Read_symbols::do_read_symbols(Workqueue* workqueue)
         }
     }
 
-  if (read_size >= 4)
+  if (is_elf)
     {
-      static unsigned char elfmagic[4] =
-       {
-         elfcpp::ELFMAG0, elfcpp::ELFMAG1,
-         elfcpp::ELFMAG2, elfcpp::ELFMAG3
-       };
-      if (memcmp(ehdr, elfmagic, 4) == 0)
-       {
-         // This is an ELF object.
+      // This is an ELF object.
 
-         bool unconfigured;
-         Object* obj = make_elf_object(input_file->filename(),
-                                       input_file, 0, ehdr, read_size,
-                                       &unconfigured);
-         if (obj == NULL)
+      bool unconfigured;
+      Object* obj = make_elf_object(input_file->filename(),
+                                   input_file, 0, ehdr, read_size,
+                                   &unconfigured);
+      if (obj == NULL)
+       {
+         if (unconfigured && input_file->will_search_for())
            {
-             if (unconfigured && input_file->will_search_for())
-               {
-                 Read_symbols::incompatible_warning(this->input_argument_,
-                                                    input_file);
-                 input_file->file().release();
-                 input_file->file().unlock(this);
-                 delete input_file;
-                 ++this->dirindex_;
-                 return this->do_read_symbols(workqueue);
-               }
-             return false;
+             Read_symbols::incompatible_warning(this->input_argument_,
+                                                input_file);
+             input_file->file().release();
+             input_file->file().unlock(this);
+             delete input_file;
+             ++this->dirindex_;
+             return this->do_read_symbols(workqueue);
            }
+         return false;
+       }
 
-         Read_symbols_data* sd = new Read_symbols_data;
-         obj->read_symbols(sd);
-
-         // Opening the file locked it, so now we need to unlock it.
-         // We need to unlock it before queuing the Add_symbols task,
-         // because the workqueue doesn't know about our lock on the
-         // file.  If we queue the Add_symbols task first, it will be
-         // stuck on the end of the file lock, but since the
-         // workqueue doesn't know about that lock, it will never
-         // release the Add_symbols task.
-
-         input_file->file().unlock(this);
-
-         // We use queue_next because everything is cached for this
-         // task to run right away if possible.
-
-         workqueue->queue_next(new Add_symbols(this->input_objects_,
-                                               this->symtab_, this->layout_,
-                                               this->dirpath_,
-                                               this->dirindex_,
-                                               this->mapfile_,
-                                               this->input_argument_,
-                                               this->input_group_,
-                                               obj,
-                                               sd,
-                                               this->this_blocker_,
-                                               this->next_blocker_));
+      Read_symbols_data* sd = new Read_symbols_data;
+      obj->read_symbols(sd);
+
+      // Opening the file locked it, so now we need to unlock it.  We
+      // need to unlock it before queuing the Add_symbols task,
+      // because the workqueue doesn't know about our lock on the
+      // file.  If we queue the Add_symbols task first, it will be
+      // stuck on the end of the file lock, but since the workqueue
+      // doesn't know about that lock, it will never release the
+      // Add_symbols task.
+
+      input_file->file().unlock(this);
+
+      // We use queue_next because everything is cached for this
+      // task to run right away if possible.
+
+      workqueue->queue_next(new Add_symbols(this->input_objects_,
+                                           this->symtab_, this->layout_,
+                                           this->dirpath_,
+                                           this->dirindex_,
+                                           this->mapfile_,
+                                           this->input_argument_,
+                                           this->input_group_,
+                                           obj,
+                                           sd,
+                                           this->this_blocker_,
+                                           this->next_blocker_));
 
-         return true;
-       }
+      return true;
     }
 
   // Queue up a task to try to parse this file as a script.  We use a