]>
Commit | Line | Data |
---|---|---|
6f121e54 AL |
1 | #include <inttypes.h> |
2 | #include <stdint.h> | |
3 | #include <unistd.h> | |
4 | #include <stdarg.h> | |
5 | #include <stdlib.h> | |
6 | #include <stdio.h> | |
7 | #include <string.h> | |
8 | #include <fcntl.h> | |
9 | #include <err.h> | |
10 | ||
11 | #include <sys/mman.h> | |
12 | #include <sys/types.h> | |
13 | ||
14 | #include <linux/elf.h> | |
15 | #include <linux/types.h> | |
16 | ||
17 | /* Symbols that we need in vdso2c. */ | |
18 | char const * const required_syms[] = { | |
19 | "VDSO32_NOTE_MASK", | |
20 | "VDSO32_SYSENTER_RETURN", | |
21 | "__kernel_vsyscall", | |
22 | "__kernel_sigreturn", | |
23 | "__kernel_rt_sigreturn", | |
24 | }; | |
25 | ||
26 | __attribute__((format(printf, 1, 2))) __attribute__((noreturn)) | |
27 | static void fail(const char *format, ...) | |
28 | { | |
29 | va_list ap; | |
30 | va_start(ap, format); | |
31 | fprintf(stderr, "Error: "); | |
32 | vfprintf(stderr, format, ap); | |
33 | exit(1); | |
34 | va_end(ap); | |
35 | } | |
36 | ||
37 | #define NSYMS (sizeof(required_syms) / sizeof(required_syms[0])) | |
38 | ||
39 | #define BITS 64 | |
40 | #define GOFUNC go64 | |
41 | #define Elf_Ehdr Elf64_Ehdr | |
42 | #define Elf_Shdr Elf64_Shdr | |
43 | #define Elf_Phdr Elf64_Phdr | |
44 | #define Elf_Sym Elf64_Sym | |
45 | #define Elf_Dyn Elf64_Dyn | |
46 | #include "vdso2c.h" | |
47 | #undef BITS | |
48 | #undef GOFUNC | |
49 | #undef Elf_Ehdr | |
50 | #undef Elf_Shdr | |
51 | #undef Elf_Phdr | |
52 | #undef Elf_Sym | |
53 | #undef Elf_Dyn | |
54 | ||
55 | #define BITS 32 | |
56 | #define GOFUNC go32 | |
57 | #define Elf_Ehdr Elf32_Ehdr | |
58 | #define Elf_Shdr Elf32_Shdr | |
59 | #define Elf_Phdr Elf32_Phdr | |
60 | #define Elf_Sym Elf32_Sym | |
61 | #define Elf_Dyn Elf32_Dyn | |
62 | #include "vdso2c.h" | |
63 | #undef BITS | |
64 | #undef GOFUNC | |
65 | #undef Elf_Ehdr | |
66 | #undef Elf_Shdr | |
67 | #undef Elf_Phdr | |
68 | #undef Elf_Sym | |
69 | #undef Elf_Dyn | |
70 | ||
71 | static int go(void *addr, size_t len, FILE *outfile, const char *name) | |
72 | { | |
73 | Elf64_Ehdr *hdr = (Elf64_Ehdr *)addr; | |
74 | ||
75 | if (hdr->e_ident[EI_CLASS] == ELFCLASS64) { | |
76 | return go64(addr, len, outfile, name); | |
77 | } else if (hdr->e_ident[EI_CLASS] == ELFCLASS32) { | |
78 | return go32(addr, len, outfile, name); | |
79 | } else { | |
80 | fprintf(stderr, "Error: unknown ELF class\n"); | |
81 | return 1; | |
82 | } | |
83 | } | |
84 | ||
85 | int main(int argc, char **argv) | |
86 | { | |
87 | int fd; | |
88 | off_t len; | |
89 | void *addr; | |
90 | FILE *outfile; | |
91 | int ret; | |
92 | char *name, *tmp; | |
93 | int namelen; | |
94 | ||
95 | if (argc != 3) { | |
96 | printf("Usage: vdso2c INPUT OUTPUT\n"); | |
97 | return 1; | |
98 | } | |
99 | ||
100 | /* | |
101 | * Figure out the struct name. If we're writing to a .so file, | |
102 | * generate raw output insted. | |
103 | */ | |
104 | name = strdup(argv[2]); | |
105 | namelen = strlen(name); | |
106 | if (namelen >= 3 && !strcmp(name + namelen - 3, ".so")) { | |
107 | name = NULL; | |
108 | } else { | |
109 | tmp = strrchr(name, '/'); | |
110 | if (tmp) | |
111 | name = tmp + 1; | |
112 | tmp = strchr(name, '.'); | |
113 | if (tmp) | |
114 | *tmp = '\0'; | |
115 | for (tmp = name; *tmp; tmp++) | |
116 | if (*tmp == '-') | |
117 | *tmp = '_'; | |
118 | } | |
119 | ||
120 | fd = open(argv[1], O_RDONLY); | |
121 | if (fd == -1) | |
122 | err(1, "%s", argv[1]); | |
123 | ||
124 | len = lseek(fd, 0, SEEK_END); | |
125 | if (len == (off_t)-1) | |
126 | err(1, "lseek"); | |
127 | ||
128 | addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
129 | if (addr == MAP_FAILED) | |
130 | err(1, "mmap"); | |
131 | ||
132 | outfile = fopen(argv[2], "w"); | |
133 | if (!outfile) | |
134 | err(1, "%s", argv[2]); | |
135 | ||
136 | ret = go(addr, (size_t)len, outfile, name); | |
137 | ||
138 | munmap(addr, len); | |
139 | fclose(outfile); | |
140 | ||
141 | return ret; | |
142 | } |