xref: /DragonStub/gnuefi/crt0-efi-riscv64.S (revision 58dce6991b675f6ad063bbb5a362817ba5ba55a0)
10f9c15feSbmeng@tinylab.org/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
2b95f138fSHeinrich Schuchardt/*
3b95f138fSHeinrich Schuchardt * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
4b95f138fSHeinrich Schuchardt * Copright (C) 2018 Alexander Graf <agraf@suse.de>
5b95f138fSHeinrich Schuchardt *
6b95f138fSHeinrich Schuchardt * Redistribution and use in source and binary forms, with or without
7b95f138fSHeinrich Schuchardt * modification, are permitted provided that the following conditions
8b95f138fSHeinrich Schuchardt * are met:
9b95f138fSHeinrich Schuchardt * 1. Redistributions of source code must retain the above copyright
10b95f138fSHeinrich Schuchardt *    notice and this list of conditions, without modification.
11b95f138fSHeinrich Schuchardt * 2. The name of the author may not be used to endorse or promote products
12b95f138fSHeinrich Schuchardt *    derived from this software without specific prior written permission.
13b95f138fSHeinrich Schuchardt *
14b95f138fSHeinrich Schuchardt * Alternatively, this software may be distributed under the terms of the
15b95f138fSHeinrich Schuchardt * GNU General Public License as published by the Free Software Foundation;
16b95f138fSHeinrich Schuchardt * either version 2 of the License, or (at your option) any later version.
17b95f138fSHeinrich Schuchardt */
18b95f138fSHeinrich Schuchardt
19b95f138fSHeinrich Schuchardt#ifndef EFI_SUBSYSTEM
20b95f138fSHeinrich Schuchardt#define EFI_SUBSYSTEM 10
21b95f138fSHeinrich Schuchardt#endif
22b95f138fSHeinrich Schuchardt
23b95f138fSHeinrich Schuchardt	.section	.text.head
24b95f138fSHeinrich Schuchardt
25b95f138fSHeinrich Schuchardt	/*
26b95f138fSHeinrich Schuchardt	 * Magic "MZ" signature for PE/COFF
27b95f138fSHeinrich Schuchardt	 */
28b95f138fSHeinrich Schuchardt	.globl	ImageBase
29b95f138fSHeinrich SchuchardtImageBase:
30b95f138fSHeinrich Schuchardt	.ascii	"MZ"
31b95f138fSHeinrich Schuchardt	.skip	58				// 'MZ' + pad + offset == 64
329e6cb215SCallum Farmer	.4byte	pe_header - ImageBase		// Offset to the PE header.
33b95f138fSHeinrich Schuchardtpe_header:
34b95f138fSHeinrich Schuchardt	.ascii	"PE"
359e6cb215SCallum Farmer	.2byte 	0
36b95f138fSHeinrich Schuchardtcoff_header:
379e6cb215SCallum Farmer	.2byte	0x5064				// riscv64
38*58dce699SCallum Farmer	.2byte	4				// nr_sections
399e6cb215SCallum Farmer	.4byte	0 				// TimeDateStamp
409e6cb215SCallum Farmer	.4byte	0				// PointerToSymbolTable
419e6cb215SCallum Farmer	.4byte	0				// NumberOfSymbols
429e6cb215SCallum Farmer	.2byte	section_table - optional_header	// SizeOfOptionalHeader
439e6cb215SCallum Farmer	.2byte	0x206				// Characteristics.
44b95f138fSHeinrich Schuchardt						// IMAGE_FILE_DEBUG_STRIPPED |
45b95f138fSHeinrich Schuchardt						// IMAGE_FILE_EXECUTABLE_IMAGE |
46b95f138fSHeinrich Schuchardt						// IMAGE_FILE_LINE_NUMS_STRIPPED
47b95f138fSHeinrich Schuchardtoptional_header:
489e6cb215SCallum Farmer	.2byte	0x20b				// PE32+ format
49b95f138fSHeinrich Schuchardt	.byte	0x02				// MajorLinkerVersion
50b95f138fSHeinrich Schuchardt	.byte	0x14				// MinorLinkerVersion
51*58dce699SCallum Farmer	.4byte	_text_size - ImageBase		// SizeOfCode
52*58dce699SCallum Farmer	.4byte	_alldata_size - ImageBase		// SizeOfInitializedData
539e6cb215SCallum Farmer	.4byte	0				// SizeOfUninitializedData
549e6cb215SCallum Farmer	.4byte	_start - ImageBase		// AddressOfEntryPoint
55*58dce699SCallum Farmer	.4byte	_text - ImageBase		// BaseOfCode
56b95f138fSHeinrich Schuchardt
57b95f138fSHeinrich Schuchardtextra_header_fields:
589e6cb215SCallum Farmer	.8byte	0				// ImageBase
599e6cb215SCallum Farmer	.4byte	0x1000				// SectionAlignment
60*58dce699SCallum Farmer	.4byte	0x1000				// FileAlignment
619e6cb215SCallum Farmer	.2byte	0				// MajorOperatingSystemVersion
629e6cb215SCallum Farmer	.2byte	0				// MinorOperatingSystemVersion
639e6cb215SCallum Farmer	.2byte	0				// MajorImageVersion
649e6cb215SCallum Farmer	.2byte	0				// MinorImageVersion
659e6cb215SCallum Farmer	.2byte	0				// MajorSubsystemVersion
669e6cb215SCallum Farmer	.2byte	0				// MinorSubsystemVersion
679e6cb215SCallum Farmer	.4byte	0				// Win32VersionValue
68b95f138fSHeinrich Schuchardt
69*58dce699SCallum Farmer	.4byte	_image_end - ImageBase		// SizeOfImage
70b95f138fSHeinrich Schuchardt
71b95f138fSHeinrich Schuchardt	// Everything before the kernel image is considered part of the header
72*58dce699SCallum Farmer	.4byte	_text - ImageBase		// SizeOfHeaders
739e6cb215SCallum Farmer	.4byte	0				// CheckSum
749e6cb215SCallum Farmer	.2byte	EFI_SUBSYSTEM			// Subsystem
759e6cb215SCallum Farmer	.2byte	0				// DllCharacteristics
769e6cb215SCallum Farmer	.8byte	0				// SizeOfStackReserve
779e6cb215SCallum Farmer	.8byte	0				// SizeOfStackCommit
789e6cb215SCallum Farmer	.8byte	0				// SizeOfHeapReserve
799e6cb215SCallum Farmer	.8byte	0				// SizeOfHeapCommit
809e6cb215SCallum Farmer	.4byte	0				// LoaderFlags
81039ca9d9SCallum Farmer	.4byte	0x10				// NumberOfRvaAndSizes
82b95f138fSHeinrich Schuchardt
839e6cb215SCallum Farmer	.8byte	0				// ExportTable
849e6cb215SCallum Farmer	.8byte	0				// ImportTable
859e6cb215SCallum Farmer	.8byte	0				// ResourceTable
869e6cb215SCallum Farmer	.8byte	0				// ExceptionTable
879e6cb215SCallum Farmer	.8byte	0				// CertificationTable
88*58dce699SCallum Farmer	.4byte	_reloc - ImageBase				// BaseRelocationTable (VirtualAddress)
89*58dce699SCallum Farmer	.4byte	_reloc_vsize - ImageBase				// BaseRelocationTable (Size)
90039ca9d9SCallum Farmer	.8byte	0				// Debug
91039ca9d9SCallum Farmer	.8byte	0				// Architecture
92039ca9d9SCallum Farmer	.8byte	0				// Global Ptr
93039ca9d9SCallum Farmer	.8byte	0				// TLS Table
94039ca9d9SCallum Farmer	.8byte	0				// Load Config Table
95039ca9d9SCallum Farmer	.8byte	0				// Bound Import
96039ca9d9SCallum Farmer	.8byte	0				// IAT
97039ca9d9SCallum Farmer	.8byte	0				// Delay Import Descriptor
98039ca9d9SCallum Farmer	.8byte	0				// CLR Runtime Header
99039ca9d9SCallum Farmer	.8byte	0				// Reserved, must be zero
100b95f138fSHeinrich Schuchardt
101b95f138fSHeinrich Schuchardt	// Section table
102b95f138fSHeinrich Schuchardtsection_table:
103*58dce699SCallum Farmer
104*58dce699SCallum Farmer	.ascii	".text\0\0\0"
105*58dce699SCallum Farmer	.4byte	_text_vsize - ImageBase		// VirtualSize
106*58dce699SCallum Farmer	.4byte	_text - ImageBase	// VirtualAddress
107*58dce699SCallum Farmer	.4byte	_text_size - ImageBase		// SizeOfRawData
108*58dce699SCallum Farmer	.4byte	_text - ImageBase	// PointerToRawData
109*58dce699SCallum Farmer	.4byte	0		// PointerToRelocations (0 for executables)
110*58dce699SCallum Farmer	.4byte	0		// PointerToLineNumbers (0 for executables)
111*58dce699SCallum Farmer	.2byte	0		// NumberOfRelocations  (0 for executables)
112*58dce699SCallum Farmer	.2byte	0		// NumberOfLineNumbers  (0 for executables)
113*58dce699SCallum Farmer	.4byte	0x60000020	// Characteristics (section flags)
114*58dce699SCallum Farmer
115b95f138fSHeinrich Schuchardt	/*
116b95f138fSHeinrich Schuchardt	 * The EFI application loader requires a relocation section
117b95f138fSHeinrich Schuchardt	 * because EFI applications must be relocatable.  This is a
118b95f138fSHeinrich Schuchardt	 * dummy section as far as we are concerned.
119b95f138fSHeinrich Schuchardt	 */
120b95f138fSHeinrich Schuchardt	.ascii	".reloc\0\0"
121*58dce699SCallum Farmer	.4byte	_reloc_vsize - ImageBase			// VirtualSize
122*58dce699SCallum Farmer	.4byte	_reloc - ImageBase			// VirtualAddress
123*58dce699SCallum Farmer	.4byte	_reloc_size - ImageBase			// SizeOfRawData
124*58dce699SCallum Farmer	.4byte	_reloc - ImageBase			// PointerToRawData
1259e6cb215SCallum Farmer	.4byte	0			// PointerToRelocations
1269e6cb215SCallum Farmer	.4byte	0			// PointerToLineNumbers
1279e6cb215SCallum Farmer	.2byte	0			// NumberOfRelocations
1289e6cb215SCallum Farmer	.2byte	0			// NumberOfLineNumbers
129*58dce699SCallum Farmer	.4byte	0x42000040		// Characteristics (section flags)
130b95f138fSHeinrich Schuchardt
131*58dce699SCallum Farmer	.ascii	".data\0\0\0"
132*58dce699SCallum Farmer	.4byte	_data_vsize - ImageBase			// VirtualSize
133*58dce699SCallum Farmer	.4byte	_data - ImageBase			// VirtualAddress
134*58dce699SCallum Farmer	.4byte	_data_size - ImageBase			// SizeOfRawData
135*58dce699SCallum Farmer	.4byte	_data - ImageBase			// PointerToRawData
136*58dce699SCallum Farmer	.4byte	0			// PointerToRelocations
137*58dce699SCallum Farmer	.4byte	0			// PointerToLineNumbers
138*58dce699SCallum Farmer	.2byte	0			// NumberOfRelocations
139*58dce699SCallum Farmer	.2byte	0			// NumberOfLineNumbers
140*58dce699SCallum Farmer	.4byte	0xC0000040		// Characteristics (section flags)
141b95f138fSHeinrich Schuchardt
142*58dce699SCallum Farmer	.ascii	".rodata\0"
143*58dce699SCallum Farmer	.4byte	_rodata_vsize - ImageBase			// VirtualSize
144*58dce699SCallum Farmer	.4byte	_rodata - ImageBase			// VirtualAddress
145*58dce699SCallum Farmer	.4byte	_rodata_size - ImageBase			// SizeOfRawData
146*58dce699SCallum Farmer	.4byte	_rodata - ImageBase			// PointerToRawData
147*58dce699SCallum Farmer	.4byte	0			// PointerToRelocations
148*58dce699SCallum Farmer	.4byte	0			// PointerToLineNumbers
149*58dce699SCallum Farmer	.2byte	0			// NumberOfRelocations
150*58dce699SCallum Farmer	.2byte	0			// NumberOfLineNumbers
151*58dce699SCallum Farmer	.4byte	0x40000040		// Characteristics (section flags)
152b95f138fSHeinrich Schuchardt
153*58dce699SCallum Farmer	.text
154b95f138fSHeinrich Schuchardt	.globl _start
15564027ee9SCallum Farmer	.type _start,%function
156b95f138fSHeinrich Schuchardt_start:
157b95f138fSHeinrich Schuchardt	addi		sp, sp, -24
158b95f138fSHeinrich Schuchardt	sd		a0, 0(sp)
159b95f138fSHeinrich Schuchardt	sd		a1, 8(sp)
160b95f138fSHeinrich Schuchardt	sd		ra, 16(sp)
161b95f138fSHeinrich Schuchardt	lla		a0, ImageBase
162b95f138fSHeinrich Schuchardt	lla		a1, _DYNAMIC
163b95f138fSHeinrich Schuchardt	call		_relocate
164b95f138fSHeinrich Schuchardt	bne		a0, zero, 0f
165b95f138fSHeinrich Schuchardt	ld		a1, 8(sp)
166b95f138fSHeinrich Schuchardt	ld		a0, 0(sp)
167d96b34e0SCallum Farmer	call		_entry
168b95f138fSHeinrich Schuchardt	ld		ra, 16(sp)
169b95f138fSHeinrich Schuchardt0:	addi		sp, sp, 24
170b95f138fSHeinrich Schuchardt	ret
171803b49c4SSergei Trofimovich
172*58dce699SCallum Farmer// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
173*58dce699SCallum Farmer
174*58dce699SCallum Farmer 	.data
175*58dce699SCallum Farmerdummy:	.4byte	0
176*58dce699SCallum Farmer
177*58dce699SCallum Farmer#define IMAGE_REL_ABSOLUTE	0
178*58dce699SCallum Farmer 	.section .reloc, "a"
179*58dce699SCallum Farmerlabel1:
180*58dce699SCallum Farmer	.4byte	dummy-label1				// Page RVA
181*58dce699SCallum Farmer	.4byte	12					// Block Size (2*4+2*2), must be aligned by 32 Bits
182*58dce699SCallum Farmer	.2byte	(IMAGE_REL_ABSOLUTE<<12) +  0		// reloc for dummy
183*58dce699SCallum Farmer	.2byte	(IMAGE_REL_ABSOLUTE<<12) +  0		// reloc for dummy
184*58dce699SCallum Farmer
185803b49c4SSergei Trofimovich#if defined(__ELF__) && defined(__linux__)
186803b49c4SSergei Trofimovich	.section .note.GNU-stack,"",%progbits
187803b49c4SSergei Trofimovich#endif
188