hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/scripts/recordmcount.c
....@@ -1,8 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * recordmcount.c: construct a table of the locations of calls to 'mcount'
34 * so that ftrace can find them quickly.
45 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
5
- * Licensed under the GNU General Public License, version 2 (GPLv2).
66 *
77 * Restructured to fit Linux format, as well as other updates:
88 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
....@@ -27,7 +27,6 @@
2727 #include <getopt.h>
2828 #include <elf.h>
2929 #include <fcntl.h>
30
-#include <setjmp.h>
3130 #include <stdio.h>
3231 #include <stdlib.h>
3332 #include <string.h>
....@@ -43,60 +42,43 @@
4342 #define R_ARM_THM_CALL 10
4443 #define R_ARM_CALL 28
4544
45
+#define R_AARCH64_CALL26 283
46
+
4647 static int fd_map; /* File descriptor for file being modified. */
4748 static int mmap_failed; /* Boolean flag. */
4849 static char gpfx; /* prefix for global symbol name (sometimes '_') */
4950 static struct stat sb; /* Remember .st_size, etc. */
50
-static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
5151 static const char *altmcount; /* alternate mcount symbol name */
5252 static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
5353 static void *file_map; /* pointer of the mapped file */
5454 static void *file_end; /* pointer to the end of the mapped file */
5555 static int file_updated; /* flag to state file was changed */
5656 static void *file_ptr; /* current file pointer location */
57
+
5758 static void *file_append; /* added to the end of the file */
5859 static size_t file_append_size; /* how much is added to end of file */
5960
60
-/* setjmp() return values */
61
-enum {
62
- SJ_SETJMP = 0, /* hardwired first return */
63
- SJ_FAIL,
64
- SJ_SUCCEED
65
-};
66
-
6761 /* Per-file resource cleanup when multiple files. */
68
-static void
69
-cleanup(void)
62
+static void file_append_cleanup(void)
7063 {
71
- if (!mmap_failed)
72
- munmap(file_map, sb.st_size);
73
- else
74
- free(file_map);
75
- file_map = NULL;
7664 free(file_append);
7765 file_append = NULL;
7866 file_append_size = 0;
7967 file_updated = 0;
8068 }
8169
82
-static void __attribute__((noreturn))
83
-fail_file(void)
70
+static void mmap_cleanup(void)
8471 {
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;
8777 }
8878
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. */
9580
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)
10082 {
10183 switch (whence) {
10284 case SEEK_SET:
....@@ -111,27 +93,16 @@
11193 }
11294 if (file_ptr < file_map) {
11395 fprintf(stderr, "lseek: seek before file\n");
114
- fail_file();
96
+ return -1;
11597 }
11698 return file_ptr - file_map;
11799 }
118100
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)
132102 {
133103 size_t cnt = count;
134104 off_t idx = 0;
105
+ void *p = NULL;
135106
136107 file_updated = 1;
137108
....@@ -139,12 +110,17 @@
139110 off_t aoffset = (file_ptr + count) - file_end;
140111
141112 if (aoffset > file_append_size) {
142
- file_append = realloc(file_append, aoffset);
113
+ p = realloc(file_append, aoffset);
114
+ if (!p)
115
+ free(file_append);
116
+ file_append = p;
143117 file_append_size = aoffset;
144118 }
145119 if (!file_append) {
146120 perror("write");
147
- fail_file();
121
+ file_append_cleanup();
122
+ mmap_cleanup();
123
+ return -1;
148124 }
149125 if (file_ptr < file_end) {
150126 cnt = file_end - file_ptr;
....@@ -164,16 +140,80 @@
164140 return count;
165141 }
166142
167
-static void *
168
-umalloc(size_t size)
143
+static void * umalloc(size_t size)
169144 {
170145 void *const addr = malloc(size);
171146 if (addr == 0) {
172147 fprintf(stderr, "malloc failed: %zu bytes\n", size);
173
- fail_file();
148
+ file_append_cleanup();
149
+ mmap_cleanup();
150
+ return NULL;
174151 }
175152 return addr;
176153 }
154
+
155
+/*
156
+ * Get the whole file as a programming convenience in order to avoid
157
+ * malloc+lseek+read+free of many pieces. If successful, then mmap
158
+ * avoids copying unused pieces; else just read the whole file.
159
+ * Open for both read and write; new info will be appended to the file.
160
+ * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
161
+ * do not propagate to the file until an explicit overwrite at the last.
162
+ * This preserves most aspects of consistency (all except .st_size)
163
+ * for simultaneous readers of the file while we are appending to it.
164
+ * However, multiple writers still are bad. We choose not to use
165
+ * locking because it is expensive and the use case of kernel build
166
+ * makes multiple writers unlikely.
167
+ */
168
+static void *mmap_file(char const *fname)
169
+{
170
+ /* Avoid problems if early cleanup() */
171
+ fd_map = -1;
172
+ mmap_failed = 1;
173
+ file_map = NULL;
174
+ file_ptr = NULL;
175
+ file_updated = 0;
176
+ sb.st_size = 0;
177
+
178
+ fd_map = open(fname, O_RDONLY);
179
+ if (fd_map < 0) {
180
+ perror(fname);
181
+ return NULL;
182
+ }
183
+ if (fstat(fd_map, &sb) < 0) {
184
+ perror(fname);
185
+ goto out;
186
+ }
187
+ if (!S_ISREG(sb.st_mode)) {
188
+ fprintf(stderr, "not a regular file: %s\n", fname);
189
+ goto out;
190
+ }
191
+ file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
192
+ fd_map, 0);
193
+ if (file_map == MAP_FAILED) {
194
+ mmap_failed = 1;
195
+ file_map = umalloc(sb.st_size);
196
+ if (!file_map) {
197
+ perror(fname);
198
+ goto out;
199
+ }
200
+ if (read(fd_map, file_map, sb.st_size) != sb.st_size) {
201
+ perror(fname);
202
+ free(file_map);
203
+ file_map = NULL;
204
+ goto out;
205
+ }
206
+ } else
207
+ mmap_failed = 0;
208
+out:
209
+ close(fd_map);
210
+ fd_map = -1;
211
+
212
+ file_end = file_map + sb.st_size;
213
+
214
+ return file_map;
215
+}
216
+
177217
178218 static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
179219 static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
....@@ -198,8 +238,10 @@
198238 return -1;
199239
200240 /* convert to nop */
201
- ulseek(fd_map, offset - 1, SEEK_SET);
202
- uwrite(fd_map, ideal_nop, 5);
241
+ if (ulseek(offset - 1, SEEK_SET) < 0)
242
+ return -1;
243
+ if (uwrite(ideal_nop, 5) < 0)
244
+ return -1;
203245 return 0;
204246 }
205247
....@@ -247,10 +289,12 @@
247289 return -1;
248290
249291 /* Convert to nop */
250
- ulseek(fd_map, off, SEEK_SET);
292
+ if (ulseek(off, SEEK_SET) < 0)
293
+ return -1;
251294
252295 do {
253
- uwrite(fd_map, ideal_nop, nop_size);
296
+ if (uwrite(ideal_nop, nop_size) < 0)
297
+ return -1;
254298 } while (--cnt > 0);
255299
256300 return 0;
....@@ -267,57 +311,20 @@
267311 return -1;
268312
269313 /* Convert to nop */
270
- ulseek(fd_map, offset, SEEK_SET);
271
- uwrite(fd_map, ideal_nop, 4);
314
+ if (ulseek(offset, SEEK_SET) < 0)
315
+ return -1;
316
+ if (uwrite(ideal_nop, 4) < 0)
317
+ return -1;
272318 return 0;
273319 }
274320
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)
321
+static int write_file(const char *fname)
315322 {
316323 char tmp_file[strlen(fname) + 4];
317324 size_t n;
318325
319326 if (!file_updated)
320
- return;
327
+ return 0;
321328
322329 sprintf(tmp_file, "%s.rc", fname);
323330
....@@ -329,25 +336,28 @@
329336 fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
330337 if (fd_map < 0) {
331338 perror(fname);
332
- fail_file();
339
+ return -1;
333340 }
334341 n = write(fd_map, file_map, sb.st_size);
335342 if (n != sb.st_size) {
336343 perror("write");
337
- fail_file();
344
+ close(fd_map);
345
+ return -1;
338346 }
339347 if (file_append_size) {
340348 n = write(fd_map, file_append, file_append_size);
341349 if (n != file_append_size) {
342350 perror("write");
343
- fail_file();
351
+ close(fd_map);
352
+ return -1;
344353 }
345354 }
346355 close(fd_map);
347356 if (rename(tmp_file, fname) < 0) {
348357 perror(fname);
349
- fail_file();
358
+ return -1;
350359 }
360
+ return 0;
351361 }
352362
353363 /* w8rev, w8nat, ...: Handle endianness. */
....@@ -398,8 +408,7 @@
398408 static uint32_t (*w2)(uint16_t);
399409
400410 /* Names of the sections that could contain calls to mcount. */
401
-static int
402
-is_mcounted_section_name(char const *const txtname)
411
+static int is_mcounted_section_name(char const *const txtname)
403412 {
404413 return strncmp(".text", txtname, 5) == 0 ||
405414 strcmp(".init.text", txtname) == 0 ||
....@@ -409,10 +418,10 @@
409418 strcmp(".irqentry.text", txtname) == 0 ||
410419 strcmp(".softirqentry.text", txtname) == 0 ||
411420 strcmp(".kprobes.text", txtname) == 0 ||
412
- strcmp(".cpuidle.text", txtname) == 0 ||
413
- (strncmp(".text.", txtname, 6) == 0 &&
414
- strcmp(".text..ftrace", txtname) != 0);
421
+ strcmp(".cpuidle.text", txtname) == 0;
415422 }
423
+
424
+static char const *already_has_rel_mcount = "success"; /* our work here is done! */
416425
417426 /* 32 bit and 64 bit are very similar */
418427 #include "recordmcount.h"
....@@ -429,6 +438,11 @@
429438 }
430439
431440 return 1;
441
+}
442
+
443
+static int arm64_is_fake_mcount(Elf64_Rel const *rp)
444
+{
445
+ return ELF64_R_TYPE(w8(rp->r_info)) != R_AARCH64_CALL26;
432446 }
433447
434448 /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
....@@ -464,11 +478,15 @@
464478 }).r_info;
465479 }
466480
467
-static void
468
-do_file(char const *const fname)
481
+static int do_file(char const *const fname)
469482 {
470
- Elf32_Ehdr *const ehdr = mmap_file(fname);
471483 unsigned int reltype = 0;
484
+ Elf32_Ehdr *ehdr;
485
+ int rc = -1;
486
+
487
+ ehdr = mmap_file(fname);
488
+ if (!ehdr)
489
+ goto out;
472490
473491 w = w4nat;
474492 w2 = w2nat;
....@@ -478,8 +496,7 @@
478496 default:
479497 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
480498 ehdr->e_ident[EI_DATA], fname);
481
- fail_file();
482
- break;
499
+ goto out;
483500 case ELFDATA2LSB:
484501 if (*(unsigned char const *)&endian != 1) {
485502 /* main() is big endian, file.o is little endian. */
....@@ -507,53 +524,56 @@
507524 push_bl_mcount_thumb = push_bl_mcount_thumb_be;
508525 break;
509526 } /* 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) {
527
+ if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0 ||
528
+ w2(ehdr->e_type) != ET_REL ||
529
+ ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
513530 fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
514
- fail_file();
531
+ goto out;
515532 }
516533
517
- gpfx = 0;
534
+ gpfx = '_';
518535 switch (w2(ehdr->e_machine)) {
519536 default:
520537 fprintf(stderr, "unrecognized e_machine %u %s\n",
521538 w2(ehdr->e_machine), fname);
522
- fail_file();
523
- break;
539
+ goto out;
524540 case EM_386:
525541 reltype = R_386_32;
526542 rel_type_nop = R_386_NONE;
527543 make_nop = make_nop_x86;
528544 ideal_nop = ideal_nop5_x86_32;
529545 mcount_adjust_32 = -1;
546
+ gpfx = 0;
530547 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;
548
+ case EM_ARM:
549
+ reltype = R_ARM_ABS32;
550
+ altmcount = "__gnu_mcount_nc";
551
+ make_nop = make_nop_arm;
552
+ rel_type_nop = R_ARM_NONE;
553
+ is_fake_mcount32 = arm_is_fake_mcount;
554
+ gpfx = 0;
555
+ break;
537556 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;
557
+ reltype = R_AARCH64_ABS64;
558
+ make_nop = make_nop_arm64;
559
+ rel_type_nop = R_AARCH64_NONE;
560
+ ideal_nop = ideal_nop4_arm64;
561
+ is_fake_mcount64 = arm64_is_fake_mcount;
562
+ break;
563
+ case EM_IA_64: reltype = R_IA64_IMM64; break;
564
+ case EM_MIPS: /* reltype: e_class */ break;
565
+ case EM_PPC: reltype = R_PPC_ADDR32; break;
566
+ case EM_PPC64: reltype = R_PPC64_ADDR64; break;
567
+ case EM_S390: /* reltype: e_class */ break;
568
+ case EM_SH: reltype = R_SH_DIR32; gpfx = 0; break;
569
+ case EM_SPARCV9: reltype = R_SPARC_64; break;
551570 case EM_X86_64:
552571 make_nop = make_nop_x86;
553572 ideal_nop = ideal_nop5_x86_64;
554573 reltype = R_X86_64_64;
555574 rel_type_nop = R_X86_64_NONE;
556575 mcount_adjust_64 = -1;
576
+ gpfx = 0;
557577 break;
558578 } /* end switch */
559579
....@@ -561,20 +581,20 @@
561581 default:
562582 fprintf(stderr, "unrecognized ELF class %d %s\n",
563583 ehdr->e_ident[EI_CLASS], fname);
564
- fail_file();
565
- break;
584
+ goto out;
566585 case ELFCLASS32:
567586 if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
568587 || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
569588 fprintf(stderr,
570589 "unrecognized ET_REL file: %s\n", fname);
571
- fail_file();
590
+ goto out;
572591 }
573592 if (w2(ehdr->e_machine) == EM_MIPS) {
574593 reltype = R_MIPS_32;
575594 is_fake_mcount32 = MIPS32_is_fake_mcount;
576595 }
577
- do32(ehdr, fname, reltype);
596
+ if (do32(ehdr, fname, reltype) < 0)
597
+ goto out;
578598 break;
579599 case ELFCLASS64: {
580600 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
....@@ -582,7 +602,7 @@
582602 || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
583603 fprintf(stderr,
584604 "unrecognized ET_REL file: %s\n", fname);
585
- fail_file();
605
+ goto out;
586606 }
587607 if (w2(ghdr->e_machine) == EM_S390) {
588608 reltype = R_390_64;
....@@ -594,17 +614,20 @@
594614 Elf64_r_info = MIPS64_r_info;
595615 is_fake_mcount64 = MIPS64_is_fake_mcount;
596616 }
597
- do64(ghdr, fname, reltype);
617
+ if (do64(ghdr, fname, reltype) < 0)
618
+ goto out;
598619 break;
599620 }
600621 } /* end switch */
601622
602
- write_file(fname);
603
- cleanup();
623
+ rc = write_file(fname);
624
+out:
625
+ file_append_cleanup();
626
+ mmap_cleanup();
627
+ return rc;
604628 }
605629
606
-int
607
-main(int argc, char *argv[])
630
+int main(int argc, char *argv[])
608631 {
609632 const char ftrace[] = "/ftrace.o";
610633 int ftrace_size = sizeof(ftrace) - 1;
....@@ -631,7 +654,6 @@
631654 /* Process each file in turn, allowing deep failure. */
632655 for (i = optind; i < argc; i++) {
633656 char *file = argv[i];
634
- int const sjval = setjmp(jmpenv);
635657 int len;
636658
637659 /*
....@@ -644,28 +666,10 @@
644666 strcmp(file + (len - ftrace_size), ftrace) == 0)
645667 continue;
646668
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 */
669
+ if (do_file(file)) {
662670 fprintf(stderr, "%s: failed\n", file);
663671 ++n_error;
664
- break;
665
- case SJ_SUCCEED: /* premature success */
666
- /* do nothing */
667
- break;
668
- } /* end switch */
672
+ }
669673 }
670674 return !!n_error;
671675 }