All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stefan Reinauer <stepan@openbios.org>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: [PATCH] ELF program header
Date: Sat, 13 Oct 2007 00:03:37 +0200	[thread overview]
Message-ID: <20071012220337.GB21996@coresystems.de> (raw)
In-Reply-To: <20071012161955.GA26491@thorin>

* Robert Millan <rmh@aybabtu.com> [071012 18:19]:
> 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.
> 
> 
>   - Our own ELF loader doesn't like phdroff > 0x2000 either, from
>     loader/i386/pc/multiboot.c:

I think such a fixed address makes little sense, however there are
reasons to put the phdr in front.

>   /* 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 assume this was with LinuxBIOSv3 and Qemu?

> I'm not completely sure of its correctness though, and would appreciate if
> someone with a better understanding of ELF can comment on it.  

There's one good reason to put ELF headers in the beginning, that is
streaming of ELF files. If you get your ELF from a streaming
decompression algorithm you have to 
- either copy your elf to memory completely before loading it a second
  time
- or decompress it twice
- or you put the phdr in the beginning :-)

> 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?

We tried the same but the patch was a lot bigger, trying to actually
leave space in the beginning. Your patch might be enough, that would be
really nice. I didn't really get the sense behind the layers of grub
magic around read() and write(). Looking at the code, it looks
completely wrong, but it surprisingly seemed to work in LBv3.

>    /* 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);

Stefan

-- 
coresystems GmbH • Brahmsstr. 16 • D-79104 Freiburg i. Br.
      Tel.: +49 761 7668825 • Fax: +49 761 7664613
Email: info@coresystems.de  • http://www.coresystems.de/



  parent reply	other threads:[~2007-10-12 22:03 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-12 16:19 [PATCH] ELF program header Robert Millan
2007-10-12 20:24 ` Andrei E. Warkentin
2007-10-14 11:57   ` Robert Millan
2007-10-12 22:03 ` Stefan Reinauer [this message]
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=20071012220337.GB21996@coresystems.de \
    --to=stepan@openbios.org \
    --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.