home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / ld / pass1.c < prev    next >
C/C++ Source or Header  |  1991-02-22  |  4KB  |  227 lines

  1.  
  2. /*
  3.  * Copyright (c) 1991 by Sozobon, Limited.  Author: Johann Ruegg
  4.  *
  5.  * Permission is granted to anyone to use this software for any purpose
  6.  * on any computer system, and to redistribute it freely, with the
  7.  * following restrictions:
  8.  * 1) No charge may be made other than reasonable charges for reproduction.
  9.  * 2) Modified versions must be clearly marked as such.
  10.  * 3) The authors are not responsible for any harmful consequences
  11.  *    of using this software, even if they result from defects in it.
  12.  */
  13.  
  14. #ifndef UNIXHOST
  15. #include <ar.h>
  16. #define SWAPW(a,b)
  17. #define SWAPL(a,b)
  18. #else
  19. #include "crossar.h"
  20. long crossl();
  21. #define SWAPW(a,b)    swapw(a,b)
  22. #define SWAPL(a,b)    swapl(a,b)
  23. #endif
  24.  
  25. #include "syms.h"
  26. #include "structs.h"
  27.  
  28. struct finfo *fhead;
  29. struct oinfo *ohead;
  30. struct ainfo *ahead;
  31. char *mmalloc();
  32.  
  33. extern int tb, db, trelb, drelb;
  34. extern int vflag;
  35.  
  36. pass1(s)
  37. char *s;
  38. {
  39.     struct finfo *fp;
  40.     int fd;
  41.  
  42.     fp = (struct finfo *)mmalloc(strlen(s) + sizeof(struct finfo));
  43.     listadd(&fhead, fp);
  44.     strcpy(fp->name, s);
  45.  
  46.     fd = open(s, 0);
  47.     if (vflag)
  48.         printf("process %s:\n", fp->name);
  49.     process(fd, fp);
  50.     close(fd);
  51. }
  52.  
  53. process(fd, fp)
  54. struct finfo *fp;
  55. {
  56.     unsigned short magic;
  57.  
  58.     magic = 0;
  59.     rread(fd, &magic, 2);
  60.     SWAPW(&magic, 1);
  61.     if (magic == OMAGIC)
  62.         doobj(fd, fp);
  63.     else if (magic == AMAGIC)
  64.         doarch(fd, fp);
  65.     else {
  66.         fatals("Bad magic", fp->name);
  67.     }
  68. }
  69.  
  70. doarch(fd, fp)
  71. struct finfo *fp;
  72. {
  73.     struct ar_hdr a;
  74.     long size, ostart, tell();
  75.     struct ainfo *ap;
  76.     int reuse;
  77.  
  78.     ostart = 2;
  79.     reuse = 0;
  80.     while (read(fd, &a, sizeof(a)) == sizeof(a) && a.ar_name[0]) {
  81. #ifndef UNIXHOST
  82.         size = a.ar_size;
  83. #else
  84.         size = crossl(a.ar_size);
  85. #endif
  86.         if (vflag > 2)
  87.             printf("\n%.14s size %lx\n", a.ar_name, size);
  88.  
  89.         if (reuse == 0) {
  90.             ap = (struct ainfo *)mmalloc(sizeof(struct ainfo));
  91.             listadd(&ahead, ap);
  92.         }
  93.  
  94.         ap->aoffs = ostart + sizeof(a);
  95.         ap->used = 0;
  96.         ap->sp = 0;
  97.         bcopy(a.ar_name, ap->aname, 14);
  98.  
  99.         reuse = ahdsyms(fd, ap, size);
  100.         ostart += size + sizeof(a);
  101.         llseek(fd, ostart, 0);
  102.     }
  103.  
  104.     ascan(fd, fp);
  105.     afree();
  106. }
  107.  
  108. ahdsyms(fd, ap, size)
  109. struct ainfo *ap;
  110. long size;
  111. {
  112.     char skip[HDRFILL];
  113.     short magic;
  114.     long tdlen;
  115.  
  116.     rread(fd, &magic, 2);
  117.     SWAPW(&magic, 1);
  118.     if (magic != OMAGIC)
  119.         return 1;
  120.     rread(fd, &ap->ah, sizeof(struct hdr_l));
  121.     SWAPL(&ap->ah, sizeof(struct hdr_l)/4);
  122.     rread(fd, skip, HDRFILL);
  123.  
  124.     tdlen = ap->ah.tsize + ap->ah.dsize;
  125.     llseek(fd, tdlen, 1);
  126.     getsyms(fd, ap->ah.syms, &ap->sp);
  127.     ap->havex = chkx(ap->sp);
  128.     return 0;
  129. }
  130.  
  131. ascan(fd, fp)
  132. struct finfo *fp;
  133. {
  134.     struct ainfo *ap;
  135.     int some = 0;
  136.     struct oinfo *op;
  137.  
  138. again:
  139.     for (ap = ahead; ap != 0; ap = ap->next) {
  140.         if (ap->used)
  141.             continue;
  142.         if (needed(ap->sp, ap->havex) == 0)
  143.             continue;
  144.         some = 1;
  145.  
  146.         op = (struct oinfo *)mmalloc(sizeof(struct oinfo));
  147.         listadd(&ohead, op);
  148.  
  149.         op->oh = ap->ah;
  150.  
  151.         if (vflag > 1) {
  152.             printf("%s(%.14s): ", fp->name, ap->aname);
  153.         printf("T %lx D %lx B %lx S %lx\n",
  154.             op->oh.tsize, op->oh.dsize, op->oh.bsize,
  155.             op->oh.syms);
  156.         }
  157.  
  158.         bcopy(ap->aname, op->aname, 14);
  159.         op->finfo = fp;
  160.  
  161.         llseek(fd, ap->aoffs+HDRSIZE, 0);
  162.         fbcopy(fd, tb, op->oh.tsize);
  163.         fbcopy(fd, db, op->oh.dsize);
  164.         llseek(fd, op->oh.syms, 1);
  165.         fbcopy(fd, trelb, op->oh.tsize);
  166.         fbcopy(fd, drelb, op->oh.dsize);
  167.  
  168.         ap->used = 1;
  169.         op->sp = ap->sp;
  170.         op->havex = ap->havex;
  171.  
  172.         p1syms(op->sp, op->havex, op);
  173.     }
  174.  
  175.     if (some) {
  176.         some = 0;
  177.         goto again;
  178.     }
  179. }
  180.  
  181. afree()
  182. {
  183.     struct ainfo *ap, *next;
  184.  
  185.     for (ap=ahead; ap != 0; ap = next) {
  186.         if (ap->used == 0) {
  187.             if (ap->sp)
  188.                 symfree(ap->sp);
  189.         }
  190.         next = ap->next;
  191.         free(ap);
  192.     }
  193.     ahead = 0;
  194. }
  195.  
  196. doobj(fd, fp)
  197. struct finfo *fp;
  198. {
  199.     struct oinfo *op;
  200.     char skip[HDRFILL];
  201.  
  202.     op = (struct oinfo *)mmalloc(sizeof(struct oinfo));
  203.     listadd(&ohead, op);
  204.  
  205.     rread(fd, &op->oh, sizeof(struct hdr_l));
  206.     SWAPL(&op->oh, sizeof(struct hdr_l)/4);
  207.     rread(fd, skip, HDRFILL);
  208.     op->aname[0] = 0;
  209.     op->finfo = fp;
  210.  
  211.     if (vflag > 1) {
  212.         printf("%s: ", fp->name);
  213.         printf("T %lx D %lx B %lx S %lx\n",
  214.             op->oh.tsize, op->oh.dsize, op->oh.bsize,
  215.             op->oh.syms);
  216.     }
  217.  
  218.     fbcopy(fd, tb, op->oh.tsize);
  219.     fbcopy(fd, db, op->oh.dsize);
  220.     getsyms(fd, op->oh.syms, &op->sp);
  221.     fbcopy(fd, trelb, op->oh.tsize);
  222.     fbcopy(fd, drelb, op->oh.dsize);
  223.     op->havex = chkx(op->sp);
  224.  
  225.     p1syms(op->sp, op->havex, op);
  226. }
  227.