diff --git a/nm-otool/Makefile b/nm-otool/Makefile index a085f998..ca206866 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -35,10 +35,9 @@ OTOOL_OBJ = $(OBJ_DIR)ft_otool.o SRC_BASE = \ dump_symtab.c\ -fetch_header.c\ ft_nm.c\ ft_otool.c\ -mach_o_parse.c\ +mach_64.c\ symbol_filter.c\ symbol_format.c\ symbol_free.c\ diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index d0354951..22c8b43c 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/10/08 16:37:58 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:49:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,8 @@ # include # include -# define IS_MAGIC_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) +# define IS_MACH_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) +# define IS_MACH_32(x) (x == MH_MAGIC || x == MH_CIGAM) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) /* @@ -44,8 +45,8 @@ ** filtering flags ** -a show all symbols ** -g filter-out local symbols -** -u show only undefined symbols ** -U filter-out undefined symbols +** -u show only undefined symbols */ # define NM_ALL (1 << 4) @@ -120,23 +121,26 @@ struct s_symbolmap extern t_symbolmap g_symbolmap[]; extern t_machodata *g_data; -int mach_o_parse(t_machodata *data); -int fetch_header(t_machodata *data); - int symbol_init(t_symbol *symbol, char *stringtable, struct nlist_64 *array, int i); int symbol_set(t_symbol *symbol, t_machodata *data); -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_sort(t_list **syms, t_flag flag); + +int symbol_filter(t_list **syms, t_flag flag); +int is_external(t_symbol *s); +int is_not_external(t_symbol *s); + int symbol_format(t_symbol *symbol, t_nmdata *data); void symbol_format_dfl(t_symbol *symbol); void symbol_format_m(t_symbol *symbol); void symbol_format_full(t_symbol *symbol); +void mach_64_dump(struct mach_header_64 *file, + t_nmdata *data); void mach_64_parse(t_machodata *data); void dump_dysymtab(t_machodata *data, - struct dysymtab_command *dysymtab); + struct dysymtab_command *dysymtab); #endif diff --git a/nm-otool/libft/includes/ft_types.h b/nm-otool/libft/includes/ft_types.h index a8f001cc..e79cb979 100644 --- a/nm-otool/libft/includes/ft_types.h +++ b/nm-otool/libft/includes/ft_types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 01:56:58 by jhalford #+# #+# */ -/* Updated: 2017/03/25 02:02:06 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 15:58:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/libft/srcs/lst/ft_lst_filter.c b/nm-otool/libft/srcs/lst/ft_lst_filter.c index cc528142..70aa29fb 100644 --- a/nm-otool/libft/srcs/lst/ft_lst_filter.c +++ b/nm-otool/libft/srcs/lst/ft_lst_filter.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/04 11:09:17 by jhalford #+# #+# */ -/* Updated: 2017/05/16 17:34:15 by ariard ### ########.fr */ +/* Updated: 2017/10/23 16:28:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/libft/srcs/lst/ft_lst_filterout.c b/nm-otool/libft/srcs/lst/ft_lst_filterout.c index 9a97c09d..26ed8bd3 100644 --- a/nm-otool/libft/srcs/lst/ft_lst_filterout.c +++ b/nm-otool/libft/srcs/lst/ft_lst_filterout.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/26 16:56:02 by jhalford #+# #+# */ -/* Updated: 2017/03/26 19:10:03 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:29:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/srcs/dump_symtab.c b/nm-otool/srcs/dump_symtab.c index 2a3d0eda..6c345c83 100644 --- a/nm-otool/srcs/dump_symtab.c +++ b/nm-otool/srcs/dump_symtab.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 19:39:15 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:26:32 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:49:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/srcs/ft_nm.c b/nm-otool/srcs/ft_nm.c index 81995af6..8ee0175d 100644 --- a/nm-otool/srcs/ft_nm.c +++ b/nm-otool/srcs/ft_nm.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/10/08 11:33:44 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:33:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,12 +21,12 @@ t_cliopts g_nm_opts[] = {'p', NULL, NM_NOSORT, 0, NULL, 0}, {'r', NULL, NM_RSORT, 0, NULL, 0}, - {0xff, "full", NM_FULL, 0, NULL, 0}, {'a', NULL, NM_ALL, 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}, + {0xff, "full", NM_FULL, 0, NULL, 0}, {'o', NULL, NM_OFORMAT, 0, NULL, 0}, {'m', NULL, NM_MFORMAT, 0, NULL, 0}, @@ -35,25 +35,14 @@ t_cliopts g_nm_opts[] = {'j', NULL, 0, 0, NULL, 0}, }; -void mach_64_dump(struct mach_header_64 *file, t_nmdata *data) -{ - t_machodata mach; - - mach.sects = NULL; - mach.symbols = NULL; - mach.file = file; - mach_64_parse(&mach); - symbol_sort(&mach.symbols, data->flag); - symbol_filter(&mach.symbols, data->flag); - ft_lstiter(mach.symbols, symbol_format, data); -} - int nm_file(void *file, t_nmdata *data) { uint32_t magic; magic = *(int*)file; - if (IS_MAGIC_64(magic)) + if (IS_MACH_64(magic)) + mach_64_dump(file, data); + else if (IS_MACH_32(magic)) mach_64_dump(file, data); else if (IS_FAT(magic)) ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic); diff --git a/nm-otool/srcs/ft_otool.c b/nm-otool/srcs/ft_otool.c index f520dc62..10cefb19 100644 --- a/nm-otool/srcs/ft_otool.c +++ b/nm-otool/srcs/ft_otool.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:35:37 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:37:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ void otool(void *file) magic = *(int *)file; if (IS_FAT(magic)) ft_printf("fat binary not supported yet\n"); - else if (IS_MAGIC_64(magic)) + else if (IS_MACH_64(magic)) { sect = get_text_section(file); ft_printf("Contents of (__TEXT,__text) section\n"); diff --git a/nm-otool/srcs/fetch_header.c b/nm-otool/srcs/mach_64.c similarity index 80% rename from nm-otool/srcs/fetch_header.c rename to nm-otool/srcs/mach_64.c index e447d19d..4e14dca8 100644 --- a/nm-otool/srcs/fetch_header.c +++ b/nm-otool/srcs/mach_64.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* fetch_header.c :+: :+: :+: */ +/* mach_64.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/23 15:56:37 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:31:01 by jhalford ### ########.fr */ +/* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */ +/* Updated: 2017/10/23 16:42:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,3 +69,16 @@ void mach_64_parse(t_machodata *data) lc = (void*)lc + lc->cmdsize; } } + +void mach_64_dump(struct mach_header_64 *file, t_nmdata *data) +{ + t_machodata mach; + + mach.sects = NULL; + mach.symbols = NULL; + mach.file = file; + mach_64_parse(&mach); + symbol_sort(&mach.symbols, data->flag); + symbol_filter(&mach.symbols, data->flag); + ft_lstiter(mach.symbols, symbol_format, data); +} diff --git a/nm-otool/srcs/mach_o_parse.c b/nm-otool/srcs/mach_o_parse.c deleted file mode 100644 index 6c5ba302..00000000 --- a/nm-otool/srcs/mach_o_parse.c +++ /dev/null @@ -1,19 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* mach_o_parse.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/03/25 20:56:50 by jhalford #+# #+# */ -/* Updated: 2017/03/25 22:09:56 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_nm_otool.h" - -int mach_o_parse(t_machodata *data) -{ - (void)data; - return (0); -} diff --git a/nm-otool/srcs/symbol_filter.c b/nm-otool/srcs/symbol_filter.c index f87bb5c1..971249b6 100644 --- a/nm-otool/srcs/symbol_filter.c +++ b/nm-otool/srcs/symbol_filter.c @@ -6,38 +6,40 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/28 20:34:36 by jhalford #+# #+# */ -/* Updated: 2017/03/28 20:35:11 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:45:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -int cmp_symtype(t_symbol *sym, void *type) +int cmp_symtype(t_symbol *sym, void *type) { - return (sym->type - *(t_type*)type); + return ((1 << sym->type) & *(uint64_t*)type); } -int cmp_no_symtype(t_symbol *sym, void *type) +inline int is_not_external(t_symbol *s) { - return (!(sym->type - *(t_type*)type)); + return (!(s->nlist->n_type & N_EXT)); } -int mask_nlisttype(t_symbol *sym, void *type) +inline int is_external(t_symbol *s) { - return (sym->nlist->n_type & *(t_type*)type); + return (s->nlist->n_type & N_EXT); } -int symbol_filter(t_list **symbols, t_flag flag) +int symbol_filter(t_list **symbols, t_flag flag) { - t_type symtype; + uint64_t allowed_syms; - 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); + allowed_syms = ~(1 << SYM_STAB); + if (flag & NM_ALL) + allowed_syms |= (1 << SYM_STAB); + if (flag & NM_NO_UNDF) + allowed_syms &= ~(1 << SYM_UNDF); + if (flag & NM_ONLY_UNDF) + allowed_syms &= (1 << SYM_UNDF); + ft_lst_filterout(symbols, &allowed_syms, cmp_symtype, symbol_free); + if (flag & NM_NO_LOCAL) + ft_lst_filterout(symbols, NULL, is_external, symbol_free); return (0); } diff --git a/nm-otool/srcs/symbol_format.c b/nm-otool/srcs/symbol_format.c index d0d42108..18a8e63a 100644 --- a/nm-otool/srcs/symbol_format.c +++ b/nm-otool/srcs/symbol_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/28 20:34:32 by jhalford #+# #+# */ -/* Updated: 2017/10/08 11:34:43 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:31:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ void symbol_format_desc(t_symbol *symbol) { if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) ft_printf(" [referenced dynamically]"); - if (symbol->nlist->n_type & N_EXT) + if (is_external(symbol)) ft_printf(" external"); else { @@ -66,11 +66,12 @@ void symbol_format_full(t_symbol *symbol) void symbol_format_dfl(t_symbol *symbol) { t_symbolmap map; - char c; map = g_symbolmap[symbol->type]; - c = symbol->nlist->n_type & N_EXT ? map.c : map.c + 32; - ft_printf(" %c", c); + if (is_external(symbol)) + ft_printf(" %c", map.c); + else + ft_printf(" %c", map.c + 'a' - 'A'); if (symbol->type == SYM_STAB) ft_printf(" %02x %04b %#x", symbol->nlist->n_sect, symbol->nlist->n_desc, diff --git a/nm-otool/srcs/symbol_init.c b/nm-otool/srcs/symbol_init.c index 16bbd321..f540d3d4 100644 --- a/nm-otool/srcs/symbol_init.c +++ b/nm-otool/srcs/symbol_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 21:22:06 by jhalford #+# #+# */ -/* Updated: 2017/10/08 11:02:50 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:41:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,10 @@ int symbol_init(t_symbol *symbol, return (0); } +/* +** Calculate the sym->type field +*/ + static int symbol_gettype(int type_mask, struct nlist_64 *nlist, struct section_64 *section) { @@ -47,6 +51,10 @@ static int symbol_gettype(int type_mask, return (SYM_OTHER); } +/* +** Construct symbol data +*/ + int symbol_set(t_symbol *sym, t_machodata *data) { t_list *lst; diff --git a/nm-otool/srcs/symbol_sort.c b/nm-otool/srcs/symbol_sort.c index d12c7895..e513c4a2 100644 --- a/nm-otool/srcs/symbol_sort.c +++ b/nm-otool/srcs/symbol_sort.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:29:45 by jhalford ### ########.fr */ +/* Updated: 2017/10/23 16:38:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */