All of lore.kernel.org
 help / color / mirror / Atom feed
From: Christian Franke <Christian.Franke@t-online.de>
To: grub-devel@gnu.org
Subject: [PATCH] Load ELF module with bad pc-relative relocation info
Date: Mon, 19 Nov 2007 21:00:15 +0100	[thread overview]
Message-ID: <4741EB4F.7060402@t-online.de> (raw)

[-- Attachment #1: Type: text/plain, Size: 528 bytes --]

This patch allows to load modules with bad pc-relative relocation info 
produced by "objcopy -I pe-i386 -O elf32-i386"
(See also thread "Building GRUB on platforms without ELF support")

Christian

2007-11-19  Christian Franke <franke@computer.org> 

	* kern/i386/dl.c [__CYGWIN__] (fix_pc_rel_relocation): New function
	to fix bad PC relative relocation produced by objcopy.
	[__CYGWIN__] (grub_arch_dl_relocate_symbols): Add fix of PC relative relocation.
	(grub_arch_dl_relocate_symbols): Abort on unknown relocation type.



[-- Attachment #2: grub2-i386_dl-Cygwin.patch --]
[-- Type: text/x-patch, Size: 1491 bytes --]

--- grub2.orig/kern/i386/dl.c	2007-07-22 01:32:26.000000000 +0200
+++ grub2/kern/i386/dl.c	2007-11-19 20:50:52.609375000 +0100
@@ -37,6 +37,28 @@ grub_arch_dl_check_header (void *ehdr)
   return GRUB_ERR_NONE;
 }
 
+#ifdef __CYGWIN__
+/* Fix PC relative relocation.  Objcopy does not adjust
+the addent when converting from pe-i386 to elf32-i386.  */
+static int
+fix_pc_rel_relocation (Elf32_Word *addr)
+{
+  /* To be safe, check instruction first.  */
+  const unsigned char * pc = (const unsigned char *)addr - 1;
+  if (!(*pc == 0xe8/*call*/ || *pc == 0xe9/*jmp*/))
+    return grub_error (GRUB_ERR_BAD_MODULE, "unknown pc-relative instruction %02x", *pc);
+  /* Check and adjust offset.  */
+  if (*addr != (Elf32_Word)-4)
+    {
+      if (*addr != 0)
+	return grub_error (GRUB_ERR_BAD_MODULE, "invalid pc-relative relocation base %lx",
+			   (long)(*addr));
+      *addr = (Elf32_Word)-4;
+    }
+  return GRUB_ERR_NONE;
+}
+#endif
+
 /* Relocate symbols.  */
 grub_err_t
 grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
@@ -99,9 +121,17 @@ grub_arch_dl_relocate_symbols (grub_dl_t
 		    break;
 
 		  case R_386_PC32:
+#ifdef __CYGWIN__
+		    if (fix_pc_rel_relocation (addr))
+		      return grub_errno;
+#endif
 		    *addr += (sym->st_value - (Elf32_Word) seg->addr
 			      - rel->r_offset);
 		    break;
+
+		  default:
+		    return grub_error (GRUB_ERR_BAD_MODULE, "unknown relocation type %x.",
+				       ELF32_R_TYPE (rel->r_info));
 		  }
 	      }
 	  }

                 reply	other threads:[~2007-11-19 20:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4741EB4F.7060402@t-online.de \
    --to=christian.franke@t-online.de \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.