Subversion Repositories Kolibri OS

Rev

Rev 6702 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ; If you know macro language of FASM, there is almost nothing to comment here.
  2. ; If you don't know macro language of FASM, comments would not help you.
  3.  
  4. filename equ '%EXENAME%'
  5.  
  6. SPE_DIR_ORDER fix IMPORT EXPORT BASERELOC EXCEPTION TLS BOUND_IMPORT RESOURCE
  7. count = 0
  8. irps dir,SPE_DIR_ORDER
  9. {
  10. SPE_DIRECTORY_#dir = count
  11. count = count + 1
  12. }
  13.  
  14. IMAGE_DIRECTORY_ENTRY_EXPORT = 0
  15. IMAGE_DIRECTORY_ENTRY_IMPORT = 1
  16. IMAGE_DIRECTORY_ENTRY_RESOURCE = 2
  17. IMAGE_DIRECTORY_ENTRY_EXCEPTION = 3
  18. IMAGE_DIRECTORY_ENTRY_BASERELOC = 5
  19. IMAGE_DIRECTORY_ENTRY_TLS = 9
  20. IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT = 11
  21.  
  22. virtual at 0
  23. file filename:3Ch,4
  24. load pehea dword from 0
  25. end virtual
  26.  
  27. virtual at 0
  28. file filename:pehea,0F8h
  29. load NumberOfSections           word  from 6
  30. load TimeDateStamp              dword from 8
  31. load SizeOfOptionalHeader       word  from 14h
  32. if SizeOfOptionalHeader<>0E0h
  33. error Nonstandard PE header
  34. end if
  35. load Characteristics            word  from 16h
  36. load AddressOfEntryPoint        dword from 28h
  37. load ImageBase                  dword from 34h
  38. load SectionAlignment           dword from 38h
  39. load FileAlignment              dword from 3Ch
  40. load MajorOperatingSystemVersion word from 40h
  41. load MinorOperatingSystemVersion word from 42h
  42. load MajorSubsystemVersion      word  from 48h
  43. load MinorSubsystemVersion      word  from 4Ah
  44. load SizeOfImage                dword from 50h
  45. load SizeOfHeaders              dword from 54h
  46. load Subsystem                  word  from 5Ch
  47. load SizeOfStackReserve         dword from 60h
  48. load SizeOfHeapReserve          dword from 68h
  49. load SrcNumberOfRvaAndSizes     dword from 74h
  50.  
  51. DstNumberOfRvaAndSizes = 0
  52. irps dir,SPE_DIR_ORDER
  53. {
  54. if IMAGE_DIRECTORY_ENTRY_#dir < SrcNumberOfRvaAndSizes
  55. load DirRVA_#dir dword from 78h + 8*IMAGE_DIRECTORY_ENTRY_#dir
  56. load DirSize_#dir dword from 7Ch + 8*IMAGE_DIRECTORY_ENTRY_#dir
  57. else
  58. DirRVA_#dir = 0
  59. DirSize_#dir = 0
  60. end if
  61. if DirRVA_#dir > 0 & DirSize_#dir > 0
  62. DstNumberOfRvaAndSizes = SPE_DIRECTORY_#dir + 1
  63. end if
  64. }
  65.  
  66. end virtual
  67.  
  68. SectionAlignmentLog = 0
  69. while SectionAlignment <> 1 shl SectionAlignmentLog
  70. SectionAlignmentLog = SectionAlignmentLog + 1
  71. end while
  72. FileAlignmentLog = 0
  73. while FileAlignment <> 1 shl FileAlignmentLog
  74. FileAlignmentLog = FileAlignmentLog + 1
  75. end while
  76.  
  77. ; header
  78.         dw      'PE' xor 'S'    ; Signature
  79.         dw      Characteristics or 0x100        ; IMAGE_FILE_32BIT_MACHINE
  80.         dd      AddressOfEntryPoint
  81.         dd      ImageBase
  82.         db      SectionAlignmentLog
  83.         db      FileAlignmentLog
  84.         db      MajorSubsystemVersion
  85.         db      MinorSubsystemVersion
  86.         dd      SizeOfImage
  87.         dd      SizeOfStackReserve
  88.         dd      SizeOfHeapReserve
  89. SizeOfHeadersField:
  90.         dd      0
  91.         db      Subsystem
  92.         db      DstNumberOfRvaAndSizes
  93.         dw      NumberOfSections
  94. ; directories
  95. irps dir,SPE_DIR_ORDER
  96. {
  97. if SPE_DIRECTORY_#dir < DstNumberOfRvaAndSizes
  98.         dd      DirRVA_#dir, DirSize_#dir
  99. end if
  100. }
  101.  
  102. NumBytesDeleted = pehea + 0F8h - $ + NumberOfSections*0Ch
  103. DeltaDeleted = NumBytesDeleted and not (FileAlignment - 1)
  104. ; Use store instead of declaring SizeOfHeaders - DeltaDeleted directly in dd
  105. ; to avoid the second compilation pass.
  106. store dword SizeOfHeaders - DeltaDeleted at SizeOfHeadersField
  107. TimeStampInExportTable = 0
  108. ; sections
  109. repeat NumberOfSections
  110. file filename:pehea+0F8h+(%-1)*28h,18h
  111. load VirtualSize dword from $-10h
  112. load VirtualAddress dword from $-0Ch
  113. load SizeOfRawData dword from $-8
  114. load PointerToRawData dword from $-4
  115. PointerToRawData = PointerToRawData - DeltaDeleted
  116. store dword PointerToRawData at $-4
  117. if DirRVA_EXPORT <> 0 & DirRVA_EXPORT+4 >= VirtualAddress & DirRVA_EXPORT+8 <= VirtualAddress + VirtualSize & DirRVA_EXPORT+8 <= VirtualAddress + SizeOfRawData
  118. TimeStampInExportTable = DirRVA_EXPORT+4 - VirtualAddress + PointerToRawData
  119. end if
  120. file filename:pehea+0F8h+(%-1)*28h+24h,4
  121. end repeat
  122. ; padding to keep FileAlignment
  123. times NumBytesDeleted - DeltaDeleted db 0
  124. ; data
  125. file filename:pehea+0F8h+NumberOfSections*28h
  126. if TimeDateStamp <> 0 & TimeStampInExportTable <> 0
  127. store dword TimeDateStamp at TimeStampInExportTable
  128. end if
  129.