otool 64 and 32 bit output
This commit is contained in:
		
							parent
							
								
									e125f85672
								
							
						
					
					
						commit
						544f01f380
					
				
					 6 changed files with 132 additions and 42 deletions
				
			
		
							
								
								
									
										10
									
								
								inc/ft_nm.h
									
										
									
									
									
								
							
							
						
						
									
										10
									
								
								inc/ft_nm.h
									
										
									
									
									
								
							|  | @ -6,7 +6,7 @@ | ||||||
| /*   By: gtertysh <gtertysh@student.unit.ua>        +#+  +:+       +#+        */ | /*   By: gtertysh <gtertysh@student.unit.ua>        +#+  +:+       +#+        */ | ||||||
| /*                                                +#+#+#+#+#+   +#+           */ | /*                                                +#+#+#+#+#+   +#+           */ | ||||||
| /*   Created: 2019/05/06 22:37:16 by foton             #+#    #+#             */ | /*   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 | void								print_addr | ||||||
| ( | ( | ||||||
| 	size_t addr | 	size_t addr, | ||||||
| ); | 	size_t count | ||||||
| 
 |  | ||||||
| void								print_addr_32 |  | ||||||
| ( |  | ||||||
| 	size_t addr |  | ||||||
| ); | ); | ||||||
| 
 | 
 | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | @ -40,6 +40,7 @@ nm_sources = [ | ||||||
| 
 | 
 | ||||||
| otool_sources = [ | otool_sources = [ | ||||||
| 	'src/nm/nm_file.c', | 	'src/nm/nm_file.c', | ||||||
|  | 	'src/nm/print_address.c', | ||||||
| 	'src/otool/main.c' | 	'src/otool/main.c' | ||||||
| ] | ] | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -62,7 +62,7 @@ static void				print_symbol_table(t_nm_mach_32 *mach32) | ||||||
| 		// some shit herustic should be used
 | 		// some shit herustic should be used
 | ||||||
| 		// to determine to print address or not
 | 		// to determine to print address or not
 | ||||||
| 		if (symbol.n_value) | 		if (symbol.n_value) | ||||||
| 			print_addr_32(symbol.n_value); | 			print_addr(symbol.n_value, 8); | ||||||
| 		else | 		else | ||||||
| 			ft_putstr("        "); | 			ft_putstr("        "); | ||||||
| 		ft_putchar(' '); | 		ft_putchar(' '); | ||||||
|  |  | ||||||
|  | @ -62,7 +62,7 @@ static void				print_symbol_table(t_nm_mach_64 *mach64) | ||||||
| 		// some shit herustic should be used
 | 		// some shit herustic should be used
 | ||||||
| 		// to determine to print address or not
 | 		// to determine to print address or not
 | ||||||
| 		if (symbol.n_value) | 		if (symbol.n_value) | ||||||
| 			print_addr(symbol.n_value); | 			print_addr(symbol.n_value, 16); | ||||||
| 		else | 		else | ||||||
| 			ft_putstr("                "); | 			ft_putstr("                "); | ||||||
| 		ft_putchar(' '); | 		ft_putchar(' '); | ||||||
|  |  | ||||||
|  | @ -2,32 +2,11 @@ | ||||||
| #include "libft.h" | #include "libft.h" | ||||||
| #include <stddef.h> | #include <stddef.h> | ||||||
| 
 | 
 | ||||||
| void	print_addr(size_t addr) | void	print_addr(size_t addr, size_t count) | ||||||
| { | { | ||||||
| 	size_t	i; | 	size_t	i; | ||||||
| 	size_t	bit_4; | 	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; | 	i = 0; | ||||||
| 	while (i < count) | 	while (i < count) | ||||||
| 	{ | 	{ | ||||||
|  |  | ||||||
							
								
								
									
										136
									
								
								src/otool/main.c
									
										
									
									
									
								
							
							
						
						
									
										136
									
								
								src/otool/main.c
									
										
									
									
									
								
							|  | @ -18,7 +18,7 @@ t_segment_command_64	*find_text_command_64 | ||||||
| 	i = 0; | 	i = 0; | ||||||
| 	while(i < count) | 	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) |             ft_strcmp(((t_segment_command_64 *)lc)->segname, SEG_TEXT) == 0) | ||||||
| 		{ | 		{ | ||||||
| 			text_command = (t_segment_command_64 *)lc; | 			text_command = (t_segment_command_64 *)lc; | ||||||
|  | @ -30,6 +30,31 @@ t_segment_command_64	*find_text_command_64 | ||||||
| 	return text_command; | 	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_section_64       *find_text_section_64 | ||||||
| ( | ( | ||||||
|     t_segment_command_64 *text_segment |     t_segment_command_64 *text_segment | ||||||
|  | @ -41,7 +66,7 @@ t_section_64       *find_text_section_64 | ||||||
| 
 | 
 | ||||||
|     i = 0; |     i = 0; | ||||||
|     text_section = NULL; |     text_section = NULL; | ||||||
|     runner = (t_section_64 *)((uintptr_t)text_segment +  |     runner = (t_section_64 *)((uintptr_t)text_segment + | ||||||
|         sizeof(t_segment_command_64)); |         sizeof(t_segment_command_64)); | ||||||
| 
 | 
 | ||||||
|     while(i < text_segment->nsects) |     while(i < text_segment->nsects) | ||||||
|  | @ -57,15 +82,91 @@ t_section_64       *find_text_section_64 | ||||||
|     return (text_section); |     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); |     uint32_t        i; | ||||||
|     ft_putstr("\n section size: "); |     t_section_32    *text_section; | ||||||
|     ft_putnbr(mach64->text_section->size); |     t_section_32    *runner; | ||||||
|     ft_putstr("\n"); | 
 | ||||||
|  |     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) | void                otool64(t_file *file) | ||||||
|  | @ -77,7 +178,19 @@ void                otool64(t_file *file) | ||||||
| 	mach64.text_command = find_text_command_64(mach64.commands, | 	mach64.text_command = find_text_command_64(mach64.commands, | ||||||
| 		mach64.header->ncmds); | 		mach64.header->ncmds); | ||||||
|     mach64.text_section = find_text_section_64(mach64.text_command); |     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) | void		hanldle_file(const char *filename) | ||||||
|  | @ -91,8 +204,8 @@ void		hanldle_file(const char *filename) | ||||||
| 	magic = *(uint32_t *)file.file; | 	magic = *(uint32_t *)file.file; | ||||||
| 	if (magic == MH_MAGIC_64) | 	if (magic == MH_MAGIC_64) | ||||||
| 		otool64(&file); | 		otool64(&file); | ||||||
| 	// else if (magic == MH_MAGIC)
 | 	else if (magic == MH_MAGIC) | ||||||
| 	// 	macho32(&file);
 | 		otool32(&file); | ||||||
| 	// else if (magic == FAT_CIGAM)
 | 	// else if (magic == FAT_CIGAM)
 | ||||||
| 	// 	fat(&file);
 | 	// 	fat(&file);
 | ||||||
| 	// else if (ft_strncmp(file.file, ARMAG, SARMAG) == 0)
 | 	// 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(argv[i]); | ||||||
|         ft_putstr(":\n"); |         ft_putstr(":\n"); | ||||||
|  | 		ft_putstr("Contents of (__TEXT,__text) section\n"); | ||||||
|         hanldle_file(argv[i]); |         hanldle_file(argv[i]); | ||||||
|         i++; |         i++; | ||||||
|     } |     } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue