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>
26 #include "../util/jitdump.h"
30 #define BUILD_ID_URANDOM /* different uuid for each run */
35 #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */
36 #undef BUILD_ID_URANDOM /* different uuid for each run */
39 #include <openssl/sha.h>
43 #include <openssl/md5.h>
49 unsigned int namesz; /* Size of entry's owner string */
50 unsigned int descsz; /* Size of the note descriptor */
51 unsigned int type; /* Interpretation of the descriptor */
52 char name[0]; /* Start of the name+desc data */
60 static char shd_string_table[] = {
62 '.', 't', 'e', 'x', 't', 0, /* 1 */
63 '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0, /* 7 */
64 '.', 's', 'y', 'm', 't', 'a', 'b', 0, /* 17 */
65 '.', 's', 't', 'r', 't', 'a', 'b', 0, /* 25 */
66 '.', 'n', 'o', 't', 'e', '.', 'g', 'n', 'u', '.', 'b', 'u', 'i', 'l', 'd', '-', 'i', 'd', 0, /* 33 */
67 '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
68 '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
69 '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
72 static struct buildid_note {
73 Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */
74 char name[4]; /* GNU\0 */
78 static Elf_Sym symtab[]={
79 /* symbol 0 MUST be the undefined symbol */
80 { .st_name = 0, /* index in sym_string table */
81 .st_info = ELF_ST_TYPE(STT_NOTYPE),
82 .st_shndx = 0, /* for now */
84 .st_other = ELF_ST_VIS(STV_DEFAULT),
87 { .st_name = 1, /* index in sym_string table */
88 .st_info = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
90 .st_value = 0, /* for now */
91 .st_other = ELF_ST_VIS(STV_DEFAULT),
92 .st_size = 0, /* for now */
96 #ifdef BUILD_ID_URANDOM
98 gen_build_id(struct buildid_note *note,
99 unsigned long load_addr __maybe_unused,
100 const void *code __maybe_unused,
101 size_t csize __maybe_unused)
104 size_t sz = sizeof(note->build_id);
107 fd = open("/dev/urandom", O_RDONLY);
109 err(1, "cannot access /dev/urandom for builid");
111 sret = read(fd, note->build_id, sz);
115 if (sret != (ssize_t)sz)
116 memset(note->build_id, 0, sz);
122 gen_build_id(struct buildid_note *note,
123 unsigned long load_addr __maybe_unused,
127 if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
128 errx(1, "build_id too small for SHA1");
130 SHA1(code, csize, (unsigned char *)note->build_id);
136 gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
140 if (sizeof(note->build_id) < 16)
141 errx(1, "build_id too small for MD5");
144 MD5_Update(&context, &load_addr, sizeof(load_addr));
145 MD5_Update(&context, code, csize);
146 MD5_Final((unsigned char *)note->build_id, &context);
151 * fd: file descriptor open for writing for the output file
152 * load_addr: code load address (could be zero, just used for buildid)
153 * sym: function name (for native code - used as the symbol)
154 * code: the native code
155 * csize: the code size in bytes
158 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
159 const void *code, int csize,
160 void *debug, int nr_debug_entries)
171 if (elf_version(EV_CURRENT) == EV_NONE) {
172 warnx("ELF initialization failed");
176 e = elf_begin(fd, ELF_C_WRITE, NULL);
178 warnx("elf_begin failed");
185 ehdr = elf_newehdr(e);
187 warnx("cannot get ehdr");
191 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
192 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
193 ehdr->e_machine = GEN_ELF_ARCH;
194 ehdr->e_type = ET_DYN;
195 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
196 ehdr->e_version = EV_CURRENT;
197 ehdr->e_shstrndx= 2; /* shdr index for section name */
204 warnx("cannot create section");
208 d = elf_newdata(scn);
210 warnx("cannot get new data");
216 d->d_buf = (void *)code;
217 d->d_type = ELF_T_BYTE;
219 d->d_version = EV_CURRENT;
221 shdr = elf_getshdr(scn);
223 warnx("cannot get section header");
228 shdr->sh_type = SHT_PROGBITS;
229 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
230 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
231 shdr->sh_entsize = 0;
234 * setup section headers string table
238 warnx("cannot create section");
242 d = elf_newdata(scn);
244 warnx("cannot get new data");
250 d->d_buf = shd_string_table;
251 d->d_type = ELF_T_BYTE;
252 d->d_size = sizeof(shd_string_table);
253 d->d_version = EV_CURRENT;
255 shdr = elf_getshdr(scn);
257 warnx("cannot get section header");
261 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
262 shdr->sh_type = SHT_STRTAB;
264 shdr->sh_entsize = 0;
267 * setup symtab section
269 symtab[1].st_size = csize;
270 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
274 warnx("cannot create section");
278 d = elf_newdata(scn);
280 warnx("cannot get new data");
287 d->d_type = ELF_T_SYM;
288 d->d_size = sizeof(symtab);
289 d->d_version = EV_CURRENT;
291 shdr = elf_getshdr(scn);
293 warnx("cannot get section header");
297 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
298 shdr->sh_type = SHT_SYMTAB;
300 shdr->sh_entsize = sizeof(Elf_Sym);
301 shdr->sh_link = 4; /* index of .strtab section */
304 * setup symbols string table
305 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
307 symlen = 2 + strlen(sym);
308 strsym = calloc(1, symlen);
310 warnx("cannot allocate strsym");
313 strcpy(strsym + 1, sym);
317 warnx("cannot create section");
321 d = elf_newdata(scn);
323 warnx("cannot get new data");
330 d->d_type = ELF_T_BYTE;
332 d->d_version = EV_CURRENT;
334 shdr = elf_getshdr(scn);
336 warnx("cannot get section header");
340 shdr->sh_name = 25; /* offset in shd_string_table */
341 shdr->sh_type = SHT_STRTAB;
343 shdr->sh_entsize = 0;
346 * setup build-id section
350 warnx("cannot create section");
354 d = elf_newdata(scn);
356 warnx("cannot get new data");
361 * build-id generation
363 gen_build_id(&bnote, load_addr, code, csize);
364 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
365 bnote.desc.descsz = sizeof(bnote.build_id);
366 bnote.desc.type = NT_GNU_BUILD_ID;
367 strcpy(bnote.name, "GNU");
372 d->d_type = ELF_T_BYTE;
373 d->d_size = sizeof(bnote);
374 d->d_version = EV_CURRENT;
376 shdr = elf_getshdr(scn);
378 warnx("cannot get section header");
382 shdr->sh_name = 33; /* offset in shd_string_table */
383 shdr->sh_type = SHT_NOTE;
385 shdr->sh_flags = SHF_ALLOC;
386 shdr->sh_size = sizeof(bnote);
387 shdr->sh_entsize = 0;
389 if (debug && nr_debug_entries) {
390 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
394 if (elf_update(e, ELF_C_WRITE) < 0) {
395 warnx("elf_update 4 failed");
412 static unsigned char x86_code[] = {
413 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
414 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
415 0xCD, 0x80 /* int $0x80 */
418 static struct options options;
420 int main(int argc, char **argv)
424 while ((c = getopt(argc, argv, "o:h")) != -1) {
427 options.output = optarg;
430 printf("Usage: genelf -o output_file [-h]\n");
433 errx(1, "unknown option");
437 fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
439 err(1, "cannot create file %s", options.output);
441 ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
445 unlink(options.output);