crt0-efi-loongarch64.S 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * crt0-efi-loongarch64.S - PE/COFF header for LoongArch64 EFI applications
  3. *
  4. * Copyright (C) 2021 Loongson Technology Corporation Limited. <[email protected]>
  5. * Copyright (C) 2014 Linaro Ltd. <[email protected]>
  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. .4byte pe_header - ImageBase // Offset to the PE header.
  31. pe_header:
  32. .ascii "PE"
  33. .2byte 0
  34. coff_header:
  35. .2byte 0x6264 // loongarch64 little endian
  36. .2byte 2 // nr_sections
  37. .4byte 0 // TimeDateStamp
  38. .4byte 0 // PointerToSymbolTable
  39. .4byte 1 // NumberOfSymbols
  40. .2byte section_table - optional_header // SizeOfOptionalHeader
  41. .2byte 0x206 // Characteristics.
  42. // IMAGE_FILE_DEBUG_STRIPPED |
  43. // IMAGE_FILE_EXECUTABLE_IMAGE |
  44. // IMAGE_FILE_LINE_NUMS_STRIPPED
  45. optional_header:
  46. .2byte 0x20b // PE32+ format
  47. .byte 0x02 // MajorLinkerVersion
  48. .byte 0x14 // MinorLinkerVersion
  49. .4byte _edata - _start // SizeOfCode
  50. .4byte 0 // SizeOfInitializedData
  51. .4byte 0 // SizeOfUninitializedData
  52. .4byte _start - ImageBase // AddressOfEntryPoint
  53. .4byte _start - ImageBase // BaseOfCode
  54. extra_header_fields:
  55. .8byte 0 // ImageBase
  56. .4byte 0x20 // SectionAlignment
  57. .4byte 0x8 // FileAlignment
  58. .2byte 0 // MajorOperatingSystemVersion
  59. .2byte 0 // MinorOperatingSystemVersion
  60. .2byte 0 // MajorImageVersion
  61. .2byte 0 // MinorImageVersion
  62. .2byte 0 // MajorSubsystemVersion
  63. .2byte 0 // MinorSubsystemVersion
  64. .4byte 0 // Win32VersionValue
  65. .4byte _edata - ImageBase // SizeOfImage
  66. // Everything before the kernel image is considered part of the header
  67. .4byte _start - ImageBase // SizeOfHeaders
  68. .4byte 0 // CheckSum
  69. .2byte EFI_SUBSYSTEM // Subsystem
  70. .2byte 0 // DllCharacteristics
  71. .8byte 0 // SizeOfStackReserve
  72. .8byte 0 // SizeOfStackCommit
  73. .8byte 0 // SizeOfHeapReserve
  74. .8byte 0 // SizeOfHeapCommit
  75. .4byte 0 // LoaderFlags
  76. .4byte 0x10 // NumberOfRvaAndSizes
  77. .8byte 0 // ExportTable
  78. .8byte 0 // ImportTable
  79. .8byte 0 // ResourceTable
  80. .8byte 0 // ExceptionTable
  81. .8byte 0 // CertificationTable
  82. .8byte 0 // BaseRelocationTable
  83. .8byte 0 // Debug
  84. .8byte 0 // Architecture
  85. .8byte 0 // Global Ptr
  86. .8byte 0 // TLS Table
  87. .8byte 0 // Load Config Table
  88. .8byte 0 // Bound Import
  89. .8byte 0 // IAT
  90. .8byte 0 // Delay Import Descriptor
  91. .8byte 0 // CLR Runtime Header
  92. .8byte 0 // Reserved, must be zero
  93. // Section table
  94. section_table:
  95. /*
  96. * The EFI application loader requires a relocation section
  97. * because EFI applications must be relocatable. This is a
  98. * dummy section as far as we are concerned.
  99. */
  100. .ascii ".reloc"
  101. .byte 0
  102. .byte 0 // end of 0 padding of section name
  103. .4byte 0
  104. .4byte 0
  105. .4byte 0 // SizeOfRawData
  106. .4byte 0 // PointerToRawData
  107. .4byte 0 // PointerToRelocations
  108. .4byte 0 // PointerToLineNumbers
  109. .2byte 0 // NumberOfRelocations
  110. .2byte 0 // NumberOfLineNumbers
  111. .4byte 0x42100040 // Characteristics (section flags)
  112. .ascii ".text"
  113. .byte 0
  114. .byte 0
  115. .byte 0 // end of 0 padding of section name
  116. .4byte _edata - _start // VirtualSize
  117. .4byte _start - ImageBase // VirtualAddress
  118. .4byte _edata - _start // SizeOfRawData
  119. .4byte _start - ImageBase // PointerToRawData
  120. .4byte 0 // PointerToRelocations (0 for executables)
  121. .4byte 0 // PointerToLineNumbers (0 for executables)
  122. .2byte 0 // NumberOfRelocations (0 for executables)
  123. .2byte 0 // NumberOfLineNumbers (0 for executables)
  124. .4byte 0xe0500020 // Characteristics (section flags)
  125. .align 4
  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. move $a2, $a0 // a2: ImageHandle
  134. move $a3, $a1 // a3: SystemTable
  135. la.local $a0, ImageBase // a0: ImageBase
  136. la.local $a1, _DYNAMIC // a1: DynamicSection
  137. bl _relocate
  138. bnez $a0, 0f
  139. ld.d $a0, $sp, 8
  140. ld.d $a1, $sp, 16
  141. bl efi_main
  142. 0: ld.d $ra, $sp, 0
  143. addi.d $sp, $sp, 24
  144. jr $ra
  145. .end _start