]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
riscv: separate .data and .text sections of EFI binaries
authorHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Thu, 25 Jan 2024 08:38:15 +0000 (09:38 +0100)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Mon, 29 Jan 2024 10:30:10 +0000 (11:30 +0100)
EFI binaries should not contain sections that are both writable and
executable. Separate the RX .text section from the RW .data section

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
arch/riscv/lib/crt0_riscv_efi.S
arch/riscv/lib/elf_riscv32_efi.lds
arch/riscv/lib/elf_riscv64_efi.lds

index ffe18fa387b007cfc0d8f2c0ec5f26fedcd1308a..46b08552371cafdd6552afd589f32da194397cda 100644 (file)
@@ -53,7 +53,7 @@ pe_header:
        .long   IMAGE_NT_SIGNATURE              /* 'PE' */
 coff_header:
        .short  PE_MACHINE                      /* RISC-V 64/32-bit */
-       .short  2                               /* nr_sections */
+       .short  3                               /* nr_sections */
        .long   0                               /* TimeDateStamp */
        .long   0                               /* PointerToSymbolTable */
        .long   0                               /* NumberOfSymbols */
@@ -144,16 +144,35 @@ section_table:
        .byte   0
        .byte   0
        .byte   0                       /* end of 0 padding of section name */
-       .long   _edata - _start         /* VirtualSize */
+       .long   _etext - _start         /* VirtualSize */
        .long   _start - ImageBase      /* VirtualAddress */
-       .long   _edata - _start         /* SizeOfRawData */
+       .long   _etext - _start         /* SizeOfRawData */
        .long   _start - ImageBase      /* PointerToRawData */
-
-       .long   0               /* PointerToRelocations (0 for executables) */
-       .long   0               /* PointerToLineNumbers (0 for executables) */
-       .short  0               /* NumberOfRelocations  (0 for executables) */
-       .short  0               /* NumberOfLineNumbers  (0 for executables) */
-       .long   0xe0500020      /* Characteristics (section flags) */
+       .long   0                       /* PointerToRelocations (0 for executables) */
+       .long   0                       /* PointerToLineNumbers (0 for executables) */
+       .short  0                       /* NumberOfRelocations  (0 for executables) */
+       .short  0                       /* NumberOfLineNumbers  (0 for executables) */
+       /* Characteristics (section flags) */
+       .long   (IMAGE_SCN_MEM_READ | \
+                IMAGE_SCN_MEM_EXECUTE | \
+                IMAGE_SCN_CNT_CODE)
+
+       .ascii  ".data"
+       .byte   0
+       .byte   0
+       .byte   0                       /* end of 0 padding of section name */
+       .long   _edata - _data          /* VirtualSize */
+       .long   _data - ImageBase       /* VirtualAddress */
+       .long   _edata - _data          /* SizeOfRawData */
+       .long   _data - ImageBase       /* PointerToRawData */
+       .long   0                       /* PointerToRelocations */
+       .long   0                       /* PointerToLineNumbers */
+       .short  0                       /* NumberOfRelocations */
+       .short  0                       /* NumberOfLineNumbers */
+       /* Characteristics (section flags) */
+       .long   (IMAGE_SCN_MEM_WRITE | \
+                IMAGE_SCN_MEM_READ | \
+                IMAGE_SCN_CNT_INITIALIZED_DATA)
 
        .align          12
 _start:
index 940a5816b824588ad6cca3294a9d255a34812cc1..7b9bd7b7f15e113a88d48183fda8f1fb7b25ccd4 100644 (file)
@@ -7,6 +7,12 @@
 
 OUTPUT_FORMAT("elf32-littleriscv", "elf32-littleriscv", "elf32-littleriscv")
 OUTPUT_ARCH(riscv)
+
+PHDRS
+{
+       data PT_LOAD FLAGS(3); /* SHF_WRITE | SHF_ALLOC */
+}
+
 ENTRY(_start)
 SECTIONS
 {
@@ -18,11 +24,17 @@ SECTIONS
                *(.gnu.linkonce.t.*)
                *(.srodata)
                *(.rodata*)
+               . = ALIGN(16);
+               *(.dynamic);
                . = ALIGN(512);
        }
+       .rela.dyn : { *(.rela.dyn) }
+       .rela.plt : { *(.rela.plt) }
+       .rela.got : { *(.rela.got) }
+       .rela.data : { *(.rela.data) *(.rela.data*) }
        _etext = .;
        _text_size = . - _text;
-       .dynamic  : { *(.dynamic) }
+       . = ALIGN(4096);
        .data : {
                _data = .;
                *(.sdata)
@@ -47,12 +59,8 @@ SECTIONS
                . = ALIGN(512);
                _bss_end = .;
                _edata = .;
-       }
-       .rela.dyn : { *(.rela.dyn) }
-       .rela.plt : { *(.rela.plt) }
-       .rela.got : { *(.rela.got) }
-       .rela.data : { *(.rela.data) *(.rela.data*) }
-       _data_size = . - _etext;
+       } :data
+       _data_size = _edata - _data;
 
        . = ALIGN(4096);
        .dynsym   : { *(.dynsym) }
index be9f0ac6022588010acdb5dc3e68d20be0b4a6b0..d0b4f3d1d644de3dd0054d2e63842634f1892af8 100644 (file)
@@ -7,6 +7,12 @@
 
 OUTPUT_FORMAT("elf64-littleriscv", "elf64-littleriscv", "elf64-littleriscv")
 OUTPUT_ARCH(riscv)
+
+PHDRS
+{
+       data PT_LOAD FLAGS(3); /* SHF_WRITE | SHF_ALLOC */
+}
+
 ENTRY(_start)
 SECTIONS
 {
@@ -18,11 +24,17 @@ SECTIONS
                *(.gnu.linkonce.t.*)
                *(.srodata)
                *(.rodata*)
+               . = ALIGN(16);
+               *(.dynamic);
                . = ALIGN(512);
        }
+       .rela.dyn : { *(.rela.dyn) }
+       .rela.plt : { *(.rela.plt) }
+       .rela.got : { *(.rela.got) }
+       .rela.data : { *(.rela.data) *(.rela.data*) }
        _etext = .;
        _text_size = . - _text;
-       .dynamic  : { *(.dynamic) }
+       . = ALIGN(4096);
        .data : {
                _data = .;
                *(.sdata)
@@ -47,12 +59,8 @@ SECTIONS
                . = ALIGN(512);
                _bss_end = .;
                _edata = .;
-       }
-       .rela.dyn : { *(.rela.dyn) }
-       .rela.plt : { *(.rela.plt) }
-       .rela.got : { *(.rela.got) }
-       .rela.data : { *(.rela.data) *(.rela.data*) }
-       _data_size = . - _etext;
+       } :data
+       _data_size = _edata - _data;
 
        . = ALIGN(4096);
        .dynsym   : { *(.dynsym) }