recordmcount.c 17 KB

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