xref: /DragonStub/gnuefi/crt0-efi-loongarch64.S (revision f412fd2a1a248b546b7085648dece8d908077fab)
1/*
2 * crt0-efi-loongarch64.S - PE/COFF header for LoongArch64 EFI applications
3 *
4 * Copyright (C) 2021 Loongson Technology Corporation Limited. <zhoumingtao@loongson.cn>
5 * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice and this list of conditions, without modification.
12 * 2. The name of the author may not be used to endorse or promote products
13 *    derived from this software without specific prior written permission.
14 *
15 * Alternatively, this software may be distributed under the terms of the
16 * GNU General Public License as published by the Free Software Foundation;
17 * either version 2 of the License, or (at your option) any later version.
18 */
19
20#ifndef EFI_SUBSYSTEM
21#define EFI_SUBSYSTEM 0xa
22#endif
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	0x6264				// loongarch64 little endian
38	.2byte	2				// nr_sections
39	.4byte	0 				// TimeDateStamp
40	.4byte	0				// PointerToSymbolTable
41	.4byte	1				// 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	_edata - _start			// SizeOfCode
52	.4byte	0				// SizeOfInitializedData
53	.4byte	0				// SizeOfUninitializedData
54	.4byte	_start - ImageBase		// AddressOfEntryPoint
55	.4byte	_start - ImageBase		// BaseOfCode
56
57extra_header_fields:
58	.8byte	0				// ImageBase
59	.4byte	0x20				// SectionAlignment
60	.4byte	0x8				// 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	_edata - ImageBase		// SizeOfImage
70
71	// Everything before the kernel image is considered part of the header
72	.4byte	_start - 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	.8byte	0				// BaseRelocationTable
89	.8byte	0				// Debug
90	.8byte	0				// Architecture
91	.8byte	0				// Global Ptr
92	.8byte	0				// TLS Table
93	.8byte	0				// Load Config Table
94	.8byte	0				// Bound Import
95	.8byte	0				// IAT
96	.8byte	0				// Delay Import Descriptor
97	.8byte	0				// CLR Runtime Header
98	.8byte	0				// Reserved, must be zero
99
100	// Section table
101section_table:
102
103	/*
104	 * The EFI application loader requires a relocation section
105	 * because EFI applications must be relocatable.  This is a
106	 * dummy section as far as we are concerned.
107	 */
108	.ascii	".reloc"
109	.byte	0
110	.byte	0			// end of 0 padding of section name
111	.4byte	0
112	.4byte	0
113	.4byte	0			// SizeOfRawData
114	.4byte	0			// PointerToRawData
115	.4byte	0			// PointerToRelocations
116	.4byte	0			// PointerToLineNumbers
117	.2byte	0			// NumberOfRelocations
118	.2byte	0			// NumberOfLineNumbers
119	.4byte	0x42100040		// Characteristics (section flags)
120
121
122	.ascii	".text"
123	.byte	0
124	.byte	0
125	.byte	0        		// end of 0 padding of section name
126	.4byte	_edata - _start		// VirtualSize
127	.4byte	_start - ImageBase	// VirtualAddress
128	.4byte	_edata - _start		// SizeOfRawData
129	.4byte	_start - ImageBase	// PointerToRawData
130
131	.4byte	0			// PointerToRelocations (0 for executables)
132	.4byte	0			// PointerToLineNumbers (0 for executables)
133	.2byte	0			// NumberOfRelocations  (0 for executables)
134	.2byte	0			// NumberOfLineNumbers  (0 for executables)
135	.4byte	0xe0500020		// Characteristics (section flags)
136
137	.align  4
138
139	.globl	_start
140	.type	_start, @function
141_start:
142	addi.d	  $sp, $sp, -24
143	st.d	  $ra, $sp, 0
144	st.d	  $a0, $sp, 8
145	st.d 	  $a1, $sp, 16
146
147	move	  $a2, $a0		// a2: ImageHandle
148	move	  $a3, $a1 		// a3: SystemTable
149	la.local  $a0, ImageBase	// a0: ImageBase
150	la.local  $a1, _DYNAMIC		// a1: DynamicSection
151	bl        _relocate
152	bnez	  $a0, 0f
153
154	ld.d	  $a0, $sp, 8
155	ld.d	  $a1, $sp, 16
156	bl        efi_main
157
1580:	ld.d	  $ra, $sp, 0
159	addi.d	  $sp, $sp, 24
160	jr	      $ra
161	.end	  _start
162