Skip to content

Commit 55de1e4

Browse files
Alexandre Ghitipalmer-dabbelt
authored andcommitted
riscv: Prepare EFI header for relocatable kernels
ld does not handle relocations correctly as explained here [1], a fix for that was proposed by Nelson there but we have to support older toolchains and then provide this fix. Note that llvm does not need this fix and is then excluded. [1] https://sourceware.org/pipermail/binutils/2023-March/126690.html Signed-off-by: Alexandre Ghiti <alexghiti@rivosinc.com> Link: https://lore.kernel.org/r/20230329045329.64565-2-alexghiti@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent fe15c26 commit 55de1e4

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

arch/riscv/include/asm/set_memory.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,7 @@ bool kernel_page_present(struct page *page);
5656
#define SECTION_ALIGN L1_CACHE_BYTES
5757
#endif /* CONFIG_STRICT_KERNEL_RWX */
5858

59+
#define PECOFF_SECTION_ALIGNMENT 0x1000
60+
#define PECOFF_FILE_ALIGNMENT 0x200
61+
5962
#endif /* _ASM_RISCV_SET_MEMORY_H */

arch/riscv/kernel/efi-header.S

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <linux/pe.h>
88
#include <linux/sizes.h>
9+
#include <asm/set_memory.h>
910

1011
.macro __EFI_PE_HEADER
1112
.long PE_MAGIC
@@ -33,7 +34,11 @@ optional_header:
3334
.byte 0x02 // MajorLinkerVersion
3435
.byte 0x14 // MinorLinkerVersion
3536
.long __pecoff_text_end - efi_header_end // SizeOfCode
36-
.long __pecoff_data_virt_size // SizeOfInitializedData
37+
#ifdef __clang__
38+
.long __pecoff_data_virt_size // SizeOfInitializedData
39+
#else
40+
.long __pecoff_data_virt_end - __pecoff_text_end // SizeOfInitializedData
41+
#endif
3742
.long 0 // SizeOfUninitializedData
3843
.long __efistub_efi_pe_entry - _start // AddressOfEntryPoint
3944
.long efi_header_end - _start // BaseOfCode
@@ -91,9 +96,17 @@ section_table:
9196
IMAGE_SCN_MEM_EXECUTE // Characteristics
9297

9398
.ascii ".data\0\0\0"
94-
.long __pecoff_data_virt_size // VirtualSize
99+
#ifdef __clang__
100+
.long __pecoff_data_virt_size // VirtualSize
101+
#else
102+
.long __pecoff_data_virt_end - __pecoff_text_end // VirtualSize
103+
#endif
95104
.long __pecoff_text_end - _start // VirtualAddress
96-
.long __pecoff_data_raw_size // SizeOfRawData
105+
#ifdef __clang__
106+
.long __pecoff_data_raw_size // SizeOfRawData
107+
#else
108+
.long __pecoff_data_raw_end - __pecoff_text_end // SizeOfRawData
109+
#endif
97110
.long __pecoff_text_end - _start // PointerToRawData
98111

99112
.long 0 // PointerToRelocations

arch/riscv/kernel/vmlinux.lds.S

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ ENTRY(_start)
2727

2828
jiffies = jiffies_64;
2929

30-
PECOFF_SECTION_ALIGNMENT = 0x1000;
31-
PECOFF_FILE_ALIGNMENT = 0x200;
32-
3330
SECTIONS
3431
{
3532
/* Beginning of code and text segment */
@@ -132,6 +129,7 @@ SECTIONS
132129
#ifdef CONFIG_EFI
133130
.pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
134131
__pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end);
132+
__pecoff_data_raw_end = ABSOLUTE(.);
135133
#endif
136134

137135
/* End of data section */
@@ -142,6 +140,7 @@ SECTIONS
142140
#ifdef CONFIG_EFI
143141
. = ALIGN(PECOFF_SECTION_ALIGNMENT);
144142
__pecoff_data_virt_size = ABSOLUTE(. - __pecoff_text_end);
143+
__pecoff_data_virt_end = ABSOLUTE(.);
145144
#endif
146145
_end = .;
147146

0 commit comments

Comments
 (0)