edk2/BaseTools/Source/C
Dongyan Qian 0225975462 BaseTools/GenFw: Fix LoongArch ELF-to-PE RVA conversion
GenFw converts linked ELF images to PE/COFF images using a new section
layout. The generated PE/COFF section RVAs are not required to match the
linked ELF section addresses, so relocation fixups that rewrite section
contents must use the generated PE/COFF RVA space.

A linker script layout change can expose this on LoongArch64 when .text
and .data are linked with 0x1000 alignment while .hii still keeps a
0x4000 alignment. GenFw then keeps a 0x4000 PE/COFF section alignment
because of .hii, producing different ELF and PE/COFF layouts.

Fix two LoongArch relocation paths exposed by this layout mismatch.
R_LARCH_64 entries stored in section contents, such as switch jump
tables, must be translated from the linked ELF section address space to
the generated PE/COFF RVA space. R_LARCH_PCALA_* and R_LARCH_GOT_PC_*
must also convert the referenced symbol to its generated PE/COFF RVA
before calculating the PC-relative offset.

The LoongArch ELF ABI defines PCALA/GOT_PC relocations as page-based,
but this GenFw path rewrites the HI/LO pair to PCADDU12I plus ADDI.D.
After that rewrite, the offset split must be based on the generated
PE/COFF instruction-relative offset.

For example, in the failing LogoDxe image, _gUefiDriverRevision is at
ELF address 0x5400 in .text, and the relocation referencing it is at ELF
address 0x104c. With ELF .text at 0x1000 and PE/COFF .text at 0x4000,
the generated PE/COFF RVAs are 0x8400 for the symbol and 0x404c for the
relocation site. Mixing the ELF symbol address with the PE/COFF
relocation-site RVA makes the entry wrapper read PE/COFF RVA 0x5400
instead of 0x8400, causing EFI_INCOMPATIBLE_VERSION.

Use PE/COFF RVAs consistently when rewriting LoongArch absolute and
PC-relative references during WriteSections64(). The existing
WriteRelocations64() base relocation emission is kept for load-time
image rebasing.

Reported-by: Chao Li <lichao@loongson.cn>
Signed-off-by: Dongyan Qian <qiandongyan@loongson.cn>
Signed-off-by: Chao Li <lichao@loongson.cn>
Tested-by: Dongyan Qian <qiandongyan@loongson.cn>
2026-06-22 03:36:43 +00:00
..
BrotliCompress BaseTools: Fix multiple security vulnerabilities (Defense in Depth) 2025-12-22 04:53:27 +00:00
Common BaseTools: Replace include guards with #pragma once 2026-02-23 21:01:28 +00:00
DevicePath BaseTools: Replace include guards with #pragma once 2026-02-23 21:01:28 +00:00
EfiRom BaseTools: Add missing machine types while dumping Option Rom 2026-04-23 05:31:47 +00:00
GenCrc32 BaseTools: Replace BSD License with BSD+Patent License 2019-04-09 09:10:20 -07:00
GenFfs BaseTools: GenFfs/GenFv/GenSec: Fixing Warning 4319 from MSVC 2026-03-13 19:14:49 +00:00
GenFv BaseTools: GenFfs/GenFv/GenSec: Fixing Warning 4319 from MSVC 2026-03-13 19:14:49 +00:00
GenFw BaseTools/GenFw: Fix LoongArch ELF-to-PE RVA conversion 2026-06-22 03:36:43 +00:00
GenSec BaseTools: GenFfs/GenFv/GenSec: Fixing Warning 4319 from MSVC 2026-03-13 19:14:49 +00:00
Include BaseTools/Source/C/Include/Common: Add back include guard 2026-02-27 07:15:11 +00:00
LzmaCompress BaseTools: Allow users to specify compiler to use with make CC= CXX= 2023-04-05 15:44:48 +00:00
Makefiles BaseTools: Enable control flow guard for Windows builds 2026-03-26 02:28:23 +00:00
PyEfiCompressor BaseTools: Replace BSD License with BSD+Patent License 2019-04-09 09:10:20 -07:00
TianoCompress BaseTools: Replace include guards with #pragma once 2026-02-23 21:01:28 +00:00
VfrCompile BaseTools/Source/C/VfrCompile: Fix parallel make failures 2026-02-27 04:54:43 +00:00
VolInfo BaseTools: Replace include guards with #pragma once 2026-02-23 21:01:28 +00:00
GNUmakefile Revert "BaseTools: Add support for out-of-tree builds" 2026-02-11 22:53:29 +00:00
Makefile BaseTools: Adding cross compilation of BaseTool for Windows ARM/ARM64 2024-12-20 22:57:04 +00:00