hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
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,24 +93,12 @@
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;
....@@ -144,7 +114,9 @@
144114 }
145115 if (!file_append) {
146116 perror("write");
147
- fail_file();
117
+ file_append_cleanup();
118
+ mmap_cleanup();
119
+ return -1;
148120 }
149121 if (file_ptr < file_end) {
150122 cnt = file_end - file_ptr;
....@@ -164,16 +136,80 @@
164136 return count;
165137 }
166138
167
-static void *
168
-umalloc(size_t size)
139
+static void * umalloc(size_t size)
169140 {
170141 void *const addr = malloc(size);
171142 if (addr == 0) {
172143 fprintf(stderr, "malloc failed: %zu bytes\n", size);
173
- fail_file();
144
+ file_append_cleanup();
145
+ mmap_cleanup();
146
+ return NULL;
174147 }
175148 return addr;
176149 }
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
+
177213
178214 static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
179215 static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
....@@ -198,8 +234,10 @@
198234 return -1;
199235
200236 /* 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;
203241 return 0;
204242 }
205243
....@@ -247,10 +285,12 @@
247285 return -1;
248286
249287 /* Convert to nop */
250
- ulseek(fd_map, off, SEEK_SET);
288
+ if (ulseek(off, SEEK_SET) < 0)
289
+ return -1;
251290
252291 do {
253
- uwrite(fd_map, ideal_nop, nop_size);
292
+ if (uwrite(ideal_nop, nop_size) < 0)
293
+ return -1;
254294 } while (--cnt > 0);
255295
256296 return 0;
....@@ -267,57 +307,20 @@
267307 return -1;
268308
269309 /* 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;
272314 return 0;
273315 }
274316
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)
315318 {
316319 char tmp_file[strlen(fname) + 4];
317320 size_t n;
318321
319322 if (!file_updated)
320
- return;
323
+ return 0;
321324
322325 sprintf(tmp_file, "%s.rc", fname);
323326
....@@ -329,25 +332,28 @@
329332 fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
330333 if (fd_map < 0) {
331334 perror(fname);
332
- fail_file();
335
+ return -1;
333336 }
334337 n = write(fd_map, file_map, sb.st_size);
335338 if (n != sb.st_size) {
336339 perror("write");
337
- fail_file();
340
+ close(fd_map);
341
+ return -1;
338342 }
339343 if (file_append_size) {
340344 n = write(fd_map, file_append, file_append_size);
341345 if (n != file_append_size) {
342346 perror("write");
343
- fail_file();
347
+ close(fd_map);
348
+ return -1;
344349 }
345350 }
346351 close(fd_map);
347352 if (rename(tmp_file, fname) < 0) {
348353 perror(fname);
349
- fail_file();
354
+ return -1;
350355 }
356
+ return 0;
351357 }
352358
353359 /* w8rev, w8nat, ...: Handle endianness. */
....@@ -398,8 +404,7 @@
398404 static uint32_t (*w2)(uint16_t);
399405
400406 /* 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)
403408 {
404409 return strncmp(".text", txtname, 5) == 0 ||
405410 strcmp(".init.text", txtname) == 0 ||
....@@ -409,10 +414,10 @@
409414 strcmp(".irqentry.text", txtname) == 0 ||
410415 strcmp(".softirqentry.text", txtname) == 0 ||
411416 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;
415418 }
419
+
420
+static char const *already_has_rel_mcount = "success"; /* our work here is done! */
416421
417422 /* 32 bit and 64 bit are very similar */
418423 #include "recordmcount.h"
....@@ -429,6 +434,11 @@
429434 }
430435
431436 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;
432442 }
433443
434444 /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
....@@ -464,11 +474,15 @@
464474 }).r_info;
465475 }
466476
467
-static void
468
-do_file(char const *const fname)
477
+static int do_file(char const *const fname)
469478 {
470
- Elf32_Ehdr *const ehdr = mmap_file(fname);
471479 unsigned int reltype = 0;
480
+ Elf32_Ehdr *ehdr;
481
+ int rc = -1;
482
+
483
+ ehdr = mmap_file(fname);
484
+ if (!ehdr)
485
+ goto out;
472486
473487 w = w4nat;
474488 w2 = w2nat;
....@@ -478,8 +492,7 @@
478492 default:
479493 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
480494 ehdr->e_ident[EI_DATA], fname);
481
- fail_file();
482
- break;
495
+ goto out;
483496 case ELFDATA2LSB:
484497 if (*(unsigned char const *)&endian != 1) {
485498 /* main() is big endian, file.o is little endian. */
....@@ -507,53 +520,56 @@
507520 push_bl_mcount_thumb = push_bl_mcount_thumb_be;
508521 break;
509522 } /* 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) {
513526 fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
514
- fail_file();
527
+ goto out;
515528 }
516529
517
- gpfx = 0;
530
+ gpfx = '_';
518531 switch (w2(ehdr->e_machine)) {
519532 default:
520533 fprintf(stderr, "unrecognized e_machine %u %s\n",
521534 w2(ehdr->e_machine), fname);
522
- fail_file();
523
- break;
535
+ goto out;
524536 case EM_386:
525537 reltype = R_386_32;
526538 rel_type_nop = R_386_NONE;
527539 make_nop = make_nop_x86;
528540 ideal_nop = ideal_nop5_x86_32;
529541 mcount_adjust_32 = -1;
542
+ gpfx = 0;
530543 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;
537552 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;
551566 case EM_X86_64:
552567 make_nop = make_nop_x86;
553568 ideal_nop = ideal_nop5_x86_64;
554569 reltype = R_X86_64_64;
555570 rel_type_nop = R_X86_64_NONE;
556571 mcount_adjust_64 = -1;
572
+ gpfx = 0;
557573 break;
558574 } /* end switch */
559575
....@@ -561,20 +577,20 @@
561577 default:
562578 fprintf(stderr, "unrecognized ELF class %d %s\n",
563579 ehdr->e_ident[EI_CLASS], fname);
564
- fail_file();
565
- break;
580
+ goto out;
566581 case ELFCLASS32:
567582 if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
568583 || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
569584 fprintf(stderr,
570585 "unrecognized ET_REL file: %s\n", fname);
571
- fail_file();
586
+ goto out;
572587 }
573588 if (w2(ehdr->e_machine) == EM_MIPS) {
574589 reltype = R_MIPS_32;
575590 is_fake_mcount32 = MIPS32_is_fake_mcount;
576591 }
577
- do32(ehdr, fname, reltype);
592
+ if (do32(ehdr, fname, reltype) < 0)
593
+ goto out;
578594 break;
579595 case ELFCLASS64: {
580596 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
....@@ -582,7 +598,7 @@
582598 || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
583599 fprintf(stderr,
584600 "unrecognized ET_REL file: %s\n", fname);
585
- fail_file();
601
+ goto out;
586602 }
587603 if (w2(ghdr->e_machine) == EM_S390) {
588604 reltype = R_390_64;
....@@ -594,17 +610,20 @@
594610 Elf64_r_info = MIPS64_r_info;
595611 is_fake_mcount64 = MIPS64_is_fake_mcount;
596612 }
597
- do64(ghdr, fname, reltype);
613
+ if (do64(ghdr, fname, reltype) < 0)
614
+ goto out;
598615 break;
599616 }
600617 } /* end switch */
601618
602
- write_file(fname);
603
- cleanup();
619
+ rc = write_file(fname);
620
+out:
621
+ file_append_cleanup();
622
+ mmap_cleanup();
623
+ return rc;
604624 }
605625
606
-int
607
-main(int argc, char *argv[])
626
+int main(int argc, char *argv[])
608627 {
609628 const char ftrace[] = "/ftrace.o";
610629 int ftrace_size = sizeof(ftrace) - 1;
....@@ -631,7 +650,6 @@
631650 /* Process each file in turn, allowing deep failure. */
632651 for (i = optind; i < argc; i++) {
633652 char *file = argv[i];
634
- int const sjval = setjmp(jmpenv);
635653 int len;
636654
637655 /*
....@@ -644,28 +662,10 @@
644662 strcmp(file + (len - ftrace_size), ftrace) == 0)
645663 continue;
646664
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)) {
662666 fprintf(stderr, "%s: failed\n", file);
663667 ++n_error;
664
- break;
665
- case SJ_SUCCEED: /* premature success */
666
- /* do nothing */
667
- break;
668
- } /* end switch */
668
+ }
669669 }
670670 return !!n_error;
671671 }