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