keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Labels, Directives and Section in Keystone

Open brendabrandy opened this issue 8 years ago • 7 comments

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?

brendabrandy avatar Jun 22 '16 17:06 brendabrandy

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

aquynh avatar Jun 22 '16 18:06 aquynh

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?

brendabrandy avatar Jun 22 '16 19:06 brendabrandy

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

aquynh avatar Jun 22 '16 21:06 aquynh


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.

brendabrandy avatar Jun 23 '16 16:06 brendabrandy

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

cocateh avatar Feb 28 '21 22:02 cocateh

Getting same segfault when using .text

SamuelAl avatar Dec 06 '22 07:12 SamuelAl

Still getting this when using .text

guusec avatar Mar 26 '23 17:03 guusec