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 7173559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:50:58+00:00 2026-05-28T15:50:58+00:00

Let’s suppose I have a function: int f1(int x){ // some more or less

  • 0

Let’s suppose I have a function:

int f1(int x){
 // some more or less complicated operations on x
 return x;
}

And that I have another function

int f2(int x){
 // we simply return x
 return x;
}

I would like to be able to do something like the following:

char* _f1 = (char*)f1;
char* _f2 = (char*)f2;
int i;
for (i=0; i<FUN_LENGTH; ++i){
 f1[i] = f2[i];
}

I.e. I would like to interpret f1 and f2 as raw byte arrays and “overwrite f1 byte by byte” and thus, replace it by f2.

I know that usually callable code is write-protected, however, in my particular situation, you can simply overwrite the memory location where f1 is located. That is, I can copy the bytes over onto f1, but afterwards, if I call f1, the whole thing crashes.

So, is my approach possible in principle? Or are there some machine/implementation/whatsoever-dependent issues I have to take into consideration?

  • 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-05-28T15:50:59+00:00Added an answer on May 28, 2026 at 3:50 pm

    It would be easier to replace the first few bytes of f1 with a machine jump instruction to the beginning of f2. That way, you won’t have to deal with any possible code relocation issues.

    Also, the information about how many bytes a function occupies (FUN_LENGTH in your question) is normally not available at runtime. Using a jump would avoid that problem too.

    For x86, the relative jump instruction opcode you need is E9 (according to here). This is a 32-bit relative jump, which means you need to calculate the relative offset between f2 and f1. This code might do it:

    int offset = (int)f2 - ((int)f1 + 5); // 5 bytes for size of instruction
    char *pf1 = (char *)f1;
    pf1[0] = 0xe9;
    pf1[1] = offset & 0xff;
    pf1[2] = (offset >> 8) & 0xff;
    pf1[3] = (offset >> 16) & 0xff;
    pf1[4] = (offset >> 24) & 0xff;
    

    The offset is taken from the end of the JMP instruction, so that’s why there is 5 added to the address of f1 in the offset calculation.

    It’s a good idea to step through the result with an assembly level debugger to make sure you’re poking the correct bytes. Of course, this is all not standards compliant so if it breaks you get to keep both pieces.

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

Sidebar

Related Questions

Let's suppose I have a Discussion model, and that a discussion can be tagged.
Let's say that I have classes like this: public class Parent { public int
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say I have a simple Login servlet that checks the passed name and
Let's say I have saved this kind of data (some text '.date(d).' some text)
Let's say I have some content classes like Page, TabGroup, Tab, etc. Certain of
Let's say I have the string: hello world; some random text; foo; How could
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>

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.