Skip to contentSkip to conversation

generic ELF loading

PATCH In review Hollis Blanchard 4 participants 9 messages Oct 13, 2006 – Oct 25, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 generic ELF loading

This patch adds generic ELF loading infrastructure for both 32-bit and 64-bit ELF. It provides an "iterate" function for program headers, and a "load" function for convenience.

I have converted the PowerPC Linux loader to use this infrastructure (see next mail), and possibly the i386 multiboot loader as well (though I will need others to test it). The module loader looks a little more annoying because it actually loads *sections*, not segments.

I will be away next week but intend to commit this soon after, so please review.

This code is literally duplicated, once for 32-bit and once for 64-bit. I'm trying to come up with a better way of doing that, and I'm considering something like this:

        elf32.c:
        #define Elf_Ehdr Elf32_Ehdr
        #define Elf_Phdr Elf32_Phdr
        #include "elfclass.c"
        
        elf64.c:
        #define Elf_Ehdr Elf64_Ehdr
        #define Elf_Phdr Elf64_Phdr
        #include "elfclass.c"

If you strongly object to #including C files, or have a better way of doing this, please speak up.

-Hollis

include/grub/elfload.h new file +57 -0
diff -r b53dfa2812ad include/grub/elfload.h
@@ -0,0 +1,57 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef GRUB_ELFLOAD_HEADER
+#define GRUB_ELFLOAD_HEADER	1
+
+#include <grub/err.h>
+#include <grub/elf.h>
+#include <grub/file.h>
+#include <grub/symbol.h>
+#include <grub/types.h>
+
+struct grub_elf_file
+{
+  grub_file_t file;
+  union {
+    Elf64_Ehdr ehdr64;
+    Elf32_Ehdr ehdr32;
+  } ehdr;
+  void *phdrs;
+};
+typedef struct grub_elf_file *grub_elf_t;
+
+grub_elf_t EXPORT_FUNC(grub_elf_open) (const char *name);
+grub_err_t EXPORT_FUNC(grub_elf_close) (grub_elf_t elf);
+
+int EXPORT_FUNC(grub_elf_is_elf32) (grub_elf_t elf);
+grub_err_t EXPORT_FUNC(grub_elf32_phdr_iterate) (grub_elf_t elf,
+			 int (*hook) (grub_elf_t, Elf32_Phdr *, void *),
+			 void *hook_arg);
+grub_err_t EXPORT_FUNC(grub_elf32_load) (grub_elf_t elf,
+					 int (*) (Elf32_Phdr *));
+
+int EXPORT_FUNC(grub_elf_is_elf64) (grub_elf_t elf);
+grub_err_t EXPORT_FUNC(grub_elf64_phdr_iterate) (grub_elf_t elf,
+			 int (*hook) (grub_elf_t, Elf64_Phdr *, void *),
+			 void *hook_arg);
+grub_err_t EXPORT_FUNC(grub_elf64_load) (grub_elf_t elf,
+					 int (*) (Elf64_Phdr *));
+
+#endif /* ! GRUB_ELFLOAD_HEADER */
kern/elf.c new file +280 -0

280 lines not on this page · raw message

#1 lore raw

Re: [PATCH] ppc64 Linux ELF loader

This patch converts the PowerPC Linux loader to use the ELF infrastructure in the last patch, which gives us 64-bit ELF support. You can see examples of how I'm using the ELF "iterate" and "load" functions.

-Hollis

include/grub/types.h +2 -0
diff -r b7b3f308a91d include/grub/types.h
@@ -22,6 +22,8 @@
 
 #include <config.h>
 #include <grub/cpu/types.h>
+
+#define __unused __attribute__ ((unused))
 
 #ifdef GRUB_UTIL
 # define GRUB_CPU_SIZEOF_VOID_P	SIZEOF_VOID_P
loader/powerpc/ieee1275/linux.c +149 -110

309 lines not on this page · raw message

#2 lore raw
In reply to Hollis Blanchard
Quoted 3 lines · Hollis Blanchard

This patch adds generic ELF loading infrastructure for both 32-bit and 64-bit ELF. It provides an "iterate" function for program headers, and a "load" function for convenience.

The idea is very good. But I don't like that loaded areas are always allocated from the heap. GRUB has a staging area for OS images on i386-pc, and I prefer to load an image directly instead of consuming the heap.

Thanks,
Okuji

#3 lore raw
Quoted 8 lines · Yoshinori K. Okuji
Quoted 3 lines · Hollis Blanchard

This patch adds generic ELF loading infrastructure for both 32-bit and 64-bit ELF. It provides an "iterate" function for program headers, and a "load" function for convenience.

The idea is very good. But I don't like that loaded areas are always allocated from the heap. GRUB has a staging area for OS images on i386-pc, and I prefer to load an image directly instead of consuming the heap.

Two points for ia64:
* the area must be allocated from EFI.
* we need to support relocation: loading the ELF file with an offset (this feature can be on/off/forced).

Tristan.

