Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!att!cbnewsk!eob From: eob@cbnewsk.ATT.COM (eamonn.j.o'brien) Newsgroups: comp.arch Subject: Re: Self-modifying code Summary: pointers to functions Message-ID: <1247@cbnewsk.ATT.COM> Date: 21 Oct 89 19:27:58 GMT References: <1080@mipos3.intel.com> <23062@cup.portal.com> Organization: AT&T Bell Laboratories Lines: 57 In article <23062@cup.portal.com>, cliffhanger@cup.portal.com (Cliff C Heyer) writes: > in general you > have no way of knowing [program] address locations that your C > compiler will create ... except for pointers to functions. The following is my attempt at self modifying C code: -------------------------------------------------- foo() { printf( "foo()\n" ); } endfoo(){} bar() { printf( "bar()\n" ); } endbar(){} main() { char* foo_ptr = foo; char* endfoo_ptr = endfoo; char* bar_ptr = bar; char* endbar_ptr = endbar; printf("before:\n"); foo(); bar(); printf("modifying:\n"); for( ; bar_ptr < endbar_ptr; ++foo_ptr,++bar_ptr ) *foo_ptr = *bar_ptr; printf("after:\n"); foo(); bar(); } ---------------------------------------------------- It calls foo() and bar(), overwrites foo() with bar(), and calls foo() and bar() again. However on my system (AT&T Unix PC -- 68000) it coredumps after printing "modifying:". Perhaps the program memory is read-only. -- Eamonn O'Brien ohm!eob eob@ohm.att.com ~~~~~~~~~~~~~~ I'm not speaking for the company, I'm just losing my mind.