xref: /DragonStub/gnuefi/crt0-efi-loongarch64.S (revision e7db4418b1fe6ea0220974b77c3b10918ab9b7a0)
1*e7db4418Szhoumingtao/*
2*e7db4418Szhoumingtao * crt0-efi-loongarch64.S - PE/COFF header for LOONGARCH64 EFI applications
3*e7db4418Szhoumingtao *
4*e7db4418Szhoumingtao * Copyright (C) 2021 Loongson Technology Corporation Limited. <zhoumingtao@loongson.cn>
5*e7db4418Szhoumingtao * Copyright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6*e7db4418Szhoumingtao *
7*e7db4418Szhoumingtao * Redistribution and use in source and binary forms, with or without
8*e7db4418Szhoumingtao * modification, are permitted provided that the following conditions
9*e7db4418Szhoumingtao * are met:
10*e7db4418Szhoumingtao * 1. Redistributions of source code must retain the above copyright
11*e7db4418Szhoumingtao *    notice and this list of conditions, without modification.
12*e7db4418Szhoumingtao * 2. The name of the author may not be used to endorse or promote products
13*e7db4418Szhoumingtao *    derived from this software without specific prior written permission.
14*e7db4418Szhoumingtao *
15*e7db4418Szhoumingtao * Alternatively, this software may be distributed under the terms of the
16*e7db4418Szhoumingtao * GNU General Public License as published by the Free Software Foundation;
17*e7db4418Szhoumingtao * either version 2 of the License, or (at your option) any later version.
18*e7db4418Szhoumingtao */
19*e7db4418Szhoumingtao
20*e7db4418Szhoumingtao	.section	.text.head
21*e7db4418Szhoumingtao
22*e7db4418Szhoumingtao	/*
23*e7db4418Szhoumingtao	 * Magic "MZ" signature for PE/COFF
24*e7db4418Szhoumingtao	 */
25*e7db4418Szhoumingtao	.globl	ImageBase
26*e7db4418SzhoumingtaoImageBase:
27*e7db4418Szhoumingtao	.ascii	"MZ"
28*e7db4418Szhoumingtao	.skip	58				// 'MZ' + pad + offset == 64
29*e7db4418Szhoumingtao	.long	pe_header - ImageBase		// Offset to the PE header.
30*e7db4418Szhoumingtaope_header:
31*e7db4418Szhoumingtao	.ascii	"PE"
32*e7db4418Szhoumingtao	.short 	0
33*e7db4418Szhoumingtaocoff_header:
34*e7db4418Szhoumingtao	.short	0x6264				// loongarch64 little endian
35*e7db4418Szhoumingtao	.short	2				// nr_sections
36*e7db4418Szhoumingtao	.long	0 				// TimeDateStamp
37*e7db4418Szhoumingtao	.long	0				// PointerToSymbolTable
38*e7db4418Szhoumingtao	.long	1				// NumberOfSymbols
39*e7db4418Szhoumingtao	.short	section_table - optional_header	// SizeOfOptionalHeader
40*e7db4418Szhoumingtao	.short	0x206				// Characteristics.
41*e7db4418Szhoumingtao						// IMAGE_FILE_DEBUG_STRIPPED |
42*e7db4418Szhoumingtao						// IMAGE_FILE_EXECUTABLE_IMAGE |
43*e7db4418Szhoumingtao						// IMAGE_FILE_LINE_NUMS_STRIPPED
44*e7db4418Szhoumingtaooptional_header:
45*e7db4418Szhoumingtao	.short	0x20b				// PE32+ format
46*e7db4418Szhoumingtao	.byte	0x02				// MajorLinkerVersion
47*e7db4418Szhoumingtao	.byte	0x14				// MinorLinkerVersion
48*e7db4418Szhoumingtao	.long	_edata - _start			// SizeOfCode
49*e7db4418Szhoumingtao	.long	0				// SizeOfInitializedData
50*e7db4418Szhoumingtao	.long	0				// SizeOfUninitializedData
51*e7db4418Szhoumingtao	.long	_start - ImageBase		// AddressOfEntryPoint
52*e7db4418Szhoumingtao	.long	_start - ImageBase		// BaseOfCode
53*e7db4418Szhoumingtao
54*e7db4418Szhoumingtaoextra_header_fields:
55*e7db4418Szhoumingtao	.quad	0				// ImageBase
56*e7db4418Szhoumingtao	.long	0x20				// SectionAlignment
57*e7db4418Szhoumingtao	.long	0x8				// FileAlignment
58*e7db4418Szhoumingtao	.short	0				// MajorOperatingSystemVersion
59*e7db4418Szhoumingtao	.short	0				// MinorOperatingSystemVersion
60*e7db4418Szhoumingtao	.short	0				// MajorImageVersion
61*e7db4418Szhoumingtao	.short	0				// MinorImageVersion
62*e7db4418Szhoumingtao	.short	0				// MajorSubsystemVersion
63*e7db4418Szhoumingtao	.short	0				// MinorSubsystemVersion
64*e7db4418Szhoumingtao	.long	0				// Win32VersionValue
65*e7db4418Szhoumingtao
66*e7db4418Szhoumingtao	.long	_edata - ImageBase		// SizeOfImage
67*e7db4418Szhoumingtao
68*e7db4418Szhoumingtao	// Everything before the kernel image is considered part of the header
69*e7db4418Szhoumingtao	.long	_start - ImageBase		// SizeOfHeaders
70*e7db4418Szhoumingtao	.long	0				// CheckSum
71*e7db4418Szhoumingtao	.short	EFI_SUBSYSTEM			// Subsystem
72*e7db4418Szhoumingtao	.short	0				// DllCharacteristics
73*e7db4418Szhoumingtao	.quad	0				// SizeOfStackReserve
74*e7db4418Szhoumingtao	.quad	0				// SizeOfStackCommit
75*e7db4418Szhoumingtao	.quad	0				// SizeOfHeapReserve
76*e7db4418Szhoumingtao	.quad	0				// SizeOfHeapCommit
77*e7db4418Szhoumingtao	.long	0				// LoaderFlags
78*e7db4418Szhoumingtao	.long	0x6				// NumberOfRvaAndSizes
79*e7db4418Szhoumingtao
80*e7db4418Szhoumingtao	.quad	0				// ExportTable
81*e7db4418Szhoumingtao	.quad	0				// ImportTable
82*e7db4418Szhoumingtao	.quad	0				// ResourceTable
83*e7db4418Szhoumingtao	.quad	0				// ExceptionTable
84*e7db4418Szhoumingtao	.quad	0				// CertificationTable
85*e7db4418Szhoumingtao	.quad	0				// BaseRelocationTable
86*e7db4418Szhoumingtao
87*e7db4418Szhoumingtao	// Section table
88*e7db4418Szhoumingtaosection_table:
89*e7db4418Szhoumingtao
90*e7db4418Szhoumingtao	/*
91*e7db4418Szhoumingtao	 * The EFI application loader requires a relocation section
92*e7db4418Szhoumingtao	 * because EFI applications must be relocatable.  This is a
93*e7db4418Szhoumingtao	 * dummy section as far as we are concerned.
94*e7db4418Szhoumingtao	 */
95*e7db4418Szhoumingtao	.ascii	".reloc"
96*e7db4418Szhoumingtao	.byte	0
97*e7db4418Szhoumingtao	.byte	0			// end of 0 padding of section name
98*e7db4418Szhoumingtao	.long	0
99*e7db4418Szhoumingtao	.long	0
100*e7db4418Szhoumingtao	.long	0			// SizeOfRawData
101*e7db4418Szhoumingtao	.long	0			// PointerToRawData
102*e7db4418Szhoumingtao	.long	0			// PointerToRelocations
103*e7db4418Szhoumingtao	.long	0			// PointerToLineNumbers
104*e7db4418Szhoumingtao	.short	0			// NumberOfRelocations
105*e7db4418Szhoumingtao	.short	0			// NumberOfLineNumbers
106*e7db4418Szhoumingtao	.long	0x42100040		// Characteristics (section flags)
107*e7db4418Szhoumingtao
108*e7db4418Szhoumingtao
109*e7db4418Szhoumingtao	.ascii	".text"
110*e7db4418Szhoumingtao	.byte	0
111*e7db4418Szhoumingtao	.byte	0
112*e7db4418Szhoumingtao	.byte	0        		// end of 0 padding of section name
113*e7db4418Szhoumingtao	.long	_edata - _start		// VirtualSize
114*e7db4418Szhoumingtao	.long	_start - ImageBase	// VirtualAddress
115*e7db4418Szhoumingtao	.long	_edata - _start		// SizeOfRawData
116*e7db4418Szhoumingtao	.long	_start - ImageBase	// PointerToRawData
117*e7db4418Szhoumingtao
118*e7db4418Szhoumingtao	.long	0			// PointerToRelocations (0 for executables)
119*e7db4418Szhoumingtao	.long	0			// PointerToLineNumbers (0 for executables)
120*e7db4418Szhoumingtao	.short	0			// NumberOfRelocations  (0 for executables)
121*e7db4418Szhoumingtao	.short	0			// NumberOfLineNumbers  (0 for executables)
122*e7db4418Szhoumingtao	.long	0xe0500020		// Characteristics (section flags)
123*e7db4418Szhoumingtao
124*e7db4418Szhoumingtao	.align  4
125*e7db4418Szhoumingtao
126*e7db4418Szhoumingtao	.globl	_start
127*e7db4418Szhoumingtao	.type	_start, @function
128*e7db4418Szhoumingtao_start:
129*e7db4418Szhoumingtao	addi.d	  $sp, $sp, -24
130*e7db4418Szhoumingtao	st.d	  $ra, $sp, 0
131*e7db4418Szhoumingtao	st.d	  $a0, $sp, 8
132*e7db4418Szhoumingtao	st.d 	  $a1, $sp, 16
133*e7db4418Szhoumingtao
134*e7db4418Szhoumingtao	move	  $a2, $a0		// a2: ImageHandle
135*e7db4418Szhoumingtao	move	  $a3, $a1 		// a3: SystemTable
136*e7db4418Szhoumingtao	la.local  $a0, ImageBase	// a0: ImageBase
137*e7db4418Szhoumingtao	la.local  $a1, _DYNAMIC		// a1: DynamicSection
138*e7db4418Szhoumingtao	bl        _relocate
139*e7db4418Szhoumingtao	bnez	  $a0, 0f
140*e7db4418Szhoumingtao
141*e7db4418Szhoumingtao	ld.d	  $a0, $sp, 8
142*e7db4418Szhoumingtao	ld.d	  $a1, $sp, 16
143*e7db4418Szhoumingtao	bl 	  efi_main
144*e7db4418Szhoumingtao
145*e7db4418Szhoumingtao0:	ld.d	  $ra, $sp, 0
146*e7db4418Szhoumingtao	addi.d	  $sp, $sp, 24
147*e7db4418Szhoumingtao	jirl	  $ra, $ra, 0
148*e7db4418Szhoumingtao	.end	  _start
149