fix leaks and clean

This commit is contained in:
gbrochar 2024-10-25 14:47:21 +02:00
parent df05b33dce
commit 7983e97be3
7 changed files with 68 additions and 135 deletions

View File

@ -8,6 +8,7 @@ SRC_FILES = main.c \
nm32.c \ nm32.c \
fetch.c \ fetch.c \
ft_printf.c \ ft_printf.c \
tree.c \
INC_FILES = ft_nm.h \ INC_FILES = ft_nm.h \

View File

@ -64,6 +64,7 @@ int strcmp_nm(void *a, void *b);
int reverse(void *a, void *b); int reverse(void *a, void *b);
int nosort(void *a, void *b); int nosort(void *a, void *b);
void put_entry(void *data); void put_entry(void *data);
void free_entry(void *data);
char ft_get_hex_digit(char c); char ft_get_hex_digit(char c);

2
libft

@ -1 +1 @@
Subproject commit e846b98fcda5878a1d4d9de8f422dec09b97456c Subproject commit cf6bb620300d5f003f28a23733e43b057370b906

View File

@ -57,7 +57,6 @@ int parse_options(char *options, t_env *env, bool *use_options) {
int parse_arguments(int ac, char **av, t_env *env) { int parse_arguments(int ac, char **av, t_env *env) {
bool use_options = true; bool use_options = true;
if (ac > 0) { if (ac > 0) {
env->bin_name = ft_strdup(av[0]);
env->bin_name = ft_strdup("nm"); env->bin_name = ft_strdup("nm");
} }
for (int i = 1; i < ac; i++) { for (int i = 1; i < ac; i++) {
@ -84,6 +83,7 @@ int nm(char *path, t_verbosity verbosity, t_ordering ordering) {
} }
struct stat stat; struct stat stat;
ft_bzero(&stat, sizeof(stat));
if (fstat(fd, &stat) == -1) { if (fstat(fd, &stat) == -1) {
ft_nm_error(path); ft_nm_error(path);
} }
@ -97,23 +97,27 @@ int nm(char *path, t_verbosity verbosity, t_ordering ordering) {
unsigned char ident[EI_NIDENT]; unsigned char ident[EI_NIDENT];
if (!ident_ptr) { if (!ident_ptr) {
ft_printf("sss", "nm: ", path, ": file format not recognized\n"); ft_printf("sss", "nm: ", path, ": file format not recognized\n");
munmap(mapped_file.ptr, mapped_file.size);
return 0; return 0;
} }
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", "nm: ", path, ": file format not recognized\n"); ft_printf("sss", "nm: ", path, ": file format not recognized\n");
munmap(mapped_file.ptr, mapped_file.size);
return 0; return 0;
} }
if (ident[EI_CLASS] == ELFCLASS32) { if (ident[EI_CLASS] == ELFCLASS32) {
if (nm32(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) { if (nm32(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) {
ft_printf("sss", "nm: ", path, ": file format not recognized\n"); ft_printf("sss", "nm: ", path, ": file format not recognized\n");
munmap(mapped_file.ptr, mapped_file.size);
return 0; return 0;
} }
} }
else if (ident[EI_CLASS] == ELFCLASS64) { else if (ident[EI_CLASS] == ELFCLASS64) {
if (nm64(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) { if (nm64(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) {
ft_printf("sss", "nm: ", path, ": file format not recognized\n"); ft_printf("sss", "nm: ", path, ": file format not recognized\n");
munmap(mapped_file.ptr, mapped_file.size);
return 0; return 0;
} }
} }
@ -121,6 +125,17 @@ int nm(char *path, t_verbosity verbosity, t_ordering ordering) {
return 0; return 0;
} }
void lst_free_func(void *content, size_t content_size) {
(void)content_size;
free(content);
}
void free_env(t_env env) {
ft_lstdel(&(env.list), &lst_free_func);
if (env.bin_name)
free(env.bin_name);
}
int main(int ac, char **av) { int main(int ac, char **av) {
t_env env; t_env env;
env.verbosity = DEFAULT_VERBOSITY; env.verbosity = DEFAULT_VERBOSITY;
@ -129,20 +144,24 @@ int main(int ac, char **av) {
env.list = NULL; env.list = NULL;
int error = parse_arguments(ac, av, &env); int error = parse_arguments(ac, av, &env);
if (env.list_len == 0) { if (env.list_len == 0) {
env.list = ft_lstnew(ft_strdup("a.out"), 6); env.list = ft_lstnew("a.out", 6);
env.list_len = 1; env.list_len = 1;
} }
if (error) { if (error) {
free_env(env);
return error; return error;
} }
if (env.list_len == 1) { if (env.list_len == 1) {
nm(env.list->content, env.verbosity, env.ordering); nm(env.list->content, env.verbosity, env.ordering);
free_env(env);
return 0; return 0;
} }
while (env.list) { t_list *tmp = env.list;
while (tmp) {
ft_printf("\ns:\n", env.list->content); ft_printf("\ns:\n", env.list->content);
nm(env.list->content, env.verbosity, env.ordering); nm(env.list->content, env.verbosity, env.ordering);
env.list = env.list->next; tmp = tmp->next;
} }
free_env(env);
return 0; return 0;
} }

121
src/nm.c
View File

@ -1,13 +1,6 @@
#include "ft_nm.h" #include "ft_nm.h"
void ppp(Elf64_Sym sym, Elf64_Shdr sec);
void print_section(Elf64_Shdr sec);
void print_symbol(Elf64_Sym sym);
char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) { char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) {
// if ((sec.sh_flags & SHF_COMPRESSED) == SHF_COMPRESSED) {
// return ("N");
// }
*tool = 0; *tool = 0;
if (ELF64_ST_TYPE(sym.st_info) == STT_SECTION) { if (ELF64_ST_TYPE(sym.st_info) == STT_SECTION) {
*tool = 1; *tool = 1;
@ -82,7 +75,7 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) {
return ("D"); return ("D");
} }
} }
if (/*sec.sh_type == SHT_PROGBITS &&*/ sec.sh_flags & SHF_ALLOC) { if (sec.sh_flags & SHF_ALLOC) {
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) { if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("r"); return ("r");
} }
@ -107,7 +100,6 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) {
} }
} }
if (sym.st_shndx == SHN_UNDEF) { if (sym.st_shndx == SHN_UNDEF) {
//return ("U");
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) { if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("u"); return ("u");
} }
@ -143,30 +135,6 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) {
return ("n"); return ("n");
} }
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;
return ft_strcmp(s1, s2);
}
int reverse(void *a, void *b) {
return strcmp_nm(b, a);
}
int nosort(void *a, void *b) {
(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);
}
int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) { int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) {
t_root *tree = NULL; t_root *tree = NULL;
t_entry *entry = (t_entry *)malloc(sizeof(t_entry)); t_entry *entry = (t_entry *)malloc(sizeof(t_entry));
@ -215,28 +183,21 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
else { else {
str = sec_str; str = sec_str;
} }
if (str != sec_str)
free(sec_str);
// ici la str (le symbole) est bon, on la charge dans le truc, ensuite on genere la vrai string a afficher, avant de sort // 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); entry->symbol = ft_strdup(str);
free(str); free(str);
char *sym_char = ft_strdup(get_sym_char(sym, sec, &tool)); char *sym_char = ft_strdup(get_sym_char(sym, sec, &tool));
//if (entry->symbol[0] == '.' || ft_strnstr(entry->symbol, "lpstub", 6)) {
if (ft_strnstr(entry->symbol, ".debug", 6) && ft_strequ(sym_char, "n")) { if (ft_strnstr(entry->symbol, ".debug", 6) && ft_strequ(sym_char, "n")) {
free(sym_char); free(sym_char);
sym_char = ft_strdup("N"); sym_char = ft_strdup("N");
} }
/*
if (sym_char[0] == 'N') {
ft_putstr(entry->symbol);
ft_putchar(*sym_char);
ft_putchar('\n');
print_symbol(sym);
}*/
// ici le sym char a ete calculer, a partir de la on determine la verbosity // 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 // ici on fait if entry->verbosity >= verbosity, si c'est pas bon on skip toute la suite
entry->verbosity = DEFAULT_VERBOSITY; entry->verbosity = DEFAULT_VERBOSITY;
if ((sym_char[0] == 'a' && sym.st_info == 4) || sym_char[0] == 'N' || tool == 1)// || entry->symbol[0] == '.') if ((sym_char[0] == 'a' && sym.st_info == 4) || sym_char[0] == 'N' || tool == 1)
entry->verbosity = ALL; entry->verbosity = ALL;
if (sym_char[0] == 'u' || sym_char[0] == 'v' || sym_char[0] == 'w' || (sym_char[0] >= 'A' && sym_char[0] <= 'Z' && sym_char[0] != 'N')) if (sym_char[0] == 'u' || sym_char[0] == 'v' || sym_char[0] == 'w' || (sym_char[0] >= 'A' && sym_char[0] <= 'Z' && sym_char[0] != 'N'))
entry->verbosity = GLOBAL; entry->verbosity = GLOBAL;
@ -283,85 +244,21 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm); ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm);
break; break;
} }
// pas de bst oops :D
fflush(stdout); fflush(stdout);
} else {
free(entry->symbol);
free(sym_char);
} }
} }
} }
char *str = ft_strdup(mapped_file.ptr + shstrtb.sh_offset + sh.sh_name);
free(str);
} }
free(entry);
// ici on affiche tout ou no symbols en cas de list vide // ici on affiche tout ou no symbols en cas de list vide
if (tree) { if (tree) {
ft_putrbt(tree, &put_entry); ft_putrbt(tree, &put_entry);
ft_delrbt(tree, &free_entry);
} else { } else {
ft_printf("sss", "nm: ", path, ": no symbols\n"); ft_printf("sss", "nm: ", path, ": no symbols\n");
} }
return FT_NM_SUCCESS; return FT_NM_SUCCESS;
} }
void print_st_info(unsigned char st_info) {
unsigned char type = ELF64_ST_TYPE(st_info);
unsigned char bind = ELF64_ST_BIND(st_info);
if (type == STT_NOTYPE) {
ft_putstr("symbol has no type (STT_NOTYPE)\n");
} else if (type == STT_OBJECT) {
ft_putstr("symbol has object type (STT_OBJECT)\n");
} else if (type == STT_FUNC) {
ft_putstr("symbol has func type (STT_FUNC)\n");
} else if (type == STT_SECTION) {
ft_putstr("symbol has section type (STT_SECTION)\n");
} else if (type == STT_FILE) {
ft_putstr("symbol has file type (STT_FILE)\n");
} else if (type >= STT_LOPROC && type <= STT_HIPROC) {
ft_putnbr(type);
ft_putstr(" reserved for processor-specific semantics (STT_LOPROC <= type <= STT_HIPROC)\n");
} else {
ft_putstr("symbol TYPE unknown (warning: no matching STT_****)\n");
}
if (bind == STB_LOCAL) {
ft_putstr("symbol has local bind (STB_LOCAL)\n");
} else if (bind == STB_GLOBAL) {
ft_putstr("symbol has global bind (STB_GLOBAL)\n");
} else if (bind == STB_WEAK) {
ft_putstr("symbol has weak bind (STB_WEAK)\n");
} else if (bind >= STB_LOPROC && bind <= STB_HIPROC) {
ft_putnbr(bind);
ft_putstr(" reserved for processor-specific semantics (STB_LOPROC <= bind <= STB_HIPROC)\n");
} else {
ft_putstr("symbol BIND unknown (warning: no matching STB_****)\n");
}
}
void print_symbol(Elf64_Sym sym) {
printf("st_name: %d\n", sym.st_name);
printf("st_info: %d\n", sym.st_info);
print_st_info(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);
}
void ppp(Elf64_Sym sym, Elf64_Shdr sec) {
print_symbol(sym);
print_section(sec);
}

