recordmcount.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. /*
  34. * glibc synced up and added the metag number but didn't add the relocations.
  35. * Work around this in a crude manner for now.
  36. */
  37. #ifndef EM_METAG
  38. #define EM_METAG 174
  39. #endif
  40. #ifndef R_METAG_ADDR32
  41. #define R_METAG_ADDR32 2
  42. #endif
  43. #ifndef R_METAG_NONE
  44. #define R_METAG_NONE 3
  45. #endif
  46. #ifndef EM_AARCH64
  47. #define EM_AARCH64 183
  48. #define R_AARCH64_NONE 0
  49. #define R_AARCH64_ABS64 257
  50. #endif
  51. #define R_ARM_PC24 1
  52. #define R_ARM_THM_CALL 10
  53. #define R_ARM_CALL 28
  54. static int fd_map; /* File descriptor for file being modified. */
  55. static int mmap_failed; /* Boolean flag. */
  56. static char gpfx; /* prefix for global symbol name (sometimes '_') */
  57. static struct stat sb; /* Remember .st_size, etc. */
  58. static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
  59. static const char *altmcount; /* alternate mcount symbol name */
  60. static int warn_on_notrace_sect; /* warn when section has mcount not being recorded */
  61. static void *file_map; /* pointer of the mapped file */
  62. static void *file_end; /* pointer to the end of the mapped file */
  63. static int file_updated; /* flag to state file was changed */
  64. static void *file_ptr; /* current file pointer location */
  65. static void *file_append; /* added to the end of the file */
  66. static size_t file_append_size; /* how much is added to end of file */
  67. /* setjmp() return values */
  68. enum {
  69. SJ_SETJMP = 0, /* hardwired first return */
  70. SJ_FAIL,
  71. SJ_SUCCEED
  72. };
  73. /* Per-file resource cleanup when multiple files. */
  74. static void
  75. cleanup(void)
  76. {
  77. if (!mmap_failed)
  78. munmap(file_map, sb.st_size);
  79. else
  80. free(file_map);
  81. file_map = NULL;
  82. free(file_append);
  83. file_append = NULL;
  84. file_append_size = 0;
  85. file_updated = 0;
  86. }
  87. static void __attribute__((noreturn))
  88. fail_file(void)
  89. {
  90. cleanup();
  91. longjmp(jmpenv, SJ_FAIL);
  92. }
  93. static void __attribute__((noreturn))
  94. succeed_file(void)
  95. {
  96. cleanup();
  97. longjmp(jmpenv, SJ_SUCCEED);
  98. }
  99. /* ulseek, uread, ...: Check return value for errors. */
  100. static off_t
  101. ulseek(int const fd, off_t const offset, int const whence)
  102. {
  103. switch (whence) {
  104. case SEEK_SET:
  105. file_ptr = file_map + offset;
  106. break;
  107. case SEEK_CUR:
  108. file_ptr += offset;
  109. break;
  110. case SEEK_END:
  111. file_ptr = file_map + (sb.st_size - offset);
  112. break;
  113. }
  114. if (file_ptr < file_map) {
  115. fprintf(stderr, "lseek: seek before file\n");
  116. fail_file();
  117. }
  118. return file_ptr - file_map;
  119. }
  120. static size_t
  121. uread(int const fd, void *const buf, size_t const count)
  122. {
  123. size_t const n = read(fd, buf, count);
  124. if (n != count) {
  125. perror("read");
  126. fail_file();
  127. }
  128. return n;
  129. }
  130. static size_t
  131. uwrite(int const fd, void const *const buf, size_t const count)
  132. {
  133. size_t cnt = count;
  134. off_t idx = 0;
  135. file_updated = 1;
  136. if (file_ptr + count >= file_end) {
  137. off_t aoffset = (file_ptr + count) - file_end;
  138. if (aoffset > file_append_size) {
  139. file_append = realloc(file_append, aoffset);
  140. file_append_size = aoffset;
  141. }
  142. if (!file_append) {
  143. perror("write");
  144. fail_file();
  145. }
  146. if (file_ptr < file_end) {
  147. cnt = file_end - file_ptr;
  148. } else {
  149. cnt = 0;
  150. idx = aoffset - count;
  151. }
  152. }
  153. if (cnt)
  154. memcpy(file_ptr, buf, cnt);
  155. if (cnt < count)
  156. memcpy(file_append + idx, buf + cnt, count - cnt);
  157. file_ptr += count;
  158. return count;
  159. }
  160. static void *
  161. umalloc(size_t size)
  162. {
  163. void *const addr = malloc(size);
  164. if (addr == 0) {
  165. fprintf(stderr, "malloc failed: %zu bytes\n", size);
  166. fail_file();
  167. }
  168. return addr;
  169. }
  170. static unsigned char ideal_nop5_x86_64[5] = { 0x0f, 0x1f, 0x44, 0x00, 0x00 };
  171. static unsigned char ideal_nop5_x86_32[5] = { 0x3e, 0x8d, 0x74, 0x26, 0x00 };
  172. static unsigned char *ideal_nop;
  173. static char rel_type_nop;
  174. static int (*make_nop)(void *map, size_t const offset);
  175. static int make_nop_x86(void *map, size_t const offset)
  176. {
  177. uint32_t *ptr;
  178. unsigned char *op;
  179. /* Confirm we have 0xe8 0x0 0x0 0x0 0x0 */
  180. ptr = map + offset;
  181. if (*ptr != 0)
  182. return -1;
  183. op = map + offset - 1;
  184. if (*op != 0xe8)
  185. return -1;
  186. /* convert to nop */
  187. ulseek(fd_map, offset - 1, SEEK_SET);
  188. uwrite(fd_map, ideal_nop, 5);
  189. return 0;
  190. }
  191. static unsigned char ideal_nop4_arm_le[4] = { 0x00, 0x00, 0xa0, 0xe1 }; /* mov r0, r0 */
  192. static unsigned char ideal_nop4_arm_be[4] = { 0xe1, 0xa0, 0x00, 0x00 }; /* mov r0, r0 */
  193. static unsigned char *ideal_nop4_arm;
  194. static unsigned char bl_mcount_arm_le[4] = { 0xfe, 0xff, 0xff, 0xeb }; /* bl */
  195. static unsigned char bl_mcount_arm_be[4] = { 0xeb, 0xff, 0xff, 0xfe }; /* bl */
  196. static unsigned char *bl_mcount_arm;
  197. static unsigned char push_arm_le[4] = { 0x04, 0xe0, 0x2d, 0xe5 }; /* push {lr} */
  198. static unsigned char push_arm_be[4] = { 0xe5, 0x2d, 0xe0, 0x04 }; /* push {lr} */
  199. static unsigned char *push_arm;
  200. static unsigned char ideal_nop2_thumb_le[2] = { 0x00, 0xbf }; /* nop */
  201. static unsigned char ideal_nop2_thumb_be[2] = { 0xbf, 0x00 }; /* nop */
  202. static unsigned char *ideal_nop2_thumb;
  203. static unsigned char push_bl_mcount_thumb_le[6] = { 0x00, 0xb5, 0xff, 0xf7, 0xfe, 0xff }; /* push {lr}, bl */
  204. static unsigned char push_bl_mcount_thumb_be[6] = { 0xb5, 0x00, 0xf7, 0xff, 0xff, 0xfe }; /* push {lr}, bl */
  205. static unsigned char *push_bl_mcount_thumb;
  206. static int make_nop_arm(void *map, size_t const offset)
  207. {
  208. char *ptr;
  209. int cnt = 1;
  210. int nop_size;
  211. size_t off = offset;
  212. ptr = map + offset;
  213. if (memcmp(ptr, bl_mcount_arm, 4) == 0) {
  214. if (memcmp(ptr - 4, push_arm, 4) == 0) {
  215. off -= 4;
  216. cnt = 2;
  217. }
  218. ideal_nop = ideal_nop4_arm;
  219. nop_size = 4;
  220. } else if (memcmp(ptr - 2, push_bl_mcount_thumb, 6) == 0) {
  221. cnt = 3;
  222. nop_size = 2;
  223. off -= 2;
  224. ideal_nop = ideal_nop2_thumb;
  225. } else
  226. return -1;
  227. /* Convert to nop */
  228. ulseek(fd_map, off, SEEK_SET);
  229. do {
  230. uwrite(fd_map, ideal_nop, nop_size);
  231. } while (--cnt > 0);
  232. return 0;
  233. }
  234. static unsigned char ideal_nop4_arm64[4] = {0x1f, 0x20, 0x03, 0xd5};
  235. static int make_nop_arm64(void *map, size_t const offset)
  236. {
  237. uint32_t *ptr;
  238. ptr = map + offset;
  239. /* bl <_mcount> is 0x94000000 before relocation */
  240. if (*ptr != 0x94000000)
  241. return -1;
  242. /* Convert to nop */
  243. ulseek(fd_map, offset, SEEK_SET);
  244. uwrite(fd_map, ideal_nop, 4);
  245. return 0;
  246. }
  247. /*
  248. * Get the whole file as a programming convenience in order to avoid
  249. * malloc+lseek+read+free of many pieces. If successful, then mmap
  250. * avoids copying unused pieces; else just read the whole file.
  251. * Open for both read and write; new info will be appended to the file.
  252. * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
  253. * do not propagate to the file until an explicit overwrite at the last.
  254. * This preserves most aspects of consistency (all except .st_size)
  255. * for simultaneous readers of the file while we are appending to it.
  256. * However, multiple writers still are bad. We choose not to use
  257. * locking because it is expensive and the use case of kernel build
  258. * makes multiple writers unlikely.
  259. */
  260. static void *mmap_file(char const *fname)
  261. {
  262. fd_map = open(fname, O_RDONLY);
  263. if (fd_map < 0 || fstat(fd_map, &sb) < 0) {
  264. perror(fname);
  265. fail_file();
  266. }
  267. if (!S_ISREG(sb.st_mode)) {
  268. fprintf(stderr, "not a regular file: %s\n", fname);
  269. fail_file();
  270. }
  271. file_map = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
  272. fd_map, 0);
  273. mmap_failed = 0;
  274. if (file_map == MAP_FAILED) {
  275. mmap_failed = 1;
  276. file_map = umalloc(sb.st_size);
  277. uread(fd_map, file_map, sb.st_size);
  278. }
  279. close(fd_map);
  280. file_end = file_map + sb.st_size;
  281. return file_map;
  282. }
  283. static void write_file(const char *fname)
  284. {
  285. char tmp_file[strlen(fname) + 4];
  286. size_t n;
  287. if (!file_updated)
  288. return;
  289. sprintf(tmp_file, "%s.rc", fname);
  290. /*
  291. * After reading the entire file into memory, delete it
  292. * and write it back, to prevent weird side effects of modifying
  293. * an object file in place.
  294. */
  295. fd_map = open(tmp_file, O_WRONLY | O_TRUNC | O_CREAT, sb.st_mode);
  296. if (fd_map < 0) {
  297. perror(fname);
  298. fail_file();
  299. }
  300. n = write(fd_map, file_map, sb.st_size);
  301. if (n != sb.st_size) {
  302. perror("write");
  303. fail_file();
  304. }
  305. if (file_append_size) {
  306. n = write(fd_map, file_append, file_append_size);
  307. if (n != file_append_size) {
  308. perror("write");
  309. fail_file();
  310. }
  311. }
  312. close(fd_map);
  313. if (rename(tmp_file, fname) < 0) {
  314. perror(fname);
  315. fail_file();
  316. }
  317. }
  318. /* w8rev, w8nat, ...: Handle endianness. */
  319. static uint64_t w8rev(uint64_t const x)
  320. {
  321. return ((0xff & (x >> (0 * 8))) << (7 * 8))
  322. | ((0xff & (x >> (1 * 8))) << (6 * 8))
  323. | ((0xff & (x >> (2 * 8))) << (5 * 8))
  324. | ((0xff & (x >> (3 * 8))) << (4 * 8))
  325. | ((0xff & (x >> (4 * 8))) << (3 * 8))
  326. | ((0xff & (x >> (5 * 8))) << (2 * 8))
  327. | ((0xff & (x >> (6 * 8))) << (1 * 8))
  328. | ((0xff & (x >> (7 * 8))) << (0 * 8));
  329. }
  330. static uint32_t w4rev(uint32_t const x)
  331. {
  332. return ((0xff & (x >> (0 * 8))) << (3 * 8))
  333. | ((0xff & (x >> (1 * 8))) << (2 * 8))
  334. | ((0xff & (x >> (2 * 8))) << (1 * 8))
  335. | ((0xff & (x >> (3 * 8))) << (0 * 8));
  336. }
  337. static uint32_t w2rev(uint16_t const x)
  338. {
  339. return ((0xff & (x >> (0 * 8))) << (1 * 8))
  340. | ((0xff & (x >> (1 * 8))) << (0 * 8));
  341. }
  342. static uint64_t w8nat(uint64_t const x)
  343. {
  344. return x;
  345. }
  346. static uint32_t w4nat(uint32_t const x)
  347. {
  348. return x;
  349. }
  350. static uint32_t w2nat(uint16_t const x)
  351. {
  352. return x;
  353. }
  354. static uint64_t (*w8)(uint64_t);
  355. static uint32_t (*w)(uint32_t);
  356. static uint32_t (*w2)(uint16_t);
  357. /* Names of the sections that could contain calls to mcount. */
  358. static int
  359. is_mcounted_section_name(char const *const txtname)
  360. {
  361. return strncmp(".text", txtname, 5) == 0 ||
  362. strcmp(".init.text", txtname) == 0 ||
  363. strcmp(".ref.text", txtname) == 0 ||
  364. strcmp(".sched.text", txtname) == 0 ||
  365. strcmp(".spinlock.text", txtname) == 0 ||
  366. strcmp(".irqentry.text", txtname) == 0 ||
  367. strcmp(".softirqentry.text", txtname) == 0 ||
  368. strcmp(".kprobes.text", txtname) == 0 ||
  369. strcmp(".cpuidle.text", txtname) == 0 ||
  370. (strncmp(".text.", txtname, 6) == 0 &&
  371. strcmp(".text..ftrace", txtname) != 0);
  372. }
  373. /* 32 bit and 64 bit are very similar */
  374. #include "recordmcount.h"
  375. #define RECORD_MCOUNT_64
  376. #include "recordmcount.h"
  377. static int arm_is_fake_mcount(Elf32_Rel const *rp)
  378. {
  379. switch (ELF32_R_TYPE(w(rp->r_info))) {
  380. case R_ARM_THM_CALL:
  381. case R_ARM_CALL:
  382. case R_ARM_PC24:
  383. return 0;
  384. }
  385. return 1;
  386. }
  387. /* 64-bit EM_MIPS has weird ELF64_Rela.r_info.
  388. * http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf
  389. * We interpret Table 29 Relocation Operation (Elf64_Rel, Elf64_Rela) [p.40]
  390. * to imply the order of the members; the spec does not say so.
  391. * typedef unsigned char Elf64_Byte;
  392. * fails on MIPS64 because their <elf.h> already has it!
  393. */
  394. typedef uint8_t myElf64_Byte; /* Type for a 8-bit quantity. */
  395. union mips_r_info {
  396. Elf64_Xword r_info;
  397. struct {
  398. Elf64_Word r_sym; /* Symbol index. */
  399. myElf64_Byte r_ssym; /* Special symbol. */
  400. myElf64_Byte r_type3; /* Third relocation. */
  401. myElf64_Byte r_type2; /* Second relocation. */
  402. myElf64_Byte r_type; /* First relocation. */
  403. } r_mips;
  404. };
  405. static uint64_t MIPS64_r_sym(Elf64_Rel const *rp)
  406. {
  407. return w(((union mips_r_info){ .r_info = rp->r_info }).r_mips.r_sym);
  408. }
  409. static void MIPS64_r_info(Elf64_Rel *const rp, unsigned sym, unsigned type)
  410. {
  411. rp->r_info = ((union mips_r_info){
  412. .r_mips = { .r_sym = w(sym), .r_type = type }
  413. }).r_info;
  414. }
  415. static void
  416. do_file(char const *const fname)
  417. {
  418. Elf32_Ehdr *const ehdr = mmap_file(fname);
  419. unsigned int reltype = 0;
  420. w = w4nat;
  421. w2 = w2nat;
  422. w8 = w8nat;
  423. switch (ehdr->e_ident[EI_DATA]) {
  424. static unsigned int const endian = 1;
  425. default:
  426. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  427. ehdr->e_ident[EI_DATA], fname);
  428. fail_file();
  429. break;
  430. case ELFDATA2LSB:
  431. if (*(unsigned char const *)&endian != 1) {
  432. /* main() is big endian, file.o is little endian. */
  433. w = w4rev;
  434. w2 = w2rev;
  435. w8 = w8rev;
  436. }
  437. ideal_nop4_arm = ideal_nop4_arm_le;
  438. bl_mcount_arm = bl_mcount_arm_le;
  439. push_arm = push_arm_le;
  440. ideal_nop2_thumb = ideal_nop2_thumb_le;
  441. push_bl_mcount_thumb = push_bl_mcount_thumb_le;
  442. break;
  443. case ELFDATA2MSB:
  444. if (*(unsigned char const *)&endian != 0) {
  445. /* main() is little endian, file.o is big endian. */
  446. w = w4rev;
  447. w2 = w2rev;
  448. w8 = w8rev;
  449. }
  450. ideal_nop4_arm = ideal_nop4_arm_be;
  451. bl_mcount_arm = bl_mcount_arm_be;
  452. push_arm = push_arm_be;
  453. ideal_nop2_thumb = ideal_nop2_thumb_be;
  454. push_bl_mcount_thumb = push_bl_mcount_thumb_be;
  455. break;
  456. } /* end switch */
  457. if (memcmp(ELFMAG, ehdr->e_ident, SELFMAG) != 0
  458. || w2(ehdr->e_type) != ET_REL
  459. || ehdr->e_ident[EI_VERSION] != EV_CURRENT) {
  460. fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
  461. fail_file();
  462. }
  463. gpfx = 0;
  464. switch (w2(ehdr->e_machine)) {
  465. default:
  466. fprintf(stderr, "unrecognized e_machine %d %s\n",
  467. w2(ehdr->e_machine), fname);
  468. fail_file();
  469. break;
  470. case EM_386:
  471. reltype = R_386_32;
  472. rel_type_nop = R_386_NONE;
  473. make_nop = make_nop_x86;
  474. ideal_nop = ideal_nop5_x86_32;
  475. mcount_adjust_32 = -1;
  476. break;
  477. case EM_ARM: reltype = R_ARM_ABS32;
  478. altmcount = "__gnu_mcount_nc";
  479. make_nop = make_nop_arm;
  480. rel_type_nop = R_ARM_NONE;
  481. is_fake_mcount32 = arm_is_fake_mcount;
  482. break;
  483. case EM_AARCH64:
  484. reltype = R_AARCH64_ABS64;
  485. make_nop = make_nop_arm64;
  486. rel_type_nop = R_AARCH64_NONE;
  487. ideal_nop = ideal_nop4_arm64;
  488. gpfx = '_';
  489. break;
  490. case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break;
  491. case EM_METAG: reltype = R_METAG_ADDR32;
  492. altmcount = "_mcount_wrapper";
  493. rel_type_nop = R_METAG_NONE;
  494. /* We happen to have the same requirement as MIPS */
  495. is_fake_mcount32 = MIPS32_is_fake_mcount;
  496. break;
  497. case EM_MIPS: /* reltype: e_class */ gpfx = '_'; break;
  498. case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break;
  499. case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break;
  500. case EM_S390: /* reltype: e_class */ gpfx = '_'; break;
  501. case EM_SH: reltype = R_SH_DIR32; break;
  502. case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break;
  503. case EM_X86_64:
  504. make_nop = make_nop_x86;
  505. ideal_nop = ideal_nop5_x86_64;
  506. reltype = R_X86_64_64;
  507. rel_type_nop = R_X86_64_NONE;
  508. mcount_adjust_64 = -1;
  509. break;
  510. } /* end switch */
  511. switch (ehdr->e_ident[EI_CLASS]) {
  512. default:
  513. fprintf(stderr, "unrecognized ELF class %d %s\n",
  514. ehdr->e_ident[EI_CLASS], fname);
  515. fail_file();
  516. break;
  517. case ELFCLASS32:
  518. if (w2(ehdr->e_ehsize) != sizeof(Elf32_Ehdr)
  519. || w2(ehdr->e_shentsize) != sizeof(Elf32_Shdr)) {
  520. fprintf(stderr,
  521. "unrecognized ET_REL file: %s\n", fname);
  522. fail_file();
  523. }
  524. if (w2(ehdr->e_machine) == EM_MIPS) {
  525. reltype = R_MIPS_32;
  526. is_fake_mcount32 = MIPS32_is_fake_mcount;
  527. }
  528. do32(ehdr, fname, reltype);
  529. break;
  530. case ELFCLASS64: {
  531. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  532. if (w2(ghdr->e_ehsize) != sizeof(Elf64_Ehdr)
  533. || w2(ghdr->e_shentsize) != sizeof(Elf64_Shdr)) {
  534. fprintf(stderr,
  535. "unrecognized ET_REL file: %s\n", fname);
  536. fail_file();
  537. }
  538. if (w2(ghdr->e_machine) == EM_S390) {
  539. reltype = R_390_64;
  540. mcount_adjust_64 = -14;
  541. }
  542. if (w2(ghdr->e_machine) == EM_MIPS) {
  543. reltype = R_MIPS_64;
  544. Elf64_r_sym = MIPS64_r_sym;
  545. Elf64_r_info = MIPS64_r_info;
  546. is_fake_mcount64 = MIPS64_is_fake_mcount;
  547. }
  548. do64(ghdr, fname, reltype);
  549. break;
  550. }
  551. } /* end switch */
  552. write_file(fname);
  553. cleanup();
  554. }
  555. int
  556. main(int argc, char *argv[])
  557. {
  558. const char ftrace[] = "/ftrace.o";
  559. int ftrace_size = sizeof(ftrace) - 1;
  560. int n_error = 0; /* gcc-4.3.0 false positive complaint */
  561. int c;
  562. int i;
  563. while ((c = getopt(argc, argv, "w")) >= 0) {
  564. switch (c) {
  565. case 'w':
  566. warn_on_notrace_sect = 1;
  567. break;
  568. default:
  569. fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
  570. return 0;
  571. }
  572. }
  573. if ((argc - optind) < 1) {
  574. fprintf(stderr, "usage: recordmcount [-w] file.o...\n");
  575. return 0;
  576. }
  577. /* Process each file in turn, allowing deep failure. */
  578. for (i = optind; i < argc; i++) {
  579. char *file = argv[i];
  580. int const sjval = setjmp(jmpenv);
  581. int len;
  582. /*
  583. * The file kernel/trace/ftrace.o references the mcount
  584. * function but does not call it. Since ftrace.o should
  585. * not be traced anyway, we just skip it.
  586. */
  587. len = strlen(file);
  588. if (len >= ftrace_size &&
  589. strcmp(file + (len - ftrace_size), ftrace) == 0)
  590. continue;
  591. switch (sjval) {
  592. default:
  593. fprintf(stderr, "internal error: %s\n", file);
  594. exit(1);
  595. break;
  596. case SJ_SETJMP: /* normal sequence */
  597. /* Avoid problems if early cleanup() */
  598. fd_map = -1;
  599. mmap_failed = 1;
  600. file_map = NULL;
  601. file_ptr = NULL;
  602. file_updated = 0;
  603. do_file(file);
  604. break;
  605. case SJ_FAIL: /* error in do_file or below */
  606. fprintf(stderr, "%s: failed\n", file);
  607. ++n_error;
  608. break;
  609. case SJ_SUCCEED: /* premature success */
  610. /* do nothing */
  611. break;
  612. } /* end switch */
  613. }
  614. return !!n_error;
  615. }