feat: 32bit
This commit is contained in:
parent
91510dc86f
commit
bf21c93f1f
1
Makefile
1
Makefile
|
@ -5,6 +5,7 @@ SRC_FILES = main.c \
|
|||
header.c \
|
||||
ident.c \
|
||||
nm.c \
|
||||
nm32.c \
|
||||
fetch.c \
|
||||
ft_printf.c \
|
||||
|
||||
|
|
|
@ -54,11 +54,17 @@ void ft_nm_error(const char *path);
|
|||
int check_ident(unsigned char ident[EI_NIDENT]);
|
||||
void *fetch(t_mapped_file mapped_file, size_t offset, size_t fetch_size);
|
||||
|
||||
int nm32(t_mapped_file mapped_file);
|
||||
int nm32(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering);
|
||||
int get_header32(t_mapped_file mapped_file, Elf32_Ehdr *header);
|
||||
|
||||
int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering);
|
||||
int get_header64(t_mapped_file mapped_file, Elf64_Ehdr *header);
|
||||
|
||||
int strcmp_nm(void *a, void *b);
|
||||
int reverse(void *a, void *b);
|
||||
int nosort(void *a, void *b);
|
||||
void put_entry(void *data);
|
||||
|
||||
char ft_get_hex_digit(char c);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -9,3 +9,12 @@ int get_header64(t_mapped_file mapped_file, Elf64_Ehdr *header) {
|
|||
return FT_NM_SUCCESS;
|
||||
}
|
||||
|
||||
int get_header32(t_mapped_file mapped_file, Elf32_Ehdr *header) {
|
||||
void *header_ptr = fetch(mapped_file, 0, 52);
|
||||
if (!header_ptr) {
|
||||
return FT_NM_FAILURE;
|
||||
}
|
||||
ft_memcpy(header, header_ptr, 52);
|
||||
return FT_NM_SUCCESS;
|
||||
}
|
||||
|
||||
|
|
|
@ -106,7 +106,7 @@ int nm(char *path, t_verbosity verbosity, t_ordering ordering) {
|
|||
return 0;
|
||||
}
|
||||
if (ident[EI_CLASS] == ELFCLASS32) {
|
||||
if (nm32(mapped_file) == FT_NM_FAILURE) {
|
||||
if (nm32(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
8
src/nm.c
8
src/nm.c
|
@ -225,12 +225,13 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
|
|||
free(sym_char);
|
||||
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 on fait if entry->verbosity >= verbosity, si c'est pas bon on skip toute la suite
|
||||
|
@ -359,11 +360,6 @@ void print_section(Elf64_Shdr sec) {
|
|||
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);
|
||||
|
|
|
@ -0,0 +1,272 @@
|
|||
#include "ft_nm.h"
|
||||
|
||||
char *get_sym_char32(Elf32_Sym sym, Elf32_Shdr sec, int *tool) {
|
||||
// if ((sec.sh_flags & SHF_COMPRESSED) == SHF_COMPRESSED) {
|
||||
// return ("N");
|
||||
// }
|
||||
*tool = 0;
|
||||
if (ELF32_ST_TYPE(sym.st_info) == STT_SECTION) {
|
||||
*tool = 1;
|
||||
}
|
||||
if (sym.st_shndx == SHN_ABS) {
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("a");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("A");
|
||||
}
|
||||
}
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_GNU_UNIQUE) {
|
||||
return ("u");
|
||||
}
|
||||
if (ELF32_ST_TYPE(sym.st_info) == STT_GNU_IFUNC) {
|
||||
return ("i");
|
||||
}
|
||||
if (sec.sh_flags & SHF_EXECINSTR) {
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("t");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("T");
|
||||
}
|
||||
}
|
||||
if (sec.sh_type == SHT_NOBITS) {
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("b");
|
||||
}
|
||||
else if (ELF32_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 (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("d");
|
||||
}
|
||||
else if (ELF32_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 (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("d");
|
||||
}
|
||||
else if (ELF32_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 (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("d");
|
||||
}
|
||||
else if (ELF32_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 (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("d");
|
||||
}
|
||||
else if (ELF32_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 (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("d");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("D");
|
||||
}
|
||||
}
|
||||
if (/*sec.sh_type == SHT_PROGBITS &&*/ sec.sh_flags & SHF_ALLOC) {
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("r");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("R");
|
||||
}
|
||||
}
|
||||
if (sec.sh_type == SHT_NOTE && sec.sh_flags == SHF_ALLOC) {
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("r");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("R");
|
||||
}
|
||||
}
|
||||
if (sym.st_shndx == SHN_COMMON) {
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("c");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("C");
|
||||
}
|
||||
}
|
||||
if (sym.st_shndx == SHN_UNDEF) {
|
||||
//return ("U");
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("u");
|
||||
}
|
||||
else if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL) {
|
||||
return ("U");
|
||||
}
|
||||
}
|
||||
if (ELF32_ST_BIND(sym.st_info) == STB_WEAK) {
|
||||
if (ELF32_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 (ELF32_ST_BIND(sym.st_info) == STB_WEAK) {
|
||||
if (sec.sh_type == SHT_NULL) {
|
||||
return ("w");
|
||||
}
|
||||
else {
|
||||
return ("W");
|
||||
}
|
||||
}
|
||||
return ("n");
|
||||
}
|
||||
int nm32(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) {
|
||||
t_root *tree = NULL;
|
||||
t_entry *entry = (t_entry *)malloc(sizeof(t_entry));
|
||||
int tool;
|
||||
if (!entry)
|
||||
return FT_NM_FAILURE;
|
||||
Elf32_Ehdr header;
|
||||
if (get_header32(mapped_file, &header) == FT_NM_FAILURE) {
|
||||
return FT_NM_FAILURE;
|
||||
}
|
||||
uint32_t addr = header.e_shoff + header.e_shentsize * header.e_shstrndx;
|
||||
Elf32_Shdr shstrtb;
|
||||
shstrtb = *(Elf32_Shdr *)fetch(mapped_file, addr, header.e_shentsize);
|
||||
for (int i = 0; i < header.e_shnum; i++) {
|
||||
uint32_t addr = header.e_shoff + header.e_shentsize * i;
|
||||
Elf32_Shdr sh;
|
||||
sh = *(Elf32_Shdr *)fetch(mapped_file, addr, header.e_shentsize);
|
||||
if (sh.sh_type == SHT_SYMTAB) {
|
||||
uint32_t addr2 = header.e_shoff + header.e_shentsize * sh.sh_link;
|
||||
Elf32_Shdr strtab;
|
||||
strtab = *(Elf32_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;
|
||||
}
|
||||
for (uint64_t j = sh.sh_entsize; j < sh.sh_size; j += sh.sh_entsize) {
|
||||
Elf32_Sym sym;
|
||||
Elf32_Shdr sec;
|
||||
sym = *(Elf32_Sym *)fetch(mapped_file, sh.sh_offset + j, sh.sh_entsize);
|
||||
if (sym.st_shndx < header.e_shnum) {
|
||||
sec = *(Elf32_Shdr *)fetch(mapped_file, header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
|
||||
}
|
||||
char *str;
|
||||
char *sec_str;
|
||||
Elf32_Shdr shdr;
|
||||
if (sym.st_shndx != SHN_ABS) {
|
||||
shdr = *(Elf32_Shdr *)fetch(mapped_file, header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
|
||||
sec_str = ft_strdup(mapped_file.ptr + shstrtb.sh_offset + shdr.sh_name);
|
||||
} else {
|
||||
sec_str = ft_strdup("");
|
||||
}
|
||||
if (sym.st_name) {
|
||||
str = ft_strdup(mapped_file.ptr + strtab.sh_offset + sym.st_name);
|
||||
}
|
||||
else {
|
||||
str = 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
|
||||
entry->symbol = ft_strdup(str);
|
||||
free(str);
|
||||
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")) {
|
||||
free(sym_char);
|
||||
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 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' && sym.st_info == 4) || sym_char[0] == 'N' || tool == 1)// || entry->symbol[0] == '.')
|
||||
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'))
|
||||
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 = 8 + 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[8] = ' ';
|
||||
entry->string[9] = sym_char[0];
|
||||
entry->string[10] = ' ';
|
||||
ft_memcpy(entry->string + 11, entry->symbol, ft_strlen(entry->symbol));
|
||||
if (sym.st_value && !ft_strstr(entry->symbol, "@@")) {
|
||||
for (char i = 7; i >= 0; i--) {
|
||||
entry->string[7 - i] = ft_get_hex_digit((sym.st_value >> i * 4) & 0xF);
|
||||
}
|
||||
} else {
|
||||
if (ft_strcmp(sym_char, "U") && ft_strcmp(sym_char, "w") && ft_strcmp(sym_char, "v")) {
|
||||
|
||||
for (char i = 7; i >= 0; i--) {
|
||||
entry->string[7 - i] = ft_get_hex_digit((sym.st_value >> i * 4) & 0xF);
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < 8; 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);
|
||||
break;
|
||||
case REVERSE:
|
||||
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &reverse);
|
||||
break;
|
||||
case DEFAULT_ORDERING:
|
||||
ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm);
|
||||
break;
|
||||
}
|
||||
// pas de bst oops :D
|
||||
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) {
|
||||
ft_putrbt(tree, &put_entry);
|
||||
} else {
|
||||
ft_printf("sss", "nm: ", path, ": no symbols\n");
|
||||
}
|
||||
return FT_NM_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue