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>
19 #ifdef HAVE_DWARF_SUPPORT
24 #include "../util/jitdump.h"
25 #include <linux/compiler.h>
27 #ifndef NT_GNU_BUILD_ID
28 #define NT_GNU_BUILD_ID 3
31 #define BUILD_ID_URANDOM /* different uuid for each run */
33 #ifdef HAVE_LIBCRYPTO_SUPPORT
36 #undef BUILD_ID_SHA /* does not seem to work well when linked with Java */
37 #undef BUILD_ID_URANDOM /* different uuid for each run */
40 #include <openssl/sha.h>
44 #include <openssl/evp.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 */
72 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', '_', 'h', 'd', 'r', 0, /* 90 */
73 '.', 'e', 'h', '_', 'f', 'r', 'a', 'm', 'e', 0, /* 104 */
76 static struct buildid_note {
77 Elf_Note desc; /* descsz: size of build-id, must be multiple of 4 */
78 char name[4]; /* GNU\0 */
82 static Elf_Sym symtab[]={
83 /* symbol 0 MUST be the undefined symbol */
84 { .st_name = 0, /* index in sym_string table */
85 .st_info = ELF_ST_TYPE(STT_NOTYPE),
86 .st_shndx = 0, /* for now */
88 .st_other = ELF_ST_VIS(STV_DEFAULT),
91 { .st_name = 1, /* index in sym_string table */
92 .st_info = ELF_ST_BIND(STB_LOCAL) | ELF_ST_TYPE(STT_FUNC),
94 .st_value = 0, /* for now */
95 .st_other = ELF_ST_VIS(STV_DEFAULT),
96 .st_size = 0, /* for now */
100 #ifdef BUILD_ID_URANDOM
102 gen_build_id(struct buildid_note *note,
103 unsigned long load_addr __maybe_unused,
104 const void *code __maybe_unused,
105 size_t csize __maybe_unused)
108 size_t sz = sizeof(note->build_id);
111 fd = open("/dev/urandom", O_RDONLY);
113 err(1, "cannot access /dev/urandom for buildid");
115 sret = read(fd, note->build_id, sz);
119 if (sret != (ssize_t)sz)
120 memset(note->build_id, 0, sz);
126 gen_build_id(struct buildid_note *note,
127 unsigned long load_addr __maybe_unused,
131 if (sizeof(note->build_id) < SHA_DIGEST_LENGTH)
132 errx(1, "build_id too small for SHA1");
134 SHA1(code, csize, (unsigned char *)note->build_id);
140 gen_build_id(struct buildid_note *note, unsigned long load_addr, const void *code, size_t csize)
144 if (sizeof(note->build_id) < 16)
145 errx(1, "build_id too small for MD5");
147 mdctx = EVP_MD_CTX_new();
149 errx(2, "failed to create EVP_MD_CTX");
151 EVP_DigestInit_ex(mdctx, EVP_md5(), NULL);
152 EVP_DigestUpdate(mdctx, &load_addr, sizeof(load_addr));
153 EVP_DigestUpdate(mdctx, code, csize);
154 EVP_DigestFinal_ex(mdctx, (unsigned char *)note->build_id, NULL);
155 EVP_MD_CTX_free(mdctx);
160 jit_add_eh_frame_info(Elf *e, void* unwinding, uint64_t unwinding_header_size,
161 uint64_t unwinding_size, uint64_t base_offset)
166 uint64_t unwinding_table_size = unwinding_size - unwinding_header_size;
169 * setup eh_frame section
173 warnx("cannot create section");
177 d = elf_newdata(scn);
179 warnx("cannot get new data");
185 d->d_buf = unwinding;
186 d->d_type = ELF_T_BYTE;
187 d->d_size = unwinding_table_size;
188 d->d_version = EV_CURRENT;
190 shdr = elf_getshdr(scn);
192 warnx("cannot get section header");
197 shdr->sh_type = SHT_PROGBITS;
198 shdr->sh_addr = base_offset;
199 shdr->sh_flags = SHF_ALLOC;
200 shdr->sh_entsize = 0;
203 * setup eh_frame_hdr section
207 warnx("cannot create section");
211 d = elf_newdata(scn);
213 warnx("cannot get new data");
219 d->d_buf = unwinding + unwinding_table_size;
220 d->d_type = ELF_T_BYTE;
221 d->d_size = unwinding_header_size;
222 d->d_version = EV_CURRENT;
224 shdr = elf_getshdr(scn);
226 warnx("cannot get section header");
231 shdr->sh_type = SHT_PROGBITS;
232 shdr->sh_addr = base_offset + unwinding_table_size;
233 shdr->sh_flags = SHF_ALLOC;
234 shdr->sh_entsize = 0;
240 * fd: file descriptor open for writing for the output file
241 * load_addr: code load address (could be zero, just used for buildid)
242 * sym: function name (for native code - used as the symbol)
243 * code: the native code
244 * csize: the code size in bytes
247 jit_write_elf(int fd, uint64_t load_addr, const char *sym,
248 const void *code, int csize,
249 void *debug __maybe_unused, int nr_debug_entries __maybe_unused,
250 void *unwinding, uint64_t unwinding_header_size, uint64_t unwinding_size)
258 uint64_t eh_frame_base_offset;
263 if (elf_version(EV_CURRENT) == EV_NONE) {
264 warnx("ELF initialization failed");
268 e = elf_begin(fd, ELF_C_WRITE, NULL);
270 warnx("elf_begin failed");
277 ehdr = elf_newehdr(e);
279 warnx("cannot get ehdr");
283 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
284 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
285 ehdr->e_machine = GEN_ELF_ARCH;
286 ehdr->e_type = ET_DYN;
287 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
288 ehdr->e_version = EV_CURRENT;
289 ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
292 * setup program header
294 phdr = elf_newphdr(e, 1);
295 phdr[0].p_type = PT_LOAD;
296 phdr[0].p_offset = 0;
299 phdr[0].p_filesz = csize;
300 phdr[0].p_memsz = csize;
301 phdr[0].p_flags = PF_X | PF_R;
309 warnx("cannot create section");
313 d = elf_newdata(scn);
315 warnx("cannot get new data");
321 d->d_buf = (void *)code;
322 d->d_type = ELF_T_BYTE;
324 d->d_version = EV_CURRENT;
326 shdr = elf_getshdr(scn);
328 warnx("cannot get section header");
333 shdr->sh_type = SHT_PROGBITS;
334 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
335 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
336 shdr->sh_entsize = 0;
339 * Setup .eh_frame_hdr and .eh_frame
342 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
343 retval = jit_add_eh_frame_info(e, unwinding,
344 unwinding_header_size, unwinding_size,
345 eh_frame_base_offset);
352 * setup section headers string table
356 warnx("cannot create section");
360 d = elf_newdata(scn);
362 warnx("cannot get new data");
368 d->d_buf = shd_string_table;
369 d->d_type = ELF_T_BYTE;
370 d->d_size = sizeof(shd_string_table);
371 d->d_version = EV_CURRENT;
373 shdr = elf_getshdr(scn);
375 warnx("cannot get section header");
379 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
380 shdr->sh_type = SHT_STRTAB;
382 shdr->sh_entsize = 0;
385 * setup symtab section
387 symtab[1].st_size = csize;
388 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
392 warnx("cannot create section");
396 d = elf_newdata(scn);
398 warnx("cannot get new data");
405 d->d_type = ELF_T_SYM;
406 d->d_size = sizeof(symtab);
407 d->d_version = EV_CURRENT;
409 shdr = elf_getshdr(scn);
411 warnx("cannot get section header");
415 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
416 shdr->sh_type = SHT_SYMTAB;
418 shdr->sh_entsize = sizeof(Elf_Sym);
419 shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
422 * setup symbols string table
423 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
425 symlen = 2 + strlen(sym);
426 strsym = calloc(1, symlen);
428 warnx("cannot allocate strsym");
431 strcpy(strsym + 1, sym);
435 warnx("cannot create section");
439 d = elf_newdata(scn);
441 warnx("cannot get new data");
448 d->d_type = ELF_T_BYTE;
450 d->d_version = EV_CURRENT;
452 shdr = elf_getshdr(scn);
454 warnx("cannot get section header");
458 shdr->sh_name = 25; /* offset in shd_string_table */
459 shdr->sh_type = SHT_STRTAB;
461 shdr->sh_entsize = 0;
464 * setup build-id section
468 warnx("cannot create section");
472 d = elf_newdata(scn);
474 warnx("cannot get new data");
479 * build-id generation
481 gen_build_id(&bnote, load_addr, code, csize);
482 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
483 bnote.desc.descsz = sizeof(bnote.build_id);
484 bnote.desc.type = NT_GNU_BUILD_ID;
485 strcpy(bnote.name, "GNU");
490 d->d_type = ELF_T_BYTE;
491 d->d_size = sizeof(bnote);
492 d->d_version = EV_CURRENT;
494 shdr = elf_getshdr(scn);
496 warnx("cannot get section header");
500 shdr->sh_name = 33; /* offset in shd_string_table */
501 shdr->sh_type = SHT_NOTE;
503 shdr->sh_flags = SHF_ALLOC;
504 shdr->sh_size = sizeof(bnote);
505 shdr->sh_entsize = 0;
507 #ifdef HAVE_DWARF_SUPPORT
508 if (debug && nr_debug_entries) {
509 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
515 if (elf_update(e, ELF_C_WRITE) < 0) {
516 warnx("elf_update 4 failed");