dimanche 30 avril 2023

How does asmjit get code relocation when use AsmParser(&a).parse

I use asmjit in my c++ code and defined a function like below:

    // parse asm_str to byte code, return the length of byte code
    int assemble(bool isx64, unsigned long long addr, const char* asm_str, char buffer[MAX_INSTRUCTION_LENGTH])
    {
        // for test, I modified param's value
        isx64 = true;
        addr = 0x6a9ec0;
        asm_str = "call 0x00007FFF1CF8CEE0";

        auto arch = isx64 ? Arch::kX64 : Arch::kX86;

        // Initialize Environment with the requested architecture.
        Environment environment;
        environment.setArch(arch);

        // Initialize CodeHolder.
        CodeHolder code;
        Error err = code.init(environment, addr);

        if (err) {
            dbg_print_err("code.init failed, reason:%s", DebugUtils::errorAsString(err));
            return 0;
        }
        x86::Assembler a(&code);
        err = AsmParser(&a).parse(asm_str, strlen(asm_str));

        if (err) {
            dbg_print_err("AsmParser(&a).parse failed, asm_str=\"%s\" addr=0x%llx reason:%s", asm_str, addr, DebugUtils::errorAsString(err));
            return 0;
        }
        else {
            CodeBuffer& buf = code.sectionById(0)->buffer();
            memcpy(buffer, buf.data(), buf.size());
            print_byte_hex(buffer, buf.size());
            return (int)buf.size();
        }
    }

When I run this funciton and got the result of buffer is 40 E8 00 00 00 00 and not find any error. Actually, I known about that this instruction could not compile to byte code in addr(0x6a9ec0). So, I want to know how to determine if such instructions are compiled successfully in the code.

How to determine if such instructions are compiled with errors in the byte code.

Aucun commentaire:

Enregistrer un commentaire