Last active
June 6, 2018 13:21
-
-
Save alex-pat/22f9f2d4a797906aaa7efc45cc46545d to your computer and use it in GitHub Desktop.
library call from scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/mman.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <assert.h> | |
long filepid() | |
{ | |
long (*func)() = NULL; | |
long ret; | |
int fd = open("lib.hex", O_RDONLY); | |
if (fd == -1) { | |
perror("open"); | |
return -1; | |
} | |
func = mmap(NULL, 21, PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0); | |
if (func == MAP_FAILED) { | |
perror("mmap"); | |
return -1; | |
} | |
if (close(fd) == -1) { | |
perror("close"); | |
return -1; | |
} | |
ret = func(); | |
if (munmap(func, 21) == -1) { | |
perror("munmap"); | |
return -1; | |
} | |
return ret; | |
} | |
int main() | |
{ | |
assert(getpid() == filepid()); | |
puts("Correct"); | |
return 0; | |
} |
Author
alex-pat
commented
Jun 6, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment