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 buildid");
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)
261 uint64_t eh_frame_base_offset;
266 if (elf_version(EV_CURRENT) == EV_NONE) {
267 warnx("ELF initialization failed");
271 e = elf_begin(fd, ELF_C_WRITE, NULL);
273 warnx("elf_begin failed");
280 ehdr = elf_newehdr(e);
282 warnx("cannot get ehdr");
286 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
287 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
288 ehdr->e_machine = GEN_ELF_ARCH;
289 ehdr->e_type = ET_DYN;
290 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
291 ehdr->e_version = EV_CURRENT;
292 ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
295 * setup program header
297 phdr = elf_newphdr(e, 1);
298 phdr[0].p_type = PT_LOAD;
299 phdr[0].p_offset = 0;
302 phdr[0].p_filesz = csize;
303 phdr[0].p_memsz = csize;
304 phdr[0].p_flags = PF_X | PF_R;
312 warnx("cannot create section");
316 d = elf_newdata(scn);
318 warnx("cannot get new data");
324 d->d_buf = (void *)code;
325 d->d_type = ELF_T_BYTE;
327 d->d_version = EV_CURRENT;
329 shdr = elf_getshdr(scn);
331 warnx("cannot get section header");
336 shdr->sh_type = SHT_PROGBITS;
337 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
338 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
339 shdr->sh_entsize = 0;
342 * Setup .eh_frame_hdr and .eh_frame
345 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
346 retval = jit_add_eh_frame_info(e, unwinding,
347 unwinding_header_size, unwinding_size,
348 eh_frame_base_offset);
354 * setup section headers string table
358 warnx("cannot create section");
362 d = elf_newdata(scn);
364 warnx("cannot get new data");
370 d->d_buf = shd_string_table;
371 d->d_type = ELF_T_BYTE;
372 d->d_size = sizeof(shd_string_table);
373 d->d_version = EV_CURRENT;
375 shdr = elf_getshdr(scn);
377 warnx("cannot get section header");
381 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
382 shdr->sh_type = SHT_STRTAB;
384 shdr->sh_entsize = 0;
387 * setup symtab section
389 symtab[1].st_size = csize;
390 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
394 warnx("cannot create section");
398 d = elf_newdata(scn);
400 warnx("cannot get new data");
407 d->d_type = ELF_T_SYM;
408 d->d_size = sizeof(symtab);
409 d->d_version = EV_CURRENT;
411 shdr = elf_getshdr(scn);
413 warnx("cannot get section header");
417 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
418 shdr->sh_type = SHT_SYMTAB;
420 shdr->sh_entsize = sizeof(Elf_Sym);
421 shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
424 * setup symbols string table
425 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
427 symlen = 2 + strlen(sym);
428 strsym = calloc(1, symlen);
430 warnx("cannot allocate strsym");
433 strcpy(strsym + 1, sym);
437 warnx("cannot create section");
441 d = elf_newdata(scn);
443 warnx("cannot get new data");
450 d->d_type = ELF_T_BYTE;
452 d->d_version = EV_CURRENT;
454 shdr = elf_getshdr(scn);
456 warnx("cannot get section header");
460 shdr->sh_name = 25; /* offset in shd_string_table */
461 shdr->sh_type = SHT_STRTAB;
463 shdr->sh_entsize = 0;
466 * setup build-id section
470 warnx("cannot create section");
474 d = elf_newdata(scn);
476 warnx("cannot get new data");
481 * build-id generation
483 gen_build_id(&bnote, load_addr, code, csize);
484 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
485 bnote.desc.descsz = sizeof(bnote.build_id);
486 bnote.desc.type = NT_GNU_BUILD_ID;
487 strcpy(bnote.name, "GNU");
492 d->d_type = ELF_T_BYTE;
493 d->d_size = sizeof(bnote);
494 d->d_version = EV_CURRENT;
496 shdr = elf_getshdr(scn);
498 warnx("cannot get section header");
502 shdr->sh_name = 33; /* offset in shd_string_table */
503 shdr->sh_type = SHT_NOTE;
505 shdr->sh_flags = SHF_ALLOC;
506 shdr->sh_size = sizeof(bnote);
507 shdr->sh_entsize = 0;
509 #ifdef HAVE_DWARF_SUPPORT
510 if (debug && nr_debug_entries) {
511 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
517 if (elf_update(e, ELF_C_WRITE) < 0) {
518 warnx("elf_update 4 failed");
535 static unsigned char x86_code[] = {
536 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
537 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
538 0xCD, 0x80 /* int $0x80 */
541 static struct options options;
543 int main(int argc, char **argv)
547 while ((c = getopt(argc, argv, "o:h")) != -1) {
550 options.output = optarg;
553 printf("Usage: genelf -o output_file [-h]\n");
556 errx(1, "unknown option");
560 fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
562 err(1, "cannot create file %s", options.output);
564 ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
568 unlink(options.output);