Subversion Repositories Kolibri OS

Rev

Rev 6702 | Go to most recent revision | 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 SizeOfOptionalHeader       word  from 14h
  31. if SizeOfOptionalHeader<>0E0h
  32. error Nonstandard PE header
  33. end if
  34. load Characteristics            word  from 16h
  35. load AddressOfEntryPoint        dword from 28h
  36. load ImageBase                  dword from 34h
  37. load SectionAlignment           dword from 38h
  38. load FileAlignment              dword from 3Ch
  39. load MajorOperatingSystemVersion word from 40h
  40. load MinorOperatingSystemVersion word from 42h
  41. load MajorSubsystemVersion      word  from 48h
  42. load MinorSubsystemVersion      word  from 4Ah
  43. load SizeOfImage                dword from 50h
  44. load SizeOfHeaders              dword from 54h
  45. load Subsystem                  word  from 5Ch
  46. load SizeOfStackReserve         dword from 60h
  47. load SizeOfHeapReserve          dword from 68h
  48. load SrcNumberOfRvaAndSizes     dword from 74h
  49.  
  50. DstNumberOfRvaAndSizes = 0
  51. irps dir,SPE_DIR_ORDER
  52. {
  53. if IMAGE_DIRECTORY_ENTRY_#dir < SrcNumberOfRvaAndSizes
  54. load DirRVA_#dir dword from 78h + 8*IMAGE_DIRECTORY_ENTRY_#dir
  55. load DirSize_#dir dword from 7Ch + 8*IMAGE_DIRECTORY_ENTRY_#dir
  56. else
  57. DirRVA_#dir = 0
  58. DirSize_#dir = 0
  59. end if
  60. if DirRVA_#dir > 0 & DirSize_#dir > 0
  61. DstNumberOfRvaAndSizes = SPE_DIRECTORY_#dir + 1
  62. end if
  63. }
  64.  
  65. end virtual
  66.  
  67. SectionAlignmentLog = 0
  68. while SectionAlignment <> 1 shl SectionAlignmentLog
  69. SectionAlignmentLog = SectionAlignmentLog + 1
  70. end while
  71. FileAlignmentLog = 0
  72. while FileAlignment <> 1 shl FileAlignmentLog
  73. FileAlignmentLog = FileAlignmentLog + 1
  74. end while
  75.  
  76. ; header
  77.         dw      'PE' xor 'S'    ; Signature
  78.         dw      Characteristics or 0x100        ; IMAGE_FILE_32BIT_MACHINE
  79.         dd      AddressOfEntryPoint
  80.         dd      ImageBase
  81.         db      SectionAlignmentLog
  82.         db      FileAlignmentLog
  83.         db      MajorSubsystemVersion
  84.         db      MinorSubsystemVersion
  85.         dd      SizeOfImage
  86.         dd      SizeOfStackReserve
  87.         dd      SizeOfHeapReserve
  88. SizeOfHeadersField:
  89.         dd      0
  90.         db      Subsystem
  91.         db      DstNumberOfRvaAndSizes
  92.         dw      NumberOfSections
  93. ; directories
  94. irps dir,SPE_DIR_ORDER
  95. {
  96. if SPE_DIRECTORY_#dir < DstNumberOfRvaAndSizes
  97.         dd      DirRVA_#dir, DirSize_#dir
  98. end if
  99. }
  100.  
  101. NumBytesDeleted = pehea + 0F8h - $ + NumberOfSections*0Ch
  102. DeltaDeleted = NumBytesDeleted and not (FileAlignment - 1)
  103. ; Use store instead of declaring SizeOfHeaders - DeltaDeleted directly in dd
  104. ; to avoid the second compilation pass.
  105. store dword SizeOfHeaders - DeltaDeleted at SizeOfHeadersField
  106. ; sections
  107. repeat NumberOfSections
  108. file filename:pehea+0F8h+(%-1)*28h,18h
  109. load a dword from $-4
  110. store dword a-DeltaDeleted at $-4
  111. file filename:pehea+0F8h+(%-1)*28h+24h,4
  112. end repeat
  113. ; padding to keep FileAlignment
  114. times NumBytesDeleted - DeltaDeleted db 0
  115. ; data
  116. file filename:pehea+0F8h+NumberOfSections*28h
  117.