반응형
| #include <stdio.h> #include <stdlib.h> | |
| #include <stdint.h> | |
| #include <sys/types.h> | |
| #include <sys/stat.h> | |
| #include <unistd.h> | |
| #include <fcntl.h> | |
| #include <errno.h> | |
| #include <elf.h> | |
| #include <sys/mman.h> | |
| int main(int argc, char *argv[]) | |
| { | |
| int ret; | |
| int fd; | |
| int i; | |
| uint8_t *mem; | |
| struct stat st; | |
| Elf32_Ehdr *ehdr = NULL; | |
| Elf32_Phdr *phdr = NULL; | |
| Elf32_Shdr *shdr = NULL; | |
| if (argc < 2) { | |
| printf("Usage: %s <elf file>\n", argv[0]); | |
| exit(0); | |
| } | |
| errno = 0; | |
| fd = open(argv[1], O_RDONLY); | |
| if (errno < 0 || fd == -1) { | |
| perror("open"); | |
| exit(-1); | |
| } | |
| errno = 0; | |
| ret = fstat(fd, &st); | |
| if (ret < 0) { | |
| perror("fstat"); | |
| exit(-1); | |
| } | |
| mem = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); | |
| if (mem == MAP_FAILED) { | |
| perror("mmap"); | |
| exit(-1); | |
| } | |
| ehdr = (Elf32_Ehdr *)mem; | |
| phdr = (Elf32_Phdr *)&mem[ehdr->e_phoff]; | |
| shdr = (Elf32_Shdr *)&mem[ehdr->e_shoff]; | |
| close(fd); | |
| munmap(mem, st.st_size); | |
| return 0; | |
| } |
반응형
'Linux > System Programming' 카테고리의 다른 글
| [ODP] 실행시간 측정하기 (0) | 2018.05.17 |
|---|---|
| ptrace - process tracer (writing....) (0) | 2018.03.07 |
| [socket] socketpair non-blocking read write (0) | 2017.12.30 |
| SocketPair (0) | 2017.12.18 |
| Warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] (0) | 2016.01.12 |