peDataDebug.ml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. let coff_prop_s p = match p with
  51. | RelocsStripped -> "RelocsStripped"
  52. | ExecutableImage -> "ExecutableImage"
  53. | LineNumsStripped -> "LineNumsStripped"
  54. | LocalSymsStripped -> "LocalSymsStripped"
  55. | AgressiveWsTrim -> "AgressiveWsTrim"
  56. | LargeAddressAware -> "LargeAddressAware"
  57. | BytesReversedLO -> "BytesReversedLO"
  58. | Machine32Bit -> "Machine32Bit"
  59. | DebugStripped -> "DebugStripped"
  60. | RemovableRunFromSwap -> "RemovableRunFromSwap"
  61. | NetRunFromSwap -> "NetRunFromSwap"
  62. | FileSystem -> "FileSystem"
  63. | FileDll -> "FileDll"
  64. | UpSystemOnly -> "UpSystemOnly"
  65. | BytesReversedHI -> "BytesReversedHI"
  66. let coff_header_s h =
  67. 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"
  68. (machine_type_s h.coff_machine)
  69. h.coff_nsections
  70. h.coff_timestamp
  71. h.coff_symbol_table_pointer
  72. h.coff_nsymbols
  73. h.coff_optheader_size
  74. (String.concat ", " (List.map coff_prop_s h.coff_props))
  75. let pe_magic_s = function
  76. | P32 -> "P32"
  77. | PRom -> "PRom"
  78. | P64 -> "P64"
  79. let subsystem_s = function
  80. | SUnknown -> "SUnknown" (* 0 *)
  81. | SNative -> "SNative" (* 1 *)
  82. | SWGui -> "SWGui" (* 2 *)
  83. | SWCui -> "SWCui" (* 3 *)
  84. | SPCui -> "SPCui" (* 7 *)
  85. | SWCeGui -> "SWCeGui" (* 9 *)
  86. | SEfi -> "SEfi" (* 10 *)
  87. | SEfiBoot -> "SEfiBoot" (* 11 *)
  88. | SEfiRuntime -> "SEfiRuntime" (* 12 *)
  89. | SEfiRom -> "SEfiRom" (* 13 *)
  90. | SXbox -> "SXbox" (* 14 *)
  91. let dll_prop_s = function
  92. | DDynamicBase -> "DDynamicBase" (* 0x0040 *)
  93. | DForceIntegrity -> "DForceIntegrity" (* 0x0080 *)
  94. | DNxCompat -> "DNxCompat" (* 0x0100 *)
  95. | DNoIsolation -> "DNoIsolation" (* 0x0200 *)
  96. | DNoSeh -> "DNoSeh" (* 0x0400 *)
  97. | DNoBind -> "DNoBind" (* 0x0800 *)
  98. | DWdmDriver -> "DWdmDriver" (* 0x2000 *)
  99. | DTerminalServer -> "DTerminalServer" (* 0x8000 *)
  100. let section_prop_s = function
  101. | SNoPad -> "SNoPad"
  102. | SHasCode -> "SHasCode"
  103. | SHasIData -> "SHasIData"
  104. | SHasData -> "SHasData"
  105. | SHasLinkInfo -> "SHasLinkInfo"
  106. | SLinkRemove -> "SLinkRemove"
  107. | SGlobalRel -> "SGlobalRel"
  108. | SHas16BitMem -> "SHas16BitMem"
  109. | SAlign1Bytes -> "SAlign1Bytes"
  110. | SAlign2Bytes -> "SAlign2Bytes"
  111. | SAlign4Bytes -> "SAlign4Bytes"
  112. | SAlign8Bytes -> "SAlign8Bytes"
  113. | SAlign16Bytes -> "SAlign16Bytes"
  114. | SAlign32Bytes -> "SAlign32Bytes"
  115. | SAlign64Bytes -> "SAlign64Bytes"
  116. | SAlign128Bytes -> "SAlign128Bytes"
  117. | SAlign256Bytes -> "SAlign256Bytes"
  118. | SAlign512Bytes -> "SAlign512Bytes"
  119. | SAlign1024Bytes -> "SAlign1024Bytes"
  120. | SAlign2048Bytes -> "SAlign2048Bytes"
  121. | SAlign4096Bytes -> "SAlign4096Bytes"
  122. | SAlign8192Bytes -> "SAlign8192Bytes"
  123. | SHasExtRelocs -> "SHasExtRelocs"
  124. | SCanDiscard -> "SCanDiscard"
  125. | SNotCached -> "SNotCached"
  126. | SNotPaged -> "SNotPaged"
  127. | SShared -> "SShared"
  128. | SExec -> "SExec"
  129. | SRead -> "SRead"
  130. | SWrite -> "SWrite"
  131. let pe_section_s s =
  132. Printf.sprintf "\t%s :\n\t\trva: %lx\n\t\traw size: %lx\n\t\tprops: [%s]"
  133. s.s_name
  134. s.s_vaddr
  135. s.s_raw_size
  136. (String.concat ", " (List.map section_prop_s s.s_props))
  137. let data_dirs_s a =
  138. let lst = Array.to_list (Array.mapi (fun i (r,l) ->
  139. let _,s = directory_type_info (directory_type_of_int i) in
  140. Printf.sprintf "%s: %lx (%lx)" s r l
  141. ) a) in
  142. String.concat "\n\t\t" lst
  143. let pe_header_s h =
  144. 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"
  145. (pe_magic_s h.pe_magic)
  146. h.pe_major h.pe_minor
  147. (subsystem_s h.pe_subsystem)
  148. (String.concat ", " (List.map dll_prop_s h.pe_dll_props))
  149. h.pe_ndata_dir
  150. (data_dirs_s h.pe_data_dirs)
  151. (String.concat "\n" (List.map pe_section_s (Array.to_list h.pe_sections)))
  152. let symbol_lookup_s = function
  153. | SName (hint,s) -> "SName(" ^ string_of_int hint ^ ", " ^ s ^ ")"
  154. | SOrdinal i -> "SOrdinal(" ^ string_of_int i ^ ")"
  155. let idata_table_s t =
  156. sprintf "#IMPORT %s:\n\t%s"
  157. t.imp_name
  158. (String.concat "\n\t" (List.map symbol_lookup_s t.imp_imports))
  159. let clr_flag_s = function
  160. | FIlOnly -> "FIlOnly" (* 0x1 *)
  161. | F32BitRequired -> "F32BitRequired" (* 0x2 *)
  162. | FIlLibrary -> "FIlLibrary" (* 0x4 *)
  163. | FSigned -> "FSigned" (* 0x8 *)
  164. | FNativeEntry -> "FNativeEntry" (* 0x10 *)
  165. | FTrackDebug -> "FTrackDebug" (* 0x10000 *)
  166. let clr_header_s h =
  167. sprintf "#CLR v%d.%d\n\tflags: %s"
  168. h.clr_major
  169. h.clr_minor
  170. (String.concat ", " (List.map clr_flag_s h.clr_flags))