recordmcount.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * recordmcount.h
  3. *
  4. * This code was taken out of recordmcount.c written by
  5. * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
  6. *
  7. * The original code had the same algorithms for both 32bit
  8. * and 64bit ELF files, but the code was duplicated to support
  9. * the difference in structures that were used. This
  10. * file creates a macro of everything that is different between
  11. * the 64 and 32 bit code, such that by including this header
  12. * twice we can create both sets of functions by including this
  13. * header once with RECORD_MCOUNT_64 undefined, and again with
  14. * it defined.
  15. *
  16. * This conversion to macros was done by:
  17. * Copyright 2010 Steven Rostedt <srostedt@redhat.com>, Red Hat Inc.
  18. *
  19. * Licensed under the GNU General Public License, version 2 (GPLv2).
  20. */
  21. #undef append_func
  22. #undef is_fake_mcount
  23. #undef fn_is_fake_mcount
  24. #undef MIPS_is_fake_mcount
  25. #undef sift_rel_mcount
  26. #undef find_secsym_ndx
  27. #undef __has_rel_mcount
  28. #undef has_rel_mcount
  29. #undef tot_relsize
  30. #undef do_func
  31. #undef Elf_Addr
  32. #undef Elf_Ehdr
  33. #undef Elf_Shdr
  34. #undef Elf_Rel
  35. #undef Elf_Rela
  36. #undef Elf_Sym
  37. #undef ELF_R_SYM
  38. #undef Elf_r_sym
  39. #undef ELF_R_INFO
  40. #undef Elf_r_info
  41. #undef ELF_ST_BIND
  42. #undef fn_ELF_R_SYM
  43. #undef fn_ELF_R_INFO
  44. #undef uint_t
  45. #undef _w
  46. #undef _align
  47. #undef _size
  48. #ifdef RECORD_MCOUNT_64
  49. # define append_func append64
  50. # define sift_rel_mcount sift64_rel_mcount
  51. # define find_secsym_ndx find64_secsym_ndx
  52. # define __has_rel_mcount __has64_rel_mcount
  53. # define has_rel_mcount has64_rel_mcount
  54. # define tot_relsize tot64_relsize
  55. # define do_func do64
  56. # define is_fake_mcount is_fake_mcount64
  57. # define fn_is_fake_mcount fn_is_fake_mcount64
  58. # define MIPS_is_fake_mcount MIPS64_is_fake_mcount
  59. # define Elf_Addr Elf64_Addr
  60. # define Elf_Ehdr Elf64_Ehdr
  61. # define Elf_Shdr Elf64_Shdr
  62. # define Elf_Rel Elf64_Rel
  63. # define Elf_Rela Elf64_Rela
  64. # define Elf_Sym Elf64_Sym
  65. # define ELF_R_SYM ELF64_R_SYM
  66. # define Elf_r_sym Elf64_r_sym
  67. # define ELF_R_INFO ELF64_R_INFO
  68. # define Elf_r_info Elf64_r_info
  69. # define ELF_ST_BIND ELF64_ST_BIND
  70. # define fn_ELF_R_SYM fn_ELF64_R_SYM
  71. # define fn_ELF_R_INFO fn_ELF64_R_INFO
  72. # define uint_t uint64_t
  73. # define _w w8
  74. # define _align 7u
  75. # define _size 8
  76. #else
  77. # define append_func append32
  78. # define sift_rel_mcount sift32_rel_mcount
  79. # define find_secsym_ndx find32_secsym_ndx
  80. # define __has_rel_mcount __has32_rel_mcount
  81. # define has_rel_mcount has32_rel_mcount
  82. # define tot_relsize tot32_relsize
  83. # define do_func do32
  84. # define is_fake_mcount is_fake_mcount32
  85. # define fn_is_fake_mcount fn_is_fake_mcount32
  86. # define MIPS_is_fake_mcount MIPS32_is_fake_mcount
  87. # define Elf_Addr Elf32_Addr
  88. # define Elf_Ehdr Elf32_Ehdr
  89. # define Elf_Shdr Elf32_Shdr
  90. # define Elf_Rel Elf32_Rel
  91. # define Elf_Rela Elf32_Rela
  92. # define Elf_Sym Elf32_Sym
  93. # define ELF_R_SYM ELF32_R_SYM
  94. # define Elf_r_sym Elf32_r_sym
  95. # define ELF_R_INFO ELF32_R_INFO
  96. # define Elf_r_info Elf32_r_info
  97. # define ELF_ST_BIND ELF32_ST_BIND
  98. # define fn_ELF_R_SYM fn_ELF32_R_SYM
  99. # define fn_ELF_R_INFO fn_ELF32_R_INFO
  100. # define uint_t uint32_t
  101. # define _w w
  102. # define _align 3u
  103. # define _size 4
  104. #endif
  105. /* Functions and pointers that do_file() may override for specific e_machine. */
  106. static int fn_is_fake_mcount(Elf_Rel const *rp)
  107. {
  108. return 0;
  109. }
  110. static int (*is_fake_mcount)(Elf_Rel const *rp) = fn_is_fake_mcount;
  111. static uint_t fn_ELF_R_SYM(Elf_Rel const *rp)
  112. {
  113. return ELF_R_SYM(_w(rp->r_info));
  114. }
  115. static uint_t (*Elf_r_sym)(Elf_Rel const *rp) = fn_ELF_R_SYM;
  116. static void fn_ELF_R_INFO(Elf_Rel *const rp, unsigned sym, unsigned type)
  117. {
  118. rp->r_info = ELF_R_INFO(sym, type);
  119. }
  120. static void (*Elf_r_info)(Elf_Rel *const rp, unsigned sym, unsigned type) = fn_ELF_R_INFO;
  121. /*
  122. * MIPS mcount long call has 2 _mcount symbols, only the position of the 1st
  123. * _mcount symbol is needed for dynamic function tracer, with it, to disable
  124. * tracing(ftrace_make_nop), the instruction in the position is replaced with
  125. * the "b label" instruction, to enable tracing(ftrace_make_call), replace the
  126. * instruction back. So, here, we set the 2nd one as fake and filter it.
  127. *
  128. * c: 3c030000 lui v1,0x0 <--> b label
  129. * c: R_MIPS_HI16 _mcount
  130. * c: R_MIPS_NONE *ABS*
  131. * c: R_MIPS_NONE *ABS*
  132. * 10: 64630000 daddiu v1,v1,0
  133. * 10: R_MIPS_LO16 _mcount
  134. * 10: R_MIPS_NONE *ABS*
  135. * 10: R_MIPS_NONE *ABS*
  136. * 14: 03e0082d move at,ra
  137. * 18: 0060f809 jalr v1
  138. * label:
  139. */
  140. #define MIPS_FAKEMCOUNT_OFFSET 4
  141. static int MIPS_is_fake_mcount(Elf_Rel const *rp)
  142. {
  143. static Elf_Addr old_r_offset;
  144. Elf_Addr current_r_offset = _w(rp->r_offset);
  145. int is_fake;
  146. is_fake = old_r_offset &&
  147. (current_r_offset - old_r_offset == MIPS_FAKEMCOUNT_OFFSET);
  148. old_r_offset = current_r_offset;
  149. return is_fake;
  150. }
  151. /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
  152. static void append_func(Elf_Ehdr *const ehdr,
  153. Elf_Shdr *const shstr,
  154. uint_t const *const mloc0,
  155. uint_t const *const mlocp,
  156. Elf_Rel const *const mrel0,
  157. Elf_Rel const *const mrelp,
  158. unsigned int const rel_entsize,
  159. unsigned int const symsec_sh_link)
  160. {
  161. /* Begin constructing output file */
  162. Elf_Shdr mcsec;
  163. char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
  164. ? ".rela__mcount_loc"
  165. : ".rel__mcount_loc";
  166. unsigned const old_shnum = w2(ehdr->e_shnum);
  167. uint_t const old_shoff = _w(ehdr->e_shoff);
  168. uint_t const old_shstr_sh_size = _w(shstr->sh_size);
  169. uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
  170. uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
  171. uint_t new_e_shoff;
  172. shstr->sh_size = _w(t);
  173. shstr->sh_offset = _w(sb.st_size);
  174. t += sb.st_size;
  175. t += (_align & -t); /* word-byte align */
  176. new_e_shoff = t;
  177. /* body for new shstrtab */
  178. ulseek(fd_map, sb.st_size, SEEK_SET);
  179. uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
  180. uwrite(fd_map, mc_name, 1 + strlen(mc_name));
  181. /* old(modified) Elf_Shdr table, word-byte aligned */
  182. ulseek(fd_map, t, SEEK_SET);
  183. t += sizeof(Elf_Shdr) * old_shnum;
  184. uwrite(fd_map, old_shoff + (void *)ehdr,
  185. sizeof(Elf_Shdr) * old_shnum);
  186. /* new sections __mcount_loc and .rel__mcount_loc */
  187. t += 2*sizeof(mcsec);
  188. mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
  189. + old_shstr_sh_size);
  190. mcsec.sh_type = w(SHT_PROGBITS);
  191. mcsec.sh_flags = _w(SHF_ALLOC);
  192. mcsec.sh_addr = 0;
  193. mcsec.sh_offset = _w(t);
  194. mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
  195. mcsec.sh_link = 0;
  196. mcsec.sh_info = 0;
  197. mcsec.sh_addralign = _w(_size);
  198. mcsec.sh_entsize = _w(_size);
  199. uwrite(fd_map, &mcsec, sizeof(mcsec));
  200. mcsec.sh_name = w(old_shstr_sh_size);
  201. mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
  202. ? w(SHT_RELA)
  203. : w(SHT_REL);
  204. mcsec.sh_flags = 0;
  205. mcsec.sh_addr = 0;
  206. mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
  207. mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
  208. mcsec.sh_link = w(symsec_sh_link);
  209. mcsec.sh_info = w(old_shnum);
  210. mcsec.sh_addralign = _w(_size);
  211. mcsec.sh_entsize = _w(rel_entsize);
  212. uwrite(fd_map, &mcsec, sizeof(mcsec));
  213. uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
  214. uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
  215. ehdr->e_shoff = _w(new_e_shoff);
  216. ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
  217. ulseek(fd_map, 0, SEEK_SET);
  218. uwrite(fd_map, ehdr, sizeof(*ehdr));
  219. }
  220. /*
  221. * Look at the relocations in order to find the calls to mcount.
  222. * Accumulate the section offsets that are found, and their relocation info,
  223. * onto the end of the existing arrays.
  224. */
  225. static uint_t *sift_rel_mcount(uint_t *mlocp,
  226. unsigned const offbase,
  227. Elf_Rel **const mrelpp,
  228. Elf_Shdr const *const relhdr,
  229. Elf_Ehdr const *const ehdr,
  230. unsigned const recsym,
  231. uint_t const recval,
  232. unsigned const reltype)
  233. {
  234. uint_t *const mloc0 = mlocp;
  235. Elf_Rel *mrelp = *mrelpp;
  236. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  237. + (void *)ehdr);
  238. unsigned const symsec_sh_link = w(relhdr->sh_link);
  239. Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
  240. Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
  241. + (void *)ehdr);
  242. Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
  243. char const *const str0 = (char const *)(_w(strsec->sh_offset)
  244. + (void *)ehdr);
  245. Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
  246. + (void *)ehdr);
  247. unsigned rel_entsize = _w(relhdr->sh_entsize);
  248. unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
  249. Elf_Rel const *relp = rel0;
  250. unsigned mcountsym = 0;
  251. unsigned t;
  252. for (t = nrel; t; --t) {
  253. if (!mcountsym) {
  254. Elf_Sym const *const symp =
  255. &sym0[Elf_r_sym(relp)];
  256. char const *symname = &str0[w(symp->st_name)];
  257. if ('.' == symname[0])
  258. ++symname; /* ppc64 hack */
  259. if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
  260. symname))
  261. mcountsym = Elf_r_sym(relp);
  262. }
  263. if (mcountsym == Elf_r_sym(relp) && !is_fake_mcount(relp)) {
  264. uint_t const addend = _w(_w(relp->r_offset) - recval);
  265. mrelp->r_offset = _w(offbase
  266. + ((void *)mlocp - (void *)mloc0));
  267. Elf_r_info(mrelp, recsym, reltype);
  268. if (sizeof(Elf_Rela) == rel_entsize) {
  269. ((Elf_Rela *)mrelp)->r_addend = addend;
  270. *mlocp++ = 0;
  271. } else
  272. *mlocp++ = addend;
  273. mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
  274. }
  275. relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
  276. }
  277. *mrelpp = mrelp;
  278. return mlocp;
  279. }
  280. /*
  281. * Find a symbol in the given section, to be used as the base for relocating
  282. * the table of offsets of calls to mcount. A local or global symbol suffices,
  283. * but avoid a Weak symbol because it may be overridden; the change in value
  284. * would invalidate the relocations of the offsets of the calls to mcount.
  285. * Often the found symbol will be the unnamed local symbol generated by
  286. * GNU 'as' for the start of each section. For example:
  287. * Num: Value Size Type Bind Vis Ndx Name
  288. * 2: 00000000 0 SECTION LOCAL DEFAULT 1
  289. */
  290. static unsigned find_secsym_ndx(unsigned const txtndx,
  291. char const *const txtname,
  292. uint_t *const recvalp,
  293. Elf_Shdr const *const symhdr,
  294. Elf_Ehdr const *const ehdr)
  295. {
  296. Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
  297. + (void *)ehdr);
  298. unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
  299. Elf_Sym const *symp;
  300. unsigned t;
  301. for (symp = sym0, t = nsym; t; --t, ++symp) {
  302. unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
  303. if (txtndx == w2(symp->st_shndx)
  304. /* avoid STB_WEAK */
  305. && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
  306. *recvalp = _w(symp->st_value);
  307. return symp - sym0;
  308. }
  309. }
  310. fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
  311. txtndx, txtname);
  312. fail_file();
  313. }
  314. /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
  315. static char const *
  316. __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
  317. Elf_Shdr const *const shdr0,
  318. char const *const shstrtab,
  319. char const *const fname)
  320. {
  321. /* .sh_info depends on .sh_type == SHT_REL[,A] */
  322. Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
  323. char const *const txtname = &shstrtab[w(txthdr->sh_name)];
  324. if (0 == strcmp("__mcount_loc", txtname)) {
  325. fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
  326. fname);
  327. succeed_file();
  328. }
  329. if (SHT_PROGBITS != w(txthdr->sh_type) ||
  330. !is_mcounted_section_name(txtname))
  331. return NULL;
  332. return txtname;
  333. }
  334. static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
  335. Elf_Shdr const *const shdr0,
  336. char const *const shstrtab,
  337. char const *const fname)
  338. {
  339. if (SHT_REL != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
  340. return NULL;
  341. return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
  342. }
  343. static unsigned tot_relsize(Elf_Shdr const *const shdr0,
  344. unsigned nhdr,
  345. const char *const shstrtab,
  346. const char *const fname)
  347. {
  348. unsigned totrelsz = 0;
  349. Elf_Shdr const *shdrp = shdr0;
  350. for (; nhdr; --nhdr, ++shdrp) {
  351. if (has_rel_mcount(shdrp, shdr0, shstrtab, fname))
  352. totrelsz += _w(shdrp->sh_size);
  353. }
  354. return totrelsz;
  355. }
  356. /* Overall supervision for Elf32 ET_REL file. */
  357. static void
  358. do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
  359. {
  360. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  361. + (void *)ehdr);
  362. unsigned const nhdr = w2(ehdr->e_shnum);
  363. Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
  364. char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
  365. + (void *)ehdr);
  366. Elf_Shdr const *relhdr;
  367. unsigned k;
  368. /* Upper bound on space: assume all relevant relocs are for mcount. */
  369. unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
  370. Elf_Rel *const mrel0 = umalloc(totrelsz);
  371. Elf_Rel * mrelp = mrel0;
  372. /* 2*sizeof(address) <= sizeof(Elf_Rel) */
  373. uint_t *const mloc0 = umalloc(totrelsz>>1);
  374. uint_t * mlocp = mloc0;
  375. unsigned rel_entsize = 0;
  376. unsigned symsec_sh_link = 0;
  377. for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
  378. char const *const txtname = has_rel_mcount(relhdr, shdr0,
  379. shstrtab, fname);
  380. if (txtname) {
  381. uint_t recval = 0;
  382. unsigned const recsym = find_secsym_ndx(
  383. w(relhdr->sh_info), txtname, &recval,
  384. &shdr0[symsec_sh_link = w(relhdr->sh_link)],
  385. ehdr);
  386. rel_entsize = _w(relhdr->sh_entsize);
  387. mlocp = sift_rel_mcount(mlocp,
  388. (void *)mlocp - (void *)mloc0, &mrelp,
  389. relhdr, ehdr, recsym, recval, reltype);
  390. }
  391. }
  392. if (mloc0 != mlocp) {
  393. append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
  394. rel_entsize, symsec_sh_link);
  395. }
  396. free(mrel0);
  397. free(mloc0);
  398. }