From ef40671b34d114ba3d77b33c44db978f6ddae9ee Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 2 May 2024 10:31:00 +0200 Subject: [PATCH] Add Tristan functions Print entry point address Print section name and size --- src/main.c | 71 +++++++++++++++++++++++++++++++++++++++++++++------ src/options.c | 20 ++++++++++----- 2 files changed, 77 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index df01c0f..0f7f41a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,23 +6,68 @@ #include "../include/options.h" +typedef struct Elf64_Ehdr Elf64_Ehdr; + +/** + * + * + * @param content + * @authors Maxence Puhetini, Tristant Salé + */ +void check_ELF(const char *content) { + const char elf_magic[] = {0x7F, 'E', 'L', 'F'}; + if (memcmp(content, elf_magic, sizeof(elf_magic)) != 0) { + printf("not an elf, gimme an elf please\n"); + exit(EXIT_FAILURE); + } + printf("That's and elf\n"); +} + // Nombre d'options disponibles // TODO: Ajouter un count de options[] const int NB_OPTIONS = 7; // Définition du tableau d'options Option options[] = { - {"-d", print_text_section_hex, 1}, - {"-s", print_section_count, 1}, - {"-a", print_section_name_size, 1}, - {"-e", print_entrypoint_address_program, 1}, - {"-t", option_t, 1}, - {"-l", option_l, 1}, - {"-h", option_h, 0} + {"-d", (const char *) print_text_section_hex, 1}, + {"-s", (const char *) print_section_count, 1}, + {"-a", (const char *) print_section_name_size, 1}, + {"-e", (const char *) print_entrypoint_address_program, 1}, + {"-t", (const char *) option_t, 1}, + {"-l", (const char *) option_l, 1}, + {"-h", option_h, 0} }; +char* copy_file_to_memory(const char *file_to_open) { + FILE *file = fopen(file_to_open, "rb"); + if (!file) { + perror("Erreur: impossible de lire le fichier"); + return NULL; + } + + char *buffer = NULL; + long file_size; + fseek(file, 0, SEEK_END); + file_size = ftell(file); + rewind(file); + buffer = (char *)malloc(file_size + 1); + if (buffer == NULL) { + perror("Erreur lors de l'allocation mémoire"); + fclose(file); + return "1"; + } + if (fread(buffer, 1, file_size, file) != file_size) { + perror("Erreur lors de la lecture du fichier"); + free(buffer); + fclose(file); + return "1"; + } + buffer[file_size] = '\0'; + fclose(file); + return buffer; +} int main(int argc, char *argv[]) { // Vérifier que le nom du fichier ELF a été fourni @@ -35,6 +80,16 @@ int main(int argc, char *argv[]) { // Stocker le nom du fichier ELF char *filename = argv[1]; + char* content = copy_file_to_memory(argv[1]); + // printf("Contenu du fichier :\n%100x\n", content); + print_basic_info(argv[1]); + check_ELF(content); + + Elf64_Ehdr *elf_header = malloc(sizeof(Elf64_Ehdr)); + memcpy(elf_header, content, sizeof(Elf64_Ehdr)); + free(content); + free(elf_header); + // Si aucun argument autre que le nom du fichier n'est passé, afficher les informations standards du fichier ELF if (argc == 2) { print_basic_info(filename); @@ -57,7 +112,7 @@ int main(int argc, char *argv[]) { if (!option_found) { // Si l'argument ne correspond à aucune option, afficher un message d'erreur et quitter le programme - printf("Erreur : option invalide '%s'\n", argv[i]); + printf("Erreur: option invalide '%s'\n", argv[i]); option_h(); return 1; } diff --git a/src/options.c b/src/options.c index d4d1c22..2149d38 100644 --- a/src/options.c +++ b/src/options.c @@ -6,14 +6,14 @@ #include /** - * Prints the .text section in hexadecimal + * Prints the .text sectio n in hexadecimal * This function is called when the user passes the -d argument * * @param content * @author Merieme Yaaqobi */ void print_text_section_hex(const char *content) { - int fd = open(nom_fichier, O_RDONLY); + int fd = open(content, O_RDONLY); Elf *elf; Elf_Scn *scn = NULL; GElf_Shdr shdr; @@ -60,7 +60,8 @@ void print_text_section_hex(const char *content) { * @author Merieme Yaaqobi */ void print_section_count(const char *content) { - int fd = open(nom_fichier, O_RDONLY); + int fd = open(content, O_RDONLY); + Elf *elf; size_t shnum; if (elf_version(EV_CURRENT) == EV_NONE) { @@ -89,7 +90,14 @@ void print_section_count(const char *content) { * @author Tristan Salé */ void print_section_name_size(const char *content) { - printf("Option -a appelée\n"); + Elf64_Shdr *section_headers = (Elf64_Shdr *)(content + elf_header.e_shoff); + char *section_names = (char *)(content + section_headers[elf_header.e_shstrndx].sh_offset); + + printf("Sections:\n"); + for (int i = 0; i < elf_header.e_shnum; i++) { + printf("Section %d: %s\n", i, section_names + section_headers[i].sh_name); + } + printf("Il y a %d sections dans ce fichier ELF\n", elf_header.e_shnum); } /** @@ -99,8 +107,8 @@ void print_section_name_size(const char *content) { * @param content * @author Tristan Salé */ -void print_entrypoint_address_program(const char *content) { - printf("Option -e appelée\n"); +void print_entrypoint_address_program(const char *content, Elf64_Ehdr elf_header) { + printf("Entrypoint address: 0x%lx\n", elf_header.e_entry); } void option_t(const char *content) {