^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file is included up to twice from vdso2c.c. It generates code for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * 32-bit and 64-bit vDSOs. We will eventually need both for 64-bit builds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * since 32-bit vDSOs will then be built for 32-bit userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) static void BITSFUNC(go)(void *raw_addr, size_t raw_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) void *stripped_addr, size_t stripped_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) FILE *outfile, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) int found_load = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) unsigned long load_size = -1; /* Work around bogus warning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) unsigned long mapping_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) unsigned long j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) ELF(Shdr) *symtab_hdr = NULL, *strtab_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) ELF(Ehdr) *hdr = (ELF(Ehdr) *)raw_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) ELF(Dyn) *dyn = 0, *dyn_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) INT_BITS syms[NSYMS] = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ELF(Phdr) *pt = (ELF(Phdr) *)(raw_addr + GET_BE(&hdr->e_phoff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* Walk the segment table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) for (i = 0; i < GET_BE(&hdr->e_phnum); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (GET_BE(&pt[i].p_type) == PT_LOAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (found_load)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) fail("multiple PT_LOAD segs\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (GET_BE(&pt[i].p_offset) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) GET_BE(&pt[i].p_vaddr) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) fail("PT_LOAD in wrong place\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (GET_BE(&pt[i].p_memsz) != GET_BE(&pt[i].p_filesz))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) fail("cannot handle memsz != filesz\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) load_size = GET_BE(&pt[i].p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) found_load = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) } else if (GET_BE(&pt[i].p_type) == PT_DYNAMIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) dyn = raw_addr + GET_BE(&pt[i].p_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) dyn_end = raw_addr + GET_BE(&pt[i].p_offset) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) GET_BE(&pt[i].p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (!found_load)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) fail("no PT_LOAD seg\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (stripped_len < load_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) fail("stripped input is too short\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* Walk the dynamic table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) for (i = 0; dyn + i < dyn_end &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) GET_BE(&dyn[i].d_tag) != DT_NULL; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) typeof(dyn[i].d_tag) tag = GET_BE(&dyn[i].d_tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) typeof(dyn[i].d_un.d_val) val = GET_BE(&dyn[i].d_un.d_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if ((tag == DT_RELSZ || tag == DT_RELASZ) && (val != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fail("vdso image contains dynamic relocations\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* Walk the section table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) for (i = 0; i < GET_BE(&hdr->e_shnum); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) ELF(Shdr) *sh = raw_addr + GET_BE(&hdr->e_shoff) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) GET_BE(&hdr->e_shentsize) * i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (GET_BE(&sh->sh_type) == SHT_SYMTAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) symtab_hdr = sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!symtab_hdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) fail("no symbol table\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) strtab_hdr = raw_addr + GET_BE(&hdr->e_shoff) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) GET_BE(&hdr->e_shentsize) * GET_BE(&symtab_hdr->sh_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /* Walk the symbol table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for (i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) i < GET_BE(&symtab_hdr->sh_size) / GET_BE(&symtab_hdr->sh_entsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) int k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ELF(Sym) *sym = raw_addr + GET_BE(&symtab_hdr->sh_offset) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) GET_BE(&symtab_hdr->sh_entsize) * i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) const char *name = raw_addr + GET_BE(&strtab_hdr->sh_offset) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) GET_BE(&sym->st_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) for (k = 0; k < NSYMS; k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!strcmp(name, required_syms[k].name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (syms[k]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) fail("duplicate symbol %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) required_syms[k].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * Careful: we use negative addresses, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * st_value is unsigned, so we rely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * on syms[k] being a signed type of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * correct width.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) syms[k] = GET_BE(&sym->st_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* Validate mapping addresses. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (syms[sym_vvar_start] % 8192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fail("vvar_begin must be a multiple of 8192\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) fwrite(stripped_addr, stripped_len, 1, outfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) mapping_size = (stripped_len + 8191) / 8192 * 8192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) fprintf(outfile, "/* AUTOMATICALLY GENERATED -- DO NOT EDIT */\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) fprintf(outfile, "#include <linux/cache.h>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) fprintf(outfile, "#include <asm/vdso.h>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) fprintf(outfile, "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) fprintf(outfile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "static unsigned char raw_data[%lu] __ro_after_init __aligned(8192)= {",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) mapping_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (j = 0; j < stripped_len; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (j % 10 == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) fprintf(outfile, "\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) fprintf(outfile, "0x%02X, ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) (int)((unsigned char *)stripped_addr)[j]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) fprintf(outfile, "\n};\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) fprintf(outfile, "const struct vdso_image %s_builtin = {\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) fprintf(outfile, "\t.data = raw_data,\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) fprintf(outfile, "\t.size = %lu,\n", mapping_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) for (i = 0; i < NSYMS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (required_syms[i].export && syms[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) fprintf(outfile, "\t.sym_%s = %" PRIi64 ",\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) required_syms[i].name, (int64_t)syms[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) fprintf(outfile, "};\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) }