LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-27-2006, 06:26 AM   #1
amit_bst
LQ Newbie
 
Registered: May 2006
Posts: 8

Rep: Reputation: 0
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
 
Old 06-28-2006, 01:03 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
clearly NOT an intro. moved to programming.
 
Old 06-28-2006, 06:19 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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

Last edited by jlinkels; 06-28-2006 at 06:21 AM.
 
Old 06-28-2006, 10:08 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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...

Last edited by Hko; 06-28-2006 at 10:09 AM.
 
Old 06-28-2006, 11:29 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 06-28-2006, 12:16 PM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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..]

Last edited by Hko; 06-28-2006 at 12:20 PM.
 
Old 06-28-2006, 02:19 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 06-28-2006, 02:35 PM   #8
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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.
 
Old 06-28-2006, 02:48 PM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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.

Last edited by Hko; 06-28-2006 at 02:49 PM.
 
Old 06-28-2006, 06:53 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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...

Last edited by aluser; 06-29-2006 at 09:19 AM.
 
Old 06-28-2006, 11:24 PM   #11
amit_bst
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 06-28-2006, 11:34 PM   #12
amit_bst
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
not calling copy constructor on function return jhorvath Programming 7 09-22-2009 12:43 PM
Execute code from memory? thedevilsjester Programming 2 02-25-2006 06:39 PM
Code for an os memory manager to execute apps NCC-1701&NCC-1701-D Programming 1 08-17-2005 10:06 PM
c++ copy() function greg108 Programming 3 08-11-2005 11:13 AM
postgres COPY function needed help vickr1z Linux - Newbie 1 11-08-2004 05:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration