recordmcount.c 18 KB

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