1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2014, Google, Inc
7 * Stephane Eranian <eranian@gmail.com>
10 #include <sys/types.h>
21 #ifdef HAVE_DWARF_SUPPORT
27 #include "../util/jitdump.h"
29 #ifndef NT_GNU_BUILD_ID
30 #define NT_GNU_BUILD_ID 3
35 #define BUILD_ID_URANDOM /* different uuid for each run */
40 #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */
41 #undef BUILD_ID_URANDOM /* different uuid for each run */
44 #include <openssl/sha.h>
48 #include <openssl/md5.h>
54 unsigned int namesz; /* Size of entry's owner string */
55 unsigned int descsz; /* Size of the note descriptor */
56 unsigned int type; /* Interpretation of the descriptor */
57 char name[0]; /* Start of the name+desc data */
65 static char shd_string_table[] = {
67 '.', 't', 'e', 'x', 't', 0, /* 1 */
68 '.', 's', 'h', 's', 't', 'r', 't', 'a', 'b', 0, /* 7 */
69 '.', 's', 'y', 'm', 't', 'a', 'b', 0, /* 17 */
70 '.', 's', 't', 'r', 't', 'a', 'b', 0, /* 25 */
71 '.', 'n', 'o', 't', 'e', '.', 'g', 'n', 'u', '.', 'b', 'u', 'i', 'l', 'd', '-', 'i', 'd', 0, /* 33 */
72 '.', 'd', 'e', 'b', 'u', 'g', '_', 'l', 'i', 'n', 'e', 0, /* 52 */
73 '.', 'd', 'e', 'b', 'u', 'g', '_', 'i', 'n', 'f', 'o', 0, /* 64 */
74 '.', 'd', 'e', 'b', 'u', 'g', '_', 'a', 'b', 'b', 'r', 'e', 'v', 0, /* 76 */
75 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 90 */
76 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
79 static struct buildid_note {
80 Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */
81 char name[4]; /* GNU\0 */
85 static Elf_Sym symtab[]={
86 /* symbol 0 MUST be the undefined symbol */
87 { .st_name = 0, /* index in sym_string table */
88 .st_info = ELF_ST_TYPE(STT_NOTYPE),
89 .st_shndx = 0, /* for now */
91 .st_other = ELF_ST_VIS(STV_DEFAULT),
94 { .st_name = 1, /* index in sym_string table */
95 .st_info = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
97 .st_value = 0, /* for now */
98 .st_other = ELF_ST_VIS(STV_DEFAULT),
99 .st_size = 0, /* for now */
103 #ifdef BUILD_ID_URANDOM
105 gen_build_id(struct buildid_note *note,
106 unsigned long load_addr __maybe_unused,
107 const void *code __maybe_unused,
108 size_t csize __maybe_unused)
111 size_t sz = sizeof(note->build_id);
114 fd = open("/dev/urandom", O_RDONLY);
116 err(1, "cannot access /dev/urandom for buildid");
118 sret = read(fd, note->build_id, sz);
122 if (sret != (ssize_t)sz)
123 memset(note->build_id, 0, sz);
129 gen_build_id(struct buildid_note *note,
130 unsigned long load_addr __maybe_unused,
134 if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
135 errx(1, "build_id too small for SHA1");
137 SHA1(code, csize, (unsigned char *)note->build_id);
143 gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
147 if (sizeof(note->build_id) < 16)
148 errx(1, "build_id too small for MD5");
151 MD5_Update(&context, &load_addr, sizeof(load_addr));
152 MD5_Update(&context, code, csize);
153 MD5_Final((unsigned char *)note->build_id, &context);
158 jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
159 uint64_t unwinding_size, uint64_t base_offset)
164 uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
167 * setup eh_frame section
171 warnx("cannot create section");
175 d = elf_newdata(scn);
177 warnx("cannot get new data");
183 d->d_buf = unwinding;
184 d->d_type = ELF_T_BYTE;
185 d->d_size = unwinding_table_size;
186 d->d_version = EV_CURRENT;
188 shdr = elf_getshdr(scn);
190 warnx("cannot get section header");
195 shdr->sh_type = SHT_PROGBITS;
196 shdr->sh_addr = base_offset;
197 shdr->sh_flags = SHF_ALLOC;
198 shdr->sh_entsize = 0;
201 * setup eh_frame_hdr section
205 warnx("cannot create section");
209 d = elf_newdata(scn);
211 warnx("cannot get new data");
217 d->d_buf = unwinding + unwinding_table_size;
218 d->d_type = ELF_T_BYTE;
219 d->d_size = unwinding_header_size;
220 d->d_version = EV_CURRENT;
222 shdr = elf_getshdr(scn);
224 warnx("cannot get section header");
229 shdr->sh_type = SHT_PROGBITS;
230 shdr->sh_addr = base_offset + unwinding_table_size;
231 shdr->sh_flags = SHF_ALLOC;
232 shdr->sh_entsize = 0;
238 * fd: file descriptor open for writing for the output file
239 * load_addr: code load address (could be zero, just used for buildid)
240 * sym: function name (for native code - used as the symbol)
241 * code: the native code
242 * csize: the code size in bytes
245 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
246 const void *code, int csize,
247 void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
248 void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
255 uint64_t eh_frame_base_offset;
260 if (elf_version(EV_CURRENT) == EV_NONE) {
261 warnx("ELF initialization failed");
265 e = elf_begin(fd, ELF_C_WRITE, NULL);
267 warnx("elf_begin failed");
274 ehdr = elf_newehdr(e);
276 warnx("cannot get ehdr");
280 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
281 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
282 ehdr->e_machine = GEN_ELF_ARCH;
283 ehdr->e_type = ET_DYN;
284 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
285 ehdr->e_version = EV_CURRENT;
286 ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
293 warnx("cannot create section");
297 d = elf_newdata(scn);
299 warnx("cannot get new data");
305 d->d_buf = (void *)code;
306 d->d_type = ELF_T_BYTE;
308 d->d_version = EV_CURRENT;
310 shdr = elf_getshdr(scn);
312 warnx("cannot get section header");
317 shdr->sh_type = SHT_PROGBITS;
318 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
319 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
320 shdr->sh_entsize = 0;
323 * Setup .eh_frame_hdr and .eh_frame
326 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
327 retval = jit_add_eh_frame_info(e, unwinding,
328 unwinding_header_size, unwinding_size,
329 eh_frame_base_offset);
335 * setup section headers string table
339 warnx("cannot create section");
343 d = elf_newdata(scn);
345 warnx("cannot get new data");
351 d->d_buf = shd_string_table;
352 d->d_type = ELF_T_BYTE;
353 d->d_size = sizeof(shd_string_table);
354 d->d_version = EV_CURRENT;
356 shdr = elf_getshdr(scn);
358 warnx("cannot get section header");
362 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
363 shdr->sh_type = SHT_STRTAB;
365 shdr->sh_entsize = 0;
368 * setup symtab section
370 symtab[1].st_size = csize;
371 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
375 warnx("cannot create section");
379 d = elf_newdata(scn);
381 warnx("cannot get new data");
388 d->d_type = ELF_T_SYM;
389 d->d_size = sizeof(symtab);
390 d->d_version = EV_CURRENT;
392 shdr = elf_getshdr(scn);
394 warnx("cannot get section header");
398 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
399 shdr->sh_type = SHT_SYMTAB;
401 shdr->sh_entsize = sizeof(Elf_Sym);
402 shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
405 * setup symbols string table
406 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
408 symlen = 2 + strlen(sym);
409 strsym = calloc(1, symlen);
411 warnx("cannot allocate strsym");
414 strcpy(strsym + 1, sym);
418 warnx("cannot create section");
422 d = elf_newdata(scn);
424 warnx("cannot get new data");
431 d->d_type = ELF_T_BYTE;
433 d->d_version = EV_CURRENT;
435 shdr = elf_getshdr(scn);
437 warnx("cannot get section header");
441 shdr->sh_name = 25; /* offset in shd_string_table */
442 shdr->sh_type = SHT_STRTAB;
444 shdr->sh_entsize = 0;
447 * setup build-id section
451 warnx("cannot create section");
455 d = elf_newdata(scn);
457 warnx("cannot get new data");
462 * build-id generation
464 gen_build_id(&bnote, load_addr, code, csize);
465 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
466 bnote.desc.descsz = sizeof(bnote.build_id);
467 bnote.desc.type = NT_GNU_BUILD_ID;
468 strcpy(bnote.name, "GNU");
473 d->d_type = ELF_T_BYTE;
474 d->d_size = sizeof(bnote);
475 d->d_version = EV_CURRENT;
477 shdr = elf_getshdr(scn);
479 warnx("cannot get section header");
483 shdr->sh_name = 33; /* offset in shd_string_table */
484 shdr->sh_type = SHT_NOTE;
486 shdr->sh_flags = SHF_ALLOC;
487 shdr->sh_size = sizeof(bnote);
488 shdr->sh_entsize = 0;
490 #ifdef HAVE_DWARF_SUPPORT
491 if (debug && nr_debug_entries) {
492 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
498 if (elf_update(e, ELF_C_WRITE) < 0) {
499 warnx("elf_update 4 failed");
516 static unsigned char x86_code[] = {
517 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
518 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
519 0xCD, 0x80 /* int $0x80 */
522 static struct options options;
524 int main(int argc, char **argv)
528 while ((c = getopt(argc, argv, "o:h")) != -1) {
531 options.output = optarg;
534 printf("Usage: genelf -o output_file [-h]\n");
537 errx(1, "unknown option");
541 fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
543 err(1, "cannot create file %s", options.output);
545 ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
549 unlink(options.output);