Skip to content

Commit de727cf

Browse files
committed
Update crt0/lds from GNU-EFI 4.0
* 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]>
1 parent 7645a77 commit de727cf

11 files changed

+171
-180
lines changed

efi/crt0/crt0-efi-aarch64.S

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
12
/*
2-
* crt0-efi-aarch64.S - PE/COFF header for AArch64 EFI applications
3+
* crt0-efi-aarch64-local.S - PE/COFF header for AArch64 EFI applications (without suitable objcopy)
34
*
45
* Copyright (C) 2014 Linaro Ltd. <[email protected]>
56
*
@@ -108,6 +109,17 @@ section_table:
108109
.2byte 0 // NumberOfLineNumbers (0 for executables)
109110
.4byte 0x60000020 // Characteristics (section flags)
110111

112+
.ascii ".data\0\0\0"
113+
.4byte _data_vsize - ImageBase // VirtualSize
114+
.4byte _data - ImageBase // VirtualAddress
115+
.4byte _data_size - ImageBase // SizeOfRawData
116+
.4byte _data - ImageBase // PointerToRawData
117+
.4byte 0 // PointerToRelocations
118+
.4byte 0 // PointerToLineNumbers
119+
.2byte 0 // NumberOfRelocations
120+
.2byte 0 // NumberOfLineNumbers
121+
.4byte 0xC0000040 // Characteristics (section flags)
122+
111123
/*
112124
* The EFI application loader requires a relocation section
113125
* because EFI applications must be relocatable. This is a
@@ -124,17 +136,6 @@ section_table:
124136
.2byte 0 // NumberOfLineNumbers
125137
.4byte 0x42000040 // Characteristics (section flags)
126138

127-
.ascii ".data\0\0\0"
128-
.4byte _data_vsize - ImageBase // VirtualSize
129-
.4byte _data - ImageBase // VirtualAddress
130-
.4byte _data_size - ImageBase // SizeOfRawData
131-
.4byte _data - ImageBase // PointerToRawData
132-
.4byte 0 // PointerToRelocations
133-
.4byte 0 // PointerToLineNumbers
134-
.2byte 0 // NumberOfRelocations
135-
.2byte 0 // NumberOfLineNumbers
136-
.4byte 0xC0000040 // Characteristics (section flags)
137-
138139
.ascii ".rodata\0"
139140
.4byte _rodata_vsize - ImageBase // VirtualSize
140141
.4byte _rodata - ImageBase // VirtualAddress
@@ -145,7 +146,7 @@ section_table:
145146
.2byte 0 // NumberOfRelocations
146147
.2byte 0 // NumberOfLineNumbers
147148
.4byte 0x40000040 // Characteristics (section flags)
148-
149+
149150
#ifdef USING_SBAT
150151
.ascii ".sbat\0\0\0"
151152
.4byte _sbat_vsize - ImageBase // VirtualSize
@@ -173,6 +174,7 @@ section_table:
173174
.4byte 0x40000040 // Characteristics (section flags)
174175
#endif
175176

177+
176178
.text
177179
.globl _start
178180
.type _start,%function
@@ -187,23 +189,24 @@ _start:
187189
adrp x1, _DYNAMIC
188190
add x1, x1, #:lo12:_DYNAMIC
189191
bl _relocate
190-
cbnz x0, 0f
192+
cbnz x0, .L_exit
191193

192194
ldp x0, x1, [sp, #16]
193-
bl efi_main
195+
bl _entry
194196

195-
0: ldp x29, x30, [sp], #32
197+
.L_exit:
198+
ldp x29, x30, [sp], #32
196199
ret
197-
198-
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
200+
201+
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
199202
200203
.data
201-
dummy: .4byte 0
204+
dummy0: .4byte 0
205+
dummy1: .4byte 0
202206

203207
#define IMAGE_REL_ABSOLUTE 0
204-
.section .reloc, "a"
205-
label1:
206-
.4byte dummy-label1 // Page RVA
208+
.section .reloc, "a", %progbits
209+
.4byte dummy1 - dummy0 // Page RVA
207210
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
208211
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
209212
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy

efi/crt0/crt0-efi-arm.S

+23-20
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pe_header:
3131
.2byte 0
3232
coff_header:
3333
.2byte 0x1c2 // Mixed ARM/Thumb
34-
.2byte NR_SECTIONS // nr_sections
34+
.2byte NR_SECTIONS // nr_sections
3535
.4byte 0 // TimeDateStamp
3636
.4byte 0 // PointerToSymbolTable
3737
.4byte 0 // NumberOfSymbols
@@ -48,9 +48,9 @@ optional_header:
4848
.4byte _text_size - ImageBase // SizeOfCode
4949
.4byte _alldata_size - ImageBase // SizeOfInitializedData
5050
.4byte 0 // SizeOfUninitializedData
51-
.4byte _start - ImageBase // AddressOfEntryPoint
51+
.4byte _text - ImageBase // AddressOfEntryPoint
5252
.4byte _text - ImageBase // BaseOfCode
53-
.4byte _reloc - ImageBase // BaseOfData
53+
.4byte _data - ImageBase // BaseOfData
5454

5555
extra_header_fields:
5656
.4byte 0 // ImageBase
@@ -110,6 +110,17 @@ section_table:
110110
.2byte 0 // NumberOfLineNumbers (0 for executables)
111111
.4byte 0x60000020 // Characteristics (section flags)
112112

113+
.ascii ".data\0\0\0"
114+
.4byte _data_vsize - ImageBase // VirtualSize
115+
.4byte _data - ImageBase // VirtualAddress
116+
.4byte _data_size - ImageBase // SizeOfRawData
117+
.4byte _data - ImageBase // PointerToRawData
118+
.4byte 0 // PointerToRelocations
119+
.4byte 0 // PointerToLineNumbers
120+
.2byte 0 // NumberOfRelocations
121+
.2byte 0 // NumberOfLineNumbers
122+
.4byte 0xC0000040 // Characteristics (section flags)
123+
113124
/*
114125
* The EFI application loader requires a relocation section
115126
* because EFI applications must be relocatable. This is a
@@ -126,17 +137,6 @@ section_table:
126137
.2byte 0 // NumberOfLineNumbers
127138
.4byte 0x42000040 // Characteristics (section flags)
128139

129-
.ascii ".data\0\0\0"
130-
.4byte _data_vsize - ImageBase // VirtualSize
131-
.4byte _data - ImageBase // VirtualAddress
132-
.4byte _data_size - ImageBase // SizeOfRawData
133-
.4byte _data - ImageBase // PointerToRawData
134-
.4byte 0 // PointerToRelocations
135-
.4byte 0 // PointerToLineNumbers
136-
.2byte 0 // NumberOfRelocations
137-
.2byte 0 // NumberOfLineNumbers
138-
.4byte 0xC0000040 // Characteristics (section flags)
139-
140140
.ascii ".rodata\0"
141141
.4byte _rodata_vsize - ImageBase // VirtualSize
142142
.4byte _rodata - ImageBase // VirtualAddress
@@ -147,6 +147,7 @@ section_table:
147147
.2byte 0 // NumberOfRelocations
148148
.2byte 0 // NumberOfLineNumbers
149149
.4byte 0x40000040 // Characteristics (section flags)
150+
150151
#ifdef USING_SBAT
151152
.ascii ".sbat\0\0\0"
152153
.4byte _sbat_vsize - ImageBase // VirtualSize
@@ -188,12 +189,13 @@ _start:
188189
sub r0, r0, #0x1000
189190
bl _relocate
190191
teq r0, #0
191-
bne 0f
192+
bne .L_exit
192193

193194
ldmfd sp, {r0-r1}
194-
bl efi_main
195+
bl _entry
195196

196-
0: add sp, sp, #12
197+
.L_exit:
198+
add sp, sp, #12
197199
ldr pc, [sp], #4
198200

199201
.L_DYNAMIC:
@@ -202,11 +204,12 @@ _start:
202204
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
203205
204206
.data
205-
dummy: .4byte 0
207+
dummy0: .4byte 0
208+
dummy1: .4byte 0
206209

207210
#define IMAGE_REL_ABSOLUTE 0
208-
.section .areloc
209-
.4byte dummy // Page RVA
211+
.section .areloc, "a", %progbits
212+
.4byte dummy1 - dummy0 // Page RVA
210213
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
211214
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
212215
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy

efi/crt0/crt0-efi-loongarch64.S

+19-18
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ section_table:
108108
.2byte 0 // NumberOfLineNumbers (0 for executables)
109109
.4byte 0x60000020 // Characteristics (section flags)
110110

111+
.ascii ".data\0\0\0"
112+
.4byte _data_vsize - ImageBase // VirtualSize
113+
.4byte _data - ImageBase // VirtualAddress
114+
.4byte _data_size - ImageBase // SizeOfRawData
115+
.4byte _data - ImageBase // PointerToRawData
116+
.4byte 0 // PointerToRelocations
117+
.4byte 0 // PointerToLineNumbers
118+
.2byte 0 // NumberOfRelocations
119+
.2byte 0 // NumberOfLineNumbers
120+
.4byte 0xC0000040 // Characteristics (section flags)
121+
111122
/*
112123
* The EFI application loader requires a relocation section
113124
* because EFI applications must be relocatable. This is a
@@ -124,17 +135,6 @@ section_table:
124135
.2byte 0 // NumberOfLineNumbers
125136
.4byte 0x42000040 // Characteristics (section flags)
126137

127-
.ascii ".data\0\0\0"
128-
.4byte _data_vsize - ImageBase // VirtualSize
129-
.4byte _data - ImageBase // VirtualAddress
130-
.4byte _data_size - ImageBase // SizeOfRawData
131-
.4byte _data - ImageBase // PointerToRawData
132-
.4byte 0 // PointerToRelocations
133-
.4byte 0 // PointerToLineNumbers
134-
.2byte 0 // NumberOfRelocations
135-
.2byte 0 // NumberOfLineNumbers
136-
.4byte 0xC0000040 // Characteristics (section flags)
137-
138138
.ascii ".rodata\0"
139139
.4byte _rodata_vsize - ImageBase // VirtualSize
140140
.4byte _rodata - ImageBase // VirtualAddress
@@ -187,25 +187,26 @@ _start:
187187
la.local $a0, ImageBase // a0: ImageBase
188188
la.local $a1, _DYNAMIC // a1: DynamicSection
189189
bl _relocate
190-
bnez $a0, 0f
190+
bnez $a0, .L_exit
191191

192192
ld.d $a0, $sp, 8
193193
ld.d $a1, $sp, 16
194194
bl efi_main
195195

196-
0: ld.d $ra, $sp, 0
196+
.L_exit:
197+
ld.d $ra, $sp, 0
197198
addi.d $sp, $sp, 24
198199
jr $ra
199200
.end _start
200201
// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
201202

202-
.data
203-
dummy: .4byte 0
203+
.data
204+
dummy0: .4byte 0
205+
dummy1: .4byte 0
204206

205207
#define IMAGE_REL_ABSOLUTE 0
206-
.section .reloc, "a"
207-
label1:
208-
.4byte dummy-label1 // Page RVA
208+
.section .reloc, "a", %progbits
209+
.4byte dummy1 - dummy0 // Page RVA
209210
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
210211
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
211212
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy

efi/crt0/crt0-efi-riscv64.S

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
1+
/* SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause */
22
/*
3+
* crt0-efi-riscv64-local.S - PE/COFF header for RISC-V 64 EFI applications (without suitable objcopy)
4+
*
35
* Copyright (C) 2014 Linaro Ltd. <[email protected]>
46
* Copyright (C) 2018 Alexander Graf <[email protected]>
57
*
@@ -16,7 +18,6 @@
1618
* either version 2 of the License, or (at your option) any later version.
1719
*/
1820

