riscvarchive / riscv-edk2

Port of EDK2 implementation of UEFI to RISC-V. See documentation at:
https://github.com/riscv/riscv-uefi-edk2-docs
Other
22 stars 10 forks source link

Implement GOT relocation type on RISC-V #3

Closed changab closed 3 years ago

changab commented 4 years ago

Refer to edk2 commit d2687f23c909 for relocations 0x13 and 0x14.

JohnAZoidberg commented 3 years ago

It's necessary to implement these relocation types to allow the build with a recent toolchain. See: https://bugzilla.tianocore.org/show_bug.cgi?id=3042#c6

vlsunil commented 3 years ago

I have a probable fix at https://github.com/vlsunil/riscv-edk2/commit/4efe5b4cc3a84c314364367894cf9bacafede438. However, the 10.x toolchain is generating the original ELF file itself incorrectly which is causing the boot failure. Will check with GCC11. Once confirmed, will submit the patch.

vlsunil commented 3 years ago

I have raised an issue to riscv-toolchain (https://github.com/riscv/riscv-gnu-toolchain/issues/905). This issue is causing the boot to hang since the SecMain not able to read the correct global variable "platform" of OpenSBI library. Until we get some solution to the issue, the compiler needs to be built without --with-default-pie option. In that case, even GOT relocations also won't be emitted and this fix is not required.

JohnAZoidberg commented 3 years ago

Awesome, thanks for helping! Sounds like the issue you mention isn't actually in the toolchain but something our relocator has to fix.

vlsunil commented 3 years ago

Yes, Daniel. Clarifications from linker experts helped me to understand this better. I started with GOT relocation but we need to revisit all relocation code in the edk2 toolchain since RISCV is different from other architectures. I fixed R_RISCV_32 and R_RISCV_64 in my latest commit. However, it hangs further during the boot process. Maybe some other relocation we need to fix.

vlsunil commented 3 years ago

OK fixed all the issues. Image built with the latest compiler which has "--enable-default-pie" option, is booting now in qemu with the fix at https://github.com/vlsunil/riscv-edk2/commit/a742fab0d73ab568aa2d2578a5ccfc400ffd2fa5. @changab, @JohnAZoidberg - Could you please review this fix and if possible try on a real board too when you have time?

JohnAZoidberg commented 3 years ago

Perfect, that means it will work with a standard toolchain provided by Fedora or Debian, right? Then we can stop hosting our precompiled toolchain. Sure, we'll try it.

vlsunil commented 3 years ago

That's correct. I tried with debian bullseye.

JohnAZoidberg commented 3 years ago

Can confirm that it works on Debian Buster! Builds RiscvVirt and boots to Linux.

I followed the instructions here: https://wiki.debian.org/RISC-V

$ sudo dpkg --add-architecture riscv64
$ sudo apt-get install gcc-riscv64-linux-gnu g++-riscv64-linux-gnu
$ sudo sh -c "cat >/etc/ld.so.conf.d/riscv64-linux-gnu.conf <<EOF
/usr/local/lib/riscv64-linux-gnu
/lib/riscv64-linux-gnu
/usr/lib/riscv64-linux-gnu
/usr/riscv64-linux-gnu/lib/
EOF
"
$ sudo ln -s /usr/riscv64-linux-gnu/lib/ld-linux-riscv64-lp64d.so.1 /lib

And I got:

$ riscv64-linux-gnu-gcc --version
riscv64-linux-gnu-gcc (Debian 8.3.0-2) 8.3.0
JohnAZoidberg commented 3 years ago

Can also boot emulated U540: https://github.com/riscv/riscv-edk2-platforms/actions/runs/880262010

vlsunil commented 3 years ago

Thanks Daniel. But I think it is an older version of the toolchain. Bullseye is not yet released and I installed the package manually.

Anyway, the GOT relocations and other relocation differences are seen only when the compiler is built with "--enable-default-pie" option. You can run the below command to see whether this option is enabled.

$ /usr/bin/riscv64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/riscv64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/riscv64-linux-gnu/10/lto-wrapper
Target: riscv64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 10.2.0-5ubuntu1~20.04' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libsanitizer --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --without-target-system-zlib --enable-multiarch --disable-werror --disable-multilib --with-arch=rv64imafdc --with-abi=lp64d --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=riscv64-linux-gnu --program-prefix=riscv64-linux-gnu- --includedir=/usr/riscv64-linux-gnu/include
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (Ubuntu 10.2.0-5ubuntu1~20.04) 
JohnAZoidberg commented 3 years ago

The buster toolchain is also built with enable-default-pie:

$ riscv64-linux-gnu-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-linux-gnu-gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc-cross/riscv64-linux-gnu/8/lto-wrapper
Target: riscv64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 8.3.0-2' --with-bugurl=file:///usr/share/doc/gcc-8/README.Bugs --enable-languages=c,c++,go,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-libitm --disable-libsanitizer --disable-libquadmath --disable-libquadmath-support --enable-plugin --enable-default-pie --with-system-zlib --disable-libphobos --enable-multiarch --disable-werror --disable-multilib --with-arch=rv64imafdc --with-abi=lp64d --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=riscv64-linux-gnu --program-prefix=riscv64-linux-gnu- --includedir=/usr/riscv64-linux-gnu/include
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-2) 
vlsunil commented 3 years ago

The patch for this issue has been reviewed by maintainers.

https://edk2.groups.io/g/devel/message/76792

JohnAZoidberg commented 3 years ago

Merged upstream: https://github.com/tianocore/edk2/commit/abfff7c45d43360ebd292dc26b6542094c145f5f

Thanks @vlsunil