From e21df48de15e97c9c9f517754bd23e10cf3e285d Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 15 Mar 2024 15:58:57 +0100 Subject: [PATCH] feat: error handling and header start --- Makefile | 2 ++ inc/ft_nm.h | 6 ++++++ src/error.c | 11 +++++++++++ src/header.c | 26 ++++++++++++++++++++++++++ src/main.c | 22 +++++++++++++++++----- 5 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 src/error.c create mode 100644 src/header.c diff --git a/Makefile b/Makefile index 916b12c..8c27cfd 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ NAME = ft_nm SRC_FILES = main.c \ + error.c \ + header.c \ INC_FILES = ft_nm.h \ diff --git a/inc/ft_nm.h b/inc/ft_nm.h index 381febd..835d06c 100644 --- a/inc/ft_nm.h +++ b/inc/ft_nm.h @@ -9,4 +9,10 @@ # include "libft.h" +# define FT_NM_SUCCESS 0 +# define FT_NM_FAILURE -1 + +void ft_nm_error(const char *path); +int check_header(Elf64_Ehdr *header); + #endif diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..33bae6d --- /dev/null +++ b/src/error.c @@ -0,0 +1,11 @@ +#include "ft_nm.h" +// XXX +#include + +void ft_nm_error(const char *path) { + char *str = malloc(8 + ft_strlen(path)); + ft_strcpy(str, "ft_nm: "); + ft_strcat(str, path); + perror(str); + free(str); +} diff --git a/src/header.c b/src/header.c new file mode 100644 index 0000000..6842820 --- /dev/null +++ b/src/header.c @@ -0,0 +1,26 @@ +#include "ft_nm.h" + +int check_magic_number(Elf64_Ehdr *header) { + if (ft_strncmp((void *)&header->e_ident[EI_MAG0], "\x7f""ELF", 4)) { + return FT_NM_FAILURE; + } + return FT_NM_SUCCESS; +} + +int check_format(Elf64_Ehdr *header) { + if (header->e_ident[EI_CLASS] == ELFCLASSNONE) { + printf("problem\n"); + return FT_NM_FAILURE; + } + return FT_NM_SUCCESS; +} + +int check_header(Elf64_Ehdr *header) { + if (check_magic_number(header) == FT_NM_FAILURE) { + return FT_NM_FAILURE; + } + if (check_format(header) == FT_NM_FAILURE) { + return FT_NM_FAILURE; + } + return FT_NM_SUCCESS; +} diff --git a/src/main.c b/src/main.c index 32e53a8..e0c6db4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,12 @@ #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) { @@ -7,17 +14,22 @@ int main(int ac, char **av) { path = av[1]; } int fd = open(path, O_RDONLY); - printf("Opening file %s with fd %d\n", path, fd); + 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; - write(1, ptr, 4); - write(1, "\n", 1); - write(1, header->e_ident, 4); - write(1, "\n", 1); + 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"); }