From 8caadf0df6f11fcf4b9f357374d317c023b9dc1c Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 29 Mar 2017 16:39:47 +0200 Subject: [PATCH] filtering options --- nm-otool/.gitignore | 1 + nm-otool/includes/ft_nm_otool.h | 15 ++++--- nm-otool/src/ft_nm.c | 21 ++++++---- nm-otool/src/symbol_filter.c | 23 +++++++++-- nm-otool/src/symbol_format.c | 70 +++++++++++++++++++++++++-------- 5 files changed, 97 insertions(+), 33 deletions(-) diff --git a/nm-otool/.gitignore b/nm-otool/.gitignore index 851c8c8e..4b26ffcc 100644 --- a/nm-otool/.gitignore +++ b/nm-otool/.gitignore @@ -1,2 +1,3 @@ ft_nm ft_otool +tests diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index f8630674..348dc0da 100644 --- a/nm-otool/includes/ft_nm_otool.h +++ b/nm-otool/includes/ft_nm_otool.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */ -/* Updated: 2017/03/27 20:59:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/28 20:35:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,11 +41,16 @@ ** filtering flags */ # define NM_ALL (1 << 4) +# define NM_NO_LOCAL (1 << 5) +# define NM_NO_UNDF (1 << 6) +# define NM_ONLY_UNDF (1 << 7) /* ** formating flags */ -# define NM_FULL (1 << 5) +# define NM_FULL (1 << 8) +# define NM_OFORMAT (1 << 9) +# define NM_MFORMAT (1 << 10) typedef t_data_template t_nmdata; typedef enum e_symtype t_symtype; @@ -105,10 +110,10 @@ int symbol_sort(t_list **syms, t_flag flag); int symbol_filter(t_list **syms, t_flag flag); void symbol_free(void *data, size_t size); -int symbol_format(t_symbol *symbol); +int symbol_format(t_symbol *symbol, t_nmdata *data); +int sym_format_undf(t_symbol *symbol, t_nmdata *data); +int sym_format_stab(t_symbol *symbol, t_nmdata *data); int symbol_format_full(t_symbol *symbol); -int sym_format_undf(t_symbolmap map, t_symbol *symbol); -int sym_format_stab(t_symbolmap map, t_symbol *symbol); void mach_64_parse(t_machodata *data); void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab); diff --git a/nm-otool/src/ft_nm.c b/nm-otool/src/ft_nm.c index 56391a19..0bbb2c4e 100644 --- a/nm-otool/src/ft_nm.c +++ b/nm-otool/src/ft_nm.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/03/27 20:59:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/28 20:48:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,13 +22,14 @@ t_cliopts g_nm_opts[] = {'r', NULL, NM_RSORT, 0, NULL, 0}, {0xff, "full", NM_FULL, 0, NULL, 0}, - {'g', NULL, 0, 0, NULL, 0}, - {'u', NULL, 0, 0, NULL, 0}, {'a', NULL, NM_ALL, 0, NULL, 0}, - {'U', NULL, 0, 0, NULL, 0}, - {'o', NULL, 0, 0, NULL, 0}, + {'g', NULL, NM_NO_LOCAL, 0, NULL, 0}, + {'u', NULL, NM_ONLY_UNDF, 0, NULL, 0}, + {'U', NULL, NM_NO_UNDF, 0, NULL, 0}, + + {'o', NULL, NM_OFORMAT, 0, NULL, 0}, {'A', NULL, 0, 0, NULL, 0}, - {'m', NULL, 0, 0, NULL, 0}, + {'m', NULL, NM_MFORMAT, 0, NULL, 0}, {'x', NULL, 0, 0, NULL, 0}, {'j', NULL, 0, 0, NULL, 0}, }; @@ -44,11 +45,13 @@ void mach_64_dump(struct mach_header_64 *file, t_nmdata *data) mach_64_parse(&mach); symbol_sort(&mach.symbols, data->flag); symbol_filter(&mach.symbols, data->flag); - if (data->flag & NM_FULL) + if (data->flag & NM_MFORMAT) + format = symbol_format_m; + else if (data->flag & NM_FULL) format = symbol_format_full; else format = symbol_format; - ft_lstiter(mach.symbols, format); + ft_lstiter(mach.symbols, format, data); } int nm(void *file, t_nmdata *data) @@ -85,6 +88,8 @@ int main(int ac, char **av) i = data.av_data - av; while (i < ac && av[i]) { + if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) + ft_printf("%c%s:\n", i - (data.av_data - av) ? '\n' : 0, av[i]); if ((fd = open(av[i], O_RDONLY)) < 0) return (1); if ((fstat(fd, &buf)) < 0) diff --git a/nm-otool/src/symbol_filter.c b/nm-otool/src/symbol_filter.c index b5fa99f6..f87bb5c1 100644 --- a/nm-otool/src/symbol_filter.c +++ b/nm-otool/src/symbol_filter.c @@ -1,13 +1,12 @@ -/* - ************************************************************************** */ +/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* symbol_filter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/27 03:23:32 by jhalford #+# #+# */ -/* Updated: 2017/03/27 03:23:32 by jhalford ### ########.fr */ +/* Created: 2017/03/28 20:34:36 by jhalford #+# #+# */ +/* Updated: 2017/03/28 20:35:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,11 +17,27 @@ int cmp_symtype(t_symbol *sym, void *type) return (sym->type - *(t_type*)type); } +int cmp_no_symtype(t_symbol *sym, void *type) +{ + return (!(sym->type - *(t_type*)type)); +} + +int mask_nlisttype(t_symbol *sym, void *type) +{ + return (sym->nlist->n_type & *(t_type*)type); +} + int symbol_filter(t_list **symbols, t_flag flag) { t_type symtype; if (!(flag & NM_ALL) && (symtype = SYM_STAB)) ft_lst_filterout(symbols, &symtype, cmp_symtype, symbol_free); + if ((flag & NM_NO_UNDF) && !(symtype = SYM_UNDF)) + ft_lst_filterout(symbols, &symtype, cmp_symtype, symbol_free); + if ((flag & NM_ONLY_UNDF) && !(symtype = SYM_UNDF)) + ft_lst_filterout(symbols, &symtype, cmp_no_symtype, symbol_free); + if ((flag & NM_NO_LOCAL) && (symtype = N_EXT)) + ft_lst_filterout(symbols, &symtype, mask_nlisttype, symbol_free); return (0); } diff --git a/nm-otool/src/symbol_format.c b/nm-otool/src/symbol_format.c index 1c37c718..8d24daa1 100644 --- a/nm-otool/src/symbol_format.c +++ b/nm-otool/src/symbol_format.c @@ -1,40 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* symbol_format.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/28 20:34:32 by jhalford #+# #+# */ +/* Updated: 2017/03/28 20:50:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + #include "ft_nm_otool.h" t_symbolmap g_symbolmap[] = { - {'U', sym_format_undf}, - {'A', NULL}, - {'T', NULL}, - {'D', NULL}, - {'B', NULL}, - {'C', NULL}, - {'-', sym_format_stab}, - {'S', NULL}, - {'I', NULL}, + {'U', "undefined", sym_format_undf}, + {'A', "absolute", NULL}, + {'T', NULL, NULL}, + {'D', NULL, NULL}, + {'B', NULL, NULL}, + {'C', "common", NULL}, + {'-', NULL, sym_format_stab}, + {'S', NULL, NULL}, + {'I', "indirect", NULL}, }; -int sym_format_undf(t_symbolmap map, t_symbol *symbol) +int sym_format_undf(t_symbol *symbol, t_nmdata *data) { - ft_printf("%16s %c %s\n", " ", map.c, symbol->string); + if (data->flag & NM_ONLY_UNDF) + ft_printf("%s\n", symbol->string); + else + ft_printf("%16s %c %s\n", " ", 'U', symbol->string); return (0); } -int sym_format_stab(t_symbolmap map, t_symbol *symbol) +int sym_format_stab(t_symbol *symbol, t_nmdata *data) { struct nlist_64 *stab; + (void)data; stab = symbol->nlist; ft_printf("%016llx %c %02x %04b %#x %s\n", - stab->n_value, map.c, + stab->n_value, '-', stab->n_sect, stab->n_desc, stab->n_type, symbol->string); return (0); } +int symbol_format_m(t_symbol *symbol) +{ + if (symbol->sect) + + ft_printf("%016llx (%s%c%s) %sexternal %s\n", + symbol->nlist->n_value, + symbol->sect ? symbol->sect->segname : "undefined", + symbol->sect ? ',' : 0, + symbol->sect ? symbol->sect->sectname : "", + symbol->nlist->n_type & N_EXT ? "" : "non-", + !(symbol->nlist->n_type & N_EXT) + && symbol->nlist->n_type & P_EXT ? "(was a private externel)" : "", + (symbol->nlist->n_type & N_STAB) >> 5, + (symbol->nlist->n_type & N_PEXT) >> 4, + symbol->nlist->n_type & N_TYPE, + symbol->nlist->n_sect, symbol->section->sectname, + symbol->nlist->n_desc, symbol->nlist->n_value, + symbol->string); +} + int symbol_format_full(t_symbol *symbol) { - ft_printf("%i:\t%03b|%b|%x|%b \t%i(%s) \t%#06x \t%llx %-20s\n", + ft_printf("%i:\t%03b|%b|%x|%b \t%i(%s) \t%08llx \t%llx %-20s\n", symbol->pos, (symbol->nlist->n_type & N_STAB) >> 5, (symbol->nlist->n_type & N_PEXT) >> 4, @@ -46,15 +82,17 @@ int symbol_format_full(t_symbol *symbol) return (0); } -int symbol_format(t_symbol *symbol) +int symbol_format(t_symbol *symbol, t_nmdata *data) { t_symbolmap map; char c; map = g_symbolmap[symbol->type]; + if (data->flag & NM_OFORMAT) + ft_printf("file: "); if (map.format) - map.format(map, symbol); + map.format(symbol, data); else { c = symbol->nlist->n_type & N_EXT ? map.c : map.c + 32;