All of lore.kernel.org
 help / color / mirror / Atom feed
From: Robert Millan <rmh@aybabtu.com>
To: grub-devel@gnu.org
Subject: [PATCH] ELF program header
Date: Fri, 12 Oct 2007 18:19:55 +0200	[thread overview]
Message-ID: <20071012161955.GA26491@thorin> (raw)

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


It seems that grub-mkimage generates awkward ELF files, in which the Program
header table is at the end of the file instead of right after the ELF header.

I know very little about ELF, but:

  - This figure in ELF standard seems to indicate which is the "normal" (not
  sure if mandatory) location:

    http://www.cs.ucdavis.edu/~haungs/paper/node11.html

  - Our own ELF loader doesn't like phdroff > 0x2000 either, from
    loader/i386/pc/multiboot.c:

  /* FIXME: Should we support program headers at strange locations?  */
  if (ehdr->e_phoff + ehdr->e_phnum * ehdr->e_phentsize > MULTIBOOT_SEARCH)
    return grub_error (GRUB_ERR_BAD_OS, "program header at a too high offset");

This breaks self-boot in the LinuxBIOS target.  Moving the Program header
(see attached patch) fixed it, with no apparent drawbacks or regressions in
any of the ELF loaders around (tested on LinuxBIOS ELF loader and Efika OF).

I'm not completely sure of its correctness though, and would appreciate if
someone with a better understanding of ELF can comment on it.  In particular,
I don't know if my proposed solution could overwrite valid data.  Are the
segments garanteed to always leave room for the program header, do we
have to explicitly check for that, or perhaps we need to relocate the segments
when needed?

-- 
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.diff --]
[-- Type: text/x-diff, Size: 731 bytes --]

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

	* util/elf/grub-mkimage.c (add_segments): Allocate Program header
	table right after ELF header.

diff -ur grub2/util/elf/grub-mkimage.c grub2.phdr/util/elf/grub-mkimage.c
--- grub2/util/elf/grub-mkimage.c	2007-10-12 12:22:27.000000000 +0200
+++ grub2.phdr/util/elf/grub-mkimage.c	2007-10-12 12:36:38.000000000 +0200
@@ -250,7 +250,7 @@
   ehdr.e_shstrndx = 0;
 
   /* Append entire segment table to the file.  */
-  phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long));
+  phdroff = ALIGN_UP (sizeof (ehdr), sizeof (long));
   grub_util_write_image_at (phdrs, grub_target_to_host16 (ehdr.e_phentsize)
 			    * grub_target_to_host16 (ehdr.e_phnum), phdroff,
 			    out);

             reply	other threads:[~2007-10-12 16:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-12 16:19 Robert Millan [this message]
2007-10-12 20:24 ` [PATCH] ELF program header Andrei E. Warkentin
2007-10-14 11:57   ` Robert Millan
2007-10-12 22:03 ` Stefan Reinauer
2007-10-14 12:00   ` Robert Millan
2007-10-16 17:14     ` Marcin Kurek
2007-10-17 19:20     ` Stefan Reinauer

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=20071012161955.GA26491@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.