|
|
1.1 root 1: #ifndef _I386
2: /*
3: * File: loutpatch.c
4: *
5: * Purpose: write into a l.out file
6: *
7: * $Log: loutpatch.c,v $
8: * Revision 1.3 93/05/18 07:32:52 bin
9: * *** empty log message ***
10: *
11: */
12:
13: /*
14: * ----------------------------------------------------------------------
15: * Includes.
16: */
17: #include <stdio.h>
18: #include <l.out.h>
19: #include <canon.h>
20: #include <ctype.h>
21:
22: #include <sys/machine.h>
23: #include <sys/types.h>
24: #include <sys/stat.h>
25:
26: /*
27: * ----------------------------------------------------------------------
28: * Definitions.
29: * Constants.
30: * Macros with argument lists.
31: * Typedefs.
32: * Enums.
33: */
34:
35: /*
36: * ----------------------------------------------------------------------
37: * Functions.
38: * Import Functions.
39: * Export Functions.
40: * Local Functions.
41: */
42: int loutpatch();
43:
44: /*
45: * ----------------------------------------------------------------------
46: * Global Data.
47: * Import Variables.
48: * Export Variables.
49: * Local Variables.
50: */
51: extern int xflag;
52:
53: struct ldheader ldh;
54:
55: /*
56: * ----------------------------------------------------------------------
57: * Code.
58: *
59: * Given a file name, a symbol name, a buffer, and number of bytes in
60: * the buffer, read/write the buffer into the COFF file at the symbol
61: * specified.
62: *
63: * Return nonzero if success; zero if failure.
64: */
65:
66: int
67: loutpatch(fname, sym, buf, len, do_read)
68: char *fname, *sym, *buf;
69: int len;
70: {
71: int ret = 0;
72: static struct nlist nl[2];
73: int fd;
74: long seek, seekoff, minval, maxval;
75:
76: if (strlen(sym) > NCPLN-1) {
77: fprintf(stderr, "Symbol name %s too long.\n", sym);
78: goto lp_done;
79: }
80: strcpy(nl[0].n_name, sym);
81: /*
82: * C symbols in l.out have trailing underscore.
83: */
84: strcat(nl[0].n_name, "_");
85: nlist(fname, nl);
86: if (nl[0].n_type == 0 && nl[0].n_value == 0) {
87: fprintf(stderr, "Can't find symbol %s in file %s.\n",
88: sym, fname);
89: goto lp_done;
90: }
91: if (nl[0].n_type != (L_GLOBAL + L_PRVD)) {
92: fprintf(stderr, "Symbol %s has wrong type (%d) in file %s.\n",
93: sym, nl[0].n_type, fname);
94: goto lp_done;
95: }
96: if ((fd = open(fname, 2)) < 0) {
97: fprintf(stderr, "Cannot open %s\n", fname);
98: goto lp_done;
99: }
100: if (read(fd, &ldh, sizeof(ldh)) != sizeof(ldh)) {
101: fprintf(stderr, "Cannot read %s\n", fname);
102: goto close_fd;
103: }
104: canint(ldh.l_magic);
105: if (ldh.l_magic != L_MAGIC) {
106: fprintf(stderr, "%s is not 286 executable (l.out)\n", fname);
107: goto close_fd;
108: }
109: canint(ldh.l_flag);
110: cansize(ldh.l_ssize[L_SHRI]);
111: cansize(ldh.l_ssize[L_PRVI]);
112: cansize(ldh.l_ssize[L_SHRD]);
113: cansize(ldh.l_ssize[L_PRVD]);
114: seekoff = sizeof(ldh);
115: minval = 0;
116: if (ldh.l_flag&LF_SEP) /* Separate i and d */
117: seekoff += ldh.l_ssize[L_SHRI] + ldh.l_ssize[L_PRVI];
118: else
119: minval = ldh.l_ssize[L_SHRI] + ldh.l_ssize[L_PRVI];
120: maxval = minval + ldh.l_ssize[L_SHRD] + ldh.l_ssize[L_PRVD];
121: seek = nl[0].n_value;
122: if (seek < minval || seek >= maxval) {
123: fprintf(stderr, "%s: cannot patch\n", nl[0].n_name);
124: goto close_fd;
125: }
126: lseek(fd, seekoff+seek, 0);
127: if (do_read) {
128: if (read(fd, buf, len) != len) {
129: fprintf(stderr, "Read error in %s\n", fname);
130: goto close_fd;
131: }
132: } else {
133: if (write(fd, buf, len) != len) {
134: fprintf(stderr, "Write error in %s\n", fname);
135: goto close_fd;
136: }
137: }
138:
139: ret = 1;
140: close_fd:
141: close(fd);
142:
143: lp_done:
144: return ret;
145: }
146: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.