45 lines
909 B
C
45 lines
909 B
C
#include "ft_nm.h"
|
|
|
|
void print_bits(char c) {
|
|
for (int a = 7; a >= 0; a--) {
|
|
ft_putnbr(c >> a & 1);
|
|
}
|
|
ft_putchar('\n');
|
|
}
|
|
|
|
int main(int ac, char **av) {
|
|
char *path = ft_strdup("a.out");
|
|
if (ac > 1) {
|
|
free(path);
|
|
path = av[1];
|
|
}
|
|
int fd = open(path, O_RDONLY);
|
|
if (fd == -1) {
|
|
ft_nm_error(path);
|
|
}
|
|
void *ptr = NULL;
|
|
Elf64_Ehdr *header;
|
|
|
|
ptr = mmap(NULL, 64, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
if (ptr != MAP_FAILED) {
|
|
header = (Elf64_Ehdr *)ptr;
|
|
if (check_header(header) == FT_NM_FAILURE) {
|
|
printf("ft_nm: %s: file format not recognized\n", path);
|
|
exit (1);
|
|
}
|
|
for (int i = 0; i < 64; i++) {
|
|
print_bits(((char *)ptr)[i]);
|
|
}
|
|
if (header->e_ident[EI_OSABI] == 0x00) {
|
|
printf("This ELF is for System V! :)\n");
|
|
}
|
|
else if (header->e_ident[EI_OSABI] == 0x03) {
|
|
printf("This ELF is for linux! :)\n");
|
|
} else {
|
|
printf("What's my purpose :(\n");
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|