crt0-efi-loongarch64.S 4.4 KB

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