home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / ELOAD.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  3KB  |  98 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. //************************************************************//
  7. //                                                            //
  8. //     F I L E :    E L O A D . C                             //
  9. //                                                            //
  10. //         Code for C function loader function                //
  11. //                                                            //
  12. //************************************************************//
  13.  
  14. #include <a.out.h>
  15. #include <fcntl.h>
  16. #include "emptr.h"
  17. #include "String.h"
  18.  
  19. static String symtab;
  20. static char y = 'a';
  21.  
  22. extern "C" vptp load(const char *filename) {
  23.     // load first link edits the specified file into
  24.     // a new a.out, using a previous a.out file as
  25.     // the base to resolve symbolic references.
  26.     // load then opens the new file, figures out its
  27.     // .text and .data sizes, and reads them into a
  28.     // newly allocated block of memory.  It functions
  29.     // as a dynamic loader, designed to work with an
  30.     // outboard incremental link editor.  C linkage
  31.     // is just so it can be called from C, too.
  32.  
  33.     int errcode = 0;
  34.     String newfile;
  35.     char buf[256];
  36.     long adx, oadx;
  37.     unsigned char *ldadx;
  38.     struct exec Exec;
  39.     int fd, wc;
  40.  
  41.     // use reasonable defaults first time through;  a.out will
  42.     // be the file supplying the symbol table.  Each time we do
  43.     // an incremental load, change the name of the file that
  44.     // the link editor will produce.  Clean up old files as
  45.     // we go along.
  46.  
  47.     if (!symtab.length()) {
  48.         symtab = "a.out"; newfile = "b.out";
  49.     } else {
  50.         symtab = String(++y) + ".out";
  51.         newfile = String(y+1) + ".out";
  52.     }
  53.  
  54.     // find current memory high, and pad things so memory high
  55.     // is on an even page boundary.
  56.     oadx = (long)sbrk(0);
  57.     adx = oadx + PAGSIZ - (oadx%PAGSIZ);
  58.  
  59.     // create load command to do an incremental link edit
  60.     // of the provided .o against the current a.out, specifying
  61.     // that the new code be linked at memory high
  62.     sprintf(buf, "ld -N -Ttext %X -A %s %s -o %s",
  63.         adx, (const char*)symtab, filename,
  64.         (const char*)newfile);
  65.     printf("<%s>\n", buf);
  66.     if ((errcode=system(buf)) != 0) {
  67.         printf("load: link edit returned error code %d\n",
  68.             errcode);
  69.     }
  70.     if (symtab != "a.out") unlink(symtab);
  71.  
  72.     // open it up to load it into memory
  73.     fd = open(newfile, O_RDONLY);
  74.     if (fd < 0) {
  75.         printf("load: open of \"%s\" failed\n", newfile);
  76.         return 0;
  77.     }
  78.  
  79.     // read the relocatable file header to get text,
  80.     // data sizes
  81.     read(fd, (char *)&Exec, sizeof(struct exec));
  82.  
  83.     // now do the memory pad, and allocate space for the
  84.     // new program text
  85.     sbrk(int(PAGSIZ-(oadx%PAGSIZ)));
  86.     ldadx = (unsigned char *)
  87.         sbrk(int(Exec.a_text + Exec.a_data + Exec.a_bss));
  88.  
  89.     // read the newly linked file into the running
  90.     // process at the address just calculated
  91.     wc = read(fd, (char *)ldadx,
  92.     int(Exec.a_text + Exec.a_data));
  93.     close(fd);
  94.  
  95.     // return load address
  96.     return (vptp) ldadx;
  97. }
  98.