Skip to contentSkip to conversation

new ELF loader module

PATCH Guillem Jover 2 participants 3 messages Feb 1, 2005 – Feb 5, 2005

lore
Share & tools
lore
#0 lore raw

Hi,

This patch merges some of the ELF static image loading functionality into an independent elf module. Please test on PPC as I've only tested this on x86.

2005-02-01 Guillem Jover <[email protected]>

* conf/i386-pc.rmk (pkg_MODULES): Add `elf.mod'. (elf_mod_SOURCES): New variable.
(elf_mod_CFLAGS): Likewise.
* conf/powerpc-ieee1275.rmk: Likewise.
* loader/elf.c: New file.
* loader/i386/pc/multiboot.c (grub_rescue_cmd_multiboot): Use grub_elf_load_static instead of explicit load loop.
* loader/powerpc/ieee1275/linux.c (grub_rescue_cmd_linux): Likewise. Use grub_elf_phdr instead of explicit header load.

conf/i386-pc.rmk +6 -1
diff -Naupr -x CVS grub2.dl/conf/i386-pc.rmk grub2.elf/conf/i386-pc.rmk
@@ -81,7 +81,8 @@ grub_emu_LDFLAGS = -lncurses
 genmoddep_SOURCES = util/genmoddep.c
 
 # Modules.
-pkgdata_MODULES = _chain.mod _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
+pkgdata_MODULES = _chain.mod _linux.mod linux.mod elf.mod \
+	fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod vga.mod font.mod _multiboot.mod ls.mod \
 	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod chain.mod multiboot.mod \
 	amiga.mod apple.mod pc.mod loopback.mod reboot.mod halt.mod help.mod
@@ -126,6 +127,10 @@ jfs_mod_CFLAGS = $(COMMON_CFLAGS)
 iso9660_mod_SOURCES = fs/iso9660.c
 iso9660_mod_CFLAGS = $(COMMON_CFLAGS)
 
+# For elf.mod.
+elf_mod_SOURCES = loader/elf.c
+elf_mod_CFLAGS = $(COMMON_CFLAGS)
+
 # For _linux.mod.
 _linux_mod_SOURCES = loader/i386/pc/linux.c
 _linux_mod_CFLAGS = $(COMMON_CFLAGS)
conf/powerpc-ieee1275.rmk +6 -1
diff -Naupr -x CVS grub2.dl/conf/powerpc-ieee1275.rmk grub2.elf/conf/powerpc-ieee1275.rmk
@@ -62,7 +62,8 @@ grubof_LDFLAGS = -nostdlib -static-libgc
 genmoddep_SOURCES = util/genmoddep.c
 
 # Modules.
-pkgdata_MODULES = _linux.mod linux.mod fat.mod ufs.mod ext2.mod minix.mod \
+pkgdata_MODULES = _linux.mod linux.mod elf.mod \
+	fat.mod ufs.mod ext2.mod minix.mod \
 	hfs.mod jfs.mod normal.mod hello.mod font.mod \
 	boot.mod cmp.mod cat.mod terminal.mod fshelp.mod amiga.mod apple.mod \
 	pc.mod suspend.mod loopback.mod help.mod reboot.mod halt.mod
@@ -107,6 +108,10 @@ _linux_mod_CFLAGS = $(COMMON_CFLAGS)
 linux_mod_SOURCES = loader/powerpc/ieee1275/linux_normal.c
 linux_mod_CFLAGS = $(COMMON_CFLAGS)
 
+# For elf.mod.
+elf_mod_SOURCES = loader/elf.c
+elf_mod_CFLAGS = $(COMMON_CFLAGS)
+
 # For normal.mod.
 normal_mod_SOURCES = normal/cmdline.c normal/command.c normal/main.c \
 	normal/menu.c normal/arg.c normal/powerpc/setjmp.S
loader/elf.c +104 -0

104 lines not on this page · raw message

loader/i386/pc/multiboot.c +5 -36

54 lines not on this page · raw message

loader/powerpc/ieee1275/linux.c +5 -25
diff -Naupr -x CVS grub2.dl/loader/powerpc/ieee1275/linux.c grub2.elf/loader/powerpc/ieee1275/linux.c
@@ -147,8 +147,9 @@ grub_rescue_cmd_linux (int argc, char *a
   else
     vmlinux = 0;
 
-  phdrs = (Elf32_Phdr *) grub_malloc (ehdr.e_phnum * ehdr.e_phentsize);
-  grub_file_read (file, (void *) phdrs, ehdr.e_phnum * ehdr.e_phentsize);
+  phdrs = grub_elf_get_phdr (file, ehdr);
+  if (! phdrs)
+    goto fail;
   
   /* Release the previously used memory.  */
   grub_loader_unset ();
@@ -180,31 +181,10 @@ grub_rescue_cmd_linux (int argc, char *a
       goto fail;
     }
   entry = linux_addr;
-  
-  /* Load every loadable segment in memory.  */
-  for (i = 0; i < ehdr.e_phnum; i++)
-    {
-      Elf32_Phdr *phdr = phdrs + i;
 
-      if (phdr->p_type == PT_LOAD)
-	{
-	  if (grub_file_seek (file, phdr->p_offset) == -1)
-	    {
-	      grub_error (GRUB_ERR_BAD_OS, "Invalid offset in program header");
-	      goto fail;
-	    }
-	  
-	  if (grub_file_read (file, (void *) (((char *) entry) + offset) , phdr->p_filesz) 
-	      != (grub_ssize_t) phdr->p_filesz)
-	    goto fail;
-	  
-	  if (phdr->p_filesz < phdr->p_memsz)
-	    grub_memset ((char *) (((char *) entry) + offset) + phdr->p_filesz, 0, 
-			 phdr->p_memsz - phdr->p_filesz);
+  if (grub_elf_load_static (file, ehdr, phdrs, entry, linux_addr, linux_size))
+    goto fail;
 
-	  offset += phdr->p_filesz;
-	}
-    }
   
   size = 0;
   for (i = 0; i < argc; i++)
#1 lore raw

Guillem Jover <[email protected]> writes:

Hi Guillem,

Quoted 3 lines

This patch merges some of the ELF static image loading functionality into an independent elf module. Please test on PPC as I've only tested this on x86.

Thanks for this patch. I will comment on it after I tested it.

Thanks,
Marco

#2 lore raw
In reply to Guillem Jover

Guillem Jover <[email protected]> writes:

Hi Guillem,

Quoted 3 lines

This patch merges some of the ELF static image loading functionality into an independent elf module. Please test on PPC as I've only tested this on x86.

It does not work on the PPC, unfortunately. it gives the error:

error: Segment doesn't fit in memory reserved for the image

I will get back to this later in this email.

Quoted 4 lines

2005-02-01 Guillem Jover <[email protected]>

* conf/i386-pc.rmk (pkg_MODULES): Add `elf.mod'.

I would prefer elf32.mod because we need something similar for 64 bits ELFs. Perhaps some macro magic can be used to use the same sourcecode for 64 bits ELFs...?

Quoted 3 lines

diff -Naupr -x CVS grub2.dl/loader/elf.c grub2.elf/loader/elf.c

loader/elf.c +0 -0

[...]

loader/elf.c line 39

Quoted 13 lines
loader/elf.c +13 -0
+#if GRUB_HOST_SIZEOF_VOID_P == 4
+
+typedef Elf32_Addr Elf_Addr;
+typedef Elf32_Ehdr Elf_Ehdr;
+typedef Elf32_Phdr Elf_Phdr;
+
+#elif GRUB_HOST_SIZEOF_VOID_P == 8
+
+typedef Elf64_Addr Elf_Addr;
+typedef Elf64_Ehdr Elf_Ehdr;
+typedef Elf64_Phdr Elf_Phdr;
+
+#endif

I think the type of loader should not be dependent of the processor. Perhaps we want a 64 bits ELF loader on the PPC. Or the other way around.

loader/elf.c line 47

Quoted 7 lines
loader/elf.c +7 -0
+
+Elf_Phdr *
+grub_elf_get_phdr (grub_file_t file, Elf_Ehdr *ehdr)
+{
+  Elf_Phdr *phdr = 0;
+
+  phdr = (Elf_Phdr *) grub_malloc (ehdr->e_phnum * ehdr->e_phentsize);

You did not check if the allocation was successful. This can be done using:

if (!phdr)
return 0;

loader/elf.c line 68

Quoted 4 lines
loader/elf.c +4 -0
+grub_err_t
+grub_elf_load_static (grub_file_t file, Elf_Ehdr *ehdr, Elf_Phdr *phdrs,
+		      Elf_Addr base_offs,
+		      grub_addr_t base_addr, grub_size_t base_size)

Can you explain what all arguments do and which effect they will have? I tried fixing this function so it would work for me, but I got confused and stopped.

On the PPC base_addr and base_offs are the same. They are set to the address the kernel should be loaded to. Here you just load to what is requested in the ELF, but this is not always the right thing to do. For example on the PPC you have to claim memory. If the memory can not be claimed, the linux loader just uses another address to load linux to.

Hopefully we can work on this so we can define the right interface that can handle all weird requirements we could have and still be flexible enough. It looks nice already so it should not be too much work. :)

Quoted 2 lines
loader/powerpc/ieee1275/linux.c +0 -0

[...]

loader/powerpc/ieee1275/linux.c grub_rescue_cmd_linux (int argc, char *a · line 186

Quoted 2 lines
loader/powerpc/ieee1275/linux.c +2 -0
+  if (grub_elf_load_static (file, ehdr, phdrs, entry, linux_addr, linux_size))
+    goto fail;

linux_addr and entry are set to the same values at this point.

When compiling GRUB 2 after committing this patch the following warnings are shown:

loader/powerpc/ieee1275/linux.c: In function `grub_rescue_cmd_linux':
loader/powerpc/ieee1275/linux.c:150: warning: implicit declaration of function grub_elf_get_phdr' loader/powerpc/ieee1275/linux.c:150: warning: assignment makes pointer from integer without a cast loader/powerpc/ieee1275/linux.c:185: warning: implicit declaration of function grub_elf_load_static' loader/powerpc/ieee1275/linux.c:104: warning: unused variable `offset'

Perhaps this an be fixed by adding prototypes. I am not sure if elf.h would be the right file to add this because it looks like a copy from glibc.

Hopefully we can cooperate to fix these remaining issues. In the linux loader on the PPC works and after thinking about how the elf64 loader can be implemented. no need to implement it already, I just don't want this to make it hard to implement one later.

Thanks,
Marco