debut de nm d'apres la video de l'intra
This commit is contained in:
commit
d6732360a1
7 changed files with 217 additions and 0 deletions
3
nm-otool/.gitmodules
vendored
Normal file
3
nm-otool/.gitmodules
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
[submodule "libft"]
|
||||
path = libft
|
||||
url = https://github.com/jzck/libft
|
||||
84
nm-otool/Makefile
Normal file
84
nm-otool/Makefile
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
# **************************************************************************** #
|
||||
# #
|
||||
# ::: :::::::: #
|
||||
# Makefile :+: :+: :+: #
|
||||
# +:+ +:+ +:+ #
|
||||
# By: jhalford <jack@crans.org> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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)
|
||||
|
||||
BIN
nm-otool/ft_nm
Executable file
BIN
nm-otool/ft_nm
Executable file
Binary file not shown.
25
nm-otool/includes/ft_nm.h
Normal file
25
nm-otool/includes/ft_nm.h
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_nm.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stdio.h>
|
||||
# include <sys/mman.h>
|
||||
# include <mach-o/loader.h>
|
||||
# include <mach-o/nlist.h>
|
||||
# include <fcntl.h>
|
||||
# include <sys/stat.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
#endif
|
||||
1
nm-otool/libft
Submodule
1
nm-otool/libft
Submodule
|
|
@ -0,0 +1 @@
|
|||
Subproject commit 0ca8ca817f32fc0345ef93ef74a3abe2583bd89c
|
||||
87
nm-otool/src/ft_nm.c
Normal file
87
nm-otool/src/ft_nm.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_nm.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
17
nm-otool/update_makefile.sh
Executable file
17
nm-otool/update_makefile.sh
Executable file
|
|
@ -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
|
||||
Loading…
Reference in a new issue