From 7274e130757ffb61b8489079c97ad2a4e81f3748 Mon Sep 17 00:00:00 2001 From: Gregory Date: Sat, 27 Jul 2019 20:03:42 +0300 Subject: [PATCH] simple fat --- .vscode/launch.json | 16 ++++++++++++++++ inc/ft_nm.h | 8 ++++++-- meson.build | 1 + src/fat.c | 33 +++++++++++++++++++++++++++++++++ src/main.c | 4 +++- src/nm_file.c | 2 -- 6 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/fat.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 9bbfc12..fd8fdd1 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -48,6 +48,22 @@ "environment": [], "externalConsole": false, "MIMode": "gdb", + "osx": { + "MIMode": "lldb" + } + }, + { + "name": "fat", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/debug_bld/ft_nm", + "args": ["/usr/bin/javah"], + "preLaunchTask": "build", + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", "osx": { "MIMode": "lldb" } diff --git a/inc/ft_nm.h b/inc/ft_nm.h index 95fff1d..36a5451 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 18:10:09 by gtertysh ### ########.fr */ +/* Updated: 2019/07/27 19:50:54 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include #include +#include #include typedef struct symtab_command t_symtab_command; @@ -35,7 +36,6 @@ typedef struct s_nm_file void *file; uint32_t size; int fd; - const char *filename; } t_nm_file; typedef struct s_nm_mach_64 @@ -73,6 +73,10 @@ void close_file t_nm_file *file ); +void fat +( + t_nm_file *file +); void macho64 ( diff --git a/meson.build b/meson.build index 72a702f..23c3882 100644 --- a/meson.build +++ b/meson.build @@ -32,6 +32,7 @@ sources = [ 'src/nm_file.c', 'src/macho64.c', 'src/macho32.c', + 'src/fat.c', 'src/print_address.c', 'src/find_symbol_table_command.c', ] diff --git a/src/fat.c b/src/fat.c new file mode 100644 index 0000000..f94ae07 --- /dev/null +++ b/src/fat.c @@ -0,0 +1,33 @@ +#include "ft_nm.h" +#include "libft.h" +#include + +void fat(t_nm_file *file) +{ + struct fat_arch *arch_runner; + struct fat_header *header; + t_nm_file arch_file; + uint32_t i; + + header = (struct fat_header *)file->file; + + if (header->magic != FAT_CIGAM) + { + ft_putstr_fd("only 32bit big endian fat binaries supported\n", + STDERR_FILENO); + exit(1); + } + arch_runner = file->file + sizeof(struct fat_header); + i = 0; + while (i < OSSwapInt32(header->nfat_arch)) + { + if (OSSwapInt32(arch_runner->cputype) == CPU_TYPE_X86_64) + { + arch_file.file = file->file + OSSwapInt32(arch_runner->offset); + arch_file.size = OSSwapInt32(arch_runner->size); + macho64(&arch_file); + } + arch_runner = arch_runner + sizeof(struct fat_arch); + i++; + } +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 35d0164..42d153e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: gtertysh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/13 14:52:27 by gtertysh #+# #+# */ -/* Updated: 2019/07/27 17:57:52 by gtertysh ### ########.fr */ +/* Updated: 2019/07/27 19:35:42 by gtertysh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ void hanldle_file(const char *filename) macho64(&file); else if (magic == MH_MAGIC) macho32(&file); + else if (magic == FAT_CIGAM) + fat(&file); else ft_putstr("invalid magic number."); close_file(&file); diff --git a/src/nm_file.c b/src/nm_file.c index d863128..2feba86 100644 --- a/src/nm_file.c +++ b/src/nm_file.c @@ -12,7 +12,6 @@ void init_file(t_nm_file *file) { file->fd = -1; file->file = NULL; - file->filename = NULL; file->size = 0; } @@ -20,7 +19,6 @@ void open_file(const char *filename, t_nm_file *file) { struct stat stat_buff; - file->filename = filename; file->fd = open(filename, O_RDONLY); if (file->fd == -1) {