Skip to contentSkip to conversation

generic ELF loading #3

PATCH In review Hollis Blanchard 4 participants 10 messages Nov 1, 2006 – Nov 9, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 generic ELF loading #3

Here is pass #3 of the ELF loader. Changes from #2:
- the `load' callback no longer modifies the phdr directly
- grub_elf_size() is now a standard function, and only considers PT_LOAD segments
- grub_elf_phdr_iterate() is now static (though could be made global if we need it somewhere in the future)
- better debugging output

(Again, since I will be replacing loader/i386/pc/multiboot in the near future, I have not converted it to use the new infrastructure.)

I'm just going to include them as attachments this time because it's easier. I have tested this code on PowerPC with both Mac and IBM firmware, with both 32-bit and 64-bit vmlinux files.

I believe this addresses all comments, so if there are no more issues I will check it in soon.

-Hollis

conf/i386-efi.mk +3 -3
Index: grub2-cvs/conf/i386-efi.mk
===================================================================
@@ -486,9 +486,9 @@ fs-kernel_mod-disk_efi_efidisk.lst: disk
 	set -e; 	  $(TARGET_CC) -Idisk/efi -I$(srcdir)/disk/efi $(TARGET_CPPFLAGS) $(TARGET_CFLAGS) $(kernel_mod_CFLAGS) -E $< 	  | sh $(srcdir)/genfslist.sh kernel > $@ || (rm -f $@; exit 1)
 
 
-kernel_mod_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h partition.h \
-	pc_partition.h rescue.h symbol.h term.h types.h \
+kernel_mod_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \
+	partition.h pc_partition.h rescue.h symbol.h term.h types.h \
 	i386/efi/time.h efi/efi.h efi/time.h efi/disk.h
 kernel_mod_CFLAGS = $(COMMON_CFLAGS)
 kernel_mod_ASFLAGS = $(COMMON_ASFLAGS)
conf/i386-efi.rmk +5 -4
Index: grub2-cvs/conf/i386-efi.rmk
===================================================================
@@ -43,7 +43,8 @@ grub_emu_SOURCES = commands/boot.c comma
 	fs/affs.c fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c	\
 	fs/jfs.c fs/minix.c fs/sfs.c fs/ufs.c fs/xfs.c fs/hfsplus.c	\
 	io/gzio.c							\
-	kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c 	\
+	kern/device.c kern/disk.c kern/dl.c kern/elf.c kern/env.c	\
+	kern/err.c							\
 	normal/execute.c kern/file.c kern/fs.c normal/lexer.c 		\
 	kern/loader.c kern/main.c kern/misc.c kern/parser.c		\
 	grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c	\
@@ -76,9 +77,9 @@ kernel_mod_SOURCES = kern/i386/efi/start
 	kern/i386/dl.c kern/i386/efi/init.c kern/parser.c kern/partition.c \
 	kern/env.c symlist.c kern/efi/efi.c kern/efi/init.c kern/efi/mm.c \
 	term/efi/console.c disk/efi/efidisk.c
-kernel_mod_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h partition.h \
-	pc_partition.h rescue.h symbol.h term.h types.h \
+kernel_mod_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \
+	partition.h pc_partition.h rescue.h symbol.h term.h types.h \
 	i386/efi/time.h efi/efi.h efi/time.h efi/disk.h
 kernel_mod_CFLAGS = $(COMMON_CFLAGS)
 kernel_mod_ASFLAGS = $(COMMON_ASFLAGS)
conf/i386-pc.mk +3 -3
Index: grub2-cvs/conf/i386-pc.mk
===================================================================
@@ -162,9 +162,9 @@ kernel_img-symlist.o: symlist.c
 	$(TARGET_CC) -I. -I$(srcdir)/. $(TARGET_CPPFLAGS)  $(TARGET_CFLAGS) $(kernel_img_CFLAGS) -MD -c -o $@ $<
 -include kernel_img-symlist.d
 
-kernel_img_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h partition.h \
-	pc_partition.h rescue.h symbol.h term.h types.h \
+kernel_img_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \
+	partition.h pc_partition.h rescue.h symbol.h term.h types.h \
 	machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \
 	machine/memory.h machine/loader.h machine/time.h machine/vga.h \
 	machine/vbe.h
conf/i386-pc.rmk +5 -4
Index: grub2-cvs/conf/i386-pc.rmk
===================================================================
@@ -30,9 +30,9 @@ kernel_img_SOURCES = kern/i386/pc/startu
 	kern/env.c disk/i386/pc/biosdisk.c \
 	term/i386/pc/console.c \
 	symlist.c
-kernel_img_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h partition.h \
-	pc_partition.h rescue.h symbol.h term.h types.h \
+kernel_img_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h loader.h misc.h mm.h net.h parser.h \
+	partition.h pc_partition.h rescue.h symbol.h term.h types.h \
 	machine/biosdisk.h machine/boot.h machine/console.h machine/init.h \
 	machine/memory.h machine/loader.h machine/time.h machine/vga.h \
 	machine/vbe.h
@@ -89,7 +89,8 @@ grub_emu_SOURCES = commands/boot.c comma
 	fs/affs.c fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c	\
 	fs/jfs.c fs/minix.c fs/sfs.c fs/ufs.c fs/xfs.c fs/hfsplus.c	\
 	io/gzio.c							\
-	kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c 	\
+	kern/device.c kern/disk.c kern/dl.c kern/elf.c kern/env.c	\
+	kern/err.c							\
 	normal/execute.c kern/file.c kern/fs.c normal/lexer.c 		\
 	kern/loader.c kern/main.c kern/misc.c kern/parser.c		\
 	grub_script.tab.c kern/partition.c kern/rescue.c kern/term.c	\
conf/powerpc-ieee1275.mk +25 -21

95 lines not on this page · raw message

conf/powerpc-ieee1275.rmk +9 -9
Index: grub2-cvs/conf/powerpc-ieee1275.rmk
===================================================================
@@ -10,11 +10,10 @@ COMMON_LDFLAGS += -nostdlib
 MOSTLYCLEANFILES += kernel_elf_symlist.c kernel_syms.lst
 DEFSYMFILES += kernel_syms.lst
 
-kernel_elf_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h symbol.h \
-	term.h types.h powerpc/libgcc.h loader.h \
-	partition.h pc_partition.h ieee1275/ieee1275.h machine/time.h \
-	machine/kernel.h
+kernel_elf_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h \
+	symbol.h term.h types.h powerpc/libgcc.h loader.h partition.h \
+	pc_partition.h ieee1275/ieee1275.h machine/time.h machine/kernel.h
 
 kernel_elf_symlist.c: $(addprefix include/grub/,$(kernel_elf_HEADERS)) config.h gensymlist.sh
 	/bin/sh gensymlist.sh $(filter %.h,$^) > $@ || (rm -f $@; exit 1)
@@ -45,9 +44,10 @@ grub_emu_SOURCES = commands/boot.c comma
 	fs/affs.c fs/ext2.c fs/fat.c fs/fshelp.c fs/hfs.c fs/iso9660.c	\
 	fs/jfs.c fs/minix.c fs/sfs.c fs/ufs.c fs/xfs.c fs/hfsplus.c	\
 	io/gzio.c							\
-	kern/device.c kern/disk.c kern/dl.c kern/env.c kern/err.c 	\
-	kern/file.c kern/fs.c kern/loader.c kern/main.c kern/misc.c	\
-	kern/parser.c kern/partition.c kern/rescue.c kern/term.c	\
+	kern/device.c kern/disk.c kern/dl.c kern/elf.c kern/env.c	\
+	kern/err.c kern/file.c kern/fs.c kern/loader.c kern/main.c	\
+	kern/misc.c kern/parser.c kern/partition.c kern/rescue.c	\
+	kern/term.c							\
 	normal/arg.c normal/cmdline.c normal/command.c			\
 	normal/completion.c normal/execute.c		 		\
 	normal/function.c normal/lexer.c normal/main.c normal/menu.c 	\
@@ -62,7 +62,7 @@ grub_emu_LDFLAGS = $(LIBCURSES)
 
 kernel_elf_SOURCES = kern/powerpc/ieee1275/crt0.S kern/powerpc/ieee1275/cmain.c \
 	kern/ieee1275/ieee1275.c kern/main.c kern/device.c 		\
-	kern/disk.c kern/dl.c kern/file.c kern/fs.c kern/err.c 		\
+	kern/disk.c kern/dl.c kern/err.c kern/file.c kern/fs.c 		\
 	kern/misc.c kern/mm.c kern/loader.c kern/rescue.c kern/term.c 	\
 	kern/powerpc/ieee1275/init.c term/ieee1275/ofconsole.c 		\
 	kern/powerpc/ieee1275/openfw.c disk/ieee1275/ofdisk.c 		\
conf/sparc64-ieee1275.mk +4 -5
Index: grub2-cvs/conf/sparc64-ieee1275.mk
===================================================================
@@ -10,11 +10,10 @@ COMMON_LDFLAGS = -melf64_sparc -nostdlib
 MOSTLYCLEANFILES += kernel_elf_symlist.c kernel_syms.lst
 DEFSYMFILES += kernel_syms.lst
 
-kernel_elf_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h symbol.h \
-	term.h types.h sparc64/libgcc.h loader.h \
-	partition.h pc_partition.h ieee1275/ieee1275.h machine/time.h \
-	machine/kernel.h
+kernel_elf_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h \
+	symbol.h term.h types.h sparc64/libgcc.h loader.h partition.h \
+	pc_partition.h ieee1275/ieee1275.h machine/time.h machine/kernel.h
 
 kernel_elf_symlist.c: $(addprefix include/grub/,$(kernel_elf_HEADERS)) config.h gensymlist.sh
 	/bin/sh gensymlist.sh $(filter %.h,$^) > $@ || (rm -f $@; exit 1)
conf/sparc64-ieee1275.rmk +4 -5
Index: grub2-cvs/conf/sparc64-ieee1275.rmk
===================================================================
@@ -10,11 +10,10 @@ COMMON_LDFLAGS = -melf64_sparc -nostdlib
 MOSTLYCLEANFILES += kernel_elf_symlist.c kernel_syms.lst
 DEFSYMFILES += kernel_syms.lst
 
-kernel_elf_HEADERS = arg.h boot.h device.h disk.h dl.h elf.h env.h err.h \
-	file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h symbol.h \
-	term.h types.h sparc64/libgcc.h loader.h \
-	partition.h pc_partition.h ieee1275/ieee1275.h machine/time.h \
-	machine/kernel.h
+kernel_elf_HEADERS = arg.h boot.h cache.h device.h disk.h dl.h elf.h elfload.h \
+	env.h err.h file.h fs.h kernel.h misc.h mm.h net.h parser.h rescue.h \
+	symbol.h term.h types.h sparc64/libgcc.h loader.h partition.h \
+	pc_partition.h ieee1275/ieee1275.h machine/time.h machine/kernel.h
 
 kernel_elf_symlist.c: $(addprefix include/grub/,$(kernel_elf_HEADERS)) config.h gensymlist.sh
 	/bin/sh gensymlist.sh $(filter %.h,$^) > $@ || (rm -f $@; exit 1)
include/grub/elfload.h new file +55 -0

55 lines not on this page · raw message

kern/elf.c new file +387 -0

387 lines not on this page · raw message

conf/common.rmk +6 -1
Index: grub2-cvs/conf/common.rmk
===================================================================
@@ -213,7 +213,12 @@ blocklist_mod_CFLAGS = $(COMMON_CFLAGS)
 blocklist_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
 # Misc.
-pkgdata_MODULES += gzio.mod 
+pkgdata_MODULES += gzio.mod elf.mod
+
+# For elf.mod.
+elf_mod_SOURCES = kern/elf.c
+elf_mod_CFLAGS = $(COMMON_CFLAGS)
+elf_mod_LDFLAGS = $(COMMON_LDFLAGS)
 
 # For gzio.mod.
 gzio_mod_SOURCES = io/gzio.c
include/grub/types.h +2 -0
Index: grub2-cvs/include/grub/types.h
===================================================================
@@ -23,6 +23,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
 # define GRUB_CPU_SIZEOF_LONG	SIZEOF_LONG
loader/powerpc/ieee1275/linux.c +105 -99

272 lines not on this page · raw message

#1 lore raw

include/grub/types.h line 27

Quoted 8 lines · Hollis Blanchard
include/grub/types.h +2 -0
@@ -23,6 +23,8 @@
 #include <config.h>
 #include <grub/cpu/types.h>
 
+#define __unused __attribute__ ((unused))
+

First when I saw this it made me wanna comment on it, and tell you that defines should all be in uppercase. But then I poked through the code, and saw the usage of it, and it looked quite nice, with the exception of the __-prefix. There are a lot of "common" words that are keywords in C; inline, const and auto for example. Maybe we should make "unused" a reserved keyword in GRUB? That would allow us to write code like;

  int
  grub_foo (int x, unused int foo)
  {
    ...
  }

Just a thought. Otherwise your patch looked just fine Hollis.

~j

#2 lore raw

Re: __unused

Quoted 8 lines · Johan Rydberg
Quoted 6 lines · Hollis Blanchard
include/grub/types.h +0 -0
@@ -23,6 +23,8 @@
 #include <config.h>
 #include <grub/cpu/types.h>
 
Quoted 10 lines

include/grub/types.h line 27

Quoted 2 lines
include/grub/types.h +2 -0
+#define __unused __attribute__ ((unused))
+

First when I saw this it made me wanna comment on it, and tell you that defines should all be in uppercase. But then I poked through the code, and saw the usage of it, and it looked quite nice, with the exception of the __-prefix. There are a lot of "common" words that are keywords in C; inline, const and auto for example. Maybe we should make "unused" a reserved keyword in GRUB? That would allow us to write code like;

I left it as "__unused" for now, to match Linux's usage. (We can change it later if needed.)

Quoted 5 lines
  int
  grub_foo (int x, unused int foo)
  {
    ...
  }

I believe gcc attributes can't be used as naturally as this; it would have to be "int unused foo" or "int unused *foo" (from memory).

-Hollis

#3 lore raw

Re: __unused

Quoted 8 lines · Hollis Blanchard
Quoted 5 lines
  int
  grub_foo (int x, unused int foo)
  {
    ...
  }

I believe gcc attributes can't be used as naturally as this; it would have to be "int unused foo" or "int unused *foo" (from memory).

jrydberg@fauna:~/tmp/t5 $ gcc -Dunused= -Wall -Wunused-parameter -O2 -c t1.c t1.c: In function 'foo':
t1.c:6: warning: unused parameter 'x'
t1.c:6: warning: unused parameter 'baz'
jrydberg@fauna:~/tmp/t5 $ gcc -Wall -Wunused-parameter -O2 -c t1.c t1.c: In function 'foo':
t1.c:6: warning: unused parameter 'x'
jrydberg@fauna:~/tmp/t5 $ cat t1.c
#ifndef unused

# define unused __attribute__((unused))
#endif
int
foo (int x, unused int baz)
{
  return 0;
}

~j

#4 lore raw

Re: __unused

In reply to Hollis Blanchard

At Fri, 03 Nov 2006 14:45:52 -0600,

Quoted 21 lines · Hollis Blanchard
Quoted 8 lines · Johan Rydberg
Quoted 6 lines · Hollis Blanchard
include/grub/types.h +0 -0
@@ -23,6 +23,8 @@
 #include <config.h>
 #include <grub/cpu/types.h>
 
Quoted 10 lines

include/grub/types.h line 27

Quoted 2 lines
include/grub/types.h +2 -0
+#define __unused __attribute__ ((unused))
+

First when I saw this it made me wanna comment on it, and tell you that defines should all be in uppercase. But then I poked through the code, and saw the usage of it, and it looked quite nice, with the exception of the __-prefix. There are a lot of "common" words that are keywords in C; inline, const and auto for example. Maybe we should make "unused" a reserved keyword in GRUB? That would allow us to write code like;

Quoted 2 lines

I left it as "__unused" for now, to match Linux's usage. (We can change it later if needed.)

We need another name, the __unused name is used in stat.h for an unused member in struct stat. Grub-setup gives the following compiler error:

gcc -Iutil/i386/pc -I/home/jeroen/cvs/grub2/util/i386/pc -I. -Iinclude -I/home/jeroen/cvs/grub2/include -Wall -W -DGRUB_LIBDIR=\"/usr/local/lib/grub/i386-pc\" -g -O2 -DGRUB_UTIL=1 -MD -c -o grub_setup-util_i386_pc_grub_setup.o /home/jeroen/cvs/grub2/util/i386/pc/grub-setup.c In file included from /usr/include/sys/stat.h:105,

                 from /home/jeroen/cvs/grub2/util/i386/pc/grub-setup.c:43:

/usr/include/bits/stat.h:103: error: expected identifier or ‘(’ before ‘[’ token /usr/include/bits/stat.h:164: error: expected identifier or ‘(’ before ‘[’ token
make: *** [grub_setup-util_i386_pc_grub_setup.o] Error 1

Any ideas for a new name for the define? Something like "grub_unused"?

Jeroen Dekkers

#5 lore raw

Re: __unused

Quoted 1 line · Jeroen Dekkers

Any ideas for a new name for the define? Something like "grub_unused"?

"UNUSED" ?

~j

#6 lore raw

Re: __unused

At Thu, 09 Nov 2006 16:31:58 +0100,

Quoted 6 lines · Johan Rydberg
Quoted 1 line · Jeroen Dekkers

Any ideas for a new name for the define? Something like "grub_unused"?

"UNUSED" ?

After a small discussion with Hollis on IRC I changed it to "UNUSED".

Jeroen Dekkers

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

Here is pass #3 of the ELF loader. Changes from #2:
- the `load' callback no longer modifies the phdr directly

I still don't understand why you want to pass a hook. Why don't you simply pass a load address?

Okuji

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

Here is pass #3 of the ELF loader. Changes from #2:
- the `load' callback no longer modifies the phdr directly

I still don't understand why you want to pass a hook. Why don't you simply pass a load address?

How do I know what the load address should be before I've even looked at the program header?

Just because the x86 loader uses fixed values doesn't mean other loaders should. In particular, the PowerPC loader is happy to load the ELF file wherever it likes, as long as that memory isn't in use.

-Hollis

#9 lore raw
Quoted 7 lines · Hollis Blanchard

How do I know what the load address should be before I've even looked at the program header?

Just because the x86 loader uses fixed values doesn't mean other loaders should. In particular, the PowerPC loader is happy to load the ELF file wherever it likes, as long as that memory isn't in use.

Note that some modification is necessary. Please look at offset_phdr() again to see what I'm talking about.

-Hollis