recordmcount.c 11 KB

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