.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * recordmcount.c: construct a table of the locations of calls to 'mcount' |
---|
3 | 4 | * so that ftrace can find them quickly. |
---|
4 | 5 | * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved. |
---|
5 | | - * Licensed under the GNU General Public License, version 2 (GPLv2). |
---|
6 | 6 | * |
---|
7 | 7 | * Restructured to fit Linux format, as well as other updates: |
---|
8 | 8 | * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc. |
---|
.. | .. |
---|
27 | 27 | #include <getopt.h> |
---|
28 | 28 | #include <elf.h> |
---|
29 | 29 | #include <fcntl.h> |
---|
30 | | -#include <setjmp.h> |
---|
31 | 30 | #include <stdio.h> |
---|
32 | 31 | #include <stdlib.h> |
---|
33 | 32 | #include <string.h> |
---|
.. | .. |
---|
43 | 42 | #define R_ARM_THM_CALL 10 |
---|
44 | 43 | #define R_ARM_CALL 28 |
---|
45 | 44 | |
---|
| 45 | +#define R_AARCH64_CALL26 283 |
---|
| 46 | + |
---|
46 | 47 | static int fd_map; /* File descriptor for file being modified. */ |
---|
47 | 48 | static int mmap_failed; /* Boolean flag. */ |
---|
48 | 49 | static char gpfx; /* prefix for global symbol name (sometimes '_') */ |
---|
49 | 50 | static struct stat sb; /* Remember .st_size, etc. */ |
---|
50 | | -static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */ |
---|
51 | 51 | static const char *altmcount; /* alternate mcount symbol name */ |
---|
52 | 52 | static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */ |
---|
53 | 53 | static void *file_map; /* pointer of the mapped file */ |
---|
54 | 54 | static void *file_end; /* pointer to the end of the mapped file */ |
---|
55 | 55 | static int file_updated; /* flag to state file was changed */ |
---|
56 | 56 | static void *file_ptr; /* current file pointer location */ |
---|
| 57 | + |
---|
57 | 58 | static void *file_append; /* added to the end of the file */ |
---|
58 | 59 | static size_t file_append_size; /* how much is added to end of file */ |
---|
59 | 60 | |
---|
60 | | -/* setjmp() return values */ |
---|
61 | | -enum { |
---|
62 | | - SJ_SETJMP = 0, /* hardwired first return */ |
---|
63 | | - SJ_FAIL, |
---|
64 | | - SJ_SUCCEED |
---|
65 | | -}; |
---|
66 | | - |
---|
67 | 61 | /* Per-file resource cleanup when multiple files. */ |
---|
68 | | -static void |
---|
69 | | -cleanup(void) |
---|
| 62 | +static void file_append_cleanup(void) |
---|
70 | 63 | { |
---|
71 | | - if (!mmap_failed) |
---|
72 | | - munmap(file_map, sb.st_size); |
---|
73 | | - else |
---|
74 | | - free(file_map); |
---|
75 | | - file_map = NULL; |
---|
76 | 64 | free(file_append); |
---|
77 | 65 | file_append = NULL; |
---|
78 | 66 | file_append_size = 0; |
---|
79 | 67 | file_updated = 0; |
---|
80 | 68 | } |
---|
81 | 69 | |
---|
82 | | -static void __attribute__((noreturn)) |
---|
83 | | -fail_file(void) |
---|
| 70 | +static void mmap_cleanup(void) |
---|
84 | 71 | { |
---|
85 | | - cleanup(); |
---|
86 | | - longjmp(jmpenv, SJ_FAIL); |
---|
| 72 | + if (!mmap_failed) |
---|
| 73 | + munmap(file_map, sb.st_size); |
---|
| 74 | + else |
---|
| 75 | + free(file_map); |
---|
| 76 | + file_map = NULL; |
---|
87 | 77 | } |
---|
88 | 78 | |
---|
89 | | -static void __attribute__((noreturn)) |
---|
90 | | -succeed_file(void) |
---|
91 | | -{ |
---|
92 | | - cleanup(); |
---|
93 | | - longjmp(jmpenv, SJ_SUCCEED); |
---|
94 | | -} |
---|
| 79 | +/* ulseek, uwrite, ...: Check return value for errors. */ |
---|
95 | 80 | |
---|
96 | | -/* ulseek, uread, ...: Check return value for errors. */ |
---|
97 | | - |
---|
98 | | -static off_t |
---|
99 | | -ulseek(int const fd, off_t const offset, int const whence) |
---|
| 81 | +static off_t ulseek(off_t const offset, int const whence) |
---|
100 | 82 | { |
---|
101 | 83 | switch (whence) { |
---|
102 | 84 | case SEEK_SET: |
---|
.. | .. |
---|
111 | 93 | } |
---|
112 | 94 | if (file_ptr < file_map) { |
---|
113 | 95 | fprintf(stderr, "lseek: seek before file\n"); |
---|
114 | | - fail_file(); |
---|
| 96 | + return -1; |
---|
115 | 97 | } |
---|
116 | 98 | return file_ptr - file_map; |
---|
117 | 99 | } |
---|
118 | 100 | |
---|
119 | | -static size_t |
---|
120 | | -uread(int const fd, void *const buf, size_t const count) |
---|
121 | | -{ |
---|
122 | | - size_t const n = read(fd, buf, count); |
---|
123 | | - if (n != count) { |
---|
124 | | - perror("read"); |
---|
125 | | - fail_file(); |
---|
126 | | - } |
---|
127 | | - return n; |
---|
128 | | -} |
---|
129 | | - |
---|
130 | | -static size_t |
---|
131 | | -uwrite(int const fd, void const *const buf, size_t const count) |
---|
| 101 | +static ssize_t uwrite(void const *const buf, size_t const count) |
---|
132 | 102 | { |
---|
133 | 103 | size_t cnt = count; |
---|
134 | 104 | off_t idx = 0; |
---|
.. | .. |
---|
144 | 114 | } |
---|
145 | 115 | if (!file_append) { |
---|
146 | 116 | perror("write"); |
---|
147 | | - fail_file(); |
---|
| 117 | + file_append_cleanup(); |
---|
| 118 | + mmap_cleanup(); |
---|
| 119 | + return -1; |
---|
148 | 120 | } |
---|
149 | 121 | if (file_ptr < file_end) { |
---|
150 | 122 | cnt = file_end - file_ptr; |
---|
.. | .. |
---|
164 | 136 | return count; |
---|
165 | 137 | } |
---|
166 | 138 | |
---|
167 | | -static void * |
---|
168 | | -umalloc(size_t size) |
---|
| 139 | +static void * umalloc(size_t size) |
---|
169 | 140 | { |
---|
170 | 141 | void *const addr = malloc(size); |
---|
171 | 142 | if (addr == 0) { |
---|
172 | 143 | fprintf(stderr, "malloc failed: %zu bytes\n", size); |
---|
173 | | - fail_file(); |
---|
| 144 | + file_append_cleanup(); |
---|
| 145 | + mmap_cleanup(); |
---|
| 146 | + return NULL; |
---|
174 | 147 | } |
---|
175 | 148 | return addr; |
---|
176 | 149 | } |
---|
| 150 | + |
---|
| 151 | +/* |
---|
| 152 | + * Get the whole file as a programming convenience in order to avoid |
---|
| 153 | + * malloc+lseek+read+free of many pieces. If successful, then mmap |
---|
| 154 | + * avoids copying unused pieces; else just read the whole file. |
---|
| 155 | + * Open for both read and write; new info will be appended to the file. |
---|
| 156 | + * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr |
---|
| 157 | + * do not propagate to the file until an explicit overwrite at the last. |
---|
| 158 | + * This preserves most aspects of consistency (all except .st_size) |
---|
| 159 | + * for simultaneous readers of the file while we are appending to it. |
---|
| 160 | + * However, multiple writers still are bad. We choose not to use |
---|
| 161 | + * locking because it is expensive and the use case of kernel build |
---|
| 162 | + * makes multiple writers unlikely. |
---|
| 163 | + */ |
---|
| 164 | +static void *mmap_file(char const *fname) |
---|
| 165 | +{ |
---|
| 166 | + /* Avoid problems if early cleanup() */ |
---|
| 167 | + fd_map = -1; |
---|
| 168 | + mmap_failed = 1; |
---|
| 169 | + file_map = NULL; |
---|
| 170 | + file_ptr = NULL; |
---|
| 171 | + file_updated = 0; |
---|
| 172 | + sb.st_size = 0; |
---|
| 173 | + |
---|
| 174 | + fd_map = open(fname, O_RDONLY); |
---|
| 175 | + if (fd_map < 0) { |
---|
| 176 | + perror(fname); |
---|
| 177 | + return NULL; |
---|
| 178 | + } |
---|
| 179 | + if (fstat(fd_map, &sb) < 0) { |
---|
| 180 | + perror(fname); |
---|
| 181 | + goto out; |
---|
| 182 | + } |
---|
| 183 | + if (!S_ISREG(sb.st_mode)) { |
---|
| 184 | + fprintf(stderr, "not a regular file: %s\n", fname); |
---|
| 185 | + goto out; |
---|
| 186 | + } |
---|
| 187 | + file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, |
---|
| 188 | + fd_map, 0); |
---|
| 189 | + if (file_map == MAP_FAILED) { |
---|
| 190 | + mmap_failed = 1; |
---|
| 191 | + file_map = umalloc(sb.st_size); |
---|
| 192 | + if (!file_map) { |
---|
| 193 | + perror(fname); |
---|
| 194 | + goto out; |
---|
| 195 | + } |
---|
| 196 | + if (read(fd_map, file_map, sb.st_size) != sb.st_size) { |
---|
| 197 | + perror(fname); |
---|
| 198 | + free(file_map); |
---|
| 199 | + file_map = NULL; |
---|
| 200 | + goto out; |
---|
| 201 | + } |
---|
| 202 | + } else |
---|
| 203 | + mmap_failed = 0; |
---|
| 204 | +out: |
---|
| 205 | + close(fd_map); |
---|
| 206 | + fd_map = -1; |
---|
| 207 | + |
---|
| 208 | + file_end = file_map + sb.st_size; |
---|
| 209 | + |
---|
| 210 | + return file_map; |
---|
| 211 | +} |
---|
| 212 | + |
---|
177 | 213 | |
---|
178 | 214 | static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 }; |
---|
179 | 215 | static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 }; |
---|
.. | .. |
---|
198 | 234 | return -1; |
---|
199 | 235 | |
---|
200 | 236 | /* convert to nop */ |
---|
201 | | - ulseek(fd_map, offset - 1, SEEK_SET); |
---|
202 | | - uwrite(fd_map, ideal_nop, 5); |
---|
| 237 | + if (ulseek(offset - 1, SEEK_SET) < 0) |
---|
| 238 | + return -1; |
---|
| 239 | + if (uwrite(ideal_nop, 5) < 0) |
---|
| 240 | + return -1; |
---|
203 | 241 | return 0; |
---|
204 | 242 | } |
---|
205 | 243 | |
---|
.. | .. |
---|
247 | 285 | return -1; |
---|
248 | 286 | |
---|
249 | 287 | /* Convert to nop */ |
---|
250 | | - ulseek(fd_map, off, SEEK_SET); |
---|
| 288 | + if (ulseek(off, SEEK_SET) < 0) |
---|
| 289 | + return -1; |
---|
251 | 290 | |
---|
252 | 291 | do { |
---|
253 | | - uwrite(fd_map, ideal_nop, nop_size); |
---|
| 292 | + if (uwrite(ideal_nop, nop_size) < 0) |
---|
| 293 | + return -1; |
---|
254 | 294 | } while (--cnt > 0); |
---|
255 | 295 | |
---|
256 | 296 | return 0; |
---|
.. | .. |
---|
267 | 307 | return -1; |
---|
268 | 308 | |
---|
269 | 309 | /* Convert to nop */ |
---|
270 | | - ulseek(fd_map, offset, SEEK_SET); |
---|
271 | | - uwrite(fd_map, ideal_nop, 4); |
---|
| 310 | + if (ulseek(offset, SEEK_SET) < 0) |
---|
| 311 | + return -1; |
---|
| 312 | + if (uwrite(ideal_nop, 4) < 0) |
---|
| 313 | + return -1; |
---|
272 | 314 | return 0; |
---|
273 | 315 | } |
---|
274 | 316 | |
---|
275 | | -/* |
---|
276 | | - * Get the whole file as a programming convenience in order to avoid |
---|
277 | | - * malloc+lseek+read+free of many pieces. If successful, then mmap |
---|
278 | | - * avoids copying unused pieces; else just read the whole file. |
---|
279 | | - * Open for both read and write; new info will be appended to the file. |
---|
280 | | - * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr |
---|
281 | | - * do not propagate to the file until an explicit overwrite at the last. |
---|
282 | | - * This preserves most aspects of consistency (all except .st_size) |
---|
283 | | - * for simultaneous readers of the file while we are appending to it. |
---|
284 | | - * However, multiple writers still are bad. We choose not to use |
---|
285 | | - * locking because it is expensive and the use case of kernel build |
---|
286 | | - * makes multiple writers unlikely. |
---|
287 | | - */ |
---|
288 | | -static void *mmap_file(char const *fname) |
---|
289 | | -{ |
---|
290 | | - fd_map = open(fname, O_RDONLY); |
---|
291 | | - if (fd_map < 0 || fstat(fd_map, &sb) < 0) { |
---|
292 | | - perror(fname); |
---|
293 | | - fail_file(); |
---|
294 | | - } |
---|
295 | | - if (!S_ISREG(sb.st_mode)) { |
---|
296 | | - fprintf(stderr, "not a regular file: %s\n", fname); |
---|
297 | | - fail_file(); |
---|
298 | | - } |
---|
299 | | - file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, |
---|
300 | | - fd_map, 0); |
---|
301 | | - mmap_failed = 0; |
---|
302 | | - if (file_map == MAP_FAILED) { |
---|
303 | | - mmap_failed = 1; |
---|
304 | | - file_map = umalloc(sb.st_size); |
---|
305 | | - uread(fd_map, file_map, sb.st_size); |
---|
306 | | - } |
---|
307 | | - close(fd_map); |
---|
308 | | - |
---|
309 | | - file_end = file_map + sb.st_size; |
---|
310 | | - |
---|
311 | | - return file_map; |
---|
312 | | -} |
---|
313 | | - |
---|
314 | | -static void write_file(const char *fname) |
---|
| 317 | +static int write_file(const char *fname) |
---|
315 | 318 | { |
---|
316 | 319 | char tmp_file[strlen(fname) + 4]; |
---|
317 | 320 | size_t n; |
---|
318 | 321 | |
---|
319 | 322 | if (!file_updated) |
---|
320 | | - return; |
---|
| 323 | + return 0; |
---|
321 | 324 | |
---|
322 | 325 | sprintf(tmp_file, "%s.rc", fname); |
---|
323 | 326 | |
---|
.. | .. |
---|
329 | 332 | fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode); |
---|
330 | 333 | if (fd_map < 0) { |
---|
331 | 334 | perror(fname); |
---|
332 | | - fail_file(); |
---|
| 335 | + return -1; |
---|
333 | 336 | } |
---|
334 | 337 | n = write(fd_map, file_map, sb.st_size); |
---|
335 | 338 | if (n != sb.st_size) { |
---|
336 | 339 | perror("write"); |
---|
337 | | - fail_file(); |
---|
| 340 | + close(fd_map); |
---|
| 341 | + return -1; |
---|
338 | 342 | } |
---|
339 | 343 | if (file_append_size) { |
---|
340 | 344 | n = write(fd_map, file_append, file_append_size); |
---|
341 | 345 | if (n != file_append_size) { |
---|
342 | 346 | perror("write"); |
---|
343 | | - fail_file(); |
---|
| 347 | + close(fd_map); |
---|
| 348 | + return -1; |
---|
344 | 349 | } |
---|
345 | 350 | } |
---|
346 | 351 | close(fd_map); |
---|
347 | 352 | if (rename(tmp_file, fname) < 0) { |
---|
348 | 353 | perror(fname); |
---|
349 | | - fail_file(); |
---|
| 354 | + return -1; |
---|
350 | 355 | } |
---|
| 356 | + return 0; |
---|
351 | 357 | } |
---|
352 | 358 | |
---|
353 | 359 | /* w8rev, w8nat, ...: Handle endianness. */ |
---|
.. | .. |
---|
398 | 404 | static uint32_t (*w2)(uint16_t); |
---|
399 | 405 | |
---|
400 | 406 | /* Names of the sections that could contain calls to mcount. */ |
---|
401 | | -static int |
---|
402 | | -is_mcounted_section_name(char const *const txtname) |
---|
| 407 | +static int is_mcounted_section_name(char const *const txtname) |
---|
403 | 408 | { |
---|
404 | 409 | return strncmp(".text", txtname, 5) == 0 || |
---|
405 | 410 | strcmp(".init.text", txtname) == 0 || |
---|
.. | .. |
---|
409 | 414 | strcmp(".irqentry.text", txtname) == 0 || |
---|
410 | 415 | strcmp(".softirqentry.text", txtname) == 0 || |
---|
411 | 416 | strcmp(".kprobes.text", txtname) == 0 || |
---|
412 | | - strcmp(".cpuidle.text", txtname) == 0 || |
---|
413 | | - (strncmp(".text.", txtname, 6) == 0 && |
---|
414 | | - strcmp(".text..ftrace", txtname) != 0); |
---|
| 417 | + strcmp(".cpuidle.text", txtname) == 0; |
---|
415 | 418 | } |
---|
| 419 | + |
---|
| 420 | +static char const *already_has_rel_mcount = "success"; /* our work here is done! */ |
---|
416 | 421 | |
---|
417 | 422 | /* 32 bit and 64 bit are very similar */ |
---|
418 | 423 | #include "recordmcount.h" |
---|
.. | .. |
---|
429 | 434 | } |
---|
430 | 435 | |
---|
431 | 436 | return 1; |
---|
| 437 | +} |
---|
| 438 | + |
---|
| 439 | +static int arm64_is_fake_mcount(Elf64_Rel const *rp) |
---|
| 440 | +{ |
---|
| 441 | + return ELF64_R_TYPE(w8(rp->r_info)) != R_AARCH64_CALL26; |
---|
432 | 442 | } |
---|
433 | 443 | |
---|
434 | 444 | /* 64-bit EM_MIPS has weird ELF64_Rela.r_info. |
---|
.. | .. |
---|
464 | 474 | }).r_info; |
---|
465 | 475 | } |
---|
466 | 476 | |
---|
467 | | -static void |
---|
468 | | -do_file(char const *const fname) |
---|
| 477 | +static int do_file(char const *const fname) |
---|
469 | 478 | { |
---|
470 | | - Elf32_Ehdr *const ehdr = mmap_file(fname); |
---|
471 | 479 | unsigned int reltype = 0; |
---|
| 480 | + Elf32_Ehdr *ehdr; |
---|
| 481 | + int rc = -1; |
---|
| 482 | + |
---|
| 483 | + ehdr = mmap_file(fname); |
---|
| 484 | + if (!ehdr) |
---|
| 485 | + goto out; |
---|
472 | 486 | |
---|
473 | 487 | w = w4nat; |
---|
474 | 488 | w2 = w2nat; |
---|
.. | .. |
---|
478 | 492 | default: |
---|
479 | 493 | fprintf(stderr, "unrecognized ELF data encoding %d: %s\n", |
---|
480 | 494 | ehdr->e_ident[EI_DATA], fname); |
---|
481 | | - fail_file(); |
---|
482 | | - break; |
---|
| 495 | + goto out; |
---|
483 | 496 | case ELFDATA2LSB: |
---|
484 | 497 | if (*(unsigned char const *)&endian != 1) { |
---|
485 | 498 | /* main() is big endian, file.o is little endian. */ |
---|
.. | .. |
---|
507 | 520 | push_bl_mcount_thumb = push_bl_mcount_thumb_be; |
---|
508 | 521 | break; |
---|
509 | 522 | } /* end switch */ |
---|
510 | | - if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 |
---|
511 | | - || w2(ehdr->e_type) != ET_REL |
---|
512 | | - || ehdr->e_ident[EI_VERSION] != EV_CURRENT) { |
---|
| 523 | + if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 || |
---|
| 524 | + w2(ehdr->e_type) != ET_REL || |
---|
| 525 | + ehdr->e_ident[EI_VERSION] != EV_CURRENT) { |
---|
513 | 526 | fprintf(stderr, "unrecognized ET_REL file %s\n", fname); |
---|
514 | | - fail_file(); |
---|
| 527 | + goto out; |
---|
515 | 528 | } |
---|
516 | 529 | |
---|
517 | | - gpfx = 0; |
---|
| 530 | + gpfx = '_'; |
---|
518 | 531 | switch (w2(ehdr->e_machine)) { |
---|
519 | 532 | default: |
---|
520 | 533 | fprintf(stderr, "unrecognized e_machine %u %s\n", |
---|
521 | 534 | w2(ehdr->e_machine), fname); |
---|
522 | | - fail_file(); |
---|
523 | | - break; |
---|
| 535 | + goto out; |
---|
524 | 536 | case EM_386: |
---|
525 | 537 | reltype = R_386_32; |
---|
526 | 538 | rel_type_nop = R_386_NONE; |
---|
527 | 539 | make_nop = make_nop_x86; |
---|
528 | 540 | ideal_nop = ideal_nop5_x86_32; |
---|
529 | 541 | mcount_adjust_32 = -1; |
---|
| 542 | + gpfx = 0; |
---|
530 | 543 | break; |
---|
531 | | - case EM_ARM: reltype = R_ARM_ABS32; |
---|
532 | | - altmcount = "__gnu_mcount_nc"; |
---|
533 | | - make_nop = make_nop_arm; |
---|
534 | | - rel_type_nop = R_ARM_NONE; |
---|
535 | | - is_fake_mcount32 = arm_is_fake_mcount; |
---|
536 | | - break; |
---|
| 544 | + case EM_ARM: |
---|
| 545 | + reltype = R_ARM_ABS32; |
---|
| 546 | + altmcount = "__gnu_mcount_nc"; |
---|
| 547 | + make_nop = make_nop_arm; |
---|
| 548 | + rel_type_nop = R_ARM_NONE; |
---|
| 549 | + is_fake_mcount32 = arm_is_fake_mcount; |
---|
| 550 | + gpfx = 0; |
---|
| 551 | + break; |
---|
537 | 552 | case EM_AARCH64: |
---|
538 | | - reltype = R_AARCH64_ABS64; |
---|
539 | | - make_nop = make_nop_arm64; |
---|
540 | | - rel_type_nop = R_AARCH64_NONE; |
---|
541 | | - ideal_nop = ideal_nop4_arm64; |
---|
542 | | - gpfx = '_'; |
---|
543 | | - break; |
---|
544 | | - case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break; |
---|
545 | | - case EM_MIPS: /* reltype: e_class */ gpfx = '_'; break; |
---|
546 | | - case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break; |
---|
547 | | - case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break; |
---|
548 | | - case EM_S390: /* reltype: e_class */ gpfx = '_'; break; |
---|
549 | | - case EM_SH: reltype = R_SH_DIR32; break; |
---|
550 | | - case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break; |
---|
| 553 | + reltype = R_AARCH64_ABS64; |
---|
| 554 | + make_nop = make_nop_arm64; |
---|
| 555 | + rel_type_nop = R_AARCH64_NONE; |
---|
| 556 | + ideal_nop = ideal_nop4_arm64; |
---|
| 557 | + is_fake_mcount64 = arm64_is_fake_mcount; |
---|
| 558 | + break; |
---|
| 559 | + case EM_IA_64: reltype = R_IA64_IMM64; break; |
---|
| 560 | + case EM_MIPS: /* reltype: e_class */ break; |
---|
| 561 | + case EM_PPC: reltype = R_PPC_ADDR32; break; |
---|
| 562 | + case EM_PPC64: reltype = R_PPC64_ADDR64; break; |
---|
| 563 | + case EM_S390: /* reltype: e_class */ break; |
---|
| 564 | + case EM_SH: reltype = R_SH_DIR32; gpfx = 0; break; |
---|
| 565 | + case EM_SPARCV9: reltype = R_SPARC_64; break; |
---|
551 | 566 | case EM_X86_64: |
---|
552 | 567 | make_nop = make_nop_x86; |
---|
553 | 568 | ideal_nop = ideal_nop5_x86_64; |
---|
554 | 569 | reltype = R_X86_64_64; |
---|
555 | 570 | rel_type_nop = R_X86_64_NONE; |
---|
556 | 571 | mcount_adjust_64 = -1; |
---|
| 572 | + gpfx = 0; |
---|
557 | 573 | break; |
---|
558 | 574 | } /* end switch */ |
---|
559 | 575 | |
---|
.. | .. |
---|
561 | 577 | default: |
---|
562 | 578 | fprintf(stderr, "unrecognized ELF class %d %s\n", |
---|
563 | 579 | ehdr->e_ident[EI_CLASS], fname); |
---|
564 | | - fail_file(); |
---|
565 | | - break; |
---|
| 580 | + goto out; |
---|
566 | 581 | case ELFCLASS32: |
---|
567 | 582 | if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr) |
---|
568 | 583 | || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) { |
---|
569 | 584 | fprintf(stderr, |
---|
570 | 585 | "unrecognized ET_REL file: %s\n", fname); |
---|
571 | | - fail_file(); |
---|
| 586 | + goto out; |
---|
572 | 587 | } |
---|
573 | 588 | if (w2(ehdr->e_machine) == EM_MIPS) { |
---|
574 | 589 | reltype = R_MIPS_32; |
---|
575 | 590 | is_fake_mcount32 = MIPS32_is_fake_mcount; |
---|
576 | 591 | } |
---|
577 | | - do32(ehdr, fname, reltype); |
---|
| 592 | + if (do32(ehdr, fname, reltype) < 0) |
---|
| 593 | + goto out; |
---|
578 | 594 | break; |
---|
579 | 595 | case ELFCLASS64: { |
---|
580 | 596 | Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr; |
---|
.. | .. |
---|
582 | 598 | || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) { |
---|
583 | 599 | fprintf(stderr, |
---|
584 | 600 | "unrecognized ET_REL file: %s\n", fname); |
---|
585 | | - fail_file(); |
---|
| 601 | + goto out; |
---|
586 | 602 | } |
---|
587 | 603 | if (w2(ghdr->e_machine) == EM_S390) { |
---|
588 | 604 | reltype = R_390_64; |
---|
.. | .. |
---|
594 | 610 | Elf64_r_info = MIPS64_r_info; |
---|
595 | 611 | is_fake_mcount64 = MIPS64_is_fake_mcount; |
---|
596 | 612 | } |
---|
597 | | - do64(ghdr, fname, reltype); |
---|
| 613 | + if (do64(ghdr, fname, reltype) < 0) |
---|
| 614 | + goto out; |
---|
598 | 615 | break; |
---|
599 | 616 | } |
---|
600 | 617 | } /* end switch */ |
---|
601 | 618 | |
---|
602 | | - write_file(fname); |
---|
603 | | - cleanup(); |
---|
| 619 | + rc = write_file(fname); |
---|
| 620 | +out: |
---|
| 621 | + file_append_cleanup(); |
---|
| 622 | + mmap_cleanup(); |
---|
| 623 | + return rc; |
---|
604 | 624 | } |
---|
605 | 625 | |
---|
606 | | -int |
---|
607 | | -main(int argc, char *argv[]) |
---|
| 626 | +int main(int argc, char *argv[]) |
---|
608 | 627 | { |
---|
609 | 628 | const char ftrace[] = "/ftrace.o"; |
---|
610 | 629 | int ftrace_size = sizeof(ftrace) - 1; |
---|
.. | .. |
---|
631 | 650 | /* Process each file in turn, allowing deep failure. */ |
---|
632 | 651 | for (i = optind; i < argc; i++) { |
---|
633 | 652 | char *file = argv[i]; |
---|
634 | | - int const sjval = setjmp(jmpenv); |
---|
635 | 653 | int len; |
---|
636 | 654 | |
---|
637 | 655 | /* |
---|
.. | .. |
---|
644 | 662 | strcmp(file + (len - ftrace_size), ftrace) == 0) |
---|
645 | 663 | continue; |
---|
646 | 664 | |
---|
647 | | - switch (sjval) { |
---|
648 | | - default: |
---|
649 | | - fprintf(stderr, "internal error: %s\n", file); |
---|
650 | | - exit(1); |
---|
651 | | - break; |
---|
652 | | - case SJ_SETJMP: /* normal sequence */ |
---|
653 | | - /* Avoid problems if early cleanup() */ |
---|
654 | | - fd_map = -1; |
---|
655 | | - mmap_failed = 1; |
---|
656 | | - file_map = NULL; |
---|
657 | | - file_ptr = NULL; |
---|
658 | | - file_updated = 0; |
---|
659 | | - do_file(file); |
---|
660 | | - break; |
---|
661 | | - case SJ_FAIL: /* error in do_file or below */ |
---|
| 665 | + if (do_file(file)) { |
---|
662 | 666 | fprintf(stderr, "%s: failed\n", file); |
---|
663 | 667 | ++n_error; |
---|
664 | | - break; |
---|
665 | | - case SJ_SUCCEED: /* premature success */ |
---|
666 | | - /* do nothing */ |
---|
667 | | - break; |
---|
668 | | - } /* end switch */ |
---|
| 668 | + } |
---|
669 | 669 | } |
---|
670 | 670 | return !!n_error; |
---|
671 | 671 | } |
---|