binpatch.c revision 1.5 1 /* $NetBSD: binpatch.c,v 1.5 2009/03/14 21:04:07 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1994 Christian E. Hopps
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Christian E. Hopps.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/types.h>
34 #include <a.out.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37
38 extern char *optarg;
39 extern int optind;
40
41 void error (char *) __attribute__((__noreturn__));
42
43 int test = 1;
44 int testbss;
45 char foo = 23;
46
47
48 int
49 main(int argc, char *argv[])
50 {
51 struct exec e;
52 int c;
53 u_long addr = 0, offset = 0;
54 u_long replace = 0, do_replace = 0;
55 char *symbol = 0;
56 char size = 4; /* default to long */
57 char *fname;
58 int fd;
59 int type, off;
60 u_long lval;
61 u_short sval;
62 u_char cval;
63
64
65 while ((c = getopt (argc, argv, "a:bwlr:s:o:")) != -1)
66 switch (c)
67 {
68 case 'a':
69 if (addr || symbol)
70 error ("only one address/symbol allowed");
71 if (! strncmp (optarg, "0x", 2))
72 sscanf (optarg, "%x", &addr);
73 else
74 addr = atoi (optarg);
75 if (! addr)
76 error ("invalid address");
77 break;
78
79 case 'b':
80 size = 1;
81 break;
82
83 case 'w':
84 size = 2;
85 break;
86
87 case 'l':
88 size = 4;
89 break;
90
91 case 'r':
92 do_replace = 1;
93 if (! strncmp (optarg, "0x", 2))
94 sscanf (optarg, "%x", &replace);
95 else
96 replace = atoi (optarg);
97 break;
98
99 case 's':
100 if (addr || symbol)
101 error ("only one address/symbol allowed");
102 symbol = optarg;
103 break;
104
105 case 'o':
106 if (offset)
107 error ("only one offset allowed");
108 if (! strncmp (optarg, "0x", 2))
109 sscanf (optarg, "%x", &offset);
110 else
111 offset = atoi (optarg);
112 break;
113 }
114
115 argv += optind;
116 argc -= optind;
117
118
119 if (argc < 1)
120 error ("No file to patch.");
121
122 fname = argv[0];
123 if ((fd = open (fname, 0)) < 0)
124 error ("Can't open file");
125
126 if (read (fd, &e, sizeof (e)) != sizeof (e)
127 || N_BADMAG (e))
128 error ("Not a valid executable.");
129
130 /* fake mid, so the N_ macros work on the amiga.. */
131 e.a_midmag |= 127 << 16;
132
133 if (symbol)
134 {
135 struct nlist nl[2];
136 nl[0].n_un.n_name = symbol;
137 nl[1].n_un.n_name = 0;
138 if (nlist (fname, nl) != 0)
139 error ("Symbol not found.");
140 addr = nl[0].n_value;
141 type = nl[0].n_type & N_TYPE;
142 }
143 else
144 {
145 type = N_UNDF;
146 if (addr >= N_TXTADDR(e) && addr < N_DATADDR(e))
147 type = N_TEXT;
148 else if (addr >= N_DATADDR(e) && addr < N_DATADDR(e) + e.a_data)
149 type = N_DATA;
150 }
151 addr += offset;
152
153 /* if replace-mode, have to reopen the file for writing.
154 Can't do that from the beginning, or nlist() will not
155 work (at least not under AmigaDOS) */
156 if (do_replace)
157 {
158 close (fd);
159 if ((fd = open (fname, 2)) == -1)
160 error ("Can't reopen file for writing.");
161 }
162
163 if (type != N_TEXT && type != N_DATA)
164 error ("address/symbol is not in text or data section.");
165
166 if (type == N_TEXT)
167 off = addr - N_TXTADDR(e) + N_TXTOFF(e);
168 else
169 off = addr - N_DATADDR(e) + N_DATOFF(e);
170
171 if (lseek (fd, off, 0) == -1)
172 error ("lseek");
173
174 /* not beautiful, but works on big and little endian machines */
175 switch (size)
176 {
177 case 1:
178 if (read (fd, &cval, 1) != 1)
179 error ("cread");
180 lval = cval;
181 break;
182
183 case 2:
184 if (read (fd, &sval, 2) != 2)
185 error ("sread");
186 lval = sval;
187 break;
188
189 case 4:
190 if (read (fd, &lval, 4) != 4)
191 error ("lread");
192 break;
193 }
194
195
196 if (symbol)
197 printf ("%s(0x%x): %d (0x%x)\n", symbol, addr, lval, lval);
198 else
199 printf ("0x%x: %d (0x%x)\n", addr, lval, lval);
200
201 if (do_replace)
202 {
203 if (lseek (fd, off, 0) == -1)
204 error ("write-lseek");
205 switch (size)
206 {
207 case 1:
208 cval = replace;
209 if (cval != replace)
210 error ("byte-value overflow.");
211 if (write (fd, &cval, 1) != 1)
212 error ("cwrite");
213 break;
214
215 case 2:
216 sval = replace;
217 if (sval != replace)
218 error ("word-value overflow.");
219 if (write (fd, &sval, 2) != 2)
220 error ("swrite");
221 break;
222
223 case 4:
224 if (write (fd, &replace, 4) != 4)
225 error ("lwrite");
226 break;
227 }
228 }
229
230 close (fd);
231 }
232
233
234
235 void error (str)
236 char *str;
237 {
238 fprintf (stderr, "%s\n", str);
239 exit (1);
240 }
241