refatored functions into 'dump' atoms to make for a clearer view of the Mach-O layout

This commit is contained in:
Jack Halford 2017-02-20 00:59:42 +01:00
parent 6319a23950
commit 0e0fda4740
4 changed files with 66 additions and 46 deletions

View file

@ -42,7 +42,7 @@ INDEX = 0
all: all:
@make -j $(NAME) @make -j $(NAME)
$(NAME): $(OBJ_DIR) $(OBJS) $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
@$(CC) $(FLAGS) $(D_FLAGS) \ @$(CC) $(FLAGS) $(D_FLAGS) \
-I $(INC_DIR) \ -I $(INC_DIR) \
-I $(LIBFT_INC) \ -I $(LIBFT_INC) \
@ -72,13 +72,24 @@ clean:
@$(RM) $(OBJ_DIR) @$(RM) $(OBJ_DIR)
@printf "\r\e[38;5;202m✖ clean $(NAME).\e[0m\e[K\n" @printf "\r\e[38;5;202m✖ clean $(NAME).\e[0m\e[K\n"
cleanlib:
@make -C $(LIBFT_DIR) clean
fclean: clean fclean: clean
@$(RM) $(NAME) @$(RM) $(NAME)
@printf "\r\e[38;5;196m❌ fclean $(NAME).\e[0m\e[K\n" @printf "\r\e[38;5;196m❌ fclean $(NAME).\e[0m\e[K\n"
fcleanlib: cleanlib
@make -C $(LIBFT_DIR) fclean
re: fclean all re: fclean all
.PHONY: all clean fclean re relib: fcleanlib $(LIBFT_LIB)
$(LIBFT_LIB):
@make -C $(LIBFT_DIR)
.PHONY: fclean clean re relib cleanlib fcleanlib
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

View file

@ -22,4 +22,6 @@
# include <sys/stat.h> # include <sys/stat.h>
# include <stdlib.h> # include <stdlib.h>
# define IS_MAGIC_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64)
#endif #endif

@ -1 +1 @@
Subproject commit 0ca8ca817f32fc0345ef93ef74a3abe2583bd89c Subproject commit 5e978382fff27082681383029d99f76ae5a25ddc

View file

@ -12,69 +12,76 @@
#include "ft_nm.h" #include "ft_nm.h"
void print_output(int nsyms, int symoff, int stroff, void *ptr) void dump_section_64(struct section_64* sect)
{ {
int i; ft_printf("section64: sectname=[%s] addr=[%p]\n", sect->sectname, sect->addr);
char *stringtable; }
struct nlist_64 *array;
array = ptr + symoff; void dump_segment_64(struct segment_command_64 *seg, void *file)
stringtable = (void*)ptr + stroff; {
for (i = 0; i < nsyms; ++i) uint32_t nsects;
uint32_t i;
struct section_64 *sect;
(void)file;
nsects = seg->nsects;
ft_printf("LC_SEGMENT_64: segname=[%s], nsects=[%i]\n", seg->segname, nsects);
sect = (void*)seg + sizeof(*seg);
for (i = 0; i < nsects; i++)
{ {
ft_printf("%s\n", stringtable + array[i].n_un.n_strx); dump_section_64(sect);
sect = (void*)sect + sect->size;
} }
} }
void handle_64(void *ptr) void dump_symtab(struct symtab_command *sym, void *file)
{ {
int ncmds;
int i; int i;
struct mach_header_64 *header; int nsyms;
struct load_command *lc; char *stringtable;
struct symtab_command *sym; struct nlist_64 *array;
nsyms = sym->nsyms;
array = file + sym->symoff;
stringtable = (void*)file + sym->stroff;
ft_printf("{und}LC_SYMTAB with %d symbols:{eoc}\n", sym->nsyms);
for (i = 0; i < nsyms; ++i)
ft_printf("%s\n", stringtable + array[i].n_un.n_strx);
}
void dump_load_commands(void *file)
{
uint32_t ncmds;
uint32_t i;
struct load_command *lc;
struct mach_header_64 *header = file;
header = (struct mach_header_64*)ptr;
ncmds = header->ncmds; ncmds = header->ncmds;
lc = ptr + sizeof(*header); lc = file + sizeof(*header);
for (i = 0; i < ncmds; i++) for (i = 0; i < ncmds; i++)
{ {
ft_printf("{yel}load_command #%d: %i{eoc}\n", i, lc->cmd);
if (lc->cmd == LC_SYMTAB) if (lc->cmd == LC_SYMTAB)
{ dump_symtab((struct symtab_command*)lc, file);
sym = (struct symtab_command*)lc; else if (lc->cmd == LC_SEGMENT_64)
/* ft_printf("nb symbols: %d\n", sym->nsyms); */ dump_segment_64((struct segment_command_64*)lc, file);
print_output(sym->nsyms, sym->symoff, sym->stroff, ptr);
break ;
}
lc = (void*)lc + lc->cmdsize; lc = (void*)lc + lc->cmdsize;
} }
} }
void nm(char *ptr) void nm(char *file)
{ {
unsigned int magic_number; uint32_t magic = *(int *)file;
int is_64 = IS_MAGIC_64(magic);
magic_number = *(int *)ptr; ft_printf("{gre}I'm %s a 64 bit binary{eoc}\n", is_64 ? "" : "not");
if (magic_number == MH_MAGIC_64 || magic_number == MH_CIGAM_64) dump_load_commands(file);
{
ft_printf("I'm a 64 it binary\n");
handle_64(ptr);
}
else if (magic_number == MH_MAGIC)
{
ft_printf("I'm a 32 it binary\n");
/* handle_32(ptr); */
}
else
{
ft_printf("unknown architecure\n");
}
} }
int main(int ac, char **av) int main(int ac, char **av)
{ {
int fd; int fd;
char *ptr; char *file;
struct stat buf; struct stat buf;
if (ac != 2) if (ac != 2)
@ -86,11 +93,11 @@ int main(int ac, char **av)
return (1); return (1);
if ((fstat(fd, &buf)) < 0) if ((fstat(fd, &buf)) < 0)
return (1); return (1);
if ((ptr = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED) == MAP_FAILED)
return (1); return (1);
nm(ptr); nm(file);
if (munmap(ptr, buf.st_size)) if (munmap(file, buf.st_size))
return (1); return (1);
return (0); return (0);
} }