From aecf9be263534c0e49f86f06d2f90831eac74329 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 31 Oct 2017 19:59:45 +0100 Subject: [PATCH] fat binaries done, otool doesnt work on object files --- nm-otool/42corr.sh | 31 ---------- nm-otool/includes/ft_nm_otool.h | 11 ++-- nm-otool/includes/ft_otool.h | 11 +++- nm-otool/includes/mach.h | 4 +- nm-otool/includes/mach_64.h | 4 +- nm-otool/libft/srcs/lst/ft_lstsort.c | 4 +- nm-otool/scripts/42corr.sh | 53 +++++++++++++++++ nm-otool/{ => scripts}/gen_mach.sh | 0 nm-otool/{ => scripts}/nmdiff.sh | 4 +- nm-otool/scripts/nmdiff_dir.sh | 6 ++ nm-otool/{ => scripts}/otooldiff.sh | 4 +- nm-otool/srcs/fat.c | 8 +-- nm-otool/srcs/ft_nm.c | 74 ++++++++++++++---------- nm-otool/srcs/ft_otool.c | 64 +++++++++++++++----- nm-otool/srcs/mach/mach.c | 22 ++++--- nm-otool/srcs/mach/symbol_filter.c | 6 +- nm-otool/srcs/mach/symbol_format.c | 22 +++---- nm-otool/srcs/mach/symbol_init.c | 23 ++++---- nm-otool/srcs/mach/symbol_sort.c | 6 +- nm-otool/srcs/mach_64/get_section_64.c | 22 ++++--- nm-otool/srcs/mach_64/mach_64.c | 6 +- nm-otool/srcs/mach_64/otool_mach_64.c | 14 ++++- nm-otool/srcs/mach_64/symbol_64_filter.c | 6 +- nm-otool/srcs/mach_64/symbol_64_format.c | 26 +++++---- nm-otool/srcs/mach_64/symbol_64_init.c | 23 ++++---- nm-otool/srcs/mach_64/symbol_64_sort.c | 8 +-- 26 files changed, 280 insertions(+), 182 deletions(-) delete mode 100755 nm-otool/42corr.sh create mode 100755 nm-otool/scripts/42corr.sh rename nm-otool/{ => scripts}/gen_mach.sh (100%) rename nm-otool/{ => scripts}/nmdiff.sh (60%) create mode 100755 nm-otool/scripts/nmdiff_dir.sh rename nm-otool/{ => scripts}/otooldiff.sh (59%) diff --git a/nm-otool/42corr.sh b/nm-otool/42corr.sh deleted file mode 100755 index fc20a6bf..00000000 --- a/nm-otool/42corr.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh - -make - -########## -# NM -########## - -#test_facile -./nmdiff.sh tests/test_facile - -#test_moins_facile en 64 et 32 -./nmdiff.sh tests/test_moins_facile -./nmdiff.sh tests/test_moins_facile_32-bit - -#arguments mutilples -./nmdiff.sh tests/test_facile tests/test_facile - -#fichiers objets -./nmdiff.sh objs/ft_nm.o - -#bibliotheques dynamiques -./nmdiff.sh - -#biblioteque universelle -./nmdiff.sh - - -########## -# OTOOL -########## diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index 57a4601d..2aa924c8 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/10/30 16:37:24 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:03:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include # include # include +# include # include "ft_nm.h" # include "ft_otool.h" @@ -31,10 +32,10 @@ # include "mach_64.h" # include "mach.h" -# define IS_MACH_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) -# define IS_MACH_32(x) (x == MH_MAGIC || x == MH_CIGAM) -# define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) -# define IS_REV(x) (x == MH_CIGAM || x == MH_CIGAM_64 || x == FAT_CIGAM) +# define IS_MACH_64(x) (*(uint32_t*)x == MH_MAGIC_64 || *(uint32_t*)x == MH_CIGAM_64) +# define IS_MACH_32(x) (*(uint32_t*)x == MH_MAGIC || *(uint32_t*)x == MH_CIGAM) +# define IS_FAT(x) (*(uint32_t*)x == FAT_MAGIC || *(uint32_t*)x == FAT_CIGAM) +# define IS_REV(x) (*(uint32_t*)x == MH_CIGAM || *(uint32_t*)x == MH_CIGAM_64 || *(uint32_t*)x == FAT_CIGAM) uint64_t endian(uint64_t n, uint8_t size); uint8_t bswap_8(uint8_t x); diff --git a/nm-otool/includes/ft_otool.h b/nm-otool/includes/ft_otool.h index a152c3ea..1305b42c 100644 --- a/nm-otool/includes/ft_otool.h +++ b/nm-otool/includes/ft_otool.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:46:23 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:00:39 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:15:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,13 @@ # define OTOOL_TEXT (1 << 0) # define OTOOL_DATA (1 << 1) -typedef t_data_template t_otooldata; +typedef struct s_otooldata t_otooldata; + +struct s_otooldata +{ + t_flag flag; + char **av_data; + char *filename; +}; #endif diff --git a/nm-otool/includes/mach.h b/nm-otool/includes/mach.h index b5831d18..e0bb12eb 100644 --- a/nm-otool/includes/mach.h +++ b/nm-otool/includes/mach.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:17:17 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:30:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ struct s_symbol int pos; t_symtype type; char *string; - struct nlist *nlist; + struct nlist nlist; struct section *section; }; diff --git a/nm-otool/includes/mach_64.h b/nm-otool/includes/mach_64.h index edd41aa4..5a00a36d 100644 --- a/nm-otool/includes/mach_64.h +++ b/nm-otool/includes/mach_64.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:22:12 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:30:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ struct s_symbol_64 int pos; t_symtype type; char *string; - struct nlist_64 *nlist; + struct nlist_64 nlist; struct section_64 *section; }; diff --git a/nm-otool/libft/srcs/lst/ft_lstsort.c b/nm-otool/libft/srcs/lst/ft_lstsort.c index 950d6fd3..2f5a1c02 100644 --- a/nm-otool/libft/srcs/lst/ft_lstsort.c +++ b/nm-otool/libft/srcs/lst/ft_lstsort.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/04 11:09:58 by jhalford #+# #+# */ -/* Updated: 2017/01/09 12:29:58 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:05:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ void ft_lstsort(t_list **begin_list, int (*cmp)()) indirect = begin_list; if (!*begin_list) return ; + int i = 0; while (*indirect && (*indirect)->next) { if ((*cmp)((*indirect)->content, (*indirect)->next->content) > 0) @@ -31,6 +32,7 @@ void ft_lstsort(t_list **begin_list, int (*cmp)()) *indirect = tmp2; (*indirect)->next = tmp; indirect = begin_list; + i = 0; } else indirect = &(*indirect)->next; diff --git a/nm-otool/scripts/42corr.sh b/nm-otool/scripts/42corr.sh new file mode 100755 index 00000000..91445069 --- /dev/null +++ b/nm-otool/scripts/42corr.sh @@ -0,0 +1,53 @@ +#!/bin/sh + +make + +########## +# NM +########## + +#test_facile +./scripts/nmdiff.sh tests/test_facile + +#test_moins_facile en 64 et 32 +./scripts/nmdiff.sh tests/test_moins_facile +./scripts/nmdiff.sh tests/test_moins_facile_32-bit + +#arguments mutilples +./scripts/nmdiff.sh tests/test_facile tests/test_facile + +#fichiers objets +./scripts/nmdiff.sh objs/ft_nm.o + +#bibliotheques dynamiques +./scripts/nmdiff.sh /usr/lib/libcompression.dylib + +#biblioteque universelle +./scripts/nmdiff.sh /usr/bin/audiodevice +./scripts/nmdiff.sh /usr/bin/python + + + +########## +# OTOOL +########## + +#test_facile +./scripts/otooldiff.sh -t tests/test_facile + +#test_moins_facile en 64 et 32 +./scripts/otooldiff.sh -t tests/test_moins_facile +./scripts/otooldiff.sh -t tests/test_moins_facile_32-bit + +#arguments mutilples +./scripts/otooldiff.sh -t tests/test_facile tests/test_facile + +#fichiers objets +./scripts/otooldiff.sh -t objs/ft_nm.o + +#bibliotheques dynamiques +./scripts/otooldiff.sh -t /usr/lib/libcompression.dylib + +#biblioteque universelle +./scripts/otooldiff.sh -t /usr/bin/audiodevice +./scripts/otooldiff.sh -t /usr/bin/python diff --git a/nm-otool/gen_mach.sh b/nm-otool/scripts/gen_mach.sh similarity index 100% rename from nm-otool/gen_mach.sh rename to nm-otool/scripts/gen_mach.sh diff --git a/nm-otool/nmdiff.sh b/nm-otool/scripts/nmdiff.sh similarity index 60% rename from nm-otool/nmdiff.sh rename to nm-otool/scripts/nmdiff.sh index bfa4d834..07283c24 100755 --- a/nm-otool/nmdiff.sh +++ b/nm-otool/scripts/nmdiff.sh @@ -1,7 +1,7 @@ #!/usr/bin/env zsh diff <(./ft_nm $@) <(nm $@) if [ $? -eq 0 ]; then - echo "OK ✅" + echo "nm $@\t OK ✅ " else - echo "ERROR ❎" + echo "nm $@\t ERROR ✅ " fi diff --git a/nm-otool/scripts/nmdiff_dir.sh b/nm-otool/scripts/nmdiff_dir.sh new file mode 100755 index 00000000..ce78c77a --- /dev/null +++ b/nm-otool/scripts/nmdiff_dir.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +for file in $@/*; do + printf "%-40s" $file + ./nmdiff.sh $file 2>&- +done diff --git a/nm-otool/otooldiff.sh b/nm-otool/scripts/otooldiff.sh similarity index 59% rename from nm-otool/otooldiff.sh rename to nm-otool/scripts/otooldiff.sh index 0b6033b8..34e75f56 100755 --- a/nm-otool/otooldiff.sh +++ b/nm-otool/scripts/otooldiff.sh @@ -1,7 +1,7 @@ #!/usr/bin/env zsh diff <(./ft_otool $@) <(otool $@) if [ $? -eq 0 ]; then - echo "OK ✅" + echo "otool $@\t OK ✅ " else - echo "ERROR ❎" + echo "otool $@\t ERROR ✅ " fi diff --git a/nm-otool/srcs/fat.c b/nm-otool/srcs/fat.c index 0bb4395d..5a4f82ce 100644 --- a/nm-otool/srcs/fat.c +++ b/nm-otool/srcs/fat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 16:31:48 by jhalford #+# #+# */ -/* Updated: 2017/10/30 17:49:28 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 17:45:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,11 +23,9 @@ void *fat_extract(struct fat_header *fat, arch = (struct fat_arch*)(fat + 1); while (narch--) { - if (endian(arch->cputype, 32) & cputype - && endian(arch->cpusubtype, 32) & cpusubtype) - { + if ((int32_t)endian(arch->cputype, 32) == cputype + && (int32_t)endian(arch->cpusubtype, 32) & cpusubtype) return ((void *)fat + endian(arch->offset, 32)); - } ++arch; } return (NULL); diff --git a/nm-otool/srcs/ft_nm.c b/nm-otool/srcs/ft_nm.c index 38729c4e..7095d4db 100644 --- a/nm-otool/srcs/ft_nm.c +++ b/nm-otool/srcs/ft_nm.c @@ -6,12 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/10/30 18:36:32 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:45:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -#include #define NM_USAGE "usage: nm [-agmnpruU] filename ..." t_machodata *g_data = NULL; @@ -31,45 +30,54 @@ t_cliopts g_nm_opts[] = {0xff, "full", NM_FULL, 0, NULL, 0}, {'o', NULL, NM_OFORMAT, 0, NULL, 0}, {'m', NULL, NM_MFORMAT, 0, NULL, 0}, + {0, 0, 0, 0, 0, 0}, }; -int nm_file(void *file, t_nmdata *data); +void nm_single_file(void *file, t_nmdata *data); -int nm_fat(struct fat_header *fat, t_nmdata *data) +void nm_fat_file(struct fat_header *fat, t_nmdata *data) { - void *arch; - struct utsname host; + uint32_t narch; + struct fat_arch *obj; - uname(&host); - NXGetLocalArchInfo(); - - arch = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); - if (!arch) - ft_printf("{red}fat doesn't have x86_64 !{eoc}"); + g_rev = IS_REV(fat); + obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); + if (obj) + nm_single_file(obj, data); else { - uint32_t magic; - magic = *(int*)arch; - nm_file(arch, data); + narch = endian(fat->nfat_arch, 32); + obj = (struct fat_arch*)(fat + 1); + while (narch--) + { + const NXArchInfo *arch = NXGetArchInfoFromCpuType( + endian(obj->cputype, 32), + endian(obj->cpusubtype, 32)); + ft_printf("\n%s (for architecture %s):\n", data->filename, arch->name); + nm_single_file((void*)fat + endian(obj->offset, 32), data); + g_rev = IS_REV(fat); + ++obj; + } } - return (0); } -int nm_file(void *file, t_nmdata *data) +void nm_single_file(void *file, t_nmdata *data) { - uint32_t magic; - - magic = *(int*)file; - g_rev = IS_REV(magic); - if (IS_MACH_32(magic)) + g_rev = IS_REV(file); + if (IS_MACH_32(file)) nm_mach(file, data); - else if (IS_MACH_64(magic)) + else if (IS_MACH_64(file)) nm_mach_64(file, data); - else if (IS_FAT(magic)) - nm_fat(file, data); else - ft_printf("{red}unsupported arch:{eoc} magic=%#x\n", magic); - return (0); + { + const NXArchInfo *arch = NXGetArchInfoFromCpuType( + endian(*((int32_t*)file + 1), 32), + endian(*((int32_t*)file + 2), 32)); + if (arch) + ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name); + else + ft_dprintf(2, "unknown architecture, magic=%#x\n", *((int32_t*)file)); + } } int nm(int ac, char **av, t_nmdata data) @@ -77,14 +85,13 @@ int nm(int ac, char **av, t_nmdata data) int i; struct stat buf; int fd; - char *file; + void *file; i = data.av_data - av; while (i < ac && av[i]) { + g_rev = 0; data.filename = av[i]; - 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((data.filename), O_RDONLY)) < 0) return (1); if ((fstat(fd, &buf)) < 0) @@ -92,7 +99,12 @@ int nm(int ac, char **av, t_nmdata data) if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) return (1); - nm_file(file, &data); + if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) + ft_printf("\n%s:\n", data.filename); + if (IS_FAT(file)) + nm_fat_file(file, &data); + else + nm_single_file(file, &data); if (munmap(file, buf.st_size)) return (1); i++; diff --git a/nm-otool/srcs/ft_otool.c b/nm-otool/srcs/ft_otool.c index 30ee86ba..dfd06545 100644 --- a/nm-otool/srcs/ft_otool.c +++ b/nm-otool/srcs/ft_otool.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */ -/* Updated: 2017/10/30 17:31:44 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:45:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,20 +20,51 @@ t_cliopts g_otool_opts[] = }; int g_rev = 0; -void otool_file(void *file, t_otooldata *data) -{ - uint32_t magic; +void otool_single_file(void *file, t_otooldata *data); - magic = *(int *)file; - g_rev = IS_REV(magic); - if (IS_MACH_32(magic)) - otool_mach(file, data); - else if (IS_MACH_64(magic)) - otool_mach_64(file, data); - else if (IS_FAT(magic)) - ft_printf("fat binary not supported yet\n"); +void otool_fat_file(struct fat_header *fat, t_otooldata *data) +{ + uint32_t narch; + struct fat_arch *obj; + + g_rev = IS_REV(fat); + obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); + if (obj) + otool_single_file(obj, data); else - ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); + { + narch = endian(fat->nfat_arch, 32); + obj = (struct fat_arch*)(fat + 1); + while (narch--) + { + const NXArchInfo *arch = NXGetArchInfoFromCpuType( + endian(obj->cputype, 32), + endian(obj->cpusubtype, 32)); + ft_printf("\n%s (for architecture %s):\n", data->filename, arch->name); + otool_single_file((void*)fat + endian(obj->offset, 32), data); + g_rev = IS_REV(fat); + ++obj; + } + } +} + +void otool_single_file(void *file, t_otooldata *data) +{ + g_rev = IS_REV(file); + if (IS_MACH_32(file)) + otool_mach(file, data); + else if (IS_MACH_64(file)) + otool_mach_64(file, data); + else + { + const NXArchInfo *arch = NXGetArchInfoFromCpuType( + endian(*((int32_t*)file + 1), 32), + endian(*((int32_t*)file + 2), 32)); + if (arch) + ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name); + else + ft_dprintf(2, "unknown architecture, magic=%#x\n", *((int32_t*)file)); + } } int otool(int ac, char **av, t_otooldata data) @@ -41,7 +72,7 @@ int otool(int ac, char **av, t_otooldata data) int i; struct stat buf; int fd; - char *file; + void *file; i = data.av_data - av; while (i < ac && av[i]) @@ -54,7 +85,10 @@ int otool(int ac, char **av, t_otooldata data) if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) return (1); - otool_file(file, &data); + if (IS_FAT(file)) + otool_fat_file(file, &data); + else + otool_single_file(file, &data); if (munmap(file, buf.st_size)) return (1); i++; diff --git a/nm-otool/srcs/mach/mach.c b/nm-otool/srcs/mach/mach.c index 66e66ebe..be8ab8df 100644 --- a/nm-otool/srcs/mach/mach.c +++ b/nm-otool/srcs/mach/mach.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */ -/* Updated: 2017/10/30 11:32:11 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 17:46:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,10 +20,10 @@ static void symtab_parse(t_machodata *data, struct symtab_command *symtab) struct nlist *array; data->symtab = symtab; - stringtable = data->file + symtab->stroff; - array = (struct nlist*)(data->file + symtab->symoff); + stringtable = data->file + endian(symtab->stroff, 32); + array = (struct nlist*)(data->file + endian(symtab->symoff, 32)); i = -1; - while (++i < (int)symtab->nsyms) + while (++i < (int)endian(symtab->nsyms, 32)) { symbol_init(&symbol, stringtable, array, i); symbol_set(&symbol, data); @@ -35,9 +35,9 @@ static void seg_parse(t_machodata *data, struct segment_command *seg) { uint32_t nsects; uint32_t i; - struct section *sect; + struct section *sect; - nsects = seg->nsects; + nsects = endian(seg->nsects, 32); sect = (void*)(seg + 1); i = -1; while (++i < nsects) @@ -55,17 +55,15 @@ void mach_parse(t_machodata *data) struct mach_header *header; header = data->file; - ncmds = header->ncmds; + ncmds = endian(header->ncmds, 32); lc = (void*)(header + 1); i = -1; while (++i < ncmds) { - if (lc->cmd == LC_SYMTAB) + if (endian(lc->cmd, 32) == LC_SYMTAB) symtab_parse(data, (struct symtab_command*)lc); - else if (lc->cmd == LC_DYSYMTAB) - data->dysymtab = (struct dysymtab_command*)lc; - else if (lc->cmd == LC_SEGMENT) + else if (endian(lc->cmd, 32) == LC_SEGMENT) seg_parse(data, (struct segment_command*)lc); - lc = (void*)lc + lc->cmdsize; + lc = (void*)lc + endian(lc->cmdsize, 32); } } diff --git a/nm-otool/srcs/mach/symbol_filter.c b/nm-otool/srcs/mach/symbol_filter.c index 315a3540..089c7026 100644 --- a/nm-otool/srcs/mach/symbol_filter.c +++ b/nm-otool/srcs/mach/symbol_filter.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */ -/* Updated: 2017/10/26 18:44:51 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:36:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,12 +19,12 @@ int cmp_symtype(t_symbol *sym, void *type) inline int is_not_external(t_symbol *s) { - return (!(s->nlist->n_type & N_EXT)); + return (!(s->nlist.n_type & N_EXT)); } inline int is_external(t_symbol *s) { - return (s->nlist->n_type & N_EXT); + return (s->nlist.n_type & N_EXT); } int symbol_filter(t_list **symbols, t_flag flag) diff --git a/nm-otool/srcs/mach/symbol_format.c b/nm-otool/srcs/mach/symbol_format.c index f087a291..02141dd5 100644 --- a/nm-otool/srcs/mach/symbol_format.c +++ b/nm-otool/srcs/mach/symbol_format.c @@ -15,14 +15,14 @@ t_symbolmap g_symbolmap[] = void symbol_format_desc(t_symbol *symbol) { - if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) + if (symbol->nlist.n_desc & REFERENCED_DYNAMICALLY) ft_printf(" [referenced dynamically]"); if (is_external(symbol)) ft_printf(" external"); else { ft_printf(" non-external"); - if (symbol->nlist->n_type & N_PEXT) + if (symbol->nlist.n_type & N_PEXT) ft_printf(" (was a private external)"); } } @@ -43,12 +43,12 @@ void symbol_format_full(t_symbol *symbol) { ft_printf("\t%i %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); + (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) @@ -62,8 +62,8 @@ void symbol_format_dfl(t_symbol *symbol) ft_printf(" %c", map.c + 'a' - 'A'); if (symbol->type == SYM_STAB) ft_printf(" %02x %04b %#x", - symbol->nlist->n_sect, symbol->nlist->n_desc, - symbol->nlist->n_type); + symbol->nlist.n_sect, symbol->nlist.n_desc, + symbol->nlist.n_type); } int symbol_format(t_symbol *symbol, t_nmdata *data) @@ -75,7 +75,7 @@ int symbol_format(t_symbol *symbol, t_nmdata *data) if (symbol->type == SYM_UNDF) ft_printf("%8s", " "); else - ft_printf("%08llx", symbol->nlist->n_value); + ft_printf("%08llx", symbol->nlist.n_value); if (data->flag & NM_MFORMAT) symbol_format_m(symbol); else if (data->flag & NM_FULL) diff --git a/nm-otool/srcs/mach/symbol_init.c b/nm-otool/srcs/mach/symbol_init.c index e429219f..2aa970ac 100644 --- a/nm-otool/srcs/mach/symbol_init.c +++ b/nm-otool/srcs/mach/symbol_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */ -/* Updated: 2017/10/26 18:23:24 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:42:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,17 +19,20 @@ int symbol_init(t_symbol *symbol, { symbol->type = 0; symbol->pos = i; - symbol->nlist = array + i; - symbol->string = stringtable + array[i].n_un.n_strx; + symbol->nlist.n_type = endian(array[i].n_type, 8); + symbol->nlist.n_sect = endian(array[i].n_sect, 8); + symbol->nlist.n_desc = endian(array[i].n_desc, 16); + symbol->nlist.n_value = endian(array[i].n_value, 32); + symbol->string = stringtable + endian(array[i].n_un.n_strx, 32); return (0); } static int symbol_gettype(int type_mask, - struct nlist *nlist, struct section *section) + struct nlist nlist, struct section *section) { - if (nlist->n_type & N_STAB) + if (nlist.n_type & N_STAB) return (SYM_STAB); - else if (type_mask == N_UNDF && nlist->n_type & N_EXT && nlist->n_value) + else if (type_mask == N_UNDF && nlist.n_type & N_EXT && nlist.n_value) return (SYM_COMMON); else if (type_mask == N_UNDF) return (SYM_UNDF); @@ -51,15 +54,13 @@ int symbol_set(t_symbol *sym, t_machodata *data) { t_list *lst; - if (sym->nlist->n_sect) - { - lst = ft_lst_at(data->sects, sym->nlist->n_sect - 1); + if (sym->nlist.n_sect && + (lst = ft_lst_at(data->sects, sym->nlist.n_sect - 1))) sym->section = *(struct section**)(lst->content); - } else sym->section = NULL; sym->type = symbol_gettype( - sym->nlist->n_type & N_TYPE, + sym->nlist.n_type & N_TYPE, sym->nlist, sym->section); return (0); diff --git a/nm-otool/srcs/mach/symbol_sort.c b/nm-otool/srcs/mach/symbol_sort.c index 25ba55ae..e70fbea7 100644 --- a/nm-otool/srcs/mach/symbol_sort.c +++ b/nm-otool/srcs/mach/symbol_sort.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ -/* Updated: 2017/10/26 18:46:04 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:32:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ static int sym_asort(t_symbol *sym1, t_symbol *sym2) static int sym_nsort(t_symbol *sym1, t_symbol *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 (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) diff --git a/nm-otool/srcs/mach_64/get_section_64.c b/nm-otool/srcs/mach_64/get_section_64.c index ca857d5c..c31dc658 100644 --- a/nm-otool/srcs/mach_64/get_section_64.c +++ b/nm-otool/srcs/mach_64/get_section_64.c @@ -6,29 +6,34 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:00:40 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:18:02 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:58:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -void *get_segment_64(void *file, char *lookup) +void *get_segment_64(struct mach_header_64 *file, char *lookup) { uint32_t i; struct load_command *lc; struct segment_command_64 *seg; + uint32_t ncmds; - lc = (void*)((struct mach_header_64*)file + 1); + lc = (void*)(file + 1); i = -1; - while (++i < ((struct mach_header_64*)file)->ncmds) + ncmds = endian(file->ncmds, 32); + DG("ncmds=%i", ncmds); + while (++i < ncmds) { - if (lc->cmd & LC_SEGMENT_64) + DG("i=%i", i); + if (endian(lc->cmd, 32) & LC_SEGMENT_64) { seg = (struct segment_command_64*)lc; + DG("segment: %s", seg->segname); if (ft_strcmp(seg->segname, lookup) == 0) return (seg); } - lc = (void*)lc + lc->cmdsize; + lc = (void*)lc + endian(lc->cmdsize, 32); } return (NULL); } @@ -39,10 +44,11 @@ void *get_section_64(void *file, char *segname, char *sectname) struct segment_command_64 *seg; struct section_64 *sect; - seg = get_segment_64(file, segname); + if (!(seg = get_segment_64(file, segname))) + return (NULL); sect = (void*)(seg + 1); j = -1; - while (++j < seg->nsects) + while (++j < endian(seg->nsects, 32)) { if (ft_strcmp(sect->sectname, sectname) == 0) return (sect); diff --git a/nm-otool/srcs/mach_64/mach_64.c b/nm-otool/srcs/mach_64/mach_64.c index 69b4bb3d..5fc37676 100644 --- a/nm-otool/srcs/mach_64/mach_64.c +++ b/nm-otool/srcs/mach_64/mach_64.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */ -/* Updated: 2017/10/30 16:58:03 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 18:06:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ static void symtab_64_parse(t_machodata *data, struct symtab_command *symtab) stringtable = data->file + endian(symtab->stroff, 32); array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32)); i = -1; - while (++i < (int)symtab->nsyms) + while (++i < (int)endian(symtab->nsyms,32)) { symbol_64_init(&symbol, stringtable, array, i); symbol_64_set(&symbol, data); @@ -62,7 +62,7 @@ void mach_64_parse(t_machodata *data) { if (endian(lc->cmd, 32) == LC_SYMTAB) symtab_64_parse(data, (struct symtab_command*)lc); - else if (lc->cmd == LC_SEGMENT_64) + else if (endian(lc->cmd, 32) == LC_SEGMENT_64) seg_64_parse(data, (struct segment_command_64*)lc); lc = (void*)lc + endian(lc->cmdsize, 32); } diff --git a/nm-otool/srcs/mach_64/otool_mach_64.c b/nm-otool/srcs/mach_64/otool_mach_64.c index 63b75fb8..7ee25b24 100644 --- a/nm-otool/srcs/mach_64/otool_mach_64.c +++ b/nm-otool/srcs/mach_64/otool_mach_64.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:16:36 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:31:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,20 +16,28 @@ void otool_mach_64(void *file, t_otooldata *data) { struct section_64 *sect; + DG("check"); if (data->flag & OTOOL_TEXT) { + DG("check"); if ((sect = get_section_64(file, SEG_TEXT, SECT_TEXT))) { + DG("check"); ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT); - hexdump_64(file + sect->offset, sect->addr, sect->size); + DG("check"); + DG("hexdump: %i %i %i", sect->offset, sect->addr, sect->size); + hexdump_64(file + endian(sect->offset, 32), + endian(sect->addr, 32), endian(sect->size, 32)); } + DG("check"); } if (data->flag & OTOOL_DATA) { if ((sect = get_section_64(file, SEG_DATA, SECT_DATA))) { ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA); - hexdump_64(file + sect->offset, sect->addr, sect->size); + hexdump_64(file + endian(sect->offset, 32), + endian(sect->addr, 32), endian(sect->size, 32)); } } } diff --git a/nm-otool/srcs/mach_64/symbol_64_filter.c b/nm-otool/srcs/mach_64/symbol_64_filter.c index caa2b016..db3f302a 100644 --- a/nm-otool/srcs/mach_64/symbol_64_filter.c +++ b/nm-otool/srcs/mach_64/symbol_64_filter.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */ -/* Updated: 2017/10/26 18:44:51 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:36:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,12 +19,12 @@ int cmp_symtype_64(t_symbol_64 *sym, void *type) 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_64(t_symbol_64 *s) { - return (s->nlist->n_type & N_EXT); + return (s->nlist.n_type & N_EXT); } int symbol_64_filter(t_list **symbols, t_flag flag) diff --git a/nm-otool/srcs/mach_64/symbol_64_format.c b/nm-otool/srcs/mach_64/symbol_64_format.c index 34dae43a..1900b1e9 100644 --- a/nm-otool/srcs/mach_64/symbol_64_format.c +++ b/nm-otool/srcs/mach_64/symbol_64_format.c @@ -15,14 +15,14 @@ t_symbolmap g_symbolmap_64[] = 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]"); if (is_external_64(symbol)) ft_printf(" external"); else { ft_printf(" non-external"); - if (symbol->nlist->n_type & N_PEXT) + if (symbol->nlist.n_type & N_PEXT) ft_printf(" (was a private external)"); } } @@ -43,12 +43,12 @@ void symbol_64_format_full(t_symbol_64 *symbol) { ft_printf("\t%i %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); + (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_64_format_dfl(t_symbol_64 *symbol) @@ -62,8 +62,8 @@ void symbol_64_format_dfl(t_symbol_64 *symbol) ft_printf(" %c", map.c + 'a' - 'A'); if (symbol->type == SYM_STAB) ft_printf(" %02x %04b %#x", - symbol->nlist->n_sect, symbol->nlist->n_desc, - symbol->nlist->n_type); + symbol->nlist.n_sect, symbol->nlist.n_desc, + symbol->nlist.n_type); } int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data) @@ -75,7 +75,7 @@ int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data) if (symbol->type == SYM_UNDF) ft_printf("%16s", " "); else - ft_printf("%016llx", symbol->nlist->n_value); + ft_printf("%016llx", symbol->nlist.n_value); if (data->flag & NM_MFORMAT) symbol_64_format_m(symbol); else if (data->flag & NM_FULL) @@ -83,6 +83,8 @@ int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data) else symbol_64_format_dfl(symbol); } - ft_printf(" %s\n", symbol->string); + ft_putchar(' '); + ft_putstr(symbol->string); + ft_putchar('\n'); return (0); } diff --git a/nm-otool/srcs/mach_64/symbol_64_init.c b/nm-otool/srcs/mach_64/symbol_64_init.c index 19d80d80..cd1d9d9e 100644 --- a/nm-otool/srcs/mach_64/symbol_64_init.c +++ b/nm-otool/srcs/mach_64/symbol_64_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */ -/* Updated: 2017/10/26 18:23:24 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 17:52:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,17 +19,20 @@ int symbol_64_init(t_symbol_64 *symbol, { symbol->type = 0; symbol->pos = i; - symbol->nlist = array + i; - symbol->string = stringtable + array[i].n_un.n_strx; + symbol->nlist.n_type = endian(array[i].n_type, 8); + symbol->nlist.n_sect = endian(array[i].n_sect, 8); + symbol->nlist.n_desc = endian(array[i].n_desc, 16); + symbol->nlist.n_value = endian(array[i].n_value, 64); + symbol->string = stringtable + endian(array[i].n_un.n_strx, 32); return (0); } static int symbol_64_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) return (SYM_STAB); - else if (type_mask == N_UNDF && nlist->n_type & N_EXT && nlist->n_value) + else if (type_mask == N_UNDF && nlist.n_type & N_EXT && nlist.n_value) return (SYM_COMMON); else if (type_mask == N_UNDF) return (SYM_UNDF); @@ -51,15 +54,13 @@ int symbol_64_set(t_symbol_64 *sym, t_machodata *data) { t_list *lst; - if (sym->nlist->n_sect) - { - lst = ft_lst_at(data->sects, sym->nlist->n_sect - 1); + if (sym->nlist.n_sect && + (lst = ft_lst_at(data->sects, sym->nlist.n_sect - 1))) sym->section = *(struct section_64**)(lst->content); - } else sym->section = NULL; sym->type = symbol_64_gettype( - sym->nlist->n_type & N_TYPE, + sym->nlist.n_type & N_TYPE, sym->nlist, sym->section); return (0); diff --git a/nm-otool/srcs/mach_64/symbol_64_sort.c b/nm-otool/srcs/mach_64/symbol_64_sort.c index c183a16f..dc0fb04c 100644 --- a/nm-otool/srcs/mach_64/symbol_64_sort.c +++ b/nm-otool/srcs/mach_64/symbol_64_sort.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ -/* Updated: 2017/10/26 18:46:04 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:05:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,9 +19,9 @@ static int sym_asort(t_symbol_64 *sym1, t_symbol_64 *sym2) static int sym_nsort(t_symbol_64 *sym1, t_symbol_64 *sym2) { - if (sym1->nlist->n_value == sym2->nlist->n_value) - return (sym_asort(sym1, sym2)); - return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1); + if (sym1->nlist.n_value == sym2->nlist.n_value) + return (ft_strcmp(sym1->string, sym2->string)); + return (sym1->nlist.n_value - sym2->nlist.n_value); } int symbol_64_sort(t_list **syms, t_flag flag)