recordmcount.c 15 KB

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