recordmcount.c 12 KB

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