Wrox Programmer Forums
|
BOOK: Professional Assembly Language
This is the forum to discuss the Wrox book Professional Assembly Language by Richard Blum; ISBN: 9780764579011
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Assembly Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2006, 10:31 AM
Registered User
 
Join Date: Jul 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Jump to a code block

I get a SIGSEGEV segment violation when using an inline assembly jmp instruction to jump to a block of code saved in a data area. A test program is shown below. Thanks for your help!

// jump.c - Employ inline assembly to perform a jump into code block.
// To compile: gcc -gstabs -o jump jump.c
// To show assembler: gcc -S jump.c
// To generate obj: gcc -c jump.c
// To disassemble: objdump -d jump.o
// To debug: gdb jump
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>

typedef unsigned long ulong;
int main()
{
    char *apIp;
    char *apBp = NULL, *apCp, *apEp;
    char **apLp; // Ptr to place to place holding a char ptr
    int aSize = 100; // code size in bytes
    int aRet;
    unsigned long aB = 2;

    // Allocate a block of memory to hold code. Align to a page boundry
    apBp = malloc(aSize + 4096);
    aB = (unsigned long)apBp;
    aB += 4095;
    aB &= 0xFFFFF000;
    apIp = apCp = (char *)aB;
    apEp = apIp + aSize;

    // Fill code block with no-ops
    while (apCp < apEp)
        *apCp++ = 0x90;

    // End code block with a jump back to lDone
    apCp = apEp - 6;
    *apCp++ = 0xFF; // jmp offset32
    *apCp++ = 0x25;
    apLp = (char **)apCp;
    *apLp = &&lDone;

    // Allow execute permission in data block.
    aRet = mprotect(apIp, aSize, PROT_EXEC | PROT_WRITE | PROT_READ);

    // Various jump instructions.
    ///asm ("jmp lDone"); // FF 25 ofs jmp lDone
    /// asm ("movl %0, %%eax\n\t" // 8B 45 F8 mov -8(%epb), %eax
    /// "jmp *%%eax" : :"m" (apIp)); // FF E0 jmp *%eax
    /// asm (".intel_syntax\n\tjmp dword ptr [apX]"); // Does not work
    /// asm ("leal %0, %%eax\n\t" // 8D 4f F8 lea -8(%ebp), %eax
    /// "jmp *(%%eax)" : : "m" (apIp));// FF 20 jmp *(%eax)
    asm ("jmp *%0" : : "m" (apIp)); // FF 65 F8 jmp *-8(%epb)
    printf("This statement is not reachable.\n");
    free(apBp);
    exit (0);
    // Finish up here
lDone:
    printf("Return from code block\n");
    free(apBp);
    return 0;
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Render Block to hide table rdove84 ASP.NET 1.0 and 1.1 Basics 5 February 16th, 2007 05:57 PM
Please help with Jump to URL lina_d_d Crystal Reports 0 October 26th, 2006 04:59 AM
a block of html code keyvanjan ASP.NET 1.0 and 1.1 Basics 2 July 14th, 2006 12:48 AM
why it jump? kentown C++ Programming 3 December 6th, 2004 10:51 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.