keystone-engine / keystone

Keystone assembler framework: Core (Arm, Arm64, Hexagon, Mips, PowerPC, Sparc, SystemZ & X86) + bindings
http://www.keystone-engine.org
GNU General Public License v2.0
2.3k stars 458 forks source link

Labels, Directives and Section in Keystone #204

Open brendabrandy opened 8 years ago

brendabrandy commented 8 years ago

I am relatively new to keystone and I have been trying to use keystone to assemble a simple hello world file:

    .file   "hello.c"
    .section    .rodata
.LC0:
    .string "Hello World!"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $.LC0, %edi
    call    puts
    movl    $0, %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413"
    .section    .note.GNU-stack,"",@progbits

I am going through the tutorial and the Python bindings and there doesn't seem to be functions that handle labels,sections and directives (in ARM, but I also want to know about other architectures). Could some one point me to the right direction?

aquynh commented 8 years ago

Just put this text together using \n to separate the instructions, and assemble it like in the tutorial. if you see bugs, plz report.

brendabrandy commented 8 years ago

Thanks! I tried it on a simpler multi-line assembly file and it worked! However, when I try to compile the following:

CODE2 = b".file \"hello.c\";.section    .rodata;.LC0:;.string   \"Hello World!\";.text;.globl   main;.type  main, @function'main:;.LFB0:;.cfi_startproc;pushq   %rbp;.cfi_def_cfa_offset 16;.cfi_offset 6, -16;movq %rsp, %rbp;.cfi_def_cfa_register 6;movl $.LC0, %edi;call    puts;movl   $0, %eax;popq   %rbp;.cfi_def_cfa 7, 8;ret;.cfi_endproc;.LFE0:;.size    main, .-main;.ident \"GCC: (Ubuntu 5.3.1-14ubuntu2.1) 5.3.1 20160413\";.section .note.GNU-stack,\"\",@progbits"

try:
    # Initialize engine in X86-64bit mod
    ks = Ks(KS_ARCH_X86, KS_MODE_64)
    encoding, count = ks.asm(CODE2)
    print("%s = %s (number of statements: %u)" %(CODE2, encoding, count))
except KsError as e:
    print("ERROR: %s" %e)

I got an error of " unexpected token in '.section' directive" and subsequently a seg fault... Can you shed light to the issue?

aquynh commented 8 years ago

Will be helpful if you can provide a minimal input triggering this issue, so i can quickly see what is wrong to fix it. Thanks

brendabrandy commented 8 years ago

CODE2 = b".file \"hello.c\";.section    .rodata;.LC0:;.string   \"Hello World!\""
try:
    # Initialize engine in 32-bit ARM
    ks = Ks(KS_ARCH_ARM, KS_MODE_ARM + KS_MODE_BIG_ENDIAN)
    encoding, count = ks.asm(CODE2)
    print("%s = %s (number of statements: %u)" %(CODE2,encoding, count))
    for littleBytes in encoding:
        myFileWrite.write(bytes([littleBytes]))
except KsError as e:
    print("ERROR: %s" %e)

This gave me the following response:

error: unexpected token in '.section' directive
.file   "hello.c";.section      .rodata;.LC0:;.string   "Hello World!"
                                       ^
b'.file\t"hello.c";.section\t.rodata;.LC0:;.string\t"Hello World!"' = [] (number of statements: 4)

I'll try to see where the seg fault comes in later.

cocateh commented 3 years ago

literally the minimal input to trigger the segfault is this (with gas syntax)

.text
    global _start

even with defined _start, we won't get up to point of assembling it because of the segfault.

i think it's caused by initialising sections by default but I haven't checked it yet. It segfaults on isRegistered in MCSymbol.h

SamuelAl commented 1 year ago

Getting same segfault when using .text

guusec commented 1 year ago

Still getting this when using .text