Skip to content

Commit

Permalink
Update crt0/lds from GNU-EFI 4.0
Browse files Browse the repository at this point in the history
* Align CRT0 exit function naming
* Push .reloc after .data
* Make our dummy .reloc sections not depend on section order.

Signed-off-by: Callum Farmer <[email protected]>
  • Loading branch information
gmbr3 committed Nov 23, 2024
1 parent 43320ad commit ef00375
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 209 deletions.
47 changes: 25 additions & 22 deletions efi/crt0/crt0-efi-aarch64.S
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

/*
* crt0-efi-aarch64.S - PE/COFF header for AArch64 EFI applications
* crt0-efi-aarch64-local.S - PE/COFF header for AArch64 EFI applications (without suitable objcopy)
*
* Copyright (C) 2014 Linaro Ltd. <[email protected]>
*
Expand Down Expand Up @@ -108,6 +109,17 @@ section_table:
.2byte 0 // NumberOfLineNumbers (0 for executables)
.4byte 0x60000020 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

/*
* The EFI application loader requires a relocation section
* because EFI applications must be relocatable. This is a
Expand All @@ -124,17 +136,6 @@ section_table:
.2byte 0 // NumberOfLineNumbers
.4byte 0x42000040 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

.ascii ".rodata\0"
.4byte _rodata_vsize - ImageBase // VirtualSize
.4byte _rodata - ImageBase // VirtualAddress
Expand All @@ -145,7 +146,7 @@ section_table:
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0x40000040 // Characteristics (section flags)

#ifdef USING_SBAT
.ascii ".sbat\0\0\0"
.4byte _sbat_vsize - ImageBase // VirtualSize
Expand Down Expand Up @@ -173,6 +174,7 @@ section_table:
.4byte 0x40000040 // Characteristics (section flags)
#endif


.text
.globl _start
.type _start,%function
Expand All @@ -187,23 +189,24 @@ _start:
adrp x1, _DYNAMIC
add x1, x1, #:lo12:_DYNAMIC
bl _relocate
cbnz x0, 0f
cbnz x0, .L_exit

ldp x0, x1, [sp, #16]
bl efi_main
bl _entry

0: ldp x29, x30, [sp], #32
.L_exit:
ldp x29, x30, [sp], #32
ret
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:

// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:

.data
dummy: .4byte 0
dummy0: .4byte 0
dummy1: .4byte 0

#define IMAGE_REL_ABSOLUTE 0
.section .reloc, "a"
label1:
.4byte dummy-label1 // Page RVA
.section .reloc, "a", %progbits
.4byte dummy1 - dummy0 // Page RVA
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
Expand Down
43 changes: 23 additions & 20 deletions efi/crt0/crt0-efi-arm.S
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pe_header:
.2byte 0
coff_header:
.2byte 0x1c2 // Mixed ARM/Thumb
.2byte NR_SECTIONS // nr_sections
.2byte NR_SECTIONS // nr_sections
.4byte 0 // TimeDateStamp
.4byte 0 // PointerToSymbolTable
.4byte 0 // NumberOfSymbols
Expand All @@ -48,9 +48,9 @@ optional_header:
.4byte _text_size - ImageBase // SizeOfCode
.4byte _alldata_size - ImageBase // SizeOfInitializedData
.4byte 0 // SizeOfUninitializedData
.4byte _start - ImageBase // AddressOfEntryPoint
.4byte _text - ImageBase // AddressOfEntryPoint
.4byte _text - ImageBase // BaseOfCode
.4byte _reloc - ImageBase // BaseOfData
.4byte _data - ImageBase // BaseOfData

extra_header_fields:
.4byte 0 // ImageBase
Expand Down Expand Up @@ -110,6 +110,17 @@ section_table:
.2byte 0 // NumberOfLineNumbers (0 for executables)
.4byte 0x60000020 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

/*
* The EFI application loader requires a relocation section
* because EFI applications must be relocatable. This is a
Expand All @@ -126,17 +137,6 @@ section_table:
.2byte 0 // NumberOfLineNumbers
.4byte 0x42000040 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

.ascii ".rodata\0"
.4byte _rodata_vsize - ImageBase // VirtualSize
.4byte _rodata - ImageBase // VirtualAddress
Expand All @@ -147,6 +147,7 @@ section_table:
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0x40000040 // Characteristics (section flags)

#ifdef USING_SBAT
.ascii ".sbat\0\0\0"
.4byte _sbat_vsize - ImageBase // VirtualSize
Expand Down Expand Up @@ -188,12 +189,13 @@ _start:
sub r0, r0, #0x1000
bl _relocate
teq r0, #0
bne 0f
bne .L_exit

ldmfd sp, {r0-r1}
bl efi_main
bl _entry

0: add sp, sp, #12
.L_exit:
add sp, sp, #12
ldr pc, [sp], #4

.L_DYNAMIC:
Expand All @@ -202,11 +204,12 @@ _start:
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:

.data
dummy: .4byte 0
dummy0: .4byte 0
dummy1: .4byte 0

#define IMAGE_REL_ABSOLUTE 0
.section .areloc
.4byte dummy // Page RVA
.section .areloc, "a", %progbits
.4byte dummy1 - dummy0 // Page RVA
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
Expand Down
37 changes: 19 additions & 18 deletions efi/crt0/crt0-efi-loongarch64.S
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ section_table:
.2byte 0 // NumberOfLineNumbers (0 for executables)
.4byte 0x60000020 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

/*
* The EFI application loader requires a relocation section
* because EFI applications must be relocatable. This is a
Expand All @@ -124,17 +135,6 @@ section_table:
.2byte 0 // NumberOfLineNumbers
.4byte 0x42000040 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

.ascii ".rodata\0"
.4byte _rodata_vsize - ImageBase // VirtualSize
.4byte _rodata - ImageBase // VirtualAddress
Expand Down Expand Up @@ -187,25 +187,26 @@ _start:
la.local $a0, ImageBase // a0: ImageBase
la.local $a1, _DYNAMIC // a1: DynamicSection
bl _relocate
bnez $a0, 0f
bnez $a0, .L_exit

ld.d $a0, $sp, 8
ld.d $a1, $sp, 16
bl efi_main

0: ld.d $ra, $sp, 0
.L_exit:
ld.d $ra, $sp, 0
addi.d $sp, $sp, 24
jr $ra
.end _start
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:

.data
dummy: .4byte 0
.data
dummy0: .4byte 0
dummy1: .4byte 0

#define IMAGE_REL_ABSOLUTE 0
.section .reloc, "a"
label1:
.4byte dummy-label1 // Page RVA
.section .reloc, "a", %progbits
.4byte dummy1 - dummy0 // Page RVA
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
Expand Down
44 changes: 23 additions & 21 deletions efi/crt0/crt0-efi-riscv64.S
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
/* SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause */
/*
* crt0-efi-riscv64-local.S - PE/COFF header for RISC-V 64 EFI applications (without suitable objcopy)
*
* Copyright (C) 2014 Linaro Ltd. <[email protected]>
* Copyright (C) 2018 Alexander Graf <[email protected]>
*
Expand All @@ -16,7 +18,6 @@
* either version 2 of the License, or (at your option) any later version.
*/


.section .text.head

/*
Expand Down Expand Up @@ -68,7 +69,7 @@ extra_header_fields:
// Everything before the kernel image is considered part of the header
.4byte _text - ImageBase // SizeOfHeaders
.4byte 0 // CheckSum
.2byte EFI_SUBSYSTEM // Subsystem
.2byte EFI_SUBSYSTEM // Subsystem
.2byte 0 // DllCharacteristics
.8byte 0 // SizeOfStackReserve
.8byte 0 // SizeOfStackCommit
Expand Down Expand Up @@ -109,6 +110,17 @@ section_table:
.2byte 0 // NumberOfLineNumbers (0 for executables)
.4byte 0x60000020 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

/*
* The EFI application loader requires a relocation section
* because EFI applications must be relocatable. This is a
Expand All @@ -125,17 +137,6 @@ section_table:
.2byte 0 // NumberOfLineNumbers
.4byte 0x42000040 // Characteristics (section flags)

.ascii ".data\0\0\0"
.4byte _data_vsize - ImageBase // VirtualSize
.4byte _data - ImageBase // VirtualAddress
.4byte _data_size - ImageBase // SizeOfRawData
.4byte _data - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations
.4byte 0 // PointerToLineNumbers
.2byte 0 // NumberOfRelocations
.2byte 0 // NumberOfLineNumbers
.4byte 0xC0000040 // Characteristics (section flags)

.ascii ".rodata\0"
.4byte _rodata_vsize - ImageBase // VirtualSize
.4byte _rodata - ImageBase // VirtualAddress
Expand Down Expand Up @@ -185,23 +186,24 @@ _start:
lla a0, ImageBase
lla a1, _DYNAMIC
call _relocate
bne a0, zero, 0f
bne a0, zero, .L_exit
ld a1, 8(sp)
ld a0, 0(sp)
call efi_main
call _entry
ld ra, 16(sp)
0: addi sp, sp, 24
.L_exit:
addi sp, sp, 24
ret

// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:

.data
dummy: .4byte 0
dummy0: .4byte 0
dummy1: .4byte 0

#define IMAGE_REL_ABSOLUTE 0
.section .reloc, "a"
label1:
.4byte dummy-label1 // Page RVA
.section .reloc, "a", %progbits
.4byte dummy1 - dummy0 // Page RVA
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
Expand Down
Loading

0 comments on commit ef00375

Please sign in to comment.