typenative 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/CHANGELOG.md +171 -0
  2. package/README.md +62 -22
  3. package/ROUND2-HANDOFF.md +200 -0
  4. package/bin/cli.js +1 -0
  5. package/bin/index.js +13 -2
  6. package/bin/main.js +1 -0
  7. package/bin/node/assert.js +2 -2
  8. package/bin/node/async_hooks.js +1 -1
  9. package/bin/node/buffer.js +16 -5
  10. package/bin/node/child_process.js +7 -7
  11. package/bin/node/cluster.js +3 -3
  12. package/bin/node/crypto_keys.js +21 -21
  13. package/bin/node/dgram.js +1 -1
  14. package/bin/node/diagnostics_channel.js +3 -3
  15. package/bin/node/dns.js +2 -2
  16. package/bin/node/events.js +3 -3
  17. package/bin/node/fetch.js +2 -2
  18. package/bin/node/fs.js +3 -3
  19. package/bin/node/fs_watch.js +5 -5
  20. package/bin/node/http.js +3 -3
  21. package/bin/node/http2.js +3 -3
  22. package/bin/node/net.js +8 -8
  23. package/bin/node/path.js +17 -3
  24. package/bin/node/process.js +34 -11
  25. package/bin/node/readline.js +1 -1
  26. package/bin/node/registry.js +4 -1
  27. package/bin/node/sqlite.js +6 -6
  28. package/bin/node/stream.js +81 -15
  29. package/bin/node/stream_web.js +10 -10
  30. package/bin/node/system_extra.js +3 -3
  31. package/bin/node/test.js +674 -366
  32. package/bin/node/test_coverage.js +187 -0
  33. package/bin/node/test_report.js +1392 -0
  34. package/bin/node/test_run.js +575 -0
  35. package/bin/node/timers.js +4 -4
  36. package/bin/node/tls.js +5 -5
  37. package/bin/node/util.js +31 -7
  38. package/bin/node/util_extra.js +7 -7
  39. package/bin/node/webview.js +654 -0
  40. package/bin/node/worker_threads.js +1 -1
  41. package/bin/node/zlib.js +3 -3
  42. package/bin/parse-node.js +4 -0
  43. package/bin/transpiler.js +10390 -1178
  44. package/bin/version.js +1 -1
  45. package/conformance.md +468 -340
  46. package/examples/README.md +47 -10
  47. package/examples/desktop-notes/README.md +108 -0
  48. package/examples/desktop-notes/bench.mjs +125 -0
  49. package/examples/desktop-notes/main.ts +131 -0
  50. package/examples/desktop-notes/package-lock.json +726 -0
  51. package/examples/desktop-notes/package.json +9 -0
  52. package/examples/desktop-notes/ui.ts +176 -0
  53. package/examples/dupe-finder/README.md +76 -0
  54. package/examples/dupe-finder/bench.mjs +120 -0
  55. package/examples/dupe-finder/main.ts +87 -0
  56. package/examples/mandelbrot-png/README.md +65 -0
  57. package/examples/mandelbrot-png/bench.mjs +95 -0
  58. package/examples/mandelbrot-png/main.ts +98 -0
  59. package/examples/mandelbrot-png/preview.png +0 -0
  60. package/examples/marked-cli/README.md +159 -0
  61. package/examples/marked-cli/bench.mjs +178 -0
  62. package/examples/marked-cli/main.ts +292 -0
  63. package/examples/marked-cli/parity.mjs +185 -0
  64. package/examples/marked-cli/vendor/marked/LICENSE +44 -0
  65. package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
  66. package/examples/todo-cli/README.md +69 -0
  67. package/examples/todo-cli/bench.mjs +89 -0
  68. package/examples/todo-cli/main.ts +92 -0
  69. package/examples/yaml-cli/README.md +136 -0
  70. package/examples/yaml-cli/bench.mjs +265 -0
  71. package/examples/yaml-cli/main.ts +29 -0
  72. package/examples/yaml-cli/parity.mjs +156 -0
  73. package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
  74. package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
  75. package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
  76. package/govendor/github.com/jchv/go-webview2/README.md +21 -0
  77. package/govendor/github.com/jchv/go-webview2/common.go +84 -0
  78. package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
  79. package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
  80. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
  81. package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
  82. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
  83. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
  84. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
  85. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
  86. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
  87. package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
  88. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
  89. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
  90. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
  91. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
  92. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
  93. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
  94. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
  95. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
  96. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
  97. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
  98. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
  99. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
  100. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
  101. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
  102. package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
  103. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
  104. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
  105. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
  106. package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
  107. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
  108. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
  109. package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
  110. package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
  111. package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
  112. package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
  113. package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
  114. package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
  115. package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
  116. package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
  117. package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
  118. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
  119. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
  120. package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
  121. package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
  122. package/govendor/github.com/jchv/go-winloader/README.md +124 -0
  123. package/govendor/github.com/jchv/go-winloader/common.go +11 -0
  124. package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
  125. package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
  126. package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
  127. package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
  128. package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
  129. package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
  130. package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
  131. package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
  132. package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
  133. package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
  134. package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
  135. package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
  136. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
  137. package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
  138. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
  139. package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
  140. package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
  141. package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
  142. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
  143. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
  144. package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
  145. package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
  146. package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
  147. package/govendor/manifest.txt +3 -1
  148. package/package.json +1 -1
  149. package/tsparser/main.go +4 -0
  150. package/types/typenative-npm.d.ts +190 -2
  151. package/types/typenative.d.ts +98 -31
