diff --git a/nm-otool/Makefile b/nm-otool/Makefile index cea02344..34a63eea 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -3,36 +3,35 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: jhalford +#+ +:+ +#+ # +# By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # -# Created: 2017/02/19 03:29:38 by jhalford #+# #+# # -# Updated: 2017/03/27 20:21:47 by jhalford ### ########.fr # +# Created: 2016/08/29 21:32:58 by wescande #+# #+# # +# Updated: 2017/03/31 21:05:56 by jhalford ### ########.fr # # # # **************************************************************************** # NAME = ft_nm ft_otool CC = gcc -MKDIR = mkdir -p -RM = /bin/rm -rf - W_FLAGS = -Wall -Wextra -Werror D_FLAGS = -V_FLAGS = -fvisibility=hidden -FLAGS = $(W_FLAGS) $(D_FLAGS) $(V_FLAGS) +FLAGS = $(W_FLAGS) $(D_FLAGS) -DELTA = $$(echo "$$(tput cols)-47"|bc) +LEN_NAME = `printf "%s" $(NAME) |wc -c` +DELTA = $$(echo "$$(tput cols)-31-$(LEN_NAME)"|bc) LIBFT_DIR = libft/ LIBFT_LIB = $(LIBFT_DIR)libft.a LIBFT_INC = $(LIBFT_DIR)includes/ -SRC_DIR = src/ +LIBS = + +SRC_DIR = srcs/ INC_DIR = includes/ OBJ_DIR = objs/ -FT_NM_OBJ = $(OBJ_DIR)ft_nm.o -FT_OTOOL_OBJ= $(OBJ_DIR)ft_otool.o +NM_OBJ = $(OBJ_DIR)ft_nm.o +OTOOL_OBJ = $(OBJ_DIR)ft_otool.o SRC_BASE = \ dump_symtab.c\ @@ -49,76 +48,71 @@ symbol_sort.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) -NB = $(words $(SRC_BASE) $(NAME)) +OBJS := $(filter-out $(NM_OBJ), $(OBJS)) +OBJS := $(filter-out $(OTOOL_OBJ), $(OBJS)) +NB = $(words $(SRC_BASE)) INDEX = 0 -OBJS := $(filter-out $(FT_NM_OBJ), $(OBJS)) -OBJS := $(filter-out $(FT_OTOOL_OBJ), $(OBJS)) - -all: +all : @make -C $(LIBFT_DIR) @make -j $(NAME) -ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(FT_NM_OBJ) - @echo $(FT_NM_OBJ) - @$(CC) $(FLAGS) $(D_FLAGS) \ +ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NM_OBJ) + @$(CC) $(OBJS) -o $@ \ -I $(INC_DIR) \ -I $(LIBFT_INC) \ - $(LIBS) \ - $(LIBFT_LIB) $(OBJS) $(FT_NM_OBJ) \ - -o $@ - @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) - @strip -x $@ - @printf "\r\e[48;5;15;38;5;25m✅ MAKE $@\e[0m\e[K\n" + $(LIBS) $(LIBFT_LIB) $(NM_OBJ) $(FLAGS) + @printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n" -ft_otool: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(FT_OTOOL_OBJ) - @$(CC) $(FLAGS) $(D_FLAGS) \ +ft_otool: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(OTOOL_OBJ) + @$(CC) $(OBJS) -o $@ \ -I $(INC_DIR) \ -I $(LIBFT_INC) \ - $(LIBS) \ - $(LIBFT_LIB) $(OBJS) $(FT_OTOOL_OBJ) \ - -o $@ - @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) - @strip -x $@ - @printf "\r\e[48;5;15;38;5;25m✅ MAKE $@\e[0m\e[K\n" - -$(OBJ_DIR)%.o: $(SRC_DIR)%.c | $(OBJ_DIR) - @$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB))))) - @$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB))))) - @$(eval COLOR=$(shell echo $$(($(PERCENT)%35+196)))) - @$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB))))) - @printf "\r\e[38;5;11m⌛ MAKE %10.10s : %2d%% \e[48;5;%dm%*s\e[0m%*s\e[48;5;255m \e[0m \e[38;5;11m %*s\e[0m\e[K" $@ $(PERCENT) $(COLOR) $(DONE) "" $(TO_DO) "" $(DELTA) "$@" - @$(CC) $(FLAGS) -MMD -c $< -o $@\ - -I $(INC_DIR) \ - -I $(LIBFT_INC) - @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) - -$(OBJ_DIR): - @$(MKDIR) $(OBJ_DIR) - @$(MKDIR) $(dir $(OBJS)) - -clean: - @$(RM) $(OBJ_DIR) - @printf "\r\e[38;5;202m✖ clean $(NAME).\e[0m\e[K\n" - -cleanlib: - @make -C $(LIBFT_DIR) clean - -fclean: clean - @$(RM) $(NAME) - @printf "\r\e[38;5;196m❌ fclean $(NAME).\e[0m\e[K\n" - -fcleanlib: cleanlib - @make -C $(LIBFT_DIR) fclean - -re: fclean relib all - -relib: fcleanlib $(LIBFT_LIB) + $(LIBS) $(LIBFT_LIB) $(OTOOL_OBJ) $(FLAGS) + @printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n" $(LIBFT_LIB): @make -C $(LIBFT_DIR) -.PHONY: fclean clean re relib cleanlib fcleanlib +$(OBJ_DIR) : + @mkdir -p $(OBJ_DIR) + @mkdir -p $(dir $(OBJS)) + +$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) + @$(eval DONE=$(shell echo $$(($(INDEX)*20/$(NB))))) + @$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB))))) + @$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB) - 1)))) + @$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}")) + @printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) nm/otool $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')" + @$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@\ + -I $(INC_DIR)\ + -I $(LIBFT_INC) + @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) + +clean: cleanlib + @if [ -e $(OBJ_DIR) ]; then \ + rm -rf $(OBJ_DIR); \ + printf "\r\033[38;5;202m✗ clean $(NAME) \033[0m\033[K\n"; \ + fi; + +cleanlib: + @make -C $(LIBFT_DIR) clean + +fclean: clean fcleanlib + @for file in $(NAME); do \ + if [ -e $$file ]; then \ + rm -f $$file ; \ + printf "\r\033[38;5;196m✗ fclean $$file\033[0m\033[K\n"; \ + fi; \ + done; + +fcleanlib: cleanlib + @make -C $(LIBFT_DIR) fclean + +re: fclean all + +relib: fcleanlib $(LIBFT_LIB) + +.PHONY : fclean clean re relib cleanlib fcleanlib -include $(OBJS:.o=.d) - diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index 348dc0da..3c33de5d 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/28 20:35:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/31 21:01:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,13 +52,20 @@ # define NM_OFORMAT (1 << 9) # define NM_MFORMAT (1 << 10) -typedef t_data_template t_nmdata; +typedef struct s_nmdata t_nmdata; typedef enum e_symtype t_symtype; typedef struct s_symbol t_symbol; typedef struct s_symbolmap t_symbolmap; typedef struct s_machodata t_machodata; typedef struct s_symbol t_symbol; +struct s_nmdata +{ + t_flag flag; + char **av_data; + char *filename; +}; + enum e_symtype { SYM_UNDF, @@ -72,7 +79,6 @@ enum e_symtype SYM_INDR, }; - struct s_machodata { void *file; @@ -94,7 +100,7 @@ struct s_symbol struct s_symbolmap { char c; - int (*format)(); + char *s; }; extern t_symbolmap g_symbolmap[]; @@ -111,9 +117,9 @@ int symbol_filter(t_list **syms, t_flag flag); void symbol_free(void *data, size_t size); 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); +void symbol_format_dfl(t_symbol *symbol); +void symbol_format_m(t_symbol *symbol); +void symbol_format_full(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/libft b/nm-otool/libft index 7cc44f76..3cfcbb41 160000 --- a/nm-otool/libft +++ b/nm-otool/libft @@ -1 +1 @@ -Subproject commit 7cc44f76141812079294c2e063bba6b64fb30d98 +Subproject commit 3cfcbb41103be5a16a8666b317ee397e9886fa4f diff --git a/nm-otool/src/symbol_format.c b/nm-otool/src/symbol_format.c deleted file mode 100644 index 8d24daa1..00000000 --- a/nm-otool/src/symbol_format.c +++ /dev/null @@ -1,103 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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', "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_symbol *symbol, t_nmdata *data) -{ - 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_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, '-', - 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%08llx \t%llx %-20s\n", - symbol->pos, - (symbol->nlist->n_type & N_STAB) >> 5, - (symbol->nlist->n_type & N_PEXT) >> 4, - symbol->nlist->n_type & N_TYPE, - symbol->nlist->n_type & N_EXT, - symbol->nlist->n_sect, symbol->section->sectname, - symbol->nlist->n_desc, symbol->nlist->n_value, - symbol->string); - return (0); -} - -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(symbol, data); - else - { - c = symbol->nlist->n_type & N_EXT ? map.c : map.c + 32; - ft_printf("%016llx %c %s\n", - symbol->nlist->n_value, c, symbol->string); - } - return (0); -} diff --git a/nm-otool/src/dump_symtab.c b/nm-otool/srcs/dump_symtab.c similarity index 100% rename from nm-otool/src/dump_symtab.c rename to nm-otool/srcs/dump_symtab.c diff --git a/nm-otool/src/fetch_header.c b/nm-otool/srcs/fetch_header.c similarity index 100% rename from nm-otool/src/fetch_header.c rename to nm-otool/srcs/fetch_header.c diff --git a/nm-otool/src/ft_nm.c b/nm-otool/srcs/ft_nm.c similarity index 87% rename from nm-otool/src/ft_nm.c rename to nm-otool/srcs/ft_nm.c index 0bbb2c4e..5788fb62 100644 --- a/nm-otool/src/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/03/28 20:48:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/31 21:50:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,6 @@ t_cliopts g_nm_opts[] = void mach_64_dump(struct mach_header_64 *file, t_nmdata *data) { t_machodata mach; - int (*format)(); mach.sects = NULL; mach.symbols = NULL; @@ -45,13 +44,7 @@ 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_MFORMAT) - format = symbol_format_m; - else if (data->flag & NM_FULL) - format = symbol_format_full; - else - format = symbol_format; - ft_lstiter(mach.symbols, format, data); + ft_lstiter(mach.symbols, symbol_format, data); } int nm(void *file, t_nmdata *data) @@ -77,12 +70,11 @@ int main(int ac, char **av) int i; struct stat buf; - g_argv = av; data.flag = NM_ASORT; if (cliopts_get(av, g_nm_opts, &data)) { - ft_perror(); - ft_dprintf(2, NM_USAGE); + ft_perror("ft_nm"); + ft_dprintf(2, NM_USAGE"\n"); return (1); } i = data.av_data - av; @@ -90,7 +82,7 @@ int main(int ac, char **av) { 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((data.filename = av[i]), O_RDONLY)) < 0) return (1); if ((fstat(fd, &buf)) < 0) return (1); diff --git a/nm-otool/src/ft_otool.c b/nm-otool/srcs/ft_otool.c similarity index 100% rename from nm-otool/src/ft_otool.c rename to nm-otool/srcs/ft_otool.c diff --git a/nm-otool/src/hexdump.c b/nm-otool/srcs/hexdump.c similarity index 100% rename from nm-otool/src/hexdump.c rename to nm-otool/srcs/hexdump.c diff --git a/nm-otool/src/mach_o_parse.c b/nm-otool/srcs/mach_o_parse.c similarity index 100% rename from nm-otool/src/mach_o_parse.c rename to nm-otool/srcs/mach_o_parse.c diff --git a/nm-otool/src/symbol_filter.c b/nm-otool/srcs/symbol_filter.c similarity index 100% rename from nm-otool/src/symbol_filter.c rename to nm-otool/srcs/symbol_filter.c diff --git a/nm-otool/srcs/symbol_format.c b/nm-otool/srcs/symbol_format.c new file mode 100644 index 00000000..9ae289ed --- /dev/null +++ b/nm-otool/srcs/symbol_format.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* symbol_format.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/28 20:34:32 by jhalford #+# #+# */ +/* Updated: 2017/03/31 21:49:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_nm_otool.h" + +t_symbolmap g_symbolmap[] = +{ + {'U', "undefined"}, + {'A', "absolute"}, + {'T', "segname missing"}, + {'D', "segname missing"}, + {'B', "segname missing"}, + {'C', "common"}, + {'-', "?"}, + {'S', "segname missing"}, + {'I', "indirect"}, +}; + +void symbol_format_desc(t_symbol *symbol) +{ + if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) + ft_printf(" [referenced dynamically]"); + ft_printf(" %sexternal", symbol->nlist->n_type & N_EXT ? "" : "non-"); + ft_printf("%s", + !(symbol->nlist->n_type & N_EXT) + && symbol->nlist->n_type & N_PEXT ? " (was a private externel)" : ""); +} + +void symbol_format_m(t_symbol *symbol) +{ + t_symbolmap map; + + map = g_symbolmap[symbol->type]; + ft_printf(" (%s%c%s)", + symbol->section ? symbol->section->segname : map.s, + symbol->section ? ',' : 0, + symbol->section ? symbol->section->sectname : ""); + symbol_format_desc(symbol); +} + +void symbol_format_full(t_symbol *symbol) +{ + ft_printf("\t%03b|%b|%x|%b \t%i(%s) \t%04x", + symbol->pos, + (symbol->nlist->n_type & N_STAB) >> 5, + (symbol->nlist->n_type & N_PEXT) >> 4, + symbol->nlist->n_type & N_TYPE, + symbol->nlist->n_type & N_EXT, + symbol->nlist->n_sect, symbol->section->sectname, + symbol->nlist->n_desc); +} + +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 (symbol->type == SYM_STAB) + ft_printf(" %02x %04b %#x", + symbol->nlist->n_sect, symbol->nlist->n_desc, + symbol->nlist->n_type); +} + +int symbol_format(t_symbol *symbol, t_nmdata *data) +{ + + if (data->flag & NM_OFORMAT) + ft_printf("%s: ", data->filename); + if (!(data->flag & NM_ONLY_UNDF)) + { + if (symbol->type == SYM_UNDF) + ft_printf("%16s", " "); + else + ft_printf("%016llx", symbol->nlist->n_value); + if (data->flag & NM_MFORMAT) + symbol_format_m(symbol); + else if (data->flag & NM_FULL) + symbol_format_full(symbol); + else + symbol_format_dfl(symbol); + } + ft_printf(" %s\n", symbol->string); + return (0); +} diff --git a/nm-otool/src/symbol_free.c b/nm-otool/srcs/symbol_free.c similarity index 100% rename from nm-otool/src/symbol_free.c rename to nm-otool/srcs/symbol_free.c diff --git a/nm-otool/src/symbol_init.c b/nm-otool/srcs/symbol_init.c similarity index 100% rename from nm-otool/src/symbol_init.c rename to nm-otool/srcs/symbol_init.c diff --git a/nm-otool/src/symbol_sort.c b/nm-otool/srcs/symbol_sort.c similarity index 100% rename from nm-otool/src/symbol_sort.c rename to nm-otool/srcs/symbol_sort.c diff --git a/nm-otool/update_makefile.sh b/nm-otool/update_makefile.sh deleted file mode 100755 index a1b3228d..00000000 --- a/nm-otool/update_makefile.sh +++ /dev/null @@ -1,17 +0,0 @@ -MYPATH=$(pwd) -CUR_MAKEFILE=$MYPATH/Makefile -if [ -e $CUR_MAKEFILE ] -then - echo "regenerate Makefile" - sed "`grep -n 'SRC_BASE =' $CUR_MAKEFILE | sed 's/:.*//'`, \$d" $CUR_MAKEFILE > NEWMAKEFILE - grep 'SRC_BASE =' $CUR_MAKEFILE >> NEWMAKEFILE - expr "$(find ./src | grep "\.c" | sed -e 's/src\///' -e 's/\.\///' -e 's/$/\\/')" : "\(.*\).$" >> NEWMAKEFILE - echo "" >> NEWMAKEFILE - grep 'SRCS =' $CUR_MAKEFILE >> NEWMAKEFILE - sed "1, `grep -n 'SRCS =' $CUR_MAKEFILE | sed 's/:.*//'`d" $CUR_MAKEFILE >> NEWMAKEFILE - mv $CUR_MAKEFILE ~/Documents/.OLDMakefile - mv NEWMAKEFILE $CUR_MAKEFILE - echo "Makefile done (copy still alive in ~/Documents/.OLDMakefile)" -else - echo "Makefile not found." -fi