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 42 #undef BUILD_ID_URANDOM 45 #include <openssl/sha.h> 49 #include <openssl/md5.h> 68 '.',
't',
'e',
'x',
't', 0,
69 '.',
's',
'h',
's',
't',
'r',
't',
'a',
'b', 0,
70 '.',
's',
'y',
'm',
't',
'a',
'b', 0,
71 '.',
's',
't',
'r',
't',
'a',
'b', 0,
72 '.',
'n',
'o',
't',
'e',
'.',
'g',
'n',
'u',
'.',
'b',
'u',
'i',
'l',
'd',
'-',
'i',
'd', 0,
73 '.',
'd',
'e',
'b',
'u',
'g',
'_',
'l',
'i',
'n',
'e', 0,
74 '.',
'd',
'e',
'b',
'u',
'g',
'_',
'i',
'n',
'f',
'o', 0,
75 '.',
'd',
'e',
'b',
'u',
'g',
'_',
'a',
'b',
'b',
'r',
'e',
'v', 0,
76 '.',
'e',
'h',
'_',
'f',
'r',
'a',
'm',
'e',
'_',
'h',
'd',
'r', 0,
77 '.',
'e',
'h',
'_',
'f',
'r',
'a',
'm',
'e', 0,
104 #ifdef BUILD_ID_URANDOM 107 unsigned long load_addr __maybe_unused,
108 const void *code __maybe_unused,
109 size_t csize __maybe_unused)
115 fd = open(
"/dev/urandom", O_RDONLY);
117 err(1,
"cannot access /dev/urandom for buildid");
119 sret = read(fd, note->
build_id, sz);
123 if (sret != (ssize_t)sz)
131 unsigned long load_addr __maybe_unused,
135 if (
sizeof(note->
build_id) < SHA_DIGEST_LENGTH)
136 errx(1,
"build_id too small for SHA1");
138 SHA1(code, csize, (
unsigned char *)note->
build_id);
149 errx(1,
"build_id too small for MD5");
152 MD5_Update(&context, &load_addr,
sizeof(load_addr));
153 MD5_Update(&context, code, csize);
154 MD5_Final((
unsigned char *)note->
build_id, &context);
160 uint64_t unwinding_size, uint64_t base_offset)
165 uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
172 warnx(
"cannot create section");
176 d = elf_newdata(scn);
178 warnx(
"cannot get new data");
184 d->d_buf = unwinding;
185 d->d_type = ELF_T_BYTE;
186 d->d_size = unwinding_table_size;
187 d->d_version = EV_CURRENT;
191 warnx(
"cannot get section header");
196 shdr->sh_type = SHT_PROGBITS;
197 shdr->sh_addr = base_offset;
198 shdr->sh_flags = SHF_ALLOC;
199 shdr->sh_entsize = 0;
206 warnx(
"cannot create section");
210 d = elf_newdata(scn);
212 warnx(
"cannot get new data");
218 d->d_buf = unwinding + unwinding_table_size;
219 d->d_type = ELF_T_BYTE;
220 d->d_size = unwinding_header_size;
221 d->d_version = EV_CURRENT;
225 warnx(
"cannot get section header");
230 shdr->sh_type = SHT_PROGBITS;
231 shdr->sh_addr = base_offset + unwinding_table_size;
232 shdr->sh_flags = SHF_ALLOC;
233 shdr->sh_entsize = 0;
247 const void *code,
int csize,
248 void *
debug __maybe_unused,
int nr_debug_entries __maybe_unused,
249 void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
256 uint64_t eh_frame_base_offset;
261 if (elf_version(EV_CURRENT) == EV_NONE) {
262 warnx(
"ELF initialization failed");
266 e = elf_begin(fd, ELF_C_WRITE, NULL);
268 warnx(
"elf_begin failed");
277 warnx(
"cannot get ehdr");
282 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
283 ehdr->e_machine = GEN_ELF_ARCH;
284 ehdr->e_type = ET_DYN;
286 ehdr->e_version = EV_CURRENT;
287 ehdr->e_shstrndx= unwinding ? 4 : 2;
294 warnx(
"cannot create section");
298 d = elf_newdata(scn);
300 warnx(
"cannot get new data");
306 d->d_buf = (
void *)code;
307 d->d_type = ELF_T_BYTE;
309 d->d_version = EV_CURRENT;
313 warnx(
"cannot get section header");
318 shdr->sh_type = SHT_PROGBITS;
320 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
321 shdr->sh_entsize = 0;
329 unwinding_header_size, unwinding_size,
330 eh_frame_base_offset);
340 warnx(
"cannot create section");
344 d = elf_newdata(scn);
346 warnx(
"cannot get new data");
353 d->d_type = ELF_T_BYTE;
355 d->d_version = EV_CURRENT;
359 warnx(
"cannot get section header");
364 shdr->sh_type = SHT_STRTAB;
366 shdr->sh_entsize = 0;
371 symtab[1].st_size = csize;
376 warnx(
"cannot create section");
380 d = elf_newdata(scn);
382 warnx(
"cannot get new data");
389 d->d_type = ELF_T_SYM;
390 d->d_size =
sizeof(
symtab);
391 d->d_version = EV_CURRENT;
395 warnx(
"cannot get section header");
400 shdr->sh_type = SHT_SYMTAB;
402 shdr->sh_entsize =
sizeof(
Elf_Sym);
403 shdr->sh_link = unwinding ? 6 : 4;
409 symlen = 2 + strlen(sym);
410 strsym = calloc(1, symlen);
412 warnx(
"cannot allocate strsym");
415 strcpy(strsym + 1, sym);
419 warnx(
"cannot create section");
423 d = elf_newdata(scn);
425 warnx(
"cannot get new data");
432 d->d_type = ELF_T_BYTE;
434 d->d_version = EV_CURRENT;
438 warnx(
"cannot get section header");
443 shdr->sh_type = SHT_STRTAB;
445 shdr->sh_entsize = 0;
452 warnx(
"cannot create section");
456 d = elf_newdata(scn);
458 warnx(
"cannot get new data");
474 d->d_type = ELF_T_BYTE;
475 d->d_size =
sizeof(
bnote);
476 d->d_version = EV_CURRENT;
480 warnx(
"cannot get section header");
485 shdr->sh_type = SHT_NOTE;
487 shdr->sh_flags = SHF_ALLOC;
488 shdr->sh_size =
sizeof(
bnote);
489 shdr->sh_entsize = 0;
491 #ifdef HAVE_DWARF_SUPPORT 492 if (
debug && nr_debug_entries) {
499 if (elf_update(e, ELF_C_WRITE) < 0) {
500 warnx(
"elf_update 4 failed");
517 static unsigned char x86_code[] = {
518 0xBB, 0x2A, 0x00, 0x00, 0x00,
519 0xB8, 0x01, 0x00, 0x00, 0x00,
529 while ((c = getopt(
argc, argv,
"o:h")) != -1) {
535 printf(
"Usage: genelf -o output_file [-h]\n");
538 errx(1,
"unknown option");
542 fd = open(options.
output, O_CREAT|O_TRUNC|O_RDWR, 0666);
544 err(1,
"cannot create file %s", options.
output);
#define GEN_ELF_TEXT_OFFSET
x86 movsq based memset() in arch/x86/lib/memset_64.S") MEMSET_FN(memset_erms
static void gen_build_id(struct buildid_note *note, unsigned long load_addr __maybe_unused, const void *code __maybe_unused, size_t csize __maybe_unused)
static char shd_string_table[]
int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries)
int jit_write_elf(int fd, uint64_t load_addr, const char *sym, const void *code, int csize, void *debug __maybe_unused, int nr_debug_entries __maybe_unused, void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
static struct buildid_note bnote
static int sym(yyscan_t scanner, int type, int config)
static int jit_add_eh_frame_info(Elf *e, void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size, uint64_t base_offset)