home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / language / asxsrc / lkrloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-25  |  2.8 KB  |  214 lines

  1. /* lkrloc.c */
  2.  
  3. /*
  4.  * (C) Copyright 1989
  5.  * All Rights Reserved
  6.  *
  7.  * Alan R. Baldwin
  8.  * 721 Berkeley St.
  9.  * Kent, Ohio  44240
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "aslink.h"
  14.  
  15.  
  16. /*
  17.  * Process relocation operations
  18.  * and call designated output routine
  19.  */
  20. VOID
  21. reloc(c)
  22. char c;
  23. {
  24.     if (oflag == 0)
  25.         return;
  26.  
  27.     switch(c) {
  28.  
  29.     case 'T':
  30.         relt();
  31.         break;
  32.  
  33.     case 'R':
  34.         relr();
  35.         break;
  36.  
  37.     case 'E':
  38.         rele();
  39.         break;
  40.  
  41.     default:
  42.         fprintf(stderr, "Undefined Relocation Operation\n");
  43.         break;
  44.  
  45.     }
  46. }
  47.  
  48.  
  49. /*
  50.  * Relocation 'T' processing.
  51.  */
  52. VOID
  53. relt()
  54. {
  55.     rtcnt = 0;
  56.     while (more()) {
  57.         if (rtcnt < NTXT) {
  58.             rtval[rtcnt++] = eval();
  59.         }
  60.     }
  61. }
  62.  
  63. /*
  64.  * Relocation 'R' processing
  65.  */
  66. VOID
  67. relr()
  68. {
  69.     register pc, relv, index;
  70.     int mode, rtp;
  71.     struct areax **a;
  72.     struct sym **s;
  73.  
  74.     if (eval() != (R_WORD | R_AREA) || eval())
  75.         fprintf(stderr, "R input error\n");
  76.  
  77.     /*
  78.      * Get area and symbol lists
  79.      */
  80.     a = (struct areax **) hp->a_list;
  81.     s = (struct sym **) hp->s_list;
  82.  
  83.     /*
  84.      * Get area pointer
  85.      */
  86.     index = evword();
  87.     if (index >= hp->h_narea) {
  88.         fprintf(stderr, "R area error\n");
  89.         return;
  90.     }
  91.  
  92.     /*
  93.      * Relocate address
  94.      */
  95.     pc = add_w(a[index]->a_addr, 0);
  96.  
  97.     /*
  98.      * Do remaining relocations
  99.      */
  100.     while (more()) {
  101.         mode = eval();
  102.         rtp = eval();
  103.         index = evword();
  104.  
  105.         /*
  106.          * R_SYM or R_AREA references
  107.          */
  108.         if (mode & R_SYM) {
  109.             if (index >= hp->h_nglob) {
  110.                 fprintf(stderr, "R symbol error\n");
  111.                 return;
  112.             }
  113.             relv = symval(s[index]);
  114.         } else {
  115.             if (index >= hp->h_narea) {
  116.                 fprintf(stderr, "R area error\n");
  117.                 return;
  118.             }
  119.             relv = a[index]->a_addr;
  120.         }
  121.  
  122.         /*
  123.          * R_PCR or R_NORM addressing
  124.          */
  125.         if (mode & R_PCR) {
  126.             if (mode & R_BYTE) {
  127.                 relv -= (pc + (rtp-2) + 1);
  128.             } else {
  129.                 relv -= (pc + (rtp-2) + 2);
  130.             }
  131.         }
  132.  
  133.         /*
  134.          * R_BYTE or R_WORD operation
  135.          */
  136.         if (mode & R_BYTE) {
  137.             add_b(relv, rtp);
  138.         } else {
  139.             add_w(relv, rtp);
  140.         }
  141.     }
  142.     if (oflag == 1) {
  143.         ihx(1);
  144.     } else
  145.     if (oflag == 2) {
  146.         s19(1);
  147.     }
  148. }
  149.  
  150. /*
  151.  * EOF processing
  152.  */
  153. VOID
  154. rele()
  155. {
  156.     if (oflag == 1) {
  157.         ihx(0);
  158.     } else
  159.     if (oflag == 2) {
  160.         s19(0);
  161.     }
  162. }
  163.  
  164. /*
  165.  * Evaluate word
  166.  */
  167. int
  168. evword()
  169. {
  170.     register v;
  171.  
  172.     if (hilo) {
  173.         v = (eval() << 8);
  174.         v += eval();
  175.     } else {
  176.         v = eval();
  177.         v += (eval() << 8);
  178.     }
  179.     return(v);
  180. }
  181.  
  182. /*
  183.  * Add byte values
  184.  */
  185. int
  186. add_b(v, i)
  187. register v, i;
  188. {
  189.     rtval[i] += v;
  190.     return(rtval[i] &= 0xff);
  191. }
  192.  
  193. /*
  194.  * Add word values
  195.  */
  196. int
  197. add_w(v, i)
  198. register v, i;
  199. {
  200.     register j;
  201.  
  202.     if (hilo) {
  203.         j = v + (rtval[i] << 8) + (rtval[i+1] & 0xff);
  204.         rtval[i] = (j >> 8) & 0xff;
  205.         rtval[i+1] = j & 0xff;
  206.     } else {
  207.         j = v + (rtval[i] & 0xff) + (rtval[i+1] << 8);
  208.         rtval[i] = j & 0xff;
  209.         rtval[i+1] = (j >> 8) & 0xff;
  210.     }
  211.     return(j);
  212. }
  213.  
  214.