#4 lore raw
Quoted 11 lines · Tristan Gingold
Quoted 8 lines · Yoshinori K. Okuji
Quoted 3 lines · Hollis Blanchard

This patch adds generic ELF loading infrastructure for both 32-bit and 64-bit ELF. It provides an "iterate" function for program headers, and a "load" function for convenience.

The idea is very good. But I don't like that loaded areas are always allocated from the heap. GRUB has a staging area for OS images on i386-pc, and I prefer to load an image directly instead of consuming the heap.

Two points for ia64:
* the area must be allocated from EFI.

What does this mean? Open Firmware has a "claim" call, which reserves memory and makes it available to the application. I assume EFI must have something similar. This can be called via a hook.

For error handling, you'd probably need to iterate once to claim the memory, iterate once to copy the ELF file there, and in case of error iterate again to free the claimed memory.

Quoted 2 lines

* we need to support relocation: loading the ELF file with an offset (this feature can be on/off/forced).

This is provided via `load_hook' in grub_elf32_load(). In fact, PowerPC already does this. Please see offset_phdr() in my second mail (Subject:
Re: [PATCH] ppc64 Linux ELF loader).

-Hollis

#5 lore raw
In reply to Yoshinori K. Okuji
Quoted 8 lines · Yoshinori K. Okuji
Quoted 3 lines · Hollis Blanchard

This patch adds generic ELF loading infrastructure for both 32-bit and 64-bit ELF. It provides an "iterate" function for program headers, and a "load" function for convenience.

The idea is very good. But I don't like that loaded areas are always allocated from the heap. GRUB has a staging area for OS images on i386-pc, and I prefer to load an image directly instead of consuming the heap.

Actually I'm not using the heap, I'm just directly copying wherever phdr->p_paddr says to. That's not a good thing actually; in the future we should add some error checking to make sure we don't clobber GRUB itself.

Also, I made sure that the load hook could veto loading each segment. So the x86 loader would do something like this:

        int x86_load_hook (Elf32_Phdr *phdr)
        {
        	if (phdr->p_paddr not in x86 OS load area)
        		return 1;
        	return 0;
        }
        
        grub_elf32_load (elf, x86_load_hook);

-Hollis

#6 lore raw
Quoted 8 lines · Hollis Blanchard
Quoted 3 lines

The idea is very good. But I don't like that loaded areas are always allocated from the heap. GRUB has a staging area for OS images on i386-pc, and I prefer to load an image directly instead of consuming the heap.

Actually I'm not using the heap, I'm just directly copying wherever phdr->p_paddr says to. That's not a good thing actually; in the future we should add some error checking to make sure we don't clobber GRUB itself.

Have you looked at how EFI solves this?

They keep track of all memory regions, and with each region is a "memory type" associated. Whenever you allocate memory you change the type of the region (from "free") to some that makes sense (could be "loader data", "disk cache", ...). You can only allocate memory that is marked "conventional", meaning it is considered free. The memory region database is later feed to the operating system. We could do the same.

Is this case we could allocate the regions specified by the ELF image. If any of the allocations fail we know there is something already loaded there, or the image is faulty.

~j

#7 lore raw
Quoted 21 lines · Johan Rydberg
Quoted 8 lines · Hollis Blanchard
Quoted 3 lines

The idea is very good. But I don't like that loaded areas are always allocated from the heap. GRUB has a staging area for OS images on i386-pc, and I prefer to load an image directly instead of consuming the heap.

Actually I'm not using the heap, I'm just directly copying wherever phdr->p_paddr says to. That's not a good thing actually; in the future we should add some error checking to make sure we don't clobber GRUB itself.

Have you looked at how EFI solves this?

They keep track of all memory regions, and with each region is a "memory type" associated. Whenever you allocate memory you change the type of the region (from "free") to some that makes sense (could be "loader data", "disk cache", ...). You can only allocate memory that is marked "conventional", meaning it is considered free. The memory region database is later feed to the operating system. We could do the same.

Quoted 3 lines

Is this case we could allocate the regions specified by the ELF image. If any of the allocations fail we know there is something already loaded there, or the image is faulty.

That's a great point. Doing a "claim" (as described in my reply to Tristan) will return an error if the area is already claimed, so that will accomplish the check.

On x86, the load hook will manually check to make sure the address is within the OS load area.

-Hollis

#8 lore raw
In reply to Hollis Blanchard
Quoted 4 lines · Hollis Blanchard

Actually I'm not using the heap, I'm just directly copying wherever phdr->p_paddr says to. That's not a good thing actually; in the future we should add some error checking to make sure we don't clobber GRUB itself.

OK, then it's even worse. :(

You must not assume that GRUB can always load an OS image to an appropriate location directly. I know this is the case for the current implementation of the Multiboot loader, but it is a very bad idea, generally speaking.

What GRUB should do is first to load an image to somewhere then relocate it to the right place at boot time. On i386-pc, the OS area is used for this very purpose. The x86 Multiboot loader is just a mistake, and that's why we must rewrite it.

Okuji