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>        +#+  +:+       +#+        */ | ||||
| /*                                                +#+#+#+#+#+   +#+           */ | ||||
| /*   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 | ||||
|  |  | |||
|  | @ -40,6 +40,7 @@ nm_sources = [ | |||
| 
 | ||||
| otool_sources = [ | ||||
| 	'src/nm/nm_file.c', | ||||
| 	'src/nm/print_address.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
 | ||||
| 		// 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(' '); | ||||
|  |  | |||
|  | @ -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(' '); | ||||
|  |  | |||
|  | @ -2,32 +2,11 @@ | |||
| #include "libft.h" | ||||
| #include <stddef.h> | ||||
| 
 | ||||
| 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) | ||||
| 	{ | ||||
|  |  | |||
							
								
								
									
										132
									
								
								src/otool/main.c
									
										
									
									
									
								
							
							
						
						
									
										132
									
								
								src/otool/main.c
									
										
									
									
									
								
							|  | @ -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 | ||||
|  | @ -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++; | ||||
|     } | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue