sched/core: Fix a race between try_to_wake_up() and a woken up task
[pandora-kernel.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 static int fd_map;      /* File descriptor for file being modified. */
37 static int mmap_failed; /* Boolean flag. */
38 static char gpfx;       /* prefix for global symbol name (sometimes '_') */
39 static struct stat sb;  /* Remember .st_size, etc. */
40 static jmp_buf jmpenv;  /* setjmp/longjmp per-file error escape */
41 static const char *altmcount;   /* alternate mcount symbol name */
42 static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
43 static void *file_map;  /* pointer of the mapped file */
44 static void *file_end;  /* pointer to the end of the mapped file */
45 static int file_updated; /* flag to state file was changed */
46 static void *file_ptr;  /* current file pointer location */
47 static void *file_append; /* added to the end of the file */
48 static size_t file_append_size; /* how much is added to end of file */
49
50 /* setjmp() return values */
51 enum {
52         SJ_SETJMP = 0,  /* hardwired first return */
53         SJ_FAIL,
54         SJ_SUCCEED
55 };
56
57 /* Per-file resource cleanup when multiple files. */
58 static void
59 cleanup(void)
60 {
61         if (!mmap_failed)
62                 munmap(file_map, sb.st_size);
63         else
64                 free(file_map);
65         file_map = NULL;
66         free(file_append);
67         file_append = NULL;
68         file_append_size = 0;
69         file_updated = 0;
70 }
71
72 static void __attribute__((noreturn))
73 fail_file(void)
74 {
75         cleanup();
76         longjmp(jmpenv, SJ_FAIL);
77 }
78
79 static void __attribute__((noreturn))
80 succeed_file(void)
81 {
82         cleanup();
83         longjmp(jmpenv, SJ_SUCCEED);
84 }
85
86 /* ulseek, uread, ...:  Check return value for errors. */
87
88 static off_t
89 ulseek(int const fd, off_t const offset, int const whence)
90 {
91         switch (whence) {
92         case SEEK_SET:
93                 file_ptr = file_map + offset;
94                 break;
95         case SEEK_CUR:
96                 file_ptr += offset;
97                 break;
98         case SEEK_END:
99                 file_ptr = file_map + (sb.st_size - offset);
100                 break;
101         }
102         if (file_ptr < file_map) {
103                 fprintf(stderr, "lseek: seek before file\n");
104                 fail_file();
105         }
106         return file_ptr - file_map;
107 }
108
109 static size_t
110 uread(int const fd, void *const buf, size_t const count)
111 {
112         size_t const n = read(fd, buf, count);
113         if (n != count) {
114                 perror("read");
115                 fail_file();
116         }
117         return n;
118 }
119
120 static size_t
121 uwrite(int const fd, void const *const buf, size_t const count)
122 {
123         size_t cnt = count;
124         off_t idx = 0;
125
126         file_updated = 1;
127
128         if (file_ptr + count >= file_end) {
129                 off_t aoffset = (file_ptr + count) - file_end;
130
131                 if (aoffset > file_append_size) {
132                         file_append = realloc(file_append, aoffset);
133                         file_append_size = aoffset;
134                 }
135                 if (!file_append) {
136                         perror("write");
137                         fail_file();
138                 }
139                 if (file_ptr < file_end) {
140                         cnt = file_end - file_ptr;
141                 } else {
142                         cnt = 0;
143                         idx = aoffset - count;
144                 }
145         }
146
147         if (cnt)
148                 memcpy(file_ptr, buf, cnt);
149
150         if (cnt < count)
151                 memcpy(file_append + idx, buf + cnt, count - cnt);
152
153         file_ptr += count;
154         return count;
155 }
156
157 static void *
158 umalloc(size_t size)
159 {
160         void *const addr = malloc(size);
161         if (addr == 0) {
162                 fprintf(stderr, "malloc failed: %zu bytes\n", size);
163                 fail_file();
164         }
165         return addr;
166 }
167
168 static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
169 static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
170 static unsigned char *ideal_nop;
171
172 static char rel_type_nop;
173
174 static int (*make_nop)(void *map, size_t const offset);
175
176 static int make_nop_x86(void *map, size_t const offset)
177 {
178         uint32_t *ptr;
179         unsigned char *op;
180
181         /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
182         ptr = map + offset;
183         if (*ptr != 0)
184                 return -1;
185
186         op = map + offset - 1;
187         if (*op != 0xe8)
188                 return -1;
189
190         /* convert to nop */
191         ulseek(fd_map, offset - 1, SEEK_SET);
192         uwrite(fd_map, ideal_nop, 5);
193         return 0;
194 }
195
196 /*
197  * Get the whole file as a programming convenience in order to avoid
198  * malloc+lseek+read+free of many pieces.  If successful, then mmap
199  * avoids copying unused pieces; else just read the whole file.
200  * Open for both read and write; new info will be appended to the file.
201  * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
202  * do not propagate to the file until an explicit overwrite at the last.
203  * This preserves most aspects of consistency (all except .st_size)
204  * for simultaneous readers of the file while we are appending to it.
205  * However, multiple writers still are bad.  We choose not to use
206  * locking because it is expensive and the use case of kernel build
207  * makes multiple writers unlikely.
208  */
209 static void *mmap_file(char const *fname)
210 {
211         fd_map = open(fname, O_RDONLY);
212         if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
213                 perror(fname);
214                 fail_file();
215         }
216         if (!S_ISREG(sb.st_mode)) {
217                 fprintf(stderr, "not a regular file: %s\n", fname);
218                 fail_file();
219         }
220         file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
221                         fd_map, 0);
222         mmap_failed = 0;
223         if (file_map == MAP_FAILED) {
224                 mmap_failed = 1;
225                 file_map = umalloc(sb.st_size);
226                 uread(fd_map, file_map, sb.st_size);
227         }
228         close(fd_map);
229
230         file_end = file_map + sb.st_size;
231
232         return file_map;
233 }
234
235 static void write_file(const char *fname)
236 {
237         char tmp_file[strlen(fname) + 4];
238         size_t n;
239
240         if (!file_updated)
241                 return;
242
243         sprintf(tmp_file, "%s.rc", fname);
244
245         /*
246          * After reading the entire file into memory, delete it
247          * and write it back, to prevent weird side effects of modifying
248          * an object file in place.
249          */
250         fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
251         if (fd_map < 0) {
252                 perror(fname);
253                 fail_file();
254         }
255         n = write(fd_map, file_map, sb.st_size);
256         if (n != sb.st_size) {
257                 perror("write");
258                 fail_file();
259         }
260         if (file_append_size) {
261                 n = write(fd_map, file_append, file_append_size);
262                 if (n != file_append_size) {
263                         perror("write");
264                         fail_file();
265                 }
266         }
267         close(fd_map);
268         if (rename(tmp_file, fname) < 0) {
269                 perror(fname);
270                 fail_file();
271         }
272 }
273
274 /* w8rev, w8nat, ...: Handle endianness. */
275
276 static uint64_t w8rev(uint64_t const x)
277 {
278         return   ((0xff & (x >> (0 * 8))) << (7 * 8))
279                | ((0xff & (x >> (1 * 8))) << (6 * 8))
280                | ((0xff & (x >> (2 * 8))) << (5 * 8))
281                | ((0xff & (x >> (3 * 8))) << (4 * 8))
282                | ((0xff & (x >> (4 * 8))) << (3 * 8))
283                | ((0xff & (x >> (5 * 8))) << (2 * 8))
284                | ((0xff & (x >> (6 * 8))) << (1 * 8))
285                | ((0xff & (x >> (7 * 8))) << (0 * 8));
286 }
287
288 static uint32_t w4rev(uint32_t const x)
289 {
290         return   ((0xff & (x >> (0 * 8))) << (3 * 8))
291                | ((0xff & (x >> (1 * 8))) << (2 * 8))
292                | ((0xff & (x >> (2 * 8))) << (1 * 8))
293                | ((0xff & (x >> (3 * 8))) << (0 * 8));
294 }
295
296 static uint32_t w2rev(uint16_t const x)
297 {
298         return   ((0xff & (x >> (0 * 8))) << (1 * 8))
299                | ((0xff & (x >> (1 * 8))) << (0 * 8));
300 }
301
302 static uint64_t w8nat(uint64_t const x)
303 {
304         return x;
305 }
306
307 static uint32_t w4nat(uint32_t const x)
308 {
309         return x;
310 }
311
312 static uint32_t w2nat(uint16_t const x)
313 {
314         return x;
315 }
316
317 static uint64_t (*w8)(uint64_t);
318 static uint32_t (*w)(uint32_t);
319 static uint32_t (*w2)(uint16_t);
320
321 /* Names of the sections that could contain calls to mcount. */
322 static int
323 is_mcounted_section_name(char const *const txtname)
324 {
325         return strcmp(".text",           txtname) == 0 ||
326                 strcmp(".ref.text",      txtname) == 0 ||
327                 strcmp(".sched.text",    txtname) == 0 ||
328                 strcmp(".spinlock.text", txtname) == 0 ||
329                 strcmp(".irqentry.text", txtname) == 0 ||
330                 strcmp(".kprobes.text", txtname) == 0 ||
331                 strcmp(".text.unlikely", txtname) == 0;
332 }
333
334 /* 32 bit and 64 bit are very similar */
335 #include "recordmcount.h"
336 #define RECORD_MCOUNT_64
337 #include "recordmcount.h"
338
339 /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
340  * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
341  * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
342  * to imply the order of the members; the spec does not say so.
343  *      typedef unsigned char Elf64_Byte;
344  * fails on MIPS64 because their <elf.h> already has it!
345  */
346
347 typedef uint8_t myElf64_Byte;           /* Type for a 8-bit quantity.  */
348
349 union mips_r_info {
350         Elf64_Xword r_info;
351         struct {
352                 Elf64_Word r_sym;               /* Symbol index.  */
353                 myElf64_Byte r_ssym;            /* Special symbol.  */
354                 myElf64_Byte r_type3;           /* Third relocation.  */
355                 myElf64_Byte r_type2;           /* Second relocation.  */
356                 myElf64_Byte r_type;            /* First relocation.  */
357         } r_mips;
358 };
359
360 static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
361 {
362         return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
363 }
364
365 static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
366 {
367         rp->r_info = ((union mips_r_info){
368                 .r_mips = { .r_sym = w(sym), .r_type = type }
369         }).r_info;
370 }
371
372 static void
373 do_file(char const *const fname)
374 {
375         Elf32_Ehdr *const ehdr = mmap_file(fname);
376         unsigned int reltype = 0;
377
378         w = w4nat;
379         w2 = w2nat;
380         w8 = w8nat;
381         switch (ehdr->e_ident[EI_DATA]) {
382                 static unsigned int const endian = 1;
383         default:
384                 fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
385                         ehdr->e_ident[EI_DATA], fname);
386                 fail_file();
387                 break;
388         case ELFDATA2LSB:
389                 if (*(unsigned char const *)&endian != 1) {
390                         /* main() is big endian, file.o is little endian. */
391                         w = w4rev;
392                         w2 = w2rev;
393                         w8 = w8rev;
394                 }
395                 break;
396         case ELFDATA2MSB:
397                 if (*(unsigned char const *)&endian != 0) {
398                         /* main() is little endian, file.o is big endian. */
399                         w = w4rev;
400                         w2 = w2rev;
401                         w8 = w8rev;
402                 }
403                 break;
404         }  /* end switch */
405         if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
406         ||  w2(ehdr->e_type) != ET_REL
407         ||  ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
408                 fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
409                 fail_file();
410         }
411
412         gpfx = 0;
413         switch (w2(ehdr->e_machine)) {
414         default:
415                 fprintf(stderr, "unrecognized e_machine %d %s\n",
416                         w2(ehdr->e_machine), fname);
417                 fail_file();
418                 break;
419         case EM_386:
420                 reltype = R_386_32;
421                 make_nop = make_nop_x86;
422                 ideal_nop = ideal_nop5_x86_32;
423                 mcount_adjust_32 = -1;
424                 break;
425         case EM_ARM:     reltype = R_ARM_ABS32;
426                          altmcount = "__gnu_mcount_nc";
427                          break;
428         case EM_IA_64:   reltype = R_IA64_IMM64;   gpfx = '_'; break;
429         case EM_MIPS:    /* reltype: e_class    */ gpfx = '_'; break;
430         case EM_PPC:     reltype = R_PPC_ADDR32;   gpfx = '_'; break;
431         case EM_PPC64:   reltype = R_PPC64_ADDR64; gpfx = '_'; break;
432         case EM_S390:    /* reltype: e_class    */ gpfx = '_'; break;
433         case EM_SH:      reltype = R_SH_DIR32;                 break;
434         case EM_SPARCV9: reltype = R_SPARC_64;     gpfx = '_'; break;
435         case EM_X86_64:
436                 make_nop = make_nop_x86;
437                 ideal_nop = ideal_nop5_x86_64;
438                 reltype = R_X86_64_64;
439                 mcount_adjust_64 = -1;
440                 break;
441         }  /* end switch */
442
443         switch (ehdr->e_ident[EI_CLASS]) {
444         default:
445                 fprintf(stderr, "unrecognized ELF class %d %s\n",
446                         ehdr->e_ident[EI_CLASS], fname);
447                 fail_file();
448                 break;
449         case ELFCLASS32:
450                 if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
451                 ||  w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
452                         fprintf(stderr,
453                                 "unrecognized ET_REL file: %s\n", fname);
454                         fail_file();
455                 }
456                 if (w2(ehdr->e_machine) == EM_S390) {
457                         reltype = R_390_32;
458                         mcount_adjust_32 = -4;
459                 }
460                 if (w2(ehdr->e_machine) == EM_MIPS) {
461                         reltype = R_MIPS_32;
462                         is_fake_mcount32 = MIPS32_is_fake_mcount;
463                 }
464                 do32(ehdr, fname, reltype);
465                 break;
466         case ELFCLASS64: {
467                 Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
468                 if (w2(ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
469                 ||  w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
470                         fprintf(stderr,
471                                 "unrecognized ET_REL file: %s\n", fname);
472                         fail_file();
473                 }
474                 if (w2(ghdr->e_machine) == EM_S390) {
475                         reltype = R_390_64;
476                         mcount_adjust_64 = -8;
477                 }
478                 if (w2(ghdr->e_machine) == EM_MIPS) {
479                         reltype = R_MIPS_64;
480                         Elf64_r_sym = MIPS64_r_sym;
481                         Elf64_r_info = MIPS64_r_info;
482                         is_fake_mcount64 = MIPS64_is_fake_mcount;
483                 }
484                 do64(ghdr, fname, reltype);
485                 break;
486         }
487         }  /* end switch */
488
489         write_file(fname);
490         cleanup();
491 }
492
493 int
494 main(int argc, char *argv[])
495 {
496         const char ftrace[] = "/ftrace.o";
497         int ftrace_size = sizeof(ftrace) - 1;
498         int n_error = 0;  /* gcc-4.3.0 false positive complaint */
499         int c;
500         int i;
501
502         while ((c = getopt(argc, argv, "w")) >= 0) {
503                 switch (c) {
504                 case 'w':
505                         warn_on_notrace_sect = 1;
506                         break;
507                 default:
508                         fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
509                         return 0;
510                 }
511         }
512
513         if ((argc - optind) < 1) {
514                 fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
515                 return 0;
516         }
517
518         /* Process each file in turn, allowing deep failure. */
519         for (i = optind; i < argc; i++) {
520                 char *file = argv[i];
521                 int const sjval = setjmp(jmpenv);
522                 int len;
523
524                 /*
525                  * The file kernel/trace/ftrace.o references the mcount
526                  * function but does not call it. Since ftrace.o should
527                  * not be traced anyway, we just skip it.
528                  */
529                 len = strlen(file);
530                 if (len >= ftrace_size &&
531                     strcmp(file + (len - ftrace_size), ftrace) == 0)
532                         continue;
533
534                 switch (sjval) {
535                 default:
536                         fprintf(stderr, "internal error: %s\n", file);
537                         exit(1);
538                         break;
539                 case SJ_SETJMP:    /* normal sequence */
540                         /* Avoid problems if early cleanup() */
541                         fd_map = -1;
542                         mmap_failed = 1;
543                         file_map = NULL;
544                         file_ptr = NULL;
545                         file_updated = 0;
546                         do_file(file);
547                         break;
548                 case SJ_FAIL:    /* error in do_file or below */
549                         fprintf(stderr, "%s: failed\n", file);
550                         ++n_error;
551                         break;
552                 case SJ_SUCCEED:    /* premature success */
553                         /* do nothing */
554                         break;
555                 }  /* end switch */
556         }
557         return !!n_error;
558 }
559
560