recordmcount.c 17 KB

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