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 |
clearly NOT an intro. moved to programming.
|
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 |
Quote:
Don't know if that is what the OP meant though... |
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 |
Quote:
Quote:
|
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 |
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. |
Quote:
Quote:
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. |
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... |
Quote:
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 |
Quote:
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. |