Back to home page

LXR

 
 

    


0001 /*
0002  * recordmcount.c: construct a table of the locations of calls to 'mcount'
0003  * so that ftrace can find them quickly.
0004  * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>.  All rights reserved.
0005  * Licensed under the GNU General Public License, version 2 (GPLv2).
0006  *
0007  * Restructured to fit Linux format, as well as other updates:
0008  *  Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
0009  */
0010 
0011 /*
0012  * Strategy: alter the .o file in-place.
0013  *
0014  * Append a new STRTAB that has the new section names, followed by a new array
0015  * ElfXX_Shdr[] that has the new section headers, followed by the section
0016  * contents for __mcount_loc and its relocations.  The old shstrtab strings,
0017  * and the old ElfXX_Shdr[] array, remain as "garbage" (commonly, a couple
0018  * kilobytes.)  Subsequent processing by /bin/ld (or the kernel module loader)
0019  * will ignore the garbage regions, because they are not designated by the
0020  * new .e_shoff nor the new ElfXX_Shdr[].  [In order to remove the garbage,
0021  * then use "ld -r" to create a new file that omits the garbage.]
0022  */
0023 
0024 #include <sys/types.h>
0025 #include <sys/mman.h>
0026 #include <sys/stat.h>
0027 #include <getopt.h>
0028 #include <elf.h>
0029 #include <fcntl.h>
0030 #include <setjmp.h>
0031 #include <stdio.h>
0032 #include <stdlib.h>
0033 #include <string.h>
0034 #include <unistd.h>
0035 
0036 /*
0037  * glibc synced up and added the metag number but didn't add the relocations.
0038  * Work around this in a crude manner for now.
0039  */
0040 #ifndef EM_METAG
0041 #define EM_METAG      174
0042 #endif
0043 #ifndef R_METAG_ADDR32
0044 #define R_METAG_ADDR32                   2
0045 #endif
0046 #ifndef R_METAG_NONE
0047 #define R_METAG_NONE                     3
0048 #endif
0049 
0050 #ifndef EM_AARCH64
0051 #define EM_AARCH64  183
0052 #define R_AARCH64_NONE      0
0053 #define R_AARCH64_ABS64 257
0054 #endif
0055 
0056 static int fd_map;  /* File descriptor for file being modified. */
0057 static int mmap_failed; /* Boolean flag. */
0058 static char gpfx;   /* prefix for global symbol name (sometimes '_') */
0059 static struct stat sb;  /* Remember .st_size, etc. */
0060 static jmp_buf jmpenv;  /* setjmp/longjmp per-file error escape */
0061 static const char *altmcount;   /* alternate mcount symbol name */
0062 static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
0063 static void *file_map;  /* pointer of the mapped file */
0064 static void *file_end;  /* pointer to the end of the mapped file */
0065 static int file_updated; /* flag to state file was changed */
0066 static void *file_ptr;  /* current file pointer location */
0067 static void *file_append; /* added to the end of the file */
0068 static size_t file_append_size; /* how much is added to end of file */
0069 
0070 /* setjmp() return values */
0071 enum {
0072     SJ_SETJMP = 0,  /* hardwired first return */
0073     SJ_FAIL,
0074     SJ_SUCCEED
0075 };
0076 
0077 /* Per-file resource cleanup when multiple files. */
0078 static void
0079 cleanup(void)
0080 {
0081     if (!mmap_failed)
0082         munmap(file_map, sb.st_size);
0083     else
0084         free(file_map);
0085     file_map = NULL;
0086     free(file_append);
0087     file_append = NULL;
0088     file_append_size = 0;
0089     file_updated = 0;
0090 }
0091 
0092 static void __attribute__((noreturn))
0093 fail_file(void)
0094 {
0095     cleanup();
0096     longjmp(jmpenv, SJ_FAIL);
0097 }
0098 
0099 static void __attribute__((noreturn))
0100 succeed_file(void)
0101 {
0102     cleanup();
0103     longjmp(jmpenv, SJ_SUCCEED);
0104 }
0105 
0106 /* ulseek, uread, ...:  Check return value for errors. */
0107 
0108 static off_t
0109 ulseek(int const fd, off_t const offset, int const whence)
0110 {
0111     switch (whence) {
0112     case SEEK_SET:
0113         file_ptr = file_map + offset;
0114         break;
0115     case SEEK_CUR:
0116         file_ptr += offset;
0117         break;
0118     case SEEK_END:
0119         file_ptr = file_map + (sb.st_size - offset);
0120         break;
0121     }
0122     if (file_ptr < file_map) {
0123         fprintf(stderr, "lseek: seek before file\n");
0124         fail_file();
0125     }
0126     return file_ptr - file_map;
0127 }
0128 
0129 static size_t
0130 uread(int const fd, void *const buf, size_t const count)
0131 {
0132     size_t const n = read(fd, buf, count);
0133     if (n != count) {
0134         perror("read");
0135         fail_file();
0136     }
0137     return n;
0138 }
0139 
0140 static size_t
0141 uwrite(int const fd, void const *const buf, size_t const count)
0142 {
0143     size_t cnt = count;
0144     off_t idx = 0;
0145 
0146     file_updated = 1;
0147 
0148     if (file_ptr + count >= file_end) {
0149         off_t aoffset = (file_ptr + count) - file_end;
0150 
0151         if (aoffset > file_append_size) {
0152             file_append = realloc(file_append, aoffset);
0153             file_append_size = aoffset;
0154         }
0155         if (!file_append) {
0156             perror("write");
0157             fail_file();
0158         }
0159         if (file_ptr < file_end) {
0160             cnt = file_end - file_ptr;
0161         } else {
0162             cnt = 0;
0163             idx = aoffset - count;
0164         }
0165     }
0166 
0167     if (cnt)
0168         memcpy(file_ptr, buf, cnt);
0169 
0170     if (cnt < count)
0171         memcpy(file_append + idx, buf + cnt, count - cnt);
0172 
0173     file_ptr += count;
0174     return count;
0175 }
0176 
0177 static void *
0178 umalloc(size_t size)
0179 {
0180     void *const addr = malloc(size);
0181     if (addr == 0) {
0182         fprintf(stderr, "malloc failed: %zu bytes\n", size);
0183         fail_file();
0184     }
0185     return addr;
0186 }
0187 
0188 static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
0189 static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
0190 static unsigned char *ideal_nop;
0191 
0192 static char rel_type_nop;
0193 
0194 static int (*make_nop)(void *map, size_t const offset);
0195 
0196 static int make_nop_x86(void *map, size_t const offset)
0197 {
0198     uint32_t *ptr;
0199     unsigned char *op;
0200 
0201     /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
0202     ptr = map + offset;
0203     if (*ptr != 0)
0204         return -1;
0205 
0206     op = map + offset - 1;
0207     if (*op != 0xe8)
0208         return -1;
0209 
0210     /* convert to nop */
0211     ulseek(fd_map, offset - 1, SEEK_SET);
0212     uwrite(fd_map, ideal_nop, 5);
0213     return 0;
0214 }
0215 
0216 static unsigned char ideal_nop4_arm_le[4] = { 0x00, 0x00, 0xa0, 0xe1 }; /* mov r0, r0 */
0217 static unsigned char ideal_nop4_arm_be[4] = { 0xe1, 0xa0, 0x00, 0x00 }; /* mov r0, r0 */
0218 static unsigned char *ideal_nop4_arm;
0219 
0220 static unsigned char bl_mcount_arm_le[4] = { 0xfe, 0xff, 0xff, 0xeb }; /* bl */
0221 static unsigned char bl_mcount_arm_be[4] = { 0xeb, 0xff, 0xff, 0xfe }; /* bl */
0222 static unsigned char *bl_mcount_arm;
0223 
0224 static unsigned char push_arm_le[4] = { 0x04, 0xe0, 0x2d, 0xe5 }; /* push {lr} */
0225 static unsigned char push_arm_be[4] = { 0xe5, 0x2d, 0xe0, 0x04 }; /* push {lr} */
0226 static unsigned char *push_arm;
0227 
0228 static unsigned char ideal_nop2_thumb_le[2] = { 0x00, 0xbf }; /* nop */
0229 static unsigned char ideal_nop2_thumb_be[2] = { 0xbf, 0x00 }; /* nop */
0230 static unsigned char *ideal_nop2_thumb;
0231 
0232 static unsigned char push_bl_mcount_thumb_le[6] = { 0x00, 0xb5, 0xff, 0xf7, 0xfe, 0xff }; /* push {lr}, bl */
0233 static unsigned char push_bl_mcount_thumb_be[6] = { 0xb5, 0x00, 0xf7, 0xff, 0xff, 0xfe }; /* push {lr}, bl */
0234 static unsigned char *push_bl_mcount_thumb;
0235 
0236 static int make_nop_arm(void *map, size_t const offset)
0237 {
0238     char *ptr;
0239     int cnt = 1;
0240     int nop_size;
0241     size_t off = offset;
0242 
0243     ptr = map + offset;
0244     if (memcmp(ptr, bl_mcount_arm, 4) == 0) {
0245         if (memcmp(ptr - 4, push_arm, 4) == 0) {
0246             off -= 4;
0247             cnt = 2;
0248         }
0249         ideal_nop = ideal_nop4_arm;
0250         nop_size = 4;
0251     } else if (memcmp(ptr - 2, push_bl_mcount_thumb, 6) == 0) {
0252         cnt = 3;
0253         nop_size = 2;
0254         off -= 2;
0255         ideal_nop = ideal_nop2_thumb;
0256     } else
0257         return -1;
0258 
0259     /* Convert to nop */
0260     ulseek(fd_map, off, SEEK_SET);
0261 
0262     do {
0263         uwrite(fd_map, ideal_nop, nop_size);
0264     } while (--cnt > 0);
0265 
0266     return 0;
0267 }
0268 
0269 static unsigned char ideal_nop4_arm64[4] = {0x1f, 0x20, 0x03, 0xd5};
0270 static int make_nop_arm64(void *map, size_t const offset)
0271 {
0272     uint32_t *ptr;
0273 
0274     ptr = map + offset;
0275     /* bl <_mcount> is 0x94000000 before relocation */
0276     if (*ptr != 0x94000000)
0277         return -1;
0278 
0279     /* Convert to nop */
0280     ulseek(fd_map, offset, SEEK_SET);
0281     uwrite(fd_map, ideal_nop, 4);
0282     return 0;
0283 }
0284 
0285 /*
0286  * Get the whole file as a programming convenience in order to avoid
0287  * malloc+lseek+read+free of many pieces.  If successful, then mmap
0288  * avoids copying unused pieces; else just read the whole file.
0289  * Open for both read and write; new info will be appended to the file.
0290  * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
0291  * do not propagate to the file until an explicit overwrite at the last.
0292  * This preserves most aspects of consistency (all except .st_size)
0293  * for simultaneous readers of the file while we are appending to it.
0294  * However, multiple writers still are bad.  We choose not to use
0295  * locking because it is expensive and the use case of kernel build
0296  * makes multiple writers unlikely.
0297  */
0298 static void *mmap_file(char const *fname)
0299 {
0300     fd_map = open(fname, O_RDONLY);
0301     if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
0302         perror(fname);
0303         fail_file();
0304     }
0305     if (!S_ISREG(sb.st_mode)) {
0306         fprintf(stderr, "not a regular file: %s\n", fname);
0307         fail_file();
0308     }
0309     file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
0310             fd_map, 0);
0311     mmap_failed = 0;
0312     if (file_map == MAP_FAILED) {
0313         mmap_failed = 1;
0314         file_map = umalloc(sb.st_size);
0315         uread(fd_map, file_map, sb.st_size);
0316     }
0317     close(fd_map);
0318 
0319     file_end = file_map + sb.st_size;
0320 
0321     return file_map;
0322 }
0323 
0324 static void write_file(const char *fname)
0325 {
0326     char tmp_file[strlen(fname) + 4];
0327     size_t n;
0328 
0329     if (!file_updated)
0330         return;
0331 
0332     sprintf(tmp_file, "%s.rc", fname);
0333 
0334     /*
0335      * After reading the entire file into memory, delete it
0336      * and write it back, to prevent weird side effects of modifying
0337      * an object file in place.
0338      */
0339     fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
0340     if (fd_map < 0) {
0341         perror(fname);
0342         fail_file();
0343     }
0344     n = write(fd_map, file_map, sb.st_size);
0345     if (n != sb.st_size) {
0346         perror("write");
0347         fail_file();
0348     }
0349     if (file_append_size) {
0350         n = write(fd_map, file_append, file_append_size);
0351         if (n != file_append_size) {
0352             perror("write");
0353             fail_file();
0354         }
0355     }
0356     close(fd_map);
0357     if (rename(tmp_file, fname) < 0) {
0358         perror(fname);
0359         fail_file();
0360     }
0361 }
0362 
0363 /* w8rev, w8nat, ...: Handle endianness. */
0364 
0365 static uint64_t w8rev(uint64_t const x)
0366 {
0367     return   ((0xff & (x >> (0 * 8))) << (7 * 8))
0368            | ((0xff & (x >> (1 * 8))) << (6 * 8))
0369            | ((0xff & (x >> (2 * 8))) << (5 * 8))
0370            | ((0xff & (x >> (3 * 8))) << (4 * 8))
0371            | ((0xff & (x >> (4 * 8))) << (3 * 8))
0372            | ((0xff & (x >> (5 * 8))) << (2 * 8))
0373            | ((0xff & (x >> (6 * 8))) << (1 * 8))
0374            | ((0xff & (x >> (7 * 8))) << (0 * 8));
0375 }
0376 
0377 static uint32_t w4rev(uint32_t const x)
0378 {
0379     return   ((0xff & (x >> (0 * 8))) << (3 * 8))
0380            | ((0xff & (x >> (1 * 8))) << (2 * 8))
0381            | ((0xff & (x >> (2 * 8))) << (1 * 8))
0382            | ((0xff & (x >> (3 * 8))) << (0 * 8));
0383 }
0384 
0385 static uint32_t w2rev(uint16_t const x)
0386 {
0387     return   ((0xff & (x >> (0 * 8))) << (1 * 8))
0388            | ((0xff & (x >> (1 * 8))) << (0 * 8));
0389 }
0390 
0391 static uint64_t w8nat(uint64_t const x)
0392 {
0393     return x;
0394 }
0395 
0396 static uint32_t w4nat(uint32_t const x)
0397 {
0398     return x;
0399 }
0400 
0401 static uint32_t w2nat(uint16_t const x)
0402 {
0403     return x;
0404 }
0405 
0406 static uint64_t (*w8)(uint64_t);
0407 static uint32_t (*w)(uint32_t);
0408 static uint32_t (*w2)(uint16_t);
0409 
0410 /* Names of the sections that could contain calls to mcount. */
0411 static int
0412 is_mcounted_section_name(char const *const txtname)
0413 {
0414     return strcmp(".text",           txtname) == 0 ||
0415         strcmp(".ref.text",      txtname) == 0 ||
0416         strcmp(".sched.text",    txtname) == 0 ||
0417         strcmp(".spinlock.text", txtname) == 0 ||
0418         strcmp(".irqentry.text", txtname) == 0 ||
0419         strcmp(".softirqentry.text", txtname) == 0 ||
0420         strcmp(".kprobes.text", txtname) == 0 ||
0421         strcmp(".cpuidle.text", txtname) == 0 ||
0422         strcmp(".text.unlikely", txtname) == 0;
0423 }
0424 
0425 /* 32 bit and 64 bit are very similar */
0426 #include "recordmcount.h"
0427 #define RECORD_MCOUNT_64
0428 #include "recordmcount.h"
0429 
0430 /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
0431  * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
0432  * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
0433  * to imply the order of the members; the spec does not say so.
0434  *  typedef unsigned char Elf64_Byte;
0435  * fails on MIPS64 because their <elf.h> already has it!
0436  */
0437 
0438 typedef uint8_t myElf64_Byte;       /* Type for a 8-bit quantity.  */
0439 
0440 union mips_r_info {
0441     Elf64_Xword r_info;
0442     struct {
0443         Elf64_Word r_sym;       /* Symbol index.  */
0444         myElf64_Byte r_ssym;        /* Special symbol.  */
0445         myElf64_Byte r_type3;       /* Third relocation.  */
0446         myElf64_Byte r_type2;       /* Second relocation.  */
0447         myElf64_Byte r_type;        /* First relocation.  */
0448     } r_mips;
0449 };
0450 
0451 static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
0452 {
0453     return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
0454 }
0455 
0456 static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
0457 {
0458     rp->r_info = ((union mips_r_info){
0459         .r_mips = { .r_sym = w(sym), .r_type = type }
0460     }).r_info;
0461 }
0462 
0463 static void
0464 do_file(char const *const fname)
0465 {
0466     Elf32_Ehdr *const ehdr = mmap_file(fname);
0467     unsigned int reltype = 0;
0468 
0469     w = w4nat;
0470     w2 = w2nat;
0471     w8 = w8nat;
0472     switch (ehdr->e_ident[EI_DATA]) {
0473         static unsigned int const endian = 1;
0474     default:
0475         fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
0476             ehdr->e_ident[EI_DATA], fname);
0477         fail_file();
0478         break;
0479     case ELFDATA2LSB:
0480         if (*(unsigned char const *)&endian != 1) {
0481             /* main() is big endian, file.o is little endian. */
0482             w = w4rev;
0483             w2 = w2rev;
0484             w8 = w8rev;
0485         }
0486         ideal_nop4_arm = ideal_nop4_arm_le;
0487         bl_mcount_arm = bl_mcount_arm_le;
0488         push_arm = push_arm_le;
0489         ideal_nop2_thumb = ideal_nop2_thumb_le;
0490         push_bl_mcount_thumb = push_bl_mcount_thumb_le;
0491         break;
0492     case ELFDATA2MSB:
0493         if (*(unsigned char const *)&endian != 0) {
0494             /* main() is little endian, file.o is big endian. */
0495             w = w4rev;
0496             w2 = w2rev;
0497             w8 = w8rev;
0498         }
0499         ideal_nop4_arm = ideal_nop4_arm_be;
0500         bl_mcount_arm = bl_mcount_arm_be;
0501         push_arm = push_arm_be;
0502         ideal_nop2_thumb = ideal_nop2_thumb_be;
0503         push_bl_mcount_thumb = push_bl_mcount_thumb_be;
0504         break;
0505     }  /* end switch */
0506     if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
0507     ||  w2(ehdr->e_type) != ET_REL
0508     ||  ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
0509         fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
0510         fail_file();
0511     }
0512 
0513     gpfx = 0;
0514     switch (w2(ehdr->e_machine)) {
0515     default:
0516         fprintf(stderr, "unrecognized e_machine %d %s\n",
0517             w2(ehdr->e_machine), fname);
0518         fail_file();
0519         break;
0520     case EM_386:
0521         reltype = R_386_32;
0522         rel_type_nop = R_386_NONE;
0523         make_nop = make_nop_x86;
0524         ideal_nop = ideal_nop5_x86_32;
0525         mcount_adjust_32 = -1;
0526         break;
0527     case EM_ARM:     reltype = R_ARM_ABS32;
0528              altmcount = "__gnu_mcount_nc";
0529              make_nop = make_nop_arm;
0530              rel_type_nop = R_ARM_NONE;
0531              break;
0532     case EM_AARCH64:
0533             reltype = R_AARCH64_ABS64;
0534             make_nop = make_nop_arm64;
0535             rel_type_nop = R_AARCH64_NONE;
0536             ideal_nop = ideal_nop4_arm64;
0537             gpfx = '_';
0538             break;
0539     case EM_IA_64:   reltype = R_IA64_IMM64;   gpfx = '_'; break;
0540     case EM_METAG:   reltype = R_METAG_ADDR32;
0541              altmcount = "_mcount_wrapper";
0542              rel_type_nop = R_METAG_NONE;
0543              /* We happen to have the same requirement as MIPS */
0544              is_fake_mcount32 = MIPS32_is_fake_mcount;
0545              break;
0546     case EM_MIPS:    /* reltype: e_class    */ gpfx = '_'; break;
0547     case EM_PPC:     reltype = R_PPC_ADDR32;   gpfx = '_'; break;
0548     case EM_PPC64:   reltype = R_PPC64_ADDR64; gpfx = '_'; break;
0549     case EM_S390:    /* reltype: e_class    */ gpfx = '_'; break;
0550     case EM_SH:  reltype = R_SH_DIR32;                 break;
0551     case EM_SPARCV9: reltype = R_SPARC_64;     gpfx = '_'; break;
0552     case EM_X86_64:
0553         make_nop = make_nop_x86;
0554         ideal_nop = ideal_nop5_x86_64;
0555         reltype = R_X86_64_64;
0556         rel_type_nop = R_X86_64_NONE;
0557         mcount_adjust_64 = -1;
0558         break;
0559     }  /* end switch */
0560 
0561     switch (ehdr->e_ident[EI_CLASS]) {
0562     default:
0563         fprintf(stderr, "unrecognized ELF class %d %s\n",
0564             ehdr->e_ident[EI_CLASS], fname);
0565         fail_file();
0566         break;
0567     case ELFCLASS32:
0568         if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
0569         ||  w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
0570             fprintf(stderr,
0571                 "unrecognized ET_REL file: %s\n", fname);
0572             fail_file();
0573         }
0574         if (w2(ehdr->e_machine) == EM_MIPS) {
0575             reltype = R_MIPS_32;
0576             is_fake_mcount32 = MIPS32_is_fake_mcount;
0577         }
0578         do32(ehdr, fname, reltype);
0579         break;
0580     case ELFCLASS64: {
0581         Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
0582         if (w2(ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
0583         ||  w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
0584             fprintf(stderr,
0585                 "unrecognized ET_REL file: %s\n", fname);
0586             fail_file();
0587         }
0588         if (w2(ghdr->e_machine) == EM_S390) {
0589             reltype = R_390_64;
0590             mcount_adjust_64 = -14;
0591         }
0592         if (w2(ghdr->e_machine) == EM_MIPS) {
0593             reltype = R_MIPS_64;
0594             Elf64_r_sym = MIPS64_r_sym;
0595             Elf64_r_info = MIPS64_r_info;
0596             is_fake_mcount64 = MIPS64_is_fake_mcount;
0597         }
0598         do64(ghdr, fname, reltype);
0599         break;
0600     }
0601     }  /* end switch */
0602 
0603     write_file(fname);
0604     cleanup();
0605 }
0606 
0607 int
0608 main(int argc, char *argv[])
0609 {
0610     const char ftrace[] = "/ftrace.o";
0611     int ftrace_size = sizeof(ftrace) - 1;
0612     int n_error = 0;  /* gcc-4.3.0 false positive complaint */
0613     int c;
0614     int i;
0615 
0616     while ((c = getopt(argc, argv, "w")) >= 0) {
0617         switch (c) {
0618         case 'w':
0619             warn_on_notrace_sect = 1;
0620             break;
0621         default:
0622             fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
0623             return 0;
0624         }
0625     }
0626 
0627     if ((argc - optind) < 1) {
0628         fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
0629         return 0;
0630     }
0631 
0632     /* Process each file in turn, allowing deep failure. */
0633     for (i = optind; i < argc; i++) {
0634         char *file = argv[i];
0635         int const sjval = setjmp(jmpenv);
0636         int len;
0637 
0638         /*
0639          * The file kernel/trace/ftrace.o references the mcount
0640          * function but does not call it. Since ftrace.o should
0641          * not be traced anyway, we just skip it.
0642          */
0643         len = strlen(file);
0644         if (len >= ftrace_size &&
0645             strcmp(file + (len - ftrace_size), ftrace) == 0)
0646             continue;
0647 
0648         switch (sjval) {
0649         default:
0650             fprintf(stderr, "internal error: %s\n", file);
0651             exit(1);
0652             break;
0653         case SJ_SETJMP:    /* normal sequence */
0654             /* Avoid problems if early cleanup() */
0655             fd_map = -1;
0656             mmap_failed = 1;
0657             file_map = NULL;
0658             file_ptr = NULL;
0659             file_updated = 0;
0660             do_file(file);
0661             break;
0662         case SJ_FAIL:    /* error in do_file or below */
0663             fprintf(stderr, "%s: failed\n", file);
0664             ++n_error;
0665             break;
0666         case SJ_SUCCEED:    /* premature success */
0667             /* do nothing */
0668             break;
0669         }  /* end switch */
0670     }
0671     return !!n_error;
0672 }