binpatch.c revision 1.4.80.1 1 1.4.80.1 matt /* $NetBSD: binpatch.c,v 1.4.80.1 2010/04/21 00:33:52 matt Exp $ */
2 1.1 leo
3 1.4.80.1 matt /*-
4 1.4.80.1 matt * Copyright (c) 2009 Izumi Tsutsui. All rights reserved.
5 1.1 leo *
6 1.1 leo * Redistribution and use in source and binary forms, with or without
7 1.1 leo * modification, are permitted provided that the following conditions
8 1.1 leo * are met:
9 1.1 leo * 1. Redistributions of source code must retain the above copyright
10 1.1 leo * notice, this list of conditions and the following disclaimer.
11 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 leo * notice, this list of conditions and the following disclaimer in the
13 1.1 leo * documentation and/or other materials provided with the distribution.
14 1.1 leo *
15 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 1.1 leo */
26 1.1 leo
27 1.4.80.1 matt /*
28 1.4.80.1 matt * Copyright (c) 1996 Christopher G. Demetriou
29 1.4.80.1 matt * All rights reserved.
30 1.4.80.1 matt *
31 1.4.80.1 matt * Redistribution and use in source and binary forms, with or without
32 1.4.80.1 matt * modification, are permitted provided that the following conditions
33 1.4.80.1 matt * are met:
34 1.4.80.1 matt * 1. Redistributions of source code must retain the above copyright
35 1.4.80.1 matt * notice, this list of conditions and the following disclaimer.
36 1.4.80.1 matt * 2. Redistributions in binary form must reproduce the above copyright
37 1.4.80.1 matt * notice, this list of conditions and the following disclaimer in the
38 1.4.80.1 matt * documentation and/or other materials provided with the distribution.
39 1.4.80.1 matt * 3. The name of the author may not be used to endorse or promote products
40 1.4.80.1 matt * derived from this software without specific prior written permission.
41 1.4.80.1 matt *
42 1.4.80.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 1.4.80.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 1.4.80.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 1.4.80.1 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46 1.4.80.1 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 1.4.80.1 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 1.4.80.1 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 1.4.80.1 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 1.4.80.1 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 1.4.80.1 matt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 1.4.80.1 matt *
53 1.4.80.1 matt * <<Id: LICENSE_GC,v 1.1 2001/10/01 23:24:05 cgd Exp>>
54 1.4.80.1 matt */
55 1.4.80.1 matt
56 1.4.80.1 matt #include <sys/cdefs.h>
57 1.4.80.1 matt #ifndef lint
58 1.4.80.1 matt __COPYRIGHT("@(#) Copyright (c) 1996\
59 1.4.80.1 matt Christopher G. Demetriou. All rights reserved.");
60 1.4.80.1 matt #endif /* not lint */
61 1.4.80.1 matt
62 1.4.80.1 matt #ifndef lint
63 1.4.80.1 matt __RCSID("$NetBSD: binpatch.c,v 1.4.80.1 2010/04/21 00:33:52 matt Exp $");
64 1.4.80.1 matt #endif /* not lint */
65 1.4.80.1 matt
66 1.1 leo #include <sys/types.h>
67 1.4.80.1 matt #include <sys/mman.h>
68 1.4.80.1 matt #include <sys/stat.h>
69 1.4.80.1 matt #include <sys/inttypes.h>
70 1.4.80.1 matt
71 1.4.80.1 matt #include <err.h>
72 1.4.80.1 matt #include <fcntl.h>
73 1.4.80.1 matt #include <limits.h>
74 1.4.80.1 matt #include <nlist.h>
75 1.1 leo #include <stdio.h>
76 1.4 mhitch #include <stdlib.h>
77 1.4.80.1 matt #include <stdbool.h>
78 1.4.80.1 matt #include <unistd.h>
79 1.1 leo
80 1.4.80.1 matt #include "extern.h"
81 1.1 leo
82 1.4.80.1 matt int main(int, char *[]);
83 1.4.80.1 matt static void usage(void) __dead;
84 1.1 leo
85 1.4.80.1 matt bool replace, verbose;
86 1.4.80.1 matt u_long addr, offset;
87 1.4.80.1 matt char *symbol;
88 1.4.80.1 matt size_t size;
89 1.4.80.1 matt uint64_t val;
90 1.4.80.1 matt
91 1.4.80.1 matt #ifdef NLIST_AOUT
92 1.4.80.1 matt /*
93 1.4.80.1 matt * Since we can't get the text address from an a.out executable, we
94 1.4.80.1 matt * need to be able to specify it. Note: there's no way to test to
95 1.4.80.1 matt * see if the user entered a valid address!
96 1.4.80.1 matt */
97 1.4.80.1 matt int T_flag_specified; /* the -T flag was specified */
98 1.4.80.1 matt u_long text_start; /* Start of kernel text */
99 1.4.80.1 matt #endif /* NLIST_AOUT */
100 1.4.80.1 matt
101 1.4.80.1 matt static const struct {
102 1.4.80.1 matt const char *name;
103 1.4.80.1 matt int (*check)(const char *, size_t);
104 1.4.80.1 matt int (*findoff)(const char *, size_t, u_long, size_t *);
105 1.4.80.1 matt } exec_formats[] = {
106 1.4.80.1 matt #ifdef NLIST_AOUT
107 1.4.80.1 matt { "a.out", check_aout, findoff_aout, },
108 1.4.80.1 matt #endif
109 1.4.80.1 matt #ifdef NLIST_ECOFF
110 1.4.80.1 matt { "ECOFF", check_ecoff, findoff_ecoff, },
111 1.4.80.1 matt #endif
112 1.4.80.1 matt #ifdef NLIST_ELF32
113 1.4.80.1 matt { "ELF32", check_elf32, findoff_elf32, },
114 1.4.80.1 matt #endif
115 1.4.80.1 matt #ifdef NLIST_ELF64
116 1.4.80.1 matt { "ELF64", check_elf64, findoff_elf64, },
117 1.4.80.1 matt #endif
118 1.4.80.1 matt #ifdef NLIST_COFF
119 1.4.80.1 matt { "COFF", check_coff, findoff_coff, },
120 1.4.80.1 matt #endif
121 1.4.80.1 matt };
122 1.1 leo
123 1.1 leo
124 1.1 leo int
125 1.4.80.1 matt main(int argc, char *argv[])
126 1.1 leo {
127 1.4.80.1 matt const char *fname;
128 1.4.80.1 matt struct stat sb;
129 1.4.80.1 matt struct nlist nl[2];
130 1.4.80.1 matt char *mappedfile;
131 1.4.80.1 matt size_t valoff;
132 1.4.80.1 matt void *valp;
133 1.4.80.1 matt uint8_t uval8;
134 1.4.80.1 matt int8_t sval8;
135 1.4.80.1 matt uint16_t uval16;
136 1.4.80.1 matt int16_t sval16;
137 1.4.80.1 matt uint32_t uval32;
138 1.4.80.1 matt int32_t sval32;
139 1.4.80.1 matt uint64_t uval64;
140 1.4.80.1 matt int64_t sval64;
141 1.4.80.1 matt int ch, fd, rv, i, n;
142 1.4.80.1 matt
143 1.4.80.1 matt setprogname(argv[0]);
144 1.4.80.1 matt
145 1.4.80.1 matt while ((ch = getopt(argc, argv, "bwldT:a:s:o:r:v")) != -1)
146 1.4.80.1 matt switch (ch) {
147 1.4.80.1 matt case 'b':
148 1.4.80.1 matt size = sizeof(uint8_t);
149 1.4.80.1 matt break;
150 1.4.80.1 matt case 'w':
151 1.4.80.1 matt size = sizeof(uint16_t);
152 1.4.80.1 matt break;
153 1.4.80.1 matt case 'l':
154 1.4.80.1 matt size = sizeof(uint32_t);
155 1.4.80.1 matt break;
156 1.4.80.1 matt case 'd':
157 1.4.80.1 matt size = sizeof(uint64_t);
158 1.4.80.1 matt break;
159 1.4.80.1 matt case 'a':
160 1.4.80.1 matt if (addr != 0 || symbol != NULL)
161 1.4.80.1 matt errx(EXIT_FAILURE,
162 1.4.80.1 matt "only one address/symbol allowed");
163 1.4.80.1 matt addr = strtoul(optarg, NULL, 0);
164 1.4.80.1 matt break;
165 1.4.80.1 matt case 's':
166 1.4.80.1 matt if (addr != 0 || symbol != NULL)
167 1.4.80.1 matt errx(EXIT_FAILURE,
168 1.4.80.1 matt "only one address/symbol allowed");
169 1.4.80.1 matt symbol = optarg;
170 1.4.80.1 matt break;
171 1.4.80.1 matt case 'o':
172 1.4.80.1 matt if (offset != 0)
173 1.4.80.1 matt err(EXIT_FAILURE,
174 1.4.80.1 matt "only one offset allowed");
175 1.4.80.1 matt offset = strtoul(optarg, NULL, 0);
176 1.4.80.1 matt break;
177 1.4.80.1 matt case 'r':
178 1.4.80.1 matt replace = true;
179 1.4.80.1 matt val = strtoull(optarg, NULL, 0);
180 1.4.80.1 matt break;
181 1.4.80.1 matt case 'v':
182 1.4.80.1 matt verbose = true;
183 1.4.80.1 matt break;
184 1.4.80.1 matt case 'T':
185 1.4.80.1 matt #ifdef NLIST_AOUT
186 1.4.80.1 matt T_flag_specified = 1;
187 1.4.80.1 matt text_start = strtoul(optarg, NULL, 0);
188 1.4.80.1 matt break;
189 1.4.80.1 matt #else
190 1.4.80.1 matt fprintf(stderr, "%s: unknown option -- %c\n",
191 1.4.80.1 matt getprogname(), (char)ch);
192 1.4.80.1 matt /*FALLTHROUGH*/
193 1.4.80.1 matt #endif /* NLIST_AOUT */
194 1.4.80.1 matt case '?':
195 1.4.80.1 matt default:
196 1.4.80.1 matt usage();
197 1.4.80.1 matt }
198 1.4.80.1 matt argc -= optind;
199 1.4.80.1 matt argv += optind;
200 1.4.80.1 matt
201 1.4.80.1 matt if (argc != 1)
202 1.4.80.1 matt usage();
203 1.4.80.1 matt
204 1.4.80.1 matt if (addr == 0 && symbol == NULL) {
205 1.4.80.1 matt warnx("no address or symbol specified");
206 1.4.80.1 matt usage();
207 1.4.80.1 matt }
208 1.4.80.1 matt
209 1.4.80.1 matt if (size == 0)
210 1.4.80.1 matt size = sizeof(uint32_t); /* default to int */
211 1.4.80.1 matt
212 1.4.80.1 matt fname = argv[0];
213 1.4.80.1 matt
214 1.4.80.1 matt if ((fd = open(fname, replace ? O_RDWR : O_RDONLY, 0)) == -1)
215 1.4.80.1 matt err(EXIT_FAILURE, "open %s", fname);
216 1.4.80.1 matt
217 1.4.80.1 matt if (symbol != NULL) {
218 1.4.80.1 matt nl[0].n_name = symbol;
219 1.4.80.1 matt nl[1].n_name = NULL;
220 1.4.80.1 matt if ((rv = __fdnlist(fd, nl)) != 0)
221 1.4.80.1 matt errx(EXIT_FAILURE, "could not find symbol %s in %s",
222 1.4.80.1 matt symbol, fname);
223 1.4.80.1 matt addr = nl[0].n_value;
224 1.4.80.1 matt if (verbose)
225 1.4.80.1 matt fprintf(stderr, "got symbol address 0x%lx from %s\n",
226 1.4.80.1 matt addr, fname);
227 1.4.80.1 matt }
228 1.1 leo
229 1.4.80.1 matt addr += offset * size;
230 1.4.80.1 matt
231 1.4.80.1 matt if (fstat(fd, &sb) == -1)
232 1.4.80.1 matt err(EXIT_FAILURE, "fstat %s", fname);
233 1.4.80.1 matt if (sb.st_size != (ssize_t)sb.st_size)
234 1.4.80.1 matt errx(EXIT_FAILURE, "%s too big to map", fname);
235 1.4.80.1 matt
236 1.4.80.1 matt if ((mappedfile = mmap(NULL, sb.st_size,
237 1.4.80.1 matt replace ? PROT_READ | PROT_WRITE : PROT_READ,
238 1.4.80.1 matt MAP_FILE | MAP_SHARED, fd, 0)) == (char *)-1)
239 1.4.80.1 matt err(EXIT_FAILURE, "mmap %s", fname);
240 1.4.80.1 matt if (verbose)
241 1.4.80.1 matt fprintf(stderr, "mapped %s\n", fname);
242 1.4.80.1 matt
243 1.4.80.1 matt n = __arraycount(exec_formats);
244 1.4.80.1 matt for (i = 0; i < n; i++) {
245 1.4.80.1 matt if ((*exec_formats[i].check)(mappedfile, sb.st_size) == 0)
246 1.4.80.1 matt break;
247 1.4.80.1 matt }
248 1.4.80.1 matt if (i == n)
249 1.4.80.1 matt errx(EXIT_FAILURE, "%s: unknown executable format", fname);
250 1.4.80.1 matt
251 1.4.80.1 matt if (verbose) {
252 1.4.80.1 matt fprintf(stderr, "%s is an %s binary\n", fname,
253 1.4.80.1 matt exec_formats[i].name);
254 1.4.80.1 matt #ifdef NLIST_AOUT
255 1.4.80.1 matt if (T_flag_specified)
256 1.4.80.1 matt fprintf(stderr, "kernel text loads at 0x%lx\n",
257 1.4.80.1 matt text_start);
258 1.4.80.1 matt #endif
259 1.4.80.1 matt }
260 1.4.80.1 matt
261 1.4.80.1 matt if ((*exec_formats[i].findoff)(mappedfile, sb.st_size,
262 1.4.80.1 matt addr, &valoff) != 0)
263 1.4.80.1 matt errx(EXIT_FAILURE, "couldn't find file offset for %s in %s",
264 1.4.80.1 matt symbol != NULL ? nl[0].n_name : "address" , fname);
265 1.4.80.1 matt
266 1.4.80.1 matt valp = mappedfile + valoff;
267 1.4.80.1 matt
268 1.4.80.1 matt if (symbol)
269 1.4.80.1 matt printf("%s(0x%lx): ", symbol, addr);
270 1.1 leo else
271 1.4.80.1 matt printf("0x%lx: ", addr);
272 1.4.80.1 matt
273 1.4.80.1 matt switch (size) {
274 1.4.80.1 matt case sizeof(uint8_t):
275 1.4.80.1 matt uval8 = *(uint8_t *)valp;
276 1.4.80.1 matt sval8 = *(int8_t *)valp;
277 1.4.80.1 matt printf("0x%02" PRIx8 " (%" PRIu8, uval8, uval8);
278 1.4.80.1 matt if (sval8 < 0)
279 1.4.80.1 matt printf("/%" PRId8, sval8);
280 1.4.80.1 matt printf(")");
281 1.4.80.1 matt break;
282 1.4.80.1 matt case sizeof(uint16_t):
283 1.4.80.1 matt uval16 = *(uint16_t *)valp;
284 1.4.80.1 matt sval16 = *(int16_t *)valp;
285 1.4.80.1 matt printf("0x%04" PRIx16 " (%" PRIu16, uval16, uval16);
286 1.4.80.1 matt if (sval16 < 0)
287 1.4.80.1 matt printf("/%" PRId16, sval16);
288 1.4.80.1 matt printf(")");
289 1.4.80.1 matt break;
290 1.4.80.1 matt case sizeof(uint32_t):
291 1.4.80.1 matt uval32 = *(uint32_t *)valp;
292 1.4.80.1 matt sval32 = *(int32_t *)valp;
293 1.4.80.1 matt printf("0x%08" PRIx32 " (%" PRIu32, uval32, uval32);
294 1.4.80.1 matt if (sval32 < 0)
295 1.4.80.1 matt printf("/%" PRId32, sval32);
296 1.4.80.1 matt printf(")");
297 1.4.80.1 matt break;
298 1.4.80.1 matt case sizeof(uint64_t):
299 1.4.80.1 matt uval64 = *(uint64_t *)valp;
300 1.4.80.1 matt sval64 = *(int64_t *)valp;
301 1.4.80.1 matt printf("0x%016" PRIx64 " (%" PRIu64, uval64, uval64);
302 1.4.80.1 matt if (sval64 < 0)
303 1.4.80.1 matt printf("/%" PRId64, sval64);
304 1.4.80.1 matt printf(")");
305 1.4.80.1 matt break;
306 1.1 leo }
307 1.4.80.1 matt printf(", at offset %#lx in %s\n", (unsigned long)valoff, fname);
308 1.1 leo
309 1.4.80.1 matt if (!replace)
310 1.4.80.1 matt goto done;
311 1.1 leo
312 1.4.80.1 matt printf("new value: ");
313 1.1 leo
314 1.4.80.1 matt switch (size) {
315 1.4.80.1 matt case sizeof(uint8_t):
316 1.4.80.1 matt uval8 = (uint8_t)val;
317 1.4.80.1 matt sval8 = (int8_t)val;
318 1.4.80.1 matt printf("0x%02" PRIx8 " (%" PRIu8, uval8, uval8);
319 1.4.80.1 matt if (sval8 < 0)
320 1.4.80.1 matt printf("/%" PRId8, sval8);
321 1.4.80.1 matt printf(")");
322 1.4.80.1 matt *(uint8_t *)valp = uval8;
323 1.4.80.1 matt break;
324 1.4.80.1 matt case sizeof(uint16_t):
325 1.4.80.1 matt uval16 = (uint16_t)val;
326 1.4.80.1 matt sval16 = (int16_t)val;
327 1.4.80.1 matt printf("0x%04" PRIx16 " (%" PRIu16, uval16, uval16);
328 1.4.80.1 matt if (sval16 < 0)
329 1.4.80.1 matt printf("/%" PRId16, sval16);
330 1.4.80.1 matt printf(")");
331 1.4.80.1 matt *(uint16_t *)valp = uval16;
332 1.4.80.1 matt break;
333 1.4.80.1 matt case sizeof(uint32_t):
334 1.4.80.1 matt uval32 = (uint32_t)val;
335 1.4.80.1 matt sval32 = (int32_t)val;
336 1.4.80.1 matt printf("0x%08" PRIx32 " (%" PRIu32, uval32, uval32);
337 1.4.80.1 matt if (sval32 < 0)
338 1.4.80.1 matt printf("/%" PRId32, sval32);
339 1.4.80.1 matt printf(")");
340 1.4.80.1 matt *(uint32_t *)valp = uval32;
341 1.4.80.1 matt break;
342 1.4.80.1 matt case sizeof(uint64_t):
343 1.4.80.1 matt uval64 = (uint64_t)val;
344 1.4.80.1 matt sval64 = (int64_t)val;
345 1.4.80.1 matt printf("0x%016" PRIx64 " (%" PRIu64, uval64, uval64);
346 1.4.80.1 matt if (sval64 < 0)
347 1.4.80.1 matt printf("/%" PRId64, sval64);
348 1.4.80.1 matt printf(")");
349 1.4.80.1 matt *(uint64_t *)valp = uval64;
350 1.4.80.1 matt break;
351 1.4.80.1 matt }
352 1.4.80.1 matt printf("\n");
353 1.4.80.1 matt
354 1.4.80.1 matt done:
355 1.4.80.1 matt munmap(mappedfile, sb.st_size);
356 1.4.80.1 matt close(fd);
357 1.4.80.1 matt
358 1.4.80.1 matt if (verbose)
359 1.4.80.1 matt fprintf(stderr, "exiting\n");
360 1.4.80.1 matt exit(EXIT_SUCCESS);
361 1.4.80.1 matt }
362 1.1 leo
363 1.4.80.1 matt static void
364 1.4.80.1 matt usage(void)
365 1.1 leo {
366 1.4.80.1 matt
367 1.4.80.1 matt fprintf(stderr,
368 1.4.80.1 matt "usage: %s [-b|-w|-l|-d] [-a address | -s symbol] [-o offset]\n"
369 1.4.80.1 matt " [-r value] "
370 1.4.80.1 matt #ifdef NLIST_AOUT
371 1.4.80.1 matt "[-T text_start] "
372 1.4.80.1 matt #endif
373 1.4.80.1 matt "[-v] binary\n", getprogname());
374 1.4.80.1 matt exit(EXIT_FAILURE);
375 1.1 leo }
376