recordmcount.c 17 KB

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