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