Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RISC-V objcopy + variable fixes + ARM32 split header #106

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
62 changes: 33 additions & 29 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 @@ -45,12 +45,12 @@ optional_header:
.2byte 0x10b // PE32+ format
.byte 0x02 // MajorLinkerVersion
.byte 0x14 // MinorLinkerVersion
.4byte _etext - _start // SizeOfCode
.4byte _text_size - ImageBase // SizeOfCode
.4byte _alldata_size - ImageBase // SizeOfInitializedData
.4byte 0 // SizeOfUninitializedData
.4byte _start - ImageBase // AddressOfEntryPoint
.4byte _start - ImageBase // BaseOfCode
.4byte _reloc - ImageBase // BaseOfData
.4byte _text - ImageBase // AddressOfEntryPoint
.4byte _text - ImageBase // BaseOfCode
.4byte _data - ImageBase // BaseOfData

extra_header_fields:
.4byte 0 // ImageBase
Expand All @@ -67,7 +67,7 @@ extra_header_fields:
.4byte _image_end - ImageBase // SizeOfImage

// Everything before the kernel image is considered part of the header
.4byte _start - ImageBase // SizeOfHeaders
.4byte _text - ImageBase // SizeOfHeaders
.4byte 0 // CheckSum
.2byte EFI_SUBSYSTEM // Subsystem
.2byte 0 // DllCharacteristics
Expand Down Expand Up @@ -100,16 +100,27 @@ extra_header_fields:
section_table:

.ascii ".text\0\0\0"
.4byte _evtext - _start // VirtualSize
.4byte _start - ImageBase // VirtualAddress
.4byte _etext - _start // SizeOfRawData
.4byte _start - ImageBase // PointerToRawData
.4byte _text_vsize - ImageBase // VirtualSize
.4byte _text - ImageBase // VirtualAddress
.4byte _text_size - ImageBase // SizeOfRawData
.4byte _text - ImageBase // PointerToRawData
.4byte 0 // PointerToRelocations (0 for executables)
.4byte 0 // PointerToLineNumbers (0 for executables)
.2byte 0 // NumberOfRelocations (0 for executables)
.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 All @@ -173,7 +174,7 @@ section_table:
.4byte 0x40000040 // Characteristics (section flags)
#endif

.balign 256
.text
.globl _start
.type _start,%function
_start:
Expand All @@ -184,15 +185,17 @@ _start:
adr r1, .L_DYNAMIC
ldr r0, [r1]
add r1, r0, r1
adr r0, ImageBase
adr r0, _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 @@ -201,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
Loading