This commit is contained in:
Jack Halford 2017-10-26 19:15:27 +02:00
parent b8eadccc85
commit a565b7738d
11 changed files with 108 additions and 108 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# # # Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/10/07 17:50:04 by jhalford ### ########.fr # # Updated: 2017/10/26 19:15:02 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -37,24 +37,32 @@ SRC_BASE = \
dump_symtab.c\ dump_symtab.c\
ft_nm.c\ ft_nm.c\
ft_otool.c\ ft_otool.c\
mach_64.c\ mach_64/mach_64.c\
symbol_filter.c\ mach_64/symbol_64_filter.c\
symbol_format.c\ mach_64/symbol_64_format.c\
symbol_free.c\ mach_64/symbol_64_free.c\
symbol_init.c\ mach_64/symbol_64_init.c\
symbol_sort.c mach_64/symbol_64_sort.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) OBJS = $(addprefix $(OBJ_DIR), $($(notdir SRC_BASE):.c=.o))
OBJS := $(filter-out $(NM_OBJ), $(OBJS)) OBJS := $(filter-out $(NM_OBJ), $(OBJS))
OBJS := $(filter-out $(OTOOL_OBJ), $(OBJS)) OBJS := $(filter-out $(OTOOL_OBJ), $(OBJS))
NB = $(words $(SRC_BASE)) NB = $(words $(SRC_BASE))
INDEX = 0 INDEX = 0
all : MACH_64_SRC := $(wildcard $(SRC_DIR)mach_64/*)
MACH_SRC := $(subst _64,_32, $(MACH_64_SRC:.c=.p))
all: $(MACH_SRC)
@make -C $(LIBFT_DIR) @make -C $(LIBFT_DIR)
@make -j $(NAME) @make -j $(NAME)
# $(SRC_DIR)/mach/%.c: $(SRC_DIR)/mach_64/%.c
$(MACH_SRC):
@mkdir -p $(SRC_DIR)/mach
@echo "$@ ---> $(subst _32,_64, $@)"
ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NM_OBJ) ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NM_OBJ)
@$(CC) $(OBJS) -o $@ \ @$(CC) $(OBJS) -o $@ \
-I $(INC_DIR) \ -I $(INC_DIR) \
@ -111,6 +119,6 @@ re: fclean all
relib: fcleanlib $(LIBFT_LIB) relib: fcleanlib $(LIBFT_LIB)
.PHONY : fclean clean re relib cleanlib fcleanlib .PHONY : fclean clean re relib cleanlib fcleanlib $(MACH_SRC)
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

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/23 16:49:52 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:42:47 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,7 +72,6 @@ typedef struct s_nmdata t_nmdata;
typedef enum e_symtype t_symtype; typedef enum e_symtype t_symtype;
typedef struct s_symbolmap t_symbolmap; typedef struct s_symbolmap t_symbolmap;
typedef struct s_machodata t_machodata; typedef struct s_machodata t_machodata;
typedef struct s_symbol t_symbol;
struct s_nmdata struct s_nmdata
{ {
@ -103,44 +102,18 @@ struct s_machodata
struct dysymtab_command *dysymtab; struct dysymtab_command *dysymtab;
}; };
struct s_symbol
{
int pos;
t_symtype type;
char *string;
struct nlist_64 *nlist;
struct section_64 *section;
};
struct s_symbolmap struct s_symbolmap
{ {
char c; char c;
char *s; char *s;
}; };
extern t_symbolmap g_symbolmap[]; # include "mach_64.h"
extern t_machodata *g_data;
int symbol_init(t_symbol *symbol,
char *stringtable, struct nlist_64 *array, int i);
int symbol_set(t_symbol *symbol, t_machodata *data);
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, void dump_dysymtab(t_machodata *data,
struct dysymtab_command *dysymtab); struct dysymtab_command *dysymtab);
extern t_symbolmap g_symbolmap[];
extern t_machodata *g_data;
#endif #endif

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mach_64.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:43:32 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MACH_64_H
# define MACH_64_H
typedef struct s_symbol_64 t_symbol_64;
struct s_symbol_64
{
int pos;
t_symtype type;
char *string;
struct nlist_64 *nlist;
struct section_64 *section;
};
int symbol_64_init(t_symbol_64 *symbol,
char *stringtable, struct nlist_64 *array, int i);
int symbol_64_set(t_symbol_64 *symbol, t_machodata *data);
void symbol_64_free(void *data, size_t size);
int symbol_64_sort(t_list **syms, t_flag flag);
int symbol_64_filter(t_list **syms, t_flag flag);
int is_external_64(t_symbol_64 *s);
int is_not_external_64(t_symbol_64 *s);
int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data);
void mach_64_dump(struct mach_header_64 *file,
t_nmdata *data);
#endif

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/23 16:33:42 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:46:22 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,9 +40,9 @@ int nm_file(void *file, t_nmdata *data)
uint32_t magic; uint32_t magic;
magic = *(int*)file; magic = *(int*)file;
if (IS_MACH_64(magic)) if (IS_MACH_32(magic))
mach_64_dump(file, data); mach_64_dump(file, data);
else if (IS_MACH_32(magic)) else if (IS_MACH_64(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,16 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */ /* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */
/* Updated: 2017/10/23 16:42:06 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:45:25 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
static void symtab_parse(t_machodata *data, struct symtab_command *symtab) static void symtab_64_parse(t_machodata *data, struct symtab_command *symtab)
{ {
int i; int i;
t_symbol symbol; t_symbol_64 symbol;
char *stringtable; char *stringtable;
struct nlist_64 *array; struct nlist_64 *array;
@ -25,8 +25,8 @@ static void symtab_parse(t_machodata *data, struct symtab_command *symtab)
i = -1; i = -1;
while (++i < (int)symtab->nsyms) while (++i < (int)symtab->nsyms)
{ {
symbol_init(&symbol, stringtable, array, i); symbol_64_init(&symbol, stringtable, array, i);
symbol_set(&symbol, data); symbol_64_set(&symbol, data);
ft_lsteadd(&data->symbols, ft_lstnew(&symbol, sizeof(symbol))); ft_lsteadd(&data->symbols, ft_lstnew(&symbol, sizeof(symbol)));
} }
} }
@ -61,7 +61,7 @@ void mach_64_parse(t_machodata *data)
while (++i < ncmds) while (++i < ncmds)
{ {
if (lc->cmd == LC_SYMTAB) if (lc->cmd == LC_SYMTAB)
symtab_parse(data, (struct symtab_command*)lc); symtab_64_parse(data, (struct symtab_command*)lc);
else if (lc->cmd == LC_DYSYMTAB) else if (lc->cmd == LC_DYSYMTAB)
data->dysymtab = (struct dysymtab_command*)lc; data->dysymtab = (struct dysymtab_command*)lc;
else if (lc->cmd == LC_SEGMENT_64) else if (lc->cmd == LC_SEGMENT_64)
@ -78,7 +78,7 @@ void mach_64_dump(struct mach_header_64 *file, t_nmdata *data)
mach.symbols = NULL; mach.symbols = NULL;
mach.file = file; mach.file = file;
mach_64_parse(&mach); mach_64_parse(&mach);
symbol_sort(&mach.symbols, data->flag); symbol_64_sort(&mach.symbols, data->flag);
symbol_filter(&mach.symbols, data->flag); symbol_64_filter(&mach.symbols, data->flag);
ft_lstiter(mach.symbols, symbol_format, data); ft_lstiter(mach.symbols, symbol_64_format, data);
} }

View file

@ -1,33 +1,33 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* symbol_filter.c :+: :+: :+: */ /* symbol_64_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/28 20:34:36 by jhalford #+# #+# */ /* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */
/* Updated: 2017/10/23 16:45:54 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:44:51 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
int cmp_symtype(t_symbol *sym, void *type) int cmp_symtype_64(t_symbol_64 *sym, void *type)
{ {
return ((1 << sym->type) & *(uint64_t*)type); return ((1 << sym->type) & *(uint64_t*)type);
} }
inline int is_not_external(t_symbol *s) inline int is_not_external_64(t_symbol_64 *s)
{ {
return (!(s->nlist->n_type & N_EXT)); return (!(s->nlist->n_type & N_EXT));
} }
inline int is_external(t_symbol *s) inline int is_external_64(t_symbol_64 *s)
{ {
return (s->nlist->n_type & N_EXT); return (s->nlist->n_type & N_EXT);
} }
int symbol_filter(t_list **symbols, t_flag flag) int symbol_64_filter(t_list **symbols, t_flag flag)
{ {
uint64_t allowed_syms; uint64_t allowed_syms;
@ -38,8 +38,8 @@ int symbol_filter(t_list **symbols, t_flag flag)
allowed_syms &= ~(1 << SYM_UNDF); allowed_syms &= ~(1 << SYM_UNDF);
if (flag & NM_ONLY_UNDF) if (flag & NM_ONLY_UNDF)
allowed_syms &= (1 << SYM_UNDF); allowed_syms &= (1 << SYM_UNDF);
ft_lst_filterout(symbols, &allowed_syms, cmp_symtype, symbol_free); ft_lst_filterout(symbols, &allowed_syms, cmp_symtype_64, symbol_64_free);
if (flag & NM_NO_LOCAL) if (flag & NM_NO_LOCAL)
ft_lst_filterout(symbols, NULL, is_external, symbol_free); ft_lst_filterout(symbols, NULL, is_external_64, symbol_64_free);
return (0); return (0);
} }

View file

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/28 20:34:32 by jhalford #+# #+# */
/* Updated: 2017/10/23 16:31:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] = t_symbolmap g_symbolmap[] =
@ -25,11 +13,11 @@ t_symbolmap g_symbolmap[] =
{'I', "indirect"}, {'I', "indirect"},
}; };
void symbol_format_desc(t_symbol *symbol) void symbol_64_format_desc(t_symbol_64 *symbol)
{ {
if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY)
ft_printf(" [referenced dynamically]"); ft_printf(" [referenced dynamically]");
if (is_external(symbol)) if (is_external_64(symbol))
ft_printf(" external"); ft_printf(" external");
else else
{ {
@ -39,7 +27,7 @@ void symbol_format_desc(t_symbol *symbol)
} }
} }
void symbol_format_m(t_symbol *symbol) void symbol_64_format_m(t_symbol_64 *symbol)
{ {
t_symbolmap map; t_symbolmap map;
@ -48,10 +36,10 @@ void symbol_format_m(t_symbol *symbol)
symbol->section ? symbol->section->segname : map.s, symbol->section ? symbol->section->segname : map.s,
symbol->section ? ',' : 0, symbol->section ? ',' : 0,
symbol->section ? symbol->section->sectname : ""); symbol->section ? symbol->section->sectname : "");
symbol_format_desc(symbol); symbol_64_format_desc(symbol);
} }
void symbol_format_full(t_symbol *symbol) void symbol_64_format_full(t_symbol_64 *symbol)
{ {
ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x", ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x",
symbol->pos, symbol->pos,
@ -63,12 +51,12 @@ void symbol_format_full(t_symbol *symbol)
symbol->nlist->n_desc); symbol->nlist->n_desc);
} }
void symbol_format_dfl(t_symbol *symbol) void symbol_64_format_dfl(t_symbol_64 *symbol)
{ {
t_symbolmap map; t_symbolmap map;
map = g_symbolmap[symbol->type]; map = g_symbolmap[symbol->type];
if (is_external(symbol)) if (is_external_64(symbol))
ft_printf(" %c", map.c); ft_printf(" %c", map.c);
else else
ft_printf(" %c", map.c + 'a' - 'A'); ft_printf(" %c", map.c + 'a' - 'A');
@ -78,7 +66,7 @@ void symbol_format_dfl(t_symbol *symbol)
symbol->nlist->n_type); symbol->nlist->n_type);
} }
int symbol_format(t_symbol *symbol, t_nmdata *data) int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data)
{ {
if (data->flag & NM_OFORMAT) if (data->flag & NM_OFORMAT)
ft_printf("%s: ", data->filename); ft_printf("%s: ", data->filename);
@ -89,11 +77,11 @@ int symbol_format(t_symbol *symbol, t_nmdata *data)
else else
ft_printf("%016llx", symbol->nlist->n_value); ft_printf("%016llx", symbol->nlist->n_value);
if (data->flag & NM_MFORMAT) if (data->flag & NM_MFORMAT)
symbol_format_m(symbol); symbol_64_format_m(symbol);
else if (data->flag & NM_FULL) else if (data->flag & NM_FULL)
symbol_format_full(symbol); symbol_64_format_full(symbol);
else else
symbol_format_dfl(symbol); symbol_64_format_dfl(symbol);
} }
ft_printf(" %s\n", symbol->string); ft_printf(" %s\n", symbol->string);
return (0); return (0);

View file

@ -6,15 +6,15 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 17:06:23 by jhalford #+# #+# */ /* Created: 2017/03/26 17:06:23 by jhalford #+# #+# */
/* Updated: 2017/03/26 17:09:53 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:38:31 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
void symbol_free(void *data, size_t size) void symbol_64_free(void *data, size_t size)
{ {
t_symbol *symbol; t_symbol_64 *symbol;
symbol = data; symbol = data;
(void)size; (void)size;

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* symbol_init.c :+: :+: :+: */ /* symbol_64_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 21:22:06 by jhalford #+# #+# */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */
/* Updated: 2017/10/23 16:41:15 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:23:24 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
t_machodata *g_data; t_machodata *g_data;
int symbol_init(t_symbol *symbol, int symbol_64_init(t_symbol_64 *symbol,
char *stringtable, struct nlist_64 *array, int i) char *stringtable, struct nlist_64 *array, int i)
{ {
symbol->type = 0; symbol->type = 0;
@ -24,11 +24,7 @@ int symbol_init(t_symbol *symbol,
return (0); return (0);
} }
/* static int symbol_64_gettype(int type_mask,
** Calculate the sym->type field
*/
static int symbol_gettype(int type_mask,
struct nlist_64 *nlist, struct section_64 *section) struct nlist_64 *nlist, struct section_64 *section)
{ {
if (nlist->n_type & N_STAB) if (nlist->n_type & N_STAB)
@ -51,11 +47,7 @@ static int symbol_gettype(int type_mask,
return (SYM_OTHER); return (SYM_OTHER);
} }
/* int symbol_64_set(t_symbol_64 *sym, t_machodata *data)
** Construct symbol data
*/
int symbol_set(t_symbol *sym, t_machodata *data)
{ {
t_list *lst; t_list *lst;
@ -66,7 +58,7 @@ int symbol_set(t_symbol *sym, t_machodata *data)
} }
else else
sym->section = NULL; sym->section = NULL;
sym->type = symbol_gettype( sym->type = symbol_64_gettype(
sym->nlist->n_type & N_TYPE, sym->nlist->n_type & N_TYPE,
sym->nlist, sym->nlist,
sym->section); sym->section);

View file

@ -6,25 +6,25 @@
/* 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/23 16:38:29 by jhalford ### ########.fr */ /* Updated: 2017/10/26 18:46:04 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
static int sym_asort(t_symbol *sym1, t_symbol *sym2) static int sym_asort(t_symbol_64 *sym1, t_symbol_64 *sym2)
{ {
return (ft_strcmp(sym1->string, sym2->string)); return (ft_strcmp(sym1->string, sym2->string));
} }
static int sym_nsort(t_symbol *sym1, t_symbol *sym2) static int sym_nsort(t_symbol_64 *sym1, t_symbol_64 *sym2)
{ {
if (sym1->nlist->n_value == sym2->nlist->n_value) if (sym1->nlist->n_value == sym2->nlist->n_value)
return (sym_asort(sym1, sym2)); return (sym_asort(sym1, sym2));
return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1); return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1);
} }
int symbol_sort(t_list **syms, t_flag flag) int symbol_64_sort(t_list **syms, t_flag flag)
{ {
if (!(flag & NM_NOSORT)) if (!(flag & NM_NOSORT))
{ {