recordmcount.c 9.3 KB

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