From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1Ih3qH-0005Un-DU for mharc-grub-devel@gnu.org; Sun, 14 Oct 2007 09:49:17 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1Ih3qF-0005UF-PJ for grub-devel@gnu.org; Sun, 14 Oct 2007 09:49:15 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1Ih3q6-0005SK-UM for grub-devel@gnu.org; Sun, 14 Oct 2007 09:49:15 -0400 Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ih3q6-0005SH-Oi for grub-devel@gnu.org; Sun, 14 Oct 2007 09:49:06 -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 1Ih3q2-0004I7-MS for grub-devel@gnu.org; Sun, 14 Oct 2007 09:49:06 -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 1Ih3pi-00007u-Ip for grub-devel@gnu.org; Sun, 14 Oct 2007 15:48:43 +0200 Received: from rmh by thorin with local (Exim 4.63) (envelope-from ) id 1Ih3pK-0006vV-20 for grub-devel@gnu.org; Sun, 14 Oct 2007 15:48:18 +0200 Date: Sun, 14 Oct 2007 15:48:18 +0200 From: Robert Millan To: grub-devel@gnu.org Message-ID: <20071014134818.GA26460@thorin> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="pf9I7BMVVzbSWLtt" 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] access phdr header entries like an array 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: Sun, 14 Oct 2007 13:49:16 -0000 --pf9I7BMVVzbSWLtt Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 I know my rights; I want my phone call! What use is a phone call, if you are unable to speak? (as seen on /.) --pf9I7BMVVzbSWLtt Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="phdr_array.diff" 2007-10-14 Robert Millan * 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; } --pf9I7BMVVzbSWLtt--