recordmcount.c 12 KB

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