Improved Windows support

This commit is contained in:
2024-08-18 13:29:44 +02:00
parent 3fa3ff9227
commit 0db54ff639
6 changed files with 127 additions and 63 deletions

View File

@ -8,12 +8,13 @@ import (
"git.akyoto.dev/cli/q/src/arch/x64"
"git.akyoto.dev/cli/q/src/config"
"git.akyoto.dev/cli/q/src/dll"
"git.akyoto.dev/cli/q/src/exe"
"git.akyoto.dev/cli/q/src/sizeof"
)
// Finalize generates the final machine code.
func (a Assembler) Finalize() ([]byte, []byte) {
func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) {
var (
code = make([]byte, 0, len(a.Instructions)*8)
data []byte
@ -21,6 +22,7 @@ func (a Assembler) Finalize() ([]byte, []byte) {
dataLabels map[string]Address
codePointers []*Pointer
dataPointers []*Pointer
dllPointers []*Pointer
)
for _, x := range a.Instructions {
@ -114,7 +116,7 @@ func (a Assembler) Finalize() ([]byte, []byte) {
case CALL_AT:
code = x64.CallAtAddress(code, 0x00_00_00_00)
size := 4
// label := x.Data.(*Label)
label := x.Data.(*Label)
pointer := &Pointer{
Position: Address(len(code) - size),
@ -123,12 +125,11 @@ func (a Assembler) Finalize() ([]byte, []byte) {
}
pointer.Resolve = func() Address {
destination := Address(0x1038)
distance := destination - (pointer.Position + Address(pointer.Size))
return Address(distance)
index := dlls.Index("kernel32.dll", label.Name)
return Address(index * 8)
}
codePointers = append(codePointers, pointer)
dllPointers = append(dllPointers, pointer)
case COMMENT:
continue
@ -365,5 +366,12 @@ restart:
binary.LittleEndian.PutUint32(slice, uint32(address))
}
for _, pointer := range dllPointers {
destination := Address(0x3000) + pointer.Resolve()
address := destination - Address(config.CodeOffset+pointer.Position+Address(pointer.Size))
slice := code[pointer.Position : pointer.Position+4]
binary.LittleEndian.PutUint32(slice, uint32(address))
}
return code, data
}