recordmcount.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * recordmcount.h
  4. *
  5. * This code was taken out of recordmcount.c written by
  6. * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
  7. *
  8. * The original code had the same algorithms for both 32bit
  9. * and 64bit ELF files, but the code was duplicated to support
  10. * the difference in structures that were used. This
  11. * file creates a macro of everything that is different between
  12. * the 64 and 32 bit code, such that by including this header
  13. * twice we can create both sets of functions by including this
  14. * header once with RECORD_MCOUNT_64 undefined, and again with
  15. * it defined.
  16. *
  17. * This conversion to macros was done by:
  18. * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  19. */
  20. #undef append_func
  21. #undef is_fake_mcount
  22. #undef fn_is_fake_mcount
  23. #undef MIPS_is_fake_mcount
  24. #undef mcount_adjust
  25. #undef sift_rel_mcount
  26. #undef nop_mcount
  27. #undef find_secsym_ndx
  28. #undef __has_rel_mcount
  29. #undef has_rel_mcount
  30. #undef tot_relsize
  31. #undef get_mcountsym
  32. #undef find_symtab
  33. #undef get_shnum
  34. #undef set_shnum
  35. #undef get_shstrndx
  36. #undef get_symindex
  37. #undef get_sym_str_and_relp
  38. #undef do_func
  39. #undef Elf_Addr
  40. #undef Elf_Ehdr
  41. #undef Elf_Shdr
  42. #undef Elf_Rel
  43. #undef Elf_Rela
  44. #undef Elf_Sym
  45. #undef ELF_R_SYM
  46. #undef Elf_r_sym
  47. #undef ELF_R_INFO
  48. #undef Elf_r_info
  49. #undef ELF_ST_BIND
  50. #undef ELF_ST_TYPE
  51. #undef fn_ELF_R_SYM
  52. #undef fn_ELF_R_INFO
  53. #undef uint_t
  54. #undef _w
  55. #undef _align
  56. #undef _size
  57. #ifdef RECORD_MCOUNT_64
  58. # define append_func append64
  59. # define sift_rel_mcount sift64_rel_mcount
  60. # define nop_mcount nop_mcount_64
  61. # define find_secsym_ndx find64_secsym_ndx
  62. # define __has_rel_mcount __has64_rel_mcount
  63. # define has_rel_mcount has64_rel_mcount
  64. # define tot_relsize tot64_relsize
  65. # define find_symtab find_symtab64
  66. # define get_shnum get_shnum64
  67. # define set_shnum set_shnum64
  68. # define get_shstrndx get_shstrndx64
  69. # define get_symindex get_symindex64
  70. # define get_sym_str_and_relp get_sym_str_and_relp_64
  71. # define do_func do64
  72. # define get_mcountsym get_mcountsym_64
  73. # define is_fake_mcount is_fake_mcount64
  74. # define fn_is_fake_mcount fn_is_fake_mcount64
  75. # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
  76. # define mcount_adjust mcount_adjust_64
  77. # define Elf_Addr Elf64_Addr
  78. # define Elf_Ehdr Elf64_Ehdr
  79. # define Elf_Shdr Elf64_Shdr
  80. # define Elf_Rel Elf64_Rel
  81. # define Elf_Rela Elf64_Rela
  82. # define Elf_Sym Elf64_Sym
  83. # define ELF_R_SYM ELF64_R_SYM
  84. # define Elf_r_sym Elf64_r_sym
  85. # define ELF_R_INFO ELF64_R_INFO
  86. # define Elf_r_info Elf64_r_info
  87. # define ELF_ST_BIND ELF64_ST_BIND
  88. # define ELF_ST_TYPE ELF64_ST_TYPE
  89. # define fn_ELF_R_SYM fn_ELF64_R_SYM
  90. # define fn_ELF_R_INFO fn_ELF64_R_INFO
  91. # define uint_t uint64_t
  92. # define _w w8
  93. # define _align 7u
  94. # define _size 8
  95. #else
  96. # define append_func append32
  97. # define sift_rel_mcount sift32_rel_mcount
  98. # define nop_mcount nop_mcount_32
  99. # define find_secsym_ndx find32_secsym_ndx
  100. # define __has_rel_mcount __has32_rel_mcount
  101. # define has_rel_mcount has32_rel_mcount
  102. # define tot_relsize tot32_relsize
  103. # define find_symtab find_symtab32
  104. # define get_shnum get_shnum32
  105. # define set_shnum set_shnum32
  106. # define get_shstrndx get_shstrndx32
  107. # define get_symindex get_symindex32
  108. # define get_sym_str_and_relp get_sym_str_and_relp_32
  109. # define do_func do32
  110. # define get_mcountsym get_mcountsym_32
  111. # define is_fake_mcount is_fake_mcount32
  112. # define fn_is_fake_mcount fn_is_fake_mcount32
  113. # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
  114. # define mcount_adjust mcount_adjust_32
  115. # define Elf_Addr Elf32_Addr
  116. # define Elf_Ehdr Elf32_Ehdr
  117. # define Elf_Shdr Elf32_Shdr
  118. # define Elf_Rel Elf32_Rel
  119. # define Elf_Rela Elf32_Rela
  120. # define Elf_Sym Elf32_Sym
  121. # define ELF_R_SYM ELF32_R_SYM
  122. # define Elf_r_sym Elf32_r_sym
  123. # define ELF_R_INFO ELF32_R_INFO
  124. # define Elf_r_info Elf32_r_info
  125. # define ELF_ST_BIND ELF32_ST_BIND
  126. # define ELF_ST_TYPE ELF32_ST_TYPE
  127. # define fn_ELF_R_SYM fn_ELF32_R_SYM
  128. # define fn_ELF_R_INFO fn_ELF32_R_INFO
  129. # define uint_t uint32_t
  130. # define _w w
  131. # define _align 3u
  132. # define _size 4
  133. #endif
  134. /* Functions and pointers that do_file() may override for specific e_machine. */
  135. static int fn_is_fake_mcount(Elf_Rel const *rp)
  136. {
  137. return 0;
  138. }
  139. static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
  140. static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
  141. {
  142. return ELF_R_SYM(_w(rp->r_info));
  143. }
  144. static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
  145. static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
  146. {
  147. rp->r_info = _w(ELF_R_INFO(sym, type));
  148. }
  149. static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
  150. static int mcount_adjust = 0;
  151. /*
  152. * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
  153. * _mcount symbol is needed for dynamic function tracer, with it, to disable
  154. * tracing(ftrace_make_nop), the instruction in the position is replaced with
  155. * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
  156. * instruction back. So, here, we set the 2nd one as fake and filter it.
  157. *
  158. * c: 3c030000 lui v1,0x0 <--> b label
  159. * c: R_MIPS_HI16 _mcount
  160. * c: R_MIPS_NONE *ABS*
  161. * c: R_MIPS_NONE *ABS*
  162. * 10: 64630000 daddiu v1,v1,0
  163. * 10: R_MIPS_LO16 _mcount
  164. * 10: R_MIPS_NONE *ABS*
  165. * 10: R_MIPS_NONE *ABS*
  166. * 14: 03e0082d move at,ra
  167. * 18: 0060f809 jalr v1
  168. * label:
  169. */
  170. #define MIPS_FAKEMCOUNT_OFFSET 4
  171. static int MIPS_is_fake_mcount(Elf_Rel const *rp)
  172. {
  173. static Elf_Addr old_r_offset = ~(Elf_Addr)0;
  174. Elf_Addr current_r_offset = _w(rp->r_offset);
  175. int is_fake;
  176. is_fake = (old_r_offset != ~(Elf_Addr)0) &&
  177. (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
  178. old_r_offset = current_r_offset;
  179. return is_fake;
  180. }
  181. static unsigned int get_symindex(Elf_Sym const *sym, Elf32_Word const *symtab,
  182. Elf32_Word const *symtab_shndx)
  183. {
  184. unsigned long offset;
  185. unsigned short shndx = w2(sym->st_shndx);
  186. int index;
  187. if (shndx > SHN_UNDEF && shndx < SHN_LORESERVE)
  188. return shndx;
  189. if (shndx == SHN_XINDEX) {
  190. offset = (unsigned long)sym - (unsigned long)symtab;
  191. index = offset / sizeof(*sym);
  192. return w(symtab_shndx[index]);
  193. }
  194. return 0;
  195. }
  196. static unsigned int get_shnum(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
  197. {
  198. if (shdr0 && !ehdr->e_shnum)
  199. return w(shdr0->sh_size);
  200. return w2(ehdr->e_shnum);
  201. }
  202. static void set_shnum(Elf_Ehdr *ehdr, Elf_Shdr *shdr0, unsigned int new_shnum)
  203. {
  204. if (new_shnum >= SHN_LORESERVE) {
  205. ehdr->e_shnum = 0;
  206. shdr0->sh_size = w(new_shnum);
  207. } else
  208. ehdr->e_shnum = w2(new_shnum);
  209. }
  210. static int get_shstrndx(Elf_Ehdr const *ehdr, Elf_Shdr const *shdr0)
  211. {
  212. if (ehdr->e_shstrndx != SHN_XINDEX)
  213. return w2(ehdr->e_shstrndx);
  214. return w(shdr0->sh_link);
  215. }
  216. static void find_symtab(Elf_Ehdr *const ehdr, Elf_Shdr const *shdr0,
  217. unsigned const nhdr, Elf32_Word **symtab,
  218. Elf32_Word **symtab_shndx)
  219. {
  220. Elf_Shdr const *relhdr;
  221. unsigned k;
  222. *symtab = NULL;
  223. *symtab_shndx = NULL;
  224. for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
  225. if (relhdr->sh_type == SHT_SYMTAB)
  226. *symtab = (void *)ehdr + relhdr->sh_offset;
  227. else if (relhdr->sh_type == SHT_SYMTAB_SHNDX)
  228. *symtab_shndx = (void *)ehdr + relhdr->sh_offset;
  229. if (*symtab && *symtab_shndx)
  230. break;
  231. }
  232. }
  233. /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
  234. static int append_func(Elf_Ehdr *const ehdr,
  235. Elf_Shdr *const shstr,
  236. uint_t const *const mloc0,
  237. uint_t const *const mlocp,
  238. Elf_Rel const *const mrel0,
  239. Elf_Rel const *const mrelp,
  240. unsigned int const rel_entsize,
  241. unsigned int const symsec_sh_link)
  242. {
  243. /* Begin constructing output file */
  244. Elf_Shdr mcsec;
  245. char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
  246. ? ".rela__mcount_loc"
  247. : ".rel__mcount_loc";
  248. uint_t const old_shoff = _w(ehdr->e_shoff);
  249. uint_t const old_shstr_sh_size = _w(shstr->sh_size);
  250. uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
  251. Elf_Shdr *const shdr0 = (Elf_Shdr *)(old_shoff + (void *)ehdr);
  252. unsigned int const old_shnum = get_shnum(ehdr, shdr0);
  253. unsigned int const new_shnum = 2 + old_shnum; /* {.rel,}__mcount_loc */
  254. uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
  255. uint_t new_e_shoff;
  256. shstr->sh_size = _w(t);
  257. shstr->sh_offset = _w(sb.st_size);
  258. t += sb.st_size;
  259. t += (_align & -t); /* word-byte align */
  260. new_e_shoff = t;
  261. set_shnum(ehdr, shdr0, new_shnum);
  262. /* body for new shstrtab */
  263. if (ulseek(sb.st_size, SEEK_SET) < 0)
  264. return -1;
  265. if (uwrite(old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size) < 0)
  266. return -1;
  267. if (uwrite(mc_name, 1 + strlen(mc_name)) < 0)
  268. return -1;
  269. /* old(modified) Elf_Shdr table, word-byte aligned */
  270. if (ulseek(t, SEEK_SET) < 0)
  271. return -1;
  272. t += sizeof(Elf_Shdr) * old_shnum;
  273. if (uwrite(old_shoff + (void *)ehdr,
  274. sizeof(Elf_Shdr) * old_shnum) < 0)
  275. return -1;
  276. /* new sections __mcount_loc and .rel__mcount_loc */
  277. t += 2*sizeof(mcsec);
  278. mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
  279. + old_shstr_sh_size);
  280. mcsec.sh_type = w(SHT_PROGBITS);
  281. mcsec.sh_flags = _w(SHF_ALLOC);
  282. mcsec.sh_addr = 0;
  283. mcsec.sh_offset = _w(t);
  284. mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
  285. mcsec.sh_link = 0;
  286. mcsec.sh_info = 0;
  287. mcsec.sh_addralign = _w(_size);
  288. mcsec.sh_entsize = _w(_size);
  289. if (uwrite(&mcsec, sizeof(mcsec)) < 0)
  290. return -1;
  291. mcsec.sh_name = w(old_shstr_sh_size);
  292. mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
  293. ? w(SHT_RELA)
  294. : w(SHT_REL);
  295. mcsec.sh_flags = 0;
  296. mcsec.sh_addr = 0;
  297. mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
  298. mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
  299. mcsec.sh_link = w(symsec_sh_link);
  300. mcsec.sh_info = w(old_shnum);
  301. mcsec.sh_addralign = _w(_size);
  302. mcsec.sh_entsize = _w(rel_entsize);
  303. if (uwrite(&mcsec, sizeof(mcsec)) < 0)
  304. return -1;
  305. if (uwrite(mloc0, (void *)mlocp - (void *)mloc0) < 0)
  306. return -1;
  307. if (uwrite(mrel0, (void *)mrelp - (void *)mrel0) < 0)
  308. return -1;
  309. ehdr->e_shoff = _w(new_e_shoff);
  310. if (ulseek(0, SEEK_SET) < 0)
  311. return -1;
  312. if (uwrite(ehdr, sizeof(*ehdr)) < 0)
  313. return -1;
  314. return 0;
  315. }
  316. static unsigned get_mcountsym(Elf_Sym const *const sym0,
  317. Elf_Rel const *relp,
  318. char const *const str0)
  319. {
  320. unsigned mcountsym = 0;
  321. Elf_Sym const *const symp =
  322. &sym0[Elf_r_sym(relp)];
  323. char const *symname = &str0[w(symp->st_name)];
  324. char const *mcount = gpfx == '_' ? "_mcount" : "mcount";
  325. char const *fentry = "__fentry__";
  326. if (symname[0] == '.')
  327. ++symname; /* ppc64 hack */
  328. if (strcmp(mcount, symname) == 0 ||
  329. (altmcount && strcmp(altmcount, symname) == 0) ||
  330. (strcmp(fentry, symname) == 0))
  331. mcountsym = Elf_r_sym(relp);
  332. return mcountsym;
  333. }
  334. static void get_sym_str_and_relp(Elf_Shdr const *const relhdr,
  335. Elf_Ehdr const *const ehdr,
  336. Elf_Sym const **sym0,
  337. char const **str0,
  338. Elf_Rel const **relp)
  339. {
  340. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  341. + (void *)ehdr);
  342. unsigned const symsec_sh_link = w(relhdr->sh_link);
  343. Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
  344. Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
  345. Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
  346. + (void *)ehdr);
  347. *sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
  348. + (void *)ehdr);
  349. *str0 = (char const *)(_w(strsec->sh_offset)
  350. + (void *)ehdr);
  351. *relp = rel0;
  352. }
  353. /*
  354. * Look at the relocations in order to find the calls to mcount.
  355. * Accumulate the section offsets that are found, and their relocation info,
  356. * onto the end of the existing arrays.
  357. */
  358. static uint_t *sift_rel_mcount(uint_t *mlocp,
  359. unsigned const offbase,
  360. Elf_Rel **const mrelpp,
  361. Elf_Shdr const *const relhdr,
  362. Elf_Ehdr const *const ehdr,
  363. unsigned const recsym,
  364. uint_t const recval,
  365. unsigned const reltype)
  366. {
  367. uint_t *const mloc0 = mlocp;
  368. Elf_Rel *mrelp = *mrelpp;
  369. Elf_Sym const *sym0;
  370. char const *str0;
  371. Elf_Rel const *relp;
  372. unsigned rel_entsize = _w(relhdr->sh_entsize);
  373. unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
  374. unsigned mcountsym = 0;
  375. unsigned t;
  376. get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
  377. for (t = nrel; t; --t) {
  378. if (!mcountsym)
  379. mcountsym = get_mcountsym(sym0, relp, str0);
  380. if (mcountsym && mcountsym == Elf_r_sym(relp) &&
  381. !is_fake_mcount(relp)) {
  382. uint_t const addend =
  383. _w(_w(relp->r_offset) - recval + mcount_adjust);
  384. mrelp->r_offset = _w(offbase
  385. + ((void *)mlocp - (void *)mloc0));
  386. Elf_r_info(mrelp, recsym, reltype);
  387. if (rel_entsize == sizeof(Elf_Rela)) {
  388. ((Elf_Rela *)mrelp)->r_addend = addend;
  389. *mlocp++ = 0;
  390. } else
  391. *mlocp++ = addend;
  392. mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
  393. }
  394. relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
  395. }
  396. *mrelpp = mrelp;
  397. return mlocp;
  398. }
  399. /*
  400. * Read the relocation table again, but this time its called on sections
  401. * that are not going to be traced. The mcount calls here will be converted
  402. * into nops.
  403. */
  404. static int nop_mcount(Elf_Shdr const *const relhdr,
  405. Elf_Ehdr const *const ehdr,
  406. const char *const txtname)
  407. {
  408. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  409. + (void *)ehdr);
  410. Elf_Sym const *sym0;
  411. char const *str0;
  412. Elf_Rel const *relp;
  413. Elf_Shdr const *const shdr = &shdr0[w(relhdr->sh_info)];
  414. unsigned rel_entsize = _w(relhdr->sh_entsize);
  415. unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
  416. unsigned mcountsym = 0;
  417. unsigned t;
  418. int once = 0;
  419. get_sym_str_and_relp(relhdr, ehdr, &sym0, &str0, &relp);
  420. for (t = nrel; t; --t) {
  421. int ret = -1;
  422. if (!mcountsym)
  423. mcountsym = get_mcountsym(sym0, relp, str0);
  424. if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
  425. if (make_nop)
  426. ret = make_nop((void *)ehdr, _w(shdr->sh_offset) + _w(relp->r_offset));
  427. if (warn_on_notrace_sect && !once) {
  428. printf("Section %s has mcount callers being ignored\n",
  429. txtname);
  430. once = 1;
  431. /* just warn? */
  432. if (!make_nop)
  433. return 0;
  434. }
  435. }
  436. /*
  437. * If we successfully removed the mcount, mark the relocation
  438. * as a nop (don't do anything with it).
  439. */
  440. if (!ret) {
  441. Elf_Rel rel;
  442. rel = *(Elf_Rel *)relp;
  443. Elf_r_info(&rel, Elf_r_sym(relp), rel_type_nop);
  444. if (ulseek((void *)relp - (void *)ehdr, SEEK_SET) < 0)
  445. return -1;
  446. if (uwrite(&rel, sizeof(rel)) < 0)
  447. return -1;
  448. }
  449. relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
  450. }
  451. return 0;
  452. }
  453. /*
  454. * Find a symbol in the given section, to be used as the base for relocating
  455. * the table of offsets of calls to mcount. A local or global symbol suffices,
  456. * but avoid a Weak symbol because it may be overridden; the change in value
  457. * would invalidate the relocations of the offsets of the calls to mcount.
  458. * Often the found symbol will be the unnamed local symbol generated by
  459. * GNU 'as' for the start of each section. For example:
  460. * Num: Value Size Type Bind Vis Ndx Name
  461. * 2: 00000000 0 SECTION LOCAL DEFAULT 1
  462. */
  463. static int find_secsym_ndx(unsigned const txtndx,
  464. char const *const txtname,
  465. uint_t *const recvalp,
  466. unsigned int *sym_index,
  467. Elf_Shdr const *const symhdr,
  468. Elf32_Word const *symtab,
  469. Elf32_Word const *symtab_shndx,
  470. Elf_Ehdr const *const ehdr)
  471. {
  472. Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
  473. + (void *)ehdr);
  474. unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
  475. Elf_Sym const *symp;
  476. unsigned t;
  477. for (symp = sym0, t = nsym; t; --t, ++symp) {
  478. unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
  479. if (txtndx == get_symindex(symp, symtab, symtab_shndx)
  480. /* avoid STB_WEAK */
  481. && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
  482. /* function symbols on ARM have quirks, avoid them */
  483. if (w2(ehdr->e_machine) == EM_ARM
  484. && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
  485. continue;
  486. *recvalp = _w(symp->st_value);
  487. *sym_index = symp - sym0;
  488. return 0;
  489. }
  490. }
  491. fprintf(stderr, "Cannot find symbol for section %u: %s.\n",
  492. txtndx, txtname);
  493. return -1;
  494. }
  495. /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
  496. static char const * __has_rel_mcount(Elf_Shdr const *const relhdr, /* reltype */
  497. Elf_Shdr const *const shdr0,
  498. char const *const shstrtab,
  499. char const *const fname)
  500. {
  501. /* .sh_info depends on .sh_type == SHT_REL[,A] */
  502. Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
  503. char const *const txtname = &shstrtab[w(txthdr->sh_name)];
  504. if (strcmp("__mcount_loc", txtname) == 0) {
  505. fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
  506. fname);
  507. return already_has_rel_mcount;
  508. }
  509. if (w(txthdr->sh_type) != SHT_PROGBITS ||
  510. !(_w(txthdr->sh_flags) & SHF_EXECINSTR))
  511. return NULL;
  512. return txtname;
  513. }
  514. static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
  515. Elf_Shdr const *const shdr0,
  516. char const *const shstrtab,
  517. char const *const fname)
  518. {
  519. if (w(relhdr->sh_type) != SHT_REL && w(relhdr->sh_type) != SHT_RELA)
  520. return NULL;
  521. return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
  522. }
  523. static unsigned tot_relsize(Elf_Shdr const *const shdr0,
  524. unsigned nhdr,
  525. const char *const shstrtab,
  526. const char *const fname)
  527. {
  528. unsigned totrelsz = 0;
  529. Elf_Shdr const *shdrp = shdr0;
  530. char const *txtname;
  531. for (; nhdr; --nhdr, ++shdrp) {
  532. txtname = has_rel_mcount(shdrp, shdr0, shstrtab, fname);
  533. if (txtname == already_has_rel_mcount) {
  534. totrelsz = 0;
  535. break;
  536. }
  537. if (txtname && is_mcounted_section_name(txtname))
  538. totrelsz += _w(shdrp->sh_size);
  539. }
  540. return totrelsz;
  541. }
  542. /* Overall supervision for Elf32 ET_REL file. */
  543. static int do_func(Elf_Ehdr *const ehdr, char const *const fname,
  544. unsigned const reltype)
  545. {
  546. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  547. + (void *)ehdr);
  548. unsigned const nhdr = get_shnum(ehdr, shdr0);
  549. Elf_Shdr *const shstr = &shdr0[get_shstrndx(ehdr, shdr0)];
  550. char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
  551. + (void *)ehdr);
  552. Elf_Shdr const *relhdr;
  553. unsigned k;
  554. Elf32_Word *symtab;
  555. Elf32_Word *symtab_shndx;
  556. /* Upper bound on space: assume all relevant relocs are for mcount. */
  557. unsigned totrelsz;
  558. Elf_Rel * mrel0;
  559. Elf_Rel * mrelp;
  560. uint_t * mloc0;
  561. uint_t * mlocp;
  562. unsigned rel_entsize = 0;
  563. unsigned symsec_sh_link = 0;
  564. int result = 0;
  565. totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
  566. if (totrelsz == 0)
  567. return 0;
  568. mrel0 = umalloc(totrelsz);
  569. mrelp = mrel0;
  570. if (!mrel0)
  571. return -1;
  572. /* 2*sizeof(address) <= sizeof(Elf_Rel) */
  573. mloc0 = umalloc(totrelsz>>1);
  574. mlocp = mloc0;
  575. if (!mloc0) {
  576. free(mrel0);
  577. return -1;
  578. }
  579. find_symtab(ehdr, shdr0, nhdr, &symtab, &symtab_shndx);
  580. for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
  581. char const *const txtname = has_rel_mcount(relhdr, shdr0,
  582. shstrtab, fname);
  583. if (txtname == already_has_rel_mcount) {
  584. result = 0;
  585. file_updated = 0;
  586. goto out; /* Nothing to be done; don't append! */
  587. }
  588. if (txtname && is_mcounted_section_name(txtname)) {
  589. unsigned int recsym;
  590. uint_t recval = 0;
  591. symsec_sh_link = w(relhdr->sh_link);
  592. result = find_secsym_ndx(w(relhdr->sh_info), txtname,
  593. &recval, &recsym,
  594. &shdr0[symsec_sh_link],
  595. symtab, symtab_shndx,
  596. ehdr);
  597. if (result)
  598. goto out;
  599. rel_entsize = _w(relhdr->sh_entsize);
  600. mlocp = sift_rel_mcount(mlocp,
  601. (void *)mlocp - (void *)mloc0, &mrelp,
  602. relhdr, ehdr, recsym, recval, reltype);
  603. } else if (txtname && (warn_on_notrace_sect || make_nop)) {
  604. /*
  605. * This section is ignored by ftrace, but still
  606. * has mcount calls. Convert them to nops now.
  607. */
  608. if (nop_mcount(relhdr, ehdr, txtname) < 0) {
  609. result = -1;
  610. goto out;
  611. }
  612. }
  613. }
  614. if (!result && mloc0 != mlocp)
  615. result = append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
  616. rel_entsize, symsec_sh_link);
  617. out:
  618. free(mrel0);
  619. free(mloc0);
  620. return result;
  621. }