4 * (C) Copyright 1989-1995
12 * - errdmp: show s_id as string rather than array [NCPS]
13 * - relr: add support for 11 bit 8051 addressing
14 * 02-Apr-98 JLH: don't output empty hex records
24 * The module lkrloc.c contains the functions which
25 * perform the relocation calculations.
27 * lkrloc.c contains the following functions:
46 * lkrloc.c the local variable errmsg[].
50 /* Global which holds the upper 16 bits of the last 32 bit area adress
51 * output. Useful only for iHex mode.
53 int lastExtendedAddress;
55 /*)Function VOID reloc(c)
59 * The function reloc() calls a particular relocation
60 * function determined by the process code.
66 * int lkerr error flag
69 * int fprintf() c_library
70 * VOID rele() lkrloc.c
71 * VOID relp() lkrloc.c
72 * VOID relr() lkrloc.c
73 * VOId relt() lkrloc.c
76 * Refer to the called relocation functions.
103 fprintf(stderr, "Undefined Relocation Operation\n");
111 /*)Function VOID relt()
113 * The function relt() evaluates a T line read by
114 * the linker. Each byte value read is saved in the
115 * rtval[] array, rtflg[] is set, and the number of
116 * evaluations is maintained in rtcnt.
120 * T xx xx nn nn nn nn nn ...
123 * In: "T n0 n1 n2 n3 ... nn"
125 * Out: 0 1 2 .. rtcnt
126 * +----+----+----+----+----+
127 * rtval | n0 | n1 | n2 | .. | nn |
128 * +----+----+----+----+----+
129 * rtflag| 1 | 1 | 1 | 1 | 1 |
130 * +----+----+----+----+----+
132 * The T line contains the assembled code output by the assem-
133 * bler with xx xx being the offset address from the current area
134 * base address and nn being the assembled instructions and data in
141 * int rtcnt number of values evaluated
142 * int rtflg[] array of evaluation flags
143 * int rtval[] array of evaluation values
146 * int eval() lkeval.c
150 * Linker input T line evaluated.
160 rtval[rtcnt] = eval();
167 /*)Function VOID relr()
169 * The function relr() evaluates a R line read by
170 * the linker. The R line data is combined with the
171 * previous T line data to perform the relocation of
172 * code and data bytes. The S19 / IHX output and
173 * translation of the LST files to RST files may be
178 * R 0 0 nn nn n1 n2 xx xx ...
180 * The R line provides the relocation information to the linker.
181 * The nn nn value is the current area index, i.e. which area the
182 * current values were assembled. Relocation information is en-
183 * coded in groups of 4 bytes:
185 * 1. n1 is the relocation mode and object format
186 * 1. bit 0 word(0x00)/byte(0x01)
187 * 2. bit 1 relocatable area(0x00)/symbol(0x02)
188 * 3. bit 2 normal(0x00)/PC relative(0x04) relocation
189 * 4. bit 3 1-byte(0x00)/2-byte(0x08) object format for
191 * 5. bit 4 signed(0x00)/unsigned(0x10) byte data
192 * 6. bit 5 normal(0x00)/page '0'(0x20) reference
193 * 7. bit 6 normal(0x00)/page 'nnn'(0x40) reference
195 * 2. n2 is a byte index into the corresponding (i.e. pre-
196 * ceeding) T line data (i.e. a pointer to the data to be
197 * updated by the relocation). The T line data may be
198 * 1-byte or 2-byte byte data format or 2-byte word
201 * 3. xx xx is the area/symbol index for the area/symbol be-
202 * ing referenced. the corresponding area/symbol is found
203 * in the header area/symbol lists.
205 * The groups of 4 bytes are repeated for each item requiring relo-
206 * cation in the preceeding T line.
209 * areax **a pointer to array of area pointers
210 * int aindex area index
211 * char *errmsg[] array of pointers to error strings
212 * int error error code
213 * int lkerr error flag
214 * int mode relocation mode
215 * adrr_t paga paging base area address
216 * addr_t pags paging symbol address
217 * addr_t pc relocated base address
218 * addr_t r PCR relocation value
219 * addr_t reli relocation initial value
220 * addr_t relv relocation final value
221 * int rindex symbol / area index
222 * addr_t rtbase base code address
223 * addr_t rtofst rtval[] index offset
224 * int rtp index into T data
225 * sym **s pointer to array of symbol pointers
228 * head *hp pointer to the head structure
229 * rerr rerr linker error structure
230 * FILE *stderr standard error device
233 * addr_t adb_b() lkrloc.c
234 * addr_t adb_lo() lkrloc.c
235 * addr_t adb_hi() lkrloc.c
236 * addr_t adw_w() lkrloc.c
237 * addr_t evword() lkrloc.c
238 * int eval() lkeval.c
239 * int fprintf() c_library
241 * int lkulist lklist.c
243 * VOID relerr() lkrloc.c
245 * int symval() lksym.c
248 * The R and T lines are combined to produce
249 * relocated code and data. Output S19 / IHX
250 * and relocated listing files may be produced.
258 register addr_t reli, relv;
259 int aindex, rindex, rtp, error;
260 addr_t r, rtbase, rtofst, paga, pags, pc;
265 * Get area and symbol lists
273 if (eval() != (R_WORD | R_AREA) || eval()) {
274 fprintf(stderr, "R input error\n");
282 if (aindex >= hp->h_narea) {
283 fprintf(stderr, "R area error\n");
291 rtbase = adw_w(0, 0);
297 pc = adw_w(a[aindex]->a_addr, 0);
300 printf("area %d base address: 0x%x size: 0x%x rtbase: 0x%x\n", aindex,
301 a[aindex]->a_addr, a[aindex]->a_size, rtbase);
304 * Do remaining relocations
313 * R_SYM or R_AREA references
316 if (rindex >= hp->h_nglob) {
317 fprintf(stderr, "R symbol error\n");
321 reli = symval(s[rindex]);
323 if (rindex >= hp->h_narea) {
324 fprintf(stderr, "R area error\n");
328 reli = a[rindex]->a_addr;
336 reli -= (pc + (rtp-rtofst) + 1);
338 reli -= (pc + (rtp-rtofst) + 2);
343 * R_PAG0 or R_PAG addressing
345 if (mode & (R_PAG0|R_PAG)) {
346 paga = sdp.s_area->a_addr;
352 * R_BYTE or R_WORD operation
357 relv = adb_hi(reli, rtp);
359 relv = adb_lo(reli, rtp);
362 relv = adb_b(reli, rtp);
364 } else if (IS_R_J11(mode)) {
365 /* JLH: 11 bit jump destination for 8051. Forms
366 / two byte instruction with op-code bits
368 / rtp points at 3 byte locus: first two
369 / will get the instructiion. third one
373 /* Calculate absolute destination
374 / relv must be on same 2K page as pc
376 relv = adw_w(reli, rtp);
378 if ((relv & ~0x7ff) != ((pc + rtp - rtofst) & ~0x7ff)) {
382 /* Merge MSB (byte 0) with op-code, ignoring
383 / top 5 bits of address. Then hide the op-code
385 rtval[rtp] = ((rtval[rtp] & 0x07)<<5) | rtval[rtp+2];
389 else if (IS_R_J19(mode)) {
390 /* 19 bit jump destination for DS80C390. Forms
391 / three byte instruction with op-code bits
393 / rtp points at 4 byte locus: first three
394 / will get the instructiion. fourth one
398 /* Calculate absolute destination
399 / relv must be on same 512K page as pc
401 relv = adw_24(reli, rtp);
403 if ((relv & ~0x7ffff) != ((pc + rtp - rtofst) & ~0x7ffff)) {
407 /* Merge MSB (byte 0) with op-code, ignoring
408 / top 5 bits of address. Then hide the op-code
410 rtval[rtp] = ((rtval[rtp] & 0x07)<<5) | rtval[rtp+3];
414 else if (IS_C24(mode))
417 relv = adw_24(reli, rtp);
421 /* 16 bit address. */
422 relv = adw_w(reli, rtp);
426 * R_BYTE with R_BYT2 offset adjust
435 * Unsigned Byte Checking
437 if (mode & R_USGN && mode & R_BYTE && relv & ~0xFF)
441 * PCR Relocation Error Checking
443 if (mode & R_PCR && mode & R_BYTE) {
445 if (r != (addr_t) ~0x7F && r != 0)
450 * Page Relocation Error Checking
452 /* if (mode & R_PAG0 && (relv & ~0xFF || paga || pags))
454 if (mode & R_PAG && (relv & ~0xFF))
461 rerr.aindex = aindex;
463 rerr.rtbase = rtbase + rtp - rtofst - 1;
464 rerr.rindex = rindex;
465 rerr.rval = relv - reli;
466 relerr(errmsg[error-1]);
473 /* JLH: output only if data (beyond two byte address) */
474 if ((oflag == 1) && (rtcnt > 2)) {
475 int extendedAddress = (a[aindex]->a_addr >> 16) & 0xffff;
477 /* Boy, is this a hack: for ABS sections, the
478 * base address is stored as zero, and the T records
479 * indicate the offset from zero.
481 * Since T records can only indicate a 16 bit offset, this
482 * obviously creates a problem for ABS segments located
483 * above 64K (this is only meaningful in flat24 mode).
485 * However, the size of an ABS area is stored as
486 * base address + section size (I suspect this is a bug,
487 * but it's a handy one right now). So the upper 8 bits of
488 * the 24 bit address are stored in the size record.
491 * This is another reason why we can't have areas greater
492 * than 64K yet, even in flat24 mode.
494 extendedAddress += ((a[aindex]->a_size) >> 16 & 0xffff);
496 if (extendedAddress != lastExtendedAddress)
500 printf("output extended linear address record 0x%x\n",
506 ihxEntendedLinearAddress(extendedAddress);
508 else if (extendedAddress)
510 /* Not allowed to generate extended address records,
511 * but one is called for here...
514 "warning: extended linear address encountered; "
515 "you probably want the -r flag.\n");
517 lastExtendedAddress = extendedAddress;
521 if ((oflag == 2) && (rtcnt > 2)) {
527 "Unsigned Byte error",
528 "Byte PCR relocation error",
529 "Page0 relocation error",
530 "Page Mode relocation error"
534 /*)Function VOID relp()
536 * The function relp() evaluates a P line read by
537 * the linker. The P line data is combined with the
538 * previous T line data to set the base page address
539 * and test the paging boundary and length.
543 * P 0 0 nn nn n1 n2 xx xx
545 * The P line provides the paging information to the linker as
546 * specified by a .setdp directive. The format of the relocation
547 * information is identical to that of the R line. The correspond-
548 * ing T line has the following information:
549 * T xx xx aa aa bb bb
551 * Where aa aa is the area reference number which specifies the
552 * selected page area and bb bb is the base address of the page.
553 * bb bb will require relocation processing if the 'n1 n2 xx xx' is
554 * specified in the P line. The linker will verify that the base
555 * address is on a 256 byte boundary and that the page length of an
556 * area defined with the PAG type is not larger than 256 bytes.
559 * areax **a pointer to array of area pointers
560 * int aindex area index
561 * int mode relocation mode
562 * addr_t relv relocation value
563 * int rindex symbol / area index
564 * int rtp index into T data
565 * sym **s pointer to array of symbol pointers
568 * head *hp pointer to the head structure
569 * int lkerr error flag
570 * sdp sdp base page structure
571 * FILE *stderr standard error device
574 * addr_t adw_w() lkrloc.c
575 * addr_t evword() lkrloc.c
576 * int eval() lkeval.c
577 * int fprintf() c_library
579 * int symval() lksym.c
582 * The P and T lines are combined to set
583 * the base page address and report any
591 register int aindex, rindex;
598 * Get area and symbol lists
606 if (eval() != (R_WORD | R_AREA) || eval()) {
607 fprintf(stderr, "P input error\n");
615 if (aindex >= hp->h_narea) {
616 fprintf(stderr, "P area error\n");
622 * Do remaining relocations
630 * R_SYM or R_AREA references
633 if (rindex >= hp->h_nglob) {
634 fprintf(stderr, "P symbol error\n");
638 relv = symval(s[rindex]);
640 if (rindex >= hp->h_narea) {
641 fprintf(stderr, "P area error\n");
645 relv = a[rindex]->a_addr;
654 if (aindex >= hp->h_narea) {
655 fprintf(stderr, "P area error\n");
659 sdp.s_areax = a[aindex];
660 sdp.s_area = sdp.s_areax->a_bap;
661 sdp.s_addr = adw_w(0,4);
662 if (sdp.s_area->a_addr & 0xFF || sdp.s_addr & 0xFF)
663 relerp("Page Definition Boundary Error");
666 /*)Function VOID rele()
668 * The function rele() closes all open output files
669 * at the end of the linking process.
675 * int oflag output type flag
676 * int uflag relocation listing flag
680 * VOID lkulist() lklist.c
684 * All open output files are closed.
702 /*)Function addr_t evword()
704 * The function evword() combines two byte values
705 * into a single word value.
708 * addr_t v temporary evaluation variable
711 * hilo byte ordering parameter
714 * int eval() lkeval.c
717 * Relocation text line is scanned to combine
718 * two byte values into a single word value.
737 /*)Function addr_t adb_b(v, i)
739 * int v value to add to byte
740 * int i rtval[] index
742 * The function adb_b() adds the value of v to
743 * the single byte value contained in rtval[i].
744 * The new value of rtval[i] is returned.
756 * The value of rtval[] is changed.
765 return(rtval[i] += v);
768 /*)Function addr_t adb_lo(v, i)
770 * int v value to add to byte
771 * int i rtval[] index
773 * The function adb_lo() adds the value of v to the
774 * double byte value contained in rtval[i] and rtval[i+1].
775 * The new value of rtval[i] / rtval[i+1] is returned.
776 * The MSB rtflg[] is cleared.
779 * addr_t j temporary evaluation variable
782 * hilo byte ordering parameter
788 * The value of rtval[] is changed.
789 * The rtflg[] value corresponding to the
790 * MSB of the word value is cleared to reflect
791 * the fact that the LSB is the selected byte.
814 /*)Function addr_t adb_hi(v, i)
816 * int v value to add to byte
817 * int i rtval[] index
819 * The function adb_hi() adds the value of v to the
820 * double byte value contained in rtval[i] and rtval[i+1].
821 * The new value of rtval[i] / rtval[i+1] is returned.
822 * The LSB rtflg[] is cleared.
825 * addr_t j temporary evaluation variable
828 * hilo byte ordering parameter
834 * The value of rtval[] is changed.
835 * The rtflg[] value corresponding to the
836 * LSB of the word value is cleared to reflect
837 * the fact that the MSB is the selected byte.
860 /*)Function addr_t adw_w(v, i)
862 * int v value to add to word
863 * int i rtval[] index
865 * The function adw_w() adds the value of v to the
866 * word value contained in rtval[i] and rtval[i+1].
867 * The new value of rtval[i] / rtval[i+1] is returned.
870 * addr_t j temporary evaluation variable
873 * hilo byte ordering parameter
879 * The word value of rtval[] is changed.
891 j = v + (rtval[i] << 8) + (rtval[i+1] & 0xff);
892 rtval[i] = (j >> 8) & 0xff;
893 rtval[i+1] = j & 0xff;
895 j = v + (rtval[i] & 0xff) + (rtval[i+1] << 8);
897 rtval[i+1] = (j >> 8) & 0xff;
902 /*)Function addr_t adw_24(v, i)
904 * int v value to add to word
905 * int i rtval[] index
907 * The function adw_w() adds the value of v to the
908 * 24 bit value contained in rtval[i] - rtval[i+2].
909 * The new value of rtval[i] - rtval[i+2] is returned.
912 * addr_t j temporary evaluation variable
915 * hilo byte ordering parameter
921 * The word value of rtval[] is changed.
925 adw_24(addr_t v, int i)
930 j = v + ((rtval[i] & 0xff) << 16)
931 + ((rtval[i+1] & 0xff) << 8)
932 + (rtval[i+2] & 0xff);
933 rtval[i] = (j >> 16) & 0xff;
934 rtval[i+1] = (j >> 8) & 0xff;
935 rtval[i+2] = j & 0xff;
937 j = v + (rtval[i] & 0xff)
938 + ((rtval[i+1] & 0xff) << 8)
939 + ((rtval[i+2] & 0xff) << 16);
941 rtval[i+1] = (j >> 8) & 0xff;
942 rtval[i+2] = (j >> 16) & 0xff;
947 /*)Function addr_t adw_lo(v, i)
949 * int v value to add to byte
950 * int i rtval[] index
952 * The function adw_lo() adds the value of v to the
953 * double byte value contained in rtval[i] and rtval[i+1].
954 * The new value of rtval[i] / rtval[i+1] is returned.
955 * The MSB rtval[] is zeroed.
958 * addr_t j temporary evaluation variable
961 * hilo byte ordering parameter
967 * The value of rtval[] is changed.
968 * The MSB of the word value is cleared to reflect
969 * the fact that the LSB is the selected byte.
992 /*)Function addr_t adw_hi(v, i)
994 * int v value to add to byte
995 * int i rtval[] index
997 * The function adw_hi() adds the value of v to the
998 * double byte value contained in rtval[i] and rtval[i+1].
999 * The new value of rtval[i] / rtval[i+1] is returned.
1000 * The MSB and LSB values are interchanged.
1001 * The MSB rtval[] is zeroed.
1004 * addr_t j temporary evaluation variable
1007 * hilo byte ordering parameter
1013 * The value of rtval[] is changed.
1014 * The MSB and LSB values are interchanged and
1015 * then the MSB cleared.
1028 * LSB = MSB, Clear MSB
1031 rtval[i+1] = rtval[i];
1034 rtval[i] = rtval[i+1];
1040 /*)Function VOID relerr(str)
1042 * char *str error string
1044 * The function relerr() outputs the error string to
1045 * stderr and to the map file (if it is open).
1051 * FILE *mfp handle for the map file
1054 * VOID errdmp() lkrloc.c
1057 * Error message inserted into map file.
1065 errdmp(stderr, str);
1070 /*)Function VOID errdmp(fptr, str)
1072 * FILE *fptr output file handle
1073 * char *str error string
1075 * The function errdmp() outputs the error string str
1076 * to the device specified by fptr. Additional information
1077 * is output about the definition and referencing of
1078 * the symbol / area error.
1081 * int mode error mode
1082 * int aindex area index
1083 * int lkerr error flag
1084 * int rindex error index
1085 * sym **s pointer to array of symbol pointers
1086 * areax **a pointer to array of area pointers
1087 * areax *raxp error area extension pointer
1090 * sdp sdp base page structure
1093 * int fprintf() c_library
1094 * VOID prntval() lkrloc.c
1106 int mode, aindex, rindex;
1115 aindex = rerr.aindex;
1116 rindex = rerr.rindex;
1121 fprintf(fptr, "\n?ASlink-Warning-%s", str);
1125 * Print symbol if symbol based
1128 fprintf(fptr, " for symbol %s\n",
1129 &s[rindex]->s_id[0]);
1131 fprintf(fptr, "\n");
1138 " file module area offset\n");
1140 " Refby %-8.8s %-8.8s %-8.8s ",
1143 &a[aindex]->a_bap->a_id[0]);
1144 prntval(fptr, rerr.rtbase);
1150 raxp = s[rindex]->s_axp;
1155 " Defin %-8.8s %-8.8s %-8.8s ",
1156 raxp->a_bhp->h_lfile->f_idp,
1157 &raxp->a_bhp->m_id[0],
1158 &raxp->a_bap->a_id[0]);
1160 prntval(fptr, s[rindex]->s_addr);
1162 prntval(fptr, rerr.rval);
1166 /*)Function VOID prntval(fptr, v)
1168 * FILE *fptr output file handle
1169 * addr_t v value to output
1171 * The function prntval() outputs the value v, in the
1172 * currently selected radix, to the device specified
1179 * int xflag current radix
1182 * int fprintf() c_library
1195 fprintf(fptr, "%04X\n", v);
1198 fprintf(fptr, "%06o\n", v);
1201 fprintf(fptr, "%05u\n", v);
1205 /*)Function VOID relerp(str)
1207 * char *str error string
1209 * The function relerp() outputs the paging error string to
1210 * stderr and to the map file (if it is open).
1216 * FILE *mfp handle for the map file
1219 * VOID erpdmp() lkrloc.c
1222 * Error message inserted into map file.
1230 erpdmp(stderr, str);
1235 /*)Function VOID erpdmp(fptr, str)
1237 * FILE *fptr output file handle
1238 * char *str error string
1240 * The function erpdmp() outputs the error string str
1241 * to the device specified by fptr.
1244 * head *thp pointer to head structure
1247 * int lkerr error flag
1248 * sdp sdp base page structure
1251 * int fprintf() c_library
1252 * VOID prntval() lkrloc.c
1264 register struct head *thp;
1266 thp = sdp.s_areax->a_bhp;
1271 fprintf(fptr, "\n?ASlink-Warning-%s\n", str);
1278 " file module pgarea pgoffset\n");
1280 " PgDef %-8.8s %-8.8s %-8.8s ",
1281 thp->h_lfile->f_idp,
1283 &sdp.s_area->a_id[0]);
1284 prntval(fptr, sdp.s_area->a_addr + sdp.s_addr);