Skip to contentSkip to conversation

another RFC patch: bzImage with ELF payload

PATCH Jeremy Fitzhardinge 3 participants 6 messages Jun 1, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

OK, here's another go-around. This patch leaves the bzImage itself unmodified, but it changes the payload into an ELF file. That is, the 32-bit decompression/relocation+compressed kernel is now a properly formed ELF file.

One thing that fell out of this is that code32_start end up being a pointer to the ELF header rather than an entrypoint. Rather than reproducing Vivek's (?) hack of making the ELF header itself executable, I changed the 16-bit code to check for an ELF magic number at code32_start and use the e_entry to get the actual entrypoint. This seems like approximately the right way of doing this, but I'm not sure how we want to formalize it. It's certainly easier than trying to extract the payload's entry address and copying it to code32_start in the boot_params block, and we need a pointer to the ELF file itself anyway.

Thoughts?

BTW, this won't apply as-is; I have some mucking-around patches to try and get the linux/elf*.h headers into a bit more order, but that's not ready yet.

J

---
 arch/i386/boot/Makefile               |   21 +---
 arch/i386/boot/compressed/Makefile    |   13 +-
 arch/i386/boot/compressed/notes.S     |    7 +
 arch/i386/boot/compressed/piggy.S     |   10 +
 arch/i386/boot/compressed/vmlinux.lds |   15 ++
 arch/i386/boot/compressed/vmlinux.scr |   10 -
 arch/i386/boot/header.S               |    4 
 arch/i386/boot/payload.S              |    4 
 arch/i386/boot/setup.ld               |   27 ++++-
 arch/i386/boot/tools/.gitignore       |    1 
 arch/i386/boot/tools/build.c          |  168 ---------------------------------
 include/linux/elf.h                   |   20 +++
 include/linux/elf_boot.h              |   16 +++
 13 files changed, 105 insertions(+), 211 deletions(-)

===================================================================

arch/i386/boot/Makefile +8 -13
@@ -31,6 +31,7 @@ setup-y		+= a20.o apm.o cmdline.o copy.o
 setup-y		+= a20.o apm.o cmdline.o copy.o cpu.o cpucheck.o edd.o
 setup-y		+= header.o main.o mca.o memory.o pm.o pmjump.o
 setup-y		+= printf.o string.o tty.o video.o version.o voyager.o
+setup-y		+= payload.o
 
 # The link order of the video-*.o modules can matter.  In particular,
 # video-vga.o *must* be listed first, followed by video-vesa.o.
@@ -39,10 +40,6 @@ setup-y		+= video-vga.o
 setup-y		+= video-vga.o
 setup-y		+= video-vesa.o
 setup-y		+= video-bios.o
-
-hostprogs-y	:= tools/build
-
-HOSTCFLAGS_build.o := $(LINUXINCLUDE)
 
 # ---------------------------------------------------------------------------
 
@@ -65,15 +62,10 @@ AFLAGS		:= $(CFLAGS) -D__ASSEMBLY__
 $(obj)/bzImage: IMAGE_OFFSET := 0x100000
 $(obj)/bzImage: EXTRA_CFLAGS := -D__BIG_KERNEL__
 $(obj)/bzImage: EXTRA_AFLAGS := $(SVGA_MODE) $(RAMDISK) -D__BIG_KERNEL__
-$(obj)/bzImage: BUILDFLAGS   := -b
 
-quiet_cmd_image = BUILD   $@
-cmd_image = $(obj)/tools/build $(BUILDFLAGS) $(obj)/setup.bin \
-	    $(obj)/vmlinux.bin $(ROOT_DEV) > $@
+all: $(obj)/bzImage
 
-$(obj)/zImage $(obj)/bzImage: $(obj)/setup.bin \
-			      $(obj)/vmlinux.bin $(obj)/tools/build FORCE
-	$(call if_changed,image)
+$(obj)/zImage: FORCE
 	@echo 'Kernel: $@ is ready' ' (#'`cat .version`')'
 
 $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE
