Inline 64bit Assembly in 32bit GCC C Program

前端 未结 3 793
我在风中等你
我在风中等你 2020-12-12 05:05

I\'m compiling a 32 bit binary but want to embed some 64 bit assembly in it.

void method() {
   asm(\"...64 bit assembly...\");
}

Of cours

3条回答
  •  无人及你
    2020-12-12 05:22

    Switching between long mode and compatibility mode is done by changing CS. User mode code cannot modify the descriptor table, but it can perform a far jump or far call to a code segment that is already present in the descriptor table. In Linux the required descriptor is present (in my experience; this may not be true for all installations).

    Here is sample code for 64-bit Linux (Ubuntu) that starts in 32-bit mode, switches to 64-bit mode, runs a function, and then switches back to 32-bit mode. Build with gcc -m32.

    #include 
    #include 
    #include 
    
    extern bool switch_cs(int cs, bool (*f)());
    extern bool check_mode();
    
    int main(int argc, char **argv)
    {
        int cs = 0x33;
        if (argc > 1)
            cs = strtoull(argv[1], 0, 16);
        printf("switch to CS=%02x\n", cs);
    
        bool r = switch_cs(cs, check_mode);
    
        if (r)
            printf("cs=%02x: 64-bit mode\n", cs);
        else
            printf("cs=%02x: 32-bit mode\n", cs);
    
        return 0;
    }
    
    
            .intel_syntax noprefix
            .text
    
            .code32
            .globl  switch_cs
    switch_cs:
            mov     eax, [esp+4]
            mov     edx, [esp+8]
            push    0
            push    edx
            push    eax
            push    offset .L2
            lea     eax, [esp+8]
            lcall   [esp]
            add     esp, 16
            ret
    
    .L2:
            call    [eax]
            lret
    
    
            .code64
            .globl check_mode
    check_mode:
            xor     eax, eax
            // In 32-bit mode, this instruction is executed as
            // inc eax; test eax, eax
            test    rax, rax
            setz    al
            ret
    

提交回复
热议问题