Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tristan functions #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 63 additions & 8 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down
20 changes: 14 additions & 6 deletions src/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#include <gelf.h>

/**
* 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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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) {
Expand Down