recordmcount.c 11 KB

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