feat: check ident

This commit is contained in:
gbrochar 2024-03-19 11:22:37 +01:00
parent e21df48de1
commit e08b2e8c84
6 changed files with 64 additions and 31 deletions

View File

@ -3,6 +3,7 @@ NAME = ft_nm
SRC_FILES = main.c \
error.c \
header.c \
ident.c \
INC_FILES = ft_nm.h \
@ -34,12 +35,12 @@ all: $(NAME)
$(NAME): create_libft $(OBJ) $(INC)
@$(CC) $(CFLAGS) -I $(INC_DIR) -c $(SRC)
@mv $(OBJ_FILES) $(OBJ_DIR)
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME) ./$(LIB_NAME)/libft.a
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME) ./$(LIB_NAME)/libft.a
@echo "$(GREEN)[OK]$(WHITE) $(NAME)"
create_libft:
@git submodule update --init
@make -C $(LIB_NAME)
@make -C $(LIB_NAME)
@cp $(LIB_NAME)/libft.h inc/libft.h
$(OBJ_DIR)%.o: $(SRC_DIR)%.c

View File

@ -14,5 +14,7 @@
void ft_nm_error(const char *path);
int check_header(Elf64_Ehdr *header);
int check_ident(unsigned char ident[EI_NIDENT]);
#endif

View File

@ -1,6 +1,4 @@
#include "ft_nm.h"
// XXX
#include <string.h>
void ft_nm_error(const char *path) {
char *str = malloc(8 + ft_strlen(path));
@ -9,3 +7,4 @@ void ft_nm_error(const char *path) {
perror(str);
free(str);
}

View File

@ -1,26 +1,7 @@
#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;
}
(void)header;
return FT_NM_SUCCESS;
}

47
src/ident.c Normal file
View File

@ -0,0 +1,47 @@
#include "ft_nm.h"
int check_magic_number(const char *magic_number) {
if (ft_strncmp(magic_number, "\x7f""ELF", 4)) {
return FT_NM_FAILURE;
}
return FT_NM_SUCCESS;
}
int check_arch(unsigned char arch) {
if (arch != ELFCLASS64 && arch != ELFCLASS32) {
return FT_NM_FAILURE;
}
return FT_NM_SUCCESS;
}
int check_data_format(unsigned char data_format) {
if (data_format != ELFDATA2MSB && data_format != ELFDATA2LSB) {
return FT_NM_FAILURE;
}
return FT_NM_SUCCESS;
}
int check_version(unsigned char version) {
if (version != EV_CURRENT) {
return FT_NM_FAILURE;
}
return FT_NM_SUCCESS;
}
int check_ident(unsigned char ident[EI_NIDENT]) {
if (check_magic_number((const char *)&ident[EI_MAG0])
== FT_NM_FAILURE) {
return FT_NM_FAILURE;
}
if (check_arch(ident[EI_CLASS]) == FT_NM_FAILURE) {
return FT_NM_FAILURE;
}
if (check_data_format(ident[EI_DATA]) == FT_NM_FAILURE) {
return FT_NM_FAILURE;
}
if (check_version(ident[EI_VERSION]) == FT_NM_FAILURE) {
return FT_NM_FAILURE;
}
return FT_NM_SUCCESS;
}

View File

@ -18,19 +18,20 @@ int main(int ac, char **av) {
ft_nm_error(path);
}
void *ptr = NULL;
Elf64_Ehdr *header;
// Elf64_Ehdr *header;
unsigned char ident[EI_NIDENT];
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) {
ft_memcpy(ident, ptr, EI_NIDENT);
//header = (Elf64_Ehdr *)ptr;
if (check_ident(ident) == FT_NM_FAILURE) {
printf("ft_nm: %s: file format not recognized\n", path);
exit (1);
}
for (int i = 0; i < 64; i++) {
for (int i = 0; i < 16; i++) {
print_bits(((char *)ptr)[i]);
}
if (header->e_ident[EI_OSABI] == 0x00) {
/* if (header->e_ident[EI_OSABI] == 0x00) {
printf("This ELF is for System V! :)\n");
}
else if (header->e_ident[EI_OSABI] == 0x03) {
@ -38,7 +39,9 @@ int main(int ac, char **av) {
} else {
printf("What's my purpose :(\n");
}
*/
}
return 0;
}