genelf.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * genelf.c
  3. * Copyright (C) 2014, Google, Inc
  4. *
  5. * Contributed by:
  6. * Stephane Eranian <eranian@gmail.com>
  7. *
  8. * Released under the GPL v2. (and only v2, not any later version)
  9. */
  10. #include <sys/types.h>
  11. #include <stdio.h>
  12. #include <getopt.h>
  13. #include <stddef.h>
  14. #include <libelf.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <inttypes.h>
  18. #include <limits.h>
  19. #include <fcntl.h>
  20. #include <err.h>
  21. #ifdef HAVE_DWARF_SUPPORT
  22. #include <dwarf.h>
  23. #endif
  24. #include "perf.h"
  25. #include "genelf.h"
  26. #include "../util/jitdump.h"
  27. #define JVMTI
  28. #define BUILD_ID_URANDOM /* different uuid for each run */
  29. #ifdef HAVE_LIBCRYPTO
  30. #define BUILD_ID_MD5
  31. #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */
  32. #undef BUILD_ID_URANDOM /* different uuid for each run */
  33. #ifdef BUILD_ID_SHA
  34. #include <openssl/sha.h>
  35. #endif
  36. #ifdef BUILD_ID_MD5
  37. #include <openssl/md5.h>
  38. #endif
  39. #endif
  40. typedef struct {
  41. unsigned int namesz; /* Size of entry's owner string */
  42. unsigned int descsz; /* Size of the note descriptor */
  43. unsigned int type; /* Interpretation of the descriptor */
  44. char name[0]; /* Start of the name+desc data */
  45. } Elf_Note;
  46. struct options {
  47. char *output;
  48. int fd;
  49. };
  50. static char shd_string_table[] = {
  51. 0,
  52. '.', 't', 'e', 'x', 't', 0, /* 1 */
  53. '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0, /* 7 */
  54. '.', 's', 'y', 'm', 't', 'a', 'b', 0, /* 17 */
  55. '.', 's', 't', 'r', 't', 'a', 'b', 0, /* 25 */
  56. '.', 'n', 'o', 't', 'e', '.', 'g', 'n', 'u', '.', 'b', 'u', 'i', 'l', 'd', '-', 'i', 'd', 0, /* 33 */
  57. '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
  58. '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
  59. '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
  60. };
  61. static struct buildid_note {
  62. Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */
  63. char name[4]; /* GNU\0 */
  64. char build_id[20];
  65. } bnote;
  66. static Elf_Sym symtab[]={
  67. /* symbol 0 MUST be the undefined symbol */
  68. { .st_name = 0, /* index in sym_string table */
  69. .st_info = ELF_ST_TYPE(STT_NOTYPE),
  70. .st_shndx = 0, /* for now */
  71. .st_value = 0x0,
  72. .st_other = ELF_ST_VIS(STV_DEFAULT),
  73. .st_size = 0,
  74. },
  75. { .st_name = 1, /* index in sym_string table */
  76. .st_info = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
  77. .st_shndx = 1,
  78. .st_value = 0, /* for now */
  79. .st_other = ELF_ST_VIS(STV_DEFAULT),
  80. .st_size = 0, /* for now */
  81. }
  82. };
  83. #ifdef BUILD_ID_URANDOM
  84. static void
  85. gen_build_id(struct buildid_note *note,
  86. unsigned long load_addr __maybe_unused,
  87. const void *code __maybe_unused,
  88. size_t csize __maybe_unused)
  89. {
  90. int fd;
  91. size_t sz = sizeof(note->build_id);
  92. ssize_t sret;
  93. fd = open("/dev/urandom", O_RDONLY);
  94. if (fd == -1)
  95. err(1, "cannot access /dev/urandom for builid");
  96. sret = read(fd, note->build_id, sz);
  97. close(fd);
  98. if (sret != (ssize_t)sz)
  99. memset(note->build_id, 0, sz);
  100. }
  101. #endif
  102. #ifdef BUILD_ID_SHA
  103. static void
  104. gen_build_id(struct buildid_note *note,
  105. unsigned long load_addr __maybe_unused,
  106. const void *code,
  107. size_t csize)
  108. {
  109. if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
  110. errx(1, "build_id too small for SHA1");
  111. SHA1(code, csize, (unsigned char *)note->build_id);
  112. }
  113. #endif
  114. #ifdef BUILD_ID_MD5
  115. static void
  116. gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
  117. {
  118. MD5_CTX context;
  119. if (sizeof(note->build_id) < 16)
  120. errx(1, "build_id too small for MD5");
  121. MD5_Init(&context);
  122. MD5_Update(&context, &load_addr, sizeof(load_addr));
  123. MD5_Update(&context, code, csize);
  124. MD5_Final((unsigned char *)note->build_id, &context);
  125. }
  126. #endif
  127. /*
  128. * fd: file descriptor open for writing for the output file
  129. * load_addr: code load address (could be zero, just used for buildid)
  130. * sym: function name (for native code - used as the symbol)
  131. * code: the native code
  132. * csize: the code size in bytes
  133. */
  134. int
  135. jit_write_elf(int fd, uint64_t load_addr, const char *sym,
  136. const void *code, int csize,
  137. void *debug __maybe_unused, int nr_debug_entries __maybe_unused)
  138. {
  139. Elf *e;
  140. Elf_Data *d;
  141. Elf_Scn *scn;
  142. Elf_Ehdr *ehdr;
  143. Elf_Shdr *shdr;
  144. char *strsym = NULL;
  145. int symlen;
  146. int retval = -1;
  147. if (elf_version(EV_CURRENT) == EV_NONE) {
  148. warnx("ELF initialization failed");
  149. return -1;
  150. }
  151. e = elf_begin(fd, ELF_C_WRITE, NULL);
  152. if (!e) {
  153. warnx("elf_begin failed");
  154. goto error;
  155. }
  156. /*
  157. * setup ELF header
  158. */
  159. ehdr = elf_newehdr(e);
  160. if (!ehdr) {
  161. warnx("cannot get ehdr");
  162. goto error;
  163. }
  164. ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
  165. ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
  166. ehdr->e_machine = GEN_ELF_ARCH;
  167. ehdr->e_type = ET_DYN;
  168. ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
  169. ehdr->e_version = EV_CURRENT;
  170. ehdr->e_shstrndx= 2; /* shdr index for section name */
  171. /*
  172. * setup text section
  173. */
  174. scn = elf_newscn(e);
  175. if (!scn) {
  176. warnx("cannot create section");
  177. goto error;
  178. }
  179. d = elf_newdata(scn);
  180. if (!d) {
  181. warnx("cannot get new data");
  182. goto error;
  183. }
  184. d->d_align = 16;
  185. d->d_off = 0LL;
  186. d->d_buf = (void *)code;
  187. d->d_type = ELF_T_BYTE;
  188. d->d_size = csize;
  189. d->d_version = EV_CURRENT;
  190. shdr = elf_getshdr(scn);
  191. if (!shdr) {
  192. warnx("cannot get section header");
  193. goto error;
  194. }
  195. shdr->sh_name = 1;
  196. shdr->sh_type = SHT_PROGBITS;
  197. shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
  198. shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  199. shdr->sh_entsize = 0;
  200. /*
  201. * setup section headers string table
  202. */
  203. scn = elf_newscn(e);
  204. if (!scn) {
  205. warnx("cannot create section");
  206. goto error;
  207. }
  208. d = elf_newdata(scn);
  209. if (!d) {
  210. warnx("cannot get new data");
  211. goto error;
  212. }
  213. d->d_align = 1;
  214. d->d_off = 0LL;
  215. d->d_buf = shd_string_table;
  216. d->d_type = ELF_T_BYTE;
  217. d->d_size = sizeof(shd_string_table);
  218. d->d_version = EV_CURRENT;
  219. shdr = elf_getshdr(scn);
  220. if (!shdr) {
  221. warnx("cannot get section header");
  222. goto error;
  223. }
  224. shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
  225. shdr->sh_type = SHT_STRTAB;
  226. shdr->sh_flags = 0;
  227. shdr->sh_entsize = 0;
  228. /*
  229. * setup symtab section
  230. */
  231. symtab[1].st_size = csize;
  232. symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
  233. scn = elf_newscn(e);
  234. if (!scn) {
  235. warnx("cannot create section");
  236. goto error;
  237. }
  238. d = elf_newdata(scn);
  239. if (!d) {
  240. warnx("cannot get new data");
  241. goto error;
  242. }
  243. d->d_align = 8;
  244. d->d_off = 0LL;
  245. d->d_buf = symtab;
  246. d->d_type = ELF_T_SYM;
  247. d->d_size = sizeof(symtab);
  248. d->d_version = EV_CURRENT;
  249. shdr = elf_getshdr(scn);
  250. if (!shdr) {
  251. warnx("cannot get section header");
  252. goto error;
  253. }
  254. shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
  255. shdr->sh_type = SHT_SYMTAB;
  256. shdr->sh_flags = 0;
  257. shdr->sh_entsize = sizeof(Elf_Sym);
  258. shdr->sh_link = 4; /* index of .strtab section */
  259. /*
  260. * setup symbols string table
  261. * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
  262. */
  263. symlen = 2 + strlen(sym);
  264. strsym = calloc(1, symlen);
  265. if (!strsym) {
  266. warnx("cannot allocate strsym");
  267. goto error;
  268. }
  269. strcpy(strsym + 1, sym);
  270. scn = elf_newscn(e);
  271. if (!scn) {
  272. warnx("cannot create section");
  273. goto error;
  274. }
  275. d = elf_newdata(scn);
  276. if (!d) {
  277. warnx("cannot get new data");
  278. goto error;
  279. }
  280. d->d_align = 1;
  281. d->d_off = 0LL;
  282. d->d_buf = strsym;
  283. d->d_type = ELF_T_BYTE;
  284. d->d_size = symlen;
  285. d->d_version = EV_CURRENT;
  286. shdr = elf_getshdr(scn);
  287. if (!shdr) {
  288. warnx("cannot get section header");
  289. goto error;
  290. }
  291. shdr->sh_name = 25; /* offset in shd_string_table */
  292. shdr->sh_type = SHT_STRTAB;
  293. shdr->sh_flags = 0;
  294. shdr->sh_entsize = 0;
  295. /*
  296. * setup build-id section
  297. */
  298. scn = elf_newscn(e);
  299. if (!scn) {
  300. warnx("cannot create section");
  301. goto error;
  302. }
  303. d = elf_newdata(scn);
  304. if (!d) {
  305. warnx("cannot get new data");
  306. goto error;
  307. }
  308. /*
  309. * build-id generation
  310. */
  311. gen_build_id(&bnote, load_addr, code, csize);
  312. bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
  313. bnote.desc.descsz = sizeof(bnote.build_id);
  314. bnote.desc.type = NT_GNU_BUILD_ID;
  315. strcpy(bnote.name, "GNU");
  316. d->d_align = 4;
  317. d->d_off = 0LL;
  318. d->d_buf = &bnote;
  319. d->d_type = ELF_T_BYTE;
  320. d->d_size = sizeof(bnote);
  321. d->d_version = EV_CURRENT;
  322. shdr = elf_getshdr(scn);
  323. if (!shdr) {
  324. warnx("cannot get section header");
  325. goto error;
  326. }
  327. shdr->sh_name = 33; /* offset in shd_string_table */
  328. shdr->sh_type = SHT_NOTE;
  329. shdr->sh_addr = 0x0;
  330. shdr->sh_flags = SHF_ALLOC;
  331. shdr->sh_size = sizeof(bnote);
  332. shdr->sh_entsize = 0;
  333. #ifdef HAVE_DWARF_SUPPORT
  334. if (debug && nr_debug_entries) {
  335. retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
  336. if (retval)
  337. goto error;
  338. } else
  339. #endif
  340. {
  341. if (elf_update(e, ELF_C_WRITE) < 0) {
  342. warnx("elf_update 4 failed");
  343. goto error;
  344. }
  345. }
  346. retval = 0;
  347. error:
  348. (void)elf_end(e);
  349. free(strsym);
  350. return retval;
  351. }
  352. #ifndef JVMTI
  353. static unsigned char x86_code[] = {
  354. 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
  355. 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
  356. 0xCD, 0x80 /* int $0x80 */
  357. };
  358. static struct options options;
  359. int main(int argc, char **argv)
  360. {
  361. int c, fd, ret;
  362. while ((c = getopt(argc, argv, "o:h")) != -1) {
  363. switch (c) {
  364. case 'o':
  365. options.output = optarg;
  366. break;
  367. case 'h':
  368. printf("Usage: genelf -o output_file [-h]\n");
  369. return 0;
  370. default:
  371. errx(1, "unknown option");
  372. }
  373. }
  374. fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
  375. if (fd == -1)
  376. err(1, "cannot create file %s", options.output);
  377. ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
  378. close(fd);
  379. if (ret != 0)
  380. unlink(options.output);
  381. return ret;
  382. }
  383. #endif