xref: /DragonStub/gnuefi/crt0-efi-mips64el.S (revision 039ca9d93c1eb5601f5b1a8bd812fd4b7d4de370)
13f234f5bSNigel Croxon/*
23f234f5bSNigel Croxon * crt0-efi-mips64el.S - PE/COFF header for MIPS64 EFI applications
33f234f5bSNigel Croxon *
43f234f5bSNigel Croxon * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
53f234f5bSNigel Croxon * Copright (C) 2017 Heiher <r@hev.cc>
63f234f5bSNigel Croxon *
73f234f5bSNigel Croxon * Redistribution and use in source and binary forms, with or without
83f234f5bSNigel Croxon * modification, are permitted provided that the following conditions
93f234f5bSNigel Croxon * are met:
103f234f5bSNigel Croxon * 1. Redistributions of source code must retain the above copyright
113f234f5bSNigel Croxon *    notice and this list of conditions, without modification.
123f234f5bSNigel Croxon * 2. The name of the author may not be used to endorse or promote products
133f234f5bSNigel Croxon *    derived from this software without specific prior written permission.
143f234f5bSNigel Croxon *
153f234f5bSNigel Croxon * Alternatively, this software may be distributed under the terms of the
163f234f5bSNigel Croxon * GNU General Public License as published by the Free Software Foundation;
173f234f5bSNigel Croxon * either version 2 of the License, or (at your option) any later version.
183f234f5bSNigel Croxon */
193f234f5bSNigel Croxon
203f234f5bSNigel Croxon	.section	.text.head
213f234f5bSNigel Croxon
223f234f5bSNigel Croxon	/*
233f234f5bSNigel Croxon	 * Magic "MZ" signature for PE/COFF
243f234f5bSNigel Croxon	 */
253f234f5bSNigel Croxon	.globl	ImageBase
263f234f5bSNigel CroxonImageBase:
273f234f5bSNigel Croxon	.ascii	"MZ"
283f234f5bSNigel Croxon	.skip	58				// 'MZ' + pad + offset == 64
299e6cb215SCallum Farmer	.4byte	pe_header - ImageBase		// Offset to the PE header.
303f234f5bSNigel Croxonpe_header:
313f234f5bSNigel Croxon	.ascii	"PE"
329e6cb215SCallum Farmer	.2byte 	0
333f234f5bSNigel Croxoncoff_header:
349e6cb215SCallum Farmer	.2byte	0x166				// MIPS little endian
359e6cb215SCallum Farmer	.2byte	2				// nr_sections
369e6cb215SCallum Farmer	.4byte	0 				// TimeDateStamp
379e6cb215SCallum Farmer	.4byte	0				// PointerToSymbolTable
389e6cb215SCallum Farmer	.4byte	0				// NumberOfSymbols
399e6cb215SCallum Farmer	.2byte	section_table - optional_header	// SizeOfOptionalHeader
409e6cb215SCallum Farmer	.2byte	0x206				// Characteristics.
413f234f5bSNigel Croxon						// IMAGE_FILE_DEBUG_STRIPPED |
423f234f5bSNigel Croxon						// IMAGE_FILE_EXECUTABLE_IMAGE |
433f234f5bSNigel Croxon						// IMAGE_FILE_LINE_NUMS_STRIPPED
443f234f5bSNigel Croxonoptional_header:
459e6cb215SCallum Farmer	.2byte	0x20b				// PE32+ format
463f234f5bSNigel Croxon	.byte	0x02				// MajorLinkerVersion
473f234f5bSNigel Croxon	.byte	0x14				// MinorLinkerVersion
489e6cb215SCallum Farmer	.4byte	_edata - _start			// SizeOfCode
499e6cb215SCallum Farmer	.4byte	0				// SizeOfInitializedData
509e6cb215SCallum Farmer	.4byte	0				// SizeOfUninitializedData
519e6cb215SCallum Farmer	.4byte	_start - ImageBase		// AddressOfEntryPoint
529e6cb215SCallum Farmer	.4byte	_start - ImageBase		// BaseOfCode
533f234f5bSNigel Croxon
543f234f5bSNigel Croxonextra_header_fields:
559e6cb215SCallum Farmer	.8byte	0				// ImageBase
569e6cb215SCallum Farmer	.4byte	0x20				// SectionAlignment
579e6cb215SCallum Farmer	.4byte	0x8				// FileAlignment
589e6cb215SCallum Farmer	.2byte	0				// MajorOperatingSystemVersion
599e6cb215SCallum Farmer	.2byte	0				// MinorOperatingSystemVersion
609e6cb215SCallum Farmer	.2byte	0				// MajorImageVersion
619e6cb215SCallum Farmer	.2byte	0				// MinorImageVersion
629e6cb215SCallum Farmer	.2byte	0				// MajorSubsystemVersion
639e6cb215SCallum Farmer	.2byte	0				// MinorSubsystemVersion
649e6cb215SCallum Farmer	.4byte	0				// Win32VersionValue
653f234f5bSNigel Croxon
669e6cb215SCallum Farmer	.4byte	_edata - ImageBase		// SizeOfImage
673f234f5bSNigel Croxon
683f234f5bSNigel Croxon	// Everything before the kernel image is considered part of the header
699e6cb215SCallum Farmer	.4byte	_start - ImageBase		// SizeOfHeaders
709e6cb215SCallum Farmer	.4byte	0				// CheckSum
719e6cb215SCallum Farmer	.2byte	EFI_SUBSYSTEM			// Subsystem
729e6cb215SCallum Farmer	.2byte	0				// DllCharacteristics
739e6cb215SCallum Farmer	.8byte	0				// SizeOfStackReserve
749e6cb215SCallum Farmer	.8byte	0				// SizeOfStackCommit
759e6cb215SCallum Farmer	.8byte	0				// SizeOfHeapReserve
769e6cb215SCallum Farmer	.8byte	0				// SizeOfHeapCommit
779e6cb215SCallum Farmer	.4byte	0				// LoaderFlags
78*039ca9d9SCallum Farmer	.4byte	0x10				// NumberOfRvaAndSizes
793f234f5bSNigel Croxon
809e6cb215SCallum Farmer	.8byte	0				// ExportTable
819e6cb215SCallum Farmer	.8byte	0				// ImportTable
829e6cb215SCallum Farmer	.8byte	0				// ResourceTable
839e6cb215SCallum Farmer	.8byte	0				// ExceptionTable
849e6cb215SCallum Farmer	.8byte	0				// CertificationTable
859e6cb215SCallum Farmer	.8byte	0				// BaseRelocationTable
86*039ca9d9SCallum Farmer	.8byte	0				// Debug
87*039ca9d9SCallum Farmer	.8byte	0				// Architecture
88*039ca9d9SCallum Farmer	.8byte	0				// Global Ptr
89*039ca9d9SCallum Farmer	.8byte	0				// TLS Table
90*039ca9d9SCallum Farmer	.8byte	0				// Load Config Table
91*039ca9d9SCallum Farmer	.8byte	0				// Bound Import
92*039ca9d9SCallum Farmer	.8byte	0				// IAT
93*039ca9d9SCallum Farmer	.8byte	0				// Delay Import Descriptor
94*039ca9d9SCallum Farmer	.8byte	0				// CLR Runtime Header
95*039ca9d9SCallum Farmer	.8byte	0				// Reserved, must be zero
963f234f5bSNigel Croxon
973f234f5bSNigel Croxon	// Section table
983f234f5bSNigel Croxonsection_table:
993f234f5bSNigel Croxon
1003f234f5bSNigel Croxon	/*
1013f234f5bSNigel Croxon	 * The EFI application loader requires a relocation section
1023f234f5bSNigel Croxon	 * because EFI applications must be relocatable.  This is a
1033f234f5bSNigel Croxon	 * dummy section as far as we are concerned.
1043f234f5bSNigel Croxon	 */
1053f234f5bSNigel Croxon	.ascii	".reloc"
1063f234f5bSNigel Croxon	.byte	0
1073f234f5bSNigel Croxon	.byte	0			// end of 0 padding of section name
1089e6cb215SCallum Farmer	.4byte	0
1099e6cb215SCallum Farmer	.4byte	0
1109e6cb215SCallum Farmer	.4byte	0			// SizeOfRawData
1119e6cb215SCallum Farmer	.4byte	0			// PointerToRawData
1129e6cb215SCallum Farmer	.4byte	0			// PointerToRelocations
1139e6cb215SCallum Farmer	.4byte	0			// PointerToLineNumbers
1149e6cb215SCallum Farmer	.2byte	0			// NumberOfRelocations
1159e6cb215SCallum Farmer	.2byte	0			// NumberOfLineNumbers
1169e6cb215SCallum Farmer	.4byte	0x42100040		// Characteristics (section flags)
1173f234f5bSNigel Croxon
1183f234f5bSNigel Croxon
1193f234f5bSNigel Croxon	.ascii	".text"
1203f234f5bSNigel Croxon	.byte	0
1213f234f5bSNigel Croxon	.byte	0
1223f234f5bSNigel Croxon	.byte	0        		// end of 0 padding of section name
1239e6cb215SCallum Farmer	.4byte	_edata - _start		// VirtualSize
1249e6cb215SCallum Farmer	.4byte	_start - ImageBase	// VirtualAddress
1259e6cb215SCallum Farmer	.4byte	_edata - _start		// SizeOfRawData
1269e6cb215SCallum Farmer	.4byte	_start - ImageBase	// PointerToRawData
1273f234f5bSNigel Croxon
1289e6cb215SCallum Farmer	.4byte	0		// PointerToRelocations (0 for executables)
1299e6cb215SCallum Farmer	.4byte	0		// PointerToLineNumbers (0 for executables)
1309e6cb215SCallum Farmer	.2byte	0		// NumberOfRelocations  (0 for executables)
1319e6cb215SCallum Farmer	.2byte	0		// NumberOfLineNumbers  (0 for executables)
1329e6cb215SCallum Farmer	.4byte	0xe0500020	// Characteristics (section flags)
1333f234f5bSNigel Croxon
1343f234f5bSNigel Croxon	.set		push
1353f234f5bSNigel Croxon	.set		noreorder
1363f234f5bSNigel Croxon	.align		4
1373f234f5bSNigel Croxon
1383f234f5bSNigel Croxon	.globl	_start
1393f234f5bSNigel Croxon	.ent	_start
1403f234f5bSNigel Croxon	.type	_start, @function
1413f234f5bSNigel Croxon_start:
1423f234f5bSNigel Croxon	daddiu		$sp, -32
1433f234f5bSNigel Croxon	sd		$ra, ($sp)
1443f234f5bSNigel Croxon
1453f234f5bSNigel Croxon	// Get pc & gp
1463f234f5bSNigel Croxon	.align		3
1473f234f5bSNigel Croxon	bal		1f
1483f234f5bSNigel Croxon	sd		$gp, 8($sp)
1493f234f5bSNigel Croxon_pc:
1503f234f5bSNigel Croxon	.dword		_gp
1513f234f5bSNigel Croxon	.dword		_DYNAMIC
1523f234f5bSNigel Croxon	.dword		_relocate
1533f234f5bSNigel Croxon1:
1543f234f5bSNigel Croxon	// pc in ra
1553f234f5bSNigel Croxon	ld		$gp, ($ra)
1563f234f5bSNigel Croxon	dli		$t0, _pc
1573f234f5bSNigel Croxon	dsubu		$gp, $t0
1583f234f5bSNigel Croxon	daddu		$gp, $ra
1593f234f5bSNigel Croxon
1603f234f5bSNigel Croxon	sd		$a0, 16($sp)
1613f234f5bSNigel Croxon	sd		$a1, 24($sp)
1623f234f5bSNigel Croxon
1633f234f5bSNigel Croxon	// a2: ImageHandle
1643f234f5bSNigel Croxon	move		$a2, $a0
1653f234f5bSNigel Croxon	// a3: SystemTable
1663f234f5bSNigel Croxon	move		$a3, $a1
1673f234f5bSNigel Croxon	// a0: ImageBase
1683f234f5bSNigel Croxon	dli		$t1, ImageBase - _pc
1693f234f5bSNigel Croxon	daddu		$a0, $ra, $t1
1703f234f5bSNigel Croxon	// a1: DynamicSection
1713f234f5bSNigel Croxon	ld		$t1, 8($ra)
1723f234f5bSNigel Croxon	dsubu		$t1, $t0
1733f234f5bSNigel Croxon	daddu		$a1, $ra, $t1
1743f234f5bSNigel Croxon	// call _relocate
1753f234f5bSNigel Croxon	ld		$t1, 16($ra)
1763f234f5bSNigel Croxon	dsubu		$t1, $t0
1773f234f5bSNigel Croxon	daddu		$t9, $ra, $t1
1783f234f5bSNigel Croxon	jalr		$t9
1793f234f5bSNigel Croxon	nop
1803f234f5bSNigel Croxon	bnez		$v0, 1b
1813f234f5bSNigel Croxon	nop
1823f234f5bSNigel Croxon
1833f234f5bSNigel Croxon	// a0: ImageHandle
1843f234f5bSNigel Croxon	ld		$a0, 16($sp)
1854f8b339fSPeter Jones	// call _start
1864f8b339fSPeter Jones	dla		$t9, _entry
1873f234f5bSNigel Croxon	jalr		$t9
1883f234f5bSNigel Croxon	// a1: SystemTable
1893f234f5bSNigel Croxon	ld		$a1, 24($sp)
1903f234f5bSNigel Croxon
1913f234f5bSNigel Croxon1:
1923f234f5bSNigel Croxon	ld		$gp, 8($sp)
1933f234f5bSNigel Croxon	ld		$ra, ($sp)
1943f234f5bSNigel Croxon	jr		$ra
1953f234f5bSNigel Croxon	daddiu		$sp, 32
1963f234f5bSNigel Croxon	.end		_start
1973f234f5bSNigel Croxon
1983f234f5bSNigel Croxon	.set		pop
199803b49c4SSergei Trofimovich
200803b49c4SSergei Trofimovich#if defined(__ELF__) && defined(__linux__)
201803b49c4SSergei Trofimovich	.section .note.GNU-stack,"",%progbits
202803b49c4SSergei Trofimovich#endif
203