This commit is contained in:
Jack Halford 2017-10-24 18:22:56 +02:00
parent b1ba5b3f44
commit b8eadccc85
14 changed files with 76 additions and 79 deletions

View file

@ -35,10 +35,9 @@ OTOOL_OBJ = $(OBJ_DIR)ft_otool.o
SRC_BASE = \ SRC_BASE = \
dump_symtab.c\ dump_symtab.c\
fetch_header.c\
ft_nm.c\ ft_nm.c\
ft_otool.c\ ft_otool.c\
mach_o_parse.c\ mach_64.c\
symbol_filter.c\ symbol_filter.c\
symbol_format.c\ symbol_format.c\
symbol_free.c\ symbol_free.c\

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/10/08 16:37:58 by jhalford ### ########.fr */ /* Updated: 2017/10/23 16:49:52 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,8 @@
# include <mach-o/nlist.h> # include <mach-o/nlist.h>
# include <mach-o/fat.h> # include <mach-o/fat.h>
# 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) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM)
/* /*
@ -44,8 +45,8 @@
** filtering flags ** filtering flags
** -a show all symbols ** -a show all symbols
** -g filter-out local symbols ** -g filter-out local symbols
** -u show only undefined symbols
** -U filter-out undefined symbols ** -U filter-out undefined symbols
** -u show only undefined symbols
*/ */
# define NM_ALL (1 << 4) # define NM_ALL (1 << 4)
@ -120,23 +121,26 @@ struct s_symbolmap
extern t_symbolmap g_symbolmap[]; extern t_symbolmap g_symbolmap[];
extern t_machodata *g_data; extern t_machodata *g_data;
int mach_o_parse(t_machodata *data);
int fetch_header(t_machodata *data);
int symbol_init(t_symbol *symbol, int symbol_init(t_symbol *symbol,
char *stringtable, struct nlist_64 *array, int i); char *stringtable, struct nlist_64 *array, int i);
int symbol_set(t_symbol *symbol, t_machodata *data); 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); 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); int symbol_format(t_symbol *symbol, t_nmdata *data);
void symbol_format_dfl(t_symbol *symbol); void symbol_format_dfl(t_symbol *symbol);
void symbol_format_m(t_symbol *symbol); void symbol_format_m(t_symbol *symbol);
void symbol_format_full(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 mach_64_parse(t_machodata *data);
void dump_dysymtab(t_machodata *data, void dump_dysymtab(t_machodata *data,
struct dysymtab_command *dysymtab); struct dysymtab_command *dysymtab);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 01:56:58 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:09:17 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 16:56:02 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 19:39:15 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/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}, {'p', NULL, NM_NOSORT, 0, NULL, 0},
{'r', NULL, NM_RSORT, 0, NULL, 0}, {'r', NULL, NM_RSORT, 0, NULL, 0},
{0xff, "full", NM_FULL, 0, NULL, 0},
{'a', NULL, NM_ALL, 0, NULL, 0}, {'a', NULL, NM_ALL, 0, NULL, 0},
{'g', NULL, NM_NO_LOCAL, 0, NULL, 0}, {'g', NULL, NM_NO_LOCAL, 0, NULL, 0},
{'u', NULL, NM_ONLY_UNDF, 0, NULL, 0}, {'u', NULL, NM_ONLY_UNDF, 0, NULL, 0},
{'U', NULL, NM_NO_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}, {'o', NULL, NM_OFORMAT, 0, NULL, 0},
{'m', NULL, NM_MFORMAT, 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}, {'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) int nm_file(void *file, t_nmdata *data)
{ {
uint32_t magic; uint32_t magic;
magic = *(int*)file; 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); mach_64_dump(file, data);
else if (IS_FAT(magic)) else if (IS_FAT(magic))
ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic); ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:08:14 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; magic = *(int *)file;
if (IS_FAT(magic)) if (IS_FAT(magic))
ft_printf("fat binary not supported yet\n"); 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); sect = get_text_section(file);
ft_printf("Contents of (__TEXT,__text) section\n"); ft_printf("Contents of (__TEXT,__text) section\n");

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* fetch_header.c :+: :+: :+: */ /* mach_64.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/23 15:56:37 by jhalford #+# #+# */ /* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:31:01 by jhalford ### ########.fr */ /* 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; 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);
}

View file

@ -1,19 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mach_o_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,38 +6,40 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/28 20:34:36 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" #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)) allowed_syms = ~(1 << SYM_STAB);
ft_lst_filterout(symbols, &symtype, cmp_symtype, symbol_free); if (flag & NM_ALL)
if ((flag & NM_NO_UNDF) && !(symtype = SYM_UNDF)) allowed_syms |= (1 << SYM_STAB);
ft_lst_filterout(symbols, &symtype, cmp_symtype, symbol_free); if (flag & NM_NO_UNDF)
if ((flag & NM_ONLY_UNDF) && !(symtype = SYM_UNDF)) allowed_syms &= ~(1 << SYM_UNDF);
ft_lst_filterout(symbols, &symtype, cmp_no_symtype, symbol_free); if (flag & NM_ONLY_UNDF)
if ((flag & NM_NO_LOCAL) && (symtype = N_EXT)) allowed_syms &= (1 << SYM_UNDF);
ft_lst_filterout(symbols, &symtype, mask_nlisttype, symbol_free); 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); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/28 20:34:32 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) if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY)
ft_printf(" [referenced dynamically]"); ft_printf(" [referenced dynamically]");
if (symbol->nlist->n_type & N_EXT) if (is_external(symbol))
ft_printf(" external"); ft_printf(" external");
else else
{ {
@ -66,11 +66,12 @@ void symbol_format_full(t_symbol *symbol)
void symbol_format_dfl(t_symbol *symbol) void symbol_format_dfl(t_symbol *symbol)
{ {
t_symbolmap map; t_symbolmap map;
char c;
map = g_symbolmap[symbol->type]; map = g_symbolmap[symbol->type];
c = symbol->nlist->n_type & N_EXT ? map.c : map.c + 32; if (is_external(symbol))
ft_printf(" %c", c); ft_printf(" %c", map.c);
else
ft_printf(" %c", map.c + 'a' - 'A');
if (symbol->type == SYM_STAB) if (symbol->type == SYM_STAB)
ft_printf(" %02x %04b %#x", ft_printf(" %02x %04b %#x",
symbol->nlist->n_sect, symbol->nlist->n_desc, symbol->nlist->n_sect, symbol->nlist->n_desc,

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 21:22:06 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); return (0);
} }
/*
** Calculate the sym->type field
*/
static int symbol_gettype(int type_mask, static int symbol_gettype(int type_mask,
struct nlist_64 *nlist, struct section_64 *section) struct nlist_64 *nlist, struct section_64 *section)
{ {
@ -47,6 +51,10 @@ static int symbol_gettype(int type_mask,
return (SYM_OTHER); return (SYM_OTHER);
} }
/*
** Construct symbol data
*/
int symbol_set(t_symbol *sym, t_machodata *data) int symbol_set(t_symbol *sym, t_machodata *data)
{ {
t_list *lst; t_list *lst;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 20:33:02 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */