recordmcount.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
  196. * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
  197. * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
  198. * to imply the order of the members; the spec does not say so.
  199. * typedef unsigned char Elf64_Byte;
  200. * fails on MIPS64 because their <elf.h> already has it!
  201. */
  202. typedef uint8_t myElf64_Byte; /* Type for a 8-bit quantity. */
  203. union mips_r_info {
  204. Elf64_Xword r_info;
  205. struct {
  206. Elf64_Word r_sym; /* Symbol index. */
  207. myElf64_Byte r_ssym; /* Special symbol. */
  208. myElf64_Byte r_type3; /* Third relocation. */
  209. myElf64_Byte r_type2; /* Second relocation. */
  210. myElf64_Byte r_type; /* First relocation. */
  211. } r_mips;
  212. };
  213. static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
  214. {
  215. return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
  216. }
  217. static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
  218. {
  219. rp->r_info = ((union mips_r_info){
  220. .r_mips = { .r_sym = w(sym), .r_type = type }
  221. }).r_info;
  222. }
  223. static void
  224. do_file(char const *const fname)
  225. {
  226. Elf32_Ehdr *const ehdr = mmap_file(fname);
  227. unsigned int reltype = 0;
  228. ehdr_curr = ehdr;
  229. w = w4nat;
  230. w2 = w2nat;
  231. w8 = w8nat;
  232. switch (ehdr->e_ident[EI_DATA]) {
  233. static unsigned int const endian = 1;
  234. default: {
  235. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  236. ehdr->e_ident[EI_DATA], fname);
  237. fail_file();
  238. } break;
  239. case ELFDATA2LSB: {
  240. if (1 != *(unsigned char const *)&endian) {
  241. /* main() is big endian, file.o is little endian. */
  242. w = w4rev;
  243. w2 = w2rev;
  244. w8 = w8rev;
  245. }
  246. } break;
  247. case ELFDATA2MSB: {
  248. if (0 != *(unsigned char const *)&endian) {
  249. /* main() is little endian, file.o is big endian. */
  250. w = w4rev;
  251. w2 = w2rev;
  252. w8 = w8rev;
  253. }
  254. } break;
  255. } /* end switch */
  256. if (0 != memcmp(ELFMAG, ehdr->e_ident, SELFMAG)
  257. || ET_REL != w2(ehdr->e_type)
  258. || EV_CURRENT != ehdr->e_ident[EI_VERSION]) {
  259. fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
  260. fail_file();
  261. }
  262. gpfx = 0;
  263. switch (w2(ehdr->e_machine)) {
  264. default: {
  265. fprintf(stderr, "unrecognized e_machine %d %s\n",
  266. w2(ehdr->e_machine), fname);
  267. fail_file();
  268. } break;
  269. case EM_386: reltype = R_386_32; break;
  270. case EM_ARM: reltype = R_ARM_ABS32; break;
  271. case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break;
  272. case EM_MIPS: /* reltype: e_class */ gpfx = '_'; break;
  273. case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break;
  274. case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break;
  275. case EM_S390: /* reltype: e_class */ gpfx = '_'; break;
  276. case EM_SH: reltype = R_SH_DIR32; break;
  277. case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break;
  278. case EM_X86_64: reltype = R_X86_64_64; break;
  279. } /* end switch */
  280. switch (ehdr->e_ident[EI_CLASS]) {
  281. default: {
  282. fprintf(stderr, "unrecognized ELF class %d %s\n",
  283. ehdr->e_ident[EI_CLASS], fname);
  284. fail_file();
  285. } break;
  286. case ELFCLASS32: {
  287. if (sizeof(Elf32_Ehdr) != w2(ehdr->e_ehsize)
  288. || sizeof(Elf32_Shdr) != w2(ehdr->e_shentsize)) {
  289. fprintf(stderr,
  290. "unrecognized ET_REL file: %s\n", fname);
  291. fail_file();
  292. }
  293. if (EM_S390 == w2(ehdr->e_machine))
  294. reltype = R_390_32;
  295. if (EM_MIPS == w2(ehdr->e_machine)) {
  296. reltype = R_MIPS_32;
  297. is_fake_mcount32 = MIPS32_is_fake_mcount;
  298. }
  299. do32(ehdr, fname, reltype);
  300. } break;
  301. case ELFCLASS64: {
  302. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  303. if (sizeof(Elf64_Ehdr) != w2(ghdr->e_ehsize)
  304. || sizeof(Elf64_Shdr) != w2(ghdr->e_shentsize)) {
  305. fprintf(stderr,
  306. "unrecognized ET_REL file: %s\n", fname);
  307. fail_file();
  308. }
  309. if (EM_S390 == w2(ghdr->e_machine))
  310. reltype = R_390_64;
  311. if (EM_MIPS == w2(ghdr->e_machine)) {
  312. reltype = R_MIPS_64;
  313. Elf64_r_sym = MIPS64_r_sym;
  314. Elf64_r_info = MIPS64_r_info;
  315. is_fake_mcount64 = MIPS64_is_fake_mcount;
  316. }
  317. do64(ghdr, fname, reltype);
  318. } break;
  319. } /* end switch */
  320. cleanup();
  321. }
  322. int
  323. main(int argc, char const *argv[])
  324. {
  325. const char ftrace[] = "kernel/trace/ftrace.o";
  326. int ftrace_size = sizeof(ftrace) - 1;
  327. int n_error = 0; /* gcc-4.3.0 false positive complaint */
  328. if (argc <= 1) {
  329. fprintf(stderr, "usage: recordmcount file.o...\n");
  330. return 0;
  331. }
  332. /* Process each file in turn, allowing deep failure. */
  333. for (--argc, ++argv; 0 < argc; --argc, ++argv) {
  334. int const sjval = setjmp(jmpenv);
  335. int len;
  336. /*
  337. * The file kernel/trace/ftrace.o references the mcount
  338. * function but does not call it. Since ftrace.o should
  339. * not be traced anyway, we just skip it.
  340. */
  341. len = strlen(argv[0]);
  342. if (len >= ftrace_size &&
  343. strcmp(argv[0] + (len - ftrace_size), ftrace) == 0)
  344. continue;
  345. switch (sjval) {
  346. default: {
  347. fprintf(stderr, "internal error: %s\n", argv[0]);
  348. exit(1);
  349. } break;
  350. case SJ_SETJMP: { /* normal sequence */
  351. /* Avoid problems if early cleanup() */
  352. fd_map = -1;
  353. ehdr_curr = NULL;
  354. mmap_failed = 1;
  355. do_file(argv[0]);
  356. } break;
  357. case SJ_FAIL: { /* error in do_file or below */
  358. ++n_error;
  359. } break;
  360. case SJ_SUCCEED: { /* premature success */
  361. /* do nothing */
  362. } break;
  363. } /* end switch */
  364. }
  365. return !!n_error;
  366. }