From 544f01f38089192f7b8047ce20222799d2efe16f Mon Sep 17 00:00:00 2001 From: Gregory Date: Sat, 17 Aug 2019 15:21:56 +0300 Subject: [PATCH] otool 64 and 32 bit output --- inc/ft_nm.h | 10 +-- meson.build | 1 + src/nm/macho32.c | 2 +- src/nm/macho64.c | 2 +- src/nm/print_address.c | 23 +------ src/otool/main.c | 136 +++++++++++++++++++++++++++++++++++++---- 6 files changed, 132 insertions(+), 42 deletions(-) diff --git a/inc/ft_nm.h b/inc/ft_nm.h index c74b0bf..6dd581a 100644 --- a/inc/ft_nm.h +++ b/inc/ft_nm.h @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/05/06 22:37:16 by foton #+# #+# */ -/* Updated: 2019/08/10 17:18:31 by gtertysh ### ########.fr */ +/* Updated: 2019/08/17 14:26:16 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,12 +82,8 @@ t_symtab_command *find_symbol_table_command void print_addr ( - size_t addr -); - -void print_addr_32 -( - size_t addr + size_t addr, + size_t count ); #endif diff --git a/meson.build b/meson.build index 5284a98..19aa5b9 100644 --- a/meson.build +++ b/meson.build @@ -40,6 +40,7 @@ nm_sources = [ otool_sources = [ 'src/nm/nm_file.c', + 'src/nm/print_address.c', 'src/otool/main.c' ] diff --git a/src/nm/macho32.c b/src/nm/macho32.c index 0cee6bd..db5a7a7 100644 --- a/src/nm/macho32.c +++ b/src/nm/macho32.c @@ -62,7 +62,7 @@ static void print_symbol_table(t_nm_mach_32 *mach32) // some shit herustic should be used // to determine to print address or not if (symbol.n_value) - print_addr_32(symbol.n_value); + print_addr(symbol.n_value, 8); else ft_putstr(" "); ft_putchar(' '); diff --git a/src/nm/macho64.c b/src/nm/macho64.c index 98d1ba7..d8ecb42 100644 --- a/src/nm/macho64.c +++ b/src/nm/macho64.c @@ -62,7 +62,7 @@ static void print_symbol_table(t_nm_mach_64 *mach64) // some shit herustic should be used // to determine to print address or not if (symbol.n_value) - print_addr(symbol.n_value); + print_addr(symbol.n_value, 16); else ft_putstr(" "); ft_putchar(' '); diff --git a/src/nm/print_address.c b/src/nm/print_address.c index 689d9da..383ab55 100644 --- a/src/nm/print_address.c +++ b/src/nm/print_address.c @@ -2,32 +2,11 @@ #include "libft.h" #include -void print_addr(size_t addr) +void print_addr(size_t addr, size_t count) { size_t i; size_t bit_4; - size_t count; - count = sizeof(size_t) * 2; - i = 0; - while (i < count) - { - bit_4 = (addr >> ((count - i - 1) * 4)) & 0xf; - if (bit_4 < 10) - ft_putnbr(bit_4); - else - ft_putchar('a' + bit_4 - 10); - i++; - } -} - -void print_addr_32(size_t addr) -{ - size_t i; - size_t bit_4; - size_t count; - - count = sizeof(size_t); i = 0; while (i < count) { diff --git a/src/otool/main.c b/src/otool/main.c index 84e2d05..21b4658 100644 --- a/src/otool/main.c +++ b/src/otool/main.c @@ -18,7 +18,7 @@ t_segment_command_64 *find_text_command_64 i = 0; while(i < count) { - if (lc->cmd == LC_SEGMENT_64 && + if (lc->cmd == LC_SEGMENT_64 && ft_strcmp(((t_segment_command_64 *)lc)->segname, SEG_TEXT) == 0) { text_command = (t_segment_command_64 *)lc; @@ -30,6 +30,31 @@ t_segment_command_64 *find_text_command_64 return text_command; } +t_segment_command_32 *find_text_command_32 +( + t_load_command *lc, + uint32_t count +) +{ + uint32_t i; + t_segment_command_32 *text_command; + + text_command = NULL; + i = 0; + while(i < count) + { + if (lc->cmd == LC_SEGMENT && + ft_strcmp(((t_segment_command_32 *)lc)->segname, SEG_TEXT) == 0) + { + text_command = (t_segment_command_32 *)lc; + break; + } + lc = (t_load_command *)((uintptr_t)lc + lc->cmdsize); + i++; + } + return text_command; +} + t_section_64 *find_text_section_64 ( t_segment_command_64 *text_segment @@ -41,7 +66,7 @@ t_section_64 *find_text_section_64 i = 0; text_section = NULL; - runner = (t_section_64 *)((uintptr_t)text_segment + + runner = (t_section_64 *)((uintptr_t)text_segment + sizeof(t_segment_command_64)); while(i < text_segment->nsects) @@ -57,15 +82,91 @@ t_section_64 *find_text_section_64 return (text_section); } -void print_text_64 +t_section_32 *find_text_section_32 ( - t_nm_mach_64 *mach64 + t_segment_command_32 *text_segment ) { - ft_putstr(mach64->text_section->sectname); - ft_putstr("\n section size: "); - ft_putnbr(mach64->text_section->size); - ft_putstr("\n"); + uint32_t i; + t_section_32 *text_section; + t_section_32 *runner; + + i = 0; + text_section = NULL; + runner = (t_section_32 *)((uintptr_t)text_segment + + sizeof(t_segment_command_32)); + + while(i < text_segment->nsects) + { + if (ft_strcmp(runner->sectname, SECT_TEXT) == 0) + { + text_section = runner; + break; + } + runner = (t_section_32 *)((uintptr_t)runner + sizeof(t_section_32)); + i++; + } + return (text_section); +} + +void print_text_64 +( + t_nm_mach_64 *mach64, + t_file *file +) +{ + size_t text; + size_t addr; + uint64_t i; + uint64_t j; + + text = (size_t)file->file + mach64->text_section->offset; + addr = mach64->text_section->addr; + i = 0; + while(i < mach64->text_section->size) + { + print_addr(addr + i, 16); + ft_putstr("\t"); + j = 0; + while(i < mach64->text_section->size && j < 16) + { + print_addr(*(char *)(text + i), 2); + ft_putstr(" "); + j++; + i++; + } + ft_putstr("\n"); + } +} + +void print_text_32 +( + t_nm_mach_32 *mach32, + t_file *file +) +{ + size_t text; + size_t addr; + uint64_t i; + uint64_t j; + + text = (size_t)file->file + mach32->text_section->offset; + addr = mach32->text_section->addr; + i = 0; + while(i < mach32->text_section->size) + { + print_addr(addr + i, 8); + ft_putstr("\t"); + j = 0; + while(i < mach32->text_section->size && j < 8) + { + print_addr(*(char *)(text + i), 2); + ft_putstr(" "); + j++; + i++; + } + ft_putstr("\n"); + } } void otool64(t_file *file) @@ -77,7 +178,19 @@ void otool64(t_file *file) mach64.text_command = find_text_command_64(mach64.commands, mach64.header->ncmds); mach64.text_section = find_text_section_64(mach64.text_command); - print_text_64(&mach64); + print_text_64(&mach64, file); +} + +void otool32(t_file *file) +{ + t_nm_mach_32 mach32; + + mach32.header = (t_mach_header_32 *)file->file; + mach32.commands = (t_load_command *)(file->file + sizeof(t_mach_header_32)); + mach32.text_command = find_text_command_32(mach32.commands, + mach32.header->ncmds); + mach32.text_section = find_text_section_32(mach32.text_command); + print_text_32(&mach32, file); } void hanldle_file(const char *filename) @@ -91,8 +204,8 @@ void hanldle_file(const char *filename) magic = *(uint32_t *)file.file; if (magic == MH_MAGIC_64) otool64(&file); - // else if (magic == MH_MAGIC) - // macho32(&file); + else if (magic == MH_MAGIC) + otool32(&file); // else if (magic == FAT_CIGAM) // fat(&file); // else if (ft_strncmp(file.file, ARMAG, SARMAG) == 0) @@ -117,6 +230,7 @@ int main(int argc, char **argv) { ft_putstr(argv[i]); ft_putstr(":\n"); + ft_putstr("Contents of (__TEXT,__text) section\n"); hanldle_file(argv[i]); i++; }