typenative 0.4.1 → 0.6.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.
- package/.github/workflows/ci.yml +2 -2
- package/.github/workflows/npm-publish-github-packages.yml +2 -2
- package/CHANGELOG.md +442 -1
- package/CONTRIBUTION.md +2 -1
- package/README.md +73 -25
- package/bin/cli.js +2 -0
- package/bin/index.js +75 -5
- package/bin/main.js +2 -0
- package/bin/node/assert.js +2 -2
- package/bin/node/async_hooks.js +1 -1
- package/bin/node/buffer.js +16 -5
- package/bin/node/child_process.js +7 -7
- package/bin/node/cluster.js +3 -3
- package/bin/node/core.js +41 -0
- package/bin/node/crypto_keys.js +21 -21
- package/bin/node/dgram.js +1 -1
- package/bin/node/diagnostics_channel.js +3 -3
- package/bin/node/dns.js +2 -2
- package/bin/node/events.js +18 -13
- package/bin/node/fetch.js +2 -2
- package/bin/node/fs.js +3 -3
- package/bin/node/fs_watch.js +5 -5
- package/bin/node/http.js +3 -3
- package/bin/node/http2.js +3 -3
- package/bin/node/net.js +8 -8
- package/bin/node/path.js +17 -3
- package/bin/node/process.js +47 -11
- package/bin/node/punycode.js +2 -2
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +4 -1
- package/bin/node/sqlite.js +6 -6
- package/bin/node/stream.js +81 -15
- package/bin/node/stream_web.js +10 -10
- package/bin/node/system_extra.js +3 -3
- package/bin/node/test.js +674 -366
- package/bin/node/test_coverage.js +187 -0
- package/bin/node/test_report.js +1392 -0
- package/bin/node/test_run.js +575 -0
- package/bin/node/timers.js +8 -6
- package/bin/node/tls.js +5 -5
- package/bin/node/url.js +1 -1
- package/bin/node/util.js +31 -7
- package/bin/node/util_extra.js +7 -7
- package/bin/node/webview.js +654 -0
- package/bin/node/worker_threads.js +1 -1
- package/bin/node/zlib.js +3 -3
- package/bin/parse-node.js +6 -2
- package/bin/regex_vm.js +2057 -0
- package/bin/transpiler.js +32872 -11265
- package/bin/typecheck-node.js +2 -1
- package/bin/version.js +1 -1
- package/conformance.md +515 -345
- package/examples/README.md +71 -11
- package/examples/angular-templates/README.md +139 -0
- package/examples/angular-templates/bench.mjs +173 -0
- package/examples/angular-templates/dom-type-globals.d.ts +21 -0
- package/examples/angular-templates/dom-type-stubs.ts +21 -0
- package/examples/angular-templates/main.ts +135 -0
- package/examples/angular-templates/parity.mjs +157 -0
- package/examples/angular-templates/run-node.mjs +69 -0
- package/examples/angular-templates/vendor/angular/compiler/LICENSE +21 -0
- package/examples/desktop-notes/README.md +106 -0
- package/examples/desktop-notes/bench.mjs +125 -0
- package/examples/desktop-notes/main.ts +131 -0
- package/examples/desktop-notes/package-lock.json +726 -0
- package/examples/desktop-notes/package.json +9 -0
- package/examples/desktop-notes/ui.ts +176 -0
- package/examples/dupe-finder/README.md +78 -0
- package/examples/dupe-finder/bench.mjs +120 -0
- package/examples/dupe-finder/main.ts +87 -0
- package/examples/fibonacci/README.md +7 -7
- package/examples/hello-world/README.md +6 -6
- package/examples/json-server/README.md +19 -18
- package/examples/mandelbrot-png/README.md +63 -0
- package/examples/mandelbrot-png/bench.mjs +95 -0
- package/examples/mandelbrot-png/main.ts +98 -0
- package/examples/mandelbrot-png/preview.png +0 -0
- package/examples/marked-cli/README.md +160 -0
- package/examples/marked-cli/bench.mjs +178 -0
- package/examples/marked-cli/main.ts +292 -0
- package/examples/marked-cli/parity.mjs +185 -0
- package/examples/marked-cli/vendor/marked/LICENSE +44 -0
- package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
- package/examples/openai-chat/README.md +148 -0
- package/examples/openai-chat/bench.mjs +175 -0
- package/examples/openai-chat/main.ts +488 -0
- package/examples/react-compiler/README.md +170 -0
- package/examples/react-compiler/bench.mjs +181 -0
- package/examples/react-compiler/main.ts +158 -0
- package/examples/react-compiler/parity.mjs +362 -0
- package/examples/react-compiler/run-node.mjs +88 -0
- package/examples/react-compiler/vendor/babel/LICENSE +22 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-lower.json +15 -0
- package/examples/react-compiler/vendor/babel/helper-globals/data/builtin-upper.json +51 -0
- package/examples/react-compiler/vendor/charcodes/package.json +25 -0
- package/examples/react-compiler/vendor/invariant/LICENSE +21 -0
- package/examples/react-compiler/vendor/invariant/invariant.cjs +51 -0
- package/examples/react-compiler/vendor/invariant/invariant.d.cts +11 -0
- package/examples/react-compiler/vendor/invariant/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/jridgewell/gen-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/resolve-uri/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/sourcemap-codec/LICENSE +19 -0
- package/examples/react-compiler/vendor/jridgewell/trace-mapping/LICENSE +19 -0
- package/examples/react-compiler/vendor/js-tokens/LICENSE +21 -0
- package/examples/react-compiler/vendor/js-tokens/index.cjs +23 -0
- package/examples/react-compiler/vendor/js-tokens/index.d.cts +13 -0
- package/examples/react-compiler/vendor/jsesc/LICENSE-MIT.txt +20 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.cjs +337 -0
- package/examples/react-compiler/vendor/jsesc/jsesc.d.cts +384 -0
- package/examples/react-compiler/vendor/jsesc/types/LICENSE +21 -0
- package/examples/react-compiler/vendor/picocolors/LICENSE +15 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.cjs +75 -0
- package/examples/react-compiler/vendor/picocolors/picocolors.d.cts +5 -0
- package/examples/react-compiler/vendor/picocolors/types.d.ts +51 -0
- package/examples/react-compiler/vendor/react-compiler/LICENSE +21 -0
- package/examples/react-compiler/vendor/stubs/babel-core.ts +31 -0
- package/examples/react-compiler/vendor/stubs/debug.ts +18 -0
- package/examples/react-compiler/vendor/stubs/js-tokens.d.ts +13 -0
- package/examples/react-compiler/vendor/stubs/pretty-format.ts +18 -0
- package/examples/react-compiler/vendor/zod/LICENSE +21 -0
- package/examples/react-compiler/vendor/zod-validation-error/LICENSE +9 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/NonEmptyArray.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinPath.ts +49 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/joinValues.ts +48 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/stringify.ts +50 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/utils/titleCase.ts +6 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/MessageBuilder.ts +126 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/ValidationError.ts +39 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/custom.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/errorMap.ts +78 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/index.ts +2 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidElement.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidKey.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidStringFormat.ts +131 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidType.ts +54 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidUnion.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/invalidValue.ts +40 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/notMultipleOf.ts +12 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooBig.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/tooSmall.ts +76 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/types.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/errorMap/unrecognizedKeys.ts +27 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromError.ts +13 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodError.ts +61 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/fromZodIssue.ts +38 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/index.ts +20 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationError.ts +5 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isValidationErrorLike.ts +8 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/isZodErrorLike.ts +11 -0
- package/examples/react-compiler/vendor/zod-validation-error/lib/v4/toValidationError.ts +20 -0
- package/examples/react-compiler/vendor.mjs +688 -0
- package/examples/todo-cli/README.md +69 -0
- package/examples/todo-cli/bench.mjs +89 -0
- package/examples/todo-cli/main.ts +92 -0
- package/examples/yaml-cli/README.md +146 -0
- package/examples/yaml-cli/bench.mjs +265 -0
- package/examples/yaml-cli/main.ts +29 -0
- package/examples/yaml-cli/parity.mjs +156 -0
- package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
- package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
- package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
- package/govendor/github.com/jchv/go-webview2/README.md +21 -0
- package/govendor/github.com/jchv/go-webview2/common.go +84 -0
- package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
- package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
- package/govendor/github.com/jchv/go-winloader/README.md +124 -0
- package/govendor/github.com/jchv/go-winloader/common.go +11 -0
- package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
- package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +8 -5
- package/performance/README.md +195 -0
- package/performance/any-dynamic.ts +21 -0
- package/performance/any-record-view.ts +20 -0
- package/performance/arr-hof.ts +21 -0
- package/performance/arr-push-index.ts +15 -0
- package/performance/arr-sort.ts +21 -0
- package/performance/async-await.ts +24 -0
- package/performance/class-dispatch.ts +52 -0
- package/performance/events-emit.ts +27 -0
- package/performance/exc-throw-rare.ts +25 -0
- package/performance/exc-try-catch.ts +26 -0
- package/performance/fn-closures.ts +29 -0
- package/performance/fn-recursion-tree.ts +31 -0
- package/performance/gen-for-of.ts +46 -0
- package/performance/json-roundtrip.ts +27 -0
- package/performance/map-delete.ts +24 -0
- package/performance/map-string-keys.ts +22 -0
- package/performance/num-arith-loop.ts +14 -0
- package/performance/num-bitwise.ts +21 -0
- package/performance/num-mod-int.ts +16 -0
- package/performance/num-to-string.ts +16 -0
- package/performance/obj-alloc.ts +32 -0
- package/performance/regex-exec.ts +29 -0
- package/performance/regex-site.ts +28 -0
- package/performance/set-ops.ts +20 -0
- package/performance/str-char-scan.ts +35 -0
- package/performance/str-concat-plus.ts +18 -0
- package/performance/str-concat.ts +24 -0
- package/performance/str-short-scan.ts +26 -0
- package/performance/str-split-join.ts +23 -0
- package/performance/str-template.ts +16 -0
- package/performance/union-switch.ts +57 -0
- package/tsparser/check.go +2 -1
- package/tsparser/main.go +5 -0
- package/types/typenative-npm.d.ts +193 -4
- package/types/typenative.d.ts +138 -41
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
package pe
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/binary"
|
|
5
|
+
"errors"
|
|
6
|
+
"io"
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
var (
|
|
10
|
+
// ErrBadMZSignature is returned when the MZ signature is invalid.
|
|
11
|
+
ErrBadMZSignature = errors.New("mz: bad signature")
|
|
12
|
+
|
|
13
|
+
// ErrBadPESignature is returned when the PE signature is invalid.
|
|
14
|
+
ErrBadPESignature = errors.New("pe: bad signature")
|
|
15
|
+
|
|
16
|
+
// ErrUnknownOptionalHeaderMagic is returned when the optional header
|
|
17
|
+
// magic field has an unknown value.
|
|
18
|
+
ErrUnknownOptionalHeaderMagic = errors.New("pe: unknown optional header magic")
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
// Module contains a parsed and loaded PE file.
|
|
22
|
+
type Module struct {
|
|
23
|
+
IsPE64 bool
|
|
24
|
+
DOSHeader ImageDOSHeader
|
|
25
|
+
Header ImageNTHeaders64
|
|
26
|
+
Sections []ImageSectionHeader
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// LoadModule loads a PE module into memory.
|
|
30
|
+
func LoadModule(r io.ReadSeeker) (*Module, error) {
|
|
31
|
+
m := &Module{}
|
|
32
|
+
|
|
33
|
+
r.Seek(0, io.SeekStart)
|
|
34
|
+
dos := ImageDOSHeader{}
|
|
35
|
+
if err := binary.Read(r, binary.LittleEndian, &dos); err != nil {
|
|
36
|
+
return nil, err
|
|
37
|
+
}
|
|
38
|
+
if dos.Signature != MZSignature {
|
|
39
|
+
return nil, ErrBadMZSignature
|
|
40
|
+
}
|
|
41
|
+
m.DOSHeader = dos
|
|
42
|
+
|
|
43
|
+
r.Seek(int64(dos.NewHeaderAddr), io.SeekStart)
|
|
44
|
+
pesig := [4]byte{}
|
|
45
|
+
if err := binary.Read(r, binary.LittleEndian, &pesig); err != nil {
|
|
46
|
+
return nil, err
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if pesig != PESignature {
|
|
50
|
+
return nil, ErrBadPESignature
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
optmagic := uint16(0)
|
|
54
|
+
r.Seek(int64(dos.NewHeaderAddr)+OffsetOfOptionalHeaderFromNTHeader, io.SeekStart)
|
|
55
|
+
if err := binary.Read(r, binary.LittleEndian, &optmagic); err != nil {
|
|
56
|
+
return nil, err
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
r.Seek(int64(dos.NewHeaderAddr), io.SeekStart)
|
|
60
|
+
switch optmagic {
|
|
61
|
+
case ImageNTOptionalHeader32Magic:
|
|
62
|
+
m.IsPE64 = false
|
|
63
|
+
nt := ImageNTHeaders32{}
|
|
64
|
+
if err := binary.Read(r, binary.LittleEndian, &nt); err != nil {
|
|
65
|
+
return nil, err
|
|
66
|
+
}
|
|
67
|
+
m.Header = nt.To64()
|
|
68
|
+
case ImageNTOptionalHeader64Magic:
|
|
69
|
+
m.IsPE64 = true
|
|
70
|
+
nt := ImageNTHeaders64{}
|
|
71
|
+
if err := binary.Read(r, binary.LittleEndian, &nt); err != nil {
|
|
72
|
+
return nil, err
|
|
73
|
+
}
|
|
74
|
+
m.Header = nt
|
|
75
|
+
default:
|
|
76
|
+
return nil, ErrUnknownOptionalHeaderMagic
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Seek past end of optional headers.
|
|
80
|
+
r.Seek(int64(dos.NewHeaderAddr)+OffsetOfOptionalHeaderFromNTHeader+int64(m.Header.FileHeader.SizeOfOptionalHeader), io.SeekStart)
|
|
81
|
+
|
|
82
|
+
for i := uint16(0); i < m.Header.FileHeader.NumberOfSections; i++ {
|
|
83
|
+
section := ImageSectionHeader{}
|
|
84
|
+
if err := binary.Read(r, binary.LittleEndian, §ion); err != nil {
|
|
85
|
+
return nil, err
|
|
86
|
+
}
|
|
87
|
+
m.Sections = append(m.Sections, section)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return m, nil
|
|
91
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
package pe
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"encoding/binary"
|
|
5
|
+
"errors"
|
|
6
|
+
"fmt"
|
|
7
|
+
"io"
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
// BaseRelocation is a parsed PE base relocation.
|
|
11
|
+
type BaseRelocation struct {
|
|
12
|
+
// Relative virtual address of offset (that is, 0 == first byte of PE.)
|
|
13
|
+
Offset uint64
|
|
14
|
+
|
|
15
|
+
// Type of base relocation using the ImageRelBased* constants.
|
|
16
|
+
Type int
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// LoadBaseRelocs loads relocations from memory.
|
|
20
|
+
func LoadBaseRelocs(m *Module, mem io.ReadSeeker) []BaseRelocation {
|
|
21
|
+
relocs := []BaseRelocation{}
|
|
22
|
+
dir := m.Header.OptionalHeader.DataDirectory[ImageDirectoryEntryBaseReloc]
|
|
23
|
+
if dir.Size == 0 {
|
|
24
|
+
return relocs
|
|
25
|
+
}
|
|
26
|
+
n := uint32(0)
|
|
27
|
+
mem.Seek(int64(dir.VirtualAddress), io.SeekStart)
|
|
28
|
+
for n < dir.Size {
|
|
29
|
+
hdr := ImageBaseRelocation{}
|
|
30
|
+
binary.Read(mem, binary.LittleEndian, &hdr)
|
|
31
|
+
data := make([]uint16, hdr.SizeOfBlock/2-4)
|
|
32
|
+
binary.Read(mem, binary.LittleEndian, &data)
|
|
33
|
+
for _, i := range data {
|
|
34
|
+
relocs = append(relocs, BaseRelocation{
|
|
35
|
+
Offset: uint64(hdr.VirtualAddress) + uint64(i&0xFFF),
|
|
36
|
+
Type: int(i >> 12),
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
n += hdr.SizeOfBlock
|
|
40
|
+
}
|
|
41
|
+
return relocs
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Relocate performs a series of relocations on m, where address is the load
|
|
45
|
+
// address and original is the original address. The ReadWriteSeeker is
|
|
46
|
+
// assumed to have the PE image at offset 0.
|
|
47
|
+
func Relocate(machine int, rels []BaseRelocation, address uint64, original uint64, m io.ReadWriteSeeker, o binary.ByteOrder) error {
|
|
48
|
+
b := [8]byte{}
|
|
49
|
+
delta := address - original
|
|
50
|
+
|
|
51
|
+
if delta == 0 {
|
|
52
|
+
return nil
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
read := func(off uint64, cb int) error {
|
|
56
|
+
m.Seek(int64(off), io.SeekStart)
|
|
57
|
+
err := readfully(m, b[0:cb])
|
|
58
|
+
if err != nil {
|
|
59
|
+
return err
|
|
60
|
+
}
|
|
61
|
+
m.Seek(int64(-cb), io.SeekCurrent)
|
|
62
|
+
return nil
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for _, rel := range rels {
|
|
66
|
+
switch rel.Type {
|
|
67
|
+
case ImageRelBasedAbsolute:
|
|
68
|
+
break
|
|
69
|
+
|
|
70
|
+
case ImageRelBasedHigh:
|
|
71
|
+
if err := read(rel.Offset, 2); err != nil {
|
|
72
|
+
return err
|
|
73
|
+
}
|
|
74
|
+
o.PutUint16(b[0:2], uint16(uint64(o.Uint16(b[:2]))+(delta>>16)))
|
|
75
|
+
if _, err := m.Write(b[0:2]); err != nil {
|
|
76
|
+
return err
|
|
77
|
+
}
|
|
78
|
+
break
|
|
79
|
+
|
|
80
|
+
case ImageRelBasedLow:
|
|
81
|
+
if err := read(rel.Offset, 2); err != nil {
|
|
82
|
+
return err
|
|
83
|
+
}
|
|
84
|
+
o.PutUint16(b[0:2], uint16(uint64(o.Uint16(b[:2]))+(delta>>0)))
|
|
85
|
+
if _, err := m.Write(b[0:2]); err != nil {
|
|
86
|
+
return err
|
|
87
|
+
}
|
|
88
|
+
break
|
|
89
|
+
|
|
90
|
+
case ImageRelBasedHighLow:
|
|
91
|
+
if err := read(rel.Offset, 4); err != nil {
|
|
92
|
+
return err
|
|
93
|
+
}
|
|
94
|
+
o.PutUint32(b[0:4], uint32(uint64(o.Uint32(b[:4]))+delta))
|
|
95
|
+
if _, err := m.Write(b[0:4]); err != nil {
|
|
96
|
+
return err
|
|
97
|
+
}
|
|
98
|
+
break
|
|
99
|
+
|
|
100
|
+
case ImageRelBasedDir64:
|
|
101
|
+
if err := read(rel.Offset, 8); err != nil {
|
|
102
|
+
return err
|
|
103
|
+
}
|
|
104
|
+
o.PutUint64(b[0:8], uint64(o.Uint64(b[:8]))+delta)
|
|
105
|
+
if _, err := m.Write(b[0:8]); err != nil {
|
|
106
|
+
return err
|
|
107
|
+
}
|
|
108
|
+
break
|
|
109
|
+
|
|
110
|
+
// Could use some help for ensuring we have proper support for
|
|
111
|
+
// machine-specific relocations. If you are interested in this
|
|
112
|
+
// use case for some reason, feel free to send PRs. -- john
|
|
113
|
+
case ImageRelBasedMachineSpecific5:
|
|
114
|
+
switch machine {
|
|
115
|
+
// MIPS: JMP reloc
|
|
116
|
+
case ImageFileMachineR3000, ImageFileMachineR3000BE,
|
|
117
|
+
ImageFileMachineR4000, ImageFileMachineR10000,
|
|
118
|
+
ImageFileMachineWCEMIPSv2, ImageFileMachineMIPS16,
|
|
119
|
+
ImageFileMachineMIPSFPU, ImageFileMachineMIPSFPU16:
|
|
120
|
+
return errors.New("MIPS JMP reloc not implemented")
|
|
121
|
+
// ARM: MOV32 reloc
|
|
122
|
+
case ImageFileMachineARM, ImageFileMachineTHUMB,
|
|
123
|
+
ImageFileMachineARMNT:
|
|
124
|
+
return errors.New("ARM MOV32 reloc not implemented")
|
|
125
|
+
// RISC-V: HI20 reloc
|
|
126
|
+
case ImageFileMachineRISCV32, ImageFileMachineRISCV64,
|
|
127
|
+
ImageFileMachineRISCV128:
|
|
128
|
+
return errors.New("RISC-V HI20 reloc not implemented")
|
|
129
|
+
default:
|
|
130
|
+
return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
case ImageRelBasedMachineSpecific7:
|
|
134
|
+
switch machine {
|
|
135
|
+
// THUMB: MOV32 reloc
|
|
136
|
+
case ImageFileMachineARM, ImageFileMachineTHUMB,
|
|
137
|
+
ImageFileMachineARMNT:
|
|
138
|
+
return errors.New("THUMB MOV32 reloc not implemented")
|
|
139
|
+
// RISC-V: LOW12I reloc
|
|
140
|
+
case ImageFileMachineRISCV32, ImageFileMachineRISCV64,
|
|
141
|
+
ImageFileMachineRISCV128:
|
|
142
|
+
return errors.New("RISC-V LOW12I reloc not implemented")
|
|
143
|
+
default:
|
|
144
|
+
return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
case ImageRelBasedMachineSpecific8:
|
|
148
|
+
switch machine {
|
|
149
|
+
// RISC-V: LOW12S reloc
|
|
150
|
+
case ImageFileMachineRISCV32, ImageFileMachineRISCV64,
|
|
151
|
+
ImageFileMachineRISCV128:
|
|
152
|
+
return errors.New("RISC-V LOW12S reloc not implemented")
|
|
153
|
+
default:
|
|
154
|
+
return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
case ImageRelBasedMachineSpecific9:
|
|
158
|
+
switch machine {
|
|
159
|
+
// MIPS: JMP16 reloc
|
|
160
|
+
case ImageFileMachineR3000, ImageFileMachineR3000BE,
|
|
161
|
+
ImageFileMachineR4000, ImageFileMachineR10000,
|
|
162
|
+
ImageFileMachineWCEMIPSv2, ImageFileMachineMIPS16,
|
|
163
|
+
ImageFileMachineMIPSFPU, ImageFileMachineMIPSFPU16:
|
|
164
|
+
return errors.New("MIPS JMP16 reloc not implemented")
|
|
165
|
+
// Itanium: Imm64 reloc
|
|
166
|
+
case ImageFileMachineIA64:
|
|
167
|
+
return errors.New("Itanium Imm64 reloc not implemented")
|
|
168
|
+
default:
|
|
169
|
+
return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
|
|
170
|
+
}
|
|
171
|
+
default:
|
|
172
|
+
return fmt.Errorf("unknown relocation type %d", rel.Type)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return nil
|
|
177
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
package pe
|
|
2
|
+
|
|
3
|
+
import "io"
|
|
4
|
+
|
|
5
|
+
func readfully(r io.Reader, p []byte) error {
|
|
6
|
+
n, err := r.Read(p)
|
|
7
|
+
if n < 0 || n > len(p) {
|
|
8
|
+
panic("invalid read length")
|
|
9
|
+
}
|
|
10
|
+
if err != nil && err != io.EOF {
|
|
11
|
+
return err
|
|
12
|
+
}
|
|
13
|
+
for n < len(p) {
|
|
14
|
+
m, err := r.Read(p[n:])
|
|
15
|
+
if m < 0 || m > len(p[n:]) {
|
|
16
|
+
panic("invalid read length")
|
|
17
|
+
}
|
|
18
|
+
n += m
|
|
19
|
+
if n < len(p) && err != nil {
|
|
20
|
+
return err
|
|
21
|
+
}
|
|
22
|
+
if n >= len(p) && err != nil && err != io.EOF {
|
|
23
|
+
return err
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return nil
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
func readsz(r io.Reader) string {
|
|
30
|
+
name := []byte{}
|
|
31
|
+
for {
|
|
32
|
+
b := [1]byte{}
|
|
33
|
+
r.Read(b[:])
|
|
34
|
+
if b[0] == 0 {
|
|
35
|
+
break
|
|
36
|
+
}
|
|
37
|
+
name = append(name, b[0])
|
|
38
|
+
}
|
|
39
|
+
return string(name)
|
|
40
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
package vmem
|
|
2
|
+
|
|
3
|
+
const (
|
|
4
|
+
memDecommit = 0x4000
|
|
5
|
+
memRelease = 0x8000
|
|
6
|
+
)
|
|
7
|
+
|
|
8
|
+
type systemInfo struct {
|
|
9
|
+
wProcessorArchitecture uint16
|
|
10
|
+
wReserved uint16
|
|
11
|
+
dwPageSize uint32
|
|
12
|
+
lpMinimumApplicationAddress uintptr
|
|
13
|
+
lpMaximumApplicationAddress uintptr
|
|
14
|
+
dwActiveProcessorMask uintptr
|
|
15
|
+
dwNumberOfProcessors uint32
|
|
16
|
+
dwProcessorType uint32
|
|
17
|
+
dwAllocationGranularity uint32
|
|
18
|
+
wProcessorLevel uint16
|
|
19
|
+
wProcessorRevision uint16
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Enumeration of allocation type values.
|
|
23
|
+
const (
|
|
24
|
+
MemCommit = 0x00001000
|
|
25
|
+
MemReserve = 0x00002000
|
|
26
|
+
MemReset = 0x00080000
|
|
27
|
+
MemResetUndo = 0x10000000
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
// Enumeration of protection levels.
|
|
31
|
+
const (
|
|
32
|
+
PageNoAccess = 0x01
|
|
33
|
+
PageReadOnly = 0x02
|
|
34
|
+
PageReadWrite = 0x04
|
|
35
|
+
PageWriteCopy = 0x08
|
|
36
|
+
PageExecute = 0x10
|
|
37
|
+
PageExecuteRead = 0x20
|
|
38
|
+
PageExecuteReadWrite = 0x40
|
|
39
|
+
PageExecuteWriteCopy = 0x80
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
// RoundDown rounds an address up to a given multiple of size. Size must be a
|
|
43
|
+
// power of two.
|
|
44
|
+
func RoundDown(addr uint64, size uint64) uint64 {
|
|
45
|
+
if size&(size-1) != 0 {
|
|
46
|
+
panic("alignment size is not a power of two")
|
|
47
|
+
}
|
|
48
|
+
return addr &^ (size - 1)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// RoundUp rounds an address up to a given multiple of size. Size must be a
|
|
52
|
+
// power of two.
|
|
53
|
+
func RoundUp(addr uint64, size uint64) uint64 {
|
|
54
|
+
return RoundDown(addr+size-1, size)
|
|
55
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
//go:build !windows
|
|
2
|
+
// +build !windows
|
|
3
|
+
|
|
4
|
+
package vmem
|
|
5
|
+
|
|
6
|
+
// Memory represents a raw block of memory.
|
|
7
|
+
type Memory struct {
|
|
8
|
+
data []byte
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// GetPageSize returns the size of a memory page.
|
|
12
|
+
func GetPageSize() uint64 {
|
|
13
|
+
panic("not implemented")
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Alloc allocates memory at addr of size with allocType and protect.
|
|
17
|
+
// It returns nil if it fails.
|
|
18
|
+
func Alloc(addr, size uint64, allocType, protect int) *Memory {
|
|
19
|
+
panic("not implemented")
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Get returns a range of existing memory. If the range is not a block of
|
|
23
|
+
// allocated memory, the returned memory will pagefault when accessed.
|
|
24
|
+
func Get(addr, size uint64) *Memory {
|
|
25
|
+
panic("not implemented")
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Free frees the block of memory.
|
|
29
|
+
func (m *Memory) Free() {
|
|
30
|
+
panic("not implemented")
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Addr returns the actual address of the memory.
|
|
34
|
+
func (m *Memory) Addr() uint64 {
|
|
35
|
+
panic("not implemented")
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Read implements the io.Reader interface.
|
|
39
|
+
func (m *Memory) Read(b []byte) (n int, err error) {
|
|
40
|
+
panic("not implemented")
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ReadAt implements the io.ReaderAt interface.
|
|
44
|
+
func (m *Memory) ReadAt(b []byte, off int64) (n int, err error) {
|
|
45
|
+
panic("not implemented")
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Write implements the io.Writer interface.
|
|
49
|
+
func (m *Memory) Write(b []byte) (n int, err error) {
|
|
50
|
+
panic("not implemented")
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// WriteAt implements the io.WriterAt interface.
|
|
54
|
+
func (m *Memory) WriteAt(b []byte, off int64) (n int, err error) {
|
|
55
|
+
panic("not implemented")
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Seek implements the io.Seeker interface.
|
|
59
|
+
func (m *Memory) Seek(offset int64, whence int) (int64, error) {
|
|
60
|
+
panic("not implemented")
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Protect changes the memory protection for a range of memory.
|
|
64
|
+
func (m *Memory) Protect(addr, size uint64, protect int) error {
|
|
65
|
+
panic("not implemented")
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Clear sets all bytes in the memory block to zero.
|
|
69
|
+
func (m *Memory) Clear() {
|
|
70
|
+
panic("not implemented")
|
|
71
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
//go:build windows
|
|
2
|
+
// +build windows
|
|
3
|
+
|
|
4
|
+
package vmem
|
|
5
|
+
|
|
6
|
+
import (
|
|
7
|
+
"errors"
|
|
8
|
+
"io"
|
|
9
|
+
"reflect"
|
|
10
|
+
"sync"
|
|
11
|
+
"unsafe"
|
|
12
|
+
|
|
13
|
+
"golang.org/x/sys/windows"
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
var (
|
|
17
|
+
kernel32 = windows.NewLazySystemDLL("kernel32")
|
|
18
|
+
kernel32VirtualAlloc = kernel32.NewProc("VirtualAlloc")
|
|
19
|
+
kernel32VirtualFree = kernel32.NewProc("VirtualFree")
|
|
20
|
+
kernel32VirtualProtect = kernel32.NewProc("VirtualProtect")
|
|
21
|
+
kernel32GetNativeSystemInfo = kernel32.NewProc("GetNativeSystemInfo")
|
|
22
|
+
kernel32FlushInstructionCache = kernel32.NewProc("FlushInstructionCache")
|
|
23
|
+
kernel32GetCurrentProcess = kernel32.NewProc("GetCurrentProcess")
|
|
24
|
+
|
|
25
|
+
pageSize uint64
|
|
26
|
+
pageSizeOnce sync.Once
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
// getCurrentProcess returns the current process handle.
|
|
30
|
+
func getCurrentProcess() uintptr {
|
|
31
|
+
r, _, _ := kernel32GetCurrentProcess.Call()
|
|
32
|
+
return uintptr(r)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// GetPageSize returns the size of a memory page.
|
|
36
|
+
func GetPageSize() uint64 {
|
|
37
|
+
pageSizeOnce.Do(func() {
|
|
38
|
+
if kernel32GetNativeSystemInfo.Find() != nil {
|
|
39
|
+
pageSize = 0x1000
|
|
40
|
+
}
|
|
41
|
+
info := systemInfo{}
|
|
42
|
+
kernel32GetNativeSystemInfo.Call(uintptr(unsafe.Pointer(&info)))
|
|
43
|
+
pageSize = uint64(info.dwPageSize)
|
|
44
|
+
})
|
|
45
|
+
return pageSize
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Memory represents a raw block of memory.
|
|
49
|
+
type Memory struct {
|
|
50
|
+
data []byte
|
|
51
|
+
i int64
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Alloc allocates memory at addr of size with allocType and protect.
|
|
55
|
+
// It returns nil if it fails.
|
|
56
|
+
func Alloc(addr, size uint64, allocType, protect int) *Memory {
|
|
57
|
+
r, _, _ := kernel32VirtualAlloc.Call(uintptr(addr), uintptr(size), uintptr(allocType), uintptr(protect))
|
|
58
|
+
if r == 0 {
|
|
59
|
+
return nil
|
|
60
|
+
}
|
|
61
|
+
return Get(uint64(r), size)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Get returns a range of existing memory. If the range is not a block of
|
|
65
|
+
// allocated memory, the returned memory will pagefault when accessed.
|
|
66
|
+
func Get(addr, size uint64) *Memory {
|
|
67
|
+
m := &Memory{}
|
|
68
|
+
sh := (*reflect.SliceHeader)(unsafe.Pointer(&m.data))
|
|
69
|
+
sh.Data = uintptr(addr)
|
|
70
|
+
sh.Len = int(size)
|
|
71
|
+
sh.Cap = int(size)
|
|
72
|
+
return m
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Free frees the block of memory.
|
|
76
|
+
func (m *Memory) Free() {
|
|
77
|
+
sh := (*reflect.SliceHeader)(unsafe.Pointer(&m.data))
|
|
78
|
+
kernel32VirtualFree.Call(sh.Data, 0, memRelease)
|
|
79
|
+
m.data = nil
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Addr returns the actual address of the memory.
|
|
83
|
+
func (m *Memory) Addr() uint64 {
|
|
84
|
+
return uint64((*reflect.SliceHeader)(unsafe.Pointer(&m.data)).Data)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Read implements the io.Reader interface.
|
|
88
|
+
func (m *Memory) Read(b []byte) (n int, err error) {
|
|
89
|
+
if m.i >= int64(len(m.data)) {
|
|
90
|
+
return 0, io.EOF
|
|
91
|
+
}
|
|
92
|
+
n = copy(b, m.data[m.i:])
|
|
93
|
+
m.i += int64(n)
|
|
94
|
+
return n, nil
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// ReadAt implements the io.ReaderAt interface.
|
|
98
|
+
func (m *Memory) ReadAt(b []byte, off int64) (n int, err error) {
|
|
99
|
+
if off < 0 {
|
|
100
|
+
return 0, errors.New("negative offset")
|
|
101
|
+
}
|
|
102
|
+
if off >= int64(len(m.data)) {
|
|
103
|
+
return 0, io.EOF
|
|
104
|
+
}
|
|
105
|
+
n = copy(b, m.data[off:])
|
|
106
|
+
if n < len(b) {
|
|
107
|
+
return n, io.EOF
|
|
108
|
+
}
|
|
109
|
+
return n, nil
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Write implements the io.Writer interface.
|
|
113
|
+
func (m *Memory) Write(b []byte) (n int, err error) {
|
|
114
|
+
if m.i >= int64(len(m.data)) {
|
|
115
|
+
return 0, io.ErrShortWrite
|
|
116
|
+
}
|
|
117
|
+
n = copy(m.data[m.i:], b)
|
|
118
|
+
if kernel32FlushInstructionCache.Find() == nil {
|
|
119
|
+
kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[m.i])), uintptr(n))
|
|
120
|
+
}
|
|
121
|
+
m.i += int64(n)
|
|
122
|
+
return n, nil
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// WriteAt implements the io.WriterAt interface.
|
|
126
|
+
func (m *Memory) WriteAt(b []byte, off int64) (n int, err error) {
|
|
127
|
+
if off < 0 {
|
|
128
|
+
return 0, errors.New("negative offset")
|
|
129
|
+
}
|
|
130
|
+
if off >= int64(len(m.data)) {
|
|
131
|
+
return 0, io.ErrShortWrite
|
|
132
|
+
}
|
|
133
|
+
n = copy(m.data[off:], b)
|
|
134
|
+
if kernel32FlushInstructionCache.Find() == nil {
|
|
135
|
+
kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[off])), uintptr(n))
|
|
136
|
+
}
|
|
137
|
+
if n < len(b) {
|
|
138
|
+
return n, io.ErrShortWrite
|
|
139
|
+
}
|
|
140
|
+
return n, nil
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Seek implements the io.Seeker interface.
|
|
144
|
+
func (m *Memory) Seek(offset int64, whence int) (int64, error) {
|
|
145
|
+
var n int64
|
|
146
|
+
switch whence {
|
|
147
|
+
case io.SeekStart:
|
|
148
|
+
n = offset
|
|
149
|
+
case io.SeekCurrent:
|
|
150
|
+
n = m.i + offset
|
|
151
|
+
case io.SeekEnd:
|
|
152
|
+
n = int64(len(m.data)) + offset
|
|
153
|
+
default:
|
|
154
|
+
return 0, errors.New("invalid whence")
|
|
155
|
+
}
|
|
156
|
+
if n < 0 {
|
|
157
|
+
return 0, errors.New("negative position")
|
|
158
|
+
}
|
|
159
|
+
m.i = n
|
|
160
|
+
return n, nil
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Clear sets all bytes in the memory block to zero.
|
|
164
|
+
func (m *Memory) Clear() {
|
|
165
|
+
for i := range m.data {
|
|
166
|
+
m.data[i] = 0
|
|
167
|
+
}
|
|
168
|
+
if kernel32FlushInstructionCache.Find() == nil {
|
|
169
|
+
kernel32FlushInstructionCache.Call(getCurrentProcess(), uintptr(unsafe.Pointer(&m.data[0])), uintptr(len(m.data)))
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Protect changes the memory protection for a range of memory.
|
|
174
|
+
func (m *Memory) Protect(addr, size uint64, protect int) error {
|
|
175
|
+
// TODO: error handling
|
|
176
|
+
oldProtect := uint32(0)
|
|
177
|
+
kernel32VirtualProtect.Call(uintptr(m.Addr()+addr), uintptr(size), uintptr(protect), uintptr(unsafe.Pointer(&oldProtect)))
|
|
178
|
+
return nil
|
|
179
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//go:build !windows
|
|
2
|
+
// +build !windows
|
|
3
|
+
|
|
4
|
+
package winloader
|
|
5
|
+
|
|
6
|
+
import "errors"
|
|
7
|
+
|
|
8
|
+
// MakePEBEntryForModule is a hack that inserts an entry for the loaded module
|
|
9
|
+
// into the PEB loader data to make it appear to Windows functions.
|
|
10
|
+
func MakePEBEntryForModule() error {
|
|
11
|
+
return errors.New("platform not supported")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// GetProcessHInstance gets the HINSTANCE for the current process.
|
|
15
|
+
func GetProcessHInstance() (uintptr, error) {
|
|
16
|
+
return 0, errors.New("platform not supported")
|
|
17
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
package winloader
|
|
2
|
+
|
|
3
|
+
import (
|
|
4
|
+
"errors"
|
|
5
|
+
"fmt"
|
|
6
|
+
"unsafe"
|
|
7
|
+
|
|
8
|
+
"golang.org/x/sys/windows"
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
var (
|
|
12
|
+
ntdllModule = windows.NewLazySystemDLL("ntdll")
|
|
13
|
+
ntdllNtQueryInformationProcess = ntdllModule.NewProc("NtQueryInformationProcess")
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
type _ProcessBasicInformation struct {
|
|
17
|
+
Reserved1 uintptr
|
|
18
|
+
PebBaseAddress uintptr
|
|
19
|
+
Reserved2 [2]uintptr
|
|
20
|
+
UniqueProcessID uintptr
|
|
21
|
+
Reserved3 uintptr
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type _PEB struct {
|
|
25
|
+
Reserved1 [2]byte
|
|
26
|
+
BeingDebugged byte
|
|
27
|
+
Reserved2 [1]byte
|
|
28
|
+
Reserved3 [2]uintptr
|
|
29
|
+
Ldr uintptr
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type _List struct {
|
|
33
|
+
Front uintptr
|
|
34
|
+
Back uintptr
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
type _PEBLoaderData struct {
|
|
38
|
+
Length uint32
|
|
39
|
+
Initialized uint8
|
|
40
|
+
Padding [3]uint8
|
|
41
|
+
SsHandle uintptr
|
|
42
|
+
InLoadOrderModuleList _List
|
|
43
|
+
InMemoryOrderModuleList _List
|
|
44
|
+
InInitializationOrderModuleList _List
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// MakePEBEntryForModule is a hack that inserts an entry for the loaded module
|
|
48
|
+
// into the PEB loader data to make it appear to Windows functions.
|
|
49
|
+
func MakePEBEntryForModule() error {
|
|
50
|
+
process := -1
|
|
51
|
+
sizeNeeded := uint32(0)
|
|
52
|
+
pbi := _ProcessBasicInformation{}
|
|
53
|
+
status, _, _ := ntdllNtQueryInformationProcess.Call(
|
|
54
|
+
uintptr(process),
|
|
55
|
+
0,
|
|
56
|
+
uintptr(unsafe.Pointer(&pbi)),
|
|
57
|
+
unsafe.Sizeof(pbi),
|
|
58
|
+
uintptr(unsafe.Pointer(&sizeNeeded)),
|
|
59
|
+
)
|
|
60
|
+
if status != 0 {
|
|
61
|
+
return fmt.Errorf("NtQueryInformationProcess failed: %08x", status)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// TODO: Implement attempt to insert module entry.
|
|
65
|
+
return errors.New("unimplemented")
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// GetProcessHInstance gets the HINSTANCE for the current process.
|
|
69
|
+
func GetProcessHInstance() (uintptr, error) {
|
|
70
|
+
var hinstance windows.Handle
|
|
71
|
+
windows.GetModuleHandleEx(0, nil, &hinstance)
|
|
72
|
+
return uintptr(hinstance), nil
|
|
73
|
+
}
|