From 65a9713d15865bb7e46c6362eb7bca657339c19e Mon Sep 17 00:00:00 2001 From: gbrochar Date: Tue, 26 Mar 2024 17:29:36 +0100 Subject: [PATCH] wip: symbols --- src/nm.c | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/nm.c b/src/nm.c index 3c49a45..8c0502a 100644 --- a/src/nm.c +++ b/src/nm.c @@ -5,6 +5,18 @@ int nm32(t_mapped_file mapped_file) { return FT_NM_SUCCESS; } +char *get_sym_char(Elf64_Sym sym) { + if (sym.st_shndx == SHN_ABS) { + if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) { + return ("a"); + } + else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) { + return ("A"); + } + } + return ("T"); +} + int nm64(t_mapped_file mapped_file) { Elf64_Ehdr header; if (get_header64(mapped_file, &header) == FT_NM_FAILURE) { @@ -38,23 +50,47 @@ int nm64(t_mapped_file mapped_file) { ft_putstr(str); ft_putchar('\n'); } - */ + */ uint64_t addr = header.e_shoff + header.e_shentsize * header.e_shstrndx; - ft_printf("sX", "addr: ", addr); + //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); + //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); + if (sh.sh_type == SHT_SYMTAB) { + uint64_t addr2 = header.e_shoff + header.e_shentsize * sh.sh_link; + Elf64_Shdr strtab; + ft_memcpy(&strtab, mapped_file.ptr + addr2, header.e_shentsize); + ft_printf("sdsds", "SYMTAB Size: ", sh.sh_size, " Entity size: ", sh.sh_entsize, "\n"); + for (uint64_t j = 0; j < sh.sh_size; j += sh.sh_entsize) { + Elf64_Sym sym; + ft_memcpy(&sym, mapped_file.ptr + sh.sh_offset + j, sh.sh_entsize); + char *str = ft_strdup(mapped_file.ptr + strtab.sh_offset + sym.st_name); + //if (str[ft_strlen(str) - 1] == 'c') { + printf("st_name: %d\n", sym.st_name); + printf("st_info: %d\n", sym.st_info); + printf("st_other: %d\n", sym.st_other); + printf("st_shndx: %d\n", sym.st_shndx); + printf("st_value: %ld\n", sym.st_value); + printf("st_size: %ld\n", sym.st_size); + //} + if (sym.st_value) { + ft_printf("X s ss", sym.st_value, get_sym_char(sym), str, "\n"); + } else { + ft_printf("P s ss", get_sym_char(sym), mapped_file.ptr + strtab.sh_offset + sym.st_name, "\n"); + } + } + } /*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"); + ft_printf("x x x ss", sh.sh_name, sh.sh_offset, addr, str, "\n"); free(str); //ft_putchar('\n'); }