Compare commits
No commits in common. "bf21c93f1f70b32eeb9f3498023db9f528a0a814" and "3ee3777174b26c77a41fb3b6912fa00ccc2bc7d6" have entirely different histories.
bf21c93f1f
...
3ee3777174
|
@ -1,8 +1,5 @@
|
|||
# ft_nm
|
||||
out.all
|
||||
ft_nm
|
||||
a
|
||||
b
|
||||
|
||||
# libft
|
||||
inc/libft.h
|
||||
|
|
3
Makefile
3
Makefile
|
@ -5,7 +5,6 @@ SRC_FILES = main.c \
|
|||
header.c \
|
||||
ident.c \
|
||||
nm.c \
|
||||
nm32.c \
|
||||
fetch.c \
|
||||
ft_printf.c \
|
||||
|
||||
|
@ -23,7 +22,7 @@ OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
|
|||
|
||||
CC = gcc
|
||||
|
||||
CFLAGS = -Wall -Werror -Wextra -g3
|
||||
CFLAGS = -Wall -Werror -Wextra
|
||||
|
||||
LIB_NAME = libft
|
||||
|
||||
|
|
40
inc/ft_nm.h
40
inc/ft_nm.h
|
@ -15,34 +15,6 @@
|
|||
# define FT_NM_SUCCESS 0
|
||||
# define FT_NM_FAILURE -1
|
||||
|
||||
typedef enum e_verbosity {
|
||||
ALL,
|
||||
DEFAULT_VERBOSITY,
|
||||
GLOBAL,
|
||||
UNDEFINED
|
||||
} t_verbosity;
|
||||
|
||||
typedef enum e_ordering {
|
||||
DEFAULT_ORDERING,
|
||||
REVERSE,
|
||||
NOSORT
|
||||
} t_ordering;
|
||||
|
||||
typedef struct s_entry {
|
||||
t_verbosity verbosity;
|
||||
char *string;
|
||||
char *symbol;
|
||||
} t_entry;
|
||||
|
||||
typedef struct s_env {
|
||||
char *bin_name;
|
||||
t_verbosity verbosity;
|
||||
t_ordering ordering;
|
||||
t_list *list;
|
||||
int list_len;
|
||||
bool has_multiple_files;
|
||||
} t_env;
|
||||
|
||||
typedef struct s_mapped_file {
|
||||
void *ptr;
|
||||
off_t size;
|
||||
|
@ -54,18 +26,10 @@ 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, char *path, t_verbosity verbosity, t_ordering ordering);
|
||||
int get_header32(t_mapped_file mapped_file, Elf32_Ehdr *header);
|
||||
int nm32(t_mapped_file mapped_file);
|
||||
|
||||
int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering);
|
||||
int nm64(t_mapped_file mapped_file);
|
||||
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
|
||||
|
||||
|
|
2
libft
2
libft
|
@ -1 +1 @@
|
|||
Subproject commit e846b98fcda5878a1d4d9de8f422dec09b97456c
|
||||
Subproject commit 1ab0cdf1acf97db0cb8d90940c96a6f1ccb3a7af
|
|
@ -1,13 +1,5 @@
|
|||
#include "ft_nm.h"
|
||||
|
||||
char ft_get_hex_digit(char c) {
|
||||
if (c > 9) {
|
||||
return c + 'W';
|
||||
} else {
|
||||
return c + '0';
|
||||
}
|
||||
}
|
||||
|
||||
void ft_print_hex_digit(char c) {
|
||||
if (c > 9) {
|
||||
ft_putchar(c + 'W');
|
||||
|
@ -56,9 +48,6 @@ void ft_printf(const char *format, ...) {
|
|||
case 'P':
|
||||
ft_putstr(" ");
|
||||
break;
|
||||
default:
|
||||
ft_putchar(format[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
va_end(ap);
|
||||
|
|
|
@ -9,12 +9,3 @@ 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;
|
||||
}
|
||||
|
||||
|
|
136
src/main.c
136
src/main.c
|
@ -1,148 +1,62 @@
|
|||
#include "ft_nm.h"
|
||||
|
||||
void help(char *bin_name) {
|
||||
ft_printf("sss", "Usage: ", bin_name, " [option(s)] [file(s)]\n");
|
||||
ft_putstr(
|
||||
" List symbols in [file(s)] (a.out by default).\n"
|
||||
" The options are:\n"
|
||||
" -a Display debugger-only symbols\n"
|
||||
" -g Display only external symbols\n"
|
||||
" -p Do not sort the symbols\n"
|
||||
" -r Reverse the sense of the sort\n"
|
||||
" -u Display only undefined symbols\n"
|
||||
" -h Display this information\n");
|
||||
ft_printf("ss", bin_name, ": supported targets: elf64-x86-64 elf32-x86-64\n");
|
||||
ft_putstr("Report bugs to gaetanbrochard@protonmail.com\n");
|
||||
}
|
||||
int main(int ac, char **av) {
|
||||
char *path = ft_strdup("a.out");
|
||||
|
||||
int parse_options(char *options, t_env *env, bool *use_options) {
|
||||
int len = ft_strlen(options);
|
||||
if (len == 2 && options[1] == '-') {
|
||||
*use_options = false;
|
||||
return 0;
|
||||
if (ac > 1) {
|
||||
free(path);
|
||||
path = ft_strdup(av[1]);
|
||||
}
|
||||
for (int i = 1; i < len; i++) {
|
||||
switch (options[i]) {
|
||||
case 'h':
|
||||
help(env->bin_name);
|
||||
return -2;
|
||||
break;
|
||||
case 'a':
|
||||
if (env->verbosity < ALL || env->verbosity == DEFAULT_VERBOSITY)
|
||||
env->verbosity = ALL;
|
||||
break;
|
||||
case 'g':
|
||||
if (env->verbosity < GLOBAL)
|
||||
env->verbosity = GLOBAL;
|
||||
break;
|
||||
case 'u':
|
||||
if (env->verbosity < UNDEFINED)
|
||||
env->verbosity = UNDEFINED;
|
||||
break;
|
||||
case 'r':
|
||||
if (env->ordering < REVERSE)
|
||||
env->ordering = REVERSE;
|
||||
break;
|
||||
case 'p':
|
||||
if (env->ordering < NOSORT)
|
||||
env->ordering = NOSORT;
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int parse_arguments(int ac, char **av, t_env *env) {
|
||||
bool use_options = true;
|
||||
if (ac > 0) {
|
||||
env->bin_name = ft_strdup(av[0]);
|
||||
env->bin_name = ft_strdup("nm");
|
||||
}
|
||||
for (int i = 1; i < ac; i++) {
|
||||
if (ft_strlen(av[i]) > 0 && av[i][0] == '-' && use_options) {
|
||||
int ret = parse_options(av[i], env, &use_options);
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
} else {
|
||||
if (env->list_len == 0) {
|
||||
env->list = ft_lstnew(av[i], ft_strlen(av[i]) + 1);
|
||||
} else {
|
||||
ft_lstadd_back(&env->list, ft_lstnew(av[i], ft_strlen(av[i]) + 1));
|
||||
}
|
||||
env->list_len++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nm(char *path, t_verbosity verbosity, t_ordering ordering) {
|
||||
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;
|
||||
|
||||
// ft_printf("sds", "file size: ", stat.st_size, "\n");
|
||||
mapped_file.ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
mapped_file.size = stat.st_size;
|
||||
if (mapped_file.ptr != MAP_FAILED) {
|
||||
void *ident_ptr = fetch(mapped_file, 0, EI_NIDENT);
|
||||
unsigned char ident[EI_NIDENT];
|
||||
if (!ident_ptr) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
return 0;
|
||||
|
||||
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_memcpy(ident, ident_ptr, EI_NIDENT);
|
||||
if (check_ident(ident) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
return 0;
|
||||
ft_printf("sss", "ft_nm: ", path, ": file format not recognized\n");
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (ident[EI_CLASS] == ELFCLASS32) {
|
||||
if (nm32(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
return 0;
|
||||
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) {
|
||||
if (nm64(mapped_file, path, verbosity, ordering) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
return 0;
|
||||
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;
|
||||
}
|
||||
|
||||
int main(int ac, char **av) {
|
||||
t_env env;
|
||||
env.verbosity = DEFAULT_VERBOSITY;
|
||||
env.ordering = DEFAULT_ORDERING;
|
||||
env.list_len = 0;
|
||||
env.list = NULL;
|
||||
int error = parse_arguments(ac, av, &env);
|
||||
if (env.list_len == 0) {
|
||||
env.list = ft_lstnew(ft_strdup("a.out"), 6);
|
||||
env.list_len = 1;
|
||||
}
|
||||
if (error) {
|
||||
return error;
|
||||
}
|
||||
if (env.list_len == 1) {
|
||||
nm(env.list->content, env.verbosity, env.ordering);
|
||||
return 0;
|
||||
}
|
||||
while (env.list) {
|
||||
ft_printf("\ns:\n", env.list->content);
|
||||
nm(env.list->content, env.verbosity, env.ordering);
|
||||
env.list = env.list->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
226
src/nm.c
226
src/nm.c
|
@ -4,14 +4,11 @@ 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) {
|
||||
// ppp(sym, sec);
|
||||
// if ((sec.sh_flags & SHF_COMPRESSED) == SHF_COMPRESSED) {
|
||||
// return ("N");
|
||||
// }
|
||||
*tool = 0;
|
||||
if (ELF64_ST_TYPE(sym.st_info) == STT_SECTION) {
|
||||
*tool = 1;
|
||||
}
|
||||
if (sym.st_shndx == SHN_ABS) {
|
||||
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("a");
|
||||
|
@ -107,7 +104,7 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) {
|
|||
}
|
||||
}
|
||||
if (sym.st_shndx == SHN_UNDEF) {
|
||||
//return ("U");
|
||||
//return ("U");
|
||||
if (ELF64_ST_BIND(sym.st_info) == STB_LOCAL) {
|
||||
return ("u");
|
||||
}
|
||||
|
@ -140,71 +137,73 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec, int *tool) {
|
|||
return ("W");
|
||||
}
|
||||
}
|
||||
// if section name starts with .debug....
|
||||
// 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) {
|
||||
t_root *tree = NULL;
|
||||
t_entry *entry = (t_entry *)malloc(sizeof(t_entry));
|
||||
int tool;
|
||||
if (!entry)
|
||||
return FT_NM_FAILURE;
|
||||
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;
|
||||
shstrtb = *(Elf64_Shdr *)fetch(mapped_file, addr, header.e_shentsize);
|
||||
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;
|
||||
sh = *(Elf64_Shdr *)fetch(mapped_file, addr, header.e_shentsize);
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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 = sh.sh_entsize; j < sh.sh_size; j += sh.sh_entsize) {
|
||||
Elf64_Sym sym;
|
||||
Elf64_Shdr sec;
|
||||
sym = *(Elf64_Sym *)fetch(mapped_file, sh.sh_offset + j, sh.sh_entsize);
|
||||
ft_memcpy(&sym, mapped_file.ptr + sh.sh_offset + j, sh.sh_entsize);
|
||||
// printf("st_shndx: %d\n", sym.st_shndx);
|
||||
if (sym.st_shndx < header.e_shnum) {
|
||||
sec = *(Elf64_Shdr *)fetch(mapped_file, header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
|
||||
ft_memcpy(&sec, mapped_file.ptr + header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
|
||||
}
|
||||
char *str;
|
||||
char *sec_str;
|
||||
Elf64_Shdr shdr;
|
||||
if (sym.st_shndx != SHN_ABS) {
|
||||
shdr = *(Elf64_Shdr *)fetch(mapped_file, header.e_shoff + header.e_shentsize * sym.st_shndx, header.e_shentsize);
|
||||
ft_memcpy(&shdr, mapped_file.ptr + 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("");
|
||||
|
@ -215,130 +214,44 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
|
|||
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_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 (str[ft_strlen(str) - 1] == 'c') {
|
||||
/*
|
||||
*/
|
||||
//}
|
||||
char *sym_char = ft_strdup(get_sym_char(sym, sec));
|
||||
if (ft_strnstr(sec_str, ".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 = 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 && !ft_strstr(entry->symbol, "@@")) {
|
||||
for (char i = 15; i >= 0; i--) {
|
||||
entry->string[15 - i] = ft_get_hex_digit((sym.st_value >> i * 4) & 0xF);
|
||||
}
|
||||
if (sym.st_value) {
|
||||
ft_printf("X s ss", sym.st_value, sym_char, str, "\n");
|
||||
} else {
|
||||
if (ft_strcmp(sym_char, "U") && ft_strcmp(sym_char, "w") && ft_strcmp(sym_char, "v")) {
|
||||
ft_printf("X s ss", sym.st_value, sym_char, str, "\n");
|
||||
} else {
|
||||
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] = ' ';
|
||||
}
|
||||
//ft_printf("P s ss", sym_char, mapped_file.ptr + strtab.sh_offset + sym.st_name, "\n");
|
||||
ft_printf("P s ss", sym_char, str, "\n");
|
||||
}
|
||||
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);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
/*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 x ss", sh.sh_name, sh.sh_offset, addr, str, "\n");
|
||||
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");
|
||||
//ft_putchar('\n');
|
||||
}
|
||||
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);
|
||||
|
@ -360,6 +273,11 @@ 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);
|
||||
|
|
272
src/nm32.c
272
src/nm32.c
|
@ -1,272 +0,0 @@
|
|||
#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;
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
#include "ft_nm.h"
|
||||
|
||||
int main(int ac, char **av) {
|
||||
char *path = ft_strdup("a.out");
|
||||
|
||||
if (ac > 1) {
|
||||
free(path);
|
||||
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;
|
||||
|
||||
// ft_printf("sds", "file size: ", stat.st_size, "\n");
|
||||
mapped_file.ptr = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
||||
mapped_file.size = stat.st_size;
|
||||
if (mapped_file.ptr != MAP_FAILED) {
|
||||
void *ident_ptr = fetch(mapped_file, 0, EI_NIDENT);
|
||||
unsigned char ident[EI_NIDENT];
|
||||
if (!ident_ptr) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
ft_memcpy(ident, ident_ptr, EI_NIDENT);
|
||||
if (check_ident(ident) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (ident[EI_CLASS] == ELFCLASS32) {
|
||||
if (nm32(mapped_file) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
else if (ident[EI_CLASS] == ELFCLASS64) {
|
||||
if (nm64(mapped_file) == FT_NM_FAILURE) {
|
||||
ft_printf("sss", "nm: ", path, ": file format not recognized\n");
|
||||
free(path);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
free(path);
|
||||
return 0;
|
||||
}
|
||||
|
10
unit_test.sh
10
unit_test.sh
|
@ -1,10 +0,0 @@
|
|||
#!/bin/bash
|
||||
for file in $1*/**/*
|
||||
do
|
||||
if [[ "$file" != *.a ]] ;then
|
||||
echo $file
|
||||
./ft_nm $2 $file 2>&1 | cat > a
|
||||
nm $2 $file 2>&1 | cat > b
|
||||
diff a b
|
||||
fi
|
||||
done
|
Loading…
Reference in New Issue