diff --git a/src/asm/Finalize.go b/src/asm/Finalize.go index 688f377..d231a22 100644 --- a/src/asm/Finalize.go +++ b/src/asm/Finalize.go @@ -26,10 +26,21 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) { dataLabels map[string]Address codePointers []*Pointer dataPointers []*Pointer - funcPointers []*Pointer dllPointers []*Pointer + headerEnd = Address(0) ) + switch config.TargetOS { + case "linux": + headerEnd = elf.HeaderEnd + case "macos": + headerEnd = macho.HeaderEnd + case "windows": + headerEnd = pe.HeaderEnd + } + + codeStart, _ := fs.Align(headerEnd, config.Align) + for _, x := range a.Instructions { switch x.Mnemonic { case ADD: @@ -241,7 +252,7 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) { }, }) } else { - funcPointers = append(funcPointers, &Pointer{ + codePointers = append(codePointers, &Pointer{ Position: Address(len(code) - size), OpSize: uint8(opSize), Size: uint8(size), @@ -252,7 +263,7 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) { panic("unknown label") } - return Address(destination) + return config.BaseAddress + codeStart + destination }, }) } @@ -320,7 +331,7 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) { opSize := len(code) - size - start memLabel := x.Data.(*MemoryLabel) - funcPointers = append(funcPointers, &Pointer{ + codePointers = append(codePointers, &Pointer{ Position: Address(len(code) - size), OpSize: uint8(opSize), Size: uint8(size), @@ -331,7 +342,7 @@ func (a Assembler) Finalize(dlls dll.List) ([]byte, []byte) { panic("unknown label") } - return Address(destination) + return config.BaseAddress + codeStart + destination }, }) case *MemoryRegister: @@ -423,27 +434,9 @@ restart: } } - headerEnd := Address(0) - - switch config.TargetOS { - case "linux": - headerEnd = elf.HeaderEnd - case "macos": - headerEnd = macho.HeaderEnd - case "windows": - headerEnd = pe.HeaderEnd - } - - codeStart, _ := fs.Align(headerEnd, config.Align) dataStart, _ := fs.Align(codeStart+Address(len(code)), config.Align) data, dataLabels = a.Data.Finalize() - for _, pointer := range funcPointers { - address := config.BaseAddress + Address(codeStart) + pointer.Resolve() - slice := code[pointer.Position : pointer.Position+4] - binary.LittleEndian.PutUint32(slice, uint32(address)) - } - for _, pointer := range dataPointers { address := config.BaseAddress + Address(dataStart) + pointer.Resolve() slice := code[pointer.Position : pointer.Position+4]