From: TheReader06@comcast.net
To: STROEBEL NJ <stroebelnj@hotmail.com>, linux-assembly@vger.kernel.org
Subject: Re: Executing memory mapped files
Date: Tue, 02 Aug 2005 18:11:30 +0000 [thread overview]
Message-ID: <080220051811.22540.42EFB751000E44690000580C2202888744C0CF9D0A0B0E0AAD0A08AB@comcast.net> (raw)
Here's a piece of a program I wrote to do that.. (So if it doesn't compile, that's why ;)
Also note that it's a flat binary it runs, not an ELF or aout format executable..
test5.c:
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
int inf, infLen;
void *code;
void (*func)();
if (argc != 2)
{
printf("Usage: %s <input file>\n", argv[0]);
exit(-1);
}
if ( (inf = open(argv[1], O_RDONLY)) == -1)
{
printf("Failed to open '%s'..\n", argv[1]);
exit(-1);
}
infLen = lseek(inf, 0, SEEK_END);
lseek(inf, 0, SEEK_SET);
code = malloc(infLen);
if (read(inf, code, infLen) != infLen)
{
printf("Failed to read entire file..\n");
exit(-1);
}
close(inf);
printf("Executing code..\n");
fflush(stdout);
func = (void (*)()) code;
(*func)();
return (0);
}
Good luck,
Joshua Roys
>
> Is it possible to load an executable binary (8k) into memory using mmap()
> function ant then executing the binary program in memory.
>
> I tried something like this in C and it only seemed to load the binary into
> memory. I then tried the exec family to execute the binary but nothing
> happened.Maybe in assmebler there is a better method ?
>
> here is the source:
>
>
> #include <unistd.h>
> #include <stdio.h>
> #include <ctype.h>
> #include <fcntl.h>
> #include <string.h>
> #include <malloc.h>
> #include <sys/mman.h>
> #include <sys/stat.h>
> #include <stdlib.h>
> #include <signal.h>
> #include </usr/include/sys/types.h>
> #include </usr/include/sys/stat.h>
> #include <arpa/inet.h>
> #include <netdb.h>
> #include <netinet/in.h>
> #include <sys/types.h>
> #include <sys/socket.h>
>
> // gcc -o example1 example1.c
>
> void err_quit(char *msg);
>
> int main(int argc,char *argv[])
> {
>
> int fdin;
> char *src,**dh;
> struct stat statbuf;
> off_t len;
>
>
> if (( fdin = open("prog1",O_RDONLY)) < 0) //prog1 binary executable file
> err_quit("open failed");
>
>
> if ((fstat(fdin,&statbuf)) < 0)
> err_quit("fstat failed");
>
> len = statbuf.st_size;
>
> if ((src = mmap(0, len,PROT_EXEC,MAP_SHARED,fdin,0)) == (void *)-1)
>
> err_quit("mmap failed");
>
>
>
>
> if (! fork())
> {
> int rt;
>
> rt = execve(src,src,0);
> sleep(2);
>
> }
>
> sleep(4);
> close(fdin);
> munmap(src,len);
> exit(0);
>
> }
>
>
> void err_quit(char *msg)
>
> {
> perror(msg);
> exit(EXIT_FAILURE);
>
>
> }
>
>
> Im using Fedora 3 ,PC: 1MB ram , 3.2 Ghz pentuim.
> Any suggestions ?
>
> stroebelnj@hotmail.com
>
> _________________________________________________________________
> Upgrade to MSN Hotmail Plus - your e-mail, your way!
> http://join.msn.com/?pgmarket=en-xe&DI=1054&XAPID=1816
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
next reply other threads:[~2005-08-02 18:11 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-08-02 18:11 TheReader06 [this message]
[not found] <MC6-F34jriLdElsBt8s003595b5@MC6-F34.hotmail.com>
2005-08-02 16:38 ` Executing memory mapped files STROEBEL NJ
2005-08-04 19:11 ` Brian Raiter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=080220051811.22540.42EFB751000E44690000580C2202888744C0CF9D0A0B0E0AAD0A08AB@comcast.net \
--to=thereader06@comcast.net \
--cc=linux-assembly@vger.kernel.org \
--cc=stroebelnj@hotmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).