From 1103db79be3f04eab119cbcf62c3776cbd837989 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Mon, 30 Oct 2017 18:38:24 +0100 Subject: [PATCH] 1 days work, still need to sort out archs for universal binaries --- nm-otool/42corr.sh | 31 +++++++++++++++++++++ nm-otool/Makefile | 3 ++ nm-otool/includes/ft_nm_otool.h | 14 ++++++++-- nm-otool/libft/includes/math.h | 2 +- nm-otool/nmdiff.sh | 1 - nm-otool/otooldiff.sh | 3 +- nm-otool/srcs/bswap.c | 44 ++++++++++++++++++++++++++++++ nm-otool/srcs/endianness.c | 32 ++++++++++++++++++++++ nm-otool/srcs/fat.c | 34 +++++++++++++++++++++++ nm-otool/srcs/ft_nm.c | 33 ++++++++++++++++++---- nm-otool/srcs/ft_otool.c | 7 +++-- nm-otool/srcs/mach_64/mach_64.c | 16 +++++------ nm-otool/srcs/mach_64/nm_mach_64.c | 2 +- 13 files changed, 197 insertions(+), 25 deletions(-) create mode 100755 nm-otool/42corr.sh create mode 100644 nm-otool/srcs/bswap.c create mode 100644 nm-otool/srcs/endianness.c create mode 100644 nm-otool/srcs/fat.c diff --git a/nm-otool/42corr.sh b/nm-otool/42corr.sh new file mode 100755 index 00000000..fc20a6bf --- /dev/null +++ b/nm-otool/42corr.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +make + +########## +# NM +########## + +#test_facile +./nmdiff.sh tests/test_facile + +#test_moins_facile en 64 et 32 +./nmdiff.sh tests/test_moins_facile +./nmdiff.sh tests/test_moins_facile_32-bit + +#arguments mutilples +./nmdiff.sh tests/test_facile tests/test_facile + +#fichiers objets +./nmdiff.sh objs/ft_nm.o + +#bibliotheques dynamiques +./nmdiff.sh + +#biblioteque universelle +./nmdiff.sh + + +########## +# OTOOL +########## diff --git a/nm-otool/Makefile b/nm-otool/Makefile index 1a8bae55..20284f03 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -34,7 +34,10 @@ NM_OBJ = $(OBJ_DIR)ft_nm.o OTOOL_OBJ = $(OBJ_DIR)ft_otool.o SRC_BASE = \ +bswap.c\ dump_symtab.c\ +endianness.c\ +fat.c\ ft_nm.c\ ft_otool.c\ mach/get_section.c\ diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index dafd2569..57a4601d 100644 --- a/nm-otool/includes/ft_nm_otool.h +++ b/nm-otool/includes/ft_nm_otool.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:00:14 by jhalford ### ########.fr */ +/* Updated: 2017/10/30 16:37:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,9 +31,19 @@ # include "mach_64.h" # include "mach.h" - # define IS_MACH_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) # define IS_MACH_32(x) (x == MH_MAGIC || x == MH_CIGAM) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) +# define IS_REV(x) (x == MH_CIGAM || x == MH_CIGAM_64 || x == FAT_CIGAM) + +uint64_t endian(uint64_t n, uint8_t size); +uint8_t bswap_8(uint8_t x); +uint16_t bswap_16(uint16_t x); +uint32_t bswap_32(uint32_t x); +uint64_t bswap_64(uint64_t x); + +void *fat_extract(struct fat_header *fat, + cpu_type_t cputype, + cpu_subtype_t cpusubtype); #endif diff --git a/nm-otool/libft/includes/math.h b/nm-otool/libft/includes/math.h index 4b87935e..e65b0b98 100644 --- a/nm-otool/libft/includes/math.h +++ b/nm-otool/libft/includes/math.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 15:41:59 by jhalford #+# #+# */ -/* Updated: 2017/05/16 17:30:35 by ariard ### ########.fr */ +/* Updated: 2017/10/30 15:43:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/nmdiff.sh b/nm-otool/nmdiff.sh index c8c5d073..bfa4d834 100755 --- a/nm-otool/nmdiff.sh +++ b/nm-otool/nmdiff.sh @@ -1,5 +1,4 @@ #!/usr/bin/env zsh -make diff <(./ft_nm $@) <(nm $@) if [ $? -eq 0 ]; then echo "OK ✅" diff --git a/nm-otool/otooldiff.sh b/nm-otool/otooldiff.sh index 202109b8..0b6033b8 100755 --- a/nm-otool/otooldiff.sh +++ b/nm-otool/otooldiff.sh @@ -1,6 +1,5 @@ #!/usr/bin/env zsh -make -diff <(./ft_otool $@) <(otool -t $@) +diff <(./ft_otool $@) <(otool $@) if [ $? -eq 0 ]; then echo "OK ✅" else diff --git a/nm-otool/srcs/bswap.c b/nm-otool/srcs/bswap.c new file mode 100644 index 00000000..e1f9821c --- /dev/null +++ b/nm-otool/srcs/bswap.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bswap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/30 15:20:32 by jhalford #+# #+# */ +/* Updated: 2017/10/30 15:53:14 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_nm_otool.h" + +inline uint8_t bswap_8(uint8_t x) +{ + return (x); +} + +inline uint16_t bswap_16(uint16_t x) +{ + return (( (x >> 8) & 0xffu) | ( + (x & 0xffu) << 8)); +} + +inline uint32_t bswap_32(uint32_t x) +{ + return (((x & 0xff000000u) >> 24) + | ((x & 0x00ff0000u) >> 8) + | ((x & 0x0000ff00u) << 8) + | ((x & 0x000000ffu) << 24)); +} + +inline uint64_t bswap_64(uint64_t x) +{ + return (((x & 0xff00000000000000ull) >> 56) + | ((x & 0x00ff000000000000ull) >> 40) + | ((x & 0x0000ff0000000000ull) >> 24) + | ((x & 0x000000ff00000000ull) >> 8) + | ((x & 0x00000000ff000000ull) << 8) + | ((x & 0x0000000000ff0000ull) << 24) + | ((x & 0x000000000000ff00ull) << 40) + | ((x & 0x00000000000000ffull) << 56)); +} diff --git a/nm-otool/srcs/endianness.c b/nm-otool/srcs/endianness.c new file mode 100644 index 00000000..06547257 --- /dev/null +++ b/nm-otool/srcs/endianness.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* endianese.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/30 15:18:54 by jhalford #+# #+# */ +/* Updated: 2017/10/30 15:58:55 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_nm_otool.h" + +int g_rev; + +inline uint64_t endian(uint64_t n, uint8_t size) +{ + if (g_rev) + { + if (size == 8) + return (bswap_8(n)); + else if (size == 16) + return (bswap_16(n)); + else if (size == 32) + return (bswap_32(n)); + else if (size == 64) + return (bswap_64(n)); + return (bswap_32(n)); + } + return (n); +} diff --git a/nm-otool/srcs/fat.c b/nm-otool/srcs/fat.c new file mode 100644 index 00000000..0bb4395d --- /dev/null +++ b/nm-otool/srcs/fat.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fat.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/10/30 16:31:48 by jhalford #+# #+# */ +/* Updated: 2017/10/30 17:49:28 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_nm_otool.h" + +void *fat_extract(struct fat_header *fat, + cpu_type_t cputype, + cpu_subtype_t cpusubtype) +{ + uint32_t narch; + struct fat_arch *arch; + + narch = endian(fat->nfat_arch, 32); + arch = (struct fat_arch*)(fat + 1); + while (narch--) + { + if (endian(arch->cputype, 32) & cputype + && endian(arch->cpusubtype, 32) & cpusubtype) + { + return ((void *)fat + endian(arch->offset, 32)); + } + ++arch; + } + return (NULL); +} diff --git a/nm-otool/srcs/ft_nm.c b/nm-otool/srcs/ft_nm.c index fe01b787..38729c4e 100644 --- a/nm-otool/srcs/ft_nm.c +++ b/nm-otool/srcs/ft_nm.c @@ -6,14 +6,16 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:26:03 by jhalford ### ########.fr */ +/* Updated: 2017/10/30 18:36:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" +#include #define NM_USAGE "usage: nm [-agmnpruU] filename ..." t_machodata *g_data = NULL; +int g_rev = 0; t_cliopts g_nm_opts[] = { @@ -29,23 +31,42 @@ t_cliopts g_nm_opts[] = {0xff, "full", NM_FULL, 0, NULL, 0}, {'o', NULL, NM_OFORMAT, 0, NULL, 0}, {'m', NULL, NM_MFORMAT, 0, NULL, 0}, - - {'A', NULL, 0, 0, NULL, 0}, - {'x', NULL, 0, 0, NULL, 0}, - {'j', NULL, 0, 0, NULL, 0}, }; +int nm_file(void *file, t_nmdata *data); + +int nm_fat(struct fat_header *fat, t_nmdata *data) +{ + void *arch; + struct utsname host; + + uname(&host); + NXGetLocalArchInfo(); + + arch = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); + if (!arch) + ft_printf("{red}fat doesn't have x86_64 !{eoc}"); + else + { + uint32_t magic; + magic = *(int*)arch; + nm_file(arch, data); + } + return (0); +} + int nm_file(void *file, t_nmdata *data) { uint32_t magic; magic = *(int*)file; + g_rev = IS_REV(magic); if (IS_MACH_32(magic)) nm_mach(file, data); else if (IS_MACH_64(magic)) nm_mach_64(file, data); else if (IS_FAT(magic)) - ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic); + nm_fat(file, data); else ft_printf("{red}unsupported arch:{eoc} magic=%#x\n", magic); return (0); diff --git a/nm-otool/srcs/ft_otool.c b/nm-otool/srcs/ft_otool.c index 25883f24..30ee86ba 100644 --- a/nm-otool/srcs/ft_otool.c +++ b/nm-otool/srcs/ft_otool.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */ -/* Updated: 2017/10/30 12:26:10 by jhalford ### ########.fr */ +/* Updated: 2017/10/30 17:31:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,14 @@ t_cliopts g_otool_opts[] = {'t', NULL, OTOOL_TEXT, 0, NULL, 0}, {'d', NULL, OTOOL_DATA, 0, NULL, 0}, }; +int g_rev = 0; void otool_file(void *file, t_otooldata *data) { uint32_t magic; magic = *(int *)file; + g_rev = IS_REV(magic); if (IS_MACH_32(magic)) otool_mach(file, data); else if (IS_MACH_64(magic)) @@ -44,8 +46,7 @@ int otool(int ac, char **av, t_otooldata data) i = data.av_data - av; while (i < ac && av[i]) { - if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) - ft_printf("%s:\n", av[i]); + ft_printf("%s:\n", av[i]); if ((fd = open((av[i]), O_RDONLY)) < 0) return (1); if ((fstat(fd, &buf)) < 0) diff --git a/nm-otool/srcs/mach_64/mach_64.c b/nm-otool/srcs/mach_64/mach_64.c index 33c813ab..69b4bb3d 100644 --- a/nm-otool/srcs/mach_64/mach_64.c +++ b/nm-otool/srcs/mach_64/mach_64.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */ -/* Updated: 2017/10/30 11:32:35 by jhalford ### ########.fr */ +/* Updated: 2017/10/30 16:58:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ static void symtab_64_parse(t_machodata *data, struct symtab_command *symtab) struct nlist_64 *array; data->symtab = symtab; - stringtable = data->file + symtab->stroff; - array = (struct nlist_64*)(data->file + symtab->symoff); + stringtable = data->file + endian(symtab->stroff, 32); + array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32)); i = -1; while (++i < (int)symtab->nsyms) { @@ -37,7 +37,7 @@ static void seg_64_parse(t_machodata *data, struct segment_command_64 *seg) uint32_t i; struct section_64 *sect; - nsects = seg->nsects; + nsects = endian(seg->nsects, 32); sect = (void*)(seg + 1); i = -1; while (++i < nsects) @@ -55,17 +55,15 @@ void mach_64_parse(t_machodata *data) struct mach_header_64 *header; header = data->file; - ncmds = header->ncmds; + ncmds = endian(header->ncmds, 32); lc = (void*)(header + 1); i = -1; while (++i < ncmds) { - if (lc->cmd == LC_SYMTAB) + if (endian(lc->cmd, 32) == LC_SYMTAB) symtab_64_parse(data, (struct symtab_command*)lc); - else if (lc->cmd == LC_DYSYMTAB) - data->dysymtab = (struct dysymtab_command*)lc; else if (lc->cmd == LC_SEGMENT_64) seg_64_parse(data, (struct segment_command_64*)lc); - lc = (void*)lc + lc->cmdsize; + lc = (void*)lc + endian(lc->cmdsize, 32); } } diff --git a/nm-otool/srcs/mach_64/nm_mach_64.c b/nm-otool/srcs/mach_64/nm_mach_64.c index 79adbab2..c73297a2 100644 --- a/nm-otool/srcs/mach_64/nm_mach_64.c +++ b/nm-otool/srcs/mach_64/nm_mach_64.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */ -/* Updated: 2017/10/30 11:35:53 by jhalford ### ########.fr */ +/* Updated: 2017/10/30 15:50:23 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */