Copy a function to other memory location and execute it
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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?
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)
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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?
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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!
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.
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.
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...
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. :-)
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.