From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1IgNFR-0001m9-Ks for mharc-grub-devel@gnu.org; Fri, 12 Oct 2007 12:20:25 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IgNFP-0001kB-U1 for grub-devel@gnu.org; Fri, 12 Oct 2007 12:20:24 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IgNFP-0001iz-9A for grub-devel@gnu.org; Fri, 12 Oct 2007 12:20:23 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IgNFP-0001iX-0d for grub-devel@gnu.org; Fri, 12 Oct 2007 12:20:23 -0400 Received: from aybabtu.com ([69.60.117.155]) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1IgNFO-0005Nu-MI for grub-devel@gnu.org; Fri, 12 Oct 2007 12:20:22 -0400 Received: from [192.168.10.6] (helo=thorin) by aybabtu.com with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.63) (envelope-from ) id 1IgNFL-0000Fk-5D for grub-devel@gnu.org; Fri, 12 Oct 2007 18:20:19 +0200 Received: from rmh by thorin with local (Exim 4.63) (envelope-from ) id 1IgNEx-0006xv-HH for grub-devel@gnu.org; Fri, 12 Oct 2007 18:19:55 +0200 Date: Fri, 12 Oct 2007 18:19:55 +0200 From: Robert Millan To: grub-devel@gnu.org Message-ID: <20071012161955.GA26491@thorin> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="gBBFr7Ir9EOA20Yy" Content-Disposition: inline Organization: free as in freedom X-Message-Flag: Microsoft discourages use of Outlook. X-Debbugs-No-Ack: true User-Agent: Mutt/1.5.13 (2006-08-11) X-detected-kernel: by monty-python.gnu.org: Genre and OS details not recognized. Subject: [PATCH] ELF program header X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Oct 2007 16:20:24 -0000 --gBBFr7Ir9EOA20Yy Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 I know my rights; I want my phone call! What use is a phone call, if you are unable to speak? (as seen on /.) --gBBFr7Ir9EOA20Yy Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="phdr.diff" 2007-10-12 Robert Millan * 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); --gBBFr7Ir9EOA20Yy--