riscv: Prepare EFI header for relocatable kernels
authorAlexandre Ghiti <alexghiti@rivosinc.com>
Wed, 29 Mar 2023 04:53:24 +0000 (06:53 +0200)
committerPalmer Dabbelt <palmer@rivosinc.com>
Wed, 19 Apr 2023 14:46:28 +0000 (07:46 -0700)
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>
arch/riscv/include/asm/set_memory.h
arch/riscv/kernel/efi-header.S
arch/riscv/kernel/vmlinux.lds.S

index a2c14d4b3993ea64df8c727fad27cc0e2b5dcde1..ec11001c3fe04223e3417161ca5468d29590d092 100644 (file)
@@ -56,4 +56,7 @@ bool kernel_page_present(struct page *page);
 #define SECTION_ALIGN L1_CACHE_BYTES
 #endif /* CONFIG_STRICT_KERNEL_RWX */
 
+#define PECOFF_SECTION_ALIGNMENT        0x1000
+#define PECOFF_FILE_ALIGNMENT           0x200
+
 #endif /* _ASM_RISCV_SET_MEMORY_H */
index 8e733aa48ba6cbc2f2f62e1ac74b107b737676f6..515b2dfbca75ba8e76128dfb3b16b252f388c2e0 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <linux/pe.h>
 #include <linux/sizes.h>
+#include <asm/set_memory.h>
 
        .macro  __EFI_PE_HEADER
        .long   PE_MAGIC
@@ -33,7 +34,11 @@ optional_header:
        .byte   0x02                                    // MajorLinkerVersion
        .byte   0x14                                    // MinorLinkerVersion
        .long   __pecoff_text_end - efi_header_end      // SizeOfCode
-       .long   __pecoff_data_virt_size                 // SizeOfInitializedData
+#ifdef __clang__
+       .long   __pecoff_data_virt_size                 // SizeOfInitializedData
+#else
+       .long   __pecoff_data_virt_end - __pecoff_text_end      // SizeOfInitializedData
+#endif
        .long   0                                       // SizeOfUninitializedData
        .long   __efistub_efi_pe_entry - _start         // AddressOfEntryPoint
        .long   efi_header_end - _start                 // BaseOfCode
@@ -91,9 +96,17 @@ section_table:
                IMAGE_SCN_MEM_EXECUTE                   // Characteristics
 
        .ascii  ".data\0\0\0"
-       .long   __pecoff_data_virt_size                 // VirtualSize
+#ifdef __clang__
+       .long   __pecoff_data_virt_size                 // VirtualSize
+#else
+       .long   __pecoff_data_virt_end - __pecoff_text_end      // VirtualSize
+#endif
        .long   __pecoff_text_end - _start              // VirtualAddress
-       .long   __pecoff_data_raw_size                  // SizeOfRawData
+#ifdef __clang__
+       .long   __pecoff_data_raw_size                  // SizeOfRawData
+#else
+       .long   __pecoff_data_raw_end - __pecoff_text_end       // SizeOfRawData
+#endif
        .long   __pecoff_text_end - _start              // PointerToRawData
 
        .long   0                                       // PointerToRelocations
index 53a8ad65b255fb1e35b5bf1f54cdafc387769d80..1c38294580c05e2591f1c3e8da9bee44843576a7 100644 (file)
@@ -27,9 +27,6 @@ ENTRY(_start)
 
 jiffies = jiffies_64;
 
-PECOFF_SECTION_ALIGNMENT = 0x1000;
-PECOFF_FILE_ALIGNMENT = 0x200;
-
 SECTIONS
 {
        /* Beginning of code and text segment */
@@ -132,6 +129,7 @@ SECTIONS
 #ifdef CONFIG_EFI
        .pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
        __pecoff_data_raw_size = ABSOLUTE(. - __pecoff_text_end);
+       __pecoff_data_raw_end = ABSOLUTE(.);
 #endif
 
        /* End of data section */
@@ -142,6 +140,7 @@ SECTIONS
 #ifdef CONFIG_EFI
        . = ALIGN(PECOFF_SECTION_ALIGNMENT);
        __pecoff_data_virt_size = ABSOLUTE(. - __pecoff_text_end);
+       __pecoff_data_virt_end = ABSOLUTE(.);
 #endif
        _end = .;