xref: /DragonStub/gnuefi/crt0-efi-loongarch64.S (revision e7db4418b1fe6ea0220974b77c3b10918ab9b7a0)
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	.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	.long	pe_header - ImageBase		// Offset to the PE header.
30pe_header:
31	.ascii	"PE"
32	.short 	0
33coff_header:
34	.short	0x6264				// loongarch64 little endian
35	.short	2				// nr_sections
36	.long	0 				// TimeDateStamp
37	.long	0				// PointerToSymbolTable
38	.long	1				// NumberOfSymbols
39	.short	section_table - optional_header	// SizeOfOptionalHeader
40	.short	0x206				// Characteristics.
41						// IMAGE_FILE_DEBUG_STRIPPED |
42						// IMAGE_FILE_EXECUTABLE_IMAGE |
43						// IMAGE_FILE_LINE_NUMS_STRIPPED
44optional_header:
45	.short	0x20b				// PE32+ format
46	.byte	0x02				// MajorLinkerVersion
47	.byte	0x14				// MinorLinkerVersion
48	.long	_edata - _start			// SizeOfCode
49	.long	0				// SizeOfInitializedData
50	.long	0				// SizeOfUninitializedData
51	.long	_start - ImageBase		// AddressOfEntryPoint
52	.long	_start - ImageBase		// BaseOfCode
53
54extra_header_fields:
55	.quad	0				// ImageBase
56	.long	0x20				// SectionAlignment
57	.long	0x8				// FileAlignment
58	.short	0				// MajorOperatingSystemVersion
59	.short	0				// MinorOperatingSystemVersion
60	.short	0				// MajorImageVersion
61	.short	0				// MinorImageVersion
62	.short	0				// MajorSubsystemVersion
63	.short	0				// MinorSubsystemVersion
64	.long	0				// Win32VersionValue
65
66	.long	_edata - ImageBase		// SizeOfImage
67
68	// Everything before the kernel image is considered part of the header
69	.long	_start - ImageBase		// SizeOfHeaders
70	.long	0				// CheckSum
71	.short	EFI_SUBSYSTEM			// Subsystem
72	.short	0				// DllCharacteristics
73	.quad	0				// SizeOfStackReserve
74	.quad	0				// SizeOfStackCommit
75	.quad	0				// SizeOfHeapReserve
76	.quad	0				// SizeOfHeapCommit
77	.long	0				// LoaderFlags
78	.long	0x6				// NumberOfRvaAndSizes
79
80	.quad	0				// ExportTable
81	.quad	0				// ImportTable
82	.quad	0				// ResourceTable
83	.quad	0				// ExceptionTable
84	.quad	0				// CertificationTable
85	.quad	0				// BaseRelocationTable
86
87	// Section table
88section_table:
89
90	/*
91	 * The EFI application loader requires a relocation section
92	 * because EFI applications must be relocatable.  This is a
93	 * dummy section as far as we are concerned.
94	 */
95	.ascii	".reloc"
96	.byte	0
97	.byte	0			// end of 0 padding of section name
98	.long	0
99	.long	0
100	.long	0			// SizeOfRawData
101	.long	0			// PointerToRawData
102	.long	0			// PointerToRelocations
103	.long	0			// PointerToLineNumbers
104	.short	0			// NumberOfRelocations
105	.short	0			// NumberOfLineNumbers
106	.long	0x42100040		// Characteristics (section flags)
107
108
109	.ascii	".text"
110	.byte	0
111	.byte	0
112	.byte	0        		// end of 0 padding of section name
113	.long	_edata - _start		// VirtualSize
114	.long	_start - ImageBase	// VirtualAddress
115	.long	_edata - _start		// SizeOfRawData
116	.long	_start - ImageBase	// PointerToRawData
117
118	.long	0			// PointerToRelocations (0 for executables)
119	.long	0			// PointerToLineNumbers (0 for executables)
120	.short	0			// NumberOfRelocations  (0 for executables)
121	.short	0			// NumberOfLineNumbers  (0 for executables)
122	.long	0xe0500020		// Characteristics (section flags)
123
124	.align  4
125
126	.globl	_start
127	.type	_start, @function
128_start:
129	addi.d	  $sp, $sp, -24
130	st.d	  $ra, $sp, 0
131	st.d	  $a0, $sp, 8
132	st.d 	  $a1, $sp, 16
133
134	move	  $a2, $a0		// a2: ImageHandle
135	move	  $a3, $a1 		// a3: SystemTable
136	la.local  $a0, ImageBase	// a0: ImageBase
137	la.local  $a1, _DYNAMIC		// a1: DynamicSection
138	bl        _relocate
139	bnez	  $a0, 0f
140
141	ld.d	  $a0, $sp, 8
142	ld.d	  $a1, $sp, 16
143	bl 	  efi_main
144
1450:	ld.d	  $ra, $sp, 0
146	addi.d	  $sp, $sp, 24
147	jirl	  $ra, $ra, 0
148	.end	  _start
149