recordmcount.c 14 KB

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