-----
mem.h
-----
#define GMEM_BASE 0x08000000
struct global_mem_struct
{
int any_variable;
int another_var;
};
-----
main.c
-----
#include <stdio.h>
#include "./mem.h"
struct global_mem_struct *gmem;
void main()
{
int i;
system("rm -f GMEM_LOC");
gmem = (struct global_mem_struct *)
map(GMEM_BASE,sizeof(struct global_mem_struct));
gmem->any_variable = 1;
printf("Enter an integer.\n");
scanf("%d",&i);
gmem->any_variable = 0;
printf("Other var was %d\n",gmem->another_var);
gmem->another_var = i;
}
-----
main2.c
-----
#include <stdio.h>
#include <signal.h>
#include "mem.h"
void im_ok();
struct global_mem_struct *gmem;
void main()
{
signal(11,im_ok); /* to cover SIGSEGV -- segment violation */
gmem = (struct global_mem_struct *)
map(GMEM_BASE,sizeof(struct global_mem_struct));
printf("I too am waiting for you to enter something.\n");
printf("%d %d\n",gmem->any_variable, gmem->another_var);
while (gmem->any_variable)
signal(11,im_ok); /* to cover SIGSEGV -- segment violation */
printf("goodbye world.\n");
printf("%d %d\n",gmem->any_variable, gmem->another_var);
}
-----
map.c
-----
#include <stdio.h>
#include <signal.h>
void im_ok()
{
}
char * map(address, size)
unsigned long address;
unsigned long size;
{
FILE *fp;
char *p;
signal(11,im_ok); /* to cover SIGSEGV -- segment violation */
if ((fp = fopen("GMEM_LOC","r"))==NULL)
{
if ((fp = fopen("GMEM_LOC","w"))==NULL)
{
printf("Error.\n");
exit(1);
}
else
{
p = (char *)malloc(size);
printf("Printing %p to file.\n",p);
fprintf(fp," %p\n",p);
fclose(fp);
}
}
else
{
fscanf(fp,"%p",&p);
printf("Read %p from file.\n",p);
fclose(fp);
}
return p;
}
-----
make script
-----
gcc -o main main.c map.c
gcc -o main2 main2.c map.c
-----