LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 02-25-2006, 06:11 PM   #1
thedevilsjester
Member
 
Registered: Apr 2005
Posts: 32

Rep: Reputation: 15
Execute code from memory?


Is there any way that I can take an executable, fread() it into a byte array, and have it executed purely from memory? Would any of the exec*() functions have a way of doing this?

(Linux)
 
Old 02-25-2006, 06:38 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Yes it can be done, but it's rough.

I've done it with the following conditions:

* The read executable was staticly linked.
* The read executable was linked into addresses which weren't used by the executable which loaded it. (You can do this by tweaking the linker script; my default one is /usr/lib/ldscripts/elf_i386.x and you use the -T argument to ld to use a different one.)

You can man 5 elf to read about the format of an executable. The important points are that an executable is a series of sections, each of which has an offset and length in the file, and an address and length in memory. Your job is to load it there.

To begin execution of the thing you read in, see the e_entry field in the elf format and the getcontext()/setcontext() libc functions.


You could roll dynamic linking yourself somehow, but it probably wouldn't be fun.

----

The "normal" way to do this sort of thing is to have the code you were going to read into memory be a shared library instead, and use dlopen() on it.

If you've got more questions, ask away..
 
Old 02-25-2006, 06:39 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
oh yeah, you'll have to use mmap to get memory allocated where you want it
 
  


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
Code for an os memory manager to execute apps NCC-1701&NCC-1701-D Programming 1 08-17-2005 10:06 PM
execute command from c code alaios Programming 3 06-06-2005 05:26 AM
how to execute shell script with c code? khucinx Programming 3 05-04-2004 02:54 PM
Getting PHP code to execute in phpBB templates Travis86 Linux - Software 3 12-22-2003 07:53 PM
How to compile and execute Doom 1.10 source code sharathkv Linux - Newbie 1 08-26-2003 03:01 AM


All times are GMT -5. The time now is 06:37 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration