recordmcount.c 17 KB

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