home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / src / dynamic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-04  |  2.9 KB  |  133 lines

  1. /*****
  2.  *
  3.  * File: dynamic.c
  4.  *   By: Stephen Pope
  5.  *       June 1989
  6.  *
  7.  * Cellsim, cellular automata simulator
  8.  *
  9.  * Dynamic linking routines
  10.  *
  11.  *****/
  12.  
  13. /*
  14.  * This dynamic-linking code was supplied by Stephen Pope of
  15.  * the Santa Fe Institute.
  16.  * This code allows a running program to link in an object file and
  17.  * call the first function in the object file.
  18.  */
  19.  
  20. /*
  21.  *
  22.  * Cellsim copyright 1989, 1990 by Chris Langton and Dave Hiebeler
  23.  * (cgl@lanl.gov, hiebeler@heretic.lanl.gov)
  24.  *
  25.  * This package may be freely distributed, as long as you don't:
  26.  * - remove this notice
  27.  * - try to make money by doing so
  28.  * - prevent others from copying it freely
  29.  * - distribute modified versions without clearly documenting your changes
  30.  *   and notifying us
  31.  *
  32.  * Please contact either of the authors listed above if you have questions
  33.  * or feel an exception to any of the above restrictions is in order.
  34.  *
  35.  * If you make changes to the code, or have suggestions for changes,
  36.  * let us know!  If we use your suggestion, you will receive full credit
  37.  * of course.
  38.  */
  39.  
  40. /*****
  41.  * Cellsim history:
  42.  *
  43.  * Cellsim was originally written on Apollo workstations by Chris Langton.
  44.  *
  45.  * Sun versions:
  46.  *
  47.  * - version 1.0
  48.  *   by C. Ferenbaugh and C. Langton
  49.  *   released 09/02/88
  50.  *
  51.  * - version 1.5
  52.  *   by Dave Hiebeler and C. Langton  May - June 1989
  53.  *   released 07/03/89
  54.  *
  55.  * - version 2.0
  56.  *   by Dave Hiebeler and C. Langton  July - August 1989
  57.  *   never officially released (unofficially released 09/08/89)
  58.  *
  59.  * - version 2.5
  60.  *   by Dave Hiebeler and C. Langton  September '89 - February 1990
  61.  *   released 02/26/90
  62.  *****/
  63.  
  64.  
  65.  
  66. #include <stdio.h>
  67. #include <a.out.h>
  68. #include <sys/file.h>
  69.  
  70. extern char* calloc();
  71.  
  72. char *
  73. dynamic_load(exec_name, toload_name)
  74. char* exec_name;
  75. char* toload_name;
  76. {
  77.     int size;
  78.     int pagsiz;
  79.     int init_fn;
  80.     char command[512];
  81.     int fd;
  82.     char tname[L_tmpnam];
  83.     struct exec header;
  84.     
  85.     if ((fd = open (toload_name, /* 2 */ O_RDONLY, 0)) < 0) {
  86.     fprintf (stderr, "Unable to open file %s\n", toload_name);
  87.     exit (1);
  88.     }
  89.     if (read (fd, (void*) &header, sizeof (header)) <= 0) {
  90.     fprintf (stderr, "Error in reading file %s\n", toload_name);
  91.     exit(1);
  92.     }
  93.     close (fd);
  94.  
  95.     size = header.a_text + header.a_data;
  96. #ifndef PAGSIZ
  97.     pagsiz = getpagesize();
  98. #else
  99.     pagsiz = PAGSIZ;
  100. #endif
  101.  
  102.     if (size < (pagsiz))
  103.     size = (pagsiz);
  104.  
  105.     init_fn = (int) calloc(size, sizeof(short));
  106.  
  107.     init_fn += pagsiz-1;
  108.     init_fn &= ~(pagsiz-1);
  109.  
  110.     strcpy (tname, "/tmp/dyld.XXXXXX");
  111.     mktemp (tname);
  112.  
  113.     sprintf (command, "ld -N -A %s -T %x %s -lc -lm -o %s",
  114.          exec_name, init_fn, toload_name, tname);
  115.  
  116.     if (system (command)) {
  117.     fprintf (stderr, "Error in linking file %s\n", toload_name);
  118.     exit (1);
  119.     }
  120.  
  121.     fd = open (tname, 2, 0);
  122.     if (lseek (fd, sizeof (header), L_SET) < 0) {
  123.     perror ("Error in temp file seek\n");
  124.     exit (1);
  125.     }
  126.  
  127.     read (fd, (char*) init_fn, size);
  128.     close (fd);
  129.     unlink(tname);
  130.  
  131.     return (void *)init_fn;
  132. }
  133.