xref: /DragonStub/gnuefi/crt0-efi-riscv64.S (revision deb8a7f267d96299b9aa41300539f617db54c2a9)
1/* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
2/*
3 * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
4 * Copright (C) 2018 Alexander Graf <agraf@suse.de>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice and this list of conditions, without modification.
11 * 2. The name of the author may not be used to endorse or promote products
12 *    derived from this software without specific prior written permission.
13 *
14 * Alternatively, this software may be distributed under the terms of the
15 * GNU General Public License as published by the Free Software Foundation;
16 * either version 2 of the License, or (at your option) any later version.
17 */
18
19#ifndef EFI_SUBSYSTEM
20#define EFI_SUBSYSTEM 10
21#endif
22
23	.section	.text.head
24
25	/*
26	 * Magic "MZ" signature for PE/COFF
27	 */
28	.globl	ImageBase
29ImageBase:
30	.ascii	"MZ"
31	.skip	58				// 'MZ' + pad + offset == 64
32	.4byte	pe_header - ImageBase		// Offset to the PE header.
33pe_header:
34	.ascii	"PE"
35	.2byte 	0
36coff_header:
37	.2byte	0x5064				// riscv64
38	.2byte	4				// nr_sections
39	.4byte	0 				// TimeDateStamp
40	.4byte	0				// PointerToSymbolTable
41	.4byte	0				// NumberOfSymbols
42	.2byte	section_table - optional_header	// SizeOfOptionalHeader
43	.2byte	0x206				// Characteristics.
44						// IMAGE_FILE_DEBUG_STRIPPED |
45						// IMAGE_FILE_EXECUTABLE_IMAGE |
46						// IMAGE_FILE_LINE_NUMS_STRIPPED
47optional_header:
48	.2byte	0x20b				// PE32+ format
49	.byte	0x02				// MajorLinkerVersion
50	.byte	0x14				// MinorLinkerVersion
51	.4byte	_text_size - ImageBase		// SizeOfCode
52	.4byte	_alldata_size - ImageBase		// SizeOfInitializedData
53	.4byte	0				// SizeOfUninitializedData
54	.4byte	_start - ImageBase		// AddressOfEntryPoint
55	.4byte	_text - ImageBase		// BaseOfCode
56
57extra_header_fields:
58	.8byte	0				// ImageBase
59	.4byte	0x1000				// SectionAlignment
60	.4byte	0x1000				// FileAlignment
61	.2byte	0				// MajorOperatingSystemVersion
62	.2byte	0				// MinorOperatingSystemVersion
63	.2byte	0				// MajorImageVersion
64	.2byte	0				// MinorImageVersion
65	.2byte	0				// MajorSubsystemVersion
66	.2byte	0				// MinorSubsystemVersion
67	.4byte	0				// Win32VersionValue
68
69	.4byte	_image_end - ImageBase		// SizeOfImage
70
71	// Everything before the kernel image is considered part of the header
72	.4byte	_text - ImageBase		// SizeOfHeaders
73	.4byte	0				// CheckSum
74	.2byte	EFI_SUBSYSTEM			// Subsystem
75	.2byte	0				// DllCharacteristics
76	.8byte	0				// SizeOfStackReserve
77	.8byte	0				// SizeOfStackCommit
78	.8byte	0				// SizeOfHeapReserve
79	.8byte	0				// SizeOfHeapCommit
80	.4byte	0				// LoaderFlags
81	.4byte	0x10				// NumberOfRvaAndSizes
82
83	.8byte	0				// ExportTable
84	.8byte	0				// ImportTable
85	.8byte	0				// ResourceTable
86	.8byte	0				// ExceptionTable
87	.8byte	0				// CertificationTable
88	.4byte	_reloc - ImageBase				// BaseRelocationTable (VirtualAddress)
89	.4byte	_reloc_vsize - ImageBase				// BaseRelocationTable (Size)
90	.8byte	0				// Debug
91	.8byte	0				// Architecture
92	.8byte	0				// Global Ptr
93	.8byte	0				// TLS Table
94	.8byte	0				// Load Config Table
95	.8byte	0				// Bound Import
96	.8byte	0				// IAT
97	.8byte	0				// Delay Import Descriptor
98	.8byte	0				// CLR Runtime Header
99	.8byte	0				// Reserved, must be zero
100
101	// Section table
102section_table:
103
104	.ascii	".text\0\0\0"
105	.4byte	_text_vsize - ImageBase		// VirtualSize
106	.4byte	_text - ImageBase	// VirtualAddress
107	.4byte	_text_size - ImageBase		// SizeOfRawData
108	.4byte	_text - ImageBase	// PointerToRawData
109	.4byte	0		// PointerToRelocations (0 for executables)
110	.4byte	0		// PointerToLineNumbers (0 for executables)
111	.2byte	0		// NumberOfRelocations  (0 for executables)
112	.2byte	0		// NumberOfLineNumbers  (0 for executables)
113	.4byte	0x60000020	// Characteristics (section flags)
114
115	/*
116	 * The EFI application loader requires a relocation section
117	 * because EFI applications must be relocatable.  This is a
118	 * dummy section as far as we are concerned.
119	 */
120	.ascii	".reloc\0\0"
121	.4byte	_reloc_vsize - ImageBase			// VirtualSize
122	.4byte	_reloc - ImageBase			// VirtualAddress
123	.4byte	_reloc_size - ImageBase			// SizeOfRawData
124	.4byte	_reloc - ImageBase			// PointerToRawData
125	.4byte	0			// PointerToRelocations
126	.4byte	0			// PointerToLineNumbers
127	.2byte	0			// NumberOfRelocations
128	.2byte	0			// NumberOfLineNumbers
129	.4byte	0x42000040		// Characteristics (section flags)
130
131	.ascii	".data\0\0\0"
132	.4byte	_data_vsize - ImageBase			// VirtualSize
133	.4byte	_data - ImageBase			// VirtualAddress
134	.4byte	_data_size - ImageBase			// SizeOfRawData
135	.4byte	_data - ImageBase			// PointerToRawData
136	.4byte	0			// PointerToRelocations
137	.4byte	0			// PointerToLineNumbers
138	.2byte	0			// NumberOfRelocations
139	.2byte	0			// NumberOfLineNumbers
140	.4byte	0xC0000040		// Characteristics (section flags)
141
142	.ascii	".rodata\0"
143	.4byte	_rodata_vsize - ImageBase			// VirtualSize
144	.4byte	_rodata - ImageBase			// VirtualAddress
145	.4byte	_rodata_size - ImageBase			// SizeOfRawData
146	.4byte	_rodata - ImageBase			// PointerToRawData
147	.4byte	0			// PointerToRelocations
148	.4byte	0			// PointerToLineNumbers
149	.2byte	0			// NumberOfRelocations
150	.2byte	0			// NumberOfLineNumbers
151	.4byte	0x40000040		// Characteristics (section flags)
152
153	.text
154	.globl _start
155	.type _start,%function
156_start:
157	addi		sp, sp, -24
158	sd		a0, 0(sp)
159	sd		a1, 8(sp)
160	sd		ra, 16(sp)
161	lla		a0, ImageBase
162	lla		a1, _DYNAMIC
163	call		_relocate
164	bne		a0, zero, 0f
165	ld		a1, 8(sp)
166	ld		a0, 0(sp)
167	call		_entry
168	ld		ra, 16(sp)
1690:	addi		sp, sp, 24
170	ret
171
172// hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
173
174 	.data
175dummy:	.4byte	0
176
177#define IMAGE_REL_ABSOLUTE	0
178 	.section .reloc, "a"
179label1:
180	.4byte	dummy-label1				// Page RVA
181	.4byte	12					// Block Size (2*4+2*2), must be aligned by 32 Bits
182	.2byte	(IMAGE_REL_ABSOLUTE<<12) +  0		// reloc for dummy
183	.2byte	(IMAGE_REL_ABSOLUTE<<12) +  0		// reloc for dummy
184
185#if defined(__ELF__) && defined(__linux__)
186	.section .note.GNU-stack,"",%progbits
187#endif
188