crt0-efi-riscv64.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause */
  2. /*
  3. * Copright (C) 2014 Linaro Ltd. <[email protected]>
  4. * Copright (C) 2018 Alexander Graf <[email protected]>
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice and this list of conditions, without modification.
  11. * 2. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * Alternatively, this software may be distributed under the terms of the
  15. * GNU General Public License as published by the Free Software Foundation;
  16. * either version 2 of the License, or (at your option) any later version.
  17. */
  18. #ifndef EFI_SUBSYSTEM
  19. #define EFI_SUBSYSTEM 10
  20. #endif
  21. .section .text.head
  22. /*
  23. * Magic "MZ" signature for PE/COFF
  24. */
  25. .globl ImageBase
  26. ImageBase:
  27. .ascii "MZ"
  28. .skip 58 // 'MZ' + pad + offset == 64
  29. .4byte pe_header - ImageBase // Offset to the PE header.
  30. pe_header:
  31. .ascii "PE"
  32. .2byte 0
  33. coff_header:
  34. .2byte 0x5064 // riscv64
  35. .2byte 4 // nr_sections
  36. .4byte 0 // TimeDateStamp
  37. .4byte 0 // PointerToSymbolTable
  38. .4byte 0 // NumberOfSymbols
  39. .2byte section_table - optional_header // SizeOfOptionalHeader
  40. .2byte 0x206 // Characteristics.
  41. // IMAGE_FILE_DEBUG_STRIPPED |
  42. // IMAGE_FILE_EXECUTABLE_IMAGE |
  43. // IMAGE_FILE_LINE_NUMS_STRIPPED
  44. optional_header:
  45. .2byte 0x20b // PE32+ format
  46. .byte 0x02 // MajorLinkerVersion
  47. .byte 0x14 // MinorLinkerVersion
  48. .4byte _text_size - ImageBase // SizeOfCode
  49. .4byte _alldata_size - ImageBase // SizeOfInitializedData
  50. .4byte 0 // SizeOfUninitializedData
  51. .4byte _start - ImageBase // AddressOfEntryPoint
  52. .4byte _text - ImageBase // BaseOfCode
  53. extra_header_fields:
  54. .8byte 0 // ImageBase
  55. .4byte 0x1000 // SectionAlignment
  56. .4byte 0x1000 // FileAlignment
  57. .2byte 0 // MajorOperatingSystemVersion
  58. .2byte 0 // MinorOperatingSystemVersion
  59. .2byte 0 // MajorImageVersion
  60. .2byte 0 // MinorImageVersion
  61. .2byte 0 // MajorSubsystemVersion
  62. .2byte 0 // MinorSubsystemVersion
  63. .4byte 0 // Win32VersionValue
  64. .4byte _image_end - ImageBase // SizeOfImage
  65. // Everything before the kernel image is considered part of the header
  66. .4byte _text - ImageBase // SizeOfHeaders
  67. .4byte 0 // CheckSum
  68. .2byte EFI_SUBSYSTEM // Subsystem
  69. .2byte 0 // DllCharacteristics
  70. .8byte 0 // SizeOfStackReserve
  71. .8byte 0 // SizeOfStackCommit
  72. .8byte 0 // SizeOfHeapReserve
  73. .8byte 0 // SizeOfHeapCommit
  74. .4byte 0 // LoaderFlags
  75. .4byte 0x10 // NumberOfRvaAndSizes
  76. .8byte 0 // ExportTable
  77. .8byte 0 // ImportTable
  78. .8byte 0 // ResourceTable
  79. .8byte 0 // ExceptionTable
  80. .8byte 0 // CertificationTable
  81. .4byte _reloc - ImageBase // BaseRelocationTable (VirtualAddress)
  82. .4byte _reloc_vsize - ImageBase // BaseRelocationTable (Size)
  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. .ascii ".text\0\0\0"
  96. .4byte _text_vsize - ImageBase // VirtualSize
  97. .4byte _text - ImageBase // VirtualAddress
  98. .4byte _text_size - ImageBase // SizeOfRawData
  99. .4byte _text - ImageBase // PointerToRawData
  100. .4byte 0 // PointerToRelocations (0 for executables)
  101. .4byte 0 // PointerToLineNumbers (0 for executables)
  102. .2byte 0 // NumberOfRelocations (0 for executables)
  103. .2byte 0 // NumberOfLineNumbers (0 for executables)
  104. .4byte 0x60000020 // Characteristics (section flags)
  105. /*
  106. * The EFI application loader requires a relocation section
  107. * because EFI applications must be relocatable. This is a
  108. * dummy section as far as we are concerned.
  109. */
  110. .ascii ".reloc\0\0"
  111. .4byte _reloc_vsize - ImageBase // VirtualSize
  112. .4byte _reloc - ImageBase // VirtualAddress
  113. .4byte _reloc_size - ImageBase // SizeOfRawData
  114. .4byte _reloc - ImageBase // PointerToRawData
  115. .4byte 0 // PointerToRelocations
  116. .4byte 0 // PointerToLineNumbers
  117. .2byte 0 // NumberOfRelocations
  118. .2byte 0 // NumberOfLineNumbers
  119. .4byte 0x42000040 // Characteristics (section flags)
  120. .ascii ".data\0\0\0"
  121. .4byte _data_vsize - ImageBase // VirtualSize
  122. .4byte _data - ImageBase // VirtualAddress
  123. .4byte _data_size - ImageBase // SizeOfRawData
  124. .4byte _data - ImageBase // PointerToRawData
  125. .4byte 0 // PointerToRelocations
  126. .4byte 0 // PointerToLineNumbers
  127. .2byte 0 // NumberOfRelocations
  128. .2byte 0 // NumberOfLineNumbers
  129. .4byte 0xC0000040 // Characteristics (section flags)
  130. .ascii ".rodata\0"
  131. .4byte _rodata_vsize - ImageBase // VirtualSize
  132. .4byte _rodata - ImageBase // VirtualAddress
  133. .4byte _rodata_size - ImageBase // SizeOfRawData
  134. .4byte _rodata - ImageBase // PointerToRawData
  135. .4byte 0 // PointerToRelocations
  136. .4byte 0 // PointerToLineNumbers
  137. .2byte 0 // NumberOfRelocations
  138. .2byte 0 // NumberOfLineNumbers
  139. .4byte 0x40000040 // Characteristics (section flags)
  140. .text
  141. .globl _start
  142. .type _start,%function
  143. _start:
  144. addi sp, sp, -24
  145. sd a0, 0(sp)
  146. sd a1, 8(sp)
  147. sd ra, 16(sp)
  148. lla a0, ImageBase
  149. lla a1, _DYNAMIC
  150. call _relocate
  151. bne a0, zero, 0f
  152. ld a1, 8(sp)
  153. ld a0, 0(sp)
  154. call _entry
  155. ld ra, 16(sp)
  156. 0: addi sp, sp, 24
  157. ret
  158. // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
  159. .data
  160. dummy: .4byte 0
  161. #define IMAGE_REL_ABSOLUTE 0
  162. .section .reloc, "a"
  163. label1:
  164. .4byte dummy-label1 // Page RVA
  165. .4byte 12 // Block Size (2*4+2*2), must be aligned by 32 Bits
  166. .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
  167. .2byte (IMAGE_REL_ABSOLUTE<<12) + 0 // reloc for dummy
  168. #if defined(__ELF__) && defined(__linux__)
  169. .section .note.GNU-stack,"",%progbits
  170. #endif