How to write self-modifying code in x86 assembly

后端 未结 7 1613
醉酒成梦
醉酒成梦 2020-11-28 18:38

I\'m looking at writing a JIT compiler for a hobby virtual machine I\'ve been working on recently. I know a bit of assembly, (I\'m mainly a C programmer. I can read most ass

7条回答
  •  被撕碎了的回忆
    2020-11-28 19:07

    A little bit simpler example based on the example above. Thanks to dwelch helped a lot.

    #include 
    #include 
    #include 
    #include 
    
    char buffer [0x2000];
    void* bufferp;
    
    char* hola_mundo = "Hola mundo!";
    void (*_printf)(const char*,...);
    
    void hola()
    { 
        _printf(hola_mundo);
    }
    
    int main ( void )
    {
        //Compute the start of the page
        bufferp = (void*)( ((unsigned long)buffer+0x1000) & 0xfffff000 );
        if(mprotect(bufferp, 1024, PROT_READ|PROT_EXEC|PROT_WRITE))
        {
            printf("mprotect failed\n");
            return(1);
        }
        //The printf function has to be called by an exact address
        _printf = printf;
    
        //Copy the function hola into buffer
        memcpy(bufferp,(void*)hola,60 //Arbitrary size);
    
    
        ((void (*)())bufferp)();  
    
        return(0);
    }
    

提交回复
热议问题