19-
2021
.section .text.head
2122

2223
/*
@@ -68,7 +69,7 @@ extra_header_fields:
6869
// Everything before the kernel image is considered part of the header
6970
.4byte _text - ImageBase // SizeOfHeaders
7071
.4byte 0 // CheckSum
71-
.2byte EFI_SUBSYSTEM // Subsystem
72+
.2byte EFI_SUBSYSTEM // Subsystem
7273
.2byte 0 // DllCharacteristics
7374
.8byte 0 // SizeOfStackReserve
7475
.8byte 0 // SizeOfStackCommit
@@ -109,6 +110,17 @@ section_table:
109110
.2byte 0 // NumberOfLineNumbers (0 for executables)
110111
.4byte 0x60000020 // Characteristics (section flags)
111112

113+
.ascii ".data\0\0\0"
114+
.4byte _data_vsize - ImageBase // VirtualSize
115+
.4byte _data - ImageBase // VirtualAddress
116+
.4byte _data_size - ImageBase // SizeOfRawData
117+
.4byte _data - ImageBase // PointerToRawData
118+
.4byte 0 // PointerToRelocations
119+
.4byte 0 // PointerToLineNumbers
120+
.2byte 0 // NumberOfRelocations
121+
.2byte 0 // NumberOfLineNumbers
122+
.4byte 0xC0000040 // Characteristics (section flags)
123+
112124
/*
113125
* The EFI application loader requires a relocation section
114126
* because EFI applications must be relocatable. This is a
@@ -125,17 +137,6 @@ section_table:
125137
.2byte 0 // NumberOfLineNumbers
126138
.4byte 0x42000040 // Characteristics (section flags)
127139

128-
.ascii ".data\0\0\0"
129-
.4byte _data_vsize - ImageBase // VirtualSize
130-
.4byte _data - ImageBase // VirtualAddress
131-
.4byte _data_size - ImageBase // SizeOfRawData
132-
.4byte _data - ImageBase // PointerToRawData
133-
.4byte 0 // PointerToRelocations
134-
.4byte 0 // PointerToLineNumbers
135-
.2byte 0 // NumberOfRelocations
136-
.2byte 0 // NumberOfLineNumbers
137-
.4byte 0xC0000040 // Characteristics (section flags)
138-
139140
.ascii ".rodata\0"
140141
.4byte _rodata_vsize - ImageBase // VirtualSize
141142
.4byte _rodata - ImageBase // VirtualAddress
@@ -185,23 +186,24 @@ _start:
185186
lla a0, ImageBase
186187
lla a1, _DYNAMIC
187188
call _relocate
188-
bne a0, zero, 0f
189+
bne a0, zero, .L_exit
189190
ld a1, 8(sp)
190191
ld a0, 0(sp)
191-
call efi_main
192+
call _entry
192193
ld ra, 16(sp)
193-
0: addi sp, sp, 24
194+
.L_exit:
195+
addi sp, sp, 24
194196
ret
195197

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

198200
.data
199-
dummy: .4byte 0
201+
dummy0: .4byte 0
202+
dummy1: .4byte 0
200203

201204
#define IMAGE_REL_ABSOLUTE 0
202-
.section .reloc, "a"
203-
label1:
204-
.4byte dummy-label1 // Page RVA
205+
.section .reloc, "a", %progbits
206+
.4byte dummy1 - dummy0 // Page RVA
205207
.4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
206208
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
207209
.2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy

0 commit comments

Comments
 (0)