12 #include <sys/types.h>
14 #include <tools/le_byteshift.h>
16 #include <linux/elf.h>
17 #include <linux/types.h>
19 const char *outfilename
;
21 /* Symbols that we need in vdso2c. */
28 const int special_pages
[] = {
33 char const * const required_syms
[] = {
34 [sym_vvar_page
] = "vvar_page",
35 [sym_hpet_page
] = "hpet_page",
36 [sym_end_mapping
] = "end_mapping",
38 "VDSO32_SYSENTER_RETURN",
41 "__kernel_rt_sigreturn",
44 __attribute__((format(printf
, 1, 2))) __attribute__((noreturn
))
45 static void fail(const char *format
, ...)
49 fprintf(stderr
, "Error: ");
50 vfprintf(stderr
, format
, ap
);
57 * Evil macros for little-endian reads and writes
59 #define GLE(x, bits, ifnot) \
60 __builtin_choose_expr( \
61 (sizeof(*(x)) == bits/8), \
62 (__typeof__(*(x)))get_unaligned_le##bits(x), ifnot)
64 extern void bad_get_le(void);
66 __builtin_choose_expr(sizeof(*(x)) == 1, *(x), bad_get_le())
69 GLE(x, 64, GLE(x, 32, GLE(x, 16, LAST_GLE(x))))
71 #define PLE(x, val, bits, ifnot) \
72 __builtin_choose_expr( \
73 (sizeof(*(x)) == bits/8), \
74 put_unaligned_le##bits((val), (x)), ifnot)
76 extern void bad_put_le(void);
77 #define LAST_PLE(x, val) \
78 __builtin_choose_expr(sizeof(*(x)) == 1, *(x) = (val), bad_put_le())
80 #define PUT_LE(x, val) \
81 PLE(x, val, 64, PLE(x, val, 32, PLE(x, val, 16, LAST_PLE(x, val))))
84 #define NSYMS (sizeof(required_syms) / sizeof(required_syms[0]))
88 #define Elf_Ehdr Elf64_Ehdr
89 #define Elf_Shdr Elf64_Shdr
90 #define Elf_Phdr Elf64_Phdr
91 #define Elf_Sym Elf64_Sym
92 #define Elf_Dyn Elf64_Dyn
104 #define Elf_Ehdr Elf32_Ehdr
105 #define Elf_Shdr Elf32_Shdr
106 #define Elf_Phdr Elf32_Phdr
107 #define Elf_Sym Elf32_Sym
108 #define Elf_Dyn Elf32_Dyn
118 static void go(void *addr
, size_t len
, FILE *outfile
, const char *name
)
120 Elf64_Ehdr
*hdr
= (Elf64_Ehdr
*)addr
;
122 if (hdr
->e_ident
[EI_CLASS
] == ELFCLASS64
) {
123 go64(addr
, len
, outfile
, name
);
124 } else if (hdr
->e_ident
[EI_CLASS
] == ELFCLASS32
) {
125 go32(addr
, len
, outfile
, name
);
127 fail("unknown ELF class\n");
131 int main(int argc
, char **argv
)
141 printf("Usage: vdso2c INPUT OUTPUT\n");
146 * Figure out the struct name. If we're writing to a .so file,
147 * generate raw output insted.
149 name
= strdup(argv
[2]);
150 namelen
= strlen(name
);
151 if (namelen
>= 3 && !strcmp(name
+ namelen
- 3, ".so")) {
154 tmp
= strrchr(name
, '/');
157 tmp
= strchr(name
, '.');
160 for (tmp
= name
; *tmp
; tmp
++)
165 fd
= open(argv
[1], O_RDONLY
);
167 err(1, "%s", argv
[1]);
169 len
= lseek(fd
, 0, SEEK_END
);
170 if (len
== (off_t
)-1)
173 addr
= mmap(NULL
, len
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
174 if (addr
== MAP_FAILED
)
177 outfilename
= argv
[2];
178 outfile
= fopen(outfilename
, "w");
180 err(1, "%s", argv
[2]);
182 go(addr
, (size_t)len
, outfile
, name
);