All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Millan <rmh@aybabtu.com>
To: grub-devel@gnu.org
Subject: [PATCH] access phdr header entries like an array
Date: Sun, 14 Oct 2007 15:48:18 +0200	[thread overview]
Message-ID: <20071014134818.GA26460@thorin> (raw)

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


This patch makes it easier and more intuitive to access entries in the phdr
header as if it were a C array.  It has otherwise no effect on the current
code (other than saving some space for an awkward reason), but is needed to
implement the ability to load segments at an arbitrary address, distinguishing
the relative offset rather than their absolute requested address.

I have the code for all the dance, including the ability to relocate the
payload later on, but I've chosen to split it up for revision tracking
purposes (besides, the rest needs quite a bit of cleanup yet ;-)).

I've checked there are no regressions (at least with invaders).  If there are
no objections in a few days I'll check it in.

-- 
Robert Millan

<GPLv2> I know my rights; I want my phone call!
<DRM> What use is a phone call, if you are unable to speak?
(as seen on /.)

[-- Attachment #2: phdr_array.diff --]
[-- Type: text/x-diff, Size: 4921 bytes --]

2007-10-14  Robert Millan  <rmh@aybabtu.com>

	* loader/i386/pc/multiboot.c (grub_multiboot_load_elf32): When loading
	ELF segments, use a macro for arbitrarily accessing any of them instead
	of preparing a pointer that allows access to one at a time.
	(grub_multiboot_load_elf64): Likewise.

diff -ur grub2/loader/i386/pc/multiboot.c grub2.phdr_array/loader/i386/pc/multiboot.c
--- grub2/loader/i386/pc/multiboot.c	2007-07-25 21:29:24.000000000 +0200
+++ grub2.phdr_array/loader/i386/pc/multiboot.c	2007-10-14 15:32:37.000000000 +0200
@@ -94,7 +94,7 @@
 grub_multiboot_load_elf32 (grub_file_t file, void *buffer)
 {
   Elf32_Ehdr *ehdr = (Elf32_Ehdr *) buffer;
-  Elf32_Phdr *phdr;
+  void *phdr_base;
   int i;
 
   if (ehdr->e_ident[EI_CLASS] != ELFCLASS32)
@@ -112,35 +112,38 @@
   
   entry = ehdr->e_entry;
   
+  phdr_base = (void *) buffer + ehdr->e_phoff;
+#define phdr(i)		((Elf32_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
+
   /* Load every loadable segment in memory.  */
   for (i = 0; i < ehdr->e_phnum; i++)
     {
-      phdr = (Elf32_Phdr *) ((char *) buffer + ehdr->e_phoff
-			     + i * ehdr->e_phentsize);
-      if (phdr->p_type == PT_LOAD)
+      if (phdr(i)->p_type == PT_LOAD)
         {
           /* The segment should fit in the area reserved for the OS.  */
-          if ((phdr->p_paddr < grub_os_area_addr)
-              || (phdr->p_paddr + phdr->p_memsz
+          if ((phdr(i)->p_paddr < grub_os_area_addr)
+              || (phdr(i)->p_paddr + phdr(i)->p_memsz
 		  > grub_os_area_addr + grub_os_area_size))
 	    return grub_error (GRUB_ERR_BAD_OS,
 			       "segment doesn't fit in memory reserved for the OS");
 
-          if (grub_file_seek (file, (grub_off_t) phdr->p_offset)
+          if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
 	      == (grub_off_t) -1)
 	    return grub_error (GRUB_ERR_BAD_OS,
 			       "invalid offset in program header");
 	  
-          if (grub_file_read (file, (void *) phdr->p_paddr, phdr->p_filesz)
-              != (grub_ssize_t) phdr->p_filesz)
+          if (grub_file_read (file, (void *) phdr(i)->p_paddr, phdr(i)->p_filesz)
+              != (grub_ssize_t) phdr(i)->p_filesz)
 	    return grub_error (GRUB_ERR_BAD_OS,
 			       "couldn't read segment from file");
 
-          if (phdr->p_filesz < phdr->p_memsz)
-            grub_memset ((char *) phdr->p_paddr + phdr->p_filesz, 0,
-			 phdr->p_memsz - phdr->p_filesz);
+          if (phdr(i)->p_filesz < phdr(i)->p_memsz)
+            grub_memset ((char *) phdr(i)->p_paddr + phdr(i)->p_filesz, 0,
+			 phdr(i)->p_memsz - phdr(i)->p_filesz);
         }
     }
+
+#undef phdr
   
   return grub_errno;
 }
@@ -158,7 +161,7 @@
 grub_multiboot_load_elf64 (grub_file_t file, void *buffer)
 {
   Elf64_Ehdr *ehdr = (Elf64_Ehdr *) buffer;
-  Elf64_Phdr *phdr;
+  void *phdr_base;
   int i;
 
   if (ehdr->e_ident[EI_CLASS] != ELFCLASS64)
@@ -186,39 +189,42 @@
 
   entry = ehdr->e_entry;
 
+  phdr_base = (void *) buffer + ehdr->e_phoff;
+#define phdr(i)		((Elf64_Phdr *) (phdr_base + (i) * ehdr->e_phentsize))
+
   /* Load every loadable segment in memory.  */
   for (i = 0; i < ehdr->e_phnum; i++)
     {
-      phdr = (Elf64_Phdr *) ((char *) buffer + ehdr->e_phoff
-			     + i * ehdr->e_phentsize);
-      if (phdr->p_type == PT_LOAD)
+      if (phdr(i)->p_type == PT_LOAD)
         {
           /* The segment should fit in the area reserved for the OS.  */
-          if ((phdr->p_paddr < (grub_uint64_t) grub_os_area_addr)
-              || (phdr->p_paddr + phdr->p_memsz
+          if ((phdr(i)->p_paddr < (grub_uint64_t) grub_os_area_addr)
+              || (phdr(i)->p_paddr + phdr(i)->p_memsz
 		  > ((grub_uint64_t) grub_os_area_addr
 		     + (grub_uint64_t) grub_os_area_size)))
 	    return grub_error (GRUB_ERR_BAD_OS,
 			       "segment doesn't fit in memory reserved for the OS");
 	  
-	  if (grub_file_seek (file, (grub_off_t) phdr->p_offset)
+	  if (grub_file_seek (file, (grub_off_t) phdr(i)->p_offset)
 	      == (grub_off_t) -1)
 	    return grub_error (GRUB_ERR_BAD_OS,
 			       "invalid offset in program header");
 
-	  if (grub_file_read (file, (void *) ((grub_uint32_t) phdr->p_paddr),
-			      phdr->p_filesz)
-              != (grub_ssize_t) phdr->p_filesz)
+	  if (grub_file_read (file, (void *) ((grub_uint32_t) phdr(i)->p_paddr),
+			      phdr(i)->p_filesz)
+              != (grub_ssize_t) phdr(i)->p_filesz)
 	    return grub_error (GRUB_ERR_BAD_OS,
 			       "couldn't read segment from file");
 	  
-          if (phdr->p_filesz < phdr->p_memsz)
-	    grub_memset (((char *) ((grub_uint32_t) phdr->p_paddr)
-			  + phdr->p_filesz),
+          if (phdr(i)->p_filesz < phdr(i)->p_memsz)
+	    grub_memset (((char *) ((grub_uint32_t) phdr(i)->p_paddr)
+			  + phdr(i)->p_filesz),
 			 0,
-			 phdr->p_memsz - phdr->p_filesz);
+			 phdr(i)->p_memsz - phdr(i)->p_filesz);
         }
     }
+
+#undef phdr
   
   return grub_errno;
 }

             reply	other threads:[~2007-10-14 13:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-14 13:48 Robert Millan [this message]
2008-07-31 20:48 ` [PATCH] access phdr header entries like an array Robert Millan

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=20071014134818.GA26460@thorin \
    --to=rmh@aybabtu.com \
    --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.