recordmcount.c 17 KB

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