far jump in 64-bit mode

  • Follow


Hi, all

I am doing a work which is mixing up 32-bit code and 64-bit code in a
process.
I use LLVM JIT, and try to compile some 32-bit code. I have to do "far
jump" to other code segment, to make 32-bit code can be decoded. At
this part, I inline asm into the C program, and i execute it. But I
got a segmentation fault. The follows are source code, any help and
suggestion are good for me. Thanks very much.

#include <string>
#include <memory>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS

#include <llvm/LLVMContext.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/JIT.h>


using namespace std;
using namespace llvm;

typedef unsigned long long W64;
typedef unsigned int W32;
typedef unsigned short W16;
typedef W64 Waddr;


struct FarJumpDescriptor {
    W32 offset;
    W16 seg;
};


#define LO32(x) (W32)((x) & 0xffffffffLL)
#define USER_CS       "0x33"
#define USER32_CS     "0x23"
#define USER_DS       "0x2b"

int main()
{
    InitializeNativeTarget();
    llvm_start_multithreaded();
    LLVMContext context;
    string error;
    MemoryBuffer *buffer = MemoryBuffer::getFile("tst.bc");
    Module *m = ParseBitcodeFile( buffer, context, &error);

    //setting jit builder
    EngineBuilder builder = EngineBuilder(m);
    builder.setMArch("x86");
    builder.setEngineKind(EngineKind::JIT);
    //create JIT
    ExecutionEngine* execution_engine = builder.create();
    Function* func = execution_engine->FindFunctionNamed("main");
    void (*entry_point)();
    entry_point =  reinterpret_cast<void(*)()>(execution_engine-
>getPointerToFunction(func));
    printf("entry_point:%p\n", entry_point);

    FarJumpDescriptor desc;
    desc.offset = LO32((Waddr)entry_point);
    desc.seg = 0x33;

    printf("offset:%p\n", desc.offset);

    asm volatile(
            "lea %[desc],%%eax      \n"
            "ljmp *(%%eax)          \n"
            : : [desc] "m" (desc)
    );


    return 0;
}




0
Reply joannechiou 11/25/2010 8:15:43 AM


0 Replies
587 Views

(page loaded in 0.027 seconds)

Similiar Articles:













7/26/2012 3:15:55 PM


Reply: