3 * Copyright (C) 2014, Google, Inc
6 * Stephane Eranian <eranian@gmail.com>
8 * Released under the GPL v2. (and only v2, not any later version)
11 #include <sys/types.h>
22 #ifdef HAVE_DWARF_SUPPORT
28 #include "../util/jitdump.h"
30 #ifndef NT_GNU_BUILD_ID
31 #define NT_GNU_BUILD_ID 3
36 #define BUILD_ID_URANDOM /* different uuid for each run */
38 // FIXME, remove this and fix the deprecation warnings before its removed and
39 // We'll break for good here...
40 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
42 #ifdef HAVE_LIBCRYPTO_SUPPORT
45 #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */
46 #undef BUILD_ID_URANDOM /* different uuid for each run */
49 #include <openssl/sha.h>
53 #include <openssl/md5.h>
59 unsigned int namesz; /* Size of entry's owner string */
60 unsigned int descsz; /* Size of the note descriptor */
61 unsigned int type; /* Interpretation of the descriptor */
62 char name[0]; /* Start of the name+desc data */
70 static char shd_string_table[] = {
72 '.', 't', 'e', 'x', 't', 0, /* 1 */
73 '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0, /* 7 */
74 '.', 's', 'y', 'm', 't', 'a', 'b', 0, /* 17 */
75 '.', 's', 't', 'r', 't', 'a', 'b', 0, /* 25 */
76 '.', 'n', 'o', 't', 'e', '.', 'g', 'n', 'u', '.', 'b', 'u', 'i', 'l', 'd', '-', 'i', 'd', 0, /* 33 */
77 '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
78 '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
79 '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
80 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 90 */
81 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
84 static struct buildid_note {
85 Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */
86 char name[4]; /* GNU\0 */
90 static Elf_Sym symtab[]={
91 /* symbol 0 MUST be the undefined symbol */
92 { .st_name = 0, /* index in sym_string table */
93 .st_info = ELF_ST_TYPE(STT_NOTYPE),
94 .st_shndx = 0, /* for now */
96 .st_other = ELF_ST_VIS(STV_DEFAULT),
99 { .st_name = 1, /* index in sym_string table */
100 .st_info = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
102 .st_value = 0, /* for now */
103 .st_other = ELF_ST_VIS(STV_DEFAULT),
104 .st_size = 0, /* for now */
108 #ifdef BUILD_ID_URANDOM
110 gen_build_id(struct buildid_note *note,
111 unsigned long load_addr __maybe_unused,
112 const void *code __maybe_unused,
113 size_t csize __maybe_unused)
116 size_t sz = sizeof(note->build_id);
119 fd = open("/dev/urandom", O_RDONLY);
121 err(1, "cannot access /dev/urandom for builid");
123 sret = read(fd, note->build_id, sz);
127 if (sret != (ssize_t)sz)
128 memset(note->build_id, 0, sz);
134 gen_build_id(struct buildid_note *note,
135 unsigned long load_addr __maybe_unused,
139 if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
140 errx(1, "build_id too small for SHA1");
142 SHA1(code, csize, (unsigned char *)note->build_id);
148 gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
152 if (sizeof(note->build_id) < 16)
153 errx(1, "build_id too small for MD5");
156 MD5_Update(&context, &load_addr, sizeof(load_addr));
157 MD5_Update(&context, code, csize);
158 MD5_Final((unsigned char *)note->build_id, &context);
163 jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
164 uint64_t unwinding_size, uint64_t base_offset)
169 uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
172 * setup eh_frame section
176 warnx("cannot create section");
180 d = elf_newdata(scn);
182 warnx("cannot get new data");
188 d->d_buf = unwinding;
189 d->d_type = ELF_T_BYTE;
190 d->d_size = unwinding_table_size;
191 d->d_version = EV_CURRENT;
193 shdr = elf_getshdr(scn);
195 warnx("cannot get section header");
200 shdr->sh_type = SHT_PROGBITS;
201 shdr->sh_addr = base_offset;
202 shdr->sh_flags = SHF_ALLOC;
203 shdr->sh_entsize = 0;
206 * setup eh_frame_hdr section
210 warnx("cannot create section");
214 d = elf_newdata(scn);
216 warnx("cannot get new data");
222 d->d_buf = unwinding + unwinding_table_size;
223 d->d_type = ELF_T_BYTE;
224 d->d_size = unwinding_header_size;
225 d->d_version = EV_CURRENT;
227 shdr = elf_getshdr(scn);
229 warnx("cannot get section header");
234 shdr->sh_type = SHT_PROGBITS;
235 shdr->sh_addr = base_offset + unwinding_table_size;
236 shdr->sh_flags = SHF_ALLOC;
237 shdr->sh_entsize = 0;
243 * fd: file descriptor open for writing for the output file
244 * load_addr: code load address (could be zero, just used for buildid)
245 * sym: function name (for native code - used as the symbol)
246 * code: the native code
247 * csize: the code size in bytes
250 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
251 const void *code, int csize,
252 void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
253 void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
260 uint64_t eh_frame_base_offset;
265 if (elf_version(EV_CURRENT) == EV_NONE) {
266 warnx("ELF initialization failed");
270 e = elf_begin(fd, ELF_C_WRITE, NULL);
272 warnx("elf_begin failed");
279 ehdr = elf_newehdr(e);
281 warnx("cannot get ehdr");
285 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
286 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
287 ehdr->e_machine = GEN_ELF_ARCH;
288 ehdr->e_type = ET_DYN;
289 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
290 ehdr->e_version = EV_CURRENT;
291 ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
298 warnx("cannot create section");
302 d = elf_newdata(scn);
304 warnx("cannot get new data");
310 d->d_buf = (void *)code;
311 d->d_type = ELF_T_BYTE;
313 d->d_version = EV_CURRENT;
315 shdr = elf_getshdr(scn);
317 warnx("cannot get section header");
322 shdr->sh_type = SHT_PROGBITS;
323 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
324 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
325 shdr->sh_entsize = 0;
328 * Setup .eh_frame_hdr and .eh_frame
331 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
332 retval = jit_add_eh_frame_info(e, unwinding,
333 unwinding_header_size, unwinding_size,
334 eh_frame_base_offset);
340 * setup section headers string table
344 warnx("cannot create section");
348 d = elf_newdata(scn);
350 warnx("cannot get new data");
356 d->d_buf = shd_string_table;
357 d->d_type = ELF_T_BYTE;
358 d->d_size = sizeof(shd_string_table);
359 d->d_version = EV_CURRENT;
361 shdr = elf_getshdr(scn);
363 warnx("cannot get section header");
367 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
368 shdr->sh_type = SHT_STRTAB;
370 shdr->sh_entsize = 0;
373 * setup symtab section
375 symtab[1].st_size = csize;
376 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
380 warnx("cannot create section");
384 d = elf_newdata(scn);
386 warnx("cannot get new data");
393 d->d_type = ELF_T_SYM;
394 d->d_size = sizeof(symtab);
395 d->d_version = EV_CURRENT;
397 shdr = elf_getshdr(scn);
399 warnx("cannot get section header");
403 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
404 shdr->sh_type = SHT_SYMTAB;
406 shdr->sh_entsize = sizeof(Elf_Sym);
407 shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
410 * setup symbols string table
411 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
413 symlen = 2 + strlen(sym);
414 strsym = calloc(1, symlen);
416 warnx("cannot allocate strsym");
419 strcpy(strsym + 1, sym);
423 warnx("cannot create section");
427 d = elf_newdata(scn);
429 warnx("cannot get new data");
436 d->d_type = ELF_T_BYTE;
438 d->d_version = EV_CURRENT;
440 shdr = elf_getshdr(scn);
442 warnx("cannot get section header");
446 shdr->sh_name = 25; /* offset in shd_string_table */
447 shdr->sh_type = SHT_STRTAB;
449 shdr->sh_entsize = 0;
452 * setup build-id section
456 warnx("cannot create section");
460 d = elf_newdata(scn);
462 warnx("cannot get new data");
467 * build-id generation
469 gen_build_id(&bnote, load_addr, code, csize);
470 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
471 bnote.desc.descsz = sizeof(bnote.build_id);
472 bnote.desc.type = NT_GNU_BUILD_ID;
473 strcpy(bnote.name, "GNU");
478 d->d_type = ELF_T_BYTE;
479 d->d_size = sizeof(bnote);
480 d->d_version = EV_CURRENT;
482 shdr = elf_getshdr(scn);
484 warnx("cannot get section header");
488 shdr->sh_name = 33; /* offset in shd_string_table */
489 shdr->sh_type = SHT_NOTE;
491 shdr->sh_flags = SHF_ALLOC;
492 shdr->sh_size = sizeof(bnote);
493 shdr->sh_entsize = 0;
495 #ifdef HAVE_DWARF_SUPPORT
496 if (debug && nr_debug_entries) {
497 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
503 if (elf_update(e, ELF_C_WRITE) < 0) {
504 warnx("elf_update 4 failed");
521 static unsigned char x86_code[] = {
522 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
523 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
524 0xCD, 0x80 /* int $0x80 */
527 static struct options options;
529 int main(int argc, char **argv)
533 while ((c = getopt(argc, argv, "o:h")) != -1) {
536 options.output = optarg;
539 printf("Usage: genelf -o output_file [-h]\n");
542 errx(1, "unknown option");
546 fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
548 err(1, "cannot create file %s", options.output);
550 ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
554 unlink(options.output);