okay for corrupted binaries

This commit is contained in:
Jack Halford 2017-11-07 15:53:08 +01:00
parent a997e64d17
commit 3301d5880c
30 changed files with 292 additions and 242 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ # # By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 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 #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -25,7 +25,6 @@ LIBFT_LIB = $(LIBFT_DIR)libft.a
LIBFT_INC = $(LIBFT_DIR)includes/ LIBFT_INC = $(LIBFT_DIR)includes/
LIBS = LIBS =
SRC_DIR = srcs/ SRC_DIR = srcs/
INC_DIR = includes/ INC_DIR = includes/
OBJ_DIR = objs/ OBJ_DIR = objs/
@ -40,7 +39,6 @@ ft_nm.c\
ft_otool.c\ ft_otool.c\
mach/get_section.c\ mach/get_section.c\
mach/mach.c\ mach/mach.c\
mach/nm_mach.c\
mach/otool_mach.c\ mach/otool_mach.c\
mach/symbol_filter.c\ mach/symbol_filter.c\
mach/symbol_format.c\ mach/symbol_format.c\
@ -49,13 +47,13 @@ mach/symbol_init.c\
mach/symbol_sort.c\ mach/symbol_sort.c\
mach_64/get_section_64.c\ mach_64/get_section_64.c\
mach_64/mach_64.c\ mach_64/mach_64.c\
mach_64/nm_mach_64.c\
mach_64/otool_mach_64.c\ mach_64/otool_mach_64.c\
mach_64/symbol_64_filter.c\ mach_64/symbol_64_filter.c\
mach_64/symbol_64_format.c\ mach_64/symbol_64_format.c\
mach_64/symbol_64_free.c\ mach_64/symbol_64_free.c\
mach_64/symbol_64_init.c\ mach_64/symbol_64_init.c\
mach_64/symbol_64_sort.c\ mach_64/symbol_64_sort.c\
memcheck.c\
ppc/otool_ppc.c ppc/otool_ppc.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
@ -89,7 +87,7 @@ $(LIBFT_LIB):
@make -C $(LIBFT_DIR) @make -C $(LIBFT_DIR)
$(OBJ_DIR) : $(OBJ_DIR) :
@mkdir -p $(OBJ_DIR) @mkdir -p $@
@mkdir -p $(dir $(OBJS)) @mkdir -p $(dir $(OBJS))
$(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR) $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:44:40 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_OFORMAT (1 << 9)
# define NM_MFORMAT (1 << 10) # define NM_MFORMAT (1 << 10)
typedef struct s_nmdata t_nmdata;
typedef enum e_symtype t_symtype; typedef enum e_symtype t_symtype;
typedef struct s_symbolmap t_symbolmap; typedef struct s_symbolmap t_symbolmap;
typedef struct s_machodata t_machodata; typedef struct s_machodata t_machodata;
struct s_nmdata
{
t_flag flag;
char **av_data;
char *filename;
};
enum e_symtype enum e_symtype
{ {
SYM_UNDF, SYM_UNDF,
@ -79,7 +71,7 @@ enum e_symtype
struct s_machodata struct s_machodata
{ {
void *file; t_fdata *file;
t_list *sects; t_list *sects;
t_list *symbols; t_list *symbols;
struct symtab_command *symtab; struct symtab_command *symtab;
@ -92,6 +84,6 @@ struct s_symbolmap
char *s; char *s;
}; };
void nm_single_file(void *file, t_nmdata *data); void nm_single_file(void *file, t_fdata *data);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:36:10 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 <mach-o/fat.h> # include <mach-o/fat.h>
# include <mach-o/arch.h> # include <mach-o/arch.h>
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_nm.h"
# include "ft_otool.h" # include "ft_otool.h"
@ -33,17 +51,10 @@
# include "mach.h" # include "mach.h"
# include "ppc.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); uint64_t endian(uint64_t n, uint8_t size);
void *fat_extract(struct fat_header *fat, void *fat_extract(struct fat_header *fat, char *aname);
cpu_type_t cputype, /* void memcheck(t_fdata *file, void *ptr, size_t size); */
cpu_subtype_t cpusubtype); void memcheck(t_fdata *file, void *ptr, size_t size, const char *function, int line);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:46:23 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_TEXT (1 << 0)
# define OTOOL_DATA (1 << 1) # define OTOOL_DATA (1 << 1)
typedef struct s_otooldata t_otooldata; void otool_single_file(void *file, t_fdata *data);
struct s_otooldata
{
t_flag flag;
char **av_data;
char *filename;
};
void otool_single_file(void *file, t_otooldata *data);
#endif #endif

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 12:03:56 by jhalford #+# #+# */ /* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:08:34 by jhalford ### ########.fr */ /* Updated: 2017/11/07 15:26:18 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -32,13 +32,11 @@ int symbol_sort(t_list **syms, t_flag flag);
int symbol_filter(t_list **syms, t_flag flag); int symbol_filter(t_list **syms, t_flag flag);
int is_external(t_symbol *s); int is_external(t_symbol *s);
int is_not_external(t_symbol *s); int is_not_external(t_symbol *s);
int symbol_format(t_symbol *symbol, t_nmdata *data); int symbol_format(t_symbol *symbol, t_fdata *data);
void *get_section(struct mach_header *file, void *get_section(t_machodata *data, char *lookup);
char *lookup);
void mach_parse(t_machodata *data); void mach_parse(t_machodata *data);
void nm_mach(struct mach_header *file, void nm_mach(t_fdata *data);
t_nmdata *data); void otool_mach(t_fdata *data);
void otool_mach(void *file, t_otooldata *data);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:39:31 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 symbol_64_filter(t_list **syms, t_flag flag);
int is_external_64(t_symbol_64 *s); int is_external_64(t_symbol_64 *s);
int is_not_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); int symbol_64_format(t_symbol_64 *symbol, t_fdata *data);
void *get_section_64(struct mach_header_64 *file, void *get_section_64(t_machodata *data, char *lookup);
char *lookup);
void mach_64_parse(t_machodata *data); void mach_64_parse(t_machodata *data);
void nm_mach_64(struct mach_header_64 *file, void nm_mach_64(t_fdata *data);
t_nmdata *data); void otool_mach_64(t_fdata *data);
void otool_mach_64(void *file, t_otooldata *data);
#endif #endif

View file

@ -6,13 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 11:38:10 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 #ifndef PPC_H
# define PPC_H # define PPC_H
void otool_ppc(void *file, t_otooldata *data); void otool_ppc(t_fdata *data);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/07 17:56:14 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -22,12 +22,12 @@ make
#bibliotheques dynamiques #bibliotheques dynamiques
./scripts/nmdiff.sh /usr/lib/libcompression.dylib ./scripts/nmdiff.sh /usr/lib/libcompression.dylib
#biblioteque universelle #biblioteque universelle
./scripts/nmdiff.sh /usr/bin/audiodevice ./scripts/nmdiff.sh /usr/bin/audiodevice
./scripts/nmdiff.sh /usr/bin/python ./scripts/nmdiff.sh /usr/bin/python
########## ##########
# OTOOL # OTOOL
########## ##########

44
nm-otool/scripts/mdissect.sh Executable file
View file

@ -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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 15:18:54 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,27 +6,28 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 16:31:48 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" #include "ft_nm_otool.h"
void *fat_extract(struct fat_header *fat, void *fat_extract(struct fat_header *fat, char *aname)
cpu_type_t cputype,
cpu_subtype_t cpusubtype)
{ {
uint32_t narch; uint32_t narch;
struct fat_arch *arch; struct fat_arch *obj;
const NXArchInfo *arch;
narch = endian(fat->nfat_arch, 32); narch = endian(fat->nfat_arch, 32);
arch = (struct fat_arch*)(fat + 1); obj = (struct fat_arch*)(fat + 1);
while (narch--) while (narch--)
{ {
if ((int32_t)endian(arch->cputype, 32) == cputype arch = NXGetArchInfoFromCpuType(
&& (int32_t)endian(arch->cpusubtype, 32) & cpusubtype) endian(obj->cputype, 32),
return ((void *)fat + endian(arch->offset, 32)); endian(obj->cpusubtype, 32));
++arch; if (ft_strequ(aname, arch->name))
return ((void *)fat + endian(obj->offset, 32));
++obj;
} }
return (NULL); return (NULL);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 03:09:12 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}, {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; uint32_t narch;
struct fat_arch *obj; struct fat_arch *obj;
const NXArchInfo *arch; const NXArchInfo *arch;
g_rev = IS_REV(fat); 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); nm_single_file(obj, data);
else 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; const NXArchInfo *arch;
data->file = file;
g_rev = IS_REV(file); g_rev = IS_REV(file);
if (IS_MACH_32(file)) if (IS_MACH_32(file))
nm_mach(file, data); nm_mach(data);
else if (IS_MACH_64(file)) else if (IS_MACH_64(file))
nm_mach_64(file, data); nm_mach_64(data);
else else
{ {
arch = NXGetArchInfoFromCpuType( 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; int i;
struct stat buf; struct stat buf;
int fd; int fd;
void *file;
i = data.av_data - av; i = data.av_data - av;
while (i < ac && (data.filename = av[i])) while (i < ac && (data.filename = av[i]))
{ {
if ((fd = open(data.filename, O_RDONLY)) < 0 || fstat(fd, &buf) < 0) if ((fd = open(data.filename, O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (1); 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) == MAP_FAILED)
return (1); return (1);
data.eof = data.file + buf.st_size;
if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1)
ft_printf("\n%s:\n", data.filename); ft_printf("\n%s:\n", data.filename);
if (IS_FAT(file)) if (IS_FAT(data.file))
nm_fat_file(file, &data); nm_fat_file(data.file, &data);
else else
nm_single_file(file, &data); nm_single_file(data.file, &data);
if (munmap(file, buf.st_size)) if (munmap(data.file, buf.st_size))
return (1); return (1);
i++; i++;
} }
@ -111,7 +112,7 @@ int nm(int ac, char **av, t_nmdata data)
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_nmdata data; t_fdata data;
data.flag = NM_ASORT; data.flag = NM_ASORT;
if (cliopts_get(av, g_nm_opts, &data)) if (cliopts_get(av, g_nm_opts, &data))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:08:14 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; 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; uint32_t narch;
struct fat_arch *obj; struct fat_arch *obj;
const NXArchInfo *arch; const NXArchInfo *arch;
g_rev = IS_REV(fat); 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); ft_printf("%s:\n", data->filename);
otool_single_file(obj, data); 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; const NXArchInfo *arch;
data->file = file;
g_rev = IS_REV(file); g_rev = IS_REV(file);
arch = NXGetArchInfoFromCpuType( arch = NXGetArchInfoFromCpuType(
endian(*((int32_t*)file + 1), 32), endian(*((int32_t*)file + 1), 32),
@ -60,11 +61,11 @@ void otool_single_file(void *file, t_otooldata *data)
if (arch) if (arch)
{ {
if (ft_strcmp(arch->name, "ppc") == 0) if (ft_strcmp(arch->name, "ppc") == 0)
otool_ppc(file, data); otool_ppc(data);
else if (IS_MACH_32(file)) else if (IS_MACH_32(file))
otool_mach(file, data); otool_mach(data);
else if (IS_MACH_64(file)) else if (IS_MACH_64(file))
otool_mach_64(file, data); otool_mach_64(data);
else else
ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name); 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)); 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; int i;
struct stat buf; struct stat buf;
int fd; int fd;
void *file;
i = data.av_data - av; i = data.av_data - av;
while (i < ac && (data.filename = av[i])) while (i < ac && (data.filename = av[i]))
{ {
if ((fd = open((av[i]), O_RDONLY)) < 0 || fstat(fd, &buf) < 0) if ((fd = open((av[i]), O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (1); 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) == MAP_FAILED)
return (1); return (1);
if (IS_FAT(file)) data.eof = data.file + buf.st_size;
otool_fat_file(file, &data); if (IS_FAT(data.file))
otool_fat_file(data.file, &data);
else else
{ {
ft_printf("%s:\n", data.filename); 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); return (1);
i++; i++;
} }
@ -103,7 +104,7 @@ int otool(int ac, char **av, t_otooldata data)
int main(int ac, char **av) int main(int ac, char **av)
{ {
t_otooldata data; t_fdata data;
if (cliopts_get(av, g_otool_opts, &data)) if (cliopts_get(av, g_otool_opts, &data))
{ {

View file

@ -6,39 +6,36 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:00:40 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" #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; uint32_t i;
struct load_command *lc; struct load_command *lc;
struct segment_command *seg; struct segment_command *seg;
uint32_t ncmds; uint32_t ncmds;
lc = (void*)(file + 1); MC(lc = (void*)((struct mach_header*)data->file->file + 1));
i = -1; i = -1;
ncmds = endian(file->ncmds, 32); ncmds = endian(((struct mach_header*)data->file->file)->ncmds, 32);
DG("ncmds=%i", ncmds);
while (++i < ncmds) while (++i < ncmds)
{ {
DG("i=%i", i);
if (endian(lc->cmd, 32) & LC_SEGMENT) if (endian(lc->cmd, 32) & LC_SEGMENT)
{ {
seg = (struct segment_command*)lc; MC(seg = (struct segment_command*)lc);
DG("segment: %s", seg->segname);
if (ft_strcmp(seg->segname, lookup) == 0) if (ft_strcmp(seg->segname, lookup) == 0)
return (seg); return (seg);
} }
lc = (void*)lc + endian(lc->cmdsize, 32); MC(lc = (void*)lc + endian(lc->cmdsize, 32));
} }
return (NULL); return (NULL);
} }
void *get_section(struct mach_header *file, char *lookup) void *get_section(t_machodata *data, char *lookup)
{ {
uint32_t i; uint32_t i;
uint32_t j; uint32_t j;
@ -46,23 +43,23 @@ void *get_section(struct mach_header *file, char *lookup)
struct segment_command *seg; struct segment_command *seg;
struct section *sect; struct section *sect;
lc = (void*)(file + 1); MC(lc = (void*)((struct mach_header*)data->file->file + 1));
i = -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) if (endian(lc->cmd, 32) & LC_SEGMENT)
{ {
seg = (struct segment_command*)lc; MC(seg = (struct segment_command*)lc);
sect = (void*)(seg + 1); MC(sect = (void*)(seg + 1));
j = -1; j = -1;
while (++j < endian(seg->nsects, 32)) while (++j < endian(seg->nsects, 32))
{ {
if (ft_strcmp(sect->sectname, lookup) == 0) if (ft_strcmp(sect->sectname, lookup) == 0)
return (sect); 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); return (NULL);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/23 16:06:44 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; t_symbol symbol;
char *stringtable; char *stringtable;
struct nlist *array; struct nlist *array;
int nsyms;
data->symtab = symtab; data->symtab = symtab;
stringtable = data->file + endian(symtab->stroff, 32); MC(stringtable = data->file->file + endian(symtab->stroff, 32));
array = (struct nlist*)(data->file + endian(symtab->symoff, 32)); MC(array = (struct nlist*)(data->file->file + endian(symtab->symoff, 32)));
nsyms = endian(symtab->nsyms, 32);
i = -1; i = -1;
while (++i < (int)endian(symtab->nsyms, 32)) while (++i < nsyms)
{ {
symbol_init(&symbol, stringtable, array, i); symbol_init(&symbol, stringtable, array, i);
symbol_set(&symbol, data); symbol_set(&symbol, data);
@ -38,12 +40,12 @@ static void seg_parse(t_machodata *data, struct segment_command *seg)
struct section *sect; struct section *sect;
nsects = endian(seg->nsects, 32); nsects = endian(seg->nsects, 32);
sect = (void*)(seg + 1); MC(sect = (void*)(seg + 1));
i = -1; i = -1;
while (++i < nsects) while (++i < nsects)
{ {
ft_lsteadd(&data->sects, ft_lstnew(&sect, sizeof(sect))); ft_lsteadd(&data->sects, ft_lstnew(&sect, sizeof(sect)));
sect = sect + 1; MC(sect = sect + 1);
} }
} }
@ -54,9 +56,9 @@ void mach_parse(t_machodata *data)
struct load_command *lc; struct load_command *lc;
struct mach_header *header; struct mach_header *header;
header = data->file; header = data->file->file;
ncmds = endian(header->ncmds, 32); ncmds = endian(header->ncmds, 32);
lc = (void*)(header + 1); MC(lc = (void*)(header + 1));
i = -1; i = -1;
while (++i < ncmds) while (++i < ncmds)
{ {
@ -64,6 +66,19 @@ void mach_parse(t_machodata *data)
symtab_parse(data, (struct symtab_command*)lc); symtab_parse(data, (struct symtab_command*)lc);
else if (endian(lc->cmd, 32) == LC_SEGMENT) else if (endian(lc->cmd, 32) == LC_SEGMENT)
seg_parse(data, (struct segment_command*)lc); 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:04:06 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; extern int g_rev;
void otool_mach(void *file, t_otooldata *data) void otool_mach(t_fdata *data)
{ {
struct section *sect; struct section *sect;
t_machodata mach;
mach.file = data;
if (data->flag & OTOOL_TEXT) 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); 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); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }
if (data->flag & OTOOL_DATA) 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); 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); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:31:27 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 12:33:27 by jhalford #+# #+# */ /* Created: 2017/11/01 12:37:07 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:33:27 by jhalford ### ########.fr */ /* 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) void symbol_format_full(t_symbol *symbol)
{ {
ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x", ft_printf(" %02i(%s)\t %04x %03b|%b|%x|%b \t",
symbol->pos, symbol->nlist.n_sect, symbol->section->sectname,
symbol->nlist.n_desc,
(symbol->nlist.n_type & N_STAB) >> 5, (symbol->nlist.n_type & N_STAB) >> 5,
(symbol->nlist.n_type & N_PEXT) >> 4, (symbol->nlist.n_type & N_PEXT) >> 4,
symbol->nlist.n_type & N_TYPE, symbol->nlist.n_type & N_TYPE,
symbol->nlist.n_type & N_EXT, symbol->nlist.n_type & N_EXT);
symbol->nlist.n_sect, symbol->section->sectname,
symbol->nlist.n_desc);
} }
void symbol_format_dfl(t_symbol *symbol) void symbol_format_dfl(t_symbol *symbol)
@ -78,16 +77,19 @@ void symbol_format_dfl(t_symbol *symbol)
symbol->nlist.n_type); 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) if (data->flag & NM_OFORMAT)
ft_printf("%s: ", data->filename); ft_printf("%s: ", data->filename);
if (!(data->flag & NM_ONLY_UNDF)) if (!(data->flag & NM_ONLY_UNDF))
{ {
if (symbol->type == SYM_UNDF) if (symbol->type == SYM_UNDF)
ft_printf("%8s", " "); ft_printf("%*s", nbyte, " ");
else else
ft_printf("%08llx", symbol->nlist.n_value); ft_printf("%0*llx", nbyte, symbol->nlist.n_value);
if (data->flag & NM_MFORMAT) if (data->flag & NM_MFORMAT)
symbol_format_m(symbol); symbol_format_m(symbol);
else if (data->flag & NM_FULL) else if (data->flag & NM_FULL)
@ -95,6 +97,8 @@ int symbol_format(t_symbol *symbol, t_nmdata *data)
else else
symbol_format_dfl(symbol); symbol_format_dfl(symbol);
} }
ft_printf(" %s\n", symbol->string); ft_putchar(' ');
ft_putstr(symbol->string);
ft_putchar('\n');
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 17:06:23 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:07:28 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, int symbol_init(t_symbol *symbol,
char *stringtable, struct nlist *array, int i) char *stringtable, struct nlist *array, int i)
{ {
size_t nvaluesize;
symbol->type = 0; symbol->type = 0;
symbol->pos = i; symbol->pos = i;
symbol->nlist.n_type = endian(array[i].n_type, 8); symbol->nlist.n_type = endian(array[i].n_type, 8);
symbol->nlist.n_sect = endian(array[i].n_sect, 8); symbol->nlist.n_sect = endian(array[i].n_sect, 8);
symbol->nlist.n_desc = endian(array[i].n_desc, 16); 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); symbol->string = stringtable + endian(array[i].n_un.n_strx, 32);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 20:33:02 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) static int sym_nsort(t_symbol *sym1, t_symbol *sym2)
{ {
if (sym1->nlist.n_value == sym2->nlist.n_value) if (sym1->nlist.n_value == sym2->nlist.n_value)
return (sym_asort(sym1, sym2)); return (ft_strcmp(sym1->string, sym2->string));
return (sym1->nlist.n_value > sym2->nlist.n_value ? 1 : -1); return (sym1->nlist.n_value - sym2->nlist.n_value);
} }
int symbol_sort(t_list **syms, t_flag flag) int symbol_sort(t_list **syms, t_flag flag)

View file

@ -6,39 +6,36 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:00:40 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" #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; uint32_t i;
struct load_command *lc; struct load_command *lc;
struct segment_command_64 *seg; struct segment_command_64 *seg;
uint32_t ncmds; uint32_t ncmds;
lc = (void*)(file + 1); MC(lc = (void*)((struct mach_header_64*)data->file->file + 1));
i = -1; i = -1;
ncmds = endian(file->ncmds, 32); ncmds = endian(((struct mach_header_64*)data->file->file)->ncmds, 32);
DG("ncmds=%i", ncmds);
while (++i < ncmds) while (++i < ncmds)
{ {
DG("i=%i", i);
if (endian(lc->cmd, 32) & LC_SEGMENT_64) if (endian(lc->cmd, 32) & LC_SEGMENT_64)
{ {
seg = (struct segment_command_64*)lc; MC(seg = (struct segment_command_64*)lc);
DG("segment: %s", seg->segname);
if (ft_strcmp(seg->segname, lookup) == 0) if (ft_strcmp(seg->segname, lookup) == 0)
return (seg); return (seg);
} }
lc = (void*)lc + endian(lc->cmdsize, 32); MC(lc = (void*)lc + endian(lc->cmdsize, 32));
} }
return (NULL); 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 i;
uint32_t j; uint32_t j;
@ -46,23 +43,23 @@ void *get_section_64(struct mach_header_64 *file, char *lookup)
struct segment_command_64 *seg; struct segment_command_64 *seg;
struct section_64 *sect; struct section_64 *sect;
lc = (void*)(file + 1); MC(lc = (void*)((struct mach_header_64*)data->file->file + 1));
i = -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) if (endian(lc->cmd, 32) & LC_SEGMENT_64)
{ {
seg = (struct segment_command_64*)lc; MC(seg = (struct segment_command_64*)lc);
sect = (void*)(seg + 1); MC(sect = (void*)(seg + 1));
j = -1; j = -1;
while (++j < endian(seg->nsects, 32)) while (++j < endian(seg->nsects, 32))
{ {
if (ft_strcmp(sect->sectname, lookup) == 0) if (ft_strcmp(sect->sectname, lookup) == 0)
return (sect); 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); return (NULL);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/23 16:06:44 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; t_symbol_64 symbol;
char *stringtable; char *stringtable;
struct nlist_64 *array; struct nlist_64 *array;
int nsyms;
data->symtab = symtab; data->symtab = symtab;
stringtable = data->file + endian(symtab->stroff, 32); MC(stringtable = data->file->file + endian(symtab->stroff, 32));
array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32)); MC(array = (struct nlist_64*)(data->file->file + endian(symtab->symoff, 32)));
nsyms = endian(symtab->nsyms, 32);
i = -1; i = -1;
while (++i < (int)endian(symtab->nsyms, 32)) while (++i < nsyms)
{ {
symbol_64_init(&symbol, stringtable, array, i); symbol_64_init(&symbol, stringtable, array, i);
symbol_64_set(&symbol, data); 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; struct section_64 *sect;
nsects = endian(seg->nsects, 32); nsects = endian(seg->nsects, 32);
sect = (void*)(seg + 1); MC(sect = (void*)(seg + 1));
i = -1; i = -1;
while (++i < nsects) while (++i < nsects)
{ {
ft_lsteadd(&data->sects, ft_lstnew(&sect, sizeof(sect))); ft_lsteadd(&data->sects, ft_lstnew(&sect, 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 load_command *lc;
struct mach_header_64 *header; struct mach_header_64 *header;
header = data->file; header = data->file->file;
ncmds = endian(header->ncmds, 32); ncmds = endian(header->ncmds, 32);
lc = (void*)(header + 1); MC(lc = (void*)(header + 1));
i = -1; i = -1;
while (++i < ncmds) while (++i < ncmds)
{ {
@ -64,6 +66,19 @@ void mach_64_parse(t_machodata *data)
symtab_64_parse(data, (struct symtab_command*)lc); symtab_64_parse(data, (struct symtab_command*)lc);
else if (endian(lc->cmd, 32) == LC_SEGMENT_64) else if (endian(lc->cmd, 32) == LC_SEGMENT_64)
seg_64_parse(data, (struct segment_command_64*)lc); 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);
}

View file

@ -1,26 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nm_mach_64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:04:06 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; extern int g_rev;
void otool_mach_64(void *file, t_otooldata *data) void otool_mach_64(t_fdata *data)
{ {
struct section_64 *sect; struct section_64 *sect;
t_machodata mach;
mach.file = data;
if (data->flag & OTOOL_TEXT) 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); 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); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }
if (data->flag & OTOOL_DATA) 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); 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); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 12:37:07 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); 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) if (data->flag & NM_OFORMAT)
ft_printf("%s: ", data->filename); ft_printf("%s: ", data->filename);
if (!(data->flag & NM_ONLY_UNDF)) if (!(data->flag & NM_ONLY_UNDF))
{ {
if (symbol->type == SYM_UNDF) if (symbol->type == SYM_UNDF)
ft_printf("%16s", " "); ft_printf("%*s", nbyte, " ");
else else
ft_printf("%016llx", symbol->nlist.n_value); ft_printf("%0*llx", nbyte, symbol->nlist.n_value);
if (data->flag & NM_MFORMAT) if (data->flag & NM_MFORMAT)
symbol_64_format_m(symbol); symbol_64_format_m(symbol);
else if (data->flag & NM_FULL) else if (data->flag & NM_FULL)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:07:28 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, int symbol_64_init(t_symbol_64 *symbol,
char *stringtable, struct nlist_64 *array, int i) char *stringtable, struct nlist_64 *array, int i)
{ {
size_t nvaluesize;
symbol->type = 0; symbol->type = 0;
symbol->pos = i; symbol->pos = i;
symbol->nlist.n_type = endian(array[i].n_type, 8); symbol->nlist.n_type = endian(array[i].n_type, 8);
symbol->nlist.n_sect = endian(array[i].n_sect, 8); symbol->nlist.n_sect = endian(array[i].n_sect, 8);
symbol->nlist.n_desc = endian(array[i].n_desc, 16); 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); symbol->string = stringtable + endian(array[i].n_un.n_strx, 32);
return (0); return (0);
} }

View file

@ -1,26 +1,24 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* nm_mach.c :+: :+: :+: */ /* memcheck.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */ /* Created: 2017/11/07 11:24:09 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:10:08 by jhalford ### ########.fr */ /* Updated: 2017/11/07 15:19:34 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #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; if (ptr <= file->file || (ptr + size) >= file->eof)
{
mach.sects = NULL; ft_dprintf(2, "%s:%i ", function, line);
mach.symbols = NULL; ft_dprintf(2, "%s: is corrupted\n", file->filename);
mach.file = file; ft_dprintf(2, "%p - %p (%zu) - %p --> %zu after end\n", file->file, ptr, size, file->eof, ptr - file->eof);
mach_parse(&mach); exit(1);
symbol_sort(&mach.symbols, data->flag); }
symbol_filter(&mach.symbols, data->flag);
ft_lstiter(mach.symbols, symbol_format, data);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 11:32:36 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; extern int g_rev;
void otool_ppc(void *file, t_otooldata *data) void otool_ppc(t_fdata *data)
{ {
struct section *sect; struct section *sect;
t_machodata mach;
mach.file = data;
if (data->flag & OTOOL_TEXT) 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); 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); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }
if (data->flag & OTOOL_DATA) 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); 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); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }