All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Move ELF program header
@ 2008-06-02 15:41 Bean
  2008-06-13  7:08 ` Bean
  0 siblings, 1 reply; 18+ messages in thread
From: Bean @ 2008-06-02 15:41 UTC (permalink / raw)
  To: The development of GRUB 2

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

Hi,

This patch move the elf program header to the beginning, it should
solve the problem with coreboot.

-- 
Bean

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: elfhdr.diff --]
[-- Type: text/x-diff; name=elfhdr.diff, Size: 2519 bytes --]

diff --git a/util/elf/grub-mkimage.c b/util/elf/grub-mkimage.c
index ca138dd..51c4e54 100644
--- a/util/elf/grub-mkimage.c
+++ b/util/elf/grub-mkimage.c
@@ -166,8 +166,8 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
   FILE *in;
   char *kernel_path;
   grub_addr_t grub_end = 0;
-  off_t phdroff;
-  int i;
+  off_t offset;
+  int i, phdr_size;
 
   /* Read ELF header.  */
   kernel_path = grub_util_get_path (dir, "kernel.elf");
@@ -177,8 +177,21 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
 
   grub_util_read_at (&ehdr, sizeof (ehdr), 0, in);
   
-  phdrs = xmalloc (grub_target_to_host16 (ehdr.e_phentsize)
-		   * (grub_target_to_host16 (ehdr.e_phnum) + 2));
+  offset = ALIGN_UP (sizeof (ehdr), sizeof (long));
+  ehdr.e_phoff = grub_host_to_target32 (offset);
+
+  phdr_size = (grub_target_to_host16 (ehdr.e_phentsize) *
+               grub_target_to_host16 (ehdr.e_phnum));
+
+  if (mods[0] != NULL)
+    phdr_size += grub_target_to_host16 (ehdr.e_phentsize);
+
+  if (chrp)
+    phdr_size += grub_target_to_host16 (ehdr.e_phentsize);
+
+  phdrs = xmalloc (phdr_size);
+  offset += ALIGN_UP (phdr_size, sizeof (long));
+
   /* Copy all existing segments.  */
   for (i = 0; i < grub_target_to_host16 (ehdr.e_phnum); i++)
     {
@@ -207,8 +220,11 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
   
       grub_util_read_at (segment_img, grub_target_to_host32 (phdr->p_filesz),
 			 grub_target_to_host32 (phdr->p_offset), in);
+
+      phdr->p_offset = grub_host_to_target32 (offset);
       grub_util_write_image_at (segment_img, grub_target_to_host32 (phdr->p_filesz),
-				grub_target_to_host32 (phdr->p_offset), out);
+				offset, out);
+      offset += ALIGN_UP (grub_target_to_host32 (phdr->p_filesz), sizeof (long));
 
       free (segment_img);
     }
@@ -249,14 +265,10 @@ add_segments (char *dir, FILE *out, int chrp, char *mods[])
   ehdr.e_shnum = 0;
   ehdr.e_shstrndx = 0;
 
-  /* Append entire segment table to the file.  */
-  phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
-  grub_util_write_image_at (phdrs, grub_target_to_host16 (ehdr.e_phentsize)
-			    * grub_target_to_host16 (ehdr.e_phnum), phdroff,
-			    out);
+  /* Write entire segment table to the file.  */
+  grub_util_write_image_at (phdrs, phdr_size, grub_target_to_host32 (ehdr.e_phoff), out);
 
   /* Write ELF header.  */
-  ehdr.e_phoff = grub_host_to_target32 (phdroff);
   grub_util_write_image_at (&ehdr, sizeof (ehdr), 0, out);
 
   free (phdrs);

^ permalink raw reply related	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2008-06-17 17:24 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-02 15:41 [PATCH] Move ELF program header Bean
2008-06-13  7:08 ` Bean
2008-06-14 11:37   ` Robert Millan
2008-06-14 16:31     ` Bean
2008-06-16  5:40       ` Pavel Roskin
2008-06-16 14:25         ` Bean
2008-06-16 14:33           ` Pavel Roskin
2008-06-17 14:21             ` Bean
2008-06-17 15:17               ` Robert Millan
2008-06-17 15:26                 ` Bean
2008-06-17 15:32                 ` Pavel Roskin
2008-06-17 15:34                   ` Robert Millan
2008-06-17 15:37                     ` Pavel Roskin
2008-06-17 15:47                   ` Bean
2008-06-17 15:55                     ` Pavel Roskin
2008-06-17 16:02                       ` Bean
2008-06-17 17:18                         ` Pavel Roskin
2008-06-17 17:24                           ` Bean

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.