wip: sections

This commit is contained in:
gbrochar 2024-03-25 11:06:09 +01:00
parent 2e0ed924d3
commit dac5ec5675
6 changed files with 93 additions and 23 deletions

View File

@ -35,6 +35,9 @@ WHITE = \033[0m
all: $(NAME) all: $(NAME)
dump: $(NAME)
hexdump -C ft_nm > aaa
$(NAME): create_libft $(OBJ) $(INC) $(NAME): create_libft $(OBJ) $(INC)
@$(CC) $(CFLAGS) -I $(INC_DIR) -c $(SRC) @$(CC) $(CFLAGS) -I $(INC_DIR) -c $(SRC)
@mv $(OBJ_FILES) $(OBJ_DIR) @mv $(OBJ_FILES) $(OBJ_DIR)

View File

@ -23,12 +23,13 @@ typedef struct s_mapped_file {
void ft_printf(const char *format, ...); void ft_printf(const char *format, ...);
void ft_nm_error(const char *path); void ft_nm_error(const char *path);
int check_header(Elf64_Ehdr *header);
int check_ident(unsigned char ident[EI_NIDENT]); int check_ident(unsigned char ident[EI_NIDENT]);
void *fetch(t_mapped_file mapped_file, size_t offset, size_t fetch_size); void *fetch(t_mapped_file mapped_file, size_t offset, size_t fetch_size);
void nm32(t_mapped_file mapped_file); int nm32(t_mapped_file mapped_file);
void nm64(t_mapped_file mapped_file);
int nm64(t_mapped_file mapped_file);
int get_header64(t_mapped_file mapped_file, Elf64_Ehdr *header);
#endif #endif

View File

@ -10,13 +10,13 @@ void ft_print_hex_digit(char c) {
void ft_put_address_32(uint32_t addr) { void ft_put_address_32(uint32_t addr) {
for (char i = 7; i >= 0; i--) { for (char i = 7; i >= 0; i--) {
ft_print_hex_digit((addr >> i) & 0xF); ft_print_hex_digit((addr >> i * 4) & 0xF);
} }
} }
void ft_put_address_64(uint64_t addr) { void ft_put_address_64(uint64_t addr) {
for (char i = 15; i >= 0; i--) { for (char i = 15; i >= 0; i--) {
ft_print_hex_digit((addr >> i) & 0xF); ft_print_hex_digit((addr >> i * 4) & 0xF);
} }
} }
@ -52,3 +52,4 @@ void ft_printf(const char *format, ...) {
} }
va_end(ap); va_end(ap);
} }

View File

@ -1,7 +1,11 @@
#include "ft_nm.h" #include "ft_nm.h"
int check_header(Elf64_Ehdr *header) { int get_header64(t_mapped_file mapped_file, Elf64_Ehdr *header) {
(void)header; void *header_ptr = fetch(mapped_file, 0, 64);
if (!header_ptr) {
return FT_NM_FAILURE;
}
ft_memcpy(header, header_ptr, 64);
return FT_NM_SUCCESS; return FT_NM_SUCCESS;
} }

View File

