xref: /DragonStub/gnuefi/crt0-efi-loongarch64.S (revision 039ca9d93c1eb5601f5b1a8bd812fd4b7d4de370)
1e7db4418Szhoumingtao/*
25849eacdSzhoumingtao * crt0-efi-loongarch64.S - PE/COFF header for LoongArch64 EFI applications
3e7db4418Szhoumingtao *
4e7db4418Szhoumingtao * Copyright (C) 2021 Loongson Technology Corporation Limited. <zhoumingtao@loongson.cn>
5e7db4418Szhoumingtao * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6e7db4418Szhoumingtao *
7e7db4418Szhoumingtao * Redistribution and use in source and binary forms, with or without
8e7db4418Szhoumingtao * modification, are permitted provided that the following conditions
9e7db4418Szhoumingtao * are met:
10e7db4418Szhoumingtao * 1. Redistributions of source code must retain the above copyright
11e7db4418Szhoumingtao *    notice and this list of conditions, without modification.
12e7db4418Szhoumingtao * 2. The name of the author may not be used to endorse or promote products
13e7db4418Szhoumingtao *    derived from this software without specific prior written permission.
14e7db4418Szhoumingtao *
15e7db4418Szhoumingtao * Alternatively, this software may be distributed under the terms of the
16e7db4418Szhoumingtao * GNU General Public License as published by the Free Software Foundation;
17e7db4418Szhoumingtao * either version 2 of the License, or (at your option) any later version.
18e7db4418Szhoumingtao */
19e7db4418Szhoumingtao
205849eacdSzhoumingtao#ifndef EFI_SUBSYSTEM
215849eacdSzhoumingtao#define EFI_SUBSYSTEM 0xa
225849eacdSzhoumingtao#endif
23e7db4418Szhoumingtao	.section	.text.head
24e7db4418Szhoumingtao
25e7db4418Szhoumingtao	/*
26e7db4418Szhoumingtao	 * Magic "MZ" signature for PE/COFF
27e7db4418Szhoumingtao	 */
28e7db4418Szhoumingtao	.globl	ImageBase
29e7db4418SzhoumingtaoImageBase:
30e7db4418Szhoumingtao	.ascii	"MZ"
31e7db4418Szhoumingtao	.skip	58				// 'MZ' + pad + offset == 64
329e6cb215SCallum Farmer	.4byte	pe_header - ImageBase		// Offset to the PE header.
33e7db4418Szhoumingtaope_header:
34e7db4418Szhoumingtao	.ascii	"PE"
359e6cb215SCallum Farmer	.2byte 	0
36e7db4418Szhoumingtaocoff_header:
379e6cb215SCallum Farmer	.2byte	0x6264				// loongarch64 little endian
389e6cb215SCallum Farmer	.2byte	2				// nr_sections
399e6cb215SCallum Farmer	.4byte	0 				// TimeDateStamp
409e6cb215SCallum Farmer	.4byte	0				// PointerToSymbolTable
419e6cb215SCallum Farmer	.4byte	1				// NumberOfSymbols
429e6cb215SCallum Farmer	.2byte	section_table - optional_header	// SizeOfOptionalHeader
439e6cb215SCallum Farmer	.2byte	0x206				// Characteristics.
44e7db4418Szhoumingtao						// IMAGE_FILE_DEBUG_STRIPPED |
45e7db4418Szhoumingtao						// IMAGE_FILE_EXECUTABLE_IMAGE |
46e7db4418Szhoumingtao						// IMAGE_FILE_LINE_NUMS_STRIPPED
47e7db4418Szhoumingtaooptional_header:
489e6cb215SCallum Farmer	.2byte	0x20b				// PE32+ format
49e7db4418Szhoumingtao	.byte	0x02				// MajorLinkerVersion
50e7db4418Szhoumingtao	.byte	0x14				// MinorLinkerVersion
519e6cb215SCallum Farmer	.4byte	_edata - _start			// SizeOfCode
529e6cb215SCallum Farmer	.4byte	0				// SizeOfInitializedData
539e6cb215SCallum Farmer	.4byte	0				// SizeOfUninitializedData
549e6cb215SCallum Farmer	.4byte	_start - ImageBase		// AddressOfEntryPoint
559e6cb215SCallum Farmer	.4byte	_start - ImageBase		// BaseOfCode
56e7db4418Szhoumingtao
57e7db4418Szhoumingtaoextra_header_fields:
589e6cb215SCallum Farmer	.8byte	0				// ImageBase
599e6cb215SCallum Farmer	.4byte	0x20				// SectionAlignment
609e6cb215SCallum Farmer	.4byte	0x8				// 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
68e7db4418Szhoumingtao
699e6cb215SCallum Farmer	.4byte	_edata - ImageBase		// SizeOfImage
70e7db4418Szhoumingtao
71e7db4418Szhoumingtao	// 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
81*039ca9d9SCallum Farmer	.4byte	0x10				// NumberOfRvaAndSizes
82e7db4418Szhoumingtao
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
89*039ca9d9SCallum Farmer	.8byte	0				// Debug
90*039ca9d9SCallum Farmer	.8byte	0				// Architecture
91*039ca9d9SCallum Farmer	.8byte	0				// Global Ptr
92*039ca9d9SCallum Farmer	.8byte	0				// TLS Table
93*039ca9d9SCallum Farmer	.8byte	0				// Load Config Table
94*039ca9d9SCallum Farmer	.8byte	0				// Bound Import
95*039ca9d9SCallum Farmer	.8byte	0				// IAT
96*039ca9d9SCallum Farmer	.8byte	0				// Delay Import Descriptor
97*039ca9d9SCallum Farmer	.8byte	0				// CLR Runtime Header
98*039ca9d9SCallum Farmer	.8byte	0				// Reserved, must be zero
99e7db4418Szhoumingtao
100e7db4418Szhoumingtao	// Section table
101e7db4418Szhoumingtaosection_table:
102e7db4418Szhoumingtao
103e7db4418Szhoumingtao	/*
104e7db4418Szhoumingtao	 * The EFI application loader requires a relocation section
105e7db4418Szhoumingtao	 * because EFI applications must be relocatable.  This is a
106e7db4418Szhoumingtao	 * dummy section as far as we are concerned.
107e7db4418Szhoumingtao	 */
108e7db4418Szhoumingtao	.ascii	".reloc"
109e7db4418Szhoumingtao	.byte	0
110e7db4418Szhoumingtao	.byte	0			// end of 0 padding of section name
1119e6cb215SCallum Farmer	.4byte	0
1129e6cb215SCallum Farmer	.4byte	0
1139e6cb215SCallum Farmer	.4byte	0			// SizeOfRawData
1149e6cb215SCallum Farmer	.4byte	0			// PointerToRawData
1159e6cb215SCallum Farmer	.4byte	0			// PointerToRelocations
1169e6cb215SCallum Farmer	.4byte	0			// PointerToLineNumbers
1179e6cb215SCallum Farmer	.2byte	0			// NumberOfRelocations
1189e6cb215SCallum Farmer	.2byte	0			// NumberOfLineNumbers
1199e6cb215SCallum Farmer	.4byte	0x42100040		// Characteristics (section flags)
120e7db4418Szhoumingtao
121e7db4418Szhoumingtao
122e7db4418Szhoumingtao	.ascii	".text"
123e7db4418Szhoumingtao	.byte	0
124e7db4418Szhoumingtao	.byte	0
125e7db4418Szhoumingtao	.byte	0        		// end of 0 padding of section name
1269e6cb215SCallum Farmer	.4byte	_edata - _start		// VirtualSize
1279e6cb215SCallum Farmer	.4byte	_start - ImageBase	// VirtualAddress
1289e6cb215SCallum Farmer	.4byte	_edata - _start		// SizeOfRawData
1299e6cb215SCallum Farmer	.4byte	_start - ImageBase	// PointerToRawData
130e7db4418Szhoumingtao
1319e6cb215SCallum Farmer	.4byte	0			// PointerToRelocations (0 for executables)
1329e6cb215SCallum Farmer	.4byte	0			// PointerToLineNumbers (0 for executables)
1339e6cb215SCallum Farmer	.2byte	0			// NumberOfRelocations  (0 for executables)
1349e6cb215SCallum Farmer	.2byte	0			// NumberOfLineNumbers  (0 for executables)
1359e6cb215SCallum Farmer	.4byte	0xe0500020		// Characteristics (section flags)
136e7db4418Szhoumingtao
137e7db4418Szhoumingtao	.align  4
138e7db4418Szhoumingtao
139e7db4418Szhoumingtao	.globl	_start
140e7db4418Szhoumingtao	.type	_start, @function
141e7db4418Szhoumingtao_start:
142e7db4418Szhoumingtao	addi.d	  $sp, $sp, -24
143e7db4418Szhoumingtao	st.d	  $ra, $sp, 0
144e7db4418Szhoumingtao	st.d	  $a0, $sp, 8
145e7db4418Szhoumingtao	st.d 	  $a1, $sp, 16
146e7db4418Szhoumingtao
147e7db4418Szhoumingtao	move	  $a2, $a0		// a2: ImageHandle
148e7db4418Szhoumingtao	move	  $a3, $a1 		// a3: SystemTable
149e7db4418Szhoumingtao	la.local  $a0, ImageBase	// a0: ImageBase
150e7db4418Szhoumingtao	la.local  $a1, _DYNAMIC		// a1: DynamicSection
151e7db4418Szhoumingtao	bl        _relocate
152e7db4418Szhoumingtao	bnez	  $a0, 0f
153e7db4418Szhoumingtao
154e7db4418Szhoumingtao	ld.d	  $a0, $sp, 8
155e7db4418Szhoumingtao	ld.d	  $a1, $sp, 16
156e7db4418Szhoumingtao	bl        efi_main
157e7db4418Szhoumingtao
158e7db4418Szhoumingtao0:	ld.d	  $ra, $sp, 0
159e7db4418Szhoumingtao	addi.d	  $sp, $sp, 24
1605849eacdSzhoumingtao	jr	      $ra
161e7db4418Szhoumingtao	.end	  _start
162