| header_top = """ |
| #ifndef _IRT_SYSCALLS_H |
| #define _IRT_SYSCALLS_H |
| |
| #include <sys/cdefs_elf.h> |
| #include <machine/cdefs.h> |
| |
| #include <irt.h> |
| #include <string.h> |
| |
| #include <sys/types.h> |
| #include <sys/epoll.h> |
| #include <sys/select.h> |
| #include <poll.h> |
| #include <stddef.h> |
| #include <fcntl.h> |
| #include <time.h> |
| #include <nacl_stat.h> |
| |
| #define weak_alias(oldname, newname) \\ |
| extern typeof(oldname) newname __attribute__ ((weak, alias (#oldname))); |
| |
| struct dirent; |
| struct epoll_event; |
| struct msghdr; |
| struct nacl_abi_stat; |
| struct sockaddr; |
| struct timespec; |
| struct timeval; |
| struct NaClMemMappingInfo; |
| |
| typedef void (*nacl_start_func_t)(void); |
| #define socklen_t long |
| """ |
| |
| header_mid = """ |
| // The linker reserves address 0x10030000 as a pointer to the IRT syscalls |
| // struct. This address is shared between the loader and dynamically loaded |
| // executables. |
| #define _IRT_SYSCALL_PTR (*((_irt_syscalls_t**) 0x10030000)) |
| """ |
| |
| header_bottom = """ |
| |
| #endif |
| """ |
| |
| |
| |
| def PrintFunction(ret, name, args): |
| print 'inline %s __%s(%s) {' % (ret, name, ', '.join(args)) |
| varnames = [arg.split(' ')[-1] for arg in args] |
| if ret == 'void': |
| rtype = ' ' |
| else: |
| rtype = ' return ' |
| if varnames == ['void']: |
| varnames = [] |
| print '%s_IRT_SYSCALL_PTR->%s(%s);' % (rtype, name, ', '.join(varnames)) |
| print '}' |
| |
| def PrintMacro(ret, name, args): |
| print "#define __%s (_IRT_SYSCALL_PTR->%s)" % (name, name) |
| |
| def ParseSyscalls(lines): |
| syscalls = [] |
| for line in lines: |
| ret_end = line.find('(*') |
| ret = line[:ret_end] |
| name_end = line.find(')') |
| name = line[ret_end + 2: name_end] |
| args = line[name_end + 2 : -2].split(',') |
| args = [x.strip() for x in args] |
| syscalls.append((ret, name, args)) |
| return syscalls |
| |
| lines = open('irt_syscalls.defs', 'rt').readlines() |
| syscalls = ParseSyscalls(lines) |
| |
| |
| print header_top |
| print '__BEGIN_DECLS' |
| print 'typedef struct {' |
| for ret, name, args in syscalls: |
| print ' %s (*%s)(%s);' % (ret, name, ', '.join(args)) |
| print '} _irt_syscalls_t;' |
| |
| print header_mid |
| |
| for ret, name, args in syscalls: |
| PrintMacro(ret, name, args) |
| print '__END_DECLS' |
| print header_bottom |
| |