diff --git a/nm-otool/Makefile b/nm-otool/Makefile index d6794abb..a085f998 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -38,7 +38,6 @@ dump_symtab.c\ fetch_header.c\ ft_nm.c\ ft_otool.c\ -hexdump.c\ mach_o_parse.c\ symbol_filter.c\ symbol_format.c\ diff --git a/nm-otool/srcs/dump_symtab.c b/nm-otool/srcs/dump_symtab.c index 66e84861..2a3d0eda 100644 --- a/nm-otool/srcs/dump_symtab.c +++ b/nm-otool/srcs/dump_symtab.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 19:39:15 by jhalford #+# #+# */ -/* Updated: 2017/03/26 15:22:52 by jhalford ### ########.fr */ +/* Updated: 2017/10/07 18:26:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,4 +32,3 @@ void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab) ft_printf("nlocrel %i\n", dysymtab->nlocrel); ft_putendl(""); } - diff --git a/nm-otool/srcs/fetch_header.c b/nm-otool/srcs/fetch_header.c index 06e7c408..e447d19d 100644 --- a/nm-otool/srcs/fetch_header.c +++ b/nm-otool/srcs/fetch_header.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/23 15:56:37 by jhalford #+# #+# */ -/* Updated: 2017/03/26 19:05:53 by jhalford ### ########.fr */ +/* Updated: 2017/10/07 18:31:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,14 +39,15 @@ static void seg_64_parse(t_machodata *data, struct segment_command_64 *seg) nsects = seg->nsects; sect = (void*)(seg + 1); - for (i = 0; i < nsects; i++) + i = -1; + while (++i < nsects) { ft_lsteadd(&data->sects, ft_lstnew(§, sizeof(sect))); sect = sect + 1; } } -void mach_64_parse(t_machodata *data) +void mach_64_parse(t_machodata *data) { uint32_t ncmds; uint32_t i; @@ -56,7 +57,8 @@ void mach_64_parse(t_machodata *data) header = data->file; ncmds = header->ncmds; lc = (void*)(header + 1); - for (i = 0; i < ncmds; i++) + i = -1; + while (++i < ncmds) { if (lc->cmd == LC_SYMTAB) symtab_parse(data, (struct symtab_command*)lc); @@ -67,23 +69,3 @@ void mach_64_parse(t_machodata *data) lc = (void*)lc + lc->cmdsize; } } - -/* static void fetch_fatheader(t_machodata *data) */ -/* { */ -/* int i; */ -/* int nfat_arch; */ -/* struct fat_arch *arch; */ -/* struct fat_header *header; */ - -/* header = data->file; */ -/* nfat_arch = header->nfat_arch; */ -/* arch = (void*)(header + 1); */ -/* ft_printf("{yel}{inv}FAT header w/ [%i] architures{eoc}\n", nfat_arch); */ -/* for (i = 0; i < nfat_arch; i++) */ -/* { */ -/* ft_printf("CPU type=[%i]", arch->cputype); */ -/* fetch_machheader64(data->file + arch->offset, data); */ -/* arch += 1; */ -/* } */ -/* } */ - diff --git a/nm-otool/srcs/ft_otool.c b/nm-otool/srcs/ft_otool.c index 10b870ed..f520dc62 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/03/27 20:41:01 by jhalford ### ########.fr */ +/* Updated: 2017/10/07 18:35:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,19 +15,21 @@ void *get_text_section(void *file) { uint32_t i; + uint32_t j; struct load_command *lc; - struct mach_header_64 *header = file; struct segment_command_64 *seg; struct section_64 *sect; - lc = (void*)(header + 1); - for (i = 0; i < header->ncmds; i++) + lc = (void*)((struct mach_header_64*)file + 1); + i = -1; + while (++i < ((struct mach_header_64*)file)->ncmds) { if (lc->cmd & LC_SEGMENT_64) { seg = (struct segment_command_64*)lc; sect = (void*)(seg + 1); - for (i = 0; i < seg->nsects; i++) + j = -1; + while (++j < seg->nsects) { if (ft_strcmp(sect->sectname, "__text") == 0) return (sect); @@ -41,14 +43,13 @@ void *get_text_section(void *file) void otool(void *file) { - uint32_t magic = *(int *)file; - int is_fat = IS_FAT(magic); - int is_64 = IS_MAGIC_64(magic); - struct section_64 *sect; + uint32_t magic; + struct section_64 *sect; - if (is_fat) + magic = *(int *)file; + if (IS_FAT(magic)) ft_printf("fat binary not supported yet\n"); - else if (is_64) + else if (IS_MAGIC_64(magic)) { sect = get_text_section(file); ft_printf("Contents of (__TEXT,__text) section\n"); diff --git a/nm-otool/srcs/hexdump.c b/nm-otool/srcs/hexdump.c deleted file mode 100644 index cddd7e2f..00000000 --- a/nm-otool/srcs/hexdump.c +++ /dev/null @@ -1,47 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* hexdump.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/20 15:14:33 by jhalford #+# #+# */ -/* Updated: 2017/03/26 19:11:17 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_nm_otool.h" - -static void print_hex_contents(void *addr, unsigned int size) -{ - void *a; - - a = addr; - while (a - addr < 16) - { - if ((a - addr) >= size) - break ; - else - ft_printf("%02x", *(unsigned char*)a); - ft_putchar(' '); - a++; - } -} - -void *hexdump(void *addr, unsigned int offset, unsigned int size) -{ - void *a; - - addr += offset; - a = addr; - if (addr == NULL) - return (addr); - while ((a - addr) < size) - { - ft_printf("%0*llx\t", 16, (a - addr) + (unsigned long)offset); - print_hex_contents(a, (size - (a - addr))); - ft_putchar('\n'); - a += 16; - } - return (addr); -} diff --git a/nm-otool/srcs/symbol_init.c b/nm-otool/srcs/symbol_init.c index c6630ccc..475308d5 100644 --- a/nm-otool/srcs/symbol_init.c +++ b/nm-otool/srcs/symbol_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 21:22:06 by jhalford #+# #+# */ -/* Updated: 2017/03/27 20:36:57 by jhalford ### ########.fr */ +/* Updated: 2017/10/07 18:35:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_machodata *g_data; -int symbol_init(t_symbol *symbol, char *stringtable, struct nlist_64 *array, int i) +int symbol_init(t_symbol *symbol, char *stringtable, struct nlist_64 *array, int i) { symbol->type = 0; symbol->pos = i; diff --git a/nm-otool/srcs/symbol_sort.c b/nm-otool/srcs/symbol_sort.c index a85923dd..d12c7895 100644 --- a/nm-otool/srcs/symbol_sort.c +++ b/nm-otool/srcs/symbol_sort.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ -/* Updated: 2017/03/27 20:33:03 by jhalford ### ########.fr */ +/* Updated: 2017/10/07 18:29:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ static int sym_nsort(t_symbol *sym1, t_symbol *sym2) return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1); } -int symbol_sort(t_list **syms, t_flag flag) +int symbol_sort(t_list **syms, t_flag flag) { if (!(flag & NM_NOSORT)) {