github.com/cloudwego/iasm@v0.2.0/obj/macho_test.go (about) 1 //go:build darwin 2 // +build darwin 3 4 // 5 // Copyright 2024 CloudWeGo Authors 6 // 7 // Licensed under the Apache License, Version 2.0 (the "License"); 8 // you may not use this file except in compliance with the License. 9 // You may obtain a copy of the License at 10 // 11 // http://www.apache.org/licenses/LICENSE-2.0 12 // 13 // Unless required by applicable law or agreed to in writing, software 14 // distributed under the License is distributed on an "AS IS" BASIS, 15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 // See the License for the specific language governing permissions and 17 // limitations under the License. 18 // 19 20 package obj 21 22 import ( 23 `os` 24 `os/exec` 25 `testing` 26 27 `github.com/davecgh/go-spew/spew` 28 `github.com/stretchr/testify/require` 29 ) 30 31 func TestMachO_Create(t *testing.T) { 32 fp, err := os.CreateTemp("", "macho_out-") 33 require.NoError(t, err) 34 code := []byte { 35 0x48, 0xc7, 0xc7, 0x01, 0x00, 0x00, 0x00, // MOVQ $1, %rdi 36 0x48, 0x8d, 0x35, 0x1b, 0x00, 0x00, 0x00, // LEAQ 0x1b(%rip), %rsi 37 0x48, 0xc7, 0xc2, 0x0e, 0x00, 0x00, 0x00, // MOVQ $14, %rdx 38 0x48, 0xc7, 0xc0, 0x04, 0x00, 0x00, 0x02, // MOVQ $0x02000004, %rax 39 0x0f, 0x05, // SYSCALL 40 0x31, 0xff, // XORL %edi, %edi 41 0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x02, // MOVQ $0x02000001, %rax 42 0x0f, 0x05, // SYSCALL 43 'h', 'e', 'l', 'l', 'o', ',', ' ', 44 'w', 'o', 'r', 'l', 'd', '\r', '\n', 45 } 46 err = assembleMachO(fp, code, 0, 0) 47 require.NoError(t, err) 48 err = fp.Close() 49 require.NoError(t, err) 50 err = os.Chmod(fp.Name(), 0755) 51 require.NoError(t, err) 52 println("Saved to", fp.Name()) 53 out, err := exec.Command(fp.Name()).Output() 54 require.NoError(t, err) 55 spew.Dump(out) 56 require.Equal(t, []byte("hello, world\r\n"), out) 57 err = os.Remove(fp.Name()) 58 require.NoError(t, err) 59 }