xref: /DragonStub/gnuefi/crt0-efi-riscv64.S (revision d96b34e0ddc4ccfeefd285cc956060b56b42c6b8)
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
389e6cb215SCallum Farmer	.2byte	2				// 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
519e6cb215SCallum Farmer	.4byte	_data - _start			// SizeOfCode
521e0f0df5SCallum Farmer	.4byte	_edata - _data			// SizeOfInitializedData
539e6cb215SCallum Farmer	.4byte	0				// SizeOfUninitializedData
549e6cb215SCallum Farmer	.4byte	_start - ImageBase		// AddressOfEntryPoint
559e6cb215SCallum Farmer	.4byte	_start - ImageBase		// BaseOfCode
56b95f138fSHeinrich Schuchardt
57b95f138fSHeinrich Schuchardtextra_header_fields:
589e6cb215SCallum Farmer	.8byte	0				// ImageBase
599e6cb215SCallum Farmer	.4byte	0x1000				// SectionAlignment
609e6cb215SCallum Farmer	.4byte	0x200				// 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
699e6cb215SCallum Farmer	.4byte	_edata - ImageBase		// SizeOfImage
70b95f138fSHeinrich Schuchardt
71b95f138fSHeinrich Schuchardt	// Everything before the kernel image is considered part of the header
729e6cb215SCallum Farmer	.4byte	_start - 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
819e6cb215SCallum Farmer	.4byte	0x6				// 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
889e6cb215SCallum Farmer	.8byte	0				// BaseRelocationTable
89b95f138fSHeinrich Schuchardt
90b95f138fSHeinrich Schuchardt	// Section table
91b95f138fSHeinrich Schuchardtsection_table:
92b95f138fSHeinrich Schuchardt	/*
93b95f138fSHeinrich Schuchardt	 * The EFI application loader requires a relocation section
94b95f138fSHeinrich Schuchardt	 * because EFI applications must be relocatable.  This is a
95b95f138fSHeinrich Schuchardt	 * dummy section as far as we are concerned.
96b95f138fSHeinrich Schuchardt	 */
97b95f138fSHeinrich Schuchardt	.ascii	".reloc\0\0"
989e6cb215SCallum Farmer	.4byte	0
999e6cb215SCallum Farmer	.4byte	0
1009e6cb215SCallum Farmer	.4byte	0				// SizeOfRawData
1019e6cb215SCallum Farmer	.4byte	0				// PointerToRawData
1029e6cb215SCallum Farmer	.4byte	0				// PointerToRelocations
1039e6cb215SCallum Farmer	.4byte	0				// PointerToLineNumbers
1049e6cb215SCallum Farmer	.2byte	0				// NumberOfRelocations
1059e6cb215SCallum Farmer	.2byte	0				// NumberOfLineNumbers
1069e6cb215SCallum Farmer	.4byte	0x42100040			// Characteristics (section flags)
107b95f138fSHeinrich Schuchardt
108b95f138fSHeinrich Schuchardt	.ascii	".text\0\0\0"
1099e6cb215SCallum Farmer	.4byte	_edata - _start			// VirtualSize
1109e6cb215SCallum Farmer	.4byte	_start - ImageBase		// VirtualAddress
1119e6cb215SCallum Farmer	.4byte	_edata - _start			// SizeOfRawData
1129e6cb215SCallum Farmer	.4byte	_start - ImageBase		// PointerToRawData
113b95f138fSHeinrich Schuchardt
1149e6cb215SCallum Farmer	.4byte	0				// PointerToRelocations (0 for executables)
1159e6cb215SCallum Farmer	.4byte	0				// PointerToLineNumbers (0 for executables)
1169e6cb215SCallum Farmer	.2byte	0				// NumberOfRelocations  (0 for executables)
1179e6cb215SCallum Farmer	.2byte	0				// NumberOfLineNumbers  (0 for executables)
1189e6cb215SCallum Farmer	.4byte	0xe0500020			// Characteristics (section flags)
119b95f138fSHeinrich Schuchardt
120b95f138fSHeinrich Schuchardt	.align	12
121b95f138fSHeinrich Schuchardt	.globl _start
122b95f138fSHeinrich Schuchardt_start:
123b95f138fSHeinrich Schuchardt	addi		sp, sp, -24
124b95f138fSHeinrich Schuchardt	sd		a0, 0(sp)
125b95f138fSHeinrich Schuchardt	sd		a1, 8(sp)
126b95f138fSHeinrich Schuchardt	sd		ra, 16(sp)
127b95f138fSHeinrich Schuchardt	lla		a0, ImageBase
128b95f138fSHeinrich Schuchardt	lla		a1, _DYNAMIC
129b95f138fSHeinrich Schuchardt	call		_relocate
130b95f138fSHeinrich Schuchardt	bne		a0, zero, 0f
131b95f138fSHeinrich Schuchardt	ld		a1, 8(sp)
132b95f138fSHeinrich Schuchardt	ld		a0, 0(sp)
133*d96b34e0SCallum Farmer	call		_entry
134b95f138fSHeinrich Schuchardt	ld		ra, 16(sp)
135b95f138fSHeinrich Schuchardt0:	addi		sp, sp, 24
136b95f138fSHeinrich Schuchardt	ret
137803b49c4SSergei Trofimovich
138803b49c4SSergei Trofimovich#if defined(__ELF__) && defined(__linux__)
139803b49c4SSergei Trofimovich	.section .note.GNU-stack,"",%progbits
140803b49c4SSergei Trofimovich#endif
141