xref: /DragonStub/gnuefi/crt0-efi-loongarch64.S (revision 5849eacdce81bda09fd61e71f440f15e62c25deb)
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	.long	pe_header - ImageBase		// Offset to the PE header.
33pe_header:
34	.ascii	"PE"
35	.short 	0
36coff_header:
37	.short	0x6264				// loongarch64 little endian
38	.short	2				// nr_sections
39	.long	0 				// TimeDateStamp
40	.long	0				// PointerToSymbolTable
41	.long	1				// NumberOfSymbols
42	.short	section_table - optional_header	// SizeOfOptionalHeader
43	.short	0x206				// Characteristics.
44						// IMAGE_FILE_DEBUG_STRIPPED |
45						// IMAGE_FILE_EXECUTABLE_IMAGE |
46						// IMAGE_FILE_LINE_NUMS_STRIPPED
47optional_header:
48	.short	0x20b				// PE32+ format
49	.byte	0x02				// MajorLinkerVersion
50	.byte	0x14				// MinorLinkerVersion
51	.long	_edata - _start			// SizeOfCode
52	.long	0				// SizeOfInitializedData
53	.long	0				// SizeOfUninitializedData
54	.long	_start - ImageBase		// AddressOfEntryPoint
55	.long	_start - ImageBase		// BaseOfCode
56
57extra_header_fields:
58	.quad	0				// ImageBase
59	.long	0x20				// SectionAlignment
60	.long	0x8				// FileAlignment
61	.short	0				// MajorOperatingSystemVersion
62	.short	0				// MinorOperatingSystemVersion
63	.short	0				// MajorImageVersion
64	.short	0				// MinorImageVersion
65	.short	0				// MajorSubsystemVersion
66	.short	0				// MinorSubsystemVersion
67	.long	0				// Win32VersionValue
68
69	.long	_edata - ImageBase		// SizeOfImage
70
71	// Everything before the kernel image is considered part of the header
72	.long	_start - ImageBase		// SizeOfHeaders
73	.long	0				// CheckSum
74	.short	EFI_SUBSYSTEM			// Subsystem
75	.short	0				// DllCharacteristics
76	.quad	0				// SizeOfStackReserve
77	.quad	0				// SizeOfStackCommit
78	.quad	0				// SizeOfHeapReserve
79	.quad	0				// SizeOfHeapCommit
80	.long	0				// LoaderFlags
81	.long	0x6				// NumberOfRvaAndSizes
82
83	.quad	0				// ExportTable
84	.quad	0				// ImportTable
85	.quad	0				// ResourceTable
86	.quad	0				// ExceptionTable
87	.quad	0				// CertificationTable
88	.quad	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	.long	0
102	.long	0
103	.long	0			// SizeOfRawData
104	.long	0			// PointerToRawData
105	.long	0			// PointerToRelocations
106	.long	0			// PointerToLineNumbers
107	.short	0			// NumberOfRelocations
108	.short	0			// NumberOfLineNumbers
109	.long	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	.long	_edata - _start		// VirtualSize
117	.long	_start - ImageBase	// VirtualAddress
118	.long	_edata - _start		// SizeOfRawData
119	.long	_start - ImageBase	// PointerToRawData
120
121	.long	0			// PointerToRelocations (0 for executables)
122	.long	0			// PointerToLineNumbers (0 for executables)
123	.short	0			// NumberOfRelocations  (0 for executables)
124	.short	0			// NumberOfLineNumbers  (0 for executables)
125	.long	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