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