LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Copy a function to other memory location and execute it (https://www.linuxquestions.org/questions/programming-9/copy-a-function-to-other-memory-location-and-execute-it-458717/)

amit_bst 06-27-2006 06:26 AM

Copy a function to other memory location and execute it
 
Hi,
I want to copy text and data segment of a function to some other memory location and execute it – how to do that without creating a new process or thread? The programme is running in user privilege

Regards,

Amit

acid_kewpie 06-28-2006 01:03 AM

clearly NOT an intro. moved to programming.

jlinkels 06-28-2006 06:19 AM

Execute text and data??

If you want to execute a piece of code which happens to be "somewhere" in memory: Create a function pointer. Assign the address of your code, call the function.

Can you please also enlight why you want to use this? A malign application could be to create a buffer overflow, put your "text and data" somewhere in memory and execute it. But that is not the intention, right?

Edit: oh, and do you care to tell us which language you intend to use?

jlinkels

Hko 06-28-2006 10:08 AM

Quote:

Originally Posted by jlinkels
Execute text and data??

When talking about a program in memory, the part that contains the machine-code instructions actually is called the "text-segment" IIRC (for historical reasons I guess)

Don't know if that is what the OP meant though...

jlinkels 06-28-2006 11:29 AM

Hko,

I was educated in assembly on a HP1000, PDP-11, 6800, 6809 and 68000, and in the early days of DOS I did something on a 8086, but I never liked it with so many dedicated registers. If I remember well there were registers you could only use for calculations (AX?) but not for pointing, while others (BX?) were just used for counting offsets in data. And there were also those awful segments (code segment, data segment, extra segment?) which limited program and data structures to 64 kB

Just for my curiosity, what was called the text segment?

jlinkels

Hko 06-28-2006 12:16 PM

Quote:

Originally Posted by jlinkels
Hko,

I was educated in assembly on a HP1000, PDP-11, 6800, 6809 and 68000, and in the early days of DOS I did something on a 8086, but I never liked it with so many dedicated registers. If I remember well there were registers you could only use for calculations (AX?) but not for pointing, while others (BX?) were just used for counting offsets in data. And there were also those awful segments (code segment, data segment, extra segment?) which limited program and data structures to 64 kB

Just for my curiosity, what was called the text segment?

Quoting "Advanced Programming in the UNIX Environment, Second Edition" (abbr: "apue") [Richard Stevens, Stephen Rago], paragraph 7.6 "Memory Layout of a C program":

Quote:

quoting "apue"

Historically, a C program has been composed of the following pieces:
  • Text segment, the machine instructions that the CPU executes. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, [..snip..] Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
  • Initialized data segment, [..snip..]
  • Uninitialized data segment [..snip..]
  • Stack, [..snip..]
  • Heap [..snip..]


jlinkels 06-28-2006 02:19 PM

Aaaaggggghhhhhhhhh

Now how to distinguish between pure nonsense and things one doesn't know yet?

And it looks like calling this part "text segment" is not that ancient either, the latest revision is from 1992. They must have had good reasons to call it "text" instead of "code". Never too old to learn!

jlinkels

jim mcnamara 06-28-2006 02:35 PM

Consider pthreads - If you do what you describe you would need to allocate stack space, etc. pthreads pretty much does what you just described, minus the text segment, plus a lot of other overhead you forgot to include.

Why copy text (program code)? Just use what already exists.

Hko 06-28-2006 02:48 PM

Quote:

Originally Posted by jlinkels
Now how to distinguish between pure nonsense and things one doesn't know yet?

:)

Quote:

And it looks like calling this part "text segment" is not that ancient either, the latest revision is from 1992. They must have had good reasons to call it "text" instead of "code". Never too old to learn!
I quoted from the second edition which was published in june 2005. So it's not ancient at all..

Yes. it's indeed confusing. I've read before about "code segment" instead of "text segment" for the same thing before too. I read that chapter about two weeks ago, and remembered the name "text segment" so well just because of the confusing name.

aluser 06-28-2006 06:53 PM

It would help a lot to know *why* you need to move a function around.

Here's a completely untested guess at how to go about it.

* Figure out the size of the function somehow. A reasonable hack would be to take the address of the next function down in your file and subtract; I'm pretty sure this will give you a wrong answer for some compile flags (particularly anything that includes -freorder-functions...)

* allocate memory where you want the function to go with mmap()

* copy the function from .text to the new spot

* Perhaps use mprotect() to set the new spot readable and executable only. If you don't do this you might run into trouble on systems that enforce write-or-execute memory. I don't even know if that will always fix it.

* Perhaps compile the function with -fPIC. I don't know how often a compiler emits code which is dependent on the position of the function itself as opposed to global variables.

If you want to move things that the function references from .data, you're SOL as far as I know...

amit_bst 06-28-2006 11:24 PM

Quote:

Originally Posted by jlinkels
Execute text and data??

If you want to execute a piece of code which happens to be "somewhere" in memory: Create a function pointer. Assign the address of your code, call the function.

Can you please also enlight why you want to use this? A malign application could be to create a buffer overflow, put your "text and data" somewhere in memory and execute it. But that is not the intention, right?

Edit: oh, and do you care to tell us which language you intend to use?

jlinkels


hi jlinkels,

it is easy to attach a function pointer and call the function. bt my queston was, to copy text and data segment of a function from one memory location to another memory location using memcpy(). and execute the later one NOT THE FUNTION POINTER. :-)

any language c/assembly x86, arm is ok with me.

amit

amit_bst 06-28-2006 11:34 PM

Quote:

Originally Posted by jim mcnamara
Consider pthreads - If you do what you describe you would need to allocate stack space, etc. pthreads pretty much does what you just described, minus the text segment, plus a lot of other overhead you forgot to include.

Why copy text (program code)? Just use what already exists.

jim,

solution is there in vfork [ clone ] implementaion .....

want to copy text & data area bcoz fun only ..... just r&d

amit


All times are GMT -5. The time now is 03:16 PM.