genelf.c 11 KB

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