@ -2,22 +2,24 @@
int main(int ac, char **av) { int main(int ac, char **av) {
char *path = ft_strdup("a.out"); char *path = ft_strdup("a.out");
bool path_allocated = true;
if (ac > 1) { if (ac > 1) {
free(path); free(path);
path_allocated = false; path = ft_strdup(av[1]);
path = av[1];
} }
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd == -1) { if (fd == -1) {
ft_nm_error(path); ft_nm_error(path);
free(path);
exit (EXIT_FAILURE);
} }
struct stat stat; struct stat stat;
if (fstat(fd, &stat) == -1) { if (fstat(fd, &stat) == -1) {
ft_nm_error(path); ft_nm_error(path);
free(path);
exit (EXIT_FAILURE);
} }
t_mapped_file mapped_file; t_mapped_file mapped_file;
@ -30,26 +32,31 @@ int main(int ac, char **av) {
unsigned char ident[EI_NIDENT]; unsigned char ident[EI_NIDENT];
if (!ident_ptr) { if (!ident_ptr) {
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n"); ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
if (path_allocated) {
free(path); free(path);
} exit(EXIT_FAILURE);
exit(1);
} }
ft_memcpy(ident, ident_ptr, EI_NIDENT); ft_memcpy(ident, ident_ptr, EI_NIDENT);
if (check_ident(ident) == FT_NM_FAILURE) { if (check_ident(ident) == FT_NM_FAILURE) {
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n"); ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
if (path_allocated) {
free(path); free(path);
} exit(EXIT_FAILURE);
exit (1);
} }
if (ident[EI_CLASS] == ELFCLASS32) { if (ident[EI_CLASS] == ELFCLASS32) {
nm32(mapped_file); if (nm32(mapped_file) == FT_NM_FAILURE) {
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
free(path);
exit(EXIT_FAILURE);
}
} }
else if (ident[EI_CLASS] == ELFCLASS64) { else if (ident[EI_CLASS] == ELFCLASS64) {
nm64(mapped_file); if (nm64(mapped_file) == FT_NM_FAILURE) {
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
free(path);
exit(EXIT_FAILURE);
} }
} }
}
free(path);
return 0; return 0;
} }

View File

@ -1,9 +1,63 @@
#include "ft_nm.h" #include "ft_nm.h"
void nm32(t_mapped_file mapped_file) { int nm32(t_mapped_file mapped_file) {
(void)mapped_file; (void)mapped_file;
return FT_NM_SUCCESS;
} }
void nm64(t_mapped_file mapped_file) { int nm64(t_mapped_file mapped_file) {
(void)mapped_file; Elf64_Ehdr header;
if (get_header64(mapped_file, &header) == FT_NM_FAILURE) {
return FT_NM_FAILURE;
} }
printf("Header:\n");
printf("Version: %d\n", header.e_version);
printf("Entry point: %ld\n", header.e_entry);
printf("Program header offset: ");
fflush(stdout);
ft_printf("X", header.e_phoff);
ft_putchar('\n');
printf("Section header offset: ");
fflush(stdout);
ft_printf("X", header.e_shoff);
ft_putchar('\n');
printf("Header size: %d\n", header.e_ehsize);
printf("Program header entry size: %d\n", header.e_phentsize);
printf("Program header num: %d\n", header.e_phnum);
printf("Section header entry size: %d\n", header.e_shentsize);
printf("Section header num: %d\n", header.e_shnum);
printf("Section header string table index: %d\n", header.e_shstrndx);
/*
for (int i = 0; i < header.e_phnum; i++) {
uint64_t addr = header.e_phoff + header.e_phentsize * i;
Elf64_Phdr phstr;
ft_memcpy(&phstr, mapped_file.ptr + addr, header.e_phentsize);
ft_printf("X", phstr.p_offset);
ft_putchar('\n');
char *str = ft_strdup(mapped_file.ptr + phstr.p_offset + 1);
ft_putstr(str);
ft_putchar('\n');
}
*/
uint64_t addr = header.e_shoff + header.e_shentsize * header.e_shstrndx;
ft_printf("sX", "addr: ", addr);
Elf64_Shdr shstrtb;
ft_memcpy(&shstrtb, mapped_file.ptr + addr, header.e_shentsize);
ft_printf("sX", "offset: ", shstrtb.sh_offset);
for (int i = 0; i < header.e_shnum; i++) {
uint64_t addr = header.e_shoff + header.e_shentsize * i;
Elf64_Shdr sh;
ft_memcpy(&sh, mapped_file.ptr + addr, header.e_shentsize);
/*ft_printf("X", sh.sh_offset);
ft_putchar('\n');
ft_printf("X", sh.sh_name);
ft_putchar('\n');*/
char *str = ft_strdup(mapped_file.ptr + shstrtb.sh_offset + sh.sh_name);
//ft_putstr(str);
ft_printf("x x ss", sh.sh_name, sh.sh_offset, str, "\n");
free(str);
//ft_putchar('\n');
}
return FT_NM_SUCCESS;
}