crt0-efi-loongarch64.S 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. #ifndef EFI_SUBSYSTEM
  20. #define EFI_SUBSYSTEM 0xa
  21. #endif
  22. .section .text.head
  23. /*
  24. * Magic "MZ" signature for PE/COFF
  25. */
  26. .globl ImageBase
  27. ImageBase:
  28. .ascii "MZ"
  29. .skip 58 // 'MZ' + pad + offset == 64
  30. .long pe_header - ImageBase // Offset to the PE header.
  31. pe_header:
  32. .ascii "PE"
  33. .short 0
  34. coff_header:
  35. .short 0x6264 // loongarch64 little endian
  36. .short 2 // nr_sections
  37. .long 0 // TimeDateStamp
  38. .long 0 // PointerToSymbolTable
  39. .long 1 // NumberOfSymbols
  40. .short section_table - optional_header // SizeOfOptionalHeader
  41. .short 0x206 // Characteristics.
  42. // IMAGE_FILE_DEBUG_STRIPPED |
  43. // IMAGE_FILE_EXECUTABLE_IMAGE |
  44. // IMAGE_FILE_LINE_NUMS_STRIPPED
  45. optional_header:
  46. .short 0x20b // PE32+ format
  47. .byte 0x02 // MajorLinkerVersion
  48. .byte 0x14 // MinorLinkerVersion
  49. .long _edata - _start // SizeOfCode
  50. .long 0 // SizeOfInitializedData
  51. .long 0 // SizeOfUninitializedData
  52. .long _start - ImageBase // AddressOfEntryPoint
  53. .long _start - ImageBase // BaseOfCode
  54. extra_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. .long _edata - ImageBase // SizeOfImage
  66. // Everything before the kernel image is considered part of the header
  67. .long _start - ImageBase // SizeOfHeaders
  68. .long 0 // CheckSum
  69. .short EFI_SUBSYSTEM // Subsystem
  70. .short 0 // DllCharacteristics
  71. .quad 0 // SizeOfStackReserve
  72. .quad 0 // SizeOfStackCommit
  73. .quad 0 // SizeOfHeapReserve
  74. .quad 0 // SizeOfHeapCommit
  75. .long 0 // LoaderFlags
  76. .long 0x6 // NumberOfRvaAndSizes
  77. .quad 0 // ExportTable
  78. .quad 0 // ImportTable
  79. .quad 0 // ResourceTable
  80. .quad 0 // ExceptionTable
  81. .quad 0 // CertificationTable
  82. .quad 0 // BaseRelocationTable
  83. // Section table
  84. section_table:
  85. /*
  86. * The EFI application loader requires a relocation section
  87. * because EFI applications must be relocatable. This is a
  88. * dummy section as far as we are concerned.
  89. */
  90. .ascii ".reloc"
  91. .byte 0
  92. .byte 0 // end of 0 padding of section name
  93. .long 0
  94. .long 0
  95. .long 0 // SizeOfRawData
  96. .long 0 // PointerToRawData
  97. .long 0 // PointerToRelocations
  98. .long 0 // PointerToLineNumbers
  99. .short 0 // NumberOfRelocations
  100. .short 0 // NumberOfLineNumbers
  101. .long 0x42100040 // Characteristics (section flags)
  102. .ascii ".text"
  103. .byte 0
  104. .byte 0
  105. .byte 0 // end of 0 padding of section name
  106. .long _edata - _start // VirtualSize
  107. .long _start - ImageBase // VirtualAddress
  108. .long _edata - _start // SizeOfRawData
  109. .long _start - ImageBase // PointerToRawData
  110. .long 0 // PointerToRelocations (0 for executables)
  111. .long 0 // PointerToLineNumbers (0 for executables)
  112. .short 0 // NumberOfRelocations (0 for executables)
  113. .short 0 // NumberOfLineNumbers (0 for executables)
  114. .long 0xe0500020 // Characteristics (section flags)
  115. .align 4
  116. .globl _start
  117. .type _start, @function
  118. _start:
  119. addi.d $sp, $sp, -24
  120. st.d $ra, $sp, 0
  121. st.d $a0, $sp, 8
  122. st.d $a1, $sp, 16
  123. move $a2, $a0 // a2: ImageHandle
  124. move $a3, $a1 // a3: SystemTable
  125. la.local $a0, ImageBase // a0: ImageBase
  126. la.local $a1, _DYNAMIC // a1: DynamicSection
  127. bl _relocate
  128. bnez $a0, 0f
  129. ld.d $a0, $sp, 8
  130. ld.d $a1, $sp, 16
  131. bl efi_main
  132. 0: ld.d $ra, $sp, 0
  133. addi.d $sp, $sp, 24
  134. jr $ra
  135. .end _start