home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1809 < prev    next >
Internet Message Format  |  1990-12-28  |  5KB

  1. From: devil@techunix.BITNET (Gil Tene)
  2. Newsgroups: alt.sources
  3. Subject: [comp.unix.programmer] ld -A EXAMPLE !
  4. Message-ID: <1990Sep11.160938.9179@math.lsa.umich.edu>
  5. Date: 11 Sep 90 16:09:38 GMT
  6.  
  7. Archive-name: dyn/11-Sep-90
  8. Original-posting-by: devil@techunix.BITNET (Gil Tene)
  9. Original-subject: ld -A EXAMPLE !
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.unix.programmer.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. Hello Unix programmers,
  16.  
  17. I have replied last week to a question about dynamic loading, and
  18. mentioned that I have some example code for using ld -A.
  19.  
  20. Since then I have been SWAMPED with requests for this example code,
  21. so I put a little program together. This program (dyn.c) accepts
  22. the name of an object file (compiled with cc -c), loads it while
  23. running, and runs it.
  24.  
  25. I have not put too much sweat into commenting this, and it is mainly
  26. whipped up from some old code I used, and some stuff I got from other
  27. people. So no flames or grudges about bad looking code, please. Just
  28. use this as an example.
  29.  
  30. The FIRST function in the object file that is loaded is the
  31. one that is run.
  32.  
  33. I have tested this on a Sun 3 running SunOs 4.0.3 and on a VAX
  34. running BSD. This program DOES NOT work on a sun-4, I have no
  35. idea why, but everyone out there is welcome to fix it up and
  36. let me know what the fix was... There is also some strange
  37. printf behaviour on the Sun-3.
  38.  
  39. Notice that the program expects the name of the object file
  40. (h.o) without the .o extension, i.e. "h" for "h.o".
  41.  
  42. VAX (BSD) :
  43.         cc -o dyn dyn.c
  44.  
  45. SUN :
  46.         cc -o dyn -Bstatic dyn.c
  47.         (the program doesn't work with dynamic libs. ).
  48.  
  49. Well, read, use, and enjoy.
  50.  
  51. Gil.
  52.  
  53. ------------------------------------------------------------------
  54. dyn.c :
  55. Cut here :
  56. -------------------------------------------------------------------
  57.  
  58. /*dyn.c an example "incremental" load (ld -A) program : */
  59.  
  60. #include <a.out.h>
  61. #include <stdio.h>
  62.  
  63. char *sbrk();
  64.  
  65. /* static global variables: */
  66. static char *base;              /* base for for algorithm load. */
  67. static char myname[50];         /* main's executable file name */
  68. static char (*load())(); /* the load function's body comes later on.. */
  69.  
  70. runc(name)
  71. char *name;
  72. {
  73.         char (*c_func)();       /* the algorithm's function */
  74.         c_func = load(name);
  75.         if (c_func != 0)
  76.                 (*(int(*)())c_func)();
  77. }
  78.  
  79.  
  80. #define round(x,s) ((((x)-1) & ~((s)-1)) + (s))
  81.  
  82. char ld[] = "/bin/ld";  /* used in load below */
  83.  
  84. static char (*load(name))()
  85. char *name;
  86. /* This function loads an algorithm from a given file */
  87. {
  88.         struct exec exec;
  89.         char n1[30];
  90.         char n2[30];
  91.         char basec[20];
  92.         char libc_path[50];
  93.         int fd;
  94.         int nread,nalloc,nread_text,nread_data,database;
  95.         char *oldbase;
  96.  
  97.         oldbase = base;
  98.         /* align base to page size : */
  99.         base = (char *) round((int)base,getpagesize());
  100.         sbrk(base - oldbase);
  101.         base = sbrk(0);
  102.         strcpy(n1,name);
  103.         strcat(n1,".o");
  104.         strcpy(n2,name);
  105.         strcat(n2,".out");
  106.         sprintf(basec,"%x",(int)base);
  107.         execx(ld,"/bin/ld",
  108.                 "-N",
  109.                 "-x",
  110.                 "-A",myname,"-T",basec,
  111.                 n1,"-o",n2,
  112.                 "-lc",
  113.                 0);
  114.         fd = open(n2,0);
  115.         if (fd > 0)
  116.         {
  117.                 read(fd,(char *)&exec,sizeof exec);
  118.                 nread_text = round(exec.a_text,4);
  119.                 nread_data = round(exec.a_data,4);
  120.                 nalloc = nread_text + nread_data + exec.a_bss;
  121.  
  122.                 nalloc = round(nalloc,1024);
  123.                 oldbase = sbrk(nalloc);
  124.  
  125.                 (int)database = (int)oldbase + nread_text;
  126.                 base += nalloc;
  127.                 read(fd,oldbase,nread_text);
  128.                 read(fd,database,nread_data);
  129.                 close(fd);
  130.                 return( (char (*)()) oldbase);
  131.         }
  132.         else
  133.         {
  134.                 printf("error while loading algorithm... !");
  135.                 close(fd);
  136.                 return(0);
  137.         }
  138. }
  139.  
  140. execx(name,arg1)
  141. char *name;
  142. char *arg1;
  143. /* called by load above... */
  144. {
  145. char **a,**arg;
  146. int rc;
  147.         a = arg = &arg1;
  148.         if(vfork() == 0) {
  149.                 execv(name,arg);
  150.         }
  151.         wait(&rc);
  152. }
  153.  
  154. main(argc,argv)
  155. int argc;
  156. char **argv;
  157. {
  158.         char name[50];
  159.         printf("main image name : %s\n",argv[0]);
  160.         strcpy(myname,argv[0]); /* get the running image name into myname */
  161.         printf("enter file name to load and run (no .o) :");
  162.         gets(name);
  163.         printf("loading and running : \n");
  164.         runc(name);
  165.         printf("main done...\n");
  166. }
  167.  
  168. ------------------------------------------------------------------
  169. h.c :
  170. Cut here :
  171. -------------------------------------------------------------------
  172.  
  173. /* h.c : an example program to be incrementally loaded : */
  174.  
  175. my_first_func()
  176. {
  177.         char s[80];
  178.         printf("hello world.\n");
  179.         printf(" Enter a string : ");
  180.         gets(s);
  181.         printf(" I got : %s\n",s);
  182.         printf(" bye bye.\n");
  183. }
  184.  
  185. --------------------------------------------------------
  186.  
  187. --
  188. --------------------------------------------------------------------
  189. | Gil Tene                      "Some days it just doesn't pay     |
  190. | devil@techunix.technion.ac.il   to go to sleep in the morning."  |
  191. --------------------------------------------------------------------
  192.