--- grub-mkimage.c.orig 2007-10-05 20:33:50.000000000 +0000 +++ grub-mkimage.c 2007-10-15 18:20:03.000000000 +0000 @@ -104,7 +104,7 @@ FILE *in; char *kernel_path; grub_addr_t grub_end = 0; - off_t phdroff; + off_t phdroff, pstart; int i; /* Read ELF header. */ @@ -115,18 +115,27 @@ grub_util_read_at (&ehdr, sizeof (ehdr), 0, in); + /* The +1 is to leave a hole for the program header for extra modules. */ phdrs = xmalloc (grub_le_to_cpu16 (ehdr.e_phentsize) - * (grub_le_to_cpu16 (ehdr.e_phnum) + 2)); + * (grub_le_to_cpu16 (ehdr.e_phnum) + 1)); + + /* Append entire segment table to the file. */ + //phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long)); + phdroff = ALIGN_UP (sizeof(ehdr), sizeof (long)); + grub_util_write_image_at (phdrs, grub_le_to_cpu16 (ehdr.e_phentsize) + *(grub_le_to_cpu16 (ehdr.e_phnum) + 1), phdroff, + out); + /* Copy all existing segments. */ grub_util_info ("%u segments", grub_le_to_cpu16 (ehdr.e_phnum)); + pstart = grub_util_get_fp_size (out); + /* set up first phdr offset */ for (i = 0; i < grub_le_to_cpu16 (ehdr.e_phnum); i++) { char *segment_img; grub_size_t segment_end; - phdr = phdrs + i; - - /* Read segment header. */ + /* Read segment header. */ grub_util_read_at (phdr, sizeof (Elf32_Phdr), (grub_le_to_cpu32 (ehdr.e_phoff) + (i * grub_le_to_cpu16 (ehdr.e_phentsize))), @@ -146,9 +155,11 @@ grub_util_read_at (segment_img, grub_le_to_cpu32 (phdr->p_filesz), grub_le_to_cpu32 (phdr->p_offset), in); - grub_util_write_image_at (segment_img, grub_le_to_cpu32 (phdr->p_filesz), + phdr->p_offset = grub_cpu_to_le32(pstart); + grub_util_write_image_at (segment_img, grub_le_to_cpu32 (phdr->p_filesz), grub_le_to_cpu32 (phdr->p_offset), out); + pstart += phdr->p_filesz; free (segment_img); } @@ -176,9 +187,9 @@ ehdr.e_shstrndx = 0; /* Append entire segment table to the file. */ - phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long)); + // phdroff = ALIGN_UP (grub_util_get_fp_size (out), sizeof (long)); grub_util_write_image_at (phdrs, grub_le_to_cpu16 (ehdr.e_phentsize) - * grub_le_to_cpu16 (ehdr.e_phnum), phdroff, + * (grub_le_to_cpu16 (ehdr.e_phnum)), phdroff, out); /* Write ELF header. */