recordmcount.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /*
  2. * recordmcount.c: construct a table of the locations of calls to 'mcount'
  3. * so that ftrace can find them quickly.
  4. * Copyright 2009 John F. Reiser <jreiser@BitWagon.com>. All rights reserved.
  5. * Licensed under the GNU General Public License, version 2 (GPLv2).
  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 <elf.h>
  26. #include <fcntl.h>
  27. #include <setjmp.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. static int fd_map; /* File descriptor for file being modified. */
  33. static int mmap_failed; /* Boolean flag. */
  34. static void *ehdr_curr; /* current ElfXX_Ehdr * for resource cleanup */
  35. static char gpfx; /* prefix for global symbol name (sometimes '_') */
  36. static struct stat sb; /* Remember .st_size, etc. */
  37. static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */
  38. /* setjmp() return values */
  39. enum {
  40. SJ_SETJMP = 0, /* hardwired first return */
  41. SJ_FAIL,
  42. SJ_SUCCEED
  43. };
  44. /* Per-file resource cleanup when multiple files. */
  45. static void
  46. cleanup(void)
  47. {
  48. if (!mmap_failed)
  49. munmap(ehdr_curr, sb.st_size);
  50. else
  51. free(ehdr_curr);
  52. close(fd_map);
  53. }
  54. static void __attribute__((noreturn))
  55. fail_file(void)
  56. {
  57. cleanup();
  58. longjmp(jmpenv, SJ_FAIL);
  59. }
  60. static void __attribute__((noreturn))
  61. succeed_file(void)
  62. {
  63. cleanup();
  64. longjmp(jmpenv, SJ_SUCCEED);
  65. }
  66. /* ulseek, uread, ...: Check return value for errors. */
  67. static off_t
  68. ulseek(int const fd, off_t const offset, int const whence)
  69. {
  70. off_t const w = lseek(fd, offset, whence);
  71. if ((off_t)-1 == w) {
  72. perror("lseek");
  73. fail_file();
  74. }
  75. return w;
  76. }
  77. static size_t
  78. uread(int const fd, void *const buf, size_t const count)
  79. {
  80. size_t const n = read(fd, buf, count);
  81. if (n != count) {
  82. perror("read");
  83. fail_file();
  84. }
  85. return n;
  86. }
  87. static size_t
  88. uwrite(int const fd, void const *const buf, size_t const count)
  89. {
  90. size_t const n = write(fd, buf, count);
  91. if (n != count) {
  92. perror("write");
  93. fail_file();
  94. }
  95. return n;
  96. }
  97. static void *
  98. umalloc(size_t size)
  99. {
  100. void *const addr = malloc(size);
  101. if (0 == addr) {
  102. fprintf(stderr, "malloc failed: %zu bytes\n", size);
  103. fail_file();
  104. }
  105. return addr;
  106. }
  107. /*
  108. * Get the whole file as a programming convenience in order to avoid
  109. * malloc+lseek+read+free of many pieces. If successful, then mmap
  110. * avoids copying unused pieces; else just read the whole file.
  111. * Open for both read and write; new info will be appended to the file.
  112. * Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
  113. * do not propagate to the file until an explicit overwrite at the last.
  114. * This preserves most aspects of consistency (all except .st_size)
  115. * for simultaneous readers of the file while we are appending to it.
  116. * However, multiple writers still are bad. We choose not to use
  117. * locking because it is expensive and the use case of kernel build
  118. * makes multiple writers unlikely.
  119. */
  120. static void *mmap_file(char const *fname)
  121. {
  122. void *addr;
  123. fd_map = open(fname, O_RDWR);
  124. if (0 > fd_map || 0 > fstat(fd_map, &sb)) {
  125. perror(fname);
  126. fail_file();
  127. }
  128. if (!S_ISREG(sb.st_mode)) {
  129. fprintf(stderr, "not a regular file: %s\n", fname);
  130. fail_file();
  131. }
  132. addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
  133. fd_map, 0);
  134. mmap_failed = 0;
  135. if (MAP_FAILED == addr) {
  136. mmap_failed = 1;
  137. addr = umalloc(sb.st_size);
  138. uread(fd_map, addr, sb.st_size);
  139. }
  140. return addr;
  141. }
  142. /* w8rev, w8nat, ...: Handle endianness. */
  143. static uint64_t w8rev(uint64_t const x)
  144. {
  145. return ((0xff & (x >> (0 * 8))) << (7 * 8))
  146. | ((0xff & (x >> (1 * 8))) << (6 * 8))
  147. | ((0xff & (x >> (2 * 8))) << (5 * 8))
  148. | ((0xff & (x >> (3 * 8))) << (4 * 8))
  149. | ((0xff & (x >> (4 * 8))) << (3 * 8))
  150. | ((0xff & (x >> (5 * 8))) << (2 * 8))
  151. | ((0xff & (x >> (6 * 8))) << (1 * 8))
  152. | ((0xff & (x >> (7 * 8))) << (0 * 8));
  153. }
  154. static uint32_t w4rev(uint32_t const x)
  155. {
  156. return ((0xff & (x >> (0 * 8))) << (3 * 8))
  157. | ((0xff & (x >> (1 * 8))) << (2 * 8))
  158. | ((0xff & (x >> (2 * 8))) << (1 * 8))
  159. | ((0xff & (x >> (3 * 8))) << (0 * 8));
  160. }
  161. static uint32_t w2rev(uint16_t const x)
  162. {
  163. return ((0xff & (x >> (0 * 8))) << (1 * 8))
  164. | ((0xff & (x >> (1 * 8))) << (0 * 8));
  165. }
  166. static uint64_t w8nat(uint64_t const x)
  167. {
  168. return x;
  169. }
  170. static uint32_t w4nat(uint32_t const x)
  171. {
  172. return x;
  173. }
  174. static uint32_t w2nat(uint16_t const x)
  175. {
  176. return x;
  177. }
  178. static uint64_t (*w8)(uint64_t);
  179. static uint32_t (*w)(uint32_t);
  180. static uint32_t (*w2)(uint16_t);
  181. /* Names of the sections that could contain calls to mcount. */
  182. static int
  183. is_mcounted_section_name(char const *const txtname)
  184. {
  185. return 0 == strcmp(".text", txtname) ||
  186. 0 == strcmp(".sched.text", txtname) ||
  187. 0 == strcmp(".spinlock.text", txtname) ||
  188. 0 == strcmp(".irqentry.text", txtname) ||
  189. 0 == strcmp(".text.unlikely", txtname);
  190. }
  191. /* Append the new shstrtab, Elf32_Shdr[], __mcount_loc and its relocations. */
  192. static void append32(Elf32_Ehdr *const ehdr,
  193. Elf32_Shdr *const shstr,
  194. uint32_t const *const mloc0,
  195. uint32_t const *const mlocp,
  196. Elf32_Rel const *const mrel0,
  197. Elf32_Rel const *const mrelp,
  198. unsigned int const rel_entsize,
  199. unsigned int const symsec_sh_link)
  200. {
  201. /* Begin constructing output file */
  202. Elf32_Shdr mcsec;
  203. char const *mc_name = (sizeof(Elf32_Rela) == rel_entsize)
  204. ? ".rela__mcount_loc"
  205. : ".rel__mcount_loc";
  206. unsigned const old_shnum = w2(ehdr->e_shnum);
  207. uint32_t const old_shoff = w(ehdr->e_shoff);
  208. uint32_t const old_shstr_sh_size = w(shstr->sh_size);
  209. uint32_t const old_shstr_sh_offset = w(shstr->sh_offset);
  210. uint32_t t = 1 + strlen(mc_name) + w(shstr->sh_size);
  211. uint32_t new_e_shoff;
  212. shstr->sh_size = w(t);
  213. shstr->sh_offset = w(sb.st_size);
  214. t += sb.st_size;
  215. t += (3u & -t); /* 4-byte align */
  216. new_e_shoff = t;
  217. /* body for new shstrtab */
  218. ulseek(fd_map, sb.st_size, SEEK_SET);
  219. uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
  220. uwrite(fd_map, mc_name, 1 + strlen(mc_name));
  221. /* old(modified) Elf32_Shdr table, 4-byte aligned */
  222. ulseek(fd_map, t, SEEK_SET);
  223. t += sizeof(Elf32_Shdr) * old_shnum;
  224. uwrite(fd_map, old_shoff + (void *)ehdr,
  225. sizeof(Elf32_Shdr) * old_shnum);
  226. /* new sections __mcount_loc and .rel__mcount_loc */
  227. t += 2*sizeof(mcsec);
  228. mcsec.sh_name = w((sizeof(Elf32_Rela) == rel_entsize) + strlen(".rel")
  229. + old_shstr_sh_size);
  230. mcsec.sh_type = w(SHT_PROGBITS);
  231. mcsec.sh_flags = w(SHF_ALLOC);
  232. mcsec.sh_addr = 0;
  233. mcsec.sh_offset = w(t);
  234. mcsec.sh_size = w((void *)mlocp - (void *)mloc0);
  235. mcsec.sh_link = 0;
  236. mcsec.sh_info = 0;
  237. mcsec.sh_addralign = w(4);
  238. mcsec.sh_entsize = w(4);
  239. uwrite(fd_map, &mcsec, sizeof(mcsec));
  240. mcsec.sh_name = w(old_shstr_sh_size);
  241. mcsec.sh_type = (sizeof(Elf32_Rela) == rel_entsize)
  242. ? w(SHT_RELA)
  243. : w(SHT_REL);
  244. mcsec.sh_flags = 0;
  245. mcsec.sh_addr = 0;
  246. mcsec.sh_offset = w((void *)mlocp - (void *)mloc0 + t);
  247. mcsec.sh_size = w((void *)mrelp - (void *)mrel0);
  248. mcsec.sh_link = w(symsec_sh_link);
  249. mcsec.sh_info = w(old_shnum);
  250. mcsec.sh_addralign = w(4);
  251. mcsec.sh_entsize = w(rel_entsize);
  252. uwrite(fd_map, &mcsec, sizeof(mcsec));
  253. uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
  254. uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
  255. ehdr->e_shoff = w(new_e_shoff);
  256. ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
  257. ulseek(fd_map, 0, SEEK_SET);
  258. uwrite(fd_map, ehdr, sizeof(*ehdr));
  259. }
  260. /*
  261. * append64 and append32 (and other analogous pairs) could be templated
  262. * using C++, but the complexity is high. (For an example, look at p_elf.h
  263. * in the source for UPX, http://upx.sourceforge.net) So: remember to make
  264. * the corresponding change in the routine for the other size.
  265. */
  266. static void append64(Elf64_Ehdr *const ehdr,
  267. Elf64_Shdr *const shstr,
  268. uint64_t const *const mloc0,
  269. uint64_t const *const mlocp,
  270. Elf64_Rel const *const mrel0,
  271. Elf64_Rel const *const mrelp,
  272. unsigned int const rel_entsize,
  273. unsigned int const symsec_sh_link)
  274. {
  275. /* Begin constructing output file */
  276. Elf64_Shdr mcsec;
  277. char const *mc_name = (sizeof(Elf64_Rela) == rel_entsize)
  278. ? ".rela__mcount_loc"
  279. : ".rel__mcount_loc";
  280. unsigned const old_shnum = w2(ehdr->e_shnum);
  281. uint64_t const old_shoff = w8(ehdr->e_shoff);
  282. uint64_t const old_shstr_sh_size = w8(shstr->sh_size);
  283. uint64_t const old_shstr_sh_offset = w8(shstr->sh_offset);
  284. uint64_t t = 1 + strlen(mc_name) + w8(shstr->sh_size);
  285. uint64_t new_e_shoff;
  286. shstr->sh_size = w8(t);
  287. shstr->sh_offset = w8(sb.st_size);
  288. t += sb.st_size;
  289. t += (7u & -t); /* 8-byte align */
  290. new_e_shoff = t;
  291. /* body for new shstrtab */
  292. ulseek(fd_map, sb.st_size, SEEK_SET);
  293. uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
  294. uwrite(fd_map, mc_name, 1 + strlen(mc_name));
  295. /* old(modified) Elf64_Shdr table, 8-byte aligned */
  296. ulseek(fd_map, t, SEEK_SET);
  297. t += sizeof(Elf64_Shdr) * old_shnum;
  298. uwrite(fd_map, old_shoff + (void *)ehdr,
  299. sizeof(Elf64_Shdr) * old_shnum);
  300. /* new sections __mcount_loc and .rel__mcount_loc */
  301. t += 2*sizeof(mcsec);
  302. mcsec.sh_name = w((sizeof(Elf64_Rela) == rel_entsize) + strlen(".rel")
  303. + old_shstr_sh_size);
  304. mcsec.sh_type = w(SHT_PROGBITS);
  305. mcsec.sh_flags = w8(SHF_ALLOC);
  306. mcsec.sh_addr = 0;
  307. mcsec.sh_offset = w8(t);
  308. mcsec.sh_size = w8((void *)mlocp - (void *)mloc0);
  309. mcsec.sh_link = 0;
  310. mcsec.sh_info = 0;
  311. mcsec.sh_addralign = w8(8);
  312. mcsec.sh_entsize = w8(8);
  313. uwrite(fd_map, &mcsec, sizeof(mcsec));
  314. mcsec.sh_name = w(old_shstr_sh_size);
  315. mcsec.sh_type = (sizeof(Elf64_Rela) == rel_entsize)
  316. ? w(SHT_RELA)
  317. : w(SHT_REL);
  318. mcsec.sh_flags = 0;
  319. mcsec.sh_addr = 0;
  320. mcsec.sh_offset = w8((void *)mlocp - (void *)mloc0 + t);
  321. mcsec.sh_size = w8((void *)mrelp - (void *)mrel0);
  322. mcsec.sh_link = w(symsec_sh_link);
  323. mcsec.sh_info = w(old_shnum);
  324. mcsec.sh_addralign = w8(8);
  325. mcsec.sh_entsize = w8(rel_entsize);
  326. uwrite(fd_map, &mcsec, sizeof(mcsec));
  327. uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
  328. uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);
  329. ehdr->e_shoff = w8(new_e_shoff);
  330. ehdr->e_shnum = w2(2 + w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
  331. ulseek(fd_map, 0, SEEK_SET);
  332. uwrite(fd_map, ehdr, sizeof(*ehdr));
  333. }
  334. /*
  335. * Look at the relocations in order to find the calls to mcount.
  336. * Accumulate the section offsets that are found, and their relocation info,
  337. * onto the end of the existing arrays.
  338. */
  339. static uint32_t *sift32_rel_mcount(uint32_t *mlocp,
  340. unsigned const offbase,
  341. Elf32_Rel **const mrelpp,
  342. Elf32_Shdr const *const relhdr,
  343. Elf32_Ehdr const *const ehdr,
  344. unsigned const recsym,
  345. uint32_t const recval,
  346. unsigned const reltype)
  347. {
  348. uint32_t *const mloc0 = mlocp;
  349. Elf32_Rel *mrelp = *mrelpp;
  350. Elf32_Shdr *const shdr0 = (Elf32_Shdr *)(w(ehdr->e_shoff)
  351. + (void *)ehdr);
  352. unsigned const symsec_sh_link = w(relhdr->sh_link);
  353. Elf32_Shdr const *const symsec = &shdr0[symsec_sh_link];
  354. Elf32_Sym const *const sym0 = (Elf32_Sym const *)(w(symsec->sh_offset)
  355. + (void *)ehdr);
  356. Elf32_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
  357. char const *const str0 = (char const *)(w(strsec->sh_offset)
  358. + (void *)ehdr);
  359. Elf32_Rel const *const rel0 = (Elf32_Rel const *)(w(relhdr->sh_offset)
  360. + (void *)ehdr);
  361. unsigned rel_entsize = w(relhdr->sh_entsize);
  362. unsigned const nrel = w(relhdr->sh_size) / rel_entsize;
  363. Elf32_Rel const *relp = rel0;
  364. unsigned mcountsym = 0;
  365. unsigned t;
  366. for (t = nrel; t; --t) {
  367. if (!mcountsym) {
  368. Elf32_Sym const *const symp =
  369. &sym0[ELF32_R_SYM(w(relp->r_info))];
  370. if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
  371. &str0[w(symp->st_name)]))
  372. mcountsym = ELF32_R_SYM(w(relp->r_info));
  373. }
  374. if (mcountsym == ELF32_R_SYM(w(relp->r_info))) {
  375. uint32_t const addend = w(w(relp->r_offset) - recval);
  376. mrelp->r_offset = w(offbase
  377. + ((void *)mlocp - (void *)mloc0));
  378. mrelp->r_info = w(ELF32_R_INFO(recsym, reltype));
  379. if (sizeof(Elf32_Rela) == rel_entsize) {
  380. ((Elf32_Rela *)mrelp)->r_addend = addend;
  381. *mlocp++ = 0;
  382. } else
  383. *mlocp++ = addend;
  384. mrelp = (Elf32_Rel *)(rel_entsize + (void *)mrelp);
  385. }
  386. relp = (Elf32_Rel const *)(rel_entsize + (void *)relp);
  387. }
  388. *mrelpp = mrelp;
  389. return mlocp;
  390. }
  391. static uint64_t *sift64_rel_mcount(uint64_t *mlocp,
  392. unsigned const offbase,
  393. Elf64_Rel **const mrelpp,
  394. Elf64_Shdr const *const relhdr,
  395. Elf64_Ehdr const *const ehdr,
  396. unsigned const recsym,
  397. uint64_t const recval,
  398. unsigned const reltype)
  399. {
  400. uint64_t *const mloc0 = mlocp;
  401. Elf64_Rel *mrelp = *mrelpp;
  402. Elf64_Shdr *const shdr0 = (Elf64_Shdr *)(w8(ehdr->e_shoff)
  403. + (void *)ehdr);
  404. unsigned const symsec_sh_link = w(relhdr->sh_link);
  405. Elf64_Shdr const *const symsec = &shdr0[symsec_sh_link];
  406. Elf64_Sym const *const sym0 = (Elf64_Sym const *)(w8(symsec->sh_offset)
  407. + (void *)ehdr);
  408. Elf64_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
  409. char const *const str0 = (char const *)(w8(strsec->sh_offset)
  410. + (void *)ehdr);
  411. Elf64_Rel const *const rel0 = (Elf64_Rel const *)(w8(relhdr->sh_offset)
  412. + (void *)ehdr);
  413. unsigned rel_entsize = w8(relhdr->sh_entsize);
  414. unsigned const nrel = w8(relhdr->sh_size) / rel_entsize;
  415. Elf64_Rel const *relp = rel0;
  416. unsigned mcountsym = 0;
  417. unsigned t;
  418. for (t = nrel; 0 != t; --t) {
  419. if (!mcountsym) {
  420. Elf64_Sym const *const symp =
  421. &sym0[ELF64_R_SYM(w8(relp->r_info))];
  422. char const *symname = &str0[w(symp->st_name)];
  423. if ('.' == symname[0])
  424. ++symname; /* ppc64 hack */
  425. if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
  426. symname))
  427. mcountsym = ELF64_R_SYM(w8(relp->r_info));
  428. }
  429. if (mcountsym == ELF64_R_SYM(w8(relp->r_info))) {
  430. uint64_t const addend = w8(w8(relp->r_offset) - recval);
  431. mrelp->r_offset = w8(offbase
  432. + ((void *)mlocp - (void *)mloc0));
  433. mrelp->r_info = w8(ELF64_R_INFO(recsym, reltype));
  434. if (sizeof(Elf64_Rela) == rel_entsize) {
  435. ((Elf64_Rela *)mrelp)->r_addend = addend;
  436. *mlocp++ = 0;
  437. } else
  438. *mlocp++ = addend;
  439. mrelp = (Elf64_Rel *)(rel_entsize + (void *)mrelp);
  440. }
  441. relp = (Elf64_Rel const *)(rel_entsize + (void *)relp);
  442. }
  443. *mrelpp = mrelp;
  444. return mlocp;
  445. }
  446. /*
  447. * Find a symbol in the given section, to be used as the base for relocating
  448. * the table of offsets of calls to mcount. A local or global symbol suffices,
  449. * but avoid a Weak symbol because it may be overridden; the change in value
  450. * would invalidate the relocations of the offsets of the calls to mcount.
  451. * Often the found symbol will be the unnamed local symbol generated by
  452. * GNU 'as' for the start of each section. For example:
  453. * Num: Value Size Type Bind Vis Ndx Name
  454. * 2: 00000000 0 SECTION LOCAL DEFAULT 1
  455. */
  456. static unsigned find32_secsym_ndx(unsigned const txtndx,
  457. char const *const txtname,
  458. uint32_t *const recvalp,
  459. Elf32_Shdr const *const symhdr,
  460. Elf32_Ehdr const *const ehdr)
  461. {
  462. Elf32_Sym const *const sym0 = (Elf32_Sym const *)(w(symhdr->sh_offset)
  463. + (void *)ehdr);
  464. unsigned const nsym = w(symhdr->sh_size) / w(symhdr->sh_entsize);
  465. Elf32_Sym const *symp;
  466. unsigned t;
  467. for (symp = sym0, t = nsym; t; --t, ++symp) {
  468. unsigned int const st_bind = ELF32_ST_BIND(symp->st_info);
  469. if (txtndx == w2(symp->st_shndx)
  470. /* avoid STB_WEAK */
  471. && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
  472. *recvalp = w(symp->st_value);
  473. return symp - sym0;
  474. }
  475. }
  476. fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
  477. txtndx, txtname);
  478. fail_file();
  479. }
  480. static unsigned find64_secsym_ndx(unsigned const txtndx,
  481. char const *const txtname,
  482. uint64_t *const recvalp,
  483. Elf64_Shdr const *const symhdr,
  484. Elf64_Ehdr const *const ehdr)
  485. {
  486. Elf64_Sym const *const sym0 = (Elf64_Sym const *)(w8(symhdr->sh_offset)
  487. + (void *)ehdr);
  488. unsigned const nsym = w8(symhdr->sh_size) / w8(symhdr->sh_entsize);
  489. Elf64_Sym const *symp;
  490. unsigned t;
  491. for (symp = sym0, t = nsym; t; --t, ++symp) {
  492. unsigned int const st_bind = ELF64_ST_BIND(symp->st_info);
  493. if (txtndx == w2(symp->st_shndx)
  494. /* avoid STB_WEAK */
  495. && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
  496. *recvalp = w8(symp->st_value);
  497. return symp - sym0;
  498. }
  499. }
  500. fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
  501. txtndx, txtname);
  502. fail_file();
  503. }
  504. /*
  505. * Evade ISO C restriction: no declaration after statement in
  506. * has32_rel_mcount.
  507. */
  508. static char const *
  509. __has32_rel_mcount(Elf32_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
  510. Elf32_Shdr const *const shdr0,
  511. char const *const shstrtab,
  512. char const *const fname)
  513. {
  514. /* .sh_info depends on .sh_type == SHT_REL[,A] */
  515. Elf32_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
  516. char const *const txtname = &shstrtab[w(txthdr->sh_name)];
  517. if (0 == strcmp("__mcount_loc", txtname)) {
  518. fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
  519. fname);
  520. succeed_file();
  521. }
  522. if (SHT_PROGBITS != w(txthdr->sh_type) ||
  523. !is_mcounted_section_name(txtname))
  524. return NULL;
  525. return txtname;
  526. }
  527. static char const *has32_rel_mcount(Elf32_Shdr const *const relhdr,
  528. Elf32_Shdr const *const shdr0,
  529. char const *const shstrtab,
  530. char const *const fname)
  531. {
  532. if (SHT_REL != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
  533. return NULL;
  534. return __has32_rel_mcount(relhdr, shdr0, shstrtab, fname);
  535. }
  536. static char const *__has64_rel_mcount(Elf64_Shdr const *const relhdr,
  537. Elf64_Shdr const *const shdr0,
  538. char const *const shstrtab,
  539. char const *const fname)
  540. {
  541. /* .sh_info depends on .sh_type == SHT_REL[,A] */
  542. Elf64_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
  543. char const *const txtname = &shstrtab[w(txthdr->sh_name)];
  544. if (0 == strcmp("__mcount_loc", txtname)) {
  545. fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
  546. fname);
  547. succeed_file();
  548. }
  549. if (SHT_PROGBITS != w(txthdr->sh_type) ||
  550. !is_mcounted_section_name(txtname))
  551. return NULL;
  552. return txtname;
  553. }
  554. static char const *has64_rel_mcount(Elf64_Shdr const *const relhdr,
  555. Elf64_Shdr const *const shdr0,
  556. char const *const shstrtab,
  557. char const *const fname)
  558. {
  559. if (SHT_REL != w(relhdr->sh_type) && SHT_RELA != w(relhdr->sh_type))
  560. return NULL;
  561. return __has64_rel_mcount(relhdr, shdr0, shstrtab, fname);
  562. }
  563. static unsigned tot32_relsize(Elf32_Shdr const *const shdr0,
  564. unsigned nhdr,
  565. const char *const shstrtab,
  566. const char *const fname)
  567. {
  568. unsigned totrelsz = 0;
  569. Elf32_Shdr const *shdrp = shdr0;
  570. for (; 0 != nhdr; --nhdr, ++shdrp) {
  571. if (has32_rel_mcount(shdrp, shdr0, shstrtab, fname))
  572. totrelsz += w(shdrp->sh_size);
  573. }
  574. return totrelsz;
  575. }
  576. static unsigned tot64_relsize(Elf64_Shdr const *const shdr0,
  577. unsigned nhdr,
  578. const char *const shstrtab,
  579. const char *const fname)
  580. {
  581. unsigned totrelsz = 0;
  582. Elf64_Shdr const *shdrp = shdr0;
  583. for (; nhdr; --nhdr, ++shdrp) {
  584. if (has64_rel_mcount(shdrp, shdr0, shstrtab, fname))
  585. totrelsz += w8(shdrp->sh_size);
  586. }
  587. return totrelsz;
  588. }
  589. /* Overall supervision for Elf32 ET_REL file. */
  590. static void
  591. do32(Elf32_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
  592. {
  593. Elf32_Shdr *const shdr0 = (Elf32_Shdr *)(w(ehdr->e_shoff)
  594. + (void *)ehdr);
  595. unsigned const nhdr = w2(ehdr->e_shnum);
  596. Elf32_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
  597. char const *const shstrtab = (char const *)(w(shstr->sh_offset)
  598. + (void *)ehdr);
  599. Elf32_Shdr const *relhdr;
  600. unsigned k;
  601. /* Upper bound on space: assume all relevant relocs are for mcount. */
  602. unsigned const totrelsz = tot32_relsize(shdr0, nhdr, shstrtab, fname);
  603. Elf32_Rel *const mrel0 = umalloc(totrelsz);
  604. Elf32_Rel * mrelp = mrel0;
  605. /* 2*sizeof(address) <= sizeof(Elf32_Rel) */
  606. uint32_t *const mloc0 = umalloc(totrelsz>>1);
  607. uint32_t * mlocp = mloc0;
  608. unsigned rel_entsize = 0;
  609. unsigned symsec_sh_link = 0;
  610. for (relhdr = shdr0, k = nhdr; k; --k, ++relhdr) {
  611. char const *const txtname = has32_rel_mcount(relhdr, shdr0,
  612. shstrtab, fname);
  613. if (txtname) {
  614. uint32_t recval = 0;
  615. unsigned const recsym = find32_secsym_ndx(
  616. w(relhdr->sh_info), txtname, &recval,
  617. &shdr0[symsec_sh_link = w(relhdr->sh_link)],
  618. ehdr);
  619. rel_entsize = w(relhdr->sh_entsize);
  620. mlocp = sift32_rel_mcount(mlocp,
  621. (void *)mlocp - (void *)mloc0, &mrelp,
  622. relhdr, ehdr, recsym, recval, reltype);
  623. }
  624. }
  625. if (mloc0 != mlocp) {
  626. append32(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
  627. rel_entsize, symsec_sh_link);
  628. }
  629. free(mrel0);
  630. free(mloc0);
  631. }
  632. static void
  633. do64(Elf64_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
  634. {
  635. Elf64_Shdr *const shdr0 = (Elf64_Shdr *)(w8(ehdr->e_shoff)
  636. + (void *)ehdr);
  637. unsigned const nhdr = w2(ehdr->e_shnum);
  638. Elf64_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
  639. char const *const shstrtab = (char const *)(w8(shstr->sh_offset)
  640. + (void *)ehdr);
  641. Elf64_Shdr const *relhdr;
  642. unsigned k;
  643. /* Upper bound on space: assume all relevant relocs are for mcount. */
  644. unsigned const totrelsz = tot64_relsize(shdr0, nhdr, shstrtab, fname);
  645. Elf64_Rel *const mrel0 = umalloc(totrelsz);
  646. Elf64_Rel * mrelp = mrel0;
  647. /* 2*sizeof(address) <= sizeof(Elf64_Rel) */
  648. uint64_t *const mloc0 = umalloc(totrelsz>>1);
  649. uint64_t * mlocp = mloc0;
  650. unsigned rel_entsize = 0;
  651. unsigned symsec_sh_link = 0;
  652. for ((relhdr = shdr0), k = nhdr; k; --k, ++relhdr) {
  653. char const *const txtname = has64_rel_mcount(relhdr, shdr0,
  654. shstrtab, fname);
  655. if (txtname) {
  656. uint64_t recval = 0;
  657. unsigned const recsym = find64_secsym_ndx(
  658. w(relhdr->sh_info), txtname, &recval,
  659. &shdr0[symsec_sh_link = w(relhdr->sh_link)],
  660. ehdr);
  661. rel_entsize = w8(relhdr->sh_entsize);
  662. mlocp = sift64_rel_mcount(mlocp,
  663. (void *)mlocp - (void *)mloc0, &mrelp,
  664. relhdr, ehdr, recsym, recval, reltype);
  665. }
  666. }
  667. if (mloc0 != mlocp) {
  668. append64(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
  669. rel_entsize, symsec_sh_link);
  670. }
  671. free(mrel0);
  672. free(mloc0);
  673. }
  674. static void
  675. do_file(char const *const fname)
  676. {
  677. Elf32_Ehdr *const ehdr = mmap_file(fname);
  678. unsigned int reltype = 0;
  679. ehdr_curr = ehdr;
  680. w = w4nat;
  681. w2 = w2nat;
  682. w8 = w8nat;
  683. switch (ehdr->e_ident[EI_DATA]) {
  684. static unsigned int const endian = 1;
  685. default: {
  686. fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
  687. ehdr->e_ident[EI_DATA], fname);
  688. fail_file();
  689. } break;
  690. case ELFDATA2LSB: {
  691. if (1 != *(unsigned char const *)&endian) {
  692. /* main() is big endian, file.o is little endian. */
  693. w = w4rev;
  694. w2 = w2rev;
  695. w8 = w8rev;
  696. }
  697. } break;
  698. case ELFDATA2MSB: {
  699. if (0 != *(unsigned char const *)&endian) {
  700. /* main() is little endian, file.o is big endian. */
  701. w = w4rev;
  702. w2 = w2rev;
  703. w8 = w8rev;
  704. }
  705. } break;
  706. } /* end switch */
  707. if (0 != memcmp(ELFMAG, ehdr->e_ident, SELFMAG)
  708. || ET_REL != w2(ehdr->e_type)
  709. || EV_CURRENT != ehdr->e_ident[EI_VERSION]) {
  710. fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
  711. fail_file();
  712. }
  713. gpfx = 0;
  714. switch (w2(ehdr->e_machine)) {
  715. default: {
  716. fprintf(stderr, "unrecognized e_machine %d %s\n",
  717. w2(ehdr->e_machine), fname);
  718. fail_file();
  719. } break;
  720. case EM_386: reltype = R_386_32; break;
  721. case EM_ARM: reltype = R_ARM_ABS32; break;
  722. case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break;
  723. case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break;
  724. case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break;
  725. case EM_S390: /* reltype: e_class */ gpfx = '_'; break;
  726. case EM_SH: reltype = R_SH_DIR32; break;
  727. case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break;
  728. case EM_X86_64: reltype = R_X86_64_64; break;
  729. } /* end switch */
  730. switch (ehdr->e_ident[EI_CLASS]) {
  731. default: {
  732. fprintf(stderr, "unrecognized ELF class %d %s\n",
  733. ehdr->e_ident[EI_CLASS], fname);
  734. fail_file();
  735. } break;
  736. case ELFCLASS32: {
  737. if (sizeof(Elf32_Ehdr) != w2(ehdr->e_ehsize)
  738. || sizeof(Elf32_Shdr) != w2(ehdr->e_shentsize)) {
  739. fprintf(stderr,
  740. "unrecognized ET_REL file: %s\n", fname);
  741. fail_file();
  742. }
  743. if (EM_S390 == w2(ehdr->e_machine))
  744. reltype = R_390_32;
  745. do32(ehdr, fname, reltype);
  746. } break;
  747. case ELFCLASS64: {
  748. Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
  749. if (sizeof(Elf64_Ehdr) != w2(ghdr->e_ehsize)
  750. || sizeof(Elf64_Shdr) != w2(ghdr->e_shentsize)) {
  751. fprintf(stderr,
  752. "unrecognized ET_REL file: %s\n", fname);
  753. fail_file();
  754. }
  755. if (EM_S390 == w2(ghdr->e_machine))
  756. reltype = R_390_64;
  757. do64(ghdr, fname, reltype);
  758. } break;
  759. } /* end switch */
  760. cleanup();
  761. }
  762. int
  763. main(int argc, char const *argv[])
  764. {
  765. int n_error = 0; /* gcc-4.3.0 false positive complaint */
  766. if (argc <= 1)
  767. fprintf(stderr, "usage: recordmcount file.o...\n");
  768. else /* Process each file in turn, allowing deep failure. */
  769. for (--argc, ++argv; 0 < argc; --argc, ++argv) {
  770. int const sjval = setjmp(jmpenv);
  771. switch (sjval) {
  772. default: {
  773. fprintf(stderr, "internal error: %s\n", argv[0]);
  774. exit(1);
  775. } break;
  776. case SJ_SETJMP: { /* normal sequence */
  777. /* Avoid problems if early cleanup() */
  778. fd_map = -1;
  779. ehdr_curr = NULL;
  780. mmap_failed = 1;
  781. do_file(argv[0]);
  782. } break;
  783. case SJ_FAIL: { /* error in do_file or below */
  784. ++n_error;
  785. } break;
  786. case SJ_SUCCEED: { /* premature success */
  787. /* do nothing */
  788. } break;
  789. } /* end switch */
  790. }
  791. return !!n_error;
  792. }