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