This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [gold][patch] Small code refactoring


2009/2/12 Rafael Espindola <espindola@google.com>:
>> Patch?
The correct one now.


Cheers,
-- 
Rafael Avila de Espindola

Google | Gordon House | Barrow Street | Dublin 4 | Ireland
Registered in Dublin, Ireland | Registration Number: 368047
diff --git a/gold/archive.cc b/gold/archive.cc
index b1ba6d9..495ed78 100644
--- a/gold/archive.cc
+++ b/gold/archive.cc
@@ -539,36 +539,15 @@ Archive::get_elf_object_for_member(off_t off, Input_objects* input_objects)
         }
     }
 
-  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;
+  Object* o = make_elf_object((std::string(this->input_file_->filename())
+                               + "(" + member_name + ")"),
+                              input_file, 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)
-    {
-      gold_error(_("%s: member at %zu is not an ELF object"),
-		 this->name().c_str(), static_cast<size_t>(off));
-      return NULL;
-    }
+  if (!o)
+    gold_error(_("%s: member at %zu is not an ELF object"),
+               this->name().c_str(), static_cast<size_t>(off));
 
-  return make_elf_object((std::string(this->input_file_->filename())
-				 + "(" + member_name + ")"),
-				input_file, memoff, ehdr, read_size);
+  return o;
 }
 
 // Read the symbols from all the archive members in the link.
diff --git a/gold/object.cc b/gold/object.cc
index a05dcfc..78c1d06 100644
--- a/gold/object.cc
+++ b/gold/object.cc
@@ -2112,11 +2112,11 @@ make_elf_sized_object(const std::string& name, Input_file* input_file,
 namespace gold
 {
 
-// Read an ELF file and return the appropriate instance of Object.
+// Helper of make_elf_object
 
-Object*
-make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
-		const unsigned char* p, section_offset_type bytes)
+static Object*
+make_elf_object2(const std::string& name, Input_file* input_file, off_t offset,
+                 const unsigned char* p, section_offset_type bytes)
 {
   if (bytes < elfcpp::EI_NIDENT)
     {
@@ -2232,6 +2232,34 @@ make_elf_object(const std::string& name, Input_file* input_file, off_t offset,
     }
 }
 
+// Read an ELF file and return the appropriate instance of Object.
+Object*
+make_elf_object(const std::string& name, Input_file* input_file, off_t offset)
+{
+  off_t filesize = input_file->file().filesize();
+  int read_size = elfcpp::Elf_sizes<64>::ehdr_size;
+
+  if (filesize - offset < read_size)
+    read_size = filesize - offset;
+
+  if (read_size < 4)
+    return NULL;
+
+  const unsigned char* ehdr = input_file->file().get_view(offset, 0, read_size,
+                                                          true, false);
+
+  static unsigned char elfmagic[4] =
+      {
+        elfcpp::ELFMAG0, elfcpp::ELFMAG1,
+        elfcpp::ELFMAG2, elfcpp::ELFMAG3
+      };
+
+  if (memcmp(ehdr, elfmagic, 4) != 0)
+    return NULL;
+
+  return make_elf_object2(name, input_file, offset, ehdr, read_size);
+}
+
 // Instantiate the templates we need.
 
 #ifdef HAVE_TARGET_32_LITTLE
diff --git a/gold/object.h b/gold/object.h
index 614a02e..822f6ea 100644
--- a/gold/object.h
+++ b/gold/object.h
@@ -1942,14 +1942,10 @@ struct Relocate_info
   location(size_t relnum, off_t reloffset) const;
 };
 
-// Return an Object appropriate for the input file.  P is BYTES long,
-// and holds the ELF header.
+// Return an Object appropriate for the input file.
 
 extern Object*
-make_elf_object(const std::string& name, Input_file*,
-		off_t offset, const unsigned char* p,
-		section_offset_type bytes);
-
+make_elf_object(const std::string& name, Input_file*, off_t offset);
 } // end namespace gold
 
 #endif // !defined(GOLD_OBJECT_H)
diff --git a/gold/readsyms.cc b/gold/readsyms.cc
index 412ffcd..c19dfc4 100644
--- a/gold/readsyms.cc
+++ b/gold/readsyms.cc
@@ -209,46 +209,34 @@ Read_symbols::do_read_symbols(Workqueue* workqueue)
         }
     }
 
-  if (read_size >= 4)
+  Object* obj = make_elf_object(input_file->filename(), input_file, 0);
+  if (obj)
     {
-      static unsigned char elfmagic[4] =
-	{
-	  elfcpp::ELFMAG0, elfcpp::ELFMAG1,
-	  elfcpp::ELFMAG2, elfcpp::ELFMAG3
-	};
-      if (memcmp(ehdr, elfmagic, 4) == 0)
-	{
-	  // This is an ELF object.
-
-	  Object* obj = make_elf_object(input_file->filename(),
-					input_file, 0, ehdr, read_size);
-	  if (obj == NULL)
-	    return false;
+      // This is an ELF object.
 
-	  Read_symbols_data* sd = new Read_symbols_data;
-	  obj->read_symbols(sd);
+      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.
+      // 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);
+      input_file->file().unlock(this);
 
-	  // We use queue_next because everything is cached for this
-	  // task to run right away if possible.
+      // 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_,
-						obj, sd,
-						this->this_blocker_,
-						this->next_blocker_));
+      workqueue->queue_next(new Add_symbols(this->input_objects_,
+                                            this->symtab_, this->layout_,
+                                            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
diff --git a/gold/testsuite/binary_unittest.cc b/gold/testsuite/binary_unittest.cc
index 44db01e..f5a0f39 100644
--- a/gold/testsuite/binary_unittest.cc
+++ b/gold/testsuite/binary_unittest.cc
@@ -65,9 +65,7 @@ Sized_binary_test(Target* target)
 
   Input_file input_file(task, "test.o", binary.converted_data(),
 			binary.converted_size());
-  Object* object = make_elf_object("test.o", &input_file, 0,
-				   binary.converted_data(),
-				   binary.converted_size());
+  Object* object = make_elf_object("test.o", &input_file, 0);
   CHECK(object != NULL);
   if (object == NULL)
     return false;
diff --git a/gold/testsuite/object_unittest.cc b/gold/testsuite/object_unittest.cc
index 93d4636..be49bd9 100644
--- a/gold/testsuite/object_unittest.cc
+++ b/gold/testsuite/object_unittest.cc
@@ -42,8 +42,7 @@ Sized_object_test(const unsigned char* test_file, unsigned int test_file_size,
   // We need a pretend Task.
   const Task* task = reinterpret_cast<const Task*>(-1);
   Input_file input_file(task, "test.o", test_file, test_file_size);
-  Object* object = make_elf_object("test.o", &input_file, 0,
-				   test_file, test_file_size);
+  Object* object = make_elf_object("test.o", &input_file, 0);
   CHECK(object->name() == "test.o");
   CHECK(!object->is_dynamic());
   CHECK(object->target() == target_test_pointer);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]