xref: /DragonStub/gnuefi/crt0-efi-loongarch64.S (revision 584425eb2a2cc84186ee6b025b6fc91601c6be5f)
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	0x6				// 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
90	// Section table
91section_table:
92
93	/*
94	 * The EFI application loader requires a relocation section
95	 * because EFI applications must be relocatable.  This is a
96	 * dummy section as far as we are concerned.
97	 */
98	.ascii	".reloc"
99	.byte	0
100	.byte	0			// end of 0 padding of section name
101	.4byte	0
102	.4byte	0
103	.4byte	0			// SizeOfRawData
104	.4byte	0			// PointerToRawData
105	.4byte	0			// PointerToRelocations
106	.4byte	0			// PointerToLineNumbers
107	.2byte	0			// NumberOfRelocations
108	.2byte	0			// NumberOfLineNumbers
109	.4byte	0x42100040		// Characteristics (section flags)
110
111
112	.ascii	".text"
113	.byte	0
114	.byte	0
115	.byte	0        		// end of 0 padding of section name
116	.4byte	_edata - _start		// VirtualSize
117	.4byte	_start - ImageBase	// VirtualAddress
118	.4byte	_edata - _start		// SizeOfRawData
119	.4byte	_start - ImageBase	// PointerToRawData
120
121	.4byte	0			// PointerToRelocations (0 for executables)
122	.4byte	0			// PointerToLineNumbers (0 for executables)
123	.2byte	0			// NumberOfRelocations  (0 for executables)
124	.2byte	0			// NumberOfLineNumbers  (0 for executables)
125	.4byte	0xe0500020		// Characteristics (section flags)
126
127	.align  4
128
129	.globl	_start
130	.type	_start, @function
131_start:
132	addi.d	  $sp, $sp, -24
133	st.d	  $ra, $sp, 0
134	st.d	  $a0, $sp, 8
135	st.d 	  $a1, $sp, 16
136
137	move	  $a2, $a0		// a2: ImageHandle
138	move	  $a3, $a1 		// a3: SystemTable
139	la.local  $a0, ImageBase	// a0: ImageBase
140	la.local  $a1, _DYNAMIC		// a1: DynamicSection
141	bl        _relocate
142	bnez	  $a0, 0f
143
144	ld.d	  $a0, $sp, 8
145	ld.d	  $a1, $sp, 16
146	bl        efi_main
147
1480:	ld.d	  $ra, $sp, 0
149	addi.d	  $sp, $sp, 24
150	jr	      $ra
151	.end	  _start
152