ft_nm/src/nm.c

322 lines
9.5 KiB
C
Raw Normal View History

2024-03-20 20:47:46 +00:00
#include "ft_nm.h"
2024-04-03 12:45:17 +00:00
void ppp(Elf64_Sym sym, Elf64_Shdr sec);
void print_section(Elf64_Shdr sec);
void print_symbol(Elf64_Sym sym);
2024-03-20 20:47:46 +00:00
2024-04-03 12:45:17 +00:00
char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec) {
2024-04-04 11:30:59 +00:00
// if ((sec.sh_flags & SHF_COMPRESSED) == SHF_COMPRESSED) {
// return ("N");
// }
2024-03-26 16:29:36 +00:00
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");
}
}
2024-04-04 11:30:59 +00:00
if (ELF64_ST_BIND(sym.st_info) == STB_GNU_UNIQUE) {
return ("u");
}
2024-04-03 12:45:17 +00:00
if (ELF64_ST_TYPE(sym.st_info) == STT_GNU_IFUNC) {
return ("i");
}
if (sec.sh_flags & SHF_EXECINSTR) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("t");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("T");
}
}
if (sec.sh_type == SHT_NOBITS) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("b");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("B");
}
}
if (sec.sh_type == SHT_PROGBITS && (sec.sh_flags & (SHF_ALLOC | SHF_WRITE)) == (SHF_ALLOC | SHF_WRITE)) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("d");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("D");
}
}
if (sec.sh_type == SHT_DYNAMIC && (sec.sh_flags & (SHF_ALLOC | SHF_WRITE)) == (SHF_ALLOC | SHF_WRITE)) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("d");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("D");
}
}
if (sec.sh_type == SHT_INIT_ARRAY && (sec.sh_flags & (SHF_ALLOC | SHF_WRITE)) == (SHF_ALLOC | SHF_WRITE)) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("d");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("D");
}
}
if (sec.sh_type == SHT_PREINIT_ARRAY && (sec.sh_flags & (SHF_ALLOC | SHF_WRITE)) == (SHF_ALLOC | SHF_WRITE)) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("d");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("D");
}
}
if (sec.sh_type == SHT_FINI_ARRAY && (sec.sh_flags & (SHF_ALLOC | SHF_WRITE)) == (SHF_ALLOC | SHF_WRITE)) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("d");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("D");
}
}
if (/*sec.sh_type == SHT_PROGBITS &&*/ sec.sh_flags & SHF_ALLOC) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("r");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("R");
}
}
if (sec.sh_type == SHT_NOTE && sec.sh_flags == SHF_ALLOC) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("r");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("R");
}
}
if (sym.st_shndx == SHN_COMMON) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("c");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("C");
}
}
if (sym.st_shndx == SHN_UNDEF) {
2024-10-14 19:56:32 +00:00
//return ("U");
2024-04-03 12:45:17 +00:00
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("u");
}
else if (ELF64_ST_BIND(sym.st_info) == STB_GLOBAL) {
return ("U");
}
}
if (ELF64_ST_BIND(sym.st_info) == STB_WEAK) {
if (ELF64_ST_TYPE(sym.st_info) == STT_OBJECT) {
if (sec.sh_type == SHT_NULL) {
return ("v");
}
else {
return ("V");
}
} else {
if (sec.sh_type == SHT_NULL) {
return ("w");
}
else {
return ("W");
}
}
}
if (ELF64_ST_BIND(sym.st_info) == STB_WEAK) {
if (sec.sh_type == SHT_NULL) {
return ("w");
}
else {
return ("W");
}
}
return ("n");
2024-03-26 16:29:36 +00:00
}
int strcmp_nm(void *a, void *b) {
t_entry *aa = (t_entry *)a;
t_entry *bb = (t_entry *)b;
char *s1 = aa->symbol;
char *s2 = bb->symbol;
2024-10-20 18:55:07 +00:00
return ft_strcmp(s1, s2);
2024-10-14 19:56:32 +00:00
}
int reverse(void *a, void *b) {
2024-10-14 19:56:32 +00:00
return strcmp_nm(b, a);
}
int nosort(void *a, void *b) {
2024-10-14 19:56:32 +00:00
(void)a;
(void)b;
return 1;
}
void put_entry(void *data) {
t_node *node = (t_node *)data;
t_entry *entry = (t_entry *)node->data;
ft_putstr(entry->string);
2024-10-14 19:56:32 +00:00
}
int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) {
t_root *tree = NULL;
2024-10-14 19:56:32 +00:00
t_entry *entry = (t_entry *)malloc(sizeof(t_entry));
if (!entry)
return FT_NM_FAILURE;
2024-03-25 10:06:09 +00:00
Elf64_Ehdr header;
if (get_header64(mapped_file, &header) == FT_NM_FAILURE) {
return FT_NM_FAILURE;
}
uint64_t addr = header.e_shoff + header.e_shentsize * header.e_shstrndx;
Elf64_Shdr shstrtb;
2024-10-14 19:56:32 +00:00
shstrtb = *(Elf64_Shdr *)fetch(mapped_file, addr, header.e_shentsize);
2024-03-25 10:06:09 +00:00
for (int i = 0; i < header.e_shnum; i++) {
uint64_t addr = header.e_shoff + header.e_shentsize * i;
Elf64_Shdr sh;
2024-10-14 19:56:32 +00:00
sh = *(Elf64_Shdr *)fetch(mapped_file, addr, header.e_shentsize);
2024-03-26 16:29:36 +00:00
if (sh.sh_type == SHT_SYMTAB) {
uint64_t addr2 = header.e_shoff + header.e_shentsize * sh.sh_link;
Elf64_Shdr strtab;
2024-10-14 19:56:32 +00:00
strtab = *(Elf64_Shdr *)fetch(mapped_file, addr2, header.e_shentsize);
char first = *(char *)fetch(mapped_file, strtab.sh_offset, 1);
char last = *(char *)fetch(mapped_file, strtab.sh_offset + strtab.sh_size - 1, 1);
if (first != '\0' || last != '\0') {
return FT_NM_FAILURE;
}
2024-04-03 12:45:17 +00:00
for (uint64_t j = sh.sh_entsize; j < sh.sh_size; j += sh.sh_entsize) {
2024-03-26 16:29:36 +00:00
Elf64_Sym sym;
2024-04-03 12:45:17 +00:00
Elf64_Shdr sec;
2024-10-14 19:56:32 +00:00
sym = *(Elf64_Sym *)fetch(mapped_file, sh.sh_offset + j, sh.sh_entsize);
2024-04-03 12:45:17 +00:00
if (sym.st_shndx < header.e_shnum) {
2024-10-14 19:56:32 +00:00
sec = *(Elf64_Shdr *)fetch(mapped_file, header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
2024-04-03 12:45:17 +00:00
}
char *str;
2024-04-04 11:30:59 +00:00
char *sec_str;
Elf64_Shdr shdr;
if (sym.st_shndx != SHN_ABS) {
2024-10-14 19:56:32 +00:00
shdr = *(Elf64_Shdr *)fetch(mapped_file, header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
2024-04-04 11:30:59 +00:00
sec_str = ft_strdup(mapped_file.ptr + shstrtb.sh_offset + shdr.sh_name);
} else {
sec_str = ft_strdup("");
}
2024-04-03 12:45:17 +00:00
if (sym.st_name) {
str = ft_strdup(mapped_file.ptr + strtab.sh_offset + sym.st_name);
}
else {
2024-04-04 11:30:59 +00:00
str = sec_str;
2024-04-03 12:45:17 +00:00
}
2024-10-14 19:56:32 +00:00
// ici la str (le symbole) est bon, on la charge dans le truc, ensuite on genere la vrai string a afficher, avant de sort
entry->symbol = ft_strdup(str);
free(str);
2024-04-03 12:45:17 +00:00
char *sym_char = ft_strdup(get_sym_char(sym, sec));
2024-04-04 11:30:59 +00:00
if (ft_strnstr(sec_str, ".debug", 6) && ft_strequ(sym_char, "n")) {
free(sym_char);
sym_char = ft_strdup("N");
}
2024-10-14 19:56:32 +00:00
// ici le sym char a ete calculer, a partir de la on determine la verbosity
// ici on fait if entry->verbosity >= verbosity, si c'est pas bon on skip toute la suite
entry->verbosity = DEFAULT_VERBOSITY;
if (sym_char[0] == 'a')
entry->verbosity = ALL;
if (sym_char[0] == 'u' || sym_char[0] == 'v' || sym_char[0] == 'w' || (sym_char[0] >= 'A' && sym_char[0] <= 'Z'))
entry->verbosity = GLOBAL;
if (sym_char[0] == 'w' || sym_char[0] == 'U')
entry->verbosity = UNDEFINED;
if (entry->verbosity >= verbosity) {
// ici on genere la string a afficher
// pad ' ' symchar ' ' symbol name \n \0
size_t entry_string_len = 16 + 1 + 1 + 1 + ft_strlen(entry->symbol) + 1 + 1;
entry->string = (char *)malloc(sizeof(char) * entry_string_len);
if (!entry->string)
return FT_NM_FAILURE;
entry->string[entry_string_len - 1] = '\0';
entry->string[entry_string_len - 2] = '\n';
entry->string[16] = ' ';
entry->string[17] = sym_char[0];
entry->string[18] = ' ';
ft_memcpy(entry->string + 19, entry->symbol, ft_strlen(entry->symbol));
if (sym.st_value) {
for (char i = 15; i >= 0; i--) {
entry->string[15 - i] = ft_get_hex_digit((sym.st_value >> i * 4) & 0xF);
}
2024-04-03 12:45:17 +00:00
} else {
2024-10-14 19:56:32 +00:00
if (ft_strcmp(sym_char, "U") && ft_strcmp(sym_char, "w") && ft_strcmp(sym_char, "v")) {
for (char i = 15; i >= 0; i--) {
entry->string[15 - i] = ft_get_hex_digit((sym.st_value >> i * 4) & 0xF);
}
} else {
for (int i = 0; i < 16; i++)
entry->string[i] = ' ';
}
}
free(sym_char);
// ensuite un switch ordering avec un ft_lstinsert (pointeur sur fonction serait plus opti mais osef)
switch (ordering) {
case NOSORT:
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &nosort);
2024-10-14 19:56:32 +00:00
break;
case REVERSE:
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &reverse);
2024-10-14 19:56:32 +00:00
break;
case DEFAULT_ORDERING:
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm);
2024-10-14 19:56:32 +00:00
break;
2024-04-03 12:45:17 +00:00
}
2024-10-14 19:56:32 +00:00
// pas de bst oops :D
fflush(stdout);
2024-03-26 16:29:36 +00:00
}
}
}
2024-03-25 10:06:09 +00:00
char *str = ft_strdup(mapped_file.ptr + shstrtb.sh_offset + sh.sh_name);
free(str);
}
2024-10-14 19:56:32 +00:00
// ici on affiche tout ou no symbols en cas de list vide
if (tree) {
ft_putrbt(tree, &put_entry);
} else {
ft_printf("sss", "nm: ", path, ": no symbols\n");
2024-10-14 19:56:32 +00:00
}
2024-03-25 10:06:09 +00:00
return FT_NM_SUCCESS;
2024-03-20 20:47:46 +00:00
}
2024-03-25 10:06:09 +00:00
2024-04-03 12:45:17 +00:00
void print_symbol(Elf64_Sym sym) {
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);
fflush(stdout);
}
void print_section(Elf64_Shdr sec) {
printf("sh_name : %d\n", sec.sh_name);
printf("sh_type : %d\n", sec.sh_type);
printf("sh_flags : %ld\n", sec.sh_flags);
// printf("sh_addr : %ld\n", sec.sh_addr);
// printf("sh_offset : %ld\n", sec.sh_offset);
// printf("sh_size : %ld\n", sec.sh_size);
printf("sh_link : %d\n", sec.sh_link);
printf("sh_info : %d\n", sec.sh_info);
// printf("sh_addralign : %ld\n", sec.sh_addralign);
// printf("sh_entsize : %ld\n", sec.sh_entsize);
fflush(stdout);
}
int nm32(t_mapped_file mapped_file) {
(void)mapped_file;
return FT_NM_SUCCESS;
}
void ppp(Elf64_Sym sym, Elf64_Shdr sec) {
print_symbol(sym);
print_section(sec);
}