diff --git a/.vscode/launch.json b/.vscode/launch.json index f1029b6..937402b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "version": "0.2.0", "configurations": [ { - "name": "self read", + "name": "nm self read", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/debug_bld/ft_nm", @@ -21,7 +21,7 @@ } }, { - "name": "orig nm read", + "name": "nm orig nm read", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/debug_bld/ft_nm", @@ -37,7 +37,7 @@ } }, { - "name": "32bin", + "name": "nm 32bin", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/debug_bld/ft_nm", @@ -53,7 +53,7 @@ } }, { - "name": "fat", + "name": "nm fat", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/debug_bld/ft_nm", @@ -69,7 +69,7 @@ } }, { - "name": "ar", + "name": "nm ar", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/debug_bld/ft_nm", @@ -83,6 +83,22 @@ "osx": { "MIMode": "lldb" } - } + }, + { + "name": "otool orig nm read", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/debug_bld/ft_otool", + "args": ["/usr/bin/nm"], + "preLaunchTask": "build", + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "osx": { + "MIMode": "lldb" + } + }, ] } \ No newline at end of file diff --git a/inc/ft_file.h b/inc/ft_file.h new file mode 100644 index 0000000..5573050 --- /dev/null +++ b/inc/ft_file.h @@ -0,0 +1,28 @@ +#ifndef FT_FILE +# define FT_FILE + +typedef struct s_file +{ + void *file; + uint32_t size; + int fd; +} t_file; + +void init_file +( + t_file *file +); + + +void open_file +( + const char *filename, + t_file *file +); + +void close_file +( + t_file *file +); + +#endif \ No newline at end of file diff --git a/inc/ft_nm.h b/inc/ft_nm.h index 9c98600..c74b0bf 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/07/27 20:07:37 by gtertysh ### ########.fr */ +/* Updated: 2019/08/10 17:18:31 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ #include #include #include +#include "ft_file.h" typedef struct symtab_command t_symtab_command; typedef struct load_command t_load_command; @@ -31,19 +32,13 @@ typedef struct nlist t_nlist_32; typedef struct section_64 t_section_64; typedef struct section t_section_32; - -typedef struct s_nm_file -{ - void *file; - uint32_t size; - int fd; -} t_nm_file; - typedef struct s_nm_mach_64 { t_mach_header_64 *header; t_load_command *commands; t_symtab_command *symbol_table_command; + t_segment_command_64 *text_command; + t_section_64 *text_section; t_nlist_64 *symbol_table; char *string_table; } t_nm_mach_64; @@ -53,45 +48,30 @@ typedef struct s_nm_mach_32 t_mach_header_32 *header; t_load_command *commands; t_symtab_command *symbol_table_command; + t_segment_command_32 *text_command; + t_section_32 *text_section; t_nlist_32 *symbol_table; char *string_table; } t_nm_mach_32; -void init_file -( - t_nm_file *file -); - - -void open_file -( - const char *filename, - t_nm_file *file -); - -void close_file -( - t_nm_file *file -); - void fat ( - t_nm_file *file + t_file *file ); void ar ( - t_nm_file *file + t_file *file ); void macho64 ( - t_nm_file *file + t_file *file ); void macho32 ( - t_nm_file *file + t_file *file ); t_symtab_command *find_symbol_table_command @@ -110,7 +90,4 @@ void print_addr_32 size_t addr ); - - - #endif diff --git a/meson.build b/meson.build index e1336a2..5284a98 100644 --- a/meson.build +++ b/meson.build @@ -39,6 +39,7 @@ nm_sources = [ ] otool_sources = [ + 'src/nm/nm_file.c', 'src/otool/main.c' ] @@ -58,6 +59,6 @@ ft_nm = executable( include_directories: inc, dependencies: libft_dep, install: true, -) +) #subdir('t') diff --git a/src/nm/ar.c b/src/nm/ar.c index c0cc65a..70dda2a 100644 --- a/src/nm/ar.c +++ b/src/nm/ar.c @@ -3,12 +3,12 @@ #include -void ar(t_nm_file *file) +void ar(t_file *file) { struct ar_hdr *runner; char *long_name; size_t offset; - t_nm_file obj; + t_file obj; runner = file->file + SARMAG; diff --git a/src/nm/fat.c b/src/nm/fat.c index f94ae07..195a454 100644 --- a/src/nm/fat.c +++ b/src/nm/fat.c @@ -2,11 +2,11 @@ #include "libft.h" #include -void fat(t_nm_file *file) +void fat(t_file *file) { struct fat_arch *arch_runner; struct fat_header *header; - t_nm_file arch_file; + t_file arch_file; uint32_t i; header = (struct fat_header *)file->file; diff --git a/src/nm/macho32.c b/src/nm/macho32.c index 39c7a73..0cee6bd 100644 --- a/src/nm/macho32.c +++ b/src/nm/macho32.c @@ -89,7 +89,7 @@ static void print_symbol_table(t_nm_mach_32 *mach32) } } -void macho32(t_nm_file *file) +void macho32(t_file *file) { t_nm_mach_32 mach32; diff --git a/src/nm/macho64.c b/src/nm/macho64.c index 51ee18f..98d1ba7 100644 --- a/src/nm/macho64.c +++ b/src/nm/macho64.c @@ -89,7 +89,7 @@ static void print_symbol_table(t_nm_mach_64 *mach64) } } -void macho64(t_nm_file *file) +void macho64(t_file *file) { t_nm_mach_64 mach64; diff --git a/src/nm/main.c b/src/nm/main.c index 8dc5f9e..1f918b5 100644 --- a/src/nm/main.c +++ b/src/nm/main.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/13 14:52:27 by gtertysh #+# #+# */ -/* Updated: 2019/07/31 21:37:00 by gtertysh ### ########.fr */ +/* Updated: 2019/08/10 16:05:21 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ void hanldle_file(const char *filename) { - t_nm_file file; + t_file file; uint32_t magic; // create dispatch table for this bullshit diff --git a/src/nm/nm_file.c b/src/nm/nm_file.c index 2feba86..5c12868 100644 --- a/src/nm/nm_file.c +++ b/src/nm/nm_file.c @@ -8,14 +8,14 @@ #include #include -void init_file(t_nm_file *file) +void init_file(t_file *file) { file->fd = -1; file->file = NULL; file->size = 0; } -void open_file(const char *filename, t_nm_file *file) +void open_file(const char *filename, t_file *file) { struct stat stat_buff; @@ -42,7 +42,7 @@ void open_file(const char *filename, t_nm_file *file) } } -void close_file(t_nm_file *file) +void close_file(t_file *file) { close(file->fd); munmap(file->file, file->size); diff --git a/src/otool/main.c b/src/otool/main.c index c115fed..84e2d05 100644 --- a/src/otool/main.c +++ b/src/otool/main.c @@ -1,7 +1,124 @@ +#include +#include +#include +#include "ft_file.h" +#include "ft_nm.h" #include "libft.h" -int main() +t_segment_command_64 *find_text_command_64 +( + t_load_command *lc, + uint32_t count +) { - ft_putstr("hello from otool!"); + uint32_t i; + t_segment_command_64 *text_command; + + text_command = NULL; + i = 0; + while(i < count) + { + if (lc->cmd == LC_SEGMENT_64 && + ft_strcmp(((t_segment_command_64 *)lc)->segname, SEG_TEXT) == 0) + { + text_command = (t_segment_command_64 *)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 +) +{ + uint32_t i; + t_section_64 *text_section; + t_section_64 *runner; + + i = 0; + text_section = NULL; + runner = (t_section_64 *)((uintptr_t)text_segment + + sizeof(t_segment_command_64)); + + while(i < text_segment->nsects) + { + if (ft_strcmp(runner->sectname, SECT_TEXT) == 0) + { + text_section = runner; + break; + } + runner = (t_section_64 *)((uintptr_t)runner + sizeof(t_section_64)); + i++; + } + return (text_section); +} + +void print_text_64 +( + t_nm_mach_64 *mach64 +) +{ + ft_putstr(mach64->text_section->sectname); + ft_putstr("\n section size: "); + ft_putnbr(mach64->text_section->size); + ft_putstr("\n"); +} + +void otool64(t_file *file) +{ + t_nm_mach_64 mach64; + + mach64.header = (t_mach_header_64 *)file->file; + mach64.commands = (t_load_command *)(file->file + sizeof(t_mach_header_64)); + 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); +} + +void hanldle_file(const char *filename) +{ + t_file file; + uint32_t magic; + + // create dispatch table for this bullshit + init_file(&file); + open_file(filename, &file); + magic = *(uint32_t *)file.file; + if (magic == MH_MAGIC_64) + otool64(&file); + // else if (magic == MH_MAGIC) + // macho32(&file); + // else if (magic == FAT_CIGAM) + // fat(&file); + // else if (ft_strncmp(file.file, ARMAG, SARMAG) == 0) + // ar(&file); + else + ft_putstr("invalid magic number."); + close_file(&file); +} + +int main(int argc, char **argv) +{ + int i; + + if (argc == 1) + { + ft_putstr_fd("at least one file must be specified\n", STDERR_FILENO); + return (1); + } + + i = 1; + while(i < argc) + { + ft_putstr(argv[i]); + ft_putstr(":\n"); + hanldle_file(argv[i]); + i++; + } return (0); } \ No newline at end of file