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 // FIXME, remove this and fix the deprecation warnings before its removed and
34 // We'll break for good here...
35 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
37 #ifdef HAVE_LIBCRYPTO_SUPPORT
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)
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");
275 ehdr = elf_newehdr(e);
277 warnx("cannot get ehdr");
281 ehdr->e_ident[EI_DATA] = GEN_ELF_ENDIAN;
282 ehdr->e_ident[EI_CLASS] = GEN_ELF_CLASS;
283 ehdr->e_machine = GEN_ELF_ARCH;
284 ehdr->e_type = ET_DYN;
285 ehdr->e_entry = GEN_ELF_TEXT_OFFSET;
286 ehdr->e_version = EV_CURRENT;
287 ehdr->e_shstrndx= unwinding ? 4 : 2; /* shdr index for section name */
290 * setup program header
292 phdr = elf_newphdr(e, 1);
293 phdr[0].p_type = PT_LOAD;
294 phdr[0].p_offset = 0;
297 phdr[0].p_filesz = csize;
298 phdr[0].p_memsz = csize;
299 phdr[0].p_flags = PF_X | PF_R;
307 warnx("cannot create section");
311 d = elf_newdata(scn);
313 warnx("cannot get new data");
319 d->d_buf = (void *)code;
320 d->d_type = ELF_T_BYTE;
322 d->d_version = EV_CURRENT;
324 shdr = elf_getshdr(scn);
326 warnx("cannot get section header");
331 shdr->sh_type = SHT_PROGBITS;
332 shdr->sh_addr = GEN_ELF_TEXT_OFFSET;
333 shdr->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
334 shdr->sh_entsize = 0;
337 * Setup .eh_frame_hdr and .eh_frame
340 eh_frame_base_offset = ALIGN_8(GEN_ELF_TEXT_OFFSET + csize);
341 retval = jit_add_eh_frame_info(e, unwinding,
342 unwinding_header_size, unwinding_size,
343 eh_frame_base_offset);
349 * setup section headers string table
353 warnx("cannot create section");
357 d = elf_newdata(scn);
359 warnx("cannot get new data");
365 d->d_buf = shd_string_table;
366 d->d_type = ELF_T_BYTE;
367 d->d_size = sizeof(shd_string_table);
368 d->d_version = EV_CURRENT;
370 shdr = elf_getshdr(scn);
372 warnx("cannot get section header");
376 shdr->sh_name = 7; /* offset of '.shstrtab' in shd_string_table */
377 shdr->sh_type = SHT_STRTAB;
379 shdr->sh_entsize = 0;
382 * setup symtab section
384 symtab[1].st_size = csize;
385 symtab[1].st_value = GEN_ELF_TEXT_OFFSET;
389 warnx("cannot create section");
393 d = elf_newdata(scn);
395 warnx("cannot get new data");
402 d->d_type = ELF_T_SYM;
403 d->d_size = sizeof(symtab);
404 d->d_version = EV_CURRENT;
406 shdr = elf_getshdr(scn);
408 warnx("cannot get section header");
412 shdr->sh_name = 17; /* offset of '.symtab' in shd_string_table */
413 shdr->sh_type = SHT_SYMTAB;
415 shdr->sh_entsize = sizeof(Elf_Sym);
416 shdr->sh_link = unwinding ? 6 : 4; /* index of .strtab section */
419 * setup symbols string table
420 * 2 = 1 for 0 in 1st entry, 1 for the 0 at end of symbol for 2nd entry
422 symlen = 2 + strlen(sym);
423 strsym = calloc(1, symlen);
425 warnx("cannot allocate strsym");
428 strcpy(strsym + 1, sym);
432 warnx("cannot create section");
436 d = elf_newdata(scn);
438 warnx("cannot get new data");
445 d->d_type = ELF_T_BYTE;
447 d->d_version = EV_CURRENT;
449 shdr = elf_getshdr(scn);
451 warnx("cannot get section header");
455 shdr->sh_name = 25; /* offset in shd_string_table */
456 shdr->sh_type = SHT_STRTAB;
458 shdr->sh_entsize = 0;
461 * setup build-id section
465 warnx("cannot create section");
469 d = elf_newdata(scn);
471 warnx("cannot get new data");
476 * build-id generation
478 gen_build_id(&bnote, load_addr, code, csize);
479 bnote.desc.namesz = sizeof(bnote.name); /* must include 0 termination */
480 bnote.desc.descsz = sizeof(bnote.build_id);
481 bnote.desc.type = NT_GNU_BUILD_ID;
482 strcpy(bnote.name, "GNU");
487 d->d_type = ELF_T_BYTE;
488 d->d_size = sizeof(bnote);
489 d->d_version = EV_CURRENT;
491 shdr = elf_getshdr(scn);
493 warnx("cannot get section header");
497 shdr->sh_name = 33; /* offset in shd_string_table */
498 shdr->sh_type = SHT_NOTE;
500 shdr->sh_flags = SHF_ALLOC;
501 shdr->sh_size = sizeof(bnote);
502 shdr->sh_entsize = 0;
504 #ifdef HAVE_DWARF_SUPPORT
505 if (debug && nr_debug_entries) {
506 retval = jit_add_debug_info(e, load_addr, debug, nr_debug_entries);
512 if (elf_update(e, ELF_C_WRITE) < 0) {
513 warnx("elf_update 4 failed");