diff --git a/nm-otool/Makefile b/nm-otool/Makefile index ad3c9049..fcc6e599 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/10/30 12:22:10 by jhalford ### ########.fr # +# Updated: 2017/11/07 15:52:38 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -24,8 +24,7 @@ LIBFT_DIR = libft/ LIBFT_LIB = $(LIBFT_DIR)libft.a LIBFT_INC = $(LIBFT_DIR)includes/ -LIBS = - +LIBS = SRC_DIR = srcs/ INC_DIR = includes/ OBJ_DIR = objs/ @@ -40,7 +39,6 @@ ft_nm.c\ ft_otool.c\ mach/get_section.c\ mach/mach.c\ -mach/nm_mach.c\ mach/otool_mach.c\ mach/symbol_filter.c\ mach/symbol_format.c\ @@ -49,13 +47,13 @@ mach/symbol_init.c\ mach/symbol_sort.c\ mach_64/get_section_64.c\ mach_64/mach_64.c\ -mach_64/nm_mach_64.c\ mach_64/otool_mach_64.c\ mach_64/symbol_64_filter.c\ mach_64/symbol_64_format.c\ mach_64/symbol_64_free.c\ mach_64/symbol_64_init.c\ mach_64/symbol_64_sort.c\ +memcheck.c\ ppc/otool_ppc.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) @@ -89,7 +87,7 @@ $(LIBFT_LIB): @make -C $(LIBFT_DIR) $(OBJ_DIR) : - @mkdir -p $(OBJ_DIR) + @mkdir -p $@ @mkdir -p $(dir $(OBJS)) $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) diff --git a/nm-otool/includes/ft_nm.h b/nm-otool/includes/ft_nm.h index 56caab6f..d5dc6f4e 100644 --- a/nm-otool/includes/ft_nm.h +++ b/nm-otool/includes/ft_nm.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:44:40 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:19:05 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 11:57:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,18 +52,10 @@ # define NM_OFORMAT (1 << 9) # define NM_MFORMAT (1 << 10) -typedef struct s_nmdata t_nmdata; typedef enum e_symtype t_symtype; typedef struct s_symbolmap t_symbolmap; typedef struct s_machodata t_machodata; -struct s_nmdata -{ - t_flag flag; - char **av_data; - char *filename; -}; - enum e_symtype { SYM_UNDF, @@ -79,7 +71,7 @@ enum e_symtype struct s_machodata { - void *file; + t_fdata *file; t_list *sects; t_list *symbols; struct symtab_command *symtab; @@ -92,6 +84,6 @@ struct s_symbolmap char *s; }; -void nm_single_file(void *file, t_nmdata *data); +void nm_single_file(void *file, t_fdata *data); #endif diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index d3cc5e94..ae568324 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/11/01 12:20:22 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:13:41 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,24 @@ # include # include +typedef unsigned int t_u; +typedef struct s_fdata t_fdata; + +# define IS_MACH_64(x) (*(t_u*)x == MH_MAGIC_64||*(t_u*)x == MH_CIGAM_64) +# define IS_MACH_32(x) (*(t_u*)x == MH_MAGIC||*(t_u*)x == MH_CIGAM) +# define IS_FAT(x) (*(t_u*)x == FAT_MAGIC||*(t_u*)x == FAT_CIGAM) +# define IS_REV(x) ({t_u m=*(t_u*)x;m==MH_CIGAM||m==MH_CIGAM_64||m==FAT_CIGAM;}) +# define MC(ptr) memcheck(data->file, ptr, sizeof(ptr), __FUNCTION__, __LINE__) + +struct s_fdata +{ + t_flag flag; + char **av_data; + void *file; + void *eof; + char *filename; +}; + # include "ft_nm.h" # include "ft_otool.h" @@ -33,17 +51,10 @@ # include "mach.h" # include "ppc.h" -typedef unsigned int t_u; - -# define IS_MACH_64(x) (*(t_u*)x == MH_MAGIC_64||*(t_u*)x == MH_CIGAM_64) -# define IS_MACH_32(x) (*(t_u*)x == MH_MAGIC||*(t_u*)x == MH_CIGAM) -# define IS_FAT(x) (*(t_u*)x == FAT_MAGIC||*(t_u*)x == FAT_CIGAM) -# define IS_REV(x) ({t_u m=*(t_u*)x;m==MH_CIGAM||m==MH_CIGAM_64||m==FAT_CIGAM;}) - uint64_t endian(uint64_t n, uint8_t size); -void *fat_extract(struct fat_header *fat, - cpu_type_t cputype, - cpu_subtype_t cpusubtype); +void *fat_extract(struct fat_header *fat, char *aname); +/* void memcheck(t_fdata *file, void *ptr, size_t size); */ +void memcheck(t_fdata *file, void *ptr, size_t size, const char *function, int line); #endif diff --git a/nm-otool/includes/ft_otool.h b/nm-otool/includes/ft_otool.h index aa2477b1..1ac44dca 100644 --- a/nm-otool/includes/ft_otool.h +++ b/nm-otool/includes/ft_otool.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:46:23 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:19:16 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 13:24:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,15 +21,6 @@ # define OTOOL_TEXT (1 << 0) # define OTOOL_DATA (1 << 1) -typedef struct s_otooldata t_otooldata; - -struct s_otooldata -{ - t_flag flag; - char **av_data; - char *filename; -}; - -void otool_single_file(void *file, t_otooldata *data); +void otool_single_file(void *file, t_fdata *data); #endif diff --git a/nm-otool/includes/mach.h b/nm-otool/includes/mach.h index d64ecc9e..1ded12e9 100644 --- a/nm-otool/includes/mach.h +++ b/nm-otool/includes/mach.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* mach.h :+: :+: :+: */ +/* mach.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/11/01 12:03:56 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:08:34 by jhalford ### ########.fr */ +/* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */ +/* Updated: 2017/11/07 15:26:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ struct s_symbol t_symtype type; char *string; struct nlist nlist; - struct section *section; + struct section *section; }; int symbol_init(t_symbol *symbol, @@ -32,13 +32,11 @@ int symbol_sort(t_list **syms, t_flag flag); int symbol_filter(t_list **syms, t_flag flag); int is_external(t_symbol *s); int is_not_external(t_symbol *s); -int symbol_format(t_symbol *symbol, t_nmdata *data); -void *get_section(struct mach_header *file, - char *lookup); +int symbol_format(t_symbol *symbol, t_fdata *data); +void *get_section(t_machodata *data, char *lookup); void mach_parse(t_machodata *data); -void nm_mach(struct mach_header *file, - t_nmdata *data); -void otool_mach(void *file, t_otooldata *data); +void nm_mach(t_fdata *data); +void otool_mach(t_fdata *data); #endif diff --git a/nm-otool/includes/mach_64.h b/nm-otool/includes/mach_64.h index 39a7deec..2019645f 100644 --- a/nm-otool/includes/mach_64.h +++ b/nm-otool/includes/mach_64.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:08:51 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:26:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,13 +32,11 @@ int symbol_64_sort(t_list **syms, t_flag flag); int symbol_64_filter(t_list **syms, t_flag flag); int is_external_64(t_symbol_64 *s); int is_not_external_64(t_symbol_64 *s); -int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data); -void *get_section_64(struct mach_header_64 *file, - char *lookup); +int symbol_64_format(t_symbol_64 *symbol, t_fdata *data); +void *get_section_64(t_machodata *data, char *lookup); void mach_64_parse(t_machodata *data); -void nm_mach_64(struct mach_header_64 *file, - t_nmdata *data); -void otool_mach_64(void *file, t_otooldata *data); +void nm_mach_64(t_fdata *data); +void otool_mach_64(t_fdata *data); #endif diff --git a/nm-otool/includes/ppc.h b/nm-otool/includes/ppc.h index 197c1f92..b2deeaef 100644 --- a/nm-otool/includes/ppc.h +++ b/nm-otool/includes/ppc.h @@ -6,13 +6,13 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 11:38:10 by jhalford #+# #+# */ -/* Updated: 2017/11/01 11:38:29 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:29:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef PPC_H # define PPC_H -void otool_ppc(void *file, t_otooldata *data); +void otool_ppc(t_fdata *data); #endif diff --git a/nm-otool/libft/srcs/printing/hexdump.c b/nm-otool/libft/srcs/printing/hexdump.c index b139b588..4cc9fd7c 100644 --- a/nm-otool/libft/srcs/printing/hexdump.c +++ b/nm-otool/libft/srcs/printing/hexdump.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/07 17:56:14 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:57:09 by jhalford ### ########.fr */ +/* Updated: 2017/11/01 15:29:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/scripts/42corr.sh b/nm-otool/scripts/42corr.sh index 91445069..fed6c549 100755 --- a/nm-otool/scripts/42corr.sh +++ b/nm-otool/scripts/42corr.sh @@ -22,12 +22,12 @@ make #bibliotheques dynamiques ./scripts/nmdiff.sh /usr/lib/libcompression.dylib + #biblioteque universelle ./scripts/nmdiff.sh /usr/bin/audiodevice ./scripts/nmdiff.sh /usr/bin/python - ########## # OTOOL ########## diff --git a/nm-otool/scripts/mdissect.sh b/nm-otool/scripts/mdissect.sh new file mode 100755 index 00000000..50564f80 --- /dev/null +++ b/nm-otool/scripts/mdissect.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Same as "dissect.sh" but without stdin + +### Test ft_nm on truncated files +### Run this in a temporary directory, like /tmp + +#set -e # Stop on error + +if [ -z "$1" ] || [ -z "$2" ] +then + echo "Usage: $0 ./ft_nm file" + exit 1 +fi + +filename=$2 + +# Retrieve size and trim spaces +size=$(wc -c < "$filename" | xargs) + +echo $filename $size + +for (( i=$size; i>0; i--)) +do + outname="$filename.part$i.__tmp" + head -c $i "$filename" > "$outname" + $( $1 $outname 1>/dev/null 2>/dev/null ) + #$( head -c $i "$filename" | $1 - 1>/dev/null 2>&1 ) + retval=$? + echo -n "Part $i: " + if [ $retval -ne 0 ] + then + if [ $retval -ne 1 ] + then + echo "Fatal error: exit status $retval" + exit + else + echo "Exit 1" + fi + else + echo "Ok" + fi + rm $outname +done diff --git a/nm-otool/srcs/endianness.c b/nm-otool/srcs/endianness.c index 06547257..75cf66ba 100644 --- a/nm-otool/srcs/endianness.c +++ b/nm-otool/srcs/endianness.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 15:18:54 by jhalford #+# #+# */ -/* Updated: 2017/10/30 15:58:55 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 14:32:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/srcs/fat.c b/nm-otool/srcs/fat.c index cc388817..c2fd1b20 100644 --- a/nm-otool/srcs/fat.c +++ b/nm-otool/srcs/fat.c @@ -6,27 +6,28 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 16:31:48 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:23:08 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 14:29:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -void *fat_extract(struct fat_header *fat, - cpu_type_t cputype, - cpu_subtype_t cpusubtype) +void *fat_extract(struct fat_header *fat, char *aname) { uint32_t narch; - struct fat_arch *arch; + struct fat_arch *obj; + const NXArchInfo *arch; narch = endian(fat->nfat_arch, 32); - arch = (struct fat_arch*)(fat + 1); + obj = (struct fat_arch*)(fat + 1); while (narch--) { - if ((int32_t)endian(arch->cputype, 32) == cputype - && (int32_t)endian(arch->cpusubtype, 32) & cpusubtype) - return ((void *)fat + endian(arch->offset, 32)); - ++arch; + arch = NXGetArchInfoFromCpuType( + endian(obj->cputype, 32), + endian(obj->cpusubtype, 32)); + if (ft_strequ(aname, arch->name)) + return ((void *)fat + endian(obj->offset, 32)); + ++obj; } return (NULL); } diff --git a/nm-otool/srcs/ft_nm.c b/nm-otool/srcs/ft_nm.c index cceda8ab..070771d1 100644 --- a/nm-otool/srcs/ft_nm.c +++ b/nm-otool/srcs/ft_nm.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:52:01 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 14:37:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,14 +33,14 @@ t_cliopts g_nm_opts[] = {0, 0, 0, 0, 0, 0}, }; -void nm_fat_file(struct fat_header *fat, t_nmdata *data) +void nm_fat_file(struct fat_header *fat, t_fdata *data) { uint32_t narch; struct fat_arch *obj; const NXArchInfo *arch; g_rev = IS_REV(fat); - if ((obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL))) + if ((obj = fat_extract(fat, "x86_64"))) nm_single_file(obj, data); else { @@ -60,15 +60,16 @@ void nm_fat_file(struct fat_header *fat, t_nmdata *data) } } -void nm_single_file(void *file, t_nmdata *data) +void nm_single_file(void *file, t_fdata *data) { const NXArchInfo *arch; + data->file = file; g_rev = IS_REV(file); if (IS_MACH_32(file)) - nm_mach(file, data); + nm_mach(data); else if (IS_MACH_64(file)) - nm_mach_64(file, data); + nm_mach_64(data); else { arch = NXGetArchInfoFromCpuType( @@ -81,28 +82,28 @@ void nm_single_file(void *file, t_nmdata *data) } } -int nm(int ac, char **av, t_nmdata data) +int nm(int ac, char **av, t_fdata data) { int i; struct stat buf; int fd; - void *file; i = data.av_data - av; while (i < ac && (data.filename = av[i])) { if ((fd = open(data.filename, O_RDONLY)) < 0 || fstat(fd, &buf) < 0) return (1); - if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) + if ((data.file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) return (1); + data.eof = data.file + buf.st_size; if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) ft_printf("\n%s:\n", data.filename); - if (IS_FAT(file)) - nm_fat_file(file, &data); + if (IS_FAT(data.file)) + nm_fat_file(data.file, &data); else - nm_single_file(file, &data); - if (munmap(file, buf.st_size)) + nm_single_file(data.file, &data); + if (munmap(data.file, buf.st_size)) return (1); i++; } @@ -111,7 +112,7 @@ int nm(int ac, char **av, t_nmdata data) int main(int ac, char **av) { - t_nmdata data; + t_fdata data; data.flag = NM_ASORT; if (cliopts_get(av, g_nm_opts, &data)) diff --git a/nm-otool/srcs/ft_otool.c b/nm-otool/srcs/ft_otool.c index 19059982..984744a0 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/11/01 12:33:22 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:16:18 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,14 +20,14 @@ t_cliopts g_otool_opts[] = }; int g_rev = 0; -void otool_fat_file(struct fat_header *fat, t_otooldata *data) +void otool_fat_file(struct fat_header *fat, t_fdata *data) { uint32_t narch; struct fat_arch *obj; const NXArchInfo *arch; g_rev = IS_REV(fat); - if ((obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL))) + if ((obj = fat_extract(fat, "x86_64"))) { ft_printf("%s:\n", data->filename); otool_single_file(obj, data); @@ -49,10 +49,11 @@ void otool_fat_file(struct fat_header *fat, t_otooldata *data) } } -void otool_single_file(void *file, t_otooldata *data) +void otool_single_file(void *file, t_fdata *data) { const NXArchInfo *arch; + data->file = file; g_rev = IS_REV(file); arch = NXGetArchInfoFromCpuType( endian(*((int32_t*)file + 1), 32), @@ -60,11 +61,11 @@ void otool_single_file(void *file, t_otooldata *data) if (arch) { if (ft_strcmp(arch->name, "ppc") == 0) - otool_ppc(file, data); + otool_ppc(data); else if (IS_MACH_32(file)) - otool_mach(file, data); + otool_mach(data); else if (IS_MACH_64(file)) - otool_mach_64(file, data); + otool_mach_64(data); else ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name); } @@ -72,29 +73,29 @@ void otool_single_file(void *file, t_otooldata *data) ft_dprintf(2, "unknown architecture, magic=%#x\n", *((int32_t*)file)); } -int otool(int ac, char **av, t_otooldata data) +int otool(int ac, char **av, t_fdata data) { int i; struct stat buf; int fd; - void *file; i = data.av_data - av; while (i < ac && (data.filename = av[i])) { if ((fd = open((av[i]), O_RDONLY)) < 0 || fstat(fd, &buf) < 0) return (1); - if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) + if ((data.file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED) return (1); - if (IS_FAT(file)) - otool_fat_file(file, &data); + data.eof = data.file + buf.st_size; + if (IS_FAT(data.file)) + otool_fat_file(data.file, &data); else { ft_printf("%s:\n", data.filename); - otool_single_file(file, &data); + otool_single_file(data.file, &data); } - if (munmap(file, buf.st_size)) + if (munmap(data.file, buf.st_size)) return (1); i++; } @@ -103,7 +104,7 @@ int otool(int ac, char **av, t_otooldata data) int main(int ac, char **av) { - t_otooldata data; + t_fdata data; if (cliopts_get(av, g_otool_opts, &data)) { diff --git a/nm-otool/srcs/mach/get_section.c b/nm-otool/srcs/mach/get_section.c index c8796cbf..f3d6c4cb 100644 --- a/nm-otool/srcs/mach/get_section.c +++ b/nm-otool/srcs/mach/get_section.c @@ -1,68 +1,65 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_section.c :+: :+: :+: */ +/* get_section.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:00:40 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:11:10 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:26:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -void *get_segment(struct mach_header *file, char *lookup) +void *get_segment(t_machodata *data, char *lookup) { uint32_t i; struct load_command *lc; - struct segment_command *seg; + struct segment_command *seg; uint32_t ncmds; - lc = (void*)(file + 1); + MC(lc = (void*)((struct mach_header*)data->file->file + 1)); i = -1; - ncmds = endian(file->ncmds, 32); - DG("ncmds=%i", ncmds); + ncmds = endian(((struct mach_header*)data->file->file)->ncmds, 32); while (++i < ncmds) { - DG("i=%i", i); if (endian(lc->cmd, 32) & LC_SEGMENT) { - seg = (struct segment_command*)lc; - DG("segment: %s", seg->segname); + MC(seg = (struct segment_command*)lc); if (ft_strcmp(seg->segname, lookup) == 0) return (seg); } - lc = (void*)lc + endian(lc->cmdsize, 32); + MC(lc = (void*)lc + endian(lc->cmdsize, 32)); } return (NULL); } -void *get_section(struct mach_header *file, char *lookup) +void *get_section(t_machodata *data, char *lookup) { uint32_t i; uint32_t j; struct load_command *lc; - struct segment_command *seg; - struct section *sect; + struct segment_command *seg; + struct section *sect; - lc = (void*)(file + 1); + MC(lc = (void*)((struct mach_header*)data->file->file + 1)); i = -1; - while (++i < endian(file->ncmds, 32)) + while (++i < endian(((struct mach_header*)data->file->file)->ncmds, 32)) { if (endian(lc->cmd, 32) & LC_SEGMENT) { - seg = (struct segment_command*)lc; - sect = (void*)(seg + 1); + MC(seg = (struct segment_command*)lc); + MC(sect = (void*)(seg + 1)); j = -1; while (++j < endian(seg->nsects, 32)) { if (ft_strcmp(sect->sectname, lookup) == 0) return (sect); - sect = sect + 1; + MC(sect = sect + 1); } } - lc = (void*)lc + endian(lc->cmdsize, 32); + MC(lc = (void*)lc + endian(lc->cmdsize, 32)); } return (NULL); } diff --git a/nm-otool/srcs/mach/mach.c b/nm-otool/srcs/mach/mach.c index 7dc0aaab..7c9395e4 100644 --- a/nm-otool/srcs/mach/mach.c +++ b/nm-otool/srcs/mach/mach.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* mach.c :+: :+: :+: */ +/* mach.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:09:52 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:29:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,14 @@ static void symtab_parse(t_machodata *data, struct symtab_command *symtab) t_symbol symbol; char *stringtable; struct nlist *array; + int nsyms; data->symtab = symtab; - stringtable = data->file + endian(symtab->stroff, 32); - array = (struct nlist*)(data->file + endian(symtab->symoff, 32)); + MC(stringtable = data->file->file + endian(symtab->stroff, 32)); + MC(array = (struct nlist*)(data->file->file + endian(symtab->symoff, 32))); + nsyms = endian(symtab->nsyms, 32); i = -1; - while (++i < (int)endian(symtab->nsyms, 32)) + while (++i < nsyms) { symbol_init(&symbol, stringtable, array, i); symbol_set(&symbol, data); @@ -35,15 +37,15 @@ static void seg_parse(t_machodata *data, struct segment_command *seg) { uint32_t nsects; uint32_t i; - struct section *sect; + struct section *sect; nsects = endian(seg->nsects, 32); - sect = (void*)(seg + 1); + MC(sect = (void*)(seg + 1)); i = -1; while (++i < nsects) { ft_lsteadd(&data->sects, ft_lstnew(§, sizeof(sect))); - sect = sect + 1; + MC(sect = sect + 1); } } @@ -52,11 +54,11 @@ void mach_parse(t_machodata *data) uint32_t ncmds; uint32_t i; struct load_command *lc; - struct mach_header *header; + struct mach_header *header; - header = data->file; + header = data->file->file; ncmds = endian(header->ncmds, 32); - lc = (void*)(header + 1); + MC(lc = (void*)(header + 1)); i = -1; while (++i < ncmds) { @@ -64,6 +66,19 @@ void mach_parse(t_machodata *data) symtab_parse(data, (struct symtab_command*)lc); else if (endian(lc->cmd, 32) == LC_SEGMENT) seg_parse(data, (struct segment_command*)lc); - lc = (void*)lc + endian(lc->cmdsize, 32); + MC(lc = (void*)lc + endian(lc->cmdsize, 32)); } } + +void nm_mach(t_fdata *data) +{ + t_machodata mach; + + mach.sects = NULL; + mach.symbols = NULL; + mach.file = data; + mach_parse(&mach); + symbol_sort(&mach.symbols, data->flag); + symbol_filter(&mach.symbols, data->flag); + ft_lstiter(mach.symbols, symbol_format, data); +} diff --git a/nm-otool/srcs/mach/otool_mach.c b/nm-otool/srcs/mach/otool_mach.c index ac5f7e65..d48f8b05 100644 --- a/nm-otool/srcs/mach/otool_mach.c +++ b/nm-otool/srcs/mach/otool_mach.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* otool_mach.c :+: :+: :+: */ +/* otool_mach.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:58:39 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:27:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,27 @@ extern int g_rev; -void otool_mach(void *file, t_otooldata *data) +void otool_mach(t_fdata *data) { struct section *sect; + t_machodata mach; + mach.file = data; if (data->flag & OTOOL_TEXT) { - if ((sect = get_section(file, SECT_TEXT))) + if ((sect = get_section(&mach, SECT_TEXT))) { ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT); - hexdump(file + endian(sect->offset, 32), + hexdump(data->file + endian(sect->offset, 32), endian(sect->addr, 32), endian(sect->size, 32), g_rev); } } if (data->flag & OTOOL_DATA) { - if ((sect = get_section(file, SECT_DATA))) + if ((sect = get_section(&mach, SECT_DATA))) { ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA); - hexdump(file + endian(sect->offset, 32), + hexdump(data->file + endian(sect->offset, 32), endian(sect->addr, 32), endian(sect->size, 32), g_rev); } } diff --git a/nm-otool/srcs/mach/symbol_filter.c b/nm-otool/srcs/mach/symbol_filter.c index 84f6cd3f..9d7f71fb 100644 --- a/nm-otool/srcs/mach/symbol_filter.c +++ b/nm-otool/srcs/mach/symbol_filter.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* symbol_filter.c :+: :+: :+: */ +/* symbol_filter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:11:33 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 16:36:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/srcs/mach/symbol_format.c b/nm-otool/srcs/mach/symbol_format.c index 80fdedef..08983141 100644 --- a/nm-otool/srcs/mach/symbol_format.c +++ b/nm-otool/srcs/mach/symbol_format.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* symbol_format.c :+: :+: :+: */ +/* symbol_format.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/11/01 12:33:27 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:33:27 by jhalford ### ########.fr */ +/* Created: 2017/11/01 12:37:07 by jhalford #+# #+# */ +/* Updated: 2017/11/07 13:57:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,14 +53,13 @@ void symbol_format_m(t_symbol *symbol) void symbol_format_full(t_symbol *symbol) { - ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x", - symbol->pos, + ft_printf(" %02i(%s)\t %04x %03b|%b|%x|%b \t", + symbol->nlist.n_sect, symbol->section->sectname, + symbol->nlist.n_desc, (symbol->nlist.n_type & N_STAB) >> 5, (symbol->nlist.n_type & N_PEXT) >> 4, symbol->nlist.n_type & N_TYPE, - symbol->nlist.n_type & N_EXT, - symbol->nlist.n_sect, symbol->section->sectname, - symbol->nlist.n_desc); + symbol->nlist.n_type & N_EXT); } void symbol_format_dfl(t_symbol *symbol) @@ -78,16 +77,19 @@ void symbol_format_dfl(t_symbol *symbol) symbol->nlist.n_type); } -int symbol_format(t_symbol *symbol, t_nmdata *data) +int symbol_format(t_symbol *symbol, t_fdata *data) { + int nbyte; + + nbyte = ft_strstr(__FUNCTION__, "64") ? 16 : 8; if (data->flag & NM_OFORMAT) ft_printf("%s: ", data->filename); if (!(data->flag & NM_ONLY_UNDF)) { if (symbol->type == SYM_UNDF) - ft_printf("%8s", " "); + ft_printf("%*s", nbyte, " "); else - ft_printf("%08llx", symbol->nlist.n_value); + ft_printf("%0*llx", nbyte, symbol->nlist.n_value); if (data->flag & NM_MFORMAT) symbol_format_m(symbol); else if (data->flag & NM_FULL) @@ -95,6 +97,8 @@ int symbol_format(t_symbol *symbol, t_nmdata *data) else symbol_format_dfl(symbol); } - ft_printf(" %s\n", symbol->string); + ft_putchar(' '); + ft_putstr(symbol->string); + ft_putchar('\n'); return (0); } diff --git a/nm-otool/srcs/mach/symbol_free.c b/nm-otool/srcs/mach/symbol_free.c index 5cb6ee04..6905fdac 100644 --- a/nm-otool/srcs/mach/symbol_free.c +++ b/nm-otool/srcs/mach/symbol_free.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/26 17:06:23 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:11:42 by jhalford ### ########.fr */ +/* Updated: 2017/10/26 18:38:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/srcs/mach/symbol_init.c b/nm-otool/srcs/mach/symbol_init.c index 4480ceb6..760627d9 100644 --- a/nm-otool/srcs/mach/symbol_init.c +++ b/nm-otool/srcs/mach/symbol_init.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* symbol_init.c :+: :+: :+: */ +/* symbol_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:36:06 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:30:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,15 @@ t_machodata *g_data; int symbol_init(t_symbol *symbol, char *stringtable, struct nlist *array, int i) { + size_t nvaluesize; + symbol->type = 0; symbol->pos = i; symbol->nlist.n_type = endian(array[i].n_type, 8); symbol->nlist.n_sect = endian(array[i].n_sect, 8); symbol->nlist.n_desc = endian(array[i].n_desc, 16); - symbol->nlist.n_value = endian(array[i].n_value, 32); + nvaluesize = ft_strstr(__FUNCTION__, "64") ? 64 : 32; + symbol->nlist.n_value = endian(array[i].n_value, nvaluesize); symbol->string = stringtable + endian(array[i].n_un.n_strx, 32); return (0); } diff --git a/nm-otool/srcs/mach/symbol_sort.c b/nm-otool/srcs/mach/symbol_sort.c index e70fbea7..b135e138 100644 --- a/nm-otool/srcs/mach/symbol_sort.c +++ b/nm-otool/srcs/mach/symbol_sort.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ -/* Updated: 2017/10/31 16:32:46 by jhalford ### ########.fr */ +/* Updated: 2017/10/31 19:05:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ static int sym_asort(t_symbol *sym1, t_symbol *sym2) static int sym_nsort(t_symbol *sym1, t_symbol *sym2) { if (sym1->nlist.n_value == sym2->nlist.n_value) - return (sym_asort(sym1, sym2)); - return (sym1->nlist.n_value > sym2->nlist.n_value ? 1 : -1); + return (ft_strcmp(sym1->string, sym2->string)); + return (sym1->nlist.n_value - sym2->nlist.n_value); } int symbol_sort(t_list **syms, t_flag flag) diff --git a/nm-otool/srcs/mach_64/get_section_64.c b/nm-otool/srcs/mach_64/get_section_64.c index 17295a38..4c86f7b6 100644 --- a/nm-otool/srcs/mach_64/get_section_64.c +++ b/nm-otool/srcs/mach_64/get_section_64.c @@ -6,39 +6,36 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:00:40 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:37:02 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:26:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -void *get_segment_64(struct mach_header_64 *file, char *lookup) +void *get_segment_64(t_machodata *data, char *lookup) { uint32_t i; struct load_command *lc; struct segment_command_64 *seg; uint32_t ncmds; - lc = (void*)(file + 1); + MC(lc = (void*)((struct mach_header_64*)data->file->file + 1)); i = -1; - ncmds = endian(file->ncmds, 32); - DG("ncmds=%i", ncmds); + ncmds = endian(((struct mach_header_64*)data->file->file)->ncmds, 32); while (++i < ncmds) { - DG("i=%i", i); if (endian(lc->cmd, 32) & LC_SEGMENT_64) { - seg = (struct segment_command_64*)lc; - DG("segment: %s", seg->segname); + MC(seg = (struct segment_command_64*)lc); if (ft_strcmp(seg->segname, lookup) == 0) return (seg); } - lc = (void*)lc + endian(lc->cmdsize, 32); + MC(lc = (void*)lc + endian(lc->cmdsize, 32)); } return (NULL); } -void *get_section_64(struct mach_header_64 *file, char *lookup) +void *get_section_64(t_machodata *data, char *lookup) { uint32_t i; uint32_t j; @@ -46,23 +43,23 @@ void *get_section_64(struct mach_header_64 *file, char *lookup) struct segment_command_64 *seg; struct section_64 *sect; - lc = (void*)(file + 1); + MC(lc = (void*)((struct mach_header_64*)data->file->file + 1)); i = -1; - while (++i < endian(file->ncmds, 32)) + while (++i < endian(((struct mach_header_64*)data->file->file)->ncmds, 32)) { if (endian(lc->cmd, 32) & LC_SEGMENT_64) { - seg = (struct segment_command_64*)lc; - sect = (void*)(seg + 1); + MC(seg = (struct segment_command_64*)lc); + MC(sect = (void*)(seg + 1)); j = -1; while (++j < endian(seg->nsects, 32)) { if (ft_strcmp(sect->sectname, lookup) == 0) return (sect); - sect = sect + 1; + MC(sect = sect + 1); } } - lc = (void*)lc + endian(lc->cmdsize, 32); + MC(lc = (void*)lc + endian(lc->cmdsize, 32)); } return (NULL); } diff --git a/nm-otool/srcs/mach_64/mach_64.c b/nm-otool/srcs/mach_64/mach_64.c index 2957c37c..7fa768c8 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/11/01 12:36:21 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:29:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,12 +18,14 @@ static void symtab_64_parse(t_machodata *data, struct symtab_command *symtab) t_symbol_64 symbol; char *stringtable; struct nlist_64 *array; + int nsyms; data->symtab = symtab; - stringtable = data->file + endian(symtab->stroff, 32); - array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32)); + MC(stringtable = data->file->file + endian(symtab->stroff, 32)); + MC(array = (struct nlist_64*)(data->file->file + endian(symtab->symoff, 32))); + nsyms = endian(symtab->nsyms, 32); i = -1; - while (++i < (int)endian(symtab->nsyms, 32)) + while (++i < nsyms) { symbol_64_init(&symbol, stringtable, array, i); symbol_64_set(&symbol, data); @@ -38,12 +40,12 @@ static void seg_64_parse(t_machodata *data, struct segment_command_64 *seg) struct section_64 *sect; nsects = endian(seg->nsects, 32); - sect = (void*)(seg + 1); + MC(sect = (void*)(seg + 1)); i = -1; while (++i < nsects) { ft_lsteadd(&data->sects, ft_lstnew(§, sizeof(sect))); - sect = sect + 1; + MC(sect = sect + 1); } } @@ -54,9 +56,9 @@ void mach_64_parse(t_machodata *data) struct load_command *lc; struct mach_header_64 *header; - header = data->file; + header = data->file->file; ncmds = endian(header->ncmds, 32); - lc = (void*)(header + 1); + MC(lc = (void*)(header + 1)); i = -1; while (++i < ncmds) { @@ -64,6 +66,19 @@ void mach_64_parse(t_machodata *data) symtab_64_parse(data, (struct symtab_command*)lc); else if (endian(lc->cmd, 32) == LC_SEGMENT_64) seg_64_parse(data, (struct segment_command_64*)lc); - lc = (void*)lc + endian(lc->cmdsize, 32); + MC(lc = (void*)lc + endian(lc->cmdsize, 32)); } } + +void nm_mach_64(t_fdata *data) +{ + t_machodata mach; + + mach.sects = NULL; + mach.symbols = NULL; + mach.file = data; + mach_64_parse(&mach); + symbol_64_sort(&mach.symbols, data->flag); + symbol_64_filter(&mach.symbols, data->flag); + ft_lstiter(mach.symbols, symbol_64_format, data); +} diff --git a/nm-otool/srcs/mach_64/nm_mach_64.c b/nm-otool/srcs/mach_64/nm_mach_64.c deleted file mode 100644 index c73297a2..00000000 --- a/nm-otool/srcs/mach_64/nm_mach_64.c +++ /dev/null @@ -1,26 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* nm_mach_64.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */ -/* Updated: 2017/10/30 15:50:23 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "ft_nm_otool.h" - -void nm_mach_64(struct mach_header_64 *file, t_nmdata *data) -{ - t_machodata mach; - - mach.sects = NULL; - mach.symbols = NULL; - mach.file = file; - mach_64_parse(&mach); - symbol_64_sort(&mach.symbols, data->flag); - symbol_64_filter(&mach.symbols, data->flag); - ft_lstiter(mach.symbols, symbol_64_format, data); -} diff --git a/nm-otool/srcs/mach_64/otool_mach_64.c b/nm-otool/srcs/mach_64/otool_mach_64.c index c42c8f18..9a51bb0b 100644 --- a/nm-otool/srcs/mach_64/otool_mach_64.c +++ b/nm-otool/srcs/mach_64/otool_mach_64.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:58:53 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:27:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,27 @@ extern int g_rev; -void otool_mach_64(void *file, t_otooldata *data) +void otool_mach_64(t_fdata *data) { struct section_64 *sect; + t_machodata mach; + mach.file = data; if (data->flag & OTOOL_TEXT) { - if ((sect = get_section_64(file, SECT_TEXT))) + if ((sect = get_section_64(&mach, SECT_TEXT))) { ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT); - hexdump_64(file + endian(sect->offset, 32), + hexdump_64(data->file + endian(sect->offset, 32), endian(sect->addr, 32), endian(sect->size, 32), g_rev); } } if (data->flag & OTOOL_DATA) { - if ((sect = get_section_64(file, SECT_DATA))) + if ((sect = get_section_64(&mach, SECT_DATA))) { ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA); - hexdump_64(file + endian(sect->offset, 32), + hexdump_64(data->file + endian(sect->offset, 32), endian(sect->addr, 32), endian(sect->size, 32), g_rev); } } diff --git a/nm-otool/srcs/mach_64/symbol_64_format.c b/nm-otool/srcs/mach_64/symbol_64_format.c index 4bc470e1..55e6873d 100644 --- a/nm-otool/srcs/mach_64/symbol_64_format.c +++ b/nm-otool/srcs/mach_64/symbol_64_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 12:37:07 by jhalford #+# #+# */ -/* Updated: 2017/11/01 13:20:07 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 13:57:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,16 +77,19 @@ void symbol_64_format_dfl(t_symbol_64 *symbol) symbol->nlist.n_type); } -int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data) +int symbol_64_format(t_symbol_64 *symbol, t_fdata *data) { + int nbyte; + + nbyte = ft_strstr(__FUNCTION__, "64") ? 16 : 8; if (data->flag & NM_OFORMAT) ft_printf("%s: ", data->filename); if (!(data->flag & NM_ONLY_UNDF)) { if (symbol->type == SYM_UNDF) - ft_printf("%16s", " "); + ft_printf("%*s", nbyte, " "); else - ft_printf("%016llx", symbol->nlist.n_value); + ft_printf("%0*llx", nbyte, symbol->nlist.n_value); if (data->flag & NM_MFORMAT) symbol_64_format_m(symbol); else if (data->flag & NM_FULL) diff --git a/nm-otool/srcs/mach_64/symbol_64_init.c b/nm-otool/srcs/mach_64/symbol_64_init.c index cd1d9d9e..7d1c52bc 100644 --- a/nm-otool/srcs/mach_64/symbol_64_init.c +++ b/nm-otool/srcs/mach_64/symbol_64_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */ -/* Updated: 2017/10/31 17:52:38 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:30:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,15 @@ t_machodata *g_data; int symbol_64_init(t_symbol_64 *symbol, char *stringtable, struct nlist_64 *array, int i) { + size_t nvaluesize; + symbol->type = 0; symbol->pos = i; symbol->nlist.n_type = endian(array[i].n_type, 8); symbol->nlist.n_sect = endian(array[i].n_sect, 8); symbol->nlist.n_desc = endian(array[i].n_desc, 16); - symbol->nlist.n_value = endian(array[i].n_value, 64); + nvaluesize = ft_strstr(__FUNCTION__, "64") ? 64 : 32; + symbol->nlist.n_value = endian(array[i].n_value, nvaluesize); symbol->string = stringtable + endian(array[i].n_un.n_strx, 32); return (0); } diff --git a/nm-otool/srcs/mach/nm_mach.c b/nm-otool/srcs/memcheck.c similarity index 56% rename from nm-otool/srcs/mach/nm_mach.c rename to nm-otool/srcs/memcheck.c index 539655c3..588dd1e4 100644 --- a/nm-otool/srcs/mach/nm_mach.c +++ b/nm-otool/srcs/memcheck.c @@ -1,26 +1,24 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* nm_mach.c :+: :+: :+: */ +/* memcheck.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:10:08 by jhalford ### ########.fr */ +/* Created: 2017/11/07 11:24:09 by jhalford #+# #+# */ +/* Updated: 2017/11/07 15:19:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "ft_nm_otool.h" -void nm_mach(struct mach_header *file, t_nmdata *data) +void memcheck(t_fdata *file, void *ptr, size_t size, const char *function, int line) { - t_machodata mach; - - mach.sects = NULL; - mach.symbols = NULL; - mach.file = file; - mach_parse(&mach); - symbol_sort(&mach.symbols, data->flag); - symbol_filter(&mach.symbols, data->flag); - ft_lstiter(mach.symbols, symbol_format, data); + if (ptr <= file->file || (ptr + size) >= file->eof) + { + ft_dprintf(2, "%s:%i ", function, line); + ft_dprintf(2, "%s: is corrupted\n", file->filename); + ft_dprintf(2, "%p - %p (%zu) - %p --> %zu after end\n", file->file, ptr, size, file->eof, ptr - file->eof); + exit(1); + } } diff --git a/nm-otool/srcs/ppc/otool_ppc.c b/nm-otool/srcs/ppc/otool_ppc.c index 18725fef..b8d45c2c 100644 --- a/nm-otool/srcs/ppc/otool_ppc.c +++ b/nm-otool/srcs/ppc/otool_ppc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/11/01 11:32:36 by jhalford #+# #+# */ -/* Updated: 2017/11/01 12:58:28 by jhalford ### ########.fr */ +/* Updated: 2017/11/07 15:28:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,25 +14,27 @@ extern int g_rev; -void otool_ppc(void *file, t_otooldata *data) +void otool_ppc(t_fdata *data) { struct section *sect; + t_machodata mach; + mach.file = data; if (data->flag & OTOOL_TEXT) { - if ((sect = get_section(file, SECT_TEXT))) + if ((sect = get_section(&mach, SECT_TEXT))) { ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT); - hexdump_int(file + endian(sect->offset, 32), + hexdump_int(data->file + endian(sect->offset, 32), endian(sect->addr, 32), endian(sect->size, 32), g_rev); } } if (data->flag & OTOOL_DATA) { - if ((sect = get_section(file, SECT_DATA))) + if ((sect = get_section(&mach, SECT_DATA))) { ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA); - hexdump_int(file + endian(sect->offset, 32), + hexdump_int(data->file + endian(sect->offset, 32), endian(sect->addr, 32), endian(sect->size, 32), g_rev); } }