recordmcount.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 sift_rel_mcount
  23. #undef find_secsym_ndx
  24. #undef __has_rel_mcount
  25. #undef has_rel_mcount
  26. #undef tot_relsize
  27. #undef do_func
  28. #undef Elf_Ehdr
  29. #undef Elf_Shdr
  30. #undef Elf_Rel
  31. #undef Elf_Rela
  32. #undef Elf_Sym
  33. #undef ELF_R_SYM
  34. #undef ELF_R_INFO
  35. #undef ELF_ST_BIND
  36. #undef uint_t
  37. #undef _w
  38. #undef _align
  39. #undef _size
  40. #ifdef RECORD_MCOUNT_64
  41. # define append_func append64
  42. # define sift_rel_mcount sift64_rel_mcount
  43. # define find_secsym_ndx find64_secsym_ndx
  44. # define __has_rel_mcount __has64_rel_mcount
  45. # define has_rel_mcount has64_rel_mcount
  46. # define tot_relsize tot64_relsize
  47. # define do_func do64
  48. # define Elf_Ehdr Elf64_Ehdr
  49. # define Elf_Shdr Elf64_Shdr
  50. # define Elf_Rel Elf64_Rel
  51. # define Elf_Rela Elf64_Rela
  52. # define Elf_Sym Elf64_Sym
  53. # define ELF_R_SYM ELF64_R_SYM
  54. # define ELF_R_INFO ELF64_R_INFO
  55. # define ELF_ST_BIND ELF64_ST_BIND
  56. # define uint_t uint64_t
  57. # define _w w8
  58. # define _align 7u
  59. # define _size 8
  60. #else
  61. # define append_func append32
  62. # define sift_rel_mcount sift32_rel_mcount
  63. # define find_secsym_ndx find32_secsym_ndx
  64. # define __has_rel_mcount __has32_rel_mcount
  65. # define has_rel_mcount has32_rel_mcount
  66. # define tot_relsize tot32_relsize
  67. # define do_func do32
  68. # define Elf_Ehdr Elf32_Ehdr
  69. # define Elf_Shdr Elf32_Shdr
  70. # define Elf_Rel Elf32_Rel
  71. # define Elf_Rela Elf32_Rela
  72. # define Elf_Sym Elf32_Sym
  73. # define ELF_R_SYM ELF32_R_SYM
  74. # define ELF_R_INFO ELF32_R_INFO
  75. # define ELF_ST_BIND ELF32_ST_BIND
  76. # define uint_t uint32_t
  77. # define _w w
  78. # define _align 3u
  79. # define _size 4
  80. #endif
  81. /* Append the new shstrtab, Elf_Shdr[], __mcount_loc and its relocations. */
  82. static void append_func(Elf_Ehdr *const ehdr,
  83. Elf_Shdr *const shstr,
  84. uint_t const *const mloc0,
  85. uint_t const *const mlocp,
  86. Elf_Rel const *const mrel0,
  87. Elf_Rel const *const mrelp,
  88. unsigned int const rel_entsize,
  89. unsigned int const symsec_sh_link)
  90. {
  91. /* Begin constructing output file */
  92. Elf_Shdr mcsec;
  93. char const *mc_name = (sizeof(Elf_Rela) == rel_entsize)
  94. ? ".rela__mcount_loc"
  95. : ".rel__mcount_loc";
  96. unsigned const old_shnum = w2(ehdr->e_shnum);
  97. uint_t const old_shoff = _w(ehdr->e_shoff);
  98. uint_t const old_shstr_sh_size = _w(shstr->sh_size);
  99. uint_t const old_shstr_sh_offset = _w(shstr->sh_offset);
  100. uint_t t = 1 + strlen(mc_name) + _w(shstr->sh_size);
  101. uint_t new_e_shoff;
  102. shstr->sh_size = _w(t);
  103. shstr->sh_offset = _w(sb.st_size);
  104. t += sb.st_size;
  105. t += (_align & -t); /* word-byte align */
  106. new_e_shoff = t;
  107. /* body for new shstrtab */
  108. ulseek(fd_map, sb.st_size, SEEK_SET);
  109. uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
  110. uwrite(fd_map, mc_name, 1 + strlen(mc_name));
  111. /* old(modified) Elf_Shdr table, word-byte aligned */
  112. ulseek(fd_map, t, SEEK_SET);
  113. t += sizeof(Elf_Shdr) * old_shnum;
  114. uwrite(fd_map, old_shoff + (void *)ehdr,
  115. sizeof(Elf_Shdr) * old_shnum);
  116. /* new sections __mcount_loc and .rel__mcount_loc */
  117. t += 2*sizeof(mcsec);
  118. mcsec.sh_name = w((sizeof(Elf_Rela) == rel_entsize) + strlen(".rel")
  119. + old_shstr_sh_size);
  120. mcsec.sh_type = w(SHT_PROGBITS);
  121. mcsec.sh_flags = _w(SHF_ALLOC);
  122. mcsec.sh_addr = 0;
  123. mcsec.sh_offset = _w(t);
  124. mcsec.sh_size = _w((void *)mlocp - (void *)mloc0);
  125. mcsec.sh_link = 0;
  126. mcsec.sh_info = 0;
  127. mcsec.sh_addralign = _w(_size);
  128. mcsec.sh_entsize = _w(_size);
  129. uwrite(fd_map, &mcsec, sizeof(mcsec));
  130. mcsec.sh_name = w(old_shstr_sh_size);
  131. mcsec.sh_type = (sizeof(Elf_Rela) == rel_entsize)
  132. ? w(SHT_RELA)
  133. : w(SHT_REL);
  134. mcsec.sh_flags = 0;
  135. mcsec.sh_addr = 0;
  136. mcsec.sh_offset = _w((void *)mlocp - (void *)mloc0 + t);
  137. mcsec.sh_size = _w((void *)mrelp - (void *)mrel0);
  138. mcsec.sh_link = w(symsec_sh_link);
  139. mcsec.sh_info = w(old_shnum);
  140. mcsec.sh_addralign = _w(_size);
  141. mcsec.sh_entsize = _w(rel_entsize);
  142. uwrite(fd_map, &mcsec, sizeof(mcsec));
  143. uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
  144. uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
  145. ehdr->e_shoff = _w(new_e_shoff);
  146. ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
  147. ulseek(fd_map, 0, SEEK_SET);
  148. uwrite(fd_map, ehdr, sizeof(*ehdr));
  149. }
  150. /*
  151. * Look at the relocations in order to find the calls to mcount.
  152. * Accumulate the section offsets that are found, and their relocation info,
  153. * onto the end of the existing arrays.
  154. */
  155. static uint_t *sift_rel_mcount(uint_t *mlocp,
  156. unsigned const offbase,
  157. Elf_Rel **const mrelpp,
  158. Elf_Shdr const *const relhdr,
  159. Elf_Ehdr const *const ehdr,
  160. unsigned const recsym,
  161. uint_t const recval,
  162. unsigned const reltype)
  163. {
  164. uint_t *const mloc0 = mlocp;
  165. Elf_Rel *mrelp = *mrelpp;
  166. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  167. + (void *)ehdr);
  168. unsigned const symsec_sh_link = w(relhdr->sh_link);
  169. Elf_Shdr const *const symsec = &shdr0[symsec_sh_link];
  170. Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symsec->sh_offset)
  171. + (void *)ehdr);
  172. Elf_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
  173. char const *const str0 = (char const *)(_w(strsec->sh_offset)
  174. + (void *)ehdr);
  175. Elf_Rel const *const rel0 = (Elf_Rel const *)(_w(relhdr->sh_offset)
  176. + (void *)ehdr);
  177. unsigned rel_entsize = _w(relhdr->sh_entsize);
  178. unsigned const nrel = _w(relhdr->sh_size) / rel_entsize;
  179. Elf_Rel const *relp = rel0;
  180. unsigned mcountsym = 0;
  181. unsigned t;
  182. for (t = nrel; t; --t) {
  183. if (!mcountsym) {
  184. Elf_Sym const *const symp =
  185. &sym0[ELF_R_SYM(_w(relp->r_info))];
  186. char const *symname = &str0[w(symp->st_name)];
  187. if ('.' == symname[0])
  188. ++symname; /* ppc64 hack */
  189. if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
  190. symname))
  191. mcountsym = ELF_R_SYM(_w(relp->r_info));
  192. }
  193. if (mcountsym == ELF_R_SYM(_w(relp->r_info))) {
  194. uint_t const addend = _w(_w(relp->r_offset) - recval);
  195. mrelp->r_offset = _w(offbase
  196. + ((void *)mlocp - (void *)mloc0));
  197. mrelp->r_info = _w(ELF_R_INFO(recsym, reltype));
  198. if (sizeof(Elf_Rela) == rel_entsize) {
  199. ((Elf_Rela *)mrelp)->r_addend = addend;
  200. *mlocp++ = 0;
  201. } else
  202. *mlocp++ = addend;
  203. mrelp = (Elf_Rel *)(rel_entsize + (void *)mrelp);
  204. }
  205. relp = (Elf_Rel const *)(rel_entsize + (void *)relp);
  206. }
  207. *mrelpp = mrelp;
  208. return mlocp;
  209. }
  210. /*
  211. * Find a symbol in the given section, to be used as the base for relocating
  212. * the table of offsets of calls to mcount. A local or global symbol suffices,
  213. * but avoid a Weak symbol because it may be overridden; the change in value
  214. * would invalidate the relocations of the offsets of the calls to mcount.
  215. * Often the found symbol will be the unnamed local symbol generated by
  216. * GNU 'as' for the start of each section. For example:
  217. * Num: Value Size Type Bind Vis Ndx Name
  218. * 2: 00000000 0 SECTION LOCAL DEFAULT 1
  219. */
  220. static unsigned find_secsym_ndx(unsigned const txtndx,
  221. char const *const txtname,
  222. uint_t *const recvalp,
  223. Elf_Shdr const *const symhdr,
  224. Elf_Ehdr const *const ehdr)
  225. {
  226. Elf_Sym const *const sym0 = (Elf_Sym const *)(_w(symhdr->sh_offset)
  227. + (void *)ehdr);
  228. unsigned const nsym = _w(symhdr->sh_size) / _w(symhdr->sh_entsize);
  229. Elf_Sym const *symp;
  230. unsigned t;
  231. for (symp = sym0, t = nsym; t; --t, ++symp) {
  232. unsigned int const st_bind = ELF_ST_BIND(symp->st_info);
  233. if (txtndx == w2(symp->st_shndx)
  234. /* avoid STB_WEAK */
  235. && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
  236. *recvalp = _w(symp->st_value);
  237. return symp - sym0;
  238. }
  239. }
  240. fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
  241. txtndx, txtname);
  242. fail_file();
  243. }
  244. /* Evade ISO C restriction: no declaration after statement in has_rel_mcount. */
  245. static char const *
  246. __has_rel_mcount(Elf_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
  247. Elf_Shdr const *const shdr0,
  248. char const *const shstrtab,
  249. char const *const fname)
  250. {
  251. /* .sh_info depends on .sh_type == SHT_REL[,A] */
  252. Elf_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
  253. char const *const txtname = &shstrtab[w(txthdr->sh_name)];
  254. if (0 == strcmp("__mcount_loc", txtname)) {
  255. fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
  256. fname);
  257. succeed_file();
  258. }
  259. if (SHT_PROGBITS != w(txthdr->sh_type) ||
  260. !is_mcounted_section_name(txtname))
  261. return NULL;
  262. return txtname;
  263. }
  264. static char const *has_rel_mcount(Elf_Shdr const *const relhdr,
  265. Elf_Shdr const *const shdr0,
  266. char const *const shstrtab,
  267. char const *const fname)
  268. {
  269. if (SHT_REL != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
  270. return NULL;
  271. return __has_rel_mcount(relhdr, shdr0, shstrtab, fname);
  272. }
  273. static unsigned tot_relsize(Elf_Shdr const *const shdr0,
  274. unsigned nhdr,
  275. const char *const shstrtab,
  276. const char *const fname)
  277. {
  278. unsigned totrelsz = 0;
  279. Elf_Shdr const *shdrp = shdr0;
  280. for (; nhdr; --nhdr, ++shdrp) {
  281. if (has_rel_mcount(shdrp, shdr0, shstrtab, fname))
  282. totrelsz += _w(shdrp->sh_size);
  283. }
  284. return totrelsz;
  285. }
  286. /* Overall supervision for Elf32 ET_REL file. */
  287. static void
  288. do_func(Elf_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
  289. {
  290. Elf_Shdr *const shdr0 = (Elf_Shdr *)(_w(ehdr->e_shoff)
  291. + (void *)ehdr);
  292. unsigned const nhdr = w2(ehdr->e_shnum);
  293. Elf_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
  294. char const *const shstrtab = (char const *)(_w(shstr->sh_offset)
  295. + (void *)ehdr);
  296. Elf_Shdr const *relhdr;
  297. unsigned k;
  298. /* Upper bound on space: assume all relevant relocs are for mcount. */
  299. unsigned const totrelsz = tot_relsize(shdr0, nhdr, shstrtab, fname);
  300. Elf_Rel *const mrel0 = umalloc(totrelsz);
  301. Elf_Rel * mrelp = mrel0;
  302. /* 2*sizeof(address) <= sizeof(Elf_Rel) */
  303. uint_t *const mloc0 = umalloc(totrelsz>>1);
  304. uint_t * mlocp = mloc0;
  305. unsigned rel_entsize = 0;
  306. unsigned symsec_sh_link = 0;
  307. for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
  308. char const *const txtname = has_rel_mcount(relhdr, shdr0,
  309. shstrtab, fname);
  310. if (txtname) {
  311. uint_t recval = 0;
  312. unsigned const recsym = find_secsym_ndx(
  313. w(relhdr->sh_info), txtname, &recval,
  314. &shdr0[symsec_sh_link = w(relhdr->sh_link)],
  315. ehdr);
  316. rel_entsize = _w(relhdr->sh_entsize);
  317. mlocp = sift_rel_mcount(mlocp,
  318. (void *)mlocp - (void *)mloc0, &mrelp,
  319. relhdr, ehdr, recsym, recval, reltype);
  320. }
  321. }
  322. if (mloc0 != mlocp) {
  323. append_func(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
  324. rel_entsize, symsec_sh_link);
  325. }
  326. free(mrel0);
  327. free(mloc0);
  328. }