crt0-efi-arm.S 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * crt0-efi-arm.S - PE/COFF header for ARM EFI applications
  3. *
  4. * Copright (C) 2014 Linaro Ltd. <[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. .section .text.head
  19. /*
  20. * Magic "MZ" signature for PE/COFF
  21. */
  22. .globl ImageBase
  23. ImageBase:
  24. .ascii "MZ"
  25. .skip 58 // 'MZ' + pad + offset == 64
  26. .4byte pe_header - ImageBase // Offset to the PE header.
  27. pe_header:
  28. .ascii "PE"
  29. .2byte 0
  30. coff_header:
  31. .2byte 0x1c2 // Mixed ARM/Thumb
  32. .2byte 4 // nr_sections
  33. .4byte 0 // TimeDateStamp
  34. .4byte 0 // PointerToSymbolTable
  35. .4byte 0 // NumberOfSymbols
  36. .2byte section_table - optional_header // SizeOfOptionalHeader
  37. .2byte 0x306 // Characteristics.
  38. // IMAGE_FILE_32BIT_MACHINE |
  39. // IMAGE_FILE_DEBUG_STRIPPED |
  40. // IMAGE_FILE_EXECUTABLE_IMAGE |
  41. // IMAGE_FILE_LINE_NUMS_STRIPPED
  42. optional_header:
  43. .2byte 0x10b // PE32+ format
  44. .byte 0x02 // MajorLinkerVersion
  45. .byte 0x14 // MinorLinkerVersion
  46. .4byte _etext - _start // SizeOfCode
  47. .4byte _alldata_size - ImageBase // SizeOfInitializedData
  48. .4byte 0 // SizeOfUninitializedData
  49. .4byte _start - ImageBase // AddressOfEntryPoint
  50. .4byte _start - ImageBase // BaseOfCode
  51. .4byte _reloc - ImageBase // BaseOfData
  52. extra_header_fields:
  53. .4byte 0 // ImageBase
  54. .4byte 0x1000 // SectionAlignment
  55. .4byte 0x1000 // FileAlignment
  56. .2byte 0 // MajorOperatingSystemVersion
  57. .2byte 0 // MinorOperatingSystemVersion
  58. .2byte 0 // MajorImageVersion
  59. .2byte 0 // MinorImageVersion
  60. .2byte 0 // MajorSubsystemVersion
  61. .2byte 0 // MinorSubsystemVersion
  62. .4byte 0 // Win32VersionValue
  63. .4byte _image_end - ImageBase // SizeOfImage
  64. // Everything before the kernel image is considered part of the header
  65. .4byte _start - ImageBase // SizeOfHeaders
  66. .4byte 0 // CheckSum
  67. .2byte EFI_SUBSYSTEM // Subsystem
  68. .2byte 0 // DllCharacteristics
  69. .4byte 0 // SizeOfStackReserve
  70. .4byte 0 // SizeOfStackCommit
  71. .4byte 0 // SizeOfHeapReserve
  72. .4byte 0 // SizeOfHeapCommit
  73. .4byte 0 // LoaderFlags
  74. .4byte 0x10 // NumberOfRvaAndSizes
  75. .8byte 0 // ExportTable
  76. .8byte 0 // ImportTable
  77. .8byte 0 // ResourceTable
  78. .8byte 0 // ExceptionTable
  79. .8byte 0 // CertificationTable
  80. .4byte _reloc - ImageBase // BaseRelocationTable (VirtualAddress)
  81. .4byte _reloc_vsize - ImageBase // BaseRelocationTable (Size)
  82. .8byte 0 // Debug
  83. .8byte 0 // Architecture
  84. .8byte 0 // Global Ptr
  85. .8byte 0 // TLS Table
  86. .8byte 0 // Load Config Table
  87. .8byte 0 // Bound Import
  88. .8byte 0 // IAT
  89. .8byte 0 // Delay Import Descriptor
  90. .8byte 0 // CLR Runtime Header
  91. .8byte 0 // Reserved, must be zero
  92. // Section table
  93. section_table:
  94. .ascii ".text\0\0\0"
  95. .4byte _evtext - _start // VirtualSize
  96. .4byte _start - ImageBase // VirtualAddress
  97. .4byte _etext - _start // SizeOfRawData
  98. .4byte _start - ImageBase // PointerToRawData
  99. .4byte 0 // PointerToRelocations (0 for executables)
  100. .4byte 0 // PointerToLineNumbers (0 for executables)
  101. .2byte 0 // NumberOfRelocations (0 for executables)
  102. .2byte 0 // NumberOfLineNumbers (0 for executables)
  103. .4byte 0x60000020 // Characteristics (section flags)
  104. /*
  105. * The EFI application loader requires a relocation section
  106. * because EFI applications must be relocatable. This is a
  107. * dummy section as far as we are concerned.
  108. */
  109. .ascii ".reloc\0\0"
  110. .4byte _reloc_vsize - ImageBase // VirtualSize
  111. .4byte _reloc - ImageBase // VirtualAddress
  112. .4byte _reloc_size - ImageBase // SizeOfRawData
  113. .4byte _reloc - ImageBase // PointerToRawData
  114. .4byte 0 // PointerToRelocations
  115. .4byte 0 // PointerToLineNumbers
  116. .2byte 0 // NumberOfRelocations
  117. .2byte 0 // NumberOfLineNumbers
  118. .4byte 0x42000040 // Characteristics (section flags)
  119. .ascii ".data\0\0\0"
  120. .4byte _data_vsize - ImageBase // VirtualSize
  121. .4byte _data - ImageBase // VirtualAddress
  122. .4byte _data_size - ImageBase // SizeOfRawData
  123. .4byte _data - ImageBase // PointerToRawData
  124. .4byte 0 // PointerToRelocations
  125. .4byte 0 // PointerToLineNumbers
  126. .2byte 0 // NumberOfRelocations
  127. .2byte 0 // NumberOfLineNumbers
  128. .4byte 0xC0000040 // Characteristics (section flags)
  129. .ascii ".rodata\0"
  130. .4byte _rodata_vsize - ImageBase // VirtualSize
  131. .4byte _rodata - ImageBase // VirtualAddress
  132. .4byte _rodata_size - ImageBase // SizeOfRawData
  133. .4byte _rodata - ImageBase // PointerToRawData
  134. .4byte 0 // PointerToRelocations
  135. .4byte 0 // PointerToLineNumbers
  136. .2byte 0 // NumberOfRelocations
  137. .2byte 0 // NumberOfLineNumbers
  138. .4byte 0x40000040 // Characteristics (section flags)
  139. .balign 256
  140. .globl _start
  141. .type _start,%function
  142. _start:
  143. stmfd sp!, {r0-r2, lr}
  144. mov r2, r0
  145. mov r3, r1
  146. adr r1, .L_DYNAMIC
  147. ldr r0, [r1]
  148. add r1, r0, r1
  149. adr r0, ImageBase
  150. bl _relocate
  151. teq r0, #0
  152. bne 0f
  153. ldmfd sp, {r0-r1}
  154. bl _entry
  155. 0: add sp, sp, #12
  156. ldr pc, [sp], #4
  157. .L_DYNAMIC:
  158. .4byte _DYNAMIC - .
  159. // hand-craft a dummy .reloc section so EFI knows it's a relocatable executable:
  160. .data
  161. dummy: .4byte 0
  162. #define IMAGE_REL_ABSOLUTE 0
  163. .section .areloc
  164. .4byte dummy // 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