peDataDebug.ml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. (*
  2. * This file is part of ilLib
  3. * Copyright (c)2004-2013 Haxe Foundation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. *)
  19. open PeData;;
  20. open Printf;;
  21. let machine_type_s m = match m with
  22. | TUnknown -> "TUnknown"
  23. | Ti386 -> "Ti386"
  24. | TR3000 -> "TR3000"
  25. | TR4000 -> "TR4000"
  26. | TR10000 -> "TR10000"
  27. | TWCeMipsV2 -> "TWCeMipsV2"
  28. | TAlpha -> "TAlpha"
  29. | TSh3 -> "TSh3"
  30. | TSh3Dsp -> "TSh3Dsp"
  31. | TSh3e -> "TSh3e"
  32. | TSh4 -> "TSh4"
  33. | TSh5 -> "TSh5"
  34. | TArm -> "TArm"
  35. | TArmN -> "TArmN"
  36. | TArm64 -> "TArm64"
  37. | TEbc -> "TEbc"
  38. | TThumb -> "TThumb"
  39. | TAm33 -> "TAm33"
  40. | TPowerPC -> "TPowerPC"
  41. | TPowerPCFP -> "TPowerPCFP"
  42. | TItanium64 -> "TItanium64"
  43. | TMips16 -> "TMips16"
  44. | TAlpha64 -> "TAlpha64"
  45. | TMipsFpu -> "TMipsFpu"
  46. | TMipsFpu16 -> "TMipsFpu16"
  47. | TTriCore -> "TTriCore"
  48. | TAmd64 -> "TAmd64"
  49. | TM32R -> "TM32R"
  50. | TOSXAmd64 -> "TOSXAmd64"
  51. | TLinuxAmd64 -> "TLinuxAmd64"
  52. let coff_prop_s p = match p with
  53. | RelocsStripped -> "RelocsStripped"
  54. | ExecutableImage -> "ExecutableImage"
  55. | LineNumsStripped -> "LineNumsStripped"
  56. | LocalSymsStripped -> "LocalSymsStripped"
  57. | AgressiveWsTrim -> "AgressiveWsTrim"
  58. | LargeAddressAware -> "LargeAddressAware"
  59. | BytesReversedLO -> "BytesReversedLO"
  60. | Machine32Bit -> "Machine32Bit"
  61. | DebugStripped -> "DebugStripped"
  62. | RemovableRunFromSwap -> "RemovableRunFromSwap"
  63. | NetRunFromSwap -> "NetRunFromSwap"
  64. | FileSystem -> "FileSystem"
  65. | FileDll -> "FileDll"
  66. | UpSystemOnly -> "UpSystemOnly"
  67. | BytesReversedHI -> "BytesReversedHI"
  68. let coff_header_s h =
  69. sprintf "#COFF_HEADER\n\tmachine: %s\n\tnsections: %d\n\ttimestamp: %ld\n\tsymbol_tbl_pointer: %ld\n\tnsymbols: %d\n\toptheader_size: %x\n\tprops: [%s]\n"
  70. (machine_type_s h.coff_machine)
  71. h.coff_nsections
  72. h.coff_timestamp
  73. h.coff_symbol_table_pointer
  74. h.coff_nsymbols
  75. h.coff_optheader_size
  76. (String.concat ", " (List.map coff_prop_s h.coff_props))
  77. let pe_magic_s = function
  78. | P32 -> "P32"
  79. | PRom -> "PRom"
  80. | P64 -> "P64"
  81. let subsystem_s = function
  82. | SUnknown -> "SUnknown" (* 0 *)
  83. | SNative -> "SNative" (* 1 *)
  84. | SWGui -> "SWGui" (* 2 *)
  85. | SWCui -> "SWCui" (* 3 *)
  86. | SPCui -> "SPCui" (* 7 *)
  87. | SWCeGui -> "SWCeGui" (* 9 *)
  88. | SEfi -> "SEfi" (* 10 *)
  89. | SEfiBoot -> "SEfiBoot" (* 11 *)
  90. | SEfiRuntime -> "SEfiRuntime" (* 12 *)
  91. | SEfiRom -> "SEfiRom" (* 13 *)
  92. | SXbox -> "SXbox" (* 14 *)
  93. let dll_prop_s = function
  94. | DDynamicBase -> "DDynamicBase" (* 0x0040 *)
  95. | DForceIntegrity -> "DForceIntegrity" (* 0x0080 *)
  96. | DNxCompat -> "DNxCompat" (* 0x0100 *)
  97. | DNoIsolation -> "DNoIsolation" (* 0x0200 *)
  98. | DNoSeh -> "DNoSeh" (* 0x0400 *)
  99. | DNoBind -> "DNoBind" (* 0x0800 *)
  100. | DWdmDriver -> "DWdmDriver" (* 0x2000 *)
  101. | DTerminalServer -> "DTerminalServer" (* 0x8000 *)
  102. let section_prop_s = function
  103. | SNoPad -> "SNoPad"
  104. | SHasCode -> "SHasCode"
  105. | SHasIData -> "SHasIData"
  106. | SHasData -> "SHasData"
  107. | SHasLinkInfo -> "SHasLinkInfo"
  108. | SLinkRemove -> "SLinkRemove"
  109. | SGlobalRel -> "SGlobalRel"
  110. | SHas16BitMem -> "SHas16BitMem"
  111. | SAlign1Bytes -> "SAlign1Bytes"
  112. | SAlign2Bytes -> "SAlign2Bytes"
  113. | SAlign4Bytes -> "SAlign4Bytes"
  114. | SAlign8Bytes -> "SAlign8Bytes"
  115. | SAlign16Bytes -> "SAlign16Bytes"
  116. | SAlign32Bytes -> "SAlign32Bytes"
  117. | SAlign64Bytes -> "SAlign64Bytes"
  118. | SAlign128Bytes -> "SAlign128Bytes"
  119. | SAlign256Bytes -> "SAlign256Bytes"
  120. | SAlign512Bytes -> "SAlign512Bytes"
  121. | SAlign1024Bytes -> "SAlign1024Bytes"
  122. | SAlign2048Bytes -> "SAlign2048Bytes"
  123. | SAlign4096Bytes -> "SAlign4096Bytes"
  124. | SAlign8192Bytes -> "SAlign8192Bytes"
  125. | SHasExtRelocs -> "SHasExtRelocs"
  126. | SCanDiscard -> "SCanDiscard"
  127. | SNotCached -> "SNotCached"
  128. | SNotPaged -> "SNotPaged"
  129. | SShared -> "SShared"
  130. | SExec -> "SExec"
  131. | SRead -> "SRead"
  132. | SWrite -> "SWrite"
  133. let pe_section_s s =
  134. Printf.sprintf "\t%s :\n\t\trva: %lx\n\t\traw size: %lx\n\t\tprops: [%s]"
  135. s.s_name
  136. s.s_vaddr
  137. s.s_raw_size
  138. (String.concat ", " (List.map section_prop_s s.s_props))
  139. let data_dirs_s a =
  140. let lst = Array.to_list (Array.mapi (fun i (r,l) ->
  141. let _,s = directory_type_info (directory_type_of_int i) in
  142. Printf.sprintf "%s: %lx (%lx)" s r l
  143. ) a) in
  144. String.concat "\n\t\t" lst
  145. let pe_header_s h =
  146. sprintf "#PE_HEADER\n\tmagic: %s\n\tmajor.minor %d.%d\n\tsubsystem: %s\n\tdll props: [%s]\n\tndata_dir: %i\n\t\t%s\n#SECTIONS\n%s"
  147. (pe_magic_s h.pe_magic)
  148. h.pe_major h.pe_minor
  149. (subsystem_s h.pe_subsystem)
  150. (String.concat ", " (List.map dll_prop_s h.pe_dll_props))
  151. h.pe_ndata_dir
  152. (data_dirs_s h.pe_data_dirs)
  153. (String.concat "\n" (List.map pe_section_s (Array.to_list h.pe_sections)))
  154. let symbol_lookup_s = function
  155. | SName (hint,s) -> "SName(" ^ string_of_int hint ^ ", " ^ s ^ ")"
  156. | SOrdinal i -> "SOrdinal(" ^ string_of_int i ^ ")"
  157. let idata_table_s t =
  158. sprintf "#IMPORT %s:\n\t%s"
  159. t.imp_name
  160. (String.concat "\n\t" (List.map symbol_lookup_s t.imp_imports))
  161. let clr_flag_s = function
  162. | FIlOnly -> "FIlOnly" (* 0x1 *)
  163. | F32BitRequired -> "F32BitRequired" (* 0x2 *)
  164. | FIlLibrary -> "FIlLibrary" (* 0x4 *)
  165. | FSigned -> "FSigned" (* 0x8 *)
  166. | FNativeEntry -> "FNativeEntry" (* 0x10 *)
  167. | FTrackDebug -> "FTrackDebug" (* 0x10000 *)
  168. let clr_header_s h =
  169. sprintf "#CLR v%d.%d\n\tflags: %s"
  170. h.clr_major
  171. h.clr_minor
  172. (String.concat ", " (List.map clr_flag_s h.clr_flags))