github.com/chenzhuoyu/iasm@v0.9.1/obj/macho_test.go (about)

     1  package obj
     2  
     3  import (
     4      `os`
     5      `os/exec`
     6      `testing`
     7  
     8      `github.com/davecgh/go-spew/spew`
     9      `github.com/stretchr/testify/require`
    10  )
    11  
    12  func TestMachO_Create(t *testing.T) {
    13      fp, err := os.CreateTemp("", "macho_out-")
    14      require.NoError(t, err)
    15      code := []byte {
    16          0x48, 0xc7, 0xc7, 0x01, 0x00, 0x00, 0x00,   // MOVQ    $1, %rdi
    17          0x48, 0x8d, 0x35, 0x1b, 0x00, 0x00, 0x00,   // LEAQ    0x1b(%rip), %rsi
    18          0x48, 0xc7, 0xc2, 0x0e, 0x00, 0x00, 0x00,   // MOVQ    $14, %rdx
    19          0x48, 0xc7, 0xc0, 0x04, 0x00, 0x00, 0x02,   // MOVQ    $0x02000004, %rax
    20          0x0f, 0x05,                                 // SYSCALL
    21          0x31, 0xff,                                 // XORL    %edi, %edi
    22          0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x02,   // MOVQ    $0x02000001, %rax
    23          0x0f, 0x05,                                 // SYSCALL
    24          'h', 'e', 'l', 'l', 'o', ',', ' ',
    25          'w', 'o', 'r', 'l', 'd', '\r', '\n',
    26      }
    27      err = assembleMachO(fp, code, 0, 0)
    28      require.NoError(t, err)
    29      err = fp.Close()
    30      require.NoError(t, err)
    31      err = os.Chmod(fp.Name(), 0755)
    32      require.NoError(t, err)
    33      println("Saved to", fp.Name())
    34      out, err := exec.Command(fp.Name()).Output()
    35      require.NoError(t, err)
    36      spew.Dump(out)
    37      require.Equal(t, []byte("hello, world\r\n"), out)
    38      err = os.Remove(fp.Name())
    39      require.NoError(t, err)
    40  }