xref: /DragonStub/gnuefi/crt0-efi-mips64el.S (revision 039ca9d93c1eb5601f5b1a8bd812fd4b7d4de370)
1/*
2 * crt0-efi-mips64el.S - PE/COFF header for MIPS64 EFI applications
3 *
4 * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
5 * Copright (C) 2017 Heiher <r@hev.cc>
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	.section	.text.head
21
22	/*
23	 * Magic "MZ" signature for PE/COFF
24	 */
25	.globl	ImageBase
26ImageBase:
27	.ascii	"MZ"
28	.skip	58				// 'MZ' + pad + offset == 64
29	.4byte	pe_header - ImageBase		// Offset to the PE header.
30pe_header:
31	.ascii	"PE"
32	.2byte 	0
33coff_header:
34	.2byte	0x166				// MIPS little endian
35	.2byte	2				// nr_sections
36	.4byte	0 				// TimeDateStamp
37	.4byte	0				// PointerToSymbolTable
38	.4byte	0				// NumberOfSymbols
39	.2byte	section_table - optional_header	// SizeOfOptionalHeader
40	.2byte	0x206				// Characteristics.
41						// IMAGE_FILE_DEBUG_STRIPPED |
42						// IMAGE_FILE_EXECUTABLE_IMAGE |
43						// IMAGE_FILE_LINE_NUMS_STRIPPED
44optional_header:
45	.2byte	0x20b				// PE32+ format
46	.byte	0x02				// MajorLinkerVersion
47	.byte	0x14				// MinorLinkerVersion
48	.4byte	_edata - _start			// SizeOfCode
49	.4byte	0				// SizeOfInitializedData
50	.4byte	0				// SizeOfUninitializedData
51	.4byte	_start - ImageBase		// AddressOfEntryPoint
52	.4byte	_start - ImageBase		// BaseOfCode
53
54extra_header_fields:
55	.8byte	0				// ImageBase
56	.4byte	0x20				// SectionAlignment
57	.4byte	0x8				// FileAlignment
58	.2byte	0				// MajorOperatingSystemVersion
59	.2byte	0				// MinorOperatingSystemVersion
60	.2byte	0				// MajorImageVersion
61	.2byte	0				// MinorImageVersion
62	.2byte	0				// MajorSubsystemVersion
63	.2byte	0				// MinorSubsystemVersion
64	.4byte	0				// Win32VersionValue
65
66	.4byte	_edata - ImageBase		// SizeOfImage
67
68	// Everything before the kernel image is considered part of the header
69	.4byte	_start - ImageBase		// SizeOfHeaders
70	.4byte	0				// CheckSum
71	.2byte	EFI_SUBSYSTEM			// Subsystem
72	.2byte	0				// DllCharacteristics
73	.8byte	0				// SizeOfStackReserve
74	.8byte	0				// SizeOfStackCommit
75	.8byte	0				// SizeOfHeapReserve
76	.8byte	0				// SizeOfHeapCommit
77	.4byte	0				// LoaderFlags
78	.4byte	0x10				// NumberOfRvaAndSizes
79
80	.8byte	0				// ExportTable
81	.8byte	0				// ImportTable
82	.8byte	0				// ResourceTable
83	.8byte	0				// ExceptionTable
84	.8byte	0				// CertificationTable
85	.8byte	0				// BaseRelocationTable
86	.8byte	0				// Debug
87	.8byte	0				// Architecture
88	.8byte	0				// Global Ptr
89	.8byte	0				// TLS Table
90	.8byte	0				// Load Config Table
91	.8byte	0				// Bound Import
92	.8byte	0				// IAT
93	.8byte	0				// Delay Import Descriptor
94	.8byte	0				// CLR Runtime Header
95	.8byte	0				// Reserved, must be zero
96
97	// Section table
98section_table:
99
100	/*
101	 * The EFI application loader requires a relocation section
102	 * because EFI applications must be relocatable.  This is a
103	 * dummy section as far as we are concerned.
104	 */
105	.ascii	".reloc"
106	.byte	0
107	.byte	0			// end of 0 padding of section name
108	.4byte	0
109	.4byte	0
110	.4byte	0			// SizeOfRawData
111	.4byte	0			// PointerToRawData
112	.4byte	0			// PointerToRelocations
113	.4byte	0			// PointerToLineNumbers
114	.2byte	0			// NumberOfRelocations
115	.2byte	0			// NumberOfLineNumbers
116	.4byte	0x42100040		// Characteristics (section flags)
117
118
119	.ascii	".text"
120	.byte	0
121	.byte	0
122	.byte	0        		// end of 0 padding of section name
123	.4byte	_edata - _start		// VirtualSize
124	.4byte	_start - ImageBase	// VirtualAddress
125	.4byte	_edata - _start		// SizeOfRawData
126	.4byte	_start - ImageBase	// PointerToRawData
127
128	.4byte	0		// PointerToRelocations (0 for executables)
129	.4byte	0		// PointerToLineNumbers (0 for executables)
130	.2byte	0		// NumberOfRelocations  (0 for executables)
131	.2byte	0		// NumberOfLineNumbers  (0 for executables)
132	.4byte	0xe0500020	// Characteristics (section flags)
133
134	.set		push
135	.set		noreorder
136	.align		4
137
138	.globl	_start
139	.ent	_start
140	.type	_start, @function
141_start:
142	daddiu		$sp, -32
143	sd		$ra, ($sp)
144
145	// Get pc & gp
146	.align		3
147	bal		1f
148	sd		$gp, 8($sp)
149_pc:
150	.dword		_gp
151	.dword		_DYNAMIC
152	.dword		_relocate
1531:
154	// pc in ra
155	ld		$gp, ($ra)
156	dli		$t0, _pc
157	dsubu		$gp, $t0
158	daddu		$gp, $ra
159
160	sd		$a0, 16($sp)
161	sd		$a1, 24($sp)
162
163	// a2: ImageHandle
164	move		$a2, $a0
165	// a3: SystemTable
166	move		$a3, $a1
167	// a0: ImageBase
168	dli		$t1, ImageBase - _pc
169	daddu		$a0, $ra, $t1
170	// a1: DynamicSection
171	ld		$t1, 8($ra)
172	dsubu		$t1, $t0
173	daddu		$a1, $ra, $t1
174	// call _relocate
175	ld		$t1, 16($ra)
176	dsubu		$t1, $t0
177	daddu		$t9, $ra, $t1
178	jalr		$t9
179	nop
180	bnez		$v0, 1b
181	nop
182
183	// a0: ImageHandle
184	ld		$a0, 16($sp)
185	// call _start
186	dla		$t9, _entry
187	jalr		$t9
188	// a1: SystemTable
189	ld		$a1, 24($sp)
190
1911:
192	ld		$gp, 8($sp)
193	ld		$ra, ($sp)
194	jr		$ra
195	daddiu		$sp, 32
196	.end		_start
197
198	.set		pop
199
200#if defined(__ELF__) && defined(__linux__)
201	.section .note.GNU-stack,"",%progbits
202#endif
203