|
|
1.1 ! root 1: /* ! 2: * relocate command-- ! 3: * reloc file [-]octal [ - ] ! 4: * ! 5: * relocate object or a.out file up in core ! 6: * by the possibly negated octal number. ! 7: * ! 8: * if optional 3rd arg is given, ! 9: * replace "setd" at start by "nop" ! 10: */ ! 11: ! 12: int tbuf[256]; ! 13: int rbuf[256]; ! 14: int fin; ! 15: int fout; ! 16: int *txtp; ! 17: int *relp; ! 18: long relloc; ! 19: long txtloc; ! 20: int dotdot; ! 21: long txtsiz; ! 22: unsigned t1; ! 23: unsigned t2; ! 24: unsigned t4; ! 25: unsigned t7; ! 26: int txtw; ! 27: ! 28: main(argc, argv) ! 29: char *argv[]; ! 30: { ! 31: int sign, c; ! 32: ! 33: if (argc<3) { ! 34: usage: ! 35: write(1, "reloc file [-]octal\n", 20); ! 36: exit(1); ! 37: } ! 38: dotdot = 0; ! 39: if (*argv[2] == '-') { ! 40: sign = -1; ! 41: argv[2]++; ! 42: } else ! 43: sign = 1; ! 44: while (*argv[2]) { ! 45: c = *argv[2]++ - '0'; ! 46: if (c<0 || c>7) ! 47: goto usage; ! 48: dotdot = (dotdot<<3) + c; ! 49: } ! 50: dotdot *= sign; ! 51: if ((fin = open(argv[1], 0)) < 0) { ! 52: write(1, "File not readable\n", 18); ! 53: exit(1); ! 54: } ! 55: if ((fout = open(argv[1], 1)) < 0) { ! 56: write(1, "File not writable\n", 18); ! 57: exit(1); ! 58: } ! 59: txtw = read(fin, (char *)tbuf, 512); ! 60: if (tbuf[0]!=0411 && tbuf[0]!=0410 && tbuf[0]!=0407) { /* magic */ ! 61: write(1, "Bad format\n", 11); ! 62: exit(1); ! 63: } ! 64: if (tbuf[7] != 0) { /* relocation? */ ! 65: write (1, "No relocation bits\n", 19); ! 66: exit(1); ! 67: } ! 68: t1 = tbuf[1]; ! 69: t2 = tbuf[2]; ! 70: t4 = tbuf[4]; ! 71: txtloc = 020; ! 72: if (argc>3 && argv[3][1]=='o') { ! 73: dotdot += 020; ! 74: tbuf[0] = 0405; /* old magic */ ! 75: tbuf[1] += tbuf[2]; ! 76: tbuf[2] = 0; ! 77: tbuf[4] = tbuf[3]; ! 78: tbuf[3] = 0; ! 79: tbuf[6] = 0240; ! 80: tbuf[7] = t7 = 0240; ! 81: } ! 82: txtsiz = (unsigned)tbuf[1]; ! 83: txtsiz += (unsigned)tbuf[2]; ! 84: relloc = txtloc + txtsiz; ! 85: txtsiz /= 2; ! 86: lseek(fin, relloc & ~0777, 0); ! 87: read(fin, (char *)rbuf, 512); ! 88: txtp = &tbuf[8]; ! 89: relp = &rbuf[(relloc&0777) >> 1]; ! 90: if (argc>3) /* nop out "setd" at start */ ! 91: if (tbuf[8] == 0170011) ! 92: tbuf[8] = 0240; ! 93: while(txtsiz--) { ! 94: switch (*relp & 017) { ! 95: ! 96: case 01: /* pc ref to abs */ ! 97: *txtp -= dotdot; ! 98: break; ! 99: ! 100: case 02: /* ref to text */ ! 101: case 04: /* ref to data */ ! 102: case 06: /* ref to bss */ ! 103: *txtp += dotdot; ! 104: ! 105: } ! 106: advance(); ! 107: } ! 108: if (txtp != &tbuf[0]) ! 109: write(fout, (char *)&tbuf[0], txtw); ! 110: ! 111: ! 112: txtsiz = t4 / 2; ! 113: relloc = 020; ! 114: relloc += t1; ! 115: relloc += t1; ! 116: relloc += t2; ! 117: relloc += t2; ! 118: lseek(fin, relloc & ~0777, 0); ! 119: lseek(fout, relloc & ~0777, 0); ! 120: txtw = read(fin, (char *)tbuf, 512); ! 121: txtp = &tbuf[(relloc&0777) >> 1] + 4; ! 122: while((txtsiz -= 6) > 0) { ! 123: switch(*txtp & 77) { ! 124: ! 125: case 2: ! 126: case 3: ! 127: case 4: ! 128: *(txtp + 1) += dotdot; ! 129: } ! 130: adv6(); ! 131: } ! 132: if(txtp != &tbuf[0]) ! 133: write(fout, (char *)&tbuf[0], txtw); ! 134: exit(0); ! 135: } ! 136: ! 137: advance() ! 138: { ! 139: ! 140: relp++; ! 141: relloc += 2; ! 142: if (relp == &rbuf[256]) { ! 143: lseek(fin, relloc, 0); ! 144: read(fin, (char *)&rbuf[0], 512); ! 145: relp = &rbuf[0]; ! 146: } ! 147: txtp++; ! 148: txtloc += 2; ! 149: if (txtp >= &tbuf[256]) { ! 150: write(fout, (char *)&tbuf[0], txtw); ! 151: lseek(fin, txtloc, 0); ! 152: txtw = read(fin, (char *)&tbuf[0], 512); ! 153: txtp = &tbuf[0]; ! 154: } ! 155: } ! 156: ! 157: adv6() ! 158: { ! 159: txtp += 6; ! 160: if(txtp >= &tbuf[256]) { ! 161: write(fout, (char *)tbuf, txtw); ! 162: txtw = read(fin, (char *)tbuf, 512); ! 163: txtp -= 256; ! 164: } ! 165: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.