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"
32 #define BUILD_ID_URANDOM /* different uuid for each run */
37 #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */
38 #undef BUILD_ID_URANDOM /* different uuid for each run */
41 #include <openssl/sha.h>
45 #include <openssl/md5.h>
51 unsigned int namesz; /* Size of entry's owner string */
52 unsigned int descsz; /* Size of the note descriptor */
53 unsigned int type; /* Interpretation of the descriptor */
54 char name[0]; /* Start of the name+desc data */
62 static char shd_string_table[] = {
64 '.', 't', 'e', 'x', 't', 0, /* 1 */
65 '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0, /* 7 */
66 '.', 's', 'y', 'm', 't', 'a', 'b', 0, /* 17 */
67 '.', 's', 't', 'r', 't', 'a', 'b', 0, /* 25 */
68 '.', 'n', 'o', 't', 'e', '.', 'g', 'n', 'u', '.', 'b', 'u', 'i', 'l', 'd', '-', 'i', 'd', 0, /* 33 */
69 '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
70 '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
71 '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
74 static struct buildid_note {
75 Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */
76 char name[4]; /* GNU\0 */
80 static Elf_Sym symtab[]={
81 /* symbol 0 MUST be the undefined symbol */
82 { .st_name = 0, /* index in sym_string table */
83 .st_info = ELF_ST_TYPE(STT_NOTYPE),
84 .st_shndx = 0, /* for now */
86 .st_other = ELF_ST_VIS(STV_DEFAULT),
89 { .st_name = 1, /* index in sym_string table */
90 .st_info = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
92 .st_value = 0, /* for now */
93 .st_other = ELF_ST_VIS(STV_DEFAULT),
94 .st_size = 0, /* for now */
98 #ifdef BUILD_ID_URANDOM
100 gen_build_id(struct buildid_note *note,
101 unsigned long load_addr __maybe_unused,
102 const void *code __maybe_unused,
103 size_t csize __maybe_unused)
106 size_t sz = sizeof(note->build_id);
109 fd = open("/dev/urandom", O_RDONLY);
111 err(1, "cannot access /dev/urandom for builid");
113 sret = read(fd, note->build_id, sz);
117 if (sret != (ssize_t)sz)
118 memset(note->build_id, 0, sz);
124 gen_build_id(struct buildid_note *note,
125 unsigned long load_addr __maybe_unused,
129 if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
130 errx(1, "build_id too small for SHA1");
132 SHA1(code, csize, (unsigned char *)note->build_id);
138 gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
142 if (sizeof(note->build_id) < 16)
143 errx(1, "build_id too small for MD5");
146 MD5_Update(&context, &load_addr, sizeof(load_addr));
147 MD5_Update(&context, code, csize);
148 MD5_Final((unsigned char *)note->build_id, &context);
153 * fd: file descriptor open for writing for the output file
154 * load_addr: code load address (could be zero, just used for buildid)
155 * sym: function name (for native code - used as the symbol)
156 * code: the native code
157 * csize: the code size in bytes
160 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
161 const void *code, int csize,
162 void *debug __maybe_unused, int nr_debug_entries __maybe_unused)
173 if (elf_version(EV_CURRENT) == EV_NONE) {
174 warnx("ELF initialization failed");
178 e = elf_begin(fd, ELF_C_WRITE, NULL);
180 warnx("elf_begin failed");
187 ehdr = elf_newehdr(e);
189 warnx("cannot get ehdr");
193 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
194 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
195 ehdr->e_machine = GEN_ELF_ARCH;
196 ehdr->e_type = ET_DYN;
197 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
198 ehdr->e_version = EV_CURRENT;
199 ehdr->e_shstrndx= 2; /* shdr index for section name */
206 warnx("cannot create section");
210 d = elf_newdata(scn);
212 warnx("cannot get new data");
218 d->d_buf = (void *)code;
219 d->d_type = ELF_T_BYTE;
221 d->d_version = EV_CURRENT;
223 shdr = elf_getshdr(scn);
225 warnx("cannot get section header");
230 shdr->sh_type = SHT_PROGBITS;
231 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
232 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
233 shdr->sh_entsize = 0;
236 * setup section headers string table
240 warnx("cannot create section");
244 d = elf_newdata(scn);
246 warnx("cannot get new data");
252 d->d_buf = shd_string_table;
253 d->d_type = ELF_T_BYTE;
254 d->d_size = sizeof(shd_string_table);
255 d->d_version = EV_CURRENT;
257 shdr = elf_getshdr(scn);
259 warnx("cannot get section header");
263 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
264 shdr->sh_type = SHT_STRTAB;
266 shdr->sh_entsize = 0;
269 * setup symtab section
271 symtab[1].st_size = csize;
272 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
276 warnx("cannot create section");
280 d = elf_newdata(scn);
282 warnx("cannot get new data");
289 d->d_type = ELF_T_SYM;
290 d->d_size = sizeof(symtab);
291 d->d_version = EV_CURRENT;
293 shdr = elf_getshdr(scn);
295 warnx("cannot get section header");
299 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
300 shdr->sh_type = SHT_SYMTAB;
302 shdr->sh_entsize = sizeof(Elf_Sym);
303 shdr->sh_link = 4; /* index of .strtab section */
306 * setup symbols string table
307 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
309 symlen = 2 + strlen(sym);
310 strsym = calloc(1, symlen);
312 warnx("cannot allocate strsym");
315 strcpy(strsym + 1, sym);
319 warnx("cannot create section");
323 d = elf_newdata(scn);
325 warnx("cannot get new data");
332 d->d_type = ELF_T_BYTE;
334 d->d_version = EV_CURRENT;
336 shdr = elf_getshdr(scn);
338 warnx("cannot get section header");
342 shdr->sh_name = 25; /* offset in shd_string_table */
343 shdr->sh_type = SHT_STRTAB;
345 shdr->sh_entsize = 0;
348 * setup build-id section
352 warnx("cannot create section");
356 d = elf_newdata(scn);
358 warnx("cannot get new data");
363 * build-id generation
365 gen_build_id(&bnote, load_addr, code, csize);
366 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
367 bnote.desc.descsz = sizeof(bnote.build_id);
368 bnote.desc.type = NT_GNU_BUILD_ID;
369 strcpy(bnote.name, "GNU");
374 d->d_type = ELF_T_BYTE;
375 d->d_size = sizeof(bnote);
376 d->d_version = EV_CURRENT;
378 shdr = elf_getshdr(scn);
380 warnx("cannot get section header");
384 shdr->sh_name = 33; /* offset in shd_string_table */
385 shdr->sh_type = SHT_NOTE;
387 shdr->sh_flags = SHF_ALLOC;
388 shdr->sh_size = sizeof(bnote);
389 shdr->sh_entsize = 0;
391 #ifdef HAVE_DWARF_SUPPORT
392 if (debug && nr_debug_entries) {
393 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
399 if (elf_update(e, ELF_C_WRITE) < 0) {
400 warnx("elf_update 4 failed");
417 static unsigned char x86_code[] = {
418 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
419 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
420 0xCD, 0x80 /* int $0x80 */
423 static struct options options;
425 int main(int argc, char **argv)
429 while ((c = getopt(argc, argv, "o:h")) != -1) {
432 options.output = optarg;
435 printf("Usage: genelf -o output_file [-h]\n");
438 errx(1, "unknown option");
442 fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
444 err(1, "cannot create file %s", options.output);
446 ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
450 unlink(options.output);