Opened 5 years ago

Closed 5 years ago

#4354 closed enhancement (fixed)

grub / UEFI / binutils

Reported by: thomas Owned by: lfs-book
Priority: normal Milestone: 8.4
Component: Book Version: SVN
Severity: normal Keywords:
Cc:

Description

Kevin Buckley posted on lfs-dev ("Possible patch for Grub 2.02"):


This patch may or may not be needed for Grub, as installed in the LFS book, however it is a patch to the sources that was required when I rebuilt Grub for a UEFI system, as per the UEFI hint, on a machine that has an "AMD A8-6600K APU with Radeon(tm) HD Graphics" as the CPU.

Having built an unpatched Grub as follows

         cd grub-2.02/

         ./configure --prefix=/usr  \
            --sbindir=/sbin        \
            --sysconfdir=/etc      \
            --disable-efiemu       \
            --enable-grub-mkfont   \
            --with-platform=efi    \
            --target=x86_64        \
            --program-prefix=""    \
            --with-bootdir="/boot" \
            --with-grubdir="grub" \
            --disable-werror

         make 2>&1 | tee ../make_uefi.out

         make install 2>&1 | tee ../make_uefi_install.out

coming to do the grub-install, vis:

grub-install \
  --target=x86_64-efi --efi-directory=/boot/efi \
  --bootloader-id=lfs --recheck --debug 2>&1  | \
  tee 20181006-grub-install01.txt

failed, telling me,

...
grub-install: info: locating grub_memalign at 0x4096 (0x400).
grub-install: info: dealing with the relocation section .rela.text for .text.
grub-install: info: relocating an R_X86_64_PC32 entry to 0xe3b1 at the
offset 0x3.
grub-install: info: relocating an R_X86_64_PC32 entry to 0xe3a2 at the
offset 0xa.
grub-install: error: relocation 0x4 is not implemented yet.

Having followed my nose across the interweb thing, through these sites

https://forum.manjaro.org/t/solved-cant-install-grub-after-trying-to-get-grub-silent/58046

https://aur.archlinux.org/packages/grub-luks-keyfile/

https://www.mail-archive.com/openembedded-core@lists.openembedded.org/msg112375.html

I arrived at

http://git.savannah.gnu.org/cgit/grub.git/commit/util?id=842c390469e2c2e10b5aa36700324cd3bde25875,

which says the following and provides a patch

x86-64: Treat R_X86_64_PLT32 as R_X86_64_PC32

Starting from binutils commit bd7ab16b4537788ad53521c45469a1bdae84ad4a:

https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=bd7ab16b4537788ad53521c45469a1bdae84ad4a

x86-64 assembler generates R_X86_64_PLT32, instead of R_X86_64_PC32, for 32-bit PC-relative branches.

Grub2 should treat R_X86_64_PLT32 as R_X86_64_PC32.

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a2bb054..39d7efb 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -841,6 +841,7 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, Elf_Shdr
*sections,
           break;

         case R_X86_64_PC32:
