🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: rec.games.programmer
Article #2248

Re: Writing code at run-time

#2248
From: Thomas Vogler
Date: Mon, 06 Jun 1994 20:40
94 lines
3490 bytes
In article <2suogv$s60@harbinger.cc.monash.edu.au> junaid@barney.eng.monash.edu.au (Junaid A. Walker) writes:

> 	Well its that time of year....

isnt april already over ?

> 	How does one set up an area under Linux gcc/ MSDOS djgpp 

see below.

> 1)	malloc the required # bytes
> 2)	use mmap() to create a executable segment mapping of 1)
> 3)	store a pointer to 1) in a function pointer, and invoke pointed to
> 	function (making sure function passing prolog is included in code).
> 	OR use a bit of inline assembler to make a direct call to register
> 	loaded with pointer in 1)

why not this way:

-------------- cut here --------------
#include <stdio.h>

char code[256]; /* or malloc it... */

/* code for this routine will be copied and modified */
 
int sub(int i)
{
  return 69 + i;
}

/* lets go */

int main ()
{
  int (* p) (int i); /* source */
  int (* q) (int i); /* destination */
  char *t; /* used for patch */

  p = sub;
  q = (int (*) (int)) code;

  memcpy (q, p, sizeof(code)); /* copy routine, but no 
				* more than buffer size
				* sorry, no way to find
				* out size of routine.
				*/

  for (t = code; t < code + sizeof(code); t++) {
    if (*t == (char) 69) {

      /*
       * patch the statement adding 69 to one that adds
       * 42. will definitely break with some architectures
       * like alpha and vax were small constants are not
       * stored in a separate byte.
       */

      printf ("found at offset %d\n", t - code);
      *t = 42;
      break;
    }
  }

  /*
   * and demonstrate our new code...
   */

  printf ("q(14) = %d\n", q(14));

  return 0;
}
-------------- cut here --------------

works fine with Linux, since data segments seem to be 
executable.

to get the necessary prefix and postfix code, do a 

			 'gcc -S filename.c'

of a source containing a stub routine with the
interface you want.

> 	Regards,
> 	Junaid.

panta rei
Thomas.
--
-- 
thomas vogler, sch"aferweg 25, 64354 reinheim, germany, tvo@zaphod.swb.de
phone: +-[49]-(6162)-83754. ein rabe geht im feld spazieren, da faellt der
weizen um (h.s.).
Technical Details
Message-ID: <TVO.94Jun6214006@zaphod.swb.de>
Path: arc1-ca.pugleaf.net!nntp.gmd.de!Germany.EU.net!EU.net!uunet!zib-berlin.de!news.th-darmstadt.de!brewhq!zaphod.swb.de!zaphod!tvo
References: <2suogv$s60@harbinger.cc.monash.edu.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10f6aa,e6b2b5751c2b9056
X-Google-Attributes: gid10f6aa,public
X-Google-Thread: fe306,e6b2b5751c2b9056
X-Google-Attributes: gidfe306,public
X-Google-Thread: 1103af,e6b2b5751c2b9056
X-Google-Attributes: gid1103af,public
X-Google-Thread: f9382,e6b2b5751c2b9056
X-Google-Attributes: gidf9382,public
X-Google-Thread: 10a997,e6b2b5751c2b9056
X-Google-Attributes: gid10a997,public
X-Google-Thread: 102bfa,e6b2b5751c2b9056
X-Google-Attributes: gid102bfa,public
X-Google-Thread: 102983,e6b2b5751c2b9056
X-Google-Attributes: gid102983,public
X-Google-ArrivalTime: 1994-06-07 16:18:29 PST
Path: arc1-ca.pugleaf.net!nntp.gmd.de!Germany.EU.net!EU.net!uunet!zib-berlin.de!news.th-darmstadt.de!brewhq!zaphod.swb.de!zaphod!tvo
From: tvo@zaphod.swb.de (Thomas Vogler)
Newsgroups: rec.games.programmer,alt.uu.comp.os.linux.questions,aus.computers.linux,comp.os.linux,comp.os.linux.misc,alt.msdos.programmer,comp.os.msdos.programmer
Subject: Re: Writing code at run-time
Date: 06 Jun 1994 20:40:06 GMT
Organization: Thomas Vogler, Reinheim, Germany
Lines: 94
Message-ID: <TVO.94Jun6214006@zaphod.swb.de>
References: <2suogv$s60@harbinger.cc.monash.edu.au>
NNTP-Posting-Host: zaphod.swb.de
In-reply-to: junaid@barney.eng.monash.edu.au's message of 6 Jun 1994 08:59:11 GMT