sched/core: Fix an SMP ordering race in try_to_wake_up() vs. schedule()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / scripts / recordmcount.c
1 /*
2 * recordmcount.c: construct a table of the locations of calls to 'mcount'
3 * so that ftrace can find them quickly.
4 * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
5 * Licensed under the GNU General Public License, version 2 (GPLv2).
6 *
7 * Restructured to fit Linux format, as well as other updates:
8 * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
9 */
10
11 /*
12 * Strategy: alter the .o file in-place.
13 *
14 * Append a new STRTAB that has the new section names, followed by a new array
15 * ElfXX_Shdr[] that has the new section headers, followed by the section
16 * contents for __mcount_loc and its relocations. The old shstrtab strings,
17 * and the old ElfXX_Shdr[] array, remain as "garbage" (commonly, a couple
18 * kilobytes.) Subsequent processing by /bin/ld (or the kernel module loader)
19 * will ignore the garbage regions, because they are not designated by the
20 * new .e_shoff nor the new ElfXX_Shdr[]. [In order to remove the garbage,
21 * then use "ld -r" to create a new file that omits the garbage.]
22 */
23
24 #include <sys/types.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <getopt.h>
28 #include <elf.h>
29 #include <fcntl.h>
30 #include <setjmp.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 /*
37 * glibc synced up and added the metag number but didn't add the relocations.
38 * Work around this in a crude manner for now.
39 */
40 #ifndef EM_METAG
41 #define EM_METAG 174
42 #endif
43 #ifndef R_METAG_ADDR32
44 #define R_METAG_ADDR32 2
45 #endif
46 #ifndef R_METAG_NONE
47 #define R_METAG_NONE 3
48 #endif
49
50 static int fd_map; /* File descriptor for file being modified. */
51 static int mmap_failed; /* Boolean flag. */
52 static void *ehdr_curr; /* current ElfXX_Ehdr * for resource cleanup */
53 static char gpfx; /* prefix for global symbol name (sometimes '_') */
54 static struct stat sb; /* Remember .st_size, etc. */
55 static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
56 static const char *altmcount; /* alternate mcount symbol name */
57 static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
58
59 /* setjmp() return values */
60 enum {
61 SJ_SETJMP = 0, /* hardwired first return */
62 SJ_FAIL,
63 SJ_SUCCEED
64 };
65
66 /* Per-file resource cleanup when multiple files. */
67 static void
68 cleanup(void)
69 {
70 if (!mmap_failed)
71 munmap(ehdr_curr, sb.st_size);
72 else
73 free(ehdr_curr);
74 close(fd_map);
75 }
76
77 static void __attribute__((noreturn))
78 fail_file(void)
79 {
80 cleanup();
81 longjmp(jmpenv, SJ_FAIL);
82 }
83
84 static void __attribute__((noreturn))
85 succeed_file(void)
86 {
87 cleanup();
88 longjmp(jmpenv, SJ_SUCCEED);
89 }
90
91 /* ulseek, uread, ...: Check return value for errors. */
92
93 static off_t
94 ulseek(int const fd, off_t const offset, int const whence)
95 {
96 off_t const w = lseek(fd, offset, whence);
97 if (w == (off_t)-1) {
98 perror("lseek");
99 fail_file();
100 }
101 return w;
102 }
103
104 static size_t
105 uread(int const fd, void *const buf, size_t const count)
106 {
107 size_t const n = read(fd, buf, count);
108 if (n != count) {
109 perror("read");
110 fail_file();
111 }
112 return n;
113 }
114
115 static size_t
116 uwrite(int const fd, void const *const buf, size_t const count)
117 {
118 size_t const n = write(fd, buf, count);
119 if (n != count) {
120 perror("write");
121 fail_file();
122 }
123 return n;
124 }
125
126 static void *
127 umalloc(size_t size)
128 {
129 void *const addr = malloc(size);
130 if (addr == 0) {
131 fprintf(stderr, "malloc failed: %zu bytes\n", size);
132 fail_file();
133 }
134 return addr;
135 }
136
137 static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
138 static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
139 static unsigned char *ideal_nop;
140
141 static char rel_type_nop;
142
143 static int (*make_nop)(void *map, size_t const offset);
144
145 static int make_nop_x86(void *map, size_t const offset)
146 {
147 uint32_t *ptr;
148 unsigned char *op;
149
150 /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
151 ptr = map + offset;
152 if (*ptr != 0)
153 return -1;
154
155 op = map + offset - 1;
156 if (*op != 0xe8)
157 return -1;
158
159 /* convert to nop */
160 ulseek(fd_map, offset - 1, SEEK_SET);
161 uwrite(fd_map, ideal_nop, 5);
162 return 0;
163 }
164
165 /*
166 * Get the whole file as a programming convenience in order to avoid
167 * malloc+lseek+read+free of many pieces. If successful, then mmap
168 * avoids copying unused pieces; else just read the whole file.
169 * Open for both read and write; new info will be appended to the file.
170 * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
171 * do not propagate to the file until an explicit overwrite at the last.
172 * This preserves most aspects of consistency (all except .st_size)
173 * for simultaneous readers of the file while we are appending to it.
174 * However, multiple writers still are bad. We choose not to use
175 * locking because it is expensive and the use case of kernel build
176 * makes multiple writers unlikely.
177 */
178 static void *mmap_file(char const *fname)
179 {
180 void *addr;
181
182 fd_map = open(fname, O_RDWR);
183 if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
184 perror(fname);
185 fail_file();
186 }
187 if (!S_ISREG(sb.st_mode)) {
188 fprintf(stderr, "not a regular file: %s\n", fname);
189 fail_file();
190 }
191 addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
192 fd_map, 0);
193 mmap_failed = 0;
194 if (addr == MAP_FAILED) {
195 mmap_failed = 1;
196 addr = umalloc(sb.st_size);
197 uread(fd_map, addr, sb.st_size);
198 }
199 if (sb.st_nlink != 1) {
200 /* file is hard-linked, break the hard link */
201 close(fd_map);
202 if (unlink(fname) < 0) {
203 perror(fname);
204 fail_file();
205 }
206 fd_map = open(fname, O_RDWR | O_CREAT, sb.st_mode);
207 if (fd_map < 0) {
208 perror(fname);
209 fail_file();
210 }
211 uwrite(fd_map, addr, sb.st_size);
212 }
213 return addr;
214 }
215
216 /* w8rev, w8nat, ...: Handle endianness. */
217
218 static uint64_t w8rev(uint64_t const x)
219 {
220 return ((0xff & (x >> (0 * 8))) << (7 * 8))
221 | ((0xff & (x >> (1 * 8))) << (6 * 8))
222 | ((0xff & (x >> (2 * 8))) << (5 * 8))
223 | ((0xff & (x >> (3 * 8))) << (4 * 8))
224 | ((0xff & (x >> (4 * 8))) << (3 * 8))
225 | ((0xff & (x >> (5 * 8))) << (2 * 8))
226 | ((0xff & (x >> (6 * 8))) << (1 * 8))
227 | ((0xff & (x >> (7 * 8))) << (0 * 8));
228 }
229
230 static uint32_t w4rev(uint32_t const x)
231 {
232 return ((0xff & (x >> (0 * 8))) << (3 * 8))
233 | ((0xff & (x >> (1 * 8))) << (2 * 8))
234 | ((0xff & (x >> (2 * 8))) << (1 * 8))
235 | ((0xff & (x >> (3 * 8))) << (0 * 8));
236 }
237
238 static uint32_t w2rev(uint16_t const x)
239 {
240 return ((0xff & (x >> (0 * 8))) << (1 * 8))
241 | ((0xff & (x >> (1 * 8))) << (0 * 8));
242 }
243
244 static uint64_t w8nat(uint64_t const x)
245 {
246 return x;
247 }
248
249 static uint32_t w4nat(uint32_t const x)
250 {
251 return x;
252 }
253
254 static uint32_t w2nat(uint16_t const x)
255 {
256 return x;
257 }
258
259 static uint64_t (*w8)(uint64_t);
260 static uint32_t (*w)(uint32_t);
261 static uint32_t (*w2)(uint16_t);
262
263 /* Names of the sections that could contain calls to mcount. */
264 static int
265 is_mcounted_section_name(char const *const txtname)
266 {
267 return strcmp(".text", txtname) == 0 ||
268 strcmp(".ref.text", txtname) == 0 ||
269 strcmp(".sched.text", txtname) == 0 ||
270 strcmp(".spinlock.text", txtname) == 0 ||
271 strcmp(".irqentry.text", txtname) == 0 ||
272 strcmp(".kprobes.text", txtname) == 0 ||
273 strcmp(".text.unlikely", txtname) == 0;
274 }
275
276 /* 32 bit and 64 bit are very similar */
277 #include "recordmcount.h"
278 #define RECORD_MCOUNT_64
279 #include "recordmcount.h"
280
281 /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
282 * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
283 * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
284 * to imply the order of the members; the spec does not say so.
285 * typedef unsigned char Elf64_Byte;
286 * fails on MIPS64 because their <elf.h> already has it!
287 */
288
289 typedef uint8_t myElf64_Byte; /* Type for a 8-bit quantity. */
290
291 union mips_r_info {
292 Elf64_Xword r_info;
293 struct {
294 Elf64_Word r_sym; /* Symbol index. */
295 myElf64_Byte r_ssym; /* Special symbol. */
296 myElf64_Byte r_type3; /* Third relocation. */
297 myElf64_Byte r_type2; /* Second relocation. */
298 myElf64_Byte r_type; /* First relocation. */
299 } r_mips;
300 };
301
302 static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
303 {
304 return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
305 }
306
307 static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
308 {
309 rp->r_info = ((union mips_r_info){
310 .r_mips = { .r_sym = w(sym), .r_type = type }
311 }).r_info;
312 }
313
314 static void
315 do_file(char const *const fname)
316 {
317 Elf32_Ehdr *const ehdr = mmap_file(fname);
318 unsigned int reltype = 0;
319
320 ehdr_curr = ehdr;
321 w = w4nat;
322 w2 = w2nat;
323 w8 = w8nat;
324 switch (ehdr->e_ident[EI_DATA]) {
325 static unsigned int const endian = 1;
326 default:
327 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
328 ehdr->e_ident[EI_DATA], fname);
329 fail_file();
330 break;
331 case ELFDATA2LSB:
332 if (*(unsigned char const *)&endian != 1) {
333 /* main() is big endian, file.o is little endian. */
334 w = w4rev;
335 w2 = w2rev;
336 w8 = w8rev;
337 }
338 break;
339 case ELFDATA2MSB:
340 if (*(unsigned char const *)&endian != 0) {
341 /* main() is little endian, file.o is big endian. */
342 w = w4rev;
343 w2 = w2rev;
344 w8 = w8rev;
345 }
346 break;
347 } /* end switch */
348 if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
349 || w2(ehdr->e_type) != ET_REL
350 || ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
351 fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
352 fail_file();
353 }
354
355 gpfx = 0;
356 switch (w2(ehdr->e_machine)) {
357 default:
358 fprintf(stderr, "unrecognized e_machine %d %s\n",
359 w2(ehdr->e_machine), fname);
360 fail_file();
361 break;
362 case EM_386:
363 reltype = R_386_32;
364 make_nop = make_nop_x86;
365 ideal_nop = ideal_nop5_x86_32;
366 mcount_adjust_32 = -1;
367 break;
368 case EM_ARM: reltype = R_ARM_ABS32;
369 altmcount = "__gnu_mcount_nc";
370 break;
371 case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break;
372 case EM_METAG: reltype = R_METAG_ADDR32;
373 altmcount = "_mcount_wrapper";
374 rel_type_nop = R_METAG_NONE;
375 /* We happen to have the same requirement as MIPS */
376 is_fake_mcount32 = MIPS32_is_fake_mcount;
377 break;
378 case EM_MIPS: /* reltype: e_class */ gpfx = '_'; break;
379 case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break;
380 case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break;
381 case EM_S390: /* reltype: e_class */ gpfx = '_'; break;
382 case EM_SH: reltype = R_SH_DIR32; break;
383 case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break;
384 case EM_X86_64:
385 make_nop = make_nop_x86;
386 ideal_nop = ideal_nop5_x86_64;
387 reltype = R_X86_64_64;
388 mcount_adjust_64 = -1;
389 break;
390 } /* end switch */
391
392 switch (ehdr->e_ident[EI_CLASS]) {
393 default:
394 fprintf(stderr, "unrecognized ELF class %d %s\n",
395 ehdr->e_ident[EI_CLASS], fname);
396 fail_file();
397 break;
398 case ELFCLASS32:
399 if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
400 || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
401 fprintf(stderr,
402 "unrecognized ET_REL file: %s\n", fname);
403 fail_file();
404 }
405 if (w2(ehdr->e_machine) == EM_S390) {
406 reltype = R_390_32;
407 mcount_adjust_32 = -4;
408 }
409 if (w2(ehdr->e_machine) == EM_MIPS) {
410 reltype = R_MIPS_32;
411 is_fake_mcount32 = MIPS32_is_fake_mcount;
412 }
413 do32(ehdr, fname, reltype);
414 break;
415 case ELFCLASS64: {
416 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
417 if (w2(ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
418 || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
419 fprintf(stderr,
420 "unrecognized ET_REL file: %s\n", fname);
421 fail_file();
422 }
423 if (w2(ghdr->e_machine) == EM_S390) {
424 reltype = R_390_64;
425 mcount_adjust_64 = -8;
426 }
427 if (w2(ghdr->e_machine) == EM_MIPS) {
428 reltype = R_MIPS_64;
429 Elf64_r_sym = MIPS64_r_sym;
430 Elf64_r_info = MIPS64_r_info;
431 is_fake_mcount64 = MIPS64_is_fake_mcount;
432 }
433 do64(ghdr, fname, reltype);
434 break;
435 }
436 } /* end switch */
437
438 cleanup();
439 }
440
441 int
442 main(int argc, char *argv[])
443 {
444 const char ftrace[] = "/ftrace.o";
445 int ftrace_size = sizeof(ftrace) - 1;
446 int n_error = 0; /* gcc-4.3.0 false positive complaint */
447 int c;
448 int i;
449
450 while ((c = getopt(argc, argv, "w")) >= 0) {
451 switch (c) {
452 case 'w':
453 warn_on_notrace_sect = 1;
454 break;
455 default:
456 fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
457 return 0;
458 }
459 }
460
461 if ((argc - optind) < 1) {
462 fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
463 return 0;
464 }
465
466 /* Process each file in turn, allowing deep failure. */
467 for (i = optind; i < argc; i++) {
468 char *file = argv[i];
469 int const sjval = setjmp(jmpenv);
470 int len;
471
472 /*
473 * The file kernel/trace/ftrace.o references the mcount
474 * function but does not call it. Since ftrace.o should
475 * not be traced anyway, we just skip it.
476 */
477 len = strlen(file);
478 if (len >= ftrace_size &&
479 strcmp(file + (len - ftrace_size), ftrace) == 0)
480 continue;
481
482 switch (sjval) {
483 default:
484 fprintf(stderr, "internal error: %s\n", file);
485 exit(1);
486 break;
487 case SJ_SETJMP: /* normal sequence */
488 /* Avoid problems if early cleanup() */
489 fd_map = -1;
490 ehdr_curr = NULL;
491 mmap_failed = 1;
492 do_file(file);
493 break;
494 case SJ_FAIL: /* error in do_file or below */
495 ++n_error;
496 break;
497 case SJ_SUCCEED: /* premature success */
498 /* do nothing */
499 break;
500 } /* end switch */
501 }
502 return !!n_error;
503 }
504
505