12 #include <sys/types.h>
14 #include <linux/elf.h>
15 #include <linux/types.h>
17 const char *outfilename;
19 /* Symbols that we need in vdso2c. */
26 const int special_pages[] = {
31 char const * const required_syms[] = {
32 [sym_vvar_page] = "vvar_page",
33 [sym_hpet_page] = "hpet_page",
34 [sym_end_mapping] = "end_mapping",
36 "VDSO32_SYSENTER_RETURN",
39 "__kernel_rt_sigreturn",
42 __attribute__((format(printf, 1, 2))) __attribute__((noreturn))
43 static void fail(const char *format, ...)
47 fprintf(stderr, "Error: ");
48 vfprintf(stderr, format, ap);
55 * Evil macros to do a little-endian read.
57 #define GLE(x, bits, ifnot) \
58 __builtin_choose_expr( \
59 (sizeof(x) == bits/8), \
60 (__typeof__(x))le##bits##toh(x), ifnot)
62 extern void bad_get_le(uint64_t);
64 __builtin_choose_expr(sizeof(x) == 1, (x), bad_get_le(x))
67 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_LE(x))))
69 #define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
73 #define Elf_Ehdr Elf64_Ehdr
74 #define Elf_Shdr Elf64_Shdr
75 #define Elf_Phdr Elf64_Phdr
76 #define Elf_Sym Elf64_Sym
77 #define Elf_Dyn Elf64_Dyn
89 #define Elf_Ehdr Elf32_Ehdr
90 #define Elf_Shdr Elf32_Shdr
91 #define Elf_Phdr Elf32_Phdr
92 #define Elf_Sym Elf32_Sym
93 #define Elf_Dyn Elf32_Dyn
103 static void go(void *addr, size_t len, FILE *outfile, const char *name)
105 Elf64_Ehdr *hdr = (Elf64_Ehdr *)addr;
107 if (hdr->e_ident[EI_CLASS] == ELFCLASS64) {
108 go64(addr, len, outfile, name);
109 } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) {
110 go32(addr, len, outfile, name);
112 fail("unknown ELF class\n");
116 int main(int argc, char **argv)
126 printf("Usage: vdso2c INPUT OUTPUT\n");
131 * Figure out the struct name. If we're writing to a .so file,
132 * generate raw output insted.
134 name = strdup(argv[2]);
135 namelen = strlen(name);
136 if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) {
139 tmp = strrchr(name, '/');
142 tmp = strchr(name, '.');
145 for (tmp = name; *tmp; tmp++)
150 fd = open(argv[1], O_RDONLY);
152 err(1, "%s", argv[1]);
154 len = lseek(fd, 0, SEEK_END);
155 if (len == (off_t)-1)
158 addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
159 if (addr == MAP_FAILED)
162 outfilename = argv[2];
163 outfile = fopen(outfilename, "w");
165 err(1, "%s", argv[2]);
167 go(addr, (size_t)len, outfile, name);