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]

[PATCH] [BFD] Add support for reading msdos (MZ) executables.


Allows them to be read with objdump. I ran `make test` and found no
regressions.

	bfd/
	* i386msdos.c: Add msdos_object_p().

Signed-off-by: Zebediah Figura <z.figura12@gmail.com>
---
This is my first time contributing to this project, so patience would be
appreciated.

 bfd/i386msdos.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 93 insertions(+), 2 deletions(-)

diff --git a/bfd/i386msdos.c b/bfd/i386msdos.c
index 3a272e9..be0fa9b 100644
--- a/bfd/i386msdos.c
+++ b/bfd/i386msdos.c
@@ -33,6 +33,98 @@
 #define EXE_LOAD_LOW	0xffff
 #define EXE_PAGE_SIZE	512
 
+struct external_DOS_hdr
+{
+  /* DOS header fields - always at offset zero in the EXE file.  */
+  char e_magic[2];		/* Magic number, 0x5a4d.  */
+  char e_cblp[2];		/* Bytes on last page of file, 0x90.  */
+  char e_cp[2];			/* Pages in file, 0x3.  */
+  char e_crlc[2];		/* Relocations, 0x0.  */
+  char e_cparhdr[2];		/* Size of header in paragraphs, 0x4.  */
+  char e_minalloc[2];		/* Minimum extra paragraphs needed, 0x0.  */
+  char e_maxalloc[2];		/* Maximum extra paragraphs needed, 0xFFFF.  */
+  char e_ss[2];			/* Initial (relative) SS value, 0x0.  */
+  char e_sp[2];			/* Initial SP value, 0xb8.  */
+  char e_csum[2];		/* Checksum, 0x0.  */
+  char e_ip[2];			/* Initial IP value, 0x0.  */
+  char e_cs[2];			/* Initial (relative) CS value, 0x0.  */
+  char e_lfarlc[2];		/* File address of relocation table, 0x40.  */
+  char e_ovno[2];		/* Overlay number, 0x0.  */
+  char e_res[4][2];		/* Reserved words, all 0x0.  */
+  char e_oemid[2];		/* OEM identifier (for e_oeminfo), 0x0.  */
+  char e_oeminfo[2];		/* OEM information; e_oemid specific, 0x0.  */
+  char e_res2[10][2];		/* Reserved words, all 0x0.  */
+  char e_lfanew[4];		/* File address of new exe header, usually 0x80.  */
+};
+
+static bfd_boolean
+msdos_mkobject (bfd *abfd)
+{
+  bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i8086);
+
+  return aout_32_mkobject (abfd);
+}
+
+static const bfd_target *
+msdos_object_p (bfd *abfd)
+{
+  struct external_DOS_hdr hdr;
+  bfd_byte buffer[2];
+  asection *section;
+  unsigned int size;
+
+  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
+      || bfd_bread (&hdr, (bfd_size_type) sizeof (hdr), abfd) != sizeof(hdr))
+    {
+      if (bfd_get_error () != bfd_error_system_call)
+	bfd_set_error (bfd_error_wrong_format);
+      return NULL;
+    }
+
+  if (H_GET_16 (abfd, hdr.e_magic) != EXE_MAGIC)
+    {
+      bfd_set_error (bfd_error_wrong_format);
+      return NULL;
+    }
+
+  /* Check that this isn't actually a PE, NE, or LE file.  */
+  if (bfd_seek (abfd, (file_ptr) H_GET_32 (abfd, hdr.e_lfanew), SEEK_SET) != 0
+      || bfd_bread (buffer, (bfd_size_type) 2, abfd) != 2)
+    {
+      if (bfd_get_error () == bfd_error_system_call)
+	return NULL;
+    }
+  else
+    {
+      if (H_GET_16 (abfd, buffer) == 0x4550	/* PE */
+	  || H_GET_16 (abfd, buffer) == 0x454e	/* NE */
+	  || H_GET_16 (abfd, buffer) == 0x454c)	/* LE */
+	{
+	  bfd_set_error (bfd_error_wrong_format);
+	  return NULL;
+	}
+    }
+
+  if (!msdos_mkobject (abfd))
+    return NULL;
+
+  abfd->flags = EXEC_P;
+  abfd->start_address = H_GET_16 (abfd, hdr.e_ip);
+
+  section = bfd_make_section (abfd, ".text");
+  if (section == NULL)
+    return NULL;
+
+  section->flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS);
+  section->filepos = H_GET_16 (abfd, hdr.e_cparhdr) * 16;
+  size = (H_GET_16 (abfd, hdr.e_cp) - 1) * EXE_PAGE_SIZE - section->filepos;
+  size += H_GET_16 (abfd, hdr.e_cblp);
+  bfd_set_section_size (abfd, section, size);
+  section->alignment_power = 4;
+
+  return abfd->xvec;
+}
+
 static int
 msdos_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
 		      struct bfd_link_info *info ATTRIBUTE_UNUSED)
@@ -127,7 +219,6 @@ msdos_set_section_contents (bfd *abfd,
 
 
 
-#define msdos_mkobject aout_32_mkobject
 #define msdos_make_empty_symbol aout_32_make_empty_symbol
 #define msdos_bfd_reloc_type_lookup aout_32_reloc_type_lookup
 #define msdos_bfd_reloc_name_lookup aout_32_reloc_name_lookup
@@ -203,7 +294,7 @@ const bfd_target i386_msdos_vec =
 
     {
       _bfd_dummy_target,
-      _bfd_dummy_target,		/* bfd_check_format */
+      msdos_object_p,		/* bfd_check_format */
       _bfd_dummy_target,
       _bfd_dummy_target,
     },
-- 
2.7.4


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