main_posix.c (1266B)
1 /* See LICENSE for license details. */ 2 #include <stdint.h> 3 #include <stddef.h> 4 5 typedef uint8_t u8; 6 typedef uint16_t u16; 7 typedef uint32_t u32; 8 typedef uint32_t b32; 9 typedef uint64_t u64; 10 typedef int8_t s8; 11 typedef int16_t s16; 12 typedef int32_t s32; 13 typedef int64_t s64; 14 typedef ptrdiff_t iz; 15 16 #include <stdio.h> 17 18 #include "elfinspect.c" 19 20 #include <fcntl.h> 21 #include <sys/mman.h> 22 #include <sys/stat.h> 23 #include <unistd.h> 24 25 function Arena 26 os_arena_new(iz size) 27 { 28 Arena result = {0}; 29 iz page_size = sysconf(_SC_PAGESIZE); 30 if (size % page_size) size += page_size - (size % page_size); 31 u8 *mem = mmap(0, size, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); 32 if (mem != MAP_FAILED) { 33 result.beg = mem; 34 result.end = mem + size; 35 } 36 return result; 37 } 38 39 function str8 40 os_map_file(u8 *name) 41 { 42 str8 result = {0}; 43 s32 fd = open((char *)name, O_RDONLY); 44 struct stat sb; 45 if (fd != -1 && fstat(fd, &sb) != -1) { 46 result.data = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 47 if (result.data != MAP_FAILED) 48 result.len = sb.st_size; 49 } 50 if (fd != -1) close(fd); 51 return result; 52 } 53 54 s32 55 main(s32 argc, char *argv[]) 56 { 57 Arena arena = os_arena_new(MB(32)); 58 str8 file = os_map_file((u8 *)argv[0]); 59 elfinspect(arena, file); 60 return 0; 61 }