Improved Windows DLL calls

This commit is contained in:
2024-08-19 11:11:45 +02:00
parent 0db54ff639
commit 05789d9626
12 changed files with 124 additions and 68 deletions

View File

@ -113,24 +113,6 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) {
codePointers = append(codePointers, pointer)
case CALL_AT:
code = x64.CallAtAddress(code, 0x00_00_00_00)
size := 4
label := x.Data.(*Label)
pointer := &Pointer{
Position: Address(len(code) - size),
OpSize: 2,
Size: uint8(size),
}
pointer.Resolve = func() Address {
index := dlls.Index("kernel32.dll", label.Name)
return Address(index * 8)
}
dllPointers = append(dllPointers, pointer)
case COMMENT:
continue
@ -142,6 +124,36 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) {
code = x64.CompareRegisterRegister(code, operands.Destination, operands.Source)
}
case DLLCALL:
size := 4
code = x64.SubRegisterNumber(code, x64.RSP, 32)
code = x64.CallAtAddress(code, 0x00_00_00_00)
position := len(code) - size
code = x64.AddRegisterNumber(code, x64.RSP, 32)
label := x.Data.(*Label)
pointer := &Pointer{
Position: Address(position),
OpSize: 2,
Size: uint8(size),
}
pointer.Resolve = func() Address {
dot := strings.Index(label.Name, ".")
library := label.Name[:dot]
funcName := label.Name[dot+1:]
index := dlls.Index(library, funcName)
if index == -1 {
panic("unknown DLL function " + label.Name)
}
return Address(index * 8)
}
dllPointers = append(dllPointers, pointer)
case JE, JNE, JG, JGE, JL, JLE, JUMP:
switch x.Mnemonic {
case JE:

View File

@ -20,6 +20,16 @@ func (a *Assembler) Call(name string) {
})
}
// DLLCall calls a function in a DLL file.
func (a *Assembler) DLLCall(name string) {
a.Instructions = append(a.Instructions, Instruction{
Mnemonic: DLLCALL,
Data: &Label{
Name: name,
},
})
}
// Return returns back to the caller.
func (a *Assembler) Return() {
if len(a.Instructions) > 0 {

View File

@ -29,7 +29,7 @@ const (
// Control flow
CALL
CALL_AT
DLLCALL
JE
JNE
JG
@ -55,8 +55,8 @@ func (m Mnemonic) String() string {
return "and"
case CALL:
return "call"
case CALL_AT:
return "call at"
case DLLCALL:
return "dllcall"
case COMMENT:
return "comment"
case COMPARE: