From fcc8dc3536888464ff2d18e95c4c27cd5c5aae2d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 00:12:59 +0100 Subject: [PATCH] some refactoring, separated dumping from fetching --- nm-otool/Makefile | 4 ++- nm-otool/includes/ft_nm_otool.h | 9 +++-- nm-otool/src/fetch_header.c | 2 +- nm-otool/src/ft_nm.c | 40 ++------------------- nm-otool/src/get_symbols.c | 48 ++++++++----------------- nm-otool/src/sym_dump.c | 63 ++++++++++++++++++++++++++++++++ nm-otool/src/sym_format.c | 32 +++++++++++++++++ nm-otool/src/sym_get.c | 64 +++++++++++++++++++++++++++++++++ nm-otool/src/symprint.c | 31 ---------------- 9 files changed, 186 insertions(+), 107 deletions(-) create mode 100644 nm-otool/src/sym_dump.c create mode 100644 nm-otool/src/sym_format.c create mode 100644 nm-otool/src/sym_get.c delete mode 100644 nm-otool/src/symprint.c diff --git a/nm-otool/Makefile b/nm-otool/Makefile index 560bd4a4..c4f21564 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -40,7 +40,9 @@ ft_nm.c\ ft_otool.c\ get_symbols.c\ hexdump.c\ -symprint.c +sym_dump.c\ +sym_format.c\ +sym_get.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index 885acdd7..c6a1f516 100644 --- a/nm-otool/includes/ft_nm_otool.h +++ b/nm-otool/includes/ft_nm_otool.h @@ -69,11 +69,14 @@ struct s_symbolmap extern t_symbolmap g_symbolmap[]; -t_list *get_symbols(char *file); +t_list *sym_get(char *file); int fetch_header(t_machodata *data, void *file); -int symprint(t_symbol *symbol); -int symprint_text(t_symbol *symbol); +int sym_format(t_symbol *symbol); +int sym_format_text(t_symbolmap map, t_symbol *symbol); + +void dump_symtab(struct symtab_command *symtab, void *file); +void dump_dysymtab(struct dysymtab_command *dysymtab, void *file); void *hexdump(void *addr, unsigned int offset, unsigned int size); diff --git a/nm-otool/src/fetch_header.c b/nm-otool/src/fetch_header.c index c2e2e1d5..8534d1f6 100644 --- a/nm-otool/src/fetch_header.c +++ b/nm-otool/src/fetch_header.c @@ -59,7 +59,7 @@ int fetch_header(t_machodata *data, void *file) if (is_64) fetch_machheader64(data, file); - if (is_fat) + else if (is_fat) fetch_fatheader(data, file); else ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); diff --git a/nm-otool/src/ft_nm.c b/nm-otool/src/ft_nm.c index 027c1970..d2994067 100644 --- a/nm-otool/src/ft_nm.c +++ b/nm-otool/src/ft_nm.c @@ -12,48 +12,12 @@ #include "ft_nm_otool.h" -t_symbolmap g_symbolmap[] = -{ - {'U', NULL}, - {'A', NULL}, - {'T', symprint_text}, - {'D', NULL}, - {'B', NULL}, - {'C', NULL}, - {'-', NULL}, - {'S', NULL}, - {'I', NULL}, -}; - -void dump_section_64(struct section_64 *sect) -{ - ft_printf("section64: segname=[%s], sectname=[%s]\n", - sect->segname, sect->sectname); -} - -void dump_segment_64(struct segment_command_64 *seg, void *file) -{ - uint32_t nsects; - uint32_t i; - struct section_64 *sect; - - (void)file; - nsects = seg->nsects; - ft_printf("LC_SEGMENT_64: segname=[%s], nsects=[%i]\n", seg->segname, nsects); - sect = (void*)(seg + 1); - for (i = 0; i < nsects; i++) - { - dump_section_64(sect); - sect = sect + 1; - } -} - void nm(void *file) { t_list *symbols; - symbols = get_symbols(file); - ft_lstiter(symbols, symprint); + symbols = sym_get(file); + ft_lstiter(symbols, sym_format); } int main(int ac, char **av) diff --git a/nm-otool/src/get_symbols.c b/nm-otool/src/get_symbols.c index e6f1d862..0d085e35 100644 --- a/nm-otool/src/get_symbols.c +++ b/nm-otool/src/get_symbols.c @@ -12,21 +12,20 @@ #include "ft_nm_otool.h" -static t_flag get_symtype(uint32_t i, struct dysymtab_command *dysym) +static t_flag symtype(uint32_t i, struct dysymtab_command *dysym) { - if (dysym->ilocalsym <= i && i <= dysym->ilocalsym + dysym->nlocalsym) + if (dysym->ilocalsym <= i && i < dysym->ilocalsym + dysym->nlocalsym) return (SYM_DEBUG); - if (dysym->iextdefsym <= i && i <= dysym->iextdefsym + dysym->nextdefsym) + if (dysym->iextdefsym <= i && i < dysym->iextdefsym + dysym->nextdefsym) return (SYM_TEXT); - if (dysym->iundefsym <= i && i <= dysym->iundefsym + dysym->nundefsym) + if (dysym->iundefsym <= i && i < dysym->iundefsym + dysym->nundefsym) return (SYM_UNDEF); return (0); } -t_list *get_symbols(char *file) +t_list *sym_get(char *file) { int i; - int nsyms; char *stringtable; char *string; struct nlist_64 *array; @@ -36,41 +35,24 @@ t_list *get_symbols(char *file) t_list *symbols; fetch_header(&data, file); - nsyms = data.symtab->nsyms; - array = (struct nlist_64*)(file + data.symtab->symoff); stringtable = file + data.symtab->stroff; - ft_printf("LC_SYMTAB [%d] symbols:\n", data.symtab->nsyms); - i = -1; symbols = NULL; - while (++i < nsyms) + + dump_symtab(data.symtab, file); + dump_dysymtab(data.dysymtab, file); + /* return (NULL); */ + i = -1; + array = (struct nlist_64*)(file + data.symtab->symoff); + while (++i < (int)data.symtab->nsyms) { sym.name = stringtable + array[i].n_un.n_strx; - sym.type = get_symtype(i, data.dysymtab); + sym.type = symtype(i, data.dysymtab); sym.value = 0; - ft_lstadd(&symbols, ft_lstnew(&sym, sizeof(sym))); + ft_lsteadd(&symbols, ft_lstnew(&sym, sizeof(sym))); } return (symbols); - ft_putendl(""); - - ft_printf("ilocalsym %i\n", data.dysymtab->ilocalsym); - ft_printf("nlocalsym %i\n", data.dysymtab->nlocalsym); - - ft_printf("iextdefsym %i\n", data.dysymtab->iextdefsym); - ft_printf("nextdefsym %i\n", data.dysymtab->nextdefsym); - - ft_printf("iundefsym %i\n", data.dysymtab->iundefsym); - ft_printf("nundefsym %i\n", data.dysymtab->nundefsym); - - ft_printf("LC_DYSYMTAB ntoc=[%d]\n", data.dysymtab->ntoc); - ft_printf("LC_DYSYMTAB nmodtab=[%d]\n", data.dysymtab->nmodtab); - ft_printf("LC_DYSYMTAB nextrefsyms=[%d]\n", data.dysymtab->nextrefsyms); - ft_printf("LC_DYSYMTAB nmodtab=[%d]\n", data.dysymtab->nmodtab); - ft_printf("LC_DYSYMTAB nindirectsyms=[%d]\n", data.dysymtab->nindirectsyms); - ft_printf("LC_DYSYMTAB nextrel=[%d]\n", data.dysymtab->nextrel); - ft_printf("LC_DYSYMTAB nlocrel=[%d]\n", data.dysymtab->nlocrel); - ft_putendl(""); - ref = (struct dylib_reference *)(file + data.dysymtab->indirectsymoff); i = -1; + ref = (struct dylib_reference *)(file + data.dysymtab->indirectsymoff); while (++i < (int)data.dysymtab->nindirectsyms) { string = stringtable + array[ref->isym].n_un.n_strx; diff --git a/nm-otool/src/sym_dump.c b/nm-otool/src/sym_dump.c new file mode 100644 index 00000000..6eceae59 --- /dev/null +++ b/nm-otool/src/sym_dump.c @@ -0,0 +1,63 @@ +#include "ft_nm_otool.h" + +void dump_section_64(struct section_64 *sect) +{ + ft_printf("{blu}{inv}struct section_64{eoc}: segname=[%s], sectname=[%s]\n", + sect->segname, sect->sectname); +} + +void dump_segment_64(struct segment_command_64 *seg, void *file) +{ + uint32_t nsects; + uint32_t i; + struct section_64 *sect; + + (void)file; + nsects = seg->nsects; + ft_printf("{blu}{inv}struct segment_command_64{eoc}: segname=[%s], nsects=[%i]\n", seg->segname, nsects); + sect = (void*)(seg + 1); + for (i = 0; i < nsects; i++) + { + dump_section_64(sect); + sect = sect + 1; + } +} + +void dump_symtab(struct symtab_command *symtab, void *file) +{ + int i; + char *stringtable; + char *string; + struct nlist_64 *array; + + ft_printf("{blu}{inv}struct symtab_command{eoc}\n"); + stringtable = file + symtab->stroff; + array = (struct nlist_64*)(file + symtab->symoff); + i = -1; + while (++i < (int)symtab->nsyms) + { + string = stringtable + array[i].n_un.n_strx; + ft_printf("%s\n", string); + } +} + +void dump_dysymtab(struct dysymtab_command *dysymtab, void *file) +{ + (void)file; + ft_printf("{blu}{inv}struct dysymtab_command{eoc}\n"); + ft_printf("ilocalsym %i\n", dysymtab->ilocalsym); + ft_printf("nlocalsym %i\n", dysymtab->nlocalsym); + ft_printf("iextdefsym %i\n", dysymtab->iextdefsym); + ft_printf("nextdefsym %i\n", dysymtab->nextdefsym); + ft_printf("iundefsym %i\n", dysymtab->iundefsym); + ft_printf("nundefsym %i\n", dysymtab->nundefsym); + ft_printf("---------------\n"); + ft_printf("ntoc %i\n", dysymtab->ntoc); + ft_printf("nmodtab %i\n", dysymtab->nmodtab); + ft_printf("nextrefsyms %i\n", dysymtab->nextrefsyms); + ft_printf("nmodtab %i\n", dysymtab->nmodtab); + ft_printf("nindirectsims %i\n", dysymtab->nindirectsyms); + ft_printf("nextrel %i\n", dysymtab->nextrel); + ft_printf("nlocrel %i\n", dysymtab->nlocrel); + ft_putendl(""); +} diff --git a/nm-otool/src/sym_format.c b/nm-otool/src/sym_format.c new file mode 100644 index 00000000..ba347248 --- /dev/null +++ b/nm-otool/src/sym_format.c @@ -0,0 +1,32 @@ +#include "ft_nm_otool.h" + +t_symbolmap g_symbolmap[] = +{ + {'U', NULL}, + {'A', NULL}, + {'T', sym_format_text}, + {'D', NULL}, + {'B', NULL}, + {'C', NULL}, + {'-', NULL}, + {'S', NULL}, + {'I', NULL}, +}; + +int sym_format_text(t_symbolmap map, t_symbol *symbol) +{ + ft_printf("%016x %c %s\n", symbol->value, map.c, symbol->name); + return (0); +} + +int sym_format(t_symbol *symbol) +{ + t_symbolmap map; + + map = g_symbolmap[symbol->type]; + if (map.format) + map.format(map, symbol); + else + ft_printf("%16s %c %s\n", " ", map.c, symbol->name); + return (0); +} diff --git a/nm-otool/src/sym_get.c b/nm-otool/src/sym_get.c new file mode 100644 index 00000000..f3f321e2 --- /dev/null +++ b/nm-otool/src/sym_get.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* dump_symtab.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/20 13:20:22 by jhalford #+# #+# */ +/* Updated: 2017/03/23 16:49:03 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_nm_otool.h" + +static t_flag get_symtype(uint32_t i, struct dysymtab_command *dysym) +{ + if (dysym->ilocalsym <= i && i <= dysym->ilocalsym + dysym->nlocalsym) + return (SYM_DEBUG); + if (dysym->iextdefsym <= i && i <= dysym->iextdefsym + dysym->nextdefsym) + return (SYM_TEXT); + if (dysym->iundefsym <= i && i <= dysym->iundefsym + dysym->nundefsym) + return (SYM_UNDEF); + return (0); +} + +t_list *get_symbols(char *file) +{ + int i; + int nsyms; + char *stringtable; + char *string; + struct nlist_64 *array; + struct dylib_reference *ref; + t_symbol sym; + t_machodata data; + t_list *symbols; + + fetch_header(&data, file); + nsyms = data.symtab->nsyms; + array = (struct nlist_64*)(file + data.symtab->symoff); + stringtable = file + data.symtab->stroff; + ft_printf("LC_SYMTAB [%d] symbols:\n", data.symtab->nsyms); + i = -1; + symbols = NULL; + while (++i < nsyms) + { + sym.name = stringtable + array[i].n_un.n_strx; + sym.type = get_symtype(i, data.dysymtab); + sym.value = 0; + ft_lstadd(&symbols, ft_lstnew(&sym, sizeof(sym))); + } + return (symbols); + ft_putendl(""); + + ref = (struct dylib_reference *)(file + data.dysymtab->indirectsymoff); + i = -1; + while (++i < (int)data.dysymtab->nindirectsyms) + { + string = stringtable + array[ref->isym].n_un.n_strx; + ft_printf("%i: %s,%#x\n", ref->isym, string, ref->flags); + ref += 1; + } + (void)file; +} diff --git a/nm-otool/src/symprint.c b/nm-otool/src/symprint.c deleted file mode 100644 index a76ad6c5..00000000 --- a/nm-otool/src/symprint.c +++ /dev/null @@ -1,31 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* symprint.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/23 16:05:55 by jhalford #+# #+# */ -/* Updated: 2017/03/23 16:59:36 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_nm_otool.h" - -int symprint_text(t_symbol *symbol) -{ - ft_printf("%16x %c %s\n", " ", symbol->value, symbol->name); - return (0); -} - -int symprint(t_symbol *symbol) -{ - t_symbolmap map; - - map = g_symbolmap[symbol->type]; - if (map.format) - map.format(symbol); - else - ft_printf("%16s %c %s\n", " ", map.c, symbol->name); - return (0); -}