1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vdso2c - A vdso image preparation tool 4 * Copyright (c) 2014 Andy Lutomirski and others 5 * 6 * vdso2c requires stripped and unstripped input. It would be trivial 7 * to fully strip the input in here, but, for reasons described below, 8 * we need to write a section table. Doing this is more or less 9 * equivalent to dropping all non-allocatable sections, but it's 10 * easier to let objcopy handle that instead of doing it ourselves. 11 * If we ever need to do something fancier than what objcopy provides, 12 * it would be straightforward to add here. 13 * 14 * We're keep a section table for a few reasons: 15 * 16 * The Go runtime had a couple of bugs: it would read the section 17 * table to try to figure out how many dynamic symbols there were (it 18 * shouldn't have looked at the section table at all) and, if there 19 * were no SHT_SYNDYM section table entry, it would use an 20 * uninitialized value for the number of symbols. An empty DYNSYM 21 * table would work, but I see no reason not to write a valid one (and 22 * keep full performance for old Go programs). This hack is only 23 * needed on x86_64. 24 * 25 * The bug was introduced on 2012-08-31 by: 26 * https://code.google.com/p/go/source/detail?r=56ea40aac72b 27 * and was fixed on 2014-06-13 by: 28 * https://code.google.com/p/go/source/detail?r=fc1cd5e12595 29 * 30 * Binutils has issues debugging the vDSO: it reads the section table to 31 * find SHT_NOTE; it won't look at PT_NOTE for the in-memory vDSO, which 32 * would break build-id if we removed the section table. Binutils 33 * also requires that shstrndx != 0. See: 34 * https://sourceware.org/bugzilla/show_bug.cgi?id=17064 35 * 36 * elfutils might not look for PT_NOTE if there is a section table at 37 * all. I don't know whether this matters for any practical purpose. 38 * 39 * For simplicity, rather than hacking up a partial section table, we 40 * just write a mostly complete one. We omit non-dynamic symbols, 41 * though, since they're rather large. 42 * 43 * Once binutils gets fixed, we might be able to drop this for all but 44 * the 64-bit vdso, since build-id only works in kernel RPMs, and 45 * systems that update to new enough kernel RPMs will likely update 46 * binutils in sync. build-id has never worked for home-built kernel 47 * RPMs without manual symlinking, and I suspect that no one ever does 48 * that. 49 */ 50 51 #include <inttypes.h> 52 #include <stdint.h> 53 #include <unistd.h> 54 #include <stdarg.h> 55 #include <stdlib.h> 56 #include <stdio.h> 57 #include <string.h> 58 #include <fcntl.h> 59 #include <err.h> 60 61 #include <sys/mman.h> 62 #include <sys/types.h> 63 64 #include <tools/le_byteshift.h> 65 66 #include <linux/elf.h> 67 #include <linux/types.h> 68 #include <linux/kernel.h> 69 70 const char *outfilename; 71 72 struct vdso_sym { 73 const char *name; 74 bool export; 75 }; 76 77 struct vdso_sym required_syms[] = { 78 {"__kernel_vsyscall", true}, 79 {"__kernel_sigreturn", true}, 80 {"__kernel_rt_sigreturn", true}, 81 {"int80_landing_pad", true}, 82 {"vdso32_rt_sigreturn_landing_pad", true}, 83 {"vdso32_sigreturn_landing_pad", true}, 84 }; 85 86 __attribute__((format(printf, 1, 2))) __attribute__((noreturn)) 87 static void fail(const char *format, ...) 88 { 89 va_list ap; 90 va_start(ap, format); 91 fprintf(stderr, "Error: "); 92 vfprintf(stderr, format, ap); 93 if (outfilename) 94 unlink(outfilename); 95 exit(1); 96 va_end(ap); 97 } 98 99 /* 100 * Evil macros for little-endian reads and writes 101 */ 102 #define GLE(x, bits, ifnot) \ 103 __builtin_choose_expr( \ 104 (sizeof(*(x)) == bits/8), \ 105 (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot) 106 107 extern void bad_get_le(void); 108 #define LAST_GLE(x) \ 109 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le()) 110 111 #define GET_LE(x) \ 112 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x)))) 113 114 #define PLE(x, val, bits, ifnot) \ 115 __builtin_choose_expr( \ 116 (sizeof(*(x)) == bits/8), \ 117 put_unaligned_le##bits((val), (x)), ifnot) 118 119 extern void bad_put_le(void); 120 #define LAST_PLE(x, val) \ 121 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le()) 122 123 #define PUT_LE(x, val) \ 124 PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val)))) 125 126 127 #define NSYMS ARRAY_SIZE(required_syms) 128 129 #define BITSFUNC3(name, bits, suffix) name##bits##suffix 130 #define BITSFUNC2(name, bits, suffix) BITSFUNC3(name, bits, suffix) 131 #define BITSFUNC(name) BITSFUNC2(name, ELF_BITS, ) 132 133 #define INT_BITS BITSFUNC2(int, ELF_BITS, _t) 134 135 #define ELF_BITS_XFORM2(bits, x) Elf##bits##_##x 136 #define ELF_BITS_XFORM(bits, x) ELF_BITS_XFORM2(bits, x) 137 #define ELF(x) ELF_BITS_XFORM(ELF_BITS, x) 138 139 #define ELF_BITS 64 140 #include "vdso2c.h" 141 #undef ELF_BITS 142 143 #define ELF_BITS 32 144 #include "vdso2c.h" 145 #undef ELF_BITS 146 147 static void go(void *raw_addr, size_t raw_len, 148 void *stripped_addr, size_t stripped_len, 149 FILE *outfile, const char *name) 150 { 151 Elf64_Ehdr *hdr = (Elf64_Ehdr *)raw_addr; 152 153 if (hdr->e_ident[EI_CLASS] == ELFCLASS64) { 154 go64(raw_addr, raw_len, stripped_addr, stripped_len, 155 outfile, name); 156 } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) { 157 go32(raw_addr, raw_len, stripped_addr, stripped_len, 158 outfile, name); 159 } else { 160 fail("unknown ELF class\n"); 161 } 162 } 163 164 static void map_input(const char *name, void **addr, size_t *len, int prot) 165 { 166 off_t tmp_len; 167 168 int fd = open(name, O_RDONLY); 169 if (fd == -1) 170 err(1, "open(%s)", name); 171 172 tmp_len = lseek(fd, 0, SEEK_END); 173 if (tmp_len == (off_t)-1) 174 err(1, "lseek"); 175 *len = (size_t)tmp_len; 176 177 *addr = mmap(NULL, tmp_len, prot, MAP_PRIVATE, fd, 0); 178 if (*addr == MAP_FAILED) 179 err(1, "mmap"); 180 181 close(fd); 182 } 183 184 int main(int argc, char **argv) 185 { 186 size_t raw_len, stripped_len; 187 void *raw_addr, *stripped_addr; 188 FILE *outfile; 189 char *name, *tmp; 190 int namelen; 191 192 if (argc != 4) { 193 printf("Usage: vdso2c RAW_INPUT STRIPPED_INPUT OUTPUT\n"); 194 return 1; 195 } 196 197 /* 198 * Figure out the struct name. If we're writing to a .so file, 199 * generate raw output instead. 200 */ 201 name = strdup(argv[3]); 202 namelen = strlen(name); 203 if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) { 204 name = NULL; 205 } else { 206 tmp = strrchr(name, '/'); 207 if (tmp) 208 name = tmp + 1; 209 tmp = strchr(name, '.'); 210 if (tmp) 211 *tmp = '\0'; 212 for (tmp = name; *tmp; tmp++) 213 if (*tmp == '-') 214 *tmp = '_'; 215 } 216 217 map_input(argv[1], &raw_addr, &raw_len, PROT_READ); 218 map_input(argv[2], &stripped_addr, &stripped_len, PROT_READ); 219 220 outfilename = argv[3]; 221 outfile = fopen(outfilename, "w"); 222 if (!outfile) 223 err(1, "fopen(%s)", outfilename); 224 225 go(raw_addr, raw_len, stripped_addr, stripped_len, outfile, name); 226 227 munmap(raw_addr, raw_len); 228 munmap(stripped_addr, stripped_len); 229 fclose(outfile); 230 231 return 0; 232 } 233