Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:45:36+00:00 2026-06-01T10:45:36+00:00

I want a simple C method to be able to run hex bytecode on

  • 0

I want a simple C method to be able to run hex bytecode on a Linux 64 bit machine. Here’s the C program that I have:

char code[] = "\x48\x31\xc0";
#include <stdio.h>
int main(int argc, char **argv)
{
        int (*func) ();
        func = (int (*)()) code;
        (int)(*func)();
        printf("%s\n","DONE");
}

The code that I am trying to run ("\x48\x31\xc0") I obtained by writting this simple assembly program (it’s not supposed to really do anything)

.text
.globl _start
_start:
        xorq %rax, %rax

and then compiling and objdump-ing it to obtain the bytecode.

However, when I run my C program I get a segmentation fault. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:45:38+00:00Added an answer on June 1, 2026 at 10:45 am

    Machine code has to be in an executable page. Your char code[] is in the read+write data section, without exec permission, so the code cannot be executed from there.

    Here is a simple example of allocating an executable page with mmap:

    #include <stdio.h>
    #include <string.h>
    #include <sys/mman.h>
    
    int main ()
    {
      char code[] = {
        0x8D, 0x04, 0x37,           //  lea eax,[rdi+rsi]
        0xC3                        //  ret
      };
    
      int (*sum) (int, int) = NULL;
    
      // allocate executable buffer                                             
      sum = mmap (0, sizeof(code), PROT_READ|PROT_WRITE|PROT_EXEC,
                  MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
    
      // copy code to buffer
      memcpy (sum, code, sizeof(code));
      // doesn't actually flush cache on x86, but ensure memcpy isn't
      // optimized away as a dead store.
      __builtin___clear_cache (sum, sum + sizeof(sum));  // GNU C
    
      // run code
      int a = 2;
      int b = 3;
      int c = sum (a, b);
    
      printf ("%d + %d = %d\n", a, b, c);
    }
    

    See another answer on this question for details about __builtin___clear_cache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple problem (I think): I want to be able to invoke a click method
I have a Windows Form and a class with two simple methods that run
Title says it mostly. I want to add a simple extension method to the
i want to know which parsing method is best to use among Simple XML
I have a huge table and I want simple sorting. It could be so
I want a simple function that receives a string and returns an array of
I have a series of 'tasks' that I would like to run in separate
I want to write a simple visualization of a Java program by displaying the
I run a site A and I want to be able to POST data
use case is simple: I want to run some boiler plate code before each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.