+        case R_X86_64_PLT32:
           {
             grub_uint32_t *t32 = (grub_uint32_t *) target;
             *t32 = grub_host_to_target64 (grub_target_to_host32 (*t32)
diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index 9179285..a79271f 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -19,6 +19,7 @@ struct grub_module_verifier_arch archs[] = {
       -1
     }, (int[]){
       R_X86_64_PC32,
+      R_X86_64_PLT32,
       -1
     }
   },

Applying that patch

patch -Np1 -i ../treat-R_X86_64_PLT32-as-R_X86_64_PC32.patch

before the configure allowed me to build my UEFI-aware Grub and perform the grub-install without issue.

grub-install: info: locating grub_memalign at 0x4096 (0x400).
grub-install: info: dealing with the relocation section .rela.text for .text.
grub-install: info: relocating an R_X86_64_PC32 entry to 0xe3b1 at the
offset 0x3.
grub-install: info: relocating an R_X86_64_PC32 entry to 0xe3a2 at the
offset 0xa.
grub-install: info: relocating an R_X86_64_PC32 entry to 0x7222 at the
offset 0x13.
grub-install: info: relocating an R_X86_64_64 entry to 0xe7b0 at the
offset 0x33.
....
.. and it trundles on, doing a lot more reloacating, until ...
...
grub-install: info: adding a relocation entry for 0xc9d8.
grub-install: info: adding 221 padding fixup entries.
grub-install: info: writing 872 bytes of a fixup block starting at 0xc000.
grub-install: info: reading /usr/lib/grub/x86_64-efi/fshelp.mod.
grub-install: info: reading /usr/lib/grub/x86_64-efi/ext2.mod.
grub-install: info: reading /usr/lib/grub/x86_64-efi/part_gpt.mod.
grub-install: info: kernel_img=0xd0aa60, kernel_size=0x18600.
grub-install: info: the core size is 0x1c4f0.
grub-install: info: writing 0x1d800 bytes.
grub-install: info: copying `/boot/grub/x86_64-efi/core.efi' -> `/boot/efi/EFI/l
fs/grubx64.efi'.
grub-install: info: Registering with EFI: distributor = `lfs', path = `\EFI\lfs\
grubx64.efi', ESP at hostdisk//dev/sda,gpt1.
grub-install: info: executing efibootmgr --version </dev/null >/dev/null.
grub-install: info: executing modprobe -q efivars.
grub-install: info: executing efibootmgr -b 0002 -B.
BootCurrent: 0001
Timeout: 15 seconds
BootOrder: 0000,0005,0004,0001,0006
Boot0000* ubuntu
Boot0001* CentOS
Boot0004* CD/DVD Drive
Boot0005* grub
Boot0006* Hard Drive
grub-install: info: executing efibootmgr -c -d /dev/sda -p 1 -w -L lfs
-l \EFI\lfs\grubx64.efi.
BootCurrent: 0001
Timeout: 15 seconds
BootOrder: 0002,0000,0005,0004,0001,0006
Boot0000* ubuntu
Boot0001* CentOS
Boot0004* CD/DVD Drive
Boot0005* grub
Boot0006* Hard Drive
Boot0002* lfs
Installation finished. No error reported.

however, as the patch appears not be UEFI-specific but appears to be tied to the way Grub 2.02 may see some systems/CPUs, I believe it might be worth adding into the LFS instructions.

Hoping that's useful to someone: it was certainly useful to me.


Maybe the patch could be implemented as a sed.

Attachments (1)

lfs-uefi-20180409-relocation_fix.patch (1.3 KB ) - added by Xi Ruoyao 5 years ago.
Proposed change to the hint

Download all attachments as: .zip

Change History (8)

comment:1 by Bruce Dubbs, 5 years ago

Can you test:

sed -i '/R_X86_64_PC32/a case R_X86_64_PLT32:' util/grub-mkimagexx.c

comment:2 by Pierre Labastie, 5 years ago

I'd be amazed if it works. There are two occurrences of R_X86_64_PC32. The second one is not in a case statement. Should be:

sed -e '/R_X86_64_PC32:/a case R_X86_64_PLT32:' \
    -e '/R_X86_64_PC32,/a R_X86_64_PLT32,' \
    -i util/grub-mkimagexx.c

comment:3 by Bruce Dubbs, 5 years ago

I was WAY too casual. However we are both wrong. It should be:

sed -i '/R_X86_64_PC32:/a case R_X86_64_PLT32:' util/grub-mkimagexx.c
sed -i '/R_X86_64_PC32,/a R_X86_64_PLT32,'      util/grub-module-verifier.c

comment:4 by thomas, 5 years ago

David Sprayberry confirmed that the patch works (he reported the same issue independently in lfs-support on the same day and i asked him to test the patch suggested by Kevin).

Seems so that one with write access to LFS could add the two seds to grub.xml

comment:5 by Xi Ruoyao, 5 years ago

As the lfs-uefi hint maintainer (currently) I'll test it and add the patch to the hint.

comment:6 by Xi Ruoyao, 5 years ago

I uploaded the Grub patch rebased for 2.02 to https://bf.mengyan1223.wang/assets/patches/grub-2.02-relocation_fix-1.patch. We should add this patch into LFS patches and update the URL in the hint.

by Xi Ruoyao, 5 years ago

Proposed change to the hint

comment:7 by Bruce Dubbs, 5 years ago

Resolution: fixed
Status: newclosed

I added the patch and updated the hint.

Note: See TracTickets for help on using tickets.