LAKITU: grub: Strip .interp from diskboot

boot.img and diskboot.img both need to be smaller than 512 bytes. Clang
inserts a .interp section into these binaries, which brings the binary
size over 512 bytes. Let's strip the .interp section since it is not
needed and breaks grub-install.

We cannot remove the .interp section from all build targets of type
"image" in grub, because some images are very much not
position-independent. In particular, the LZMA decompressor relies on the
symbol "L_decompressor_end" referencing the end of the LZMA decompressor
image in memory. If we remove the .interp section after linking, the
value for "L_decompressor_end" that was filled in everywhere in the
program by the linker will be invalid, because the "L_decompressor_end"
value initially accounted for the .interp section. In this case, we can
leave the .interp section in to keep the program correct. The LZMA
decompressor also has no 512 byte requirement, so we don't need to
remove .interp from it.

After we land the new ChromeOS version in COS, I'll follow up with
ChromeOS on how best to upstream this fix. It might make sense to update
clang/lld to not insert .interp if -nostdlib is provided.

TEST=local BE run

Change-Id: I99a0177bdc7877d676e5037ad24497c51f107aaf
Reviewed-by: Robert Kolchmeyer <>
Tested-by: Robert Kolchmeyer <>
diff --git a/sys-boot/grub/files/0005-strip-interp-from-diskboot.patch b/sys-boot/grub/files/0005-strip-interp-from-diskboot.patch
new file mode 100644
index 0000000..492f468
--- /dev/null
+++ b/sys-boot/grub/files/0005-strip-interp-from-diskboot.patch
@@ -0,0 +1,20 @@
+--- grub-core/Makefile.core.def	2021-07-06 23:58:33.094383161 -0700
++++ grub-core/Makefile.core.def	2021-07-06 23:59:01.142164648 -0700
+@@ -372,7 +372,7 @@
+   sparc64_ieee1275_objcopyflags = '-O a.out-sunos-big';
+   sparc64_ieee1275_ldflags = ' -Wl,-Ttext=0x4000';
+-  objcopyflags = '-O binary';
++  objcopyflags = '-R .interp -O binary';
+   enable = i386_pc;
+   enable = i386_qemu;
+   enable = sparc64_ieee1275;
+@@ -430,7 +430,7 @@
+   sparc64_ieee1275 = boot/sparc64/ieee1275/diskboot.S;
+   sparc64_ieee1275_ldflags = '-Wl,-Ttext=0x4200';
+-  objcopyflags = '-O binary';
++  objcopyflags = '-R .interp -O binary';
+   enable = i386_pc;
+   enable = sparc64_ieee1275;
diff --git a/sys-boot/grub/grub-2.02-r7.ebuild b/sys-boot/grub/grub-2.02-r8.ebuild
similarity index 100%
rename from sys-boot/grub/grub-2.02-r7.ebuild
rename to sys-boot/grub/grub-2.02-r8.ebuild
diff --git a/sys-boot/grub/grub-2.02.ebuild b/sys-boot/grub/grub-2.02.ebuild
index 42302a8..54dbd59 100644
--- a/sys-boot/grub/grub-2.02.ebuild
+++ b/sys-boot/grub/grub-2.02.ebuild
@@ -26,6 +26,7 @@
 	epatch "${FILESDIR}/0002-Forward-port-gptpriority-command-to-GRUB-2.00.patch"
 	epatch "${FILESDIR}/0003-tsc-Change-default-tsc-calibration-method-to-pmtimer.patch"
 	epatch "${FILESDIR}/0004-grub-0x4-relocation.patch"
+	epatch "${FILESDIR}/0005-strip-interp-from-diskboot.patch"
 	epatch "${FILESDIR}/b189992601-no-soft-float.patch"
 	bash || die