PageRenderTime 43ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/scripts/recordmcount.c

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