@@ -0,0 +1,522 @@
1
+ package pe
2
+
3
+ // CONSTANTS / MAGIC NUMBERS
4
+
5
+ // MZSignature is the signature of the MZ format. This is the value of the
6
+ // Signature field in ImageDOSHeader.
7
+ var MZSignature = [2]byte{'M', 'Z'}
8
+
9
+ // PESignature is the signature of the PE format. This is the value of the
10
+ // Signature field in ImageNTHeaders32 and ImageNTHeaders64.
11
+ var PESignature = [4]byte{'P', 'E', 0, 0}
12
+
13
+ // Enumeration of magic numbers
14
+ const (
15
+ // ImageNTOptionalHeader32Magic is the magic number for 32-bit optional
16
+ // header (ImageOptionalHeader32)
17
+ ImageNTOptionalHeader32Magic = 0x010b
18
+
19
+ // ImageNTOptionalHeader64Magic is the magic number for 64-bit optional
20
+ // header (ImageOptionalHeader64)
21
+ ImageNTOptionalHeader64Magic = 0x020b
22
+ )
23
+
24
+ // Enumeration of structure lengths.
25
+ const (
26
+ // SizeOfImageDOSHeader is the on-disk size of the ImageDOSHeader
27
+ // structure.
28
+ SizeOfImageDOSHeader = 64
29
+
30
+ // SizeOfImageFileHeader is the on-disk size of the ImageFileHeader
31
+ // structure.
32
+ SizeOfImageFileHeader = 20
33
+
34
+ // SizeOfImageOptionalHeader32 is the on-disk size of the
35
+ // ImageOptionalHeader32 structure.
36
+ SizeOfImageOptionalHeader32 = 224
37
+
38
+ // SizeOfImageOptionalHeader64 is the on-disk size of the
39
+ // ImageOptionalHeader64 structure.
40
+ SizeOfImageOptionalHeader64 = 240
41
+
42
+ // SizeOfImageNTHeaders32 is the on-disk size of the ImageNTHeaders32
43
+ // structure.
44
+ SizeOfImageNTHeaders32 = 248
45
+
46
+ // SizeOfImageNTHeaders64 is the on-disk size of the ImageNTHeaders64
47
+ // structure.
48
+ SizeOfImageNTHeaders64 = 264
49
+
50
+ // SizeOfImageDataDirectory is the on-disk size of the ImageDataDirectory
51
+ // structure.
52
+ SizeOfImageDataDirectory = 8
53
+ )
54
+
55
+ // Enumeration of useful field offsets.
56
+ const (
57
+ // OffsetOfOptionalHeaderFromNTHeader is the offset from the start of
58
+ // the NT header to the optional header magic value. This is helpful for
59
+ // determining if the PE file is PE32 or PE64.
60
+ OffsetOfOptionalHeaderFromNTHeader = 0x18
61
+ )
62
+
63
+ // Enumeration of fixed-size array lengths in PE
64
+ const (
65
+ // NumDirectoryEntries specifies the number of data directory entries.
66
+ NumDirectoryEntries = 16
67
+
68
+ // SectionNameLength is the size of a section short name.
69
+ SectionNameLength = 8
70
+ )
71
+
72
+ // Enumeration of known Windows Loader limits. (Some of these may not be
73
+ // imposed by the format itself and purely by Windows runtime.)
74
+ const (
75
+ // MaxNumSections specifies the maximum number of sections that are
76
+ // allowed. This is imposed by Windows Loader.
77
+ MaxNumSections = 96
78
+ )
79
+
80
+ // ENUMERATION VALUES
81
+
82
+ // Enumeration of machine values for the file header.
83
+ const (
84
+ ImageFileMachineUnknown = 0x0000
85
+ ImageFileMachineTargetHost = 0x0001
86
+ ImageFileMachinei386 = 0x014c
87
+ ImageFileMachineR3000BE = 0x0160
88
+ ImageFileMachineR3000 = 0x0162
89
+ ImageFileMachineR4000 = 0x0166
90
+ ImageFileMachineR10000 = 0x0168
91
+ ImageFileMachineWCEMIPSv2 = 0x0169
92
+ ImageFileMachineAlpha = 0x0184
93
+ ImageFileMachineSH3 = 0x01a2
94
+ ImageFileMachineSH3DSP = 0x01a3
95
+ ImageFileMachineSH3E = 0x01a4
96
+ ImageFileMachineSH4 = 0x01a6
97
+ ImageFileMachineSH5 = 0x01a8
98
+ ImageFileMachineARM = 0x01c0
99
+ ImageFileMachineTHUMB = 0x01c2
100
+ ImageFileMachineARMNT = 0x01c4
101
+ ImageFileMachineAM33 = 0x01d3
102
+ ImageFileMachinePowerPC = 0x01F0
103
+ ImageFileMachinePowerPCFP = 0x01f1
104
+ ImageFileMachineIA64 = 0x0200
105
+ ImageFileMachineMIPS16 = 0x0266
106
+ ImageFileMachineAlpha64 = 0x0284
107
+ ImageFileMachineMIPSFPU = 0x0366
108
+ ImageFileMachineMIPSFPU16 = 0x0466
109
+ ImageFileMachineAXP64 = ImageFileMachineAlpha64
110
+ ImageFileMachineTricore = 0x0520
111
+ ImageFileMachineCEF = 0x00CE
112
+ ImageFileMachineEBC = 0x0EBC
113
+ ImageFileMachineAMD64 = 0x8664
114
+ ImageFileMachineM32R = 0x9041
115
+ ImageFileMachineARM64 = 0xAA64
116
+ ImageFileMachineCEE = 0x0C0E
117
+ ImageFileMachineRISCV32 = 0x5032
118
+ ImageFileMachineRISCV64 = 0x5064
119
+ ImageFileMachineRISCV128 = 0x5128
120
+ )
121
+
122
+ // Enumeration of charateristics values for the file header.
123
+ const (
124
+ ImageFileRelocsStripped = 0x0001
125
+ ImageFileExecutableImage = 0x0002
126
+ ImageFileLineNumsStripped = 0x0004
127
+ ImageFileLocalSymsStripped = 0x0008
128
+ ImageFileAggressiveWSTrim = 0x0010
129
+ ImageFileLargeAddressAware = 0x0020
130
+ ImageFileBytesReversedLo = 0x0080
131
+ ImageFile32BitMachine = 0x0100
132
+ ImageFileDebugStripped = 0x0200
133
+ ImageFileRemovableRunFromSwap = 0x0400
134
+ ImageFileNetRunFromSwap = 0x0800
135
+ ImageFileSystem = 0x1000
136
+ ImageFileDLL = 0x2000
137
+ ImageFileUPSystemOnly = 0x4000
138
+ ImageFileBytesReversedHi = 0x8000
139
+ )
140
+
141
+ // Enumeration of image subsystem values.
142
+ const (
143
+ ImageSubsystemUnknown = 0
144
+ ImageSubsystemNative = 1
145
+ ImageSubsystemWindowsGUI = 2
146
+ ImageSubsystemWindowsCUI = 3
147
+ ImageSubsystemOS2CUI = 5
148
+ ImageSubsystemPOSIXCUI = 7
149
+ ImageSubsystemNativeWindows = 8
150
+ ImageSubsystemWindowsCEGUI = 9
151
+ ImageSubsystemEFIApplication = 10
152
+ ImageSubsystemEFIBootServiceDriver = 11
153
+ ImageSubsystemEFIRuntimeDriver = 12
154
+ ImageSubsystemEFIROM = 13
155
+ ImageSubsystemXBox = 14
156
+ ImageSubsystemWindowsBootApplication = 16
157
+ ImageSubsystemXBoxCodeCatalog = 17
158
+ )
159
+
160
+ // Enumeration of DLL characteristics values.
161
+ const (
162
+ ImageDLLCharacteristicsHighEntropyVA = 0x0020
163
+ ImageDLLCharacteristicsDynamicBase = 0x0040
164
+ ImageDLLCharacteristicsForceIntegrity = 0x0080
165
+ ImageDLLCharacteristicsNXCompat = 0x0100
166
+ ImageDLLCharacteristicsNoIsolation = 0x0200
167
+ ImageDLLCharacteristicsNoSEH = 0x0400
168
+ ImageDLLCharacteristicsNoBind = 0x0800
169
+ ImageDLLCharacteristicsAppContainer = 0x1000
170
+ ImageDLLCharacteristicsWDMDriver = 0x2000
171
+ ImageDLLCharacteristicsGuardCF = 0x4000
172
+ ImageDLLCharacteristicsTerminalServerAware = 0x8000
173
+ )
174
+
175
+ // Enumeration of image directory entry indexes. These represent indices into
176
+ // the data directory array of the optional header.
177
+ const (
178
+ ImageDirectoryEntryExport = 0
179
+ ImageDirectoryEntryImport = 1
180
+ ImageDirectoryEntryResource = 2
181
+ ImageDirectoryEntryException = 3
182
+ ImageDirectoryEntrySecurity = 4
183
+ ImageDirectoryEntryBaseReloc = 5
184
+ ImageDirectoryEntryDebug = 6
185
+ ImageDirectoryEntryCopyright = 7
186
+ ImageDirectoryEntryArchitecture = 7
187
+ ImageDirectoryEntryGlobalPtr = 8
188
+ ImageDirectoryEntryTLS = 9
189
+ ImageDirectoryEntryLoadConfig = 10
190
+ ImageDirectoryEntryBoundImport = 11
191
+ ImageDirectoryEntryIAT = 12
192
+ ImageDirectoryEntryDelayImport = 13
193
+ ImageDirectoryEntryCOMDescriptor = 14
194
+ )
195
+
196
+ // Enumeration of image section characteristics.
197
+ const (
198
+ ImageSectionCharacteristicsNoPad = 0x00000008
199
+ ImageSectionCharacteristicsContainsCode = 0x00000020
200
+ ImageSectionCharacteristicsContainsInitializedData = 0x00000040
201
+ ImageSectionCharacteristicsContainsUninitailizedData = 0x00000080
202
+ ImageSectionCharacteristicsLinkOther = 0x00000100
203
+ ImageSectionCharacteristicsLinkInfo = 0x00000200
204
+ ImageSectionCharacteristicsLinkRemove = 0x00000800
205
+ ImageSectionCharacteristicsLinkCOMDAT = 0x00001000
206
+ ImageSectionCharacteristicsNoDeferSpecExc = 0x00004000
207
+ ImageSectionCharacteristicsGPRel = 0x00008000
208
+ ImageSectionCharacteristicsMemoryFarData = 0x00008000
209
+ ImageSectionCharacteristicsMemoryPurgeable = 0x00020000
210
+ ImageSectionCharacteristicsMemory16Bit = 0x00020000
211
+ ImageSectionCharacteristicsMemoryLocked = 0x00040000
212
+ ImageSectionCharacteristicsMemoryPreload = 0x00080000
213
+ ImageSectionCharacteristicsAlign1Bytes = 0x00100000
214
+ ImageSectionCharacteristicsAlign2Bytes = 0x00200000
215
+ ImageSectionCharacteristicsAlign4Bytes = 0x00300000
216
+ ImageSectionCharacteristicsAlign8Bytes = 0x00400000
217
+ ImageSectionCharacteristicsAlign16Bytes = 0x00500000
218
+ ImageSectionCharacteristicsAlign32Bytes = 0x00600000
219
+ ImageSectionCharacteristicsAlign64Bytes = 0x00700000
220
+ ImageSectionCharacteristicsAlign128Bytes = 0x00800000
221
+ ImageSectionCharacteristicsAlign256Bytes = 0x00900000
222
+ ImageSectionCharacteristicsAlign512Bytes = 0x00A00000
223
+ ImageSectionCharacteristicsAlign1024Bytes = 0x00B00000
224
+ ImageSectionCharacteristicsAlign2048Bytes = 0x00C00000
225
+ ImageSectionCharacteristicsAlign4096Bytes = 0x00D00000
226
+ ImageSectionCharacteristicsAlign8192Bytes = 0x00E00000
227
+ ImageSectionCharacteristicsAlignMask = 0x00F00000
228
+ ImageSectionCharacteristicsLinkNumRelocOverflow = 0x01000000
229
+ ImageSectionCharacteristicsMemoryDiscardable = 0x02000000
230
+ ImageSectionCharacteristicsMemoryNotCached = 0x04000000
231
+ ImageSectionCharacteristicsMemoryNotPaged = 0x08000000
232
+ ImageSectionCharacteristicsMemoryShared = 0x10000000
233
+ ImageSectionCharacteristicsMemoryExecute = 0x20000000
234
+ ImageSectionCharacteristicsMemoryRead = 0x40000000
235
+ ImageSectionCharacteristicsMemoryWrite = 0x80000000
236
+ )
237
+
238
+ // Enumeration of TLS characteristics.
239
+ const (
240
+ ImageSectionTLSCharacteristicsScaleIndex = 0x00000001
241
+ )
242
+
243
+ // Enumeration of relocation types.
244
+ const (
245
+ ImageRelBasedAbsolute = 0
246
+ ImageRelBasedHigh = 1
247
+ ImageRelBasedLow = 2
248
+ ImageRelBasedHighLow = 3
249
+ ImageRelBasedHighAdj = 4
250
+ ImageRelBasedMachineSpecific5 = 5
251
+ ImageRelBasedReserved = 6
252
+ ImageRelBasedMachineSpecific7 = 7
253
+ ImageRelBasedMachineSpecific8 = 8
254
+ ImageRelBasedMachineSpecific9 = 9
255
+ ImageRelBasedDir64 = 10
256
+ )
257
+
258
+ // ImageDOSHeader is the structure of the DOS MZ Executable format. All PE
259
+ // files contain at least a valid stub DOS MZ executable at the top; the PE
260
+ // format itself starts at the address specified by NewHeaderAddr.
261
+ type ImageDOSHeader struct {
262
+ Signature [2]byte
263
+ LastPageBytes uint16
264
+ CountPages uint16
265
+ CountRelocs uint16
266
+ HeaderLen uint16
267
+ MinAlloc uint16
268
+ MaxAlloc uint16
269
+ InitialSS uint16
270
+ InitialSP uint16
271
+ Checksum uint16
272
+ InitialIP uint16
273
+ InitialCS uint16
274
+ RelocAddr uint16
275
+ OverlayNum uint16
276
+ Reserved [4]uint16
277
+ OEMID uint16
278
+ OEMInfo uint16
279
+ Reserved2 [10]uint16
280
+ NewHeaderAddr uint32
281
+ }
282
+
283
+ // ImageFileHeader contains some of the basic attributes about the PE/COFF
284
+ // file, including the number of sections and the machine type.
285
+ type ImageFileHeader struct {
286
+ Machine uint16
287
+ NumberOfSections uint16
288
+ TimeDateStamp uint32
289
+ PointerToSymbolTable uint32
290
+ NumberOfSymbols uint32
291
+ SizeOfOptionalHeader uint16
292
+ Characteristics uint16
293
+ }
294
+
295
+ // ImageOptionalHeader32 contains the optional header for 32-bit PE images. It
296
+ // is only 'optional' in the sense that not all PE/COFF binaries have it,
297
+ // however it is required for executables and DLLs.
298
+ type ImageOptionalHeader32 struct {
299
+ Magic uint16
300
+ MajorLinkerVersion uint8
301
+ MinorLinkerVersion uint8
302
+ SizeOfCode uint32
303
+ SizeOfInitializedData uint32
304
+ SizeOfUninitializedData uint32
305
+ AddressOfEntryPoint uint32
306
+ BaseOfCode uint32
307
+ BaseOfData uint32
308
+
309
+ ImageBase uint32
310
+ SectionAlignment uint32
311
+ FileAlignment uint32
312
+ MajorOperatingSystemVersion uint16
313
+ MinorOperatingSystemVersion uint16
314
+ MajorImageVersion uint16
315
+ MinorImageVersion uint16
316
+ MajorSubsystemVersion uint16
317
+ MinorSubsystemVersion uint16
318
+ Win32VersionValue uint32
319
+ SizeOfImage uint32
320
+ SizeOfHeaders uint32
321
+ CheckSum uint32
322
+ Subsystem uint16
323
+ DllCharacteristics uint16
324
+ SizeOfStackReserve uint32
325
+ SizeOfStackCommit uint32
326
+ SizeOfHeapReserve uint32
327
+ SizeOfHeapCommit uint32
328
+ LoaderFlags uint32
329
+ NumberOfRvaAndSizes uint32
330
+ DataDirectory [NumDirectoryEntries]ImageDataDirectory
331
+ }
332
+
333
+ // ImageOptionalHeader64 contains the optional header for 64-bit PE images.
334
+ type ImageOptionalHeader64 struct {
335
+ Magic uint16
336
+ MajorLinkerVersion uint8
337
+ MinorLinkerVersion uint8
338
+ SizeOfCode uint32
339
+ SizeOfInitializedData uint32
340
+ SizeOfUninitializedData uint32
341
+ AddressOfEntryPoint uint32
342
+ BaseOfCode uint32
343
+ ImageBase uint64
344
+ SectionAlignment uint32
345
+ FileAlignment uint32
346
+ MajorOperatingSystemVersion uint16
347
+ MinorOperatingSystemVersion uint16
348
+ MajorImageVersion uint16
349
+ MinorImageVersion uint16
350
+ MajorSubsystemVersion uint16
351
+ MinorSubsystemVersion uint16
352
+ Win32VersionValue uint32
353
+ SizeOfImage uint32
354
+ SizeOfHeaders uint32
355
+ CheckSum uint32
356
+ Subsystem uint16
357
+ DllCharacteristics uint16
358
+ SizeOfStackReserve uint64
359
+ SizeOfStackCommit uint64
360
+ SizeOfHeapReserve uint64
361
+ SizeOfHeapCommit uint64
362
+ LoaderFlags uint32
363
+ NumberOfRvaAndSizes uint32
364
+ DataDirectory [NumDirectoryEntries]ImageDataDirectory
365
+ }
366
+
367
+ // To64 converts the ImageOptionalHeader32 to an ImageOptionalHeader64.
368
+ func (i ImageOptionalHeader32) To64() ImageOptionalHeader64 {
369
+ return ImageOptionalHeader64{
370
+ Magic: i.Magic,
371
+ MajorLinkerVersion: i.MajorLinkerVersion,
372
+ MinorLinkerVersion: i.MinorLinkerVersion,
373
+ SizeOfCode: i.SizeOfCode,
374
+ SizeOfInitializedData: i.SizeOfInitializedData,
375
+ SizeOfUninitializedData: i.SizeOfUninitializedData,
376
+ AddressOfEntryPoint: i.AddressOfEntryPoint,
377
+ BaseOfCode: i.BaseOfCode,
378
+ ImageBase: uint64(i.ImageBase),
379
+ SectionAlignment: i.SectionAlignment,
380
+ FileAlignment: i.FileAlignment,
381
+ MajorOperatingSystemVersion: i.MajorOperatingSystemVersion,
382
+ MinorOperatingSystemVersion: i.MinorOperatingSystemVersion,
383
+ MajorImageVersion: i.MajorImageVersion,
384
+ MinorImageVersion: i.MinorImageVersion,
385
+ MajorSubsystemVersion: i.MajorSubsystemVersion,
386
+ MinorSubsystemVersion: i.MinorSubsystemVersion,
387
+ Win32VersionValue: i.Win32VersionValue,
388
+ SizeOfImage: i.SizeOfImage,
389
+ SizeOfHeaders: i.SizeOfHeaders,
390
+ CheckSum: i.CheckSum,
391
+ Subsystem: i.Subsystem,
392
+ DllCharacteristics: i.DllCharacteristics,
393
+ SizeOfStackReserve: uint64(i.SizeOfStackReserve),
394
+ SizeOfStackCommit: uint64(i.SizeOfStackCommit),
395
+ SizeOfHeapReserve: uint64(i.SizeOfHeapReserve),
396
+ SizeOfHeapCommit: uint64(i.SizeOfHeapCommit),
397
+ LoaderFlags: i.LoaderFlags,
398
+ NumberOfRvaAndSizes: i.NumberOfRvaAndSizes,
399
+ DataDirectory: i.DataDirectory,
400
+ }
401
+ }
402
+
403
+ // ImageNTHeaders32 contains the PE file headers for 32-bit PE images.
404
+ type ImageNTHeaders32 struct {
405
+ // Signature identifies the PE format; Always "PE\0\0".
406
+ Signature [4]byte
407
+ FileHeader ImageFileHeader
408
+ OptionalHeader ImageOptionalHeader32
409
+ }
410
+
411
+ // ImageNTHeaders64 contains the PE file headers for 64-bit PE images.
412
+ type ImageNTHeaders64 struct {
413
+ // Signature identifies the PE format; Always "PE\0\0".
414
+ Signature [4]byte
415
+ FileHeader ImageFileHeader
416
+ OptionalHeader ImageOptionalHeader64
417
+ }
418
+
419
+ // To64 converts the ImageNTHeaders32 to an ImageNTHeaders64.
420
+ func (i ImageNTHeaders32) To64() ImageNTHeaders64 {
421
+ return ImageNTHeaders64{
422
+ Signature: i.Signature,
423
+ FileHeader: i.FileHeader,
424
+ OptionalHeader: i.OptionalHeader.To64(),
425
+ }
426
+ }
427
+
428
+ // ImageDataDirectory holds a record for the given data directory. Each data
429
+ // directory contains information about another section, such as the import
430
+ // table. The index of the directory entry determines which section it
431
+ // pertains to.
432
+ type ImageDataDirectory struct {
433
+ VirtualAddress uint32
434
+ Size uint32
435
+ }
436
+
437
+ // ImageSectionHeader is the header for a section. Windows Loader uses these
438
+ // entries to configure the memory mapping of the executable. A series of
439
+ // these structures immediately follow the headers.
440
+ type ImageSectionHeader struct {
441
+ Name [SectionNameLength]byte
442
+ PhysicalAddressOrVirtualSize uint32
443
+ VirtualAddress uint32
444
+ SizeOfRawData uint32
445
+ PointerToRawData uint32
446
+ PointerToRelocations uint32
447
+ PointerToLinenumbers uint32
448
+ NumberOfRelocations uint16
449
+ NumberOfLinenumbers uint16
450
+ Characteristics uint32
451
+ }
452
+
453
+ // ImageBaseRelocation holds the header for a single page of base relocation
454
+ // data. The .reloc section of the binary contains a series of blocks of
455
+ // base relocation data, each starting with this header and followed by n
456
+ // 16-bit values that each represent a single relocation. The 4 most
457
+ // significant bits specify the type of relocation, while the 12 least
458
+ // significant bits contain the lower bits of the address (which is combined
459
+ // with the virtual address of the page.) The SizeOfBlock value specifies the
460
+ // size of an entire block, in bytes, including its header.
461
+ type ImageBaseRelocation struct {
462
+ VirtualAddress uint32
463
+ SizeOfBlock uint32
464
+ }
465
+
466
+ // The ImageImportDescriptor contains information about an imported module.
467
+ type ImageImportDescriptor struct {
468
+ OriginalFirstThunk uint32
469
+ TimeDateStamp uint32
470
+ ForwarderChain uint32
471
+ Name uint32
472
+ FirstThunk uint32
473
+ }
474
+
475
+ // The ImageExportDirectory contains information about the module's exports.
476
+ type ImageExportDirectory struct {
477
+ Characteristics uint32
478
+ TimeDateStamp uint32
479
+ MajorVersion uint16
480
+ MinorVersion uint16
481
+ Name uint32
482
+ Base uint32
483
+ NumberOfFunctions uint32
484
+ NumberOfNames uint32
485
+ AddressOfFunctions uint32
486
+ AddressOfNames uint32
487
+ AddressOfNameOrdinals uint32
488
+ }
489
+
490
+ // ImageTLSDirectory32 contains information about the module's thread local
491
+ // storage callbacks (in PE32)
492
+ type ImageTLSDirectory32 struct {
493
+ StartAddressOfRawData uint32
494
+ EndAddressOfRawData uint32
495
+ AddressOfIndex uint32
496
+ AddressOfCallBacks uint32
497
+ SizeOfZeroFill uint32
498
+ Characteristics uint32
499
+ }
500
+
501
+ // ImageTLSDirectory64 contains information about the module's thread local
502
+ // storage callbacks (in PE64)
503
+ type ImageTLSDirectory64 struct {
504
+ StartAddressOfRawData uint64
505
+ EndAddressOfRawData uint64
506
+ AddressOfIndex uint64
507
+ AddressOfCallBacks uint64
508
+ SizeOfZeroFill uint32
509
+ Characteristics uint32
510
+ }
511
+
512
+ // To64 converts the ImageTLSDirectory32 to an ImageTLSDirectory64.
513
+ func (i ImageTLSDirectory32) To64() ImageTLSDirectory64 {
514
+ return ImageTLSDirectory64{
515
+ StartAddressOfRawData: uint64(i.StartAddressOfRawData),
516
+ EndAddressOfRawData: uint64(i.EndAddressOfRawData),
517
+ AddressOfIndex: uint64(i.AddressOfIndex),
518
+ AddressOfCallBacks: uint64(i.AddressOfCallBacks),
519
+ SizeOfZeroFill: i.SizeOfZeroFill,
520
+ Characteristics: i.Characteristics,
521
+ }
522
+ }
@@ -0,0 +1,106 @@
1
+ package pe
2
+
3
+ import (
4
+ "encoding/binary"
5
+ "fmt"
6
+ "io"
7
+
8
+ "github.com/jchv/go-winloader/internal/loader"
9
+ )
10
+
11
+ // LinkModule links a PE module in-memory.
12
+ func LinkModule(m *Module, mem io.ReadWriteSeeker, ldr loader.Loader) error {
13
+ dir := m.Header.OptionalHeader.DataDirectory[ImageDirectoryEntryImport]
14
+ if dir.Size == 0 {
15
+ return nil
16
+ }
17
+
18
+ // Determine pointer size based on whether we're PE32 or PE64.
19
+ psize := 4
20
+ if m.IsPE64 {
21
+ psize = 8
22
+ }
23
+
24
+ // Load import descriptors
25
+ descs := []ImageImportDescriptor{}
26
+ mem.Seek(int64(dir.VirtualAddress), io.SeekStart)
27
+ for {
28
+ desc := ImageImportDescriptor{}
29
+ binary.Read(mem, binary.LittleEndian, &desc)
30
+
31
+ if desc.Name == 0 {
32
+ break
33
+ }
34
+
35
+ descs = append(descs, desc)
36
+ }
37
+
38
+ // Load modules.
39
+ for _, desc := range descs {
40
+ thunk := int64(desc.OriginalFirstThunk)
41
+ iat := int64(desc.FirstThunk)
42
+ if thunk == 0 {
43
+ thunk = iat
44
+ }
45
+
46
+ // Read module name
47
+ mem.Seek(int64(desc.Name), io.SeekStart)
48
+
49
+ // Load library
50
+ libname := readsz(mem)
51
+ lib, err := ldr.Load(libname)
52
+ if err != nil {
53
+ return err
54
+ }
55
+
56
+ // Read thunk addrs
57
+ b := [8]byte{}
58
+ thunks := []uint64{}
59
+ mem.Seek(thunk, io.SeekStart)
60
+ for {
61
+ mem.Read(b[:psize])
62
+ thunk := binary.LittleEndian.Uint64(b[:])
63
+ if thunk == 0 {
64
+ break
65
+ }
66
+ thunks = append(thunks, thunk)
67
+ }
68
+
69
+ // Resolve thunks
70
+ resolved := []uint64{}
71
+ for _, thunk := range thunks {
72
+ thunkord := int64(-1)
73
+ if (m.IsPE64 && thunk&0x8000000000000000 != 0) || (!m.IsPE64 && thunk&0x80000000 != 0) {
74
+ thunkord = int64(thunk & 0xFFFF)
75
+ }
76
+ if thunkord != -1 {
77
+ // Import by ordinal
78
+ if proc := lib.Ordinal(uint64(thunkord)); proc != nil {
79
+ resolved = append(resolved, proc.Addr())
80
+ } else {
81
+ return fmt.Errorf("could not resolve ordinal %d in module %q", thunkord, libname)
82
+ }
83
+ } else {
84
+ // Read name
85
+ mem.Seek(int64(thunk+2), io.SeekStart)
86
+ fnname := readsz(mem)
87
+
88
+ // Import by name
89
+ if proc := lib.Proc(fnname); proc != nil {
90
+ resolved = append(resolved, proc.Addr())
91
+ } else {
92
+ return fmt.Errorf("could not resolve symbol %q in module %q", fnname, libname)
93
+ }
94
+ }
95
+ }
96
+
97
+ // Write resolved IAT
98
+ mem.Seek(iat, io.SeekStart)
99
+ for _, fn := range resolved {
100
+ binary.LittleEndian.PutUint64(b[:], uint64(fn))
101
+ mem.Write(b[:psize])
102
+ }
103
+ }
104
+
105
+ return nil
106
+ }