commit d6732360a1948ba0084d5ca6ca28b11e16651fff Author: Jack Halford Date: Sun Feb 19 03:56:59 2017 +0100 debut de nm d'apres la video de l'intra diff --git a/nm-otool/.gitmodules b/nm-otool/.gitmodules new file mode 100644 index 00000000..c82793fb --- /dev/null +++ b/nm-otool/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libft"] + path = libft + url = https://github.com/jzck/libft diff --git a/nm-otool/Makefile b/nm-otool/Makefile new file mode 100644 index 00000000..d7903305 --- /dev/null +++ b/nm-otool/Makefile @@ -0,0 +1,84 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: jhalford +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2017/02/19 03:29:38 by jhalford #+# #+# # +# Updated: 2017/02/19 03:56:22 by jhalford ### ########.fr # +# # +# **************************************************************************** # + +NAME = ft_nm + +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) + +DELTA = $$(echo "$$(tput cols)-47"|bc) + +LIBFT_DIR = libft/ +LIBFT_LIB = $(LIBFT_DIR)libft.a +LIBFT_INC = $(LIBFT_DIR)includes/ + +SRC_DIR = src/ +INC_DIR = includes/ +OBJ_DIR = objs/ + +SRC_BASE = \ +ft_nm.c + +SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) +OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) +NB = $(words $(SRC_BASE)) +INDEX = 0 + +all: + @make -j $(NAME) + +$(NAME): $(OBJ_DIR) $(OBJS) + @$(CC) $(FLAGS) $(D_FLAGS) \ + -I $(INC_DIR) \ + -I $(LIBFT_INC) \ + $(LIBS) \ + $(LIBFT_LIB) $(OBJS) \ + -o $(NAME) + @$(eval INDEX=$(shell echo $$(($(INDEX)+1)))) + @strip -x $(NAME) + @printf "\r\e[48;5;15;38;5;25m✅ MAKE $(NAME)\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" $(NAME) $(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" + +fclean: clean + @$(RM) $(NAME) + @printf "\r\e[38;5;196m❌ fclean $(NAME).\e[0m\e[K\n" + +re: fclean all + +.PHONY: all clean fclean re + +-include $(OBJS:.o=.d) + diff --git a/nm-otool/ft_nm b/nm-otool/ft_nm new file mode 100755 index 00000000..776fcb2b Binary files /dev/null and b/nm-otool/ft_nm differ diff --git a/nm-otool/includes/ft_nm.h b/nm-otool/includes/ft_nm.h new file mode 100644 index 00000000..7b70ee1d --- /dev/null +++ b/nm-otool/includes/ft_nm.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nm.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/19 03:09:10 by jhalford #+# #+# */ +/* Updated: 2017/02/19 03:32:51 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_NM_H +# define FT_NM_H + +# include "libft.h" +# include +# include +# include +# include +# include +# include +# include + +#endif diff --git a/nm-otool/libft b/nm-otool/libft new file mode 160000 index 00000000..0ca8ca81 --- /dev/null +++ b/nm-otool/libft @@ -0,0 +1 @@ +Subproject commit 0ca8ca817f32fc0345ef93ef74a3abe2583bd89c diff --git a/nm-otool/src/ft_nm.c b/nm-otool/src/ft_nm.c new file mode 100644 index 00000000..e15c128e --- /dev/null +++ b/nm-otool/src/ft_nm.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_nm.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ +/* Updated: 2017/02/19 03:56:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_nm.h" + +void print_output(int nsyms, int symoff, int stroff, void *ptr) +{ + int i; + char *stringtable; + struct nlist_64 *array; + + array = ptr + symoff; + stringtable = (void*)ptr + stroff; + for (i = 0; i < nsyms; ++i) + { + ft_printf("%s\n", stringtable + array[i].n_un.n_strx); + } +} + +void handle_64(void *ptr) +{ + int ncmds; + int i; + struct mach_header_64 *header; + struct load_command *lc; + struct symtab_command *sym; + + header = (struct mach_header_64*)ptr; + ncmds = header->ncmds; + lc = ptr + sizeof(*header); + for (i = 0; i < ncmds; i++) + { + if (lc->cmd == LC_SYMTAB) + { + sym = (struct symtab_command*)lc; + /* ft_printf("nb symbols: %d\n", sym->nsyms); */ + print_output(sym->nsyms, sym->symoff, sym->stroff, ptr); + break ; + } + lc = (void*)lc + lc->cmdsize; + } +} + +void nm(char *ptr) +{ + unsigned int magic_number; + + magic_number = *(int *)ptr; + if (magic_number == MH_MAGIC_64) + { + /* ft_printf("je suis un binaire 64 bits\n"); */ + handle_64(ptr); + } +} + +int main(int ac, char **av) +{ + int fd; + char *ptr; + struct stat buf; + + if (ac != 2) + { + ft_dprintf(2, "Please give me an arg\n"); + return (1); + } + if ((fd = open(av[1], O_RDONLY)) < 0) + return (1); + if ((fstat(fd, &buf)) < 0) + return (1); + if ((ptr = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) + == MAP_FAILED) + return (1); + nm(ptr); + if (munmap(ptr, buf.st_size)) + return (1); + return (0); +} diff --git a/nm-otool/update_makefile.sh b/nm-otool/update_makefile.sh new file mode 100755 index 00000000..a1b3228d --- /dev/null +++ b/nm-otool/update_makefile.sh @@ -0,0 +1,17 @@ +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