wip: sections
This commit is contained in:
parent
2e0ed924d3
commit
dac5ec5675
3
Makefile
3
Makefile
|
@ -35,6 +35,9 @@ WHITE = \033[0m
|
|||
|
||||
all: $(NAME)
|
||||
|
||||
dump: $(NAME)
|
||||
hexdump -C ft_nm > aaa
|
||||
|
||||
$(NAME): create_libft $(OBJ) $(INC)
|
||||
@$(CC) $(CFLAGS) -I $(INC_DIR) -c $(SRC)
|
||||
@mv $(OBJ_FILES) $(OBJ_DIR)
|
||||
|
|
|
@ -23,12 +23,13 @@ typedef struct s_mapped_file {
|
|||
void ft_printf(const char *format, ...);
|
||||
void ft_nm_error(const char *path);
|
||||
|
||||
int check_header(Elf64_Ehdr *header);
|
||||
int check_ident(unsigned char ident[EI_NIDENT]);
|
||||
void *fetch(t_mapped_file mapped_file, size_t offset, size_t fetch_size);
|
||||
|
||||
void nm32(t_mapped_file mapped_file);
|
||||
void nm64(t_mapped_file mapped_file);
|
||||
int nm32(t_mapped_file mapped_file);
|
||||
|
||||
int nm64(t_mapped_file mapped_file);
|
||||
int get_header64(t_mapped_file mapped_file, Elf64_Ehdr *header);
|
||||
|
||||
#endif
|
||||
|
||||
|
|
|
@ -10,13 +10,13 @@ void ft_print_hex_digit(char c) {
|
|||
|
||||
void ft_put_address_32(uint32_t addr) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,11 @@
|
|||
#include "ft_nm.h"
|
||||
|
||||
int check_header(Elf64_Ehdr *header) {
|
||||
(void)header;
|
||||
int get_header64(t_mapped_file mapped_file, Elf64_Ehdr *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;
|
||||
}
|
||||
|
||||
|
|
33
src/main.c
33
src/main.c
|
@ -2,22 +2,24 @@
|
|||
|
||||
int main(int ac, char **av) {
|
||||
char *path = ft_strdup("a.out");
|
||||
bool path_allocated = true;
|
||||
|
||||
if (ac > 1) {
|
||||
free(path);
|
||||
path_allocated = false;
|
||||
path = av[1];
|
||||
path = ft_strdup(av[1]);
|
||||
}
|
||||
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
ft_nm_error(path);
|
||||
free(path);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
struct stat stat;
|
||||
if (fstat(fd, &stat) == -1) {
|
||||
ft_nm_error(path);
|
||||
free(path);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
t_mapped_file mapped_file;
|
||||
|
@ -30,26 +32,31 @@ int main(int ac, char **av) {
|
|||
unsigned char ident[EI_NIDENT];
|
||||
if (!ident_ptr) {
|
||||
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
|
||||
if (path_allocated) {
|
||||
free(path);
|
||||
}
|
||||
exit(1);
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_memcpy(ident, ident_ptr, EI_NIDENT);
|
||||
if (check_ident(ident) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
|
||||
if (path_allocated) {
|
||||
free(path);
|
||||
}
|
||||
exit (1);
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
60
src/nm.c
60
src/nm.c
|
@ -1,9 +1,63 @@
|
|||
#include "ft_nm.h"
|
||||
|
||||
void nm32(t_mapped_file mapped_file) {
|
||||
int nm32(t_mapped_file mapped_file) {
|
||||
(void)mapped_file;
|
||||
return FT_NM_SUCCESS;
|
||||
}
|
||||
|
||||
void nm64(t_mapped_file mapped_file) {
|
||||
(void)mapped_file;
|
||||
int nm64(t_mapped_file 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue