filtering options

This commit is contained in:
Jack Halford 2017-03-29 16:39:47 +02:00
parent eb7b390a96
commit 8caadf0df6
5 changed files with 97 additions and 33 deletions

1
nm-otool/.gitignore vendored
View file

@ -1,2 +1,3 @@
ft_nm ft_nm
ft_otool ft_otool
tests

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:36:10 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 ** filtering flags
*/ */
# define NM_ALL (1 << 4) # 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 ** 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 t_data_template t_nmdata;
typedef enum e_symtype t_symtype; 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); int symbol_filter(t_list **syms, t_flag flag);
void symbol_free(void *data, size_t size); 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 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 mach_64_parse(t_machodata *data);
void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab); void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 03:09:12 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}, {'r', NULL, NM_RSORT, 0, NULL, 0},
{0xff, "full", NM_FULL, 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}, {'a', NULL, NM_ALL, 0, NULL, 0},
{'U', NULL, 0, 0, NULL, 0}, {'g', NULL, NM_NO_LOCAL, 0, NULL, 0},
{'o', NULL, 0, 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}, {'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}, {'x', NULL, 0, 0, NULL, 0},
{'j', 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); mach_64_parse(&mach);
symbol_sort(&mach.symbols, data->flag); symbol_sort(&mach.symbols, data->flag);
symbol_filter(&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; format = symbol_format_full;
else else
format = symbol_format; format = symbol_format;
ft_lstiter(mach.symbols, format); ft_lstiter(mach.symbols, format, data);
} }
int nm(void *file, t_nmdata *data) int nm(void *file, t_nmdata *data)
@ -85,6 +88,8 @@ int main(int ac, char **av)
i = data.av_data - av; i = data.av_data - av;
while (i < ac && av[i]) 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) if ((fd = open(av[i], O_RDONLY)) < 0)
return (1); return (1);
if ((fstat(fd, &buf)) < 0) if ((fstat(fd, &buf)) < 0)

View file

@ -1,13 +1,12 @@
/* /* ************************************************************************** */
************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* symbol_filter.c :+: :+: :+: */ /* symbol_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 03:23:32 by jhalford #+# #+# */ /* Created: 2017/03/28 20:34:36 by jhalford #+# #+# */
/* Updated: 2017/03/27 03:23:32 by jhalford ### ########.fr */ /* 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); 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) int symbol_filter(t_list **symbols, t_flag flag)
{ {
t_type symtype; t_type symtype;
if (!(flag & NM_ALL) && (symtype = SYM_STAB)) if (!(flag & NM_ALL) && (symtype = SYM_STAB))
ft_lst_filterout(symbols, &symtype, cmp_symtype, symbol_free); 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); return (0);
} }

View file

@ -1,40 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/28 20:34:32 by jhalford #+# #+# */
/* Updated: 2017/03/28 20:50:00 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] = t_symbolmap g_symbolmap[] =
{ {
{'U', sym_format_undf}, {'U', "undefined", sym_format_undf},
{'A', NULL}, {'A', "absolute", NULL},
{'T', NULL}, {'T', NULL, NULL},
{'D', NULL}, {'D', NULL, NULL},
{'B', NULL}, {'B', NULL, NULL},
{'C', NULL}, {'C', "common", NULL},
{'-', sym_format_stab}, {'-', NULL, sym_format_stab},
{'S', NULL}, {'S', NULL, NULL},
{'I', 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); 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; struct nlist_64 *stab;
(void)data;
stab = symbol->nlist; stab = symbol->nlist;
ft_printf("%016llx %c %02x %04b %#x %s\n", 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, stab->n_sect, stab->n_desc, stab->n_type,
symbol->string); symbol->string);
return (0); 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) 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->pos,
(symbol->nlist->n_type & N_STAB) >> 5, (symbol->nlist->n_type & N_STAB) >> 5,
(symbol->nlist->n_type & N_PEXT) >> 4, (symbol->nlist->n_type & N_PEXT) >> 4,
@ -46,15 +82,17 @@ int symbol_format_full(t_symbol *symbol)
return (0); return (0);
} }
int symbol_format(t_symbol *symbol) int symbol_format(t_symbol *symbol, t_nmdata *data)
{ {
t_symbolmap map; t_symbolmap map;
char c; char c;
map = g_symbolmap[symbol->type]; map = g_symbolmap[symbol->type];
if (data->flag & NM_OFORMAT)
ft_printf("file: ");
if (map.format) if (map.format)
map.format(map, symbol); map.format(symbol, data);
else else
{ {
c = symbol->nlist->n_type & N_EXT ? map.c : map.c + 32; c = symbol->nlist->n_type & N_EXT ? map.c : map.c + 32;