@@ -81,8 +73,11 @@ cmd_image = $(obj)/tools/build $(BUILDFL
 
 SETUP_OBJS = $(addprefix $(obj)/,$(setup-y))
 
-LDFLAGS_setup.elf	:= -T
-$(obj)/setup.elf: $(src)/setup.ld $(SETUP_OBJS) FORCE
+$(obj)/payload.o:	EXTRA_AFLAGS := -Wa,-I$(obj)
+$(obj)/payload.o: $(src)/kernel.S $(obj)/vmlinux.bin
+
+LDFLAGS_bzImage	:= -T
+$(obj)/bzImage: $(src)/setup.ld $(SETUP_OBJS) FORCE
 	$(call if_changed,ld)
 
 OBJCOPYFLAGS_setup.bin	:= -O binary

===================================================================

arch/i386/boot/compressed/Makefile +7 -6
@@ -4,7 +4,8 @@
 # create a compressed vmlinux image from the original vmlinux
 #
 
-targets		:= vmlinux vmlinux.bin vmlinux.bin.gz head.o misc.o piggy.o \
+targets		:= vmlinux vmlinux.bin vmlinux.bin.gz \
+			elfhdr.o head.o misc.o notes.o piggy.o \
 			vmlinux.bin.all vmlinux.relocs
 EXTRA_AFLAGS	:= -traditional
 
@@ -17,7 +18,9 @@ CFLAGS  := -m32 -D__KERNEL__ $(LINUX_INC
 	   $(call cc-option,-fno-stack-protector)
 LDFLAGS := -m elf_i386
 
-$(obj)/vmlinux: $(src)/vmlinux.lds $(obj)/head.o $(obj)/misc.o $(obj)/piggy.o FORCE
+OBJS=$(addprefix $(obj)/,elfhdr.o head.o misc.o piggy.o)
+
+$(obj)/vmlinux: $(src)/vmlinux.lds $(OBJS) FORCE
 	$(call if_changed,ld)
 	@:
 
@@ -44,7 +47,5 @@ else
 	$(call if_changed,gzip)
 endif
 
-LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
-
-$(obj)/piggy.o: $(src)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
-	$(call if_changed,ld)
+$(obj)/piggy.o:	EXTRA_AFLAGS := -Wa,-I$(obj)
+$(obj)/piggy.o: $(obj)/vmlinux.bin.gz

===================================================================

arch/i386/boot/compressed/notes.S new file +7 -0
@@ -0,0 +1,7 @@
+#include <linux/elfnote.h>
+#include <linux/elf_boot.h>
+#include <linux/utsrelease.h>
+
+ELFNOTE(ELF_NOTE_BOOT, EIN_PROGRAM_NAME, .asciz "Linux")
+ELFNOTE(ELF_NOTE_BOOT, EIN_PROGRAM_VERSION, .asciz UTS_RELEASE)
+ELFNOTE(ELF_NOTE_BOOT, EIN_ARGUMENT_STYLE, .asciz "Linux")

===================================================================

arch/i386/boot/compressed/piggy.S new file +10 -0
@@ -0,0 +1,10 @@
+.section .data.compressed,"a",@progbits
+
+.globl input_data, input_len, output_len
+
+input_len:	.long input_data_end - input_data
+
+input_data:
+.incbin "vmlinux.bin.gz"
+output_len = .-4
+input_data_end:

===================================================================

arch/i386/boot/compressed/vmlinux.lds +11 -4
@@ -1,13 +1,12 @@ OUTPUT_FORMAT("elf32-i386", "elf32-i386"
-OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+/* OUTPUT_FORMAT("binary") */
+OUTPUT_FORMAT("elf32-i386")
 OUTPUT_ARCH(i386)
 ENTRY(startup_32)
 SECTIONS
 {
-        /* Be careful parts of head.S assume startup_32 is at
-         * address 0.
-	 */
 	. =  0 	;
 	.text.head : {
+		*(.elfhdr)
 		_head = . ;
 		*(.text.head)
 		_ehead = . ;
@@ -33,6 +32,7 @@ SECTIONS
 		*(.data.*)
 		_edata = . ;
 	}
+	_filesz = . ;
 	.bss : {
 		_bss = . ;
 		*(.bss)
@@ -40,4 +40,11 @@ SECTIONS
 		*(COMMON)
 		_end = . ;
 	}
+	_memsz = .;
+
+	.notes : {
+		_notes = . ;
+		*(.note*)
+		_notesz = . - _notes ;
+	}
 }

===================================================================

arch/i386/boot/compressed/vmlinux.scr deleted +0 -10
@@ -1,10 +0,0 @@
-SECTIONS
-{
-  .data.compressed : {
-	input_len = .;
-	LONG(input_data_end - input_data) input_data = .; 
-	*(.data) 
-	output_len = . - 4;
-	input_data_end = .; 
-	}
-}

===================================================================

arch/i386/boot/header.S +2 -2
@@ -97,9 +97,9 @@ bugger_off_msg:
 	.section ".header", "a"
 	.globl	hdr
 hdr:
-setup_sects:	.byte SETUPSECTS
+setup_sects:	.byte __setup_sects
 root_flags:	.word ROOT_RDONLY
-syssize:	.long SYSSIZE
+syssize:	.long kernel_size_para
 ram_size:	.word RAMDISK
 vid_mode:	.word SVGA_MODE
 root_dev:	.word ROOT_DEV

===================================================================

arch/i386/boot/payload.S new file +4 -0
@@ -0,0 +1,4 @@
+.section .kernel,"a",@progbits
+.balign 4096
+
+.incbin "vmlinux.bin"

===================================================================

arch/i386/boot/setup.ld +23 -4
@@ -3,7 +3,7 @@
  *
  * Linker script for the i386 setup code
  */
-OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386")
+OUTPUT_FORMAT("binary")
 OUTPUT_ARCH(i386)
 ENTRY(_start)
 
@@ -15,6 +15,7 @@ SECTIONS
 
 	. = 497;
 	.header		: { *(.header) }
+	_text = .;
 	.inittext	: { *(.inittext) }
 	.initdata	: { *(.initdata) }
 	.text		: { *(.text*) }
@@ -36,6 +37,7 @@ SECTIONS
 		LONG(0x5a5aaa55)
 	}
 
+	_filesz = . - _text;
 
 	. = ALIGN(16);
 	__bss_start = .;
@@ -46,8 +48,25 @@ SECTIONS
 	. = ALIGN(16);
 	_end = .;
 
-	/DISCARD/ : { *(.note*) }
+	_memsz = . - _text;
 
-	. = ASSERT(_end <= 0x8000, "Setup too big!");
-	. = ASSERT(hdr == 0x1f1, "The setup header has the wrong offset!");
+	__setup_sects = . / 512;
+
+	_ = ASSERT(_end <= 0x8000, "Setup too big!");
+	_ = ASSERT(hdr == 0x1f1, "The setup header has the wrong offset!");
+
+	.notes		: {
+		notes = .;
+		*(.note*)
+		notes_end = .;
+	}
+	notes_size = notes_end - notes;
+
+	.kernel		: {
+		kernel = .;
+		*(.kernel)
+		kernel_end = .;
+		kernel_size = kernel_end - kernel;
+		kernel_size_para = (kernel_size + 15) / 16;
+	}
 }

===================================================================

arch/i386/boot/tools/.gitignore deleted +0 -1
@@ -1,1 +0,0 @@
-build

===================================================================

arch/i386/boot/tools/build.c deleted +0 -168

168 lines not on this page · raw message

===================================================================

include/linux/elf.h +17 -3
@@ -1,9 +1,10 @@
 #ifndef _LINUX_ELF_H
 #define _LINUX_ELF_H
 
+#include <linux/elf-em.h>
+#ifndef __ASSEMBLY__
 #include <linux/types.h>
 #include <linux/auxvec.h>
-#include <linux/elf-em.h>
 #include <asm/elf.h>
 #include <asm-generic/module.h>
 
@@ -32,6 +33,7 @@ typedef __u32	Elf64_Word;
 typedef __u32	Elf64_Word;
 typedef __u64	Elf64_Xword;
 typedef __s64	Elf64_Sxword;
+#endif	/* __ASSEMBLY__ */
 
 /* These constants are for the segment types stored in the image headers */
 #define PT_NULL    0
@@ -124,6 +126,7 @@ typedef __s64	Elf64_Sxword;
 #define ELF64_ST_BIND(x)	ELF_ST_BIND(x)
 #define ELF64_ST_TYPE(x)	ELF_ST_TYPE(x)
 
+#ifndef __ASSEMBLY__
 struct elf32_dyn {
   Elf32_Sword d_tag;
   union{
@@ -139,6 +142,7 @@ struct elf64_dyn {
     Elf64_Addr d_ptr;
   } d_un;
 };
+#endif	/* __ASSEMBLY__ */
 
 /* The following are used with relocations */
 #define ELF32_R_SYM(x) ((x) >> 8)
@@ -147,6 +151,7 @@ struct elf64_dyn {
 #define ELF64_R_SYM(i)			((i) >> 32)
 #define ELF64_R_TYPE(i)			((i) & 0xffffffff)
 
+#ifndef __ASSEMBLY__
 struct elf32_rel {
   Elf32_Addr	r_offset;
   Elf32_Word	r_info;
@@ -186,10 +191,11 @@ struct elf64_sym {
   Elf64_Addr st_value;		/* Value of the symbol */
   Elf64_Xword st_size;		/* Associated symbol size */
 };
-
+#endif	/* __ASSEMBLY__ */
 
 #define EI_NIDENT	16
 
+#ifndef __ASSEMBLY__
 struct elf32_hdr {
   unsigned char	e_ident[EI_NIDENT];
   Elf32_Half	e_type;
@@ -223,6 +229,7 @@ struct elf64_hdr {
   Elf64_Half e_shnum;
   Elf64_Half e_shstrndx;
 };
+#endif	/* __ASSEMBLY__ */
 
 /* These constants define the permissions on sections in the program
    header, p_flags. */
@@ -230,6 +237,7 @@ struct elf64_hdr {
 #define PF_W		0x2
 #define PF_X		0x1
 
+#ifndef __ASSEMBLY__
 struct elf32_phdr {
   Elf32_Word	p_type;
   Elf32_Off	p_offset;
@@ -251,6 +259,7 @@ struct elf64_phdr {
   Elf64_Xword p_memsz;		/* Segment size in memory */
   Elf64_Xword p_align;		/* Segment alignment, file & memory */
 };
+#endif	/* __ASSEMBLY__ */
 
 /* sh_type */
 #define SHT_NULL	0
@@ -285,7 +294,8 @@ struct elf64_phdr {
 #define SHN_ABS		0xfff1
 #define SHN_COMMON	0xfff2
 #define SHN_HIRESERVE	0xffff
- 
+
+#ifndef __ASSEMBLY__
 struct elf32_shdr {
   Elf32_Word	sh_name;
   Elf32_Word	sh_type;
@@ -311,6 +321,7 @@ struct elf64_shdr {
   Elf64_Xword sh_addralign;	/* Section alignment */
   Elf64_Xword sh_entsize;	/* Entry size if section holds table */
 };
+#endif	/* __ASSEMBLY__ */
 
 #define	EI_MAG0		0		/* e_ident[] indexes */
 #define	EI_MAG1		1
@@ -344,6 +355,7 @@ struct elf64_shdr {
 
 #define ELFOSABI_NONE	0
 #define ELFOSABI_LINUX	3
+#define ELFOSABI_STANDALONE	255
 
 #ifndef ELF_OSABI
 #define ELF_OSABI ELFOSABI_NONE
@@ -358,6 +370,7 @@ struct elf64_shdr {
 #define NT_PRXFPREG     0x46e62b7f      /* copied from gdb5.1/include/elf/common.h */
 
 
+#ifndef __ASSEMBLY__
 /* Note header in a PT_NOTE section */
 struct elf32_note {
   Elf32_Word	n_namesz;	/* Name size */
@@ -397,5 +410,6 @@ static inline void arch_write_notes(stru
 #define ELF_CORE_EXTRA_NOTES_SIZE arch_notes_size()
 #define ELF_CORE_WRITE_EXTRA_NOTES arch_write_notes(file)
 #endif /* ARCH_HAVE_EXTRA_ELF_NOTES */
+#endif	/* __ASSEMBLY__ */
 
 #endif /* _LINUX_ELF_H */

===================================================================

include/linux/elf_boot.h new file +16 -0
@@ -0,0 +1,16 @@
+#ifndef ELF_BOOT_H
+#define ELF_BOOT_H
+
+/* Elf notes to help bootloaders identify what program they are booting.
+ */
+
+/* Standardized Elf image notes for booting... The name for all of these is ELFBoot */
+#define ELF_NOTE_BOOT		ELFBoot
+
+#define EIN_PROGRAM_NAME	1 /* The program in this ELF file */
+#define EIN_PROGRAM_VERSION	2 /* The version of the program in this ELF file */
+#define EIN_PROGRAM_CHECKSUM	3 /* ip style checksum of the memory image. */
+#define EIN_ARGUMENT_STYLE	4 /* String identifying argument passing style */
+#define EIN_BOOT_PARAMS		5 /* Offset of boot_params block */
+
+#endif /* ELF_BOOT_H */
#1 lore raw
Quoted 4 lines · Jeremy Fitzhardinge

BTW, this won't apply as-is; I have some mucking-around patches to try and get the linux/elf*.h headers into a bit more order, but that's not ready yet.

BTW2: I've been basically ignoring zImage. Do we need to support it at all? Has anyone used it in the last 10 years?

J

#2 lore raw
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 4 lines · Jeremy Fitzhardinge

BTW, this won't apply as-is; I have some mucking-around patches to try and get the linux/elf*.h headers into a bit more order, but that's not ready yet.

BTW2: I've been basically ignoring zImage. Do we need to support it at all? Has anyone used it in the last 10 years?

zImage is uninteresting. I think HPA explicitly dropped support a few years ago so we can't even build it.

That said a bImage (non-compressed bzImage). Might actually be interesting.

The kernel image history is:

Image:   -- noncompressed image loaded  below 1M.
zImage:  -- compressed image loaded below 1M.
bzImage: -- compressed image loaded above 1M.

For people who want to compress it themselves a
bImage: -- non-compressed image loaded above 1M.
could be interesting.

Eric

#3 lore raw
Quoted 10 lines · Eric W. Biederman
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines · Jeremy Fitzhardinge

BTW, this won't apply as-is; I have some mucking-around patches to try and get the linux/elf*.h headers into a bit more order, but that's not ready yet.

Quoted 7 lines
Quoted 3 lines

BTW2: I've been basically ignoring zImage. Do we need to support it at all? Has anyone used it in the last 10 years?

zImage is uninteresting. I think HPA explicitly dropped support a few years ago so we can't even build it.

Hm, its still there, and he put explicit support for it into the new bootcode rewrite. Doesn't boot for me though.

Quoted 3 lines

That said a bImage (non-compressed bzImage). Might actually be interesting.

That's pretty easy in my current plan. You just glue vmlinux into the image rather than compressor+compressed data.

J

#5 lore raw
In reply to Jeremy Fitzhardinge
Quoted 5 lines · Jeremy Fitzhardinge

OK, here's another go-around. This patch leaves the bzImage itself unmodified, but it changes the payload into an ELF file. That is, the 32-bit decompression/relocation+compressed kernel is now a properly formed ELF file.

Quoted 10 lines

One thing that fell out of this is that code32_start end up being a pointer to the ELF header rather than an entrypoint. Rather than reproducing Vivek's (?) hack of making the ELF header itself executable, I changed the 16-bit code to check for an ELF magic number at code32_start and use the e_entry to get the actual entrypoint. This seems like approximately the right way of doing this, but I'm not sure how we want to formalize it. It's certainly easier than trying to extract the payload's entry address and copying it to code32_start in the boot_params block, and we need a pointer to the ELF file itself anyway.

No, that breaks any boot loader that uses the boot loader hook mechanism. That's definitely broken.

Either way, I still believe that this should be done at the other end of the decompression chain. Give out the information about *how the kernel will be executed*, not about the intermediate, transient step.

-hpa