View File

@ -1,9 +1,6 @@
#include "ft_nm.h" #include "ft_nm.h"
char *get_sym_char32(Elf32_Sym sym, Elf32_Shdr sec, int *tool) { char *get_sym_char32(Elf32_Sym sym, Elf32_Shdr sec, int *tool) {
// if ((sec.sh_flags & SHF_COMPRESSED) == SHF_COMPRESSED) {
// return ("N");
// }
*tool = 0; *tool = 0;
if (ELF32_ST_TYPE(sym.st_info) == STT_SECTION) { if (ELF32_ST_TYPE(sym.st_info) == STT_SECTION) {
*tool = 1; *tool = 1;
@ -78,7 +75,7 @@ char *get_sym_char32(Elf32_Sym sym, Elf32_Shdr sec, int *tool) {
return ("D"); return ("D");
} }
} }
if (/*sec.sh_type == SHT_PROGBITS &&*/ sec.sh_flags & SHF_ALLOC) { if (sec.sh_flags & SHF_ALLOC) {
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) { if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("r"); return ("r");
} }
@ -103,7 +100,6 @@ char *get_sym_char32(Elf32_Sym sym, Elf32_Shdr sec, int *tool) {
} }
} }
if (sym.st_shndx == SHN_UNDEF) { if (sym.st_shndx == SHN_UNDEF) {
//return ("U");
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) { if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
return ("u"); return ("u");
} }
@ -190,19 +186,10 @@ int nm32(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
entry->symbol = ft_strdup(str); entry->symbol = ft_strdup(str);
free(str); free(str);
char *sym_char = ft_strdup(get_sym_char32(sym, sec, &tool)); char *sym_char = ft_strdup(get_sym_char32(sym, sec, &tool));
//if (entry->symbol[0] == '.' || ft_strnstr(entry->symbol, "lpstub", 6)) {
if (ft_strnstr(entry->symbol, ".debug", 6) && ft_strequ(sym_char, "n")) { if (ft_strnstr(entry->symbol, ".debug", 6) && ft_strequ(sym_char, "n")) {
free(sym_char); free(sym_char);
sym_char = ft_strdup("N"); sym_char = ft_strdup("N");
} }
/*
if (sym_char[0] == 'N') {
ft_putstr(entry->symbol);
ft_putchar(*sym_char);
ft_putchar('\n');
print_symbol(sym);
}*/
// ici le sym char a ete calculer, a partir de la on determine la verbosity // 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 // ici on fait if entry->verbosity >= verbosity, si c'est pas bon on skip toute la suite
@ -254,15 +241,11 @@ int nm32(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm); ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm);
break; break;
} }
// pas de bst oops :D
fflush(stdout); fflush(stdout);
} }
} }
} }
char *str = ft_strdup(mapped_file.ptr + shstrtb.sh_offset + sh.sh_name);
free(str);
} }
// ici on affiche tout ou no symbols en cas de list vide
if (tree) { if (tree) {
ft_putrbt(tree, &put_entry); ft_putrbt(tree, &put_entry);
} else { } else {

32
src/tree.c Normal file
View File

@ -0,0 +1,32 @@
#include "ft_nm.h"
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;
return ft_strcmp(s1, s2);
}
int reverse(void *a, void *b) {
return strcmp_nm(b, a);
}
int nosort(void *a, void *b) {
(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);
}
void free_entry(void *data) {
t_node *node = (t_node *)data;
t_entry *entry = (t_entry *)node->data;
free(entry->string);
free(entry->symbol);
}